Beispiel #1
0
def avgFWHM(image,coords):
	'''use the iraf task psfmeasure to compute the average fwhm of a few stars
	INPUT: 
	image: the fits image you want to measure the fwhm for
	coords: a text file listing the x,y pixel coordinates of stars in image to use to compute the fwhm.
			I'd recommend choosing ~10, bright stars, evenly sampled across the frame.
	OUTPUT: a string of the average fwhm
		also your graphics terminal will open up with a gross iraf graphics terminal
		If you are interested you can look at it to see info like elipticity and average fwhm of the image.
		
	If you run this in batch mode and consistently are seeing fwhm values >~ 5, it might mean you need to choose different
	stars in your coords file, or check that the coordinates are accurate.
	'''

	#we need to create a text file that has the letter 'q' in it
	#this gets fed to psfmeasure as the graphcur input. it forces iraf to quit out of the graphics terminal
	#this way you can compute the fwhm with out the user having to click out of the graphics terminal anything
	#I tried to disable to graphics output alltogether, but couldn't. so we use this work around instead
	with open('graphcur','w') as f:
		f.write('q')

	#use the iraf task psfmeasure to get the fwhm
	#the output is not machine readable friendly, but basically we are after the last thing it prints out
	#this is the avg fwhm measured
	stdOutput=iraf.psfmeasure(image,imagecur=coords,display='no', graphcur='graphcur',Stdout=1, logfile='')
	FWHM=stdOutput[-1].split()[-1]

	#clean up the graphcur textfile we made
	iraf.delete('graphcur')

	return FWHM
def makeArcDark(arcdarklist, arcdark, calflat, over, log):
    """"Prepare with iraf.nfprepare and combine the daytime arc darks.

    Processing with NFPREPARE will rename the data extension and add
    variance and data quality extensions. By default (see NSHEADERS)
    the extension names are SCI for science data, VAR for variance, and
    DQ for data quality (0 = good). Generation of the data quality
    plane (DQ) is important in order to fix hot and dark pixels on the
    NIFS detector in subsequent steps in the data reduction process.
    Various header keywords (used later) are also added in NFPREPARE.
    NFPREPARE will also add an MDF file (extension MDF) describing the
    NIFS image slicer pattern and how the IFU maps to the sky field.

    """

    # Update arc dark frames with mdf offset value and generate variance and data quality extensions.
    for image in arcdarklist:
        image = str(image).strip()
        if over:
            iraf.delete("n"+image+".fits")
        iraf.nfprepare(image, rawpath='.', shiftimage="s"+calflat, \
                       bpm=sflat_bpm,fl_vardq='yes',fl_corr='no',fl_nonl='no', logfile=log)
    arcdarklist = checkLists(arcdarklist, '.', 'n', '.fits')

    # Combine arc dark frames, "n"+image+".fits". Output combined file will have the name of the first arc dark file.
    if over:
        iraf.delete("gn"+arcdark+".fits")
    if len(arcdarklist) > 1:
        iraf.gemcombine(listit(arcdarklist,"n"),output="gn"+arcdark, fl_dqpr='yes',fl_vardq='yes',masktype="none",logfile=log)
    else:
        iraf.copy('n'+arcdark+'.fits', 'gn'+arcdark+'.fits')

    # Put the name of the combined and prepared arc dark frame "gn"+arcdark into a text
    # file called arcdarkfile to be used by the pipeline later.
    open("arcdarkfile", "w").write("gn"+arcdark)
def ronchi(ronchilist, ronchiflat, calflat, over, flatdark, log):
    """Establish Spatial-distortion calibration with nfsdist.

    NFSDIST uses the information in the "Ronchi" Calibration images
    to calibrate the spatial dimension of the NIFS IFU field. The
    Ronchi frame is a dispersed flat field image with a slit-mask
    in the field so that the illumination on the IFU is in a
    pattern of ~10 different slitlets that are stacked in the
    y-dimension on the field. Proper alignment of the slits across
    the image slicer pattern can be used for spatial rectification
    of the on-sky science data. The spatial solution determined by
    NFSDIST is linked to the science data in NFFITCOORDS.

    """

    # Update ronchi flat frames with offset value and generate variance and data quality extensions.
    # Output: "n"+image+".fits".
    for image in ronchilist:
        image = str(image).strip()
        if over:
            iraf.delete("n"+image+'.fits')
        iraf.nfprepare(image,rawpath=".", shiftimage="s"+calflat, \
                       bpm="rgn"+calflat+"_sflat_bpm.pl", fl_vardq="yes",fl_corr="no",fl_nonl="no", \
                       logfile=log)
    ronchilist = checkLists(ronchilist, '.', 'n', '.fits')

    # Combine nfprepared ronchi flat images "n"+image+".fits". Output: combined file will
    # have the name of the first ronchi flat file, "gn"+ronchiflat+".fits".
    if over:
        iraf.delete("gn"+ronchiflat+".fits")
    if len(ronchilist) > 1:
        iraf.gemcombine(listit(ronchilist,"n"),output="gn"+ronchiflat,fl_dqpr="yes", \
                               masktype="none",fl_vardq="yes",logfile=log)
    else:
        iraf.copy("n"+ronchiflat+".fits","gn"+ronchiflat+".fits")

    # NSREDUCE combined ronchi frame, "gn"+ronchiflat+".fits", to extract the slices into unique MEF extensions and
    # apply an approximate wavelength calibration to each slice. Output: "rgn"+ronchiflat+".fits".
    if over:
        iraf.delete("rgn"+ronchiflat+".fits")
    iraf.nsreduce("gn"+ronchiflat, outpref="r", dark="rgn"+flatdark+"_dark", flatimage="rgn"+calflat+"_flat", fl_cut="yes", fl_nsappw="yes", fl_flat="yes", fl_sky="no", fl_dark="yes", fl_vardq="no", logfile=log)

    if os.path.exists("ronchifile"):
        if over:
            iraf.delete("ronchifile")
            iraf.delete("rgn"+ronchiflat)
        else:
            print "\nOutput file exists and -over not set - ",\
            "not performing ronchi calibration with iraf.nfsdist.\n"
            return
    else:
        # Determine the spatial distortion correction. Output: overwrites "rgn"+ronchiflat+".fits" and makes
        # changes to files in /database directory.
        iraf.nfsdist("rgn"+ronchiflat,fwidth=6.0, cradius=8.0, glshift=2.8, minsep=6.5, thresh=2000.0, nlost=3, fl_inter='no',logfile=log)

    # Put the name of the spatially referenced ronchi flat "rgn"+ronchiflat into a
    # text file called ronchifile to be used by the pipeline later. Also associated files
    # are in the "database/" directory.

    open("ronchifile", "w").write("rgn"+ronchiflat)
Beispiel #4
0
def makeArcDark(arcdarklist, arcdark, calflat, over, log):
    # combine the daytime arc darks
    for image in arcdarklist:
        image = str(image).strip()
        if over:
            iraf.delete("n" + image + ".fits")
        iraf.nfprepare(image,
                       rawpath='./',
                       shiftimage="s" + calflat,
                       bpm="rgn" + calflat + "_sflat_bpm.pl",
                       fl_vardq='yes',
                       fl_corr='no',
                       fl_nonl='no',
                       logfile=log)
    arcdarklist = checkLists(arcdarklist, '.', 'n', '.fits')

    if over:
        iraf.delete("gn" + arcdark + ".fits")
    if len(arcdarklist) > 1:
        iraf.gemcombine(listit(arcdarklist, "n"),
                        output="gn" + arcdark,
                        fl_dqpr='yes',
                        fl_vardq='yes',
                        masktype="none",
                        logfile=log)
    else:
        iraf.copy('n' + arcdark + '.fits', 'gn' + arcdark + '.fits')

    open("arcdarkfile", "w").write("gn" + arcdark)
Beispiel #5
0
 def daophot(self, framenum, coords, outfile, apertures, verbose = "no"):
     """
     Aperture photometry of stars in the coords file using IRAF PHOT routine.
     
     Parameters
     ----------
     framenum : int
         Frame number in the image cube to perform aperture photometry on.
         
     coords : string
         Text file with coordinate of the stars
         
     outfile : string
         Text file to which photometry results are written
     
     Returns
     -------
     None
     """
     # load iraf packages
     self.setiraf()
     
     iraf.delete(outfile)
     iraf.phot(image = self.sci_file + "[,," + str(framenum) + "]", coords = coords, output = outfile, fwhmpsf = self.fwhmpsf, sigma = self.sigma, readnoise = self.readnoise, epadu = self.epadu, exposure = self.exposure, calgorithm = self.calgorithm, cbox = self.cbox, maxshift = self.maxshift, salgorithm = self.salgorithm, annulus = self.annulus, dannulus = self.dannulus, apertures = apertures, zmag = self.zmag, interactive = "no", verify = "no", verbose = verbose)        
     
     return
Beispiel #6
0
    def daophot(self, framenum, coords, outfile, apertures, verbose = "no"):
        """
        Aperture photometry of stars in the coords file using IRAF PHOT routine.

        Parameters
        ----------
        framenum : int
            Frame number in the image cube to perform aperture photometry on.

        coords : string
            Text file with coordinate of the stars

        outfile : string
            Text file to which photometry results are written

        Returns
        -------
        None
        """
        # load iraf packages
        self.setiraf()

        iraf.delete(outfile)
        iraf.phot(image = self.sci_file + "[,," + str(framenum) + "]", coords = coords, output = outfile, fwhmpsf = self.fwhmpsf, sigma = self.sigma, readnoise = self.readnoise, epadu = self.epadu, exposure = self.exposure, calgorithm = self.calgorithm, cbox = self.cbox, maxshift = self.maxshift, salgorithm = self.salgorithm, annulus = self.annulus, dannulus = self.dannulus, apertures = apertures, zmag = self.zmag, interactive = "no", verify = "no", verbose = verbose)

        return
Beispiel #7
0
def transform(objlist, log, over):
    """Apply the transformation determined in iraf.nffitcoords with
    iraf.nstransform. Output: -->tfbrsgn

    NSTRANSFORM - Spatially rectify and wavelength calibrate data.

    NFTRANSFORM applies the wavelength solution found by
    NSWAVELENGTH and the spatial correction found by NFSDIST,
    aligning all the IFU extensions consistently onto a common
    coordinate system. The output of this routine is still in 2D
    format, with each of the IFU slices represented by its own data
    extension.

    """

    for frame in objlist:
        frame = str(frame).strip()
        if os.path.exists("tfbrsn" + frame + ".fits"):
            if over:
                iraf.delete("tfbrsn" + frame + ".fits")
            else:
                logging.info(
                    "Output file exists and -over not set - skipping transform_list"
                )
                continue
        iraf.nstransform("fbrsn" + frame, logfile=log)
