Пример #1
0
def make_master(obj, file_type=None):
    """
    This routine takes the name of a file containing the raw calibration images in the **current** directory that are to
    be combined to form a master calibration image. The type of calibration images (i.e. bias, dark or flat) is specified
    by the file_type keyword; this is necessary because the type of calibration image determines how the multiple raw
    calibration images are to be combined (eg. pixel averages vs medians).
    """
    # Get the name of the master calibration file:
    if file_type=='bias':
        list_file = obj.bias_list
    elif file_type=='dark':
        list_file = obj.dark_list
    elif file_type=='flat':
        list_file = obj.flat_list
    dirpath = os.path.dirname(list_file)+'/'
    filename = 'master_'+file_type+'.fits'
    master_filename = dirpath+filename
    if os.path.exists(master_filename): os.remove(master_filename)
    # Now make the master calibration file:
    if file_type=='bias':
        iraf.zerocombine(input='@'+list_file, output=master_filename, combine='average', ccdtype='', process='no', delete='no')
        obj.master_bias = master_filename
    elif file_type=='dark':
        iraf.darkcombine(input='@'+list_file, output=master_filename, combine='average', ccdtype='', process='no', delete='no')
        obj.master_dark = master_filename
    elif file_type=='flat':
        iraf.flatcombine(input='@'+list_file, output=master_filename, combine='median', ccdtype='', process='no', delete='no')
        obj.master_flat = master_filename
    return None
Пример #2
0
def mkbiasINTWFC(filelist, type='median'):
    """
    Creates a master bias from given list of bias frames.
    Saves each extension to a different FITS file.

    Reads readnoise and gain from the FITS header, as each WFC chip has
    different values.

    :note: This function has been written specifically for INT WFC.

    :param filelist:
    :type filelist:
    :param type: combining parameter (default = median)
    :type type: string

    :return: None
    """
    input1 = ''
    for line in filelist:
        input1 += line[0] + '[1],'
    input2 = input1.replace('[1]', '[2]')
    input3 = input1.replace('[1]', '[3]')
    input4 = input1.replace('[1]', '[4]')
    inputs = [input1, input2, input3, input4]

    #note there are four SCI extensions
    for ext, input in enumerate(inputs):
        iraf.unlearn('zerocombine')
        iraf.zerocombine(input=input[:-1],
                         output='BIAS%i.fits' % (ext+1),
                         combine=type,
                         rdnoise='READNOIS',
                         gain='GAIN')
Пример #3
0
def mkbiasINTWFC(filelist, type='median'):
    """
    Creates a master bias from given list of bias frames.
    Saves each extension to a different FITS file.

    Reads readnoise and gain from the FITS header, as each WFC chip has
    different values.

    :note: This function has been written specifically for INT WFC.

    :param filelist:
    :type filelist:
    :param type: combining parameter (default = median)
    :type type: string

    :return: None
    """
    input1 = ''
    for line in filelist:
        input1 += line[0] + '[1],'
    input2 = input1.replace('[1]', '[2]')
    input3 = input1.replace('[1]', '[3]')
    input4 = input1.replace('[1]', '[4]')
    inputs = [input1, input2, input3, input4]

    #note there are four SCI extensions
    for ext, input in enumerate(inputs):
        iraf.unlearn('zerocombine')
        iraf.zerocombine(input=input[:-1],
                         output='BIAS%i.fits' % (ext + 1),
                         combine=type,
                         rdnoise='READNOIS',
                         gain='GAIN')
Пример #4
0
def combinebias(lstfn):
    """
    call iraf command zerocombine, combine bias fits.
    lstfn : lst file name
    type : string
    output file : Zero.fits
    """
    iraf.noao()
    iraf.imred()
    iraf.ccdred()
    iraf.zerocombine(input='o//@' + lstfn,
                     output='Zero',
                     combine='average',
                     reject='minmax',
                     ccdtype='',
                     process=False,
                     delete=False,
                     clobber=False,
                     scale='none',
                     statsec='',
                     nlow=0,
                     nhigh=1,
                     nkeep=1,
                     mclip=True,
                     lsigma=3.0,
                     hsigma=3.0,
                     rdnoise='rdnoise',
                     gain='gain',
                     snoise=0.0,
                     pclip=-0.5,
                     blank=0.0)
def create_masterbias(WORK_DIR):
  print "\n + Creating masterbias\n"

  try: os.remove('masterbias.fits')
  except: pass

  iraf.zerocombine(input=','.join([observations[WORK_DIR]['ORIG_DIR']+i for i in observations[WORK_DIR]['biases']]), output='masterbias.fits', combine='median', reject='none', ccdtype='', process='no', Stdout="/dev/null")
Пример #6
0
def combinebias(filename):
    iraf.noao()
    iraf.imred()
    iraf.ccdred()
    iraf.zerocombine(input = 'o//@' + filename
	, output = 'Zero', combine = 'average', reject = 'minmax'
	, ccdtype = '', process = False, delete = False
	, clobber = False, scale = 'none', statsec = ''
	, nlow = 0, nhigh = 1, nkeep = 1, mclip = True
	, lsigma = 3.0, hsigma = 3.0, rdnoise = 'rdnoise'
	, gain = 'gain', snoise = 0.0, pclip = -0.5, blank = 0.0)
Пример #7
0
def combine_bias(filename):
	if os.path.isfile('bias_spec.fits'):
		print "bias_spec.fits already exist, skip zerocombine..."
		return
	iraf.zerocombine(input = 'o//@' + filename
		, output = 'bias_spec.fits', combine = 'average', reject = 'minmax'
		, ccdtype = '', process = False, delete = False
		, clobber = False, scale = 'none', statsec = ''
		, nlow = 0, nhigh = 1, nkeep = 1, mclip = True
		, lsigma = 3.0, hsigma = 3.0, rdnoise = 9.4
		, gain = 0.35, snoise = 0.0, pclip = -0.5, blank = 0.0)
Пример #8
0
 def create_bias(self):
     """Creating Combined Bias Frame"""
     self.log.debug("Running iraf.zerocombine on image list...")
     iraf.unlearn(iraf.zerocombine)
     iraf.zerocombine(self.bias.iinat(),
         output=self.bias.iout("Bias"),
         combine=self.config["Bias.Combine"], 
         ccdtype="zero", 
         reject=self.config["Bias.Reject"], 
         scale="none", nlow=0, nhigh=1, nkeep=1, mclip="yes", lsigma=3.0, hsigma=3.0, rdnoise="0.", gain ="1."
         )
     self.bias.idone()
Пример #9
0
    def _makeBiasPyraf(self, combine='median', biaslist=[], output='zeroim.fits'):
        '''
        Creates a master bias from given list of bias frames

        :param: combine, either median or mean
        :param: biaslist, a list of bias frames to be combined
        :param: output: the name of the output file
        '''
        from pyraf import iraf
        from pyraf.iraf import imred, ccdred

        iraf.zerocombine(input=biaslist, output=output,
                         combine=combine)#, rdnoise=noise, gain=gain)
Пример #10
0
def main(raw_dir):

    if not os.path.exists("{}/step1.tmp".format(os.getcwd())):
        print "Setting up directory"
        list_dict = setup_dir(raw_dir)

        print "OT'ing"
        iraf.ccdproc(
            "@all_raw.lst",
            output="@all_ot.lst",
            overscan=True,
            trim=True,
            zerocor=False,
            darkcor=False,
            flatcor=False,
            illumco=False,
            biassec="image",
            trimsec="image",
            zero="",
            interact=True,
            order=100,
        )

        cull_bias(list_dict["zeros"], "zeros.lst")
        os.system("touch {}/step1.tmp".format(os.getcwd()))
        return

    print "Making master bias"
    iraf.zerocombine("@zeros.lst", output="Zero.fits", combine="average", reject="crreject")
    print "Subtracting bias"
    iraf.ccdproc("@all_otz.lst", overscan=False, trim=False, zerocor=True, zero="Zero.fits")

    detect_flats(os.getcwd())
    flats_to_make = glob("dflat*.lst")
    for flat in flats_to_make:
        name = "dFlat_{}.fits".format(flat.split(".lst")[0].split("_")[1])
        print "Making flat {}".format(name)
        iraf.flatcombine("@{}".format(flat), output=name, combine="average", reject="crreject")

    try:
        print "Making master dark"
        iraf.darkcombine("@darks.lst", output="Dark.fits", combine="average", reject="crreject")
    except Exception as e:
        print "\t{}".format(e)
        print "\tNO DARK FOUND"

    print "Making master comp"
    iraf.imcombine("@comps.lst", "Comp.fits", combine="average", reject="crreject")

    return
Пример #11
0
def makeZero(fileList='zeros.lst',
             inputType=INPUT_LISTFNAME,
             output="Zero.fits",
             combine='average',
             reject='minmax',
             ccdtype='zero'):
    '''create a master Zero.fits from the frames in fileList'''
    iraf.zerocombine(_genIRAFString(fileList, inputType),
                     output=output,
                     combine=combine,
                     reject=reject,
                     ccdtype=ccdtype)
    print "zerocombine successful"
    return
Пример #12
0
    def _makeBiasPyraf(self,
                       combine='median',
                       biaslist=[],
                       output='zeroim.fits'):
        '''
        Creates a master bias from given list of bias frames

        :param: combine, either median or mean
        :param: biaslist, a list of bias frames to be combined
        :param: output: the name of the output file
        '''
        from pyraf import iraf
        from pyraf.iraf import imred, ccdred

        iraf.zerocombine(input=biaslist, output=output,
                         combine=combine)  #, rdnoise=noise, gain=gain)
Пример #13
0
def BIAS(bias):
    #iraf.unlearn(iraf.zerocombine)
    path_bias = os.path.dirname(bias)
    bias = bias.replace(path_bias + '/','')

    iraf.chdir(path_bias)
    os.chdir(path_bias)
    iraf.zerocombine.input = '@' + bias
    if os.path.isfile('MasterBias.fits'):
       subprocess.call(['rm','MasterBias.fits'])
    iraf.zerocombine.output = 'MasterBias.fits'
    iraf.zerocombine.rdnoise = 'HIERARCH ESO DET OUT1 RON'
    iraf.zerocombine.gain = 'HIERARCH ESO DET OUT1 GAIN'
    iraf.zerocombine.ccdtype = ''
    #iraf.lpar(iraf.zerocombine)
    iraf.zerocombine(mode='h')
def create_masterflat(WORK_DIR):
  print '\n + Creating masterflat\n'

  try: os.remove('masterflat.fits')
  except: pass

  # subtract masterbias from flats
  for flat in observations[WORK_DIR]['flats']:
    try: os.remove(flat+'.fits')
    except: pass
    iraf.imarith(operand1=observations[WORK_DIR]['ORIG_DIR']+flat, op='-', operand2='masterbias.fits', result=flat, Stdout="/dev/null")
  
  iraf.zerocombine(input=','.join([flat for flat in observations[WORK_DIR]['flats']]), output='masterflat.fits', combine='median', reject='none', ccdtype='', process='no')

  for flat in observations[WORK_DIR]['flats']:
    os.remove(flat+'.fits')
