def stack_rectified(wavenames, maskname, band, wavops): N = len(wavenames) lamnames = [] suffixs = [] for i in xrange(N): lamnames.append(Wavelength.filelist_to_wavename(wavenames[i], band, maskname, wavops).rstrip(".fits")) suffixs.append(lamnames[i].lstrip("wave_stack_%s_" % band)) path = os.path.join(wavops["outdir"], maskname) recs = [] ivars = [] sns = [] try: ls = [get_path(os.path.join(path, "eps_%s_%s_%s.fits") % (maskname, suffix, band)) for suffix in suffixs] imcombine(ls, maskname, "eps_%s_%s.fits" % (maskname, band), wavops, "rate") except: pass try: ls = [get_path(os.path.join(path, "ivars_%s_%s_%s.fits") % (maskname, suffix, band)) for suffix in suffixs] imcombine(ls, maskname, "ivars_%s_%s.fits" % (maskname, band), wavops, "ivar-rate") except: pass try: ls = [get_path(os.path.join(path, "snrs_%s_%s_%s.fits") % (maskname, suffix, band)) for suffix in suffixs] imcombine(ls, maskname, "snrs_%s_%s.fits" % (maskname, band), wavops, "snr-rate") except: pass
def rename_files(wavenames, maskname, band, wavops): lamname = Wavelength.filelist_to_wavename(wavenames[0], band, maskname, wavops).rstrip(".fits") suffix = lamname.lstrip("wave_stack_%s_" % band) path = os.path.join(wavops["outdir"], maskname) fnames = ["rectified_%s%s.fits", "rectified_ivar_%s%s.fits", "rectified_sn_%s%s.fits"] for fname in fnames: try: a = get_path(os.path.join(path, fname % (band, "_" + suffix))) b = os.path.join(path, fname % (band, "")) + gz(a) os.rename(a, b) except: print "Ignoring renaming of: ", fname pass edges = IO.load_edges(maskname, band, wavops) n_slits = len(edges[0]) for i in xrange(1, n_slits + 1): S = "S%2.2i" % (i) a = get_path(os.path.join(path, "eps_%s_%s_%s.fits" % (band, suffix, S))) a_h = pf.open(a)[0].header obj = a_h["object"] b = os.path.join(path, "%s_%s_%s_eps.fits" % (maskname, band, obj)) + gz(a) os.rename(a, b) a = get_path(os.path.join(path, "ivar_%s_%s_%s.fits" % (band, suffix, S))) a_h = pf.open(a)[0].header obj = a_h["object"] b = os.path.join(path, "%s_%s_%s_ivar.fits" % (maskname, band, obj)) + gz(a) os.rename(a, b) a = get_path(os.path.join(path, "eps_%s_%s_%s.fits" % (maskname, suffix, band))) b = os.path.join(path, "%s_%s_eps.fits" % (maskname, band)) + gz(a) os.rename(a, b) a = get_path(os.path.join(path, "snrs_%s_%s_%s.fits" % (maskname, suffix, band))) b = os.path.join(path, "%s_%s_snrs.fits" % (maskname, band)) + gz(a) os.rename(a, b) a = get_path(os.path.join(path, "ivars_%s_%s_%s.fits" % (maskname, suffix, band))) b = os.path.join(path, "%s_%s_ivars.fits" % (maskname, band)) + gz(a) os.rename(a, b)
def stack_rectified(wavenames, maskname, band, wavops): N = len(wavenames) lamnames = [] suffixs = [] for i in xrange(N): lamnames.append( Wavelength.filelist_to_wavename(wavenames[i], band, maskname, wavops).rstrip(".fits")) suffixs.append(lamnames[i].lstrip("wave_stack_%s_" % band)) path = os.path.join(wavops["outdir"], maskname) recs = [] ivars = [] sns = [] try: ls = [ get_path( os.path.join(path, "eps_%s_%s_%s.fits") % (maskname, suffix, band)) for suffix in suffixs ] imcombine(ls, maskname, "eps_%s_%s.fits" % (maskname, band), wavops, 'rate') except: pass try: ls = [ get_path( os.path.join(path, "ivars_%s_%s_%s.fits") % (maskname, suffix, band)) for suffix in suffixs ] imcombine(ls, maskname, "ivars_%s_%s.fits" % (maskname, band), wavops, 'ivar-rate') except: pass try: ls = [ get_path( os.path.join(path, "snrs_%s_%s_%s.fits") % (maskname, suffix, band)) for suffix in suffixs ] imcombine(ls, maskname, "snrs_%s_%s.fits" % (maskname, band), wavops, 'snr-rate') except: pass
def printWavelengthFit(self): if self.type is 'longslit' or self.type is 'long2pos': addLongSlit = ",longslit=longslit" else: addLongSlit = "" if self.type is 'slitmask' or self.type is 'longslit': self.useNeon = False self.useArgon = False # determine is Argon and Neon files contain data for K bands if self.isEmpty('Ar.txt') is False and self.band is 'K': self.useArgon = True if self.isEmpty('Ne.txt') is False and self.band is 'K': self.useNeon = True self.addLine( "Wavelength.imcombine(obsfiles, maskname, band, waveops)") if self.useArgon: self.addLine( "Wavelength.imcombine('Ar.txt', maskname, band, waveops)") if self.useNeon: self.addLine( "Wavelength.imcombine('Ne.txt', maskname, band, waveops)") self.addLine( "Wavelength.fit_lambda_interactively(maskname, band, obsfiles,waveops" + addLongSlit + ", noninteractive=noninteractiveflag)") if self.useArgon: self.addLine( "Wavelength.apply_interactive(maskname, band, waveops, apply=obsfiles, to='Ar.txt', argon=True)" ) if self.useNeon: self.addLine( "Wavelength.apply_interactive(maskname, band, waveops, apply=obsfiles, to='Ne.txt', neon=True)" ) self.addLine( "Wavelength.fit_lambda(maskname, band, obsfiles, obsfiles,waveops" + addLongSlit + ")") if self.useArgon and self.useNeon: self.addLine( "Wavelength.fit_lambda(maskname, band, 'Ne.txt', 'Ne.txt',waveops, wavenames2='Ar.txt'" + addLongSlit + ")") if self.useArgon and not self.useNeon: self.addLine( "Wavelength.fit_lambda(maskname, band, 'Ar.txt', 'Ar.txt',waveops" + addLongSlit + ")") if self.useNeon and not self.useArgon: self.addLine( "Wavelength.fit_lambda(maskname, band, 'Ne.txt', 'Ne.txt',waveops" + addLongSlit + ")") if self.useNeon or self.useArgon: self.addLine("LROI = [[21000,22800]]*1") if self.useNeon: self.addLine( "LROIs = Wavelength.check_wavelength_roi(maskname, band, obsfiles, 'Ne.txt', LROI, waveops)" ) if self.useArgon and not self.useNeon: self.addLine( "LROIs = Wavelength.check_wavelength_roi(maskname, band, obsfiles, 'Ar.txt', LROI, waveops)" ) self.addLine( "Wavelength.apply_lambda_simple(maskname, band, obsfiles, waveops" + addLongSlit + ")") if self.useArgon and self.useNeon: self.addLine( "Wavelength.apply_lambda_sky_and_arc(maskname, band, obsfiles, 'Ne.txt', LROIs, waveops)" ) if self.useArgon and not self.useNeon: self.addLine( "Wavelength.apply_lambda_sky_and_arc(maskname, band, obsfiles, 'Ar.txt', LROIs, waveops)" ) if self.useNeon and not self.useArgon: self.addLine( "Wavelength.apply_lambda_sky_and_arc(maskname, band, obsfiles, 'Ne.txt', LROIs, waveops)" ) # determine waveleng name files = IO.list_file_to_strings(self.offsetFiles) if self.useNeon: neon_files = IO.list_file_to_strings('Ne.txt') self.waveName = "merged_lambda_solution_" + str( Wavelength.filelist_to_wavename( files, self.band, self.maskName, "")).rstrip(".fits") + "_and_" + str( Wavelength.filelist_to_wavename( neon_files, self.band, self.maskName, "")) elif self.useArgon and not self.useNeon: argon_files = IO.list_file_to_strings('Ar.txt') self.waveName = "merged_lambda_solution_" + str( Wavelength.filelist_to_wavename( files, self.band, self.maskName, "")).rstrip(".fits") + "_and_" + str( Wavelength.filelist_to_wavename( argon_files, self.band, self.maskName, "")) else: self.waveName = "lambda_solution_" + str( Wavelength.filelist_to_wavename(files, self.band, self.maskName, "")) if self.type is 'long2pos' or self.type is 'long2pos_specphot': calibWith = "" if self.isEmpty('Ar.txt') is False: self.addLine("argon = ['Ar.txt']") calibWith = "argon" waveFiles = IO.list_file_to_strings('Ar.txt') if self.isEmpty('Ne.txt') is False: self.addLine("neon = ['Ne.txt']") calibWith = "neon" waveFiles = IO.list_file_to_strings('Ne.txt') if calibWith: # we have either Argon, or Neon, or both, so we can use arcs for the reduction self.addLine("Wavelength.imcombine(" + str(calibWith) + ", maskname, band, waveops)") self.addLine( "Wavelength.fit_lambda_interactively(maskname, band, " + str(calibWith) + ",waveops,longslit=longslit, " + str(calibWith) + "=True, noninteractive=noninteractiveflag)") self.addLine("Wavelength.fit_lambda(maskname, band, " + str(calibWith) + "," + str(calibWith) + ",waveops,longslit=longslit)") self.addLine( "Wavelength.apply_lambda_simple(maskname, band, " + str(calibWith) + ", waveops, longslit=longslit, smooth=True)") self.waveName = "lambda_solution_" + str( Wavelength.filelist_to_wavename(waveFiles, self.band, self.maskName, "")) else: # we have no arcs. For the time being, we can try with sky lines but this only works with long2pos specphot print "#####################################################################################################" print "WARNING: There are no arc calibration files" print " The pipeline will try to use sky lines but this only works if the observation is long enough" print " and if you are only using long2pos. It will NOT work on long2pos_specphot" print " Please contact the MosfireDRP team to obtain a standard wavelength solution" print "#####################################################################################################" self.addLine( "obsfiles = obsfiles_posAnarrow + obsfiles_posCnarrow") self.addLine( "Wavelength.imcombine(obsfiles, maskname, band, waveops)") self.addLine( "Wavelength.fit_lambda_interactively(maskname, band, obsfiles ,waveops,longslit=longslit, noninteractive=noninteractiveflag)" ) self.addLine( "Wavelength.fit_lambda(maskname, band, obsfiles,obsfiles ,waveops,longslit=longslit)" ) self.addLine( "Wavelength.apply_lambda_simple(maskname, band, obsfiles, waveops, longslit=longslit, smooth=True)" ) files = IO.list_file_to_strings(self.offsetFiles) self.waveName = "lambda_solution_" + str( Wavelength.filelist_to_wavename(files, self.band, self.maskName, "")) self.addLine("") self.addLine("Wavelength_file = '" + str(self.waveName) + "'") self.addLine("")
def printWavelengthFit(self): if self.type is 'longslit' or self.type is 'long2pos': addLongSlit = ",longslit=longslit" else: addLongSlit = "" if self.type is 'slitmask' or self.type is 'longslit': self.useNeon = False self.useArgon = False # determine is Argon and Neon files contain data for K bands if self.isEmpty('Ar.txt') is False and self.band is 'K': self.useArgon = True if self.isEmpty('Ne.txt') is False and self.band is 'K': self.useNeon = True self.addLine("Wavelength.imcombine(obsfiles, maskname, band, waveops)") if self.useArgon: self.addLine("Wavelength.imcombine('Ar.txt', maskname, band, waveops)") if self.useNeon: self.addLine("Wavelength.imcombine('Ne.txt', maskname, band, waveops)") self.addLine("Wavelength.fit_lambda_interactively(maskname, band, obsfiles,waveops"+addLongSlit+", bypass=bypassflag)") if self.useArgon: self.addLine("Wavelength.apply_interactive(maskname, band, waveops, apply=obsfiles, to='Ar.txt', argon=True)") if self.useNeon: self.addLine("Wavelength.apply_interactive(maskname, band, waveops, apply=obsfiles, to='Ne.txt', neon=True)") self.addLine("Wavelength.fit_lambda(maskname, band, obsfiles, obsfiles,waveops"+addLongSlit+")") if self.useArgon and self.useNeon: self.addLine("Wavelength.fit_lambda(maskname, band, 'Ne.txt', 'Ne.txt',waveops, wavenames2='Ar.txt'"+addLongSlit+")") if self.useArgon and not self.useNeon: self.addLine("Wavelength.fit_lambda(maskname, band, 'Ar.txt', 'Ar.txt',waveops"+addLongSlit+")") if self.useNeon and not self.useArgon: self.addLine("Wavelength.fit_lambda(maskname, band, 'Ne.txt', 'Ne.txt',waveops"+addLongSlit+")") if self.useNeon or self.useArgon: self.addLine("LROI = [[21000,22800]]*1") if self.useNeon: self.addLine("LROIs = Wavelength.check_wavelength_roi(maskname, band, obsfiles, 'Ne.txt', LROI, waveops)") if self.useArgon and not self.useNeon: self.addLine("LROIs = Wavelength.check_wavelength_roi(maskname, band, obsfiles, 'Ar.txt', LROI, waveops)") self.addLine("Wavelength.apply_lambda_simple(maskname, band, obsfiles, waveops"+addLongSlit+")") if self.useArgon and self.useNeon: self.addLine("Wavelength.apply_lambda_sky_and_arc(maskname, band, obsfiles, 'Ne.txt', LROIs, waveops)") if self.useArgon and not self.useNeon: self.addLine("Wavelength.apply_lambda_sky_and_arc(maskname, band, obsfiles, 'Ar.txt', LROIs, waveops)") if self.useNeon and not self.useArgon: self.addLine("Wavelength.apply_lambda_sky_and_arc(maskname, band, obsfiles, 'Ne.txt', LROIs, waveops)") # determine waveleng name files = IO.list_file_to_strings(self.offsetFiles) if self.useNeon: neon_files = IO.list_file_to_strings('Ne.txt') self.waveName = "merged_lambda_solution_"+str(Wavelength.filelist_to_wavename(files, self.band, self.maskName,"")).rstrip(".fits")+"_and_"+str(Wavelength.filelist_to_wavename(neon_files, self.band, self.maskName,"")) elif self.useArgon and not self.useNeon: argon_files = IO.list_file_to_strings('Ar.txt') self.waveName = "merged_lambda_solution_"+str(Wavelength.filelist_to_wavename(files, self.band, self.maskName,"")).rstrip(".fits")+"_and_"+str(Wavelength.filelist_to_wavename(argon_files, self.band, self.maskName,"")) else: self.waveName = "lambda_solution_"+str(Wavelength.filelist_to_wavename(files, self.band, self.maskName,"")) if self.type is 'long2pos' or self.type is 'long2pos_specphot': calibWith = "" if self.isEmpty('Ar.txt') is False: self.addLine("argon = ['Ar.txt']") calibWith = "argon" waveFiles = IO.list_file_to_strings('Ar.txt') if self.isEmpty('Ne.txt') is False: self.addLine("neon = ['Ne.txt']") calibWith = "neon" waveFiles = IO.list_file_to_strings('Ne.txt') if calibWith: # we have either Argon, or Neon, or both, so we can use arcs for the reduction self.addLine("Wavelength.imcombine("+str(calibWith)+", maskname, band, waveops)") self.addLine("Wavelength.fit_lambda_interactively(maskname, band, "+str(calibWith)+",waveops,longslit=longslit, "+str(calibWith)+"=True, bypass=bypassflag)") self.addLine("Wavelength.fit_lambda(maskname, band, "+str(calibWith)+","+str(calibWith)+",waveops,longslit=longslit)") self.addLine("Wavelength.apply_lambda_simple(maskname, band, "+str(calibWith)+", waveops, longslit=longslit, smooth=True)") self.waveName = "lambda_solution_"+str(Wavelength.filelist_to_wavename(waveFiles, self.band, self.maskName,"")) else: # we have no arcs. For the time being, we can try with sky lines but this only works with long2pos specphot print "#####################################################################################################" print "WARNING: There are no arc calibration files" print " The pipeline will try to use sky lines but this only works if the observation is long enough" print " and if you are only using long2pos. It will NOT work on long2pos_specphot" print " Please contact the MosfireDRP team to obtain a standard wavelength solution" print "#####################################################################################################" self.addLine("obsfiles = obsfiles_posAnarrow + obsfiles_posCnarrow") self.addLine("Wavelength.imcombine(obsfiles, maskname, band, waveops)") self.addLine("Wavelength.fit_lambda_interactively(maskname, band, obsfiles ,waveops,longslit=longslit, bypass=bypassflag)") self.addLine("Wavelength.fit_lambda(maskname, band, obsfiles,obsfiles ,waveops,longslit=longslit)") self.addLine("Wavelength.apply_lambda_simple(maskname, band, obsfiles, waveops, longslit=longslit, smooth=True)") files = IO.list_file_to_strings(self.offsetFiles) self.waveName = "lambda_solution_"+str(Wavelength.filelist_to_wavename(files, self.band, self.maskName,"")) self.addLine("") self.addLine("Wavelength_file = '"+str(self.waveName)+"'") self.addLine("Wavelength.bary_corr(obsfiles, Wavelength_file, maskname, band, waveops)") self.addLine("")
def rename_files(wavenames, maskname, band, wavops): lamname = Wavelength.filelist_to_wavename(wavenames[0], band, maskname, wavops).rstrip(".fits") suffix = lamname.lstrip("wave_stack_%s_" % band) path = os.path.join(wavops["outdir"], maskname) fnames = ["rectified_%s%s.fits", "rectified_ivar_%s%s.fits", "rectified_sn_%s%s.fits"] for fname in fnames: try: a = get_path(os.path.join(path, fname % (band, "_" + suffix))) b = os.path.join(path, fname % (band, "")) + gz(a) os.rename(a, b) except: print "Ignoring renaming of: ", fname pass edges = IO.load_edges(maskname, band, wavops) n_slits = len(edges[0]) for i in xrange(1, n_slits+1): S = "S%2.2i" % (i) a = get_path(os.path.join(path, "eps_%s_%s_%s.fits" % (band, suffix, S))) a_h = pf.open(a)[0].header obj = a_h['object'] b = os.path.join(path, "%s_%s_%s_eps.fits" % (maskname, band, obj)) + \ gz(a) os.rename(a,b) a = get_path(os.path.join(path, "ivar_%s_%s_%s.fits" % (band, suffix, S))) a_h = pf.open(a)[0].header obj = a_h['object'] b = os.path.join(path, "%s_%s_%s_ivar.fits" % (maskname, band, obj)) + \ gz(a) os.rename(a,b) a = get_path(os.path.join(path, "eps_%s_%s_%s.fits" % (maskname, suffix, band))) b = os.path.join(path, "%s_%s_eps.fits" % (maskname, band)) + gz(a) os.rename(a,b) a = get_path(os.path.join(path, "snrs_%s_%s_%s.fits" % (maskname, suffix, band))) b = os.path.join(path, "%s_%s_snrs.fits" % (maskname, band)) + gz(a) os.rename(a, b) a = get_path(os.path.join(path, "ivars_%s_%s_%s.fits" % (maskname, suffix, band))) b = os.path.join(path, "%s_%s_ivars.fits" % (maskname, band)) + gz(a) os.rename(a, b)
def go(maskname, band, filenames, wavefile, wavoptions, longoptions, use_flat=False): ''' The go command is the main entry point into this module. Inputs: maskname: String of the mask name band: String of 'Y', 'J', 'H', or 'K' filenames: List of filenames to reduce wavefile: String of path to FITS file with the wavelength solution wavoptions: The Wavelength Options dictionary longoptions: Dictionary containing: {'yrange': The pixel range to extract over 'row_position': The row to solve the initial wavelength solution on} use_flat: Boolean False [default] means to use no flat field Boolean True means to divide by the pixelflat ''' wavename = Wavelength.filelist_to_wavename(filenames, band, maskname, wavoptions).rstrip(".fits") print "Wavefile: {0}".format(wavefile) lamhdr, lamdat = IO.readfits(wavefile) positions = [] objname = None for listfile in filenames: fnames = IO.list_file_to_strings(listfile) if len(fnames) != 1: raise Exception("I currently expect only one file per position. Remove multiple entries and try again") header, data, bs = IO.readmosfits(fnames[0], wavoptions) if objname is None: objname = header["object"] if objname != header["object"]: print ("Trying to combine longslit stack of object {0} " "with object {1}".format(objname, header["object"])) print("{0:18s} {1:30s} {2:2s} {3:4.1f}".format(file, header["object"], header["frameid"], header["yoffset"])) positions.append([fnames[0], header, data, bs]) print("{0:2g} nod positions found. Producing stacked difference" \ " image.".format(len(positions))) for i in xrange(len(positions)-1): A = positions[i] B = positions[i+1] print("----------- -----".format(A,B)) dname, varname = imdiff(A, B, maskname, band, header, wavoptions) if use_flat: apply_flat(dname, maskname, band) apply_flat(varname, maskname, band) rectify(dname, lamdat, A, B, maskname, band, wavoptions, longoptions) rectify(varname, lamdat, A, B, maskname, band, wavoptions, longoptions) print dname dname, vname = imdiff(B, A, maskname, band, header, wavoptions) if use_flat: apply_flat(dname, maskname, band) apply_flat(vname, maskname, band) rectify(dname, lamdat, B, A, maskname, band, wavoptions, longoptions) rectify(vname, lamdat, B, A, maskname, band, wavoptions, longoptions) if False: fname = os.path.join(path, wavename + ".fits") B = IO.readfits(fname) B = [fname, B[0], B[1]] for i in xrange(len(positions)): A = positions[i] imdiff(A, B, maskname, band, wavoptions) rectify(path, dname, lamdat, A, B, maskname, band, wavoptions, longoptions) imdiff(B, A, maskname, band, wavoptions) rectify(path, dname, lamdat, B, A, maskname, band, wavoptions, longoptions)
def go(maskname, band, filenames, wavefile, wavoptions, longoptions, use_flat=False): ''' The go command is the main entry point into this module. Inputs: maskname: String of the mask name band: String of 'Y', 'J', 'H', or 'K' filenames: List of filenames to reduce wavefile: String of path to FITS file with the wavelength solution wavoptions: The Wavelength Options dictionary longoptions: Dictionary containing: {'yrange': The pixel range to extract over 'row_position': The row to solve the initial wavelength solution on} use_flat: Boolean False [default] means to use no flat field Boolean True means to divide by the pixelflat ''' wavename = Wavelength.filelist_to_wavename(filenames, band, maskname, wavoptions).rstrip(".fits") print "Wavefile: {0}".format(wavefile) lamhdr, lamdat = IO.readfits(wavefile) positions = [] objname = None for listfile in filenames: fnames = IO.list_file_to_strings(listfile) if len(fnames) != 1: raise Exception( "I currently expect only one file per position. Remove multiple entries and try again" ) header, data, bs = IO.readmosfits(fnames[0], wavoptions) if objname is None: objname = header["object"] if objname != header["object"]: print( "Trying to combine longslit stack of object {0} " "with object {1}".format(objname, header["object"])) print("{0:18s} {1:30s} {2:2s} {3:4.1f}".format(file, header["object"], header["frameid"], header["yoffset"])) positions.append([fnames[0], header, data, bs]) print("{0:2g} nod positions found. Producing stacked difference" \ " image.".format(len(positions))) for i in xrange(len(positions) - 1): A = positions[i] B = positions[i + 1] print("----------- -----".format(A, B)) dname, varname = imdiff(A, B, maskname, band, header, wavoptions) if use_flat: apply_flat(dname, maskname, band) apply_flat(varname, maskname, band) rectify(dname, lamdat, A, B, maskname, band, wavoptions, longoptions) rectify(varname, lamdat, A, B, maskname, band, wavoptions, longoptions) print dname dname, vname = imdiff(B, A, maskname, band, header, wavoptions) if use_flat: apply_flat(dname, maskname, band) apply_flat(vname, maskname, band) rectify(dname, lamdat, B, A, maskname, band, wavoptions, longoptions) rectify(vname, lamdat, B, A, maskname, band, wavoptions, longoptions) if False: fname = os.path.join(path, wavename + ".fits") B = IO.readfits(fname) B = [fname, B[0], B[1]] for i in xrange(len(positions)): A = positions[i] imdiff(A, B, maskname, band, wavoptions) rectify(path, dname, lamdat, A, B, maskname, band, wavoptions, longoptions) imdiff(B, A, maskname, band, wavoptions) rectify(path, dname, lamdat, B, A, maskname, band, wavoptions, longoptions)