Example #1
0
def initDirs():
    #bias
    createDir(os.path.join(OUTPUTDIR, "bias"))
    for f in FILTERS:
        #flat
        fdir = os.path.join(OUTPUTDIR, "flat", f)
        createDir(fdir)
        #object
        fdir = os.path.join(OUTPUTDIR, "object", f)
        createDir(fdir)
    iraf.hselect.setParam("fields", "$I")
    #copy bias files
    iraf.hselect.setParam("images", IMGDIR + "*.fits")
    iraf.hselect.setParam("expr", 'IMAGETYP=="bias"')
    for imgname in iraf.hselect(Stdout=1):
        shutil.copy(imgname, os.path.join(OUTPUTDIR, "bias"))
    for f in FILTERS:
        #copy flat files
        iraf.hselect.setParam("expr", 'IMAGETYP=="flat"&&INSFILTE=="%s"' % f)
        for imgname in iraf.hselect(Stdout=1):
            print("imgname to copy for flat files and filter %s is %s" %
                  (f, imgname))
            shutil.copy(imgname, os.path.join(OUTPUTDIR, "flat", f))
        #copy object files
        iraf.hselect.setParam("expr", 'IMAGETYP=="object"&&INSFILTE=="%s"' % f)
        for imgname in iraf.hselect(Stdout=1):
            shutil.copy(imgname, os.path.join(OUTPUTDIR, "object", f))
Example #2
0
def listobs(expr):
    """
    Saves a list of all observations in the working directory into obsmenu.list file.
    INPUT:
    ------
    expr: str
        Expression to filter the data. e.g.: expr = 'OWNER=="YALE-03A-0001"' or expr = 'OWNER=="STANDARD"'
        The keyword OWNER is from the header of the fits file.
    """
    # Clean intermediate step products if there are any leftovers from previous run.
    tobedeleted = ['smartsdates', 'objectmenutemp.list', 'objectmenu.list']
    for i in tobedeleted:
        if i in glob.glob(i):
            os.system('rm ' + i)

    # Find all the sources that agrees with the selection criteria and write them into obsmenutemp.list file
    iraf.hselect(images='rccd*',
                 fields='$I,CCDFLTID,DATE-OBS,TIME-OBS,OBJECT',
                 expr=expr,
                 Stdout='obsmenutemp.list')
    # Here create the smarts dates from file names.
    os.system('awk "{print $1}" obsmenutemp.list | cut -c 5-10 > smartsdates')
    # Paste the smarts dates into the first column
    os.system('paste smartsdates obsmenutemp.list > obsmenu.list')
    # Remove intermedate step file
    os.system('rm smartsdates obsmenutemp.list')

    print "created obsmenu.list file."
Example #3
0
def initDirs():
	#bias
	createDir(os.path.join(OUTPUTDIR, "bias"))
	for f in FILTERS:
		#flat
		fdir = 	os.path.join(OUTPUTDIR, "flat", f)
		createDir(fdir)
		#object
		fdir = 	os.path.join(OUTPUTDIR, "object", f)
		createDir(fdir)
	iraf.hselect.setParam("fields", "$I")
	#copy bias files
	iraf.hselect.setParam("images", IMGDIR + "*.fits")
	iraf.hselect.setParam("expr", 'IMAGETYP=="bias"')
	for imgname in iraf.hselect(Stdout=1):
		shutil.copy(imgname, os.path.join(OUTPUTDIR, "bias"))
	for f in FILTERS:
		#copy flat files	
		iraf.hselect.setParam("expr", 'IMAGETYP=="flat"&&INSFILTE=="%s"' % f)
		for imgname in iraf.hselect(Stdout=1):
			print("imgname to copy for flat files and filter %s is %s" % (f, imgname))
			shutil.copy(imgname, os.path.join(OUTPUTDIR, "flat", f))
		#copy object files	
		iraf.hselect.setParam("expr", 'IMAGETYP=="object"&&INSFILTE=="%s"' % f)
		for imgname in iraf.hselect(Stdout=1):
			shutil.copy(imgname, os.path.join(OUTPUTDIR, "object", f))
def vhelio_correct(WORK_DIR):
  print '\n + VHELIO correction\n'
    
  for obj in observations[WORK_DIR]['objects']:
    obj = obj+'.ec'

    utm = iraf.hselect(images=obj, fields='UTMIDDLE', Stdout=1, expr='yes')
    year = utm[0][:4]
    month = utm[0][5:7]
    day = utm[0][8:10]
    h = int(utm[0][11:13])
    m = int(utm[0][14:16])
    s = int(utm[0][17:19])

    ra = iraf.hselect(images=obj, fields='RA', Stdout=1, expr='yes')[0].split(':')
    dec = iraf.hselect(images=obj, fields='DEC', Stdout=1, expr='yes')[0].split(':')
    ra = float(ra[0])+int(ra[1])/60.+float(ra[2])/3600.
    dec = float(dec[0])+int(dec[1])/60.+float(dec[2])/3600.
    
    shutil.copy(obj+'.fits', obj+'.vh.fits')
    iraf.hedit(images=obj+'.vh', fields='UT', value=h+m/60.+s/3600., add='yes', verify='no')
    iraf.hedit(images=obj+'.vh', fields='EPOCH', value=year, add='yes', verify='no')
    iraf.rvcorrect(images=obj+'.vh', imupdat='yes', epoch=year, observa='ekar', year=year, month=month, day=day, ut=h+m/60.+s/3600., ra=ra, dec=dec)
    iraf.dopcor(input=obj+'.vh', redshift='-vhelio', isveloc='yes', dispers='yes')

    obj = obj+'.nosky'
    shutil.copy(obj+'.fits', obj+'.vh.fits')
    iraf.hedit(images=obj+'.vh', fields='UT', value=h+m/60.+s/3600., add='yes', verify='no')
    iraf.hedit(images=obj+'.vh', fields='EPOCH', value=year, add='yes', verify='no')
    iraf.rvcorrect(images=obj+'.vh', imupdat='yes', epoch=year, observa='ekar', year=year, month=month, day=day, ut=h+m/60.+s/3600., ra=ra, dec=dec)
    iraf.dopcor(input=obj+'.vh', redshift='-vhelio', isveloc='yes', dispers='yes')
def remove_cosmics(WORK_DIR):
  print '\n + Removing cosmics\n'
  
  overdo = 5
  
  for obj in observations[WORK_DIR]['objects']+['mastercalib']:
    obj = obj+'.fits'

    rogain = iraf.hselect(images=obj, fields="GAIN", exp='yes', Stdout=1)
    ronoise = iraf.hselect(images=obj, fields="RDNOISE", exp='yes', Stdout=1)
    array, header = cosmics.fromfits(obj)
    exptime = float(iraf.hselect(images=obj, fields='EXPTIME', Stdout=1, expr='yes')[0])

    if exptime <= 600:
      c = cosmics.cosmicsimage(array, gain=float(rogain[0]), readnoise=float(ronoise[0]), sigclip=30.0+overdo, sigfrac=0.5, objlim=3.0)
    elif exptime <= 1200:
      c = cosmics.cosmicsimage(array, gain=float(rogain[0]), readnoise=float(ronoise[0]), sigclip=20.0+overdo, sigfrac=0.5, objlim=3.0)
    else:
      c = cosmics.cosmicsimage(array, gain=float(rogain[0]), readnoise=float(ronoise[0]), sigclip=15.0+overdo, sigfrac=0.5, objlim=3.0)
    
    c.run(maxiter = 4, xb=5.0, yb=3.0)
    cosmics.tofits(obj, c.cleanarray, header)
    cosmics.tofits("cosmics/" + obj, c.mask, header)

  iraf.imsum(input='cosmics/*.fits', output='cosmics/sum.fits', option='sum')
  os.system('ds9 cosmics/sum.fits')
Example #6
0
def correct_coordinates(list_files, radius, twomassmag=15.):
    # makes a copy of the original file and corrects the coordinates of the copies, using the 2MASS catalogue.
    for image in list_files[:]:
        object=iraf.hselect(image+'[0]','OBJECT', 'yes', Stdout=1)[0]
        filter = iraf.hselect(image+'[0]','FILTER', 'yes', Stdout=1)[0]
        catalogue=os.path.join(os.path.dirname(image),"2mass_"+str(object)+'_'+str(filter)+'_'+str(twomassmag)+".cat")
        print catalogue
        correct_wcs(image, clobber=True, radius=radius, minmag=twomassmag)
        twomass_to_ds9(catalogue)
Example #7
0
def hselToDf(color,frameFlavor='raw'):
	'''
	this function packs the file name, observation time, and YYMMDD date 
	of smarts fits files into a pandas data frame
	color is either 'J' or 'H', the broadbandfilter you're interested in
	if frameFlavor='raw' this function is hard coded to look in either the Hraw/ or Jraw/
	if frameFlavor='flat' this function is hard coded to look in irflats/
	it then pickles the data frame
	'''
	#prepare a file handle. this will be a temporary storing space for the data
	fileHandle=StringIO.StringIO()
	#use iraf hselect to spit out file name and obs time for all files
	#save in the file handle
	if frameFlavor=='raw':
		iraf.hselect(color.upper()+"raw/*fits", fields='$I,JD', expr='yes', Stdout=fileHandle)
	elif frameFlavor=='flat':
		iraf.hselect("irflats/*"+color.lower()+"*fits", fields='$I,JD', expr='yes', Stdout=fileHandle)
	else:
		print "use either 'raw' or 'flat' for frameFlavor variable"
		return

	fileHandle.seek(0)
	#stick the file handle into a pandas parser to read it into a data frame
	fileTable=pd.io.parsers.read_fwf(fileHandle, names=['file','JulianDate'])
	fileHandle.close()

	#Try to force the JulianDate data to floats. This wont work if its corrupted somehow,
	#or has a bad value printed, like a string.
	try:
		fileTable['JulianDate']=fileTable['JulianDate'].astype(float)
		#grab the YYMMDD dates from the file name and list these in a column
		if frameFlavor=='raw':
			fileTable['Date']=fileTable.file.apply(lambda i: i[-16:-10]).astype(int)
			#check for nulls in the dataframe
			if checkNullColumn(fileTable):
				#pickle the data frame
				fileTable.to_pickle(color.upper()+'rawList.pkl')
				return
			else:
				return
		else:
			if checkNullColumn(fileTable):
				fileTable['Date']=fileTable.file.apply(lambda i: i[-17:-11]).astype(int)
				#pickle the data frame
				fileTable.to_pickle(color.upper()+'flatList.pkl')	
				return
			else:
				return
	except ValueError:
		print "encountered a value error. There is a bad value in either the JD field of a header, or a nonstandard filename"
		print "fix these and try again"
		return
Example #8
0
def get_ao_performance_data():
    # Retrieve information from image headers
    outfile = workdir + 'data/ao_perform.txt'

    ir.images()
    ir.imutil()
    ir.hselect('*_img.fits', 'DATAFILE,DATE-OBS,UTC,AOLBFWHM,LGRMSWF', 'yes', Stdout=outfile)

    # Retrieve MASS/DIMM data files
    # get_mass_dimm.go('20081021')   # this one doesn't exist
    get_mass_dimm.go('20100815')
    get_mass_dimm.go('20100828')
    get_mass_dimm.go('20100829')
Example #9
0
def cor_airmass(lstfile):
    f = open(lstfile)
    l = f.readlines()
    f.close()
    l = [tmp.split('\n')[0] for tmp in l]
    fitlst = ['awftbo' + tmp for tmp in l]
    for fitname in fitlst:
        if os.path.isfile(fitname):
            fit = pyfits.open(fitname)
            objname = fit[0].header['object'].replace('_', ' ').split()[0]
            print(fitname + ' ' + objname)
            objname_new = find_normal_objname(objname)
            if len(objname_new) == 0:
                objname_new = raw_input('please input object name:')
            radec = findradec(objname_new)
            if len(radec) == 0:
                radec = raw_input('please input ra dec of objname:')
                radec = radec.split()
            fitextnum = len(fit)
            fit.close()
            for lay in range(fitextnum):
                airold = iraf.hselect(images = fitname + '[%i]' % lay, fields = 'airmass', expr = 'yes', Stdout = 1)
                airold = float(airold[0])
                print(fitname + ' ' + objname + ' ' + str(lay) + ' airmass old: ' + str(airold))
                fitnamelay = fitname + '[%i]' % lay
                iraf.hedit(images = fitnamelay, fields = 'airold', 
                    value = airold, add = 'yes', addonly = 'yes', delete = 'no', 
                    verify = 'no', show = 'yes', update = 'yes')
                iraf.hedit(images = fitnamelay, fields = 'sname', 
                    value = objname_new, add = 'yes', addonly = 'yes', delete = 'no', 
                    verify = 'no', show = 'yes', update = 'yes')
                iraf.hedit(images = fitnamelay, fields = 'RA', 
                    value = radec[0], add = 'yes', addonly = 'yes', delete = 'no', 
                    verify = 'no', show = 'yes', update = 'yes')
                iraf.hedit(images = fitnamelay, fields = 'DEC', 
                    value = radec[1], add = 'yes', addonly = 'yes', delete = 'no', 
                    verify = 'no', show = 'yes', update = 'yes')
                iraf.twodspec()
                stdpath = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'standarddir' + os.sep
                iraf.longslit(dispaxis = 2, nsum = 1, observatory = 'Lijiang', 
                    extinction = 'onedstds$LJextinct.dat', caldir = stdpath)
                iraf.setairmass(images = fitnamelay,
                    observatory = 'Lijiang', intype = 'beginning', 
                    outtype = 'effective', ra = 'ra', dec = 'dec', 
                    equinox = 'epoch', st = 'lst', ut = 'date-obs', 
                    date = 'date-obs', exposure = 'exptime', airmass = 'airmass', 
                    utmiddle = 'utmiddle', scale = 750.0, show = 'yes', 
                    override = 'yes', update = 'yes')
                print('name airmass_new airmass_old')
                iraf.hselect(fitnamelay, fields = '$I,airmass,airold', 
                             expr = 'yes')
Example #10
0
def runsex(img, fwhm, thresh, pix_scale):  ## run_sextractor  fwhm in pixel
    import lsc

    mina = 5.
    seeing = fwhm * pix_scale

    cdef = open(lsc.__path__[0] + '/standard/sex/default2.param')
    riga = cdef.readlines()
    cparam = []
    for r in riga:
        if r[0] != '#' and len(r.strip()) > 0: \
                cparam.append(r.split()[0])

    pid = subprocess.Popen("sex '" + img + ".fits[0]' -catalog_name tmp.cat" + \
                           " -c  " + lsc.__path__[0] + '/standard/sex/default2.sex' \
                                                       " -PARAMETERS_NAME " + lsc.__path__[
                               0] + "/standard/sex/default2.param" + \
                           " -STARNNW_NAME " + lsc.__path__[0] + "/standard/sex/default2.nnw" + \
                           " -PIXEL_SCALE " + str(pix_scale) + \
                           " -DETECT_MINAREA " + str(mina) + \
                           " -DETECT_THRESH  " + str(thresh) + \
                           " -ANALYSIS_THRESH  " + str(thresh) + \
                           " -PHOT_FLUXFRAC 0.5" + \
                           " -SEEING_FWHM " + str(seeing),
                           stdout=subprocess.PIPE, shell=True)

    output, error = pid.communicate()

    csex = open("tmp.cat")
    tab = {}
    riga = csex.readlines()
    for k in cparam:
        tab[k] = []
    for r in riga:
        if r[0] != '#':
            for i in range(len(cparam)):
                tab[cparam[i]].append(float(r.split()[i]))
    for k in cparam:
        tab[k] = np.array(tab[k])

    xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1) \
        [0].split()

    xcoo, ycoo, ra, dec, magbest, classstar, fluxrad, bkg = [], [], [], [], [], [], [], []
    for i in range(len(tab['X_IMAGE'])):
        x, y = tab['X_IMAGE'][i], tab['Y_IMAGE'][i]
        if 5 < x < int(xdim) - 5 and 5 < y < int(ydim) - 5:  # trim border
            xcoo.append(x)
            ycoo.append(y)
            ra.append(tab['X_WORLD'][i])
            dec.append(tab['Y_WORLD'][i])
            magbest.append(tab['MAG_BEST'][i])
            classstar.append(tab['CLASS_STAR'][i])
            fluxrad.append(tab['FLUX_RADIUS'][i])
            bkg.append(tab['BACKGROUND'][i])

    return np.array(xcoo), np.array(ycoo), np.array(ra), np.array(dec), np.array(magbest), \
           np.array(classstar), np.array(fluxrad), np.array(bkg)
Example #11
0
def showImageProperties():
	iraf.hselect.setParam("images", IMGDIR + "*.fits")
	iraf.hselect.setParam("fields", "$I, IMAGETYP, INSFILTE, BIASSEC, DATASEC")
	iraf.hselect.setParam("expr", 'yes')
	print "start"
	print "filename\ttype\tfilter\tbiassec\tdatasec"
	for r in iraf.hselect(Stdout=1):
		print r
	print "end"
Example #12
0
def showImageProperties():
    iraf.hselect.setParam("images", IMGDIR + "*.fits")
    iraf.hselect.setParam("fields", "$I, IMAGETYP, INSFILTE, BIASSEC, DATASEC")
    iraf.hselect.setParam("expr", 'yes')
    print "start"
    print "filename\ttype\tfilter\tbiassec\tdatasec"
    for r in iraf.hselect(Stdout=1):
        print r
    print "end"
Example #13
0
def runsex(img, fwhm, thresh, pix_scale):  ## run_sextractor  fwhm in pixel
    import lsc

    mina = 5.
    seeing = fwhm * pix_scale

    cdef = open(lsc.__path__[0] + '/standard/sex/default2.param')
    riga = cdef.readlines()
    cparam = []
    for r in riga:
        if r[0] != '#' and len(r.strip()) > 0: \
                cparam.append(r.split()[0])

    pid = subprocess.Popen("sex '" + img + ".fits[0]' -catalog_name tmp.cat" + \
                           " -c  " + lsc.__path__[0] + '/standard/sex/default2.sex' \
                                                       " -PARAMETERS_NAME " + lsc.__path__[
                               0] + "/standard/sex/default2.param" + \
                           " -STARNNW_NAME " + lsc.__path__[0] + "/standard/sex/default2.nnw" + \
                           " -PIXEL_SCALE " + str(pix_scale) + \
                           " -DETECT_MINAREA " + str(mina) + \
                           " -DETECT_THRESH  " + str(thresh) + \
                           " -ANALYSIS_THRESH  " + str(thresh) + \
                           " -PHOT_FLUXFRAC 0.5" + \
                           " -SEEING_FWHM " + str(seeing),
                           stdout=subprocess.PIPE, shell=True)

    output, error = pid.communicate()

    csex = open("tmp.cat")
    tab = {}
    riga = csex.readlines()
    for k in cparam: tab[k] = []
    for r in riga:
        if r[0] != '#':
            for i in range(len(cparam)):
                tab[cparam[i]].append(float(r.split()[i]))
    for k in cparam: tab[k] = np.array(tab[k])

    xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1) \
        [0].split()

    xcoo, ycoo, ra, dec, magbest, classstar, fluxrad, bkg = [], [], [], [], [], [], [], []
    for i in range(len(tab['X_IMAGE'])):
        x, y = tab['X_IMAGE'][i], tab['Y_IMAGE'][i]
        if 5 < x < int(xdim) - 5 and 5 < y < int(ydim) - 5:  # trim border
            xcoo.append(x)
            ycoo.append(y)
            ra.append(tab['X_WORLD'][i])
            dec.append(tab['Y_WORLD'][i])
            magbest.append(tab['MAG_BEST'][i])
            classstar.append(tab['CLASS_STAR'][i])
            fluxrad.append(tab['FLUX_RADIUS'][i])
            bkg.append(tab['BACKGROUND'][i])

    return np.array(xcoo), np.array(ycoo), np.array(ra), np.array(dec), np.array(magbest), \
           np.array(classstar), np.array(fluxrad), np.array(bkg)