Пример #15
0
def makebiasPyraf(filelist, output, noise, gain):
    """
    Creates a master bias from given list of bias frames

    @param filelist: list of names of bias frames
    @type: list
    @param
    """
    from pyraf import iraf
    from pyraf.iraf import imred, ccdred

    iraf.zerocombine(input=filelist,
                     output=output,
                     combine="median",
                     rdnoise=noise,
                     gain=gain)
Пример #16
0
    def the_zerocombine(self,
                        in_file_list,
                        out_file,
                        method="median",
                        rejection="minmax",
                        ccdtype="",
                        gain="",
                        rdnoise=""):

        self.etc.log(
            "Zerocombine started for {} files using combine({}) and rejection({})"
            .format(len(in_file_list), method, rejection))

        try:
            if self.fop.is_file(out_file):
                self.fop.rm(out_file)

            files = []
            for file in in_file_list:
                if self.fit.is_fit(file):
                    files.append(file)

            if not len(files) == 0:
                biases = ",".join(files)
                # Load packages
                # Unlearn settings
                iraf.imred.unlearn()
                iraf.ccdred.unlearn()
                iraf.ccdred.ccdproc.unlearn()
                iraf.ccdred.combine.unlearn()
                iraf.ccdred.zerocombine.unlearn()

                ccdred.instrument = "ccddb$kpno/camera.dat"
                iraf.imred()
                iraf.ccdred()

                iraf.zerocombine(input=biases,
                                 output=out_file,
                                 combine=method,
                                 reject=rejection,
                                 ccdtype=ccdtype,
                                 Stdout="/dev/null")
            else:
                self.etc.log("No files to combine")

        except Exception as e:
            self.etc.log(e)
Пример #17
0
def optdomecomb(date, fwheel=['bias','B','V','R','I']):
	'''
	#combine biases and optical domes
	#Requires: the uncombined fits images
	#	if you are combining a dome, you must have a bias from the same night as the dome to preform appropriate bias subtraction
	#Input: the date the domes were observed YYMMDD, and fwheel, a list that contains the filters of the domes to be combined
	#Outupt: combined dome fits frame for each color where uncombined frames are in the directory 
	'''
	#convert date to string incase it was entered as an int of float
	date=str(date)
	if len(glob.glob('*bias*')) < 1:
		print "no biases found, exiting"
		return
	else:
		for color in fwheel:
			if color=='bias':
				biaslist=glob.glob('*bias.[0-9]*')
				if len(biaslist) > 10:
					print "only "+str(len(biaslist))+" biases found. you need at least 10"
				else:
					with open("bias.list",'w') as BILIS:
						for i in biaslist:
							BILIS.write(i+'\n')
					iraf.zerocombine("@bias.list",output="ccd"+str(date)+".bias.fits",combine="average",reject="minmax",scale="none",ccdtype="",process="no",delete="no",clobber="no",nlow=1,nhigh=1,nkeep=1)
					print "created ccd"+str(date)+".bias.fits"
					os.system('rm bias.list')

			elif color in ['B','V','R','I']:
				domelist=glob.glob('*dome'+color+'.[0-9]*')
				if len(domelist) < 1:
					print 'no '+color+' domes found'
				elif len(domelist) > 10:
					print 'only '+str(len(domelist))+' domes found. you need at least 10'
				else:
					with open('flat'+color+'.list', 'w') as flist:
						for i in domelist:
							flist.write(i+'\n')
					iraf.ccdproc("@flat"+color+".list", output="z@flat"+color+".list",ccdtype=" ",noproc="no", fixpix="no",overscan="yes", trim="no", zerocor="yes",darkcor="no",flatcor="no", illumcor="no", fringec="no", readcor="no", scancor="no", readaxis="line", biassec="[3:14,1:1024]", zero="ccd"+str(date)+".bias.fits", interactive="no", functio="spline3", order=11)
					iraf.flatcombine("z@flat"+color+".list", output="ccd"+str(date)+".dome"+color+".fits",combine="average", reject="crreject", ccdtype="", process="no", subsets="no", delete="no", clobber="no", scale="mode", rdnoise=6.5, gain=2.3)
					os.system('rm z*dome'+color+'*fits')
					print "created ccd"+str(date)+".dome"+color+".fits"
					os.system('rm flat'+color+'.list')

			else:
				print "your input for the filter was not recognized. Please use either 'bias', 'B', 'V', 'R', or 'I' and try again"
		return
Пример #18
0
        def zerocombine(self,
                        files,
                        output,
                        method="median",
                        rejection="minmax",
                        ccdtype="",
                        overwrite=True):
            """IRAF zerocombine"""
            self.logger.info("Zerocombine Started")
            try:
                iraf.imred.unlearn()
                iraf.ccdred.unlearn()
                iraf.ccdred.ccdproc.unlearn()
                iraf.ccdred.combine.unlearn()
                iraf.ccdred.zerocombine.unlearn()
                iraf.ccdred.zerocombine.unlearn()
                ccdred.instrument = self.instrument_path

                biases = ",".join(files)

                out_file = "{}/myraf_biases.flist".format(self.fop.tmp_dir)
                with open(out_file, "w") as f2w:
                    for i in files:
                        f2w.write("{}\n".format(i))

                biases = "@{}".format(out_file)

                if self.fop.is_file(output) and overwrite:
                    self.logger.warning("Over Writing file({})".format(output))
                    self.fop.rm(output)

                iraf.zerocombine(input=biases,
                                 output=output,
                                 combine=method,
                                 reject=rejection,
                                 ccdtype=ccdtype,
                                 Stdout="/dev/null")
                return True
            except Exception as e:
                self.logger.error(e)
                return False
Пример #19
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('.')
Пример #20
0
def biascom(inim_list,
            outim_list,
            combine='median',
            reject='minmax',
            scale='none'):
    import os, sys
    from pyraf import iraf
    iraf.noao()
    iraf.imred()
    iraf.ccdred()
    iraf.ccdred.setinst(instrume='camera',
                        directo='/iraf/iraf/noao/imred/ccdred/ccddb/',
                        query='q',
                        review='no')
    iraf.zerocombine(input=inim_list,
                     output=outim_list,
                     combine=combine,
                     reject=reject,
                     process='no',
                     scale=scale,
                     ccdtype='')
    print('Output masterbias is created.')
Пример #21
0
def combine_bias(blst):
    iraf.zerocombine(input='@' + blst,
                     output='Zero',
                     combine='average',
                     reject='minmax',
                     ccdtype='',
                     process=False,
                     delete=False,
                     clobber=False,
                     scale='none',
                     statsec='',
                     nlow=0,
                     nhigh=1,
                     nkeep=1,
                     mclip=True,
                     lsigma=3.0,
                     hsigma=3.0,
                     rdnoise='CCDRON',
                     gain='CCDGAIN',
                     snoise=0.0,
                     pclip=-0.5,
                     blank=0.0)
    iraf.flpr()
    print '<<<<<combine bias successfully>>>>>'
Пример #22
0
def optdomecomb(date, fwheel=['bias', 'B', 'V', 'R', 'I']):
    '''
	#combine biases and optical domes
	#Requires: the uncombined fits images
	#	if you are combining a dome, you must have a bias from the same night as the dome to preform appropriate bias subtraction
	#Input: the date the domes were observed YYMMDD, and fwheel, a list that contains the filters of the domes to be combined
	#Outupt: combined dome fits frame for each color where uncombined frames are in the directory 
	'''
    #convert date to string incase it was entered as an int of float
    date = str(date)
    if len(glob.glob('*bias*')) < 1:
        print "no biases found, exiting"
        return
    else:
        for color in fwheel:
            if color == 'bias':
                biaslist = glob.glob('*bias.[0-9]*')
                if len(biaslist) > 10:
                    print "only " + str(
                        len(biaslist)) + " biases found. you need at least 10"
                else:
                    with open("bias.list", 'w') as BILIS:
                        for i in biaslist:
                            BILIS.write(i + '\n')
                    iraf.zerocombine("@bias.list",
                                     output="ccd" + str(date) + ".bias.fits",
                                     combine="average",
                                     reject="minmax",
                                     scale="none",
                                     ccdtype="",
                                     process="no",
                                     delete="no",
                                     clobber="no",
                                     nlow=1,
                                     nhigh=1,
                                     nkeep=1)
                    print "created ccd" + str(date) + ".bias.fits"
                    os.system('rm bias.list')

            elif color in ['B', 'V', 'R', 'I']:
                domelist = glob.glob('*dome' + color + '.[0-9]*')
                if len(domelist) < 1:
                    print 'no ' + color + ' domes found'
                elif len(domelist) > 10:
                    print 'only ' + str(
                        len(domelist)) + ' domes found. you need at least 10'
                else:
                    with open('flat' + color + '.list', 'w') as flist:
                        for i in domelist:
                            flist.write(i + '\n')
                    iraf.ccdproc("@flat" + color + ".list",
                                 output="z@flat" + color + ".list",
                                 ccdtype=" ",
                                 noproc="no",
                                 fixpix="no",
                                 overscan="yes",
                                 trim="no",
                                 zerocor="yes",
                                 darkcor="no",
                                 flatcor="no",
                                 illumcor="no",
                                 fringec="no",
                                 readcor="no",
                                 scancor="no",
                                 readaxis="line",
                                 biassec="[3:14,1:1024]",
                                 zero="ccd" + str(date) + ".bias.fits",
                                 interactive="no",
                                 functio="spline3",
                                 order=11)
                    iraf.flatcombine("z@flat" + color + ".list",
                                     output="ccd" + str(date) + ".dome" +
                                     color + ".fits",
                                     combine="average",
                                     reject="crreject",
                                     ccdtype="",
                                     process="no",
                                     subsets="no",
                                     delete="no",
                                     clobber="no",
                                     scale="mode",
                                     rdnoise=6.5,
                                     gain=2.3)
                    os.system('rm z*dome' + color + '*fits')
                    print "created ccd" + str(date) + ".dome" + color + ".fits"
                    os.system('rm flat' + color + '.list')

            else:
                print "your input for the filter was not recognized. Please use either 'bias', 'B', 'V', 'R', or 'I' and try again"
        return