Beispiel #8
0
def psffit2(img, fwhm, psfstars, hdr, _datamax, psffun='gauss',  fixaperture=False):
    ''' 
    giving an image, a psffile,  calculate the magnitudes of strs in the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'  
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.photpars.zmag = zmag
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord
    iraf.delete("_als2,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img + '[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_als2', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als2", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Beispiel #9
0
def psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss',  fixaperture=False):
    ''' 
    giving an image, a psffile,  calculate the magnitudes of strs in the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'  
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.photpars.zmag = zmag
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord
    iraf.delete("_als2,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img + '[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_als2', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als2", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Beispiel #10
0
def copyImage(input, output, over):
    # copy an image (used to add the correct prefix)

    if os.path.exists(output):
        if over:
            iraf.delete(output)
        else:
            print "Output file exists and -over not set - skipping copy_ima"
            return
    iraf.copy('n' + input[0] + '.fits', output)
Beispiel #11
0
def psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss', fixaperture=False):
    import agnkey

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric
    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = agnkey.util.readhdr(img+'.fits')
        _pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    _center='no'
    iraf.fitskypars.annulus = a4
    iraf.fitskypars.dannulus = a4
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.wsannul = int(a4)
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    #    iraf.photpars.apertures = '%d,%d,%d'%(a2,a3,a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = agnkey.util.readkey3(hdr, 'ron')
    iraf.datapars.epadu = agnkey.util.readkey3(hdr, 'gain')
    iraf.datapars.exposure = 'exptime'  #agnkey.util.readkey3(hdr,'exptime')
    iraf.datapars.airmass = 'airmass'
    iraf.datapars.filter = 'filter2'
    iraf.centerpars.calgori = 'gauss'
    iraf.centerpars.cbox = 1
    iraf.daopars.recenter = _center
    iraf.photpars.zmag = zmag

    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = int(a4)
    iraf.daopars.wsannul = int(a4)
    iraf.daopars.recenter = _center
    iraf.daopars.varorder = varord
    iraf.delete("_als,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img+'[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img+'[0]', '_psf.grp', img + '.psf', '_als', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Beispiel #12
0
def psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss', fixaperture=False):
    import agnkey

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric
    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = agnkey.util.readhdr(img+'.fits')
        _pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    _center='no'
    iraf.fitskypars.annulus = a4
    iraf.fitskypars.dannulus = a4
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.wsannul = int(a4)
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    #    iraf.photpars.apertures = '%d,%d,%d'%(a2,a3,a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = agnkey.util.readkey3(hdr, 'ron')
    iraf.datapars.epadu = agnkey.util.readkey3(hdr, 'gain')
    iraf.datapars.exposure = 'exptime'  #agnkey.util.readkey3(hdr,'exptime')
    iraf.datapars.airmass = 'airmass'
    iraf.datapars.filter = 'filter2'
    iraf.centerpars.calgori = 'gauss'
    iraf.centerpars.cbox = 1
    iraf.daopars.recenter = _center
    iraf.photpars.zmag = zmag

    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = int(a4)
    iraf.daopars.wsannul = int(a4)
    iraf.daopars.recenter = _center
    iraf.daopars.varorder = varord
    iraf.delete("_als,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img+'[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img+'[0]', '_psf.grp', img + '.psf', '_als', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Beispiel #13
0
def transform(objlist, log, over):
    # apply the transformation determined in the nffitcoords step

    for image in objlist:
        image = str(image).strip()
        if os.path.exists("tfbrgn" + image + ".fits"):
            if over:
                iraf.delete("tfbrgn" + image + ".fits")
            else:
                print "Output file exists and -over not set - skipping transform_list"
                continue
        iraf.nstransform("fbrgn" + image, logfile=log)
Beispiel #14
0
def copyImage(input, output, over):
    """Copy a frame (used to add the correct prefix when skipping steps)."""

    if os.path.exists(output):
        if over:
            iraf.delete(output)
        else:
            logging.info(
                "Output file exists and -over not set - skipping copy_ima")
            return

    iraf.copy('n' + input[0] + '.fits', output)
Beispiel #15
0
def create_coadd_img(qd,
                     targets,
                     dbFile,
                     data_dir,
                     prefix='mrg',
                     overwrite=True):
    '''
    despite the fact that it looks like this can be run from outside the
    raw directory, it can't be.
    
    Caveats:
    * It takes a lot of patience and trial-and-error tweaking of parameters 
        to get good results
    * There is little control over sky background
    * The output image is no bigger than the first (reference) image, 
        rather than the union of the image footprints
    
    '''
    ## Co-add the images, per position and filter.
    print(" -- Begin image co-addition --")
    cur_dir = os.getcwd()
    iraf.chdir(data_dir)

    # Use primarily the default task parameters.
    gemtools.imcoadd.unlearn()
    coaddFlags = {
        'fwhm': 3,
        'datamax': 6.e4,
        'geointer': 'nearest',
        'logfile': 'imcoaddLog.txt'
    }

    filters = ['Ha', 'HaC', 'SII', 'r', 'i']
    for f in filters:
        print "  - Co-addding science images in filter: {}".format(f)
        qd['Filter2'] = f + '_G%'
        for t in targets:
            qd['Object'] = t + '%'
            print "  - Co-addding science images for position: {}".format(t)
            outImage = t + '_' + f + '.fits'
            coAddFiles = fileSelect.fileListQuery(
                dbFile, fileSelect.createQuery('sciImg', qd), qd)
            if len(coAddFiles) > 1:
                gemtools.imcoadd(','.join(prefix + str(x) for x in coAddFiles),
                                 outimage=outImage,
                                 **coaddFlags)

    iraf.delete("*_trn*,*_pos,*_cen")
    iraf.imdelete("*badpix.pl,*_med.fits,*_mag.fits")
    #iraf.imdelete ("mrgS*.fits")

    print("=== Finished Calibration Processing ===")
    iraf.chdir(cur_dir)
Beispiel #16
0
def run_fitparams(weight=True):
    if weight: 
        weighting = 'photometric'
    else:
        weighting = 'uniform'

    ir.delete('standards.transParams')
    ir.digiphot()
    ir.photcal()
    ir.unlearn('fitparams')
    ir.fitparams('standards.instMag', 'photcal_ukirt_faint.dat',
                 'standards.config', 'standards.transParams',
                 weighting=weighting)
Beispiel #17
0
def linefitAuto(spectrum, band):
    """automatically fit Lorentz profiles to lines defined in existing cur* files
    Go to x position in cursor file and use space bar to find spectrum at each of those points
    """

    specpos = iraf.bplot(images=spectrum+'[SCI,1]', cursor='cur'+band, Stdout=1, StdoutG='/dev/null')
    specpose = str(specpos).split("'x,y,z(x):")
    nextcur = 'nextcur'+band+'.txt'
    # Write line x,y info to file containing Lorentz fitting commands for bplot
    write_line_positions(nextcur, specpos)
    iraf.delete('final_tel_no_hlines_no_norm.fits',ver="no",go_ahead='yes',Stderr='/dev/null')
    # Fit and subtract Lorentz profiles. Might as well write output to file.
    iraf.bplot(images=spectrum+'[sci,1]',cursor='nextcur'+band+'.txt', new_image='final_tel_no_hlines_no_norm', overwrite="yes",StdoutG='/dev/null',Stdout='Lorentz'+band)
Beispiel #18
0
def hLineCorrection(combined_extracted_1d_spectra, grating, path, hlineinter, tempInter, hline_method, log, over, airmass_std=1.0):
    """
    Remove hydrogen lines from the spectrum of a telluric standard,
    using a model of vega's atmosphere.

    """

    # File for recording shift/scale from calls to "telluric"
    telluric_shift_scale_record = open('telluric_hlines.txt', 'w')

    # Remove H lines from standard star correction spectrum
    no_hline = False
    if os.path.exists("final_tel_no_hlines_no_norm.fits"):
        if over:
            iraf.delete("final_tel_no_hlines_no_norm.fits")
        else:
            no_hline = True
            logging.info("Output file exists and -over- not set - skipping H line removal")

    if hline_method == "vega" and not no_hline:
        vega(combined_extracted_1d_spectra, grating, path, hlineinter, telluric_shift_scale_record, log, over)

    #if hline_method == "linefitAuto" and not no_hline:
    #    linefitAuto(combined_extracted_1d_spectra, grating)

    # Disabled and untested because interactive scripted iraf tasks are broken...
    #if hline_method == "linefitManual" and not no_hline:
    #    linefitManual(combined_extracted_1d_spectra+'[sci,1]', grating)

    #if hline_method == "vega_tweak" and not no_hline:
        #run vega removal automatically first, then give user chance to interact with spectrum as well
    #    vega(combined_extracted_1d_spectra,grating, path, hlineinter, telluric_shift_scale_record, log, over)
    #    linefitManual("final_tel_no_hlines_no_norm", grating)

    #if hline_method == "linefit_tweak" and not no_hline:
        #run Lorentz removal automatically first, then give user chance to interact with spectrum as well
    #    linefitAuto(combined_extracted_1d_spectra,grating)
    #    linefitManual("final_tel_no_hlines_no_norm", grating)

    if hline_method == "none" and not no_hline:
        #need to copy files so have right names for later use
        iraf.imcopy(input=combined_extracted_1d_spectra+'[sci,'+str(1)+']', output="final_tel_no_hlines_no_norm", verbose='no')
    # Plot the non-hline corrected spectrum and the h-line corrected spectrum.
    uncorrected = astropy.io.fits.open(combined_extracted_1d_spectra+'.fits')[1].data
    corrected = astropy.io.fits.open("final_tel_no_hlines_no_norm.fits")[0].data
    if hlineinter or tempInter:
        plt.title('Before and After HLine Correction')
        plt.plot(uncorrected)
        plt.plot(corrected)
        plt.show()
Beispiel #19
0
def finalMergeCubes(mergeType, over):
    """
    Merge final merged cubes from all observations.
    """
    # Load data from the previous step.
    path = os.getcwd()
    with open('mergedInfo.txt') as data_file:
        mergedData = json.load(data_file)
    mergedCubes = mergedData['mergedCubes']
    Merged = mergedData['Merged']

    if len(mergedCubes)>1:
        os.chdir(Merged)
        iraffunctions.chdir(Merged)
        gratlist = []
        for i in range(len(mergedCubes)):
            cubeheader = astropy.io.fits.open(mergedCubes[i])
            grat = cubeheader[0].header['GRATING']
            gratlist.append(grat)
        print "gratlist is: ", gratlist
        # TODO(nat): right now we do more final merges here than we have to. Eg, if there are three H
        # grating directories in gratlist, we will do a final merge three times. We should only be doing this once!
        # Right now it is only a problem when overwrite is turned on but it should still be fixed.
        for n in range(len(gratlist)): # For each unique grating
            # Grab the indices of the cubes associated with that grating.
            indices = [k for k, x in enumerate(gratlist) if x==gratlist[n]]
            newcubelist = []
            for ind in indices:
                newcubelist.append(mergedCubes[ind])
            print newcubelist
            # Do some housekeeping before the final cube merging.
            resizeAndCenterCubes(newcubelist, over)
            makeWavelengthOffsets(newcubelist, grat)
            for i in range(len(newcubelist)):
                # Build an input string containing all the cubes to combine.
                if i==0:
                    inputstring = newcubelist[i]+'[1]'
                else:
                    inputstring += ','+newcubelist[i]+'[1]'
            if os.path.exists('temp_merged'+gratlist[n][0]+'.fits'):
                if over:
                    iraf.delete('temp_merged'+gratlist[n][0]+'.fits')
                    iraf.imcombine(inputstring, output = 'temp_merged'+gratlist[n][0]+'.fits', combine = mergeType, offsets = 'waveoffsets'+grat[0]+'.txt')
                    iraf.fxcopy(input=newcubelist[0]+'[0], temp_merged'+gratlist[n][0]+'.fits', output = 'TOTAL_merged'+gratlist[0][0]+'.fits')
                else:
                    logging.info('Output exists and -over- not set - skipping final cube merge')
            else:
                iraf.imcombine(inputstring, output = 'temp_merged'+gratlist[n][0]+'.fits', combine = mergeType, offsets = 'waveoffsets'+grat[0]+'.txt')
                iraf.fxcopy(input=newcubelist[0]+'[0], temp_merged'+gratlist[n][0]+'.fits', output = 'TOTAL_merged'+gratlist[n][0]+'.fits')
    os.chdir(path)
Beispiel #20
0
    def checkMDF(self, rawMDF, flatList, **kwargs):

        # copy raw MDF to local directory
        mdf = rawMDF
        if not os.path.exists(mdf):
            print "\nFETCHING RAW MDF"
            mdfPath = "/Users/roedigerj/anaconda/envs/astroconda/iraf_extern/gemini/gmos/data/"
            rawMDF = mdfPath + rawMDF
            shutil.copy(rawMDF, "./")

        # TODO: check that flatList is not empty

        # grab the name of a single flat to reduce
        im = utils.getImPaths(flatList)[0]

        print "\nCHECKING MDF WITH " + im

        # repeat the following steps until the MDF deemed correct
        while True:

            # do basic reduction of selected exposure
            _ = self.reduceIm(im, fl_inter="no", fl_gscrrej="no", \
             fl_extract="no", fl_wavtran="no", fl_skysub="no", \
             fl_fluxcal="no", mdffile=mdf, mdfdir="./", verbose="no", \
             **kwargs)

            # extract spectrum to check match between MDF and IFU flat
            logFile = ""
            if "logfile" in kwargs.keys():
                logFile = kwargs["logfile"]
            iraf.delete("database/aperg" + im[:im.find(".")] + "*",
                        verify="yes")
            self.extractSpec("rg" + im, fl_inter="yes", logfile=logFile, \
             verbose="no")

            # TODO: remove any extracted files

            # call tcalc to fix MDF (if necessary)
            fixMDF = bool(input("\nDoes MDF need fixing? (True/False): "))
            if fixMDF:
                fiber = input("Fiber to fix: ")
                flag = input("Flag value: ")
                iraf.tcalc(
                    mdf, "BEAM", "if NO == " + str(fiber) + "then " +
                    str(flag) + " else BEAM")
            else:
                break

        return
Beispiel #21
0
def combineImages(inlist, out, log, over):
    # gemcombine a list of images
    print inlist
    if os.path.exists(out + ".fits"):
        if over:
            iraf.delete(out + ".fits")
        else:
            print "Output file exists and -over not set - skipping combine_ima"
            return
    iraf.gemcombine(listit(inlist, "n"),
                    output=out,
                    fl_dqpr='yes',
                    fl_vardq='yes',
                    masktype="none",
                    logfile=log)
def run_fitparams(weight=True):
    if weight:
        weighting = 'photometric'
    else:
        weighting = 'uniform'

    ir.delete('standards.transParams')
    ir.digiphot()
    ir.photcal()
    ir.unlearn('fitparams')
    ir.fitparams('standards.instMag',
                 'photcal_ukirt_faint.dat',
                 'standards.config',
                 'standards.transParams',
                 weighting=weighting)
Beispiel #23
0
def saveset_peakcoords(basename, threshold=100, hwhm=3):
    """
  Attempts to find the peak of each saveset in
  an already tprepared image.

  Parameters:
  -----------
  basename : string
    Everything before the .fits
  threshold : number
    Detection threshold for images.imcoords.starfind
    in counts

  Returns:
  --------
  c : numpy.array
    Row stacked coordinates for each of the savesets
  """
    #deleting files from previous runs
    iraf.delete('*fits*obj*')
    iraf.delete('images_ext.lst')
    iraf.images()
    iraf.imcoords()

    exts = len(pf.open(basename + '.fits')) - 1

    f = open('images_ext.lst', 'w')
    for i in range(exts):
        f.write(basename + '.fits[' + str(i + 1) + '][*,*,3]\n')
    f.close()

    iraf.starfind(r'@images_ext.lst', 'default', hwhm, threshold)
    a = [basename + '.fits' + str(i + 1) + '.obj.1' for i in range(exts)]
    b = [loadtxt(a[i]) for i in range(exts) if len(loadtxt(a[i])) != 0]
    c = row_stack(b)

    f = open(a[0], 'r')
    d = f.readlines()
    f.close()

    header = [i for i in d if i[0] == '#']

    f = open(basename + '_coords.dat', 'w')
    for i in header:
        f.write(i)
    f.write('\n')

    savetxt(f, c, fmt='%.3f')

    f.close()

    iraf.delete('*fits*obj*')
    iraf.delete('images_ext.lst')

    return c
Beispiel #24
0
def combineImages(inlist, out, log, over):
    """Gemcombine multiple frames. Output: -->gn."""

    if os.path.exists(out + ".fits"):
        if over:
            iraf.delete(out + ".fits")
        else:
            logging.info(
                "Output file exists and -over not set - skipping combine_ima")
            return

    iraf.gemcombine(listit(inlist, "n"),
                    output=out,
                    fl_dqpr='yes',
                    fl_vardq='yes',
                    masktype="none",
                    combine="median",
                    logfile=log)
Beispiel #25
0
 def daocog(self, tolerance = 0.01):
     """
     Curve of growth to determine nominal aperture for photometry using DAOPHOT.
     
     Parameters
     ----------
     tolerance : float
         Magnitude difference tolerance between different apertures
     
     Returns
     -------
     aperture : float
         Nominal aperture radius for photmetry
     """
     # load iraf packages
     self.setiraf()
     
     # Randomly peform curve of growth on 5 frames
     framenum = np.random.randint(1, self.nframes, 5)
     
     apertures = np.linspace(2,20,19)
     
     # Iterate through the frames and determine nominal aperture
     nom_aper = np.zeros(5, dtype = np.float32)
     cnt = 0
     for val in framenum:
         outfile = self.sci_file.replace(".fits", "." + str(val) + ".cog.phot.1")
         iraf.delete(outfile)
         self.daophot(val, self.coords, outfile, apertures = "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20")
         mags = iraf.pdump(outfile, "mag", "yes", Stdout = 1)
         mags = [mag if mag != 'INDEF' else 30.0 for mag in mags]
         mags_arr = np.array(mags[1].split(),dtype = np.float32)
         mags_diff = np.diff(mags_arr)
         idx = np.where((np.abs(mags_diff) < 0.01) & (np.abs(mags_diff) != 0.0))
         if len(idx[0]) != 0:
             nom_aper[cnt] = apertures[idx[0][0]]
         else:
             nom_aper[cnt] = 10.0
         cnt += 1
         iraf.delete(outfile)
         
     return np.median(nom_aper)
Beispiel #26
0
    def daocog(self, tolerance = 0.01):
        """
        Curve of growth to determine nominal aperture for photometry using DAOPHOT.

        Parameters
        ----------
        tolerance : float
            Magnitude difference tolerance between different apertures

        Returns
        -------
        aperture : float
            Nominal aperture radius for photmetry
        """
        # load iraf packages
        self.setiraf()

        # Randomly peform curve of growth on 5 frames
        framenum = np.random.randint(1, self.nframes, 5)

        apertures = np.linspace(2,20,19)

        # Iterate through the frames and determine nominal aperture
        nom_aper = np.zeros(5, dtype = np.float32)
        cnt = 0
        for val in framenum:
            outfile = self.sci_file.replace(".fits", "." + str(val) + ".cog.phot.1")
            iraf.delete(outfile)
            self.daophot(val, self.coords, outfile, apertures = "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20")
            mags = iraf.pdump(outfile, "mag", "yes", Stdout = 1)
            mags = [mag if mag != 'INDEF' else 30.0 for mag in mags]
            mags_arr = np.array(mags[1].split(),dtype = np.float32)
            mags_diff = np.diff(mags_arr)
            idx = np.where((np.abs(mags_diff) < tolerance) & (np.abs(mags_diff) != 0.0))
            if len(idx[0]) != 0:
                nom_aper[cnt] = apertures[idx[0][0]]
            else:
                nom_aper[cnt] = 10.0
            cnt += 1
            iraf.delete(outfile)

        return np.median(nom_aper)
Beispiel #27
0
def makeCube(pre, scienceFrameList, log, over):
    """ Reformat the data into a 3-D datacube using iraf.nifcube. Output: -->ctfbrsgn.

    NIFCUBE - Construct 3D NIFS datacubes.

    NIFCUBE takes input from data output by either NFFITCOORDS or
    NFTRANSFORM and converts the 2D data images into data cubes
    that have coordinates of x, y, lambda.

    """
    for frame in scienceFrameList:
        if os.path.exists("c" + pre + frame + ".fits"):
            if over:
                iraf.delete("c" + pre + frame + ".fits")
            else:
                logging.info(
                    "Output file exists and -over not set - skipping make_cube_list"
                )
                continue
        iraf.nifcube(pre + frame, outcubes='c' + pre + frame, logfile=log)
Beispiel #28
0
def makeCube(pre, objlist, tel, obsDir, log, over):
    # reformat the data into a 3-D datacube
    os.chdir(obsDir)
    for image in objlist:
        if os.path.exists("c" + pre + image + ".fits"):
            if over:
                iraf.delete("c" + pre + image + ".fits")
            else:
                print "Output file exists and -over not set - skipping make_cube_list"
                continue
        if tel:
            iraf.nifcube(pre + image, outcubes='c' + pre + image, logfile=log)
            hdulist = pyfits.open('c' + pre + image + '.fits', mode='update')
            #            hdulist.info()
            exptime = hdulist[0].header['EXPTIME']
            cube = hdulist[1].data
            gain = 2.8
            cube_calib = cube / (exptime * gain)
            hdulist[1].data = cube_calib
            hdulist.flush()
        else:
            iraf.nifcube(pre + image, outcubes='c' + pre + image, logfile=log)
Beispiel #29
0
def wavecal(arc, log, over):
    # Determine the wavelength of the observation and set the arc coordinate
    # file.  If the user wishes to change the coordinate file to a different
    # one, they need only to change the "clist" variable to their line list
    # in the coordli= parameter in the nswavelength call.

    if os.path.exists("w" + arc + ".fits"):
        if over:
            iraf.delete("w" + arc + ".fits")
        else:
            print "Output file exists and -over not set - skipping wavecal"
            return

    hdulist = pyfits.open(arc + ".fits")
    band = hdulist[0].header['GRATING'][0:1]

    if band == "Z":
        clist = "nifs$data/ArXe_Z.dat"
        my_thresh = 100.0
    elif band == "K":
        clist = "gnirs$data/argon.dat"
        my_thresh = 50.0
    else:
        clist = "gnirs$data/argon.dat"
        my_thresh = 100.0

    iraf.nswavelength(arc,
                      coordli=clist,
                      nsum=10,
                      thresho=my_thresh,
                      trace='yes',
                      fwidth=2.0,
                      match=-6,
                      cradius=8.0,
                      fl_inter='no',
                      nfound=10,
                      nlost=10,
                      logfile=log)
Beispiel #30
0
def makeTelluric(objlist, log, over):
    # Extracts 1-D spectra and combines them
    # This step is currently only done interactively

    for image in objlist:
        image = str(image).strip()
        if os.path.exists("xtfbrgn" + image + ".fits"):
            if over:
                iraf.delete("xtfbrgn" + image + ".fits")
            else:
                print "Output file exists and -over not set - skipping extraction in make_telluric"
                continue
        diam = 0.5

        iraf.nfextract("tfbrgn" + image,
                       outpref="x",
                       diameter=diam,
                       fl_int='yes',
                       logfile=log)

    #combine all the 1D spectra to one final output file
    telluric = str(objlist[0]).strip()
    if os.path.exists("gxtfbrgn" + telluric + ".fits"):
        if over:
            iraf.delete("gxtfbrgn" + telluric + ".fits")
        else:
            print "Output file exists and -over not set - skipping gemcombine in make_telluric"
            return
    iraf.gemcombine(listit(objlist, "xtfbrgn"),
                    output="gxtfbrgn" + telluric,
                    statsec="[*]",
                    combine="median",
                    logfile=log,
                    masktype="none",
                    fl_vardq="yes")

    # Write the name of the final file to a standard file in the telluric directory
    open("telluricfile", "w").write("gxtfbrgn" + telluric)
Beispiel #31
0
def avgFWHM(image, coords):
    """
    use the iraf task psfmeasure to compute the average fwhm of a few stars
    INPUT: 
    image: the fits image you want to measure the fwhm for
    coords: a text file listing the x,y pixel coordinates of stars in image to use to compute the fwhm.
    I'd recommend choosing ~10, bright stars, evenly sampled across the frame.
    OUTPUT: a string of the average fwhm also your graphics terminal will open up with a gross iraf graphics terminal
    If you are interested you can look at it to see info like ellipticity and average fwhm of the image.
    If you run this in batch mode and consistently are seeing fwhm values >~ 5, it might mean you need to choose
    different stars in your coords file, or check that the coordinates are accurate.
    """

    # we need to create a text file that has the letter 'q' in it
    # this gets fed to psfmeasure as the graphcur input. it forces iraf to quit out of the terminal
    # this way you can compute the fwhm with out the user having to click on anything
    with open('graphcur', 'w') as f:
        f.write('q')

    # use the iraf task psfmeasure to get the fwhm
    # the output is not machine readable friendly, but basically we are after the last thing it prints out
    # this is the avg fwhm measured
    stdOutput = iraf.psfmeasure(image,
                                imagecur=coords,
                                display='no',
                                graphcur='graphcur',
                                Stdout=1,
                                logfile='')
    #FWHM = stdOutput[-1].split()[-1]

    psfstarlines = stdOutput[4:-2]
    linessplitted = [x.split() for x in psfstarlines]
    FWHM = np.median(np.array([x[3] for x in linessplitted]).astype(float))

    # clean up the graphcur textfile we made
    iraf.delete('graphcur')

    return FWHM
Beispiel #32
0
def ecpsf(img, ofwhm, threshold, interactive, ds9, fixaperture=False,_catalog=''):
    try:
        import agnkey
        import string

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

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

        if instrument in ['kb05', 'kb70', 'kb71', 'kb73', 'kb74', 'kb75', 'kb76', 'kb77', 'kb78', 'kb79']:
            scale = pixelscale
            _datamax = 45000
        elif instrument in ['fl02', 'fl03', 'fl04']:
            scale = pixelscale
            _datamax = 120000
        elif instrument in ['fs01', 'em03']:
            scale = pixelscale
            _datamax = 65000
        elif instrument in ['fs02', 'fs03']:
            scale = pixelscale
            _datamax = 65000
        elif instrument in ['em01']:
            scale = pixelscale
            _datamax = 65000
        try:
            _wcserr = agnkey.util.readkey3(hdr, 'wcserr')
            if float(_wcserr) == 0:
                if instrument in ['kb05', 'kb70', 'kb71', 'kb73', 'kb74', 'kb75', 'kb76', 'kb77', 'kb78', 'kb79']:
                    seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                elif instrument in ['fl02', 'fl03', 'fl04']:
                    seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                elif instrument in ['fs01', 'fs02', 'fs03', 'em03', 'em01']:
                    if 'L1FWHM' in hdr:
                        seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75
                    elif 'L1SEEING' in hdr:
                        seeing = float(agnkey.util.readkey3(hdr, 'L1SEEING')) * scale
                    else:
                        seeing = 3
                else:
                    seeing = 3
            else:
                seeing = float(agnkey.util.readkey3(hdr, 'PSF_FWHM'))
                sys.exit('astrometry not good')
        except:
            sys.exit('astrometry not good')

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

        if interactive:
            iraf.display(img, 1, fill=True)
            iraf.delete('tmp.lo?', verify=False)
            print '_' * 80
            print '>>> Mark reference stars with "a". Then "q"'
            print '-' * 80
            iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True)
            xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1)
            xns, yns, _fws = [], [], []
            ff = open('_ap.coo', 'w')
            for i in range(len(xyrefer)):
                xns.append(float(xyrefer[i].split()[0]))
                yns.append(float(xyrefer[i].split()[1]))
                _fws.append(float(xyrefer[i].split()[3]))
                ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i])))
            ff.close()

        elif _catalog:
