11import numpy as np
22
33from astropy .io import fits
4- from astropy .nddata import StdDevUncertainty
4+ from astropy .nddata import StdDevUncertainty , Covariance
55from astropy .table import Table
66import astropy .units as u
77from astropy .wcs import WCS
@@ -131,6 +131,7 @@ def tabular_fits_writer(spectrum, file_name, hdu=1, update_header=False, store_d
131131 **kwargs
132132 Additional optional keywords passed to :func:`~astropy.io.fits.HDUList.writeto`.
133133 """
134+ # TODO: `hdu` is not used below. Is this necessary?
134135 if hdu < 1 :
135136 raise ValueError (f'FITS does not support BINTABLE extension in HDU { hdu } .' )
136137
@@ -144,7 +145,7 @@ def tabular_fits_writer(spectrum, file_name, hdu=1, update_header=False, store_d
144145 isinstance (keyword [1 ], hdr_types )])
145146
146147 # Strip header of FITS reserved keywords
147- for keyword in ['NAXIS' , 'NAXIS1' , 'NAXIS2' ]:
148+ for keyword in ['EXTNAME' , ' NAXIS' , 'NAXIS1' , 'NAXIS2' ]:
148149 header .remove (keyword , ignore_missing = True )
149150
150151 # Add dispersion array and unit
@@ -168,20 +169,26 @@ def tabular_fits_writer(spectrum, file_name, hdu=1, update_header=False, store_d
168169 colnames = [dispname , "flux" ]
169170
170171 # Include uncertainty - units to be inferred from spectrum.flux
172+ correl = None
171173 if spectrum .uncertainty is not None :
172- try :
173- unc = (
174- spectrum
175- .uncertainty
176- .represent_as (StdDevUncertainty )
177- .quantity
178- .to (funit , equivalencies = u .spectral_density (disp ))
179- )
180- columns .append (unc .astype (ftype ))
174+ if isinstance (spectrum .uncertainty , Covariance ):
175+ var , correl = spectrum .uncertainty .to_tables ()
176+ columns .append (np .sqrt (var ))
181177 colnames .append ("uncertainty" )
182- except RuntimeWarning :
183- raise ValueError ("Could not convert uncertainty to StdDevUncertainty due"
184- " to divide-by-zero error." )
178+ else :
179+ try :
180+ unc = (
181+ spectrum
182+ .uncertainty
183+ .represent_as (StdDevUncertainty )
184+ .quantity
185+ .to (funit , equivalencies = u .spectral_density (disp ))
186+ )
187+ columns .append (unc .astype (ftype ))
188+ colnames .append ("uncertainty" )
189+ except RuntimeWarning :
190+ raise ValueError ("Could not convert uncertainty to StdDevUncertainty due"
191+ " to divide-by-zero error." )
185192
186193 # Add mask column if present
187194 if spectrum .mask is not None :
@@ -197,22 +204,22 @@ def tabular_fits_writer(spectrum, file_name, hdu=1, update_header=False, store_d
197204 colnames .append ('mask' )
198205
199206 # For > 1D data transpose from row-major format
207+ # TODO: revisit this
200208 for c in range (1 , len (columns )):
201209 if columns [c ].ndim > 1 :
202210 columns [c ] = columns [c ].T
203211
204212 tab = Table (columns , names = colnames )
205213 if store_data_header :
206214 hdu0 = fits .PrimaryHDU ()
207- hdu1 = fits .BinTableHDU (data = tab , header = header )
215+ hdu1 = fits .BinTableHDU (data = tab , header = header , name = 'DATA' )
208216 else :
209217 hdu0 = fits .PrimaryHDU (header = header )
210- hdu1 = fits .BinTableHDU (data = tab )
211-
212- # This will overwrite any 'EXTNAME' previously read from a valid header; should it?
213- hdu1 .header .update (EXTNAME = 'DATA' )
218+ hdu1 = fits .BinTableHDU (data = tab , name = 'DATA' )
214219
215220 hdulist = fits .HDUList ([hdu0 , hdu1 ])
221+ if correl is not None :
222+ hdulist .append (fits .BinTableHDU (data = correl , name = 'CORREL' ))
216223
217224 # TODO: Use output_verify options to check for valid FITS
218225 hdulist .writeto (file_name , ** kwargs )
0 commit comments