Пример #23
0
def biascom(imlist_name, camera='STX16803'):
    """
    1. Description 
    : This function makes a master bias image of KCT using Pyraf. Put bias images to 20XX-XX-XX/bias/ directory. Run this code on 20XX-XX-XX 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 2018-XX-XX directory. Make bias directory which contains each bias frame. Naming of each bias images should be zero*.fit. Then just use biascom()
    
    >>> 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
    2020.03.01 Modified for KL4040 process
    2020.03.06 Modified for KCT STX16803 process
    """
    import glob
    import os, sys
    import numpy as np
    from pyraf import iraf
    from astropy.io import fits
    savedir = '/data1/KCT/'
    curdir = os.getcwd()
    '''
    yy   = curdir.split('/')[-1].split('-')[0]
    mm   = curdir.split('/')[-1].split('-')[1]
    dd   = curdir.split('/')[-1].split('-')[2]
    curdate = yy+mm+dd
    '''
    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')
    iraf.chdir('./bias')
    input_name = 'bias.list'
    output_name = curdate + '_zero.fits'
    #os.system('ls cal*bias.fit > '+input_name)
    calibrations = glob.glob(imlist_name)
    f = open(input_name, 'w+')
    for i in range(len(calibrations)):
        hdr = fits.getheader(calibrations[i])
        IMAGETYP = hdr['IMAGETYP']
        if IMAGETYP == 'Bias Frame':
            f.write(calibrations[i] + '\n')
    f.close()
    print('Zerocombine is running...')
    iraf.zerocombine(input='@' + input_name,
                     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 + ' ../')
    os.system('mkdir ' + savedir + 'masterbias')
    os.system('/usr/bin/cp ' + output_name + ' ' + savedir + 'masterbias/')
    iraf.chdir('../')
    iraf.dir('.')
Пример #24
0
def makeZero(fileList='zeros.lst',inputType=INPUT_LISTFNAME,output="Zero.fits",combine='average',reject='minmax',ccdtype='zero'):
	'''create a master Zero.fits from the frames in fileList'''
	iraf.zerocombine(_genIRAFString(fileList,inputType),output=output,combine=combine,reject=reject,ccdtype=ccdtype)
	print "zerocombine successful"
	return
Пример #25
0
def createAverageBias(biasImagesList):
    biasString = getIrafString(biasImagesList)
    iraf.zerocombine(input=biasString,
                     output="averagedBias",
                     process="No",
                     reject="none")
Пример #26
0
def main(argv):
    
    import getopt, os
    import param as p

    try:
        # accept options -h, -m, -t, -b, -d, -f, -r
        opts, argv = getopt.getopt(argv, 'hm:t:b:d:f:r', ['help', 'master=', 'telescope=', 'bias=', 'dark=', 'flat=', 'reduce'])
        
    except getopt.GetoptError:           
        usage()                         
        sys.exit(2)                     
    
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(2)
        
        # need to select the telescope, see param.py
        if opt in ('-r', '--reduce') or opt in ('-m', '--master'):
            for opt2, arg2 in opts:
                if opt2 in ('-t', '--telescope'):
                    telescope = p.telescopes[arg2]
                    
            try: telescope
            except:
                usage()
                sys.exit(2)
        
        # option -r, reduce frames
        if opt in ('-r', '--reduce'):
            
            from pyraf import iraf
            import pyfits

            # inizialise variables
            p = {}
            framelist = []
            filters = []
            
            if arg:
                # one can specify a list of frames, divided by a space 
                # @todo not tested!
                framelist.append(arg.split(' '))
                
            else:
                # get list of frames, using function list_frames
                frameType = 'Light Frame'
                frames = list_frames('.')[0]
                for date in frames[frameType]: # select just frameType
                    for temperature in frames[frameType][date]:
                        for bin in frames[frameType][date][temperature]:
                            framesProc = frames[frameType][date][temperature][bin]
                            for frameProc in framesProc:
                                # exclude master frames (shouldn't be necessary!)
                                if not os.path.basename(frameProc['path'])[0:6] == 'Master':   
                                    # add frame path to framelist                                 
                                    if not frameProc['filter'] in filters: filters.append(frameProc['filter'])
                                    f = {}
                                    f['path'] = os.path.abspath(frameProc['path'])
                                    f['filter'] = frameProc['filter']
                                    framelist.append(f)
            
            # set bias, dark and flats.
            # if nothing is specified, use default Master*.fits

            for opt1, arg1 in opts:
                if opt1 in ('-b', '--bias'):
                    if arg1: p['bias'] = arg1
                if opt1 in ('-d', '--dark'):
                    if arg1: p['dark'] = arg1
                if opt1 in ('-f', '--flat'):
                    if arg1: p['flat'] = arg1
        
            if not 'bias' in p: p['bias'] = 'MasterBias.fits'
            if not 'dark' in p: p['dark'] = 'MasterDark.fits'
            
            # set flat fields, divided by filter
            flatfiles = []
            if not 'flats' in p: 
                import glob
                for file in glob.glob('./MasterFlat*'):
                    f = {}
                    f['path'] = os.path.abspath(file)
                    header=pyfits.getheader(file)
                    f['filter'] = header['FILTER']
                    flatfiles.append(f)
            
            # check that files  exist
            try:
                with open(p['dark']): pass
            except IOError:
                print '%s not found' % p['dark']
            try:
                with open(p['bias']): pass
            except IOError:
                print '%s not found' % p['bias']

            # create copies of original files, with prefix CORR_
            import shutil
            for f in framelist:
                shutil.copy(f['path'], os.path.splitext(f['path'])[0]+'_reduced.fits')
                
            # reduce images by filter
            for filter in filters:
                
                
                # create text list of files for iraf
                tmplistcorr = open('tmplistcorr', 'w')        
                for frame in framelist:
                    if frame['filter'] == filter:
                        tmplistcorr.write("%s_reduced.fits\n" % (os.path.splitext(frame['path'])[0]))
                tmplistcorr.close()
                
                # select flat field
                for flat in flatfiles:
                    if flat['filter'] == filter: p['flat'] = flat['path']
                                
                # reduce frames
                print 'Calibrate images with %s filter' % filter
                iraf.ccdproc(images='@tmplistcorr',output='', ccdtype='', fixpix='no', \
                overscan='no', trim='No', zerocor='yes', zero=p['bias'],            \
                darkcor='Yes', dark=p['dark'], flatcor='yes', flat=p['flat'], readaxi='column', \
                biassec='',    trimsec='')
            
            
        # option -m type: create master frames                
        elif opt in ('-m', '--master'):
          
            from pyraf import iraf
            import pyfits
            
            p = {}
            framelist = []

            # select frame type
            if arg == 'bias' or arg == 'dark' or arg == 'flat':   
                if arg == 'bias': frameType = 'Bias Frame' # we need 'Bias Frame', not just 'bias'!
                elif arg == 'dark': frameType = 'Dark Frame' # same
                elif arg == 'flat': 
                    frameType = 'Flat Field' # same
                    frameflat = [] # frameflat is used to divide files by filter
                    filters = []
                    
                # use function list_frames from reduceall, to list all the frames
                # divided by frametype, date, temperature, bin
                # and add them to a single list
                frames = list_frames('.')[0]
                for date in frames[frameType]: # select just frameType
                    for temperature in frames[frameType][date]:
                        for bin in frames[frameType][date][temperature]:
                            framesProc = frames[frameType][date][temperature][bin]
                            for frameProc in framesProc:
                                # exclude master frames
                                if not os.path.basename(frameProc['path'])[0:6] == 'Master':   
                                    # add frame path to framelist                                 
                                    framelist.append(os.path.abspath(frameProc['path']))
                                    # for flat fields, create a second list (frameflat) 
                                    # that specifies the filter name
                                    if arg == 'flat':
                                        if not frameProc['filter'] in filters: filters.append(frameProc['filter'])
                                        flat = {}
                                        flat['path'] = os.path.abspath(frameProc['path'])
                                        flat['filter'] = frameProc['filter']
                                        frameflat.append(flat)
                
                # set bias frame and/or dark frame.
                # if nothing is specified, use default Master*.fits
                # check that files actually exist
                if arg == 'dark' or arg == 'flat':
                    for opt1, arg1 in opts:
                        if opt1 in ('-b', '--bias'):
                            if arg1: p['bias'] = arg1
                        if opt1 in ('-d', '--dark') and arg == 'flat':
                            if arg1: p['dark'] = arg1
                    
                    if not 'bias' in p: p['bias'] = 'MasterBias.fits'
                    if not 'dark' in p and arg == 'flat': p['dark'] = 'MasterDark.fits'
                    
                    if 'dark' in p:
                        try:
                            with open(p['dark']): pass
                        except IOError:
                            print '%s not found' % p['dark']
                    if 'bias' in p:
                        try:
                            with open(p['bias']): pass
                        except IOError:
                            print '%s not found' % p['bias']


                # create copies of original files, with prefix CORR_
                import shutil
                for f in framelist:
                    shutil.copy(f, os.path.join(os.path.dirname(f), 'CORR_'+os.path.basename(f)))
                
                # create text lists of original and copied files
                tmplistcorr = open('tmplistcorr', 'w')        
                tmplist = open('tmplist', 'w')        
                for f in framelist: 
                    tmplistcorr.write("%s/CORR_%s\n" % (os.path.dirname(f), os.path.basename(f)))
                    tmplist.write("%s\n" % f)
                tmplistcorr.close()
                tmplist.close()

                # create MasterBias
                if arg == 'bias':
                    
                        print 'Trim bias frames'
                        iraf.ccdproc (images='@tmplistcorr',output ='', ccdtype = '', fixpix='no', \
                        oversca = 'no', trim = 'yes', zerocor = 'no', zero = '',   \
                        flatcor = 'no', darkcor = 'no', readaxi = 'column',        \
                        biassec = '', trimsec=telescope.trimsection)
                    
                        print 'Creating MasterBias.fits'
                        iraf.zerocombine (PYinput='@tmplistcorr', output='MasterBias.fits', ccdtype='', \
                        combine='median', reject='none', rdnoise=telescope.rdnoise, gain=telescope.egain)

                # create MasterDark
                if arg == 'dark': 
                                            
                        print 'Bias subtract dark frames'
                        iraf.ccdproc(images='@tmplistcorr',output='', ccdtype='', fixpix='no', \
                        overscan='no', trim='yes', zerocor='yes', zero=p['bias'],            \
                        darkcor='No', flatcor='no', readaxi='column',        \
                        biassec='',    trimsec=telescope.trimsection)
                        
                        print 'Creating MasterDark.fits'
                        iraf.darkcombine (PYinput='@tmplistcorr', output='MasterDark.fits', ccdtype='', \
                        combine='median', reject='none', process='no', delete='no',      \
                        clobber='no', scale='exposure', statsec='', nlow='0', nhigh='1', \
                        nkeep='1', mclip='yes', lsigma='5.', hsigma='4.',rdnoise=telescope.rdnoise,\
                        gain=telescope.egain, snoise='0.', pclip='-0.5', blank='0.')
                
                # create FlatFields, divided by filter
                if arg == 'flat':
                    
                        print 'Bias and dark subtract flat fields'
                        iraf.ccdproc(images='@tmplistcorr',output='', ccdtype='', fixpix='no', \
                        overscan='no', trim='yes', zerocor='yes', zero=p['bias'],            \
                        darkcor='Yes', dark=p['dark'], flatcor='no', readaxi='column',        \
                        biassec='',    trimsec=telescope.trimsection)
                     
                        # divide images by filter and flat combine
                        for filter in filters:
                            print 'Combine flat fields - Filter %s' % filter
                            
                            # create text list for iraf
                            flatlist = open('flatlist', 'w')        
                            for frame in frameflat:
                                if frame['filter'] == filter:
                                    flatlist.write("%s\n" % frame['path'])
                            flatlist.close()
                            
                            iraf.flatcombine(PYinput = '@flatlist', output='MasterFlat-%s.fits' % filter, ccdtype='',   \
                            combine='median', reject='none', process='no', subsets='no',     \
                            delete='no', clobber='no', scale='mode', statsec=' ', nlow='1',  \
                            nhigh='1', nkeep='1', mclip='yes', lsigma='5.', hsigma='5.',     \
                            rdnoise=telescope.rdnoise, gain=telescope.egain, snoise='0.', pclip='-0.5',blank='1.')
                            
                            os.remove('flatlist')


                # remove working files & temp lists
                for f in framelist:
                    os.remove(os.path.join(os.path.dirname(f), 'CORR_'+os.path.basename(f)))
                os.remove('tmplist')
                os.remove('tmplistcorr')