#            cat1=agnkey.agnastrodef.readtxt(_catalog)
#            cat1['ra'],cat1['dec']
            ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img,inwcs='world',outwcs='logical',
                              units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no')
            ddd=[i for i in ddd if i[0]!='#']
            ddd=['  '.join(i.split()[0:3]) for i in ddd]
            ff = open('_ap.coo', 'w')
            for i in ddd:
                a,b,c = string.split(i)
                #print a,b,c
                ff.write('%10s %10s %10s \n' % (a, b, c))
            ff.close()
            print 'use catalog'
        else:
            xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale)
            ff = open('_ap.coo', 'w')
            for i in range(len(xs)):
                    ff.write('%10.3f %10.3f %7.2f \n' % (xs[i], ys[i], float(fluxrad[i])))
            ff.close()  ## End automatic selection

        print 80 * "#"
        photmag = apfit(img, fwhm, hdr, interactive, _datamax, fixaperture)

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

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

        #######################################
        rap, decp, magp2, magp3, magp4, smagf = [], [], [], [], [], []
        merrp2, merrp3, merrp4, smagerrf = [], [], [], []
        rap0, decp0 = [], []
        for i in range(len(radec)):
            aa = radec[i].split()
            rap.append(aa[0])
            decp.append(aa[1])
            rap0.append(agnkey.agnabsphotdef.deg2HMS(ra=aa[0]))
            decp0.append(agnkey.agnabsphotdef.deg2HMS(dec=aa[1]))
            idp = aa[2]
            magp2.append(aa[3])
            magp3.append(aa[4])
            magp4.append(aa[5])
            merrp2.append(aa[6])
            merrp3.append(aa[7])
            merrp4.append(aa[8])

        tbhdu = pyfits.new_table(pyfits.ColDefs([
            pyfits.Column(name='ra', format='20A', array=np.array(rap)),
            pyfits.Column(name='dec', format='20A', array=np.array(decp)),
            pyfits.Column(name='ra0', format='E', array=np.array(rap0)),
            pyfits.Column(name='dec0', format='E', array=np.array(decp0)),
            pyfits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                            np.array(magp2), 9999), float)),
            pyfits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'),
                                                                            np.array(magp3), 9999), float)),
            pyfits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'),
                                                                            np.array(magp4), 9999), float)),
            pyfits.Column(name='merrp2', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'),
                                                                             np.array(merrp2), 9999), float)),
            pyfits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'),
                                                                             np.array(merrp3), 9999), float)),
            pyfits.Column(name='merrp4', format='E', array=np.array(np.where((np.array(merrp4) != 'INDEF'),
                                                                             np.array(merrp4), 9999), float)),
            pyfits.Column(name='smagf', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'),
                                                                            np.array(magp2), 9999), float)),
            pyfits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'),
                                                                               np.array(merrp2), 9999), float)),

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

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Beispiel #33
0
        'fwhm':3,'datamax':6.e4,'geointer':'nearest','logfile':'imcoaddLog.txt'
        }
    targets = ['M8-1', 'M8-2', 'M8-3']
    prefix = 'mrg'
    for f in filters:
        print "  - Co-addding science images in filter: %s" % (f)
        qd['Filter2'] = f + '_G%'
        for t in targets:
            qd['Object'] = t + '%'
            print "  - Co-addding science images for position: %s" % (t)
            outImage = t + '_' + f + '.fits'
            coAddFiles = fs.fileListQuery(dbFile, fs.createQuery('sciImg', qd), qd)
            gemtools.imcoadd(','.join(prefix+str(x) for x in coAddFiles),
                    outimage=outImage, **coaddFlags)

    iraf.delete ("*_trn*,*_pos,*_cen")
    iraf.imdelete ("*badpix.pl,*_med.fits,*_mag.fits")
    #iraf.imdelete ("mrgS*.fits")

    print ("=== Finished Calibration Processing ===")

