grizli icon indicating copy to clipboard operation
grizli copied to clipboard

Error when generating new multibeam object from a list of saved beam fits files

Open amn3142 opened this issue 7 years ago • 3 comments

Punchline: The error occurs in 1263 of the class imageData in model.py

self.exptime = hdulist[0].header['EXPTIME']

Changing this to

self.exptime = header['EXPTIME']

Fixes the issue. I'm assuming this was just an oversight when editing the code, since everywhere else keywords are accessed in this function it is from the variable 'header'. This 'header' variable is looking at the correct fits extension, not extension 0.

For reference, the exact sequence of commands that led to this issue was:

##anna's program
beams = ogrp.get_beams(id, size = 25)
for beam in beams:
                name = '%s/%s_%s'%(oDir,componentName,i)
                beamFitsNames.append(name)
		beam.write_fits(name)
		i+=1

mb = MultiBeam(beamFitsNames)

Which leads to the following output:

Traceback (most recent call last):
  File "forwardModel3_0810_dcont_dsky_1bw_griz.py", line 52, in <module>
    mb = MultiBeam(beamFitsNames)
  File "build/bdist.linux-x86_64/egg/grizli/multifit.py", line 985, in __init__
  File "build/bdist.linux-x86_64/egg/grizli/multifit.py", line 1252, in load_beam_fits
  File "build/bdist.linux-x86_64/egg/grizli/model.py", line 3317, in __init__
  File "build/bdist.linux-x86_64/egg/grizli/model.py", line 3446, in load_fits
  File "build/bdist.linux-x86_64/egg/grizli/model.py", line 1262, in __init__
  File "/opt/miniconda2/envs/astroconda/lib/python2.7/site-packages/astropy/io/fits/header.py", line 150, in __getitem__
    card = self._cards[self._cardindex(key)]
  File "/opt/miniconda2/envs/astroconda/lib/python2.7/site-packages/astropy/io/fits/header.py", line 1675, in _cardindex
    raise KeyError("Keyword {!r} not found.".format(keyword))
KeyError: "Keyword 'EXPTIME' not found."

This is presumably failing because load_fits is incorrectly trying to find the exposure time keyword in the 0th extension instead of the first.

amn3142 avatar Jan 14 '18 00:01 amn3142

Thanks, @amn3142! While that is a bug that should be fixed, the recommended approach is using the MultiBeam.write_master_fits method to write all of the individual beams to a single multi-extension FITS file with all of the necessary metadata. You can then read this in again later, e.g.,

beams = ogrp.get_beams(id, size = 25)
mb0 = MultiBeam(beams, group_name='test')

# write the beams.fits file
mb0.write_master_fits() 

# Read it again (e.g., later)
mb1 = MultiBeam('test_{0:05d}.beams.fits'.format(id))

gbrammer avatar Jan 14 '18 02:01 gbrammer

Ah! Got it, thanks :)

amn3142 avatar Jan 14 '18 04:01 amn3142

Update: Making the above change causes

grp = GroupFLT(grism_files = all_grism_files,direct_files = all_direct_files,seg_file=segName, catalog = catName,group_name='real',cpu_count = -1)

To fail because the exposure time keyword is not in the first fits extension of the original science flt, it appears to be in the zeroth extension?

amn3142 avatar Jan 17 '18 01:01 amn3142