Пример #27
0
 def createAverageBias(self):
     iraf.zerocombine(input=self.inputFolder,output=self.avaragedBias)
Пример #28
0
def run_zerocom(input):
    iraf.noao.imred(_doprint=0)
    iraf.noao.imred.ccdred(_doprint=0)
    iraf.zerocombine(input[0],output=input[1],combine="average",reject="minmax",ccdtype="",process="no",delete="no",clobber="no",scale="none",statsec="[100:1000,100:1000]",nlow="1",nhigh="1",nkeep="3",mclip="no",lsigma="3",hsigma="3",rdnoise="6",gain="7",snoise="0")
Пример #29
0
def process():
    ovrsc = '[1:4,*]'
    trim = '[200:2046,50:200]'
    imglist = glob.glob('FORS2*.fits')
#    objdict = {}
#    objdict.clear()
    for img in imglist:
        hdu = pyfits.getheader(img)
        if 'OBJECT' in hdu:
            obj = hdu['OBJECT']
            if obj not in objdict:
                objdict[obj] = [img]
            else:
                objdict[obj].append(img)

    if 'STD' not in objdict:
        os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T09:15:14.678.fits ./')
        objdict['STD'] = ['FORS2.2016-05-12T09:15:14.678.fits']
        os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T04:24:38.547.fits ./')
        objdict['STD'].append('FORS2.2016-05-12T04:24:38.547.fits')

    stars = {'specphot-LTT7379':'l7379','specphot-LDS749B':'lds749b','specphot-EG274':'eg274','specphot-G138-31':'g13831','specphot-C-32d9927':'cd32','specphot-LTT9491':'l9491','specphot-LTT7987':'l7987'}

    i = 0
    for key in objdict.keys():
        if 'STD' in key:
            for img in objdict[key]:
                i = i + 1
                numstars = len(objdict['STD'])
                hds = pyfits.getheader(img)
                _starname = stars[hds['HIERARCH ESO OBS NAME']]
                if _starname in ['lds749b','g13831']:
                    print 'Bad standard, copying from 2016-05-12'
                    if not os.path.isdir('badstd'):
                        os.mkdir('badstd')
                    os.system('mv '+img+' ./badstd')
                    if i >= numstars:
                        objdict.pop('STD')
                        os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T09:15:14.678.fits ./')
                        objdict['STD'] = ['FORS2.2016-05-12T09:15:14.678.fits']
                        os.system('cp /dark/jsamuel/agn/standards/FORS2.2016-05-12T04:24:38.547.fits ./')
                        objdict['STD'].append('FORS2.2016-05-12T04:24:38.547.fits')



    

    if os.path.isfile('biaslist'):
        os.remove('biaslist')

    if os.path.isfile('masterbias.fits'):
        os.remove('masterbias.fits')

    f = open('biaslist','w')
    for img in objdict['BIAS']:
        f.write(img+'\n')
    f.close()

    imglist = '@biaslist'
    name = 'masterbias.fits'
    hdb = pyfits.getheader(objdict['BIAS'][0])
    _gain = hdb['HIERARCH ESO DET OUT1 GAIN']
    _ron = hdb['HIERARCH ESO DET OUT1 RON']
    iraf.zerocombine(imglist,output=name,combine='average',reject='minmax',ccdtype='none',process='no',gain=_gain,rdnoise=_ron,Stdout=1)



    if os.path.isfile('flatlist'):
        os.remove('flatlist')

    if os.path.isfile('sciflatlist'):
        os.remove('sciflatlist')

    if os.path.isfile('stdflatlist'):
        os.remove('stdflatlist')

    if os.path.isfile('masterflat.fits'):
        os.remove('masterflat.fits')

    if os.path.isfile('scimasterflat.fits'):
        os.remove('scimasterflat.fits')

    if os.path.isfile('stdmasterflat.fits'):
        os.remove('stdmasterflat.fits')



        
    f = open('sciflatlist','w')
    for img in objdict['FLAT,LAMP']:
        hdu = pyfits.getheader(img)
        if hdu['HIERARCH ESO DPR TECH'] == 'SPECTRUM':
            f.write(img+'\n')
    f.close()

    j = 0
    f = open('stdflatlist','w')
    for img in objdict['FLAT,LAMP']:
        hdu = pyfits.getheader(img)
        if hdu['HIERARCH ESO DPR TECH'] == 'MOS':
            f.write(img+'\n')
            j = j + 1
    f.close()





    imglist = '@sciflatlist'
    name = 'scimasterflat.fits'
    hdf = pyfits.getheader(objdict['FLAT,LAMP'][0])
    _gain = hdf['HIERARCH ESO DET OUT1 GAIN']
    _ron = hdf['HIERARCH ESO DET OUT1 RON']
    iraf.flatcombine(imglist,output=name,combine='average',reject='avsigclip',ccdtype='none',process='no',subsets='yes',delete='no',clobber='no',gain=_gain,rdnoise=_ron,Stdout=1)


    if j == 0:
        imglist = '@sciflatlist'
    elif j >= 1:
        imglist = '@stdflatlist'
    name = 'stdmasterflat.fits'
    hdf = pyfits.getheader(objdict['FLAT,LAMP'][0])
    _gain = hdf['HIERARCH ESO DET OUT1 GAIN']
    _ron = hdf['HIERARCH ESO DET OUT1 RON']
    iraf.flatcombine(imglist,output=name,combine='average',reject='avsigclip',ccdtype='none',process='no',subsets='yes',delete='no',clobber='no',gain=_gain,rdnoise=_ron,Stdout=1)





    if os.path.isfile('tmasterbias.fits'):
        os.remove('tmasterbias.fits')

    fits = 'masterbias.fits'
    name = 'tmasterbias.fits'
    zcor = 'no'
    fcor = 'no'
    _zero = ''
    _flat = ''
    iraf.ccdproc(fits,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=_flat,Stdout=1)


    if os.path.isfile('tmasterflat.fits'):
        os.remove('tmasterflat.fits')

    if os.path.isfile('tscimasterflat.fits'):
        os.remove('tscimasterflat.fits')

    if os.path.isfile('tstdmasterflat.fits'):
        os.remove('tstdmasterflat.fits')



    fits = 'scimasterflat.fits'
    name = 'tscimasterflat.fits'
    zcor = 'yes'
    fcor = 'no'
    _zero = 'tmasterbias.fits'
    _flat = ''
    iraf.ccdproc(fits,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=_flat,Stdout=1)

    fits = 'stdmasterflat.fits'
    name = 'tstdmasterflat.fits'
    zcor = 'yes'
    fcor = 'no'
    _zero = 'tmasterbias.fits'
    _flat = ''
    iraf.ccdproc(fits,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=_flat,Stdout=1)
                




    if os.path.isfile('ntmasterflat.fits'):
        os.remove('ntmasterflat.fits')

    if os.path.isfile('ntscimasterflat.fits'):
        os.remove('ntscimasterflat.fits')

    if os.path.isfile('ntstdmasterflat.fits'):
        os.remove('ntstdmasterflat.fits')

    cal = 'tscimasterflat.fits'
    resp = 'ntscimasterflat.fits'
    _order = 100
    iraf.response(calibration=cal,normalization=cal,response=resp,interactive='no',function='legendre',order=_order,graphics='stdgraph')

    cal = 'tstdmasterflat.fits'
    resp = 'ntstdmasterflat.fits'
    _order = 100
    iraf.response(calibration=cal,normalization=cal,response=resp,interactive='no',function='legendre',order=_order,graphics='stdgraph')




##########

    ovrsc = '[1:4,*]'
    trim = '[200:2046,50:200]'
    
    fimg = 'ntscimasterflat'
    _area = '[1:150,*]'
    _value = 1.0
    iraf.imreplace(images=fimg+_area,value=_value)

    fimg = 'ntstdmasterflat'
    _area = '[1:150,*]'
    _value = 1.0
    iraf.imreplace(images=fimg+_area,value=_value)
    

    alist = glob.glob('*AGN*.fits')
    for f in alist:
        os.remove(f)
    slist = glob.glob('STD*.fits')
    for f in slist:
        os.remove(f)
    llist = glob.glob('LAMP*.fits')
    for f in llist:
        os.remove(f)


    zcor = 'yes'
    fcor = 'yes'
    _zero = 'tmasterbias.fits'
    sciflat = 'ntscimasterflat.fits'
    stdflat = 'ntstdmasterflat.fits'

    for key in objdict.keys():
        if 'AGN' in key:
            for img in objdict[key]:
                hds = pyfits.getheader(img)
                if 'EXPTIME' in hds:
                    if hds['EXPTIME'] >= 50.0:
                        num = img.rsplit('.',2)[1]
                        name = key+'_'+num
                        '''
                        iraf.ccdproc(img,output='trim_'+name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor='no',darkcor='no',flatcor='no',illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero='',flat='',Stdout=1)

                        _gain = hds['HIERARCH ESO DET OUT1 GAIN']
                        _ron = hds['HIERARCH ESO DET OUT1 RON']
                        cosmics.lacos('trim_'+name+'.fits', output='c_'+name+'.fits', gain=_gain, readn=_ron, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, verbose=True, interactive=False)

                        iraf.ccdproc('c_'+name,output=name,ccdtype='',noproc='no',fixpix='no',overscan='no',trim='no',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec='',trimsec='',zero=_zero,flat=sciflat,Stdout=1)
                        '''

                        iraf.ccdproc(img,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=sciflat,Stdout=1)

                    else:
                        pass


        elif 'STD' in key:
            for img in objdict[key]:
                num = img.rsplit('.',2)[1]
                name = key+'_'+num
                iraf.ccdproc(img,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=stdflat,Stdout=1)

        elif 'WAVE' in key:
            for img in objdict[key]:
                num = img.rsplit('.',2)[1]
                name = 'LAMP_'+num
                iraf.ccdproc(img,output=name,ccdtype='',noproc='no',fixpix='no',overscan='yes',trim='yes',zerocor=zcor,darkcor='no',flatcor=fcor,illumcor='no',fringecor='no',readcor='no',scancor='no',biassec=ovrsc,trimsec=trim,zero=_zero,flat=stdflat,Stdout=1)
