Skip to content
Open
90 changes: 90 additions & 0 deletions cosipy/threeml/custom_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)

import healpy as hp
import hashlib

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -349,3 +350,92 @@ def get_total_spatial_integral(self, z, avg_int=False, nside=None):
intensity_2d /= len(intensity_3d) # return average intensity

return intensity_2d



class SpatialTemplate_2D_Healpix(Function2D, metaclass=FunctionMeta):
r"""
description :
User input Spatial Template using HealpixMap from mhealpy.

latex : $hi$

parameters :
K :
desc : normalization
initial value : 1
fix : yes
hash :
desc: hash of model map
initial value: 1
fix: yes

properties:
fits_file:
desc: fits file name which contain HealpixMap object from mhealpy package
defer: True
function: _load_file

"""

def _set_units(self, x_unit, y_unit, z_unit):

self.K.unit = z_unit

# This is optional, and it is only needed if we need more setup after the
# constructor provided by the meta class

def _load_file(self):

try:
from mhealpy import HealpixMap
except ValueError:
print("You need to install mhealpy with pip for using this class")

self._fitsfile = self.fits_file.value

self._hpmap = HealpixMap.read_map(self._fitsfile)

# test if the map is normalized as expected
area = self._hpmap.pixarea().value

total = np.sum(self._hpmap) * area

if not np.isclose(total, 1, rtol=1e-2):
log.warning("2D template is normalized to {} (expected: 1)".format(total))

# hash sum uniquely identifying the template function (defined by its 2D map
# array and coordinate system) this is needed so that the memoization won't
# confuse different SpatialTemplate_2D objects.
h = hashlib.sha224()
h.update(np.asarray(self._hpmap).tobytes())
self.hash = int(h.hexdigest(), 16)

def evaluate(self, x, y, K, hash):

# X and Y are defined by the frame (ICRS,galactic, etc..)
coord = SkyCoord(x, y, frame=self._hpmap.coordsys, unit="deg")

# transform input coordinates to pixel coordinates;
pix = self._hpmap.ang2pix(coord)

out = self._hpmap[pix]

return np.multiply(K, out)

def get_boundaries(self):

return self._hpmap.boundaries

def get_total_spatial_integral(self, z=None):
"""Returns the total integral (for 2D functions) or the integral over
the spatial components (for 3D functions). needs to be implemented in
subclasses.

:return: an array of values of the integral (same dimension as
z).
"""

if isinstance(z, u.Quantity):
z = z.value
return np.multiply(self.K.value, np.ones_like(z))
22 changes: 22 additions & 0 deletions docs/tutorials/run_tutorials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,25 @@ tutorials:
checksum: 570a0ef083044123a811fb95d803098a


point_source_sensitivity:
notebook:
- spectral_fits/Sensitivity_calculator/PointSource_Sensitivity.ipynb
wasabi_files:
COSI-SMEX/develop/Data/Orientation/DC3_final_530km_3_month_with_slew_15sbins_GalacticEarth_SAA.fits:
checksum: e86df2407eb052cf0c1db4a8e7598727
COSI-SMEX/DC4/Data/Responses/Response26Al.o4.e1805_1812.s10036231691364.m1045.filtered.nonsparse.binnedimaging.imagingresponse.h5:
checksum: 4de7c15f95c62698ff139585f5f5cdd1
COSI-SMEX/DC4/Data/Backgrounds/gal_DC4_bkg_Al26_binned_smoothed_fwhm_10.0deg.hdf5:
checksum: bd6a32f8abee5b989b87149111487faa

extended_source_sensitivity:
notebook:
- spectral_fits/Sensitivity_calculator/ExtendedSource_Sensitivity.ipynb
wasabi_files:
COSI-SMEX/develop/Data/Orientation/DC3_final_530km_3_month_with_slew_15sbins_GalacticEarth_SAA.fits:
checksum: e86df2407eb052cf0c1db4a8e7598727
COSI-SMEX/DC3/Data/Responses/extended_source_response/extended_source_response_Al26_merged.h5.gz:
unzip: True
checksum: 218d088159c6e11d0ddecc03ca9fa399
COSI-SMEX/DC4/Data/Backgrounds/gal_DC4_bkg_Al26_binned_smoothed_fwhm_10.0deg.hdf5:
checksum: bd6a32f8abee5b989b87149111487faa
Loading
Loading