Example #14
0
def showObjProp(ft):
	print "start"
	print "object\tzenith dist\tright asc\tdecl"
	iraf.hselect.setParam("images", os.path.join(OUTPUTDIR, "object", ft) + "/*.fits")
	iraf.hselect.setParam("fields", "OBJECT,ZD,RA,DEC")
	iraf.hselect.setParam("expr", 'yes')
	print "start"
	for r in iraf.hselect(Stdout=1):
		print r
	print "end"
Example #15
0
def bias_correct(file_list,xmin='18',xmax='4111',ymin='350',ymax='1570',bindefault=1):
    irf_prm.set_ccdproc(iraf.ccdproc)
    biassecs = {1:'[4114:4142,1:1896]',2:'[4114:4142,1:1896]',3:'[4114:4142,1:1896]',4:'[4114:4142,1:1896]'} #NEED UPDATED
    #iraf.ccdproc.biassec = biassecs[binsize]
    iraf.ccdproc.trimsec = '['+xmin+':'+xmax+','+ymin+':'+ymax+']'
    for ff in file_list:
        comm = scnd.get_comment(ff,'PARAM18')
        if comm == '1 / Serial Binning,Pixels':
            binsize = iraf.hselect(ff,'PARAM18','yes')
        else:
            param_name = scnd.find_param_with_comment(ff,'1 / Serial Binning,Pixels')
            if param_name == 'NullReturn':
                binsize=bindefault
            else:
                binsize = iraf.hselect(ff,param_name,'yes')
        iraf.ccdproc.biassec = biassecs[binsize]
        output = 'b'+ff
        iraf.ccdproc(images=ff,output=output,ccdtype = "")
    return
Example #16
0
def sort_frames(targetdir):

    """
    This checks that all objects can be sorted in to AB pairs.

    """

    print 'In directory ' + targetdir
    print 'Sorting Frames...'

    names = np.array([name + '[1]' for name in os.listdir(targetdir) if (name.endswith('.fit')) & (name.startswith('r'))])


    mjd = np.array([iraf.hselect( os.path.join(targetdir,f), 'MJD-OBS', 'yes', Stdout=1)[0] for f in names])

    # Sort files names in order of time of observation
    names = names[np.argsort(mjd)]

    for n in names: print n.replace('.fit[1]','')

    # Check everything is in the right order
    decoff = np.asarray([iraf.hselect(os.path.join(targetdir,f) , 'DECOFF', 'yes', Stdout=1)[0] for f in names],dtype='float')

    pos = np.repeat('A',len(names))
    pos[ decoff < -1.] = 'B'

    if len(names) % 2 != 0:
        print 'Odd number of nods!'
        return False

    for i in range(int(len(names)/2)):

        pair = pos[i*2] + pos[(i*2)+1]
        print pair

        if (pair == 'AA') or (pair == 'BB'):
            print 'Mismatched pairs!'
            return False

    print 'Everything is okay!'

    return True
Example #17
0
def showObjProp(ft):
    print "start"
    print "object\tzenith dist\tright asc\tdecl"
    iraf.hselect.setParam("images",
                          os.path.join(OUTPUTDIR, "object", ft) + "/*.fits")
    iraf.hselect.setParam("fields", "OBJECT,ZD,RA,DEC")
    iraf.hselect.setParam("expr", 'yes')
    print "start"
    for r in iraf.hselect(Stdout=1):
        print r
    print "end"
Example #18
0
def showFlatProp():
	print "start showFlatProp"
	for ft in FILTERS:	
		print("start %s" % ft)
		print "object\texptime"
		iraf.hselect.setParam("images", os.path.join(OUTPUTDIR, "flat", ft) + "/*.fits")
		iraf.hselect.setParam("fields", "$I,OBJECT,EXPTIME")
		iraf.hselect.setParam("expr", 'yes')
		for r in iraf.hselect(Stdout=1, mode="h"):
			print r
	print "end showFlatProp"
Example #19
0
def showObjProp():
	print "start showObjProp"
	for ft in FILTERS:	
		print("start %s" % ft)
		print "object\tzenith dist\tright asc\tdecl\texptime\tairmass\tUT"
		iraf.hselect.setParam("images", os.path.join(OUTPUTDIR, "object", ft) + "/*.fits")
		iraf.hselect.setParam("fields", "$I,OBJECT,ZD,RA,DEC,EXPTIME,AIRMASS,UT")
		iraf.hselect.setParam("expr", 'yes')
		for r in iraf.hselect(Stdout=1, mode="h"):
			print r
	print "end showObjProp"
Example #20
0
def showTimeConv():
	iraf.hselect.setParam("images", IMGDIR + "*.fits")
	iraf.hselect.setParam("fields", "OBJECT,ST,UT,INSFILTE,DATE-OBS")
	iraf.hselect.setParam("expr", 'yes')
	print "start"
	longit = 0.27925
	for rstr in iraf.hselect(Stdout=1, mode="h"):
		r = rstr.split("\t")
		#ST - UT - RA
		print r[0] +"\t"+ r[1] + "\t" + r[2] + "\t" + r[3] + "\t" + str(timeToSec(r[1]) - timeToSec(r[2])) +"\t"+ solarTime(r[4], r[2], longit).strftime("%Y-%m-%d %H:%M:%S")
	print "end"
Example #21
0
def showFlatProp():
    print "start showFlatProp"
    for ft in FILTERS:
        print("start %s" % ft)
        print "object\texptime"
        iraf.hselect.setParam("images",
                              os.path.join(OUTPUTDIR, "flat", ft) + "/*.fits")
        iraf.hselect.setParam("fields", "$I,OBJECT,EXPTIME")
        iraf.hselect.setParam("expr", 'yes')
        for r in iraf.hselect(Stdout=1, mode="h"):
            print r
    print "end showFlatProp"
Example #22
0
    def queryKeywords(self, file, keywords):
        '''
		Queries keywords from header. Can be used to get e.g. exposure times
		from multiple raw files with wildcard in file name.
		Returns:
		Queried keywords
		'''
        self.file = file
        self.keywords = keywords

        keyw = I.hselect(self.file, self.keywords, 'yes', Stdout=1)
        return keyw
Example #23
0
	def queryKeywords(self, file, keywords):
		'''
		Queries keywords from header. Can be used to get e.g. exposure times
		from multiple raw files with wildcard in file name.
		Returns:
		Queried keywords
		'''
		self.file = file
		self.keywords = keywords		

		keyw = I.hselect(self.file, self.keywords, 'yes', Stdout=1)
		return keyw
Example #24
0
def showTimeConv():
    iraf.hselect.setParam("images", IMGDIR + "*.fits")
    iraf.hselect.setParam("fields", "OBJECT,ST,UT,INSFILTE,DATE-OBS")
    iraf.hselect.setParam("expr", 'yes')
    print "start"
    longit = 0.27925
    for rstr in iraf.hselect(Stdout=1, mode="h"):
        r = rstr.split("\t")
        #ST - UT - RA
        print r[0] + "\t" + r[1] + "\t" + r[2] + "\t" + r[3] + "\t" + str(
            timeToSec(r[1]) - timeToSec(r[2])) + "\t" + solarTime(
                r[4], r[2], longit).strftime("%Y-%m-%d %H:%M:%S")
    print "end"
Example #25
0
def standard():
    f = open(standoutput)
    l = f.readlines()
    f.close()
    namelst = [i.split('\n')[0] for i in l]
    temp = ''
    for i in namelst:
        temp = temp + i + ','
    temp = temp[0:-1]

    #    for i in xrange(len(namelst)):
    #        iraf.hselect(images = namelst[i], fields = '$I,object', expr = 'yes')
    #    standname = raw_input('please input standard star name:')
    #    print 'standard star name:', standname
    iraf.twodspec()
    iraf.longslit(dispaxis=2,
                  nsum=1,
                  observatory='Lijiang',
                  extinction='onedstds$LJextinct.dat',
                  caldir='onedstds$ctiocal/')
    for i in xrange(len(namelst)):
        print '+' * 10, namelst[i]
        iraf.hselect(images=namelst[i], fields='$I,object', expr='yes')
        standname = raw_input('please input standard star name:')
        print 'standard star name:', standname
        #iraf.standard(input = namelst[i], output = namelst[i].split('.')[0] + '.std',
        #        samestar = 'yes', interact = 'yes', star_name = standname, airmass = '',
        #        exptime = '', extinction = 'onedstds$LJextinct.dat',
        #        caldir = 'onedstds$ctiocal/')
        iraf.standard(input=namelst[i],
                      output='std',
                      samestar='yes',
                      interact='yes',
                      star_name=standname,
                      airmass='',
                      exptime='',
                      extinction='onedstds$LJextinct.dat',
                      caldir='onedstds$ctiocal/')
Example #26
0
def pyhselect(img, hdrlist, lst):

    #<--- count number of lines in the list and backup the orignal file --->

    # open image list
    if lst:
        try:
            fimg = open(img)
        except IOError:
            print >> sys.stderr, 'cannot open %s' % img
            return 1

    # open header list
    try:
        fhdr = open(hdrlist)
    except IOError:
        print >> sys.stderr, 'cannot open %s' % hdrlist
        return 1

    # read header list
    hdr = '$I'
    for line in fhdr:
        if not line.startswith('#'):
            param = line.split()
            hdr = hdr + ',' + param[0]
    fhdr.close()

    if lst:
        for line in fimg:
            if not line.startswith('#'):
                im_file = line[:-1]
                iraf.hselect(im_file, hdr, expr='yes')
        fimg.close()
    else:
        #print hdr
        iraf.hselect(img, hdr, expr='yes')

    return 0
Example #27
0
def image_shift(targetdir):

    """
    Shift all B-A images by 48 pixels in y-direction
    """

    print 'Target directory is ' + targetdir
    print 'Shifting images...'

    innames, outnames = [], []

    for n in os.listdir(targetdir):
        if (n.endswith('.fit')) & (n.startswith('dfcr')):
            outnames.append(os.path.join(targetdir,'s' + n ) )
            innames.append(os.path.join(targetdir,n) )
            if os.path.exists( os.path.join(targetdir,'s' + n) ):
                os.remove(os.path.join(targetdir,'s' + n))
                print 'Removing file ' + os.path.join(targetdir,'s' + n)

    with open(os.path.join(targetdir,'input.list'), 'w') as f:
        for name in innames:
            print name.replace(targetdir + '/','')
            f.write( name + '\n' )

    with open(os.path.join(targetdir,'output.list'), 'w') as f:
        for name in outnames:
            f.write( name + '\n' )

    names = np.array([name.replace('dfcr','r') + '[1]' for name in innames])

    decoff = np.asarray([iraf.hselect(f , 'DECOFF', 'yes', Stdout=1)[0] for f in names],dtype='float')

    with open(os.path.join(targetdir,'shifts.lst'),'w') as f:
        for d in decoff:
            if d >= -1.0:
                yshift = '0.0'
            else:
                yshift = '-48.0'
            f.write('0.0 ' + yshift + '\n')

    iraf.images(_doprint=0)
    iraf.imgeom(_doprint=0)

    iraf.imshift.setParam('input', '@' + os.path.join(targetdir, 'input.list'))
    iraf.imshift.setParam('output', '@' + os.path.join(targetdir, 'output.list'))
    iraf.imshift.setParam('shifts_file', os.path.join(targetdir,'shifts.lst') )

    iraf.imshift()

    return None
Example #28
0
def showObjProp():
    print "start showObjProp"
    for ft in FILTERS:
        print("start %s" % ft)
        print "object\tzenith dist\tright asc\tdecl\texptime\tairmass\tUT"
        iraf.hselect.setParam(
            "images",
            os.path.join(OUTPUTDIR, "object", ft) + "/*.fits")
        iraf.hselect.setParam("fields",
                              "$I,OBJECT,ZD,RA,DEC,EXPTIME,AIRMASS,UT")
        iraf.hselect.setParam("expr", 'yes')
        for r in iraf.hselect(Stdout=1, mode="h"):
            print r
    print "end showObjProp"
Example #29
0
def correct_wcs(image,clobber=False,minmag=13, radius=20.):
  '''correct wcs of fitsfile to 2MASS coordinates
  
  This method loads the 2MASS catalog in the region covered by the fits file
  and shifts,stretches, and rotates the wcs in the fits file to match the 2MASS coordinates
  The initial wcs needs to be approximately correct (within a few arcsec), otherwise 
  the matching will fail.
  image = filename
  clobber = overwrite existing 2MASS catalog file?
  minmag = mag of faintest object retrieved from 2MASS'''
  object = iraf.hselect(image+'[0]','OBJECT', 'yes', Stdout=1)[0]
  filter = iraf.hselect(image+'[0]','FILTER', 'yes', Stdout=1)[0]
  catalogue=os.path.join(os.path.dirname(image),"2mass_"+str(object)+'_'+str(filter)+'_'+str(minmag)+".cat")
  #Gererate catalogue, if it does not exist in this directory
  if clobber or not os.path.isfile(catalogue):
    get_catalogue(catalogue,iraf.hselect(image+'[0]','RA','yes', Stdout=1)[0],iraf.hselect(image+'[0]','DEC','yes', Stdout=1)[0], radius=radius, minmag=minmag)
  #cfrac: maximum fraction of targets which do not center correctly
  #       a large number is no problem here, since often there are a few 100 sources in the catalog to match.
  iraf.mscred(_doprint=0)
  #matchout=iraf.msccmatch(image,catalogue,interactive=False, Stdout=1,cfrac=0.95,rms=3)
  iraf.cd(os.path.dirname(image))
  matchout=iraf.msccmatch(os.path.basename(image),catalogue,interactive=False, Stdout=1,cfrac=0.9,rms=10,maxshif=100., nsearch=100, nfit=5, update='yes')
  try: print matchout[-5]
  except IndexError: print matchout
Example #30
0
def showTimeConv():
	iraf.hselect.setParam("images", IMGDIR + "*.fits")
	iraf.hselect.setParam("fields", "OBJECT,ST,UT,INSFILTE,DATE-OBS,RA,ZD,DEC")
	iraf.hselect.setParam("expr", 'yes')
	print "start"
	#print "object\tst\tut\tra\t(st-ut-ra)"
	print "object\tst\tut\tut(Deg)\tra\t(st-ut)Hours\t(st-ut)Deg\tzd\tdec\t(zd+dec)"
	longit = 0.27925
	for rstr in iraf.hselect(Stdout=1, mode="h"):
		r = rstr.split("\t")
		#ST - UT - RA
		#print r[0] +"\t"+ r[1] + "\t" + r[2] + "\t" + r[3] + "\t" + r[5] + "\t" +  dif(dif(r[1],r[2]),r[5])
		timeDif = sdif(r[1],r[2])
		print(r[0] +"\t"+ r[1] + "\t" + r[2] + "\t" + hoursToDegreesFormat(r[2],True)+ "\t" + r[5] + "\t" +  hourFormatPositive(timeDif)+ "\t" + hoursToDegreesFormat(timeDif, True) + "\t"+ r[6] + "\t" + r[7]+ "\t"+ angleFormatPositive(ssum(r[6],r[7])))
	print "end"
Example #31
0
def hselToDf(color,frameFlavor='raw'):
	'''
	this function packs the file name, observation time, and YYMMDD date 
	of smarts fits files into a pandas data frame
	color is either 'J' or 'H', the broadbandfilter you're interested in
	if frameFlavor='raw' this function is hard coded to look in either the Hraw/ or Jraw/
	if frameFlavor='flat' this function is hard coded to look in irflats/
	it then pickles the data frame
	'''
	#prepare a file handle. this will be a temporary storing space for the data
	fileHandle=StringIO.StringIO()
	#use iraf hselect to spit out file name and obs time for all files
	#save in the file handle
	if frameFlavor=='raw':
		iraf.hselect(color.upper()+"raw/*fits", fields='$I,JD', expr='yes', Stdout=fileHandle)
	elif frameFlavor=='flat':
		iraf.hselect("irflats/*"+color.lower()+"*fits", fields='$I,JD', expr='yes', Stdout=fileHandle)
	else:
		print "use either 'raw' or 'flat' for frameFlavor variable"
		return

	fileHandle.seek(0)
	#stick the file handle into a pandas parser to read it into a data frame
	fileTable=pd.io.parsers.read_fwf(fileHandle, names=['file','JulianDate'])
	fileHandle.close()
	#grab the YYMMDD dates from the file name and list these in a column

	if frameFlavor=='raw':
		fileTable['Date']=fileTable.file.apply(lambda i: i[-16:-10]).astype(int)
		#pickle the data frame
		fileTable.to_pickle(color.upper()+'rawList.pkl')
	else:
		fileTable['Date']=fileTable.file.apply(lambda i: i[-17:-11]).astype(int)
		#pickle the data frame
		fileTable.to_pickle(color.upper()+'flatList.pkl')	
	return
Example #32
0
def showTimeConv():
    iraf.hselect.setParam("images", IMGDIR + "*.fits")
    iraf.hselect.setParam("fields", "OBJECT,ST,UT,INSFILTE,DATE-OBS,RA,ZD,DEC")
    iraf.hselect.setParam("expr", 'yes')
    print "start"
    #print "object\tst\tut\tra\t(st-ut-ra)"
    print "object\tst\tut\tut(Deg)\tra\t(st-ut)Hours\t(st-ut)Deg\tzd\tdec\t(zd+dec)"
    longit = 0.27925
    for rstr in iraf.hselect(Stdout=1, mode="h"):
        r = rstr.split("\t")
        #ST - UT - RA
        #print r[0] +"\t"+ r[1] + "\t" + r[2] + "\t" + r[3] + "\t" + r[5] + "\t" +  dif(dif(r[1],r[2]),r[5])
        timeDif = sdif(r[1], r[2])
        print(r[0] + "\t" + r[1] + "\t" + r[2] + "\t" +
              hoursToDegreesFormat(r[2], True) + "\t" + r[5] + "\t" +
              hourFormatPositive(timeDif) + "\t" +
              hoursToDegreesFormat(timeDif, True) + "\t" + r[6] + "\t" + r[7] +
              "\t" + angleFormatPositive(ssum(r[6], r[7])))
    print "end"
Example #33
0
def speedup():
	'''
	prepares optical images for reduction
	requires: skyflatB, ccd domes, ccd bias, ccd data, in directory when function is run
	input: none
	output: in.{B,V,R,I}, are txt files which list images observed in b,v,r, and i filters
	'''
	#the observer may have forgotten to delete focus, trim, and junk frames
	if len(glob.glob('*junk*')) > 0:
		os.system('rm *junk*')
	if len(glob.glob('*foco*')) > 0:
		os.system('rm *foco*')
	if len(glob.glob('*trim*')) > 0:
		os.system("rm *trim*")
	
	os.system("mkdir calibs")
	os.system("mv *bias* calibs")
	os.system("mv *ky* calibs")
	os.system("mv *dome* calibs")
	
	rawimages=glob.glob('ccd*.fits')
	#open in files for writting
	B = open("in.B",'w')
	V = open("in.V",'w')
	R = open("in.R",'w')
	I = open("in.I",'w')

	for im in rawimages:
		hdulist=pyfits.open(im)
		filt=hdulist[0].header['CCDFLTID']
		hdulist.close()
		if filt=='B':
			B.write(im+'\n')
		elif filt=='V' or filt=='V+ND4':
			V.write(im+'\n')
		elif filt=='R':
			R.write(im+'\n')
		elif filt=='I' or filt=='I+ND4':
			I.write(im+'\n')
		else:
			print "filter for "+im+" is listed as "+filt+" and is not recognized"
			print im+" will not be reduced"

	#close in files
	B.close()
	V.close()
	R.close()
	I.close()

	os.chdir("calibs")			#os.system("cd wherever") doesnt work o_O
	if len(glob.glob("*dome*.0*")) > 0:
		os.system("rm *dome*.0*")
	if len(glob.glob('*domeB*')) > 0:
		os.system("rm *domeB*")
	if len(glob.glob("*bias.0*")) > 0:
		os.system("rm *bias.0*")
	iraf.hselect(images="*",fields="$I,date-obs,time-obs,ccdfltid,exptime",expr="yes")
	print ("------------------------------")
	print ("hsel *ky* $I,ra,dec,ccdfltid,exptime")
	iraf.hselect(images="*ky*",field="$I,ra,dec,ccdfltid,exptime", expr="yes")
	print ("------------------------------")
	os.system("mv * ../")
	os.chdir("../")		#new to this version, go back one directory to processed/ level
	return
