Esempio n. 1
0
def run_imstat(input):
    """
    Call imstatistics for a list of images and show in terminal
    """
    #iraf.images()
    for image in input:
        iraf.imstat(image)
def badimage(img, _type):
    from pyraf import iraf
    import string
    import floyds
    hdr = floyds.util.readhdr(img)
    _tel = floyds.util.readkey3(hdr, 'telescop')
    rr1 = float(
        string.split(iraf.imstat(img + '[1250:1270,140:200]', Stdout=1)[1])[2])
    rr2 = float(
        string.split(iraf.imstat(img + '[1570:1620,170:210]', Stdout=1)[1])[2])
    bb1 = float(
        string.split(iraf.imstat(img + '[1090:1110,370:400]', Stdout=1)[1])[2])
    bb2 = float(
        string.split(iraf.imstat(img + '[1560:1700,340:360]', Stdout=1)[1])[2])
    bg1 = float(
        string.split(iraf.imstat(img + '[1500:1540,290:310]', Stdout=1)[1])[2])
    #print rr1,rr2,bb1,bb2,bg1
    good = 1
    if _type in ['flat', 'arc', 'lamp']:
        if rr1 / rr2 <= 1.25 and bb1 / bb2 <= 1.25:
            good = 0
    if good == 1:
        if _type in ['flat']:
            if rr1 / bg1 < 20 and bb1 / bg1 > 2.5 and 'South' in _tel:
                good = 0
            if bb1 / bg1 < 1.0:
                good = 0
            if rr1 < 1000:
                good = 0
    if good == 0: print '\n### warning:  image ' + img + ' rejected'
    return good
Esempio n. 3
0
def badimage(img,_type):
    from pyraf import iraf
    import string
    import floyds
    hdr=floyds.util.readhdr(img)
    _tel=floyds.util.readkey3(hdr,'telescop')
    rr1=float(string.split(iraf.imstat(img+'[1250:1270,140:200]',Stdout=1)[1])[2])
    rr2=float(string.split(iraf.imstat(img+'[1570:1620,170:210]',Stdout=1)[1])[2])
    bb1=float(string.split(iraf.imstat(img+'[1090:1110,370:400]',Stdout=1)[1])[2])
    bb2=float(string.split(iraf.imstat(img+'[1560:1700,340:360]',Stdout=1)[1])[2])
    bg1=float(string.split(iraf.imstat(img+'[1500:1540,290:310]',Stdout=1)[1])[2])
    #print rr1,rr2,bb1,bb2,bg1
    good=1
    if _type in ['flat','arc','lamp']:
        if rr1/rr2<=1.25 and bb1/bb2<=1.25:   
            good=0
    if good==1:
        if _type in ['flat']:
            if rr1/bg1<20 and bb1/bg1>2.5 and 'South' in _tel:
                good=0
            if bb1/bg1<1.0:
                good=0
            if rr1<1000:
                good=0
    if good==0:  print '\n### warning:  image '+img+' rejected'
    return good
def get_imstats(image):
    output = []
    with Capturing(output) as output:
        iraf.imstat(image)
    output = output[1].split("   ")
    print output
    max = float(output[-1])
    min = float(output[-2])
    return max, min
def get_imstats(image):
    output = []
    with Capturing(output) as output:
        iraf.imstat(image)
    output = output[1].split("   ")
    print output
    max = float(output[-1])
    min = float(output[-2])
    return max, min
Esempio n. 6
0
def rejectbias(lista, _interactive, nn=10):
    # print "LOGX:: Entering `rejectbias` method/function in %(__file__)s" %
    # globals()
    import numpy as np
    import ntt
    from pyraf import iraf

    iraf.images(_doprint=0)

    listgood = []
    f = open('_listgoodbias', 'w')
    for img in lista:
        f.write(img + '\n')
    f.close()
    biasstd = np.array(iraf.imstat(
        '@_listgoodbias', fields='stddev', format='no', Stdout=1), float)
    #     biasmedian=np.array(iraf.imstat('@_listgoodbias', fields='mean',format='no',Stdout=1),float)

    #     print lista
    median = np.median(biasstd)
    sigma = (np.percentile(biasstd, 75) - np.percentile(biasstd, 25)) * 1.349
    lista1 = np.compress((biasstd < (median + nn * sigma))
                         & (biasstd > (median - nn * sigma)), np.array(lista))
    #     lista1=np.compress((np.mean(biasstd)-2*np.std(biasstd)<=biasstd)&(np.mean(biasstd)+2*np.std(biasstd)>=biasstd)&
    #                        (np.mean(biasmedian)-2*np.std(biasmedian)<=biasmedian)&
    #                        (np.mean(biasmedian)+2*np.std(biasmedian)>=biasmedian), np.array(lista))
    ntt.util.delete('_listgoodbias')
    for i in range(0, len(lista)):
        if lista[i] not in lista1:
            print lista[i] + ' ' + str(biasstd[i]) + ' rejected'
        else:
            print lista[i], biasstd[i]
    for img in lista1:
        if _interactive:
            aa, bb, cc = ntt.util.display_image(img, 1, '', '', False)
            titolo, result = iraf.imstat(img, Stdout=1)
            print titolo[1:]
            print result
            answ = 'nn'
            while answ not in ['g', 'G', 'b', 's']:
                answ = raw_input('good/bad  [[g]/b/G(all good)/s(stop) ] ? ')
                if not answ:
                    answ = 'g'
                if answ not in ['g', 'G', 'b', 's']:
                    print 'warning: value not valid, try again'
            if answ == 'g':
                listgood.append(img)
            elif answ == 'G':
                listgood = lista1
                break
            elif answ == 's':
                break
        else:
            listgood.append(img)
    return listgood
Esempio n. 7
0
def writecountingstatstoafile(images, ditgroupcounter, bandpass, target):
    imagesinirafformat = str(images).replace("', '", ",").replace("['",
                                                                  "").replace(
                                                                      "']", "")
    date = images[0][5:11]
    outputfilename = '%s.%s.%s.%s.txt' % (target, date, bandpass,
                                          ditgroupcounter)
    iraf.imstat.unlearn()
    iraf.imstat(imagesinirafformat,
                field='image, mean, midpt, mode, stddev, npix',
                Stdout=outputfilename)
Esempio n. 8
0
def darkcom(input_dark):
    import glob
    import os, sys
    import numpy as np    
    from pyraf import iraf
    from astropy.io import fits
    from astropy.io import ascii
    curdir = os.getcwd()
    curdate = curdir.split('/')[-1]
    #dark = ", ".join(input_dark)
    iraf.noao()
    iraf.imred()
    iraf.ccdred()
    iraf.ccdred.setinst(instrume='camera', directo='/iraf/iraf/noao/imred/ccdred/ccddb/', query='q', review='no')
    #iraf.chdir('./dark')
    #dark = glob.glob('cal*dk*.fit')
    #dark = ascii.read('dark.list', guess=True, data_start=0)
    '''
    allexptime = []
    for i in range(len(dark)) :
        hdr = fits.getheader(dark[i])
        allexptime.append(hdr['exptime'])
    expset = set(allexptime)
    exptime = list(sorted(expset))
    i=0
    for i in range(len(exptime)) :
        print('Find images with exptime of '+str(exptime[i]))
        imlist = []
        for j in range(len(dark)) :
            hdr = fits.getheader(dark[j])
            if hdr['exptime'] == exptime[i] :
                imlist.append(dark[j])
            else :
                pass
        print(imlist)
        input_name = 'dark'+str(int(exptime[i]))+'.list'
        output_name = curdate+'_dark'+str(int(exptime[i]))+'.fits'
        #input_name = output_name[:-5]+'.list'
        f=open(input_name,'w+')
        for k in range(len(imlist)) : 
            f.write(imlist[k]+'\n')
        f.close()
    '''
    #output_name = curdate+'_dark'+str(int(exptime[i]))+'.fits'
    output_name = curdate+'_'+input_dark[:-5]+'.fits'
    print('Darkcombine is running...')
    iraf.imstat(images='z@'+input_dark)
    iraf.darkcombine(input='z@'+input_dark, output=output_name, combine='median', reject='minmax', process='no', scale='none', ccdtype='' )
    #os.system('/usr/bin/cp '+output_name+' ../')
    os.system('/usr/bin/cp '+output_name+' /data1/KHAO/MDFTS/red/masterdark/')
    #os.system('/usr/bin/rm d*.list')
    #iraf.chdir('../')
    iraf.dir('.')
    print('Output master '+output_name+' is created.') 
Esempio n. 9
0
def flatcom(input_flat='zFLAT*.fit'):
    import glob
    import os, sys
    import itertools
    import numpy as np
    from pyraf import iraf
    from astropy.io import fits
    from astropy.io import ascii
    iraf.noao()
    iraf.imred()
    iraf.ccdred()
    iraf.ccdred.setinst(instrume='camera', directo='/iraf/iraf/noao/imred/ccdred/ccddb/', query='q', review='no')
    # Filter classification
    calflat = glob.glob(input_flat)
    allfilter = []
    i=0
    for i in range(len(calflat)) :
        hdr = fits.getheader(calflat[i])
        allfilter.append(hdr['filter'])
    filterset = set(allfilter)
    infilter = list(sorted(filterset))
    i=0
    for i in range(len(infilter)) :
        print('Find images with filter of '+str(infilter[i]))
        imlist = []
        for j in range(len(calflat)) :
            hdr = fits.getheader(calflat[j])
            if hdr['filter'] == infilter[i] :
                imlist.append(calflat[j])
            else :
                pass
        print(imlist)
        imlist.sort()
        input_name = str(infilter[i])+'flat.list'
        k=0
        f=open(input_name,'w+')
        for k in range(len(imlist)) : 
            f.write(imlist[k]+'\n')
        f.close()
        output_name = input_name[:-5]+'.fits'
        #iraf.flatcombine(input='@'+input_name, output=output_name, combine='average', reject='crreject', process='no', scale='mode', ccdtype='', lsigma='3.', hsigma='3.' )
        iraf.flatcombine(input='@'+input_name, output=output_name, combine='median', reject='minmax', process='no', scale='mode', ccdtype='')
        print(output_name+' is created. Normalizing...')
        data, newhdr = fits.getdata(output_name, header=True)
        x = np.mean(data)
        nimage = data/x
        newflat_name = curdate+'_n'+str(infilter[i])+'flat.fits'
        fits.writeto(newflat_name, nimage, header=newhdr, overwrite=True)
        #os.system('/usr/bin/cp '+newflat_name+' ../')
        os.system('/usr/bin/cp '+newflat_name+' /data1/KHAO/MDFTS/red/masterflat_'+infilter[i]+'/')
    print('Normalised master flats are created.')
    iraf.imstat(images='*n?flat.fits')
Esempio n. 10
0
def cull_bias(biaslist, listname):

    iraf.imstat("@{}".format(listname))
    bads = raw_input("Select any biasi you want to remove, q to continue\n")
    while bads.lower() != "q":

        badlist = bads.split(",")
        print "badlist is {}".format(badlist)
        newlist = [i for i in biaslist if not any([j.strip() in i for j in badlist])]
        write_list(newlist, listname)
        iraf.imstat("@{}".format(listname))
        bads = raw_input("Select any biasi you want to remove, q to continue\n")

    return
Esempio n. 11
0
def main(fits, x0, x1, y0, y1):
    x0 = int(round(x0, 0))
    x1 = int(round(x1, 0))
    y0 = int(round(y0, 0))
    y1 = int(round(y1, 0))

    reg = "[%d:%d,%d:%d]" % (x0, x1 - 1, y0, y1 - 1)
    key_ls = ["IMAGE", "NPIX", "MEAN", "STDDEV", "MIDPT", "MODE", "MIN", "MAX"]
    fields = ",".join(key_ls).lower()
    result = iraf.imstat(fits + reg,
                         Stdout=1,
                         nclip=5,
                         lsigma=3.,
                         usigma=3.,
                         fields=fields,
                         format="no")
    #result		= iraf.imstat(fits+reg,Stdout=1,fields=fields,format="no")
    val_ls = [
        float(val) if str(val).replace(".", "").isdigit() else str(val)
        for val in result[0].split()
    ]
    d = dict([(key, val) for key, val in zip(key_ls, val_ls)])

    print d
    return d
Esempio n. 12
0
	def getObjectStatistics(self, photObject, multiple=2.5):

		#photObject.printInfo()

		# Will get statistics around a box of width = w + multiple/2., height = h + multiple/2.
		_boxX1 = int(photObject._pixelx - multiple*(self._MEDFWHM/2.))
		_boxX2 = int(photObject._pixelx + multiple*(self._MEDFWHM/2.))

		_boxY1 = int(photObject._pixely - multiple*(self._MEDFWHM/2.))
		_boxY2 = int(photObject._pixely + multiple*(self._MEDFWHM/2.))

		irafinput = "%s[%d:%d,%d:%d]" % (self._Name, _boxX1, _boxX2, _boxY1, _boxY2)

		tmp = iraf.imstat(irafinput, 
					fields='npix,mean,stddev,min,max,midpt',
					format=0,
					Stdout=1
				)

		tmp = [i for i in tmp[0].split(" ") if i != ""]
		photObject.setBox([_boxX1,_boxX2,_boxY1,_boxY2])
		photObject.setNPIX(float(tmp[0]))
		photObject.setMEAN(float(tmp[1]))
		photObject.setSTDDEV(float(tmp[2]))
		photObject.setMIN(float(tmp[3]))
		photObject.setMAX(float(tmp[4]))
		photObject.setMEDIAN(float(tmp[5]))
Esempio n. 13
0
def show_bias_files_statistics(list_of_files):
    """ Show the statistics for the bias files received.
    
    This function applies imstat to the files received and print the results.
    
    Args: 
        list_of_files: List of files to get their statistics.   
    
    """

    # Control pyraf exception.
    try:
        # Get statistics for the list of bias files using imstat.
        means = iraf.imstat(list_of_files, fields='mean', Stdout=1)
        means = means[IMSTAT_FIRST_VALUE:]
        mean_strings = [str(m).translate(None, ",\ ") for m in means]
        mean_values = [float(m) for m in mean_strings]

        # Print the stats results.
        logging.debug("Bias images stats: Max. mean: %d Min. mean: %d" %
                      (max(mean_values), min(mean_values)))

    except iraf.IrafError as exc:
        logging.error("Error executing imstat: Stats for bias images: %s" %
                      (list_of_files))
        logging.error("Iraf error is: %s" % (exc))

    except ValueError as ve:
        logging.error("Error calculating mean values: %s" % (mean_strings))
        logging.error("Error is: %s" % (ve))
Esempio n. 14
0
def get_stat_from_files(input, stat):
    iraf.images()
    result = []
    for image in input:
        result.append(
            float(iraf.imstat(image, fields=stat, format=0, Stdout=1)[0]))
    return result
Esempio n. 15
0
def show_bias_files_statistics(list_of_files):
    """ Show the statistics for the bias files received.
    
    This function applies imstat to the files received and print the results.
    
    Args: 
        list_of_files: List of files to get their statistics.   
    
    """
    
    # Control pyraf exception.
    try:
        # Get statistics for the list of bias files using imstat.
    	means = iraf.imstat(list_of_files, fields='mean', Stdout=1)
    	means = means[IMSTAT_FIRST_VALUE:]
    	mean_strings = [str(m).translate(None, ",\ ") for m in means]
    	mean_values = [float(m) for m in mean_strings]
    	
        # Print the stats results.
    	logging.debug("Bias images stats: Max. mean: %d Min. mean: %d" %
                      (max(mean_values), min(mean_values)))	
        
    except iraf.IrafError as exc:
    	logging.error("Error executing imstat: Stats for bias images: %s" %
                      (list_of_files))
    	logging.error("Iraf error is: %s" % (exc))  
        
    except ValueError as ve: 	
        logging.error("Error calculating mean values: %s" % (mean_strings))
        logging.error("Error is: %s" % (ve))          	
