Ejemplo n.º 1
0
def txdump_photometry_info(target_dir, data_dir_name):
    """Extract the results of photometry from files to save them to a text file.
    
    This function search files containing the results of photometry
    and using txdump extracts the coordinates and photometric measure
    to save it to a text file. 
    
    Args:    
        target_dir: Directory that contains the files to process.
        data_dir_name: Name for the directories with data.    
    
    """
    
    # Walk from current directory.
    for path,dirs,files in os.walk(target_dir):
        
        # Process only directories without subdirectories.
        if len(dirs) == 0:
            split_path = path.split(os.sep) 
            
            # Check if current directory is for data images.
            if split_path[-2] == data_dir_name:
                logging.debug("Found a directory for data: %s" % (path))

                # Get the list of magnitude files.
                mag_files = glob.glob(os.path.join(path, "*.%s" %
                                                   MAGNITUDE_FILE_EXT))
                
                logging.debug("Found %d magnitude files" % (len(mag_files)))    
                
                # Reduce each data file one by one.
                for mfile in mag_files:                  
    
                    # Get the name of the file where the magnitude data will 
                    # be saved.
                    mag_dest_file_name = \
                        mfile.replace(".%s" %(MAGNITUDE_FILE_EXT), 
                                      "%s%s.%s" %
                                      (FILE_NAME_PARTS_DELIM,
                                      MAGNITUDE_FILE_EXT, CSV_FILE_EXT))
                    
                    # Remove the destiny file if exists.
                    if os.path.exists(mag_dest_file_name):
                        os.remove(mag_dest_file_name)
                        
                    try:                                            
                        mag_dest_file = open(mag_dest_file_name, 'w' )
                    
                        iraf.txdump(mfile, fields=TXDUMP_FIELDS, expr='yes', \
                                    Stdout=mag_dest_file)
                        
                        mag_dest_file.close()
                        
                    except iraf.IrafError as exc:
                        logging.error("Error executing txdump to get: %s" %
                                      (mag_dest_file_name))
                        logging.error("Iraf error is: %s" % (exc)) 
                        
                    except IOError as ioe:
                        logging.error("Reading file: %s" % (mag_dest_file_name))                                           
Ejemplo n.º 2
0
def Photometry(setn, flist, band):
    # J-band
    os.system("head -1 -q %s/xylist/%sf*pos | awk '{print $3}'> tmp" %
              (setn, band))
    ff = open("tmp")
    fr = ff.readlines()
    ff.close
    r = []
    for x in fr:
        r.append(float(x))
    radi = np.nanmean(r)
    print s, band, radi

    # aperture,annulus,dannulus
    iraf.apphot.photpars.apertures = radi
    iraf.apphot.fitskypars.annulus = radi + 4
    iraf.apphot.fitskypars.dannulus = 10

    for x in flist:
        if x[0] in s:
            infile = "%s/xylist/%sf%s.pos" % (setn, band, x[1])
            outfile = "%s/photometry/%sf%s.mag" % (setn, band, x[1])
            dumped = "%s/photometry/%sf%s.txt" % (setn, band, x[1])
            fits = "%s/%sf%smasked.fits" % (setn, band, x[1])
            print infile, fits
            iraf.phot(fits, coords=infile, output=outfile, wcsin="physical")
            iraf.txdump(outfile,
                        fields="xc,yc,mag,merr,flux,msky",
                        expr="yes",
                        Stdout=dumped)
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def transform(self, image: Image):

        if not self.requirements_check(image):
            raise ValueError(
                "Missing required transformations on image. Need:" +
                str(self.REQUIRES))
        # INIT
        from pyraf import iraf
        iraf.noao.digiphot(_doprint=0)
        iraf.noao.digiphot.daophot(_doprint=0)
        capable.OF_GRAPHICS = False

        # File handling
        temp_final_out = CONFIG["FILE_DUMP"] + 'TempPhotOut.dat'
        if os.path.exists(temp_final_out):
            os.remove(temp_final_out)

        base = image.fixed_parameters["path"].split('/')[-1]
        dao_phot_out = CONFIG["FILE_DUMP"] + base + ".mag.dat"
        phot_txt_out = CONFIG["FILE_DUMP"] + base + "PhoTxOut.dat"

        # Setting pyraf phot parameters
        photpars = iraf.photpars.getParList()
        iraf.photpars.setParam('apertures', CONFIG["APERTURE"])
        iraf.phot.setParam('image', image.fixed_parameters["path"])
        iraf.phot.setParam('coords', self._get_coordinates_file(image))
        iraf.phot.setParam('verify', 'no')
        iraf.phot.setParam('output', dao_phot_out)
        iraf.phot.setParam('interactive', 'no')

        if os.path.exists(dao_phot_out):
            os.remove(dao_phot_out)

        # Running IRAF task
        dump = iraf.phot(mode='h', Stdout=1)

        if os.path.exists(phot_txt_out):
            os.remove(phot_txt_out)

        # Getting better formatted file with magnitudes
        iraf.txdump(dao_phot_out,
                    'XCENTER,YCENTER,MAG,MERR',
                    'yes',
                    Stdout=phot_txt_out)

        # Getting results from output file
        with open(phot_txt_out) as output_file:
            results = {}
            i = 0
            for lines in output_file:
                parts = lines.split()
                results[self.objects[i].fixed_parameters["id"]] = (
                    float(parts[2]) if parts[2] != "INDEF" else None,
                    float(parts[3]) if parts[3] != "INDEF" else None)
                i += 1

        image.processing_parameters["photometry"] = results
        return image
Ejemplo n.º 6
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
Ejemplo n.º 7
0
def save_matched_coords(image, coordfile='',output='',box=50,**kwargs):
    '''
    Finds and stores co-ordinates of the objects on all the frames based on the reference co-ordinates
    '''
    im = filenames2list(image)
    imlist = [im] if type(im)==str else im
    if output!='':
        output = filenames2list(output)
    urefcoo = True
    coordsprefix = kwargs.get('coordsprefix','')
    if coordfile=='':
        refim = kwargs.get('refimage', 0)
        if type(refim) == int:
            refim, irefim = imlist[refim], refim
        else:
            refim, irefim = refim, imlist.index(refim)
        coordfile = get_coords_filename_from_prefix(image=refim, prefix=coordsprefix)
        urefcoo = kwargs.get('update_refim_coords',True)
    if type(im)==str:
        if output=='':
            output = get_coords_filename_from_prefix(image=im,prefix=coordsprefix)
        if coordfile!=output and not urefcoo:
            os.system(f'cp {coordfile} {output}')
        if urefcoo:
            task = ir.center
            task.centerpars.calgorithm = 'centroid'
            task.centerpars.cbox = box
            task.centerpars.cmaxiter = 10
            task.centerpars.maxshift = 2
            task.plotfile = ''
            task.interactive = 'no'
            task.radplots = 'no'
            task.verify = 'no'
            task.update = 'no'
            task.verbose = 'no'
            if os.path.exists(j2s('__coo__')):
                os.remove(j2s('__coo__'))
            task(image=im, coords=coordfile, output=j2s('__coo__'))
            ir.txdump(textfiles=j2s('__coo__'), fields='xcenter,ycenter', expr='yes', headers='no', parameters='no', Stdout=output)
        return output
    if type(output)==str and output!='':
        warn('For multiple input images you need to give multiple output files either as a tuple or list of textfiles '
             'or a comma separated string of textfilenames or a list of filenames. Output file names will be set '
             'automatically for now.')
        output = ''
    oplist = []
    for i,im in enumerate(imlist):
        if output!='':
            j = list(range(len(imlist))).remove(irefim)[i]
            op = output[j]
        else:
            op = ''
        oplist.append(save_matched_coords(im,coordfile,op,box,**kwargs))
    return oplist
Ejemplo n.º 8
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
Ejemplo n.º 9
0
def get_phot_output(output, silent=False):
    from pyraf import iraf as ir

    # Now get the results using txdump
    radStr = ir.txdump(output, 'RAPERT', 'yes', Stdout=1)
    fluxStr = ir.txdump(output, 'FLUX', 'yes', Stdout=1)
    magStr = ir.txdump(output, 'MAG', 'yes', Stdout=1)
    merrStr = ir.txdump(output, 'MERR', 'yes', Stdout=1)
    pierStr = ir.txdump(output, 'PIER', 'yes', Stdout=1)

    radFields = radStr[0].split()
    fluxFields = fluxStr[0].split()
    magFields = magStr[0].split()
    merrFields = merrStr[0].split()
    pierFields = pierStr[0].split()

    count = len(radFields)

    radius = np.zeros(count, dtype=float)
    flux = np.zeros(count, dtype=float)
    mag = np.zeros(count, dtype=float)
    merr = np.zeros(count, dtype=float)

    for rr in range(count):
        radius[rr] = float(radFields[rr])

        if (int(pierFields[rr]) != 0 or magFields[rr] == 'INDEF'
                or merrFields[rr] == 'INDEF'):
            print('Problem in image: ' + output)

            # Error
            flux[rr] = 0
            mag[rr] = 0
            merr[rr] = 0
        else:
            flux[rr] = float(fluxFields[rr])
            mag[rr] = float(magFields[rr])
            merr[rr] = float(merrFields[rr])

    if not silent:
        print('%6s  %10s  %6s  %6s' % ('Radius', 'Flux', 'Mag', 'MagErr'))
        for ii in range(count):
            print( '%8.1f  %10d  %6.3f  %6.3f' % \
                (radius[ii], flux[ii], mag[ii], merr[ii]))

    return (radius, flux, mag, merr)
Ejemplo n.º 10
0
def get_phot_output(output, silent=False):
    # Now get the results using txdump
    radStr = ir.txdump(output, 'RAPERT', 'yes', Stdout=1)
    fluxStr = ir.txdump(output, 'FLUX', 'yes', Stdout=1)
    magStr = ir.txdump(output, 'MAG', 'yes', Stdout=1)
    merrStr = ir.txdump(output, 'MERR', 'yes', Stdout=1)
    pierStr = ir.txdump(output, 'PIER', 'yes', Stdout=1)

    radFields = radStr[0].split()
    fluxFields = fluxStr[0].split()
    magFields = magStr[0].split()
    merrFields = merrStr[0].split()
    pierFields = pierStr[0].split()

    count = len(radFields)

    radius = np.zeros(count, dtype=float)
    flux = np.zeros(count, dtype=float)
    mag = np.zeros(count, dtype=float)
    merr = np.zeros(count, dtype=float)

    for rr in range(count):
        radius[rr] = float(radFields[rr])

        if (int(pierFields[rr]) != 0 or magFields[rr] == 'INDEF' or
            merrFields[rr] == 'INDEF'):
            print 'Problem in image: ' + output

            # Error
            flux[rr] = 0
            mag[rr] = 0
            merr[rr] = 0
        else:
            flux[rr] = float(fluxFields[rr])
            mag[rr] = float(magFields[rr])
            merr[rr] = float(merrFields[rr])

    if not silent:
        print '%6s  %10s  %6s  %6s' % ('Radius', 'Flux', 'Mag', 'MagErr')
        for ii in range(count):
            print '%8.1f  %10d  %6.3f  %6.3f' % \
                (radius[ii], flux[ii], mag[ii], merr[ii])
    
    return (radius, flux, mag, merr)
Ejemplo n.º 11
0
def calc_apcorr(file):

	'''  MEASURE APPERTURE CORRECTION GIVEN TWO MAGNITUDES'''
	'''    file -- daophot filename (expects only 2 circular aperture magnitude estimates in file)'''

	# Save a trimmed daophot catalog with columns relevant to this study (xc,yc,mag1,mag2)
        trimfile = file+'.trimmed'
        file_query = os.access(trimfile, os.R_OK)
        if file_query == True: os.remove(trimfile)      # remove previous trimmed files
        iraf.txdump(file,'xcenter,ycenter,mag', 'yes', Stdout=trimfile)

        # Measure the aperture correction assuming a pt source
        xc, yc, mag1,mag2 = np.loadtxt(trimfile, unpack=True)
        deltamag=mag1-mag2

	# Use somewhat hacky method to extract box in mag.vs.deltamag plot where we will estimate median deltamag
	xgd=[21.8,25.0]		# fixed--works well for both filters
	#estimate y-range from a rough median estimate
	tmp_median = np.median(deltamag[(mag1 >= xgd[0])&(mag1 <= xgd[1])&(np.abs(deltamag) < 1)])
	ygd=[tmp_median-0.1, tmp_median+0.1]
        f_deltamag=deltamag[(mag1 >= xgd[0])&(mag1 <= xgd[1])&(deltamag > ygd[0])&(deltamag <ygd[1])]     # avoid saturated and faint sources
	ac05 = np.median(f_deltamag)


	# Plot the aperture correction on delta_mag vs. mag scatter diagram
        pylab.scatter(mag1, deltamag,s=1.0,c='r',marker='o')
        pylab.ylim(-0.5,1.0)
        pylab.xlim(19.0,29.0)
        pylab.xlabel('m3')
        pylab.ylabel('m3 - m16')
        pylab.axhline(0.0, c='k', ls='--', linewidth=3)
        pylab.axhline(ac05)
	pylab.vlines(xgd,ygd[0],ygd[1], colors='red',linestyles='dotted')
	pylab.hlines(ygd,xgd[0],xgd[1], colors='red',linestyles='dotted')
        pylab.annotate('ac05='+str(ac05),[19.3,ac05-0.07])
        pylab.annotate('m_corr = m1 - ac05 - AC05',[19.3,-0.4])
        fsplit=file.split('_')
        pylab.annotate(fsplit[0], [20.0,-0.2],color='r')
        pylab.savefig('deltamag_vs_mag_'+fsplit[0]+'.pdf')
        pylab.clf()

	return ac05
Ejemplo n.º 12
0
 def textdump(self):
     '''
     Prepares photometry data for science
     '''
     
     countlist = glob.glob(root + '/*counts.fits')
     photlist = glob.glob(root + '/*phot.dat')
     
     for image, phot_file in zip(countlist, photlist):
         saveout = sys.stdout
         fsock = open(image[:-12] + '_txtdump_tmp.dat', 'w')
         sys.stdout = fsock
         iraf.txdump(textfiles=phot_file, \
                     fields='xcenter,ycenter,id,msky,area,flux,mag,merr', \
                     expr='yes', headers='yes', parameters='no')
         sys.stdout = saveout
         fsock.close()
         sfile = open(image[:-12] + '_txtdump_tmp.dat')
         rfile = open(image[:-12] + '_txtdump.dat', 'w')
         for s in sfile:
             rfile.write(s.replace('INDEF', ' 99.99 '))
         sfile.close()
         rfile.close()
         os.remove(image[:-12] + '_txtdump_tmp.dat')
Ejemplo n.º 13
0
    def _get_photometry(self, seeing=None, aperture=None):
        """ Get the photometry for the target.

        If the target is a standard star, aperture photometry will be performed. For the moment nothing is done with
        the others, but in due time (TODO) photometry.py will be included here. """

        if self.objtype is not "standards":
            return None

        basename = "standards"
        fd, coords_file = tempfile.mkstemp(prefix=basename, suffix=".coords")
        os.write(fd, "{0} {1} \n".format(self.RA, self.DEC))
        os.close(fd)


        # If aperture was not given by the user, try and use the seeing as a reference for default values
        if not aperture:
            try:
                seeing = self.header.hdr[self.header.seeingk]
                aperture = 3 * seeing
            except ValueError:  # keyword was not correctly guessed
                pass

        # If aperture exists, do the photometry using it
        if aperture:
            annulus = 2 * aperture
            dannulus = max([aperture, 3])  # minimum of 3 pixels thickness for the sky annulus
            fd, photfile_name = tempfile.mkstemp(".mag.1")
            utilities.if_exists_remove(photfile_name)
            kwargs =  dict(output=photfile_name, coords=coords_file, salgorithm='median',
                      wcsin='world', fwhm=seeing, gain=self.header.gaink, exposure=self.header.exptimek,
                      airmass=self.header.airmassk, annulus=annulus, dannulus=dannulus,
                      apertures=aperture, verbose="no", verify="no", interac="no")
            iraf.phot(self.header.im_name, **kwargs)
            [counts] = iraf.txdump(photfile_name, 'FLUX', 'yes', Stdout=subprocess.PIPE)
        else:
            sys.exit("\n \n Sorry, no aperture was passed by you, and a seeing keyword was not "
                     "found in the header. \n\n ")

        utilities.if_exists_remove(coords_file)
        return float(counts)
Ejemplo n.º 14
0
 def _getfrommagfile(magfile,get_t=True,get_apert=True,get_others=True):
     res = []
     if get_t:
         ir.txdump(magfile, fields='otime', expr='yes', headers=False, parameters=False, Stdout=j2s('__obs__'))
         try: t = np.loadtxt(j2s('__obs__'),)
         except ValueError: t = np.loadtxt(j2s('__obs__'), usecols=0, dtype=object)
         res.append(t)
         os.remove(j2s('__obs__'))
     if get_others:
         print(magfile)
         ir.txdump(magfile, fields='itime,stdev,nsky', expr='yes', headers=False, parameters=False, Stdout=j2s('__isn__'))
         exptime, stdev, nsky = np.loadtxt(j2s('__isn__'), unpack=True)
         os.remove(j2s('__isn__'))
         ir.txdump(magfile, fields='flux', expr='yes', headers=False, parameters=False, Stdout=j2s('__flux__'))
         flux = np.loadtxt(j2s('__flux__'), ndmin=2)
         os.remove(j2s('__flux__'))
         ir.txdump(magfile, fields='area', expr='yes', headers=False, parameters=False, Stdout=j2s('__area__'))
         area = np.loadtxt(j2s('__area__'), ndmin=2)
         os.remove(j2s('__area__'))
         ir.txdump(magfile, fields='mag', expr='yes', headers=False, parameters=False, Stdout=j2s('__mag__'))
         mag = np.loadtxt(j2s('__mag__'), ndmin=2)
         os.remove(j2s('__mag__'))
         res += [exptime, stdev, nsky,flux,area,mag]
     if get_apert:
         ir.txdump(magfile, fields='rapert', expr='yes', headers=False, parameters=False, Stdout=j2s('__apert__'))
         apert = np.loadtxt(j2s('__apert__'))[0]
         res.append(apert)
         os.remove(j2s('__apert__'))
     return res
Ejemplo n.º 15
0
def manu(dmag0, apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag,
         truemag, magerr, centx, centy, z22, z11, midpt, size, fwhm0, img, x1,
         y1, arterr):
    from pyraf import iraf
    import string, os, sys
    from numpy import array

    a1 = int(fwhm0 + .5)
    a2 = int(2. * fwhm0 + .5)
    a3 = int(3. * fwhm0 + .5)
    a4 = int(4. * fwhm0 + .5)
    a5 = int(5. * fwhm0 + .5)
    ap = str(a1) + "," + str(a2) + "," + str(a3)

    fdmag = 10**(-0.4 * float(dmag0))
    iraf.delete(",_snfit.fit?,skyfit.fit?", ve='no')
    iraf.imarith("snfit", "*", fdmag, "_snfit")
    iraf.imarith("original", "-", "_snfit", "skyfit")
    iraf.display("original",
                 1,
                 fill='yes',
                 xcen=.25,
                 ycen=.25,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z22,
                 z1=z11)
    z01 = z11 - midpt
    z02 = z22 - midpt
    s1 = 1
    s2 = -int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='yes',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    iraf.display("_snfit",
                 1,
                 erase='no',
                 fill='yes',
                 xcen=.25,
                 ycen=.75,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z02,
                 z1=z01)
    iraf.delete("tmptbl", ve='no')
    tmptbl = iraf.txdump(img + ".sn.als", "xcen,ycen", expr='yes', Stdout=1)
    ff = open('tmptbl', 'w')
    for i in tmptbl:
        ff.write(i)
    ff.close()
    lra = int((2 * size * fwhm0) * 2)
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="circle",
                number='yes',
                nyoffset=lra,
                radi=a2,
                txsize=2,
                inter='no')
    s1 = 1
    s2 = -int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " FITTED")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)
    iraf.display("skyfit",
                 1,
                 erase='no',
                 fill='yes',
                 xcen=.75,
                 ycen=.25,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z22,
                 z1=z11)
    s1 = 1
    s2 = -int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    #   newmag=truemag
    #   for i in range(0,len(truemag)):
    #       try:
    #           newmag[i]=float(truemag[i])+float(dmag0)
    #       except:
    #           newmag[i]=float(dmag0)
    newmag = list(array(truemag) + float(dmag0))

    print "***************************************************************************"
    print "#id  x_ori   y_ori     x     y    ap_ori ap_bgsub  fit_mag  err_art  err_fit"
    for i in range(len(fitmag)):
        print "SN", i, str(centx[i] + x1), str(centy[i] + y1), str(
            centx[i]), str(centy[i]), "  ", str(apori3[i]), "  ", str(
                apmag3[i]), "  ", str(newmag[i]), "  ", str(arterr), "  ", str(
                    magerr[i])
    print "**************************************************************************"

    return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy, newmag
Ejemplo n.º 16
0
if __name__ == "__main__":
    iraf.imred()
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s: %(message)s')
    logger = logging.getLogger(__name__)
    #iraf.reset(use_new_imt="no")
    root = sys.argv[1]
    logger.info("Root: " + sys.argv[1])
    fitsExt = ".fits"

    cooFnRgx = "*.coo"

    bfns = [os.path.splitext(os.path.relpath(os.path.join(dirpath, f), root))[0]
            for dirpath, dirnames, files in os.walk(root)
            for f in fnmatch.filter(files, cooFnRgx)]
    bfns.sort()

    for bfn in bfns:
        logger.debug("Doing photometry on " + bfn)
        phot(root + bfn, root + bfn + ".mag")

    txdmpFnRgx = "*.txdmp"
    txdmpFns = [os.path.relpath(os.path.join(dirpath, f), root)
            for dirpath, dirnames, files in os.walk(root)
            for f in fnmatch.filter(files, txdmpFnRgx)]
    txdmpFns.sort()

    for fn in txdmpFns:
        #txdump *.mag.1 image,id,mag,otime yes > output.txt
        iraf.txdump(textfiles = "@" + root + fn, fields= "id,mag,rapert,ifilter", expr =  "yes", Stdout = root + fn + ".res")
Ejemplo n.º 17
0
def performPhotometry(task, logger):
  #iraf.prcacheOff()
  [iraf.unlearn(t) for t in ('phot','pstselect','psf','allstar')]
  iraf.set(imtype="fits,noinherit")   # set image output format
  iraf.set(clobber="yes")
  hdu=pyfits.open(task['images'])[0] 
  hdr = hdu.header
  imdata = hdu.data  
  for key,value in task['fits'].iteritems():
    task[key] = hdr.get(value,1)

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

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

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

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

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

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

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

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

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

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

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


  for phototype in photometry:
    kwargs = dict(input='STDIN',
                  output='STDOUT',
                  insystem='%s physical' % task['images'],
                  outsystem='%s world' % task['images'],
                  ilatuni='physical',
                  ilnguni='physical',
                  olnguni='degrees',
                  olatuni='degrees',
                  ilngfor='%10.7f',
                  ilatfor='%10.7f',
                  olngfor='%10.5f',
                  olatfor='%10.5f',
                  Stdin=photometry[phototype],Stdout=1)
    photometry[phototype] = [i.split() for i in iraf.skyctran(**kwargs) if i and not i.startswith('#') and 'INDEF' not in i]
    photometry[phototype] = [map(float,(i[4],i[5],i[2],i[3])) for i in photometry[phototype] ] #Now we have [(ra,dec,'mag','mageerr'),...]
  results = calibrate((task['objwcs'][0],task['objwcs'][1]),task,photometry,logger)