Example #34
0
 def __init__(self,imagen): 
     temp=sys.stdout                                                                            # Store original stdout object for later
     sys.stdout =open("hsel.tmp",'w')                                                           # redirect all output to the file
     self.test=  iraf.hselect(images="obj_comb.fits[0]",fields="$I,OBSID,FILTER,GAIN,RDNOISE,EXPTIME,AIRMASS",expr='yes')
     sys.stdout.close()                          # closing the file
     sys.stdout=temp                             # back to the normal print command
Example #35
0
def mscimage(input=None, output=None, format='image', pixmask=no,verbose=')_.verbose',wcssource='image',reference='',ra=INDEF,dec=INDEF,scale=INDEF,rotation=INDEF,blank=0.0,interpolant='poly5',minterpolant='linear',boundary='reflect',constant=0.0,fluxconserve=no,ntrim=8,nxblock=INDEF,nyblock=INDEF,interactive=no,nx=10,ny=20,fitgeometry='general',xxorder=4,xyorder=4,xxterms='half',yxorder=4,yyorder=4,yxterms='half',fd_in='',fd_ext='',fd_coord='',mode='ql',DOLLARnargs=0,taskObj=None):

	Vars = IrafParList('mscimage')
	Vars.addParam(makeIrafPar(input, datatype='string', name='input', mode='a',prompt='List of input mosaic exposures'))
	Vars.addParam(makeIrafPar(output, datatype='string', name='output',mode='a',prompt='List of output images'))
	Vars.addParam(makeIrafPar(format, datatype='string', name='format',enum=['image', 'mef'],mode='h',prompt='Output format (image|mef)'))
	Vars.addParam(makeIrafPar(pixmask, datatype='bool', name='pixmask',mode='h',prompt='Create pixel mask?'))
	Vars.addParam(makeIrafPar(verbose, datatype='bool', name='verbose',mode='h',prompt='Verbose output?\n\n# Output WCS parameters'))
	Vars.addParam(makeIrafPar(wcssource, datatype='string', name='wcssource',enum=['image', 'parameters', 'match'],mode='h',prompt='Output WCS source (image|parameters|match)'))
	Vars.addParam(makeIrafPar(reference, datatype='file', name='reference',mode='h',prompt='Reference image'))
	Vars.addParam(makeIrafPar(ra, datatype='real', name='ra', max=24.0,min=0.0,mode='h',prompt='RA of tangent point (hours)'))
	Vars.addParam(makeIrafPar(dec, datatype='real', name='dec', max=90.0,min=-90.0,mode='h',prompt='DEC of tangent point (degrees)'))
	Vars.addParam(makeIrafPar(scale, datatype='real', name='scale', mode='h',prompt='Scale (arcsec/pixel)'))
	Vars.addParam(makeIrafPar(rotation, datatype='real', name='rotation',max=360.0,min=-360.0,mode='h',prompt='Rotation of DEC from N to E (degrees)\n\n# Resampling parmeters'))
	Vars.addParam(makeIrafPar(blank, datatype='real', name='blank', mode='h',prompt='Blank value'))
	Vars.addParam(makeIrafPar(interpolant, datatype='string',name='interpolant',mode='h',prompt='Interpolant for data'))
	Vars.addParam(makeIrafPar(minterpolant, datatype='string',name='minterpolant',mode='h',prompt='Interpolant for mask'))
	Vars.addParam(makeIrafPar(boundary, datatype='string', name='boundary',enum=['nearest', 'constant', 'reflect', 'wrap'],mode='h',prompt='Boundary extension'))
	Vars.addParam(makeIrafPar(constant, datatype='real', name='constant',mode='h',prompt='Constant boundary extension value'))
	Vars.addParam(makeIrafPar(fluxconserve, datatype='bool',name='fluxconserve',mode='h',prompt='Preserve flux per unit area?'))
	Vars.addParam(makeIrafPar(ntrim, datatype='int', name='ntrim', min=0,mode='h',prompt='Edge trim in each extension'))
	Vars.addParam(makeIrafPar(nxblock, datatype='int', name='nxblock',mode='h',prompt='X dimension of working block size in pixels'))
	Vars.addParam(makeIrafPar(nyblock, datatype='int', name='nyblock',mode='h',prompt='Y dimension of working block size in pixels\n\n# Geometric mapping parameters'))
	Vars.addParam(makeIrafPar(interactive, datatype='bool', name='interactive',mode='h',prompt='Fit mapping interactively?'))
	Vars.addParam(makeIrafPar(nx, datatype='int', name='nx', mode='h',prompt='Number of x grid points'))
	Vars.addParam(makeIrafPar(ny, datatype='int', name='ny', mode='h',prompt='Number of y grid points'))
	Vars.addParam(makeIrafPar(fitgeometry, datatype='string',name='fitgeometry',enum=['shift', 'xyscale', 'rotate', 'rscale', 'rxyscale', 'general'],mode='h',prompt='Fitting geometry'))
	Vars.addParam(makeIrafPar(xxorder, datatype='int', name='xxorder', min=2,mode='h',prompt='Order of x fit in x'))
	Vars.addParam(makeIrafPar(xyorder, datatype='int', name='xyorder', min=2,mode='h',prompt='Order of x fit in y'))
	Vars.addParam(makeIrafPar(xxterms, datatype='string', name='xxterms',mode='h',prompt='X fit cross terms type'))
	Vars.addParam(makeIrafPar(yxorder, datatype='int', name='yxorder', min=2,mode='h',prompt='Order of y fit in x'))
	Vars.addParam(makeIrafPar(yyorder, datatype='int', name='yyorder', min=2,mode='h',prompt='Order of y fit in y'))
	Vars.addParam(makeIrafPar(yxterms, datatype='string', name='yxterms',mode='h',prompt='Y fit cross terms type\n\n'))
	Vars.addParam(makeIrafPar(fd_in, datatype='struct', name='fd_in',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(fd_ext, datatype='struct', name='fd_ext',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(fd_coord, datatype='struct', name='fd_coord',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(mode, datatype='string', name='mode', mode='h',prompt=''))
	Vars.addParam(makeIrafPar(DOLLARnargs, datatype='int', name='$nargs',mode='h'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='in', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='out', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='ref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pl', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='image', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='trimsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='outsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='plsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='inlists', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='extlist', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pllist', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='coord', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='db', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='wcsref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='outtemp', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pltemp', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nc', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nl', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='ncref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nlref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='cmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='cmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='lmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='lmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nimage', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nimages', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nxblk', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nyblk', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='x', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='y', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='rval', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='xmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='xmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='ymin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='ymax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='crpix1', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='crpix2', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='string', name='extname',mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='string', name='str', mode='u'))

	iraf.cache('mscextensions', 'mscgmask')
	Vars.inlists = iraf.mktemp('tmp$iraf')
	Vars.extlist = iraf.mktemp('tmp$iraf')
	Vars.pllist = iraf.mktemp('tmp$iraf')
	Vars.coord = iraf.mktemp('tmp$iraf')
	Vars.db = iraf.mktemp('tmp$iraf')
	Vars.outtemp = iraf.mktemp('tmp')
	Vars.wcsref = iraf.mktemp('tmp')
	Vars.pltemp = iraf.mktemp('tmp')
	iraf.joinlists(Vars.input, Vars.output, output = Vars.inlists, delim = ' ',short=yes,type = 'image')
	Vars.fd_in = Vars.inlists
	while (iraf.fscan(locals(), 'Vars.fd_in', 'Vars.PYin', 'Vars.out') != EOF):
		if (iraf.imaccess(Vars.out)):
			iraf.printf('Warning: Image already exists (%s)\n', Vars.out)
			continue
		if (Vars.pixmask):
			Vars.pl = Vars.out
			Vars.nc = iraf.strlen(Vars.pl)
			if (Vars.nc > 5 and iraf.substr(Vars.pl, Vars.nc - 4, Vars.nc) == '.fits'):
				Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 5)
			elif (Vars.nc > 4 and iraf.substr(Vars.out, Vars.nc - 3, Vars.nc) == '.imh'):
				Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 4)
			Vars.pl = Vars.pl + '_bpm'
			if (Vars.format == 'image' and iraf.imaccess(Vars.pl)):
				iraf.printf('Warning: Mask already exists (%s)\n', Vars.pl)
				continue
		else:
			Vars.pl = ''
		iraf.mscextensions(Vars.PYin, output = 'file', index = '0-',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
		Vars.nimages = int(iraf.mscextensions.nimages)
		Vars.nimage = 0
		if (Vars.nimages < 1):
			iraf.printf("WARNING: No input image data found in `%s'.\
",Vars.PYin)
			iraf.delete(Vars.extlist, verify = no)
			continue
		if (not iraf.imaccess(Vars.wcsref)):
			Vars.ref = Vars.reference
			if (Vars.wcssource == 'match'):
				Vars.wcsref = Vars.ref
			else:
				iraf.mscwtemplate('@' + Vars.extlist, Vars.wcsref,wcssource = Vars.wcssource,reference = Vars.ref,ra = Vars.ra,dec = Vars.dec,scale = Vars.scale,rotation = Vars.rotation,projection = '',verbose = Vars.verbose)
		Vars.fd_ext = Vars.extlist
		while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
			Vars.nimage = Vars.nimage + 1
			if (Vars.nimages > 1):
				Pipe1 = iraf.hselect(Vars.image, 'extname', yes, Stdout=1)
				iraf.scan(locals(), 'Vars.extname', Stdin=Pipe1)
				del Pipe1
				if (iraf.nscan() == 0):
					Vars.extname = 'im' + str(Vars.nimage)
				Pipe1 = iraf.printf('%s[%s,append]\n', Vars.outtemp,Vars.extname,Stdout=1)
				iraf.scan(locals(), 'Vars.outsec', Stdin=Pipe1)
				del Pipe1
				Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
				iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
				del Pipe1
			else:
				Vars.extname = ''
				Vars.outsec = Vars.outtemp
				Vars.plsec = Vars.pl
			if (Vars.pixmask and iraf.imaccess(Vars.plsec)):
				iraf.delete(Vars.coord, verify = no)
				iraf.delete(Vars.db, verify = no)
				iraf.printf('Warning: Mask already exists (%s)\n', Vars.plsec)
				continue
			if (Vars.verbose):
				iraf.printf('Resampling %s ...\n', Vars.image)
			Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2', yes, Stdout=1)
			iraf.scan(locals(), 'Vars.nc', 'Vars.nl', Stdin=Pipe1)
			del Pipe1
			Vars.cmin = 1 + Vars.ntrim
			Vars.cmax = Vars.nc - Vars.ntrim
			Vars.lmin = 1 + Vars.ntrim
			Vars.lmax = Vars.nl - Vars.ntrim
			Pipe1 = iraf.printf('[%d:%d,%d:%d]\n', Vars.cmin, Vars.cmax,Vars.lmin,Vars.lmax,Stdout=1)
			iraf.scan(locals(), 'Vars.trimsec', Stdin=Pipe1)
			del Pipe1
			if (Vars.wcssource == 'match'):
				Pipe1 = iraf.hselect(Vars.ref, 'naxis1,naxis2', yes, Stdout=1)
				iraf.scan(locals(), 'Vars.ncref', 'Vars.nlref', Stdin=Pipe1)
				del Pipe1
				Vars.xmin = (Vars.ncref - 1.) / (Vars.nx - 1.)
				Vars.ymin = (Vars.nlref - 1.) / (Vars.ny - 1.)
				Vars.ymax = 1
				while (Vars.ymax <= Vars.nlref + 1):
					Vars.xmax = 1
					while (Vars.xmax <= Vars.ncref + 1):
						iraf.clPrint(Vars.xmax, Vars.ymax, Vars.xmax,Vars.ymax,StdoutAppend=Vars.coord)
						Vars.xmax = Vars.xmax + Vars.xmin
					Vars.ymax = Vars.ymax + Vars.ymin
				iraf.mscctran(Vars.coord, Vars.db, Vars.ref, 'logical','world',columns = '3 4',units = '',formats = '%.4H %.3h',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.coord, verify=no)
				iraf.wcsctran(Vars.db, Vars.coord, Vars.image + Vars.trimsec,inwcs = 'world',outwcs = 'logical',columns = '3 4',units = 'hours native',formats = '',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.db, verify=no)
			else:
				Vars.nc = Vars.cmax - Vars.cmin + 1
				Vars.nl = Vars.lmax - Vars.lmin + 1
				Vars.xmin = (Vars.nc - 1.) / (Vars.nx - 1.)
				Vars.ymin = (Vars.nl - 1.) / (Vars.ny - 1.)
				Vars.ymax = 1
				while (Vars.ymax <= Vars.nl + 1):
					Vars.xmax = 1
					while (Vars.xmax <= Vars.nc + 1):
						iraf.clPrint(Vars.xmax, Vars.ymax, Vars.xmax,Vars.ymax,StdoutAppend=Vars.coord)
						Vars.xmax = Vars.xmax + Vars.xmin
					Vars.ymax = Vars.ymax + Vars.ymin
				iraf.mscctran(Vars.coord, Vars.db, Vars.image + Vars.trimsec,'logical','world',columns = '1 2',units = '',formats = '%.4H %.3h',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.coord, verify=no)
				iraf.wcsctran(Vars.db, Vars.coord, Vars.wcsref,inwcs = 'world',outwcs = 'logical',columns = '1 2',units = 'hours native',formats = '',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.db, verify=no)
			Vars.xmax = 0.
			Vars.xmin = 1.
			Vars.ymax = 0.
			Vars.ymin = 1.
			Vars.fd_coord = Vars.coord
			while (iraf.fscan(locals(), 'Vars.fd_coord', 'Vars.x', 'Vars.y') != EOF):
				if (iraf.nscan() < 2):
					continue
				if (Vars.xmax < Vars.xmin):
					Vars.xmin = Vars.x
					Vars.xmax = Vars.x
					Vars.ymin = Vars.y
					Vars.ymax = Vars.y
				else:
					Vars.xmin = float(iraf.minimum(Vars.x, Vars.xmin))
					Vars.xmax = float(iraf.maximum(Vars.x, Vars.xmax))
					Vars.ymin = float(iraf.minimum(Vars.y, Vars.ymin))
					Vars.ymax = float(iraf.maximum(Vars.y, Vars.ymax))
			Vars.fd_coord = ''
			if (Vars.xmax <= Vars.xmin or Vars.ymax <= Vars.ymin):
				iraf.error(1, 'No overlap for matching reference')
			Vars.cmin = int(iraf.nint(Vars.xmin - 1.5))
			Vars.cmax = int(iraf.nint(Vars.xmax + 1.5))
			Vars.lmin = int(iraf.nint(Vars.ymin - 1.5))
			Vars.lmax = int(iraf.nint(Vars.ymax + 1.5))
			iraf.geomap(Vars.coord, Vars.db, Vars.cmin, Vars.cmax, Vars.lmin,Vars.lmax,transforms = '',results = '',fitgeometry = Vars.fitgeometry,function = 'chebyshev',xxorder = Vars.xxorder,xyorder = Vars.xyorder,xxterms = Vars.xxterms,yxorder = Vars.yxorder,yyorder = Vars.yyorder,yxterms = Vars.yxterms,reject = INDEF,calctype = 'double',verbose = no,interactive = Vars.interactive,graphics = 'stdgraph',cursor = '')
			if (Vars.wcssource == 'match'):
				Vars.cmin = 1
				Vars.lmin = 1
				Vars.cmax = Vars.ncref
				Vars.lmax = Vars.nlref
			if (Vars.nxblock == INDEF):
				Vars.nxblk = Vars.cmax - Vars.cmin + 3
			else:
				Vars.nxblk = Vars.nxblock
			if (Vars.nyblock == INDEF):
				Vars.nyblk = Vars.lmax - Vars.lmin + 3
			else:
				Vars.nyblk = Vars.nyblock
			iraf.geotran(Vars.image + Vars.trimsec, Vars.outsec, Vars.db,Vars.coord,geometry = 'geometric',xin = INDEF,yin = INDEF,xshift = INDEF,yshift = INDEF,xout = INDEF,yout = INDEF,xmag = INDEF,ymag = INDEF,xrotation = INDEF,yrotation = INDEF,xmin = Vars.cmin,xmax = Vars.cmax,ymin = Vars.lmin,ymax = Vars.lmax,xsample = 10.,ysample = 10.,xscale = 1.,yscale = 1.,ncols = INDEF,nlines = INDEF,interpolant = Vars.interpolant,boundary = 'constant',constant = Vars.constant,fluxconserve = Vars.fluxconserve,nxblock = Vars.nxblk,nyblock = Vars.nyblk,verbose = no)
			iraf.wcscopy(Vars.outsec, Vars.wcsref, verbose=no)
			Vars.xmin = 0.
			Vars.ymin = 0.
			Pipe1 = iraf.hselect(Vars.outsec, 'crpix1,crpix2', yes, Stdout=1)
			iraf.scan(locals(), 'Vars.xmin', 'Vars.ymin', Stdin=Pipe1)
			del Pipe1
			Vars.xmin = Vars.xmin - Vars.cmin + 1
			Vars.ymin = Vars.ymin - Vars.lmin + 1
			if (Vars.nimage == 1):
				Vars.crpix1 = Vars.xmin
				Vars.crpix2 = Vars.ymin
			else:
				Vars.crpix1 = float(iraf.maximum(Vars.crpix1, Vars.xmin))
				Vars.crpix2 = float(iraf.maximum(Vars.crpix2, Vars.ymin))
			iraf.hedit(Vars.outsec, 'crpix1', Vars.xmin, add=yes, verify=no,show=no,update=yes)
			iraf.hedit(Vars.outsec, 'crpix2', Vars.ymin, add=yes, verify=no,show=no,update=yes)
			if (Vars.pixmask):
				Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
				iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
				del Pipe1
				iraf.mscgmask(Vars.image + Vars.trimsec, Vars.pltemp + '.pl','BPM',mval = 10000)
				iraf.geotran(Vars.pltemp, Vars.plsec + '.fits', Vars.db,Vars.coord,geometry = 'geometric',xin = INDEF,yin = INDEF,xshift = INDEF,yshift = INDEF,xout = INDEF,yout = INDEF,xmag = INDEF,ymag = INDEF,xrotation = INDEF,yrotation = INDEF,xmin = Vars.cmin,xmax = Vars.cmax,ymin = Vars.lmin,ymax = Vars.lmax,xsample = 10.,ysample = 10.,interpolant = Vars.minterpolant,boundary = 'constant',constant = 20000.,fluxconserve = no,nxblock = Vars.nxblk,nyblock = Vars.nyblk,verbose = no)
				iraf.imdelete(Vars.pltemp, verify=no)
				iraf.mscpmask(Vars.plsec + '.fits', Vars.plsec + '.pl')
				iraf.imdelete(Vars.plsec + '.fits', verify=no)
				iraf.hedit(Vars.outsec, 'BPM', Vars.plsec + '.pl', add=yes,show=no,verify=no,update=yes)
				iraf.wcscopy(Vars.plsec, Vars.outsec, verbose=no)
				iraf.clPrint(Vars.plsec, StdoutAppend=Vars.pllist)
			else:
				iraf.hedit(Vars.outsec, 'BPM', PYdel=yes, add=no, addonly=no,show=no,verify=no,update=yes)
			iraf.delete(Vars.coord, verify = no)
			iraf.delete(Vars.db, verify = no)
		Vars.fd_ext = ''
		iraf.delete(Vars.extlist, verify = no)
		if (Vars.nimages > 1 and Vars.format == 'image'):
			if (Vars.verbose):
				iraf.printf('Creating image %s ...\n', Vars.out)
			iraf.mscextensions(Vars.outtemp, output = 'file', index = '',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
			if (Vars.pixmask):
				iraf.combine('@' + Vars.pllist, Vars.pltemp + '.pl',headers = '',bpmasks = Vars.pl,rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'none',maskvalue = '0',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = 0.99,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
				iraf.imdelete(Vars.pltemp, verify=no)
				iraf.combine('@' + Vars.extlist, Vars.out, headers = '',bpmasks = '',rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'badvalue',maskvalue = '2',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = INDEF,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
				iraf.hedit(Vars.out, 'BPM', Vars.pl, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.pl, 'IMCMB???,PROCID??', add=no, addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			else:
				iraf.combine('@' + Vars.extlist, Vars.out, headers = '',bpmasks = '',rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'none',maskvalue = '2',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = INDEF,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
			Pipe2 = iraf.hselect('@' + Vars.extlist, 'gain', yes, Stdout=1)
			Pipe1 = iraf.average(data_value = 0., Stdin=Pipe2, Stdout=1)
			del Pipe2
			iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
			del Pipe1
			iraf.hedit(Vars.out, 'gain', Vars.rval, add=yes, PYdel=no,update=yes,verify=no,show=no)
			Pipe2 = iraf.hselect('@' + Vars.extlist, 'rdnoise', yes, Stdout=1)
			Pipe1 = iraf.average(data_value = 0., Stdin=Pipe2, Stdout=1)
			del Pipe2
			iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
			del Pipe1
			iraf.hedit(Vars.out, 'rdnoise', Vars.rval, add=yes, PYdel=no,update=yes,verify=no,show=no)
			iraf.hedit(Vars.out, 'IMCMB???,PROCID??', add=no, addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			iraf.hedit(Vars.out,'NEXTEND,DETSEC,CCDSEC,AMPSEC,IMAGEID,DATASEC,TRIMSEC,BIASSEC',add=no,addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			iraf.imdelete(Vars.outtemp, verify=no)
			if (iraf.access(Vars.pllist)):
				iraf.imdelete('@' + Vars.pllist, verify=no)
				iraf.delete(Vars.pllist, verify=no)
			iraf.delete(Vars.extlist, verify = no)
		elif (Vars.nimages > 1):
			iraf.imrename(Vars.outtemp, Vars.out, verbose=no)
			iraf.mscextensions(Vars.out, output = 'file', index = '',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
			Vars.fd_ext = Vars.extlist
			while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
				Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2,crpix1,crpix2',yes,Stdout=1)
				iraf.scan(locals(), 'Vars.nc', 'Vars.nl', 'Vars.xmin','Vars.ymin',Stdin=Pipe1)
				del Pipe1
				Vars.cmin = int(iraf.nint(Vars.crpix1 - Vars.xmin + 1))
				Vars.lmin = int(iraf.nint(Vars.crpix2 - Vars.ymin + 1))
				Vars.cmax = Vars.nc + Vars.cmin - 1
				Vars.lmax = Vars.nl + Vars.lmin - 1
				Pipe1 = iraf.printf('[%d:%d,%d:%d]\n', Vars.cmin, Vars.cmax,Vars.lmin,Vars.lmax,Stdout=1)
				iraf.scan(locals(), 'Vars.str', Stdin=Pipe1)
				del Pipe1
				iraf.hedit(Vars.image, 'DETSEC', Vars.str, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTM1_1', 1., add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTM2_2', 1., add=yes, verify=no,show=no,update=yes)
				Vars.cmin = Vars.cmin - 1
				Vars.lmin = Vars.lmin - 1
				iraf.hedit(Vars.image, 'DTV1', Vars.cmin, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTV2', Vars.lmin, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image,'CCDSUM,CCDSEC,AMPSEC,ATM1_1,ATM2_2,ATV1,ATV2',PYdel=yes,add=no,addonly=no,verify=no,show=no,update=yes)
			Vars.fd_ext = ''
			iraf.delete(Vars.extlist, verify=no)
		else:
			iraf.imrename(Vars.outsec, Vars.out, verbose=no)
		if (iraf.access(Vars.pllist)):
			iraf.delete(Vars.pllist, verify=no)
	Vars.fd_in = ''
	iraf.delete(Vars.inlists, verify = no)
	if (Vars.wcssource != 'match' and iraf.imaccess(Vars.wcsref)):
		iraf.imdelete(Vars.wcsref, verify=no)
logger.info("Unlearning..")

iraf.unlearn("gemini")
iraf.unlearn("gmos")
iraf.unlearn("gemtools")
iraf.set(stdimage="imtgmos2", homedir=os.path.expanduser("~/"))

logger.info("Cleaning up from any previous reductions..")
os.system("rm -Rf master_flat.fits mosaic_master_flat.fits stgscg*.fits "
    "cg*.fits tgs*.fits g*.fits estgsc*.fits database J??????.?????????.?.fits")
                                                      
files = np.array(glob("[N|S]20*.fits"))

logger.info("Getting object types..")
obstypes = np.array([iraf.hselect(filename + "[0]", fields="OBSTYPE",
    expr="1=1", Stdout=1)[0] for filename in files])

obsclass = np.array([iraf.hselect(filename + "[0]", fields="OBSCLASS",
    expr="1=1", Stdout=1)[0] for filename in files])

folder = os.path.dirname(files[0])

# Check that we have one of each
if "OBJECT" not in obstypes:
    raise IOError("no OBJECT image found in {0}".format(folder))

if "ARC" not in obstypes:
    raise IOError("no ARC image found in {0}".format(folder))

if "FLAT" not in obstypes:
    raise IOError("no FLAT image found in {0}".format(folder))
Example #37
0
def makesky_lp2(files, nite, wave):
    """Make L' skies by carefully treating the ROTPPOSN angle
    of the K-mirror. Uses only 2 skies combined."""

    # Start out in something like '06maylgs1/reduce/kp/'
    waveDir = os.getcwd() + '/'
    redDir = util.trimdir(os.path.abspath(waveDir + '../') + '/')
    rootDir = util.trimdir(os.path.abspath(redDir + '../') + '/')
    skyDir = waveDir + 'sky_' + nite + '/'
    rawDir = rootDir + 'raw/'

    util.mkdir(skyDir)

    raw = [rawDir + 'n' + str(i).zfill(4) for i in files]
    skies = [skyDir + 'n' + str(i).zfill(4) for i in files]
    
    _rawlis = skyDir + 'raw.lis'
    _nlis = skyDir + 'n.lis'
    _skyRot = skyDir + 'skyRot.txt'
    _txt = skyDir + 'rotpposn.txt'
    _out = skyDir + 'sky'
    _log = _out + '.log'
    util.rmall([_rawlis, _nlis, _skyRot, _txt, _out, _log])
    util.rmall([sky + '.fits' for sky in skies])

    open(_rawlis, 'w').write('\n'.join(raw)+'\n')
    open(_nlis, 'w').write('\n'.join(skies)+'\n')

    print 'makesky_lp: Getting raw files'
    ir.imcopy('@' + _rawlis, '@' + _nlis, verbose='no')
    ir.hselect('@' + _nlis, "$I,ROTPPOSN", 'yes', Stdout=_skyRot) 

    # Read in the list of files and rotation angles
    rotTab = asciidata.open(_skyRot)
    files = rotTab[0].tonumpy()
    angles = rotTab[1].tonumpy()

    # Fix angles to be between -180 and 180
    angles[angles > 180] -= 360.0
    angles[angles < -180] += 360.0
    
    sidx = np.argsort(angles)
    
    # Make sorted numarrays
    angles = angles[sidx]
    files = files[sidx]

    f_log = open(_log, 'w')
    f_txt = open(_txt, 'w')

    # Skip the first and last since we are going to 
    # average every 3 files.
    print 'makesky_lp: Combining to make skies.'
    for i in range(1, len(sidx)):
        angav = (angles[i] + angles[i-1])/2.
	sky = 'sky%.1f' % (angav)
	skyFits = skyDir + sky + '.fits'
	util.rmall([skyFits])

	# Average 2 images
	list = [file for file in files[i-1:i+1]]
	short = [file for file in files[i-1:i+1]]

	# Make short names
	for j in range(len(list)):
	    tmp = (short[j]).rsplit('/', 1)
	    short[j] = tmp[len(tmp)-1]
	    
	print '%s: %s %s' % (sky, short[0], short[1])
	f_log.write('%s: %s %s  %6.1f %6.1f\n' %
		    (sky, short[0], short[1], 
		     angles[i-1], angles[i]))

	ir.unlearn('imcombine')
	ir.imcombine.combine = 'average'
	ir.imcombine.reject = 'none'
	ir.imcombine.nlow = 1
	ir.imcombine.nhigh = 1
	ir.imcombine.logfile = ''
	ir.imcombine(list[1]+','+list[0], skyFits)
	
	ir.hedit(skyFits, 'SKYCOMB', 
		 '%s: %s %s' % (sky, short[0], short[1]), 
		 add='yes', show='no', verify='no')
	
	f_txt.write('%13s %8.3f\n' % (sky, angav))
	
    f_txt.close()
    f_log.close()
Example #38
0
# signal = ( flux - bias - dark * t ) / norm-flat

iraf.imstat.unlearn()
iraf.imarith.unlearn()

for filter in [ 'B', 'V', 'R' ]:

    print; print "Reducing science in filter " + filter + "..."

    for line in open( config.root + 'list/science-' + filter, 'r' ).readlines():

        fn = line.rstrip ()

        # Get exposures and calculate the scaled exposure for the dark

        tSci   = iraf.hselect( 'RAW$' + fn, "EXPTIME", 'yes', Stdout=1 )[0]
        tDark  = iraf.hselect( 'master/dark', "EXPTIME", 'yes', Stdout=1 )[0]
        tRatio = float(tSci) / float(tDark)

        # Reduce the science image

        sciMinusBias = 'T$s1-' + fn
        darkNormalized = 'T$s2-' + fn
        sciMinusBiasMinusDark = 'T$s3-' + fn

        iraf.imdelete( 'OUT$' + fn )
        iraf.imdelete( sciMinusBias )
        iraf.imdelete( darkNormalized )
        iraf.imdelete( sciMinusBiasMinusDark )

        if config.noReduce :  # Do not use bias & darks (use flats only)
Example #39
0
print 'Changing to reduced data directory'
os.chdir('reduced/' + date)

# SET DATA TYPE
# Set the type of data being extracted to 'Echelle'. Forced review of the
# parameters being set is suppressed.
iraf.setinstrument('echelle', review=no)

# MAKE IMAGE LISTS
# note there are blue flats and 'no filter' (red) flats for ARCES.
# this is because the instrument is undersensitive in the blue.
# lists include: allflats, flat_blue, flat_red, biases, arcs (Th-Ar), targets
# also makes the list objflat (contains both targets and flats)
# (darks are also an option, but are omitted by default)
print 'Generating file lists...'
iraf.hselect('*.fits', '$I', 'IMAGETYP == "flat"', Stdout='allflats')
#iraf.hselect('*.fits','$I','IMAGETYP == "Flat"',Stdout='allflats')
iraf.hselect('@allflats', '$I', 'FILTER == "Blue"', Stdout='flat_blue')
iraf.hselect('@allflats', '$I', 'FILTER == "Open"', Stdout='flat_red')
iraf.hselect('*.fits', '$I', 'IMAGETYP == "zero"', Stdout='biases')
#iraf.hselect('*.fits','$I','IMAGETYP == "bias"',Stdout='biases')
#iraf.hselect('*.fits','$I','IMAGETYP == "Bias"',Stdout='biases')
#iraf.hselect('*.fits','$I','IMAGETYP == "dark"',Stdout='darks')
iraf.hselect('*.fits', '$I', 'IMAGETYP == "comp"', Stdout='arcs')
#iraf.hselect('*.fits','$I','IMAGETYP == "Comp"',Stdout='arcs')
iraf.hselect('*.fits', '$I', 'IMAGETYP == "object"', Stdout='targets')
#iraf.hselect('*.fits','$I','IMAGETYP == "Object"',Stdout='targets')
outfile = open('objflat', 'w')
iraf.hselect('*.fits', '$I', 'IMAGETYP == "flat"', Stdout=outfile)
#iraf.hselect('*.fits','$I','IMAGETYP == "Flat"',Stdout=outfile)
iraf.hselect('*.fits', '$I', 'IMAGETYP == "object"', Stdout=outfile)
Example #40
0
def makesky_lp(files, nite, wave, number=3, rejectHsigma=None):
    """Make L' skies by carefully treating the ROTPPOSN angle
    of the K-mirror. Uses 3 skies combined (set by number keyword)."""

    # Start out in something like '06maylgs1/reduce/kp/'
    waveDir = os.getcwd() + '/'
    redDir = util.trimdir(os.path.abspath(waveDir + '../') + '/')
    rootDir = util.trimdir(os.path.abspath(redDir + '../') + '/')
    skyDir = waveDir + 'sky_' + nite + '/'
    rawDir = rootDir + 'raw/'

    util.mkdir(skyDir)

    raw = [rawDir + 'n' + str(i).zfill(4) for i in files]
    skies = [skyDir + 'n' + str(i).zfill(4) for i in files]
    
    _rawlis = skyDir + 'raw.lis'
    _nlis = skyDir + 'n.lis'
    _skyRot = skyDir + 'skyRot.txt'
    _txt = skyDir + 'rotpposn.txt'
    _out = skyDir + 'sky'
    _log = _out + '.log'
    util.rmall([_rawlis, _nlis, _skyRot, _txt, _out, _log])
    util.rmall([sky + '.fits' for sky in skies])

    open(_rawlis, 'w').write('\n'.join(raw)+'\n')
    open(_nlis, 'w').write('\n'.join(skies)+'\n')

    print 'makesky_lp: Getting raw files'
    ir.imcopy('@' + _rawlis, '@' + _nlis, verbose='no')
    ir.hselect('@' + _nlis, "$I,ROTPPOSN", 'yes', Stdout=_skyRot) 

    # Read in the list of files and rotation angles
    rotTab = asciidata.open(_skyRot)
    files = rotTab[0].tonumpy()
    angles = rotTab[1].tonumpy()

    # Fix angles to be between -180 and 180
    angles[angles > 180] -= 360.0
    angles[angles < -180] += 360.0
    
    sidx = np.argsort(angles)
    
    # Make sorted numarrays
    angles = angles[sidx]
    files = files[sidx]

    f_log = open(_log, 'w')
    f_txt = open(_txt, 'w')

    # Skip the first and last since we are going to 
    # average every NN files.
    print 'makesky_lp: Combining to make skies.'
    startIdx = number / 2
    stopIdx = len(sidx) - (number / 2)
    for i in range(startIdx, stopIdx):
	sky = 'sky%.1f' % (angles[i])
	skyFits = skyDir + sky + '.fits'
	util.rmall([skyFits])

	# Take NN images
        start = i - (number/2)
        stop = start + number
	list = [file for file in files[start:stop]]
	short = [file for file in files[start:stop]]
        angleTmp = angles[start:stop]

	# Make short names
	for j in range(len(list)):
	    tmp = (short[j]).rsplit('/', 1)
	    short[j] = tmp[len(tmp)-1]

	print '%s: %s' % (sky, " ".join(short))
        f_log.write('%s:' % sky)
        for j in range(len(short)):
            f_log.write(' %s' % short[j])
	for j in range(len(angleTmp)):
            f_log.write(' %6.1f' % angleTmp[j])
        f_log.write('\n')

	ir.unlearn('imcombine')
	ir.imcombine.combine = 'median'

        if (rejectHsigma == None):
            ir.imcombine.reject = 'none'
            ir.imcombine.nlow = 1
            ir.imcombine.nhigh = 1
        else:
            ir.imcombine.reject = 'sigclip'
            ir.imcombine.lsigma = 100
            ir.imcombine.hsigma = rejectHsigma
            ir.imcombine.zero = 'median'

	ir.imcombine.logfile = ''
	ir.imcombine(','.join(list), skyFits)
	
	ir.hedit(skyFits, 'SKYCOMB', 
		 '%s: %s' % (sky, ' '.join(short)), 
		 add='yes', show='no', verify='no')
	
	f_txt.write('%13s %8.3f\n' % (sky, angles[i]))
	
    f_txt.close()
    f_log.close()
Example #41
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False,_catalog='',_datamax=''):
    try:
        import agnkey
        hdr = agnkey.util.readhdr(img + '.fits')
        instrument = agnkey.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

        if 'PIXSCALE' in hdr:
            pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        elif 'CCDSCALE' in hdr:
            if 'CCDXBIN' in hdr:
                pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * agnkey.util.readkey3(hdr, 'CCDXBIN')
            elif 'CCDSUM' in hdr:
                pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * int(
                    string.split(agnkey.util.readkey3(hdr, 'CCDSUM'))[0])

        if 'kb' in instrument:  
            scale = pixelscale
            if not _datamax:
                _datamax = 45000
        elif 'fl' in instrument:
            scale = pixelscale
            if not _datamax:
                _datamax = 60000
        elif 'fs' in instrument:
            scale = pixelscale
            if not _datamax:
                _datamax = 65000
        try:
        #if 1==1:
            if 'WCSERR' in hdr:
                _wcserr = hdr['WCSERR']
            elif 'WCS_ERR' in hdr:
                _wcserr = hdr['WCS_ERR']
            print _wcserr
            if float(_wcserr) == 0:
                if 'kb' in instrument:  
                    seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                elif 'fl' in instrument:     
                    seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                elif 'fs' in instrument: 
                    if 'L1FWHM' in hdr:
                        seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                    elif 'L1SEEING' in hdr:
                        seeing = float(agnkey.util.readkey3(hdr, 'L1SEEING')) * scale
                    else:
                        seeing = 3
                else:
                    seeing = 3
            else:
                seeing = float(agnkey.util.readkey3(hdr, 'PSF_FWHM'))
                sys.exit('astrometry not good')
        except ValueError:
#        except:
            sys.exit('astrometry not good')

        fwhm = seeing / scale
        print 'FWHM[header]  ', fwhm, '   in pixel'
        if ofwhm: fwhm = float(ofwhm)
        print '    FWHM[input]  ', fwhm, ' in pixel'

        xdim, ydim = iraf.hselect(img+'.fits[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split()
        print img, fwhm, threshold, scale

        #################################################################################
        ###################        write file to compute psf     _psf.coo    ############
        #################################################################################
        if interactive:
            iraf.set(stdimage='imt1024')
            iraf.display(img+'.fits[0]', 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img+'[0]', 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []

            #############      write    file for PSF                           #########################
            ff = open('_psf.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()
            fwhm = np.median(_fws)
        else:
            ############              run  sextractor                #####################################
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            
            _ra1,_dec1,xx11,yy11,_mag,_dist = agnkey.agnastrodef.starsfields(img+'.fits',20,19)
            if len(_ra1):
                dist,pos0,pos1 = agnkey.agnastrodef.crossmatchxy(xs,ys,xx11,yy11,10)
                if len(pos0):
                    xs = xs[pos0]
                    ys = ys[pos0]
                    ran = ran[pos0]
                    decn = decn[pos0]
                    magbest = magbest[pos0]
                    classstar = classstar[pos0]
                    fluxrad = fluxrad[pos0]
                    bkg = bkg[pos0]


            ff = open('tmp.cursor', 'w')
            for i in range(len(xs)):
                x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3)
                y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3)
                sect = '[' + str(x1) + ':' + str(x2) + ',' + str(y1) + ':' + str(y2) + ']'
                try:
                    fmax = iraf.imstat(img+'.fits[0]' + sect, fields='max', Stdout=1)[1]
                    ##########       cut saturated object               ########################
                    if float(fmax) < _datamax:  # not saturated
                        ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i]))
                except:
                    print sect
                #    print 'problem here'
                #    pass
            ff.close()

            iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False)
            iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3,
                            display=False, StdoutG='tmp.gki')

            ff = open('tmp.log')
            righe = ff.readlines()
            xn = [float(righe[3].split()[1])]
            yn = [float(righe[3].split()[2])]
            _fw = [float(righe[3].split()[4])]
            for r in righe[4:-2]:
                if len(r) > 0:
                    xn.append(float(r.split()[0]))
                    yn.append(float(r.split()[1]))
                    _fw.append(float(r.split()[3]))
            print 'FWHM: ', righe[-1].split()[-1]
            print 80 * "#"
            ######
            ##############            eliminate double object identification         ###########################
            xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]]
            for i in range(1, len(xn)):
                if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2:
                    xns.append(xn[i])
                    yns.append(yn[i])
                    _fws.append(_fw[i])
            #########      write clean   file for PSF                           #########################
            fw = []
            ff = open('_psf.coo', 'w')
            for i in range(len(xns)):
                    ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
                    fw.append(_fws[i])
            ff.close()  ## End automatic selection
        ######################################################################################
        ###################        write file of object to store in  fits table  #############
        ######################################################################################
        if interactive:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_psf2.coo', 'w')
            for i in range(len(xs)):
                ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i]))
            ff.close()
        elif _catalog:
            print '\n#### use catalog '
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img,inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_psf2.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
            print 'use catalog'
        else:
            os.system('cp _psf.coo _psf2.coo')
#                print '\n###   use sextractor'
#                xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
#                ff = open('_psf2.coo', 'w')
#                for i in range(len(xs)):
#                    ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i]))
#                ff.close()
        ###################################################################################

        print 80 * "#"
        photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture)

        photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture)

        radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag, \
                              Stdout=1, image=img+'.fits[0]', inwcs='logical', outwcs='world', columns="1 2", \
                              format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2, \
                               Stdout=1, image=img+'.fits[0]', inwcs='logical', outwcs='world', columns="1 2", \
                               format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        if ds9 == 0 and interactive:
            iraf.set(stdimage='imt1024')
            iraf.display(img, 1, fill=True, Stdout=1)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=False, Stdin=photmag)
            iraf.tvmark(1, coords='STDIN', mark='rectangle', length=35, label=False, Stdin=pst)
            iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204)

        idpsf = []
        for i in range(len(pst)):
            idpsf.append(pst[i].split()[2])
        dmag = []
        for i in range(len(radec)):
            ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split()
            dmag.append(9.99)
            for j in range(len(fitmag)):
                raf, decf, idf, magf, magerrf = fitmag[j].split()
                if idph == idf and idph in idpsf and \
                                magp3 != 'INDEF' and magf != 'INDEF':
                    dmag[i] = float(magp3) - float(magf)
                    break

        _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag))

        print '>>> Aperture correction (phot)   %6.3f +/- %5.3f %3d ' % \
              (np.mean(_dmag), np.std(_dmag), len(_dmag))
        if len(_dmag) > 3:
            _dmag = np.compress(abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag)
            print '>>>         2 sigma rejection)   %6.3f +/- %5.3f %3d  [default]' \
                  % (np.mean(_dmag), np.std(_dmag), len(_dmag))
            print '>>>     fwhm   %s  ' % (str(fwhm))
        for i in range(len(dmag)):
            if dmag[i] == 9.99:
                dmag[i] = ''
            else:
                dmag[i] = '%6.3f' % (dmag[i])

        exptime = agnkey.util.readkey3(hdr, 'exptime')
        object = agnkey.util.readkey3(hdr, 'object').replace(' ', '')
        filtro = agnkey.util.readkey3(hdr, 'filter')

        #######################################
        rap, decp, magp2, magp3, magp4, smagf = [], [], [], [], [], []
        merrp2, merrp3, merrp4, smagerrf = [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec2)):
            aa = radec2[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(agnkey.agnabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(agnkey.agnabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp2.append(aa[6])
            merrp3.append(aa[7])
            merrp4.append(aa[8])
            _smagf, _smagerrf = 9999, 9999
            for j in range(len(fitmag2)):
                raf, decf, idf, magf, magerrf = fitmag2[j].split()
                if idf == idp:
                    _smagf = magf
                    _smagerrf = magerrf
                    break
            smagf.append(_smagf)
            smagerrf.append(_smagerrf)

        new_cols = pyfits.ColDefs([
            pyfits.Column(name='ra', format='20A', array=np.array(rap)),
            pyfits.Column(name='dec', format='20A', array=np.array(decp)),
            pyfits.Column(name='ra0', format='E', array=np.array(rap0)),
            pyfits.Column(name='dec0', format='E', array=np.array(decp0)),
            pyfits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                            np.array(magp2), 9999), float)),
            pyfits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                            np.array(magp3), 9999), float)),
            pyfits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                            np.array(magp4), 9999), float)),
            pyfits.Column(name='merrp2', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'),
                                                                             np.array(merrp2), 9999), float)),
            pyfits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                             np.array(merrp3), 9999), float)),
            pyfits.Column(name='merrp4', format='E', array=np.array(np.where((np.array(merrp4) != 'INDEF'),
                                                                             np.array(merrp4), 9999), float)),
            pyfits.Column(name='smagf', format='E', array=np.array(np.where((np.array(smagf) != 'INDEF'),
                                                                            np.array(smagf), 9999), float)),
            pyfits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(smagerrf) != 'INDEF'),
                                                                               np.array(smagerrf), 9999), float)),
        ])
        
        tbhdu = pyfits.BinTableHDU.from_columns(new_cols)
        
        hdu = pyfits.PrimaryHDU(header=hdr)
        thdulist = pyfits.HDUList([hdu, tbhdu])
        agnkey.util.delete(img + '.sn2.fits')
        thdulist.writeto(img + '.sn2.fits')
        agnkey.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']})
        agnkey.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']})
        agnkey.util.updateheader(img + '.sn2.fits', 0,
                                 {'XDIM': [agnkey.util.readkey3(hdr, 'naxis1'), 'x number of pixels']})
        agnkey.util.updateheader(img + '.sn2.fits', 0,
                                 {'YDIM': [agnkey.util.readkey3(hdr, 'naxis2'), 'y number of pixels']})
        agnkey.util.updateheader(img + '.sn2.fits', 0,
                                 {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']})
        os.chmod(img + '.sn2.fits', 0664)
        os.chmod(img + '.psf.fits', 0664)
        result = 1
    except IOError as e:
        print e
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Example #42
0
print
print "Creating master DARK..."

iraf.ccdred.darkcombine.unlearn()
iraf.ccdred.darkcombine.setParam('output', '')
iraf.ccdred.darkcombine.setParam('ccdtype', '')
iraf.ccdred.darkcombine.setParam('process', 'no')
iraf.ccdred.darkcombine.setParam('combine', config.dark_comb)
iraf.ccdred.darkcombine.setParam('reject', config.dark_reject)
iraf.ccdred.darkcombine.setParam('scale', config.dark_scale)

iraf.imdelete('master$dark-uns')
iraf.ccdred.darkcombine('T$@list$dark', output='master$dark-uns')

iraf.imdelete('master$dark')
tExp = float(iraf.hselect('master$dark-uns', "EXPTIME", 'yes', Stdout=1)[0])
iraf.imarith.unlearn()
iraf.imarith(operand1='master$dark-uns',
             op='/',
             operand2=str(tExp),
             result='master$dark')

iraf.hedit('master$dark', 'EXPTIME,EXPOSURE', 1, update='yes', ver='no')

###############################################################################
# Make comment

if not noReduce:
    iraf.hedit('master$dark',
               'OBS_I',
               'Combined darks, bias removed',
Example #43
0
def ReadMJD(infile):
    filel=infile.replace(".txt","masked.fits").replace("/"+photdir,"")
    #print filel
    t=iraf.hselect(filel,fields="MJD",expr="yes",Stdout=1) 
    #print t
    return float(t[0])
Example #44
0
def hselToDf(color, frameFlavor='raw'):
    '''
	this function packs the file name, observation time, and YYMMDD date 
	of smarts fits files into a pandas data frame
	color is either 'J' or 'H', the broadbandfilter you're interested in
	if frameFlavor='raw' this function is hard coded to look in either the Hraw/ or Jraw/
	if frameFlavor='flat' this function is hard coded to look in irflats/
	it then pickles the data frame
	'''
    #prepare a file handle. this will be a temporary storing space for the data
    fileHandle = StringIO.StringIO()
    #use iraf hselect to spit out file name and obs time for all files
    #save in the file handle
    if frameFlavor == 'raw':
        iraf.hselect(color.upper() + "/*fits",
                     fields='$I,JD',
                     expr='yes',
                     Stdout=fileHandle)
    elif frameFlavor == 'flat':
        iraf.hselect("irflats/*" + color.lower() + "*fits",
                     fields='$I,JD',
                     expr='yes',
                     Stdout=fileHandle)
    else:
        print "use either 'raw' or 'flat' for frameFlavor variable"
        return

    fileHandle.seek(0)
    #stick the file handle into a pandas parser to read it into a data frame
    fileTable = pd.io.parsers.read_fwf(fileHandle,
                                       names=['file', 'JulianDate'])
    fileHandle.close()

    #Try to force the JulianDate data to floats. This wont work if its corrupted somehow,
    #or has a bad value printed, like a string.
    try:
        fileTable['JulianDate'] = fileTable['JulianDate'].astype(float)
        #grab the YYMMDD dates from the file name and list these in a column
        if frameFlavor == 'raw':
            fileTable['Date'] = fileTable.file.apply(
                lambda i: i[-16:-10]).astype(int)
            #check for nulls in the dataframe
            if checkNullColumn(fileTable):
                #pickle the data frame
                fileTable.to_csv(color.upper() + 'rawList.csv')
                return
            else:
                return
        else:
            if checkNullColumn(fileTable):
                fileTable['Date'] = fileTable.file.apply(
                    lambda i: i[-17:-11]).astype(int)
                #pickle the data frame
                fileTable.to_csv(color.upper() + 'flatList.csv')
                return
            else:
                return
    except ValueError:
        print "encountered a value error. There is a bad value in either the JD field of a header, or a nonstandard filename"
        print "fix these and try again"
        return
Example #45
0
def speedup():
    '''
	prepares optical images for reduction
	requires: skyflatB, ccd domes, ccd bias, ccd data, in directory when function is run
	input: none
	output: in.{B,V,R,I}, are txt files which list images observed in b,v,r, and i filters
	'''
    #the observer may have forgotten to delete focus, trim, and junk frames
    if len(glob.glob('*junk*')) > 0:
        os.system('rm *junk*')
    if len(glob.glob('*foco*')) > 0:
        os.system('rm *foco*')
    if len(glob.glob('*trim*')) > 0:
        os.system("rm *trim*")

    os.system("mkdir calibs")
    os.system("mv *bias* calibs")
    os.system("mv *ky* calibs")
    os.system("mv *dome* calibs")

    rawimages = glob.glob('ccd*.fits')
    #open in files for writting
    B = open("in.B", 'w')
    V = open("in.V", 'w')
    R = open("in.R", 'w')
    I = open("in.I", 'w')

    for im in rawimages:
        hdulist = pyfits.open(im)
        filt = hdulist[0].header['CCDFLTID']
        hdulist.close()
        if filt == 'B':
            B.write(im + '\n')
        elif filt == 'V' or filt == 'V+ND4':
            V.write(im + '\n')
        elif filt == 'R':
            R.write(im + '\n')
        elif filt == 'I' or filt == 'I+ND4':
            I.write(im + '\n')
        else:
            print "filter for " + im + " is listed as " + filt + " and is not recognized"
            print im + " will not be reduced"

    #close in files
    B.close()
    V.close()
    R.close()
    I.close()

    os.chdir("calibs")  #os.system("cd wherever") doesnt work o_O
    if len(glob.glob("*dome*.0*")) > 0:
        os.system("rm *dome*.0*")
    if len(glob.glob('*domeB*')) > 0:
        os.system("rm *domeB*")
    if len(glob.glob("*bias.0*")) > 0:
        os.system("rm *bias.0*")
    iraf.hselect(images="*",
                 fields="$I,date-obs,time-obs,ccdfltid,exptime",
                 expr="yes")
    print("------------------------------")
    print("hsel *ky* $I,ra,dec,ccdfltid,exptime")
    iraf.hselect(images="*ky*", field="$I,ra,dec,ccdfltid,exptime", expr="yes")
    print("------------------------------")
    os.system("mv * ../")
    os.chdir(
        "../")  #new to this version, go back one directory to processed/ level
    return
Example #46
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False):
    try:
        import lsc, string

        hdr = lsc.util.readhdr(img + '.fits')
        instrument = lsc.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

        if 'PIXSCALE' in hdr:
            scale = lsc.util.readkey3(hdr, 'PIXSCALE')
        elif 'CCDSCALE' in hdr:
            if 'CCDXBIN' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * lsc.util.readkey3(hdr, 'CCDXBIN')
            elif 'CCDSUM' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * int(string.split(lsc.util.readkey3(hdr, 'CCDSUM'))[0])

        if _datamax is None and 'kb' in instrument:
            _datamax = 45000
        elif _datamax is None:
            _datamax = 65000
        _wcserr = lsc.util.readkey3(hdr, 'wcserr')
        print _wcserr
        if float(_wcserr) == 0:
            if 'L1FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1FWHM'))
            elif 'L1SEEING' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1SEEING')) * scale
            else:
                seeing = 3
        else:
            seeing = 3
            if 'PSF_FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'PSF_FWHM'))
            else:
                sys.exit('astrometry not good')
        #except:  sys.exit('astrometry not good')

        fwhm = seeing / scale
        print 'FWHM[header]  ', fwhm, '   in pixel'
        if ofwhm: fwhm = float(ofwhm)
        print '    FWHM[input]  ', fwhm, ' in pixel'

        xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split()
        print img, fwhm, threshold, scale,xdim

        #################################################################################
        ###################        write file to compute psf     _psf.coo    ############
        #################################################################################
        if interactive:
            iraf.display(img, 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []
            #############      write    file for PSF                           #########################
            ff = open('_psf.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()
            fwhm = np.median(_fws)
        elif _catalog:
            print '\n#### use catalog to measure the psf'
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            line=''
            for i in ddd:
                a,b,c = string.split(i)
                if float(a) < float(xdim) and  float(b) < float(ydim) and float(b) > 0:
                    line = line + '%10s %10s %10s \n' % (a, b, c)
            if line:
                ff = open('_psf.coo', 'w')
                ff.write(line)
                ff.close()
            else:
                sys.exit('error: no catalog objects are in the field')
        else:
            ############              run  sextractor                #####################################
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            tot = np.compress(abs(np.array(fluxrad) * 1.6 - fwhm) / fwhm < .5, fluxrad)
            if len(tot) < 5:
                print 'warning: fwhm from sexractor different from fwhm computed during pre-reduction'
                print 'try using option --fwhm xxx'

            ff = open('tmp.cursor', 'w')
            image_hdu = fits.open(img + '.fits')
            for i in range(len(xs)):
                _xs = np.delete(xs, i)
                _ys = np.delete(ys, i)
                dist2 = np.sqrt((_xs - xs[i]) ** 2 + (_ys - ys[i]) ** 2)
                ###########           cut  star, not near other object    ##########################
                if abs(fluxrad[i] * 1.6 - fwhm) / fwhm < .5 and min(dist2) > distance * fwhm:
                    x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3)
                    y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3)
                    if x1 < 1: x1 = 1
                    if y1 < 1: y1 = 1
                    if x2 > int(xdim):
                        x2 = int(xdim)
                    if y2 > int(ydim):
                        y2 = int(ydim)
                    fmax = np.max(image_hdu[0].data[y1-1:y2, x1-1:x2])
                ##########       cut saturated object               ########################
                    if float(fmax) < _datamax:  # not saturated
                        ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i]))
            ff.close()
            image_hdu.close()

            iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False)
            iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3,
                            display=False, StdoutG='tmp.gki')

            ff = open('tmp.log')
            righe = ff.readlines()
            xn = [float(righe[3].split()[1])]
            yn = [float(righe[3].split()[2])]
            _fw = [float(righe[3].split()[4])]
            for r in righe[4:-2]:
                if len(r) > 0:
                    xn.append(float(r.split()[0]))
                    yn.append(float(r.split()[1]))
                    _fw.append(float(r.split()[3]))
            print 'FWHM: ', righe[-1].split()[-1]
            print 80 * "#"
            ######
            ##############            eliminate double object identification         ###########################
            xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]]
            for i in range(1, len(xn)):
                if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2:
                    xns.append(xn[i])
                    yns.append(yn[i])
                    _fws.append(_fw[i])
            #########      write clean   file for PSF                           #########################
            fw = []
            ff = open('_psf.coo', 'w')
            for i in range(len(xns)):
                if abs(_fws[i] - fwhm) / fwhm < .3:
                    ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
                    fw.append(_fws[i])
            ff.close()  ## End automatic selection
        ######################################################################################
        ###################        write file of object to store in  fits table  #############
        ######################################################################################
        if interactive:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_psf2.coo', 'w')
            for i in range(len(xs)):
                ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i]))
            ff.close()
        elif _catalog:
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_psf2.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
        else:
            os.system('cp _psf.coo _psf2.coo')
