def combine_flat(filename): outname = filename.replace('.lst','.fits') print 'run function flatcombine...' print 'make file', outname iraf.flatcombine(input = 'tbo//@' + filename , output = outname, combine = 'average', reject = 'avsigclip' , ccdtype = '', process = False, subsets = True , delete = False, clobber = False, scale = 'mode' , statsec = '', nlow = 1, nhigh = 1, nkeep = 1 , mclip = True, lsigma = 3.0, hsigma = 3.0 , rdnoise = 9.4, gain = 0.35, snoise = 0.0 , pclip = -0.5, blank = 1.0) iraf.noao() iraf.twodspec() iraf.longslit() print 'run function response...' print 'make file', 're' + outname iraf.response(calibration = outname , normalization = outname, response = 're' + outname , interactive = True, threshold = 'INDEF', sample = '*' , naverage = 1, function = 'spline3', order = 7 , low_reject = 0.0, high_reject = 0.0, niterate = 1 , grow = 0.0, graphics = 'stdgraph', cursor = '') print 'run function illumination...' print 'make file', 'il' + outname iraf.illumination(images = 're' + outname , illuminations = 'il' + outname, interactive = False , bins = '', nbins = 5, sample = '*', naverage = 1 , function = 'spline3', order = 1, low_reject = 0.0 , high_reject = 0.0, niterate = 1, grow = 0.0 , interpolator = 'poly3', graphics = 'stdgraph', cursor = '') print 'run function imarith...' print 'make file', 'per' + outname iraf.imarith(operand1 = 're' + outname , op = '/', operand2 = 'il' + outname, result = 'per' + outname , title = '', divzero = 0.0, hparams = '', pixtype = '' , calctype = '', verbose = True, noact = False) return outname, 're' + outname, 'il' + outname, 'per' + outname
reject='sigclip', lsigma=3.0, hsigma=3.0, weight='exposure', expname='EXPTIME') pyfits.setval(combineoutname, 'DISPAXIS', value=1) # We want to make an illumination correction file # before running response: illumoutname = 'flats/flt%05.2fill.fits' % (ga) iraf.unlearn(iraf.illumination) iraf.illumination(images=combineoutname, illuminations=illumoutname, interactive=False, naverage=-40, order=11, low_reject=3.0, high_reject=3.0, niterate=5, mode='hl') # Flag any pixels in the illumination correction< 0.1 illumhdu = pyfits.open(illumoutname, mode='update') illumhdu[0].data[illumhdu[0].data <= 0.1] = 0.0 illumhdu[0].data[np.isnan(illumhdu[0].data)] = 0.0 illumhdu.flush() # Get 40 pixels out of the middle of the image and # median them to run response combinehdu = pyfits.open(combineoutname) ny = combinehdu[0].data.shape[0]
def makeflats(fs=None): # Note the list of files need to not include any paths relative to # the work directory. # Maybe not the greatest convention, but we can update this later iraf.cd('work') if fs is None: fs = glob('pysalt/bxgp*.fits') if len(fs) == 0: print "WARNING: No flat-fields to combine and normalize." # Fail gracefully by going up a directory iraf.cd('..') return # make a flats directory if not os.path.exists('flats'): os.mkdir('flats') # Figure out which images are flats and which grating angles were used allflats, grangles = get_ims(fs, 'flat') # For each grating angle for ga in np.unique(grangles): # grab the flats for this gr angle flats = allflats[grangles == ga] # For each chip for c in range(1, 7): # run imcombine with average and sigclip, weighted by exposure time flatlist = '' for f in flats: flatlist += '%s[%i],' % (f, c) # Add the exptime keyword to each extension pyfits.setval(f, 'EXPTIME', ext=c, value=pyfits.getval(f, 'EXPTIME')) # set the output combined file name combineoutname = 'flats/flt%05.2fcomc%i.fits' % (ga, c) if os.path.exists(combineoutname): os.remove(combineoutname) # initialize the iraf command iraf.unlearn(iraf.imcombine) print(flatlist) # don't forget to remove the last comma in the filelist iraf.imcombine(input=flatlist[:-1], output=combineoutname, combine='average', reject='sigclip', lsigma=3.0, hsigma=3.0, weight='exposure', expname='EXPTIME') pyfits.setval(combineoutname, 'DISPAXIS', value=1) # We want to make an illumination correction file # before running response: illumoutname = 'flats/flt%05.2fillc%i.fits' % (ga, c) iraf.unlearn(iraf.illumination) iraf.illumination(images=combineoutname, illuminations=illumoutname, interactive=False, naverage=-40, order=11, low_reject=3.0, high_reject=3.0, niterate=5, mode='hl') # Flag any pixels in the illumination correction< 0.1 illumhdu = pyfits.open(illumoutname, mode='update') illumhdu[0].data[illumhdu[0].data <= 0.1] = 0.0 illumhdu.flush() # Get 40 pixels out of the middle of the image and # median them to run response combinehdu = pyfits.open(combineoutname) ny = combinehdu[0].data.shape[0] # divide out the illumination correction before running response flat1d = np.median(combinehdu[0].data[ny / 2 - 21: ny / 2 + 20, :] / illumhdu[0].data[ny / 2 - 21: ny / 2 + 20, :], axis=0) # close the illumination file because we don't need it anymore illumhdu.close() # File stage m1d for median 1-D flat1dfname = 'flats/flt%05.2fm1dc%i.fits' % (ga, c) tofits(flat1dfname, flat1d, hdr=combinehdu[0].header.copy()) # run response # r1d = response1d resp1dfname = 'flats/flt%05.2fr1dc%i.fits' % (ga, c) iraf.response(flat1dfname, flat1dfname, resp1dfname, order=31, interactive=False, naverage=-5, low_reject=3.0, high_reject=3.0, niterate=5, mode='hl') resp1dhdu = pyfits.open(resp1dfname) resp1d = resp1dhdu[0].data.copy() resp1dhdu.close() # After response divide out the response function # normalize the 1d resp to its median resp1d /= np.median(resp1d) # Chuck any outliers flatsig = np.std(resp1d - 1.0) resp1d[abs(resp1d - 1.0) > 5.0 * flatsig] = 1.0 resp = flat1d / resp1d resp2dfname = 'flats/flt%05.2fresc%i.fits' % (ga, c) resp2d = combinehdu[0].data.copy() / resp tofits(resp2dfname, resp2d, hdr=combinehdu[0].header.copy()) combinehdu.close() # close the combined flat because we don't need it anymore combinehdu.close() pyfits.setval(resp2dfname, 'DISPAXIS', value=1) # Reset any pixels in the flat field correction< 0.1 # We could flag bad pixels here if we want, but not right now flathdu = pyfits.open(resp2dfname, mode='update') flathdu[0].data[flathdu[0].data <= 0.1] = 0.0 flathdu.flush() flathdu.close() # Step back up to the top directory iraf.cd('..')