#  if 'PSF' not in results:
    
  return results
Ejemplo n.º 18
0
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2, plotdir=".", box=15):
    '''
    coords: files: 
    wcsin: can be "world", "logic"
    '''
    # Load packages; splot is in the onedspec package, which is in noao. 
    # The special keyword _doprint=0 turns off displaying the tasks 
    # when loading a package. 
    
    if (not plot_only):
        iraf.noao(_doprint=0)
        iraf.digiphot(_doprint=0)
        iraf.apphot(_doprint=0)
        iraf.unlearn("apphot")

    imdir = os.path.dirname(image)
    imname = os.path.basename(image)
    plotdir = os.path.join(imdir, "photometry")
    
    if not os.path.isdir(plotdir):
        os.makedirs(plotdir)
        
    out_name = os.path.join(plotdir, imname +  ".seq.mag")
    clean_name = os.path.join(plotdir, imname +  ".app.mag")

    
    # Read values from .ec file
    ecfile= image+".ec"
    filter_value=''.join(ecfile).split('.',1)[0]
    
    fwhm_value = fwhm
    
    if (fitsutils.has_par(image, 'FWHM')):
        fwhm_value = fitsutils.get_par(image, 'FWHM')
    if (fitsutils.has_par(image, 'AIRMASS')):
        airmass_value = fitsutils.get_par(image, 'AIRMASS')
    else:
	airmass_value = 1.3
    exptime = fitsutils.get_par(image, 'EXPTIME')
    gain = fitsutils.get_par(image, 'GAIN')

    try:      
        with open(''.join(ecfile),'r') as f:
            for line in f:
                if "airmass" in line:
                    airmass_value = line.split('=',1)[1]
                else:
                    airmass_value = 1
                if "FWHM" in line:
                    print line
                    fwhm_value =  line.split('FWHM=',1)[1]
                    fwhm_value = fwhm_value.rsplit("aperture")[0]
    except:
        pass
    
    print "FWHM", fwhm_value
    aperture_rad = math.ceil(float(fwhm_value)*2)      # Set aperture radius to three times the PSF radius
    sky_rad= math.ceil(aperture_rad)*5
    
    print aperture_rad, sky_rad

    if (not plot_only):

        if os.path.isfile(out_name): os.remove(out_name)
        if os.path.isfile(clean_name): os.remove(clean_name)

        # Check if files in list, otherwise exit
        if not ecfile:
           print "No .ec files in directory, exiting"
           sys.exit()
        
        
   
   
        iraf.noao.digiphot.apphot.qphot(image = image,\
        cbox = box ,\
        annulus = sky_rad ,\
        dannulus = 15. ,\
        aperture = str(aperture_rad),\
        coords = coords ,\
        output = out_name ,\
        plotfile = "" ,\
        zmag = 0. ,\
        exposure = "exptime" ,\
        airmass = "airmass" ,\
        filter = "filters" ,\
        obstime = "DATE" ,\
        epadu = gain ,\
        interactive = "no" ,\
        radplots = "yes" ,\
        verbose = "no" ,\
        graphics = "stdgraph" ,\
        display = "stdimage" ,\
        icommands = "" ,\
        wcsin = wcsin,
        wcsout = "logical",
        gcommands = "") 
        
         
        #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name)
        iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name)
        
    
    ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"),  ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")])
    if (ma.size > 0):    
        m = ma[~np.isnan(ma["fit_mag"])]
    else:
        print "Only one object found!"
        m = np.array([ma])
        
    hdulist = pf.open(image)
    prihdr = hdulist[0].header
    img = hdulist[0].data * 1.
    nx, ny = img.shape

    
    
    dimX = int(4)
    dimY = int(np.ceil(len(m)*1./4))
    outerrad = sky_rad+10
    cutrad = outerrad + 15
    
    plt.suptitle("FWHM="+str(fwhm_value))
    k = 0
    for i in np.arange(dimX):
        for j in np.arange(dimY):
            if ( k < len(m)):
                ax = plt.subplot2grid((dimX,dimY),(i, j))
                y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad
                y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2)
                try:
                    zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25)
                except:
                    sh= img[x1:x2,y1:y2].shape
                    if sh[0]>0 and sh[1]>0:
                        zmin = np.nanmin(img[x1:x2,y1:y2])
                        zmax = np.nanmax(img[x1:x2,y1:y2])
                        continue
                    else:
                        continue
                ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax)
                c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=5.)
                c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad)
                c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+10)
                plt.gca().add_artist(c1)
                plt.gca().add_artist(c2)
                plt.gca().add_artist(c3)
                ax.set_xticks([])
                ax.set_yticks([])
        
                plt.text(+5, +5, "%d"%m[k]["id"])
                plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b")
            k = k+1
    
    plt.savefig(os.path.join(plotdir, imname + "plot.png"))
    plt.clf()
Ejemplo n.º 19
0
import numpy as np
import astropy.io.fits as pyfits
from pyraf import iraf
from iraf import noao, digiphot, apphot, phot, txdump
import matplotlib
from matplotlib import pyplot as plt

iraf.noao.digiphot.apphot.phot.interac = 'no'
iraf.noao.digiphot.apphot.phot.centerp.calgori = 'none'
iraf.noao.digiphot.apphot.phot.photpar.apertur = '3.9'
iraf.noao.digiphot.apphot.phot.fitskypars.salgori = 'constant'
iraf.noao.digiphot.apphot.phot.cache = 'yes'
iraf.noao.digiphot.apphot.phot.salgori = 'constant'

iraf.phot(image='hff_abells1063-hffpar_f140w_wht.fits',
          interactive='no',
          calgorithm='none',
          salgori='constant',
          coords='box_coords_f140w.coo',
          output='PHOT_WHT_hff_abells1063_par_f140w.txt',
          verify='no')

iraf.txdump(textfile='PHOT_WHT_hff_abells1063_par_f140w.txt',
            fields='XCEN, YCEN, SUM',
            expr='yes',
            Stdout='PHOT_WHT_hff_abells1063_par_f140w_OUTFILE.txt')
Ejemplo n.º 20
0
def build(f):

    ### is this an MEF file
    current_ext = 0
    NEXTEND = 0

    EXTEND = f[0].header["EXTEND"]
    if EXTEND == "T":
        NEXTEND = f[0].header["NEXTEND"]
        current_ext = 1

    ### create the name of the output MEF psf file
    if not f[0].header.has_key("FILENAME"):
        os.unlink(opt.filename)
        sys.exit("The fits file " + opt.filename + " has no EXPNUM keyword\n")

    sexp = f[0].header["FILENAME"]
    mef_psf = sexp + "p_psf_iraf.fits"
    ### create an MEF file that will contian the PSF(s)
    import pyfits

    fitsobj = pyfits.HDUList()
    prihdu = pyfits.PrimaryHDU()
    import re

    prihdu.header.update("FILENAME", sexp, comment="CFHT Exposure Numebr")
    prihdu.header.update("NEXTEND", NEXTEND, comment="number of extensions")
    version = re.match(r"\$Rev.*: (\d*.\d*) \$", __Version__).group(1)
    prihdu.header.update("MKPSF_V", float(version), comment="Version number of mkpsf")
    fitsobj.append(prihdu)
    if os.access(mef_psf, os.F_OK):
        os.unlink(mef_psf)
    fitsobj.writeto(mef_psf)
    fitsobj.close()
    outfits = pyfits.open(mef_psf, "append")
    prihdr = outfits[0].header

    import jjkmode

    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

    ### keep all the parameters locally cached.
    iraf.set(uparm="./")

    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()

    ### temp file name hash.
    tfile = {}

    while current_ext <= NEXTEND:

        ### this is a psf SCRIPT so the showplots and interactive are off by force

        print "Working on image section " + str(current_ext)

        iraf.findpars.sharplo = 0
        iraf.findpars.sharphi = 0.7
        iraf.findpars.roundlo = -0.7
        iraf.findpars.roundhi = 0.7

        iraf.datapars.datamax = 20000
        iraf.datapars.airmass = "AIRMASS"
        iraf.datapars.filter = "FILTER"
        iraf.datapars.obstime = "TIME-OBS"
        iraf.datapars.exposure = "EXPTIME"
        iraf.datapars.gain = "GAIN"
        iraf.datapars.ccdread = "RDNOISE"
        iraf.datapars.fwhmpsf = opt.fwhm

        iraf.daopars.nclean = 2
        iraf.daopars.psfrad = 5.0 * opt.fwhm
        iraf.daopars.fitrad = 0.85 * opt.fwhm
        iraf.daopars.function = "gauss"

        iraf.centerpars.calgorithm = "centroid"

        zero_mag = 26.19
        iraf.photpars.zmag = zero_mag
        iraf.photpars.apertures = int(0.85 * opt.fwhm)
        iraf.fitskypars.annulus = 2 + int(opt.fwhm * 4.00)
        iraf.fitskypars.dannulus = int(opt.fwhm * 2.0)
        iraf.daophot.verbose = no
        iraf.daophot.verify = no
        iraf.daophot.update = no

        iraf.psf.interactive = no
        iraf.pstselect.interactive = no

        iraf.datapars.saveParList()
        iraf.fitskypars.saveParList()
        iraf.centerpars.saveParList()
        iraf.findpars.saveParList()
        iraf.photpars.saveParList()

        tfiles = [
            "coo_bright",
            "coo_ok",
            "coo_faint",
            "mag_all",
            "mag_bright",
            "mag_ok",
            "mag_good",
            "mag_best",
            "pst_in",
            "pst_out",
            "pst_out2",
            "prf",
            "psg_org",
            "psg",
            "psf_1.fits",
            "psf_2.fits",
            "psf_final.fits",
            "psf_3.fits",
            "psf_4.fits",
            "mag_pst",
            "coo_pst",
            "nst",
            "nrj",
            "seepsf.fits",
            "sub.fits",
            "fwhm",
            "apcor",
        ]

        for file in tfiles:
            extname = "chip" + str(f[current_ext].header.get("IMAGEID", str(current_ext))).zfill(2)
            tfile[file] = sexp + "_" + extname + "." + file
            if os.access(tfile[file], os.F_OK):
                os.unlink(tfile[file])

        if EXTEND == "T":
            this_image = opt.filename + "[" + extname + "]"
        else:
            this_image = opt.filename
        gain = f[current_ext].header.get("GAIN", 1.0)
        #### set sky/sigma parameters specific to this frame.
        (skyvalue, sigma) = jjkmode.stats(f[current_ext].data)
        import math

        sigma = math.sqrt(skyvalue / gain)
        datamin = float(skyvalue) - 8.0 * float(sigma)
        print "Determined sky level to be " + str(skyvalue) + " +/-" + str(sigma)

        iraf.datapars.datamin = datamin
        iraf.datapars.sigma = float(sigma)
        iraf.datapars.saveParList()

        iraf.fitskypars.skyvalue = skyvalue
        iraf.fitskypars.saveParList()
        ### find the bright stars in the image.
        print "sextracting for stars in " + this_image

        ###iraf.daophot.daofind(image=this_image,
        ###                     output=tfile['coo_bright'],threshold=4.0)

        os.system(
            "sex -c /home/cadc/kavelaar/12kproc/config/default.sex -SATUR_LEVEL 25000 -CATALOG_NAME "
            + tfile["coo_bright"]
            + " "
            + this_image
        )

        ### print "finding stellar locus in sround/ground space"
        print "clipping using star_class > 0.85 "
        fcoo = open(tfile["coo_bright"], "r")
        lines = fcoo.readlines()
        fcoo.close()
        import numarray, math

        fout = open(tfile["coo_ok"], "w")
        for line in lines:
            if re.match(r"^#", line) or re.search(r"INDEF", line):
                continue
            values = line.split()
            star_class = float(values[2])
            if star_class > 0.75:
                fout.write(line)
        fout.close()

        print "Measuring photometry for psf candidate stars in " + tfile["coo_ok"]
        iraf.daophot.phot(image=this_image, coords=tfile["coo_ok"], output=tfile["mag_bright"])

        ### do this selection in 2 steps because of the way IRAF handles INDEFs
        print "Selecting stars that have good centroids and magnitudes "
        iraf.pselect(
            tfile["mag_bright"], tfile["mag_ok"], "(CIER==0)&&(PIER==0)&&(SIER==0)&&(MSKY>0)&&(MSKY<2e5)&&(MSKY!=INDEF)"
        )
        print "Selecting stars that have normal sky levels"
        condition = "(abs(MSKY -" + str(skyvalue) + ") < 5.0*" + str(sigma) + ")"
        iraf.pselect(tfile["mag_ok"], tfile["mag_good"], condition)

        a = iraf.txdump(tfile["mag_good"], "SSKEW", iraf.yes, Stdout=1)
        aa = []
        for v in a:
            aa.append(float(v))

        a = numarray.array(aa)
        mean = a.mean()
        aa = a * a

        stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2)
        limit = mean + 2 * stddev

        os.unlink(tfile["mag_good"])
        condition = condition + " && SSKEW < " + str(limit)
        iraf.pselect(tfile["mag_ok"], tfile["mag_good"], condition)

        print "Choosing the psf stars"
        iraf.pstselect(image=this_image, photfile=tfile["mag_good"], pstfile=tfile["pst_in"], maxnpsf=25)

        ## construct an initial PSF image
        print "computing psf with neighbor stars based on complete star list"
        iraf.psf.mode = "a"
        iraf.psf(
            image=this_image,
            photfile=tfile["mag_bright"],
            pstfile=tfile["pst_in"],
            psfimage=tfile["psf_1.fits"],
            opstfile=tfile["pst_out"],
            groupfile=tfile["psg_org"],
            varorder=0,
        )

        try:
            print "subtracting the psf neighbors and placing the results in " + tfile["sub.fits"]
            iraf.daophot.nstar(
                image=this_image,
                groupfile=tfile["psg_org"],
                psfimage=tfile["psf_1.fits"],
                nstarfile=tfile["nst"],
                rejfile=tfile["nrj"],
            )

            iraf.daophot.substar(
                image=this_image,
                photfile=tfile["nst"],
                exfile=tfile["pst_in"],
                psfimage=tfile["psf_1.fits"],
                subimage=tfile["sub.fits"],
            )

            a = iraf.daophot.txdump(tfile["nst"], "chi", "yes", Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2)
            limit = mean + 2.5 * stddev

            print "Selecting those psf stars with CHI^2 <" + str(limit) + " after fitting with trial psf"
            iraf.pselect(tfile["nst"], tfile["mag_best"], "CHI < " + str(limit))

            os.unlink(tfile["pst_out"])
            ##    os.unlink(tfile['psg'])
            ## rebuild the PSF file with the psf stars that fit well..
            ## using the neighbor subtracted image

            print "Rebuilding the PSF"

            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_2.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=0,
            )

            print "re-subtracting with rebuilt psf"

            os.unlink(tfile["nst"])
            os.unlink(tfile["nrj"])
            iraf.daophot.nstar(
                image=this_image,
                groupfile=tfile["psg"],
                psfimage=tfile["psf_2.fits"],
                nstarfile=tfile["nst"],
                rejfile=tfile["nrj"],
            )

            os.unlink(tfile["sub.fits"])
            iraf.daophot.substar(
                image=this_image,
                photfile=tfile["nst"],
                exfile=tfile["pst_in"],
                psfimage=tfile["psf_2.fits"],
                subimage=tfile["sub.fits"],
            )

            os.unlink(tfile["psg"])
            os.unlink(tfile["pst_out"])
            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_3.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=0,
            )

            os.unlink(tfile["nrj"])
            os.unlink(tfile["nst"])
            iraf.daophot.nstar(
                image=this_image,
                groupfile=tfile["psg"],
                psfimage=tfile["psf_3.fits"],
                nstarfile=tfile["nst"],
                rejfile=tfile["nrj"],
            )

            a = iraf.daophot.txdump(tfile["nst"], "chi", "yes", Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2)
            limit = mean + 2 * stddev
            limit = 2.0

            # print "Selecting those psf stars with CHI^2 < "+str(limit)+" after fit with GOOD psf"

            os.unlink(tfile["mag_best"])
            iraf.pselect(tfile["nst"], tfile["mag_best"], "CHI < " + str(limit))

            print "Building final PSF.... "
            os.unlink(tfile["sub.fits"])
            iraf.daophot.substar(
                image=this_image,
                photfile=tfile["nst"],
                exfile=tfile["pst_in"],
                psfimage=tfile["psf_3.fits"],
                subimage=tfile["sub.fits"],
            )

            os.unlink(tfile["psg"])
            os.unlink(tfile["pst_out"])
            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_final.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=0,
            )

            print "building an analytic psf for the FWHM calculations"
            os.unlink(tfile["pst_out"])
            os.unlink(tfile["psg"])
            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_4.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=-1,
            )
        except:
            print sys.exc_info()[1]
            print "ERROR: Reverting to first pass psf"
            tfile["psf_final.fits"] = tfile["psf_1.fits"]

            iraf.daophot.psf(
                image=this_image,
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_4.fits"],
                opstfile=tfile["pst_out2"],
                groupfile=tfile["psg"],
                varorder=-1,
            )

        psf_ap = iraf.photpars.apertures
        ap1 = int(psf_ap)
        ap2 = int(4.0 * opt.fwhm)
        apcor = "INDEF"
        aperr = "INDEF"
        if 0:
            # try
            ### now that we have the psf use the output list of psf stars
            ### to compute the aperature correction
            lines = iraf.txdump(tfile["pst_out"], "xcen,ycen,mag,id", iraf.yes, Stdout=tfile["coo_pst"])

            ## set the lower ap value for the COG (normally set to 2)
            if ap1 < 3:
                smallap = 1
            else:
                smallap = 2 - ap1 + 1
                ap1 = 2

            ap2 = int(math.floor(4.0 * opt.fwhm))
            naperts = ap2 - ap1 + 1

            iraf.photpars.apertures = str(ap1) + ":" + str(ap2) + ":1"
            iraf.photpars.saveParList()
            iraf.daophot.phot(image=this_image, coords=tfile["coo_pst"], output=tfile["mag_pst"])
            iraf.photcal()
            iraf.photcal.mkapfile(
                tfile["mag_pst"],
                naperts=naperts,
                apercors=tfile["apcor"],
                smallap=smallap,
                verify="no",
                gcommands="",
                interactive=0,
            )
            fin = open(tfile["apcor"], "r")
            lines = fin.readlines()
            values = lines[2].split()
            apcor = values[1]
            aperr = values[2]
        # except:

        ## compute the FWHM of the PSF image using the analytic PSF (VarOrd=-1)
        psf_file = pyfits.open(tfile["psf_4.fits"])

        fwhm = psf_file[0].header.get("PAR1", 99.0) + psf_file[0].header.get("PAR2", 99.0)
        psf_file.close()
        #   ## Open the psf.fits
        infits = pyfits.open(tfile["psf_final.fits"])
        hdu = infits[0]
        inhdu = hdu.header
        inhdu.update("XTENSION", "IMAGE", before="SIMPLE")
        inhdu.update("PCOUNT", 0, after="NAXIS2")
        inhdu.update("GCOUNT", 1, after="PCOUNT")
        del hdu.header["SIMPLE"]
        del hdu.header["EXTEND"]
        inhdu.update("EXTNAME", extname, comment="image extension identifier")
        # inhdu.update("SLOW",slow,comment="SROUND low cutoff")
        # inhdu.update("SIGH",sigh,comment="SROUND high cutoff")
        inhdu.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting")
        inhdu.update("ZMAG", zero_mag, comment="ZMAG of PSF ")
        inhdu.update("BCKG", skyvalue, comment="Mean sky level in counts")
        inhdu.update("BCKG_STD", sigma, comment="standard deviation of sky in counts")
        inhdu.update("AP1", psf_ap, comment="Apperture used for PSF flux")
        inhdu.update("AP2", ap2, comment="Full Flux aperture")
        inhdu.update("APCOR", apcor, comment="Apperture correction (ap1->ap2)")
        inhdu.update("APERR", apcor, comment="Uncertainty in APCOR")

        #    ### append this psf to the output images....
        print "Sticking this PSF onto the output file"
        f[current_ext].header.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting")
        f[current_ext].header.update("BCKG", skyvalue, comment="Mean sky level in counts")
        f[current_ext].header.update("BCKG_STD", sigma, comment="Standard deviation of sky in counts")
        f.flush()
        outfits.append(hdu)
        outfits.flush()
        infits.close()

        ### remove the temp file we used for this computation.
        for tf in tfile.keys():
            if os.access(tfile[tf], os.F_OK):
                os.unlink(tfile[tf])

        current_ext = current_ext + 1

    outfits.close()
    return mef_psf
Ejemplo n.º 21
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')
Ejemplo n.º 22
0
def iraf_phot(f,x,y,zmag=26.5,apin=10,skyin=15,skywidth=10):
    """Compute the magnitude of the star at location x/y"""
    import pyfits
    import re
    infits=pyfits.open(f,'update')
    f=re.sub(r'.fits$','',f)
    
    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

    ### keep all the parameters locally cached.
    iraf.set(uparm="./")
    iraf.set(imtype="fits")


    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()
    
    ### temp file name hash.
    tfile={}
    
    iraf.datapars.datamax=60000
    iraf.datapars.datamin=-1000
    iraf.datapars.airmass='AIRMASS'
    iraf.datapars.filter='FILTER'
    iraf.datapars.obstime='TIME-OBS'
    iraf.datapars.exposure='EXPTIME'
    iraf.datapars.gain='GAIN'
    iraf.datapars.ccdread='RDNOISE'
    iraf.datapars.fwhmpsf=5.0
    
    iraf.centerpars.calgorithm='centroid'
    iraf.photpars.zmag=zmag
    iraf.photpars.apertures=apin
    iraf.fitskypars.annulus=skyin
    iraf.fitskypars.dannulus=skywidth
    iraf.daophot.verbose=iraf.no
    iraf.daophot.verify=iraf.no
    iraf.daophot.update=iraf.no
    
    iraf.psf.interactive=iraf.no
    iraf.pstselect.interactive=iraf.no
    
    iraf.datapars.saveParList()
    iraf.fitskypars.saveParList()
    iraf.centerpars.saveParList()
    iraf.findpars.saveParList()
    iraf.photpars.saveParList()
    
    tfiles = ['coo','mag']
    

    
    for file in tfiles:
        extname=f
        tfile[file]=extname+"."+file
        if ( os.access(tfile[file],os.F_OK) ):
            os.unlink(tfile[file])
            
    this_image=f
        
    fd = open(tfile['coo'],'w')
    fd.write('%f %f\n' % ( x, y) )
    fd.close()
    
    print "Measuring photometry psf star in "+tfile['coo']
    iraf.daophot.phot(image=this_image,
                      coords=tfile['coo'],
                      output=tfile['mag'])

    import string
    a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1)
    (mag,x,y)=string.split(a[0])

    inhdu=infits[0].header
    
    inhdu.update("PSFMAG",float(mag),comment="PSF Magnitude")
    inhdu.update("PSF_X",float(x),comment="PSF Magnitude")
    inhdu.update("PSF_Y",float(y),comment="PSF Magnitude")
    inhdu.update("ZMAG",zmag,comment="ZMAG of PSF ")

    ## now measure using a smaller aperture to get aper correction
    iraf.photpars.apertures=apin*3.0
    os.unlink(tfile['mag'])
    iraf.daophot.phot(image=this_image,
                      coords=tfile['coo'],
                      output=tfile['mag'])
    a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1)
    (magout,x,y)=string.split(a[0])
    inhdu.update("APCOR",float(magout)-float(mag),comment="AP_OUT - AP_IN")
    inhdu.update("AP_IN",apin*3.0,comment="Small aperature")
    inhdu.update("AP_OUT",apin,comment="Large aperture")

    
    #    ### append this psf to the output images....
    
    infits.close()
    
    ### remove the temp file we used for this computation.
    #for tf in tfile.keys():
    #    if os.access(tfile[tf],os.F_OK):
    #        os.unlink(tfile[tf])
    return 0