Esempio n. 16
0
def createFlatFiles():
	import re
	print "CreateFlatFiles start"
	for f in FILTERS:
		if(os.listdir(os.path.join(OUTPUTDIR, "flat", f))):
			iraf.imcombine.setParam("input", os.path.join(OUTPUTDIR, "flat", f) + "/*.fits")
			flatFile = os.path.join(OUTPUTDIR, "flat", f , "Flat.fits")
			if os.path.exists(flatFile):
				print("flatFile %s alreday exists deleting"  % flatFile)
				os.remove(flatFile)
			iraf.imcombine.setParam("output", flatFile)
			#from doc:	
			#http://www.iac.es/sieinvens/siepedia/pmwiki.php?n=HOWTOs.PythonianIRAF
			#--> iraf.listpix(mode='ql')     # confirms parameter
			#--> iraf.listpix(mode='h')     # doesn't ask for parameter...@@
			iraf.imcombine(mode="h")
			#NORMALIZE
			#imstat
			res = iraf.imstat(flatFile, Stdout=1)
			print(res[0].strip()) 
			print(res[1].strip()) 
			resArray = re.split("\s+", res[1].strip())
			#max value
			#toDivValue = float(resArray[5])
			#meanValue
			toDivValue = float(resArray[2])
			flatNormFile = os.path.join(OUTPUTDIR, "flat", f , "FlatNorm.fits")
			if os.path.exists(flatNormFile):
				print("flatNormFile %s alreday exists deleting"  % flatNormFile)
				os.remove(flatNormFile)
			#divide by max value
			iraf.imarith(flatFile, '/', toDivValue, flatNormFile)
		else:
			print("NO FLAT FILES for filter %s PRESENT" %f)
	print "CreateFlatFiles end"
Esempio n. 17
0
	def getBackgroundSTDEV(self):

		# load the header
		self.loadHeader()

		# Use iraf to get the sky background STDEV
	        skySTDEV = iraf.imstat(images=self._Name,
					fields='stddev',
					nclip = 25,
					format=0,
					Stdout=1
				)

		self._skySTDEV = float(skySTDEV[0])

		# Skynoise in JHK is interpolation smoothed due to blowing up the images
		# In the Drizzle algorithm the ratio of true to output noise is R = 1/(1-r/3) where r is pixfrac/scale

		if self._Band in 'JHK':
			self._skySTDEV = self._skySTDEV * self.getHeader("INTERPSM", 1.662)
		else:
			self._skySTDEV = self._skySTDEV * self.getHeader("INTERPSM", 1.07)

		print "SKY STDDEV: %f" % (self._skySTDEV)

		return self._skySTDEV
Esempio n. 18
0
def doDarks(dark, readnoise, images):
    '''This function will take the provided name of the dark frame (dark),
      determine the dark signal (median of the frame) and compute
      the dark current expected for each file name in images.  This is then
      compared to the readnoise.  A list of all images in which the dark 
      current is expected to be greater than the readnoise is printed,
      separated by commas, so you can cut-n-paste into ccdproc's epar.'''

    if not iraf.imaccess(dark):
        print "Error:  can't open dark frame %s" % (dark)
        return

    # First off, get the dark current and exposure time
    dc = float(iraf.imstat(images=dark, fields='mean', Stdout=1)[1])
    dt = get_header(dark, 'darktime', float)[dark]
    print 'dark:  count=%f   exptime=%f   dark current rate=%f' % (dc, dt,
                                                                   dc / dt)

    # Next, get the exposure times for each image
    res = get_header(images=images, field='exptime', type=float)

    # a list to be printed out at the end (good for cut-n-paste)
    names = []
    print "expected dark currents:"
    for name in res.keys():
        print name, "%.2f" % (res[name] * dc / dt)
        if res[name] * dc / dt > readnoise:
            names.append(name)
    print "expected dark current greater than read noise:"
    print string.join(names, ',')
Esempio n. 19
0
def getPeakPixel():
	x1=int(eval(xySkyDict[1][0]))-smallBox
	x2=int(eval(xySkyDict[1][0]))+smallBox
	y1=int(eval(xySkyDict[1][1]))-smallBox
	y2=int(eval(xySkyDict[1][1]))+smallBox
	foo = iraf.imstat(fitsDir+fitsFile+"["+str(x1)+":"+str(x2)+","+str(y1)+":"+str(y2)+"]",Stdout=1)
	# assumes max is last thing in imstat
	bar = foo[-1].split()[-1]
	return eval(bar)
Esempio n. 20
0
def biascom(input_bias='bias.list'):
    """
    1. Description 
    : This function makes a master bias image of SAO 1-m using Pyraf. Put bias images to 201XXXXX/bias/ directory. Run this code on 201XXXXX directory, then pyraf chdir task enter bias directory and makes process. Output image is zero.fits and it will be copied on upper directory. Due to iraf.chdir task, you should reset python when this code is finished in order to make confusion of current directory between iraf and python! 
    
    2. Usage 
    : Start on 2018XXXX directory. Make bias directory which contains each bias frame. Naming of each bias images should be cal*bias.fit. Then just use SAO_biascom()
    
    >>> SAO_biascom()

    3. History
    2018.03    Created by G.Lim.
    2018.12.17 Edited by G.Lim. Define SAO_biascom function. 
    2019.02.07 Assign archive of masterbias in each date by G. Lim
    """
    import glob
    import os, sys
    import numpy as np
    from pyraf import iraf
    from astropy.io import fits
    curdir = os.getcwd()
    curdate = curdir.split('/')[-1]
    iraf.noao()
    iraf.imred()
    iraf.ccdred()
    iraf.ccdred.setinst(instrume='camera', directo='/iraf/iraf/noao/imred/ccdred/ccddb/', query='q', review='no')
    #input_name = 'bias.list'
    output_name = curdate+'_zero.fits'
    #calibrations = input_bias
    #f    = open(input_name,'w+')
    #for i in range(len(calibrations)):    
    #    f.write(calibrations[i]+'\n')
    #f.close()
    print('Zerocombine is running...')
    iraf.imstat(images='@'+input_bias)
    iraf.zerocombine(input='@'+input_bias, output=output_name, combine='median', reject='minmax', process='no', scale='none', ccdtype='' )
    print('Output master '+output_name+' is created.')
    os.system('/usr/bin/cp '+output_name+' /data1/KHAO/MDFTS/red/masterbias/')
    iraf.dir('.')
Esempio n. 21
0
def photometry(filenames):
    """
        Do photometry of the files
        
    """
    for item in range(len(filenames)):
        print('Photometry on file', filenames[item][0])
        iraf.imstat(filenames[item][0])
        iraf.display(filenames[item][0], 1)
        print(
            'Press , (comma) on your target to get the coord and then q, but DONT press r \n'
        )
        imx = iraf.imexam(Stdout=1)
        # If there is an error like "# ^SyntaxError: unexpected EOF while parsing" then make imx[2] instead imx[3] in all places. It is mainly a file reading problem
        Xref = eval(
            imx[1].split()
            [0])  #Using the first star as ref star for cose shift calculation
        Yref = eval(imx[1].split()[1])
        if (imx[1].split()[10] == 'INDEF'):
            fwhm = 4.0
            print('fwhm:', 'INDEF', 'but taken as 4.0')
        else:
            fwhm = eval(imx[1].split()[10])
            print('fwhm:', fwhm)
        print('Reference cord:', Xref, Yref)
        file1 = open('coord_list', 'w')
        file1.write('{0:0.3f}\t{1:0.3f}\n'.format(Xref, Yref))
        file1.close()
        Xref1, Yref1 = np.loadtxt('coord_list')
        print('Xref1, Yref1', Xref1, Yref1)
        do_phot(filenames[item][0],
                'coord_list',
                fwhm=fwhm,
                sky_annulus=10.0,
                width_sky=10.,
                zeropoint=25.)

    print('All photometry is done. Enjoy! Check .phot file')
Esempio n. 22
0
def scaleimages(images):

    ims = str(images).replace("', '", ", ").replace("']", "").replace("['", "")
    iraf.imstat.unlearn()
    imstat = iraf.imstat(ims, field='image, mean, stddev', Stdout=1)
    means = [float(x.split()[1]) for x in imstat[1:]]
    stds = [float(x.split()[2]) for x in imstat[1:]]
    meanofmeans = np.mean(means)
    upperlimits = np.array(meanofmeans) + 2. * np.array(stds)
    lowerlimits = np.array(meanofmeans) - 2. * np.array(stds)

    means2 = np.array([])
    stds2 = np.array([])
    for index, im in enumerate(images):
        iraf.imstat.unlearn()
        lower = lowerlimits[index]
        upper = upperlimits[index]
        imstat = iraf.imstat(im,
                             field='image, mean, stddev',
                             Stdout=1,
                             lower=lower,
                             upper=upper)
        means2 = np.append(means2, float(imstat[1].split()[1]))
        stds2 = np.append(stds2, float(imstat[1].split()[2]))
    meanofmeans = np.mean(means2)

    for index, im in enumerate(images):
        scale = meanofmeans / means[index]
        scaledimage = 'sc' + im
        iraf.imarith.unlearn()
        iraf.imarith(im, '*', scale, result=scaledimage)
        oim = 'o' + im
        expr = 'cp %s %s' % (im, oim)
        os.system(expr)
        expr = 'mv %s %s' % (scaledimage, im)
        os.system(expr)
Esempio n. 23
0
def subtractskyrescaling(images, sky):
    for im in images:
        iraf.imstat.unlearn()
        imstat = iraf.imstat(im,
                             field='image, mean, midpt, mode, stddev',
                             Stdout=1)
        #meanim = np.mean([float(x.split()[1]) for x in imstat[1:]])
        iraf.imstat.unlearn()
        skystat = iraf.imstat(sky,
                              field='image, mean, midpt, mode, stddev',
                              Stdout=1)
        #meansky = np.mean([float(x.split()[1]) for x in skystat[1:]])
        #addresfact = meanim - meansky
        #addresfact=0.
        iraf.imarith.unlearn()
        #iraf.imarith(sky, '-' , addresfact, 'res'+sky)
        #iraf.imarith(im, '-', 'res'+sky, 's'+im)
        iraf.imarith(im, '-', sky, 's' + im)
        #removetempsky = 'rm %s' % ('res'+sky)
        #os.system(removetempsky)

    skysubtim = ['s' + x for x in images]

    return skysubtim
Esempio n. 24
0
def get_sky(image):
    '''return estimate for sky value and standard deviation of sky
    
    implicit assumption is that the sky is constant throughout the image
    and that most of the image actually shows the sky (no very crowded fields)!
    calls iraf.imstat with ncli=10
    this calculates mean and standard dev of mean, clips all values significantly above
    the standard and then recalculates mean and std dev. This loop runs 10 times to 
    clip of values of the stellar counts. 
    stat=iraf.imstat(image,fields='midpt,stddev',ncli=10,Stdout=1)
    return float(stat[1].split()[0]), float(stat[1].split()[1])

    Usage: sky,skydev=get_sky(filename)''' 
    stat=iraf.imstat(image,fields='midpt,stddev',ncli=10,Stdout=1)
    
    return float(stat[1].split()[0]), float(stat[1].split()[1])
Esempio n. 25
0
def trim_remove_bias(WhatToTrim):
    imstat = WhatToTrim + "[2098:2147,*]"
    mean_overscan = iraf.imstat(imstat, Stdout=1, fields="mean", format="no")

    single_file_new = WhatToTrim.replace(".fits",
                                         ".-overscan_will_be_removed.fits")
    iraf.imarith(WhatToTrim, "-", mean_overscan[0], single_file_new)

    old = single_file_new + "[51:2097,3:2063]"
    new = WhatToTrim.replace(".fits", ".trim_will_be_removed.fits")

    iraf.imcopy(old, new)

    os.remove(single_file_new)

    return new
Esempio n. 26
0
def _imstat(filename):
    """
    Call imstat on a file and convert the result to a dictionary.
    """
    s = iraf.imstat(filename, Stdout=1)
    if s[1].startswith("Error"):
        return {}

    stat_dict = dict(zip(s[0].split()[1:], s[1].split()))
    for key, val in stat_dict.items():
        try:
            val = float(val)
        except ValueError:
            pass
        stat_dict[key] = val
    return stat_dict
Esempio n. 27
0
def normalize_flats(files):
    """ Normalize a set of flat files. 
    
    This function receives a list of flat files and returns a list of
    files of the flat files after normalize them.
    The normalization is performed dividing the flat image by the mean
    value of the flat image. This mean is the result of applying imstat
    to each image.
    
    Args: 
        files: The names of the files corresponding to the flat images.
    
    """

    for fl in files:
        # Get the 'work' and 'normalized' names for the flat files to process.
        work_file = fl.replace("." + FIT_FILE_EXT, WORK_FILE_SUFFIX)

        norm_file = fl.replace("." + FIT_FILE_EXT, NORM_FILE_SUFFIX)

        # Getting statistics for flat file.
        try:
            flat_stats = iraf.imstat(work_file, fields=IMSTAT_MEAN, Stdout=1)
            flat_stats = flat_stats[IMSTAT_FIRST_VALUE]

            try:
                mean_value = float(flat_stats)

                # Normalize flat dividing flat by its mean value.
                iraf.imarith(work_file, '/', mean_value, norm_file)

            except iraf.IrafError as exc:
                logging.error(
                    "Error executing imarith: normalizing flat image: %s" %
                    (fl))
                logging.error("Iraf error is: %s" % (exc))

            except ValueError as ve:
                logging.error("Error calculating mean value for: %s" %
                              (flat_stats))
                logging.error("Error is: %s" % (ve))

        except iraf.IrafError as exc:
            logging.error(
                "Error executing imstat: getting stats for flat image: %s" %
                (fl))
            logging.error("Iraf error is: %s" % (exc))
Esempio n. 28
0
def normalize_flats(files):
    """ Normalize a set of flat files. 
    
    This function receives a list of flat files and returns a list of
    files of the flat files after normalize them.
    The normalization is performed dividing the flat image by the mean
    value of the flat image. This mean is the result of applying imstat
    to each image.
    
    Args: 
        files: The names of the files corresponding to the flat images.
    
    """
    	
    for fl in files:
        # Get the 'work' and 'normalized' names for the flat files to process.
        work_file = fl.replace("." + FIT_FILE_EXT, WORK_FILE_SUFFIX)
        
        norm_file = fl.replace("." + FIT_FILE_EXT, NORM_FILE_SUFFIX)
        
        # Getting statistics for flat file.
        try:
            flat_stats = iraf.imstat(work_file, fields=IMSTAT_MEAN, Stdout=1)
            flat_stats = flat_stats[IMSTAT_FIRST_VALUE]    
            
            try:
                mean_value = float(flat_stats)
                                
                # Normalize flat dividing flat by its mean value.
                iraf.imarith(work_file, '/', mean_value, norm_file)
    			
            except iraf.IrafError as exc:
                logging.error("Error executing imarith: normalizing flat image: %s" %
                              (fl))
                logging.error("Iraf error is: %s" % (exc))
                
            except ValueError as ve:     
                logging.error("Error calculating mean value for: %s" % 
                              (flat_stats))
                logging.error("Error is: %s" % (ve))                      
    	
        except iraf.IrafError as exc:
            logging.error("Error executing imstat: getting stats for flat image: %s" %
                          (fl))
            logging.error("Iraf error is: %s" % (exc))       
