Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified Example Notebooks with new ori file #264

Merged
merged 14 commits into from
Nov 14, 2024
Merged
9 changes: 7 additions & 2 deletions cosipy/spacecraftfile/SpacecraftFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ def get_scatt_map(self,
scheme = 'ring',
coordsys = 'galactic',
r_earth = 6378.0,
earth_occ = True
):

"""
Expand All @@ -537,7 +538,10 @@ def get_scatt_map(self,
The coordinate system used in the scatt map (the default is "galactic).
r_earth : float, optional
Earth radius in km (default is 6378 km).

earth_occ : bool, optional
Option to include Earth occultation in scatt map calculation.
Default is True.

Returns
-------
h_ori : cosipy.spacecraftfile.scatt_map.SpacecraftAttitudeMap
Expand Down Expand Up @@ -574,7 +578,8 @@ def get_scatt_map(self,

# Define weights and set to 0 if blocked by Earth:
weight = np.diff(timestamps.gps)*u.s
weight[earth_occ_index[:-1]] = 0
if earth_occ == True:
weight[earth_occ_index[:-1]] = 0

# Fill histogram:
h_ori.fill(x, y, weight = weight)
Expand Down
17 changes: 13 additions & 4 deletions cosipy/threeml/COSILike.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@
attached to them
precomputed_psr_file : str, optional
Full path to precomputed point source response in Galactic coordinates
earth_occ : bool, optional
Option to include Earth occultation in fit (default is True).
"""
def __init__(self, name, dr, data, bkg, sc_orientation,
nuisance_param=None, coordsys=None, precomputed_psr_file=None, **kwargs):
nuisance_param=None, coordsys=None, precomputed_psr_file=None, earth_occ=True, **kwargs):

# create the hash for the nuisance parameters. We have none for now.
self._nuisance_parameters = collections.OrderedDict()
Expand All @@ -78,6 +80,7 @@
self._data = data
self._bkg = bkg
self._sc_orientation = sc_orientation
self.earth_occ = earth_occ

try:
if data.axes["PsiChi"].coordsys.name != bkg.axes["PsiChi"].coordsys.name:
Expand Down Expand Up @@ -194,7 +197,7 @@
dwell_time_map = self._get_dwell_time_map(coord)
self._psr[name] = self._dr.get_point_source_response(exposure_map=dwell_time_map)
elif self._coordsys == 'galactic':
scatt_map = self._get_scatt_map()
scatt_map = self._get_scatt_map(coord)

Check warning on line 200 in cosipy/threeml/COSILike.py

View check run for this annotation

Codecov / codecov/patch

cosipy/threeml/COSILike.py#L200

Added line #L200 was not covered by tests
self._psr[name] = self._dr.get_point_source_response(coord=coord, scatt_map=scatt_map)
else:
raise RuntimeError("Unknown coordinate system")
Expand Down Expand Up @@ -325,16 +328,22 @@

return dwell_time_map

def _get_scatt_map(self):
def _get_scatt_map(self, coord):
"""
Get the spacecraft attitude map of the source in the inertial (spacecraft) frame.

Parameters
----------
coord : astropy.coordinates.SkyCoord
The coordinates of the target object.

Returns
-------
scatt_map : cosipy.spacecraftfile.scatt_map.SpacecraftAttitudeMap
"""

scatt_map = self._sc_orientation.get_scatt_map(nside = self._dr.nside * 2, coordsys = 'galactic')
scatt_map = self._sc_orientation.get_scatt_map(coord, nside = self._dr.nside * 2, \
coordsys = 'galactic', earth_occ = self.earth_occ)

return scatt_map

Expand Down
379 changes: 159 additions & 220 deletions docs/tutorials/spectral_fits/continuum_fit/crab/SpectralFit_Crab.ipynb

Large diffs are not rendered by default.

312 changes: 125 additions & 187 deletions docs/tutorials/spectral_fits/continuum_fit/grb/SpectralFit_GRB.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"This tutotrial also walks through all the steps needed when performing a spectral fit, starting with the unbinned data, i.e. creating the combined data set, and binning the data. \n",
"\n",
"For the first two examples, you will need the following files (available on wasabi):<br>\n",
"**20280301_3_month.ori <br>\n",
"**20280301_3_month_with_orbital_info.ori <br>\n",
"cosmic_photons_3months_unbinned_data.fits.gz <br>\n",
"511_Testing_3months.fits.gz <br>\n",
"SMEXv12.511keV.HEALPixO4.binnedimaging.imagingresponse.nonsparse_nside16.area.h5 <br>\n",
Expand Down Expand Up @@ -94,7 +94,7 @@
"# ori file:\n",
"# wasabi path: COSI-SMEX/DC2/Data/Orientation/20280301_3_month.ori\n",
"# File size: 684 MB\n",
"fetch_wasabi_file('COSI-SMEX/DC2/Data/Orientation/20280301_3_month.ori')"
"fetch_wasabi_file('COSI-SMEX/DC2/Data/Orientation/20280301_3_month_with_orbital_info.ori')"
]
},
{
Expand Down Expand Up @@ -770,7 +770,7 @@
"source": [
"response_file = \"SMEXv12.511keV.HEALPixO4.binnedimaging.imagingresponse.nonsparse_nside16.area.h5\"\n",
"response = FullDetectorResponse.open(response_file)\n",
"ori = SpacecraftFile.parse_from_file(\"20280301_3_month.ori\")\n",
"ori = SpacecraftFile.parse_from_file(\"20280301_3_month_with_orbital_info.ori\")\n",
"psr_file = \"psr_gal_511_DC2.h5\""
]
},
Expand Down Expand Up @@ -3157,7 +3157,7 @@
"# if not previously loaded in example 1, load the response, ori, and psr: \n",
"response_file = \"SMEXv12.511keV.HEALPixO4.binnedimaging.imagingresponse.nonsparse_nside16.area.h5\"\n",
"response = FullDetectorResponse.open(response_file)\n",
"ori = SpacecraftFile.parse_from_file(\"20280301_3_month.ori\")\n",
"ori = SpacecraftFile.parse_from_file(\"20280301_3_month_with_orbital_info.ori\")\n",
"psr_file = \"psr_gal_511_DC2.h5\""
]
},
Expand Down Expand Up @@ -4299,9 +4299,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:COSI]",
"display_name": "Python [conda env:analysis]",
"language": "python",
"name": "conda-env-COSI-py"
"name": "conda-env-analysis-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -4313,7 +4313,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
Empty file removed tests/spectral_fits/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
from threeML import Band, PointSource, Model, JointLikelihood, DataList
from astromodels import Parameter
from astropy.coordinates import SkyCoord

data_path = test_data.path

Expand Down Expand Up @@ -70,3 +71,7 @@ def test_point_source_spectral_fit():
[1.0743623124061388, -1.1000643881813548, -2.299033632814098, 449.99790270666415, 1.0], atol=[0.1, 0.1, 0.1, 1.0, 0.1])

assert np.allclose([cosi.get_log_like()], [337.17196587486285], atol=[1.0])

# Test scatt map method:
coord = SkyCoord(l=184.56*u.deg,b=-5.78*u.deg,frame="galactic")
cosi._get_scatt_map(coord)
Loading