def galextract(img, yc=None, dy=None, normalize=True, calfile=None, convert=True, specformat='ascii'): #set up some files that will be needed logfile='specext.log' #create the spectra text files for all of our objects spec_list=[] #skynormalize the data if normalize: specslitnormalize(img, 'n'+img, '', response=None, response_output=None, order=3, conv=1e-2, niter=20, startext=0, clobber=True,logfile='salt.log',verbose=True) hdu=pyfits.open('n'+img) target=hdu[0].header['OBJECT'] ofile='%s.%s_%i_%i.ltxt' % (target, extract_date(img), extract_number(img), yc) #ofile = img.replace('fits', 'txt') extract_spectra(hdu, yc, dy, ofile, smooth=False, grow=10, clobber=True, specformat=specformat, convert=convert) if calfile is not None: airmass=hdu[0].header['AIRMASS'] exptime=hdu[0].header['EXPTIME'] extfile=iraf.osfn("pysalt$data/site/suth_extinct.dat") speccal(ofile, ofile.replace("txt", "spec"), calfile, extfile, airmass, exptime, clobber=True, logfile='salt.log', verbose=True)
def galextract(img, yc=None, dy=None, normalize=True, calfile=None, convert=True, specformat='ascii'): #set up some files that will be needed logfile = 'specext.log' #create the spectra text files for all of our objects spec_list = [] #skynormalize the data if normalize: specslitnormalize(img, 'n' + img, '', response=None, response_output=None, order=3, conv=1e-2, niter=20, startext=0, clobber=True, logfile='salt.log', verbose=True) hdu = pyfits.open('n' + img) target = hdu[0].header['OBJECT'] ofile = '%s.%s_%i_%i.ltxt' % (target, extract_date(img), extract_number(img), yc) #ofile = img.replace('fits', 'txt') extract_spectra(hdu, yc, dy, ofile, smooth=False, grow=10, clobber=True, specformat=specformat, convert=convert) if calfile is not None: airmass = hdu[0].header['AIRMASS'] exptime = hdu[0].header['EXPTIME'] extfile = iraf.osfn("pysalt$data/site/suth_extinct.dat") speccal(ofile, ofile.replace("txt", "spec"), calfile, extfile, airmass, exptime, clobber=True, logfile='salt.log', verbose=True)
def extract_spectra(img, yc=None, oy=10, dy=50, minsize=5, thresh=3, findobject=False, niter=5, calfile=None, smooth=False, maskzeros=False, clobber=True): """Create a list of spectra for each of the objects in the images""" #okay we need to identify the objects for extraction and identify the regions for sky extraction #first find the objects in the image #skynormalize the data #specslitnormalize(img, 'n'+img, '', response=None, response_output=None, order=3, conv=1e-2, niter=20, # startext=0, clobber=False,logfile='salt.log',verbose=True) print 'Extract Spectra from ', img hdu=pyfits.open(img) target=hdu[0].header['OBJECT'].replace(' ', '') propcode=hdu[0].header['PROPID'] airmass=hdu[0].header['AIRMASS'] exptime=hdu[0].header['EXPTIME'] if smooth: data=smooth_data(hdu[1].data) else: data=hdu[1].data #replace the zeros with the average from the frame if maskzeros: mean,std=iterstat(data[data>0]) #rdata=mean np.random.normal(mean, std, size=data.shape) print mean, std data[data<=0]=mean #rdata[data<=0] #use manual intervention to get section if findobject: section=findobj.findObjects(data, method='median', specaxis=1, minsize=minsize, thresh=thresh, niter=niter) if yc is None and len(section)>0: yc = np.mean(section[0]) elif len(section)>0: diff = 1e6 for i in range(len(section)): y = np.mean(section[i]) if abs(y-yc) < diff: bestyc = y diff = abs(y-yc) yc = bestyc if yc is None: os.system('ds9 %s &' % img) print len(hdu) if len(hdu)==2: print 'Using basic extraction' if yc is None: y1=int(raw_input('y1:')) y2=int(raw_input('y2:')) sy1=int(raw_input('sky y1:')) sy2=int(raw_input('sky y2:')) ap_list=extract(hdu, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=True) sk_list=extract(hdu, method='normal', section=[(sy1,sy2)], minsize=minsize, thresh=thresh, convert=True) ap_list[0].ldata=ap_list[0].ldata-float(y2-y1)/(sy2-sy1)*sk_list[0].ldata ofile='%s.%s_%i.txt' % (target, extract_date(img), extract_number(img)) write_extract(ofile, [ap_list[0]], outformat='ascii', clobber=clobber) w, f, e = np.loadtxt(ofile, usecols=(0,1,2), unpack=True) w, f, e=cleanspectra(w, f, e, neg=True) m = (w>3900)*(w<8100) write_spectra(ofile, w[m], f[m], e[m]) else: print 'Using advanced extraction' if yc is None: yc=int(raw_input('yc:')) w0=hdu[1].header['CRVAL1'] dw=hdu[1].header['CD1_1'] xarr = np.arange(len(hdu[1].data[0])) warr=w0+dw*xarr print warr.min(), warr.max() print hdu[1].data[yc, 1462], hdu[2].data[yc,1462] warr, madata, var = skysub_region(warr, hdu[1].data, hdu[2].data, hdu[3].data, yc, oy, dy) print warr.min(), warr.max() w, f, e = masked_extract(warr, madata[yc-oy:yc+oy, :], var[yc-oy:yc+oy, :]) print yc ofile='%s.%s_%i_%i.txt' % (target, extract_date(img), extract_number(img), yc) write_spectra(ofile, w, f, e) if calfile is not None: extfile=iraf.osfn("pysalt$data/site/suth_extinct.dat") speccal(ofile, ofile.replace("txt", "spec"), calfile, extfile, airmass, exptime, clobber=True, logfile='salt.log', verbose=True) spec_list=[ofile, airmass, exptime, propcode] return spec_list
def extract_spectra(img, yc=None, oy=10, dy=50, minsize=5, thresh=3, findobject=False, niter=5, calfile=None, smooth=False, maskzeros=False, clobber=True): """Create a list of spectra for each of the objects in the images""" #okay we need to identify the objects for extraction and identify the regions for sky extraction #first find the objects in the image #skynormalize the data #specslitnormalize(img, 'n'+img, '', response=None, response_output=None, order=3, conv=1e-2, niter=20, # startext=0, clobber=False,logfile='salt.log',verbose=True) print 'Extract Spectra from ', img hdu = pyfits.open(img) target = hdu[0].header['OBJECT'].replace(' ', '') propcode = hdu[0].header['PROPID'] airmass = hdu[0].header['AIRMASS'] exptime = hdu[0].header['EXPTIME'] if smooth: data = smooth_data(hdu[1].data) else: data = hdu[1].data #replace the zeros with the average from the frame if maskzeros: mean, std = iterstat(data[data > 0]) #rdata=mean np.random.normal(mean, std, size=data.shape) print mean, std data[data <= 0] = mean #rdata[data<=0] #use manual intervention to get section if findobject: section = findobj.findObjects(data, method='median', specaxis=1, minsize=minsize, thresh=thresh, niter=niter) if yc is None and len(section) > 0: yc = np.mean(section[0]) elif len(section) > 0: diff = 1e6 for i in range(len(section)): y = np.mean(section[i]) if abs(y - yc) < diff: bestyc = y diff = abs(y - yc) yc = bestyc if yc is None: os.system('ds9 %s &' % img) print len(hdu) if len(hdu) == 2: print 'Using basic extraction' if yc is None: y1 = int(raw_input('y1:')) y2 = int(raw_input('y2:')) sy1 = int(raw_input('sky y1:')) sy2 = int(raw_input('sky y2:')) ap_list = extract(hdu, method='normal', section=[(y1, y2)], minsize=minsize, thresh=thresh, convert=True) sk_list = extract(hdu, method='normal', section=[(sy1, sy2)], minsize=minsize, thresh=thresh, convert=True) ap_list[0].ldata = ap_list[0].ldata - float(y2 - y1) / ( sy2 - sy1) * sk_list[0].ldata ofile = '%s.%s_%i.txt' % (target, extract_date(img), extract_number(img)) write_extract(ofile, [ap_list[0]], outformat='ascii', clobber=clobber) w, f, e = np.loadtxt(ofile, usecols=(0, 1, 2), unpack=True) w, f, e = cleanspectra(w, f, e, neg=True) m = (w > 3900) * (w < 8100) write_spectra(ofile, w[m], f[m], e[m]) else: print 'Using advanced extraction' if yc is None: yc = int(raw_input('yc:')) w0 = hdu[1].header['CRVAL1'] dw = hdu[1].header['CD1_1'] xarr = np.arange(len(hdu[1].data[0])) warr = w0 + dw * xarr print warr.min(), warr.max() print hdu[1].data[yc, 1462], hdu[2].data[yc, 1462] warr, madata, var = skysub_region(warr, hdu[1].data, hdu[2].data, hdu[3].data, yc, oy, dy) print warr.min(), warr.max() w, f, e = masked_extract(warr, madata[yc - oy:yc + oy, :], var[yc - oy:yc + oy, :]) print yc ofile = '%s.%s_%i_%i.txt' % (target, extract_date(img), extract_number(img), yc) write_spectra(ofile, w, f, e) if calfile is not None: extfile = iraf.osfn("pysalt$data/site/suth_extinct.dat") speccal(ofile, ofile.replace("txt", "spec"), calfile, extfile, airmass, exptime, clobber=True, logfile='salt.log', verbose=True) spec_list = [ofile, airmass, exptime, propcode] return spec_list
def specred(rawdir, prodir, imreduce=True, specreduce=True, calfile=None, lamp='Ar', automethod='Matchlines', skysection=[800,1000], cleanup=True): print rawdir print prodir #get the name of the files infile_list=glob.glob(rawdir+'*.fits') infiles=','.join(['%s' % x for x in infile_list]) #get the current date for the files obsdate=os.path.basename(infile_list[0])[1:9] print obsdate #set up some files that will be needed logfile='spec'+obsdate+'.log' flatimage='FLAT%s.fits' % (obsdate) dbfile='spec%s.db' % obsdate #create the observation log obs_dict=obslog(infile_list) if imreduce: #prepare the data saltprepare(infiles, '', 'p', createvar=False, badpixelimage='', clobber=True, logfile=logfile, verbose=True) #bias subtract the data saltbias('pP*fits', '', 'b', subover=True, trim=True, subbias=False, masterbias='', median=False, function='polynomial', order=5, rej_lo=3.0, rej_hi=5.0, niter=10, plotover=False, turbo=False, clobber=True, logfile=logfile, verbose=True) #gain correct the data saltgain('bpP*fits', '', 'g', usedb=False, mult=True, clobber=True, logfile=logfile, verbose=True) #cross talk correct the data saltxtalk('gbpP*fits', '', 'x', xtalkfile = "", usedb=False, clobber=True, logfile=logfile, verbose=True) #cosmic ray clean the data #only clean the object data for i in range(len(infile_list)): if obs_dict['CCDTYPE'][i].count('OBJECT') and obs_dict['INSTRUME'][i].count('RSS'): img='xgbp'+os.path.basename(infile_list[i]) saltcrclean(img, img, '', crtype='edge', thresh=5, mbox=11, bthresh=5.0, flux_ratio=0.2, bbox=25, gain=1.0, rdnoise=5.0, fthresh=5.0, bfactor=2, gbox=3, maxiter=5, multithread=True, clobber=True, logfile=logfile, verbose=True) #flat field correct the data flat_imgs='' for i in range(len(infile_list)): if obs_dict['CCDTYPE'][i].count('FLAT'): if flat_imgs: flat_imgs += ',' flat_imgs += 'xgbp'+os.path.basename(infile_list[i]) if len(flat_imgs)!=0: saltcombine(flat_imgs,flatimage, method='median', reject=None, mask=False, \ weight=True, blank=0, scale='average', statsec='[200:300, 600:800]', lthresh=3, \ hthresh=3, clobber=True, logfile=logfile, verbose=True) saltillum(flatimage, flatimage, '', mbox=11, clobber=True, logfile=logfile, verbose=True) saltflat('xgbpP*fits', '', 'f', flatimage, minflat=500, clobber=True, logfile=logfile, verbose=True) else: flats=None imfiles=glob.glob('cxgbpP*fits') for f in imfiles: shutil.copy(f, 'f'+f) #mosaic the data geomfile=iraf.osfn("pysalt$data/rss/RSSgeom.dat") saltmosaic('fxgbpP*fits', '', 'm', geomfile, interp='linear', cleanup=True, geotran=True, clobber=True, logfile=logfile, verbose=True) #clean up the images if cleanup: for f in glob.glob('p*fits'): os.remove(f) for f in glob.glob('bp*fits'): os.remove(f) for f in glob.glob('gbp*fits'): os.remove(f) for f in glob.glob('xgbp*fits'): os.remove(f) for f in glob.glob('fxgbp*fits'): os.remove(f) #set up the name of the images if specreduce: for i in range(len(infile_list)): if obs_dict['OBJECT'][i].upper().strip()=='ARC': lamp=obs_dict['LAMPID'][i].strip().replace(' ', '') arcimage='mfxgbp'+os.path.basename(infile_list[i]) lampfile=iraf.osfn("pysalt$data/linelists/%s.txt" % lamp) specidentify(arcimage, lampfile, dbfile, guesstype='rss', guessfile='', automethod=automethod, function='legendre', order=5, rstep=100, rstart='middlerow', mdiff=10, thresh=3, niter=5, inter=True, clobber=True, logfile=logfile, verbose=True) specrectify(arcimage, outimages='', outpref='x', solfile=dbfile, caltype='line', function='legendre', order=3, inttype='interp', w1=None, w2=None, dw=None, nw=None, blank=0.0, clobber=True, logfile=logfile, verbose=True) objimages='' for i in range(len(infile_list)): if obs_dict['CCDTYPE'][i].count('OBJECT') and obs_dict['INSTRUME'][i].count('RSS'): if objimages: objimages += ',' objimages+='mfxgbp'+os.path.basename(infile_list[i]) if specreduce: #run specidentify on the arc files specrectify(objimages, outimages='', outpref='x', solfile=dbfile, caltype='line', function='legendre', order=3, inttype='interp', w1=None, w2=None, dw=None, nw=None, blank=0.0, clobber=True, logfile=logfile, verbose=True) #create the spectra text files for all of our objects spec_list=[] for img in objimages.split(','): spec_list.extend(createspectra('x'+img, obsdate, smooth=False, skysection=skysection, clobber=True)) print spec_list #determine the spectrophotometric standard extfile=iraf.osfn('pysalt$data/site/suth_extinct.dat') for spec, am, et, pc in spec_list: if pc=='CAL_SPST': stdstar=spec.split('.')[0] print stdstar, am, et stdfile=iraf.osfn('pysalt$data/standards/spectroscopic/m%s.dat' % stdstar.lower().replace('-', '_')) print stdfile ofile=spec.replace('txt', 'sens') calfile=ofile #assumes only one observations of a SP standard specsens(spec, ofile, stdfile, extfile, airmass=am, exptime=et, stdzp=3.68e-20, function='polynomial', order=3, thresh=3, niter=5, clobber=True, logfile='salt.log',verbose=True) for spec, am, et, pc in spec_list: if pc!='CAL_SPST': ofile=spec.replace('txt', 'spec') speccal(spec, ofile, calfile, extfile, airmass=am, exptime=et, clobber=True, logfile='salt.log',verbose=True) #clean up the spectra for bad pixels cleanspectra(ofile)