def gmos_img_proc2(dbFile="./raw/obsLog.sqlite3", qd={'use_me': 1,'Instrument': 'GMOS-S', 'CcdBin': '2 2', 'RoI': 'Full', 'Object': 'M8-%', 'DateObs': '2006-09-01:2006-10-30'},
                   bias_dateobs="2006-09-01:2006-10-30",
                   biasFlags={'logfile': 'biasLog.txt', 'rawpath': './raw/', 'fl_vardq': 'yes', 'verbose': 'yes'},
                   flat_dateobs='2006-09-10:2006-10-10',
                   flatFlags = {'fl_scale': 'yes', 'sctype': 'mean', 'fl_vardq': 'yes','rawpath': './raw/', 'logfile': 'giflatLog.txt', 'verbose': 'yes'},
                   filters = ['Ha', 'HaC', 'SII', 'r', 'i'],
                   sciFlags={'fl_over': 'yes', 'fl_trim': 'yes', 'fl_bias':'yes', 'fl_dark': 'no','fl_flat': 'yes', 'logfile':'gireduceLog.txt', 'rawpath': './raw/','fl_vardq': 'yes','bpm':bpm_gmos, 'verbose': 'yes'},
                   mosaicFlags = {'fl_paste': 'no', 'fl_fixpix': 'no', 'fl_clean': 'yes', 'geointer': 'nearest', 'logfile': 'gmosaicLog.txt', 'fl_vardq': 'yes', 'fl_fulldq': 'yes', 'verbose': 'yes'},
                   coaddFlags = {'fwhm': 3, 'datamax': 6.e4, 'geointer': 'nearest', 'logfile': 'imcoaddLog.txt'},
                   targets = ['M8-1', 'M8-2', 'M8-3'],
Beispiel #34
0
def gmos_img_proc2(dbFile="./raw/obsLog.sqlite3", qd={'use_me': 1,'Instrument': 'GMOS-S', 'CcdBin': '2 2', 'RoI': 'Full', 'Object': 'M8-%', 'DateObs': '2006-09-01:2006-10-30'},
                   bias_dateobs="2006-09-01:2006-10-30",
                   biasFlags={'logfile': 'biasLog.txt', 'rawpath': './raw/', 'fl_vardq': 'yes', 'verbose': 'yes'},
                   flat_dateobs='2006-09-10:2006-10-10',
                   flatFlags = {'fl_scale': 'yes', 'sctype': 'mean', 'fl_vardq': 'yes','rawpath': './raw/', 'logfile': 'giflatLog.txt', 'verbose': 'yes'},
                   filters = ['Ha', 'HaC', 'SII', 'r', 'i'],
                   sciFlags={'fl_over': 'yes', 'fl_trim': 'yes', 'fl_bias':'yes', 'fl_dark': 'no','fl_flat': 'yes', 'logfile':'gireduceLog.txt', 'rawpath': './raw/','fl_vardq': 'yes','bpm':bpm_gmos, 'verbose': 'yes'},
                   mosaicFlags = {'fl_paste': 'no', 'fl_fixpix': 'no', 'fl_clean': 'yes', 'geointer': 'nearest', 'logfile': 'gmosaicLog.txt', 'fl_vardq': 'yes', 'fl_fulldq': 'yes', 'verbose': 'yes'},
                   coaddFlags = {'fwhm': 3, 'datamax': 6.e4, 'geointer': 'nearest', 'logfile': 'imcoaddLog.txt'},
                   targets = ['M8-1', 'M8-2', 'M8-3'],
                   clean_files = False
                   ):
    """
    Parameters
    ----------
    dbFile : str
        Filename containing the SQL sqlite3 database created by obslog.py
        It must be placed in the ./raw/ directory
        Default is `./raw/obsLog.sqlite3`
    qd : dictionary
        Query Dictionary of essential parameter=value pairs.
        Select bias exposures within ~2 months of the target observations
        e.g.
         qd= {'use_me': 1,
          'Instrument': 'GMOS-S', 'CcdBin': '2 2', 'RoI': 'Full', 'Object': 'M8-%',
          'DateObs': '2006-09-01:2006-10-30'
          }
    bias_dateobs : str
        String representing the bias search Obsdate
        e.g. bias_dateobs = `2006-09-01:2006-10-30`

    biasFlags : dict
        Dictionary for the keyword flags of gmos.gbias() function

    flat_dateobs : str
        String representing the flat search Obsdate
        e.g. flat_dateobs = `2006-09-10:2006-10-10`

    flatFlags : dict
        Dictionary for the keyword flags of gmos.giflat() function
        e.g. flatFlags = {'fl_scale': 'yes', 'sctype': 'mean', 'fl_vardq': 'yes','rawpath': './raw/',
                         'logfile': 'giflatLog.txt', 'verbose': 'yes'}
    filters : list
        List of filter names to perform reduction
        e.g. filters=['Ha', 'HaC', 'SII', 'r', 'i']

    sciFlags : dict
        Dictionary for the keyword flags of gmos.gireduce() function

    mosaicFlags : dict
        Dictionary for the keyword flags of gmos.gimosaic() function

    coaddFlags : dict
        Dictionary for the keyword flags of gemtools.imcoadd() function

    targets : list
        List of names of target observations for the co-addition
        e.g.  targets = ['M8-1', 'M8-2', 'M8-3']

    clean_files : bool
        Whether to clean intermediate files from reduction process

    Returns
    -------
    Reduce GMOS imaging based on tutorial example.


    """
    print ("### Begin Processing GMOS/MOS Images ###")
    print ("###")
    print ("=== Creating MasterCals ===")

    # From the work_directory:
    # Create the query dictionary of essential parameter=value pairs.
    # Select bias exposures within ~2 months of the target observations:

    print (" --Creating Bias MasterCal--")
    qd.update({'DateObs': bias_dateobs})

    # Set the task parameters.
    gmos.gbias.unlearn()
    # The following SQL generates the list of files to process.
    SQL = fs.createQuery('bias', qd)
    biasFiles = fs.fileListQuery(dbFile, SQL, qd)

    # The str.join() function is needed to transform a python list into a string
    # filelist that IRAF can understand.
    if len(biasFiles) > 1:
        files_all = ','.join(str(x) for x in biasFiles)
        # import pdb; pdb.set_trace()
        gmos.gbias(files_all, 'MCbias.fits', **biasFlags)

    # Clean up
    year_obs = qd['DateObs'].split('-')[0]
    if clean_files:
        iraf.imdel('gS{}*.fits'.format(year_obs))

    ask_user("MC Bias done. Would you like to continue to proceed with Master Flats? (y/n): ",['y','yes'])

    print (" --Creating Twilight Imaging Flat-Field MasterCal--")
    # Select flats obtained contemporaneously with the observations.
    qd.update({'DateObs': flat_dateobs})

    # Set the task parameters.
    gmos.giflat.unlearn()

    #filters = ['Ha', 'HaC', 'SII', 'r', 'i']
    for f in filters:
        print "  Building twilight flat MasterCal for filter: %s" % (f)

        # Select filter name using a substring of the official designation.
        qd['Filter2'] = f + '_G%'
        mcName = 'MCflat_%s.fits' % (f)
        flatFiles = fs.fileListQuery(dbFile, fs.createQuery('twiFlat', qd), qd)
        if len(flatFiles) > 0:
            files_all = ','.join(str(x) for x in flatFiles)
            # import pdb; pdb.set_trace()
            gmos.giflat(files_all, mcName, bias='MCbias', **flatFlags)

    if clean_files:
        iraf.imdel('gS{}*.fits,rgS{}*.fits'.format(year_obs, year_obs))

    ask_user("MC Flats done. Would you like to continue to proceed with processing Science Images? (y/n): ", ['yes','y'])

    print ("=== Processing Science Images ===")
    # Remove restriction on date range
    qd['DateObs'] = '*'
    prefix = 'rg'

    gmos.gireduce.unlearn()
    gemtools.gemextn.unlearn()  # disarms a bug in gmosaic
    gmos.gmosaic.unlearn()
    # Reduce the science images, then mosaic the extensions in a loop
    for f in filters:
        print "    Processing science images for filter: %s" % (f)
        qd['Filter2'] = f + '_G%'
        flatFile = 'MCflat_' + f + '.fits'
        SQL = fs.createQuery('sciImg', qd)
        sciFiles = fs.fileListQuery(dbFile, SQL, qd)
        if len(sciFiles) > 0:
            # Make sure BPM table is in sciFlags for employing the imaging Static BPM for this set of detectors.
            # import pdb; pdb.set_trace()
            all_files = ','.join(str(x) for x in sciFiles)
            gmos.gireduce(all_files, bias='MCbias', flat1=flatFile, **sciFlags)
            for file in sciFiles:
                gmos.gmosaic(prefix + file, **mosaicFlags)
        else:
            print("No Science images found for filter {}. Check database.".format(f))
            import pdb; pdb.set_trace()

    if clean_files:
        iraf.imdelete('gS{}*.fits,rgS{}*.fits'.format(year_obs,year_obs))

    ask_user("Science Images done. Would you like to continue to proceed with image co-addition? (y/n): ", ['y','yes'])

    ## Co-add the images, per position and filter.
    print (" -- Begin image co-addition --")

    # Use primarily the default task parameters.
    gemtools.imcoadd.unlearn()
    prefix = 'mrg'
    for f in filters:
        print "  - Co-addding science images in filter: %s" % (f)
        qd['Filter2'] = f + '_G%'
        for t in targets:
            qd['Object'] = t + '%'
            print "  - Co-addding science images for position: %s" % (t)
            outImage = t + '_' + f + '.fits'
            coAddFiles = fs.fileListQuery(dbFile, fs.createQuery('sciImg', qd), qd)
            all_files = ','.join(prefix + str(x) for x in coAddFiles)
            if all_files == '':
                print('No files available for co-addition. Check that the target names are written correctly.')
                import pdb; pdb.set_trace()
            gemtools.imcoadd(all_files, outimage=outImage, **coaddFlags)

    ask_user("Co-addition done. Would you like to clean the latest intermediate reduction files? (y/n): ", ['y','yes'])

    if clean_files:
        iraf.delete("*_trn*,*_pos,*_cen")
        iraf.imdelete("*badpix.pl,*_med.fits,*_mag.fits")
        # iraf.imdelete ("mrgS*.fits")

    print ("=== Finished Calibration Processing ===")
Beispiel #35
0
    #
    #   Apsumming the stellar spectra
    #
for m, i in enumerate(star):
    iraf.imdelete('astexlrg'+i)
    iraf.gfapsum(
        'stexlrg'+i, fl_inter='no', lthreshold=400.,
        reject='avsigclip')
#-------------------------------------------------------------------------------------

#
#   Building sensibility function
#

for m in range(len(star)):
    iraf.delete('std'+str(m))
    iraf.delete('sens'+str(m)+'.fits')


for m in range(len(star)):
    iraf.gsstandard(
        'astexlrg'+star[m], starname=stdstar[m],
        observatory=observatory[m], sfile='std'+str(m),
        sfunction='sens'+str(m), caldir=caldir[m])


'''
# Create list of list of star (to be used in gsstandard)
# Nao funcionou

tmpstar = [star_idx, star]
Beispiel #36
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False,_catalog='',_datamax=''):
    try:
        import agnkey
        hdr = agnkey.util.readhdr(img + '.fits')
        instrument = agnkey.util.readkey3(hdr, 'instrume')
        print 'INSTRUMENT:', instrument

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

    start(kind): Do a full reduction of either Science or Telluric data.

    nifsReduce- for the telluric and science data reduction.

    Reduces NIFS telluric and science frames and attempts a flux calibration.

    Parameters are loaded from runtimeData/config.cfg. This script will
    automatically detect if it is being run on telluric data or science data.

    There are 6 steps.

    INPUT:
    + Raw files
        - Science frames
        - Sky frames
    + Calibration files
        - MDF shift file
        - Bad Pixel Mask (BPM)
        - Flat field frame
        - Reduced arc frame
        - Reduced ronchi mask frame
        - arc and ronchi database/ files

    OUTPUT:
        - If telluric reduction an efficiency spectrum used to telluric correct and absolute flux
          calibrate science frames
        - If science reduction a reduced science data cube.

    Args:
        kind (string): either 'Telluric' or 'Science'.
        telluricDirectoryList (string): Used by low memory pipeline.
        scienceDirectoryList (string): Used by low memory pipeline.

    """

    # TODO(nat): Right now the pipeline will crash if you decide to skip, say, doing a bad
    # pixel correction. This is because each step adds a prefix to the frame name, and most following
    # steps depend on that prefix being there.
    # One way to fix this is if a step is to be skipped, iraf.copy() is called instead to copy the frame and
    # add the needed prefix. Messy but it might work for now.

    ###########################################################################
    ##                                                                       ##
    ##                  BEGIN - GENERAL REDUCTION SETUP                      ##
    ##                                                                       ##
    ###########################################################################

    # Store current working directory for later use.
    path = os.getcwd()

    # Set up the logging file.
    log = os.getcwd() + '/Nifty.log'

    logging.info('\n#################################################')
    logging.info('#                                               #')
    logging.info('# Start the NIFS Science and Telluric Reduction #')
    logging.info('#                                               #')
    logging.info('#################################################\n')

    # Set up/prepare IRAF.
    iraf.gemini()
    iraf.gemtools()
    iraf.gnirs()
    iraf.nifs()

    # Reset to default parameters the used IRAF tasks.
    iraf.unlearn(iraf.gemini, iraf.gemtools, iraf.gnirs, iraf.nifs,
                 iraf.imcopy)

    # From http://bishop.astro.pomona.edu/Penprase/webdocuments/iraf/beg/beg-image.html:
    # Before doing anything involving image display the environment variable
    # stdimage must be set to the correct frame buffer size for the display
    # servers (as described in the dev$graphcap file under the section "STDIMAGE
    # devices") or to the correct image display device. The task GDEVICES is
    # helpful for determining this information for the display servers.
    iraf.set(stdimage='imt2048')

    # Prepare the IRAF package for NIFS.
    # NSHEADERS lists the header parameters used by the various tasks in the
    # NIFS package (excluding headers values which have values fixed by IRAF or
    # FITS conventions).
    iraf.nsheaders("nifs", logfile=log)

    # Set clobber to 'yes' for the script. This still does not make the gemini
    # tasks overwrite files, so:
    # YOU WILL LIKELY HAVE TO REMOVE FILES IF YOU RE_RUN THE SCRIPT.
    user_clobber = iraf.envget("clobber")
    iraf.reset(clobber='yes')

    # This helps make sure all variables are initialized to prevent bugs.
    scienceSkySubtraction = None
    scienceOneDExtraction = None
    extractionXC = None
    extractionYC = None
    extractionRadius = None
    telluricSkySubtraction = None

    # Load reduction parameters from runtimeData/config.cfg.
    with open('./config.cfg') as config_file:
        config = ConfigObj(config_file, unrepr=True)
        # Read general pipeline config.
        over = config['over']
        manualMode = config['manualMode']
        calDirList = config['calibrationDirectoryList']
        scienceOneDExtraction = config['scienceOneDExtraction']
        extractionXC = config['extractionXC']
        extractionYC = config['extractionYC']
        extractionRadius = config['extractionRadius']

        if kind == 'Telluric':
            # Telluric reduction specific config.
            telluricReductionConfig = config['telluricReductionConfig']
            if telluricDirectoryList:
                observationDirectoryList = telluricDirectoryList
            elif not telluricDirectoryList:
                observationDirectoryList = config['telluricDirectoryList']
            start = telluricReductionConfig['telStart']
            stop = telluricReductionConfig['telStop']
            telluricSkySubtraction = telluricReductionConfig[
                'telluricSkySubtraction']

        if kind == 'Science':
            # Science reduction specific config.
            scienceReductionConfig = config['scienceReductionConfig']
            if scienceDirectoryList:
                observationDirectoryList = scienceDirectoryList
            elif not scienceDirectoryList:
                observationDirectoryList = config['scienceDirectoryList']
            start = scienceReductionConfig['sciStart']
            stop = scienceReductionConfig['sciStop']
            scienceSkySubtraction = scienceReductionConfig[
                'scienceSkySubtraction']

    ###########################################################################
    ##                                                                       ##
    ##                 COMPLETE - GENERAL REDUCTION SETUP                    ##
    ##                                                                       ##
    ###########################################################################

    # nifsReduce has two nested loops that reduced data.
    # It loops through each science (or telluric) directory, and
    # runs through a series of calibrations steps on the data in that directory.

    # Loop through all the observation (telluric or science) directories to perform a reduction on each one.
    for observationDirectory in observationDirectoryList:

        ###########################################################################
        ##                                                                       ##
        ##                  BEGIN - OBSERVATION SPECIFIC SETUP                   ##
        ##                                                                       ##
        ###########################################################################

        # Print the current directory of data being reduced.
        logging.info(
            "\n#################################################################################"
        )
        logging.info("                                   ")
        logging.info("  Currently working on reductions in")
        logging.info("  in " + str(observationDirectory))
        logging.info("                                   ")
        logging.info(
            "#################################################################################\n"
        )

        os.chdir(observationDirectory)
        tempObs = observationDirectory.split(os.sep)
        obsid = tempObs[-1]

        # Change the iraf directory to the current directory.
        pwd = os.getcwd()
        iraffunctions.chdir(pwd)

        # Copy relevant calibrations over to the science directory.
        # Open and store the name of the MDF shift reference file from shiftfile into shift.
        shift = 'calibrations/shiftFile'
        # Open and store the name of the flat frame
        flat = 'calibrations/finalFlat'
        # Open and store the bad pixel mask
        finalBadPixelMask = 'calibrations/finalBadPixelMask'
        # Ronchi, arc and database must all be in local calibrations directory
        # Open and store the name of the reduced spatial correction ronchi flat frame name from ronchifile in ronchi.
        ronchi = 'finalRonchi'
        # Open and store the name of the reduced wavelength calibration arc frame from arclist in arc.
        arc = 'finalArc'

        if os.path.exists(os.getcwd() + '/' + ronchi + ".fits"):
            if over:
                iraf.delete(os.getcwd() + '/calibrations/finalRonchi.fits')
                # Copy the spatial calibration ronchi flat frame from Calibrations_grating to the observation directory.
                shutil.copy(os.getcwd() + '/calibrations/finalRonchi.fits',
                            ronchi + '.fits')
            else:
                print "\nOutput exists and -over not set - skipping copy of reduced ronchi"
        else:
            shutil.copy(os.getcwd() + '/calibrations/finalRonchi.fits',
                        ronchi + '.fits')

        if os.path.exists(os.getcwd() + '/' + arc + ".fits"):
            if over:
                iraf.delete(os.getcwd() + '/calibrations/finalArc.fits')
                # Copy the spatial calibration arc flat frame from Calibrations_grating to the observation directory.
                shutil.copy(os.getcwd() + '/calibrations/finalArc.fits',
                            arc + '.fits')
            else:
                print "\nOutput exists and -over not set - skipping copy of reduced arc"
        else:
            shutil.copy(os.getcwd() + '/calibrations/finalArc.fits',
                        arc + '.fits')
        # Make sure the database files are in place. Current understanding is that
        # these should be local to the reduction directory, so need to be copied from
        # the calDir.
        if os.path.isdir("./database"):
            if over:
                shutil.rmtree("./database")
                os.mkdir("./database")
                for item in glob.glob("calibrations/database/*"):
                    shutil.copy(item, "./database/")
            else:
                print "\nOutput exists and -over not set - skipping copy of database directory"
        else:
            os.mkdir('./database/')
            for item in glob.glob("calibrations/database/*"):
                shutil.copy(item, "./database/")

        if telluricSkySubtraction or scienceSkySubtraction:
            # Read the list of sky frames in the observation directory.
            try:
                skyFrameList = open("skyFrameList", "r").readlines()
                skyFrameList = [frame.strip() for frame in skyFrameList]
            except:
                logging.info(
                    "\n#####################################################################"
                )
                logging.info(
                    "#####################################################################"
                )
                logging.info("")
                logging.info(
                    "     WARNING in reduce: No sky frames were found in a directory."
                )
                logging.info("              Please make a skyFrameList in: " +
                             str(os.getcwd()))
                logging.info("")
                logging.info(
                    "#####################################################################"
                )
                logging.info(
                    "#####################################################################\n"
                )
                raise SystemExit
            sky = skyFrameList[0]

        # If we are doing a telluric reduction, open the list of telluric frames in the observation directory.
        # If we are doing a science reduction, open the list of science frames in the observation directory.
        if kind == 'Telluric':
            tellist = open('tellist', 'r').readlines()
            tellist = [frame.strip() for frame in tellist]
        elif kind == 'Science':
            scienceFrameList = open("scienceFrameList", "r").readlines()
            scienceFrameList = [frame.strip() for frame in scienceFrameList]
            # For science frames, check to see if the number of sky frames matches the number of science frames.
            # IF NOT duplicate the sky frames and rewrite the sky file and skyFrameList.
            if scienceSkySubtraction:
                if not len(skyFrameList) == len(scienceFrameList):
                    skyFrameList = makeSkyList(skyFrameList, scienceFrameList,
                                               observationDirectory)

        ###########################################################################
        ##                                                                       ##
        ##                 COMPLETE - OBSERVATION SPECIFIC SETUP                 ##
        ##                BEGIN DATA REDUCTION FOR AN OBSERVATION                ##
        ##                                                                       ##
        ###########################################################################

        # Check start and stop values for reduction steps. Ask user for a correction if
        # input is not valid.
        valindex = start
        while valindex > stop or valindex < 1 or stop > 6:
            logging.info(
                "\n#####################################################################"
            )
            logging.info(
                "#####################################################################"
            )
            logging.info("")
            logging.info(
                "     WARNING in reduce: invalid start/stop values of observation"
            )
            logging.info("                           reduction steps.")
            logging.info("")
            logging.info(
                "#####################################################################"
            )
            logging.info(
                "#####################################################################\n"
            )

            valindex = int(
                raw_input(
                    "\nPlease enter a valid start value (1 to 7, default 1): ")
            )
            stop = int(
                raw_input(
                    "\nPlease enter a valid stop value (1 to 7, default 7): "))

        while valindex <= stop:

            ###########################################################################
            ##  STEP 1: Prepare raw data; science, telluric and sky frames ->n       ##
            ###########################################################################

            if valindex == 1:
                if manualMode:
                    a = raw_input(
                        "About to enter step 1: locate the spectrum.")
                if kind == 'Telluric':
                    tellist = prepare(tellist, shift, finalBadPixelMask, log,
                                      over)
                elif kind == 'Science':
                    scienceFrameList = prepare(scienceFrameList, shift,
                                               finalBadPixelMask, log, over)
                if telluricSkySubtraction or scienceSkySubtraction:
                    skyFrameList = prepare(skyFrameList, shift,
                                           finalBadPixelMask, log, over)
                logging.info(
                    "\n##############################################################################"
                )
                logging.info("")
                logging.info(
                    "  STEP 1: Locate the Spectrum (and prepare raw data) ->n - COMPLETED "
                )
                logging.info("")
                logging.info(
                    "##############################################################################\n"
                )

            ###########################################################################
            ##  STEP 2: Sky Subtraction ->sn                                         ##
            ###########################################################################

            elif valindex == 2:
                if manualMode:
                    a = raw_input("About to enter step 2: sky subtraction.")
                # Combine telluric sky frames.
                if kind == 'Telluric':
                    if telluricSkySubtraction:
                        if len(skyFrameList) > 1:
                            combineImages(skyFrameList, "gn" + sky, log, over)
                        else:
                            copyImage(skyFrameList, 'gn' + sky + '.fits', over)
                        skySubtractTel(tellist, "gn" + sky, log, over)
                    else:
                        for image in tellist:
                            iraf.copy('n' + image + '.fits',
                                      'sn' + image + '.fits')

                if kind == 'Science':
                    if scienceSkySubtraction:
                        skySubtractObj(scienceFrameList, skyFrameList, log,
                                       over)
                    else:
                        for image in scienceFrameList:
                            iraf.copy('n' + image + '.fits',
                                      'sn' + image + '.fits')

                logging.info(
                    "\n##############################################################################"
                )
                logging.info("")
                logging.info("  STEP 2: Sky Subtraction ->sn - COMPLETED ")
                logging.info("")
                logging.info(
                    "##############################################################################\n"
                )

            ##############################################################################
            ##  STEP 3: Flat field, slice, subtract dark and correct bad pixels ->brsn  ##
            ##############################################################################

            elif valindex == 3:
                if manualMode:
                    a = raw_input(
                        "About to enter step 3: flat fielding and bad pixels correction."
                    )
                if kind == 'Telluric':
                    applyFlat(tellist, flat, log, over, kind)
                    fixBad(tellist, log, over)
                elif kind == 'Science':
                    applyFlat(scienceFrameList, flat, log, over, kind)
                    fixBad(scienceFrameList, log, over)
                logging.info(
                    "\n##############################################################################"
                )
                logging.info("")
                logging.info(
                    "  STEP 3: Flat fielding and Bad Pixels Correction ->brsn - COMPLETED "
                )
                logging.info("")
                logging.info(
                    "##############################################################################\n"
                )

            ###########################################################################
            ##  STEP 4: Derive and apply 2D to 3D transformation ->tfbrsn            ##
            ###########################################################################

            elif valindex == 4:
                if manualMode:
                    a = raw_input(
                        "About to enter step 4: 2D to 3D transformation and Wavelength Calibration."
                    )
                if kind == 'Telluric':
                    fitCoords(tellist, arc, ronchi, log, over, kind)
                    transform(tellist, log, over)
                elif kind == 'Science':
                    fitCoords(scienceFrameList, arc, ronchi, log, over, kind)
                    transform(scienceFrameList, log, over)
                logging.info(
                    "\n##############################################################################"
                )
                logging.info("")
                logging.info(
                    "  STEP 4: 2D to 3D transformation and Wavelength Calibration ->tfbrsn - COMPLETED "
                )
                logging.info("")
                logging.info(
                    "##############################################################################\n"
                )

            ############################################################################
            ##  STEP 5 (tellurics): For telluric data derive a telluric               ##
            ##                     correction ->gxtfbrsn                              ##
            ##  STEP 5 (science): For science apply an efficiency correction and make ##
            ##           a data cube (not necessarily in that order).                 ##
            ##           (i) Python method applies correction to nftransformed cube.  ##
            ##           Good for faint objects.                        ->cptfbrsn    ##
            ##           (ii) iraf.telluric method applies correction to              ##
            ##           nftransformed result (not quite a data cube) then            ##
            ##           nftransforms cube.                             ->catfbrsn    ##
            ##           (iii) If no telluric correction/flux calibration to be       ##
            ##           applied make a plain data cube.                ->ctfbrsn     ##
            ############################################################################

            elif valindex == 5:
                if manualMode:
                    a = raw_input("About to enter step 5.")
                # For telluric data:
                # Make a combined extracted 1D standard star spectrum.
                if kind == 'Telluric':
                    extractOneD(tellist, kind, log, over, extractionXC,
                                extractionYC, extractionRadius)

                    # TODO(nat): add this as a parameter; encapsulate this.
                    copyToScience = True
                    if copyToScience:
                        # Copy final extracted results to science directory.
                        try:
                            with open("scienceMatchedTellsList", "r") as f:
                                lines = f.readlines()
                            lines = [x.strip() for x in lines]

                            for i in range(len(lines)):
                                if "obs" in lines[i]:
                                    k = 1
                                    while i + k != len(
                                            lines) and "obs" not in lines[i +
                                                                          k]:
                                        copyResultsToScience(
                                            "gxtfbrsn" + tellist[0] + ".fits",
                                            "0_tel" + lines[i + k] + ".fits",
                                            over)
                                        k += 1
                        except IOError:
                            logging.info(
                                "\nNo scienceMatchedTellsList found in " +
                                os.getcwd() +
                                " . Skipping copy of extracted spectra to science directory."
                            )

                    logging.info(
                        "\n##############################################################################"
                    )
                    logging.info("")
                    logging.info(
                        "  STEP 5a: Extract 1D Spectra and Make Combined 1D Standard Star Spectrum"
                    )
                    logging.info("           ->gxtfbrsn - COMPLETED")
                    logging.info("")
                    logging.info(
                        "##############################################################################\n"
                    )
                    #TODO(nat): add this as a parameter.
                    makeTelluricCube = True
                    if makeTelluricCube:
                        makeCube('tfbrsn', tellist, log, over)
                        logging.info(
                            "\n##############################################################################"
                        )
                        logging.info("")
                        logging.info(
                            "  STEP 5b: Make uncorrected standard star data cubes, ->ctfbrsn  - COMPLETED"
                        )
                        logging.info("")
                        logging.info(
                            "##############################################################################\n"
                        )

                # For Science data:
                # Possibly extract 1D spectra, and make uncorrected cubes.
                elif kind == 'Science':
                    if scienceOneDExtraction:
                        extractOneD(scienceFrameList, kind, log, over,
                                    extractionXC, extractionYC,
                                    extractionRadius)
                        copyExtracted(scienceFrameList, over)
                        logging.info(
                            "\n##############################################################################"
                        )
                        logging.info("")
                        logging.info(
                            "  STEP 5a: Make extracted 1D Science spectra, ->ctgbrsn  - COMPLETED"
                        )
                        logging.info("")
                        logging.info(
                            "##############################################################################\n"
                        )
                    makeCube('tfbrsn', scienceFrameList, log, over)

                    # TODO(nat): encapsulate this inside a function.
                    if os.path.exists('products_uncorrected'):
                        if over:
                            shutil.rmtree('products_uncorrected')
                            os.mkdir('products_uncorrected')
                        else:
                            logging.info(
                                "\nOutput exists and -over not set - skipping creating of products_uncorrected directory"
                            )
                    else:
                        os.mkdir('products_uncorrected')
                    for item in scienceFrameList:
                        if os.path.exists('products_uncorrected/ctfbrsn' +
                                          item + '.fits'):
                            if over:
                                os.remove('products_uncorrected/ctfbrsn' +
                                          item + '.fits')
                                shutil.copy(
                                    'ctfbrsn' + item + '.fits',
                                    'products_uncorrected/ctfbrsn' + item +
                                    '.fits')
                            else:
                                logging.info(
                                    "\nOutput exists and -over not set - skipping copy of uncorrected cube"
                                )
                        else:
                            shutil.copy(
                                'ctfbrsn' + item + '.fits',
                                'products_uncorrected/ctfbrsn' + item +
                                '.fits')

                    if os.path.exists('products_telluric_corrected'):
                        if over:
                            shutil.rmtree('products_telluric_corrected')
                            os.mkdir('products_telluric_corrected')
                        else:
                            logging.info(
                                "\nOutput exists and -over not set - skipping creating of products_telluric_corrected directory"
                            )
                    else:
                        os.mkdir('products_telluric_corrected')
                    for item in scienceFrameList:
                        if os.path.exists(
                                'products_telluric_corrected/ctfbrsn' + item +
                                '.fits'):
                            if over:
                                os.remove(
                                    'products_telluric_corrected/ctfbrsn' +
                                    item + '.fits')
                                shutil.copy(
                                    'ctfbrsn' + item + '.fits',
                                    'products_telluric_corrected/ctfbrsn' +
                                    item + '.fits')
                            else:
                                logging.info(
                                    "\nOutput exists and -over not set - skipping copy of uncorrected cube"
                                )
                        else:
                            shutil.copy(
                                'ctfbrsn' + item + '.fits',
                                'products_telluric_corrected/ctfbrsn' + item +
                                '.fits')

                    logging.info(
                        "\n##############################################################################"
                    )
                    logging.info("")
                    logging.info(
                        "  STEP 5b: Make uncorrected science data cubes, ->ctfbrsn  - COMPLETED"
                    )
                    logging.info("")
                    logging.info(
                        "##############################################################################\n"
                    )

            valindex += 1

        logging.info(
            "\n##############################################################################"
        )
        logging.info("")
        logging.info("  COMPLETE - Reductions completed for " +
                     str(observationDirectory))
        logging.info("")
        logging.info(
            "##############################################################################\n"
        )

    # Return to directory script was begun from.
    os.chdir(path)
Beispiel #38
0
def process(infile, coords, fwhmpsf, sigma, aperture, annulus, dannulus, output, zmag, debug):
    """
    Entry point function to process science image.
    
    Parameters
    ----------
    infile : string
        Science image or list of science images
        
    coords : string
        Input text file with coordinates of stars
        
    fwhmpsf : float
        FWHM of the stelar psf in pixels
        
    sigma : float
        Sky background sigma
        
    annulus : int
        Inner sky annulus radius in pixels
        
    dannulus : int
        Radius of sky annulus in pixels 
        
    output : string
        Output file name
        
    zmag : string
        Photometric zero point
                
    Returns
    -------
    None 
    """
    print "PHOTOMETRY: CHIMERA Aperture Photometry Routine"
    
    fwhmpsf = float(fwhmpsf)
    sigma = float(sigma)
    annulus = int(annulus)
    dannulus = int(dannulus)
    
    # Check if input is a string of FITS images or a text file with file names
    if infile[0] == "@":
        infile = infile[1:]
        
        if not os.path.exists(infile):
            raise IOError("PHOTOMETRY: Not able to locate file %s. Stopping." %infile)
        
        image_cubes = []
        with open(infile, "r") as fd:
            for line in fd.readlines():
                if len(line) > 1:
                    image_cubes.append(line.replace("\n", ""))
    else:
        image_cubes = infile.split(",")

    # Number of images
    ncubes = len(image_cubes)

    # Check if the input coordinates file exists. Take a tmp copy of the input
    # file and use that. Delete at the end of processing.
    if not os.path.exists(coords):
        raise IOError("Input coordinate file %s does not exist. Stopping." %coords)
    else:
        tmp_coords = coords + ".tmp"
        iraf.copy(coords, tmp_coords)
    

    # Fields to extract from phot file
    fields = "XCEN,YCEN,CIER,MSKY,STDEV,NSKY,SIER,SUM,AREA,FLUX,MERR,PIER"

    total_phot_data = []
    img_sec = []
    for i in range(ncubes):
        sci_file = image_cubes[i]
        
        if not os.path.exists(sci_file):
            raise IOError("FITS image %s does not exist. Stopping." %sci_file)
        
        fpath, fname = os.path.split(sci_file)
        
        print "\n  Processing science image %s" %fname

        # Instantiate an Aperphot object
        ap = chimera.Aperphot(sci_file, coords)
        
        # Set fwhmpsf, sigma, annulus. dannulus and zero point
        ap.fwhmpsf = fwhmpsf
        ap.sigma = sigma
        ap.annulus = annulus
        ap.dannulus = dannulus
        
        if zmag != "":
            ap.zmag = float(zmag)
        
        # Read the input FITS image
        if i == 0:
            img, imghdr = chimera.fitsread(ap.sci_file, header = True)
        else:
            img = chimera.fitsread(ap.sci_file)

        
        # Determine nominal aperture radius for photometry
        if i == 0:
            if aperture:
                nom_aper = float(aperture)
            else:
                nom_aper = ap.daocog()
            
            print "  Nominal aperture radius : %4.1f pixels" %nom_aper
           
           
        # Perform aperture photometry on all the frames
        dtype = [("DATETIME", "S25"),("XCEN", "f4"),("YCEN", "f4"),("CIER", "i4"),("MSKY", "f4"),("STDEV", "f4"),("NSKY", "i4"),("SIER", "i4"),("SUM", "f4"),("AREA", "f4"),("FLUX_ADU", "f4"),("FLUX_ELEC", "f4"),("FERR", "f4"),("MAG", "f4"),("MERR", "f4"),("PIER", "i4"),]
        phot_data = np.zeros([ap.nframes], dtype = dtype)
        for j in range(ap.nframes):
            print "    Processing frame number : %d" %(j+1)
             
            outfile = sci_file.replace(".fits", "_" + str(j) + ".phot.1")
            ap.daophot(j+1, tmp_coords, outfile, nom_aper)
            objcen = dump(outfile, "XCEN,YCEN")
            with open(tmp_coords, "w") as fd:
                fd.write(objcen + '\n')
            
            aperphot_data = dump(outfile, fields).split()
            
            phot_data[j]['DATETIME'] = ap.addtime(j * ap.kintime).isoformat()
            phot_data[j]['XCEN'] = float(aperphot_data[0])
            phot_data[j]['YCEN'] = float(aperphot_data[1])
            phot_data[j]['CIER'] = int(aperphot_data[2])
            phot_data[j]['MSKY'] = float(aperphot_data[3])
            phot_data[j]['STDEV'] = float(aperphot_data[4])
            phot_data[j]['NSKY'] = int(aperphot_data[5])
            phot_data[j]['SIER'] = int(aperphot_data[6])
            phot_data[j]['SUM'] = float(aperphot_data[7])
            phot_data[j]['AREA'] = float(aperphot_data[8])
            phot_data[j]['FLUX_ADU'] = float(aperphot_data[9])
            phot_data[j]['FLUX_ELEC'] = float(aperphot_data[9]) * ap.epadu
            phot_data[j]['MAG'] = ap.zmag - 2.5 * np.log10(phot_data[j]['FLUX_ELEC']/ap.exptime)
            if aperphot_data[10] == 'INDEF':
                phot_data[j]['MERR'] = -10
            else:
                phot_data[j]['MERR'] = float(aperphot_data[10])
            phot_data[j]['PIER'] = int(aperphot_data[11])
            
            # Calculate error in flux - using the formula
            # err = sqrt(flux * gain + npix * (1 + (npix/nsky)) * (flux_sky * gain + R**2))
            phot_data[j]['FERR'] = np.sqrt(phot_data[j]['FLUX_ELEC'] + phot_data[j]['AREA'] * (1 + phot_data[j]['AREA']/phot_data[j]['NSKY']) * (phot_data[j]['MSKY'] * ap.epadu + ap.readnoise**2))
                      
            # Save a 51x51 image section of the object
            xmin, xmax = int(phot_data[j]['XCEN']) - 25, int(phot_data[j]['XCEN']) + 25
            ymin, ymax = int(phot_data[j]['YCEN']) - 25, int(phot_data[j]['YCEN']) + 25            
            
            img_sec.append(img[j, ymin:ymax, xmin:xmax])


        # Save photometry of all the image cubes in a single file
        total_phot_data.append(phot_data)
                
        # If debug mode -
        # 1. save DAOPHOT phot files
        # 2. save individual phot data as npy file
        # 3. Plot light cuve for each data cube separatly
        
        if debug:
            # Save photometry data in numpy binary format
            print "  Saving photometry data as numpy binary"
            if output != "":
                npy_outfile = output + ".npy"
            else:
                npy_outfile = coords + "_phot.npy"
        
            if os.path.exists(npy_outfile):
                os.remove(npy_outfile)
            
            np.save(npy_outfile, phot_data)

        
            # Plot first pass light curve
            if plot_flag:
                print "  Plotting normalized light curve"
                if output != "":
                    plt_outfile = output + ".png"
                else:
                    plt_outfile = coords + "_lc.png"
                plotter(phot_data, ap.nframes, ap.kintime, plt_outfile)
        else:
            # Delete intermediate files is not debug mode
            iraf.delete(os.path.join(fpath, '*.phot.1'))    
                    
    # Convert the total_phot_data to array and reshape it
    print '  Saving consolidated photometry data...'
    total_phot_data_arr = np.concatenate(total_phot_data)
        
    # Save the array as npy file
    np.save(coords + "_total.phot.npy", total_phot_data_arr)

    # Save the image section with object as FITS file
    print '  Saving image section with object as FITS image...'
    img_sec_arr = np.asarray(img_sec)

    img_fname = coords + "_obj.fits"
    if os.path.exists(img_fname):
        os.remove(img_fname)
            
    chimera.fitswrite(img_sec_arr, coords + "_obj.fits", header = imghdr)
                        
    # Delete temporary coordinate file
    if os.path.exists(tmp_coords):
        os.remove(tmp_coords)
    
    return
Beispiel #39
0
def applyTelluric(objlist, obsid, skylist, telinter, log, over):
    # corrects the data for telluric absorption features
    # nftelluric is currently only run interactively

    obsDir = os.getcwd()
    os.chdir('../Tellurics')
    telDirList = glob.glob('*')

    if telinter == 'no':
        telCor(obsDir, telDirList, over)
    else:
        for telDir in telDirList:
            if 'obs' in telDir:
                os.chdir(telDir)
                if os.path.exists('objtellist'):
                    objtellist = open("objtellist", "r").readlines()
                    objlist = [image.strip() for image in objtellist]
                else:
                    os.chdir('..')
                    continue
                try:
                    telluric = str(open('corrtellfile',
                                        'r').readlines()[0]).strip()
                except:
                    print "No telluric spectrum found in ", telDir
                    os.chdir('..')
                    continue
                shutil.copy(telluric + '.fits', obsDir)
                '''
                continuum = str(open('continuumfile', 'r').readlines()[0]).strip()
                bblist = open('blackbodyfile', 'r').readlines()
                bblist = [image.strip() for image in bblist]
                '''

                os.chdir(obsDir)
                iraffunctions.chdir(obsDir)
                if obsid in objlist:
                    index = objlist.index(obsid)
                    i = index + 1

                    while i < len(objlist) and 'obs' not in objlist[i]:
                        if os.path.exists("atfbrgn" + objlist[i] + ".fits"):
                            if over:
                                iraf.delete("atfbrgn" + objlist[i] + ".fits")
                                iraf.nftelluric('tfbrgn' + objlist[i],
                                                outprefix='a',
                                                calspec=telluric,
                                                fl_inter='yes',
                                                logfile=log)
                            else:
                                print "Output file exists and -over not set - skipping nftelluric in applyTelluric"
                        elif not os.path.exists('atfbrgn' + objlist[i] +
                                                '.fits'):
                            iraf.nftelluric('tfbrgn' + objlist[i],
                                            outprefix='a',
                                            calspec=telluric,
                                            fl_inter='yes',
                                            logfile=log)
                        '''
                        # remove continuum fit from reduced science image
                        if over:
                            if os.path.exists("cont"+objlist[i]+".fits"):
                                iraf.delete("cont"+objlist[i]+".fits")
                            MEFarithpy('atfbrgn'+objlist[i], '../Tellurics/'+telDir+'/'+continuum, 'divide', 'cont'+objlist[i]+'.fits')
                        elif not os.path.exists('cont'+objlist[i]+'.fits'):
                            MEFarithpy('atfbrgn'+objlist[i], '../Tellurics/'+telDir+'/'+continuum, 'divide', 'cont'+objlist[i]+'.fits')
                        else:
                            print "Output file exists and -over not set - skipping continuum division in applyTelluric"

                        # multiply science by blackbody
                        for bb in bblist:
                            objheader = pyfits.open(obsDir+'/'+objlist[i]+'.fits')
                            exptime = objheader[0].header['EXPTIME']
                            if str(int(exptime)) in bb:
                                if over:
                                    if os.path.exists('bbatfbrgn'+objlist[i]+'.fits'):
                                        os.remove('bbatfbrgn'+objlist[i]+'.fits')
                                    MEFarithpy('cont'+objlist[i], '../Tellurics/'+telDir+'/'+bb, 'multiply', 'bbatfbrgn'+objlist[i]+'.fits') 
                                elif not os.path.exists('bbatfbrgn'+objlist[i]+'.fits'):
                                    MEFarithpy('cont'+objlist[i], '../Tellurics/'+telDir+'/'+bb, 'multiply', 'bbatfbrgn'+objlist[i]+'.fits') 
                                else:
                                    print "Output file exists and -over- not set - skipping blackbody calibration in applyTelluric"
                        '''
                        i += 1
            os.chdir('../Tellurics')
Beispiel #40
0
    for img in imglist:
        if '.fits' in img: img = img[:-5]
        if os.path.exists(img + '.sn2.fits') and not option.redo:
            print img + ': psf already calculated'
        else:
            ds9 = os.system("ps -U" + str(os.getuid()) + "|grep -v grep | grep ds9")
            if option.interactive and ds9 != 0:
                pid = subprocess.Popen(['ds9']).pid
                time.sleep(2)
                ds9 = 0

            result, fwhm = ecpsf(img, option.fwhm, option.threshold, option.interactive, ds9, fixaperture,_catalog)
            print '\n### ' + str(result)

            iraf.delete("tmp.*", verify="no")
            iraf.delete("_psf.*", verify="no")
            print  "********** Completed in ", int(time.time() - start_time), "sec"
            print result
            if option.show:
                agnkey.util.marksn2(img + '.fits', img + '.sn2.fits', 1, '')
            try:
                import string
                if result == 1:
                    agnkey.agnsqldef.updatevalue('dataredulco', 'fwhm', fwhm, string.split(img, '/')[-1] + '.fits')
                    agnkey.agnsqldef.updatevalue('dataredulco', 'mag', 9999, string.split(img, '/')[-1] + '.fits')
                    agnkey.agnsqldef.updatevalue('dataredulco', 'apmag', 9999, string.split(img, '/')[-1] + '.fits')
                    if os.path.isfile(img + '.diff.fits') and os.path.isfile(
                                    img + '.sn2.fits'):  # update diff info is file available
                        os.system('cp ' + img + '.sn2.fits ' + img + '.diff.sn2.fits')
                        agnkey.agnsqldef.updatevalue('dataredulco', 'fwhm', fwhm,
Beispiel #41
0
def mscimage(input=None, output=None, format='image', pixmask=no,verbose=')_.verbose',wcssource='image',reference='',ra=INDEF,dec=INDEF,scale=INDEF,rotation=INDEF,blank=0.0,interpolant='poly5',minterpolant='linear',boundary='reflect',constant=0.0,fluxconserve=no,ntrim=8,nxblock=INDEF,nyblock=INDEF,interactive=no,nx=10,ny=20,fitgeometry='general',xxorder=4,xyorder=4,xxterms='half',yxorder=4,yyorder=4,yxterms='half',fd_in='',fd_ext='',fd_coord='',mode='ql',DOLLARnargs=0,taskObj=None):

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

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

    imsets = open(imsetsFilename, 'w')
    obspar = open(obsparFilename, 'w')
    shifts = open(shiftsFilename, 'w')

    for star in stars:
        roots = []

        for filter in filters:
            dir = star.lower() + '_' + filter.lower()
            if filter == 'Lp':
                dir += '2'
                
            images = glob.glob(dataDir + dir + '/c????.fits')
            imageRoots = []
            for ii in images:
                fileRoot = ii.split('/')[-1]
                imageRoots.append(fileRoot.replace('.fits', ''))

            roots.append(imageRoots)
            print star, filter, len(imageRoots)
            
        longCount = np.array([len(r) for r in roots]).max()
        print 'Long Count for %s = %d' % (star, longCount)

        for ii in range(longCount):
            imsets.write('%5s :  ' % star)

            for ff in range(len(roots)):
                if ii < len(roots[ff]):
                    # Write obsParams 
                    ir.txdump(roots[ff][ii] + '.phot.mag', 
                              'IMAGE,IFILTER,ITIME,XAIRMASS,OTIME', 
                              'yes', Stdout=obspar)

                    # Write shifts
                    posInfo = ir.txdump(roots[ff][ii] + '.phot.mag', 
                                        'IMAGE,XCENTER,YCENTER', 
                                        'yes', Stdout=1)
                    posFields = posInfo[0].split()
                    shiftX = -1.0 * float(posFields[1])
                    shiftY = -1.0 * float(posFields[2])
                    shifts.write('%-10s  %7.2f  %7.2f\n' % 
                                 (posFields[0], shiftX, shiftY))
                                                          
                    
                    # Write imageSet
                    imsets.write('%-10s  ' % (roots[ff][ii] + '.fits'))
                else:
                    imsets.write('%-10s  ' % ('INDEF'))

            imsets.write('\n')

    imsets.close()
    obspar.close()
    shifts.close()

    # Now run mknobsfile
    ir.delete(iMagniFilename)
    ir.digiphot()
    ir.photcal()
    ir.unlearn('mknobsfile')
    ir.mknobsfile.obsparams = obsparFilename
    ir.mknobsfile.shifts = shiftsFilename
    ir.mknobsfile('*.phot.mag', ','.join(filters), imsetsFilename, 
                  iMagniFilename, verbose='no', wrap='no')
    
    
    # Run mkconfig
    ir.delete('standards.config')
    ir.unlearn('mkconfig')
    ir.mkconfig('standards.config', 'fphotcal_ukirt_faint.dat', 
                'fstandards.instMag.dat', 'tphotcal_ukirt_faint.dat', 
                check='no', edit='no')
Beispiel #43
0
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax=45000, psffun='gauss', fixaperture=False):
    import agnkey

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = agnkey.util.readhdr(img+'.fits')
        _pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
#        a1, a2, a3, a4, = int(5), int(8), int(10), int(12)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)


    _center='no'

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.dannulus = a4
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.wsannul = int(a4)
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    #    iraf.photpars.apertures = '%d,%d,%d'%(a2,a3,a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = agnkey.util.readkey3(hdr, 'ron')
    iraf.datapars.epadu = agnkey.util.readkey3(hdr, 'gain')
    iraf.datapars.exposure = 'exptime'  #agnkey.util.readkey3(hdr,'exptime')
    iraf.datapars.airmass = 'airmass'
    iraf.datapars.filter = 'filter2'
    iraf.centerpars.calgori = 'gauss'
    iraf.centerpars.cbox = 1
    iraf.daopars.recenter = _center
    iraf.photpars.zmag = zmag

    iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False)
    iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = int(a4)
    iraf.daopars.wsannul = int(a4)
    iraf.daopars.recenter = _center
    iraf.daopars.varorder = varord

    if interactive:
        shutil.copyfile('_psf.mag', '_psf.pst')
        print '_' * 80
        print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \
              ' "w"-write and "q"-uit (cursor on ds9)'
        print '-' * 80
    else:
        iraf.pstselect(img+'.fits[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False)

    iraf.psf(img+'.fits[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive,
             verify=False, verbose=False)

#    if os.path.isfile(img + '.psf.fits'):
#        print 'file there'

    iraf.group(img+'.fits[0]', '_psf.mag', img + '.psf.fits', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img+'.fits[0]', '_psf.grp', img + '.psf.fits', '_psf.nst', '_psf.nrj', verify=False, verbose=False)

    photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, pst, fitmag
Beispiel #44
0
print "niri\n"  
unlearn ("niri")
print "f2\n" 
unlearn ("f2")
print "gemtools\n" 
unlearn ("gemtools")

print "geomap\n" 
unlearn ("geomap")




#deleting logfile if exist

iraf.delete("f2_image_logfile.log",verify='no')

# creating logfile

f2.logfile= "f2_image_logfile.log"


#loading headers keywords for F2

print "loading headers keywords for F2"


#
#Two times, just to be sure that it loads
# CRITICAL STEP!!!
#
Beispiel #45
0
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax=45000, psffun='gauss', fixaperture=False):
    ''' 
    giving an image, a psffile compute the psf using the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.photpars.zmag = zmag

    iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False)
    iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord

    if interactive:
        shutil.copyfile('_psf.mag', '_psf.pst')
        print '_' * 80
        print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \
              ' "w"-write and "q"-uit (cursor on ds9)'
        print '-' * 80
    else:
        iraf.pstselect(img+'[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False)

    iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive,
             verify=False, verbose=False)
    iraf.group(img + '[0]', '_psf.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_psf.nst', '_psf.nrj', verify=False, verbose=False)

    photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, pst, fitmag
