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

XraySpectrum1D class with loaders #427

Open
wants to merge 94 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
c2e4cbc
First pass at XraySpectrum1D
eblur May 1, 2018
20c8b28
Can load an X-ray spectrum
eblur May 1, 2018
6ac1c08
Added attribute
eblur May 1, 2018
4571350
Added ARF and RMF objects
eblur May 2, 2018
a92bd09
Added assign_rmf and assign_arf methods
eblur May 2, 2018
7077dc7
Added exposure input
eblur May 2, 2018
9c8159d
Fixed bugs
eblur May 2, 2018
fb5f1b6
Updated docstrings
eblur May 2, 2018
a08d8ae
Store bin and energy units as astropy.units.Unit
eblur May 2, 2018
20ee61d
Added bin_lo and hi because wcs.bin_edges did not exist
eblur May 2, 2018
6cd49f0
Added apply_resp method
eblur May 2, 2018
9c08caa
Adjust apply_resp output if spectrum is stored by wavelength
eblur May 2, 2018
35d0932
Don't need bin_unit
eblur May 2, 2018
da39e47
Because pha-file wavelength is in reverse order, the output from appl…
eblur May 2, 2018
c5db843
added save_model_counts option
eblur May 2, 2018
291be38
Changed my mind on model_counts thing for now
eblur May 3, 2018
fcd9e25
Hard coded in a rest_value
eblur May 23, 2018
9349654
Merge branch 'master' of github.com:astropy/specutils into xrayspectrum
eblur May 23, 2018
fe8c1b5
Added unit to rest_value
eblur May 23, 2018
1c7f27d
Commented out distracting print statements
eblur May 23, 2018
c6f5aaa
Wrote tests
eblur May 24, 2018
c192935
Added docstrings
eblur May 24, 2018
3314f7d
Removed scipy dependence
eblur May 24, 2018
801e3b6
Spelled out acronyms, link to edu materials
Jun 1, 2018
4dff316
Added option to specify RMF FITS extension for response matrix
Jun 1, 2018
cdf3373
corrected typos
Jun 1, 2018
2e4724c
Removing superfluous returns and pass
Jun 1, 2018
a8a8245
Catch ValueError
Jun 1, 2018
5d4d52c
Spelled out ARF and RMF class names
Jun 1, 2018
a297c45
updated docstring
Jun 1, 2018
86f4a3b
Updated tests
Jun 1, 2018
769c923
initialize attributes to None before loading from file
Jun 12, 2018
a89af6b
save filename attribute for attempting load function
Jun 12, 2018
e27a0cd
Merge branch 'master' of github.com:astropy/specutils into xrayspectrum
Jun 12, 2018
7cf979f
Merge branch 'master' of github.com:eblur/specutils into xrayspectrum
eblur Dec 6, 2018
ccad80e
Merge branch 'xrayspectrum' of github.com:eblur/specutils into xraysp…
eblur Dec 6, 2018
dfb9475
First pass at XraySpectrum1D
eblur May 1, 2018
2fa2142
Can load an X-ray spectrum
eblur May 1, 2018
0f37ccd
Added attribute
eblur May 1, 2018
56612ca
Added ARF and RMF objects
eblur May 2, 2018
b7303e7
Added assign_rmf and assign_arf methods
eblur May 2, 2018
003b636
Added exposure input
eblur May 2, 2018
ce4349e
Fixed bugs
eblur May 2, 2018
753ab5e
Updated docstrings
eblur May 2, 2018
4f195e1
Store bin and energy units as astropy.units.Unit
eblur May 2, 2018
67493c5
Added bin_lo and hi because wcs.bin_edges did not exist
eblur May 2, 2018
7974939
Added apply_resp method
eblur May 2, 2018
7f54eae
Adjust apply_resp output if spectrum is stored by wavelength
eblur May 2, 2018
5eded3d
Don't need bin_unit
eblur May 2, 2018
6705068
Because pha-file wavelength is in reverse order, the output from appl…
eblur May 2, 2018
80d559d
added save_model_counts option
eblur May 2, 2018
73535d9
Changed my mind on model_counts thing for now
eblur May 3, 2018
e9be109
Hard coded in a rest_value
eblur May 23, 2018
baa613c
Added unit to rest_value
eblur May 23, 2018
56b4c1a
Commented out distracting print statements
eblur May 23, 2018
afb6333
Wrote tests
eblur May 24, 2018
a15b101
Added docstrings
eblur May 24, 2018
4ba7b58
Removed scipy dependence
eblur May 24, 2018
f5a996a
Spelled out acronyms, link to edu materials
Jun 1, 2018
eed588c
Added option to specify RMF FITS extension for response matrix
Jun 1, 2018
0fa3718
corrected typos
Jun 1, 2018
aa45811
Removing superfluous returns and pass
Jun 1, 2018
674ae64
Catch ValueError
Jun 1, 2018
029f8ab
Spelled out ARF and RMF class names
Jun 1, 2018
2590b7a
updated docstring
Jun 1, 2018
f6b66f5
Updated tests
Jun 1, 2018
4111295
initialize attributes to None before loading from file
Jun 12, 2018
8a1aa62
save filename attribute for attempting load function
Jun 12, 2018
4afde8f
[skip ci] provide xrayspectrum1d
eblur Dec 6, 2018
5ec1428
Merge branch 'xrayspectrum' of github.com:eblur/specutils into xraysp…
eblur Dec 6, 2018
90f8ad6
specutils flux input needs units
eblur Dec 6, 2018
fa80d24
attempting to fix docstrings
eblur Dec 6, 2018
f14e9af
further docstring fixes for sphinx
eblur Dec 6, 2018
eecce75
added extension keyword to _load_arf
eblur Dec 7, 2018
bdc5a41
updated init and load calls with extension keyword
eblur Dec 7, 2018
3fef5b9
XraySpectrum1d uses astropy.Quantity for all inputs
eblur Dec 7, 2018
8b4b945
tested xrayspectrum1d with quantity inputs
eblur Dec 7, 2018
fd052cd
first pass at hetg loader
eblur Dec 7, 2018
7cc6af6
fixed typo
eblur Dec 7, 2018
ee33c64
it runs in my test notebook
eblur Dec 7, 2018
227520d
updated docstrings
eblur Dec 7, 2018
66173d6
added file extensions to the list
eblur Dec 7, 2018
0e814f3
rewrote to use astropy.Quantity at all times
eblur Dec 7, 2018
1edf177
Moved arf loader to an outside function
eblur Dec 26, 2018
fe052a0
now possible to make custom RMF
eblur Dec 27, 2018
fd3455c
docstring reflects changes to RMF init
eblur Dec 27, 2018
0d9ce50
Added unit to arf exposure time
eblur Dec 27, 2018
9d52562
updated docstring
eblur Dec 27, 2018
c760cf4
added basic tests for a fake arf
eblur Dec 27, 2018
2929b37
made static read method for ARF and RMF
eblur Dec 28, 2018
462c7fb
switch back to ARF and RMF convention
eblur Dec 28, 2018
b00601c
dont need os module
eblur Jan 17, 2019
f137c02
fixed calls to ARF and RMF
eblur Jan 18, 2019
4225836
removed rest value keyword
eblur Jan 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions specutils/io/default_loaders/chandra_hetg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from astropy.io import fits
from astropy.units import Unit