Esempio n. 29
0
def rejectflat(lista, _interactive):

    from numpy import where, size
    from ntt.util import display_image
    from pyraf import iraf

    iraf.images(_doprint=0)
    import os
    import string

    listone = []
    for img in lista:
        dataimg = pyfits.open(img)[0].data
        numberpixels = size(dataimg)
        indices = where(dataimg > 60000)
        numbersaturated = size(dataimg[indices])
        indices2 = where(dataimg < 1000)
        numberlow = size(dataimg[indices2])
        if 100 * float(numbersaturated) / float(numberpixels) <= 5 and \
                100 * float(numberlow) / float(numberpixels) <= 10:
            listone.append(img)
    listgood = []
    if _interactive:
        for img in listone:
            aa, bb, cc = display_image(img, 1, '', '', False)
            titolo, result = iraf.imstat(img, Stdout=1)
            print titolo[1:]
            print result
            answ = 'nn'
            while answ not in ['g', 'G', 'b', 's']:
                answ = raw_input('good/bad  [[g]/b/G(all good)/s(stop)]? ')
                if not answ:
                    answ = 'g'
                if answ not in ['g', 'G', 'b', 's']:
                    print 'warning: value not valid, try again'
            if answ == 'g':
                listgood.append(img)
            elif answ == 'G':
                listgood = listone
                break
            elif answ == 's':
                break
    else:
        listgood = listone
    return listgood
Esempio n. 30
0
	def trimMyself(self, outname="remcut.fits", region="[400:500,400:500]", verbose=False):
		
                self._logger["trimMyself"] = []

		if self._Name == "None":
			self._logger["trimMyself"].append("No filename set, please check.")

		imCopy = imFits()
		imCopy._Name = outname
		iraf.imcopy("%s%s" % (self._Name,region), imCopy._Name)
		
		if verbose:
			self._logger["trimMyself"].append(iraf.imstat(self._Name))	

			for log in self._logger["trimMyself"]:
				print log

		return imCopy
Esempio n. 31
0
def calculate_datamin(image_file_name, phot_params):
    """ Calculate a datamin value for the image received using imstat. 
    
    Args: 
        image_file_name: Name of the file with the image.
        phot_params: Parameters for phot.
    
    """

    # Set a default value for datamin.
    datamin = phot_params.datamin

    try:
        imstat_output = iraf.imstat(image_file_name, fields='mean,stddev', \
                                    Stdout=1)
        imstat_values = imstat_output[IMSTAT_FIRST_VALUE]
        values = imstat_values.split()

        # Set a calculated value for datamin.
        datamin = float(
            values[0]) - phot_params.datamin_mult * float(values[1])

    except iraf.IrafError as exc:
        logging.error("Error executing imstat: Stats for data image: %s" %
                      (image_file_name))
        logging.error("Iraf error is: %s" % (exc))

    except ValueError as ve:
        logging.error("Value Error calculating datamin for image: %s" %
                      (image_file_name))
        logging.error("mean is: %s stddev is: %s" % (values[0], values[1]))
        logging.error("Value Error is: %s" % (ve))

    if datamin < phot_params.datamin:
        datamin = phot_params.datamin

    return datamin
Esempio n. 32
0
def createFlatFiles():
    import re
    print "CreateFlatFiles start"
    for f in FILTERS:
        if (os.listdir(os.path.join(OUTPUTDIR, "flat", f))):
            iraf.imcombine.setParam(
                "input",
                os.path.join(OUTPUTDIR, "flat", f) + "/*.fits")
            flatFile = os.path.join(OUTPUTDIR, "flat", f, "Flat.fits")
            if os.path.exists(flatFile):
                print("flatFile %s alreday exists deleting" % flatFile)
                os.remove(flatFile)
            iraf.imcombine.setParam("output", flatFile)
            #from doc:
            #http://www.iac.es/sieinvens/siepedia/pmwiki.php?n=HOWTOs.PythonianIRAF
            #--> iraf.listpix(mode='ql')     # confirms parameter
            #--> iraf.listpix(mode='h')     # doesn't ask for parameter...@@
            iraf.imcombine(mode="h")
            #NORMALIZE
            #imstat
            res = iraf.imstat(flatFile, Stdout=1)
            print(res[0].strip())
            print(res[1].strip())
            resArray = re.split("\s+", res[1].strip())
            #max value
            #toDivValue = float(resArray[5])
            #meanValue
            toDivValue = float(resArray[2])
            flatNormFile = os.path.join(OUTPUTDIR, "flat", f, "FlatNorm.fits")
            if os.path.exists(flatNormFile):
                print("flatNormFile %s alreday exists deleting" % flatNormFile)
                os.remove(flatNormFile)
            #divide by max value
            iraf.imarith(flatFile, '/', toDivValue, flatNormFile)
        else:
            print("NO FLAT FILES for filter %s PRESENT" % f)
    print "CreateFlatFiles end"
Esempio n. 33
0
def skyflat(date, low=15000, high=23000, numimages=5):
	'''
	make a combined b skyflat. 
	Requires: a bias image in same directory to do the bias subtraction
		skyflats must be offset and have appropriate count number
	input: the date the skyflats were observed YYMMDD
	output: flat.B, a text file that lists the names of the skyflat fits files
		ccdYYMMDD.skyflatB.fits, the combined skyflat
	'''
	#check if biases are in this directory
	if len(glob.glob('*.bias.*')) < 1:
		print "no combined bias found, exiting"
		return
	#get image name and mean pixel value for all skyflat images
	stats=iraf.imstat('*sky*',format=False,fields='image,mean',Stdout=1)
	pairs=[i.split() for i in stats]

	#write the names of the skyflats w right ammount of counts to file
	#keep track of how many good ones there are
	goodCount=0
	with open("flat.B",'w') as FB:
		for i in pairs:
			if float(i[1]) > low and float(i[1]) < high:
				FB.write(i[0]+'\n')
				goodCount+=1

	if goodCount < numimages:
		print "only "+str(goodCount)+" skyflats have counts between "+str(low)+" and "+str(high)
		print "no combined skyflat made"
		return
	else:
		iraf.ccdproc(images="@flat.B",output=" ",fixpix="no",overscan="yes",trim="no",zerocor="yes",darkcor="no",flatcor="no",illumcor="no",fringecor="no",readcor="no",scancor="no",readaxis="line",biassec="[3:14,1:1024]",zero="*.bias.fits",interactive="no",functio="spline3",order=11)
		iraf.flatcombine("@flat.B",output="FLAT",combine="median",reject="minmax",process="no",scale="mode",ccdtype="")
		os.system("mv FLAT.fits ccd"+str(date)+".skyflatB.fits")
		print ("made combined skyflat ccd"+str(date)+".skyflatB.fits")
	return
Esempio n. 34
0
def calculate_datamin(image_file_name, phot_params):
    """ Calculate a datamin value for the image received using imstat. 
    
    Args: 
        image_file_name: Name of the file with the image.
        phot_params: Parameters for phot.
    
    """
    
    # Set a default value for datamin.
    datamin = phot_params.datamin
    
    try:
        imstat_output = iraf.imstat(image_file_name, fields='mean,stddev', \
                                    Stdout=1)
        imstat_values = imstat_output[IMSTAT_FIRST_VALUE]
        values = imstat_values.split()
        
        # Set a calculated value for datamin.
        datamin = float(values[0]) - phot_params.datamin_mult * float(values[1])
        
    except iraf.IrafError as exc:
        logging.error("Error executing imstat: Stats for data image: %s" %
                      (image_file_name))
        logging.error("Iraf error is: %s" % (exc))
        
    except ValueError as ve:
        logging.error("Value Error calculating datamin for image: %s" %
                      (image_file_name))
        logging.error("mean is: %s stddev is: %s" % (values[0], values[1]))
        logging.error("Value Error is: %s" % (ve))
        
    if datamin < phot_params.datamin:
        datamin = phot_params.datamin
        
    return datamin
Esempio n. 35
0
if not os.path.exists(save_path):
    os.makedirs(save_path)

#copy bias images to save_path
os.system('cp bias*.fits '+save_path)

#change to sabe_path
os.chdir(save_path)
#verify if previous superbias exist
if os.path.isfile('superbias.fits') == True:
    os.system('rm superbias.fits')

#create the list of bias images
bias_list = string.join(bias,',')

#combine the bias image and create the superbias
iraf.imcombine(bias_list,'superbias.fits')
iraf.imstat('superbias.fits')

#clean previos bias files
print '\n Cleaning bias*.fits images ....\n'
os.system('rm bias*.fits')
print '\n.... done.'

#Return to original directory
os.chdir(original_path)

#END of the masterbias reduction messsage
print '\nsuperbias.fits created!\n'
print '\nEND of superbias reduction!\n'
Esempio n. 36
0
def run_imstat(input):
    iraf.images()
    for image in input:
        iraf.imstat(image)