Пример #30
0
def efoscreduction(imglist, _interactive, _doflat, _dobias, listflat, listbias, _dobadpixel, badpixelmask,
                   fringingmask, _archive, typefile, filenameobjects, _system, _cosmic, _verbose=False, method='iraf'):
    # print "LOGX:: Entering `efoscreduction` method/function in %(__file__)s"
    # % globals()
    import ntt
    from ntt.efoscphotredudef import searchbias
    from ntt.util import delete, readhdr, readkey3, display_image, searchflat, rangedata, correctcard
    from numpy import argmin, min, abs, sqrt
    import string
    import os
    import re
    import math
    import sys
    from pyraf import iraf
    # ##   Call and set parameters for useful iraf tasks
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    iraf.proto(_doprint=0)

    toforget = ['ccdproc', 'zerocombine',
                'flatcombine', 'imreplace', 'proto.fixpix']
    for t in toforget:
        iraf.unlearn(t)

    iraf.ccdproc.darkcor = 'no'
    iraf.ccdproc.fixpix = 'no'
    iraf.ccdproc.flatcor = 'no'
    iraf.ccdproc.zerocor = 'no'
    iraf.ccdproc.overscan = 'no'
    iraf.ccdproc.ccdtype = ''
    iraf.ccdproc.biassec = ''
    iraf.ccdred.instrument = "/dev/null"

    if _verbose:
        iraf.ccdred.verbose = 'yes'
    else:
        iraf.ccdred.verbose = 'no'
    import datetime
    import time
    #      starttime=time.time()
    now = datetime.datetime.now()
    datenow = now.strftime('20%y%m%d%H%M')
    MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days
    outputfile = []
    reduceddata = rangedata(imglist)
    img = re.sub('\n', '', imglist[0])
    hdr = readhdr(img)
    _gain = readkey3(hdr, 'gain')
    _rdnoise = readkey3(hdr, 'ron')
    _instrume = readkey3(hdr, 'instrume')
    _trimsec = '[3:1010,1:1015]'
    biaslist = {}
    flatlist1 = {}
    flatlist2 = {}
    objectlist = {}
    filterlist1 = []
    filterlist2 = []
    for img in imglist:
        _type = ''
        img = re.sub('\n', '', img)
        hdr = readhdr(img)
        _naxis1 = readkey3(hdr, 'NAXIS1')
        _naxis2 = readkey3(hdr, 'NAXIS2')
        if _naxis1 != 1030 or _naxis2 != 1030:
            ntt.util.writeinthelog(
                'image ' + str(img) + ' different dimension =\n', './logNTT.txt')
            _type = 'not good'
        if not _type and readkey3(hdr, 'speed') != 'fastL':
            _type = 'not good'
        if not _type and readkey3(hdr, 'instrume') != 'efosc':
            _type = 'not good'
        _imagetype = readkey3(hdr, 'tech')
        if not _type and _imagetype == 'SPECTRUM':
            _type = 'spectroscopic data'
        if not _type:
            _exptime = readkey3(hdr, 'exptime')
            _date = readkey3(hdr, 'date-night')
            _filter = readkey3(hdr, 'filter')
            if float(_exptime) == 0.0:
                if _date not in biaslist:
                    biaslist[_date] = []
                biaslist[_date].append(img)
                _type = 'bias'
            if not _type:
                _object = readkey3(hdr, 'object')
                if _filter.lower() in ['g782', 'r784', 'z623', 'u640', 'b639', 'v641', 'r642',
                                       'i705'] and _imagetype == 'IMAGE':
                    if 'sky,flat' in _object.lower():
                        _type = 'flat'
                    elif 'dome' in _object.lower() or 'flat' in _object.lower():
                        _type = 'flat dome'
                    if _type == 'flat':
                        if _filter not in filterlist1:
                            filterlist1.append(_filter)
                            flatlist1[_filter] = []
                        flatlist1[_filter].append(img)
                    if _type == 'flat dome':
                        if _filter not in filterlist2:
                            filterlist2.append(_filter)
                            flatlist2[_filter] = []
                        flatlist2[_filter].append(img)
            if not _type:
                _catg = readkey3(hdr, 'catg')
                if 'science' in _catg.lower() or 'acquisition' in _catg.lower():
                    _type = 'object'
                    if _filter not in objectlist:
                        objectlist[_filter] = []
                    objectlist[_filter].append(img)
                    if 'acquisition' in _catg.lower():
                        try:
                            correctcard(img)
                            _ra1, _dec1, _name = ntt.util.correctobject(
                                img, 'standard_efosc_mab.txt')
                            _ra1, _dec1, _name = ntt.util.correctobject(
                                img, filenameobjects)
                        except:
                            pass

                elif 'focus' in _object.lower():
                    _type = 'not good'
            if not _type:
                print '\n### warning: object not recognized '
                _object = readkey3(hdr, 'object')
                print img, _object, _imagetype
                answ = raw_input(
                    'what is it: bias [1], flat [3], object[4], test [5] ?  [5] ')
                if not answ:
                    answ = '5'
                if answ == '1':
                    if _date not in biaslist:
                        biaslist[_date] = ()
                    biaslist[_date].append(img)
                elif answ == '4':
                    if _filter not in objectlist:
                        objectlist[_filter] = []
                    objectlist[_filter].append(img)
                elif answ == '3':
                    tt = raw_input('dome or sky [d/[s]] ? ')
                    if tt == 's':
                        _type = 'flat'
                        _filter = readkey3(hdr, 'filter')
                        if _filter not in filterlist1:
                            filterlist1.append(_filter)
                            flatlist1[_filter] = []
                        flatlist1[_filter].append(img)
                    elif tt == 'd':
                        _type = 'flat dome'
                        _filter = readkey3(hdr, 'filter')
                        if _filter not in filterlist2:
                            filterlist2.append(_filter)
                            flatlist2[_filter] = []
                        flatlist2[_filter].append(img)
                elif answ == '5':
                    _type = 'not good'

    filterlist = list(set(filterlist1 + filterlist2))
    if _verbose:
        print filterlist1
        print filterlist2
        print flatlist1
        print flatlist2
    flatlist = {}
    for _filt in filterlist:
        if _filt not in flatlist1.keys():
            if _filt in flatlist2.keys():
                if len(flatlist2[_filt]) >= 3:
                    flatlist[_filt] = flatlist2[_filt]
        elif len(flatlist1[_filt]) < 3:
            if _filt in flatlist2.keys():
                if len(flatlist2[_filt]) >= 3:
                    flatlist[_filt] = flatlist2[_filt]
        elif _filt in flatlist1.keys():
            if len(flatlist1[_filt]) >= 3:
                flatlist[_filt] = flatlist1[_filt]

    listaout = []
    if _verbose:
        print '\n### flat ', str(flatlist), '\n'
        print '\n### bias ', str(biaslist), '\n'
        print '\n### object ', str(objectlist), '\n'
        ###### masterbias  #################
    if _dobias:
        if not _archive:
            if listbias:
                masterbiaslist = listbias
            else:
                masterbiaslist = []
                if biaslist:
                    for _date in biaslist:
                        print '\n do bias ' + str(_date) + '\n'
                        biaslist[_date] = rejectbias(
                            biaslist[_date], False, 10)
                        if len(biaslist[_date]) >= 3:
                            masterbiasfile = 'bias_' + \
                                str(_date) + '_' + str(MJDtoday) + '.fits'
                            delete(masterbiasfile)
                            f = open('biaslist', 'w')
                            h = open('obiaslist', 'w')
                            for img in biaslist[_date]:
                                f.write(img + '\n')
                                h.write('o' + img + '\n')
                                delete('o' + img)
                            f.close()
                            h.close()
                            try:
                                print 'processing bias .....'
                                iraf.ccdproc('@biaslist', output='@obiaslist', overscan="no", trim="yes", zerocor='no',
                                             fixpix='no', ccdtype='', flatcor='no', darkcor='no', biassec='',
                                             trimsec=str(_trimsec), readaxi='column', Stdout=1)
                                iraf.zerocombine('@obiaslist', output=masterbiasfile, combine='median',
                                                 reject='ccdclip', ccdtype='', process='no',
                                                 rdnoise=_rdnoise, gain=_gain, Stdout=1)
                                correctcard(masterbiasfile)
                                num = 0
                                for img in biaslist[_date]:
                                    num = num + 1
                                    ntt.util.updateheader(masterbiasfile, 0, {
                                        'PROV' + str(num): [readkey3(readhdr(img), 'ARCFILE'), 'Originating file']})
                                    ntt.util.updateheader(masterbiasfile, 0, {
                                        'TRACE' + str(num): [readkey3(readhdr(img), 'ARCFILE'), 'Originating file']})
                                    delete('o' + img)
                                ntt.util.updateheader(masterbiasfile, 0,
                                                      {'M_EPOCH': [False, 'TRUE if resulting from multiple epochs']})
                                ntt.util.updateheader(masterbiasfile, 0,
                                                      {'SINGLEXP': [False, 'TRUE if resulting from single exposure']})
                                ntt.util.updateheader(masterbiasfile, 0, {
                                                      'FILETYPE': [11201, 'bias']})
                                masterbiaslist.append(masterbiasfile)

                                if masterbiasfile not in outputfile:
                                    outputfile.append(masterbiasfile)
                            except:
                                ntt.util.writeinthelog(
                                    'Warning ' +
                                    str(biaslist[_date]) +
                                    ' problem with this list of bias \n',
                                    './logNTT.txt')
                            if masterbiasfile and _interactive:
                                aa, bb, cc = display_image(
                                    masterbiasfile, 1, '', '', False)
                                answ = raw_input(
                                    'is the masterbias ok [[y]/n] ?')
                                if not answ:
                                    answ = 'y'
                                if answ in ['n', 'no']:
                                    sys.exit(
                                        'remove bad bias from input list and restart')
        else:
            masterbiaslist = []

    ########## masterflat   #########################

    if _doflat:
        if not _archive:
            if listflat:
                masterflatlist = listflat
            else:
                masterflatlist = []
                if flatlist:
                    for _filter in flatlist:
                        print '\n do flat ' + str(_filter) + '\n'
                        flatlist[_filter] = rejectflat(
                            flatlist[_filter], False)
                        if len(flatlist[_filter]) >= 3:
                            _date = readkey3(
                                readhdr(flatlist[_filter][0]), 'date-night')
                            masterflat = 'flat_' + \
                                str(_date) + '_' + str(_filter) + \
                                '_' + str(MJDtoday) + '.fits'
                            listaflat = 'flatlist_' + \
                                str(_date) + '_' + str(_filter)
                            _bias = ''
                            if masterbiaslist:
                                _bias = searchbias(flatlist[_filter][
                                                   0], masterbiaslist)[0]
                            if not _bias:
                                _bias = searchbias(flatlist[_filter][0], '')[0]
                            if _bias:
                                if _bias[0] == '/':
                                    os.system('cp ' + _bias + ' .')
                                    _bias = string.split(_bias, '/')[-1]
                                    _zerocor = 'yes'
                                else:
                                    _zerocor = 'yes'
                            else:
                                _zerocor = 'no'
                            answ0 = 'n'
                            while answ0 != 'y':
                                f = open(listaflat, 'w')
                                h = open('o' + listaflat, 'w')
                                for img in flatlist[_filter]:
                                    f.write(img + '\n')
                                    h.write('o' + img + '\n')
                                    delete('o' + img)
                                f.close()
                                h.close()
                                try:
                                    print 'processing flat .....'
                                    iraf.ccdproc('@' + listaflat, output='@o' + listaflat, overscan='no', trim='yes',
                                                 darkcor='no', fixpix='no',
                                                 zerocor=_zerocor, flatcor='no', trimsec=str(_trimsec), biassec='',
                                                 zero=_bias, readaxi='column', ccdtype='', Stdout=1)
                                    delete(masterflat)
                                    iraf.flatcombine('@o' + listaflat, output=masterflat, combine='average',
                                                     reject='avsigclip', ccdtype='', process='no',
                                                     rdnoise=_rdnoise, gain=_gain, statsec='[100:800,100:800]',
                                                     lsigma=3, hsigma=2, Stdout=1)
                                    masterflatlist.append(masterflat)
                                    correctcard(masterflat)
                                    num = 0
                                    for img in flatlist[_filter]:
                                        num = num + 1
                                        ntt.util.updateheader(masterflat, 0, {
                                            'PROV' + str(num): [readkey3(readhdr(img), 'ARCFILE'), 'Originating file']})
                                        ntt.util.updateheader(masterflat, 0, {
                                            'TRACE' + str(num): [readkey3(readhdr(img), 'ARCFILE'), 'Originating file']})
                                        delete('o' + img)
                                    ntt.util.updateheader(
                                        masterflat, 0, {'ZEROCOR': [_bias, '']})
                                    ntt.util.updateheader(masterflat, 0, {
                                        'M_EPOCH': [False, 'TRUE if resulting from multiple epochs']})
                                    ntt.util.updateheader(masterflat, 0, {
                                        'SINGLEXP': [False, 'TRUE if resulting from single exposure']})
                                    ntt.util.updateheader(
                                        masterflat, 0, {'FILETYPE': [11202, 'flat field']})

                                    if masterflat not in outputfile:
                                        outputfile.append(masterflat)
                                except:
                                    ntt.util.writeinthelog(
                                        'Warning ' +
                                        str(flatlist[
                                            _filter]) + ' problem with this list of flat \n',
                                        './logNTT.txt')
                                aa, bb, cc = display_image(
                                    masterflat, 1, '', '', False)
                                if masterflat and _interactive:
                                    answ = raw_input(
                                        'is the masterflat ok [[y]/n] ?')
                                    if not answ:
                                        answ = 'y'
                                    if answ.lower() in ['n', 'no']:
                                        answ1 = raw_input(
                                            'try again [[y]/n] ?')
                                        if not answ1:
                                            answ1 = 'y'
                                        if answ1.lower() in ['y', 'yes']:
                                            flatlist[_filter] = ntt.efoscphotredudef.rejectflat(
                                                flatlist[_filter], True)
                                        else:
                                            sys.exit(
                                                'error: problem with flat .... exit')
                                    else:
                                        answ0 = 'y'
                                else:
                                    answ0 = 'y'
        else:
            masterflatlist = []
    ##########################################################################
    if len(masterbiaslist) == 0:
        masterbiaslist = ''
    if len(masterflatlist) == 0:
        masterflatlist = ''
    ######################################
    if _verbose:
        print ''
        print '#############################'
        print masterflatlist
        print masterbiaslist
        print '#############################'
        print ''
    if masterflatlist:
        listaout = listaout + masterflatlist
    if masterbiaslist:
        listaout = listaout + masterbiaslist
    if typefile == 'calib':
        objectlist = {}
    for _filter in objectlist:
        for img in objectlist[_filter]:
            hdr = readhdr(img)
            print '\n#####################################################################\n'
            _object = readkey3(hdr, 'object')
            _object = re.sub(' ', '', _object)
            _object = re.sub('/', '_', _object)
            _object = re.sub('\n', '', _object)
            _exptime = readkey3(hdr, 'exptime')
            _date = readkey3(hdr, 'date-night')
            nameout = ntt.util.name_duplicate(img, str(_object) + '_' + str(_date) + '_' + str(_filter) + '_' + str(
                MJDtoday), '')
            _bias = ''
            if _dobias:
                if masterbiaslist:
                    _bias = searchbias(img, masterbiaslist)[0]
                if not _bias:
                    _bias = searchbias(img, '')[0]
            _flat = ''
            if _doflat:
                if masterflatlist:
                    _flat = searchflat(img, masterflatlist)[0]
                if not _flat:
                    _flat = searchflat(img, '')[0]
                if _bias:  # bias  ###
                    if _bias[0] == '/':
                        os.system('cp ' + _bias + ' .')
                        _bias = string.split(_bias, '/')[-1]
                    _zerocor = 'yes'
                else:
                    _zerocor = 'no'
            else:
                _zerocor = 'no'

            if _flat:  # flat  ###
                if _flat[0] == '/':
                    os.system('cp ' + _flat + ' .')
                    _flat = string.split(_flat, '/')[-1]
                _flatcor = 'yes'
            else:
                _flatcor = 'no'
            sss = str(_object) + '_' + str(_date) + '_' + str(_filter)
            print '### input', img, sss
            print '### bias ', _zerocor, _bias
            print '### flat ', _flatcor, _flat
            print '### name ', nameout
            delete(nameout)
            try:
                iraf.ccdproc(img, output=nameout, overscan="no", trim="yes", zerocor=_zerocor, flatcor='no',
                             darkcor='no', trimsec=str(_trimsec), zero=_bias, biassec='', readaxi='column', Stdout=1)
                try:
                    iraf.ccdproc(nameout, output='', overscan="no", trim="no", zerocor='no', flatcor=_flatcor,
                                 darkcor='no', flat=_flat, readaxi='column', ccdtype='', Stdout=1)
                except:
                    iraf.imrepla(images=_flat, value=0.01,
                                 lower='INDEF', upper=0.01, radius=0)
                    iraf.ccdproc(nameout, output='', overscan="no", trim="no", zerocor='no', flatcor=_flatcor,
                                 darkcor='no', flat=_flat, readaxi='column', ccdtype='', Stdout=1)
                correctcard(nameout)
                ntt.util.updateheader(nameout, 0, {'FILTER': [readkey3(readhdr(nameout), 'filter'), 'Filter name'],
                                                   'SINGLEXP': [True, 'TRUE if resulting from single exposure'],
                                                   'M_EPOCH': [False, 'TRUE if resulting from multiple epochs'],
                                                   'FLATCOR': [_flat, ''],
                                                   'ZEROCOR': [_bias, ''], 'FILETYPE': [12204, 'pre-reduced image'],
                                                   'PROV1': [readkey3(readhdr(nameout), 'ARCFILE'), 'Originating file'],
                                                   'NCOMBINE': [1, 'Number of raw science data'],
                                                   'TRACE1': [readkey3(readhdr(nameout), 'ARCFILE'),
                                                              'Originating file']})
                ntt.util.airmass(nameout)  # phase 3 definitions

                ntt.util.writeinthelog('\n', './logNTT.txt')
                ntt.util.writeinthelog(
                    'image= ' + str(img) + ' output= ' + str(nameout) + '\n', './logNTT.txt')
                ntt.util.writeinthelog(
                    'bias= ' + str(_bias) + ', flat= ' + str(_flat) + '\n', './logNTT.txt')
                ntt.util.writeinthelog('\n', './logNTT.txt')
                if nameout not in outputfile:
                    outputfile.append(nameout)
            except:
                ntt.util.writeinthelog(
                    'image ' + str(img) + ' probably corrupted\n', './logNTT.txt')
            if _dobadpixel:
                if not badpixelmask:
                    badpixelmask = 'bad_pixel_mask.fits'
                    delete(badpixelmask)
                    os.system('cp ' + ntt.__path__[0] + '/archive/' + str(
                        _instrume) + '/badpixels/badpixel.fits ' + badpixelmask)
                iraf.proto.fixpix(images=nameout, masks=badpixelmask,
                                  linterp='INDEF', cinterp='INDEF', verbose='no')
                ntt.util.updateheader(
                    nameout, 0, {'FIXPIX': [badpixelmask, '']})
                ntt.util.writeinthelog('image ' + str(nameout) + ' bad pixel corrected with ' + badpixelmask + '\n',
                                       './logNTT.txt')
                print '\n### bad pixel mask correction ..... done'
            else:
                ntt.util.writeinthelog(
                    'image ' + str(nameout) + ' bad pixel NOT corrected\n', './logNTT.txt')
            if _cosmic:
                try:
                    print '\n### cosmic  ..... '
                    ntt.cosmics.lacos_im(nameout, _output='', gain=_gain, readn=_rdnoise, xorder=9, yorder=9,
                                         sigclip=4.5, sigfrac=0.5, objlim=1, skyval=0, niter=0, verbose=True,
                                         interactive=False)
                    ntt.util.updateheader(nameout, 0, {
                        'LACOSMIC': [True, 'TRUE if Laplacian cosmic ray rejection has been applied to the image']})
                    print '\n### cosmic  .....  removed '
                except Exception, e:
                    print e
            else:
                ntt.util.updateheader(nameout, 0, {
                    'LACOSMIC': [False, 'TRUE if Laplacian cosmic ray rejection has been applied to the image']})
            try:
                ##########################
                sexvec = ntt.efoscastrodef.sextractor(nameout)
                for cat in ['2mass', 'usnoa2', 'usnob1']:
                    rmsx3, rmsy3, num3, fwhmgess, ellgess, ccc, rasys3, decsys3, magsat3 = ntt.efoscastrodef.efoscastroloop(
                        [nameout], cat, False, 40, 40, 100, 'rxyscale', 100, 30, sexvec, True, 10, method)
                    if rmsx3 <= 2 and rmsy3 <= 2:
                        break
                if rmsx3 > 2 and rmsy3 > 2:
                    for cat in ['2mass', 'usnoa2', 'usnob1']:
                        rmsx3, rmsy3, num3, fwhmgess, ellgess, ccc, rasys3, decsys3, magsat3 = ntt.efoscastrodef.efoscastroloop(
                            [nameout], cat, False, 20, int(20), int(50), 'rxyscale', 100, 30, sexvec, True, 5, method)
                        if rmsx3 <= 2 and rmsy3 <= 2:
                            break
                    if rmsx3 > 2 and rmsy3 > 2:
                        for cat in ['2mass', 'usnoa2', 'usnob1']:
                            rmsx3, rmsy3, num3, fwhmgess, ellgess, ccc, rasys3, decsys3, magsat3 = ntt.efoscastrodef.efoscastroloop(
                                [nameout], cat, False, int(10), int(10),
                                int(25), 'rxyscale', 100, 30, sexvec, True, int(3), method)
                        ##########################
                astrostring = str(rmsx3) + ' ' + str(rmsy3) + ' ' + str(num3)
                ntt.util.updateheader(
                    nameout, 0, {'ASTROMET': [astrostring, 'rmsx rmsy nstars']})
                print '\n### check astrometry: fine \n### rmsx rmsy nstars: ' + astrostring
            except Exception, e:
                print e
                rmsx3, rmsy3, num3, fwhmgess, ellgess, ccc, rasys3, decsys3, magsat3 = '', '', '', '', '', '', '', '', ''
                print '\n### problem with astrometry, do you have network ? '
            if fwhmgess and fwhmgess < 99:
                ntt.util.updateheader(nameout, 0, {'PSF_FWHM': [fwhmgess, 'Spatial resolution (arcsec)'],
                                                   'ELLIPTIC': [ellgess, 'Average ellipticity of point sources'],
                                                   'CRDER1': [(1 / sqrt(2.)) * float(rmsx3) * (1. / 3600.),
                                                              'Random error (degree)'],
                                                   'CRDER2': [(1 / sqrt(2.)) * float(rmsy3) * (1. / 3600.),
                                                              'Random error (degree)'],
                                                   'CUNIT1': ['deg', 'unit of the coord. trans.'],
                                                   'CUNIT2': ['deg', 'unit of the coord. trans.'],
                                                   'CSYER1': [rasys3, 'Systematic error (RA_m - Ra_ref)'],
                                                   'CSYER2': [decsys3, 'Systematic error (DEC_m - DEC_ref)']})
            else:
                ntt.util.updateheader(nameout, 0, {'PSF_FWHM': [9999., 'FHWM (arcsec) - computed with sectractor'],
                                                   'ELLIPTIC': [9999., 'ellipticity of point sources (1-b/a)'],
                                                   'CRDER1': [9999., 'Random error in axis 1'],
                                                   'CRDER2': [9999., 'Random error in axis 2'],
                                                   'CUNIT1': ['deg', 'unit of the coord. trans.'],
                                                   'CUNIT2': ['deg', 'unit of the coord. trans.'],
                                                   'CSYER1': [9999., 'Systematic error (RA_m - Ra_ref)'],
                                                   'CSYER2': [9999., 'Systematic error (DEC_m - DEC_ref)']})

            try:
                result = ntt.efoscastrodef.zeropoint(
                    nameout, _system, method, False, False)
            except:
                result = ''
            if result:
                if os.path.isfile(re.sub('.fits', '.ph', nameout)):
                    if re.sub('.fits', '.ph', nameout) not in outputfile:
                        outputfile.append(
                            re.sub('.fits', '.ph', nameout))
                print '\n### zeropoint ..... done'
                for ll in result:
                    valore = '%3.3s %6.6s %6.6s' % (
                        str(ll), str(result[ll][1]), str(result[ll][0]))
                    print '### ', valore
                    ntt.util.updateheader(
                        nameout, 0, {'zp' + ll: [str(valore), '']})
            if magsat3:
                if readkey3(readhdr(nameout), 'FLUXCAL') == 'ABSOLUTE':
                    try:
                        ntt.util.updateheader(nameout, 0, {
                            'ABMAGSAT': [float(magsat3) + float(readkey3(readhdr(nameout)), 'PHOTZP'),
                                         'Saturation limit for point sources (AB mags)']})
                    except:
                        ntt.util.updateheader(nameout, 0, {
                            'ABMAGSAT': [float(magsat3), 'Saturation limit for point sources (AB mags)']})
                else:
                    ntt.util.updateheader(nameout, 0, {
                        'ABMAGSAT': [float(magsat3), 'Saturation limit for point sources (AB mags)']})
            else:
                ntt.util.updateheader(nameout, 0, {'ABMAGSAT': [
                                      9999., 'Saturation limit for point sources (AB mags)']})

            maglim = ntt.util.limmag(nameout)
            if maglim:
                ntt.util.updateheader(nameout, 0,
                                      {'ABMAGLIM': [maglim, '5-sigma limiting AB magnitude for point sources']})
            else:
                ntt.util.updateheader(nameout, 0,
                                      {'ABMAGLIM': [9999., '5-sigma limiting AB magnitude for point sources']})

            if readkey3(readhdr(nameout), 'filter') in ['i705']:
                try:
                    nameout, maskname = ntt.efoscphotredudef.fringing2(
                        nameout, fringingmask, _interactive, False)
                    if nameout not in outputfile:
                        outputfile.append(nameout)
                        if maskname not in outputfile:
                            outputfile.append(maskname)

                except:
                    ntt.util.writeinthelog(
                        'image ' + str(nameout) + ' probably corrupted\n', './logNTT.txt')
                    print '\n### problem with fringing correction'