Ejemplo n.º 23
0
def Main( obj, make_plots=True ):
    """
    Does absolute photometry for one or more stars given a list of images.
    Output is generated in the form of two types of file:
       1. starX_absphot.dat for X=0,1,2,... files contain columns with the
          more detailed output for each of the stars, with each line
          corresponding to a different image.
       2. absolute.phot file containing the important numerical columns for
          each of the stars; it's supposed to be the most convenient output
          for use with numpy and for generating relative photometry.

    Summary plots are also generated by default:

      Figure 1:
        ** Top left = traces of xy drift for each of the stars
        ** Bottom left = airmass versus time
        ** Top right = absolute flux versus time for each star
        ** Bottom right = sky annulus value as a function of time for each star

      Figure 2:
        ?? Plots image number versus measured scatter divided by the calculated Poisson noise ??
    """

    # Get the list of reduced images in the data directory:
    red_images = np.loadtxt( obj.red_image_list, dtype='str' )
    if obj.datapars[ 'obstime' ]!='':
        obstimes = np.zeros( len(red_images) )
        # Read the headers and put the observation times into an array:
        for i in range( len(red_images) ):
            h = pyfits.getheader( red_images[i] )
            obstimes[i] = h[ obj.datapars['obstime'] ]
        ixs = np.argsort( obstimes )
        red_images = red_images[ ixs ]
    if obj.nimages_total==None:
        obj.nimages_total = len( red_images )
    if obj.goodbad_flags==None:
        obj.goodbad_flags =  np.ones( obj.nimages_total )
    obj.nimages_good = int( np.sum( obj.goodbad_flags ) )
    # Set the parameters for the various IRAF routines:
    set_iraf_params( obj )
    # Work out how many stars we're doing photometry for:
    if np.rank( obj.coords_input_files )>0:
        obj.nstars = len( obj.coords_input_files )
    else:
        obj.coords_input_files = [ obj.coords_input_files ]
        obj.nstars = 1
    obj.ncomparisons = obj.nstars-1
    # Determine whether we're using the coordinates from the first frame only then updating
    # as we go along, or if we have a list of coordinates for each of the frames already:
    obj.dat_files = []
    if obj.coords_input_type=='init':
        coords_temps = []
    elif obj.coords_input_type=='list':
        coords_lists = []
    # Open the various dat files that will receive the output from iraf.phot and give them
    # a header line recording the aperture parameters used for the photometry:
    open_dat_files = []
    ap_string = '# APERTURE RADIUS = %.2f pixels \n# ANNULUS RADIUS = %.2f pixels \n# DELTA ANNULUS = %.2f pixels\n\n' % \
                ( float( iraf.photpars.apertures ), float( iraf.fitskypars.annulus ), float( iraf.fitskypars.dannulus ) )
    # Refer to the IRAF Phot handbook for further information on the following:
    #   flux = sky-subtracted ADU counts due to star (i.e. sum-sky)
    #   sum = total ADU counts within aperture (i.e. star+sky)
    #   area = number of pixels within aperture
    #   msky = mean sky value ADU counts per pixel (i.e. sky=area*msky)
    #   stdev = standard deviation of the sky values per pixel in ADU counts
    # We will extract the following fields for the output:
    txdump_fields = [ 'otime', 'xairmass', 'xcenter', 'ycenter', 'flux', 'itime', 'area', 'msky', 'stdev', \
                      'cerror', 'serror', 'perror', 'image' ]
    # Set up a dictionary to hold the field name and its corresponding
    # column index in the txdump output file:
    txdump_field_ixs = {}
    for field in txdump_fields:
        txdump_field_ixs[ field ] = i
    # Make a header string containing the column names for the txdump output:
    txdump_fields_str = '# '
    for i in range( len( field_items ) ):
        for j in range( len( field_items ) ):
            if field_items[ j ][ 1 ]==i:
                txdump_fields_str += field_items[ j ][ 0 ]
        if i<len( field_items )-1:
            txdump_fields_str += ', '
        else:
            continue
    # Write the column headings to the files that will hold the photometry output:
    for j in range( obj.nstars ):
        dat_file = ( obj.analysis_dir+'/star%i_absphot.dat' % j).replace( '//' , '/' )
        obj.dat_files = obj.dat_files + [ dat_file ]
        if os.path.isfile( dat_file ): os.remove( dat_file )
        open_dat_files = open_dat_files + [ open( dat_file,'w' ) ]
        open_dat_files[ j ].write( ap_string )
        # Replace some of the txdump keywords for the column header that gets written to file:
        columns_header = str( txdump_fields_str ).replace( 'xairmass', 'airmass' )
        columns_header = str( columns_header ).replace( 'area' , 'ap_area' )
        columns_header = str( columns_header ).replace( 'msky' , 'skyval' )
        columns_header = str( columns_header ).replace( 'stdev' , 'skystdv' )
        columns_header = columns_header + '\n'
        open_dat_files[j].write( columns_header )
        if obj.coords_input_type=='init':
            # Copy the star coordinate files to temporary files that will be overwritten below at
            # the start of each new image to account for shifts in the pointing:
            coords_temps = coords_temps + [ ( obj.analysis_dir+'/star%i_coords.temp' % j ).replace( '//' , '/' ) ]
            shutil.copyfile( obj.coords_input_files[ j ], coords_temps[ j ] )
        elif obj.coords_input_type=='list':
            # Otherwise, we must already have a list of coordinates for each of the images, and we
            # can read these in as a list of xy-arrays, one for each star:
            coords_list = np.loadtxt( obj.coords_input_files[ j ] )
            coords_lists = coords_lists + [ coords_list ]
    # Now go through each of the images, and do aperture photometry for each of the stars:
    for i in range( len( red_images ) ):
        print 'Doing photometry for image %i of %i' % ( i+1, len( red_images ) )
        if obj.goodbad_flags!=None:
            if obj.goodbad_flags[ i ]==0:
                print '( ... current image flagged as bad )'
        for j in range( obj.nstars ):
            coords_temp_filename = ( obj.analysis_dir + '/star%i_coords.temp' % j ).replace( '//', '/' )
            if obj.coords_input_type=='list':
                # If we're using a list of coordinates that has been determined ahead of time,
                # extract the coordinates for the current star on the current image and put them
                # into the temporary coordinates file that IRAF will use:
                coords_string = '%.2f %.2f \n' % ( coords_lists[ j ][ i, 0 ], coords_lists[ j ][ i, 1 ] )
                coords_temp_file = open( coords_temp_filename, 'w' )
                coords_temp_file.write( coords_string )
                coords_temp_file.close()
            # The output of the iraf.phot task will be saved in the temporary mag file:
            mag_temp_filename = ( obj.analysis_dir + '/star%i_mag.temp' % j ).replace( '//', '/' )
            if os.path.isfile( mag_temp_filename ):
                os.remove( mag_temp_filename )
            # Do the photometry for the current star on the current image:
            iraf.phot( image=red_images[ i ], coords=coords_temp_filename, output=mag_temp_filename )
            # Since the mag output file generated by iraf.phot is not very easy to read, we use the
            # iraf.txdump routine to extract the fields we're interested in and append these to the
            # relevant dat file in nice columns:
            iraf.txdump( mag_temp_filename, fields=txdump_fields_str, expr='yes', Stdout=open_dat_files[ j ] )
            # The iraf.phot routine will have refined our estimate for the source coordinates, so we can
            # use these as our initial guess for the next image, assuming the pointing change between images
            # is not too severe:
            os.remove( coords_temp_filename )
            coords_temp_file = open( coords_temp_filename, 'w' )
            iraf.txdump( mag_temp_filename, fields='xcenter,ycenter', expr='yes', Stdout=coords_temp_file )
            coords_temp_file.close()
    # Save the dat files and remove the temporary mag and coords files:
    for j in range( obj.nstars ):
        open_dat_files[ j ].close()
        mag_temp_filename = obj.analysis_dir + '/star%i_mag.temp' % j
        os.remove( mag_temp_filename )
        coords_temp_filename = obj.analysis_dir + '/star%i_coords.temp' % j
        os.remove( coords_temp_filename )
    # If we did not have a list of coordinates for all the images to start with, we can one now using the
    # output from above:
    if obj.coords_input_type=='init':
        coords_init2list( obj, txdump_field_ixs )
    # We can also use the information stored in the dat file to generate a reformatted file that is nicer
    # to use with numpy, having only float entries:
    dats2absphot( obj, txdump_field_ixs )
    # Plot some of the output:
    if make_plots==True:
        plot( obj )
        
    return None
