From 16de394149697f8c9e206be1312cd18c788c8a39 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Thu, 18 Oct 2018 14:07:56 -0400 Subject: [PATCH 1/6] added wfc3ir_tools with function to flatten ramp --- wfc3tools/wfc3ir_tools.py | 195 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 wfc3tools/wfc3ir_tools.py diff --git a/wfc3tools/wfc3ir_tools.py b/wfc3tools/wfc3ir_tools.py new file mode 100644 index 0000000..855b5d6 --- /dev/null +++ b/wfc3tools/wfc3ir_tools.py @@ -0,0 +1,195 @@ +from astropy.io import fits +import glob +import os +import shutil +import numpy as np +from wfc3tools import calwf3 +import glob +import pyregion + +def _reprocess_raw_crcorr(raw_file): + + raw_hdu = fits.open(raw_file, mode = 'update') + ### if raw file is part of an asn, assert that .asn is in same directory as .raw + asn_tab = raw_hdu[0].header['ASN_TAB'] + if asn_tab != '': + if not os.path.isfile(asn_tab): + raise OSError("{} must be in same directory as raw file.".format(asn_tab)) + ### generate ima file with CRCORR set to OMIT + orig_setting = raw_hdu[0].header['CRCORR'] + raw_hdu[0].header['CRCORR'] = 'OMIT' + raw_hdu.close() + if verbose == True: + print('Removing existing calwf3 outputs for {}'.format(raw_file)) + ### remove existing files with same name + for ext in ['flt','ima']: + if os.path.isfile(raw_file.replace('raw',ext)): + os.remove(raw_file.replace('raw',ext)) + if os.path.isfile(raw_file.replace('_raw.fits','.tra')): + os.remove(raw_file.replace('_raw.fits','.tra')) + ### run calwf3 + if verbose == True: + print('Generating initial IMA') + calwf3(raw_file, verbose = verbose) +def make_flattened_ramp_flt(raw_file, + stats_subregion = None, outfile = None, verbose = False): + + """ + Corrects for non-variable background and produce a 'flattened' FLT and IMA. Users + supply a raw file, which is used to create an ima file with calwf3 with the CRCORR + switch set to OMIT. Then, the average background level from each read of the ima is + subtracted from the read, and later added back to the full exposure to preserve pixel + statistics. Finally, calwf3 is run again on the flattened ima to produce an flt. + + Note that if the raw file is part of an association, the .asn file must be in the + same directory as the raw file. + + Users may provide a region for the median to be computed, otherwise this + will default to the median over the whole image excluding the 5 pixel overscan + region on the border. + + Parameters + ----------- + raw_file : string + Full path to raw file. + stats : string + Type of statistic to calculate and subtract (median or mean). Defaults to median. + sigma : int + If 'stats' is mean, integer sigma for sigma clipping + stats_subregion : tuple + Tuple of ((xmin, xmax), (ymin, ymax)) to specify region for average calculation. + Defaults to whole image, excluding the 5-pixel overscan region. + + Outputs + -------- + An FLT and IMA file created after flattening the reads (flattened_{}_flt.fits, + flattened_{}_ima.fits, and flattened_{}.tra) + + """ + basename_raw = os.path.basename(raw_file) + os.rename(raw_file, raw_file.replace(basename_raw,'flattened_'+basename_raw)) + raw_file_temp = raw_file.replace(basename_raw,'flattened_'+basename_raw) + raw_hdu = fits.open(raw_file_temp, mode = 'update') + + ### generate ima file with CRCORR set to OMIT + asn_tab = raw_hdu[0].header['ASN_TAB'] + orig_setting = raw_hdu[0].header['CRCORR'] + raw_hdu[0].header['CRCORR'] = 'OMIT' + raw_hdu.flush() + raw_hdu.close() + ### if raw file is part of an asn, assert that .asn is in same directory as .raw + if asn_tab != '': + if not os.path.isfile(asn_tab): + raise OSError("{} must be in same directory as raw file.".format(asn_tab)) + + ### remove existing calwf3 output with same name + for ext in ['flt','ima']: + if os.path.isfile(raw_file.replace('raw',ext)): + os.remove(raw_file.replace('raw',ext)) + if os.path.isfile(raw_file.replace('_raw.fits','.tra')): + os.remove(raw_file.replace('_raw.fits','.tra')) + + ### run calwf3 + calwf3(raw_file, verbose = verbose) + + ###remove intermediate flt/tra + if os.path.isfile(raw_file.replace('raw','flt')): + os.remove(raw_file.replace('raw','flt')) + if os.path.isfile(raw_file.replace('_raw.fits','.tra')): + os.remove(raw_file.replace('_raw.fits','.tra')) + ## change switch back to what it was originally so input file is not modified + raw_hdu = fits.open(raw_file, mode = 'update') + raw_hdu[0].header['CRCORR'] = orig_setting + raw_hdu.flush() + raw_hdu.close() + os.rename(raw_file, orig_raw_path) + + #work on new ima + ima_file = raw_file.replace('raw','ima') + ima_hdu = fits.open(ima_file, mode='update') + naxis1, naxis2 = ima_hdu['SCI',1].header['naxis1'], ima_hdu['SCI',1].header['naxis2'] + + #default to whole image minus the 5 overscan pixels + if stats_subregion is None: + xmin, xmax = 5, naxis1 + ymin, ymax = 5, naxis2 + stats_region =[[xmin,xmax], [ymin,ymax]] + slx = slice(stats_region[0][0], stats_region[0][1]) + sly = slice(stats_region[1][0], stats_region[1][1]) + + #Subtract per - read median count - rate scalar and add back in + #full exposure count rate to preserve pixel statistics + total_countrate = np.median(ima_hdu['SCI',1].data[sly, slx]) + + for i in range(ima_hdu[0].header['NSAMP']-1): + med = np.median(ima_hdu['SCI',i+1].data[sly, slx]) + ima_hdu['SCI',i+1].data += total_countrate - med + if verbose == True: + print('%s, [SCI,%d], median_bkg: %.2f' %(raw_file, i+1, med)) + + #Turn back on the ramp fitting for running calwf3 in the next step + ima_hdu[0].header['CRCORR'] ='PERFORM' + ima_hdu.close() + + calwf3(ima_file) + + #remove intermediate ima & rename new one + os.remove(ima_file) + + base_dir = orig_raw_path.replace(basename_raw,'') + for f in glob.glob(base_dir+'flattened_'+basename_raw.replace('_raw.fits','') + '*'): + if '_ima' in f: + print(f,f.replace('_ima','',1)) + os.rename(f,f.replace('_ima','',1)) + +# def ir_satmask(raw_file, region_filename, imset): +# """ +# Mask satellite trails in the DQ array of individual IMA read. +# +# Note that if the raw file is part of an association, the .asn file must be +# in the same directory as the raw file. +# +# Parameters +# ---------- +# raw_input : str +# Input raw file to be sattelite corrected +# +# region_filename: str +# Input region file name. Expecting DS9 style region file +# +# imset: int +# Imset (Read number) that needs to be satellite corrected. +# +# """ +# +# orig_raw_path = raw_file +# basename_raw = os.path.basename(orig_raw_path) +# os.rename(raw_file, raw_file.replace(basename_raw,'flattened_'+basename_raw)) +# raw_file = raw_file.replace(basename_raw,'flattened_'+basename_raw) +# raw_hdu = fits.open(raw_file, mode = 'update') +# ### if raw file is part of an asn, assert that .asn is in same directory as .raw +# asn_tab = raw_hdu[0].header['ASN_TAB'] +# if asn_tab != '': +# if not os.path.isfile(asn_tab): +# raise OSError("{} must be in same directory as raw file.".format(asn_tab)) +# ### generate ima file with CRCORR set to OMIT +# orig_setting = raw_hdu[0].header['CRCORR'] +# raw_hdu[0].header['CRCORR'] = 'OMIT' +# raw_hdu.close() +# if verbose == True: +# print('Removing existing calwf3 outputs for {}'.format(raw_file)) +# ### remove existing files with same name +# for ext in ['flt','ima']: +# if os.path.isfile(raw_file.replace('raw',ext)): +# os.remove(raw_file.replace('raw',ext)) +# if os.path.isfile(raw_file.replace('_raw.fits','.tra')): +# os.remove(raw_file.replace('_raw.fits','.tra')) +# ### run calwf3 +# if verbose == True: +# print('Generating initial IMA') +# calwf3(raw_file, verbose = verbose) +# + + + + \ No newline at end of file From 82d8c1dc09e64501289f1aa0f4be6728b98bf9a7 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Thu, 18 Oct 2018 14:24:59 -0400 Subject: [PATCH 2/6] fixed docs --- wfc3tools/wfc3ir_tools.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wfc3tools/wfc3ir_tools.py b/wfc3tools/wfc3ir_tools.py index 855b5d6..6e627ed 100644 --- a/wfc3tools/wfc3ir_tools.py +++ b/wfc3tools/wfc3ir_tools.py @@ -52,13 +52,13 @@ def make_flattened_ramp_flt(raw_file, ----------- raw_file : string Full path to raw file. - stats : string - Type of statistic to calculate and subtract (median or mean). Defaults to median. - sigma : int - If 'stats' is mean, integer sigma for sigma clipping stats_subregion : tuple Tuple of ((xmin, xmax), (ymin, ymax)) to specify region for average calculation. Defaults to whole image, excluding the 5-pixel overscan region. + outfile : str + output file name. Defaults to original file rootnames with 'flt' + verbose : bool + *** This flag requires a not yet implemented fix in calwf3. Outputs -------- From f4d68aace3f9990c3d1c4bafc8bf90e298c53523 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Fri, 26 Oct 2018 14:28:37 -0400 Subject: [PATCH 3/6] deleted commented out lines --- wfc3tools/wfc3ir_tools.py | 50 +-------------------------------------- 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/wfc3tools/wfc3ir_tools.py b/wfc3tools/wfc3ir_tools.py index 6e627ed..d285d3f 100644 --- a/wfc3tools/wfc3ir_tools.py +++ b/wfc3tools/wfc3ir_tools.py @@ -31,6 +31,7 @@ def _reprocess_raw_crcorr(raw_file): if verbose == True: print('Generating initial IMA') calwf3(raw_file, verbose = verbose) + def make_flattened_ramp_flt(raw_file, stats_subregion = None, outfile = None, verbose = False): @@ -142,54 +143,5 @@ def make_flattened_ramp_flt(raw_file, print(f,f.replace('_ima','',1)) os.rename(f,f.replace('_ima','',1)) -# def ir_satmask(raw_file, region_filename, imset): -# """ -# Mask satellite trails in the DQ array of individual IMA read. -# -# Note that if the raw file is part of an association, the .asn file must be -# in the same directory as the raw file. -# -# Parameters -# ---------- -# raw_input : str -# Input raw file to be sattelite corrected -# -# region_filename: str -# Input region file name. Expecting DS9 style region file -# -# imset: int -# Imset (Read number) that needs to be satellite corrected. -# -# """ -# -# orig_raw_path = raw_file -# basename_raw = os.path.basename(orig_raw_path) -# os.rename(raw_file, raw_file.replace(basename_raw,'flattened_'+basename_raw)) -# raw_file = raw_file.replace(basename_raw,'flattened_'+basename_raw) -# raw_hdu = fits.open(raw_file, mode = 'update') -# ### if raw file is part of an asn, assert that .asn is in same directory as .raw -# asn_tab = raw_hdu[0].header['ASN_TAB'] -# if asn_tab != '': -# if not os.path.isfile(asn_tab): -# raise OSError("{} must be in same directory as raw file.".format(asn_tab)) -# ### generate ima file with CRCORR set to OMIT -# orig_setting = raw_hdu[0].header['CRCORR'] -# raw_hdu[0].header['CRCORR'] = 'OMIT' -# raw_hdu.close() -# if verbose == True: -# print('Removing existing calwf3 outputs for {}'.format(raw_file)) -# ### remove existing files with same name -# for ext in ['flt','ima']: -# if os.path.isfile(raw_file.replace('raw',ext)): -# os.remove(raw_file.replace('raw',ext)) -# if os.path.isfile(raw_file.replace('_raw.fits','.tra')): -# os.remove(raw_file.replace('_raw.fits','.tra')) -# ### run calwf3 -# if verbose == True: -# print('Generating initial IMA') -# calwf3(raw_file, verbose = verbose) -# - - \ No newline at end of file From 7f32e3d4b92c56f478bff6d812d3a8b01ddf6694 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Tue, 27 Nov 2018 16:57:48 -0500 Subject: [PATCH 4/6] added full file path input, sigma clipping. removed custom output file name option --- wfc3tools/wfc3ir_tools.py | 206 ++++++++++++++++++++++---------------- 1 file changed, 120 insertions(+), 86 deletions(-) diff --git a/wfc3tools/wfc3ir_tools.py b/wfc3tools/wfc3ir_tools.py index d285d3f..703483a 100644 --- a/wfc3tools/wfc3ir_tools.py +++ b/wfc3tools/wfc3ir_tools.py @@ -1,4 +1,5 @@ from astropy.io import fits +from astropy.stats import sigma_clipped_stats import glob import os import shutil @@ -9,31 +10,70 @@ def _reprocess_raw_crcorr(raw_file): + """Bookkeeping + call to calwf3 to make ima from raw, turning CRCORR switch off.""" + + ### temporary rename of file + os.rename(raw_file, 'temp_'+ raw_file) + raw_file = 'temp_'+ raw_file + raw_hdu = fits.open(raw_file, mode = 'update') - ### if raw file is part of an asn, assert that .asn is in same directory as .raw + + ### set CRCORR to OMIT + orig_setting = raw_hdu[0].header['CRCORR'] + raw_hdu[0].header['CRCORR'] = 'OMIT' + raw_hdu.close() + + ### if part of an association, assert .ASN is in same directory as .RAW + ### catch error now - later the .ima won't run though the pipeline without the .asn. asn_tab = raw_hdu[0].header['ASN_TAB'] if asn_tab != '': if not os.path.isfile(asn_tab): raise OSError("{} must be in same directory as raw file.".format(asn_tab)) - ### generate ima file with CRCORR set to OMIT - orig_setting = raw_hdu[0].header['CRCORR'] - raw_hdu[0].header['CRCORR'] = 'OMIT' - raw_hdu.close() - if verbose == True: - print('Removing existing calwf3 outputs for {}'.format(raw_file)) - ### remove existing files with same name - for ext in ['flt','ima']: - if os.path.isfile(raw_file.replace('raw',ext)): - os.remove(raw_file.replace('raw',ext)) - if os.path.isfile(raw_file.replace('_raw.fits','.tra')): - os.remove(raw_file.replace('_raw.fits','.tra')) + + ### remove existing 'temp' files from previous run with same name, if they exist + exts = ['flt','flc','ima'] + existing_files = [raw_file.replace('raw', ext) for ext in exts] + + for f in existing_files: + if os.path.isfile(f): + os.remove(f) + ### run calwf3 - if verbose == True: - print('Generating initial IMA') - calwf3(raw_file, verbose = verbose) + calwf3(raw_file) -def make_flattened_ramp_flt(raw_file, - stats_subregion = None, outfile = None, verbose = False): + ### removing resulting FLT and TRA, we just want IMA generated with CRCORR off + for f in existing_files[0:2]: + if os.path.isfile(f): + os.remove(f) + os.remove(raw_file.replace('_raw.fits','.tra')) + + ### rename raw file to original, IMA from 'temp' to 'flattened', and tra + os.rename(raw_file, raw_file.replace('temp_','')) + raw_file = raw_file.replace('temp_','') + os.rename('temp_'+raw_file.replace('raw','ima'), \ + 'flattened_' + raw_file.replace('raw','ima')) + + ### reset CRCORR in raw file to original setting so file is unchanged + raw_hdu = fits.open(raw_file, mode = 'update') + raw_hdu[0].header['CRCORR'] = orig_setting + raw_hdu.close() + +def _calc_avg(data, stats_method, sigma_clip, sigma, sigma_upper, sigma_lower, iters): + """ Returns a mean or median from an array, with optional sigma clipping.""" + if not sigma_clip: + if stats_method == 'median': + return np.median(data) + return np.mean(data) + else: + mean, med, s = sigma_clipped_stats(data, sigma = sigma, sigma_lower = sigma_lower, + sigma_upper = sigma_upper, iters = iters) + if stats_method == 'median': + return med + return mean + +def make_flattened_ramp_flt(raw_file, stats_subregion = None, stats_method = 'median', + sigma_clip = False, sigma = None, sigma_upper = None, + sigma_lower = None, iters = None): """ Corrects for non-variable background and produce a 'flattened' FLT and IMA. Users @@ -51,15 +91,28 @@ def make_flattened_ramp_flt(raw_file, Parameters ----------- - raw_file : string + raw_file : str Full path to raw file. stats_subregion : tuple Tuple of ((xmin, xmax), (ymin, ymax)) to specify region for average calculation. - Defaults to whole image, excluding the 5-pixel overscan region. - outfile : str - output file name. Defaults to original file rootnames with 'flt' - verbose : bool - *** This flag requires a not yet implemented fix in calwf3. + Defaults to whole image, excluding the 5-pixel overscan region. + stats_method : str + Method of stats computation on each read when subtracting the average to flatten. + Must be 'mean' or 'median'. Defaults to 'median'. + sigma_clip : bool + If True, data will be sigma-clipped when computing stats. Defaults to False. + sigma : int + The number of standard deviations to use as the lower + and upper clipping limit. Defaults to None, but must be set if sigma_clip = True. + sigma_upper : int + The number of standard deviations to use as the upper bound for the clipping + limit. If None then the value of sigma is used. Defaults to None. + sigma_lower : int + The number of standard deviations to use as the lower bound for the clipping + limit. If None then the value of sigma is used. Defaults to None. + iters : int + The number of iterations to perform sigma clipping. Defaults to None, but must + be set if sigma_clip = True. Outputs -------- @@ -67,81 +120,62 @@ def make_flattened_ramp_flt(raw_file, flattened_{}_ima.fits, and flattened_{}.tra) """ + ### input checking + if stats_method not in ['median', 'mean']: + raise ValueError('stats_method must be mean or median') + if not sigma_clip: + if sigma or sigma_upper or sigma_lower or iters: + true_params = np.array(('sigma','sigma_upper','sigma_lower'))[np.array((sigma,\ + sigma_upper,sigma_lower)).astype('bool')] + raise ValueError('sigma_clip = False, but parameters {} were set'.\ + format(true_params)) + else: + if not sigma: + raise ValueError('sigma_clipping = True, parameter sigma must be provided') + + starting_path = os.getcwd() basename_raw = os.path.basename(raw_file) - os.rename(raw_file, raw_file.replace(basename_raw,'flattened_'+basename_raw)) - raw_file_temp = raw_file.replace(basename_raw,'flattened_'+basename_raw) - raw_hdu = fits.open(raw_file_temp, mode = 'update') - - ### generate ima file with CRCORR set to OMIT - asn_tab = raw_hdu[0].header['ASN_TAB'] - orig_setting = raw_hdu[0].header['CRCORR'] - raw_hdu[0].header['CRCORR'] = 'OMIT' - raw_hdu.flush() - raw_hdu.close() - ### if raw file is part of an asn, assert that .asn is in same directory as .raw - if asn_tab != '': - if not os.path.isfile(asn_tab): - raise OSError("{} must be in same directory as raw file.".format(asn_tab)) - - ### remove existing calwf3 output with same name - for ext in ['flt','ima']: - if os.path.isfile(raw_file.replace('raw',ext)): - os.remove(raw_file.replace('raw',ext)) - if os.path.isfile(raw_file.replace('_raw.fits','.tra')): - os.remove(raw_file.replace('_raw.fits','.tra')) - - ### run calwf3 - calwf3(raw_file, verbose = verbose) - - ###remove intermediate flt/tra - if os.path.isfile(raw_file.replace('raw','flt')): - os.remove(raw_file.replace('raw','flt')) - if os.path.isfile(raw_file.replace('_raw.fits','.tra')): - os.remove(raw_file.replace('_raw.fits','.tra')) - ## change switch back to what it was originally so input file is not modified + path_raw = raw_file.replace(basename_raw,'') raw_hdu = fits.open(raw_file, mode = 'update') - raw_hdu[0].header['CRCORR'] = orig_setting - raw_hdu.flush() - raw_hdu.close() - os.rename(raw_file, orig_raw_path) + + ### must work in pwd for calwf3 + os.chdir(path_raw) + ### run calwf3 w/ CRCORR off on raw to make ima + _reprocess_raw_crcorr(basename_raw) - #work on new ima - ima_file = raw_file.replace('raw','ima') + ### work on new flattened IMA + ima_file = raw_file.replace(basename_raw, 'flattened_' + basename_raw).replace('raw','ima') ima_hdu = fits.open(ima_file, mode='update') naxis1, naxis2 = ima_hdu['SCI',1].header['naxis1'], ima_hdu['SCI',1].header['naxis2'] - - #default to whole image minus the 5 overscan pixels + + ### default to whole image minus the 5 overscan pixels if stats_subregion is None: - xmin, xmax = 5, naxis1 + xmin, xmax = 5, naxis1 ymin, ymax = 5, naxis2 stats_region =[[xmin,xmax], [ymin,ymax]] slx = slice(stats_region[0][0], stats_region[0][1]) sly = slice(stats_region[1][0], stats_region[1][1]) - - #Subtract per - read median count - rate scalar and add back in - #full exposure count rate to preserve pixel statistics - total_countrate = np.median(ima_hdu['SCI',1].data[sly, slx]) - + + ### subtract per-read median countrate scalar and add back in full exposure count + ### rate to preserve pixel statistics + total_countrate = _calc_avg(ima_hdu['SCI',1].data[sly, slx], stats_method, \ + sigma_clip, sigma, sigma_upper, sigma_lower, iters) + for i in range(ima_hdu[0].header['NSAMP']-1): - med = np.median(ima_hdu['SCI',i+1].data[sly, slx]) - ima_hdu['SCI',i+1].data += total_countrate - med - if verbose == True: - print('%s, [SCI,%d], median_bkg: %.2f' %(raw_file, i+1, med)) - - #Turn back on the ramp fitting for running calwf3 in the next step + avg = _calc_avg(ima_hdu['SCI',i+1].data[sly, slx], stats_method, \ + sigma_clip, sigma, sigma_upper, sigma_lower, iters) + ima_hdu['SCI',i+1].data += total_countrate - avg + + ### turn back on the ramp fitting and run calwf3 ima_hdu[0].header['CRCORR'] ='PERFORM' + ima_hdu.flush() ima_hdu.close() - calwf3(ima_file) - #remove intermediate ima & rename new one - os.remove(ima_file) - - base_dir = orig_raw_path.replace(basename_raw,'') - for f in glob.glob(base_dir+'flattened_'+basename_raw.replace('_raw.fits','') + '*'): - if '_ima' in f: - print(f,f.replace('_ima','',1)) + ### remove one copy of IMA file and rename newly processed files + os.remove(ima_file) + for f in glob.glob(starting_path+'/flattened_'+basename_raw.replace('_raw.fits','') + '*'): + if ('_ima_' in f) or ('.tra' in f): os.rename(f,f.replace('_ima','',1)) - - - \ No newline at end of file + + os.chdir(starting_path) \ No newline at end of file From bf4b191bceda94863f904a7f1198ca884c9bf074 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Wed, 28 Nov 2018 13:57:23 -0500 Subject: [PATCH 5/6] fix for working with files in same directory --- wfc3tools/wfc3ir_tools.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/wfc3tools/wfc3ir_tools.py b/wfc3tools/wfc3ir_tools.py index 703483a..e7d44ad 100644 --- a/wfc3tools/wfc3ir_tools.py +++ b/wfc3tools/wfc3ir_tools.py @@ -130,8 +130,9 @@ def make_flattened_ramp_flt(raw_file, stats_subregion = None, stats_method = 'me raise ValueError('sigma_clip = False, but parameters {} were set'.\ format(true_params)) else: - if not sigma: - raise ValueError('sigma_clipping = True, parameter sigma must be provided') + if not (sigma): + if not (sigma_upper and sigma_lower): + raise ValueError('Must set sigma, or both sigma_upper and sigma_lower.') starting_path = os.getcwd() basename_raw = os.path.basename(raw_file) @@ -139,7 +140,8 @@ def make_flattened_ramp_flt(raw_file, stats_subregion = None, stats_method = 'me raw_hdu = fits.open(raw_file, mode = 'update') ### must work in pwd for calwf3 - os.chdir(path_raw) + if path_raw != '': + os.chdir(path_raw) ### run calwf3 w/ CRCORR off on raw to make ima _reprocess_raw_crcorr(basename_raw) @@ -178,4 +180,5 @@ def make_flattened_ramp_flt(raw_file, stats_subregion = None, stats_method = 'me if ('_ima_' in f) or ('.tra' in f): os.rename(f,f.replace('_ima','',1)) - os.chdir(starting_path) \ No newline at end of file + if path_raw != '': + os.chdir(starting_path) \ No newline at end of file From 65a66b17ddba723eaef17994a4e829e28896e5e8 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Wed, 26 Jun 2019 16:27:01 -0400 Subject: [PATCH 6/6] added workaround for files with no asn --- wfc3tools/wfc3ir_tools.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/wfc3tools/wfc3ir_tools.py b/wfc3tools/wfc3ir_tools.py index e7d44ad..4532a13 100644 --- a/wfc3tools/wfc3ir_tools.py +++ b/wfc3tools/wfc3ir_tools.py @@ -23,12 +23,17 @@ def _reprocess_raw_crcorr(raw_file): raw_hdu[0].header['CRCORR'] = 'OMIT' raw_hdu.close() - ### if part of an association, assert .ASN is in same directory as .RAW - ### catch error now - later the .ima won't run though the pipeline without the .asn. + ## if part of an association, assert .ASN is in same directory as .RAW + ## catch error now - later the .ima won't run though the pipeline without the .asn. asn_tab = raw_hdu[0].header['ASN_TAB'] - if asn_tab != '': - if not os.path.isfile(asn_tab): - raise OSError("{} must be in same directory as raw file.".format(asn_tab)) + if asn_tab == 'NONE': #make dummy asn table. remove this clause once calwf3 is patched + new_asn_tab = np.rec.array([(os.path.basename(raw_file)[0:9],'EXP-DTH', 1)], formats = 'S14,S14,i1', names='MEMNAME,MEMTYPE,MEMPRSNT') + hdu_1 = fits.BinTableHDU(new_asn_tab) + none_hdu_list = fits.HDUList([fits.open(raw_file)[0], hdu_1]) + none_hdu_list.writeto('NONE') + + if not os.path.isfile(asn_tab): + raise OSError("{} must be in same directory as raw file.".format(asn_tab)) ### remove existing 'temp' files from previous run with same name, if they exist exts = ['flt','flc','ima']