def ApplyBadPixelMask(images, cal_path, outbase = '_bpm'): """ ##################################################################################### # Fix a list of images with a bad pixel mask # # PARAMETERS: # images - list of images to fix # cal_path - path to calibration data # outbase - addition to the name of the image after mask has been applied # # RETURNS: # list of fixed images ##################################################################################### """ print '########################################' print '# Applying Bad Pixel Masks' print '########################################' # finds the bad pixel mask badPixelMask = [FindPixelMask(i, cal_path) for i in images] # Build the list of output image names out = [os.path.splitext(i) for i in images] # Split off the extension out = [i[0] + outbase + '.fit' for i in out] # Paste the outbase at the end of the filename # and put the extension back on # Set up to call the task iraf.set(clobber='yes') iraf.proto() iraf.fixpix.unlearn() # Forget everything iraf.fixpix.mode = 'h' # Don't ask for user input for (i,o,m) in zip(images, out, badPixelMask): # gets image data and copies it to the corresponding output file name shutil.copy(i,o) #runs fixpix on the output file (fixpix overwrites file data) if not os.path.isfile(i): print i + ": Input file does not exist." elif not os.path.isfile(m): print i + ": No pixel mask applied." else: try: #set parameters iraf.fixpix.images = o iraf.fixpix.masks = m print i + ": applying mask at " + m iraf.fixpix() except iraf.IrafError, e: print "fixpix failed" print "error #" + str(e.errno) print "Msg: " + e.errmsg print "Task: " + e.errtask raise e
def fixpix(fs=None): iraf.cd('work') if fs is None: fs = glob('nrm/sci*nrm*.fits') if len(fs) == 0: print "WARNING: No rectified images to fix." iraf.cd('..') return if not os.path.exists('fix'): os.mkdir('fix') for f in fs: outname = f.replace('nrm', 'fix') # Copy the file to the fix directory shutil.copy(f, outname) # Set all of the BPM pixels = 0 h = pyfits.open(outname, mode='update') h['SCI'].data[h['BPM'].data == 1] = 0 # Grab the CRM extension from the lax file laxhdu = pyfits.open(f.replace('nrm', 'lax')) h.append(pyfits.ImageHDU(data=laxhdu['CRM'].data.copy(), header=laxhdu['CRM'].header.copy(), name='CRM')) h.flush() h.close() laxhdu.close() # Run iraf's fixpix on the cosmic rays, not ideal, # but better than nothing because apall doesn't take a bad pixel mask iraf.unlearn(iraf.fixpix) iraf.flpr() iraf.fixpix(outname + '[SCI]', outname + '[CRM]', mode='hl') iraf.cd('..')
def makeMask(resFile): shutil.copy(resFile, resFile+".orig") iraf.ccdmask.setParam("image", resFile) maskFile = resFile + ".mask" iraf.ccdmask.setParam("mask", maskFile) iraf.ccdmask(Stdout=1, mode="h") iraf.fixpix.setParam("images", resFile ) iraf.fixpix.setParam("masks", maskFile+ ".pl") iraf.fixpix(Stdout=1, mode="h")
def makeMask(resFile): shutil.copy(resFile, resFile + ".orig") iraf.ccdmask.setParam("image", resFile) maskFile = resFile + ".mask" iraf.ccdmask.setParam("mask", maskFile) iraf.ccdmask(Stdout=1, mode="h") iraf.fixpix.setParam("images", resFile) iraf.fixpix.setParam("masks", maskFile + ".pl") iraf.fixpix(Stdout=1, mode="h")
def FixBadPixels(PC,images,nightdir): """ This will run iraf task proto.fixpix to interpolate badpixels """ PixelMask = os.path.join(nightdir,PC.BadPixelMaskName) if not os.path.isfile(PixelMask): print("No Bad Pixel Mask file found by the name "+ PixelMask) print("Hence skipping Bad pixel interpolation") return iraf.proto(_doprint=0) iraf.fixpix.unlearn() iraf.fixpix(images=images,masks=PixelMask)
pssl_temp = head_temp['SKYLEVEL'] # create mask image for template mask = np.abs(data_temp) < 1e-6 fits.writeto('tempmask.fits', mask.astype('i'), overwrite=True) if args.fixpix: try: iraf.flpr() iraf.flpr() iraf.unlearn(iraf.fixpix) cwd = os.getcwd() iraf.fixpix(os.path.join(cwd, imgtarg), os.path.join(cwd, targmask), verbose='yes') iraf.flpr() iraf.flpr() iraf.unlearn(iraf.fixpix) iraf.fixpix(os.path.join(cwd, imgtemp), os.path.join(cwd, tempmask), verbose='yes') iraf.flpr() iraf.flpr() iraf.unlearn(iraf.fixpix) except: print 'FIXPIX ERROR, continuing without fixpix' # hotpants parameters iuthresh = str( sat_targ) # upper valid data count, image
print 'variance image already there, do not create noise image' # if skylevel is in the header, swarp with bkg subtraction has been applyed if 'SKYLEVEL' in head_temp: pssl_temp = head_temp['SKYLEVEL'] # create mask image for template data_temp, head_temp mask = np.abs(data_temp) < 1e-6 fits.writeto('tempmask.fits',mask.astype('i')) if _fixpix: iraf.flpr(); iraf.flpr() iraf.unlearn(iraf.fixpix) cwd = os.getcwd() iraf.fixpix(os.path.join(cwd, imgtarg), os.path.join(cwd, targmask), verbose='yes') iraf.flpr(); iraf.flpr() iraf.unlearn(iraf.fixpix) iraf.fixpix(os.path.join(cwd, imgtemp), os.path.join(cwd, tempmask), verbose='yes') iraf.flpr(); iraf.flpr() iraf.unlearn(iraf.fixpix) # hotpants parameters iuthresh = str(sat_targ) # upper valid data count, image iucthresh = str(0.95*sat_targ) # upper valid data count for kernel, image tuthresh = str(sat_temp) # upper valid data count, template tucthresh = str(0.95*sat_temp) # upper valid data count for kernel, template rkernel = str(np.median([10, 2.*max_fwhm, 20])) # convolution kernel half width radius = str(np.median([15, 3.0*max_fwhm, 25])) # HW substamp to extract around each centroid sconv = '-sconv' # all regions convolved in same direction (0)
def mkobjmask(inlist, inpref='', outpref='m', bpm='none', minpix=250, hsigma=3, lsigma=10, conv='block 3 3'): # load IRAF package iraf.nproto() # open input list and check if it exists inimg_arr = check_input(inlist, inpref) if isinstance(inimg_arr, int): return 1 # check output images outimg_arr = check_outpref(outpref, inimg_arr) if isinstance(outimg_arr, int): return 1 # check optional parameters if bpm.lower() != 'none': if not os.access(bpm, os.R_OK): print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm return 1 bpmex = 1 else: bpmex = 0 if lsigma <= 0: print >> sy.stderr, 'invalid lsigma value (%f)' % lsigma return 1 if hsigma <= 0: print >> sys.stderr, 'invalid hsigma value (%f)' % hsigma return 1 # check convolution kernel name ret = 1 param = conv.split() if len(param) == 1: if os.access(param[0], os.R_OK): conv = '@' + conv ret = 0 else: print >> sys.stderr, 'convolution kernel file (%s) does not exist' % param[ 0] elif param[0] == 'block' and len(param) == 3: if param[1].isdigit() and param[2].isdigit(): ret = 0 elif param[0] == 'bilinear' and len(param) == 3: if param[1].isdigit() and param[2].isdigit() and int( param[1]) > 0 and int(param[2]) > 0: ret = 0 elif param[0] == 'gauss' and len(param) == 5: if param[1].isdigit() and param[2].isdigit() and int( param[1]) > 0 and int(param[2]) > 0: if isfloat(param[3]) and isfloat( param[4]) and float(param[3]) > 0 and float(param[4]) > 0: ret = 0 if ret > 0: print >> sys.stderr, 'invalid convolve parameter (%s)' % conv return 1 # prefix for temoprary images tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp') tmp_prefix = tmp.name tmp.close() # check if input images exist and fix bad pixel if requested for i in range(len(inimg_arr)): tmp_inimg = tmp_prefix + os.path.basename(inimg_arr[i]) tmp_outimg = tmp_prefix + 'mask' + os.path.basename(outimg_arr[i]) iraf.unlearn('imcopy') iraf.imcopy(inimg_arr[i], tmp_inimg, verbose='no') if bpmex == 1: iraf.unlearn('fixpix') iraf.fixpix(tmp_inimg, bpm) iraf.fixpix(tmp_inimg, bpm) iraf.fixpix(tmp_inimg, bpm) iraf.unlearn('objmasks') iraf.objmasks(tmp_inimg, tmp_outimg, omtype='boolean', masks='', convolve=conv, lsigma=lsigma, hsigma=hsigma, hdetect='yes', ldetect='yes', minpix=minpix) ret = [] ret = iraf.objmasks(tmp_inimg, outimg_arr[i], omtype='boolean', masks=tmp_outimg + '[pl]', convolve=conv, lsigma=lsigma, hsigma=hsigma, hdetect='yes', ldetect='yes', minpix=minpix, Stdout=1) if len(ret) != 0: iraf.unlearn('hedit') iraf.hedit(inimg_arr[i], 'objmask', outimg_arr[i] + '[pl]', add='yes', verify='no') os.remove(tmp_inimg) os.remove(tmp_outimg) if len(ret) == 0: print >> sys.stderr, 'failed to execute iraf objmasks task for %s' % inimg_arr[ i] return 1 return 0
def combine(do_cti=False, doreduce=True, doshifts=True): if do_cti: os.system('stis_cti --crds_update') if doreduce: # Defringing didn't seem to converge because of the low S/N stistools.ocrreject.ocrreject('oc0102070_flc.fits','oc0102070_crc.fits') iraf.normspflat(inflat='oc0102070_crc.fits',outflat='oc0102070_nsp.fits', do_cal='no') iraf.imcalc(input='oc0102070_nsp.fits', output='temp_nsp.fits', equals='if(x .lt. 250) then 1 else im1') iraf.imcopy('temp_nsp.fits[1][1:250,*]', 'oc0102070_nsp.fits[1][1:250,*]') #iraf.defringe('oc0102060_flc.fits', 'oc0102070_nsp.fits', 'oc0102060_dfr.fits') #for each image for im in ['oc0102050_flc','oc0102060_flc']: outbase = 'blue' if im[:-4] == 'oc0102060': outbase = 'red' #reset the aperture table to the newer file (we maybe should check this) pyfits.setval(im +'.fits','APERTAB',value='oref$y2r1559to_apt.fits') pyfits.setval(im +'.fits', 'SPTRCTAB', value='oref$qa31608go_1dt.fits') # fixpix any negative values. In principle some of this noise # could be real, but I have found that is often not the case hdu = fits.open(im+ '.fits') mask1 = hdu[1].data < -20 mask2 = hdu[4].data < -20 hdu.close() fits.writeto(outbase+'mask1.fits', mask1.astype('i'), clobber=True) fits.writeto(outbase+'mask2.fits', mask2.astype('i'), clobber=True) iraf.unlearn(iraf.fixpix) iraf.fixpix(im+'[1]', outbase+'mask1.fits') iraf.unlearn(iraf.fixpix) iraf.fixpix(im+'[4]', outbase+'mask2.fits') # Subtract off the median value hdu = fits.open(im+ '.fits', mode='update') hdu[1].data -= np.median(hdu[1].data) hdu[4].data -= np.median(hdu[4].data) readnoise1 = 1.4826 * np.median(np.abs(hdu[1].data)) readnoise2 = 1.4826 * np.median(np.abs(hdu[4].data)) # Cosmic ray reject both images using scrappy # Make sure to treat the noise in a sensible way crmask1, clean1 = detect_cosmics(hdu[1].data, readnoise=readnoise1, sigclip=5, objlim=5, sigfrac=0.8, fsmode='median', psfmodel='gaussy', psffwhm=2., cleantype='idw') crmask2, clean2 = detect_cosmics(hdu[4].data, readnoise=readnoise2, sigclip=5, objlim=5, sigfrac=0.8, fsmode='median', psfmodel='gaussy', psffwhm=2., cleantype='idw') hdu.flush() hdu.close() fits.writeto(outbase + '_crmask1.fits', crmask1.astype('i'), clobber=True) fits.writeto(outbase + '_crmask2.fits', crmask2.astype('i'), clobber=True) # Run fixpix on the frames iraf.unlearn(iraf.fixpix) iraf.fixpix(im+'[1]', outbase+'_crmask1.fits') iraf.unlearn(iraf.fixpix) iraf.fixpix(im+'[4]', outbase+'_crmask2.fits') if outbase=='red': iraf.mkfringeflat('oc0102060_flc.fits', 'oc0102070_nsp.fits', 'oc0102070_frr.fits', beg_scale=0.6, end_scale=1.5, scale_step=0.01, beg_shift=-3.0, end_shift=3.0,shift_step=0.05) iraf.defringe('oc0102060_flc.fits', 'oc0102070_frr.fits', 'oc0102060_dfr.fits') #Run x2d on the flt frame stistools.x2d.x2d(input='oc0102060_dfr.fits',output=im[:-4]+'x2d.fits') else: stistools.x2d.x2d(input='oc0102050_flc.fits',output=im[:-4]+'x2d.fits') h = pyfits.open(im[:-4]+'x2d.fits', mode='update') #Replace all of the bad pixels in the image by -666 based on the DQ array #save them to a new file #Throw away bad reference file pixels and saturated pixels. None of the other error codes #were in the first file so I haven't included them here, but we might want to d = h[3].data badpix = logical_and(bitwise_and(d,256) == 256,bitwise_and(d,512) == 512) h[1].data[badpix] = -10000 d = h[6].data badpix = logical_and(bitwise_and(d,256) == 256,bitwise_and(d,512) == 512) h[4].data[badpix] = -10000 h.flush() # Trim the images for i in range(1,7): h[i].data = h[i].data[100:-100, 100:-100] h[i].header['CRPIX1'] -= 100 h[i].header['CRPIX2'] -= 100 h.flush() h.close() # Combine the images iraf.unlearn(iraf.imcombine) iraf.imcombine(input=im[:-4]+'x2d[1],'+im[:-4]+'x2d[4]', output=outbase+'_com.fits', reject='crreject') hdu = pyfits.open(outbase +'_com.fits') mask = hdu[0].data == 0.0 hdu.close() fits.writeto(outbase+'_mask.fits', mask.astype('i'), clobber=True) iraf.unlearn(iraf.fixpix) iraf.fixpix(outbase+'_com.fits', outbase+'_mask.fits') iraf.unlearn(iraf.apall) iraf.apall(input=outbase+'_com',output='13dh_'+outbase, review='no', nsum = -500, b_order = 1, b_function='legendre',b_niterate=30, b_naverage = -21, nfind=1,t_order=3,background='median',weights='variance', skybox=100 ) iraf.splot(outbase+'[SCI]')
# SET DISPAXIS (dispersion axis) # you can set Dispaxis = 1 (horizontal) or 2 (vertical) print 'Setting dispaxis...' iraf.hedit('*.fits', 'dispaxis', '1', add=yes, verify=no, show=yes, update=yes) # CREATE ECHELLE BAD PIXEL MASK # Use the task TEXT2MASK to create the mask from the ASCII 'badcols' file #print 'Creating bad pixel mask from file "badcols"...' #iraf.text2mask('badcols','echmask',ncols=2128,nlines=2068,linterp=1,cinterp=1, # square=1,pixel=1) # Note: This ^ doesn't work currently on 64-bit systems. Does not produce # a working mask. Use 32-bit IRAF to produce mask. # Forces interpolation of bad pixel masks along the iraf.fixpix('@objflat', 'echmask.pl', linterp=1, verbose=yes) iraf.fixpix('@arcs', 'echmask.pl', linterp=1, verbose=yes) # MAKE AN AVERAGE (FIDUCIAL) BIAS AND DARK (if applicable) # by default, we skip the darks, but this can be changed if desired iraf.imdelete('bias_fid', verify=no) iraf.zerocombine('@biases', output='bias_fid', combine='median', reject='none', ccdtype='', process=no, delete=no, scale='none') #iraf.imdelete('dark_fid',verify=no) #iraf.darkcombine('@darks',output='dark_fid',combine='average',
def adjustDQ(self, inIm, ext, txtMask): print "\nADJUSTING MASK FOR " + inIm + ext.upper() # grab dimensions of mask from header of image # TODO: will expression for num work in one-slit case? hdu = fits.open(inIm) num = 3 * (int(ext[-2]) - 1) + 2 xDim = hdu[num].header["NAXIS1"] yDim = hdu[num].header["NAXIS2"] hdu.close() # display input image print "Displaying 2D spectra" iraf.display(inIm + ext) if ext[-2] == "1": iraf.imdelete("x" + inIm, verify="yes") # allow user to adjust mask iteratively while True: # ask user to input coordinate ranges to mask fixDQ = bool(input("\nDoes mask need improvement? (True/False): ")) if fixDQ: os.system("rm -iv " + txtMask) ranges = raw_input( "Coord ranges to be masked (space-separated): ") cmd = "echo '" + ranges + "' >> " + txtMask os.system(cmd) else: break # transform text mask to pixel list format plMask = txtMask[:txtMask.find(".")] + ".pl" iraf.delete(plMask) iraf.text2mask(txtMask, plMask, xDim, yDim) # interpolate over bad pixels and mark them in DQ plane tmp = "tmp_" + inIm[:inIm.find(".")] + "_" + ext[-2] + ".fits" tmpdq = "tmpdq_" + inIm[:inIm.find(".")] + "_" + ext[-2] + ".fits" images = tmp + "," + tmpdq iraf.imdelete(images) if ext[-2] == "1": iraf.copy(inIm, "x" + inIm) iraf.imcopy(inIm + ext, tmp) iraf.fixpix(tmp, plMask, linterp="1,2,3,4") iraf.imcopy(tmp + "[*,*]", "x" + inIm + ext + "[*,*]") iraf.imarith(plMask, "+", "x" + inIm + "[DQ," + ext[-2:], tmpdq) iraf.imcopy(tmpdq + "[*,*]", "x" + inIm + "[DQ," + ext[-2:] + \ "[*,*]") # display result iraf.display("x" + inIm + ext) # delete temporary files iraf.imdelete(images) return
def combine(do_cti=False, doreduce=True, doshifts=True): if do_cti: os.system('stis_cti --crds_update') if doreduce: # Defringing didn't seem to converge because of the low S/N stistools.ocrreject.ocrreject('oc0102070_flc.fits', 'oc0102070_crc.fits') iraf.normspflat(inflat='oc0102070_crc.fits', outflat='oc0102070_nsp.fits', do_cal='no') iraf.imcalc(input='oc0102070_nsp.fits', output='temp_nsp.fits', equals='if(x .lt. 250) then 1 else im1') iraf.imcopy('temp_nsp.fits[1][1:250,*]', 'oc0102070_nsp.fits[1][1:250,*]') #iraf.defringe('oc0102060_flc.fits', 'oc0102070_nsp.fits', 'oc0102060_dfr.fits') #for each image for im in ['oc0102050_flc', 'oc0102060_flc']: outbase = 'blue' if im[:-4] == 'oc0102060': outbase = 'red' #reset the aperture table to the newer file (we maybe should check this) pyfits.setval(im + '.fits', 'APERTAB', value='oref$y2r1559to_apt.fits') pyfits.setval(im + '.fits', 'SPTRCTAB', value='oref$qa31608go_1dt.fits') # fixpix any negative values. In principle some of this noise # could be real, but I have found that is often not the case hdu = fits.open(im + '.fits') mask1 = hdu[1].data < -20 mask2 = hdu[4].data < -20 hdu.close() fits.writeto(outbase + 'mask1.fits', mask1.astype('i'), clobber=True) fits.writeto(outbase + 'mask2.fits', mask2.astype('i'), clobber=True) iraf.unlearn(iraf.fixpix) iraf.fixpix(im + '[1]', outbase + 'mask1.fits') iraf.unlearn(iraf.fixpix) iraf.fixpix(im + '[4]', outbase + 'mask2.fits') # Subtract off the median value hdu = fits.open(im + '.fits', mode='update') hdu[1].data -= np.median(hdu[1].data) hdu[4].data -= np.median(hdu[4].data) readnoise1 = 1.4826 * np.median(np.abs(hdu[1].data)) readnoise2 = 1.4826 * np.median(np.abs(hdu[4].data)) # Cosmic ray reject both images using scrappy # Make sure to treat the noise in a sensible way crmask1, clean1 = detect_cosmics(hdu[1].data, readnoise=readnoise1, sigclip=5, objlim=5, sigfrac=0.8, fsmode='median', psfmodel='gaussy', psffwhm=2., cleantype='idw') crmask2, clean2 = detect_cosmics(hdu[4].data, readnoise=readnoise2, sigclip=5, objlim=5, sigfrac=0.8, fsmode='median', psfmodel='gaussy', psffwhm=2., cleantype='idw') hdu.flush() hdu.close() fits.writeto(outbase + '_crmask1.fits', crmask1.astype('i'), clobber=True) fits.writeto(outbase + '_crmask2.fits', crmask2.astype('i'), clobber=True) # Run fixpix on the frames iraf.unlearn(iraf.fixpix) iraf.fixpix(im + '[1]', outbase + '_crmask1.fits') iraf.unlearn(iraf.fixpix) iraf.fixpix(im + '[4]', outbase + '_crmask2.fits') if outbase == 'red': iraf.mkfringeflat('oc0102060_flc.fits', 'oc0102070_nsp.fits', 'oc0102070_frr.fits', beg_scale=0.6, end_scale=1.5, scale_step=0.01, beg_shift=-3.0, end_shift=3.0, shift_step=0.05) iraf.defringe('oc0102060_flc.fits', 'oc0102070_frr.fits', 'oc0102060_dfr.fits') #Run x2d on the flt frame stistools.x2d.x2d(input='oc0102060_dfr.fits', output=im[:-4] + 'x2d.fits') else: stistools.x2d.x2d(input='oc0102050_flc.fits', output=im[:-4] + 'x2d.fits') h = pyfits.open(im[:-4] + 'x2d.fits', mode='update') #Replace all of the bad pixels in the image by -666 based on the DQ array #save them to a new file #Throw away bad reference file pixels and saturated pixels. None of the other error codes #were in the first file so I haven't included them here, but we might want to d = h[3].data badpix = logical_and( bitwise_and(d, 256) == 256, bitwise_and(d, 512) == 512) h[1].data[badpix] = -10000 d = h[6].data badpix = logical_and( bitwise_and(d, 256) == 256, bitwise_and(d, 512) == 512) h[4].data[badpix] = -10000 h.flush() # Trim the images for i in range(1, 7): h[i].data = h[i].data[100:-100, 100:-100] h[i].header['CRPIX1'] -= 100 h[i].header['CRPIX2'] -= 100 h.flush() h.close() # Combine the images iraf.unlearn(iraf.imcombine) iraf.imcombine(input=im[:-4] + 'x2d[1],' + im[:-4] + 'x2d[4]', output=outbase + '_com.fits', reject='crreject') hdu = pyfits.open(outbase + '_com.fits') mask = hdu[0].data == 0.0 hdu.close() fits.writeto(outbase + '_mask.fits', mask.astype('i'), clobber=True) iraf.unlearn(iraf.fixpix) iraf.fixpix(outbase + '_com.fits', outbase + '_mask.fits') iraf.unlearn(iraf.apall) iraf.apall(input=outbase + '_com', output='13dh_' + outbase, review='no', nsum=-500, b_order=1, b_function='legendre', b_niterate=30, b_naverage=-21, nfind=1, t_order=3, background='median', weights='variance', skybox=100) iraf.splot(outbase + '[SCI]')
def fixpix(scifiles): # Run fixpix to interpolate over cosmic rays for f in scifiles: # run fixpix iraf.unlearn(iraf.fixpix) iraf.fixpix('t' + f[:-4] + '.fits[2]', f[:-4] + '.lamask.fits', mode='h')
pssl_temp = 0 print 'variance image already there, do not create noise image' # if skylevel is in the header, swarp with bkg subtraction has been applyed if 'SKYLEVEL' in head_temp: pssl_temp = head_temp['SKYLEVEL'] # create mask image for template data_temp, head_temp mask = np.abs(data_temp) < 1e-6 pyfits.writeto(temp_file0+'_tempmask3.fits',mask.astype('i')) if _fixpix: iraf.flpr(); iraf.flpr() iraf.unlearn(iraf.fixpix) iraf.fixpix('./'+imgtarg, './'+targmask, verbose='no') iraf.flpr(); iraf.flpr() iraf.unlearn(iraf.fixpix) iraf.fixpix('./'+imgtemp, './'+tempmask, verbose='no') iraf.flpr(); iraf.flpr() iraf.unlearn(iraf.fixpix) # hotpants parameters iuthresh = str(sat_targ) # upper valid data count, image iucthresh = str(0.95*sat_targ) # upper valid data count for kernel, image tuthresh = str(sat_temp) # upper valid data count, template tucthresh = str(0.95*sat_temp) # upper valid data count for kernel, template rkernel = str(np.median([10, 2.*max_fwhm, 20])) # convolution kernel half width radius = str(np.median([15, 3.0*max_fwhm, 25])) # HW substamp to extract around each centroid sconv = '-sconv' # all regions convolved in same direction (0) normalize = _normalize #normalize to (t)emplate, (i)mage, or (u)nconvolved (t)
def flatfield(inlist, flatimg, inpref='', outpref='f', dark='none', bpm='none'): # open input list and check if it exists inimg_arr = check_input(inlist, inpref) if isinstance(inimg_arr, int): return 1 # check flat image if os.access(flatimg, os.R_OK): im_flat = pyfits.open(flatimg) flat_data = im_flat[0].data flat_shape = flat_data.shape im_flat.close() else: print >> sys.stderr, 'cannot read flat image (%s)' % flatimg f.close() return 1 # check dark frame if dark.lower() != 'none': subdark = 1 if os.access(dark, os.R_OK): im_dark = pyfits.open(dark) dark_data = im_dark[0].data dark_shape = dark_data.shape im_dark.close() else: print >> sys.stderr, 'cannot read dark image (%s)' % dark return 1 else: subdark = 0 # check bad pixel mask if bpm.lower() != 'none': bpmex = 1 if os.access(bpm, os.R_OK) == False: print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm f.close() return 1 else: bpmex = 0 # check output prefix outimg_arr = check_outpref(outpref, inimg_arr) if isinstance(outimg_arr, int): return 1 # check if file exist and record exptime, coadds, ndr, slwcnt for i in range(len(inimg_arr)): # read image data and fits header im = pyfits.open(inimg_arr[i]) hdr0 = im[0].header im_shape = im[0].data.shape exptime = float(im[0].header['EXP1TIME']) coadds = int(im[0].header['COADDS']) ndr = int(im[0].header['NDR']) # normalize by coadds and ndr values out_data = im[0].data / (coadds * ndr) # close input image im.close() # subtract dark if requested if subdark == 1: if im_shape[0] == dark_shape[0] and im_shape[1] == dark_shape[1]: out_data = out_data - dark_data else: print >> sys.stderr, 'input image (%s) and dark (%s) have different image shape' % ( inimg, dark) return 1 # divide by flat frame if im_shape[0] == flat_shape[0] and im_shape[1] == flat_shape[1]: out_data = out_data / flat_data else: print >> sys.stderr, 'input image (%s) and flat (%s) have different image shape' % ( inimg, flatimg) return 1 # normalized by EXP1TIME out_data = out_data / exptime # write output data into fits hdu = pyfits.PrimaryHDU(out_data) imgout = pyfits.HDUList([hdu]) imgout[0].header = hdr0 imgout[0].header['EXPTIME'] = 1.0 imgout[0].header['EXPTORG'] = exptime * coadds imgout.writeto(outimg_arr[i]) imgout.close() if bpmex == 1: iraf.unlearn('fixpix') iraf.fixpix(outimg_arr[i], bpm, verbose='no') iraf.fixpix(outimg_arr[i], bpm, verbose='no') iraf.fixpix(outimg_arr[i], bpm, verbose='no') return 0