Ejemplo n.º 24
0
def makeillumination(lista, flatfield):  #,outputfile,illum_frame):
    import os, glob, string, re
    from astropy.io import fits as pyfits
    import ntt
    from ntt.util import readhdr, readkey3, delete, display_image, defsex, name_duplicate, correctcard
    from numpy import compress, array, argmax, argmin, min, argsort, float32
    import datetime
    MJDtoday = 55927 + (datetime.date.today() -
                        datetime.date(2012, 01, 01)).days
    _date = readkey3(readhdr(lista[0]), 'date-night')
    _filter = readkey3(readhdr(lista[0]), 'filter')
    illum_frame = name_duplicate(
        lista[0], 'illum_' + _date + '_' + _filter + '_' + str(MJDtoday), '')
    from pyraf import iraf
    iraf.images(_doprint=0, Stdout=0)
    iraf.imutil(_doprint=0, Stdout=0)
    iraf.utilities(_doprint=0, Stdout=0)
    iraf.noao(_doprint=0, Stdout=0)
    iraf.imred(_doprint=0, Stdout=0)
    iraf.ccdred(_doprint=0, Stdout=0)
    iraf.digiphot(_doprint=0, Stdout=0)
    iraf.daophot(_doprint=0, Stdout=0)
    iraf.generic(_doprint=0, Stdout=0)
    toforget = [
        'digiphot.daophot', 'imutil.imarith', 'image', 'utilities.surfit'
    ]
    for t in toforget:
        iraf.unlearn(t)
    n = len(lista)
    #   start loop to read image names from the input file
    lista1 = []
    iraf.ccdred.verbose = 'no'
    ff = open('templist.lst', 'w')
    for i in range(0, len(lista)):
        ff.write('C' + lista[i] + '\n')
        delete('C' + lista[i])
        delete('C' + re.sub('.fits', '_sub.fits', lista[i]))
        ntt.sofiphotredudef.crosstalk(lista[i], 'C' + lista[i])
        iraf.noao.imred.ccdred.ccdproc('C' + lista[i],
                                       output='',
                                       overscan="no",
                                       trim="yes",
                                       ccdtype='',
                                       darkcor='no',
                                       fixpix='no',
                                       zerocor="no",
                                       flatcor='yes',
                                       illumco='no',
                                       trimsec='[1:1024,1:1007]',
                                       biassec='',
                                       flat=flatfield,
                                       illum='')
        correctcard('C' + lista[i])
        lista1.append('C' + lista[i])
    ff.close()
    print '\n### prereducing STD frames to compute illumination correction ........'
    lista2, skyfile = ntt.sofiphotredudef.skysub(
        lista1, readkey3(readhdr(lista1[0]), 'ron'),
        readkey3(readhdr(lista1[0]), 'gain'), True)
    lista2 = ntt.sofiphotredudef.sortbyJD(lista2)
    print '\n### use x on the star and q  to continue....'
    display_image(lista2[0], 2, '', '', False)
    delete('tmpone.coo')
    iraf.image.tv.imexamine(lista2[0],
                            2,
                            logfile='tmpone.coo',
                            keeplog='yes',
                            xformat='',
                            yformat='',
                            wcs='logical')
    iraf.tvmark(2,
                'tmpone.coo',
                mark="circle",
                number='yes',
                label='no',
                radii=8,
                nxoffse=5,
                nyoffse=5,
                color=204,
                txsize=2)
    xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
    x0, y0 = string.split(xycoo[0])
    x0 = float(x0)
    y0 = float(y0)
    xcum0 = readkey3(readhdr(lista2[0]), 'xcum')
    ycum0 = readkey3(readhdr(lista2[0]), 'ycum')
    iraf.digiphot(_doprint=0, Stdout=0)
    iraf.daophot(_doprint=0, Stdout=0)
    iraf.noao.digiphot.daophot.datapars.datamin = -1000
    iraf.noao.digiphot.daophot.datapars.datamax = 60000
    iraf.noao.digiphot.daophot.daopars.function = 'gauss'
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    namesex = defsex('default.sex')
    for i in range(0, len(lista2)):
        j = i + 1
        xcum = readkey3(readhdr(lista2[i]), 'xcum')
        ycum = readkey3(readhdr(lista2[i]), 'ycum')
        xx = x0 - xcum0 + xcum
        yy = y0 - ycum0 + ycum
        # sex objects
        os.system('sex ' + lista2[i] + ' -c ' + namesex + '>  _logsex')
        delete('_logsex')
        xpix = iraf.proto.fields('detections.cat', fields='2', Stdout=1)
        ypix = iraf.proto.fields('detections.cat', fields='3', Stdout=1)
        cm = iraf.proto.fields('detections.cat', fields='4', Stdout=1)
        cm = compress((array(xpix) != ''), array(cm, float))
        ypix = compress((array(xpix) != ''), array(ypix, float))
        xpix = compress((array(xpix) != ''), array(xpix, float))
        if len(xpix) > 300:
            num = 300
        else:
            num = len(xpix) - 1
        xpix = xpix[argsort(cm)][0:num]
        ypix = ypix[argsort(cm)][0:num]
        distance = (ypix - yy)**2 + (xpix - xx)**2
        xx1, yy1 = xpix[argmin(distance)], ypix[argmin(distance)]
        f = open('tmpone.coo', 'w')
        f.write(str(xx1) + ' ' + str(yy1) + '\n')
        f.close()
        display_image(lista2[i], 1, '', '', False)
        iraf.tvmark(1,
                    'tmpone.coo',
                    mark="circle",
                    number='yes',
                    label='no',
                    radii=8,
                    nxoffse=5,
                    nyoffse=5,
                    color=204,
                    txsize=2)
        answ = 'n'
        while answ != 'y':
            answ = raw_input('selected the right one [[y]/n] ?')
            if not answ:
                answ = 'y'
            if answ in ['y', 'YES', 'yes', 'Y']:
                print lista2[i]
                delete('pippo.' + str(j) + '.mag')
                gggg = iraf.digiphot.daophot.phot(lista2[i],
                                                  "tmpone.coo",
                                                  output="pippo." + str(j) +
                                                  ".mag",
                                                  verify='no',
                                                  interac='no',
                                                  Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'
            else:
                print '\n### select the std star'
                display_image(lista2[i], 1, '', '', False)
                iraf.image.tv.imexamine(lista2[i],
                                        1,
                                        logfile='tmpone.coo',
                                        keeplog='yes',
                                        xformat='',
                                        yformat='',
                                        wcs='logical')
                xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
                x2, y2 = string.split(xycoo[0])
                f = open('tmpone.coo', 'w')
                f.write(str(x2) + ' ' + str(y2) + '\n')
                f.close()
                delete('pippo.' + str(j) + '.mag')
                print '###### new selection ' + str(x2), str(y2)
                gggg = iraf.digiphot.daophot.phot(lista2[i],
                                                  "tmpone.coo",
                                                  output='pippo.' + str(j) +
                                                  '.mag',
                                                  verify='no',
                                                  interac='no',
                                                  Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'

    os.system('ls pippo.*.mag > tempmag.lst')
    tmptbl0 = iraf.txdump(textfile="@tempmag.lst",
                          fields="XCENTER,YCENTER,FLUX",
                          expr='yes',
                          Stdout=1)
    ff = open('magnitudini', 'w')
    for i in tmptbl0:
        ff.write(i + '\n')
    ff.close()
    #   delete the temporary images and files
    delete("temp*.fits")
    delete('temp*.lst')
    delete(illum_frame)
    print '\n### fitting the illumination surface...'
    aaa = iraf.utilities.surfit('magnitudini',
                                image=illum_frame,
                                function="polynomial",
                                xorder=2,
                                yorder=2,
                                xterms="full",
                                ncols=1024,
                                nlines=1024,
                                Stdout=1)
    iraf.noao.imred.generic.normalize(illum_frame)
    correctcard(lista[0])
    data, hdr = pyfits.getdata(illum_frame, 0, header=True)
    data0, hdr0 = pyfits.getdata(lista[0], 0, header=True)
    delete(illum_frame)
    pyfits.writeto(illum_frame, float32(data), hdr0)
    flatfield0 = string.split(flatfield, '/')[-1]
    ntt.util.updateheader(illum_frame, 0,
                          {'MKILLUM': [flatfield0, 'flat field']})
    display_image(illum_frame, 1, '', '', False)
    for i in range(0, len(lista)):  # in lista:
        img = lista[i]
        delete('pippo.' + str(i) + '.mag')
        delete('C' + img)
        delete('C' + re.sub('.fits', '_sky.fits', img))
#    delete('C*.fits.mag.1')
#    iraf.hedit(illum_frame,'MKILLUM','Illum. corr. created '+flatfield,add='yes',update='yes',verify='no')
    return illum_frame
Ejemplo n.º 25
0
        daophot.pselect(infiles=image + '.pst.' + str(j),
                        outfiles=image + '.pst.' + str(j + 1),
                        expr='ID!=' + str(rmStar))

        daophot.psf(image=image,
                    photfile='default',
                    pstfile='default',
                    psfimage='default',
                    opstfile='default',
                    groupfil='default',
                    verify='no',
                    interactive='no')

        iraf.txdump(textfile=image + '.pst.' + str(j + 1),
                    fields='ID',
                    expr='yes',
                    Stdout=os.path.join(outdir, image + '_psf_stars.txt'))

        psfList = np.genfromtxt(os.path.join(outdir, image + '_psf_stars.txt'),
                                dtype='int')

        for k in psfList:
            ax1.errorbar(co[k - 1, 0],
                         co[k - 1, 1],
                         fmt='*',
                         mfc='none',
                         markeredgecolor='lime',
                         markeredgewidth=2,
                         markersize=30,
                         label='Used in PSF fit')
Ejemplo n.º 26
0
    for file2del in files2del:
        os.remove(file2del)

    try:
        
        sexcatname=imgroot+'_sex.cat'
        sexcommand='sex '+image+' -c '+sys.argv[2]+' -CATALOG_NAME '+sexcatname+' -PARAMETERS_NAME '+sys.argv[3]+' -DETECT_THRESH 100 -VERBOSE_TYPE QUIET'
        os.system(sexcommand)
        
        iraf.datapars.setParam('scale','1')
        iraf.datapars.setParam('fwhmpsf','2.8')
        iraf.datapars.setParam('datamin','-200')
        iraf.datapars.setParam('gain','GAIN')
        iraf.datapars.setParam('exposure','EXPTIME')
        iraf.centerpars.setParam('cbox','10.0')
        iraf.fitskypars.setParam('annulus','30')
        iraf.fitskypars.setParam('dannulus','10')
        iraf.photpars.setParam('zmag','17.5')
        iraf.photpars.setParam('apertures','20')
        iraf.daophot.phot(image=image,coords=sexcatname,output=imgroot+'_superphot.mags',interactive=0,verify=0,update=0,verbose=0)
        iraf.txdump(textfiles=imgroot+'_superphot.mags',fields='IMAGE,XCENTER,YCENTER,MSKY,STDEV,NSKY,RAPERT,AREA,FLUX,MAG,MERR',expr='yes',headers=0,parameters=0,Stdout=aperall)

    except Exception, e:
        exstr = traceback.format_exc()
        print >> logerror, image, '\n', exstr

if len(open(errlogname).read().strip()) == 0:
        os.remove(errlogname)

print ' Finished !\n'
Ejemplo n.º 27
0
def fitsn(_recenter, img, imgpsf, fwhm0, apco0, z22, z11, midpt, size, nor,
          _values, DM):
    from pyraf import iraf
    import string, os, sys
    from numpy import log10

    a1 = int(fwhm0 + .5)
    a2 = int(2. * fwhm0 + .5)
    a3 = int(3. * fwhm0 + .5)
    a4 = int(4. * fwhm0 + .5)
    a5 = int(5. * fwhm0 + .5)
    ap = str(a1) + "," + str(a2) + "," + str(a3)

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)

    from iraf import digiphot
    from iraf import daophot
    from iraf import ptools

    zmag = 0
    iraf.noao.digiphot.daophot.photpars.zmag = zmag

    iraf.delete("apori", verify='no')
    iraf.delete(img + ".sn.mag", verify='no')
    iraf.noao.digiphot.daophot.phot("original",
                                    img + ".sn.coo",
                                    "apori",
                                    veri='no')
    iraf.noao.digiphot.daophot.phot("sn",
                                    img + ".sn.coo",
                                    img + ".sn.mag",
                                    veri='no')
    iraf.noao.digiphot.daophot.daopars.psfrad = a4
    iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0
    iraf.noao.digiphot.daophot.daopars.fitsky = 'no'
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)

    if _recenter:
        answ = raw_input(">>> recentering for targets [yes/no] ? [yes] ")
        if not answ: answ = 'yes'
    else:
        answ = 'yes'
    iraf.noao.digiphot.daophot.daopars.recenter = answ
    iraf.noao.digiphot.daophot.daopars.fitsky = 'yes'
    iraf.delete(img + ".sn.als", verify='no')
    iraf.allstar("sn",
                 img + ".sn.mag",
                 imgpsf,
                 img + ".sn.als",
                 "",
                 "residual",
                 veri='no',
                 verb='no')
    iraf.delete("snfit.fits", verify='no')
    iraf.imarith("sn", "-", "residual", "snfit")
    iraf.delete("skyfit.fits", verify='no')
    iraf.imarith("original", "-", "snfit", "skyfit")
    iraf.txsort(img + ".sn.als", "ID")
    tmptbl = iraf.txdump(img + ".sn.als",
                         "mag,merr,xcenter,ycenter",
                         expr='yes',
                         Stdout=1)
    magerr, fitmag, centx, centy = [], [], [], []
    for i in tmptbl:
        try:
            fitmag.append(float(string.split(i)[0]))  #+2.5*log10(nor))
        except:
            fitmag.append('INDEF')
        try:
            magerr.append(float(string.split(i)[1]))
        except:
            magerr.append('INDEF')

        centx.append(float(string.split(i)[2]))
        centy.append(float(string.split(i)[3]))
    tmptbl = iraf.txdump("apori", "mag", expr='yes', Stdout=1)
    apori1, apori2, apori3 = [], [], []
    for i in tmptbl:
        try:
            apori1.append(
                float(string.split(i)[0]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apori1.append('INDEF')
        try:
            apori2.append(
                float(string.split(i)[1]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apori2.append('INDEF')
        try:
            apori3.append(
                float(string.split(i)[2]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apori3.append('INDEF')

    iraf.txsort(img + ".sn.mag", "YCENTER")
    tmptbl = iraf.txdump(img + ".sn.mag", "mag,magerr", expr='yes', Stdout=1)

    print "********************************************************************"
    print "ID <apmag on original>  <apmag on bgsubt> fitmag truemag err_fit"
    print "     ", a1, "       ", a2, "      ", a3, "        ", a1, "     ", a2, "     ", a3

    apmag1, apmag2, apmag3, truemag = [], [], [], []
    for i in range(len(tmptbl)):
        try:
            apmag1.append(
                float(string.split(tmptbl[i])[0]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apmag1.append('INDEF')
        try:
            apmag2.append(
                float(string.split(tmptbl[i])[1]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apmag2.append('INDEF')
        try:
            apmag3.append(
                float(string.split(tmptbl[i])[2]) - float(_values) -
                float(DM))  #+2.5*log10(no)
        except:
            apmag3.append('INDEF')
        try:
            truemag.append(
                float(fitmag[i]) + float(apco0) + 2.5 * log10(nor) -
                float(_values) - float(DM))
        except:
            truemag.append('INDEF')
        print i, apori1[i], apori2[i], apori3[i], apmag1[i], apmag2[i], apmag3[
            i], fitmag[i], truemag[i], magerr[i]

    print "********************************************************************"

    iraf.display("original",
                 1,
                 fill='yes',
                 xcen=.25,
                 ycen=.25,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z22,
                 z1=z11)

    z01 = z11 - midpt
    z02 = z22 - midpt
    s1 = 1
    s2 = -int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    iraf.display("snfit",
                 1,
                 erase='no',
                 fill='yes',
                 xcen=.25,
                 ycen=.75,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z02,
                 z1=z01)
    iraf.delete("tmptbl", ve='no')
    tmptbl0 = iraf.txdump(img + ".sn.als", "xcen,ycen", expr='yes', Stdout=1)
    ff = open('tmptbl', 'w')
    for i in tmptbl0:
        ff.write(i + '\n')
    ff.close()
    lra = int((2 * float(size) * float(fwhm0)) * 2)
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="circle",
                number='yes',
                nyoffset=lra,
                radi=a2,
                txsize=2,
                inter='no')
    s1 = 1
    s2 = -1 * int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " FITTED")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    iraf.display("skyfit",
                 1,
                 erase='no',
                 fill='yes',
                 xcen=.75,
                 ycen=.25,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z22,
                 z1=z11)
    s1 = 1
    s2 = -1 * int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)
    print '###'
    print '### magnitude scaled to target exposure time using: ' + str(
        2.5 * log10(nor))
    print '### fit magnitude corrected with aperture correction: ' + str(
        float(apco0))
    print '### magnitude scaled to target using DM: ' + str(DM)
    print '###'
    return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy
Ejemplo n.º 28
0
        sigma, sky = findsky(FitsFileName,10)
        print 'Skyvalue: ',sky
        print 'Stdv_sky: ',sigma
        iraf.datapars.setParam('sigma',sigma)
        iraf.fitskypars.setParam('skyvalu',sky)
        pars['gain'] = gain
        pars['rdnoise'] = rdnoise
        pars['sigma'] = sigma
        pars['sky'] = sky

        iraf.datapars.setParam('fwhmpsf',4.0)
        iraf.daofind.setParam('image',FitsFileName)
        iraf.daofind.setParam('output',base+'im'+ext+'.guess.coo')
        iraf.daofind(mode='h',Stdout=1)

        iraf.txdump(textfile=base+'im'+ext+'.guess.coo',fields='xcenter,ycenter,sharpness',expr='sharpness!=INDEF',
        Stdout=base+'daofindtmp')
        daofind = np.loadtxt(base+'daofindtmp')
        if os.path.exists(base+'im'+ext+'.guess.coo') == True: os.remove(base+'im'+ext+'.guess.coo')
        i = (daofind[:,2] < np.median(daofind[:,2]) + 0.12) & (daofind[:,2] > np.median(daofind[:,2]) - 0.12)
        np.savetxt(base+'im'+ext+'.guess.coo',daofind[i,0:2],fmt=['%-10.3f','%-10.3f'])
        iraf.fitpsf.setParam('image',FitsFileName)
        iraf.fitpsf.setParam('coords',base+'im'+ext+'.guess.coo')

        val = linspace(2.,6.,11.)
        best = 0.0
        res = 99999.99
        for i in val:
            iraf.datapars.setParam('fwhmpsf',i)
            iraf.fitpsf.setParam('output',base+'fitpsf')
            iraf.fitpsf(mode='h',Stdout=1)
            asd = iraf.txdump(textfile=base+'fitpsf',fields='rsigma',expr='rsigma!=INDEF && rsigma < 7.0',Stdout=1)
Ejemplo n.º 29
0
def txdump_photometry_info(target_dir, data_dir_name):
    """Extract the results of photometry from files to save them to a text file.
    
    This function search files containing the results of photometry
    and using txdump extracts the coordinates and photometric measure
    to save it to a text file. 
    
    Args:    
        target_dir: Directory that contains the files to process.
        data_dir_name: Name for the directories with data.    
    
    """

    # Walk from current directory.
    for path, dirs, files in os.walk(target_dir):

        # Process only directories without subdirectories.
        if len(dirs) == 0:
            split_path = path.split(os.sep)

            # Check if current directory is for data images.
            if split_path[-2] == data_dir_name:
                logging.debug("Found a directory for data: %s" % (path))

                # Get the list of magnitude files.
                mag_files = glob.glob(
                    os.path.join(path, "*.%s" % MAGNITUDE_FILE_EXT))

                logging.debug("Found %d magnitude files" % (len(mag_files)))

                # Reduce each data file one by one.
                for mfile in mag_files:

                    # Get the name of the file where the magnitude data will
                    # be saved.
                    mag_dest_file_name = \
                        mfile.replace(".%s" %(MAGNITUDE_FILE_EXT),
                                      "%s%s.%s" %
                                      (FILE_NAME_PARTS_DELIM,
                                      MAGNITUDE_FILE_EXT, CSV_FILE_EXT))

                    # Remove the destiny file if exists.
                    if os.path.exists(mag_dest_file_name):
                        os.remove(mag_dest_file_name)

                    try:
                        mag_dest_file = open(mag_dest_file_name, 'w')

                        iraf.txdump(mfile, fields=TXDUMP_FIELDS, expr='yes', \
                                    Stdout=mag_dest_file)

                        mag_dest_file.close()

                    except iraf.IrafError as exc:
                        logging.error("Error executing txdump to get: %s" %
                                      (mag_dest_file_name))
                        logging.error("Iraf error is: %s" % (exc))

                    except IOError as ioe:
                        logging.error("Reading file: %s" %
                                      (mag_dest_file_name))
Ejemplo n.º 30
0
def phot(im,coords,radii,
         useOtherData=None,
         aperCorrs=[],
         refImDir='refImages',
         cent='centroid',
         xxx='',
         linearInterp=False,
         psfInterp=False):

    """Aperture photometry of WFC3 ir/uv1,2 images.

    This code uses iraf's phot package to perform
    simple aperture photometry.

    It then returns a list of arrays, where each entry 
    is the magnitudes and errors for every coordinate pair 
    given.

    It can do three different versions. The most
    basic (and default) is photometry where
    all pixels with imquality flags>0 are 
    ignored. This is the fastest, but typically 
    produces faintward biased answers.

    Another technique is the use of linear interpolation
    using the nearest four good pixels in a cross pattern
    that bound the bad pixel. This produces less biased
    answers the the simple verion. WARNING: not very well
    tested! This is set with linearInterp=True

    The last and most reliable technique (also the slowest)
    uses tinytim psfs to perform the interpolation. All
    good pixels within the aperture are used to match the
    psf flux to that observed. The bad pixels are sampled
    off the psf and scaled to the correct flux to produce
    the corrected image from which the photometry is 
    acquired. This is set with psfInterp=True

    Inputs:
    im - the image filename without the '.fits'.
    coords - an array of coordinates in form [x,y]. These
             are in iraf coordinates ie. (+1,+1) from pyfits
             coordinates!
    radii - an array of radii [r1,r2,r3,..] float acceptable.
    aperCorrs - an array of aperture corrections to apply.
    refImDir='refImages' - the directory with reference images.
                           This will actually be ../refImages
    cent='centroid' - perform an iraf centroid as default.
                      set to 'none' if no centroid desired.
                      
                      if centroiding, returns IRAF type 
                      coordinates.

    xxx='' - a string to add to the corrected image filename.
    """

    if psfInterp:
        import WFC3_PSFFits as P
        import sys

    from pyraf import iraf

    ###for pyraf
    yes=1
    no=0

    #import the necessary packages
    iraf.digiphot()
    iraf.daophot()
    
    #set local parameters
    iraf.set(uparm="./")
    
    iraf.daophot.verbose=no
    iraf.daophot.verify=no
    iraf.daophot.update=no
    
    iraf.centerpars.calgori=cent
    iraf.centerpars.maxshift=2.
    iraf.centerpars.minsnra=1.0
    #iraf.daopars.fitrad=1.0      #enough to leave set
    
    iraf.photpars.apertur=2
    iraf.photpars.zmag=0.0
    
    iraf.datapars.fwhmpsf=2.
    iraf.datapars.sigma=0.0
    iraf.datapars.datamin=-1000
    iraf.datapars.datamax=45000
    iraf.datapars.ccdread=""
    iraf.datapars.gain=""
    iraf.datapars.readnoi=0.0
    iraf.datapars.epadu=1.0
    iraf.datapars.exposure=""
    iraf.datapars.itime=1.0
    iraf.datapars.airmass=""
    iraf.datapars.filter=""
    iraf.datapars.obstime=""
    
    iraf.fitskypars.salgori='mode'
    iraf.fitskypars.annulus=max(radii)*1.2
    iraf.fitskypars.dannulu=8
    
    #save the new parameters
    iraf.datapars.saveParList()
    iraf.photpars.saveParList()
    #iraf.daopars.saveParList()
    iraf.centerpars.saveParList()
    iraf.daophot.saveParList()
    iraf.fitskypars.saveParList()



    if '.fits' in im: fn=im
    else: fn=im+'.fits'
    
    #get the image data
    ha=pyfits.open(fn)
    header0=ha[0].header
    header1=ha[1].header
    data=ha[1].data.astype('f')
    error=ha[2].data
    qual=ha[3].data

    if useOtherData<>None: data=useOtherData
    (a,b)=data.shape
        
    DETECTOR=header0['DETECTOR']
    FILTER=string.strip(header0['FILTER'])
    GAIN=float(header0['CCDGAIN'])
    EXPTIME=float(header0['EXPTIME'])
    if DETECTOR=="UVIS":
        PHOTZPT=float(header1['PHOTZPT'])
        PHOTFLAM=float(header1['PHOTFLAM'])
        data/=EXPTIME
        error/=EXPTIME
    else:
        PHOTZPT=float(header0['PHOTZPT'])
        PHOTFLAM=float(header0['PHOTFLAM'])
    XCEN=int(float(header1['CENTERA1']))
    YCEN=int(float(header1['CENTERA2']))
    XSIZE=int(float(header1['SIZAXIS1']))
    YSIZE=int(float(header1['SIZAXIS2']))

    #headers are f****d in some of the ir images. This is a 
    #cluge to fix that
    if XSIZE==522: XSIZE=512
    if YSIZE==522: YSIZE=512


    ###get the flat
    #flathan=pyfits.open('flat_'+FILTER+'.fits')
    #flatdat=flathan[1].data
    #flathan.close()

    #get the pixel area map
    if DETECTOR=='UVIS':
        if YCEN<2051:
            #chip 1
            name='UVIS1wfc3_map.fits'
            detector='uv1'
        else:
            #chip 2
            name='UVIS1wfc3_map.fits'
            detector='uv2'
    else:
        name='ir_wfc3_map.fits'
        detector='ir'
    PAMhan=pyfits.open(refImDir+'/'+name)
    (A,B)=PAMhan[1].data.shape
    if (a==YSIZE or a==A):
        my=0
        My=a
    else:
        if detector=='uv1' or detector=='uv2':
            my=YCEN-YSIZE/2-2051-1
            My=my+YSIZE+1
        else:
            my=YCEN-YSIZE/2-1
            My=my+YSIZE+1

    if (b==XSIZE or b==B):
        mx=0
        Mx=b
    else:
        mx=XCEN-XSIZE/2-1
        Mx=mx+XSIZE+1
    PAMdata=PAMhan[1].data.astype('f')[my:My,mx:Mx]
    data*=PAMdata
    error*=PAMdata
    #data/=flatdat

    #get the hand-selected bad-pixel map
    badHan=open(im+'.fits.badPix')
    dunk=badHan.readlines()
    badHan.close()
    badPixManual=[]
    for ii in range(len(dunk)):
        s=dunk[ii].split()
        xbp=int(float(s[0]))
        ybp=int(float(s[1]))
        for yy in range(-1,2):
            for xx in range(-1,2):
                badPixManual.append([xbp-1+xx,ybp-1+yy])

    #fill in all the bad pixels using the median bg value around the images 
    #edges (4 pixels wide) and with a linear interpolation within the 
    #central area near each individual input coordinate
    #ldata=data.reshape(a*b)*1.0
    #ldata.sort()
    #median=ldata[a*b/2]
    
    median=num.median(data)
    noise=num.std(data)
    
    #at least mark each manually identified bac pixel with the median of the image
    for ii in range(len(badPixManual)):
            data[badPixManual[ii][1],badPixManual[ii][0]]=median
    
    """
    for i in range(a):
        for j in range(4):
            if qual[i][j]>0:
                data[i][j]=median
            if qual[i][b-j-1]>0:
                data[i][b-j-1]=median
    for i in range(b):
        for j in range(4):
            if qual[j][i]>0:
                data[j][i]=median
            if qual[a-j-1][i]>0:
                data[a-j-1][i]=median
    """          
    

    if psfInterp:
        for kk in range(len(coords)):
            xp=int(coords[kk][0]-1+0.5)
            yp=int(coords[kk][1]-1+0.5)
            R=int(max(radii))+2

            x=[]
            y=[]
            gx=[]
            gy=[]
            for i in range(yp-R,yp+R):
                for j in range(xp-R,xp+R):
                    if (j<0 or j>b or i<0 or i>a): continue
                    
                    if qual[i][j]>0:# or ([j,i] in badPixManual):

                        x.append(j)
                        y.append(i)
                    else:
                        gx.append(j)
                        gy.append(i)
            
            bad_x=num.array(x)
            bad_y=num.array(y)
            good_x=num.array(gx)
            good_y=num.array(gy)
            
            if len(x)==0: continue

            [d,f]=im.split('/')
            os.chdir(d)
            
            FILTER=FILTER.replace('W','w').replace('M','m')
            obj=P.PSFFit(f+'.fits',1,useMemory='n')
            obj.initObject(0,coords[kk][0]-1,coords[kk][1]-1,1,3.5)
            try: os.remove('genImage.fits')
            except: pass
            try: os.remove('genImagePSF.fits')
            except: pass
            obj.produceImage(write='y')
            
            psfHan=pyfits.open('genImage.fits')
            psfData=(psfHan[0].data/EXPTIME)*PAMdata
            psfHan.close()
            
            
            try: os.remove('genImage.fits')
            except: pass
            try: os.remove('genImagePSF.fits')
            except: pass
            
            os.chdir('../')
            
            
            xmin=max(0,xp-R)
            xmax=min(b,xp+R)
            ymin=max(0,yp-R)
            ymax=min(a,yp+R)


            dcut=data[good_y,good_x]
            psfCut=psfData[good_y,good_x]
            w=num.where(dcut>median+median**0.5)[0]
            multi=num.sum(dcut[w]/psfCut[w])/len(w)
            
            data[bad_y,bad_x]=psfData[bad_y,bad_x]*multi+median

    if linearInterp: #probably will fail if the peak of the psf is on a bad pixel!
        for kk in range(len(coords)):                    
            xp=int(coords[kk][0]-1+0.5)
            yp=int(coords[kk][1]-1+0.5)
            R=int(max(radii))+2
            for i in range(yp-R,yp+R):
                for j in range(xp-R,xp+R):
                    if (j<0 or j>b or i<0 or i>a): continue
                    
                    if qual[i][j]>0 or ([j,i] in badPix):
                        x=[]
                        y=[]
                        z=[]
                        val=-1
                        c=-1
                        while val==-1:
                            if qual[i+c][j]==0:
                                val=data[i+c][j]
                            else:
                                c-=1
                        x.append(j)
                        y.append(i+c)
                        z.append(val)
                        
                        val=-1
                        c=1
                        while val==-1:
                            if qual[i+c][j]==0:
                                val=data[i+c][j]
                            else:
                                c+=1
                        x.append(j)
                        y.append(i+c)
                        z.append(val)
                        
                        val=-1
                        c=-1
                        while val==-1:
                            if qual[i][j+c]==0:
                                val=data[i][j+c]
                            else:
                                c-=1
                        x.append(j+c)
                        y.append(i)
                        z.append(val)
                        
                        val=-1
                        c=1
                        while val==-1:
                            if qual[i][j+c]==0:
                                val=data[i][j+c]
                            else:
                                c+=1
                                
                        x.append(j+c)
                        y.append(i)
                        z.append(val)
                        
                        inte=interp.bisplrep(x,y,z,kx=1,ky=1)
                        value=interp.bisplev([i],[j],inte)
                        data[i][j]=value
                        #print "BORKED! Something about linear interpolation produces negative pixel values. This has not been checked to see if it's OK. It could just be noise. Though it might not be!!!"


    #write the coordinates file
    han=open('spot','w+')
    for ii in range(len(coords)):
        print >>han,coords[ii][0],coords[ii][1]
    han.close()

    #setup the necessary keywords
    iraf.datapars.gain='CCDGAIN'
    iraf.datapars.itime=1.0
    iraf.datapars.saveParList()
    iraf.photpars.zmag=PHOTZPT

    
    #write out the interpolated image for photometry

    #get centroid
    #only works on the non-phot-flam-ed image
    if cent=='centroid':

                            
        try:
            os.remove('junk'+xxx+'_cent.fits')
            os.remove('junk'+xxx+'_cent.fits1.ctr.1')
        except:
            pass
        for ii in range(a):
            ha[1].data[ii,:]=data[ii,:]
        ha.writeto('junk'+xxx+'_cent.fits')


        iraf.photpars.apertur=radii[0]
        iraf.photpars.saveParList()

        iraf.center(image='junk'+xxx+'_cent.fits[1]',coords='spot',verify=no,interactive=no)
        Zing= iraf.txdump(textfiles = 'junk'+xxx+'_cent.fits1.ctr.1', fields = 'XCENTER,YCENTER', expr=iraf.yes, Stdout=1)
        
        try:
            os.remove('junk'+xxx+'_cent.fits1.ctr.1')
        except:
            pass

        XC=[]
        YC=[]
        ZZ=Zing[0].split()
        for ii in range(len(ZZ)/2):
            if 'INDEF' not in ZZ[0]:
                XC.append(float(ZZ[0]))
                YC.append(float(ZZ[1]))
            else:
                XC.append(-32768.)
                YC.append(-32768.)

        #write the new centroided coordinates file
        han=open('spot','w+')
        for ii in range(len(coords)):
            print >>han,XC[ii],YC[ii]
        han.close()

        iraf.centerpars.calgori=cent
        iraf.centerpars.saveParList()
    else:
        XC=[]
        YC=[]
        for ii in range(len(coords)):
            XC.append(coords[ii][0])    
            YC.append(coords[ii][1])


    data*=PHOTFLAM
    error*=PHOTFLAM
    
    for ii in range(a):
        ha[1].data[ii,:]=data[ii,:]

    try:
        os.remove('junk'+xxx+'.fits')
    except:
        pass

    ha.writeto('junk'+xxx+'.fits')
    ha.close()


    #get the quadrature sum photometric error approximately
    repFac=1
    E=num.repeat(num.repeat(error,repFac,axis=0),repFac,axis=1)/float(repFac*repFac)
    xcoord=num.repeat(num.array([num.arange(b*repFac)+1.]),(a)*repFac,axis=0)/float(repFac)
    ycoord=num.repeat(num.transpose(num.array([num.arange(a*repFac)+1.])),b*repFac,axis=1)/float(repFac)

    

    ES=[]
    for i in range(len(coords)):
        ES.append([])
        for kk in range(len(radii)):
            EEr=E*1.0
            dist=((xcoord-XC[i])**2+(ycoord-YC[i])**2)**0.5
            w=num.where(dist>radii[kk])
            EEr[w]*=0.0
            
            ES[i].append(num.sqrt(num.sum(EEr**2)))
            



    MS=[]
    MES=[]
    for i in range(len(coords)):
        MS.append([])
        MES.append([])
        
    for kk in range(len(radii)):
        iraf.photpars.apertur=radii[kk]
        iraf.photpars.saveParList()


        try:
            os.remove('spottedm')
        except:
            pass
        iraf.daophot.phot(image='junk'+xxx+'.fits[1]', coords='spot', output= 'spottedm', interactive=no, verbose=no)


        ####magnitude equation is
        ####  m = Z-2.5*LOG(f_DN*PHOTFLAM)
        ####magnitude error is then
        ####  dm=1.09/sqrt(f_DN*EXPTIME*GAIN)
        #### Remember that to get f back in ADU we need to divide by PHOTFLAM
        #### 0.95 included to consider approximate qe effect
        Zing= iraf.txdump(textfiles = 'spottedm', fields = 'MAG,FLUX,SUM', expr=iraf.yes, Stdout=1)

        for i in range(len(MS)):
            ZZ=Zing[i].split()
            #print ZZ
            if 'INDEF' not in ZZ[0]:
                magVal=float(ZZ[0])
                if aperCorrs<>[]: magVal-=aperCorrs[i]
                MS[i].append(magVal)
                
                ES[i][kk]*=2.5/num.log(10)
                ES[i][kk]/=float(ZZ[1])
                
                flux=float(ZZ[1])/PHOTFLAM
                dflux=num.sqrt(float(ZZ[2])/PHOTFLAM)

                #print flux,dflux
                MES[i].append(1.09*dflux/(flux*num.sqrt(GAIN*EXPTIME/0.95)))

            else:
                MS[i].append(-32768)
                MES[i].append(-32768)

    try:
        os.remove('junk'+xxx+'_cent.fits')
    except:
        pass
    try:
        os.remove('junk'+xxx+'.fits')
    except:
        pass


    if cent<>'none':
        return (MS,ES,median,XC,YC)
    else:
        return (MS,ES,median)
Ejemplo n.º 31
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')
Ejemplo n.º 32
0
def execute():

    ################NGC1023####################################################
    ## use GALFIT to fit and to create a model disk vs bulge##
    ##galfit.feedme##

    iraf.images()

    # path to fits files:
    total = config.iraf_input_dir+config.totalfits
    bulge = config.iraf_input_dir+config.bulgefits
    fraction = config.iraf_tmp_dir+config.fractionfits

    input_dir = config.iraf_input_dir
    tmp_dir = config.iraf_tmp_dir
    if not os.path.exists(tmp_dir):
        os.mkdir(tmp_dir)

    if os.path.exists(fraction):
         iraf.imdelete(fraction)

    print fraction
    print bulge

    # Makes the fraction image by dividing the bulge by the total:
    iraf.imarith(bulge, "/", total, fraction)

    """ LLT: I don't think we need this anymore:

    if os.path.exists(tmp_dir+"snb.fits"):
        iraf.module.imdelete(tmp_dir+"snb.fits")
    if os.path.exists(tmp_dir+"tnb.fits"):
        iraf.module.imdelete(tmp_dir+"tnb.fits")

    iraf.module.imcopy(bulge, tmp_dir+"snb.fits")
    iraf.module.imcopy(total, tmp_dir+"tnb.fits")
    """

    ##########################################################################
    ##Note: the coordinate in pixel as obtained from RA and Dec differ from the
    ##pixels one because of distorsion corrections as done with astrometry==>

    ##RA and Dec coordinate in arcsec are the correct one and don't need do be
    ##transfer in pixels

    ############################################################################
    ##Consistency check

    #displ(image="../fits_inputs/galaxy.fits",frame=1) 
    #tvmark(frame=1,coords="Kall.dat",color=205,lab-,num+)

    #displ(image="f.fits",zrange=no,zscale=no,ztrans="linear",z1=0,z2=1,frame=3) 
    #tvmark(frame=3,coords="Kall.dat",color=201,mark="rectangle",lengths="6",lab-,num-)

    ###########################################################################
    ##Use program overplot.sm to create both Kall.dat & compagna.dat
    ##in the RA & Dec of PNS there is a rotation of 90deg respect to the pixel
    ## x-->-x 
    ##after long reflexion I decide the right PA is 90-47.11=42.89 (just turning the PNe as observed on the R image)

    ##########################################################################
    ############################################################################

    ## to measure the relative flux use phot (daophot) ##with centerpars=none!!!###

    ##I used sigma=stdv(skys)=0  ===> no bg 
    ##fwhmpsf=2.27 as in galfit psf2.fits (imexam A enclosed)
    ##skyvalue=0 ===> no bg

    ##no centerpars
    ##apertures=3.0 pixels (=1.8 arcsec)

    ##zmag=25

    ##snb.fits & tnb.fits without bg (when I made the model)

    # filenames

    silentremove(config.bulgemag)
    silentremove(config.bulgetxt)
    silentremove(config.totalmag)
    silentremove(config.totaltxt)
    silentremove(config.fractionmag)
    silentremove(config.fractiontxt)

    iraf.noao(_doprint=False)
    iraf.digiphot(_doprint=False)
    iraf.daophot(_doprint=False)

    iraf.phot(bulge,config.position_file, config.bulgemag, skyfile="", datapars="",scale=1.,fwhmpsf=2.27,emissio="yes",sigma=0.0,datamin=-100, datamax=1e5,noise="poisson", centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1., cmaxiter=0,maxshift=0,clean="no",rclean=0.,rclip=0.,kclean=0., mkcenter="no", fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3., shireject=3.,khist=3.,binsize=0.1,smooth="no",rgrow=0., mksky="no", photpars="",weighting="constant",apertures=3,zmag=0.,mkapert="no", interactive="no",radplots="no",verify="no",update="no",verbose="no", graphics="stdgraph",display="stdimage",icommands="",gcommands="")

    # LLT: Corrected to pyraf, use stdout = instead of >>
    iraf.txdump(config.bulgemag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.bulgetxt)

    iraf.phot(total, config.position_file, config.totalmag, skyfile="", datapars="",scale=1.,fwhmpsf=2.27,emissio="yes",sigma=0.0,datamin=-100, datamax=1e5,noise="poisson", centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1., cmaxiter=0,maxshift=0.,clean="no",rclean=0.,rclip=0.,kclean=0., mkcenter="no", fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3., shireject=3.,khist=3.,binsize=0.1,smooth="no",rgrow=0., mksky="no", photpars="",weighting="constant",apertures=3,zmag=25.,mkapert="no", interactive="no",radplots="no",verify="no",update="no",verbose="no", graphics="stdgraph",display="stdimage",icommands="",gcommands="")

    iraf.txdump(config.totalmag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.totaltxt)

    iraf.phot(fraction,config.position_file, config.fractionmag, skyfile="", datapars="", scale=1., fwhmpsf=2.27, emissio="no", sigma=0.0, datamin=-100, datamax=1e5, noise="poisson", centerpars="", calgorithm="none", cbox=0, cthreshold=0., minsnratio=1., cmaxiter=0, maxshift=0., clean="no", rclean=0., rclip=0., kclean=0., mkcenter="no", fitskypars="", salgorithm="constant", annulus=10, dannulus=10, skyvalue=0., smaxiter=10, sloclip=0., shiclip=0., snreject=50, sloreject=3., shireject=3., khist=3., binsize=0.1, smooth="no", rgrow=0., mksky="no", photpars="", weighting="constant", apertures=3, zmag=25., mkapert="no", interactive="no", radplots="no", verify="no", update="no", verbose="no", graphics="stdgraph", display="stdimage", icommands="", gcommands="")

    iraf.txdump(config.fractionmag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.fractiontxt)
Ejemplo n.º 33
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
Ejemplo n.º 34
0
			else:
			    print 'NO OBJECTS MATCHED SHARPNESS/ROUNDNESS CRITERIA.'
                            pdb.set_trace()
		else:
		    sxx = np.array([sharp])
		    rxx = np.array([round])
		    gxx = np.array([ground])

		#if find_name[ff] == 'iaaug1jlq_flt.clean_cnts.fits0.coo.1': pdb.set_trace()
		find_sharp.append(sxx[0])
		find_round.append(rxx[0])
		find_ground.append(gxx[0])

		back, backrms = run_daophot(cnts_name[ff], coordfile=find_name[ff], outfile=phot_name[ff], calgorithm='gauss', cbox=10.,backmethod=backmeth)
		replace_filevalue(phot_name[ff], 'INDEF',-9999.0)
		iraf.txdump(phot_name[ff],'xcenter,ycenter,flux, mag', 'yes',Stdout=phot_name[ff]+'.trimmed')
		xc,yc,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,f25,f26, \
		m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16,m17,m18,m19,m20,m21,m22,m23,m24,m25,m26=np.loadtxt(phot_name[ff]+'.trimmed',unpack=True)

		# record various properties of observation (chip,amp,mjd, etc.)
		dd=pyfits.open(file)
		hdr0=dd[0].header
		im = dd[0].data
		dd.close()
		amps = hdr0['CCDAMP']
		filter = hdr0['FILTER']
		LTV1 = hdr0['LTV1']
		LTV2 = hdr0['LTV2']
		chip = hdr0['CCDCHIP']
		shut = hdr0['SHUTRPOS']
		expt = hdr0['EXPTIME']
Ejemplo n.º 35
0
        iraf.datapars.setParam('exposure','EXPTIME')
        iraf.centerpars.setParam('cbox','5.0')
        iraf.fitskypars.setParam('annulus','50')
        iraf.fitskypars.setParam('dannulus','10')
        iraf.photpars.setParam('zmag','17.5')

        clipcat=np.loadtxt(imgroot+'_psfms.clip')
        clipdemen = len(clipcat.shape)
 
        if clipdemen==1:
            fwhm = clipcat[3]
            napertures = 22
            apertures ="%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % tuple(np.array([0.5,0.7,1,1.2,1.5,2,2.5,3,4,5,6,7,8,9,10,12,14,16,18,20,22,25])*fwhm)
            iraf.photpars.setParam('apertures',apertures)
            iraf.daophot.phot(image=image,coords=imgroot+'_psfms.clip',output=imgroot+'_superphot.mags',interactive=0,verify=0,update=0,verbose=0)
            iraf.txdump(textfiles=imgroot+'_superphot.mags',fields='XCENTER,YCENTER,RAPERT,FLUX,MAG,MERR',expr='yes',headers=0,parameters=0,Stdout=imgroot+'_photdump')
            #iraf.txdump(textfiles=imgroot+'_superphot.mags',fields='IMAGE,XCENTER,YCENTER,MSKY,STDEV,NSKY,RAPERT,AREA,FLUX,MAG,MERR',expr='yes',headers=0,parameters=0,Stdout=aperall)
        if clipdemen==2:
            for i in range(clipcat.shape[0]):
                fwhm = clipcat[i,3]
                apertures ="%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % tuple(np.array([0.5,0.7,1,1.2,1.5,2,2.5,3,4,5,6,7,8,9,10,12,14,16,18,20,22,25])*fwhm)
                iraf.photpars.setParam('apertures',apertures)
                iraf.daophot.phot(image=image,coords=imgroot+'_psfms.clip',output=imgroot+'_s1'+'_superphot.mags',interactive=0,verify=0,update=0,verbose=0)
                iraf.txdump(textfiles=imgroot+'_superphot.mags',fields='XCENTER,YCENTER,RAPERT,FLUX,MAG,MERR',expr='yes',headers=0,parameters=0,Stdout=imgroot+'_photdump')
                #iraf.txdump(textfiles=imgroot+'_s1'+'_superphot.mags',fields='IMAGE,XCENTER,YCENTER,MSKY,STDEV,NSKY,RAPERT,AREA,FLUX,MAG,MERR',expr='yes',headers=0,parameters=0,Stdout=aperall)

        growth = open(imgroot+'_growth.dat','w')
        dumpdata = np.loadtxt(imgroot+'_photdump')
        growthdata = np.transpose(dumpdata[2:(3+napertures*4)].reshape((4,napertures)))
        growthhead = 'RAPERT FLUX MAG MERR'
        np.savetxt(imgroot+'_growth.dat',growthdata,header=growthhead)
Ejemplo n.º 36
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
Ejemplo n.º 37
0
iraf.noao.digiphot.apphot.phot.fitskypars.salgori = 'constant'
iraf.noao.digiphot.apphot.phot.cache = 'yes'
iraf.noao.digiphot.apphot.phot.salgori = 'constant'
iraf.phot(image='hff_abells1063-hffpar_f814w_microJy_backsub.fits',
          interactive='no',
          calgorithm='none',
          salgori='constant',
          coords='coords.coo',
          apertures='radii.txt',
          output='test.txt',
          verify='no')

iraf.txdump.textfile = 'test.txt'
iraf.txdump.expr = 'yes'

iraf.txdump(textfile='test.txt', fields='SUM', expr='yes', Stdout='stuff.txt')


def tidy_txdump(filename, band):
    radii = np.loadtxt("radii.txt")
    data = np.loadtxt(filename)
    a = np.array([data])
    b = a.T
    b = b.reshape(radii_pts, 1)
    radii = np.array([radii])
    radii = radii.reshape(radii_pts, 1)
    c = np.hstack((radii, b / np.max(b)))
    np.savetxt("out_" + band + ".txt", c)

    d = b / np.max(b)
Ejemplo n.º 38
0
def build(f):

    ### is this an MEF file
    current_ext = 0
    NEXTEND = 0

    EXTEND = f[0].header['EXTEND']
    if (EXTEND == "T"):
        NEXTEND = f[0].header['NEXTEND']
        current_ext = 1

    ### create the name of the output MEF psf file
    if not f[0].header.has_key('FILENAME'):
        os.unlink(opt.filename)
        sys.exit('The fits file ' + opt.filename + ' has no EXPNUM keyword\n')

    sexp = f[0].header['FILENAME']
    mef_psf = sexp + "p_psf_iraf.fits"
    ### create an MEF file that will contian the PSF(s)
    import pyfits
    fitsobj = pyfits.HDUList()
    prihdu = pyfits.PrimaryHDU()
    import re
    prihdu.header.update('FILENAME', sexp, comment='CFHT Exposure Numebr')
    prihdu.header.update('NEXTEND', NEXTEND, comment='number of extensions')
    version = re.match(r'\$Rev.*: (\d*.\d*) \$', __Version__).group(1)
    prihdu.header.update('MKPSF_V',
                         float(version),
                         comment="Version number of mkpsf")
    fitsobj.append(prihdu)
    if os.access(mef_psf, os.F_OK):
        os.unlink(mef_psf)
    fitsobj.writeto(mef_psf)
    fitsobj.close()
    outfits = pyfits.open(mef_psf, "append")
    prihdr = outfits[0].header

    import jjkmode

    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

    ### keep all the parameters locally cached.
    iraf.set(uparm="./")

    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()

    ### temp file name hash.
    tfile = {}

    while (current_ext <= NEXTEND):

        ### this is a psf SCRIPT so the showplots and interactive are off by force

        print "Working on image section " + str(current_ext)

        iraf.findpars.sharplo = 0
        iraf.findpars.sharphi = 0.7
        iraf.findpars.roundlo = -0.7
        iraf.findpars.roundhi = 0.7

        iraf.datapars.datamax = 20000
        iraf.datapars.airmass = 'AIRMASS'
        iraf.datapars.filter = 'FILTER'
        iraf.datapars.obstime = 'TIME-OBS'
        iraf.datapars.exposure = 'EXPTIME'
        iraf.datapars.gain = 'GAIN'
        iraf.datapars.ccdread = 'RDNOISE'
        iraf.datapars.fwhmpsf = opt.fwhm

        iraf.daopars.nclean = 2
        iraf.daopars.psfrad = 5.0 * opt.fwhm
        iraf.daopars.fitrad = 0.85 * opt.fwhm
        iraf.daopars.function = "gauss"

        iraf.centerpars.calgorithm = 'centroid'

        zero_mag = 26.19
        iraf.photpars.zmag = zero_mag
        iraf.photpars.apertures = int(0.85 * opt.fwhm)
        iraf.fitskypars.annulus = 2 + int(opt.fwhm * 4.00)
        iraf.fitskypars.dannulus = int(opt.fwhm * 2.0)
        iraf.daophot.verbose = no
        iraf.daophot.verify = no
        iraf.daophot.update = no

        iraf.psf.interactive = no
        iraf.pstselect.interactive = no

        iraf.datapars.saveParList()
        iraf.fitskypars.saveParList()
        iraf.centerpars.saveParList()
        iraf.findpars.saveParList()
        iraf.photpars.saveParList()

        tfiles = [
            'coo_bright', 'coo_ok', 'coo_faint', 'mag_all', 'mag_bright',
            'mag_ok', 'mag_good', 'mag_best', 'pst_in', 'pst_out', 'pst_out2',
            'prf', 'psg_org', 'psg', 'psf_1.fits', 'psf_2.fits',
            'psf_final.fits', 'psf_3.fits', 'psf_4.fits', 'mag_pst', 'coo_pst',
            'nst', 'nrj', 'seepsf.fits', 'sub.fits', 'fwhm', 'apcor'
        ]

        for file in tfiles:
            extname = "chip" + str(f[current_ext].header.get(
                'IMAGEID', str(current_ext))).zfill(2)
            tfile[file] = sexp + "_" + extname + "." + file
            if (os.access(tfile[file], os.F_OK)):
                os.unlink(tfile[file])

        if (EXTEND == "T"):
            this_image = opt.filename + "[" + extname + "]"
        else:
            this_image = opt.filename
        gain = f[current_ext].header.get('GAIN', 1.0)
        #### set sky/sigma parameters specific to this frame.
        (skyvalue, sigma) = jjkmode.stats(f[current_ext].data)
        import math
        sigma = math.sqrt(skyvalue / gain)
        datamin = float(skyvalue) - 8.0 * float(sigma)
        print "Determined sky level to be " + str(skyvalue) + " +/-" + str(
            sigma)

        iraf.datapars.datamin = datamin
        iraf.datapars.sigma = float(sigma)
        iraf.datapars.saveParList()

        iraf.fitskypars.skyvalue = skyvalue
        iraf.fitskypars.saveParList()
        ### find the bright stars in the image.
        print "sextracting for stars in " + this_image

        ###iraf.daophot.daofind(image=this_image,
        ###                     output=tfile['coo_bright'],threshold=4.0)

        os.system(
            "sex -c /home/cadc/kavelaar/12kproc/config/default.sex -SATUR_LEVEL 25000 -CATALOG_NAME "
            + tfile['coo_bright'] + " " + this_image)

        ### print "finding stellar locus in sround/ground space"
        print "clipping using star_class > 0.85 "
        fcoo = open(tfile['coo_bright'], 'r')
        lines = fcoo.readlines()
        fcoo.close()
        import numarray, math
        fout = open(tfile['coo_ok'], 'w')
        for line in lines:
            if re.match(r'^#', line) or re.search(r'INDEF', line):
                continue
            values = line.split()
            star_class = float(values[2])
            if star_class > 0.75:
                fout.write(line)
        fout.close()

        print "Measuring photometry for psf candidate stars in " + tfile[
            'coo_ok']
        iraf.daophot.phot(image=this_image,
                          coords=tfile['coo_ok'],
                          output=tfile['mag_bright'])

        ### do this selection in 2 steps because of the way IRAF handles INDEFs
        print "Selecting stars that have good centroids and magnitudes "
        iraf.pselect(
            tfile['mag_bright'], tfile['mag_ok'],
            "(CIER==0)&&(PIER==0)&&(SIER==0)&&(MSKY>0)&&(MSKY<2e5)&&(MSKY!=INDEF)"
        )
        print "Selecting stars that have normal sky levels"
        condition = "(abs(MSKY -" + str(skyvalue) + ") < 5.0*" + str(
            sigma) + ")"
        iraf.pselect(tfile['mag_ok'], tfile['mag_good'], condition)

        a = iraf.txdump(tfile['mag_good'], "SSKEW", iraf.yes, Stdout=1)
        aa = []
        for v in a:
            aa.append(float(v))

        a = numarray.array(aa)
        mean = a.mean()
        aa = a * a

        stddev = math.sqrt(aa.sum() / len(aa) - mean**2)
        limit = mean + 2 * stddev

        os.unlink(tfile['mag_good'])
        condition = condition + " && SSKEW < " + str(limit)
        iraf.pselect(tfile['mag_ok'], tfile['mag_good'], condition)

        print "Choosing the psf stars"
        iraf.pstselect(image=this_image,
                       photfile=tfile['mag_good'],
                       pstfile=tfile['pst_in'],
                       maxnpsf=25)

        ## construct an initial PSF image
        print "computing psf with neighbor stars based on complete star list"
        iraf.psf.mode = 'a'
        iraf.psf(image=this_image,
                 photfile=tfile['mag_bright'],
                 pstfile=tfile['pst_in'],
                 psfimage=tfile['psf_1.fits'],
                 opstfile=tfile['pst_out'],
                 groupfile=tfile['psg_org'],
                 varorder=0)

        try:
            print "subtracting the psf neighbors and placing the results in " + tfile[
                'sub.fits']
            iraf.daophot.nstar(image=this_image,
                               groupfile=tfile['psg_org'],
                               psfimage=tfile['psf_1.fits'],
                               nstarfile=tfile['nst'],
                               rejfile=tfile['nrj'])

            iraf.daophot.substar(image=this_image,
                                 photfile=tfile['nst'],
                                 exfile=tfile['pst_in'],
                                 psfimage=tfile['psf_1.fits'],
                                 subimage=tfile['sub.fits'])

            a = iraf.daophot.txdump(tfile['nst'], 'chi', 'yes', Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean**2)
            limit = mean + 2.5 * stddev

            print "Selecting those psf stars with CHI^2 <" + str(
                limit) + " after fitting with trial psf"
            iraf.pselect(tfile['nst'], tfile['mag_best'],
                         "CHI < " + str(limit))

            os.unlink(tfile['pst_out'])
            ##    os.unlink(tfile['psg'])
            ## rebuild the PSF file with the psf stars that fit well..
            ## using the neighbor subtracted image

            print "Rebuilding the PSF"

            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_2.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=0)

            print "re-subtracting with rebuilt psf"

            os.unlink(tfile['nst'])
            os.unlink(tfile['nrj'])
            iraf.daophot.nstar(image=this_image,
                               groupfile=tfile['psg'],
                               psfimage=tfile['psf_2.fits'],
                               nstarfile=tfile['nst'],
                               rejfile=tfile['nrj'])

            os.unlink(tfile['sub.fits'])
            iraf.daophot.substar(image=this_image,
                                 photfile=tfile['nst'],
                                 exfile=tfile['pst_in'],
                                 psfimage=tfile['psf_2.fits'],
                                 subimage=tfile['sub.fits'])

            os.unlink(tfile['psg'])
            os.unlink(tfile['pst_out'])
            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_3.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=0)

            os.unlink(tfile['nrj'])
            os.unlink(tfile['nst'])
            iraf.daophot.nstar(image=this_image,
                               groupfile=tfile['psg'],
                               psfimage=tfile['psf_3.fits'],
                               nstarfile=tfile['nst'],
                               rejfile=tfile['nrj'])

            a = iraf.daophot.txdump(tfile['nst'], 'chi', 'yes', Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean**2)
            limit = mean + 2 * stddev
            limit = 2.0

            #print "Selecting those psf stars with CHI^2 < "+str(limit)+" after fit with GOOD psf"

            os.unlink(tfile['mag_best'])
            iraf.pselect(tfile['nst'], tfile['mag_best'],
                         "CHI < " + str(limit))

            print "Building final PSF.... "
            os.unlink(tfile['sub.fits'])
            iraf.daophot.substar(image=this_image,
                                 photfile=tfile['nst'],
                                 exfile=tfile['pst_in'],
                                 psfimage=tfile['psf_3.fits'],
                                 subimage=tfile['sub.fits'])

            os.unlink(tfile['psg'])
            os.unlink(tfile['pst_out'])
            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_final.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=0)

            print "building an analytic psf for the FWHM calculations"
            os.unlink(tfile['pst_out'])
            os.unlink(tfile['psg'])
            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_4.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=-1)
        except:
            print sys.exc_info()[1]
            print "ERROR: Reverting to first pass psf"
            tfile['psf_final.fits'] = tfile['psf_1.fits']

            iraf.daophot.psf(image=this_image,
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_4.fits'],
                             opstfile=tfile['pst_out2'],
                             groupfile=tfile['psg'],
                             varorder=-1)

        psf_ap = iraf.photpars.apertures
        ap1 = int(psf_ap)
        ap2 = int(4.0 * opt.fwhm)
        apcor = "INDEF"
        aperr = "INDEF"
        if (0):
            #try
            ### now that we have the psf use the output list of psf stars
            ### to compute the aperature correction
            lines = iraf.txdump(tfile['pst_out'],
                                'xcen,ycen,mag,id',
                                iraf.yes,
                                Stdout=tfile['coo_pst'])

            ## set the lower ap value for the COG (normally set to 2)
            if (ap1 < 3):
                smallap = 1
            else:
                smallap = 2 - ap1 + 1
                ap1 = 2

            ap2 = int(math.floor(4.0 * opt.fwhm))
            naperts = ap2 - ap1 + 1

            iraf.photpars.apertures = str(ap1) + ":" + str(ap2) + ":1"
            iraf.photpars.saveParList()
            iraf.daophot.phot(image=this_image,
                              coords=tfile['coo_pst'],
                              output=tfile['mag_pst'])
            iraf.photcal()
            iraf.photcal.mkapfile(tfile['mag_pst'],
                                  naperts=naperts,
                                  apercors=tfile['apcor'],
                                  smallap=smallap,
                                  verify='no',
                                  gcommands='',
                                  interactive=0)
            fin = open(tfile['apcor'], 'r')
            lines = fin.readlines()
            values = lines[2].split()
            apcor = values[1]
            aperr = values[2]
        #except:

        ## compute the FWHM of the PSF image using the analytic PSF (VarOrd=-1)
        psf_file = pyfits.open(tfile['psf_4.fits'])

        fwhm = (psf_file[0].header.get('PAR1', 99.0) +
                psf_file[0].header.get('PAR2', 99.0))
        psf_file.close()
        #   ## Open the psf.fits
        infits = pyfits.open(tfile['psf_final.fits'])
        hdu = infits[0]
        inhdu = hdu.header
        inhdu.update('XTENSION', 'IMAGE', before='SIMPLE')
        inhdu.update('PCOUNT', 0, after='NAXIS2')
        inhdu.update('GCOUNT', 1, after='PCOUNT')
        del hdu.header['SIMPLE']
        del hdu.header['EXTEND']
        inhdu.update("EXTNAME", extname, comment="image extension identifier")
        #inhdu.update("SLOW",slow,comment="SROUND low cutoff")
        #inhdu.update("SIGH",sigh,comment="SROUND high cutoff")
        inhdu.update("PFWHM",
                     fwhm,
                     comment="FWHM of stars based on PSF fitting")
        inhdu.update("ZMAG", zero_mag, comment="ZMAG of PSF ")
        inhdu.update("BCKG", skyvalue, comment="Mean sky level in counts")
        inhdu.update("BCKG_STD",
                     sigma,
                     comment="standard deviation of sky in counts")
        inhdu.update("AP1", psf_ap, comment="Apperture used for PSF flux")
        inhdu.update("AP2", ap2, comment="Full Flux aperture")
        inhdu.update("APCOR", apcor, comment="Apperture correction (ap1->ap2)")
        inhdu.update("APERR", apcor, comment="Uncertainty in APCOR")

        #    ### append this psf to the output images....
        print "Sticking this PSF onto the output file"
        f[current_ext].header.update(
            "PFWHM", fwhm, comment="FWHM of stars based on PSF fitting")
        f[current_ext].header.update("BCKG",
                                     skyvalue,
                                     comment="Mean sky level in counts")
        f[current_ext].header.update(
            "BCKG_STD", sigma, comment="Standard deviation of sky in counts")
        f.flush()
        outfits.append(hdu)
        outfits.flush()
        infits.close()

        ### remove the temp file we used for this computation.
        for tf in tfile.keys():
            if os.access(tfile[tf], os.F_OK):
                os.unlink(tfile[tf])

        current_ext = current_ext + 1

    outfits.close()
    return mef_psf
        recen = raw_input('\n> Adjust recentering radius? [n]  ')

        if not recen: recen = 'n'

        if recen != 'n':
            recen_rad = raw_input('\n> Enter radius [' + str(recen_rad_1) +
                                  ']  ')
            if not recen_rad: recen_rad = recen_rad_1
            recen_rad = int(recen_rad)
            iraf.centerpars.cbox = recen_rad
            recen_rad_1 = recen_rad
            j += 1

    iraf.txdump(textfile=image + '.mag.' + str(j),
                fields='MAG,MERR',
                expr='yes',
                Stdout=os.path.join(outdir, image + '_SN_ap.txt'))

    iraf.txdump(textfile=image + '.als.' + str(j),
                fields='MAG,MERR',
                expr='yes',
                Stdout=os.path.join(outdir, image + '_SN_dao.txt'))

    apmag = np.genfromtxt(os.path.join(outdir, image + '_SN_ap.txt'))

    SNap = apmag[0]
    errSNap = apmag[1]

    daomag = np.genfromtxt(os.path.join(outdir, image + '_SN_dao.txt'))

    try:
Ejemplo n.º 40
0
def get_app_phot_target(image, ra=None, dec=None, plot=True, store=True, wcsin="logical", fwhm=None, box=15, arcsecpix=0.394, app=2):
    '''
    coords: files: 
    wcsin: can be "world", "logic"
    fwhm: in arcsec
    
    '''
    # Load packages; splot is in the onedspec package, which is in noao. 
    # The special keyword _doprint=0 turns off displaying the tasks 
    # when loading a package. 
    
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        fxn()

    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.unlearn("apphot")
    
    impf = pf.open(image)
    wcs = WCS(impf[0].header)
    #Check that actually the object is within this frame.
    if (ra is None or dec is None):
        if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")):
            ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC'))
        else:
            ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC'))
        print "Assuming ra=%.5f, dec=%.5f"%(ra, dec)
        pra, pdec = get_xy_coords(image, ra, dec)

    else:
        if("logic" in wcsin):
            pra, pdec = ra, dec
        else:
        #Using new method to derive the X, Y pixel coordinates, as wcs module does not seem to be working well.
            try:
                #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1)
                #print "Retrieved the pixel number"
                pra, pdec = get_xy_coords(image, ra, dec)
            except IndexError:
                print "Error with astrometry.net. trying the rudimentary method."
                pra, pdec = wcs.wcs_sky2pix(ra, dec, 1)
            #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0]

    shape = impf[0].data.shape
    
    if (pra > 0)  and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]):
        pass
    else:
        print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!"
        print pra, pdec, shape
        return
    
        
    imdir = os.path.dirname(image)
    imname = os.path.basename(image)
    plotdir = os.path.join(imdir, "photometry")

    if not os.path.isdir(plotdir):
        os.makedirs(plotdir)
        
    out_name = os.path.join(plotdir, imname +  ".seq.mag")
    clean_name = os.path.join(plotdir, imname +  ".objapp.mag")
    
    if (not fwhm is None):
        fwhm_value = fwhm
    elif (fitsutils.has_par(image, 'FWHM')):
        fwhm_value = fitsutils.get_par(image, 'FWHM')
    else:
        #Put some default value for Palomar
        fwhm_value=1.5
        
    if (wcsin == 'logical'):
        fwhm_value = fwhm_value / arcsecpix 
        
    if (fitsutils.has_par(image, 'AIRMASS')):
        airmass_value = fitsutils.get_par(image, 'AIRMASS')
    else:
        airmass_value = 1.3
        
    if (not fitsutils.has_par(image, "EXPTIME")):
        if (fitsutils.has_par(image, "ITIME") and fitsutils.has_par(image, "COADDS")):
            exptime = fitsutils.get_par(image, "ITIME")*fitsutils.get_par(image, "COADDS")
            fitsutils.update_par(image, "EXPTIME", exptime)
    exptime = fitsutils.get_par(image, 'EXPTIME')
    gain = fitsutils.get_par(image, 'GAIN')
    
    #print "FWHM", fwhm_value
    aperture_rad = math.ceil(float(fwhm_value)*app)      # Set aperture radius to two times the PSF radius
    sky_rad= math.ceil(aperture_rad*app*2)
    
    #print aperture_rad, sky_rad

    
    
    print "Saving coodinates for the object in pixels",pra,pdec
    coords = "/tmp/coords.dat"    
    np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f")

   
    
    if os.path.isfile(out_name): os.remove(out_name)
    if os.path.isfile(clean_name): os.remove(clean_name)


    iraf.noao.digiphot.apphot.qphot(image = image,\
    cbox = box ,\
    annulus = sky_rad ,\
    dannulus = 20. ,\
    aperture = str(aperture_rad),\
    coords = coords ,\
    output = out_name ,\
    plotfile = "" ,\
    zmag = 0. ,\
    exposure = "exptime" ,\
    airmass = "airmass" ,\
    filter = "filter" ,\
    obstime = "DATE" ,\
    epadu = gain ,\
    interactive = "no" ,\
    radplots = "yes" ,\
    verbose = "no" ,\
    graphics = "stdgraph" ,\
    display = "stdimage" ,\
    icommands = "" ,\
    wcsin = "logical",
    wcsout = "logical",
    gcommands = "") 


    #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name)
    iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name)

    

    ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"),  ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), \
        ("stdev","<f4"), ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")])
    if (ma.size > 0):  
        ma = np.array([ma])
        m = ma[~np.isnan(ma["fit_mag"])]
    else:
        print "Only one object found!"
        m = np.array([ma])
        

    insmag =  np.round(ma['fit_mag'][0] , 3)
    insmagerr = np.round(ma['fiterr'][0], 3)  
    if (fitsutils.has_par(image, "ZEROPT") and fitsutils.has_par(image, "ZEROPTU")):
        mag =  insmag + float(fitsutils.get_par(image, "ZEROPT"))
        magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2)  
    else:
	mag = 0
	magerr = 0
	
    if np.isnan(mag):
        mag, magerr = 0, 0
        insmag, insmagerr = 0,0           

   
    fitsutils.update_par(image, "INSMAG", "%.3f"%insmag )
    fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr)
    fitsutils.update_par(image, "APPMAG", np.round(mag, 3) )
    fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3))

         
    if (plot):
        
        #zmin, zmax = zscale.zscale(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50])
        
        #zmin, zmax = zscale.zscale(impf[0].data)
           
        #im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom")
        print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95)
        impf[0].data[np.isnan(impf[0].data)] = np.nanmedian(impf[0].data)
        print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95)

        im = plt.imshow(impf[0].data, vmin=np.percentile(impf[0].data, 5), vmax=np.percentile(impf[0].data, 95), origin="bottom")
       
        X = int(ma["X"][0])
        Y = int(ma["Y"][0])
        pra = int(pra)
        pdec = int(pdec)
        
        plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red")
        plt.colorbar(im)
        plt.savefig(os.path.join(plotdir, imname+".png"), dpi=200)
        plt.clf()
        
        zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T)
        im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50))
        c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position")
        c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad)
        c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid")
        c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad)
        plt.gca().add_artist(c1)
        plt.gca().add_artist(c11)
        plt.gca().add_artist(c2)
        plt.gca().add_artist(c22)
        plt.colorbar(im)
        
        myhandles = []
        markers = ["o", "o"]
        labels = ["Initial position", "Adjusted centroid"]
        cols = ["k", "orange"]
        for i in np.arange(len(markers)):
                myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i]))
        plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True)

        plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50])))
        plt.savefig(os.path.join(plotdir, imname+"_zoom.png"))
        plt.clf()