#            dflux = fluxrad - np.median(fluxrad)
#            fstar = np.compress(dflux < np.std(fluxrad), fluxrad)
#################################################################################################################

        print 80 * "#"
        photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture)

        photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture)

        radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag,
                              Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                              format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2,
                               Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                               format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        if ds9 == 0 and (interactive or show):
            iraf.set(stdimage='imt1024')
            iraf.display(img, 1, fill=True, Stdout=1)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=True, Stdin=photmag, nxoffset=5, nyoffset=5, txsize=2)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=35, label=False, Stdin=pst, color=208)
#            iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204)

        idpsf = []
        for i in range(len(pst)):
            idpsf.append(pst[i].split()[2])
        dmag = []
        for i in range(len(radec)):
            ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split()
            dmag.append(9.99)
            for j in range(len(fitmag)):
                raf, decf, idf, magf, magerrf = fitmag[j].split()
                if idph == idf and idph in idpsf and \
                                magp3 != 'INDEF' and magf != 'INDEF':
                    dmag[i] = float(magp3) - float(magf)
                    break

        _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag))

        print '>>> Aperture correction (phot)   %6.3f +/- %5.3f %3d ' % \
              (np.mean(_dmag), np.std(_dmag), len(_dmag))
        if len(_dmag) > 3:
            _dmag = np.compress(np.abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag)
            print '>>>         2 sigma rejection)   %6.3f +/- %5.3f %3d  [default]' \
                  % (np.mean(_dmag), np.std(_dmag), len(_dmag))
            print '>>>     fwhm   %s  ' % (str(fwhm))
        for i in range(len(dmag)):
            if dmag[i] == 9.99:
                dmag[i] = ''
            else:
                dmag[i] = '%6.3f' % (dmag[i])

        exptime = lsc.util.readkey3(hdr, 'exptime')
        object = lsc.util.readkey3(hdr, 'object').replace(' ', '')
        filtro = lsc.util.readkey3(hdr, 'filter')

        #######################################
        rap, decp, magp2, magp3, magp4, smagf, merrp3, smagerrf = [], [], [], [], [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec2)):
            aa = radec2[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(lsc.lscabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(lsc.lscabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp3.append(aa[7])
            _smagf, _smagerrf = 9999, 9999
            for j in range(len(fitmag2)):
                raf, decf, idf, magf, magerrf = fitmag2[j].split()
                if idf == idp:
                    _smagf = magf
                    _smagerrf = magerrf
                    break
            smagf.append(_smagf)
            smagerrf.append(_smagerrf)
        tbhdu = fits.BinTableHDU.from_columns(fits.ColDefs([fits.Column(name='ra', format='20A', array=np.array(rap)),
                                               fits.Column(name='dec', format='20A', array=np.array(decp)),
                                               fits.Column(name='ra0', format='E', array=np.array(rap0)),
                                               fits.Column(name='dec0', format='E', array=np.array(decp0)),
                                               fits.Column(name='magp2', format='E',
                                                           array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                                   np.array(magp2), 9999), float)),
                                               fits.Column(name='magp3', format='E',
                                                               array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                                       np.array(magp3), 9999), float)),
                                               fits.Column(name='merrp3', format='E',
                                                               array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                                       np.array(merrp3), 9999), float)),
                                               fits.Column(name='magp4', format='E',
                                                               array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                                       np.array(magp4), 9999), float)),
                                               fits.Column(name='smagf', format='E',
                                                               array=np.array(np.where((np.array(smagf) != 'INDEF'),
                                                                                       np.array(smagf), 9999), float)),
                                               fits.Column(name='smagerrf', format='E',
                                                               array=np.array(np.where((np.array(smagerrf) != 'INDEF'),
                                                                                       np.array(smagerrf), 9999),
                                                                              float)),
        ]))

        hdu = fits.PrimaryHDU(header=hdr)
        thdulist = fits.HDUList([hdu, tbhdu])
        lsc.util.delete(img + '.sn2.fits')
        thdulist.writeto(img + '.sn2.fits')
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [lsc.util.readkey3(hdr, 'naxis1'), 'x number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [lsc.util.readkey3(hdr, 'naxis2'), 'y number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0,
                              {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']})
        os.chmod(img + '.sn2.fits', 0664)
        os.chmod(img + '.psf.fits', 0664)
        result = 1

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
import csv

#########################################################
# Varibles/lists with global usage
pathtoscripts = ("/d/www/jcole/public_html/scripts/")
yesresponses = frozenset(["yes","Yes","YES","yEs","y","Y"])
noresponses = frozenset(["no","No","NO","nO","n","N"])
hashblock = "#######################"

#########################################################
# Preliminary log-header comparison

os.system('ls -1 *.fit > allfit.list')
os.system('cp allfit.list infiles.txt')
os.system('rm -rf images_info.list')
iraf.hselect(images='@allfit.list',fields='$I,FILTER',expr='yes',Stdout='images_info.list')

####################################################
os.system('rm -rf *z.fits')
os.system('rm -rf os_subtract.list')
os.system('touch os_subtract.list')
os.system('sed "s/.fit/_z.fits/" infiles.txt > os_subtract.list')

with open("infiles.txt") as file:
	with open ("os_subtract.list") as file2:
		lines = file.read().splitlines()
		outnames = file2.read().splitlines()
		for line, outname in zip(lines, outnames):
			iraf.imcopy(input="%s[8:2100,1:2048]" % (line),output="foo1.fits",verbose="no")
			#ccdproc(images="foo1.fits", output="foo1b.fits", overscan="yes", trim="yes", readaxis="column", biassec="[2054:2089,1:2048]", trimsec="[1:2048,*]")
			iraf.colbias(input="foo1.fits",output="foo1b.fits",bias="[2054:2089,1:2048]",trim="[1:2048,*]",interactive="no", order=3)
def main():
    print '################################################################'
    print '#'
    print '# WIRO Double Prime PyRAF Reduction Script'
    print '#'
    print '# Author: Jackson L. Cole'
    print '# Affiliation: Middle Tennessee State University'
    print '# 2017 University of Wyoming Astronomy REU'
    print '#'
    print '################################################################'
    print '################################################################'
    print '#'
    print '# Please make sure every file from the night of data'
    print '# being reduced is in the current working directory.'
    print '# This reduction script will handle all calibration,'
    print '# reduction, and reorganization of files for any'
    print '# number of objects observed during the night.'
    print '#'
    print '################################################################'
    print '#'
    print '# MAKE SURE YOU READ ALL OF THE PROMPTS AS THEY ARE'
    print '# PRESENTED. CHECK ALL LISTS AS PROMPTED AND TAKE'
    print '# CARE TO *****REMOVE PROBLEM IMAGES FROM THE LISTS'
    print '# ONLY*****'
    print '#'
    print '################################################################'

    print '################################################################'
    print '#'
    print '# ACKNOWLEDGEMENTS'
    print '#'
    print '# Dr. Chip Kobulnicky:'
    print '# \tThe overscan subtraction and trimming of overscan'
    print '# \tregions was adapted from a cl script written by'
    print '# \tDr. Kobulnicky specifically for the WIRO Double'
    print '# \tPrime Imager.'
    print '#'
    print '# David Kasper:'
    print '# \tThe utc_update script used in this script was'
    print '# \twritten by David in order to correct an'
    print '# \tissue found in the DATEOBS keyword in the FITS'
    print '# \theaders of WIRO images.'
    print '#'
    print '################################################################'

    raw_input('Please press [ENTER].')

    os.system('rm -rf ./*.list')

    os.system('ls -1 *.fit > allfit.list')
    os.system('cp allfit.list infiles.txt')
    os.system('rm -rf images_info.list')
    iraf.hselect(images='@allfit.list',
                 fields='$I,FILTER,EXPTIME',
                 expr='yes',
                 Stdout='images_info.list')

    #########################################################
    # Preliminary log-header comparison
    if os.path.exists('./filter_change_lists') == True:
        os.system('mv ./filter_change_lists/filter*change.list .')

    if os.path.isfile('filter_g_change.list') == False:
        os.system('touch ./filter_g_change.list')

    if os.path.isfile('filter_r_change.list') == False:
        os.system('touch ./filter_r_change.list')

    if os.path.isfile('filter_u_change.list') == False:
        os.system('touch ./filter_u_change.list')

    if os.path.isfile('filter_z_change.list') == False:
        os.system('touch ./filter_z_change.list')

    if os.path.isfile('filter_i_change.list') == False:
        os.system('touch ./filter_i_change.list')

    print "The following files have been created in the working directory:\n\nfilter_g_change.list\nfilter_r_change.list\nfilter_u_change.list\nfilter_z_change.list\nfilter_i_change.list\n\nimages_info.list\n\nPlease compare the images_info.list file with the log.txt file to check for correctness of the filter information in the headers. For each image that needs the filter information changed, add the image name to the appropriate file.\n\nExample: Changing a057.fit's filter information from r' to g' would require adding 'a057.fit' to the file filter_g_change.list."
    while True:
        answer = raw_input(
            'Have the images been added to the appropriate change files? (yes/no):\n'
        )
        if answer in yesresponses:
            break
    raw_input('Please press [ENTER] to continue: ')

    iraf.hedit(images='@filter_g_change.list',
               fields='FILTER',
               value="Filter 1: g'' 8009",
               add='yes',
               verify="no")
    iraf.hedit(images='@filter_r_change.list',
               fields='FILTER',
               value="Filter 2: r'' 21126",
               add='yes',
               verify="no")
    iraf.hedit(images='@filter_u_change.list',
               fields='FILTER',
               value="Filter 3: u'' 34242",
               add='yes',
               verify="no")
    iraf.hedit(images='@filter_z_change.list',
               fields='FILTER',
               value="Filter 4: z'' 47347",
               add='yes',
               verify="no")
    iraf.hedit(images='@filter_i_change.list',
               fields='FILTER',
               value="Filter 5: i'' 60445",
               add='yes',
               verify="no")

    if os.path.exists('.filter_change_lists') == False:
        os.system('mkdir filter_change_lists')

    os.system('mv ./filter*change.list ./filter_change_lists/')

    while True:

        raw_input(
            'It would be wise to double check the header information before continuing. Please press [ENTER] when completed.\n'
        )
        raw_input(
            'You should double check that information again. Press [ENTER] when finished.\n'
        )
        raw_input(
            'You sure you want to continue? Type Ctrl-c to stop script. Otherwise, please press [ENTER].\n'
        )

        #########################################################
        # Determining the number of objects and the image numbers
        # corresponding to each object
        while True:

            def objectfunc(first, last):
                file = open("allfit.list", 'r')
                lines = file.readlines()
                lines = lines[int(first) - 1:int(last)]
                file.close()

                os.system('touch object.list')
                file = open("object.list", 'w')
                for line in lines:
                    file.write(str(line) + '\n')
                file.close()

            print hashblock
            raw_input(
                'The following requested entries will ask for the first overall object image and the last overall object image.\nIf there are any issues relating to non-object images taken during the middle of the night, simply enter the first object image and last object image, and you will be allowed to edit before continuing.\nPress [ENTER].'
            )
            print hashblock

            while True:
                firstobject = raw_input(
                    'Please enter the number corresponding to the FIRST object image:\n'
                )
                if firstobject.isdigit() == True:
                    break

            while True:
                lastobject = raw_input(
                    'Please enter the number corresponding to the LAST object image:\n'
                )
                if lastobject.isdigit() == True:
                    break

            objectfunc(int(firstobject), int(lastobject))

            os.system('awk "NF" object.list > object.list.tmp')
            os.system('rm -rf object.list')
            os.system('cat object.list.tmp > object.list')
            os.system('rm -rf object.list.tmp')

            answer = raw_input(
                'Please check the file objects.list. Is everything correct? (yes/no):\n'
            )
            if answer in yesresponses:
                break

        print hashblock
        while True:
            print hashblock
            raw_input(
                'Now, you will be required to enter the NUMBER OF OBJECTS observed during the night, as well as the number of the images corresponding to the first and last image of each object. More than likely, only one object will have been observed.\nPlease press [ENTER].'
            )

            while True:
                noofobjects = raw_input(
                    'How many objects were observed during the night?:\n')
                if noofobjects.isdigit() == True:
                    break

            noofobjects = int(noofobjects)
            objects = []
            objectsrange = np.zeros((2, int(noofobjects)))
            names = []

            for i in range(0, int(noofobjects)):
                print hashblock
                while True:
                    name = raw_input(
                        'WITH NO SPACES, please enter the name of object {}:\n'
                        .format(i + 1))
                    objects.insert(i, name)
                    answer = raw_input(
                        "You have entered that object {} is named {}. Is this correct? (yes/no):\n"
                        .format(i + 1, name))
                    if answer in yesresponses:
                        break

                if noofobjects == 1:
                    os.system('cp ./object.list ./{}.object.list'.format(name))
                else:
                    while True:
                        start = raw_input(
                            'Enter the two or three digit number corresponding to the FIRST image of object {}:\n'
                            .format(name))
                        if start.isdigit() == True:
                            break
                    while True:
                        end = raw_input(
                            'Enter the two or three digit number corresponding to the LAST image of object {}:\n'
                            .format(name))
                        if end.isdigit() == True:
                            break

                    objectsrange[0][i] = int(start)
                    objectsrange[1][i] = int(end)

                    file = open("allfit.list", 'r')
                    lines = file.readlines()
                    lines = lines[start - 1:end]
                    file.close()

                    os.system('touch {}.object.list'.format(name))
                    file = open("{}.object.list".format(name), 'w')
                    for line in lines:
                        file.write(str(line) + '\n')
                    file.close()

                os.system('rm -rf object.{}.{}'.format(i + 1, name))
                os.system('mkdir object.{}.{}'.format(i + 1, name))
                os.system(
                    'sed -e "s/a/mv\ a/" -e "s/.fit/_zbft.fits\ \.\/object.{}.{}\//" {}.object.list > move.{}.object.cl'
                    .format(i + 1, name, name, name))
                names.insert(i, name)

                os.system(
                    'awk "NF" {}.object.list > {}.object.list.tmp'.format(
                        name, name))
                os.system('rm -rf {}.object.list'.format(name))
                os.system('cat {}.object.list.tmp > {}.object.list'.format(
                    name, name))
                os.system('rm -rf {}.object.list.tmp'.format(name))

                os.system('awk "NF" move.{}.object.cl > move.{}.object.cl.tmp'.
                          format(name, name))
                os.system('rm -rf move.{}.object.cl'.format(name))
                os.system('awk "NF" move.{}.object.cl.tmp > move.{}.object.cl'.
                          format(name, name))
                os.system('rm -rf move.{}.object.cl.tmp'.format(name))

            answer = raw_input(
                'Please check each object.{#}.{objectname} file before continuing. Is everything correct? (yes/no):\n'
            )
            if answer in yesresponses:
                break

        os.system('ls -1 move* > movescripts.list')

        #########################################################
        # Determining the number of bias images
        while True:
            while True:
                print hashblock
                start = raw_input(
                    'Enter the two or three digit number corresponding to the FIRST bias image:\n'
                )
                if start.isdigit() == True:
                    break
            while True:
                end = raw_input(
                    'Enter the two or three digit number corresponding to the LAST bias image:\n'
                )
                if end.isdigit() == True:
                    break

                answer = raw_input(
                    'You have entered that the bias range is {}-{}. Is this correct? You will have a chance to change things if there are breaks in the bias range. (yes/no):\n'
                    .format(start, end))
                if answer in yesresponses:
                    break

            file = open("allfit.list", 'r')
            lines = file.readlines()
            lines = lines[int(start) - 1:int(end)]
            file.close()

            os.system('touch bias.list')
            file = open("bias.list", 'w')
            for line in lines:
                file.write(str(line) + '\n')
            file.close()

            os.system('awk "NF" bias.list > bias.list.tmp')
            os.system('rm -rf bias.list')
            os.system('cat bias.list.tmp > bias.list')
            os.system('rm -rf bias.list.tmp')

            answer = raw_input(
                'Please check bias.list against the log file to ensure it is correct. Is everything correct? (yes/no):\n'
            )
            if answer in yesresponses:
                break

        #########################################################
        # Determining the number of flats
        print hashblock
        while True:
            answer = raw_input(
                'Will you be using flats taken on this night or during another night? (tonight/other):\n'
            )
            if answer == 'tonight' or 'other':
                break

        if answer == 'tonight':
            while True:
                while True:
                    while True:
                        start = raw_input(
                            'Enter the two or three digit number corresponding to the FIRST flat image:\n'
                        )
                        if start.isdigit() == True:
                            break
                    while True:
                        end = raw_input(
                            'Enter the two or three digit number corresponding to the LAST flat image:\n'
                        )
                        if end.isdigit() == True:
                            break

                    answer = raw_input(
                        'You have entered that the flat range is {}-{}. Is this correct? You will have a chance to change things if there are breaks in the flat range. (yes/no):\n'
                        .format(start, end))
                    if answer in yesresponses:
                        break

                file = open("allfit.list", 'r')
                lines = file.readlines()
                lines = lines[int(start) - 1:(end)]
                file.close()

                os.system('touch flat.list')
                file = open("flat.list", 'w')
                for line in lines:
                    file.write(str(line) + '\n')
                file.close()

                os.system('awk "NF" flat.list > flat.list.tmp')
                os.system('rm -rf flat.list')
                os.system('cat flat.list.tmp > flat.list')
                os.system('rm -rf flat.list.tmp')

                answer = raw_input(
                    'Please check flat.list against the log file for correctness. Is it correct? (yes/no):\n'
                )
                if answer in yesresponses:
                    break

        elif answer == 'other':
            while True:
                if os.path.exists('./otherflats') == False:
                    os.system('mkdir ./otherflats')
                elif os.path.exists('./otherflats') == True:
                    while True:
                        try:
                            os.rmdir('./otherflats')
                            empty = True
                            os.system('mkdir ./otherflats')

                        except OSError:
                            empty = False

                        if empty == True:
                            raw_input(
                                'The directory ./otherflats exists, but is empty. Please move the flats you intend to use to this directory. Press [ENTER] when done.'
                            )
                            answer = raw_input(
                                'Does the directory now contain the flats you intend to use? (yes/no):\n'
                            )

                        elif empty == False:
                            answer = raw_input(
                                'The directory ./otherflats exists and contains images. Are these the flats you intend to use? (yes/no)'
                            )

                        if answer in yesresponses:
                            break

                otherflatlist = []
                files = os.listdir('./otherflats')
                for file in files:
                    if file.endswith('.fit'):
                        otherflatlist.append(file)
                otherflatlist.sort()

                datalist = []
                files = os.listdir('.')
                for file in files:
                    if file.endswith('.fit'):
                        datalist.append(file)

                datalist.sort()

                highest = len(datalist)
                noofflats = len(otherflatlist)
                newnames = range(highest + 1, highest + 1 + noofflats)

                for i, element in enumerate(newnames):
                    newnames[i] = str((str(newnames[i]).zfill(3)))
                newnames = ["a" + name + ".fit" for name in newnames]

                for newname, flat in zip(newnames, otherflatlist):
                    os.system('mv ./otherflats/{} ./{}'.format(flat, newname))

                os.system('touch flat.list')

                with open('flat.list', 'w') as file:
                    for name in newnames:
                        file.write(str(name) + '\n')

                answer = raw_input(
                    'Please check the flats and make sure that no data images have been overwritten. Is everything good? (yes/no):\n'
                )
                if answer in yesresponses:
                    break

        #########################################################
        # Determining the number of darks
        while True:
            print hashblock
            darkanswer = raw_input('Were any dark exposures taken? (yes/no): ')

            if darkanswer in noresponses:
                print "Awesome! Proceeding as though no dark exposures were taken."
            elif darkanswer in yesresponses:
                while True:
                    while True:
                        start = raw_input(
                            'Enter the two or three digit number corresponding to the FIRST dark image: '
                        )
                        if start.isdigit() == True:
                            break
                    while True:
                        end = raw_input(
                            'Enter the two or three digit number corresponding to the LAST dark image: '
                        )
                        if end.isdigit() == True:
                            break

                    answer = raw_input(
                        'You have entered that the dark range is {}-{}. Is this correct? You will have a chance to change things if there are breaks in the dark range. (yes/no):\n'
                        .format(start, end))
                    if answer in yesresponses:
                        break

                file = open("allfit.list", 'r')
                lines = file.readlines()
                file.close()

                os.system('touch dark.list')
                file = open("dark.list", 'w')
                for line in lines[int(start) - 1:(end)]:
                    file.write(str(line) + '\n')
                file.close()

            os.system('awk "NF" dark.list > dark.list.tmp')
            os.system('rm -rf dark.list')
            os.system('cat dark.list.tmp > dark.list')
            os.system('rm -rf dark.list.tmp')

            answer = raw_input(
                'Please check dark.list to ensure it is correct. If no dark exposures were taken, this file should not exist. Is everything correct? (yes/no):\n'
            )
            if answer in yesresponses:
                break

        #########################################################
        # The following is meant to make sure that everything has
        # been thoroughly checked over before proceeding.
        while True:
            print hashblock
            allgood = raw_input(
                'Is everything good?\nReady to rock and roll?\nCan the data reduction process now move on seamlessly without human intervention based on how well you\'ve checked over the header information and list files? Are you confident that all will be well?\nThis is a reminder to be 100% sure of the status of things before proceeding.\nPlease enter YESYESYES to proceed:\n'
            )
            if allgood == 'YESYESYES':
                break
        goodtogo = raw_input('Are we good to go? (yes/no): ')
        if goodtogo in yesresponses:
            break

    #########################################################
    #########################################################
    # Reduction begins here
    iraf.hedit(images='@bias.list',
               fields='ccdtype',
               value='bias',
               add='yes',
               verify='no')
    iraf.hedit(images='@bias.list',
               fields='imagetype',
               value='zero',
               add='yes',
               verify='no')
    iraf.hedit(images='@object.list',
               fields='ccdtype',
               value='object',
               add='yes',
               verify='no')
    iraf.hedit(images='@object.list',
               fields='ccdtype',
               value='object',
               add='yes',
               verify='no')
    iraf.hedit(images='@flat.list',
               fields='ccdtype',
               value='flat',
               add='yes',
               verify='no')
    iraf.hedit(images='@flat.list',
               fields='ccdtype',
               value='flat',
               add='yes',
               verify='no')
    iraf.hedit(images='@dark.list',
               fields='ccdtype',
               value='dark',
               add='yes',
               verify='no')
    iraf.hedit(images='@dark.list',
               fields='ccdtype',
               value='dark',
               add='yes',
               verify='no')

    os.system('rm -rf flat_u_temp.list')
    os.system('rm -rf flat_g_temp.list')
    os.system('rm -rf flat_r_temp.list')
    os.system('rm -rf flat_i_temp.list')
    os.system('rm -rf flat_z_temp.list')
    os.system('rm -rf flat_filter.list')

    iraf.hselect(images='@flat.list',
                 fields='$I,FILTER',
                 expr='yes',
                 Stdout="flat_filter.list")
    os.system(
        'grep "Filter 1: g" flat_filter.list | cut -c1-8 > flat_g_temp.list')
    os.system(
        'grep "Filter 2: r" flat_filter.list | cut -c1-8 > flat_r_temp.list')
    os.system(
        'grep "Filter 3: u" flat_filter.list | cut -c1-8 > flat_u_temp.list')
    os.system(
        'grep "Filter 4: z" flat_filter.list | cut -c1-8 > flat_z_temp.list')
    os.system(
        'grep "Filter 5: i" flat_filter.list | cut -c1-8 > flat_i_temp.list')

    ####################################################
    os.system('rm -rf infiles.txt')
    os.system('ls -1 *.fit > infiles.txt')
    os.system('rm -rf *z.fits')
    os.system('rm -rf os_subtract.list')
    os.system('touch os_subtract.list')
    os.system('sed "s/.fit/_z.fits/" infiles.txt > os_subtract.list')

    print 'Now beginning overscan subtraction...'

    with open("infiles.txt") as file:
        with open("os_subtract.list") as file2:
            lines = file.read().splitlines()
            outnames = file2.read().splitlines()
            for line, outname in zip(lines, outnames):
                iraf.imcopy(input="%s[8:2100,1:2048]" % (line),
                            output="foo1.fits",
                            verbose="no")
                iraf.colbias(input="foo1.fits",
                             output="foo1b.fits",
                             bias="[2054:2089,1:2048]",
                             trim="[1:2048,*]",
                             interactive="no",
                             order=3)

                iraf.imcopy(input="%s[2101:4193,1:2048]" % (line),
                            output="foo2.fits",
                            verbose="no")
                iraf.colbias(input="foo2.fits",
                             output="foo2b.fits",
                             bias="[4:40,1:2048]",
                             trim="[46:2093,*]",
                             interactive="no",
                             order=3)

                iraf.imcopy(input="%s[8:2100,2049:4096]" % (line),
                            output="foo3.fits",
                            verbose="no")
                iraf.colbias(input="foo3.fits",
                             output="foo3b.fits",
                             bias="[2054:2088,1:2048]",
                             trim="[1:2048,*]",
                             interactive="no",
                             order=3)

                iraf.imcopy(input="%s[2101:4193,2049:4096]" % (line),
                            output="foo4.fits",
                            verbose="no")
                iraf.colbias(input="foo4.fits",
                             output="foo4b.fits",
                             bias="[4:40,1:2048]",
                             trim="[46:2093,*]",
                             interactive="no",
                             order=3)

                iraf.imcopy(input="%s[1:4096,1:4096]" % (line),
                            output="foo5.fits",
                            verbose="no")
                iraf.imarith(operand1="foo5.fits",
                             op="*",
                             operand2=1.0,
                             result="foo6.fits")
                iraf.imcopy(input="foo1b.fits",
                            output="foo6.fits[1:2048,1:2048]",
                            verbose="no")
                iraf.imcopy(input="foo2b.fits",
                            output="foo6.fits[2049:4096,1:2048]",
                            verbose="no")
                iraf.imcopy(input="foo3b.fits",
                            output="foo6.fits[1:2048,2049:4096]",
                            verbose="no")
                iraf.imcopy(input="foo4b.fits",
                            output="foo6.fits[2049:4096,2049:4096]",
                            verbose="no")
                iraf.imcopy(input="foo6.fits", output=outname, verbose="no")

                os.system('rm -rf foo*.fits')
                print "{} ----> {}".format(line, outname)

    print 'Moving original fits images to ./originals...'

    os.system('mkdir originals')
    os.system('mv *.fit ./originals/')

    os.system('rm -rf bias_z.list')
    os.system('rm -rf flat_z.list')
    os.system('rm -rf object_z.list')
    os.system('rm -rf dark_z.list')

    os.system('sed "s/.fit/_z.fits/" bias.list > bias_z.list')
    os.system('sed "s/.fit/_z.fits/" object.list > object_z.list')
    os.system('sed "s/.fit/_z.fits/" flat.list > flat_z.list')
    if darkanswer == 'yes' or 'Yes' or 'y' or 'Y':
        os.system('sed "s/.fit/_z.fits/" dark.list > dark_z.list')

    os.system('rm -rf zero_out.list')
    os.system('rm -rf bias_out_temp.list')
    os.system('ls -1 *_z.fits > zero_out.list')
    os.system('cp zero_out.list bias_out_temp.list')
    os.system('sed "s/_z.fits/_zb.fits/" bias_out_temp.list > bias_out.list')

    print 'Now subtracting bias...'
    iraf.imcombine(input='@bias_z.list',
                   output='masterbias_average.fits',
                   combine='average',
                   reject='minmax')
    iraf.unlearn('imarith')
    iraf.imarith(operand1='@zero_out.list',
                 op='-',
                 operand2='masterbias_average.fits',
                 result='@bias_out.list')

    os.system('rm -rf ./*z.fits')

    os.system('sed "s/.fit/_zb.fits/" flat_u_temp.list > flat_u.list')
    os.system('sed "s/.fit/_zb.fits/" flat_g_temp.list > flat_g.list')
    os.system('sed "s/.fit/_zb.fits/" flat_r_temp.list > flat_r.list')
    os.system('sed "s/.fit/_zb.fits/" flat_i_temp.list > flat_i.list')
    os.system('sed "s/.fit/_zb.fits/" flat_z_temp.list > flat_z.list')

    os.system('rm -rf masterflat_u.fits')
    os.system('rm -rf masterflat_g.fits')
    os.system('rm -rf masterflat_r.fits')
    os.system('rm -rf masterflat_i.fits')
    os.system('rm -rf masterflat_z.fits')

    print 'Combining flats...'
    try:
        iraf.imcombine(input='@flat_u.list',
                       output='masterflat_u.fits',
                       combine='median',
                       reject='avsigclip',
                       scale='median',
                       weight='median')
    except:
        pass

    try:
        iraf.imcombine(input='@flat_g.list',
                       output='masterflat_g.fits',
                       combine='median',
                       reject='avsigclip',
                       scale='median',
                       weight='median')
    except:
        pass

    try:
        iraf.imcombine(input='@flat_r.list',
                       output='masterflat_r.fits',
                       combine='median',
                       reject='avsigclip',
                       scale='median',
                       weight='median')
    except:
        pass

    try:
        iraf.imcombine(input='@flat_i.list',
                       output='masterflat_i.fits',
                       combine='median',
                       reject='avsigclip',
                       scale='median',
                       weight='median')
    except:
        pass

    try:
        iraf.imcombine(input='@flat_z.list',
                       output='masterflat_z.fits',
                       combine='median',
                       reject='avsigclip',
                       scale='median',
                       weight='median')
    except:
        pass

    os.system('rm -rf masterflat_u_norm.fits')
    os.system('rm -rf masterflat_g_norm.fits')
    os.system('rm -rf masterflat_r_norm.fits')
    os.system('rm -rf masterflat_i_norm.fits')
    os.system('rm -rf masterflat_z_norm.fits')

    print 'Normalizing flats...'
    try:
        iraf.imarith(operand1="masterflat_u.fits",
                     op="/",
                     operand2=np.mean(
                         (pyfits.open('masterflat_u.fits'))[0].data),
                     result="masterflat_u_norm.fits")
    except:
        pass
    try:
        iraf.imarith(operand1="masterflat_g.fits",
                     op="/",
                     operand2=np.mean(
                         (pyfits.open('masterflat_g.fits'))[0].data),
                     result="masterflat_g_norm.fits")
    except:
        pass

    try:
        iraf.imarith(operand1="masterflat_r.fits",
                     op="/",
                     operand2=np.mean(
                         (pyfits.open('masterflat_r.fits'))[0].data),
                     result="masterflat_r_norm.fits")
    except:
        pass

    try:
        iraf.imarith(operand1="masterflat_i.fits",
                     op="/",
                     operand2=np.mean(
                         (pyfits.open('masterflat_i.fits'))[0].data),
                     result="masterflat_i_norm.fits")
    except:
        pass

    try:
        z_mean = iraf.imarith(operand1="masterflat_z.fits",
                              op="/",
                              operand2=np.mean(
                                  (pyfits.open('masterflat_z.fits'))[0].data),
                              result="masterflat_z_norm.fits")
    except:
        pass

    os.system('rm -rf masterscience.list')
    os.system('rm -rf masterscience_g.list')
    os.system('rm -rf masterscience_r.list')
    os.system('rm -rf masterscience_u.list')
    os.system('rm -rf masterscience_z.list')
    os.system('rm -rf masterscience_i.list')
    os.system('rm -rf masterscience_g_f.list')
    os.system('rm -rf masterscience_r_f.list')
    os.system('rm -rf masterscience_u_f.list')
    os.system('rm -rf masterscience_z_f.list')
    os.system('rm -rf masterscience_i_f.list')

    #os.system('touch masterscience.list')
    iraf.hselect(images='*_zb.fits',
                 fields='$I,FILTER',
                 expr='yes',
                 Stdout="masterscience.list")

    os.system(
        'grep "Filter 1: g" masterscience.list | cut -c1-12 > masterscience_g.list'
    )
    os.system(
        'grep "Filter 2: r" masterscience.list | cut -c1-12 > masterscience_r.list'
    )
    os.system(
        'grep "Filter 3: u" masterscience.list | cut -c1-12 > masterscience_u.list'
    )
    os.system(
        'grep "Filter 4: z" masterscience.list | cut -c1-12 > masterscience_z.list'
    )
    os.system(
        'grep "Filter 5: i" masterscience.list | cut -c1-12 > masterscience_i.list'
    )

    os.system(
        'sed "s/_zb.fits/_zbf.fits/" masterscience_g.list > masterscience_g_f.list'
    )
    os.system(
        'sed "s/_zb.fits/_zbf.fits/" masterscience_r.list > masterscience_r_f.list'
    )
    os.system(
        'sed "s/_zb.fits/_zbf.fits/" masterscience_u.list > masterscience_u_f.list'
    )
    os.system(
        'sed "s/_zb.fits/_zbf.fits/" masterscience_z.list > masterscience_z_f.list'
    )
    os.system(
        'sed "s/_zb.fits/_zbf.fits/" masterscience_i.list > masterscience_i_f.list'
    )

    print 'Now flat fielding...'
    try:
        iraf.imarith('@masterscience_g.list', '/', 'masterflat_g_norm.fits',
                     '@masterscience_g_f.list')
    except:
        pass

    try:
        iraf.imarith('@masterscience_r.list', '/', 'masterflat_r_norm.fits',
                     '@masterscience_r_f.list')
    except:
        pass

    try:
        iraf.imarith('@masterscience_u.list', '/', 'masterflat_u_norm.fits',
                     '@masterscience_u_f.list')
    except:
        pass

    try:
        iraf.imarith('@masterscience_z.list', '/', 'masterflat_z_norm.fits',
                     '@masterscience_z_f.list')
    except:
        pass

    try:
        iraf.imarith('@masterscience_i.list', '/', 'masterflat_i_norm.fits',
                     '@masterscience_i_f.list')
    except:
        pass

    os.system('rm -rf ./*zb.fits')

    os.system('rm -rf object_filter.list')
    #os.system('object_zbf_g.list')
    #os.system('object_zbf_r.list')
    #os.system('object_zbf_u.list')
    #os.system('object_zbf_z.list')
    #os.system('object_zbf_i.list')
    #os.system('object_zbf_g.tmp')
    #os.system('object_zbf_r.tmp')
    #os.system('object_zbf_u.tmp')
    #os.system('object_zbf_z.tmp')
    #os.system('object_zbf_i.tmp')
    #os.system('object_zbf_g.cl')
    #os.system('object_zbf_r.cl')
    #os.system('object_zbf_u.cl')
    #os.system('object_zbf_z.cl')
    #os.system('object_zbf_i.cl')

    os.system('cp /d/www/jcole/public_html/scripts/utc_update.py .')
    print 'Now correcting the UTC information in the image headers...'

    os.system('python utc_update.py')
    os.system('rm -rf ./*zbf.fits')

    # The following moves the time corrected images into the appropriate object directories.
    '''
	with open("movescripts.list", 'r') as file:
		lines = file.readlines()
		for line in lines:
			print line

			iraf.cl(str(line))
	'''

    with open("movescripts.list", 'r') as file:
        lines = file.read().splitlines()
        for line in lines:
            with open(line, 'r') as file2:
                lines2 = file2.read().splitlines()
                for line2 in lines2:
                    os.system(str(line2))

    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    print 'Organizing based on object and filter...'
    for i, name in enumerate(names):
        where = os.getcwd()
        directory = "object.{}.{}".format(i + 1, name)
        os.system('rm -rf {}.list'.format(directory))
        path = "./{}".format(directory)
        os.chdir(path)
        os.system('rm -rf *list')
        os.system('rm -rf *cl')
        # In an object directory
        image_file = "{}_zbft.list".format(directory)
        filter_file = "{}_filter.list".format(directory)

        os.system('rm -rf {}'.format(image_file))
        #os.system('ls -1 *zbft.fits > {}'.format(image_file))
        iraf.files('*zbft.fits', Stdout='{}'.format(image_file))
        os.system('rm -rf {}'.format(filter_file))
        os.system('touch {}'.format(filter_file))

        images = []
        with open(image_file, 'r') as file:
            images = file.read().splitlines()

        os.system('rm -rf filter_g')
        os.system('rm -rf filter_r')
        os.system('rm -rf filter_u')
        os.system('rm -rf filter_z')
        os.system('rm -rf filter_i')

        os.system('mkdir filter_g')
        os.system('mkdir filter_r')
        os.system('mkdir filter_u')
        os.system('mkdir filter_z')
        os.system('mkdir filter_i')

        for image in images:
            data, header = fits.getdata(image, header=True)
            if header['FILTER'] == "Filter 1: g' 8009":
                os.system('mv ./{} ./filter_g/'.format(image))

            elif header['FILTER'] == "Filter 2: r' 21126":
                os.system('mv ./{} ./filter_r/'.format(image))

            elif header['FILTER'] == "Filter 3: u' 34242":
                os.system('mv ./{} ./filter_u/'.format(image))

            elif header['FILTER'] == "Filter 4: z' 47347":
                os.system('mv ./{} ./filter_z/'.format(image))

            elif header['FILTER'] == "Filter 5: i' 60445":
                os.system('mv ./{} ./filter_i/'.format(image))

        os.chdir(where)

    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #######################################################################
    #iraf.cl('object_zbf_g.cl')
    #iraf.cl('object_zbf_r.cl')
    #iraf.cl('object_zbf_u.cl')
    #iraf.cl('object_zbf_z.cl')
    #iraf.cl('object_zbf_i.cl')

    print 'Cleaning up...'
    os.system('rm -rf ./*z.fits')
    os.system('rm -rf ./*zb.fits')
    os.system('rm -rf ./*zbf.fits')
    os.system('rm -rf ./*zbft.fits')
    os.system('rm -rf ./masterflat*.fits')
    os.system('rm -rf ./masterflat*norm.fits')
    os.system('rm -rf ./masterbias*.fits')
    os.system('rm -rf ./masterdark*.fits')
    os.system('rm -rf ./*flat*.fits')

    print 'All done!'
Example #49
0
    print help
    sys.exit()
img = sys.argv[1]

options, args = getopt.getopt(sys.argv[2:], 'm:,i', ['magsel=', 'interactive'])
for opt, arg in options:
    if opt in ('-m', '--magsel'): magsel = arg
    if opt in ('-i', '--interactive'): interactive = True

iraf.display(img, 1)
iraf.delete('tmp*.*', verify='no')

##########    GET FIELD COORDINATES
RA, DEC = 0, 0
try:
    coord = iraf.hselect(img, 'RA,DEC', 'yes', Stdout=1)
    RA = float(string.split(coord[0], '\t')[0])
    DEC = float(string.split(coord[0], '\t')[1])
except:
    print "RA,DEC not found"

ra = iraf.clDms(RA)
dec = iraf.clDms(DEC)
answ = raw_input('>> RA,Dec [' + str(ra) + ' ' + str(dec) + '] ? ')
if len(answ) > 0:
    ra, dec = string.split(answ)
    RA = iraf.real(ra)
    DEC = iraf.real(dec)
iraf.printf("RA=%h DEC=%m\n ", RA, DEC)

ff = open('tmp.reg', 'w')
Example #50
0
def correct_airmass():
    stdout = os.popen("ls Y*otbfmsw.fits").readlines()
    namelst = [i.split('\n')[0] for i in stdout]
    for i in xrange(len(namelst)):
        fits = pyfits.open(namelst[i])
        extnum = len(fits)
        objname = fits[0].header['object']
        fits.close()
        print '#' * 50
        print namelst[i], objname
        name = raw_input('please input the name of object:')
        ra, dec = findradec(name)
        print name, ra, dec
        for j in xrange(extnum):
            stdout = iraf.hselect(images=namelst[i] + '[%i]' % j,
                                  fields='airmass',
                                  expr='yes',
                                  Stdout=1)
            airold = float(stdout[0])
            print '+' * 5, namelst[i], 'ext:', j, 'airmass_old:', airold
            iraf.hedit(images=namelst[i] + '[%i]' % j,
                       fields='airold',
                       value=airold,
                       add='yes',
                       addonly='yes',
                       delete='no',
                       verify='no',
                       show='yes',
                       update='yes')
            iraf.hedit(images=namelst[i] + '[%i]' % j,
                       fields='sname',
                       value=name,
                       add='yes',
                       addonly='yes',
                       delete='no',
                       verify='no',
                       show='yes',
                       update='yes')
            iraf.hedit(images=namelst[i] + '[%i]' % j,
                       fields='sname',
                       value=name,
                       add='yes',
                       addonly='yes',
                       delete='no',
                       verify='no',
                       show='yes',
                       update='yes')
            iraf.hedit(images=namelst[i] + '[%i]' % j,
                       fields='ra',
                       value=ra,
                       add='yes',
                       addonly='yes',
                       delete='no',
                       verify='no',
                       show='yes',
                       update='yes')
            iraf.hedit(images=namelst[i] + '[%i]' % j,
                       fields='dec',
                       value=dec,
                       add='yes',
                       addonly='yes',
                       delete='no',
                       verify='no',
                       show='yes',
                       update='yes')
            iraf.twodspec()
            iraf.longslit(dispaxis=2,
                          nsum=1,
                          observatory='Lijiang',
                          extinction='onedstds$LJextinct.dat',
                          caldir='onedstds$spec50cal/')
            iraf.setairmass(images=namelst[i] + '[%i]' % j,
                            observatory='Lijiang',
                            intype='beginning',
                            outtype='effective',
                            ra='ra',
                            dec='dec',
                            equinox='epoch',
                            st='lst',
                            ut='date-obs',
                            date='date-obs',
                            exposure='exptime',
                            airmass='airmass',
                            utmiddle='utmiddle',
                            scale=750.0,
                            show='yes',
                            override='yes',
                            update='yes')
            print 'name                            airmass_new     airmass_old'
            iraf.hselect(images=namelst[i] + '[%i]' % j,
                         fields='$I,airmass,airold',
                         expr='yes')
Example #51
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False):
    try:
        import lsc, string

        hdr = lsc.util.readhdr(img + '.fits')
        instrument = lsc.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

        if 'PIXSCALE' in hdr:
            scale = lsc.util.readkey3(hdr, 'PIXSCALE')
        elif 'CCDSCALE' in hdr:
            if 'CCDXBIN' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * lsc.util.readkey3(hdr, 'CCDXBIN')
            elif 'CCDSUM' in hdr:
                scale = lsc.util.readkey3(hdr, 'CCDSCALE') * int(string.split(lsc.util.readkey3(hdr, 'CCDSUM'))[0])

        if _datamax is None:
            _datamax = lsc.util.readkey3(hdr, 'datamax')
        _wcserr = lsc.util.readkey3(hdr, 'wcserr')
        print _wcserr
        if float(_wcserr) == 0:
            if 'L1FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1FWHM'))
            elif 'L1SEEING' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'L1SEEING')) * scale
            else:
                seeing = 3
        else:
            seeing = 3
            if 'PSF_FWHM' in hdr:
                seeing = float(lsc.util.readkey3(hdr, 'PSF_FWHM'))
            else:
                sys.exit('astrometry not good')
        #except:  sys.exit('astrometry not good')

        fwhm = seeing / scale
        print 'FWHM[header]  ', fwhm, '   in pixel'
        if ofwhm: fwhm = float(ofwhm)
        print '    FWHM[input]  ', fwhm, ' in pixel'

        xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split()
        print img, fwhm, threshold, scale,xdim

        #################################################################################
        ###################        write file to compute psf     _psf.coo    ############
        #################################################################################
        if _catalog:
            print '\n#### use catalog to measure the psf'
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            line=''
            for i in ddd:
                a,b,c = string.split(i)
                if float(a) < float(xdim) and  float(b) < float(ydim) and float(b) > 0:
                    line = line + '%10s %10s %10s \n' % (a, b, c)
            if line:
                ff = open('_psf.coo', 'w')
                ff.write(line)
                ff.close()
            else:
                sys.exit('error: no catalog objects are in the field')
        elif interactive:
            iraf.display(img + '[0]', 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []
            #############      write    file for PSF                           #########################
            ff = open('_psf.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()
            fwhm = np.median(_fws)
        else:
            ############              run  sextractor                #####################################
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            tot = np.compress(abs(np.array(fluxrad) * 1.6 - fwhm) / fwhm < .5, fluxrad)
            if len(tot) < 5:
                print 'warning: fwhm from sexractor different from fwhm computed during pre-reduction'
                print 'try using option --fwhm xxx'

            ff = open('tmp.cursor', 'w')
            image_hdu = fits.open(img + '.fits')
            for i in range(len(xs)):
                _xs = np.delete(xs, i)
                _ys = np.delete(ys, i)
                dist2 = np.sqrt((_xs - xs[i]) ** 2 + (_ys - ys[i]) ** 2)
                ###########           cut  star, not near other object    ##########################
                if abs(fluxrad[i] * 1.6 - fwhm) / fwhm < .5 and min(dist2) > distance * fwhm:
                    x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3)
                    y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3)
                    if x1 < 1: x1 = 1
                    if y1 < 1: y1 = 1
                    if x2 > int(xdim):
                        x2 = int(xdim)
                    if y2 > int(ydim):
                        y2 = int(ydim)
                    fmax = np.max(image_hdu[0].data[y1-1:y2, x1-1:x2])
                ##########       cut saturated object               ########################
                    if float(fmax) < _datamax:  # not saturated
                        ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i]))
            ff.close()
            image_hdu.close()

            iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False)
            iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3,
                            display=False, StdoutG='tmp.gki')

            ff = open('tmp.log')
            righe = ff.readlines()
            xn = [float(righe[3].split()[1])]
            yn = [float(righe[3].split()[2])]
            _fw = [float(righe[3].split()[4])]
            for r in righe[4:-2]:
                if len(r) > 0:
                    xn.append(float(r.split()[0]))
                    yn.append(float(r.split()[1]))
                    _fw.append(float(r.split()[3]))
            print 'FWHM: ', righe[-1].split()[-1]
            print 80 * "#"
            ######
            ##############            eliminate double object identification         ###########################
            xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]]
            for i in range(1, len(xn)):
                if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2:
                    xns.append(xn[i])
                    yns.append(yn[i])
                    _fws.append(_fw[i])
            #########      write clean   file for PSF                           #########################
            fw = []
            ff = open('_psf.coo', 'w')
            for i in range(len(xns)):
                if abs(_fws[i] - fwhm) / fwhm < .3:
                    ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
                    fw.append(_fws[i])
            ff.close()  ## End automatic selection
        ######################################################################################
        ###################        write file of object to store in  fits table  #############
        ######################################################################################
        if _catalog:
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_psf2.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
        elif interactive:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_psf2.coo', 'w')
            for i in range(len(xs)):
                ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i]))
            ff.close()
        else:
            os.system('cp _psf.coo _psf2.coo')