Beispiel #46
0
# Aperture photometry 3*Seeing 
#
#######

PHOT_APER=FWHMpsf*3.

TOT_APER=str(PHOT_APER)+","+NAPERTURES
print TOT_APER

iraf.daophot.fitskypars.annulus=ANNULUS   
iraf.daophot.fitskypars.dannulus=DANNULUS  
#iraf.daophot.photpars.apertures=NAPERTURES
iraf.daophot.photpars.apertures=TOT_APER
iraf.daophot.photpars.zmag=ZERO_POINT

iraf.delete(files=image+".phot.1",verify='no')

iraf.daophot.phot.coords=image+".coo"
iraf.daophot.phot.output=image+".phot.1"
iraf.daophot.phot.verify="no"
iraf.daophot.phot.verify="no"
iraf.daophot.verify="no"
iraf.daophot.verify="no"

#iraf.daophot.phot(image=image+area+"[sci]",verify="no")
iraf.daophot.phot(image=image+area+"[sci]",verify="no")

###################
#
#
#  txdump, 
Beispiel #47
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False):
    try:
        import lsc, string

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Beispiel #48
0
                _catalog = lsc.util.getcatalog(img, system)
                if _catalog:
                    break
        if '.fits' in img: img = img[:-5]
        if os.path.exists(img + '.sn2.fits') and not option.redo:
            print img + ': psf already calculated'
        else:
            fwhm0 = option.fwhm
            while True:
                result, fwhm = ecpsf(img, fwhm0, option.threshold, option.psfstars,
                                     option.distance, option.interactive, psffun,
                                     fixaperture, _catalog, option.datamax, option.show)
                print '\n### ' + str(result)
                if option.show:
#                    lsc.util.marksn2(img + '.fits', img + '.sn2.fits', 1, '')
                    iraf.delete('tmp.psf.fit?', verify=False)
                    iraf.seepsf(img + '.psf', '_psf.psf')
                    iraf.surface('_psf.psf')
                    aa = raw_input('>>>good psf [[y]/n] ? ')
                    if not aa or aa.lower()[0] == 'y':
                        break
                    if aa.lower()[0] == 'n':
                        result = 0
                        bb = raw_input('If you want to try again, type the new FWHM to try here. Otherwise press enter to continue. ')
                        if bb: fwhm0 = float(bb)
                        else: break
                else: 
                    break

            iraf.delete("tmp.*", verify="no")
            iraf.delete("_psf.*", verify="no")
Beispiel #49
0
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False):
    try:
        import lsc, string

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    except:
        result = 0
        fwhm = 0.0
        traceback.print_exc()
    return result, fwhm * scale
Beispiel #50
0
def extractOneD(inputList,
                kind,
                log,
                over,
                extractionXC=15.0,
                extractionYC=33.0,
                extractionRadius=2.5):
    """Extracts 1-D spectra with iraf.nfextract and combines them with iraf.gemcombine.
    iraf.nfextract is currently only done interactively. Output: -->xtfbrsn and gxtfbrsn

    NFEXTRACT - Extract NIFS spectra.

    This could be used to extract a 1D spectra from IFU data and is
    particularly useful for extracting the bright spectra of
    telluric calibrator stars. Note that this routine only works
    on data that has been run through NFTRANSFORM.

    """

    for frame in inputList:
        frame = str(frame).strip()
        if os.path.exists("xtfbrsn" + frame + ".fits"):
            if over:
                iraf.delete("xtfbrsn" + frame + ".fits")
            else:
                logging.info(
                    "Output file exists and -over not set - skipping nfextract in extract1D"
                )
                continue

        iraf.nfextract("tfbrsn" + frame,
                       outpref="x",
                       xc=extractionXC,
                       yc=extractionYC,
                       diameter=extractionRadius,
                       fl_int='no',
                       logfile=log)
    inputList = checkLists(inputList, '.', 'xtfbrsn', '.fits')
    # Combine all the 1D spectra to one final output file with the name of the first input file.
    combined = str(inputList[0]).strip()
    if len(inputList) > 1:
        if os.path.exists("gxtfbrsn" + combined + ".fits"):
            if over:
                iraf.delete("gxtfbrsn" + combined + ".fits")
            else:
                logging.info(
                    "Output file exists and -over not set - skipping gemcombine in extract1D"
                )
                return
        iraf.gemcombine(listit(inputList, "xtfbrsn"),
                        output="gxtfbrsn" + combined,
                        statsec="[*]",
                        combine="median",
                        masktype="none",
                        fl_vardq="yes",
                        logfile=log)
    else:
        if over:
            iraf.delete("gxtfbrsn" + combined + ".fits")
        iraf.copy(input="xtfbrsn" + combined + ".fits",
                  output="gxtfbrsn" + combined + ".fits")

    if kind == 'Telluric':
        # Put the name of the final combined file into a text file called
        # telluricfile to be used by the pipeline later.
        open("telluricfile", "w").write("gxtfbrsn" + combined)
    elif kind == 'Science':
        open("combinedOneD", "w").write("gxtfbrsn" + combined)
Beispiel #51
0
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun='gauss', fixaperture=False):
    ''' 
    giving an image, a psffile compute the psf using the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.photpars.zmag = zmag

    iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False)
    iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False)

    # removes saturated stars from the list (IRAF just issues a warning)
    with open('_psf.mag') as f:
        text = f.read()
    text = re.sub('(.*\n){6}.*BadPixels\* \n', '', text)
    with open('_psf.mag', 'w') as f:
        f.write(text)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord

    if interactive: # not possible to run pstselect or psf interactively on 64-bit linux (Error 851)
        shutil.copyfile('_psf.mag', '_psf.pst')
        print '_' * 80
        print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \
              ' "w"-write and "q"-uit (cursor on ds9)'
        print '-' * 80
    else:
        iraf.pstselect(img+'[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False)

    iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive,
             verify=False, verbose=False)
    iraf.group(img + '[0]', '_psf.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_psf.nst', '_psf.nrj', verify=False, verbose=False)

    photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, pst, fitmag