def calibrate(namelst): iraf.noao() iraf.twodspec() iraf.longslit(dispaxis=2, nsum=1, observatory='ca', extinction=extpath, caldir=stdpath) for fitname in namelst: outname = 'mark_' + fitname if os.path.isfile(outname): print('remove file ' + outname) os.remove(outname) iraf.calibrate(input=fitname, output=outname, extinct='yes', flux='yes', extinction=extpath, ignoreaps='yes', sensitivity='Sens', fnu='no') iraf.splot(images=outname) iraf.flpr()
def wl_cal(filename, outname, cals): print 'run wavelength calibration...' if os.path.isfile(outname): print 'file %s is already exist' % outname else: iraf.imred() iraf.specred() print 'The calibration file is listed below:' for i in xrange(len(cals)): print i, cals[i], get_fits_objname(cals[i]) valget = 0 if len(cals) > 1: valget = raw_input('Which one are you want to use?:') while type(eval(valget)) != int: valget = raw_input('input is not a int type, please reinput:') valget = int(valget) iraf.refspectra(input = filename , references = cals[valget], apertures = '', refaps = '' , ignoreaps = True, select = 'interp', sort = '' , group = '', time = False, timewrap = 17.0 , override = False, confirm = True, assign = True , logfiles = 'STDOUT,logfile', verbose = False, answer = 'yes') print 'make file ' + outname + '...' iraf.dispcor(input = filename , output = outname, linearize = True, database = 'database' , table = '', w1 = 'INDEF', w2 = 'INDEF', dw = 'INDEF' , nw = 'INDEF', log = False, flux = True, blank = 0.0 , samedisp = False, ignoreaps = True , confirm = False, listonly = False, verbose = True , logfile = '') print 'splot %s' % outname iraf.splot(images = outname)
def standard(namelst): iraf.noao() iraf.twodspec() iraf.longslit(dispaxis=2, nsum=1, observatory=func.obs.name, extinction=extpath, caldir=stdpath) if os.path.isfile('Std'): print('remove file Std') os.remove('Std') for std_fitsname in namelst: stdname, stdmag, stdmagband = func.standard_star_info(std_fitsname) print(colored('the standard star is ' + stdname, 'green')) wid, sep = get_band_width_sep(std_fitsname) airmas = pyfits.getval(std_fitsname, 'airmass') exposure = pyfits.getval(std_fitsname, 'exptime') iraf.standard(input=std_fitsname, output='Std', samestar=True, beam_switch=False, apertures='', bandwidth=wid, bandsep=sep, # 30.0 20.0 fnuzero=3.6800000000000E-20, extinction=extpath, caldir=stdpath, observatory=func.obs.name, interact=True, graphics='stdgraph', cursor='', star_name=stdname, airmass=airmas, exptime=exposure, mag=stdmag, magband=stdmagband, teff='', answer='yes') if os.path.isfile('Sens.fits'): print('remove file Sens.fits') os.remove('Sens.fits') iraf.sensfunc(standards='Std', sensitivity='Sens', extinction=extpath, function='spline3', order=9) iraf.splot('Sens')
def lris_standard(standards, xcorr=yes): '''Extract standard stars and calculate sensitivity functions.''' for ofile, nstd in standards: shutil.copy(ofile, "%s.fits" % nstd) # Extract standard if get_head("%s.fits" % nstd, "SKYSUB"): iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") if xcorr: # Cross correlate to tweak wavelength iraf.xcsao("%s.ms" % nstd, templates=XCTEMPLATE, correlate="wavelength", logfiles="%s.xcsao" % ofile) # If a solution was found, apply shift try: xcsaof = open("%s.xcsao" % ofile) shift = float(xcsaof.readlines()[6].split()[14]) except: shift = 0.0 iraf.specshift("%s.ms" % nstd, -shift) # Create telluric iraf.splot("%s.ms" % nstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % nstd, "/", "a%s.ms" % nstd, "telluric.%s.fits" % nstd) iraf.imreplace("telluric.%s.fits" % nstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.%s.fits" % nstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % nstd, "s%s.ms" % nstd, 5.0) iraf.sarith("%s.ms" % nstd, "/", "s%s.ms" % nstd, "ds%s.ms" % nstd) # Apply telluric correction iraf.telluric("ds%s.ms" % nstd, "tds%s.ms" % nstd, "telluric.%s.fits" % nstd, xcorr=no, tweakrms=no, interactive=no, sample='4000:4010,6850:6975,7150:7350,7575:7725,8050:8400,8900:9725') # Define bandpasses for standard star calculation obj=get_head("%s.fits" % nstd, "OBJECT") iraf.standard("tds%s.ms" % nstd, "%s.std" % nstd, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=obj, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % nstd, '%s.sens' % nstd, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def SplotTask(self, InputFile, Fits_Folder, xmin = 'INDEF', xmax = 'INDEF', ymin = 'INDEF', ymax='INDEF'): splot_conf = self.SplotAttributes(InputFile, Fits_Folder,xmin, xmax, ymin, ymax) iraf.splot(**splot_conf) return
def get_cal_spec_line(filename1, filename2): print 'Run get_cal_spec_line, apall...' outname = 'a' + filename1 if os.path.isfile(outname): print outname, 'is already exist' else: print 'display %s 1' % filename1 iraf.display(image = filename1, frame = 1) iraf.apall(input = filename1 , output = outname, apertures = '', format = 'multispec' , references = filename2, profiles = '', interactive = True , find = False, recenter = False, resize = False , edit = False, trace = False, fittrace = False , extract = True, extras = False, review = True , line = 'INDEF', nsum = 10, lower = -15.0 , upper = 15.0, apidtable = '', b_function = 'chebyshev' , b_order = 1, b_sample = '-30:-20,20:30', b_naverage = -3 , b_niterate = 0, b_low_reject = 3.0, b_high_reject = 3.0 , b_grow = 0.0, width = 5.0, radius = 10.0 , threshold = 0.0, nfind = 1, minsep = 5.0 , maxsep = 1000.0, order = 'increasing', aprecenter = '' , npeaks = 'INDEF', shift = True, llimit ='INDEF' , ulimit = 'INDEF', ylevel = 0.1, peak = True , bkg = True, r_grow = 0.0, avglimits = False , t_nsum = 10, t_step = 10, t_nlost = 3, t_function = 'spline3' , t_order = 3, t_sample = '*', t_naverage = 1 , t_niterate = 0, t_low_reject = 3.0, t_high_reject = 3.0 , t_grow = 0.0, background = 'none', skybox = 1 , weights = 'none', pfit = 'fit1d', clean = False , saturation = 'INDEF', readnoise = 9.4, gain = 0.35 , lsigma = 4.0, usigma = 4.0, nsubaps = 1) print 'splot %s' % outname iraf.splot(images = outname) return outname
def calibrate(scifiles, extfile, observatory): for f in scifiles: redorblue = getredorblue(f) iraf.unlearn(iraf.gscalibrate) iraf.gscalibrate('et' + f[:-4] + '.fits', sfunc='sens' + redorblue + '.fits', fl_ext=True, fl_vardq=dodq, extinction=extfile, observatory=observatory) if os.path.exists('cet' + f[:-4] + '.fits'): iraf.unlearn(iraf.splot) iraf.splot('cet' + f.replace('.txt', '.fits') + '[sci]') # just to check
def calibrate(scifiles, extfile, observatory): for f in scifiles: redorblue = getredorblue(f) iraf.unlearn(iraf.gscalibrate) iraf.gscalibrate('et' + f[:-4] + '.fits', sfunc='sens' + redorblue + '.fits', fl_ext=True, extinction=extfile, observatory=observatory) if os.path.exists('cet' + f[:-4] + '.fits'): iraf.unlearn(iraf.splot) iraf.splot('cet' + f.replace('.txt', '.fits') + '[sci]') # just to check
def standard(namelst): iraf.noao() iraf.twodspec() iraf.longslit(dispaxis=2, nsum=1, observatory='ca', extinction=extpath, caldir=stdpath) std_fitsname = namelst[0] stdname, stdmag, stdmagband = standard_star_info(std_fitsname) wid, sep = get_band_width_sep(stdname) print('<<<<<the standard star is ', stdname, '>>>>>') print std_fitsname if os.path.isfile('Std'): print('remove file Std') os.remove('Std') iraf.standard( input=std_fitsname, output='Std', samestar=True, beam_switch=False, apertures='', bandwidth=wid, bandsep=sep, # 30.0 20.0 fnuzero=3.6800000000000E-20, extinction=extpath, caldir=stdpath, observatory='ca', interact=True, graphics='stdgraph', cursor='', star_name=stdname, airmass='', exptime='', mag=stdmag, magband=stdmagband, teff='', answer='yes') if os.path.isfile('Sens.fits'): print('remove file Sens.fits') os.remove('Sens.fits') iraf.sensfunc(standards='Std', sensitivity='Sens', extinction=extpath, function='spline3', order=15) iraf.splot('Sens') iraf.flpr()
def sumFibers(self, inIm, **kwargs): print "\nSUMMING FIBERS FROM " + inIm # delete previous summed spectra # TODO: add attribute to class to turn imdelete verifications on/off iraf.imdelete("a" + inIm) # sum the fibers and view the final spectrum iraf.gfapsum(inIm, **kwargs) iraf.splot("a" + inIm + "[SCI,1]") return
def linefitManual(spectrum, band): """ Enter splot so the user can fit and subtract lorents (or, actually, any) profiles """ iraf.splot(images=spectrum, new_image='final_tel_no_hlines_no_norm', save_file='../PRODUCTS/lorentz_hlines.txt', overwrite='yes') # it's easy to forget to use the 'i' key to actually write out the line-free spectrum, so check that it exists: # with the 'tweak' options, the line-free spectrum will already exists, so this lets the user simply 'q' and move on w/o editing (too bad if they edit and forget to hit 'i'...) while True: try: with open("final_tel_no_hlines_no_norm.fits") as f: pass break except IOError as e: logging.info("It looks as if you didn't use the i key to write out the lineless spectrum. We'll have to try again. --> Re-entering splot") iraf.splot(images=spectrum, new_image='final_tel_no_hlines_no_norm', save_file='../PRODUCTS/lorentz_hlines.txt', overwrite='yes')
def sensfunc(standards, obs): """ sensfunc -- Determine sensitivity and extinction functions sensfunc standards sensitivity """ import glob import os, sys from pyraf import iraf iraf.noao() iraf.onedspec() output_sens = "sens" iraf.sensfunc(standards=standards, sensitivity=output_sens, extinct='/iraf/iraf/noao/lib/onedstds/ctioextinct.dat', observatory=obs) iraf.splot(output_sens)
def calibrate(lstfile): stdpath = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'standarddir' + os.sep extpath = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'LJextinct.dat' iraf.noao() iraf.twodspec() iraf.longslit(dispaxis = 2, nsum = 1, observatory = 'Lijiang', extinction = extpath, caldir = stdpath) f = open(lstfile) l = f.readlines() f.close() l = [tmp.split('\n')[0] for tmp in l] for fitname in l: stdobjname = select_std(fitname) stdfitname = 'sensawftbo' + stdgroup[stdobjname][0] iraf.calibrate(input = 'awftbo'+ fitname, output = 'mark_awftbo' + fitname, extinct = 'yes', flux = 'yes', extinction = extpath, ignoreaps = 'yes', sensitivity = stdfitname, fnu = 'no') iraf.splot(images = 'mark_awftbo' + fitname)
def dispcor(imlist_name, database='database'): """ dispcor -- Dispersion correct and resample spectra dispcor input output [records] database -- path in which idXXX file. ex) /data1/SN2019ein/work/SAO_Spectrum/red/20190509/arc/database/ dispcor fcdbstd.ms.fits wfcdbstd.ms.fits """ import glob import os, sys from pyraf import iraf iraf.noao() iraf.imred() iraf.kpnoslit() imlist = glob.glob(imlist_name) imlist.sort() for i in range(len(imlist)): inim = imlist[i] iraf.dispcor(input=inim, output='w' + inim, database='database', linearize='no') iraf.splot(images='w' + inim)
def combinespecs(inputre, scale='exposure', rdnoise='rdnoise', gain='gain'): ''' combine two or more spectra tha matches the input regular expression ''' specfiles = glob.glob(specre) specstring = ', '.join(specfiles) print 'The following spectra will be combined: ' print specfiles specout = str(raw_input('Enter output file name: ')) iraf.scombine.unlearn() iraf.scombine.scale = scale iraf.scombine.rdnoise = rdnoise iraf.scombine.gain = gain iraf.scombine(input=specstring, output=specout) ask = str(raw_input('Plot output with splot? Y/N: ')) if (ask == 'y') or (ask == 'Y'): iraf.splot.unlearn() iraf.splot(specout)
def execute(self): """Execute pyraf task: splot""" log.debug("SplotETI.execute()") # Populate object lists xcldict = copy(self.clparam_dict) for fil in self.file_objs: xcldict.update(fil.get_parameter()) for par in self.param_objs: xcldict.update(par.get_parameter()) iraf.unlearn(iraf.splot) # Use setParam to list the parameters in the logfile for par in xcldict: #Stderr and Stdout are not recognized by setParam if par != "Stderr" and par !="Stdout": iraf.splot.setParam(par,xcldict[par]) log.fullinfo("\nSPLOT PARAMETERS:\n") iraf.lpar(iraf.splot, Stderr=xcldict["Stderr"], \ Stdout=xcldict["Stdout"]) # Execute the task using the same dict as setParam # (but this time with Stderr and Stdout) iraf.splot(**xcldict)
def lris_extract(science, standards, dostandard=yes): '''Extract and flux calibrate LRIS spectra (assuming they have been processed into 2D images using LRIS_pipe above).''' if dostandard: lris_standard(standards) for src in science: bimages = iraffiles("%s_??_B.fits" % src) rimages = iraffiles("%s_??_R.fits" % src) joinstr = "" for bimage in bimages: bimage = bimage.split(".")[0] # Find appropriate standard for this image bstd = get_head("%s.fits" % bimage, "STDNAME") # Extract 1D spectra if get_head("%s.fits" % bimage, "SKYSUB"): iraf.apall(bimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none", reference=bstd) else: iraf.apall(bimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit", reference=bstd) # Normalize by the standard continuua iraf.sarith("%s.ms" % bimage, "/", "../standards/s%s.ms.fits" % bstd, "s%s.ms" % bimage, w1=INDEF, w2=INDEF) # Telluric correct iraf.telluric("s%s.ms" % bimage, "ts%s.ms" % bimage, "../standards/telluric.%s.fits" % bstd, tweakrms=yes, interactive=yes, sample='6850:6950,7575:7700,8750:9925') # Flux calibration iraf.calibrate("ts%s.ms" % bimage, "fts%s.ms" % bimage, extinct=yes, flux=yes, extinction="home$extinct/maunakeaextinct.dat", observatory="Keck", sens="../standards/%s.sens" % bstd, airmass='', exptime='') joinstr += "fts%s.ms," % bimage for rimage in rimages: rimage = rimage.split(".")[0] # Find appropriate standard for this image rstd = get_head("%s.fits" % rimage, "STDNAME") # Extract 1D spectra if get_head("%s.fits" % rimage, "SKYSUB"): iraf.apall(rimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none", reference=rstd) else: iraf.apall(rimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit", reference=rstd) # Normalize by the standard continuua iraf.sarith("%s.ms" % rimage, "/", "../standards/s%s.ms.fits" % rstd, "s%s.ms" % rimage, w1=5500, w2=INDEF) # Telluric correct iraf.telluric("s%s.ms" % rimage, "ts%s.ms" % rimage, "../standards/telluric.%s.fits" % rstd, tweakrms=yes, interactive=yes, sample='6850:6950,7575:7700') # Flux calibration iraf.calibrate("ts%s.ms" % rimage, "fts%s.ms" % rimage, extinct=yes, flux=yes, extinction="home$extinct/maunakeaextinct.dat", observatory="Keck", sens="../standards/%s.sens" % rstd, airmass='', exptime='') joinstr += "fts%s.ms," % rimage # Combine iraf.scombine(joinstr[:-1], "%s.ms.fits" % src, combine="average", reject="avsigclip", w1=INDEF, w2=INDEF, dw=INDEF, nw=INDEF, scale="median", zero="none", weight="none", sample="5450:5600", lsigma=3.0, hsigma=3.0, gain=RCCDGAIN, rdnoise=RCCDRNOISE) # Plot to enable final tweaks iraf.splot("%s.ms.fits" % src) return
import glob # Load third-party modules from pyraf import iraf # choose spectra to combine specre = str(raw_input("Enter regular expression for spectra files: ")) specfiles = glob.glob(specre) specstring = ', '.join(specfiles) # choose spectra output name specout = str(raw_input("Enter output combined spectra name: ")) # unlearn previous settings iraf.scombine.unlearn() # setup iraf.scombine.combine = 'median' iraf.scombine.reject = 'sigclip' iraf.scombine.scale = 'exposure' iraf.scombine.rdnoise = 'rdnoise' iraf.scombine.gain = 'gain' # call scombine iraf.scombine(input=specstring, output=specout) # visualize combined spectra with splot to check iraf.splot.unlearn() iraf.splot(specout) print '--- DONE ---'
def deimos_standard(standard): '''Extract standard star and calculate sensitivit function.''' bstd = "%s_01_B" % standard rstd = "%s_01_R" % standard # Extract standard if get_head("%s.fits" % bstd, "SKYSUB"): iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") # Fit the continuum #iraf.continuum("%s.ms" % bstd, output="s%s.ms" % bstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) #iraf.continuum("%s.ms" % rstd, output="s%s.ms" % rstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) # Create telluric iraf.splot("%s.ms" % bstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.%s.fits" % bstd) iraf.imreplace("telluric.B.%s.fits" % bstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.B.%s.fits" % bstd) # Remove stellar features and resave iraf.splot("%s.ms" % rstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.%s.fits" % rstd) iraf.imreplace("telluric.R.%s.fits" % rstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.R.%s.fits" % rstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % bstd, "s%s.ms" % bstd, 5.0) iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) iraf.gauss("a%s.ms[*,1,1]" % rstd, "s%s.ms" % rstd, 5.0) iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Divide through by smoothed standard #iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) #iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Create and apply telluric correction #iraf.splot("%s.ms" % bstd) # Remove telluric, save as a%s.ms #iraf.splot("%s.ms" % rstd) # Remove telluric, save as a%s.ms #iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.fits") #iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.fits") #iraf.imreplace("telluric.B.fits", 1.0, lower=0.0, upper=0.0) #iraf.imreplace("telluric.R.fits", 1.0, lower=0.0, upper=0.0) iraf.telluric("ds%s.ms" % bstd, "tds%s.ms" % bstd, "telluric.B.%s.fits" % bstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') iraf.telluric("ds%s.ms" % rstd, "tds%s.ms" % rstd, "telluric.R.%s.fits" % rstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') # Define bandpasses for standard star calculation iraf.standard("tds%s.ms" % bstd, "%s.B.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') iraf.standard("tds%s.ms" % rstd, "%s.R.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.B.std' % standard, '%s.B.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) iraf.sensfunc('%s.R.std' % standard, '%s.R.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def deimos_extract(science, standard, dostandard=yes): '''Extract and flux calibrate DEIMOS spectra (assuming they have been processed into 2D images using deimos_pipe above).''' if dostandard: deimos_standard(standard) for source in science: images = iraffiles("%s_??_B.fits" % source) joinstr = "" for image in images: bimage = image.split(".")[0] rimage = bimage[:-1] + "R" update_head(bimage, "FLUX_OBJ", standard) update_head(rimage, "FLUX_OBJ", standard) # Extract 1D spectra if get_head("%s.fits" % bimage, "SKYSUB"): iraf.apall(bimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none", reference="%s_01_B" % standard) iraf.apall(rimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none", reference="%s_01_R" % standard) else: iraf.apall(bimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit", reference="%s_01_B" % standard) iraf.apall(rimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit", reference="%s_01_R" % standard) # Normalize by the standard continuua iraf.sarith("%s.ms" % bimage, "/", "s%s_01_B.ms.fits" % standard, "s%s.ms" % bimage) iraf.sarith("%s.ms" % rimage, "/", "s%s_01_R.ms.fits" % standard, "s%s.ms" % rimage) # Telluric correct bstd = "%s_01_B" % standard rstd = "%s_01_R" % standard iraf.telluric("s%s.ms" % bimage, "ts%s.ms" % bimage, "telluric.B.%s.fits" % bstd, tweakrms=yes, interactive=yes, sample='6850:6950,7575:7700') iraf.telluric("s%s.ms" % rimage, "ts%s.ms" % rimage, "telluric.R.%s.fits" % rstd, tweakrms=yes, interactive=yes, sample='6850:6950,7575:7700') # Flux calibration iraf.calibrate("ts%s.ms" % bimage, "fts%s.ms" % bimage, extinct=yes, flux=yes, extinction="home$extinct/maunakeaextinct.dat", observatory="Keck", sensitivity="%s.B.sens" % standard, airmass='', exptime='') iraf.calibrate("ts%s.ms" % rimage, "fts%s.ms" % rimage, extinct=yes, flux=yes, extinction="home$extinct/maunakeaextinct.dat", observatory="Keck", sensitivity="%s.R.sens" % standard, airmass='', exptime='') joinstr += "fts%s.ms,fts%s.ms," % (bimage, rimage) # Combine iraf.scombine(joinstr[:-1], "%s.ms.fits" % source, combine="average", reject="avsigclip", w1=INDEF, w2=INDEF, dw=INDEF, nw=INDEF, scale="none", zero="none", weight="none", lsigma=3.0, hsigma=3.0, gain=CCDGAIN, rdnoise=CCDRNOISE) # Plot to enable final tweaks iraf.splot("%s.ms.fits" % source) 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 blue_standard(image, arcs, flats, object=None, biassec1=BLUEBIAS1, trimsec1=BLUETRIM1, biassec2=BLUEBIAS2, trimsec2=BLUETRIM2, outflat='Flat-Blue.fits', gain=BLUEGAIN, rdnoise=BLUERDNOISE, arc='Arc-Blue.fits', caldir='home$standards/'): '''Reduce and calibrate standard star observation with blue CCD''' # Bias subtract everything first bluebias(image, biassec1=biassec1, trimsec1=trimsec1, biassec2=biassec2, trimsec2=trimsec2) bluebias(arcs, biassec1=biassec1, trimsec1=trimsec1, biassec2=biassec2, trimsec2=trimsec2) bluebias(flats, biassec1=biassec1, trimsec1=trimsec1, biassec2=biassec2, trimsec2=trimsec2) # Create and apply flat-field for i in range(len(flats)): flats[i]='j%s' % flats[i] make_flat(flats, outflat, gain=gain, rdnoise=rdnoise) iraf.ccdproc('j%s' % image[0], ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) iraf.ccdproc('j%s' % arcs[0], ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) # Extract spectrum of standard if object==None: object=get_head(image, 'OBJECT') iraf.apall('j%s' % image[0], output=object, references='', interactive=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background='fit', weights='variance', pfit='fit1d', readnoise=rdnoise, gain=gain) # Extract arc and fit wavelength solution reference_arc('j%s' % arcs[0], arc, 'j%s' % image[0]) # Apply wavelength solution to standard iraf.refspec(object, references=arc, sort="", group="", override=yes, confirm=no, assign=yes) iraf.dispcor(object, '%s.w' % object, confirm=no, listonly=no) # Remove absorption features and smooth iraf.splot('%s.w' % object, 1, 1) iraf.gauss('temp1[*,1,1]', '%s.smooth' % object, 3.0) iraf.sarith('%s.w' % object, '/', '%s.smooth' % object, '%s.s' % object) # Define bandpasses for standard star calculation iraf.standard('%s.s' % object, '%s.std' % object, extinction='onedstds$kpnoextinct.dat', caldir=caldir, observatory='Lick', interact=yes, star_name=object, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % object, '%s.sens' % object, extinction='onedstds$kpnoextinct.dat', newextinction='extinct.dat', observatory='Lick', function='legendre', order=3, interactive=yes) return
extinction='mk_extinct.txt', observatory='Keck', ignoreaps=yes, sensitivity='sensstar2.fits') inputname = ptfsn1f + ',' + ptfsn2f #Produce combined spectrum iraf.scombine(input=inputname, out='finalspectrum.ms', reject='avsigclip', scale='median', sample='5500:6500') l.write('calib=yes') iraf.splot(images='finalspectrum.ms.fits') iraf.wspectext(input='finalspectrum.ms.fits[*,1]', output=ptfsn1_name + '.ascii', header='NO') iraf.wspectext(input='ptfsn1.f.fits[*,1]', output=ptfsn1_name + '_r400.ascii', header='NO') iraf.wspectext(input='ptfsn2.f.fits[*,1]', output=ptfsn1_name + '_b600.ascii', header='NO') l.write('finalname=' + ptfsn1_name + '.ascii') l.close()
# Load third-party modules from pyraf import iraf # choose spectra to combine specre = str(raw_input("Enter regular expression for spectra files: ")) specfiles = glob.glob(specre) specstring = ", ".join(specfiles) # choose spectra output name specout = str(raw_input("Enter output combined spectra name: ")) # unlearn previous settings iraf.scombine.unlearn() # setup iraf.scombine.combine = "median" iraf.scombine.reject = "sigclip" iraf.scombine.scale = "exposure" iraf.scombine.rdnoise = "rdnoise" iraf.scombine.gain = "gain" # call scombine iraf.scombine(input=specstring, output=specout) # visualize combined spectra with splot to check iraf.splot.unlearn() iraf.splot(specout) print "--- DONE ---"
head = pf.getheader(f) start = head['CRVAL1'] step = head['CDELT1'] length = head['NAXIS1'] x = start + pl.arange(0,length)*step hi = x > Lines[line][0] low = x < Lines[line][1] xx = hi*low med = pl.median(data[xx]) print med cursor = open('cursor','w') cursor.write('%s %s 1 e\n%s %s 1 e\n'%(Lines[line][0],med,Lines[line][1],med)) cursor.close() iraf.splot(images=f,\ cursor='cursor',\ save_file='splot.log') # read splot.log and extract the results myfile = open('splot.log','r') lines = myfile.readlines() try: # the first log written to empty file has header temp = string.split(string.strip(lines[3])) results.append(float(temp[0])) flux.append(float(temp[2])) fwhm.append(float(temp[3])) except: temp = (string.split(string.strip(lines[2]))) results.append(float(temp[0])) flux.append(float(temp[2]))
args.xmax + ' 1 1 d \n' ) for i in args.points: filecur.write( i +' 1 1 g \n' ) filecur.write( '0 0 1 q \n' '0 0 1 a \n' '0 0 1 a \n' '0 0 1 y \n' '0 0 1 t \n' '0 0 1 q \n' ) iraf.splot(args.spectra[0],cursor=source+'.cur' , save_file=args.log+'.log', graphics='eps') iraf.gflush #print 'call:\n python2 rename.py '+ source +' '+args.log+'.log' #print 'to plot: \n python2 plotspectra.py '+ args.spectra[0] ##move log and images #os.rename(args.log+'.log', './logs/'+args.log+'.log') #imagesiraf = glob.glob('sgi*.eps') # #for i,j in enumerate(imagesiraf): # print j # im = Image.open(j) # im.save('./plots/'+source+'_'+str(i)+'.png')
nlow=1, nhigh=1, nkeep=2) iraf.scombine(input=combstr, out='combinedspec.ms', combine='average', reject='minmax', scale='none', sample='5000:7500', nlow=0, nhigh=1, nkeep=1) l.write('calib=yes') iraf.splot(images='combinedspec_rmlow.ms.fits') for objname in objects_pars: if 'sn' in objname: SNName = objects_pars[objname][0] break iraf.wspectext(input='combinedspec.ms.fits[*,1]', output=SNName + '.ascii', header='NO') iraf.wspectext(input='combinedspec_rmlow.ms.fits[*,1]', output=SNName + '_rmlow.ascii', header='NO') l.write('finalname=' + SNName + '.ascii') l.close()
def flux_cal_new(objname, stdobjnames): print 'run flux_cal...' iraf.imred() iraf.kpnoslit() stdname, stdmag, stdmagband = get_std_name(stdobjnames[0]) print 'the standard star is ' + stdname stdmag = float(stdmag) stdairmass, stdexptime = get_fits_airmass_exptime(stdobjnames[0]) scripath = sys.argv[0] tempindex = scripath.rfind(os.sep) scripath = scripath[:tempindex] extpath = scripath + os.sep + 'LJextinct.dat' calpath = scripath + os.sep + 'standarddir' + os.sep outname1 = 'std' + stdobjnames[0] stdobjname = '' for tempstdname in stdobjnames: stdobjname = stdobjname + tempstdname + ',' stdobjname = stdobjname[:-1] if os.path.isfile(outname1): print 'file %s is already exist' % outname1 else: print 'run standard...' print 'make file %s ...' % outname1 iraf.standard(input = stdobjname , output = outname1, samestar = True, beam_switch = False , apertures = '', bandwidth = 30.0, bandsep = 20.0 , fnuzero = 3.6800000000000E-20, extinction = extpath , caldir = calpath, observatory = ')_.observatory' , interact = True, graphics = 'stdgraph', cursor = '' , star_name = stdname, airmass = stdairmass, exptime = stdexptime , mag = stdmag, magband = stdmagband, teff = '', answer = 'yes') outname2 = 'sens' + stdobjname if os.path.isfile(outname2): print 'file %s is already exist' % outname2 else: print 'run sensfunc...' print 'make file %s ...' % outname2 iraf.sensfunc(standards = outname1 , sensitivity = outname2, apertures = '', ignoreaps = True , logfile = 'logfile', extinction = ')_.extinction' , newextinction = extpath, observatory = 'bao', function = 'spline3' , order = 6, interactive = True, graphs = 'sr' , marks = 'plus cross box', colors = '2 1 3 4', cursor ='' , device = 'stdgraph', answer = 'yes') outname3 = 'c' + objname if os.path.isfile(outname3): print 'file %s is already exist' % outname3 else: print 'run calibrate...' print 'make file %s ...' % outname3 iraf.calibrate(input = objname , output = outname3, extinct = True, flux = True , extinction = extpath, observatory = 'bao', ignoreaps = True , sensitivity = outname2, fnu = False) final_outname = 'mark_' + objname if os.path.isfile(final_outname): print 'file %s is already exist' else: print 'run scopy' print 'make file %s ...' % final_outname iraf.scopy(input = outname3 , output = final_outname, w1 = 'INDEF', w2 = 'INDEF' , apertures = '', bands = 1, beams = '', apmodulus = 0 , format = 'multispec', renumber = False, offset = 0 , clobber = False, merge = False, rebin = True, verbose = False) print 'splot %s' % final_outname iraf.splot(images = final_outname) return final_outname
def promptLine(specFile, specType): ''' Helper function - prompts user to measure a line in splot and to provide the identity of the measured line, finding both the rest and measured wavelengths. This and other helper functions are the only locations where values are hard-coded, making for easier customizability. Provides shortcuts for certain lines, allowing for high precision for rest wavelengths. Args: specFile absolute path to the spectrum file specType type of spectrum to be measured; either 'sky' or 'science' Returns: restW rest wavelength of one measured line in the spectrum userW user-measured wavelength of one line in the spectrum, or: [empty] only if user wants to skip line measuring (because e.g. the spectrum has no good lines) ''' ##Obtain measurement of a single line to get spectrum's redshift. #set up names and files specPath, __ = split(specFile) logFile = join(specPath, 'splot.log') #run splot to let user measure 1 line print('\n*****In the splot window, measure one line with a known rest ' \ 'wavelength, then press q. If there are no lines, just press q.') iraf.splot(images=specFile, save_file=logFile) ##Prompt user for the identity of the measured line while (True): #continue until the user gives valid input if specType == 'science': #user is measuring actual emission lines try: #ask for valid input key = eval( input('*****If you measured a line, enter:\n' '0 for no lines - skip this spectrum\n' '1 for H-alpha \n' '2 for OIII \n' '3 for OII \n' 'the wavelength of another line\n')) key = float(key) #trigger a Name/ValueError if not a float except (NameError, ValueError, SyntaxError): print( 'Invalid input - please enter 0, 1, 2, 3, or a wavelength:\n' ) continue if key > 3 and key < 100: continue #set the wavelength based on input key #it would be nice if Python had a switch-statement... if key == 0: return ('', '', 1) #user wants to skip elif key == 1: restW = 6562.82 #H-alpha elif key == 2: restW = 5006.84 #OIII elif key == 3: restW = 3727.53 #OII blend else: restW = key break elif specType == 'sky': #user is measuring sky emission lines try: #ask for valid input key = eval( input('*****If you measured a line, enter:\n' '0 to skip\n' '1 for Hg 4358\n' '2 for Hg 5460\n' '3 for O I 5577\n' '4 for Na D 5891\n' '5 for O I 6300\n' '6 for O I 6363\n' 'the wavelength of another line\n')) key = float(key) #trigger a Name/ValueError if not a float except (NameError, ValueError, SyntaxError): print( 'Invalid input - please enter a number key or a wavelength:\n' ) continue #set the wavelength based on input key if key == 0: return ('', '', 1) elif key == 1: restW = 4358.33 #Hg elif key == 2: restW = 5460.74 #Hg elif key == 3: restW = 5577.35 #OI elif key == 4: restW = 5891.94 #Na D elif key == 5: restW = 6300.23 #OI elif key == 6: restW = 6363.88 #OI else: restW = key break #read the measured wavelength from the splot log file entry = readSplotLog(logFile) userW = entry[0] skip = 0 return (userW, restW, skip)
iraf.apall(input=filename, find="No", recenter="No", resize="No", interactive="Yes") # Make sure that the dispersion axis is in the header. iraf.hedit(images=[filename], fields=["DISPAXIS"], value=["1"], add="Yes") iraf.identify(filename[:-5], coordli=home + "/Downloads/HgNe(1).dat", section="line 105 125", crval=crval, cdelt=dispersion, fwidth=5) # Tell the extracted spectrum what the wavelength solutions are. iraf.hedit(images=[extracted_filename], fields=["REFSPEC1"], \ value=[filename], add="Yes") iraf.refspec(input=extracted_filename, referen=filename[:-5], sort='', group='', confirm='no') iraf.dispcor(input=extracted_filename, output=calibrated_filename) # Plot the extracted spectrum? iraf.splot(calibrated_filename)
def check_spectra(WORK_DIR, ext): print "\n + Visually check spectra before proceeding\n" for obj in observations[WORK_DIR]['objects']: iraf.splot(obj+ext)
start = head['CRVAL1'] step = head['CDELT1'] length = head['NAXIS1'] x = start + pl.arange(0,length)*step HJD.append(float(head['HJD'])) SNR = [] # calculate snr using splot for every region. for reg in enumerate(['Ha','red','yellow','Hb','He2','blue']): # write to cursor file #os.remove('cursor') cfile = file('cursor','w') s = "%s 0.001 1 m\n%s 0.001 1 m\n" % (regions[reg[1]][0],regions[reg[1]][1]) cfile.write(s) cfile.close() # do this otherwise get freaky bugs out = iraf.splot(images=spec.strip(),cursor='cursor',Stdout=1) SNR.append(read_out(out)) print reg, SNR[0] # Now print output s = [' '.join('%s'%i for i in SNR)] #print s output.write(str(head['HJD'])+' ' + s[0]+'\n') output.close() # run bplot #iraf.bplot(images='@speclist',cursor='cursor') #HJDfile = open('HJD.dat','w') #for d in HJD:
iraf.unlearn('gfapsum') iraf.unlearn('gsstandard') # ---------- Sensitivity function (after reducing standard star) ---------- # std = np.loadtxt(ic.lst_std, dtype=str) if (std.size > 1): raise ValueError("Please check if there is only one image for the standard star.") std0 = std.item(0) # Sum the fibers iraf.imdelete('astxeqxbrg@'+ic.lst_std) iraf.gfapsum('stxeqxbrg'+std0, combine='sum', fl_inter='no') iraf.splot('astxeqxbrg'+std0+'[sci,1]') # Plot the sum spectra spec_sum, hd = fits.getdata('astxeqxbrg'+std0+'.fits', ext=1, header=True) w = hd['CRVAL1'] + np.arange(len(spec_sum))*hd['CD1_1'] fig, ax = plt.subplots(1, 1, figsize=(7,5)) plt.subplots_adjust(left=0.16, right=0.96, bottom=0.13, top=0.91) plt.suptitle("Sum of spectra", x=0.55, ha='center', y=0.97, va='top', fontsize=16.0, fontweight='bold') ax.set_xlabel(r"Wavelength [${\rm \AA}$]", fontsize=12.0) ax.set_ylabel("Counts", fontsize=12.0) # ax.set_yscale('log') ax.tick_params(axis='both', labelsize=12.0) ax.plot(w, spec_sum, color='C0', linewidth=1.5, alpha=0.8)
#gt = x > 6400.0 #lt = x < 6600.0 # Hydrogen Beta gt = x > 4700.0 lt = x < 4900.0 continuum = pl.median(data[gt*lt]) # write to cursor file cfile = open('cursor','w') #s = "4676 %s 1 e\n4696 %s 1 e\n" % (continuum,continuum) s = "4850 %s 1 e\n4870 %s 1 e\n" % (continuum,continuum) #s = "6553 %s 1 e\n 6573 %s 1 e\n" % (continuum,continuum) cfile.write(s) iraf.splot(images=spec.strip(),cursor='cursor') # run bplot #iraf.bplot(images='@speclist',cursor='cursor') HJDfile = open('HJD.dat','w') for d in HJD: HJDfile.write('%s\n'%d) HJDfile.close() # get the output. Tune this grep command for the wavelength #os.system('grep 65 splot.log > rv.dat')
head = pf.getheader('fec2117_%04d.fits'%i) start = head['CRVAL1'] step = head['CDELT1'] length = head['NAXIS1'] x = start + pl.arange(0,length)*step hi = x > 4640 low = x < 4730 xx = hi*low med = pl.median(data[xx]) print med cursor = open('cursor','w') cursor.write('4640 %s 1 k\n4730 %s 1 k\n'%(med,med)) cursor.close() iraf.splot(images='fec2117_%04d.fits'%i,\ cursor='cursor',\ save_file='splot.log') # read splot.log and extract the results myfile = open('splot.log','r') lines = myfile.readlines() try: # the first log written to empty file has header temp = string.split(string.strip(lines[3])) results.append(float(temp[0])) fwhm.append(float(temp[5])) except: temp = (string.split(string.strip(lines[2]))) results.append(float(temp[0])) fwhm.append(float(temp[5])) # remove the log file
print ' ' print ':::::::::::::::::::::::::::::::::::::::::::::::::::' print 'PLOTTING EXTRACTED SPECTRA:' print ':::::::::::::::::::::::::::::::::::::::::::::::::::' print 'The extracted, wavelength-calibrated spectrum ' print str(object_b_ec_w) print 'is now plotted at order ' + str(order) + ' for the user' print 'specified wavelength at ' + str(wvlength) + 'A:' print ':::::::::::::::::::::::::::::::::::::::::::::::::::' print ' ' plot_order = str(-(int(order)) + 86) iraf.noao(_doprint=0) iraf.onedspec(_doprint=0) iraf.splot(images=object_b_ec_w, line=plot_order) # ::::::::::::::::::::::::::::::: # STEP 8: REMOVE TEMPORARY FILES: # ::::::::::::::::::::::::::::::: print ' ' print ' ' print ':::::::::::::::::::::::::::::::::::::::::::::::::::' print 'TEMPORARY FILE REMOVAL: ' print ':::::::::::::::::::::::::::::::::::::::::::::::::::' print 'Temporary files clean up: ' print ':::::::::::::::::::::::::::::::::::::::::::::::::::' print ' '
def blue_standard(image, arcs, flats, object=None, biassec1=BLUEBIAS1, trimsec1=BLUETRIM1, biassec2=BLUEBIAS2, trimsec2=BLUETRIM2, outflat='Flat-Blue.fits', gain=BLUEGAIN, rdnoise=BLUERDNOISE, arc='Arc-Blue.fits', caldir='home$standards/'): '''Reduce and calibrate standard star observation with blue CCD''' # Bias subtract everything first bluebias(image, biassec1=biassec1, trimsec1=trimsec1, biassec2=biassec2, trimsec2=trimsec2) bluebias(arcs, biassec1=biassec1, trimsec1=trimsec1, biassec2=biassec2, trimsec2=trimsec2) bluebias(flats, biassec1=biassec1, trimsec1=trimsec1, biassec2=biassec2, trimsec2=trimsec2) # Create and apply flat-field for i in range(len(flats)): flats[i] = 'j%s' % flats[i] make_flat(flats, outflat, gain=gain, rdnoise=rdnoise) iraf.ccdproc('j%s' % image[0], ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) iraf.ccdproc('j%s' % arcs[0], ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) # Extract spectrum of standard if object == None: object = get_head(image, 'OBJECT') iraf.apall('j%s' % image[0], output=object, references='', interactive=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background='fit', weights='variance', pfit='fit1d', readnoise=rdnoise, gain=gain) # Extract arc and fit wavelength solution reference_arc('j%s' % arcs[0], arc, 'j%s' % image[0]) # Apply wavelength solution to standard iraf.refspec(object, references=arc, sort="", group="", override=yes, confirm=no, assign=yes) iraf.dispcor(object, '%s.w' % object, confirm=no, listonly=no) # Remove absorption features and smooth iraf.splot('%s.w' % object, 1, 1) iraf.gauss('temp1[*,1,1]', '%s.smooth' % object, 3.0) iraf.sarith('%s.w' % object, '/', '%s.smooth' % object, '%s.s' % object) # Define bandpasses for standard star calculation iraf.standard('%s.s' % object, '%s.std' % object, extinction='onedstds$kpnoextinct.dat', caldir=caldir, observatory='Lick', interact=yes, star_name=object, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % object, '%s.sens' % object, extinction='onedstds$kpnoextinct.dat', newextinction='extinct.dat', observatory='Lick', function='legendre', order=3, interactive=yes) 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 deimos_extract(science, standard, dostandard=yes): '''Extract and flux calibrate DEIMOS spectra (assuming they have been processed into 2D images using deimos_pipe above).''' if dostandard: deimos_standard(standard) for source in science: images = iraffiles("%s_??_B.fits" % source) joinstr = "" for image in images: bimage = image.split(".")[0]; rimage = bimage[:-1] + "R" update_head(bimage, "FLUX_OBJ", standard) update_head(rimage, "FLUX_OBJ", standard) # Extract 1D spectra if get_head("%s.fits" % bimage, "SKYSUB"): iraf.apall(bimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none", reference="%s_01_B" % standard) iraf.apall(rimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none", reference="%s_01_R" % standard) else: iraf.apall(bimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit", reference="%s_01_B" % standard) iraf.apall(rimage, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit", reference="%s_01_R" % standard) # Normalize by the standard continuua iraf.sarith("%s.ms" % bimage, "/", "s%s_01_B.ms.fits" % standard, "s%s.ms" % bimage) iraf.sarith("%s.ms" % rimage, "/", "s%s_01_R.ms.fits" % standard, "s%s.ms" % rimage) # Telluric correct bstd = "%s_01_B" % standard; rstd = "%s_01_R" % standard iraf.telluric("s%s.ms" % bimage, "ts%s.ms" % bimage, "telluric.B.%s.fits" % bstd, tweakrms=yes, interactive=yes, sample='6850:6950,7575:7700') iraf.telluric("s%s.ms" % rimage, "ts%s.ms" % rimage, "telluric.R.%s.fits" % rstd, tweakrms=yes, interactive=yes, sample='6850:6950,7575:7700') # Flux calibration iraf.calibrate("ts%s.ms" % bimage, "fts%s.ms" % bimage, extinct=yes, flux=yes, extinction="home$extinct/maunakeaextinct.dat", observatory="Keck", sensitivity="%s.B.sens" % standard, airmass='', exptime='') iraf.calibrate("ts%s.ms" % rimage, "fts%s.ms" % rimage, extinct=yes, flux=yes, extinction="home$extinct/maunakeaextinct.dat", observatory="Keck", sensitivity="%s.R.sens" % standard, airmass='', exptime='') joinstr += "fts%s.ms,fts%s.ms," % (bimage, rimage) # Combine iraf.scombine(joinstr[:-1], "%s.ms.fits" % source, combine="average", reject="avsigclip", w1=INDEF, w2=INDEF, dw=INDEF, nw=INDEF, scale="none", zero="none", weight="none", lsigma=3.0, hsigma=3.0, gain=CCDGAIN, rdnoise=CCDRNOISE) # Plot to enable final tweaks iraf.splot("%s.ms.fits" % source) return
def deimos_standard(standard): '''Extract standard star and calculate sensitivit function.''' bstd = "%s_01_B" % standard; rstd = "%s_01_R" % standard # Extract standard if get_head("%s.fits" % bstd, "SKYSUB"): iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") # Fit the continuum #iraf.continuum("%s.ms" % bstd, output="s%s.ms" % bstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) #iraf.continuum("%s.ms" % rstd, output="s%s.ms" % rstd, lines=1, bands=1, # type="fit", sample="*", naverage=1, function="chebyshev", # order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0, # interac=yes) # Create telluric iraf.splot("%s.ms" % bstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.%s.fits" % bstd) iraf.imreplace("telluric.B.%s.fits" % bstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.B.%s.fits" % bstd) # Remove stellar features and resave iraf.splot("%s.ms" % rstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.%s.fits" % rstd) iraf.imreplace("telluric.R.%s.fits" % rstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.R.%s.fits" % rstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % bstd, "s%s.ms" % bstd, 5.0) iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) iraf.gauss("a%s.ms[*,1,1]" % rstd, "s%s.ms" % rstd, 5.0) iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Divide through by smoothed standard #iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd) #iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd) # Create and apply telluric correction #iraf.splot("%s.ms" % bstd) # Remove telluric, save as a%s.ms #iraf.splot("%s.ms" % rstd) # Remove telluric, save as a%s.ms #iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.fits") #iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.fits") #iraf.imreplace("telluric.B.fits", 1.0, lower=0.0, upper=0.0) #iraf.imreplace("telluric.R.fits", 1.0, lower=0.0, upper=0.0) iraf.telluric("ds%s.ms" % bstd, "tds%s.ms" % bstd, "telluric.B.%s.fits" % bstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') iraf.telluric("ds%s.ms" % rstd, "tds%s.ms" % rstd, "telluric.R.%s.fits" % rstd, xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') # Define bandpasses for standard star calculation iraf.standard("tds%s.ms" % bstd, "%s.B.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') iraf.standard("tds%s.ms" % rstd, "%s.R.std" % standard, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=standard, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.B.std' % standard, '%s.B.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) iraf.sensfunc('%s.R.std' % standard, '%s.R.sens' % standard, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def red_standard(image, arcs, flats, object=None, biassec=REDBIAS, trimsec=REDTRIM, outflat='Flat-Red.fits', gain=REDGAIN, rdnoise=REDRDNOISE, arc='Arc-Red.fits', caldir='home$standards/'): '''Reduce and calibrate standard star observation with red CCD''' # Bias subtract everything first redbias(image, biassec=biassec, trimsec=trimsec) redbias(arcs, biassec=biassec, trimsec=trimsec) redbias(flats, biassec=biassec, trimsec=trimsec) # Create and apply flat-field make_flat(flats, outflat, gain=gain, rdnoise=rdnoise) iraf.ccdproc(image[0], ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) arcimages=','.join(arcs) iraf.ccdproc(arcs, ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) # Extract spectrum of standard if object==None: object=get_head(image, 'OBJECT') iraf.apall(image[0], output=object, references='', interactive=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background='fit', weights='variance', pfit='fit1d', readnoise=rdnoise, gain=gain) # Extract arc and fit wavelength solution iraf.imarith(arcs[0], '+', arcs[1], 'Arc-Sum.fits') reference_arc('Arc-Sum.fits', arc, image[0]) # Apply wavelength solution to standard iraf.refspec(object, references=arc, sort="", group="", override=yes, confirm=no, assign=yes) iraf.dispcor(object, '%s.w' % object, confirm=no, listonly=no) # Remove absorption features and smooth iraf.splot('%s.w' % object, 1, 1) iraf.gauss('temp1[*,1,1]', '%s.smooth' % object, 3.0) iraf.sarith('%s.w' % object, '/', '%s.smooth' % object, '%s.s' % object) # Create and apply telluric correction iraf.sarith('%s.w' % object, '/', 'temp1', 'telluric') iraf.splot('telluric', 1, 1) iraf.telluric('%s.s' % object, '%s.t' % object, 'telluric', xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') # Define bandpasses for standard star calculation iraf.standard('%s.t' % object, '%s.std' % object, extinction='onedstds$kpnoextinct.dat', caldir=caldir, observatory='Lick', interact=yes, star_name=object, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % object, '%s.sens' % object, extinction='onedstds$kpnoextinct.dat', newextinction='extinct.dat', observatory='Lick', function='legendre', order=3, interactive=yes) return
def lris_standard(standards, xcorr=yes): '''Extract standard stars and calculate sensitivity functions.''' for ofile, nstd in standards: shutil.copy(ofile, "%s.fits" % nstd) # Extract standard if get_head("%s.fits" % nstd, "SKYSUB"): iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="none") else: iraf.apall(nstd, output="", inter=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background="fit") if xcorr: # Cross correlate to tweak wavelength iraf.xcsao("%s.ms" % nstd, templates=XCTEMPLATE, correlate="wavelength", logfiles="%s.xcsao" % ofile) # If a solution was found, apply shift try: xcsaof = open("%s.xcsao" % ofile) shift = float(xcsaof.readlines()[6].split()[14]) except: shift = 0.0 iraf.specshift("%s.ms" % nstd, -shift) # Create telluric iraf.splot("%s.ms" % nstd) # Remove telluric and absorption, save as a%s.ms iraf.sarith("%s.ms" % nstd, "/", "a%s.ms" % nstd, "telluric.%s.fits" % nstd) iraf.imreplace("telluric.%s.fits" % nstd, 1.0, lower=0.0, upper=0.0) iraf.splot("telluric.%s.fits" % nstd) # Remove stellar features and resave # Create smoothed standard iraf.gauss("a%s.ms[*,1,1]" % nstd, "s%s.ms" % nstd, 5.0) iraf.sarith("%s.ms" % nstd, "/", "s%s.ms" % nstd, "ds%s.ms" % nstd) # Apply telluric correction iraf.telluric( "ds%s.ms" % nstd, "tds%s.ms" % nstd, "telluric.%s.fits" % nstd, xcorr=no, tweakrms=no, interactive=no, sample='4000:4010,6850:6975,7150:7350,7575:7725,8050:8400,8900:9725' ) # Define bandpasses for standard star calculation obj = get_head("%s.fits" % nstd, "OBJECT") iraf.standard("tds%s.ms" % nstd, "%s.std" % nstd, extinction='home$extinct/maunakeaextinct.dat', caldir='home$standards/', observatory='Keck', interac=yes, star_name=obj, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % nstd, '%s.sens' % nstd, extinction='home$extinct/maunakeaextinct.dat', newextinction='extinct.dat', observatory='Keck', function='legendre', order=4, interactive=yes) return
def viewSpec(self, image): iraf.splot(image) return
def MeasureWidthsInter(spectraFilename, lineFilename): # Function: MeasureWidthsInter(spectraFilename, lineData, apBoundaries) # # Inputs: spectraFilename : The absolute path of the .fits file containing the spectrum to measure # lineFilename: A file containing information on lines to measure # Outputs: EQWidths - A list of the measured widths, each tuple of the form: # (wl, ion, ep, gf, c6, eqw, meas. wl, fwhm (gaussian)). # # Desription: This function calls the iraf 'splot' task, and asks the user to manually # measure each line in the passed list. The splot log file is read and the actual # line equivalent width is assumed to be the last measurement in the log file. objectName, numApertures, apertureSize, apBoundaries = SD.ReadSpectraInfo( spectraFilename) lineData, unused = getLinesForApertures(lineFilename, apBoundaries) EQWidths = [] badLines = [] dopplerCount = 0 dopplerCheck = 0.0 for [wl, ion, ep, gf, c6, solarEqw, comment] in lineData: contentAps = [ apBoundaries.index(x) for x in apBoundaries if x[0] <= wl <= x[1] ] if len(contentAps) == 0: print( 'Wavelength ({0:8.3f}) for {1:4.1f} line not covered by spectra. Continuing.' .format(wl, ion)) continue wlUpper = wl + k.HalfWindowWidth wlLower = wl - k.HalfWindowWidth print( 'Measuring {0:4.1f} line: {1:8.3f} (displaying first of {2:1d} orders)' .format(ion, wl, len(contentAps))) iraf.onedspec() iraf.splot(spectraFilename, line=contentAps[0] + 1, band=1, xmin=wlLower + 1., xmax=wlUpper + 1., save_file=k.SplotLogFilename, options='nosysid, auto') # iraf.splot(spectraFilename, line=contentAps[0]+1, band=1, xmin=wlLower+1., xmax=wlUpper+1., ymin=k.SpecFluxMin, ymax=k.SpecFluxMax, save_file=k.SplotLogFilename, options='nosysid, auto') try: with open(k.SplotLogFilename) as filePointer: # Assume that the desired fit is the _last_ fit made. # It is important for the blended fit ('d' option) that the # actual line is the last one selected in the blend dataLine = filePointer.readlines()[-1].split() # splot data line is: line center, cursor loc. (cont. level), flux(?), # eqw, core depth from cursor, gfwhm, lfwhm lineCenter = float(dataLine[0]) print( 'Measured line: {0:8.3f} at: {1:5.1f} mAngstroms (FWHM = {2:5.4f})' .format(wl, float(dataLine[3]) * 1000.0, float(dataLine[5]))) EQWidths.append([ wl, ion, ep, gf, c6, float(dataLine[3]) * 1000.0, lineCenter, float(dataLine[5]) ]) thisDoppler = ((lineCenter - wl) / wl) * k.SpeedofLight dopplerCheck += thisDoppler dopplerCount += 1 os.remove(k.SplotLogFilename) except (IOError, ValueError): # IOError - No line is fit (no log was created) # ValueError - Log output from splot is in unrecognizeable format badLines.append([wl, ion, k.unknownReason, k.unknownStr]) print('No line measured for wl: {0:8.3f}.'.format(wl)) return EQWidths, badLines, dopplerCount, dopplerCheck
# Delete previous results. os.system("rm "+extracted_filename+" "+calibrated_filename) # Make sure that the dispersion axis is in the header. iraf.hedit(images=[filename], fields=["DISPAXIS"], value=["1"], add="Yes") # Run the spectral extraction program. iraf.apextract.setParam("dispaxis", "1") iraf.apall(input=filename, find="No", recenter="No", resize="No") # Now identify the spectral lines in the arc lamps. Need to replace l1 l2 # with the range of rows that have the spectral lines. iraf.identify(filename, section="line 265 285") # Tell the extracted spectrum what the wavelength solutions are. iraf.hedit(images=[extracted_filename], fields=["REFSPEC1"], \ value=[filename], add="Yes") iraf.dispcor(input=extracted_filename, output=calibrated_filename) # Plot the extracted spectrum? iraf.splot(calibrated_filename)
def red_standard(image, arcs, flats, object=None, biassec=REDBIAS, trimsec=REDTRIM, outflat='Flat-Red.fits', gain=REDGAIN, rdnoise=REDRDNOISE, arc='Arc-Red.fits', caldir='home$standards/'): '''Reduce and calibrate standard star observation with red CCD''' # Bias subtract everything first redbias(image, biassec=biassec, trimsec=trimsec) redbias(arcs, biassec=biassec, trimsec=trimsec) redbias(flats, biassec=biassec, trimsec=trimsec) # Create and apply flat-field make_flat(flats, outflat, gain=gain, rdnoise=rdnoise) iraf.ccdproc(image[0], ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) arcimages = ','.join(arcs) iraf.ccdproc(arcs, ccdtype='', noproc=no, fixpix=no, overscan=no, trim=no, zerocor=no, darkcor=no, flatcor=yes, illumcor=no, fringecor=no, readcor=no, scancor=no, flat=outflat) # Extract spectrum of standard if object == None: object = get_head(image, 'OBJECT') iraf.apall(image[0], output=object, references='', interactive=yes, find=yes, recenter=yes, resize=yes, edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes, review=no, background='fit', weights='variance', pfit='fit1d', readnoise=rdnoise, gain=gain) # Extract arc and fit wavelength solution iraf.imarith(arcs[0], '+', arcs[1], 'Arc-Sum.fits') reference_arc('Arc-Sum.fits', arc, image[0]) # Apply wavelength solution to standard iraf.refspec(object, references=arc, sort="", group="", override=yes, confirm=no, assign=yes) iraf.dispcor(object, '%s.w' % object, confirm=no, listonly=no) # Remove absorption features and smooth iraf.splot('%s.w' % object, 1, 1) iraf.gauss('temp1[*,1,1]', '%s.smooth' % object, 3.0) iraf.sarith('%s.w' % object, '/', '%s.smooth' % object, '%s.s' % object) # Create and apply telluric correction iraf.sarith('%s.w' % object, '/', 'temp1', 'telluric') iraf.splot('telluric', 1, 1) iraf.telluric('%s.s' % object, '%s.t' % object, 'telluric', xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700') # Define bandpasses for standard star calculation iraf.standard('%s.t' % object, '%s.std' % object, extinction='onedstds$kpnoextinct.dat', caldir=caldir, observatory='Lick', interact=yes, star_name=object, airmass='', exptime='') # Determine sensitivity function iraf.sensfunc('%s.std' % object, '%s.sens' % object, extinction='onedstds$kpnoextinct.dat', newextinction='extinct.dat', observatory='Lick', function='legendre', order=3, interactive=yes) return