#            dflux = fluxrad - np.median(fluxrad)
#            fstar = np.compress(dflux < np.std(fluxrad), fluxrad)
#################################################################################################################

        print 80 * "#"
        photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture)

        photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture)

        radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag,
                              Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                              format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2,
                               Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2",
                               format='%13.3H %12.2h', min_sig=9, mode='h')[3:]

        if interactive or show:
            iraf.set(stdimage='imt1024')
            iraf.display(img + '[0]', 1, fill=True, Stdout=1)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=True, Stdin=photmag, nxoffset=5, nyoffset=5, txsize=2)
            iraf.tvmark(1, coords='STDIN', mark='circle', radii=35, label=False, Stdin=pst, color=208)
#            iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204)

        idpsf = []
        for i in range(len(pst)):
            idpsf.append(pst[i].split()[2])
        dmag = []
        for i in range(len(radec)):
            ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split()
            dmag.append(9.99)
            for j in range(len(fitmag)):
                raf, decf, idf, magf, magerrf = fitmag[j].split()
                if idph == idf and idph in idpsf and \
                                magp3 != 'INDEF' and magf != 'INDEF':
                    dmag[i] = float(magp3) - float(magf)
                    break

        _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag))

        print '>>> Aperture correction (phot)   %6.3f +/- %5.3f %3d ' % \
              (np.mean(_dmag), np.std(_dmag), len(_dmag))
        if len(_dmag) > 3:
            _dmag = np.compress(np.abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag)
            print '>>>         2 sigma rejection)   %6.3f +/- %5.3f %3d  [default]' \
                  % (np.mean(_dmag), np.std(_dmag), len(_dmag))
            print '>>>     fwhm   %s  ' % (str(fwhm))
        for i in range(len(dmag)):
            if dmag[i] == 9.99:
                dmag[i] = ''
            else:
                dmag[i] = '%6.3f' % (dmag[i])

        exptime = lsc.util.readkey3(hdr, 'exptime')
        object = lsc.util.readkey3(hdr, 'object').replace(' ', '')
        filtro = lsc.util.readkey3(hdr, 'filter')

        #######################################
        rap, decp, magp2, magp3, magp4, smagf, merrp3, smagerrf = [], [], [], [], [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec2)):
            aa = radec2[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(lsc.lscabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(lsc.lscabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp3.append(aa[7])
            _smagf, _smagerrf = 9999, 9999
            for j in range(len(fitmag2)):
                raf, decf, idf, magf, magerrf = fitmag2[j].split()
                if idf == idp:
                    _smagf = magf
                    _smagerrf = magerrf
                    break
            smagf.append(_smagf)
            smagerrf.append(_smagerrf)
        tbhdu = fits.BinTableHDU.from_columns(fits.ColDefs([fits.Column(name='ra', format='20A', array=np.array(rap)),
                                               fits.Column(name='dec', format='20A', array=np.array(decp)),
                                               fits.Column(name='ra0', format='E', array=np.array(rap0)),
                                               fits.Column(name='dec0', format='E', array=np.array(decp0)),
                                               fits.Column(name='magp2', format='E',
                                                           array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                                   np.array(magp2), 9999), float)),
                                               fits.Column(name='magp3', format='E',
                                                               array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                                       np.array(magp3), 9999), float)),
                                               fits.Column(name='merrp3', format='E',
                                                               array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                                       np.array(merrp3), 9999), float)),
                                               fits.Column(name='magp4', format='E',
                                                               array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                                       np.array(magp4), 9999), float)),
                                               fits.Column(name='smagf', format='E',
                                                               array=np.array(np.where((np.array(smagf) != 'INDEF'),
                                                                                       np.array(smagf), 9999), float)),
                                               fits.Column(name='smagerrf', format='E',
                                                               array=np.array(np.where((np.array(smagerrf) != 'INDEF'),
                                                                                       np.array(smagerrf), 9999),
                                                                              float)),
        ]))

        hdu = fits.PrimaryHDU(header=hdr)
        thdulist = fits.HDUList([hdu, tbhdu])
        lsc.util.delete(img + '.sn2.fits')
        thdulist.writeto(img + '.sn2.fits')
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [lsc.util.readkey3(hdr, 'naxis1'), 'x number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [lsc.util.readkey3(hdr, 'naxis2'), 'y number of pixels']})
        lsc.util.updateheader(img + '.sn2.fits', 0,
                              {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']})
        os.chmod(img + '.sn2.fits', 0664)
        os.chmod(img + '.psf.fits', 0664)
        result = 1

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Example #52
0
def main():

    # Make lists of each for Everything
    lc_out_list = np.asarray(param_lines[1].split())

    coords_list = np.asarray(param_lines[2].split())

    rootstr_list = np.asarray(param_lines[4].split())

    isOneRootStr = False
    if(rootstr_list.size == 1):
        isOneRootStr = True

    # check to see if endstr is static
    endstr_list = np.asarray(param_lines[5].split())
    if(endstr_list.size == 1):
        ep.endstr = endstr_list[0]  # if only one ending str
    else:
        checkSameSize(rootstr_list, endstr_list, "root string", "end string") # check list size
    fwhmpsf_list = np.asarray(map(float, param_lines[6].split()))

    annulus_list = np.asarray(map(float, param_lines[12].split()))

    dannulus_list = np.asarray(map(float, param_lines[13].split()))

    skyvalue_list = np.asarray(map(float, param_lines[14].split()))

    sigma_list = np.asarray(map(float, param_lines[7].split()))

    if(sigma_list.size == 1):
        ep.sigma = 1.0 * sigma_list[0]
    else:
        checkSameSize(rootstr_list, sigma_list, "root string", "sigma_list") # check list size

    hiclip_list = np.asarray(map(float, param_lines[8].split()))
    if(hiclip_list.size == 1):
        ep.hiclip = 1.0 * hiclip_list[0]
    else:
        checkSameSize(rootstr_list, hiclip_list, "root string", "hiclip") # check list size

    lowclip_list = np.asarray(map(float, param_lines[9].split()))
    if(lowclip_list.size == 1):
        ep.lowclip = 1.0 * lowclip_list[0]
    else:
        checkSameSize(rootstr_list, lowclip_list, "root string", "lowclip") # check list size


    # Set the absolute static parameters like egain and ccdread for easy_phot_params.py
    ep.image_dir = image_dir
    ep.egain = egain * 1.0
    ep.ccdread = ccdread * 1.0
    ep.amass = amass
    ep.obstime = obstime
    ep.exp = exp


    #### Throw some errors that check list size
    checkSameSize(rootstr_list, lc_out_list, "root string", "lc_out")
    checkSameSize(rootstr_list, coords_list, "root string", "coordinate")
    checkSameSize(rootstr_list, fwhmpsf_list, "root string", "fwhmpsf")
    checkSameSize(rootstr_list, annulus_list, "root string", "annulus")
    checkSameSize(rootstr_list, dannulus_list, "root string", "dannulus")
    checkSameSize(rootstr_list, skyvalue_list, "root string", "skyvalue")

    ### Check for indef in coordinate files
    #coordFiles = np.genfromtxt(coords_list_file, usecols=[0], dtype='str')
    noINDEF = []
    if isOneRootStr is not True:
        for k in range(coords_list.size):
            noINDEF.append(checkIndef(coords_list[k]))
    else:
        noINDEF.append(checkIndef(coords_list))

    if False in noINDEF:
        print
        print "Exiting from program..."
        sys.exit(1)



    final = open(final_file_name, 'w')


    # For loop that feeds easy_phot update parameters for a run on certain images
    numOfExecutes = 0
    if isOneRootStr is not True:
        numOfExecutes = rootstr_list.size
    else:
        numOfExecutes = 1

    #if isOneRootStr is not True:
    for i in range(numOfExecutes):

        # Update parameters for easy_phot_params
        ep.lc_out = lc_out_list[i]
        ep.coofnamein = coords_list[i]
        ep.rootstr = rootstr_list[i]
        ep.fwhmpsf = fwhmpsf_list[i]
        ep.annulus = annulus_list[i]
        ep.dannulus = dannulus_list[i]
        ep.skyvalue = skyvalue_list[i]

        # Those parameters that may be a list or not.
        if(endstr_list.size > 1):
            ep.endstr = endstr_list[i]

        if(sigma_list.size > 1):
            ep.sigma = sigma_list[i]

        if(hiclip_list.size > 1):
            ep.hiclip = hiclip_list[i]

        if(lowclip_list.size > 1):
            ep.lowclip = lowclip_list[i]


        # This runs easy_phot.py
        standard_output = sys.stdout # pass stdout to a variable rather than console
        f = open("easy_out.txt", 'w') # make a temporary file
        sys.stdout = f # prepares to dump stdout into file

        ep.execute() # execute easy_phot.py

        sys.stdout = standard_output # gives back stdout to console
        f.close()

        # Get the file names of this easy_phot.py instance
        f = open("easy_out.txt", 'r')

        fileList=[] # list for storing fits file names

        # goes through every line in the easy_phot.py console output file and isolates the fits file names
        # for storage.
        for line in f:
            if(line.startswith("/")):
                file = line.split("/")
                file = file[len(file) - 1]
                file = file.split("\n")
                file = file[0]
                fileList.append(file)
            else:
                continue

        f.close()

        # Get the JD from file header
        header_data = []
        for fileName in fileList:
            for item in header_items:
                data = iraf.hselect(images=fileName, fields=item , expr="yes", missing="INDEF", Stdout=1)
                header_data.append(data[0])

        header_data = np.asarray(header_data)

        mag = []
        merr = []

        # Get the number of stars from coordinate_list made by easy_phot.py
        numMeasurements = np.loadtxt("coords_list.txt", usecols=[0]).size

        # Grab mag values and mag error values
        for j in range(1, 2 * numMeasurements + 1):
            if(j % 2 == 1):
                temp_mag = np.genfromtxt(lc_out_list[i], usecols=[j], dtype='str')
                mag.append(temp_mag)
            else:
                temp_merr = np.genfromtxt(lc_out_list[i], usecols=[j], dtype='str')
                merr.append(temp_merr)


        # Write to everything to one file

        # Write a header line
        if(i == 0): # runs only once in the first loop
            num_header_items = len(header_items)
            final.write("name")
            for j in range(num_header_items):
                final.write(",%s"%header_items[j])
            for k in range(1, numMeasurements + 1):
                final.write(",Star%d"%k)
                final.write(",Star%d_err"%k)
            final.write("\n")

        for j in range(len(fileList)):
            final.write("%s,"%fileList[j])
            for num_item in range(num_header_items):
                final.write("%s,"%header_data[num_header_items * j + num_item])

            final.write("%s,"%mag[0][j])
            final.write("%s"%merr[0][j])
            for k in range(1, numMeasurements):
                final.write(",%s"%mag[k][j])
                final.write(",%s"%merr[k][j])
            final.write("\n")

        # Remove temporary files
        os.remove("easy_out.txt")

    # exit for loop and put finishing touches on.
    final.close()
    for j in lc_out_list:
        os.remove(j)

    print
    print "Program exited correctly...look for %s as your output file."%final_file_name

    print
    print "If the outputed file is missing lines make sure your rootstr and endstr arguements are correct."
Example #53
0
def sky_subtraction(targetdir):

    """
    Subtract A-B frames to remove sky

    """

    print 'Target directory is ' + targetdir
    print 'Doing sky subtraction...'

    names = np.array([name + '[1]' for name in os.listdir(targetdir) if (name.endswith('.fit')) & (name.startswith('fcr'))])

    mjd = np.array([iraf.hselect( os.path.join(targetdir,f), 'MJD-OBS', 'yes', Stdout=1)[0] for f in names])

    # Sort files names in order of time of observation
    names = names[np.argsort(mjd)]
    decoff = np.asarray([iraf.hselect(os.path.join(targetdir,f) , 'DECOFF', 'yes', Stdout=1)[0] for f in names],dtype='float')

    for n,d in zip(names,decoff):
        if d > -1.0:
            spos = 'A'
        else:
            spos = 'B'
        print n.replace('.fit[1]',''), spos


    if os.path.exists(os.path.join(targetdir,'resultsAB.list') ):
        os.remove(os.path.join(targetdir,'resultsAB.list') )
        print 'Removing file ' + os.path.join(targetdir,'resultsAB.list')

    if os.path.exists(os.path.join(targetdir,'resultsBA.list') ):
        os.remove(os.path.join(targetdir,'resultsBA.list') )
        print 'Removing file ' + os.path.join(targetdir,'resultsBA.list')

    if os.path.exists(os.path.join(targetdir,'A.list') ):
        os.remove(os.path.join(targetdir,'A.list' ) )
        print 'Removing file ' + os.path.join(targetdir,'A.list' )

    if os.path.exists(os.path.join(targetdir,'B.list') ):
        os.remove(os.path.join(targetdir,'B.list') )
        print 'Removing file ' + os.path.join(targetdir,'B.list')

    for i in range(int(len(names)/2)):

        j = i * 2

        # Looping through pairs, determine which is A and which is B

        if decoff[j] > -1.0:
            nameA = names[j]
            nameB = names[j+1]
        else:
            nameA = names[j+1]
            nameB = names[j]

        with open( os.path.join( targetdir,'A.list'),'a') as f:
            f.write( os.path.join(targetdir,nameA.replace('[1]','')) + '\n')

        with open( os.path.join( targetdir,'B.list'),'a') as f:
            f.write( os.path.join(targetdir,nameB.replace('[1]','')) + '\n')

        nameResultAB = 'd' + nameA
        nameResultAB = nameResultAB.replace('.fit[1]','') + '.fit'
        nameResultAB = os.path.join(targetdir,nameResultAB)

        with open(os.path.join(targetdir,'resultsAB.list'), 'a') as f:
            f.write(nameResultAB + '\n')

        if os.path.exists(os.path.join(targetdir,nameResultAB) ):
            os.remove(os.path.join(targetdir,nameResultAB) )
            print 'Removing file ' + os.path.join(targetdir,nameResultAB)

        nameResultBA = 'd' + nameB
        nameResultBA = nameResultBA.replace('.fit[1]','') + '.fit'
        nameResultBA = os.path.join(targetdir,nameResultBA)

        with open(os.path.join(targetdir,'resultsBA.list'), 'a') as f:
            f.write(nameResultBA + '\n')

        if os.path.exists(os.path.join(targetdir,nameResultBA) ):
            os.remove(os.path.join(targetdir,nameResultBA) )
            print 'Removing file ' + os.path.join(targetdir,nameResultBA)

    iraf.imarith.setParam('operand1', '@' + os.path.join(targetdir,'A.list') )
    iraf.imarith.setParam('operand2', '@' + os.path.join(targetdir,'B.list') )
    iraf.imarith.setParam('op','-')
    iraf.imarith.setParam('result','@' + os.path.join(targetdir,'resultsAB.list') )

    iraf.imarith()

    iraf.unlearn('imarith')

    iraf.imarith.setParam('operand1', '@' + os.path.join(targetdir,'resultsAB.list') )
    iraf.imarith.setParam('operand2', -1.0)
    iraf.imarith.setParam('op','*')
    iraf.imarith.setParam('result', '@' + os.path.join(targetdir,'resultsBA.list'))

    iraf.imarith()

    return None