spacetelescope / stdatamodels

https://stdatamodels.readthedocs.io
Other
5 stars 24 forks source link

stdatamodels treatment of units #240

Open braingram opened 9 months ago

braingram commented 9 months ago

FITS BinTable extensions support using TUNIT keywords to define units for columns within the table. The use of these keywords can be abstracted using interfaces like FITS_rec which provide access to these units via the columns attribute (a ColDefs instance).

Units are used in jwst code and what follows are a few examples (and by no means an exhaustive list):

1) Extract1D uses the FITS_rec interface to assign units to columns in a table. 1) the miri pathloss schema assigns units to the TUNIT headers directly 1) the miri pathloss reference file contains TUNIT headers for the PATHLOSS BinTable 1) the mastargacq.schema also contains 'unit' entries in the datatype which appear to do nothing 1) the niswfss_apcorr schema defines the column unit separate from the table dtype (and doesn't use TUNIT)

There are some considerations when examining how the pipeline uses units:

compatibility with fits and asdf formats

As datamodels should be saveable in both fits and ASDF formats the use of TUNIT for saving a unit has some issues.

The last option above (the niswfss_apcorr example) should work for both ASDF and FITS files (in the context of the jwst pipeline). However, opening the table directly in astropy (or some other FITS supporting program) will fail to associate units with the table columns as they are not using the standard TUNIT keyword(s) (in this case using SIZEUNIT).

attribute interface and model state

Depending on the state of the attribute that contains the table, stdatamodels doesn't appear to provide a consistent interface to the table or units. Some initial testing shows:

braingram commented 9 months ago

@jemorrison

braingram commented 9 months ago

I opened a test PR with a modified SpecModel schema to add dynamic units to the spec_table. https://github.com/spacetelescope/stdatamodels/pull/243

This works by:

A test was added to:

This strategy is only necessary for dynamic units (or units where we expect the user might change the unit outside the pipeline). For static units, defining them in the schema is much simpler.

Aside from the changes in #243 the test PR has only test and schema changes (no code changes appear to be necessary to make this strategy work however it might be nice to investigate how to avoid needing to define the unit attribute after the table to allow the schemas to be a bit more flexible).

braingram commented 1 month ago

XREF: https://github.com/spacetelescope/jwst/issues/2869