def estimateBackground(fine, infile, flex=None, outname=None): if outname is None: print "Need an output name" return dX = 0 dY = 0 infile[0].data = infile[0].data.astype(np.float64) data = infile[0].data.copy() for ff in fine: if not ff.ok: continue if ff.xrange is None: continue if ff.poly is None: continue xs = np.arange(*ff.xrange) ys = np.round(np.poly1d(ff.poly)(xs)).astype(np.int) for dY in xrange(-5,6): ty = ys.copy() - dY try: data[ty,xs] = np.nan except: pass from astropy.convolution import convolve, convolve_fft, Box2DKernel print "Traditional convolve (pass 1)" k = Box2DKernel(17) flt = data.copy() NaNs = ~np.isfinite(data) OKs = np.isfinite(data) for i in xrange(5): flt = convolve(flt, k) flt[OKs] = data[OKs] #IO.writefits(pf.PrimaryHDU(flt), "test_%i.fits.gz" % i, clobber=True) data[NaNs] = flt[NaNs] fname = os.path.join(os.path.dirname(outname), "lf_" + os.path.basename(outname)) #IO.writefits(data, fname, clobber=True) print "FFT convolve (pass 2)" #k = Box2DKernel(70) #flt = convolve_fft(data, k) flt = gaussian_filter(data, 100) fname = os.path.join(os.path.dirname(outname), "bgd_" + os.path.basename(outname)) HDU = pf.PrimaryHDU(flt) IO.writefits(HDU, fname, clobber=True) fname = os.path.join(os.path.dirname(outname), "bs_" + os.path.basename(outname)) infile[0].header["BGDSUB"] = "Background subtracted with %s" % fname infile[0].data -= flt IO.writefits(infile, fname, clobber=True) return flt
description="""Divides a science frame by the flat frame, handles flexure """, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('flatfits', type=str, help='Flat field fits file') parser.add_argument('toflattenfits', type=str, help='To flatten fits file') parser.add_argument('--flexnpy', type=str, help='Flexure .npy file', default=None) parser.add_argument('--outfile', type=str, help='Output filename', default=None) args = parser.parse_args() flat = pf.open(args.flatfits)[0].data toflat = pf.open(args.toflattenfits) if args.flexnpy is not None: flex = np.load(args.flexnpy)[0] nmToPix = flex['skyline']/240 dX = np.int(np.round(flex['dXnm'] * nmToPix)) dY = np.int(np.round(flex['dYpix'])) print("Rolling by %i/%i" % (dX, dY)) flat = np.roll(flat, dY, 0) flat = np.roll(flat, dX, 1) toflat[0].header["FLATROLL"] = ("%s/%s" % (dX, dY), "Flat field flexure correction") toflat[0].data /= flat toflat[0].header["FLATBY"] = (args.toflattenfits, "Flat field applied") toflat[0].data = toflat[0].data.astype(np.float32) IO.writefits( toflat, args.outfile, clobber=True, no_lossy_compress=True)
def estimateBackground(fine, infile, flex=None, outname=None): if outname is None: print "Need an output name" return dX = 0 dY = 0 infile[0].data = infile[0].data.astype(np.float64) data = infile[0].data.copy() for ff in fine: if not ff.ok: continue if ff.xrange is None: continue if ff.poly is None: continue xs = np.arange(*ff.xrange) ys = np.round(np.poly1d(ff.poly)(xs)).astype(np.int) for dY in xrange(-5, 6): ty = ys.copy() - dY try: data[ty, xs] = np.nan except: pass from astropy.convolution import convolve, convolve_fft, Box2DKernel print "Traditional convolve (pass 1)" k = Box2DKernel(17) flt = data.copy() NaNs = ~np.isfinite(data) OKs = np.isfinite(data) for i in xrange(5): flt = convolve(flt, k) flt[OKs] = data[OKs] #IO.writefits(pf.PrimaryHDU(flt), "test_%i.fits.gz" % i, clobber=True) data[NaNs] = flt[NaNs] fname = os.path.join(os.path.dirname(outname), "lf_" + os.path.basename(outname)) #IO.writefits(data, fname, clobber=True) print "FFT convolve (pass 2)" #k = Box2DKernel(70) #flt = convolve_fft(data, k) flt = gaussian_filter(data, 100) fname = os.path.join(os.path.dirname(outname), "bgd_" + os.path.basename(outname)) HDU = pf.PrimaryHDU(flt) IO.writefits(HDU, fname, clobber=True) fname = os.path.join(os.path.dirname(outname), "bs_" + os.path.basename(outname)) infile[0].header["BGDSUB"] = "Background subtracted with %s" % fname infile[0].data -= flt IO.writefits(infile, fname, clobber=True) return flt
def estimateBackground(fine, infile, gausswidth=100, outname=None): if outname is None: print "Need an output name" return infile[0].data = infile[0].data.astype(np.float64) data = infile[0].data.copy() for ff in fine: if not ff.ok: continue if ff.xrange is None: continue if ff.poly is None: continue xs = np.arange(*ff.xrange) ys = np.round(np.poly1d(ff.poly)(xs)).astype(np.int) for dY in xrange(-5, 6): ty = ys.copy() - dY try: data[ty, xs] = np.nan except: pass from astropy.convolution import convolve, convolve_fft, Box2DKernel print "Traditional convolve (pass 1)" k = Box2DKernel(17) flt = data.copy() nans = ~np.isfinite(data) oks = np.isfinite(data) for i in xrange(5): flt = convolve(flt, k) flt[oks] = data[oks] print "\tIteration %d of 5" % (i+1) # IO.writefits(pf.PrimaryHDU(flt), "test_%i.fits.gz" % i, clobber=True) data[nans] = flt[nans] # fname = os.path.join(os.path.dirname(outname), # "lf_" + os.path.basename(outname)) # IO.writefits(data, fname, clobber=True) # print "FFT convolve (pass 2)" print "Gaussian filter with width = %d (pass 2)" % gausswidth # k = Box2DKernel(70) # flt = convolve_fft(data, k) flt = gaussian_filter(data, gausswidth) ofname = os.path.join(os.path.dirname(outname), "bgd_" + os.path.basename(outname)) HDU = pf.PrimaryHDU(flt) HDU.header["GAUFWID"] = (gausswidth, 'Gaussian filter width in pixels') IO.writefits(HDU, ofname, clobber=True) print "Background image in %s" % ofname + ".gz" infile[0].header["BGDSUB"] = "Background subtracted using %s" % ofname infile[0].header["GAUFWID"] = (gausswidth, 'Gaussian filter width in pixels') ofname = os.path.join(os.path.dirname(outname), "bs_" + os.path.basename(outname)) infile[0].data -= flt IO.writefits(infile, ofname, clobber=True) print "Subtracted image in %s" % ofname + ".gz" return flt
def estimateBackground(fine, infile, gausswidth=100, outname=None): if outname is None: print "Need an output name" return infile[0].data = infile[0].data.astype(np.float64) data = infile[0].data.copy() for ff in fine: if not ff.ok: continue if ff.xrange is None: continue if ff.poly is None: continue xs = np.arange(*ff.xrange) ys = np.round(np.poly1d(ff.poly)(xs)).astype(np.int) for dY in xrange(-5, 6): ty = ys.copy() - dY try: data[ty, xs] = np.nan except: pass from astropy.convolution import convolve, convolve_fft, Box2DKernel print "Traditional convolve (pass 1)" k = Box2DKernel(17) flt = data.copy() nans = ~np.isfinite(data) oks = np.isfinite(data) for i in xrange(5): flt = convolve(flt, k) flt[oks] = data[oks] print "\tIteration %d of 5" % (i + 1) # IO.writefits(pf.PrimaryHDU(flt), "test_%i.fits.gz" % i, clobber=True) data[nans] = flt[nans] # fname = os.path.join(os.path.dirname(outname), # "lf_" + os.path.basename(outname)) # IO.writefits(data, fname, clobber=True) # print "FFT convolve (pass 2)" print "Gaussian filter with width = %d (pass 2)" % gausswidth # k = Box2DKernel(70) # flt = convolve_fft(data, k) flt = gaussian_filter(data, gausswidth) ofname = os.path.join(os.path.dirname(outname), "bgd_" + os.path.basename(outname)) HDU = pf.PrimaryHDU(flt) HDU.header["GAUFWID"] = (gausswidth, 'Gaussian filter width in pixels') IO.writefits(HDU, ofname, clobber=True) print "Background image in %s" % ofname + ".gz" infile[0].header["BGDSUB"] = "Background subtracted using %s" % ofname infile[0].header["GAUFWID"] = (gausswidth, 'Gaussian filter width in pixels') ofname = os.path.join(os.path.dirname(outname), "bs_" + os.path.basename(outname)) infile[0].data -= flt IO.writefits(infile, ofname, clobber=True) print "Subtracted image in %s" % ofname + ".gz" return flt
''', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('flatfits', type=str, help='Flat field fits file') parser.add_argument('toflattenfits', type=str, help='To flatten fits file') parser.add_argument('--flexnpy', type=str, help='Flexure .npy file', default=None) parser.add_argument('--outfile', type=str, help='Output filename', default=None) args = parser.parse_args() flat = pf.open(args.flatfits)[0].data toflat = pf.open(args.toflattenfits) if args.flexnpy is not None: flex = np.load(args.flexnpy)[0] nmToPix = flex['skyline']/240 dX = np.int(np.round(flex['dXnm'] * nmToPix)) dY = np.int(np.round(flex['dYpix'])) print "Rolling by %i/%i" % (dX, dY) flat = np.roll(flat, dY, 0) flat = np.roll(flat, dX, 1) toflat[0].header["FLATROLL"] = ("%s/%s" % (dX, dY), "Flat field flexure correction") toflat[0].data /= flat toflat[0].header["FLATBY"] = (args.toflattenfits, "Flat field applied") toflat[0].data = toflat[0].data.astype(np.float64) IO.writefits( toflat, args.outfile, clobber=True, no_lossy_compress=True)
def estimate_background(fine, infile, gausswidth=100, outname=None, outint=False, fft_filt=False): if outname is None: print("Need an output name") return infile[0].data = infile[0].data.astype(np.float32) data = infile[0].data.copy() # loop over each trace for ff in fine: if not ff.ok and not ff.bkg_ok: continue if ff.xrange is None: continue if ff.poly is None: continue # get trace spatial ranges (xs, ys) xs = np.arange(*ff.xrange) ys = np.round(np.poly1d(ff.poly)(xs)).astype(np.int) # mask above and below each trace with nan's for dY in range(-4, 5): ty = ys.copy() - dY try: data[ty, xs] = np.nan except: pass from astropy.convolution import convolve, convolve_fft, Box2DKernel print("Traditional convolve (pass 1)") # get convolution kernel k = Box2DKernel(17) # start with original masked image for background bkg = data.copy() # write out starting image if requested if outint: IO.writefits(pf.PrimaryHDU(bkg), "test_0.fits", clobber=True) print("Wrote test_0.fits.gz") # keep track of nans nans = ~np.isfinite(data) # good data oks = np.isfinite(data) # iterate five times to remove object light from bkg for i in range(5): # convolve entire image bkg = convolve(bkg, k) # replace background light bkg[oks] = data[oks] print("\tIteration %d of 5" % (i + 1)) # write out each iteration if requested if outint: IO.writefits(pf.PrimaryHDU(bkg), "test_%i.fits" % (i + 1), clobber=True) print("Wrote test_%i.fits.gz" % i) # insert iterative smoothed object pixels data[nans] = bkg[nans] # any remaining nans? nans = ~np.isfinite(data) # fill them in with median value if np.count_nonzero(nans) > 0: data[nans] = np.nanmedian(data[oks]) # write out intermediate result if requested if outint: fname = os.path.join(os.path.dirname(outname), "lf_" + os.path.basename(outname)) IO.writefits(data, fname, clobber=True) print("Wrote %s" % fname + ".gz") # use FFT filter if requested if fft_filt: print("FFT convolve (pass 2)") k = Box2DKernel(70) bkg = convolve_fft(data, k) # else, use gaussian filter of requested width in pixels else: print("Gaussian filter with width = %d (pass 2)" % gausswidth) bkg = gaussian_filter(data, gausswidth) # write resulting background to a gzipped fits file ofname = os.path.join(os.path.dirname(outname), "bgd_" + os.path.basename(outname)) HDU = pf.PrimaryHDU(bkg) HDU.header["GAUFWID"] = (gausswidth, 'Gaussian filter width in pixels') IO.writefits(HDU, ofname, clobber=True) print("Background image in %s" % ofname + ".gz") # record which file in output header infile[0].header["BGDSUB"] = "Background subtracted using %s" % ofname infile[0].header["GAUFWID"] = (gausswidth, 'Gaussian filter width in pixels') ofname = os.path.join(os.path.dirname(outname), "bs_" + os.path.basename(outname)) # subtract background infile[0].data -= bkg # write out the resulting fits file IO.writefits(infile, ofname, clobber=True) print("Subtracted image in %s" % ofname + ".gz") return bkg