Пример #31
0
def run_zerocombine(images, output, ccdtype="none"):
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    iraf.zerocombine(input=images, output=output, ccdtype=ccdtype, nhigh=0)
Пример #32
0
def createAverageBias(biasImagesList):
    biasString = getIrafString(biasImagesList)
    iraf.zerocombine(input=biasString,output="averagedBias",process="No",reject="none")
Пример #33
0
        if(verbose == True):
            print sys.argv[0],"STATUS: Output master bias image as",biasfits
           
    # Edit the images headers by changing IMAGETYP to "zero"
    if(verbose == True):
        print sys.argv[0],"STATUS: Running hedit to set IMAGETYP=zero"
    if(verbose == True):
        iraf.hedit(atlist, fields="IMAGETYP", value="zero", add="yes", verify="no", update="yes", show="yes")
    else:
        iraf.hedit(atlist, fields="IMAGETYP", value="zero", add="yes", verify="no", update="yes", show="no")

    # Run the zerocombine
    if(verbose == True):
        print sys.argv[0],"STATUS: Running zerocombine on imagelist =",biaslist,"and output as",biasfits,"with combine =",combinetype,"and reject =",rejecttype
    # hardwired some of the parameters at the moment
    iraf.zerocombine(atlist, output=biasfits, combine=combinetype, ccdtype="zero", reject=rejecttype, scale="none", nlow=0, nhigh=1, nkeep=1, mclip="yes", lsigma=3.0, hsigma=3.0, rdnoise="0.", gain ="1.")
    
    # Rename the logfile 
    outlog=biasfits.rstrip('fits')+'log'
    os.rename("logfile",outlog)

    # Finally print the ending message
    print sys.argv[0],"STATUS: zerocombine done"