Ejemplo n.º 41
0
def psffit(img,
           fwhm,
           psfstars,
           hdr,
           interactive,
           _datamin,
           _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 gain 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 = _datamin
    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

    psfout = img.replace('.zogypsf', '') + '.psf.fits'
    iraf.delete('_psf.ma*,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?,' + psfout,
                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)
        os.system('cp _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',
             psfout,
             '_psf.psto',
             '_psf.psg',
             interac=interactive,
             verify=False,
             verbose=False)
    iraf.group(img + '[0]',
               '_psf.mag',
               psfout,
               '_psf.grp',
               verify=False,
               verbose=False)
    iraf.nstar(img + '[0]',
               '_psf.grp',
               psfout,
               '_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
Ejemplo n.º 42
0
	iraf.phot.setParam('image',raw_filenames[i])
	iraf.phot.setParam('coords','../coo_'+exam_date+'/')
	iraf.phot.setParam('output','../mag_'+exam_date+'/'+raw_filenames[i]+'.mag.1')
	iraf.phot.setParam('verify','no')
	photstring=iraf.phot(mode='h',Stdout=1)
                                                                                
	lenphotstring=len(photstring)
	print lenphotstring
	print photstring[0].split()[0]
	print photstring[0].split()[1]
	print photstring[0].split()[2]
	print photstring[0].split()[3]
	print photstring[0].split()[4]
	print photstring[0].split()[5]
  	iraf.txdump('../mag_'+exam_date+'/'+raw_filenames[i]+'.mag.1',
		    "xcenter,ycenter,xerr,yerr,mag,flux,area",'yes',
	            Stdout='../mag_'+exam_date+'/'+raw_filenames[i]+'.mag.1.txt')	
	txstr=iraf.txdump('../mag_'+exam_date+'/'+raw_filenames[i]+'.mag.1',
                    "xcenter,ycenter,xerr,yerr,mag,flux,area",'yes',
                    Stdout=1)
	lentxdump=len(txstr)
	print txstr[1]
	print len(txstr[1])

	for spot in range(1,lentxdump-1):
	      
		xcoord=float(txstr[spot].split()[0])
		ycoord=float(txstr[spot].split()[1])
		index_check=where((abs(x-xcoord) < 11) &
				  (abs(y-ycoord) < 11))
		if index_check[0].nelements()==0: 
Ejemplo n.º 43
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
Ejemplo n.º 44
0
iraf.unlearn('daophot')
iraf.setimpars('int','yes','no') 
FWHM = raw_input('What is the stellar FWHM in pixels?\n')
skysigma = raw_input('What is the sky sigma in the first image?\n')
iraf.datapars.scale = 1/float(FWHM)
iraf.datapars.sigma = float(skysigma)

# Finding calibration stars
os.system('rm *.coo.*')
iraf.daofind('../SCIENCE/' + image0,'default',verify='no',verbose='no')
os.system('rm *.mag.*')
iraf.phot('../SCIENCE/' + image0,'default','default',verify='no',verbose='no')
try:
	os.remove('calibcoords')
except: True
iraf.txdump(image0 + '.mag.1','xcenter,ycenter',\
"cerror='NoError' && perror='NoError' && serror='NoError' && merr<0.005 && mag>0",Stdout = 'calibcoords')

coordarray = []
f = open('calibcoords')
calibcoords = f.readlines()
f.close()
for i in calibcoords:
	coordarray.append(i)

# Calculate mean calibration star magnitude for each imagine
os.system('rm *.mag.*')
iraf.phot('@imagelist','calibcoords','default',verify='no',verbose='no')

os.system('rm magfiles')
iraf.files(path + 'lightcurve/al*mag.1',Stdout = 'magfiles')
magarray = []
Ejemplo n.º 45
0
    # Open files to store data that imalign will need later on
    obj_list = open(current_object + ".lis", "w")
    shifts_list = open(current_object + ".shifts", "w")    
    coords_list = open(current_object + ".coords","w")
    output_list = open(current_object + ".out", "w")

    # All images of this object. Read fiirst as reference
    whr = np.where(list_images["objname"] == current_object)[0]
    ref_im = list_images["filename"][whr[0]]
    ref_catalog = utilities.replace_extension(ref_im, ".cat")
    
    # Txdump writes to a temporary file, then we read it
    iraf.ptools(_doprint=0)
    if os.path.isfile("temp.txt"):
        os.remove("temp.txt")
    iraf.txdump(ref_catalog, "xcenter, ycenter, mag", "yes", Stdout="temp.txt")
    x_ref=y_ref=mag_ref=np.array([],dtype=np.float16)
    with open("temp.txt") as f:
        for line in f:
            x_ref = np.append(x_ref,float(line.split()[0]))
            y_ref = np.append(y_ref,float(line.split()[1]))
            mag_ref = np.append(mag_ref, float(line.split()[2]))  
    brightest_stars = np.argsort(mag_ref)[:nstars] 
    x_ref = x_ref[brightest_stars] 
    y_ref = y_ref[brightest_stars]
    
    #Write to a file, which imalign will need later
    for ii,jj in zip(x_ref,y_ref):
        coords_list.write( str(ii) + " " + str(jj) + "\n")
    coords_list.close()
            
Ejemplo n.º 46
0
def errore(size, truemag, fwhm0, leng0):
    from pyraf import iraf
    import string, os, sys

    dartf = 100
    while dartf >= size - 1:
        artfac0 = raw_input(
            '>>> Dispersion of artificial star positions (in units of FWHM) [1] '
        )
        if not artfac0: artfac0 = 1
        try:
            artfac0 = float(artfac0)
            if float(artfac0) >= size - 1:
                print '!!! WARNING: ' + str(
                    artfac0) + ' too large (max ' + str(size) + '- 1)'
                print 'try again....'
            else:
                dartf = artfac0
        except:
            print '#### WARNING: ' + str(artfac0) + ' should be a number !!!!'
            print 'try again....'
    iraf.delete("tmpar?", ve='no')
    i = 0
    tmpart = []
    while i <= 8:
        iraf.delete(
            "reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?",
            ve='no')
        artrad = fwhm0 / 2.
        #artseed = artseed+1234
        artx = int(i / 3.) - 1
        if i <= 2: arty = artx + i
        if 3 <= i <= 5: arty = artx - 1 + i - 3
        if i >= 6: arty = artx - 2 + i - 6

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

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

        iraf.delete("artlist.coo", ve='no')
        ff = open("artlist.coo", 'w')
        ff.write(str(xbb) + '  ' + str(ybb) + '  ' + str(truemag[0]) + "  1")
        ff.close()

        xb1 = int(float(xbb) - fwhm0 * float(leng0) / 2)
        xb2 = int(float(xbb) + fwhm0 * float(leng0) / 2)
        yb1 = int(float(ybb) - fwhm0 * float(leng0) / 2)
        yb2 = int(float(ybb) + fwhm0 * float(leng0) / 2)
        sec = "1 " + str(xb1) + " 1 " + str(nay) + '\n'
        sec = sec + str(xb2) + ' ' + str(nax) + " 1 " + str(nay) + '\n'
        sec = sec + str(xb1) + ' ' + str(xb2) + " 1 " + str(yb1) + '\n'
        sec = sec + str(xb1) + ' ' + str(xb2) + ' ' + str(yb2) + ' ' + str(
            nay) + '\n'
        ff = open('sec', 'w')
        ff.write(sec)
        ff.close()

        iraf.delete("reserr.art", ve='no')
        iraf.delete("artlist.mag", ve='no')
        iraf.delete("artlist.als", ve='no')
        iraf.addstar("skyfit",
                     "artlist.coo",
                     imgpsf,
                     "reserr",
                     nstar=1,
                     veri='no',
                     simple='yes')
        iraf.imsurfit("reserr",
                      "artbg",
                      xorder=xbgord0,
                      yorder=ybgord0,
                      regions="section",
                      section="sec")
        iraf.imarith("reserr", "-", "artbg", "artstar")
        iraf.phot("artstar", "artlist.coo", "artlist.mag", veri='no')
        iraf.allstar("artstar",
                     "artlist.mag",
                     imgpsf,
                     "artlist.als",
                     "",
                     "artres",
                     veri='no',
                     verb='no')
        iraf.imarith("artstar", "-", "artres", "artfit")
        iraf.imarith("reserr", "-", "artfit", "artskyfit")
        if i == 0: era = 'yes'
        else: era = 'no'
        artx = .5 + .25 * artx
        arty = .5 + .25 * arty
        iraf.display("artskyfit",
                     1,
                     fill='yes',
                     erase=era,
                     xcen=artx,
                     ycen=arty,
                     xsize=.25,
                     ysize=.25)
        try:
            tmpart.append(
                float(
                    iraf.txdump("artlist.als", "mag", expr='yes',
                                Stdout=1)[0]))
        except:
            pass
        i = i + 1

    for i in tmpart:
        print i

    print " ########## "
    try:
        media = mean(array(tmpart))
        arterr = std(array(tmpart))
        arterr2 = std(
            compress((average(tmpart) - std(tmpart) < array(tmpart)) &
                     (array(tmpart) < average(tmpart) + std(tmpart)),
                     array(tmpart)))
    except:
        media = 0
        arterr = 0
        arterr2 = 0
    print '### average = %6.6s \t arterr= %6.6s ' % (str(media), str(arterr))
    print '###  %6.6s \t (error at 1 sigma rejection) ' % (str(arterr2))
    iraf.delete(
        "reserr.fit?,artbg.fit?,artstar.fit?,artres.fit?,artfit.fit?,artskyfit.fit?",
        ve='no')
    iraf.delete("reserr.art", ve='no')
    iraf.delete("artlist.*", ve='no')
    return arterr2, arterr
Ejemplo n.º 47
0
def makeillumination(lista,flatfield):#,outputfile,illum_frame):
    import os,glob,string,re
    from astropy.io import fits as pyfits
    import ntt
    from ntt.util import readhdr, readkey3, delete, display_image, defsex,  name_duplicate, correctcard
    from numpy import compress, array, argmax, argmin, min, argsort, float32
    import datetime
    MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days
    _date = readkey3(readhdr(lista[0]), 'date-night')
    _filter = readkey3(readhdr(lista[0]), 'filter')
    illum_frame = name_duplicate(
        lista[0], 'illum_' + _date + '_' + _filter + '_' + str(MJDtoday), '')
    from pyraf import iraf
    iraf.images(_doprint=0)
    iraf.imutil(_doprint=0)
    iraf.utilities(_doprint=0)
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.generic(_doprint=0)
    toforget = ['digiphot.daophot', 'imutil.imarith',
                'image', 'utilities.surfit']
    for t in toforget:
        iraf.unlearn(t)
    n = len(lista)
#   start loop to read image names from the input file
    lista1 = []
    iraf.ccdred.verbose = 'no'
    ff = open('templist.lst', 'w')
    for i in range(0, len(lista)):
        ff.write('C' + lista[i] + '\n')
        delete('C' + lista[i])
        delete('C' + re.sub('.fits', '_sub.fits', lista[i]))
        ntt.sofiphotredudef.crosstalk(lista[i], 'C' + lista[i])
        iraf.noao.imred.ccdred.ccdproc('C' + lista[i], output='', overscan="no", trim="yes", ccdtype='', darkcor='no', fixpix='no', zerocor="no", flatcor='yes',
                                       illumco='no', trimsec='[1:1024,1:1007]', biassec='', flat=flatfield, illum='')
        correctcard('C' + lista[i])
        lista1.append('C' + lista[i])
    ff.close()
    print '\n### prereducing STD frames to compute illumination correction ........'
    lista2, skyfile = ntt.sofiphotredudef.skysub(lista1, readkey3(
        readhdr(lista1[0]), 'ron'), readkey3(readhdr(lista1[0]), 'gain'), True)
    lista2 = ntt.sofiphotredudef.sortbyJD(lista2)
    print '\n### use x on the star and q  to continue....'
    display_image(lista2[0], 2, '', '', False)
    delete('tmpone.coo')
    iraf.image.tv.imexamine(lista2[0], 2, logfile='tmpone.coo',
                            keeplog='yes', xformat='', yformat='', wcs='logical')
    iraf.tvmark(2, 'tmpone.coo', mark="circle", number='yes',
                label='no', radii=8, nxoffse=5, nyoffse=5, color=204, txsize=2)
    xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
    x0, y0 = string.split(xycoo[0])
    x0 = float(x0)
    y0 = float(y0)
    xcum0 = readkey3(readhdr(lista2[0]), 'xcum')
    ycum0 = readkey3(readhdr(lista2[0]), 'ycum')
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.noao.digiphot.daophot.datapars.datamin = -1000
    iraf.noao.digiphot.daophot.datapars.datamax = 60000
    iraf.noao.digiphot.daophot.daopars.function = 'gauss'
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    namesex = defsex('default.sex')
    for i in range(0, len(lista2)):
        j = i + 1
        xcum = readkey3(readhdr(lista2[i]), 'xcum')
        ycum = readkey3(readhdr(lista2[i]), 'ycum')
        xx = x0 - xcum0 + xcum
        yy = y0 - ycum0 + ycum
        # sex objects
        os.system('sex ' + lista2[i] + ' -c ' + namesex + '>  _logsex')
        delete('_logsex')
        xpix = iraf.proto.fields('detections.cat', fields='2', Stdout=1)
        ypix = iraf.proto.fields('detections.cat', fields='3', Stdout=1)
        cm = iraf.proto.fields('detections.cat', fields='4', Stdout=1)
        cm = compress((array(xpix) != ''), array(cm, float))
        ypix = compress((array(xpix) != ''), array(ypix, float))
        xpix = compress((array(xpix) != ''), array(xpix, float))
        if len(xpix) > 300:
            num = 300
        else:
            num = len(xpix) - 1
        xpix = xpix[argsort(cm)][0:num]
        ypix = ypix[argsort(cm)][0:num]
        distance = (ypix - yy)**2 + (xpix - xx)**2
        xx1, yy1 = xpix[argmin(distance)], ypix[argmin(distance)]
        f = open('tmpone.coo', 'w')
        f.write(str(xx1) + ' ' + str(yy1) + '\n')
        f.close()
        display_image(lista2[i], 1, '', '', False)
        iraf.tvmark(1, 'tmpone.coo', mark="circle", number='yes',
                    label='no', radii=8, nxoffse=5, nyoffse=5, color=204, txsize=2)
        answ = 'n'
        while answ != 'y':
            answ = raw_input('selected the right one [[y]/n] ?')
            if not answ:
                answ = 'y'
            if answ in ['y', 'YES', 'yes', 'Y']:
                print lista2[i]
                delete('pippo.' + str(j) + '.mag')
                gggg = iraf.digiphot.daophot.phot(
                    lista2[i], "tmpone.coo", output="pippo." + str(j) + ".mag", verify='no', interac='no', Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'
            else:
                print '\n### select the std star'
                display_image(lista2[i], 1, '', '', False)
                iraf.image.tv.imexamine(lista2[
                                        i], 1, logfile='tmpone.coo', keeplog='yes', xformat='', yformat='', wcs='logical')
                xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
                x2, y2 = string.split(xycoo[0])
                f = open('tmpone.coo', 'w')
                f.write(str(x2) + ' ' + str(y2) + '\n')
                f.close()
                delete('pippo.' + str(j) + '.mag')
                print '###### new selection ' + str(x2), str(y2)
                gggg = iraf.digiphot.daophot.phot(
                    lista2[i], "tmpone.coo", output='pippo.' + str(j) + '.mag', verify='no', interac='no', Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'

    os.system('ls pippo.*.mag > tempmag.lst')
    tmptbl0 = iraf.txdump(textfile="@tempmag.lst",
                          fields="XCENTER,YCENTER,FLUX", expr='yes', Stdout=1)
    ff = open('magnitudini', 'w')
    for i in tmptbl0:
        ff.write(i + '\n')
    ff.close()
#   delete the temporary images and files
    delete("temp*.fits")
    delete('temp*.lst')
    delete(illum_frame)
    print '\n### fitting the illumination surface...'
    aaa = iraf.utilities.surfit('magnitudini', image=illum_frame, function="polynomial",
                                xorder=2, yorder=2, xterms="full", ncols=1024, nlines=1024, Stdout=1)
    iraf.noao.imred.generic.normalize(illum_frame)
    correctcard(lista[0])
    data, hdr = pyfits.getdata(illum_frame, 0, header=True)
    data0, hdr0 = pyfits.getdata(lista[0], 0, header=True)
    delete(illum_frame)
    pyfits.writeto(illum_frame, float32(data), hdr0)
    flatfield0 = string.split(flatfield, '/')[-1]
    ntt.util.updateheader(
        illum_frame, 0, {'MKILLUM': [flatfield0, 'flat field']})
    display_image(illum_frame, 1, '', '', False)
    for i in range(0, len(lista)):  # in lista:
        img = lista[i]
        delete('pippo.' + str(i) + '.mag')
        delete('C' + img)
        delete('C' + re.sub('.fits', '_sky.fits', img))
#    delete('C*.fits.mag.1')
#    iraf.hedit(illum_frame,'MKILLUM','Illum. corr. created '+flatfield,add='yes',update='yes',verify='no')
    return illum_frame
Ejemplo n.º 48
0
def manusn(img,imgpsf,dmag0,apori1,apori2,apori3,apmag1,apmag2,apmag3,fitmag,truemag,magerr,centx,centy,z11,z22,midpt,size,fwhm0,x1,y1,arterr):
   from pyraf import iraf
   import lsc
   import string,re,sys,os
   from numpy import zeros
   a2 = int(2.*fwhm0+.5)
   fdmag = 10**(-0.4*float(dmag0))
   lsc.util.delete(",_snfit.fit?,skyfit.fit?,_snfit.ar?")
   if truemag[0]=='INDEF':
       print 'ACTUNG'
       magerr[0]=0.0
       lsc.util.delete('test.fits')
       os.system('echo '+str(iraf.field(img+'.sn.coo', field='1,2', Stdout=1)[0])+' '+dmag0+' > dddd')
       iraf.addstar("snfit","dddd",imgpsf,"_snfit",nstar=1,veri='no',simple='yes',verb='yes')
       lsc.util.delete('dddd')
   else:
       iraf.imarith("snfit.fits","*",fdmag,"_snfit.fits")   
   iraf.imarith("original.fits","-","_snfit.fits","skyfit.fits")
   _tmp1,_tmp2,goon=lsc.util.display_image('original.fits',1, z11, z22, False, _xcen=.25, _ycen=.25, _xsize=.3, _ysize=.3)
   print z11,z22,midpt
   z01 = float(z11)-float(midpt)
   z02 = float(z22)-float(midpt) 
   s1 = 1
   s2 = -int(fwhm0)
   lsc.util.delete("tmptbl")
   ff=open('tmptbl','w')
   ff.write(str(s1)+' '+str(s2)+" ORIGINAL") 
   ff.close()
   iraf.tvmark(1,"tmptbl",autol='yes',mark="none",inter='no',label='yes',txsize=2)
   _tmp1,_tmp2,goon=lsc.util.display_image('_snfit.fits',1, z01, z02, False, _xcen=.25, _ycen=.75, _xsize=.3, _ysize=.3, _erase='no')
   lsc.util.delete("tmptbl")
   tmptbl=iraf.txdump(img+".sn.als","xcen,ycen",expr='yes', Stdout=1)
   ff=open('tmptbl','w')
   for i in tmptbl:
       ff.write(i) 
   ff.close()  
   lra = int((2*size*fwhm0)*2)
   iraf.tvmark(1,"tmptbl",autol='no',mark="circle", number='yes',nyoffset=lra,radi=a2,txsize=2,inter='no')
   s1 = 1
   s2 = -int(fwhm0)
   lsc.util.delete("tmptbl")
   ff=open('tmptbl','w')
   ff.write(str(s1)+' '+str(s2)+" FITTED") 
   ff.close()  
   iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
   _tmp1,_tmp2,goon=lsc.util.display_image('skyfit.fits',1, z11, z22, False, _xcen=.75, _ycen=.25, _xsize=.3, _ysize=.3, _erase='no')
   s1 = 1
   s2 = -int(fwhm0)
   lsc.util.delete("tmptbl")
   ff=open('tmptbl','w')
   ff.write(str(s1)+' '+str(s2)+" RESIDUAL") 
   ff.close()
   iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)

   newmag=list(zeros(len(truemag)))
   for i in range(0,len(truemag)):
       try:
           newmag[i]=float(truemag[i])+float(dmag0)
       except:
           newmag[i]=float(dmag0)
           magerr[i]=0.0
   print truemag
   print newmag

   print "***************************************************************************" 
   print "#id  x_ori   y_ori     x     y    ap_ori ap_bgsub  fit_mag  err_art  err_fit" 
   for i in range(len(fitmag)):
       print "SN",i,str(centx[i]+x1),str(centy[i]+y1),str(centx[i]),str(centy[i]),"  ",str(apori3[i]),"  ",str(apmag3[i]),"  ",str(newmag[i]),"  ",str(arterr),"  ",str(magerr[i])
   print "**************************************************************************"

   return apori1,apori2,apori3,apmag1,apmag2,apmag3,fitmag,truemag,magerr,centx,centy,newmag
Ejemplo n.º 49
0
def execute():

    #here we define the coords variable as a list
    coords=[]

    #indenting matters in python. Everything
    #indented under the for loop is inside the loop
    #pdb.set_trace() # break point for python debugger
    for line in open(coofnamein):
        if (line.startswith("#") or line.startswith("z1")):
            continue
        #this skips the lines that start with a #
        line=map(float,line.split())
        #this takes the line and splits it into a list of floats
        if len(line) != 4:
            continue
        #skips the lines that don't have 4 floats
        coords.append((line[0],line[1]))
        #adds the most recent coords (the 0th and 1st elements
        # to the list of coordinates

        #print coords

    coofnameout1=open(coofnameout, "w")

    for ci in coords:
        coofnameout1.write("%.4g\t%.4g\n" % ci)
        #the strange syntax is the formatting for the
        #output. It prints each coordinate with 4 decimal places
        # a tab in between, and then a new line at the end
    coofnameout1.close()
    #sys.exit(1)

    l=os.listdir(image_dir)
    l=[li for li in l if li.endswith(endstr)]
    l=[li for li in l if li.startswith(rootstr)]
    l =[image_dir + li for li in l]
    #this list takes all the files in the directory,
    #then only keeps those with the correct beginning and ending

    #l = l[:10]
    #this line can tell the code to only run on the first 10 images
    #useful for debugging

    lc_out1 = open(lc_out,"w")
    for li in l:
        print li
        #print coofnameout
        #print phot_out
        iraf.phot(li,coofnameout, phot_out, plotfile="", datapars="", centerpars="", fitskypars="", photpars="",interactive="no", radplots="no", icommands="", gcommands="", verify="no", weighting="constant", calgorithm="centroid", obstime=obstime, annulus=annulus,dannulus=dannulus,skyvalue=skyvalue,fwhmpsf=fwhmpsf,sigma=sigma)
        #this actually does the photometry
        #note all of the parameters that must be defined

        standard_output = sys.stdout
        #saves the current output -ie to the screen
        txdumpfile = open(txdumpout, 'w')
        sys.stdout = txdumpfile
        #directs the output to the txdumpfile

        iraf.txdump(phot_out, "otime,mag,merr", "yes", headers="no", parameters="no")
        #does the txdump - might you want to output other information?

        sys.stdout = standard_output # restores the stdout
        txdumpfile.close() # close the file
        os.remove(phot_out)
        #deletes the file

        phots  =[]

        for line in open(txdumpout):
            #line=map(string,line.split())
            line=str.split(line)
            phots.append((line[1],line[2]))
            #print phots

        #print line[0]

        #lc_out1.write(line[0] + "\t")
        #print stringtohours(line[0])
        lc_out1.write( "%10.7f \t" % (stringtohours(line[0])))


        for photsi in phots:
            lc_out1.write(photsi[0] + "\t")
            lc_out1.write(photsi[1] + "\t")
        lc_out1.write("\n")

        os.remove(txdumpout)
Ejemplo n.º 50
0
def fitsn(img,imgpsf,coordlist,_recenter,fwhm0,original,sn,residual,_show,_interactive,dmax,dmin,z11='',z22='',midpt='',size=7,apco0=0):
    import lsc
    lsc.util.delete("apori")
    lsc.util.delete(img+".sn.mag")
#################################
    from pyraf import iraf
    import string
    iraf.imcoords(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    from iraf import digiphot
    from iraf import daophot
    from iraf import ptools
    a1 = int(fwhm0)
    a2 = int(2.*fwhm0+.5)
    a3 = int(3.*fwhm0+.5)
    a4 = int(4.*fwhm0+.5)
    ap = str(a1)+","+str(a2)+","+str(a3)
########################################
    if _recenter:        answ='yes'
    else:                answ='no'
#########################################
    hdr=lsc.util.readhdr(img+'.fits')
    _gain=lsc.util.readkey3(hdr,'gain')
    _ron=lsc.util.readkey3(hdr,'ron')
    _exptime=lsc.util.readkey3(hdr,'exptime')
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    iraf.noao.digiphot.daophot.datapars.readnoi = _gain
    iraf.noao.digiphot.daophot.datapars.epadu = _ron
    iraf.noao.digiphot.daophot.datapars.datamin = dmin
    iraf.noao.digiphot.daophot.datapars.datamax = dmax
    iraf.noao.daophot.fitskypars.annulus=a3
    iraf.noao.daophot.photpars.apertures = ap
    iraf.noao.digiphot.daophot.datapars.exposure = 'exptime'
    iraf.noao.digiphot.daophot.datapars.airmass = 'airmass'
    iraf.noao.digiphot.daophot.datapars.filter = 'filter2'
    iraf.noao.digiphot.daophot.daopars.psfrad = a4
    #  modify fitrad to 3 fwhm to see if works better     
    iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0 #* 3
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.recenter = answ
    iraf.noao.digiphot.daophot.daopars.fitsky = 'yes'
#    iraf.noao.digiphot.daophot.centerpars.cbox = 0
    iraf.noao.digiphot.daophot.centerpars.cbox = 4
    iraf.noao.digiphot.daophot.centerpars.calgori = 'gauss'

    #  fitskypars.salgorithm = "constant"
    #  fitskypars.skyvalue = 0
    print '\n### recentering: '+str(answ)
    if _show:
        iraf.noao.digiphot.daophot.phot(original,coordlist,"apori",veri='no')   
        iraf.noao.digiphot.daophot.phot(sn,coordlist,img+".sn.mag",veri='no')   
    else:
        iraf.noao.digiphot.daophot.phot(original,coordlist,"apori",veri='no',verb='no')   
        iraf.noao.digiphot.daophot.phot(sn,coordlist,img+".sn.mag",veri='no',verb='no')   

    lsc.util.delete(img+".sn.als")
    print sn,imgpsf,img
    iraf.allstar(sn,img+".sn.mag",imgpsf,img+".sn.als","",residual,veri='no',verb='no')
    lsc.util.delete("snfit.fits")
    iraf.imarith(sn+'.fits',"-",residual+'.fits',"snfit.fits")
    lsc.util.delete("skyfit.fits")
    iraf.imarith(original+'.fits',"-","snfit.fits","skyfit.fits")
    iraf.txsort(img+".sn.als","ID")
    tmptbl = iraf.txdump(img+".sn.als","mag,merr,xcenter,ycenter",expr='yes', Stdout=1)
    magerr,fitmag,centx,centy=[],[],[],[]
    for i in tmptbl:
        try:
            fitmag.append(float(string.split(i)[0]))#-2.5*log10(_exptime))
        except:
            fitmag.append(string.split(i)[0])
        try:
            magerr.append(float(string.split(i)[1]))
        except:
            magerr.append(string.split(i)[1])
        centx.append(float(string.split(i)[2]))
        centy.append(float(string.split(i)[3]))
    tmptbl=iraf.txdump("apori","mag",expr='yes', Stdout=1)
    apori1,apori2,apori3=[],[],[]
    for i in tmptbl:
        try:
            apori1.append(float(string.split(i)[0]))#-2.5*log10(_exptime))
        except:
            apori1.append(string.split(i)[0])
        try:            
            apori2.append(float(string.split(i)[1]))#-2.5*log10(_exptime))
        except:
            apori2.append(string.split(i)[1])
        try:
            apori3.append(float(string.split(i)[2]))#-2.5*log10(_exptime))
        except:
            apori3.append(string.split(i)[2])
            
    iraf.txsort(img+".sn.mag","YCENTER")
    tmptbl=iraf.txdump(img+".sn.mag","mag,magerr",expr='yes', Stdout=1) 

    if _show:
        print "********************************************************************"
        print "ID <apmag on original>  <apmag on bgsubt> fitmag truemag err_fit"         
        print "     ",a1,"       ",a2,"      ",a3,"        ",a1,"     ",a2,"     ",a3 


    apmag1,apmag2,apmag3,truemag=[],[],[],[]
    for i in range(len(tmptbl)):
        try:
            apmag1.append(float(string.split(tmptbl[i])[0]))#-2.5*log10(_exptime))
        except:
            apmag1.append(9999)
        try:
            apmag2.append(float(string.split(tmptbl[i])[1]))#-2.5*log10(_exptime))
        except:
            apmag2.append(9999)
        try:
            apmag3.append(float(string.split(tmptbl[i])[2]))#-2.5*log10(_exptime))
        except:
            apmag3.append(9999)
        try:
            truemag.append(fitmag[i]+float(apco0))
        except:
            truemag.append('INDEF')
        if _show:
            print i,apori1[i],apori2[i],apori3[i],apmag1[i],apmag2[i],apmag3[i],fitmag[i],truemag[i],magerr[i]
    if _show:
        print "********************************************************************"

    if _show:
        print midpt,z11,z22
        _tmp1,_tmp2,goon=lsc.util.display_image(original+'.fits',1, z11, z22, False, _xcen=.25, _ycen=.25, _xsize=.3, _ysize=.3)
        z01 = float(z11)-float(midpt)
        z02 = float(z22)-float(midpt) 
        s1 = 1
        s2 = -int(fwhm0)
        lsc.util.delete("tmptbl")
        ff=open('tmptbl','w')
        ff.write(str(s1)+' '+str(s2)+" ORIGINAL")
        ff.close()    
        iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
        _tmp1,_tmp2,goon=lsc.util.display_image('snfit.fits',1, z01, z02, False, _xcen=.25, _ycen=.75, _xsize=.3, _ysize=.3, _erase='no')
        lsc.util.delete("tmptbl")
        tmptbl0=iraf.txdump(img+".sn.als","xcen,ycen",expr='yes',Stdout=1)
        ff=open('tmptbl','w')
        for i in tmptbl0:
            ff.write(i+'\n')
        ff.close()    
        lra = int((2*float(size)*float(fwhm0))*2)
        iraf.tvmark(1,"tmptbl",autol='no',mark="circle", number='yes',nyoffset=lra,radi=a2,txsize=2,inter='no')
        s1 = 1
        s2 = -1*int(fwhm0)
        lsc.util.delete("tmptbl")
        ff=open('tmptbl','w')
        ff.write(str(s1)+' '+str(s2)+" FITTED")
        ff.close()    
        iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
        _tmp1,_tmp2,goon=lsc.util.display_image('skyfit.fits',1, z11, z22, False, _xcen=.75, _ycen=.25, _xsize=.3, _ysize=.3, _erase='no')
        s1 = 1
        s2 = -1*int(fwhm0)
        lsc.util.delete("tmptbl")
        ff=open('tmptbl','w')
        ff.write(str(s1)+' '+str(s2)+" RESIDUAL")
        ff.close()    
        iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
    return apori1,apori2,apori3,apmag1,apmag2,apmag3,fitmag,truemag,magerr,centx,centy
Ejemplo n.º 51
0
def get_app_phot_target(image, plot=False, store=True, wcsin="logical", fwhm=2, box=4, ra=None, dec=None):
    '''
    coords: files: 
    wcsin: can be "world", "logic"
    '''
    # Load packages; splot is in the onedspec package, which is in noao. 
    # The special keyword _doprint=0 turns off displaying the tasks 
    # when loading a package. 
    
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        fxn()

    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.unlearn("apphot")
    
    impf = pf.open(image)
    wcs = pywcs.WCS(impf[0].header)
    #Check that actually the object is within this frame.
    if (ra is None or dec is None):
        if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")):
            ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC'))
        else:
            ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC'))
        pra, pdec = get_xy_coords(image, ra, dec)

    else:
        if(wcsin == "logical"):
            pra, pdec = ra, dec
        else:
        #Using new method to derive the X, Y pixel coordinates, as pywcs does not seem to be working well.
            pra, pdec = get_xy_coords(image, ra, dec)
            #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1)
            #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0]

    shape = impf[0].data.shape
    
    if (pra > 0)  and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]):
        pass
    else:
        print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!"
        print pra, pdec, shape
        return
    
        
    imdir = os.path.dirname(image)
    imname = os.path.basename(image)
    plotdir = os.path.join(imdir, "photometry")

    if not os.path.isdir(plotdir):
        os.makedirs(plotdir)
        
    out_name = os.path.join(plotdir, imname +  ".seq.mag")
    clean_name = os.path.join(plotdir, imname +  ".objapp.mag")
    
    
    fwhm_value = fwhm

    nsrc, fwhm_value, ellip = sextractor.get_image_pars(image)
    if np.isnan(fwhm_value):
	fwhm_value=99
    fitsutils.update_par(image, 'FWHM', fwhm_value)
        
    if (fitsutils.has_par(image, 'AIRMASS')):
        airmass_value = fitsutils.get_par(image, 'AIRMASS')
    else:
        airmass_value = 1.3
        
    exptime = fitsutils.get_par(image, 'EXPTIME')
    gain = fitsutils.get_par(image, 'GAIN')
    
    
    #print "FWHM", fwhm_value
    aperture_rad = math.ceil(float(fwhm_value)*3)      # Set aperture radius to three times the PSF radius
    sky_rad= math.ceil(aperture_rad*4)
    
    #print aperture_rad, sky_rad

    
    
    print "Saving coodinates for the object in pixels",pra,pdec
    coords = "/tmp/coords.dat"    
    np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f")


    if (plot):    
        zmin, zmax = zscale.zscale(impf[0].data)
           
        im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom")
        plt.scatter(pra, pdec, marker="o", s=100, facecolor="none")
        plt.savefig(os.path.join(plotdir, imname+".png"))
        plt.clf()
    
    
    if os.path.isfile(out_name): os.remove(out_name)
    if os.path.isfile(clean_name): os.remove(clean_name)


    iraf.noao.digiphot.apphot.qphot(image = image,\
    cbox = box ,\
    annulus = sky_rad ,\
    dannulus = 15. ,\
    aperture = str(aperture_rad),\
    coords = coords ,\
    output = out_name ,\
    plotfile = "" ,\
    zmag = 0. ,\
    exposure = "exptime" ,\
    airmass = "airmass" ,\
    filter = "filter" ,\
    obstime = "DATE" ,\
    epadu = gain ,\
    interactive = "no" ,\
    radplots = "yes" ,\
    verbose = "no" ,\
    graphics = "stdgraph" ,\
    display = "stdimage" ,\
    icommands = "" ,\
    wcsin = "logical",
    wcsout = "logical",
    gcommands = "") 


    #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name)
    iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name)
    

    ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"),  ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")])
    if (ma.size > 0):  
        if (ma.size==1):
            ma = np.array([ma])
        m = ma[~np.isnan(ma["fit_mag"])]
    else:
        print "Only one object found!"
        m = np.array([ma])
        

    insmag =  np.round(ma['fit_mag'][0] , 3)
    insmagerr = np.round(ma['fiterr'][0], 3)  
    if (fitsutils.has_par(image, "ZEROPT")):
        mag =  insmag + float(fitsutils.get_par(image, "ZEROPT"))
        magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2)  
	
    if np.isnan(mag):
        mag, magerr = 0, 0
        insmag, insmagerr = 0,0           

   
    fitsutils.update_par(image, "INSMAG", "%.3f"%insmag )
    fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr)
    fitsutils.update_par(image, "APPMAG", np.round(mag, 3) )
    fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3))

         
    if (plot):
        X = int(ma["X"][0])
        Y = int(ma["Y"][0])
        pra = int(pra)
        pdec = int(pdec)
        
        plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red")
        plt.colorbar(im)
        plt.savefig(os.path.join(plotdir, imname+".png"))
        plt.clf()
        
        zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T)
        im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50))
        c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position")
        c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad)
        c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid")
        c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad)
        plt.gca().add_artist(c1)
        plt.gca().add_artist(c11)
        plt.gca().add_artist(c2)
        plt.gca().add_artist(c22)
        plt.colorbar(im)
        
        myhandles = []
        markers = ["o", "o"]
        labels = ["Initial position", "Adjusted centroid"]
        cols = ["k", "orange"]
        for i in np.arange(len(markers)):
                myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i]))
        plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True)

        plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50])))
        plt.savefig(os.path.join(plotdir, imname+"_zoom.png"))
        plt.clf()