from specutils.io.registers import data_loader
from specutils import XraySpectrum1D

__all__ = ['chandra_hetg_identify', 'hetg_spectrum_loader']

def chandra_hetg_identify(origin, *args, **kwargs):
"""Check whether given file contains Chandra HETG spectral data."""
with fits.open(args[0]) as hdu:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail catastrophically if the file it's called on it not a fits file, but e.g. a txt file. Yes, Chandra files will always be fits file, but the "identify" function should work on any file to identify if this is a Chandra file. See discussion in #404

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, what if I have a txt file that for some weird reason ends on "fit", let's say "best_model.fit"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write me a solution for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I think https://github.com/astropy/specutils/blob/master/specutils/io/default_loaders/hst_cos.py#L13 would work. Essentially, that just wraps your check on HDU keywords in the try-except block, which will take care of non-fits files.

However, I now wonder, why you you want to identify HETG spectra? Since ARF, RMF and PHA (TYPE I or II) are all OGIP standardized, would it not make more sense to have an "identify OGIP complient spectrum" identifier and then get all X-ray spectra (XMM, Chandra, ACIS, HETG, SUSAku,...) for the price of one?

# Check that it is a Chandra spectrum file
if hdu[0].header['TELESCOP'] == 'CHANDRA' and 'SPECTRUM' in [h.name for h in hdu]:
# Check that it's an HETG file
if hdu['SPECTRUM'].header['GRATING'] == 'HETG':
return True
return False

@data_loader(label="chandra_hetg", identifier=chandra_hetg_identify, extensions=['pha','pha2', 'PHA', 'PHA2', 'FITS', 'FIT', 'fits', 'fit'])
def hetg_spectrum_loader(file_name, arf=None, rmf=None):
"""
Load Chandra HETG spectral data from a file into a spectrum object.

Parameters
----------
file_name: str
The path to the FITS file

arf : str OR AreaResponse
Filename for the area response file (ARF) or a pre-loaded AreaResponse object

rmf : str OR ResponseMatrix
Filename for the response matrix file (RMF) or a pre-loaded ResponseMatrix object

Returns
-------
data: XraySpectrum1D
The spectrum that is represented by the data in this table.
"""

with fits.open(file_name) as hdu:
header = hdu[0].header
meta = {'header': header}
data = hdu[1].data

bin_unit = Unit(data.columns['BIN_LO'].unit)
bin_lo = data['BIN_LO'] * bin_unit
bin_hi = data['BIN_HI'] * bin_unit

counts = data['COUNTS'] * Unit('ct')
exposure = hdu[1].header['EXPOSURE'] * Unit('second')

return XraySpectrum1D(bin_lo, bin_hi, counts,
exposure=exposure, arf=arf, rmf=rmf)
2 changes: 2 additions & 0 deletions specutils/spectra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
from .spectral_region import * # noqa
from .spectrum_collection import * #noqa
from .spectrum_list import * #noqa

from .xrayspectrum1d import *