if __name__ == "__main__":
    main()


#----------------------------------------------------
#  Date      Version    Note
#----------------------------------------------------
Пример #34
0
#iraf.text2mask('badcols','echmask',ncols=2128,nlines=2068,linterp=1,cinterp=1,
#               square=1,pixel=1)
#  Note: This ^ doesn't work currently on 64-bit systems. Does not produce
#  a working mask. Use 32-bit IRAF to produce mask.

#  Forces interpolation of bad pixel masks along the
iraf.fixpix('@objflat', 'echmask.pl', linterp=1, verbose=yes)
iraf.fixpix('@arcs', 'echmask.pl', linterp=1, verbose=yes)

# MAKE AN AVERAGE (FIDUCIAL) BIAS AND DARK (if applicable)
# by default, we skip the darks, but this can be changed if desired
iraf.imdelete('bias_fid', verify=no)
iraf.zerocombine('@biases',
                 output='bias_fid',
                 combine='median',
                 reject='none',
                 ccdtype='',
                 process=no,
                 delete=no,
                 scale='none')
#iraf.imdelete('dark_fid',verify=no)
#iraf.darkcombine('@darks',output='dark_fid',combine='average',
#                 reject='avsigclip',ccdtype='',process=no,delete=no,
#                 scale='none')

# CALIBRATE ARCS, OBJECT AND FLAT FIELD SPECTRA
# Calibrate object images and flat field images with the task ccdproc.  The
# calibrations applied at this step are the bias level subtraction
# and trimming.  The trim section is hardcoded here.
trimsec = '[200:1850,1:2048]'
print 'Calibrating object images and flats (bias, and trimming)...'
iraf.ccdproc('@objflat',
Пример #35
0
flatcup = str(raw_input('Flat cupula: '))
flati = str(raw_input('Flat interno: '))

bias = str(raw_input('Zero level: '))
linelist = str(raw_input('Lista de linhas (linelists$thar.dat): '))

iraf.imred()
iraf.ccdred()
iraf.specred()
ds9()

iraf.zerocombine.unlearn()
iraf.zerocombine(input=bias,
                 reject='avsigclip',
                 ccdtype='',
                 rdnoise='rdnoise',
                 gain='gain')

iraf.imstat('bias*')
iraf.imstat('Zero')

iraf.imexamine('Zero')

iraf.flatcombine.unlearn()
iraf.flatcombine(input=flatcup,
                 output='Flat',
                 ccdtype='',
                 process=False,
                 subsets=False,
                 rdnoise='rdnoise',
Пример #36
0
def preprocess(path):  #, mode='EABA'):
    """ Pipeline that applies the basic CCD reduction tasks.
    Asks for the path were all files are present, and
    performs Bias combination, Dark combination, Flat
    combination and the inherent substractions
    Works in TORITOS mode, and EABA mode, the latter using UBVRI
    filters.

    Pipeline que aplica una reducción básica a imágenes de tipo CCD
    Realiza combinación de Bias, de Darks, y Flats.
    Luego realiza las restas inherentes
    Funciona en dos modos, TORITOS y EABA, el último usando filtros UBVRI
    """
    bornpath = os.path.abspath('.')
    workdir = os.path.abspath(path)
    biasdir = os.path.join(workdir, "bias")
    flatdir = os.path.join(workdir, "flat")
    darkdir = os.path.join(workdir, "dark")
    scidir = os.path.join(workdir, "science")
    for x in [biasdir, flatdir, darkdir, scidir]:
        print "creating {} directory".format(str(x))
        mkdir(x)
    cd(workdir)
    #-------------------------------------------------------------------------#
    fitslist = []
    for root, dirs, files in os.walk(path, topdown=True):
        for name in fnmatch.filter(files, '*.fit*'):
            fitslist.append(os.path.join(root, name))
    #fitslist = glob.glob('*.fits') + glob.glob('*.fit')
    #load_ccdred()
    #-------------------------------------------------------------------------#
    imagetypes = {}
    for img in fitslist:
        imghead = fits.getheader(img)
        imgty = imghead['IMAGETYP']
        imagetypes[str(img)] = imgty
    #-------------------------------------------------------------------------#
    # now we get the image types in the dictionary imagetypes
    # now we make different lists for different image types
    #-------------------------------------------------------------------------#
    biaslist = []
    sciencelist = []
    flatlist = []
    darklist = []
    #-------------------------------------------------------------------------#
    for k, v in imagetypes.iteritems():
        print k, v
        if v.upper() == 'LIGHT' or v.upper() == 'OBJECT' or v.upper(
        ) == 'LIGHT FRAME':
            #            print str(k)+' is a '+str(v)+' file'
            sciencelist.append(str(k))
        elif v.upper() == 'BIAS' or v.upper() == 'ZERO' or v.upper(
        ) == 'BIAS FRAME':
            #            print str(k)+' is a '+str(v)+' file'
            biaslist.append(str(k))
        elif v.upper() == 'FLAT' or v.upper() == 'FLAT FRAME' or v.upper(
        ) == 'FLAT FIELD':
            #           print str(k)+' is a '+str(v)+' file'
            flatlist.append(str(k))
        elif v.upper() == 'DARK' or v.upper() == 'DARK FRAME':
            #           print str(k)+' is a '+str(v)+' file'
            darklist.append(str(k))
    #-------------------------------------------------------------------------#
    insp = raw_input("Inspeccionar imagenes de correccion con ds9? (si/NO)\n")
    if insp.upper() in ('S', 'Y', 'SI', 'YES'):
        print('\n')
        print(u"""Comienza la inspeccion visual de bias
            ante cualquier anomalia en alguna imagen se la vetará
              Starting visual inspection of Bias frames. If there exists any
              anomalies image will be discarded""")
        for idx, img in enumerate(list(biaslist)):
            I = raw_input("inspeccionar {} mediante ds9? (si/NO)\n".format(
                str(img)))
            if I.upper() in ('S', 'Y', 'SI', 'YES'):
                print(""" inspeccionando {} mediante ds9""".format(str(img)))
                print("\n")
                command = shlex.split('ds9 {}'.format(str(img)))
                subprocess.call(command)
                V = raw_input(" Vetar la imagen? (si/NO)")
                print("\n")
                if V.upper() in ('S', 'Y', 'SI', 'YES'):
                    S = raw_input('Es una imagen de ciencia? (LIGHT)  (SI/no)')
                    print("\n")
                    if S.upper() in ('S', 'Y', 'SI', 'YES'):
                        hdu = fits.open(img, mode='update')
                        hdr = hdu[0].header
                        hdr.set('IMAGETYP', 'LIGHT')
                        hdu.close()
                        sciencelist.append(img)
                    elif S.upper() in ('N', 'NO'):
                        os.rename(img, img + '.vet')
                    biaslist.remove(img)
        #-------------------------------------------------------------------------#
        print('\n')
        print(u"""Comienza la inspeccion visual de flats
            ante cualquier anomalia en alguna imagen se la vetará \n""")
        for idx, img in enumerate(list(flatlist)):
            I = raw_input("inspeccionar {} mediante ds9? (si/NO)\n".format(
                str(img)))
            if I.upper() in ('S', 'Y', 'SI', 'YES'):
                print(""" inspeccionando {} mediante ds9""".format(str(img)))
                print("\n")
                command = shlex.split('ds9 {}'.format(str(img)))
                subprocess.call(command)
                V = raw_input(" Vetar la imagen? (si/NO) ")
                print("\n")
                if V.upper() in ('S', 'Y', 'SI', 'YES'):
                    S = raw_input(
                        'Es una imagen de ciencia? (LIGHT)  (SI/no) ')
                    print("\n")
                    if S.upper() in ('S', 'Y', 'SI', 'YES'):
                        hdu = fits.open(img, mode='update')
                        hdr = hdu[0].header
                        hdr.set('IMAGETYP', 'LIGHT')
                        hdu.close()
                        sciencelist.append(img)
                    elif S.upper() in ('N', 'NO'):
                        os.rename(img, img + '.vet')
                    flatlist.remove(img)
        #-------------------------------------------------------------------------#
        print("\n")
        print(u"""Comienza la inspeccion visual de darks
            ante cualquier anomalia en alguna imagen se la vetará \n""")
        for idx, img in enumerate(list(darklist)):
            I = raw_input("inspeccionar {} mediante ds9? (si/NO)\n".format(
                str(img)))
            if I.upper() in ('S', 'Y', 'SI', 'YES'):
                print(""" inspeccionando {} mediante ds9""".format(str(img)))
                print("\n")
                command = shlex.split('ds9 {}'.format(str(img)))
                subprocess.call(command)
                V = raw_input(" Vetar la imagen? (si/NO)")
                print("\n")
                if V.upper() in ('S', 'Y', 'SI', 'YES'):
                    S = raw_input('Es una imagen de ciencia? (LIGHT)  (SI/no)')
                    print("\n")
                    if S.upper() in ('S', 'Y', 'SI', 'YES'):
                        hdu = fits.open(img, mode='update')
                        hdr = hdu[0].header
                        hdr.set('IMAGETYP', 'LIGHT')
                        hdu.close()
                        sciencelist.append(img)
                    elif S.upper() in ('N', 'NO'):
                        os.rename(img, img + '.vet')
                    darklist.remove(img)
    #-------------------------------------------------------------------------#
    #posee listas de todos los files.
    #comienzo por los bias:
    #primero creo una lista (file) para darle a zerocombine
    write_tolist(biaslist, 'lbias', biasdir)  #, workdir)
    #-------------------------------------------------------------------------#
    iraf.ccdhedit('@lbias', parameter='imagetype', value='zero')
    iraf.zerocombine.unlearn()
    iraf.zerocombine('@lbias', output='Zero.fits')
    #baseflat = [os.path.basename(x) for x  in flatlist]
    #basesci  = [os.path.basename(x) for x  in sciencelist]
    #basedark = [os.path.basename(x) for x  in darklist]
    #-------------------------------------------------------------------------#
    #ahora corrijo las imagenes de flat, dark y objetos por bias.
    load_ccdproc()
    iraf.ccdproc.zero = 'Zero.fits'
    for names in flatlist:
        iraf.ccdproc(names,
                     output=os.path.join(flatdir,
                                         'fz' + os.path.basename(names)))
    for names in darklist:
        iraf.ccdproc(names,
                     output=os.path.join(darkdir,
                                         'dz' + os.path.basename(names)))
    #-------------------------------------------------------------------------#
    #recreate lists of new corrected objects
    flatlist = []
    darklist = []
    for root, dirs, files in os.walk(path, topdown=True):
        for name in fnmatch.filter(files, 'fz*'):
            flatlist.append(os.path.join(flatdir, name))
    for root, dirs, files in os.walk(path, topdown=True):
        for name in fnmatch.filter(files, 'dz*'):
            darklist.append(os.path.join(darkdir, name))
    #-------------------------------------------------------------------------#
    #combino darks tal como hice con los bias
    #-------------------------------------------------------------------------#
    write_tolist(darklist, 'ldark')
    #write_tolist(sciencelist, 'lsci')
    iraf.darkcombine.unlearn()
    iraf.ccdhedit('@ldark', parameter='imagetype', value='dark')
    iraf.darkcombine('@ldark', output='Dark.fits')
    #-------------------------------------------------------------------------#
    # discrimino por filtro los datos. es importante esta parte!
    #.------------------------------------------------------------------------#
    #SE USARON FILTROS?
    filteruse = True
    for v in flatlist:
        try:
            hdr = fits.getheader(v)
            FILTER = hdr['FILTER']
            print 'FILTROS HALLADOS'
        except KeyError:
            filteruse = False
            print 'NO SE USARON FILTROS'
    #---------------------------IF FILTERS USED ------------------------------#
    if filteruse:
        FD = {'U': [], 'B': [], 'V': [], 'R': [], 'I': []}
        for idx, img in enumerate(list(flatlist)):
            hdr = fits.getheader(img)
            FR = hdr['FILTER']
            #creo un diccionario de filtros
            FD[str(FR)].append(img)
        print(FD)
        #-------------------------------------------------------------------------#
        #combino los flats
        #-------------------------------------------------------------------------#
        for k, v in FD.iteritems():
            if not v == []:
                print('writing to list for {} filter'.format(k))
                print(v)
                lname = str(k) + 'flat'
                #print(lname)
                write_tolist(v, lname)
                iraf.flatcombine.unlearn()
                iraf.flatcombine.combine = 'median'
                iraf.ccdhedit('@' + lname, parameter='imagetype', value='flat')
                iraf.flatcombine('@' + lname, output='Flat' + k + '.fits')
        #-------------------------------------------------------------------------#
        SD = {'U': [], 'B': [], 'V': [], 'R': [], 'I': []}
        for idx, img in enumerate(list(sciencelist)):
            hdr = fits.getheader(img)
            FR = hdr['FILTER']
            #creo un diccionario de filtros
            SD[str(FR)].append(img)
        print(SD)
        #-------------------------------------------------------------------------#
        for k, v in SD.iteritems():
            iraf.ccdproc.flatcor = 'yes'
            iraf.ccdproc.darkcor = 'yes'
            iraf.ccdproc.dark = 'Dark.fits'
            for img in v:
                if os.path.isfile('Flat' + k + '.fits'):
                    iraf.ccdproc.flat = 'Flat' + k + '.fits'
                    iraf.ccdproc(img, output='reduced_' + img)
    #----------------------IF NO FILTERS--------------------------------------#
    else:
        lname = 'flist'
        write_tolist(flatlist, lname)
        iraf.flatcombine.unlearn()
        iraf.flatcombine.combine = 'median'
        iraf.ccdhedit('@' + lname, parameter='imagetype', value='flat')
        iraf.flatcombine('@' + lname, output='Flat.fits')
        iraf.ccdproc.flatcor = 'yes'
        iraf.ccdproc.darkcor = 'yes'
        iraf.ccdproc.dark = 'Dark.fits'
        iraf.ccdproc.flat = 'Flat.fits'
        iraf.ccdproc.ccdtype = ''
        for sciname in sciencelist:
            print 'ccdproc of ', sciname, '\n', os.path.join(
                scidir, 'reduced_' + os.path.basename(sciname))
            iraf.ccdproc(sciname, output=os.path.join(scidir,\
                            'reduced_'+os.path.basename(sciname)))
    #[os.remove(x) for x in fitslist]
    aux = glob.glob('sz*.fits') + glob.glob('dz*.fits') + glob.glob('fz*.fits')
    [os.remove(x) for x in aux]
    aux = glob.glob('reduced*.fits')
    [os.rename(x, x[10:-4].upper() + x[-4:].lower()) for x in aux]
    #-------------------------------------------------------------------------#
    print(u""" Imágenes reducidas exitosamente APARENTEMENTE,
            chequea todo obsesivamente, porque ni el desarrollador de
            esto confia en que ande bien \n
            """)
    #-------------------------------------------------------------------------#
    cd(bornpath)
    #print sys.argv[:]
    return ()
Пример #37
0
 def zerocombine(self):
     '''Combine all of the bias images into a single bias flat'''
     iraf.zerocombine()
Пример #38
0
# <codecell>

cd ~/Documents/HERMES/reductions/iraf/bias/140821/1

# <codecell>

biases = glob.glob('*.fits')`

# <codecell>

biases = ",".join(biases)

# <codecell>

iraf.zerocombine(input=biases, output='masterbias', combine='median', reject='none', ccdtype='',Stdout="/dev/null")

# <codecell>

cd ~/Documents/HERMES/reductions/iraf/HD1581/0_20aug/1/

# <codecell>

a = pf.open('20aug10052.fits')
a[0].header['RUNCMD']

# <headingcell level=3>

# FLAT

# <codecell>