Ejemplo n.º 52
0
def manusn(img, imgpsf, dmag0, apori1, apori2, apori3, apmag1, apmag2, apmag3,
           fitmag, truemag, magerr, centx, centy, z11, z22, midpt, size, fwhm0,
           x1, y1, arterr):
    from pyraf import iraf
    import lsc
    import string, re, sys, os
    from numpy import zeros
    a2 = int(2. * fwhm0 + .5)
    fdmag = 10**(-0.4 * float(dmag0))
    lsc.util.delete(",_snfit.fit?,skyfit.fit?,_snfit.ar?")
    if truemag[0] == 'INDEF':
        print 'ACTUNG'
        magerr[0] = 0.0
        lsc.util.delete('test.fits')
        os.system('echo ' +
                  str(iraf.field(img + '.sn.coo', field='1,2', Stdout=1)[0]) +
                  ' ' + dmag0 + ' > dddd')
        iraf.addstar("snfit",
                     "dddd",
                     imgpsf,
                     "_snfit",
                     nstar=1,
                     veri='no',
                     simple='yes',
                     verb='yes')
        lsc.util.delete('dddd')
    else:
        iraf.imarith("snfit.fits", "*", fdmag, "_snfit.fits")
    iraf.imarith("original.fits", "-", "_snfit.fits", "skyfit.fits")
    _tmp1, _tmp2, goon = lsc.util.display_image('original.fits',
                                                1,
                                                z11,
                                                z22,
                                                False,
                                                _xcen=.25,
                                                _ycen=.25,
                                                _xsize=.3,
                                                _ysize=.3)
    print z11, z22, midpt
    z01 = float(z11) - float(midpt)
    z02 = float(z22) - float(midpt)
    s1 = 1
    s2 = -int(fwhm0)
    lsc.util.delete("tmptbl")
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='yes',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)
    _tmp1, _tmp2, goon = lsc.util.display_image('_snfit.fits',
                                                1,
                                                z01,
                                                z02,
                                                False,
                                                _xcen=.25,
                                                _ycen=.75,
                                                _xsize=.3,
                                                _ysize=.3,
                                                _erase='no')
    lsc.util.delete("tmptbl")
    tmptbl = iraf.txdump(img + ".sn.als", "xcen,ycen", expr='yes', Stdout=1)
    ff = open('tmptbl', 'w')
    for i in tmptbl:
        ff.write(i)
    ff.close()
    lra = int((2 * size * fwhm0) * 2)
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="circle",
                number='yes',
                nyoffset=lra,
                radi=a2,
                txsize=2,
                inter='no')
    s1 = 1
    s2 = -int(fwhm0)
    lsc.util.delete("tmptbl")
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " FITTED")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)
    _tmp1, _tmp2, goon = lsc.util.display_image('skyfit.fits',
                                                1,
                                                z11,
                                                z22,
                                                False,
                                                _xcen=.75,
                                                _ycen=.25,
                                                _xsize=.3,
                                                _ysize=.3,
                                                _erase='no')
    s1 = 1
    s2 = -int(fwhm0)
    lsc.util.delete("tmptbl")
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    newmag = list(zeros(len(truemag)))
    for i in range(0, len(truemag)):
        try:
            newmag[i] = float(truemag[i]) + float(dmag0)
        except:
            newmag[i] = float(dmag0)
            magerr[i] = 0.0
    print truemag
    print newmag

    print "***************************************************************************"
    print "#id  x_ori   y_ori     x     y    ap_ori ap_bgsub  fit_mag  err_art  err_fit"
    for i in range(len(fitmag)):
        print "SN", i, str(centx[i] + x1), str(centy[i] + y1), str(
            centx[i]), str(centy[i]), "  ", str(apori3[i]), "  ", str(
                apmag3[i]), "  ", str(newmag[i]), "  ", str(arterr), "  ", str(
                    magerr[i])
    print "**************************************************************************"

    return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy, newmag
