question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unify FITS read classmethods

See original GitHub issue

We should unify the FITS read classmethods in Gammapy.

You can browse what we currently have here: https://github.com/gammapy/gammapy/search?utf8=✓&q="def+read(cls"&type=Code

This PR is triggered by https://github.com/gammapy/gammapy/pull/537#issuecomment-222380381, where for SkyMap @adonath so far has been calling astropy.io.fits.getdata (see here for other places where it’s called), while in many other places we’ve been calling astropy.io.fits.open and in addition accepted a hdu argument that was then used to select the HDU. This is also what others have been doing, e.g. Table.read in Astropy or spectral_cube.SpectralCube.read accept the hdu keyword and not the ext keyword that getdata takes. This is confusing for users if read methods behave differently for different classes in Gammapy.

I think I like this API and calling astropy.io.fits.open followed by HDU selection best

    def read(cls, filename, format=None, hdu=None, **kwargs):
        """Read from FITS file.

        Parameters
        ----------
        filename : str
            The file to read the cube from
        hdu : int or str
            For FITS files, the HDU to read in (can be the ID or name of an
            HDU).
        kwargs : dict
            Passed to :func:`~astropy.io.fits.open`.
        """

and then an implementation similar to https://github.com/radio-astro-tools/spectral-cube/blob/master/spectral_cube/io/fits.py which mostly does the handling of the hdu=None case to make it convenient if there is only one good HDU in the FITS file for this kind of object (e.g. one 2-dim image), and if there are several, uses the first and emits a warning. This could be mostly contained in one or a few helper functions in gammapy.utils.fits.

For cases where multiple HDUs aaa and bbb are needed (like SkyCube.read I guess we should add hdu_aaa and hdu_bbb keyword arguments to read?

We also have to make a decision on whether we want to accept the HDU as part of the filename in brackets and implement the gammapy.utils.fits.get_hdu helper function or remove it. I think this could be convenient for command line tools.

@OlgaVorokh @adonath @joleroi - Thoughts? (If you don’t like the basic plan of calling astropy.io.fits.open and adding an hdu option, please point out why and ideally propose an alternative.)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
adonathcommented, May 30, 2016

@joleroi Good point, I actually like the option to pass an ImageHDU or BintableHDU to the .read() method as well, instead of having an additional method from_hdu(). It makes a smaller and more intuitive (to me…) API. E.g. Table.read() takes HDU objects as well.

0reactions
cdeilcommented, Apr 5, 2018

I think now this is working properly both for the old SkyImage and the new Map.

Example from above with SkyImage:

>>> from gammapy.image import SkyImage
>>> filename = '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits'
>>> image = SkyImage.read(filename)
>>> image.data.shape
(1800, 3600)
>>> image.wcs
WCS Keywords

Number of WCS axes: 2
CTYPE : 'GLON-CAR'  'GLAT-CAR'  
CRVAL : 0.0  0.0  
CRPIX : 1800.5  900.5  
PC1_1 PC1_2  : 1.0  0.0  
PC2_1 PC2_2  : 0.0  1.0  
CDELT : -0.1  0.1  
NAXIS : 3600  1800

And with Map:

>>> from gammapy.maps import Map
>>> filename = '/Users/deil/code/gammapy-extra/datasets/exclusion_masks/tevcat_exclusion.fits'
>>> m = Map.read(filename)
>>> m.data.shape
(1800, 3600)
>>> m.geom.wcs
WCS Keywords

Number of WCS axes: 2
CTYPE : 'GLON-CAR'  'GLAT-CAR'  
CRVAL : 0.0  0.0  
CRPIX : 1800.5  900.5  
PC1_1 PC1_2  : 1.0  0.0  
PC2_1 PC2_2  : 0.0  1.0  
CDELT : -0.1  0.1  
NAXIS : 3600  1800

Closing this issue now.

I’ll open a PR with improvements to FITS table I/O (that was discussed a bit above) later today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 575 – Unifying function/method classes
A new read-only attribute __parent__ , represented in the C structure as m_parent . If this attribute exists, it represents the defining object....
Read more >
HDF5, YAML, ASDF, Parquet, pickle (astropy.io.misc)
This package contains functions for reading and writing Parquet tables that are not meant to be used directly, but instead are available as...
Read more >
Spectrum1D — specutils v1.9.0 - Read the Docs
This function provides the NDDataBase interface to the astropy unified I/O layer ... from astropy.nddata import CCDData >>> dat = CCDData.read('image.fits').
Read more >
class Image (class methods and instance methods a-d)
Creates one or more images from the image file, omitting the pixel data. Only the attributes are stored in the images. This method...
Read more >
Supercharge Your Classes With Python super()
This is an object that delegates calls to the correct class methods without making an ... You can read more about slant heights...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found