diff --git a/pypeit/scripts/sensfunc.py b/pypeit/scripts/sensfunc.py index d17ee5cb0d..75b7dd77c1 100644 --- a/pypeit/scripts/sensfunc.py +++ b/pypeit/scripts/sensfunc.py @@ -120,21 +120,21 @@ def main(args): " multi_spec_det = 3,7\n" "\n") - # Determine the spectrograph - hdul = io.fits_open(args.spec1dfile) - spectrograph = load_spectrograph(hdul[0].header['PYP_SPEC']) - spectrograph_config_par = spectrograph.config_specific_par(hdul) - - # Construct a primary FITS header that includes the spectrograph's - # config keys for inclusion in the output sensfunc file - primary_hdr = io.initialize_header() - add_keys = ( - ['PYP_SPEC', 'DATE-OBS', 'TELESCOP', 'INSTRUME', 'DETECTOR'] - + spectrograph.configuration_keys() + spectrograph.raw_header_cards() - ) - for key in add_keys: - if key.upper() in hdul[0].header.keys(): - primary_hdr[key.upper()] = hdul[0].header[key.upper()] + # Determine the spectrograph and generate the primary FITS header + with io.fits_open(args.spec1dfile) as hdul: + spectrograph = load_spectrograph(hdul[0].header['PYP_SPEC']) + spectrograph_config_par = spectrograph.config_specific_par(hdul) + + # Construct a primary FITS header that includes the spectrograph's + # config keys for inclusion in the output sensfunc file + primary_hdr = io.initialize_header() + add_keys = ( + ['PYP_SPEC', 'DATE-OBS', 'TELESCOP', 'INSTRUME', 'DETECTOR'] + + spectrograph.configuration_keys() + spectrograph.raw_header_cards() + ) + for key in add_keys: + if key.upper() in hdul[0].header.keys(): + primary_hdr[key.upper()] = hdul[0].header[key.upper()] # If the .sens file was passed in read it and overwrite default parameters diff --git a/pypeit/tests/test_runpypeit.py b/pypeit/tests/test_runpypeit.py index 7640591405..eb1c157868 100644 --- a/pypeit/tests/test_runpypeit.py +++ b/pypeit/tests/test_runpypeit.py @@ -75,7 +75,6 @@ def test_run_pypeit(): sf = sensfunc.SensFunc.from_file(str(sens_file)) assert len(sf.wave) > 0 assert len(sf.zeropoint) > 0 - sf = None # Flux calibrate with open(outdir / "fluxfile", "w") as f: @@ -113,7 +112,6 @@ def test_run_pypeit(): assert len(specObjs[0].BOX_FLAM[mask]) != 0 assert len(specObjs[0].BOX_FLAM_IVAR[mask]) != 0 assert len(specObjs[0].BOX_FLAM_SIG[mask]) != 0 - specObjs = None # Clean-up shutil.rmtree(outdir)