Ejemplo n.º 53
0
            rxx = np.array([round])
            gxx = np.array([ground])

        find_sharp.append(sxx[0])
        find_round.append(rxx[0])
        find_ground.append(gxx[0])

        back, backrms = run_daophot(cnts_name[ff],
                                    coordfile=find_name[ff],
                                    outfile=phot_name[ff],
                                    calgorithm='gauss',
                                    backmethod=backmeth,
                                    cbox=10.)
        replace_filevalue(phot_name[ff], 'INDEF', -9999.0)
        iraf.txdump(phot_name[ff],
                    'xcenter,ycenter,flux, mag',
                    'yes',
                    Stdout=phot_name[ff] + '.trimmed')
        xc,yc,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,f25,f26, \
        m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16,m17,m18,m19,m20,m21,m22,m23,m24,m25,m26=np.loadtxt(phot_name[ff]+'.trimmed',unpack=True)

        # record various properties of observation (chip,amp,mjd, etc.)
        dd = pyfits.open(file)
        hdr0 = dd[0].header
        hdr1 = dd[1].header
        im = dd[1].data
        dd.close()
        amps = hdr0['CCDAMP']
        biaslev = hdr0['BIASLEV' + amps]
        filter = hdr0['FILTER']
        LTV1 = hdr1['LTV1']
        LTV2 = hdr1['LTV2']
