-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
eblur
wants to merge
94
commits into
astropy:main
Choose a base branch
from
eblur:xrayspectrum_loaders
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 20c8b28
Can load an X-ray spectrum
eblur 6ac1c08
Added attribute
eblur 4571350
Added ARF and RMF objects
eblur a92bd09
Added assign_rmf and assign_arf methods
eblur 7077dc7
Added exposure input
eblur 9c8159d
Fixed bugs
eblur fb5f1b6
Updated docstrings
eblur a08d8ae
Store bin and energy units as astropy.units.Unit
eblur 20ee61d
Added bin_lo and hi because wcs.bin_edges did not exist
eblur 6cd49f0
Added apply_resp method
eblur 9c08caa
Adjust apply_resp output if spectrum is stored by wavelength
eblur 35d0932
Don't need bin_unit
eblur da39e47
Because pha-file wavelength is in reverse order, the output from appl…
eblur c5db843
added save_model_counts option
eblur 291be38
Changed my mind on model_counts thing for now
eblur fcd9e25
Hard coded in a rest_value
eblur 9349654
Merge branch 'master' of github.com:astropy/specutils into xrayspectrum
eblur fe8c1b5
Added unit to rest_value
eblur 1c7f27d
Commented out distracting print statements
eblur c6f5aaa
Wrote tests
eblur c192935
Added docstrings
eblur 3314f7d
Removed scipy dependence
eblur 801e3b6
Spelled out acronyms, link to edu materials
4dff316
Added option to specify RMF FITS extension for response matrix
cdf3373
corrected typos
2e4724c
Removing superfluous returns and pass
a8a8245
Catch ValueError
5d4d52c
Spelled out ARF and RMF class names
a297c45
updated docstring
86f4a3b
Updated tests
769c923
initialize attributes to None before loading from file
a89af6b
save filename attribute for attempting load function
e27a0cd
Merge branch 'master' of github.com:astropy/specutils into xrayspectrum
7cf979f
Merge branch 'master' of github.com:eblur/specutils into xrayspectrum
eblur ccad80e
Merge branch 'xrayspectrum' of github.com:eblur/specutils into xraysp…
eblur dfb9475
First pass at XraySpectrum1D
eblur 2fa2142
Can load an X-ray spectrum
eblur 0f37ccd
Added attribute
eblur 56612ca
Added ARF and RMF objects
eblur b7303e7
Added assign_rmf and assign_arf methods
eblur 003b636
Added exposure input
eblur ce4349e
Fixed bugs
eblur 753ab5e
Updated docstrings
eblur 4f195e1
Store bin and energy units as astropy.units.Unit
eblur 67493c5
Added bin_lo and hi because wcs.bin_edges did not exist
eblur 7974939
Added apply_resp method
eblur 7f54eae
Adjust apply_resp output if spectrum is stored by wavelength
eblur 5eded3d
Don't need bin_unit
eblur 6705068
Because pha-file wavelength is in reverse order, the output from appl…
eblur 80d559d
added save_model_counts option
eblur 73535d9
Changed my mind on model_counts thing for now
eblur e9be109
Hard coded in a rest_value
eblur baa613c
Added unit to rest_value
eblur 56b4c1a
Commented out distracting print statements
eblur afb6333
Wrote tests
eblur a15b101
Added docstrings
eblur 4ba7b58
Removed scipy dependence
eblur f5a996a
Spelled out acronyms, link to edu materials
eed588c
Added option to specify RMF FITS extension for response matrix
0fa3718
corrected typos
aa45811
Removing superfluous returns and pass
674ae64
Catch ValueError
029f8ab
Spelled out ARF and RMF class names
2590b7a
updated docstring
f6b66f5
Updated tests
4111295
initialize attributes to None before loading from file
8a1aa62
save filename attribute for attempting load function
4afde8f
[skip ci] provide xrayspectrum1d
eblur 5ec1428
Merge branch 'xrayspectrum' of github.com:eblur/specutils into xraysp…
eblur 90f8ad6
specutils flux input needs units
eblur fa80d24
attempting to fix docstrings
eblur f14e9af
further docstring fixes for sphinx
eblur eecce75
added extension keyword to _load_arf
eblur bdc5a41
updated init and load calls with extension keyword
eblur 3fef5b9
XraySpectrum1d uses astropy.Quantity for all inputs
eblur 8b4b945
tested xrayspectrum1d with quantity inputs
eblur fd052cd
first pass at hetg loader
eblur 7cc6af6
fixed typo
eblur ee33c64
it runs in my test notebook
eblur 227520d
updated docstrings
eblur 66173d6
added file extensions to the list
eblur 0e814f3
rewrote to use astropy.Quantity at all times
eblur 1edf177
Moved arf loader to an outside function
eblur fe052a0
now possible to make custom RMF
eblur fd3455c
docstring reflects changes to RMF init
eblur 0d9ce50
Added unit to arf exposure time
eblur 9d52562
updated docstring
eblur c760cf4
added basic tests for a fake arf
eblur 2929b37
made static read method for ARF and RMF
eblur 462c7fb
switch back to ARF and RMF convention
eblur b00601c
dont need os module
eblur f137c02
fixed calls to ARF and RMF
eblur 4225836
removed rest value keyword
eblur File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?