Esempio n. 37
0
def imshiftcomb(inlist,
                outimg,
                fitgeom='shift',
                inpref='',
                objmask='none',
                combine='average',
                reject='none',
                fscale=False,
                fbase=100,
                fhead='F1',
                second=False,
                first_pref='sdfr',
                second_pref='sdf2r',
                indep=False,
                sigmap='none',
                expmap='none',
                whtmap='none',
                gain=5.6,
                ffpref=''):

    # check output image
    if os.access(outimg, os.R_OK):
        print >> sys.stderr, 'operation would overwrite existing image (%s)' % outimg
        return 1

    # check input image list
    inimg_arr = check_input(inlist, inpref)
    if isinstance(inimg_arr, int):
        return 1

    # check input image list
    if sigmap != 'none':
        ffimg_arr = check_input2(inlist, ffpref)
        if isinstance(ffimg_arr, int):
            return 1

    # check optional output image
    if sigmap != 'none':
        if os.access(sigmap, os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing image (%s)' % sigmap
            return 1
    if expmap != 'none':
        if os.access(expmap, os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing image (%s)' % expmap
            return 1

    if whtmap != 'none':
        if os.access(whtmap, os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing image (%s)' % whtmap
            return 1

    # get array size
    im = pyfits.open(inimg_arr[0])
    nx = im[0].header['NAXIS1']
    ny = im[0].header['NAXIS2']
    im.close()

    # check geomap data
    dx = []
    dy = []
    gmp_arr = []
    gmp2_arr = []
    dbs_arr = []
    for i in range(len(inimg_arr)):
        fname, ext = os.path.splitext(inimg_arr[i])
        gmp = fname + '.gmp'
        gmp2 = fname + '.gmp2'
        dbs = fname + '.dbs'

        gmp_arr.append(gmp)
        gmp2_arr.append(gmp2)
        dbs_arr.append(dbs)

        if not os.access(gmp, os.R_OK):
            print >> sys.stderr, 'geomap file (%s) does not exist' % (gmp)
            return 1

        if os.access(dbs, os.R_OK):
            print >> sys.stderr, 'database file (%s) is already exist' % (dbs)
            return 1

        if os.access(gmp2, os.R_OK):
            print >> sys.stderr, 'modified geomap file (%s) is already exist' % (
                gmp2)
            return 1

        fgmp = open(gmp)
        nl = 1
        dx_ave = 0.0
        dy_ave = 0.0
        for line in fgmp:
            if not line.startswith('#'):
                param = line[:-1].split()
                if len(param) != 4:
                    print >> sys.stderr, 'Invalid format in line %d of %s: %s' % (
                        nl, gmp, line[:-1])
                    fgmp.close()
                    return 1
                else:
                    if isfloat(param[0]) == False or isfloat(
                            param[1]) == False or isfloat(
                                param[2]) == False or isfloat(
                                    param[3]) == False:
                        print >> sys.stderr, 'failed to decode line %d of %s: %s' % (
                            nl, gmp, line[:-1])
                        fgmp.close()
                        return 1
                    else:
                        dx_ave += float(param[0]) - float(param[2])
                        dy_ave += float(param[1]) - float(param[3])

                nl += 1
        #print inimg_arr[i],nl
        dx.append(dx_ave / (nl - 1))
        dy.append(dy_ave / (nl - 1))

    if len(inimg_arr) != len(dx):
        print >> sys.stderr, 'number of input images does not match with that of offsets'
        return 1

    #print 'debug'
    #print dx, max(dx), min(dx)
    #print dy, max(dy), min(dy)

    # check object mask
    if objmask.lower() == 'none':
        objmask = ''
    else:
        objmask_arr = check_inpref(objmask, inimg_arr)
        if isinstance(objmask_arr, int):
            return 1

    # independent run flag
    if indep:
        second = True

    # prepare for temporary file
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # calculate image median for zero shift
    iraf.unlearn('imstat')
    iraf.unlearn('mimstat')
    bgmed = []
    for i in range(len(inimg_arr)):
        if objmask == '':
            ret = iraf.imstat(inimg_arr[i],
                              format='no',
                              fields='midpt',
                              nclip=50,
                              lsigma=3.,
                              usigma=3.,
                              Stdout=1)
        else:
            if not second:
                ret = iraf.mimstat(inimg_arr[i],
                                   imasks=objmask_arr[i] + '[pl]',
                                   format='no',
                                   fields='midpt',
                                   nclip=50,
                                   lsigma=3.,
                                   usigma=3.,
                                   Stdout=1)
            else:
                ret = iraf.mimstat(inimg_arr[i],
                                   imasks=objmask_arr[i],
                                   format='no',
                                   fields='midpt',
                                   nclip=50,
                                   lsigma=3.,
                                   usigma=3.,
                                   Stdout=1)
        if len(ret) == 1:
            bgmed.append(-1.0 * float(ret[0]))
        else:
            fout.close()
            remove_temp_all(tmp_prefix)
            print >> sys.stderr, 'failed to calculate median of the background in %s' % inimg_arr[
                i]
            return 1

    # get large array size and combined image size
    ret = get_large_region(nx, ny, dx, dy)
    if len(ret) != 6:
        print >> sys.stderr, 'failed to get large array size'
        return 1

    x_size = ret[0]
    y_size = ret[1]
    xcmin = ret[2]
    xcmax = ret[3]
    ycmin = ret[4]
    ycmax = ret[5]

    # calculate image region in the large format
    xmin = int((x_size - nx) / 2) + 1
    xmax = nx + int((x_size - nx) / 2)
    ymin = int((y_size - ny) / 2) + 1
    ymax = ny + int((y_size - ny) / 2)

    #print 'debug'
    #print x_size, y_size, xcmin, xcmax, ycmin, ycmax
    #print xmin, xmax, ymin, ymax

    # copy image to larger format and shift image #
    iraf.unlearn('geomap')
    iraf.unlearn('geotran')

    obj_list = tmp_prefix + '_obj.lst'
    if os.access(obj_list, os.R_OK):
        os.remove(obj_list)
    fobj = open(obj_list, 'w')

    # for exposure time weight
    expweight = tmp_prefix + '_exp.lst'
    if os.access(expweight, os.R_OK):
        os.remove(expweight)
    fexp = open(expweight, 'w')

    # for zero offset
    zeroshift = tmp_prefix + '_zeroshift.dat'
    if os.access(zeroshift, os.R_OK):
        os.remove(zeroshift)
    fzero = open(zeroshift, 'w')

    # save the original fit geometry
    fitgeom_org = fitgeom

    # preparing for the sigma list and mask
    if sigmap == 'none':
        tmp_rejmask = ''
    else:
        tmp_rejmask = tmp_prefix + 'rejmask.fits'
        if os.access(tmp_rejmask, os.R_OK):
            os.remove(tmp_rejmask)
        inverse_var_list = tmp_prefix + '_var.lst'
        if os.access(inverse_var_list, os.R_OK):
            os.remove(inverse_var_list)
        finverse_var = open(inverse_var_list, 'w')

    for i in range(len(inimg_arr)):

        # restore the original fit geometry
        fitgeom = fitgeom_org

        # geometry transformation
        fgmp = open(gmp_arr[i])
        fgmp2 = open(gmp2_arr[i], 'w')
        nobj = 0
        for line in fgmp:
            if not line.startswith('#'):
                param = line[:-1].split()
                xref = float(param[0]) + xmin - 1
                yref = float(param[1]) + ymin - 1
                xin = float(param[2]) + xmin - 1
                yin = float(param[3]) + ymin - 1
                fgmp2.write('%.3f %.3f %.3f %.3f\n' % (xref, yref, xin, yin))
                nobj += 1
        fgmp.close()
        fgmp2.close()

        # check number of objects
        if i == 0 and nobj == 1 and fitgeom == 'rotate':
            print 'Warning: Number of reference objects is not enought to measure the rotation'
            print 'Warning: Only shift applied for all images'
            fitgeom = 'shift'
            fitgeom_org = 'shift'

        if nobj == 1 and fitgeom == 'rotate':
            print 'Warning: Number of objects in %s is not enought to measure the rotation' % (
                inimg_arr[i])
            print 'Warning: Only shift applied for %s' % (inimg_arr[i])
            fitgeom = 'shift'

        # mapping geometry
        iraf.geomap(gmp2_arr[i],
                    dbs_arr[i],
                    1,
                    x_size,
                    1,
                    y_size,
                    fitgeom=fitgeom,
                    interac='no')

        # mask frame
        msk = np.ones((y_size, x_size))
        msk[ymin - 1:ymax, xmin - 1:xmax] = 0
        hdu = pyfits.PrimaryHDU(msk)
        msk_img = pyfits.HDUList([hdu])
        msk_fits = tmp_prefix + 'mask' + os.path.basename(inimg_arr[i])
        msktr_fits = tmp_prefix + 'masktr' + os.path.basename(inimg_arr[i])
        if os.access(msk_fits, os.R_OK):
            os.remove(msk_fits)
        if os.access(msktr_fits, os.R_OK):
            os.remove(msktr_fits)
        msk_img.writeto(msk_fits)
        msk_img.close()

        # transform mask geometry
        iraf.geotran(msk_fits,
                     msktr_fits,
                     dbs_arr[i],
                     gmp2_arr[i],
                     geometr='linear',
                     boundar='constant',
                     constant=1)
        os.remove(msk_fits)
        convert_maskfits_int(msktr_fits, msktr_fits)

        # load original frame
        img = pyfits.open(inimg_arr[i])
        if sigmap != 'none':
            ffimg = pyfits.open(ffimg_arr[i])

        # for exposure time weight
        try:
            t = float(img[0].header['EXP1TIME'])
            coadd = float(img[0].header['COADDS'])
            expt = t * coadd
        except KeyError:
            print >> sys.stderr, 'can not read exposure time from the header of %s' % inimg_arr[
                i]
            img.close()
            return 1

        # for flux scaling and weight
        if fscale:
            try:
                flux = float(img[0].header[fhead])
            except KeyError:
                print >> sys.stderr, 'can not read flux keyword (%s) from the header of %s' % (
                    fhead, inimg_arr[i])
                img.close()
                return 1

            flux_scale = fbase / flux
            weight = expt * (1.0 / flux_scale)**2

        else:
            flux_scale = 1.0
            weight = expt

        fzero.write('%f\n' % (bgmed[i] * flux_scale))
        fexp.write('%f\n' % weight)

        # object frame
        obj = np.zeros((y_size, x_size))
        obj[ymin - 1:ymax, xmin - 1:xmax] = img[0].data * flux_scale
        hdu = pyfits.PrimaryHDU(obj)
        obj_img = pyfits.HDUList([hdu])
        obj_img[0].header = img[0].header
        obj_img[0].header['bpm'] = msktr_fits
        obj_img[0].header['expmap'] = expt
        obj_fits = tmp_prefix + 'obj' + os.path.basename(inimg_arr[i])
        objtr_fits = tmp_prefix + 'objtr' + os.path.basename(inimg_arr[i])
        if os.access(obj_fits, os.R_OK):
            os.remove(obj_fits)
        obj_img.writeto(obj_fits)
        obj_img.close()
        iraf.geotran(obj_fits,
                     objtr_fits,
                     dbs_arr[i],
                     gmp2_arr[i],
                     geometr='linear',
                     boundar='constant',
                     constant=0)
        fobj.write('%s\n' % objtr_fits)
        img.close()

        if sigmap != 'none':
            inverse_var = np.zeros((y_size, x_size))
            inverse_var[ymin - 1:ymax, xmin -
                        1:xmax] = (np.sqrt(ffimg[0].data / (gain * expt)) *
                                   flux_scale)**-2
            hdu_var = pyfits.PrimaryHDU(inverse_var)
            inverse_var_img = pyfits.HDUList([hdu_var])
            inverse_var_img[0].header = img[0].header
            inverse_var_img[0].header['bpm2'] = '%s[*,*,%d]' % (tmp_rejmask,
                                                                i + 1)
            inverse_var_fits = tmp_prefix + 'var' + os.path.basename(
                inimg_arr[i])
            inverse_vartr_fits = tmp_prefix + 'vartr' + os.path.basename(
                inimg_arr[i])
            if os.access(inverse_var_fits, os.R_OK):
                os.remove(inverse_var_fits)
            if os.access(inverse_vartr_fits, os.R_OK):
                os.remove(inverse_vartr_fits)
            inverse_var_img.writeto(inverse_var_fits)
            inverse_var_img.close()
            iraf.geotran(inverse_var_fits,
                         inverse_vartr_fits,
                         dbs_arr[i],
                         gmp2_arr[i],
                         geometr='linear',
                         boundar='constant',
                         constant=0)
            finverse_var.write('%s\n' % inverse_vartr_fits)
            ffimg.close()

    # close file handlers
    fobj.close()
    fexp.close()
    fzero.close()
    if sigmap != 'none':
        finverse_var.close()

    # combine image
    comb_img = tmp_prefix + '_comb.fits'
    if os.access(comb_img, os.R_OK):
        os.remove(comb_img)
    if expmap == 'none':
        tmp_expmap = ''
    else:
        tmp_expmap = tmp_prefix + 'expmap.fits'
        if os.access(tmp_expmap, os.R_OK):
            os.remove(tmp_expmap)

    iraf.unlearn('imcombine')
    try:
        iraf.imcombine('@' + obj_list,
                       comb_img,
                       sigma='',
                       rejmask=tmp_rejmask,
                       expmasks=tmp_expmap,
                       combine=combine,
                       reject=reject,
                       masktype='!BPM',
                       maskvalue=0.0,
                       zero='@' + zeroshift,
                       weight='@' + expweight,
                       expname='EXPMAP')
    except:
        if os.access(comb_img, os.R_OK):
            os.remove(comb_img)
        if expmap != 'none':
            if os.access(tmp_expmap, os.R_OK):
                os.remove(tmp_expmap)
        iraf.imcombine('@' + obj_list,
                       comb_img,
                       sigma='',
                       rejmask='',
                       expmasks=tmp_expmap,
                       combine=combine,
                       reject=reject,
                       masktype='!BPM',
                       maskvalue=0.0,
                       zero='@' + zeroshift,
                       weight='@' + expweight,
                       expname='EXPMAP')

    if sigmap != 'none':
        tmp_inverse_var_sum = tmp_prefix + 'inverse_var.fits'
        if os.access(tmp_inverse_var_sum, os.R_OK):
            os.remove(tmp_inverse_var_sum)
        iraf.imcombine('@' + inverse_var_list,
                       tmp_inverse_var_sum,
                       combine='sum',
                       reject='none',
                       masktype='!BPM',
                       maskvalue=0.0)
        iraf.stsdas()
        tmp_sigma = tmp_prefix + 'sigma.fits'
        if os.access(tmp_sigma, os.R_OK):
            os.remove(tmp_sigma)
        iraf.imcalc(tmp_inverse_var_sum,
                    tmp_sigma,
                    'sqrt(1.0/im1)',
                    pixtype='double')

    # cut image
    iraf.unlearn('imcopy')
    cut_img = '%s[%d:%d,%d:%d]' % (comb_img, xcmin, xcmax, ycmin, ycmax)
    iraf.imcopy(cut_img, outimg)
    if expmap != 'none':
        cut_exp = '%s[%d:%d,%d:%d]' % (tmp_expmap, xcmin, xcmax, ycmin, ycmax)
        iraf.imcopy(cut_exp, expmap)
    if sigmap != 'none':
        cut_sigma = '%s[%d:%d,%d:%d]' % (tmp_sigma, xcmin, xcmax, ycmin, ycmax)
        iraf.imcopy(cut_sigma, sigmap)
        if whtmap != 'none':
            cut_wht = '%s[%d:%d,%d:%d]' % (tmp_inverse_var_sum, xcmin, xcmax,
                                           ycmin, ycmax)
            iraf.imcopy(cut_wht, whtmap)

    # delete temporary object files
    remove_temp_all(tmp_prefix + 'obj')
    os.remove(obj_list)
    os.remove(comb_img)

    # record relative offset between input images and combined image and rotation
    for i in range(len(inimg_arr)):
        im = pyfits.open(inimg_arr[i], mode='update')

        if second:

            if indep:

                # calculate offset
                dxc = xcmin - xmin - dx[i]
                dyc = ycmin - ymin - dy[i]

                # retrieve rotation
                rot = 0.0
                fdbs = open(dbs_arr[i])
                for line in fdbs:
                    param = line[:-1].split()
                    if param[0] == 'xrotation':
                        rot = float(param[1])

                if rot > 180.0:
                    rot = rot - 360.0

                im[0].header['dx'] = dx[i]
                im[0].header['dy'] = dy[i]
                im[0].header['dxc'] = dxc
                im[0].header['dyc'] = dyc
                im[0].header['rotation'] = rot

            else:
                # check number of objects in the geomap file
                nobj = 0
                fgmp = open(gmp_arr[0])
                for line in fgmp:
                    nobj += 1

                im1 = pyfits.open(inimg_arr[i].replace(second_pref,
                                                       first_pref),
                                  mode='update')
                for j in range(nobj):
                    key = 'XC%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                    key = 'YC%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                    key = 'PEAK%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                    key = 'FWHM%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                key = 'DX'
                im[0].header[key] = float(im1[0].header[key])
                key = 'DY'
                im[0].header[key] = float(im1[0].header[key])
                key = 'DXC'
                im[0].header[key] = float(im1[0].header[key])
                key = 'DYC'
                im[0].header[key] = float(im1[0].header[key])
                key = 'ROTATION'
                im[0].header[key] = float(im1[0].header[key])
                im1.close()

        else:

            # calculate offset
            dxc = xcmin - xmin - dx[i]
            dyc = ycmin - ymin - dy[i]

            # retrieve rotation
            rot = 0.0
            fdbs = open(dbs_arr[i])
            for line in fdbs:
                param = line[:-1].split()
                if param[0] == 'xrotation':
                    rot = float(param[1])

            if rot > 180.0:
                rot = rot - 360.0

            im[0].header['dx'] = dx[i]
            im[0].header['dy'] = dy[i]
            im[0].header['dxc'] = dxc
            im[0].header['dyc'] = dyc
            im[0].header['rotation'] = rot

        im.close()

    # remove all temporary files
    remove_temp_all(tmp_prefix)

    return 0
Esempio n. 38
0
def lacos_im(_input,
             _output='clean.fits',
             outmask='mask.fits',
             gain=1.3,
             readn=9,
             xorder=9,
             yorder=9,
             sigclip=4.5,
             sigfrac=0.5,
             objlim=1,
             skyval=0,
             niter=2,
             verbose=True,
             interactive=False):
    import floyds
    from floyds.util import delete
    import sys, re, os, string
    from pyraf import iraf
    import numpy as np

    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # make temporary files
    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits',\
                                                     'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits',\
                                                  'starreject.fits'
    inputmask, gfirstsel = 'inputmask.fits', 'gfirstsel.fits'
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    # initialize loop
    usegain = gain
    i = 1
    stop = 'no'
    previous = 0
    if not _output: _output = _input

    arrayinput, headerinput = floyds.cosmics.fromfits(_input, verbose=False)
    floyds.cosmics.tofits(outmask,
                          np.float32(arrayinput - arrayinput),
                          headerinput,
                          verbose=False)

    delete(oldoutput)
    if skyval > 0:
        arrayoldoutput = arrayinput + skyval
    else:
        arrayoldoutput = arrayinput
    floyds.cosmics.tofits(oldoutput,
                          np.float32(arrayoldoutput),
                          headerinput,
                          verbose=False)
    # start iterations
    while stop == 'no':
        # take second-order derivative (Laplacian) of input image
        # kernel is convolved with subsampled image, in order to remove negative
        # pattern around high pixels
        delete(blk)
        delete(lapla)
        delete(deriv2)
        iraf.blkrep(oldoutput, blk, 2, 2)
        iraf.convolve(blk, lapla, '_kernel')
        iraf.imreplace(lapla, 0, upper=0, lower='INDEF', radius=0)
        iraf.blkavg(lapla, deriv2, 2, 2, option="average")
        delete(med5)
        # create model of background flux - 5x5 box should exclude all CRs
        iraf.median(oldoutput,
                    med5,
                    5,
                    5,
                    zlo='INDEF',
                    zhi='INDEF',
                    verbose='no')
        iraf.imreplace(med5, 0.0001, upper=0, lower='INDEF', radius=0)
        # create noise model
        delete(noise)
        iraf.imutil.imexpr(expr='sqrt(a*' + str(usegain) + '+' + str(readn) +
                           '**2)/' + str(usegain),
                           a=med5,
                           output=noise,
                           verbose='no')
        # divide Laplacian by noise model
        delete(sigmap)
        iraf.imarith(deriv2, "/", noise, sigmap)
        # Laplacian of blkreplicated image counts edges twice:
        iraf.imarith(sigmap, "/", 2., sigmap)
        # removal of large structure (bright, extended objects)
        delete(med5)
        iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
        arraysigmap, headersigmap = floyds.cosmics.fromfits(sigmap,
                                                            verbose=False)
        arraymed5, headermed5 = floyds.cosmics.fromfits(med5, verbose=False)
        arraysigmap = arraysigmap - arraymed5
        iraf.imarith(sigmap, "-", med5, sigmap)
        # find all candidate cosmic rays
        # this selection includes sharp features such as stars and HII regions

        delete(firstsel)
        iraf.imcopy(sigmap, firstsel, verbose='no')
        iraf.imreplace(firstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(firstsel, 1, lower=0.1, upper='INDEF', radius=0)
        #		arraygfirst=arraysigmap
        #		arraygfirst = np.where(arraygfirst<sigclip,0,arraygfirst)
        #		arraygfirst = np.where(arraygfirst>0.1,1,arraygfirst)

        # compare candidate CRs to median filtered image
        # this step rejects bright, compact sources from the initial CR list
        # subtract background and smooth component of objects
        delete(med3)
        delete(med7)
        iraf.median(oldoutput,
                    med3,
                    3,
                    3,
                    zlo='INDEF',
                    zhi='INDEF',
                    verbose='no')
        iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
        iraf.imarith(med3, "-", med7, med3)
        iraf.imarith(med3, "/", noise, med3)
        iraf.imreplace(med3, 0.01, upper=0.01, lower='INDEF', radius=0)
        # compare CR flux to object flux
        delete(starreject)
        iraf.imutil.imexpr(expr="(a*b)/c",
                           a=firstsel,
                           b=sigmap,
                           c=med3,
                           output=starreject,
                           verbose='no')
        # discard if CR flux <= objlim * object flux
        iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF', radius=0)
        iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(firstsel, "*", starreject, firstsel)
        # grow CRs by one pixel and check in original sigma map
        delete(gfirstsel)
        iraf.convolve(firstsel, gfirstsel, '_gkernel')
        iraf.imreplace(gfirstsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(gfirstsel, "*", sigmap, gfirstsel)
        iraf.imreplace(gfirstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(gfirstsel, 1, lower=0.1, upper='INDEF', radius=0)
        # grow CRs by one pixel and lower detection limit
        sigcliplow = sigfrac * sigclip
        delete(finalsel)
        iraf.convolve(gfirstsel, finalsel, '_gkernel')
        iraf.imreplace(finalsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(finalsel, "*", sigmap, finalsel)
        iraf.imreplace(finalsel, 0, upper=sigcliplow, lower='INDEF', radius=0)
        iraf.imreplace(finalsel, 1, lower=0.1, upper='INDEF', radius=0)
        # determine number of CRs found in this iteration
        delete(gfirstsel)
        iraf.imutil.imexpr(expr="(1-b)*a",
                           a=outmask,
                           b=finalsel,
                           output=gfirstsel,
                           verbose='no')

        npix = iraf.imstat(gfirstsel,
                           fields="npix",
                           lower=0.5,
                           upper='INDEF',
                           Stdout=1)
        # create cleaned output image; use 3x3 median with CRs excluded
        delete(med5)
        iraf.imarith(outmask, "+", finalsel, outmask)
        iraf.imreplace(outmask, 1, lower=1, upper='INDEF', radius=0)

        delete(inputmask)
        iraf.imutil.imexpr(expr="(1-10000*a)",
                           a=outmask,
                           output=inputmask,
                           verbose='no')
        iraf.imarith(oldoutput, "*", inputmask, inputmask)
        delete(med5)
        iraf.median(inputmask,
                    med5,
                    5,
                    5,
                    zloreject=-9999,
                    zhi='INDEF',
                    verbose='no')
        iraf.imarith(outmask, "*", med5, med5)
        if i > 1: delete(_output)

        delete(_output)
        iraf.imutil.imexpr(expr="(1.-b)*a+c",
                           a=oldoutput,
                           b=outmask,
                           c=med5,
                           output=_output,
                           verbose='no')

        # cleanup and get ready for next iteration
        delete(oldoutput)
        iraf.imcopy(_output, oldoutput, verbose='no')

        if npix == 0: stop = 'yes'
        i = i + 1
        if i > niter: stop = 'yes'
        # delete temp files
        delete(blk + "," + lapla + "," + deriv2 + "," + med5)
        delete(med3 + "," + med7 + "," + noise + "," + sigmap)
        delete(firstsel + "," + starreject + "," + gfirstsel)
        delete(finalsel + "," + inputmask)

    if skyval > 0: iraf.imarith(_output, "-", skyval, _output)
    delete('_kernel' + "," + '_gkernel')
    delete(oldoutput)
Esempio n. 39
0
def get_photometry(filelist, fwhmpsf, thresh, mkiraf=True):
    """
    Performs sextracting by daofind and photometry by daophot 
    filename is WITHOUT suffix .fits
    fwhmpsf is rough estimation of typical frame FWHM
    THRESH is signal-to-noise ratio, typically 4 or so
    Outputs are two files *.coo.1 and *.mag.1 with x-y coordinates
    """

    # Create login.cl at execution of the script if flag set to true
    if mkiraf:
        proc = subprocess.Popen(['mkiraf'],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        outs, errs = proc.communicate('y\nxterm')

    # Now we make the Pyraf imports
    from pyraf import iraf
    from pyraf.iraf import daophot

    # Parameter to get rid of the faint stars with high magnitude error
    magnitude_error_threshold = 0.5

    iraf.noao
    iraf.digiphot
    iraf.daophot

    iraf.unlearn('phot')
    iraf.unlearn('datapars')
    iraf.unlearn('photpars')
    iraf.unlearn('daopars')

    iraf.datapars.epadu = 1
    iraf.datapars.readnoi = 6
    iraf.datapars.datamin = "INDEF"
    iraf.datapars.datamax = "50000"

    # fwhm tarot 1.5   oaj 3.5
    iraf.datapars.fwhm = fwhmpsf

    for filename in filelist:

        IMstatres = iraf.imstat(filename, Stdout=1)
        IMmean = IMstatres[1].split()[2]
        iraf.datapars.sigma = math.sqrt(
            float(IMmean) * float(iraf.datapars.epadu) +
            float(iraf.datapars.readnoi)**2) / float(iraf.datapars.epadu)

        print("--- performing daophot sextracting ---")
        iraf.daofind(filename,
                     output="default",
                     verify="no",
                     verbose="no",
                     threshold=thresh)
        iraf.datapars.datamax = "INDEF"
        print("--- performing daophot photometry ---")
        iraf.daophot.phot(image=filename,
                          coords="default",
                          output="default",
                          interactive="no",
                          sigma="INDEF",
                          airmass="AIRMASS",
                          exposure="EXPOSURE",
                          filter="FILTER",
                          obstime="JD",
                          calgorithm="gauss",
                          verify="no",
                          verbose="no")
Esempio n. 40
0
def errore(img,imgpsf,coordlist,size,truemag,fwhm0,leng0,_show,_interactive,_numiter,z11,z22,midpt,nax,nay,xbgord0,ybgord0,_recenter,apco0,dmax=51000, dmin=-100):
    import agnkey
    import os,sys,re,string
    from numpy import array,mean,std,compress,average
    from pyraf import iraf
    if not _numiter: _numiter=3
    dartf=100
    while dartf>=size-1:
        if _interactive:
            artfac0 =raw_input('>>> Dispersion of artificial star positions (in units of FWHM) [1] ')
            if not artfac0: artfac0=1
        else:
            artfac0=1
        try:  
            artfac0=float(artfac0)
            if float(artfac0)>=size-1:
                print '!!! WARNING: '+str(artfac0)+' too large (max '+str(size)+'- 1)'
                print 'try again....'
            else:
                dartf = artfac0
        except:
            print '#### WARNING: '+str(artfac0)+' should be a number !!!!'
            print 'try again....'
    agnkey.util.delete("tmpar?")

    agnkey.util.delete('artskyfit.fits')
    os.system('cp skyfit.fits artskyfit.fits')
    i=0
    tmpart=[]
    while i<=8:
        agnkey.util.delete("reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?")
        artrad = fwhm0/2.
        #artseed = artseed+1234
        artx =  int(i/3.)-1
        if i<=2: arty = artx+i
        if 3<=i<=5: arty = artx-1+i-3
        if i>=6: arty = artx-2+i-6

        ff=open(img+".sn.coo",'r')
        ss=ff.readline()
        ff.close()
        xbb=float(string.split(ss)[0])
        ybb=float(string.split(ss)[1])

        xbb=xbb+artx*fwhm0*artfac0
        ybb=ybb+arty*fwhm0*artfac0

        agnkey.util.delete(coordlist)
        ff=open(coordlist,'w')
        ff.write(str(xbb)+'  '+str(ybb)+'  '+str(truemag[0])+"  1")
        ff.close()
                        
        xb1 = int(float(xbb)-fwhm0*float(leng0)/2)
        xb2 = int(float(xbb)+fwhm0*float(leng0)/2) 
        yb1 = int(float(ybb)-fwhm0*float(leng0)/2)
        yb2 = int(float(ybb)+fwhm0*float(leng0)/2)
        sec="1 "+str(xb1)+" 1 "+str(nay)+'\n'
        sec=sec+str(xb2)+' '+str(nax)+" 1 "+str(nay)+'\n'
        sesc=sec+str(xb1)+' '+str(xb2)+" 1 "+str(yb1)+'\n'
        sec=sec+str(xb1)+' '+str(xb2)+' '+str(yb2)+' '+str(nay)+'\n'
        ff = open('sec','w')
        ff.write(sec)
        ff.close()

        agnkey.util.delete("reserr.ar?")
        agnkey.util.delete("artlist.ma?")
        agnkey.util.delete("artsky.fit?")
        agnkey.util.delete("artbg.fit?")
        agnkey.util.delete("artbgs.fit?")
        agnkey.util.delete("artsn.fit?")
        agnkey.util.delete("artres.fit?")
        agnkey.util.delete("artlist.al?")

        iraf.addstar("artskyfit",coordlist,imgpsf,"reserr",nstar=1,veri='no',simple='yes',verb='no')  # reserr = skyfit + artificial star
########
        inp = "artbg.fits["+str(xb1)+":"+str(xb2)+","+str(yb1)+":"+str(yb2)+"]"
        out = "artsky.fits["+str(xb1)+":"+str(xb2)+","+str(yb1)+":"+str(yb2)+"]" 
        iraf.imsurfit("reserr","artbg",xorder=xbgord0,yorder=ybgord0,regions="section",section="sec")  
        midpt=float(iraf.imstat("artbg",field="mean", Stdout=1)[1])
        iraf.imcopy('reserr.fits','artsky.fits')
        iraf.imcopy(inp,'artbgs.fits')
        iraf.imcopy("artbgs.fits",out)
        iraf.imarith("reserr","-","artsky","artsn",calctype="r",pixtype="r",verb='no') 
        iraf.imarith("artsn","+",midpt,"artsn",verb='no')

        artap1,artap2,artap3,artmag1,artmag2,artmag3,artfitmag,arttruemag,artmagerr,artcentx,artcenty = \
            fitsn(img,imgpsf,coordlist,_recenter,fwhm0,'reserr','artsn','artres',_show,_interactive,z11,z22,midpt,size,apco0,dmax)

        for ii in range(0,_numiter):
            agnkey.util.delete("reserr.ar?")
            agnkey.util.delete("artlist.ma?")
            agnkey.util.delete("artsky.fit?")
            agnkey.util.delete("artbg.fit?")
            agnkey.util.delete("artbgs.fit?")
            agnkey.util.delete("artsn.fit?")
            agnkey.util.delete("artres.fit?")
            agnkey.util.delete("artlist.al?")

            iraf.imsurfit("skyfit","artbg",xorder=xbgord0,yorder=ybgord0,regions="section",section="sec")  
            midpt=float(iraf.imstat("artbg",field="mean", Stdout=1)[1])
            iraf.imcopy("reserr.fits","artsky.fits")
            iraf.imcopy(inp,"artbgs.fits")
            iraf.imcopy("artbgs.fits",out)

            iraf.imarith("reserr","-","artsky","artsn",calctype="r",pixtype="r",verb='no') 
            iraf.imarith("artsn.fits","+",midpt,"artsn.fits",verb='no') 
            artap1,artap2,artap3,artmag1,artmag2,artmag3,artfitmag,arttruemag,artmagerr,artcentx,artcenty=fitsn(img,imgpsf,coordlist,_recenter,fwhm0,'reserr','artsn','artres',_show,_interactive,z11,z22,midpt,size,0, dmax, dmin)
####### 
        if i==0: era='yes'
        else:    era='no'
        artx = .5+.25*artx
        arty = .5+.25*arty
        if _show:
            _tmp1,_tmp2,goon=agnkey.util.display_image('skyfit.fits',1,'', '', False, _xcen=artx, _ycen=arty, _xsize=.25, _ysize=.25, _erase=era)
	try:
            tmpart.append(float(arttruemag[0]))
	except: pass
        i=i+1

    for i in tmpart:  print i 
    
    print " ########## "
    try:
        media=mean(array(tmpart))
        arterr=std(array(tmpart))
        arterr2=std(compress((average(tmpart)-std(tmpart)<array(tmpart))&(array(tmpart)<average(tmpart)+std(tmpart)),array(tmpart)))
    except:
        media=0
        arterr=0
        arterr2=0
    print '### average = %6.6s \t arterr= %6.6s ' % (str(media),str(arterr))
    print '###  %6.6s \t (error at 1 sigma rejection) ' % (str(arterr2))
    agnkey.util.delete("reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?")
    agnkey.util.delete("reserr.ar?")
    agnkey.util.delete("artlist.co?")
    return arterr2,arterr
Esempio n. 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
Esempio n. 42
0
#     pixtype = "",\
#     calctype = "",\
#     verbose = 1,\
#     noact = 0,\
#     mode = "ql")    

# os.system("rm " + "master_smooth.fits")
# os.system("mv " + "temp_master_smooth.fits master_smooth.fits")

smooth_max = iraf.imstat(
    images = "master_smooth.fits[*,1,1]",\
    fields = "max",\
    lower = "INDEF",\
    upper = "INDEF",\
    nclip = 1,\
    lsigma = 5.0,\
    usigma = 5.0,\
    binwidth = 0.1,\
    format = 1,\
    cache = 1,\
    mode = "al",\
    Stdout = 1)

iraf.imarith(
    operand1 = "master_smooth.fits",\
    op = "/",\
    operand2 = smooth_max[1],\
    result = "temp_master_smooth.fits",\
    title = "",\
    divzero = 0.,\
    hparams = "",\
Esempio n. 43
0
def performPhotometry(task, logger):
  #iraf.prcacheOff()
  [iraf.unlearn(t) for t in ('phot','pstselect','psf','allstar')]
  iraf.set(imtype="fits,noinherit")   # set image output format
  iraf.set(clobber="yes")
  hdu=pyfits.open(task['images'])[0] 
  hdr = hdu.header
  imdata = hdu.data  
  for key,value in task['fits'].iteritems():
    task[key] = hdr.get(value,1)

  #Sextractor to find stars; add an object for the force detect
  logger.info('Running SExtractor on [%s]' % os.path.basename(task['images']))
  sex = sextractor.SExtractor()
  makeSexConfig(sex,task)
  sex.run(task['images'])
  catalog = sex.catalog()

  #Set up image parameters
  MIN_X = max(1,int(task['numpixx']*task['filtfactor']))
  MIN_Y = max(1,int(task['numpixy']*task['filtfactor']))
  MAX_X = int(task['numpixx']*(1-task['filtfactor']))
  MAX_Y = int(task['numpixy']*(1-task['filtfactor']))
  AREAXY = '[%s:%s,%s:%s]' % (MIN_X, MAX_X, MIN_Y, MAX_Y)
  AREANO = '[%s:%s,%s:%s]' % (MIN_X, MAX_X-2*MIN_X, MIN_Y, MAX_Y-2*MIN_Y)
  try:
    task['pixscale'] = abs(hdr.get('CD1_1'))*3600.
  except TypeError:
    task['pixscale'] = abs(hdr.get('CDELT1'))*3600.
  task['seeing'] = np.median( sorted([i['FWHM_IMAGE'] for i in catalog])[:int(-len(catalog)*0.5)] ) #Take the median of the "bottom" 50% of objects
  
  logger.info('--> %s SExtractor detected bright objects in the field' % (len(catalog),) )
  logger.info('--> %0.2f median FWHM of bright objects in the field, in arcsec' % (task['seeing']*task['pixscale'],))

  task['objects'] = [(i['ALPHA_J2000'],i['DELTA_J2000']) for i in catalog]
  task['objects'].append(task['objwcs'])  
  task['objectlist'] = open(os.path.join(task['output_directory'],'objectlist'),'w')  
  task['objectlist'].write('\n'.join([' %s %s' % (i[0],i[1]) for i in task['objects']]))
  task['objectlist'].close()

  logger.info('Running iraf.imstat')
  irafoutput = iraf.imstat(images=task['images']+AREANO,fields='midpt,min,max,stddev', format=0, Stdout=1)
  task['nimgs'] = hdr.get('NIMGS',1)
  task['gain'] *= task['nimgs']*2/3.
  task['ron'] *= np.sqrt(task['nimgs'])/task['nimgs']*constants.INTERPSM[task['band']]
  task['datamean'], task['datamin'], task['datamax'], task['datastdev']  = map(float, irafoutput[0].split())
  irafoutput = iraf.imstat(images=task['images'],fields='stddev,midpt',nclip=25,format=0,cache='yes',Stdout=1)
  task['skynoise'], task['datamean'] = map(float, irafoutput[0].split() )
  task['skynoise'] *= constants.INTERPSM[task['band']]
  task['airmass'] = hdr.get('AIRMASS',1)
  task['zmag'] -= (float(task['airmass'])-1.0)*constants.extinction_coefficients[task['band']]  
  task['match_proximity'] = 2.5 * task['seeing']
  logger.info('--> %5.2f counts: Sky noise, corrected for drizzle imcombine' % task['skynoise'])
  logger.info('--> %5.2f Median count value, after background subtraction' % task['datamean'])
  logger.info('--> %5.2f Airmass' % task['airmass'])

  #prepare temp files that iraf will use
  for filename in ('photfile','pstfile','psfimg','opstfile','groupfile','allstarfile','rejfile','subimage'): 
    task[filename] = open(os.path.join(task['output_directory'],filename),'w')
    task[filename].close()

  #iraf.phot to get APP magnitudes
  logger.info('Running iraf.apphot.phot')
  #apsizes = [i*task['faperture']*task['seeing'] for i in (0.4,0.5,0.6,0.8,1.0,1.2,1.5,2.0,2.5,3.0)]
  #irafapsizes = ','.join(['%.2f' % i for i in apsizes])
  irafapsizes = '%0.2f' % (task['faperture']*task['seeing'])
  kwargs = dict(image=task['images'],coords=task['objectlist'].name,
    output=task['photfile'].name,
    interac='no',scale=1,
    fwhmpsf=task['seeing'], 
    wcsin='world', wcsout='physical',
    sigma=task['skynoise'],
    datamin=task['datamin'],
    datamax=task['datamax'],
    readnoi=task['ron'],
    epadu=task['gain'],
    itime=task['exposure'],
    xairmass=task['airmass'],
    ifilter=task['band'],
    otime=task['dateobs'],
    aperture= irafapsizes,
    zmag=task['zmag'],
    annulus=task['fannulus']*task['seeing'],
    dannulus=task['fdannulus']*task['seeing'],
    calgorithm='gauss',
    cbox = 1.5*task['seeing'],
    maxshift=2.0*task['seeing'],
    mode="h",Stdout=1,verify=0)
  iraf.phot(**kwargs)

  if task['band'] not in constants.infrared:
    #iraf.pstselect to choose objects for PSF modelling
    logger.info('Running iraf.daophot.pstselect')
    kwargs = dict(image=task['images'],
                     photfile=task['photfile'].name,pstfile=task['pstfile'].name,
                     maxnpsf=task['pstnumber'],
                     wcsin='physical',
                     wcsout='physical',
                     interac="no",verify='no',scale=1,
                     fwhmpsf=task['seeing'],
                     datamin=0,
                     datamax=task['datamax'],
                     psfrad=3.0*task['seeing'],
                     fitrad=1.0*task['seeing'],
                     recente='yes',
                     nclean=task['nclean'],
                     mode="h",Stdout=1)
    iraf.pstselect(**kwargs)

    #iraf.psf to model PSF
    logger.info('Running iraf.daophot.psf')
    kwargs = dict( image=task['images'],
              photfile=task['photfile'].name,
              pstfile=task['pstfile'].name,
              psfimage=task['psfimg'].name,
              opstfile=task['opstfile'].name,
              groupfile=task['groupfile'].name,
              wcsin='physical',wcsout='physical',
              interac="no",verify="no",scale=1,
              fwhmpsf=task['seeing'],
              sigma=task['skynoise'],
              datamin=task['datamin'],
              datamax=task['datamax'],
              readnoi=task['ron'],
              epadu=task['gain'],
              itime=task['exposure'],
              xairmass=task['airmass'],
              ifilter=task['band'],
              otime=task['dateobs'],
              function=task['func'],
              varorder=task['varorder'],
              saturat='no',
              psfrad=3.0*task['seeing'],
              fitrad=1.*task['faperture']*task['seeing'],
              nclean=task['nclean'],
              mergerad=1.5*task['seeing'],
              mode='h',Stdout=1)  

    iraf.psf(**kwargs)
    logger.info('Running iraf.daophot.allstar')
    #iraf.allstars to compute PSF photometry; recenter with recenter='yes', mergerad=<value> to avoid duplicate detection
    kwargs = dict(image=task['images'],
                  photfile=task['photfile'].name,
                  wcsin='physical',
                  wcsout='physical',
                  psfimage=task['psfimg'].name,
                  allstarf=task['allstarfile'].name,
                  rejfile=task['rejfile'].name,
                  subimage=task['subimage'].name,
                  verbose=1,verify='no',scale=1,
                  fwhmpsf=task['seeing'],
                  sigma=task['skynoise'],
                  datamin=task['datamin'],
                  datamax=task['datamax'],
                  readnoi=task['ron'],
                  epadu=task['gain'],
                  itime=task['exposure'],
                  xairmass=task['airmass'],
                  ifilter=task['band'],
                  otime=task['dateobs'],
                  function=task['func'],
                  varorder=task['varorder'],
                  psfrad=3.*task['seeing'],
                  fitrad=1.*task['faperture']*task['seeing'],
                  recenter='yes',
                  mergerad=1.5*task['seeing'],
                  mode='h',Stdout=1)
    iraf.allstar(**kwargs)
  

  #Parse both photometry, convert to RA,DEC,MAG,MAGERR
  logger.info('iraf tasks complete. Parsing results and calibrating')
  photometry = {}
  photometry['APP'] = iraf.txdump(textfiles=task['photfile'].name,
                        fields='XCENTER,YCENTER,MAG,MERR',expr='yes',
                        headers='no',Stdout=1)

  if task['band'] not in constants.infrared:
    photometry['PSF'] = iraf.txdump(textfiles=task['allstarfile'].name,
                          fields='XCENTER,YCENTER,MAG,MERR',expr='yes',
                          headers='no',Stdout=1)


  for phototype in photometry:
    kwargs = dict(input='STDIN',
                  output='STDOUT',
                  insystem='%s physical' % task['images'],
                  outsystem='%s world' % task['images'],
                  ilatuni='physical',
                  ilnguni='physical',
                  olnguni='degrees',
                  olatuni='degrees',
                  ilngfor='%10.7f',
                  ilatfor='%10.7f',
                  olngfor='%10.5f',
                  olatfor='%10.5f',
                  Stdin=photometry[phototype],Stdout=1)
    photometry[phototype] = [i.split() for i in iraf.skyctran(**kwargs) if i and not i.startswith('#') and 'INDEF' not in i]
    photometry[phototype] = [map(float,(i[4],i[5],i[2],i[3])) for i in photometry[phototype] ] #Now we have [(ra,dec,'mag','mageerr'),...]
  results = calibrate((task['objwcs'][0],task['objwcs'][1]),task,photometry,logger)
#  if 'PSF' not in results:
    
  return results
Esempio n. 44
0
                                    _ybgord0 = ybgord0
                            try:
                                float(xbgord0)
                                float(ybgord0)
                                checkorder = 'no'
                            except:
                                print 'WARNING: value not valid !!'
                                checkorder = 'yes'
                        iraf.imsurfit("original",
                                      "bg",
                                      xorder=xbgord0,
                                      yorder=ybgord0,
                                      regions="sections",
                                      sections="sec")

                    midpt = float(iraf.imstat("bg", field="mean", Stdout=1)[1])
                    iraf.imcopy("original.fits", "sky.fits")
                    iraf.imcopy(inp, "bgs.fits")
                    iraf.imcopy("bgs.fits", out)
                    iraf.imarith("original.fits", "-", "sky.fits", "sn.fits")
                    iraf.imarith("sn.fits", "+", midpt, "sn.fits")
                    answ0 = 'y'
                    print answ0
                    if _show or _interactive:
                        _tmp1, _tmp2, goon = agnkey.util.display_image(
                            'original.fits',
                            1,
                            z11,
                            z22,
                            False,
                            _xcen=.25,
Esempio n. 45
0
File: rcred.py Progetto: nblago/kpy
def create_masterflat(flatdir=None, biasdir=None, channel='rc'):
    '''
    Creates a masterflat from both dome flats and sky flats if the number of counts in the given filter
    is not saturated and not too low (between 1500 and 40000). 
    '''
    
    
    if (flatdir == None or flatdir==""): flatdir = "."
        
    if (biasdir == None or biasdir==""): biasdir = "."
        
    os.chdir(flatdir)
    
    if (len(glob.glob("Flat_%s*norm.fits"%channel)) == 4):
        print "Master Flat exists!"
        return 
    else:
        print "Starting the Master Flat creation!"

    bias_slow = "Bias_%s_fast.fits"%channel
    bias_fast = "Bias_%s_fast.fits"%channel
    
    if (not os.path.isfile(bias_slow) and not os.path.isfile(bias_fast) ):
        create_masterbias(biasdir)
     
    lsflat = []
    lfflat = []
    
    #Select all filts that are Flats with same instrument
    for f in glob.glob("*fits"):
        #try:
        if fitsutils.has_par(f, "OBJECT"):
            obj = str.upper(fitsutils.get_par(f, "OBJECT"))
        else:
            continue
        
        if ( ("DOME" in  obj or "FLAT" in obj) and (channel == fitsutils.get_par(f, "CHANNEL"))):
            if (fitsutils.get_par(f, "ADCSPEED")==2):
                lfflat.append(f)
            else:
                lsflat.append(f)
        #except:
        #    print "Error with retrieving parameters for file", f
        #    pass
                
    print "Files for slow flat", lsflat
    print "Files for fast flat", lfflat
    
    fsfile ="lflat_slow_"+channel
    np.savetxt(fsfile, np.array(lsflat), fmt="%s")
    fffile ="lflat_fast_"+channel
    np.savetxt(fffile, np.array(lfflat), fmt="%s")



    # Running IRAF
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    
    #Remove bias from the flat
    if len(lsflat) >0:
        iraf.imarith("@"+fsfile, "-", bias_slow, "b_@"+fsfile)
    
    if len(lfflat) >0:
        iraf.imarith("@"+fffile, "-", bias_fast, "b_@"+fffile)    
    
    #Slices the flats.
    debiased_flats = glob.glob("b_*.fits")
    for f in debiased_flats:
        print "Slicing file", f
        slice_rc(f)
        #Remove the un-sliced file
        os.remove(f)
        
    #Selects the ones that are suitable given the number of counts and combines them.
    bands = ['u', 'g', 'r', 'i']
    for b in bands:
        out = "Flat_%s_%s.fits"%(channel, b)
        out_norm = out.replace(".fits","_norm.fits")

        if (os.path.isfile(out_norm)):
            print "Master Flat for filter %s exists. Skipping..."%b
            continue
        
        lfiles = []
        for f in glob.glob('b_*_%s.fits'%b):
            d = pf.open(f)[0].data
            if np.percentile(d, 90)>1500 and np.percentile(d, 90)<40000:
                lfiles.append(f)

        if len(lfiles) == 0:
            print "WARNING!!! Could not find suitable flats for band %s"%b
            continue
        ffile ="lflat_"+b
        np.savetxt(ffile, np.array(lfiles), fmt="%s")
    
        
        #Cleaning of old files
        if(os.path.isfile(out)): os.remove(out)
        if(os.path.isfile(out_norm)): os.remove(out_norm)
        if(os.path.isfile("Flat_stats")): os.remove("Flat_stats")
        
        
        #Combine flats
        iraf.imcombine(input = "@"+ffile, \
                        output = out, \
                        combine = "median",\
                        scale = "mode",
                        weight = "exposure")
        iraf.imstat(out, fields="image,npix,mean,stddev,min,max,mode", Stdout="Flat_stats")
        st = np.genfromtxt("Flat_stats", names=True, dtype=None)
        #Normalize flats
        iraf.imarith(out, "/", st["MODE"], out_norm)
        
        #Do some cleaning
        print 'Removing from lfiles'
        for f in glob.glob('b_*_%s.fits'%b):
            os.remove(f)

        os.remove(ffile)
        
        
        if os.path.isfile(fsfile):
            os.remove(fsfile)
        if os.path.isfile(fffile):
            os.remove(fffile)
Esempio n. 46
0
print bexoplanet

#exoplanet = string.join(exoplanet,',') #create the list string of exoplanet science images
#bexoplanet = string.join(bexoplanet,',')#create the list string of bexoplanet science images

print '\nSubtracting superbias.fits from all '+planet+'*.fits images ....\n'
for i in range(len(exoplanet)):
    iraf.imarith(exoplanet[i],'-','superbias.fits',bexoplanet[i])
    update_progress((i+1.)/len(bexoplanet))

print '\n.... cleaning '+planet+'*.fits images\n'
os.system('rm '+planet+'*.fits')

print '\n Statistics of B'+planet+'*.fits images: \n'
for i in range(len(bexoplanet)):
    iraf.imstat(bexoplanet[i])
#%%
print '\nFlatfielding the B'+planet+'*.fits ....\n'
#create the names for exoplanet science images with bias subtracted and flatfielding
abexoplanet = []
for i in bexoplanet:
    abexoplanet.append('A'+i)
    #verify if previous superbias exist
    if os.path.isfile('A'+i) == True:
        os.system('rm A'+i)

print '\n Will be create this images: \n'
print abexoplanet
#flatifielding images
for i in range(len(abexoplanet)):
    iraf.imarith(bexoplanet[i],'/','superflat.fits',abexoplanet[i])
Esempio n. 47
0
def run_imstat(input):
    iraf.images()
    for image in input:
        iraf.imstat(image)
Esempio n. 48
0
def fringe_sub(inlist, inpref='', outpref='s', mskpref='none', iternum=50, nsigma=3, bpm='none', second=False, fr_x0=0.0, fr_y0=0.0, fr_step=5, surfit=False, xsorder=4, ysorder=4):
    
    # load IRAF package
    iraf.proto()

    # check input image list 
    inimg_arr = check_input(inlist, inpref)
    if isinstance(inimg_arr, int):
        return 1

    # check output 
    outimg_arr = check_outpref(outpref, inimg_arr)
    if isinstance(outimg_arr, int):
        return 1

    # check mask images 
    if mskpref.lower() == 'none' or mskpref == '':
        mask = 0
    else:
        mask = 1
        mskimg_arr = check_inpref(mskpref, inimg_arr)
        if isinstance(mskimg_arr, int):
            return 1

    # check bad pixel mask
    if bpm.lower() != 'none':
        bpmex = 1
        if os.access(bpm, os.R_OK) == False:
            print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm
            return 1
    else:
        bpmex = 0

    # prefix for temoprary images
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # subtract sky
    for i in range(len(inimg_arr)):
        if mask == 1:
            iraf.unlearn('mimstat')
            if not second:
                ret = iraf.mimstat(inimg_arr[i], imasks=mskimg_arr[i]+'[pl]', format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
            else:
                if bpmex == 1:
                    tmp_mask = tmp_prefix + os.path.basename(mask_img_arr[i])
                    iraf.imarith(maskimg_arr[i], '+', bpm, tmp_mask, verbose='no') 
                    convert_maskfits_int(tmp_mask, tmp_mask)
                    ret = iraf.mimstat(inimg_arr[i], imasks=tmp_mask, format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
                else:
                    ret = iraf.mimstat(inimg_arr[i], imasks=mskimg_arr[i], format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
        else: 
            iraf.unlearn('imstat')
            ret = iraf.imstat(inimg_arr[i], format='no', fields='midpt', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
        
        if len(ret) == 1:
            objmed = float(ret[0])
        else : 
            print >> sys.stderr, 'failed to calculate median of the object background'
            return 1

        # record input fits data into memory
        im = pyfits.open(inimg_arr[i])
        im_hdr = im[0].header
        im_data = im[0].data
        out_data = im_data - objmed
        if surfit:
            out_data_org = np.copy(out_data)
        im.close()
            
        # fringe subtraction for NB imaging 
        if mask == 1:
            if bpmex == 1:
                tmp_mask = tmp_prefix + os.path.basename(mask_img_arr[i])
                mask_im = pyfits.open(tmp_mask)
            else:
                mask_im = pyfits.open(mskimg_arr[i])
            if not second:
                mask_data = mask_im[1].data
            else:
                mask_data = mask_im[0].data
            mask_im.close()
                
        x = np.ones(out_data.shape[0]*out_data.shape[1]).reshape(out_data.shape[0],out_data.shape[1])* np.arange(out_data.shape[1]) + 1
        y = (np.ones(out_data.shape[1]*out_data.shape[0]).reshape(out_data.shape[1],out_data.shape[0]) * np.arange(out_data.shape[0]) + 1).T
        r = np.sqrt((x-fr_x0)*(x-fr_x0) + (y-fr_y0)*(y-fr_y0))
        rmax = np.max(r)
        rmin = np.min(r)

        r1 = rmin
        while r1 <= rmax:
            r2 = r1 + fr_step
            idx = np.logical_and(r>=r1, r<r2)
            out_subset = out_data[idx]
            
            if mask == 1:
                mask_subset = mask_data[idx]
                idx_sky = np.where(mask_subset == 0)
                #if len(out_subset[idx_sky]) != len(out_subset):
                #    print "masked:",inimg_arr[i],r1,r2,fr_x0,fr_y0,len(out_subset[idx_sky]),"/",len(out_subset),meanclip(out_subset[idx_sky],median=1)[0],meanclip(out_subset, median=1)[0]
                med, sigma = meanclip(out_subset[idx_sky],median=1)
            else:
                med, sigma = meanclip(out_data[idx],median=1)

            out_data[idx] = out_data[idx] - med
            r1 += fr_step

        # save output fits file 
        hdu = pyfits.PrimaryHDU(out_data)
        imgout = pyfits.HDUList([hdu])
        imgout[0].header = im_hdr
        if surfit:
            tmp_fsub = tmp_prefix + "fsub" + os.path.basename(inimg_arr[i])
            tmp_fsub_fit = tmp_prefix + "fsub_fit" + os.path.basename(inimg_arr[i])
            imgout.writeto(tmp_fsub)
            imgout.close()
            
            iraf.imsurfit(tmp_fsub, tmp_fsub_fit, xsorder, ysorder)
            imfit = pyfits.open(tmp_fsub_fit)
            imfit_data = imfit[0].data
            out_data_fs = np.copy(out_data_org - imfit_data)

            r1 = rmin
            while r1 <= rmax:
                r2 = r1 + fr_step
                idx = np.logical_and(r>=r1, r<r2)
                out_subset = out_data_fs[idx]
            
                if mask == 1:
                    mask_subset = mask_data[idx]
                    idx_sky = np.where(mask_subset == 0)
                    med, sigma = meanclip(out_subset[idx_sky],median=1)
                else:
                    med, sigma = meanclip(out_data_fs[idx],median=1)

                out_data_org[idx] = out_data_org[idx] - med
                r1 += fr_step
            
            hdu = pyfits.PrimaryHDU(out_data_org)
            imgout = pyfits.HDUList([hdu])
            imgout[0].header = im_hdr
            imgout.writeto(outimg_arr[i])
            imgout.close()

            remove_temp_all(tmp_prefix)
            
        else:
            imgout.writeto(outimg_arr[i])
            imgout.close()

            if bpmex == 1:
                remove_temp_all(tmp_prefix)

    return 0
Esempio n. 49
0
def doflatsofi(flats, _doflat, illum, _output):
    # print "LOGX:: Entering `doflatsofi` method/function in %(__file__)s" %
    # globals()
    import ntt
    from ntt.util import display_image, delete, searchflat, name_duplicate
    from pyraf import iraf
    import glob
    import string
    onofflimit = {'J': 1000, 'H': 1000, 'Ks': 5000}
    masklimit = {'J': {'ON': 1000, 'OFF': 30}, 'H': {
        'ON': 1000, 'OFF': 40}, 'Ks': {'ON': 1000, 'OFF': 1000}}
    if flats and _doflat:
        listaflat = []
        for _filter in flats:
            for ID in flats[_filter]:
                images = flats[_filter][ID]
                if len(images) == 8:
                    mflat = makeflat(images)
                    listaflat.append(mflat)
                    display_image(mflat, 1, '', '', False)
                    raw_input('go on ')
                elif len(images) != 8:  # % 8 == 0:
                    print '\n###  to compute a flat field you need a sequence of 8 calibration files in the following orders:'
                    print 'OFF  OFFMASK  ONMASK  ON  ON   ONMASK   OFFMASK     OFF\n'
                    print len(images), _filter, ID
                    tipo = ['OFF', 'OFFMASK', 'ONMASK', 'ON',
                            'ON', 'ONMASK', 'OFFMASK', 'OFF']
                    listtmp = []
                    ii = 0
                    nn = 0
                    for img in images:
                        onoffvalue = float(string.split(iraf.imstat(
                            img + '[500:600,900:1000]', Stdout=1)[1])[2])
                        maskvalue = float(string.split(iraf.imstat(
                            img + '[100:200,900:1000]', Stdout=1)[1])[2])
                        if onoffvalue >= onofflimit[_filter]:
                            onoff = 'ON'
                        else:
                            onoff = 'OFF'
                        if maskvalue >= masklimit[_filter][onoff]:
                            mask = 'none'
                        else:
                            mask = 'MASK'
#                        display_image(img,1,'','',False)
                        print onoff, mask, onoffvalue, maskvalue, img, tipo[nn]
                    for img in images:
                        onoffvalue = float(string.split(iraf.imstat(
                            img + '[500:600,900:1000]', Stdout=1)[1])[2])
                        maskvalue = float(string.split(iraf.imstat(
                            img + '[100:200,900:1000]', Stdout=1)[1])[2])
                        if onoffvalue >= onofflimit[_filter]:
                            onoff = 'ON'
                        else:
                            onoff = 'OFF'
                        if maskvalue >= masklimit[_filter][onoff]:
                            mask = 'none'
                        else:
                            mask = 'MASK'
                        display_image(img, 1, '', '', False)
                        print onoff, mask, onoffvalue, maskvalue, img, tipo[nn]
                        answ = raw_input('ok  [[y]/n/r/s] ? ')
                        if not answ:
                            answ = 'y'
                        if answ == 'y':
                            listtmp.append(img)
                            ii = ii + 1
                            nn = nn + 1
                            if len(listtmp) == 8:
                                print '### number images selected: ', str(len(listtmp))
                                mflat = ntt.soficalibdef.makeflat(listtmp)
                                listaflat.append(mflat)
                                display_image(mflat, 1, '', '', False)
                                nn = 0
                                listtmp = []
                        elif answ == 'r':
                            listtmp = []
                            ii = 0
                            nn = 0
                        elif answ == 's':
                            break
                        else:
                            print len(images), _filter, ID
                        print '### number images selected: ', str(len(listtmp))
    else:
        listaflat = flats
    listaillum = []
    if illum:
        for _filter in illum:
            for ID in illum[_filter]:
                images = illum[_filter][ID]
                flatfield = searchflat(images[0], listaflat)[0]
                if flatfield:
                    illum_frame = ntt.soficalibdef.makeillumination(
                        images, flatfield)
                    listaillum.append(illum_frame)
                else:
                    print 'flat field not found'

        for img in listaflat:
            try:
                ntt.util.phase3header(img)  # phase 3 definitions
                ntt.util.updateheader(
                    img, 0, {'BUNIT': ['ADU', 'pixel units(ADU,electrons)']})
                ntt.util.updateheader(
                    img, 0, {'FILETYPE': [31202, 'flat field']})
            except:
                print '\n### problems with phase 3 definitions'

        for img in listaillum:
            try:
                ntt.util.phase3header(img)  # phase 3 definitions
                ntt.util.updateheader(
                    img, 0, {'BUNIT': ['ADU', 'pixel units(ADU,electrons)']})
                ntt.util.updateheader(
                    img, 0, {'FILETYPE': [31213, 'illum corr  frames']})
            except:
                print '\n### problems with phase 3 definitions'
    return listaflat, listaillum
Esempio n. 50
0
File: rcred.py Progetto: scizen9/kpy
def create_masterbias(biasdir=None, channel='rc'):
    '''
    Combines slow and fast readout mode biases for the specified channel.
    '''
    
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    
    if (biasdir is None) or biasdir=="": biasdir = "."
        
    outs = "Bias_%s_slow.fits"%channel
    outf = "Bias_%s_fast.fits"%channel

    doslow = True
    dofast = True
    if (os.path.isfile(os.path.join(biasdir,outs))): 
        logger.warn( "%s master Bias exists!"%outs)
        doslow = False
    if ( os.path.isfile(os.path.join(biasdir,outf))):
        logger.warn("%s master Bias exists!"%outs)
        dofast = False
    if(doslow or dofast):
        logger.info("Starting the Master Bias creation!")
    else:
        return

    os.chdir(biasdir)        
        
    lfastbias = []
    lslowbias = []
    
    #Select all filts that are Bias with same instrument
    for f in glob.glob("rc*fits"):
        try:
            if ( "BIAS" in str.upper(fitsutils.get_par(f, "IMGTYPE")) ):
                if (fitsutils.get_par(f, "ADCSPEED")==2):
                    lfastbias.append(f)
                else:
                    lslowbias.append(f)
        except:
            pass
                
    logger.info("Files for bias SLOW mode: %s"% lslowbias)
    logger.info( "Files for bias FAST mode: %s"% lfastbias)
    
    if len(lfastbias) > 0 and dofast:
        bfile_fast ="lbias_fast_"+channel
        np.savetxt(bfile_fast, np.array(lfastbias), fmt="%s")
        if (os.path.isfile("Bias_stats_fast")): os.remove("Bias_stats_fast")
        iraf.imstat("@"+bfile_fast, Stdout="Bias_stats_fast")
        
        st = np.genfromtxt("Bias_stats_fast", names=True, dtype=None)
        logger.info("%s"%st)
        
        iraf.imcombine(input = "@"+bfile_fast, \
                    output = outf, \
                    combine = "median",\
                    scale = "mode")
        os.remove(bfile_fast)
        
        #copy into the reference folder with current date
        newdir = os.path.join("../../refphot/", os.path.basename(os.path.abspath(biasdir)))
        if (not os.path.isdir(newdir)):
            os.makedirs(newdir)
        shutil.copy(outf, os.path.join(newdir, os.path.basename(outf)) )
    else:
        copy_ref_calib(biasdir, outf)


    if len(lslowbias) > 0 and doslow:

        bfile_slow ="lbias_slow_"+channel
        np.savetxt(bfile_slow, np.array(lslowbias), fmt="%s")
        if (os.path.isfile("Bias_stats_slow")): os.remove("Bias_stats_slow")
        iraf.imstat("@"+bfile_slow, Stdout="Bias_stats_slow")
        
        st = np.genfromtxt("Bias_stats_slow", names=True, dtype=None)
        logger.info("%s"%st)
        
        iraf.imcombine(input = "@"+bfile_slow, \
                    output = outs, \
                    combine = "median",\
                    scale = "mode")
        os.remove(bfile_slow)
        
        #copy into the reference folder with current date
        newdir = os.path.join("../../refphot/", os.path.basename(os.path.abspath(biasdir)))
        if (not os.path.isdir(newdir)):
            os.makedirs(newdir)
        shutil.copy(outs, os.path.join(newdir, os.path.basename(outs)) )  
    else:
        copy_ref_calib(biasdir, outs)
Esempio n. 51
0
def irafflatten():
    os.system('cp /Users/rfinn/clusters/spitzer/flatsexfiles/* .')
    infile=open('InputImageList.txt','r')
    outfile=open('FlatImageList.txt','w')
    sky=[]
    for line in infile:
	im=line[0:(len(line)-1)]
	mask='mask'+im
	skyim='s'+im
	outline='f'+line
	iraf.imgets(im,'DRIBKGND')
	t=iraf.imgets.value
	sky.append(float(t))
	outfile.write(outline)
    #get object positions using sextractor
	iraf.imarith(im,'-',t,skyim)#subtract sky before running sextractor (otherwise it doesn't detect any objects - don't know why...)
	s='sex '+skyim
	os.system(s)
	x=[]
	y=[]
	catfile=open('test.cat','r')
	for line in catfile:
	    if line.find('#') > -1:
		continue
	    t=line.split()
	    x.append(float(t[10]))
	    y.append(float(t[11]))
	catfile.close()
	x=N.array(x,'f')
	y=N.array(y,'f')
        
	try:#create image of ones same size as image
	    iraf.imarith(im,'/',im,'mask')
	except:
	    iraf.imdelete('mask')
	    iraf.imarith(im,'/',im,'mask')
	print "masking objects"
	for j in range(len(x)): #mask objects and radius around each position using imreplace, radius=10 pix
	    for k in range(11):
		y1=int(y[j]+5-k)
		if y1 < 1:
		    continue
		if y1 > 128:
		    continue
		xmin=int(x[j]-5)
		if xmin < 1:
		    xmin=1
		xmax=int(x[j]+5)
		if xmax > 128:
		    xmax=128

		s='mask['+str(xmin)+':'+str(xmax)+","+str(y1)+":"+str(y1)+"]"
		iraf.imreplace(s,0.)
	iraf.imrename('mask',mask)
	print "updating BPM field in header"
	iraf.hedit(im,fields='BPM',value=mask,add='yes',verify='no',update='yes')
    outfile.close()
    infile.close()
    avesky=N.average(N.array(sky,'f'))
    lthresh=avesky-1.
    hthresh=avesky+.6
    iraf.imcombine('@InputImageList.txt','flat',combine='average',reject='ccdclip',scale='none',zero='mode',lthreshold=lthresh,hthreshold=hthresh,lsigma=2.,hsigma=2.,rdnoise=5.,gain=5.,blank=1.,grow=12.,masktype='badvalue',maskvalue='0')
    t=iraf.imstat('flat',fields='mean',format='no',Stdout=1)
    ave=float(t[0])
    iraf.imarith('flat','/',ave,'nflat')
    iraf.imarith('@InputImageList.txt','/','nflat','@FlatImageList.txt')
Esempio n. 52
0
def masterflat(input_file):
    """
    Obtain the masterflat image for calibration.
    ___
    INPUT:
    For obtain this parameters, use the input_info function.

    data_path: string, path where are the images data.
    save_path: string, path where will save all reduced images.
    input_file: dict, with information describe in the YAML file.

    OUTPUT:
    It is possible that the function return some of these values:

    0. Create the masterflat image on the save_path.
    1. It do not create the masterflat image, because of some erros.
    """
    #set original directory
    original_path = os.getcwd()
    data_path = input_file['data_path']
    save_path = input_file['save_path']
    #Change your directory to data diretory
    os.chdir(data_path)
    #list all flat images
    flat = glob.glob('flat*.fits')
    print 'Loading flat images \nTotal of flat files = ',len(flat),'\nFiles = \n'
    print flat
    #if save_path exist, continue; if not, create.
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    #create a list of bias images and copy images to save_path
    os.system('cp flat*.fits '+save_path)
    #creating the names of flat with bias subctracted
    bflat = []
    for i in flat:
        bflat.append('B'+i)
    print '\n Names os flat images with bias subtracted: \n \n',bflat
    #change for save_path directory
    os.chdir(save_path)
    #verify if previous superbias exist
    if os.path.isfile('superflat.fits') == True:
        os.system('rm superflat.fits')
    #verify if exits previous bflat*.fits files and remove then.
    for i in bflat:
        if os.path.isfile(i) == True:
            os.system('rm -f '+i)
    print '\nCreating superflat .... \n'
    #create the list of flat images  and bflat images
    #flat = string.join(flat,',')
    #bflat = string.join(bflat,',')
    print '\n Subtracting bias from flat images and creating bflat images.... \n'
    #iraf.imarith()
    for i in range(len(flat)):
        iraf.imarith(flat[i],'-','superbias.fits',bflat[i])
        #print statistics from bflat*.fits images
        iraf.imstat(bflat[i])
    print '\n .... done \n'
    #clean previos flat*.fits files
    print '\n Clean flat*.fits images .... \n'
    os.system('rm flat*.fits')
    print '\n .... done. \n'
    #normalizing each flat
    print '\nNormalizing each flat ....\n'
    #checking if mean from numpy is the same from your bflat images using imstat
    #take the mean of each bflat image
    bflat_mean = np.zeros(len(bflat))
    for i in range(len(bflat)):
        image = fits.getdata(bflat[i])
        image = np.array(image,dtype='Float64')
        bflat_mean[i] = round(np.mean(image))
    image = 0 #clean image allocate to this variable
    print 'The mean of each bflat image, respectivaly ...'
    print bflat_mean
    #creating the names of bflat images after the normalization:
    abflat = []
    for i in bflat:
        abflat.append('A'+i)
    print '\n Names os bflat images with bias subtracted and normalizad: \n \n',abflat
    #verify if exist previous ABflat*.fits images and remove then.
    for i in abflat:
        if os.path.isfile(i) == True:
            os.system('rm -f '+i)
    for i in range(len(abflat)):
        iraf.imarith(bflat[i],'/',bflat_mean[i],abflat[i])
    print '\n.... done!\n'
    print '\n Cleaning bflat*.fits images ....\n'
    os.system('rm Bflat*.fits')
    print '\n.... done.\n'
    print 'Statistics of the abflat*.fits images .... \n'
    for i in range(len(abflat)):
        iraf.imstat(abflat[i])
    print '\n Combining abflat images ....\n'
    ablist = string.join(abflat,',')
    iraf.imcombine(ablist,'superflat.fits')
    iraf.imstat('superflat.fits')
    print '\n .... done. \n'
    print '\nCleaning ABflat*.fits images ....\n'
    os.system('rm ABflat*.fits')
    print '\n.... done!'
    #Verify if the image was created:
    output = glob.glob('superflat*.fits')
    if len(output) != 0:
        output = 0
    else:
        output = 1
    #Return to original directory
    os.chdir(original_path)
    #last mensage
    print '\n MASTERFLAT.FITS created! \n'
    print '\n END of Data Reduction for create a masterflat.fits file. \n'
    #obtain the value of return
    if output == 1:
        print '!!! ERROR/WARNING !!!'
        print 'Check if the superbias was created or if there is more than one superbias image.'
    return output
Esempio n. 53
0
def lacos_im(_input, _output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, skyval=0, niter=2, verbose=True, interactive=False):
    # print "LOGX:: Entering `lacos_im` method/function in %(__file__)s" %
    # globals()
    import ntt
    from ntt.util import delete
    import sys
    import re
    import os
    import string
    from pyraf import iraf
    import numpy as np
    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # make temporary files
    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits'
    inputmask, gfirstsel = 'inputmask.fits', 'gfirstsel.fits'
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    # initialize loop
    usegain = gain
    i = 1
    stop = 'no'
    previous = 0
    if not _output:
        _output = _input

    arrayinput, headerinput = ntt.cosmics.fromfits(_input, verbose=False)
    ntt.cosmics.tofits(outmask, np.float32(
        arrayinput - arrayinput), headerinput, verbose=False)

    delete(oldoutput)
    if skyval > 0:
        arrayoldoutput = arrayinput + skyval
    else:
        arrayoldoutput = arrayinput
    ntt.cosmics.tofits(oldoutput, np.float32(
        arrayoldoutput), headerinput, verbose=False)
    # start iterations
    while stop == 'no':
        # take second-order derivative (Laplacian) of input image
        # kernel is convolved with subsampled image, in order to remove negative
        # pattern around high pixels
        delete(blk)
        delete(lapla)
        delete(deriv2)
        iraf.blkrep(oldoutput, blk, 2, 2)
        iraf.convolve(blk, lapla, '_kernel')
        iraf.imreplace(lapla, 0, upper=0, lower='INDEF', radius=0)
        iraf.blkavg(lapla, deriv2, 2, 2, option="average")
        delete(med5)
        # create model of background flux - 5x5 box should exclude all CRs
        iraf.median(oldoutput, med5, 5, 5, zlo='INDEF',
                    zhi='INDEF', verbose='no')
        iraf.imreplace(med5, 0.0001, upper=0, lower='INDEF', radius=0)
        # create noise model
        delete(noise)
        iraf.imutil.imexpr(expr='sqrt(a*' + str(usegain) + '+' + str(readn) +
                           '**2)/' + str(usegain), a=med5, output=noise, verbose='no')
        # divide Laplacian by noise model
        delete(sigmap)
        iraf.imarith(deriv2, "/", noise, sigmap)
        # Laplacian of blkreplicated image counts edges twice:
        iraf.imarith(sigmap, "/", 2., sigmap)
        # removal of large structure (bright, extended objects)
        delete(med5)
        iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
        arraysigmap, headersigmap = ntt.cosmics.fromfits(sigmap, verbose=False)
        arraymed5, headermed5 = ntt.cosmics.fromfits(med5, verbose=False)
        arraysigmap = arraysigmap - arraymed5
        iraf.imarith(sigmap, "-", med5, sigmap)
        # find all candidate cosmic rays
        # this selection includes sharp features such as stars and HII regions

        delete(firstsel)
        iraf.imcopy(sigmap, firstsel, verbose='no')
        iraf.imreplace(firstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(firstsel, 1, lower=0.1, upper='INDEF', radius=0)
#		arraygfirst=arraysigmap
#		arraygfirst = np.where(arraygfirst<sigclip,0,arraygfirst)
#		arraygfirst = np.where(arraygfirst>0.1,1,arraygfirst)

        # compare candidate CRs to median filtered image
        # this step rejects bright, compact sources from the initial CR list
        # subtract background and smooth component of objects
        delete(med3)
        delete(med7)
        iraf.median(oldoutput, med3, 3, 3, zlo='INDEF',
                    zhi='INDEF', verbose='no')
        iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
        iraf.imarith(med3, "-", med7, med3)
        iraf.imarith(med3, "/", noise, med3)
        iraf.imreplace(med3, 0.01, upper=0.01, lower='INDEF', radius=0)
        # compare CR flux to object flux
        delete(starreject)
        iraf.imutil.imexpr(expr="(a*b)/c", a=firstsel, b=sigmap,
                           c=med3, output=starreject, verbose='no')
        # discard if CR flux <= objlim * object flux
        iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF', radius=0)
        iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(firstsel, "*", starreject, firstsel)
        # grow CRs by one pixel and check in original sigma map
        delete(gfirstsel)
        iraf.convolve(firstsel, gfirstsel, '_gkernel')
        iraf.imreplace(gfirstsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(gfirstsel, "*", sigmap, gfirstsel)
        iraf.imreplace(gfirstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(gfirstsel, 1, lower=0.1, upper='INDEF', radius=0)
        # grow CRs by one pixel and lower detection limit
        sigcliplow = sigfrac * sigclip
        delete(finalsel)
        iraf.convolve(gfirstsel, finalsel, '_gkernel')
        iraf.imreplace(finalsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(finalsel, "*", sigmap, finalsel)
        iraf.imreplace(finalsel, 0, upper=sigcliplow, lower='INDEF', radius=0)
        iraf.imreplace(finalsel, 1, lower=0.1, upper='INDEF', radius=0)
        # determine number of CRs found in this iteration
        delete(gfirstsel)
        iraf.imutil.imexpr(expr="(1-b)*a", a=outmask,
                           b=finalsel, output=gfirstsel, verbose='no')

        npix = iraf.imstat(gfirstsel, fields="npix",
                           lower=0.5, upper='INDEF', Stdout=1)
        # create cleaned output image; use 3x3 median with CRs excluded
        delete(med5)
        iraf.imarith(outmask, "+", finalsel, outmask)
        iraf.imreplace(outmask, 1, lower=1, upper='INDEF', radius=0)

        delete(inputmask)
        iraf.imutil.imexpr(expr="(1-10000*a)", a=outmask,
                           output=inputmask, verbose='no')
        iraf.imarith(oldoutput, "*", inputmask, inputmask)
        delete(med5)
        iraf.median(inputmask, med5, 5, 5, zloreject=-
                    9999, zhi='INDEF', verbose='no')
        iraf.imarith(outmask, "*", med5, med5)
        if i > 1:
            delete(_output)

        delete(_output)
        iraf.imutil.imexpr(expr="(1.-b)*a+c", a=oldoutput,
                           b=outmask, c=med5, output=_output, verbose='no')

        # cleanup and get ready for next iteration
        delete(oldoutput)
        iraf.imcopy(_output, oldoutput, verbose='no')

        if npix == 0:
            stop = 'yes'
        i = i + 1
        if i > niter:
            stop = 'yes'
        # delete temp files
        delete(blk + "," + lapla + "," + deriv2 + "," + med5)
        delete(med3 + "," + med7 + "," + noise + "," + sigmap)
        delete(firstsel + "," + starreject + "," + gfirstsel)
        delete(finalsel + "," + inputmask)

    if skyval > 0:
        iraf.imarith(_output, "-", skyval, _output)
    delete('_kernel' + "," + '_gkernel')
    delete(oldoutput)
Esempio n. 54
0
File: rcred.py Progetto: nblago/kpy
def create_masterbias(biasdir=None, channel='rc'):
    '''
    Combines slow and fast readout mode biases for the specified channel.
    '''
    
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    
    if (biasdir == None) or biasdir=="": biasdir = "."
        
    outs = "Bias_%s_slow.fits"%channel
    outf = "Bias_%s_fast.fits"%channel

    if (os.path.isfile(os.path.join(biasdir,outs)) and os.path.isfile(os.path.join(biasdir,outf))):
        print "Master Bias exists!"
        return
    else:
        print "Starting the Master Bias creation!"

    os.chdir(biasdir)        
        
    lfastbias = []
    lslowbias = []
    
    #Select all filts that are Bias with same instrument
    for f in glob.glob("*fits"):
        try:
            if (channel == fitsutils.get_par(f, "CHANNEL") and "BIAS" in str.upper(fitsutils.get_par(f, "OBJECT")) ):
                if (fitsutils.get_par(f, "ADCSPEED")==2):
                    lfastbias.append(f)
                else:
                    lslowbias.append(f)
        except:
            pass
                
    print "Files for bias SLOW mode: ", lslowbias
    print "Files for bias FAST mode: ", lfastbias
    
    if len(lfastbias) > 0:
        bfile_fast ="lbias_fast_"+channel
        np.savetxt(bfile_fast, np.array(lfastbias), fmt="%s")
        if (os.path.isfile("Bias_stats_fast")): os.remove("Bias_stats_fast")
        iraf.imstat("@"+bfile_fast, Stdout="Bias_stats_fast")
        
        st = np.genfromtxt("Bias_stats_fast", names=True, dtype=None)
        print st
        
        iraf.imcombine(input = "@"+bfile_fast, \
                    output = outf, \
                    combine = "median",\
                    scale = "mode")
        os.remove(bfile_fast)

    if len(lslowbias) > 0:

        bfile_slow ="lbias_slow_"+channel
        np.savetxt(bfile_slow, np.array(lslowbias), fmt="%s")
        if (os.path.isfile("Bias_stats_slow")): os.remove("Bias_stats_slow")
        iraf.imstat("@"+bfile_slow, Stdout="Bias_stats_slow")
        
        st = np.genfromtxt("Bias_stats_slow", names=True, dtype=None)
        print st
        
        iraf.imcombine(input = "@"+bfile_slow, \
                    output = outs, \
                    combine = "median",\
                    scale = "mode")
        os.remove(bfile_slow)
Esempio n. 55
0
def science_reduction(input_file):
    """
    Calibrate science images with masterflat (or superflat) and masterbias (or superbias) images.
    ___
    INPUT:
    For obtain this parameters, use the input_info function.

    data_path: string, path where are the images data.
    save_path: string, path where will save all reduced images.
    input_file: dict, with information describe in the YAML file.

    OUTPUT:
    It is possible that the function return some of these values:

    0. Create the masterflat image on the save_path.
    1. It do not create the masterflat image, because of some erros.
    """
    #name of the planet
    planet = input_file['exoplanet']
    #set original directory
    original_path = os.getcwd()
    save_path = input_file['save_path']
    data_path = input_file['data_path']
    #Change your directory to data diretory
    os.chdir(data_path)
    #list all flat images
    exoplanet = glob.glob(planet+'*.fits')
    print '\nLoading exoplanet images \nTotal of '+planet+'*.fits  files = ',len(exoplanet),'\nFiles = \n'
    print exoplanet
    #if save_path exist, continue; if not, create.
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    #create a list of bias images and copy images to save_path
    print '\nCopy science images to save_path directory to main reduction: ....'
    os.system('cp '+planet+'*.fits '+save_path)
    print '\n .... done. \n'
    #change to save_path
    os.chdir(save_path)
    #create the names for exoplanet science mages with bias subtracted
    bexoplanet = []
    for i in exoplanet:
        bexoplanet.append('B'+i)
        #verify if previous superbias exist
        if os.path.isfile('B'+i) == True:
            os.system('rm B'+i)
    print '\n Will be create this images: \n'
    print bexoplanet
    #exoplanet = string.join(exoplanet,',') #create the list string of exoplanet science images
    #bexoplanet = string.join(bexoplanet,',')#create the list string of bexoplanet science images
    print '\nSubtracting superbias.fits from all '+planet+'*.fits images ....\n'
    for i in range(len(exoplanet)):
        iraf.imarith(exoplanet[i],'-','superbias.fits',bexoplanet[i])
        use.update_progress((i+1.)/len(bexoplanet))
    print '\n.... cleaning '+planet+'*.fits images\n'
    os.system('rm '+planet+'*.fits')
    print '\n Statistics of B'+planet+'*.fits images: \n'
    for i in range(len(bexoplanet)):
        iraf.imstat(bexoplanet[i])
    print '\nFlatfielding the B'+planet+'*.fits ....\n'
    #create the names for exoplanet science images with bias subtracted and flatfielding
    abexoplanet = []
    for i in bexoplanet:
        abexoplanet.append('A'+i)
        #verify if previous superbias exist
        if os.path.isfile('A'+i) == True:
            os.system('rm A'+i)
    print '\n Will be create this images: \n'
    print abexoplanet
    #flatifielding images
    for i in range(len(abexoplanet)):
        iraf.imarith(bexoplanet[i],'/','superflat.fits',abexoplanet[i])
        use.update_progress((i+1.)/len(abexoplanet))
    print '\n.... cleaning B'+planet+'*.fits images\n'
    os.system('rm B'+planet+'*.fits')
    print '\n Statistics of AB'+planet+'*.fits images: \n'
    for i in range(len(abexoplanet)):
        iraf.imstat(abexoplanet[i])
    os.chdir(original_path) #change to save_path
    return
Esempio n. 56
0
#! /usr/bin/env python
from pyraf import iraf
iraf.images()
iraf.imstat("dev$pix")
Esempio n. 57
0
def masterbias(input_file):
    """
    Obtain the masterbias.fits image.
    ___
    Input:
    For obtain this parameters, use the input_info function.

    data_path: string, path where are the images data.
    save_path: string, path where will save all reduced images.
    input_file: dict, with information describe in the YAML file.

    Output:
    It is possible that the function return some of these values:

    0. Create the masterbias image on the save_path.
    1. It do not create the masterbias image, because of some error
    ___
    """
    #Set original directory
    original_path = os.getcwd()
    save_path = input_file['save_path']
    data_path = input_file['data_path']
    #Change your directory to data diretory
    os.chdir(data_path)
    #list all bias images
    bias = glob.glob('bias*.fits')
    print 'Loading bias images \nTotal of bias files = ',len(bias),'\nFiles = \n'
    print bias
    print '\nCreating superbias \n'
    #if save_path exist, continue; if not, create.
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    #copy bias images to save_path
    os.system('cp bias*.fits '+save_path)
    #change to sabe_path
    os.chdir(save_path)
    #verify if previous superbias exist
    if os.path.isfile('superbias.fits') == True:
        os.system('rm superbias.fits')
    #create the list of bias images
    bias_list = string.join(bias,',')
    #combine the bias image and create the superbias
    iraf.imcombine(bias_list,'superbias.fits')
    iraf.imstat('superbias.fits')
    #clean previos bias files
    print '\n Cleaning bias*.fits images ....\n'
    os.system('rm bias*.fits')
    print '\n.... done.'
    #print output
    #test of outpu value
    #os.remove('superbias.fits')
    #Verify if the image was created:
    output = glob.glob('superbias*.fits')
    if len(output) != 0:
        output = 0
    else:
        output = 1
    #Return to original directory
    os.chdir(original_path)
    #END of the masterbias reduction messsage
    print '\nsuperbias.fits created!\n'
    print '\nEND of superbias reduction!\n'
    #obtain the value of return
    if output == 1:
        print '!!! ERROR/WARNING !!!'
        print 'Check if the superbias was created or if there is more than one superbias image.'
    return output