Ejemplo n.º 54
0
def fitsn(img,
          imgpsf,
          coordlist,
          _recenter,
          fwhm0,
          original,
          sn,
          residual,
          _show,
          _interactive,
          z11='',
          z22='',
          midpt='',
          size=7,
          apco0=0,
          dmax=51000,
          dmin=-500):
    import lsc
    lsc.util.delete("apori")
    lsc.util.delete(img + ".sn.mag")
    #################################
    from pyraf import iraf
    import string
    iraf.imcoords(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    from iraf import digiphot
    from iraf import daophot
    from iraf import ptools
    a1 = int(fwhm0)
    a2 = int(2. * fwhm0 + .5)
    a3 = int(3. * fwhm0 + .5)
    a4 = int(4. * fwhm0 + .5)
    ap = str(a1) + "," + str(a2) + "," + str(a3)
    ########################################
    if _recenter: answ = 'yes'
    else: answ = 'no'
    #########################################
    hdr = lsc.util.readhdr(img + '.fits')
    _gain = lsc.util.readkey3(hdr, 'gain')
    _ron = lsc.util.readkey3(hdr, 'ron')
    _exptime = lsc.util.readkey3(hdr, 'exptime')
    _datamin = dmin
    _datamax = dmax
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    iraf.noao.digiphot.daophot.datapars.readnoi = _gain  #1.4   #_ron
    iraf.noao.digiphot.daophot.datapars.epadu = _ron  #  13      #_gain
    iraf.noao.digiphot.daophot.datapars.datamin = _datamin  # -100  #_datamin
    iraf.noao.digiphot.daophot.datapars.datamax = _datamax
    iraf.noao.daophot.fitskypars.annulus = a3
    iraf.noao.daophot.photpars.apertures = ap
    iraf.noao.digiphot.daophot.datapars.exposure = 'exptime'
    iraf.noao.digiphot.daophot.datapars.airmass = 'airmass'
    iraf.noao.digiphot.daophot.datapars.filter = 'filter2'
    iraf.noao.digiphot.daophot.daopars.psfrad = a4
    #  modify fitrad to 3 fwhm to see if works better
    iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0  #* 3
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.recenter = answ
    iraf.noao.digiphot.daophot.daopars.fitsky = 'yes'
    #    iraf.noao.digiphot.daophot.centerpars.cbox = 0
    iraf.noao.digiphot.daophot.centerpars.cbox = 4
    iraf.noao.digiphot.daophot.centerpars.calgori = 'gauss'

    #  fitskypars.salgorithm = "constant"
    #  fitskypars.skyvalue = 0
    print '\n### recentering: ' + str(answ)
    if _show:
        iraf.noao.digiphot.daophot.phot(original,
                                        coordlist,
                                        "apori",
                                        veri='no')
        iraf.noao.digiphot.daophot.phot(sn,
                                        coordlist,
                                        img + ".sn.mag",
                                        veri='no')
    else:
        iraf.noao.digiphot.daophot.phot(original,
                                        coordlist,
                                        "apori",
                                        veri='no',
                                        verb='no')
        iraf.noao.digiphot.daophot.phot(sn,
                                        coordlist,
                                        img + ".sn.mag",
                                        veri='no',
                                        verb='no')

    lsc.util.delete(img + ".sn.als")
    print sn, imgpsf, img
    iraf.allstar(sn,
                 img + ".sn.mag",
                 imgpsf,
                 img + ".sn.als",
                 "",
                 residual,
                 veri='no',
                 verb='no')
    lsc.util.delete("snfit.fits")
    iraf.imarith(sn + '.fits', "-", residual + '.fits', "snfit.fits")
    lsc.util.delete("skyfit.fits")
    iraf.imarith(original + '.fits', "-", "snfit.fits", "skyfit.fits")
    iraf.txsort(img + ".sn.als", "ID")
    tmptbl = iraf.txdump(img + ".sn.als",
                         "mag,merr,xcenter,ycenter",
                         expr='yes',
                         Stdout=1)
    magerr, fitmag, centx, centy = [], [], [], []
    for i in tmptbl:
        try:
            fitmag.append(float(string.split(i)[0]))  #-2.5*log10(_exptime))
        except:
            fitmag.append(string.split(i)[0])
        try:
            magerr.append(float(string.split(i)[1]))
        except:
            magerr.append(string.split(i)[1])
        centx.append(float(string.split(i)[2]))
        centy.append(float(string.split(i)[3]))
    tmptbl = iraf.txdump("apori", "mag", expr='yes', Stdout=1)
    apori1, apori2, apori3 = [], [], []
    for i in tmptbl:
        try:
            apori1.append(float(string.split(i)[0]))  #-2.5*log10(_exptime))
        except:
            apori1.append(string.split(i)[0])
        try:
            apori2.append(float(string.split(i)[1]))  #-2.5*log10(_exptime))
        except:
            apori2.append(string.split(i)[1])
        try:
            apori3.append(float(string.split(i)[2]))  #-2.5*log10(_exptime))
        except:
            apori3.append(string.split(i)[2])

    iraf.txsort(img + ".sn.mag", "YCENTER")
    tmptbl = iraf.txdump(img + ".sn.mag", "mag,magerr", expr='yes', Stdout=1)

    if _show:
        print "********************************************************************"
        print "ID <apmag on original>  <apmag on bgsubt> fitmag truemag err_fit"
        print "     ", a1, "       ", a2, "      ", a3, "        ", a1, "     ", a2, "     ", a3

    apmag1, apmag2, apmag3, truemag = [], [], [], []
    for i in range(len(tmptbl)):
        try:
            apmag1.append(float(string.split(
                tmptbl[i])[0]))  #-2.5*log10(_exptime))
        except:
            apmag1.append(9999)
        try:
            apmag2.append(float(string.split(
                tmptbl[i])[1]))  #-2.5*log10(_exptime))
        except:
            apmag2.append(9999)
        try:
            apmag3.append(float(string.split(
                tmptbl[i])[2]))  #-2.5*log10(_exptime))
        except:
            apmag3.append(9999)
        try:
            truemag.append(fitmag[i] + float(apco0))
        except:
            truemag.append('INDEF')
        if _show:
            print i, apori1[i], apori2[i], apori3[i], apmag1[i], apmag2[
                i], apmag3[i], fitmag[i], truemag[i], magerr[i]
    if _show:
        print "********************************************************************"

    if _show:
        print midpt, z11, z22
        _tmp1, _tmp2, goon = lsc.util.display_image(original + '.fits',
                                                    1,
                                                    z11,
                                                    z22,
                                                    False,
                                                    _xcen=.25,
                                                    _ycen=.25,
                                                    _xsize=.3,
                                                    _ysize=.3)
        z01 = float(z11) - float(midpt)
        z02 = float(z22) - float(midpt)
        s1 = 1
        s2 = -int(fwhm0)
        lsc.util.delete("tmptbl")
        ff = open('tmptbl', 'w')
        ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL")
        ff.close()
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="none",
                    inter='no',
                    label='yes',
                    txsize=2)
        _tmp1, _tmp2, goon = lsc.util.display_image('snfit.fits',
                                                    1,
                                                    z01,
                                                    z02,
                                                    False,
                                                    _xcen=.25,
                                                    _ycen=.75,
                                                    _xsize=.3,
                                                    _ysize=.3,
                                                    _erase='no')
        lsc.util.delete("tmptbl")
        tmptbl0 = iraf.txdump(img + ".sn.als",
                              "xcen,ycen",
                              expr='yes',
                              Stdout=1)
        ff = open('tmptbl', 'w')
        for i in tmptbl0:
            ff.write(i + '\n')
        ff.close()
        lra = int((2 * float(size) * float(fwhm0)) * 2)
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="circle",
                    number='yes',
                    nyoffset=lra,
                    radi=a2,
                    txsize=2,
                    inter='no')
        s1 = 1
        s2 = -1 * int(fwhm0)
        lsc.util.delete("tmptbl")
        ff = open('tmptbl', 'w')
        ff.write(str(s1) + ' ' + str(s2) + " FITTED")
        ff.close()
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="none",
                    inter='no',
                    label='yes',
                    txsize=2)
        _tmp1, _tmp2, goon = lsc.util.display_image('skyfit.fits',
                                                    1,
                                                    z11,
                                                    z22,
                                                    False,
                                                    _xcen=.75,
                                                    _ycen=.25,
                                                    _xsize=.3,
                                                    _ysize=.3,
                                                    _erase='no')
        s1 = 1
        s2 = -1 * int(fwhm0)
        lsc.util.delete("tmptbl")
        ff = open('tmptbl', 'w')
        ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL")
        ff.close()
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="none",
                    inter='no',
                    label='yes',
                    txsize=2)
    return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy
Ejemplo n.º 55
0
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2.5, plotdir=None, box=15, arcsecpix=0.394):
    '''
    coords: files: 
    wcsin: can be "world", "logic"
    '''
    # Load packages; splot is in the onedspec package, which is in noao. 
    # The special keyword _doprint=0 turns off displaying the tasks 
    # when loading a package. 
    
    from pyraf import iraf 

    if (not plot_only):
        iraf.noao(_doprint=0)
        iraf.digiphot(_doprint=0)
        iraf.apphot(_doprint=0)
        iraf.unlearn("apphot")

    imdir = os.path.dirname(image)
    imname = os.path.basename(image)
    
    if (plotdir is None):
        plotdir = os.path.join(imdir, "photometry")
    
    if not os.path.isdir(plotdir):
        os.makedirs(plotdir)
        
    out_name = os.path.join(plotdir, imname +  ".seq.mag")
    clean_name = os.path.join(plotdir, imname +  ".app.mag")

    print "Will create output files", out_name, clean_name

    # Read values from .ec file
    
    fwhm_value = fwhm/arcsecpix
    
    if (fitsutils.has_par(image, 'FWHM')):
        fwhm_value = fitsutils.get_par(image, 'FWHM')/arcsecpix
    elif (fwhm is None):
        fwhm_value=3.5/arcsecpix
    if (fitsutils.has_par(image, 'AIRMASS')):
        airmass_value = fitsutils.get_par(image, 'AIRMASS')
    else:
	airmass_value = 1.3
 
    exptime = fitsutils.get_par(image, 'EXPTIME')
    gain = fitsutils.get_par(image, 'GAIN')
    noise = fitsutils.get_par(image, 'RDNOISE')

    
    print "FWHM: %.1f pixels, %.1f arcsec"%(fwhm_value, fwhm_value*arcsecpix)
    aperture_rad = math.ceil(float(fwhm_value)*1.5)      # Set aperture radius to three times the PSF radius
    sky_rad= math.ceil(aperture_rad)*4
    

    if (not plot_only):

        iraf.noao.digiphot.apphot.qphot(image = image,\
        cbox = box ,\
        annulus = sky_rad ,\
        dannulus = 20. ,\
        aperture = str(aperture_rad),\
        coords = coords ,\
        output = out_name ,\
        plotfile = "" ,\
        zmag = 0. ,\
        exposure = "exptime",\
        airmass = "airmass" ,\
        filter = "filter" ,\
        obstime = "DATE" ,\
        #fwhm = fwhm_value,\
        epadu = gain ,\
        interactive = "no" ,\
        radplots = "yes" ,\
        verbose = "no" ,\
        graphics = "stdgraph" ,\
        display = "stdimage" ,\
        icommands = "" ,\
        wcsin = wcsin,
        wcsout = "logical",
        gcommands = "") 
        
         
        #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name)
        iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name)
        
    
    ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"),  ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), ("stdev","<f4"),\
        ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")])
    if (ma.size > 0):    
        m = ma[~np.isnan(ma["fit_mag"])]
    else:
        print "Only one object found!"
        m = np.array([ma])
        
    hdulist = pf.open(image)
    prihdr = hdulist[0].header
    img = hdulist[0].data * 1.
    nx, ny = img.shape

    
    
    dimX = int(np.floor(np.sqrt(len(m))))
    dimY = int(np.ceil(len(m)*1./dimX))
    outerrad = sky_rad+10
    cutrad = outerrad + 15
    
    print "Cutrad %.1f"%cutrad

    plt.suptitle("FWHM=%.2f arcsec. %d stars"%(fwhm_value*arcsecpix, len(m)))
    for i in np.arange(dimX):
        for j in np.arange(dimY):
            if ( i*dimY + j < len(m)):
                k = i*dimY + j
		#print dimX, dimY, i, j, k
                ax = plt.subplot2grid((dimX,dimY),(i, j))
                y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad
                y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2)
                y1 = np.maximum(y1, 0); y2=np.maximum(y2, 0); x1=np.maximum(x1, 0); x2 = np.maximum(x2, 0)
                try:
                    zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25)
                except ValueError:
		    print y1, y2, x1, x2 
		    print img[x1:x2,y1:y2]
                    sh= img[x1:x2,y1:y2].shape
                    if sh[0]>0 and sh[1]>0:
                        zmin = np.nanmin(img[x1:x2,y1:y2])
                        zmax = np.nanmax(img[x1:x2,y1:y2])
                ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax)
                c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=aperture_rad)
                c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad)
                c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+20)
                plt.gca().add_artist(c1)
                plt.gca().add_artist(c2)
                plt.gca().add_artist(c3)
                ax.set_xticks([])
                ax.set_yticks([])
        
                plt.text(+5, +5, "%d"%m[k]["id"])
                plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b")
		
    plt.tight_layout()
    plt.savefig(os.path.join(plotdir, imname + "plot.png"))
    plt.clf()
Ejemplo n.º 56
0
iraf.noao(_doprint=0)
iraf.digiphot(_doprint=0)              #loads some needed iraf libraries
iraf.daophot(_doprint=0)
iraf.ptools(_doprint=0)

#starting photometry

#iraf.phot(path+"snb.fits",path+"2MASS.GC.cat",path+'N'+gal+'B_GC.mag',skyfile="",datapars="",scale=1.,fwhmpsf=2.92,emissio='yes',sigma=0.0,datamin=-100, datamax=1e5,noise="poisson",centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1.,cmaxiter=0,maxshift=0,clean='no',rclean=0.,rclip=0.,kclean=0.,mkcenter='no',fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3.,shireject=3.,khist=3.,binsize=0.1,smooth='no',rgrow=0.,mksky='no',photpars="",weighting="constant",apertures=1.8,zmag=20.,mkapert='no',interactive='no',radplots='no',verify='no',update='no',verbose='no', graphics="stdgraph",display="stdimage",icommands="",gcommands="")
#iraf.phot(path+"tnb.fits",path+"2MASS.GC.cat",path+'N'+gal+'T_GC.mag',skyfile="",datapars="",scale=1.,fwhmpsf=2.92,emissio='yes',sigma=0.0,datamin=-100, datamax=1e5,noise="poisson",centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1.,cmaxiter=0,maxshift=0,clean='no',rclean=0.,rclip=0.,kclean=0.,mkcenter='no',fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3.,shireject=3.,khist=3.,binsize=0.1,smooth='no',rgrow=0.,mksky='no',photpars="",weighting="constant",apertures=1.8,zmag=20.,mkapert='no',interactive='no',radplots='no',verify='no',update='no',verbose='no', graphics="stdgraph",display="stdimage",icommands="",gcommands="")
iraf.phot(path+"f.fits",path+"2MASS.GC.cat",path+'N'+gal+'F_GC.mag',skyfile="",datapars="",scale=1.,fwhmpsf=2.92,emissio='yes',sigma=0.0,datamin=-100, datamax=1e5,noise="poisson",centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1.,cmaxiter=0,maxshift=0,clean='no',rclean=0.,rclip=0.,kclean=0.,mkcenter='no',fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3.,shireject=3.,khist=3.,binsize=0.1,smooth='no',rgrow=0.,mksky='no',photpars="",weighting="constant",apertures=1.8,zmag=20.,mkapert='no',interactive='no',radplots='no',verify='no',update='no',verbose='no', graphics="stdgraph",display="stdimage",icommands="",gcommands="")
 
#dump outputs from iraf in readable format

#iraf.txdump(path+'N'+gal+'B_GC.mag',"XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID",'yes', headers='no',paramet='no', Stdout=path+'N'+gal+'B_GC.txt')
#iraf.txdump(path+'N'+gal+'T_GC.mag',"XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID",'yes', headers='no',paramet='no', Stdout=path+'N'+gal+'T_GC.txt')
iraf.txdump(path+'N'+gal+'F_GC.mag',"XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID",'yes', headers='no',paramet='no', Stdout=path+'N'+gal+'F_GC.txt')

#run ffinder to create the inputs to prob_bulge_bd.f

#ffinder(path+'N'+gal+'B_GC.txt', path+'N'+gal+'T_GC.txt', path+'N'+gal+'F_GC.txt', path+'N'+gal+'_f.GC.dat')
ffinder(path+'N'+gal+'F_GC.txt', path+'N'+gal+'_f.GC.dat')

#plot the fmap if requested:

plot_histf(path+'N'+gal+'_f.GC.dat')

op = raw_input("Plot the fmap? Y/N \n")

if op == "y" or "yes":
    plot_fmap(gal_x, gal_y)
    plot_vmap(gal_x, gal_y)
Ejemplo n.º 57
0
import numpy as np
import astropy.io.fits as pyfits
from pyraf import iraf
from iraf import noao, digiphot, apphot, phot, txdump
import matplotlib
from matplotlib import pyplot as plt

iraf.noao.digiphot.apphot.phot.interac='no'
iraf.noao.digiphot.apphot.phot.centerp.calgori='none'
iraf.noao.digiphot.apphot.phot.fitskypars.salgori='constant'
iraf.noao.digiphot.apphot.phot.cache='yes'
iraf.noao.digiphot.apphot.phot.salgori='constant'

for band, aperture in zip(['f105w','f125w','f140w','f160w','f435w','f606w','f814w'], ['3.2','3.5','3.9','4.1','3.3','3.3','3.3']):
    iraf.noao.digiphot.apphot.phot.photpar.apertur=aperture
    iraf.phot(image='SEG_' + band + '_sex_hff_abells1063_par_CHECK.fits', interactive='no',calgorithm='none', salgori='constant',coords='box_coords_' + band + '.coo', output='PHOT_SEG_hff_abells1063_par_' + band + '.txt',verify='no')
    iraf.txdump(textfile='PHOT_SEG_hff_abells1063_par_' + band + '.txt', fields='XCEN, YCEN, SUM', expr='yes', Stdout='PHOT_SEG_hff_abells1063_par_' + band + '_OUTFILE.txt')
Ejemplo n.º 58
0
	'''Generate source catalogs'''
	# catalogs for src detection & photometry within same image
        for file, ff in zip(file_list,xrange(len(file_list))):
		mk_counts_image(file, outfile=cnts_name[ff])					
		run_daofind(cnts_name[ff], outfile=find_name[ff], dthreshold=4.0)			
		run_daophot(cnts_name[ff], coordfile=find_name[ff], outfile=phot_name[ff])
		replace_filevalue(phot_name[ff], 'INDEF',-9999.0)
		ac05[ff] = calc_apcorr(phot_name[ff])

	# color catalogs--src detection in one image, photometry in other (***KLUGE*** ASSUMES ONLY 2 INPUT IMAGES)
	# f606w color cats
	run_daophot(cnts_name[0], outfile=col_name[0], coordfile=find_name[1])
        replace_filevalue(col_name[0], 'INDEF', -9999.0)
	file_query = os.access(tcol_name[0], os.R_OK)
	if file_query == True: os.remove(tcol_name[0])      # remove previous trimmed files
	iraf.txdump(col_name[0],'xcenter,ycenter,mag', 'yes', Stdout=tcol_name[0])

	#f814w color cats
	run_daophot(cnts_name[1], outfile=col_name[1], coordfile=find_name[0])
        replace_filevalue(col_name[1], 'INDEF', -9999.0)
	file_query = os.access(tcol_name[1], os.R_OK)
	if file_query == True: os.remove(tcol_name[1])      # remove previous trimmed files
	iraf.txdump(col_name[1],'xcenter,ycenter,mag', 'yes', Stdout=tcol_name[1])


        '''make CMD'''
	# extract color mags (apply aperture corrections)
        xca, yca, mag1a,mag2a = np.loadtxt(phot_name[0]+'.trimmed', unpack=True)
	xcb, ycb, mag1b,mag2b = np.loadtxt(tcol_name[1], unpack=True)
	tmaga = mag1a - ac05[0] - AC05
	tmagb = mag1b - ac05[1] - AC05