diff --git a/grizli/prep.py b/grizli/prep.py index cde8a72e..b639a0b2 100644 --- a/grizli/prep.py +++ b/grizli/prep.py @@ -6196,19 +6196,6 @@ def get_jwst_wfssbkg_file(file, valid_flat=[0.6, 1.3], make_figure=False): from jwst.wfss_contam import WfssContamStep from jwst.flatfield import FlatFieldStep - - with pyfits.open(file) as im: - if im[0].header['INSTRUME'] == 'NIRISS': - # Test local file - bkg_file = "nis-{0}-{1}_skyflat.fits" - bkg_file = os.path.join(GRIZLI_PATH, 'CONF', - bkg_file.format(im[0].header['PUPIL'], - im[0].header['FILTER'])) - bkg_file = bkg_file.lower() - - if os.path.exists(bkg_file): - return bkg_file - bkg_file = WfssContamStep().get_reference_file(file, 'wfssbkg') # Not a FITS file? e.g., N/A for imaging exposures @@ -6222,8 +6209,25 @@ def get_jwst_wfssbkg_file(file, valid_flat=[0.6, 1.3], make_figure=False): _im.close() return bkg_file + h = pyfits.getheader(file,0) + if h['INSTRUME'] == 'NIRISS': + + # Test local file + local_bkg_file = "nis-{0}-{1}_skyflat.fits" + local_bkg_file = local_bkg_file.format(h['PUPIL'],h['FILTER']) + local_bkg_file = local_bkg_file.lower() + local_bkg_file = os.path.join(GRIZLI_PATH, 'CONF',local_bkg_file) + + # If local file exists and is up-to-date, use it + if os.path.exists(local_bkg_file) and pyfits.getval(local_bkg_file,'DATE',0) == h['DATE']: + return local_bkg_file + flat_file = FlatFieldStep().get_reference_file(file, 'flat') + # If we don't have write access copy to local file + if (not os.access(bkg_file,os.W_OK)) or (('CRDS_READONLY_CACHE' in os.environ) and (os.environ['CRDS_READONLY_CACHE'] == '1')): + shutil.copy(bkg_file,local_bkg_file) + bkg_file = local_bkg_file wf = pyfits.open(bkg_file, mode='update') key = f"{wf[0].header['FILTER']} {wf[0].header['PUPIL']}"