Exemple #1
0
def GeoTran():
    filters = ['J', '1113', '1184']
    for filt in filters:
        s = 'mfm*' + filt + '*.fits'
        files = glob.glob(s)
        flist = 'flist' + str(filt)
        glist = 'glist' + str(filt)
        fout = open(flist, 'w')
        gout = open(glist, 'w')
        for file in files:
            ffile = str(file)  #+'\n'
            gfile = 'g' + str(file)  #+'\n'
            fout.write(ffile + '\n')
            gout.write(gfile + '\n')
            iraf.geotran(input=ffile,
                         output=gfile,
                         database='PiscesBok',
                         transforms=filt,
                         fluxconserve='no')
            iraf.rotate(input=gfile, output=gfile, rotation=90)
            gfilein = 'g' + str(file) + '[-*,*]'
            gfileout = 'g' + str(file)
            iraf.imcopy(gfilein, gfileout)

        infiles = '@' + str(flist)
        outfiles = '@' + str(glist)
        #infiles=str(flist)
        #outfiles=str(glist)
        fout.close()
        gout.close()
def imtrans(impath, refimpath, dbfile, geomap_infile, lims):

    '''Run geotran on an image

    INPUT:

    impath : the full path to the image to be transformed

    refimpath : the full path to the reference image

    dbfile: the geomap database file
    
    geomap_infile: the name of the input coordinate file for the
    geomap transformation, which is used as the record identifier for
    the transfor

    lims: a dictionary that defines the minimum and maximum 

    '''

    #find the x and y limits of the reference image
    refim = fits.open(refimpath)
    xmax = refim[0].header['NAXIS1']
    ymax = refim[0].header['NAXIS2']
    xmin = 1
    ymin = 1
    refim.close()

    outpath = impath.replace('.fits[1]','.trans.fits')

    iraf.geotran(impath, outpath, dbfile, geomap_infile, xmin = xmin, xmax = xmax, \
                 ymin = ymin, ymax = ymax, xscale = 1.0, yscale = 1.0)
Exemple #3
0
 def geotran(self):
     # Run geotran on self.input_image... is this what I want to do?
     output_image = os.path.splitext(self.input_image)[0] + '_geotran.fits'
     if os.path.exists(output_image):
         os.remove(output_image)
     h = fits.getheader(self.input_image)
     iraf.geotran(self.input_image, output_image, self.database,
                  self.coofile, 
                  geometry='geometric', 
                  xscale=self.relscale, yscale=self.relscale,
                  interpolant='spline3')
def AlignImage(TargetImg,Template,AlignedImageName,fitgeometry="general"):
    """Align Template Image to TargetImage to create output image AlignedImageName.
    fitgeometry can be set to 'rotate' when the Template is also TIRSPEC data
    Otherwise if Template is 2MASS or other instrument set it as 'general'  """
    MatchingXYcoordsFile = os.path.splitext(AlignedImageName)[0]+'.XYXY'
    if not os.path.isfile(MatchingXYcoordsFile) :
        CreateMatchingXYCoords(TargetImg,Template,MatchingXYcoordsFile)
    else:
        print('Using Old {0} file'.format(MatchingXYcoordsFile))

    XYtranformdb = os.path.splitext(AlignedImageName)[0]+'rtran.db'
    iraf.geomap(input=MatchingXYcoordsFile, database=XYtranformdb, xmin=1, xmax=1024, ymin=1, ymax=1024, interactive=0,fitgeometry=fitgeometry)
    #Now geotran the Template image to match the TargetImg
    iraf.geotran(input=Template,output=AlignedImageName,database=XYtranformdb,transforms=MatchingXYcoordsFile)
    #return the transfgorm .db filename just incase it is needed for the user
    return XYtranformdb
Exemple #5
0
def distcor(inlist, database, inpref='', outpref='d'):

    # open input list and check if it exists
    inimg_arr = check_input(inlist, inpref)
    if isinstance(inimg_arr, int):
        return 1

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

    # check database
    try:
        fd = open(database)
    except IOError:
        print >> sys.stderr, 'cannot open database file (%s)' % database
        f.close()
        return 1

    # read geomap coordinate file
    read = 0
    for line in fd:
        param = line[:-1].split()
        if param[0] == 'begin':
            gmp = param[1]
            read += 1
    fd.close()

    if read == 0:
        print >> sys.stderr, 'cannot read coordinate file in geomap database (%s)' % database
        f.close
        return 1

    # correct distortion
    for i in range(len(inimg_arr)):
        # correct distortion
        iraf.unlearn('geotran')
        iraf.geotran(inimg_arr[i], outimg_arr[i], database, gmp)

    return 0
Exemple #6
0
def mygeotran(input,
              output,
              database,
              transforms,
              geometry='geometric',
              interpolant="linear"):

    # First, we get the shifts from the database
    f = open(database, 'r')
    for line in f.readlines():
        if re.search('xshift', line):
            xshift = float(string.split(line)[1])
        elif re.search('yshift', line):
            yshift = float(string.split(line)[1])
    f.close()
    f = open('offsets.dat', 'w')
    print >> f, 0, 0
    print >> f, xshift + 1, yshift + 1
    f.close()

    # Now we make a blank holder for the mapped image
    nuke('placeholder.fits')
    iraf.imcombine(input + ',' + input,
                   'placeholder.fits',
                   offsets='offsets.dat')
    iraf.imarith('placeholder.fits', '*', 0.0, 'placeholder.fits')

    xsize, ysize = get_image_size(input)
    # next, copy the input into this place holder
    iraf.imcopy(input + '[*,*]',
                'placeholder.fits[1:%d,1:%d]' % (xsize, ysize))

    # now run geogran on this padded image
    iraf.geotran(input='placeholder.fits',
                 output=output,
                 database=database,
                 transforms=transforms,
                 geometry=geometry,
                 interpolant=interpolant)
def imshiftcomb(inlist,
                outimg,
                fitgeom='shift',
                inpref='',
                objmask='none',
                combine='average',
                reject='none',
                fscale=False,
                fbase=100,
                fhead='F1',
                second=False,
                first_pref='sdfr',
                second_pref='sdf2r',
                indep=False,
                sigmap='none',
                expmap='none',
                whtmap='none',
                gain=5.6,
                ffpref=''):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    # independent run flag
    if indep:
        second = True

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

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

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

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

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

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

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

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

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

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

    # save the original fit geometry
    fitgeom_org = fitgeom

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

    for i in range(len(inimg_arr)):

        # restore the original fit geometry
        fitgeom = fitgeom_org

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

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

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

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

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

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

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

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

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

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

        else:
            flux_scale = 1.0
            weight = expt

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

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

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

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

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

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

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

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

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

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

        if second:

            if indep:

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

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

                if rot > 180.0:
                    rot = rot - 360.0

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

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

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

        else:

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

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

            if rot > 180.0:
                rot = rot - 360.0

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

        im.close()

    # remove all temporary files
    remove_temp_all(tmp_prefix)

    return 0
Exemple #8
0
def rectify_image(image,
                  reference,
                  output,
                  refout=None,
                  fitgeometry='general',
                  function='polynomial',
                  xxorder=3,
                  yyorder=3,
                  xyorder=3,
                  yxorder=3,
                  clobber=1,
                  objects=None,
                  interactive=1,
                  maxiter=5,
                  reject=3,
                  xrange=None,
                  yrange=None,
                  thresh=2.0,
                  scale=0.125,
                  fwhm=4,
                  image_sigma=None,
                  reference_sigma=None,
                  debug=0,
                  aoffset=0):
    '''This task will take an image and a reference image and apply geomap/
   geotran to aligned them.  image is registered and output to output.
   You can specify the fit geometry (shift, xyscale, rotate, rscale,
   rxyscale, general).  You can also choose the geenral function
   (legendre, chebyshev, polynomial).  You can specfy the order of all
   the terms (xxorder, yyorder, xyorder, yxorder).  If you choose
   clobber=1 (the default), new images overwrite older ones.  Lastly, if
   you want to use a pre-existing list of objects, specify them as the
   objects keyword.'''

    # First, if we already have objects to use, save time by not finding them
    #  again!

    if objects is None:
        sex(image,
            output='image.cat',
            thresh=thresh,
            scale=scale,
            fwhm=fwhm,
            sigma_map=image_sigma)
        sex(reference,
            output='reference.cat',
            thresh=thresh,
            scale=scale,
            fwhm=fwhm,
            sigma_map=reference_sigma)

        im_masks1 = get_mask(image)
        im_masks2 = get_mask(reference)
        if aoffset == 0:
            # Check the ROTANG header field to see if we need to give a rotation
            # offset to the object matcher.
            rotang1 = get_header(reference, "ROTANG", float)
            rotang2 = get_header(image, "ROTANG", float)
            if rotang1 is not None and rotang2 is not None:
                if abs(rotang1 - rotang2) > 0.1:
                    aoffset = rotang1 - rotang2

        # Run the matchum routine to get corresponding objects:
        (x2, y2, x1, y1, o1, o2) = matchum('image.cat',
                                           'reference.cat',
                                           im_masks1=im_masks1,
                                           im_masks2=im_masks2,
                                           xrange=xrange,
                                           yrange=yrange,
                                           debug=debug,
                                           aoffset=aoffset)

        f = open('geomap.objects', 'w')
        for i in range(len(x1)):
            f.write('%.2f %.2f %.2f %.2f\n' % (x1[i], y1[i], x2[i], y2[i]))
        f.close()
        objects = "geomap.objects"
    else:
        data = pygplot.columns(objects)
        x1 = data[0]
        y1 = data[1]
        x2 = data[2]
        y2 = data[3]

    xshift = int(stats.bwt(x2 - x1)[0])
    yshift = int(stats.bwt(y2 - y1)[0])

    # We now have some selected objects to use with geomap.  Let's figure out
    #  the area of validity for the data using ths shifts computed above.
    (r_xmax, r_ymax) = get_image_size(reference)
    if xshift < 0:
        xmin = -xshift
        xmax = r_xmax
    else:
        xmin = 1
        xmax = r_xmax - xshift
    if yshift < 0:
        ymin = -yshift
        ymax = r_ymax
    else:
        ymin = 1
        ymax = r_ymax - yshift
    #xmin = min(x1);  xmax = max(x1)
    #ymin = min(y1);  ymax = max(y1)
    print xshift, yshift, xmin, xmax, ymin, ymax

    iraf.images()
    if os.path.isfile('geomap.db'): os.unlink('geomap.db')

    # We may not have enough points to do a general fit, so we do them in order of
    #   complexity.  Hopefully, we have at least ONE point (for the shift)
    fitgeometries = [fitgeometry, 'rxyscale', 'rotate', 'shift']

    success = 0
    for fitgeo in fitgeometries:
        print "using fitgeometry = ", fitgeo
        res = iraf.geomap(input=objects,
                          database='geomap.db',
                          xmin=1,
                          ymin=1,
                          xmax=r_xmax,
                          ymax=r_ymax,
                          fitgeometry=fitgeo,
                          function=function,
                          xxorder=xxorder,
                          yyorder=yyorder,
                          xyorder=xyorder,
                          yxorder=yxorder,
                          results='',
                          transforms=objects,
                          interactive=interactive,
                          maxiter=maxiter,
                          reject=reject,
                          Stdout=1)
        # assume all went well
        success = 1
        for line in res:
            print line
            if re.search('Too few points', line):
                success = 0
        if success:
            break

    if clobber and os.path.isfile(output): iraf.imdel(output)
    iraf.geotran(input=image,
                 output='temp.rectify',
                 database='geomap.db',
                 transforms=objects,
                 geometry='geometric',
                 interpolant="linear")
    section = "[%d:%d,%d:%d]" % (xmin, xmax, ymin, ymax)
    print 'Valid section of data:', section
    iraf.imcopy(input='temp.rectify', output=output)
    iraf.imdel('temp.rectify')
    return (xmin, xmax, ymin, ymax)
Exemple #9
0
def combine_images(images,
                   output,
                   reference=None,
                   method='average',
                   zscale=1,
                   fitgeometry='general',
                   function='polynomial',
                   xxorder=3,
                   yyorder=3,
                   xyorder=3,
                   yxorder=3,
                   objects=None,
                   trim=0,
                   interactive=1,
                   maxiter=5,
                   reject=3,
                   sigma=None,
                   xrange=None,
                   yrange=None,
                   rectify=1,
                   thresh=2.0,
                   scale=0.125,
                   fwhm=4.0,
                   debug=0,
                   aoffset=0,
                   creject=None):
    '''This Routine will take a list of images and use rectify_image to rectify
   each with respect to referece.  It will then use imcombine to combine the
   images into one and output to "output".  Default method is 'average', but
   you can use any available to imcombine.  The rest of the arguments are sent
   to rectify_images.  If zscale=1, all the images are scaled to a common
   zmag (30).  Also, if a file with _sigma.fits is found for all input files,
   we combine them too.  If you specify a sigma, that file will hold the
   standard deviations from the files.'''
    names = get_files(images)
    if reference is None:
        reference = names[0]
        names = names[1:]

    pclip = -0.5
    lsigma = 3.
    hsigma = 3.
    nkeep = -2
    if creject:
        creject = "pclip"
    else:
        creject = "none"

    # Check to see if we have _sigma files for each input file
    if os.path.isfile(reference.replace('.fits', '_sigma.fits')):
        do_sigmas = 1
    else:
        do_sigmas = 0

    sigmas = []
    for name in names:
        if os.path.isfile(name.replace('.fits', '_sigma.fits')):
            sigmas.append(name.replace('.fits', '_sigma.fits'))
        else:
            do_sigmas = 0
            break

    # Put the reference image first, to ensure that all positional header keywords are
    #  valid.
    if do_sigmas:
        ref_sig = reference.replace('.fits', '_sigma.fits')
    update = 0
    if zscale:
        zmag = get_header(reference, "ZMAG", float)
        if zmag is not None and zmag != 26:
            update = 1
            zmagfac = num.power(10, (zmag - 30) / 2.5)
            if zmagfac != 1:
                print 'zmagfac = ', zmagfac
                iraf.imarith(reference, '/', zmagfac, reference)
                iraf.hedit(reference, "ZMAG", 30.0, add=1, verify=0)
                if do_sigmas:
                    iraf.imarith(ref_sig, '/', zmagfac, ref_sig)
                    iraf.hedit(ref_sig, "ZMAG", 30.0, add=1, verify=0)

    print "Using %s as reference" % (reference)

    if rectify:
        nuke(reference.replace('.fits', '_rec.fits'))
        iraf.imcopy(reference, reference.replace('.fits', '_rec.fits'))
        if do_sigmas:
            nuke(reference.replace('.fits', '_rec_sigma.fits'))
            iraf.imcopy(reference.replace('.fits', '_sigma.fits'),
                        reference.replace('.fits', '_rec_sigma.fits'))
        temps = [reference.replace('.fits', '_rec.fits')]
        if do_sigmas:
            sig_temps = [reference.replace('.fits', '_rec_sigma.fits')]
    else:
        temps = [reference]
        if do_sigmas: sig_temps = [ref_sig]

    i = 0
    for name in names:
        print name
        if rectify:
            temp = name.replace('.fits', '_rec.fits')
            nuke(temp)
            if do_sigmas:
                (x0, x1, y0, y1) = rectify_image(name,
                                                 reference,
                                                 output=temp,
                                                 fitgeometry=fitgeometry,
                                                 function=function,
                                                 xxorder=xxorder,
                                                 yyorder=yyorder,
                                                 xyorder=xyorder,
                                                 yxorder=yxorder,
                                                 objects=objects,
                                                 interactive=interactive,
                                                 maxiter=maxiter,
                                                 reject=reject,
                                                 xrange=xrange,
                                                 yrange=yrange,
                                                 thresh=thresh,
                                                 scale=scale,
                                                 fwhm=fwhm,
                                                 image_sigma=sigmas[i],
                                                 reference_sigma=ref_sig,
                                                 debug=debug,
                                                 aoffset=aoffset)
            else:
                (x0, x1, y0, y1) = rectify_image(name,
                                                 reference,
                                                 output=temp,
                                                 fitgeometry=fitgeometry,
                                                 function=function,
                                                 xxorder=xxorder,
                                                 yyorder=yyorder,
                                                 xyorder=xyorder,
                                                 yxorder=yxorder,
                                                 objects=objects,
                                                 interactive=interactive,
                                                 maxiter=maxiter,
                                                 reject=reject,
                                                 xrange=xrange,
                                                 yrange=yrange,
                                                 thresh=thresh,
                                                 scale=scale,
                                                 fwhm=fwhm,
                                                 debug=debug,
                                                 aoffset=aoffset)
        else:
            temp = name
        bpm = get_header(name, 'BPM', str)
        if bpm and rectify:
            bpm_fits = bpm.replace('.pl', '.fits')
            temp_bpm = bpm.replace('.pl', '_rec.fits')
            temp_bpm_pl = bpm.replace('.pl', '_rec.pl')
            nuke(bpm_fits)
            nuke(temp_bpm)
            nuke(temp_bpm_pl)
            iraf.imcopy(bpm, bpm_fits)
            iraf.imarith(bpm_fits, "*", 10.0, bpm_fits)
            if objects is None: sigmaobjects = 'geomap.objects'
            iraf.geotran(input=bpm_fits,
                         output=temp_bpm,
                         database='geomap.db',
                         transforms=sigmaobjects,
                         geometry='geometric',
                         interpolant="linear")
            iraf.imreplace(temp_bpm, lower=0.02, upper="INDEF", value=1.)
            iraf.imcopy(temp_bpm, temp_bpm_pl)
            iraf.hedit(temp, 'BPM', temp_bpm_pl, update=1, verify=0)
        if do_sigmas:
            if rectify:
                temp_sig = sigmas[i].replace('_sigma.fits', '_rec_sigma.fits')
                nuke(temp_sig)
                if objects is None:
                    sigmaobjects = 'geomap.objects'
                else:
                    sigmaobjects = objects

                iraf.geotran(input=sigmas[i],
                             output=temp_sig,
                             database='geomap.db',
                             transforms=sigmaobjects,
                             geometry='geometric',
                             interpolant="linear")
            else:
                temp_sig = sigmas[i]
        if rectify:
            if i == 0:
                xmin = x0
                ymin = y0
                xmax = x1
                ymax = y1
            else:
                xmin = max(xmin, x0)
                ymin = max(ymin, y0)
                xmax = min(xmax, x1)
                ymax = min(ymax, y1)
        if zscale:
            zmag = get_header(name, "ZMAG", float)
            if zmag is not None and zmag != 26:
                zmagfac = num.power(10, (zmag - 30) / 2.5)
                if zmagfac != 1:
                    print 'zmagfac = ', zmagfac
                    iraf.imarith(temp, '/', zmagfac, temp)
                    iraf.hedit(temp, 'ZMAG', 30.0, add=1, verify=0)
                    if do_sigmas:
                        iraf.imarith(temp_sig, '/', zmagfac, temp_sig)
                        iraf.hedit(temp_sig, 'ZMAG', 30.0, add=1, verify=0)

        temps.append(temp)
        if do_sigmas: sig_temps.append(temp_sig)
        i = i + 1
    Nimages = len(temps)
    temps = string.join(temps, ',')
    print "Combining ", temps
    nuke(output)
    if sigma is not None:
        iraf.imcombine(temps,
                       output,
                       combine=method,
                       sigma=sigma,
                       masktyp='badvalue',
                       maskval=1.,
                       reject=creject,
                       lsigma=lsigma,
                       hsigma=hsigma,
                       nkeep=nkeep)
    else:
        iraf.imcombine(temps,
                       output,
                       combine=method,
                       masktyp='badvalue',
                       maskval=1.,
                       reject=creject,
                       lsigma=lsigma,
                       hsigma=hsigma,
                       nkeep=nkeep)

    if do_sigmas:
        Ns = []
        for this in sig_temps:
            N = this.replace('.fits', '_N.fits')
            nuke(N)
            iraf.imcopy(this, N)
            iraf.imreplace(N, lower=0.01, upper="INDEF", value=1)
            Ns.append(N)
        sig_temps = string.join(sig_temps, ',')
        Ns = string.join(Ns, ',')
        iraf.imfunction(sig_temps, sig_temps, 'square')
        file = output.replace('.fits', '_sigma.fits')
        nuke(file)
        iraf.imcombine(sig_temps, file, combine='sum')
        if method == 'average':
            nuke("N.fits")
            iraf.imcombine(Ns, 'N.fits', combine='sum')
            iraf.imfunction('N.fits', 'N.fits', 'square')
            iraf.imarith(file, '/', 'N.fits', file)
        iraf.imfunction(file, file, 'sqrt')
        iraf.imfunction(sig_temps, sig_temps, 'sqrt')
    if update:
        iraf.hedit(output, "ZMAG", 30.0, verify=0, add=1)
    #nuke('temp?.fits')

    # Now, let's clean up the gain and rdnoise headers, as they don't seem to
    #  be handled in the PANIC pipeline.  NOTE:  we really need a noise map, but
    #  for now, we'll deal with just global values.
    gain = get_header(output, "gain", float)
    rdnoise = get_header(output, "rdnoise", float)
    if gain is None:
        # have to compute it from scratch
        egain = get_header(output, "egain", float)
        enoise = get_header(output, "enoise", float)
        nloop = get_header(output, "nloops", int)
        i = 1
        key = ""
        # This seems to be the only safe way to compute this.  Can't trust
        #   NDITHERS
        while key is not None:
            key = get_header(output, "imcmb%03d" % (i), str)
            i = i + 1
        ndither = i - 1
        print "N, ndither, nloop, egain, enoise = ", Nimages, ndither, nloop, egain, enoise
        gain = egain * Nimages * nloop * ndither
        rdnoise = enoise * num.sqrt(Nimages * nloop * ndither)
    else:
        gain = gain * Nimages
        rdnoise = rdnoise * num.sqrt(Nimages)
    iraf.hedit(output, "gain", gain, verify=0, add=1)
    iraf.hedit(output, "rdnoise", rdnoise, verify=0, add=1)

    # If requested, trim to the common regions of the combined images
    if rectify and trim and Nimages > 1:
        iraf.imcopy(output + "[%d:%d,%d:%d]" % (xmin, xmax, ymin, ymax),
                    output.replace('.fits', '_trim.fits'))
Exemple #10
0
def mscimage(input=None, output=None, format='image', pixmask=no,verbose=')_.verbose',wcssource='image',reference='',ra=INDEF,dec=INDEF,scale=INDEF,rotation=INDEF,blank=0.0,interpolant='poly5',minterpolant='linear',boundary='reflect',constant=0.0,fluxconserve=no,ntrim=8,nxblock=INDEF,nyblock=INDEF,interactive=no,nx=10,ny=20,fitgeometry='general',xxorder=4,xyorder=4,xxterms='half',yxorder=4,yyorder=4,yxterms='half',fd_in='',fd_ext='',fd_coord='',mode='ql',DOLLARnargs=0,taskObj=None):

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

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

    filexy1 = prefixim1 + '-xy.cat'
    filexy2 = prefixim2 + '-xy.cat'
    print "aligning images for ", fieldid
    if runsex > 0:

        runsextractor(im1, auto)
        infile = open('test.cat', 'r')  #keep only sources w/in useable field

        outfile = open(filexy1, 'w')
        for line in infile:  #keep only targets w/in 500 pixels of center
            if line.find('#') > -1:
                continue
            t = line.split()
            out = '%8.4f %8.4f \n' % (float(t[10]), float(t[11]))
            outfile.write(out)
        infile.close()
        outfile.close()

        print "running sextractor on ", im2
        runsextractor(im2, auto)

        infile = open('test.cat', 'r')  #keep only sources w/in useable field

        outfile = open(filexy2, 'w')
        for line in infile:  #keep only targets w/in 500 pixels of center
            if line.find('#') > -1:
                continue
            t = line.split()
            out = '%8.4f %8.4f \n' % (float(t[10]), float(t[11]))
            outfile.write(out)
        infile.close()
        outfile.close()

    match = fieldid + '-match'
    dbase = fieldid + '-match-geomap'
    iraf.display(im1, 1, fill='yes')
    iraf.display(im2, 2, fill='yes')
    reffile = fieldid + 'refpoints'  #make these individually.  First line w/3points in gmos image (x1 y1 x2 y2 x3 y3). 2nd line w/same points in ediscs I-band
    flag = raw_input("Should we create a refpoints file (0=no, 1=yes)?\n")
    flag = int(flag)
    if flag > 0.1:
        s = 'cp cl1018refpoints ' + reffile
        os.system(s)
        print "open ", reffile, " in emacs, delete 2 lines"
        print "Enter x1 y1 x2 y2  x3 y3 for ref image (gmos)"
        print "Enter x1 y1 x2 y2  x3 y3 for 2nd image (vlt)"
        iraf.imexam()

    iraf.xyxymatch(filexy2,
                   filexy1,
                   match,
                   tolerance=15,
                   refpoints=reffile,
                   matching='tolerance',
                   interactive='no')
    iraf.imgets(image=im1, param='i_naxis1')
    t = iraf.imgets.value
    im1xmax = (float(t))
    iraf.imgets(image=im1, param='i_naxis2')
    t = iraf.imgets.value
    im1ymax = (float(t))

    iraf.geomap(input=match,
                database=dbase,
                function='polynomial',
                xmin=1,
                xmax=im1xmax,
                ymin=1,
                ymax=im1ymax,
                xxorder=4,
                xyorder=4,
                yyorder=4,
                yxorder=4,
                transform='gmos',
                interactive='yes')
    transformim = 1
    if transformim > 0:
        outim2 = 'g' + im2
        iraf.geotran(im2, output=outim2, database=dbase, transform='gmos')
        iraf.display(im2, 1, fill='yes')
        iraf.display(outim2, 2, fill='yes')
        iraf.display(im1, 3, fill='yes')
Exemple #12
0
def kaitsub(refimage,inlist,kernel=9,satval=50000.0,dspace=0,dbkg=0,
            nstamps=2,clobber=globclob):

    infiles = iraffiles(inlist)

    # RA and Dec of center pixel in reference image
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    xcenpix = (nx / 2.0)
    ycenpix = (ny / 2.0)
    [[ra, dec]] = impix2wcs(refimage, xcenpix, ycenpix)

    # Big loop
    for image in infiles:

        # Separate image rootname from extension
        root,ext = image.split('.')
 
        # Make sure WCS fit was successful
        if not check_head(image, 'IQWCS'):
            print 'Skipping image %s: IQWCS not successful' % image
            infiles.remove(image)
            continue
        
        # Remap image using objects detected on both frames and
        # create stamp list
        stars=Starlist(get_head(image,'STARFILE'))
        refstars=Starlist(get_head(refimage,'STARFILE'))
        refstars.pix2wcs(refimage)
        refstars.wcs2pix(image)
        match,refmatch=stars.match(refstars,useflags=yes,tol=10.0)
        nstars=len(match)
        if not (nstars>2):
            print 'Could not find star matches between reference and %s' % image
            infiles.remove(image)
            continue
        refmatch.pix2wcs(image)
        refmatch.wcs2pix(refimage)
        matchfile=open('%s.match' % root, 'w')
        stampfile=open('%s.stamps' % root, 'w')
        for i in range(len(match)):
            matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % (refmatch[i].xval,
                            refmatch[i].yval,match[i].xval,match[i].yval))
            stampfile.write('%10.3f%10.3f\n' % (refmatch[i].xval,
                            refmatch[i].yval))
        matchfile.close()
        stampfile.close()
        check_exist('%s.geodb' % root, 'w', clobber=clobber)
        iraf.geomap('%s.match' % root,'%s.geodb' % root,1.0,nx,1.0,ny,
                    fitgeom="general",verbose=no,interactive=no)
        check_exist('%s.shift.fits' % root, 'w', clobber=clobber)
        iraf.geotran(image,'%s.shift' % root,'%s.geodb' % root,
                     '%s.match' % root,geometry="geometric",
                     boundary="constant",verbose=no)
 
        # Run the subtraction
        check_exist('%s.sub.fits' % root, 'w', clobber=clobber)
        if os.path.exists("stamps.lis"):
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf stamps.lis -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (root, refimage, root, satval, AP_GAIN, AP_READN, satval, FL_GAIN, FL_READN, kernel, dspace, dbkg, root, nstamps, nstamps)
        else:
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf %s.stamps -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (root, refimage, root, satval, AP_GAIN, AP_READN, satval, FL_GAIN, FL_READN, kernel, root, dspace, dbkg, root, nstamps, nstamps)
            #cmd = '$REDUCTION/pois -k %i %i -s %.2f %.2f -S %s.stamps %s %s.shift.fits %s.sub.fits' % (kernel,kernel,satval,satval,root,refimage,root,root)
        fcmd = os.popen(cmd, 'r')
        sublines = fcmd.readlines()
        fcmd.close()

    # Return
    print 'Exiting successfully'
    return
Exemple #13
0
def psfphot(inlist,
            ra,
            dec,
            reffilt,
            interact,
            fwhm,
            readnoise,
            gain,
            threshold,
            refimage=None,
            starfile=None,
            maxnpsf=5,
            clobber=globclob,
            verbose=globver,
            skykey='SKYBKG',
            filtkey='FILTER',
            pixtol=3.0):
    """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and  
        also on a set of comparison stars, using daophot.  simultaneously 
        perform aperture photometry on all the comparison stars (after 
        subtracting off contributions from neighbors) to enable absolute 
        photometry by comparison to aperture photometry of standard stars 
        observed in other fields """

    # Defaults / constants
    psfmult = 5.0  #standard factor (multiplied by fwhm to get psfradius)
    psfmultsmall = 3.0  #similar to psfmult, adjusted for nstar and substar

    # Necessary package
    iraf.imutil()

    # Parse inputs
    infiles = iraffiles(inlist)

    # Which file is reffilt?  call it refimage
    if refimage == None:
        for image in infiles:
            if check_head(image, filtkey):
                try:
                    imgfilt = get_head(image, filtkey)
                    if imgfilt == reffilt:
                        refimage = image
                        break
                except:
                    pass

    if not refimage:
        print "BAD USER!  No image corresponds to the filter: %s" % reffilt
        return
    else:
        refroot = 's' + refimage.split('.')[0]

    #first make sure to add back in background of sky
    iraf.iqsubsky(inlist, sub=no, skykey=skykey)

    #put reference image first on list
    infiles.remove(refimage)
    infiles.insert(0, refimage)

    #setup for keywords
    if gain == "!GAIN":
        try:
            gainval = float(get_head(image, gain))
        except:
            print "Bad header keyword for gain."
    else:
        gainval = float(gain)

    if readnoise == "!READNOISE":
        try:
            readval = float(get_head(image, readnoise))
        except:
            print "Bad header keyword for readnoise."
    else:
        readval = float(readnoise)

    # Process each file in turn
    for image in infiles:

        # Check that the image is there
        check_exist(image, "r")

        # Grab image root name
        root = image.split('.')[0]

        # Map image to reference image
        if not (image == refimage):
            [nx, ny] = get_head(image, ['NAXIS1', 'NAXIS2'])
            stars = Starlist(get_head(image, 'STARFILE'))
            refstars = Starlist(get_head(refimage, 'STARFILE'))
            refstars.pix2wcs(refimage)
            refstars.wcs2pix(image)
            match, refmatch = stars.match(refstars, useflags=yes, tol=10.0)
            nstars = len(match)
            if not (nstars > 2):
                print 'Could not find star matches between reference and %s' % image
                infiles.remove(image)
                continue
            refmatch.pix2wcs(image)
            refmatch.wcs2pix(refimage)
            matchfile = open('%s.match' % root, 'w')
            for i in range(len(match)):
                matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' %
                                (refmatch[i].xval, refmatch[i].yval,
                                 match[i].xval, match[i].yval))
            matchfile.close()
            check_exist('%s.geodb' % root, 'w', clobber=clobber)
            iraf.geomap('%s.match' % root,
                        '%s.geodb' % root,
                        1.0,
                        nx,
                        1.0,
                        ny,
                        verbose=no,
                        interactive=no)
            check_exist('s%s.fits' % root, 'w', clobber=clobber)
            iraf.geotran(image,
                         's%s' % root,
                         '%s.geodb' % root,
                         '%s.match' % root,
                         geometry="geometric",
                         boundary="constant",
                         verbose=no)
        else:
            iraf.imcopy(image, 's%s' % root)
        root = 's%s' % root

        #get sky level and calculate sigma
        #if check_head(image, skykey):
        #    try:
        #        sky=float(get_head(image, skykey))
        #    except:
        #        print "No sky levels in header."

        #sigma= (((sky * gainval) + readval**2)**.5) / gainval
        iraf.iterstat(image)

        # Saturation level
        if not check_head(image, "SATURATE"):
            saturate = 60000.0
        else:
            saturate = get_head(image, "SATURATE")

        # Update datapars and daopars
        iraf.datapars.fwhmpsf = fwhm
        iraf.datapars.sigma = iraf.iterstat.sigma
        iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma
        iraf.datapars.datamax = 0.90 * saturate
        iraf.datapars.readnoise = readval
        iraf.datapars.epadu = gainval
        iraf.datapars.filter = filtkey
        iraf.daopars.psfrad = psfmult * fwhm
        iraf.daopars.fitrad = fwhm
        iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1"

        #find stars in image unless a starlist is given
        if image == refimage and starfile == None:
            iraf.daophot.daofind(root,
                                 'refimage.coo.1',
                                 threshold=threshold,
                                 verify=no,
                                 verbose=verbose)
        elif image == refimage:
            shutil.copy(starfile, 'refimage.coo.1')

        #initial photometry
        iraf.daophot.phot(root,
                          'refimage.coo.1',
                          'default',
                          aperture=fwhm,
                          verify=no,
                          verbose=verbose)

        #select stars for psf the first time
        refstarsfile = "refimage.pst.1"
        if image == refimage:
            iraf.pstselect(root,
                           'default',
                           refstarsfile,
                           maxnpsf,
                           interactive=yes,
                           verify=no,
                           verbose=verbose)

        #fit the psf
        iraf.psf(root,
                 'default',
                 refstarsfile,
                 'default',
                 'default',
                 'default',
                 interactive=interact,
                 verify=no,
                 verbose=verbose)

        #identify neighboring/interfering stars to selected stars
        groupingfile = root + ".psg.1"
        iraf.nstar(root,
                   groupingfile,
                   'default',
                   'default',
                   'default',
                   psfrad=psfmultsmall * fwhm,
                   verify=no,
                   verbose=verbose)

        #subtract out neighboring stars from image
        iraf.substar(root,
                     'default',
                     refstarsfile,
                     'default',
                     'default',
                     psfrad=psfmultsmall * fwhm,
                     verify=no,
                     verbose=verbose)

        #repeat psf to get better psf model
        #IRAF's interactive version usually crashes
        subtractedimage = root + ".sub.1"
        iraf.psf(subtractedimage,
                 root + ".nst.1",
                 refstarsfile,
                 '%s.psf.2' % root,
                 '%s.pst.2' % root,
                 '%s.psg.2' % root,
                 interactive=interact,
                 verify=no,
                 verbose=verbose)

        #Need to make sure SN was detected by daofind
        stars = Starlist('%s.mag.1' % root)
        SN = Star(name='SN', radeg=ra, dcdeg=dec, fwhm=2.0, fwhmw=2.0)
        SNlis = Starlist(stars=[SN])
        SNlis.wcs2pix(image)
        if (len(stars.match(SNlis)[0]) == 0):
            #No match - need to add to daofind file
            print "No match!"
            coofile = open('refimage.coo.1', 'a+')
            coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' %
                          (SNlis[0].xval, SNlis[0].yval, 99.999, 0.500, 0.000,
                           0.000, 999))
            coofile.close()

        #repeat aperture photometry to get good comparisons to standard fields
        iraf.daophot.phot(root,
                          'refimage.coo.1',
                          'default',
                          aperture=psfmult * fwhm,
                          verify=no,
                          verbose=verbose)

        # allstar run
        iraf.allstar(root,
                     'default',
                     'default',
                     'default',
                     'default',
                     'default',
                     verify=no,
                     verbose=verbose)
Exemple #14
0
def mscimage(input=None,
             output=None,
             format='image',
             pixmask=no,
             verbose=')_.verbose',
             wcssource='image',
             reference='',
             ra=INDEF,
             dec=INDEF,
             scale=INDEF,
             rotation=INDEF,
             blank=0.0,
             interpolant='poly5',
             minterpolant='linear',
             boundary='reflect',
             constant=0.0,
             fluxconserve=no,
             ntrim=8,
             nxblock=INDEF,
             nyblock=INDEF,
             interactive=no,
             nx=10,
             ny=20,
             fitgeometry='general',
             xxorder=4,
             xyorder=4,
             xxterms='half',
             yxorder=4,
             yyorder=4,
             yxterms='half',
             fd_in='',
             fd_ext='',
             fd_coord='',
             mode='ql',
             DOLLARnargs=0,
             taskObj=None):

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

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


    """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and  
        also on a set of comparison stars, using daophot.  simultaneously 
        perform aperture photometry on all the comparison stars (after 
        subtracting off contributions from neighbors) to enable absolute 
        photometry by comparison to aperture photometry of standard stars 
        observed in other fields """

    # Defaults / constants
    psfmult=5.0         #standard factor (multiplied by fwhm to get psfradius)
    psfmultsmall=3.0    #similar to psfmult, adjusted for nstar and substar

    # Necessary package
    iraf.imutil()

    # Parse inputs
    infiles=iraffiles(inlist)

    # Which file is reffilt?  call it refimage
    if refimage==None:
        for image in infiles:
            if check_head(image, filtkey):
                try:
                    imgfilt = get_head(image, filtkey)
                    if imgfilt == reffilt:
                        refimage = image
                        break
                except:
                    pass
            
    if not refimage:
        print "BAD USER!  No image corresponds to the filter: %s" % reffilt
        return
    else:
        refroot='s'+refimage.split('.')[0]

    #first make sure to add back in background of sky
    iraf.iqsubsky(inlist, sub=no, skykey=skykey)

    #put reference image first on list
    infiles.remove(refimage)
    infiles.insert(0,refimage)

    #setup for keywords
    if gain == "!GAIN":
        try: gainval = float(get_head(image, gain))
        except:
            print "Bad header keyword for gain."
    else:
        gainval = float(gain)

    if readnoise == "!READNOISE":
        try: readval = float(get_head(image, readnoise))
        except:
            print "Bad header keyword for readnoise."
    else:
        readval = float(readnoise)

    # Process each file in turn
    for image in infiles:

        # Check that the image is there
        check_exist(image,"r")

        # Grab image root name
        root=image.split('.')[0]

        # Map image to reference image
        if not (image==refimage):
            [nx,ny]=get_head(image,['NAXIS1','NAXIS2'])
            stars=Starlist(get_head(image,'STARFILE'))
            refstars=Starlist(get_head(refimage,'STARFILE'))
            refstars.pix2wcs(refimage)
            refstars.wcs2pix(image)
            match,refmatch=stars.match(refstars,useflags=yes,tol=10.0)
            nstars=len(match)
            if not (nstars>2):
                print 'Could not find star matches between reference and %s' % image
                infiles.remove(image)
                continue
            refmatch.pix2wcs(image)
            refmatch.wcs2pix(refimage)
            matchfile=open('%s.match' % root, 'w')
            for i in range(len(match)):
                matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % 
                               (refmatch[i].xval,refmatch[i].yval,
                                match[i].xval,match[i].yval))
            matchfile.close()
            check_exist('%s.geodb' % root, 'w', clobber=clobber)
            iraf.geomap('%s.match' % root,'%s.geodb' % root,1.0,nx,1.0,ny,
                        verbose=no,interactive=no)
            check_exist('s%s.fits' % root, 'w', clobber=clobber)
            iraf.geotran(image,'s%s' % root,'%s.geodb' % root,
                         '%s.match' % root,geometry="geometric",
                         boundary="constant",verbose=no)
        else:
            iraf.imcopy(image,'s%s' % root)
        root='s%s' % root
 
        #get sky level and calculate sigma
        #if check_head(image, skykey):
        #    try:
        #        sky=float(get_head(image, skykey))
        #    except:
        #        print "No sky levels in header."

        #sigma= (((sky * gainval) + readval**2)**.5) / gainval        
        iraf.iterstat(image)
        
        # Saturation level
        if not check_head(image, "SATURATE"):
        	saturate = 60000.0
        else:
        	saturate = get_head(image, "SATURATE")
        	        
        # Update datapars and daopars
        iraf.datapars.fwhmpsf=fwhm
        iraf.datapars.sigma=iraf.iterstat.sigma
        iraf.datapars.datamin=iraf.iterstat.median-10*iraf.iterstat.sigma
        iraf.datapars.datamax=0.90*saturate
        iraf.datapars.readnoise=readval
        iraf.datapars.epadu=gainval
        iraf.datapars.filter=filtkey
        iraf.daopars.psfrad=psfmult*fwhm
        iraf.daopars.fitrad=fwhm
        iraf.daopars.function="gauss,moffat15,moffat25,lorentz,penny1"

        #find stars in image unless a starlist is given
        if image==refimage and starfile==None:
            iraf.daophot.daofind(root,'refimage.coo.1',threshold=threshold,verify=no,
                         verbose=verbose)
        elif image==refimage:
            shutil.copy(starfile,'refimage.coo.1')

        #initial photometry
        iraf.daophot.phot(root,'refimage.coo.1','default',aperture=fwhm,verify=no,
                  verbose=verbose)

        #select stars for psf the first time
        refstarsfile = "refimage.pst.1"
        if image == refimage:
            iraf.pstselect(root,'default',refstarsfile,maxnpsf,
                           interactive=yes,verify=no,verbose=verbose)

        #fit the psf
        iraf.psf(root,'default',refstarsfile,'default','default','default',
                 interactive=interact,verify=no,verbose=verbose)

        #identify neighboring/interfering stars to selected stars
        groupingfile = root+".psg.1"
        iraf.nstar(root,groupingfile,'default','default','default',
                   psfrad= psfmultsmall * fwhm,verify=no,verbose=verbose)

        #subtract out neighboring stars from image
        iraf.substar(root,'default',refstarsfile,'default','default',
                     psfrad=psfmultsmall*fwhm,verify=no,verbose=verbose)

        #repeat psf to get better psf model
        #IRAF's interactive version usually crashes
        subtractedimage = root+".sub.1"
        iraf.psf(subtractedimage,root+".nst.1",refstarsfile,'%s.psf.2' % root,
                 '%s.pst.2' % root,'%s.psg.2' % root,interactive=interact,
                 verify=no,verbose=verbose)

        #Need to make sure SN was detected by daofind
        stars=Starlist('%s.mag.1' % root)
        SN=Star(name='SN',radeg=ra,dcdeg=dec,fwhm=2.0,fwhmw=2.0)
        SNlis=Starlist(stars=[SN])
        SNlis.wcs2pix(image)
        if (len(stars.match(SNlis)[0])==0):
            #No match - need to add to daofind file
            print "No match!"
            coofile=open('refimage.coo.1', 'a+')
            coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' % (SNlis[0].xval, SNlis[0].yval,99.999,0.500,0.000,0.000,999))
            coofile.close()    

        #repeat aperture photometry to get good comparisons to standard fields
        iraf.daophot.phot(root,'refimage.coo.1','default',aperture=psfmult*fwhm,
                  verify=no,verbose=verbose)

        # allstar run
        iraf.allstar(root,'default','default','default','default','default',
                     verify=no,verbose=verbose)
Exemple #16
0
def spitzer_sub(new, newunc, newcov, ref, refunc, refcov, out,
                stamps=None, tmass=None):

    # Remove nan from mosaics
    x = pyfits.open(new)
    y = np.nan_to_num(x[0].data)
    x[0].data = y
    new2 = "n%s" % new
    x.writeto(new2)

    # Find stars in new image
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (new2, newcov))

    # Find stars in reference
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (ref, refcov))

    # Create file for geotran
    stars = Starlist("%s.stars" % new2)
    refstars = Starlist("%s.stars" % ref)
    refstars.pix2wcs(ref)
    refstars.wcs2pix(new2)
    a,b = stars.match(refstars, maxnum=1000)

    # Create file for geotran
    b.pix2wcs(new2)
    b.wcs2pix(ref)
    refroot = ref.split(".")[0]
    outf = open("%s.match" % refroot, "w")
    for i in range(len(a)):
        outf.write("%10.3f%10.3f%10.3f%10.3f\n" % (a[i].xval, a[i].yval,
                   b[i].xval, b[i].yval))
    outf.close()

    # Geomap and geotran
    [naxis1, naxis2] = get_head(new, ["NAXIS1", "NAXIS2"])
    iraf.geomap("%s.match" % refroot, "%s.db" % refroot, 1, naxis1, 1, naxis2,
                fitgeometry="rotate", interactive=no)
    iraf.geotran(ref, "t%s" % ref, "%s.db" % refroot, "%s.match" % refroot)
    iraf.geotran(refunc, "t%s" % refunc, "%s.db" % refroot, 
                 "%s.match" % refroot)

    # Get stars for PSF matching
    if stamps != None:

        psfstars = Starlist(stamps)
        psfstars.pix2wcs(ref)
        psfstars.wcs2pix(new2)
        outf = open("stamps.lis", "w")
        for star in psfstars:
            outf.write("%10.3f%10.3f\n" % (star.xval, star.yval))
        outf.close()

    # Appropriate parameters
    iraf.iterstat(ref)
    update_head(ref, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median,
                                            iraf.iterstat.sigma])
    [refskybkg, refskysig] = get_head(ref, ["MEDSKY", "SKYSIG"])
    tl = refskybkg - 10 * refskysig; tu = 30000.0
    iraf.iterstat(new)
    update_head(new, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median, 
                                            iraf.iterstat.sigma])
    [newskybkg, newskysig] = get_head(new, ["MEDSKY", "SKYSIG"])
    il = newskybkg - 10 * newskysig; iu = 30000.0
    
    # Run hotpants
    hpcmd = "hotpants -inim %s -tmplim t%s -outim %s -tni t%s -ini %s -nsx 3 -nsy 3 -savexy %s.xy -ko 0 -bgo 0 -oni u%s -n t -tl %.2f -tu %.2f -il %.2f -iu %.2f -r 7.5 -rss 18.0" % (new2, ref, out, refunc, newunc, new2, out, tl, tu, il, iu)
    if stamps != None:
        hpcmd += " -ssf stamps.lis -afssc 0"
    os.system(hpcmd)
    #iraf.imarith(new2, "-", "t%s" % ref, out)

    # Create appropriate weight image
    #iraf.imexpr("sqrt(a**2 + b**2)", "u%s" % out, a=newunc, b=refunc)
    iraf.imarith(1, "/", "u%s" % out, "temp1.fits")
    iraf.imarith("temp1.fits", "/", "u%s" % out, "w%s" % out)
    os.remove("temp1.fits")

    # Pick up candidates
    os.system("$REDUCTION/runsex.pl %s 2.0 -weight w%s" % (out, out))
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (nax2 - EDGETOL) and refu[0].data[star.yval,star.xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)

    # Filter out bright stars
    if tmass==None:
        scands.write("cands.reg")
    else:
        stmass = Starlist(tmass)
        stmass.wcs2pix(ref)
        s2cands = scands.nomatch(stmass)
        s2cands.write("cands.reg")
    
    # More comprehensive list
    os.system("$REDUCTION/runsex.pl %s 1.5" % out)
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (nax2 - EDGETOL) and refu[0].data[star.yval,star.xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)
    scands.write("cands_all.reg")

    return
Exemple #17
0
def spitzer_sub(new,
                newunc,
                newcov,
                ref,
                refunc,
                refcov,
                out,
                stamps=None,
                tmass=None):

    # Remove nan from mosaics
    x = pyfits.open(new)
    y = np.nan_to_num(x[0].data)
    x[0].data = y
    new2 = "n%s" % new
    x.writeto(new2)

    # Find stars in new image
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (new2, newcov))

    # Find stars in reference
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (ref, refcov))

    # Create file for geotran
    stars = Starlist("%s.stars" % new2)
    refstars = Starlist("%s.stars" % ref)
    refstars.pix2wcs(ref)
    refstars.wcs2pix(new2)
    a, b = stars.match(refstars, maxnum=1000)

    # Create file for geotran
    b.pix2wcs(new2)
    b.wcs2pix(ref)
    refroot = ref.split(".")[0]
    outf = open("%s.match" % refroot, "w")
    for i in range(len(a)):
        outf.write("%10.3f%10.3f%10.3f%10.3f\n" %
                   (a[i].xval, a[i].yval, b[i].xval, b[i].yval))
    outf.close()

    # Geomap and geotran
    [naxis1, naxis2] = get_head(new, ["NAXIS1", "NAXIS2"])
    iraf.geomap("%s.match" % refroot,
                "%s.db" % refroot,
                1,
                naxis1,
                1,
                naxis2,
                fitgeometry="rotate",
                interactive=no)
    iraf.geotran(ref, "t%s" % ref, "%s.db" % refroot, "%s.match" % refroot)
    iraf.geotran(refunc, "t%s" % refunc, "%s.db" % refroot,
                 "%s.match" % refroot)

    # Get stars for PSF matching
    if stamps != None:

        psfstars = Starlist(stamps)
        psfstars.pix2wcs(ref)
        psfstars.wcs2pix(new2)
        outf = open("stamps.lis", "w")
        for star in psfstars:
            outf.write("%10.3f%10.3f\n" % (star.xval, star.yval))
        outf.close()

    # Appropriate parameters
    iraf.iterstat(ref)
    update_head(ref, ["MEDSKY", "SKYSIG"],
                [iraf.iterstat.median, iraf.iterstat.sigma])
    [refskybkg, refskysig] = get_head(ref, ["MEDSKY", "SKYSIG"])
    tl = refskybkg - 10 * refskysig
    tu = 30000.0
    iraf.iterstat(new)
    update_head(new, ["MEDSKY", "SKYSIG"],
                [iraf.iterstat.median, iraf.iterstat.sigma])
    [newskybkg, newskysig] = get_head(new, ["MEDSKY", "SKYSIG"])
    il = newskybkg - 10 * newskysig
    iu = 30000.0

    # Run hotpants
    hpcmd = "hotpants -inim %s -tmplim t%s -outim %s -tni t%s -ini %s -nsx 3 -nsy 3 -savexy %s.xy -ko 0 -bgo 0 -oni u%s -n t -tl %.2f -tu %.2f -il %.2f -iu %.2f -r 7.5 -rss 18.0" % (
        new2, ref, out, refunc, newunc, new2, out, tl, tu, il, iu)
    if stamps != None:
        hpcmd += " -ssf stamps.lis -afssc 0"
    os.system(hpcmd)
    #iraf.imarith(new2, "-", "t%s" % ref, out)

    # Create appropriate weight image
    #iraf.imexpr("sqrt(a**2 + b**2)", "u%s" % out, a=newunc, b=refunc)
    iraf.imarith(1, "/", "u%s" % out, "temp1.fits")
    iraf.imarith("temp1.fits", "/", "u%s" % out, "w%s" % out)
    os.remove("temp1.fits")

    # Pick up candidates
    os.system("$REDUCTION/runsex.pl %s 2.0 -weight w%s" % (out, out))
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (
                nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (
                    nax2 - EDGETOL) and refu[0].data[
                        star.yval, star.
                        xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)

    # Filter out bright stars
    if tmass == None:
        scands.write("cands.reg")
    else:
        stmass = Starlist(tmass)
        stmass.wcs2pix(ref)
        s2cands = scands.nomatch(stmass)
        s2cands.write("cands.reg")

    # More comprehensive list
    os.system("$REDUCTION/runsex.pl %s 1.5" % out)
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (
                nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (
                    nax2 - EDGETOL) and refu[0].data[
                        star.yval, star.
                        xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)
    scands.write("cands_all.reg")

    return
Exemple #18
0
        ffile = 'fmd' + file
        m999file = 'mfmd' + file
        gfile = 'gmfmd' + file
        if npass == 1:
            iraf.imarith(mfile, "/", flat, ffile)
            diminput = fname
        if npass == 2:
            iraf.imdelete(ffile)
            iraf.imarith(mfile, "/", flat, ffile)
            domask999(ffile, invmask)
            #            iraf.geotran(ffile,gfile,database="geodb4",trans="mmtj",geometry="geometric")

            iraf.geotran(m999file,
                         gfile,
                         database=geodb,
                         trans=geotrans,
                         geometry="geometric",
                         interpolant="nearest",
                         boundary="nearest",
                         fluxconserve="no")
            diminput = gname
    makedim = 'ls ' + diminput + ' > diminlist' + str(i)
    os.system(makedim)
    i = i + 1
gmask = 'g' + mask
try:
    iraf.geotran(
        mask,
        gmask,
        database=geodb,
        trans=geotran,
        geometry="geometric",
Exemple #19
0
def sigmap(inlist,
           sigmap,
           expmap='none',
           whtmap='none',
           inpref='',
           ffpref='',
           objmask='none',
           reject='sigclip',
           fscale=False,
           fbase=100,
           fhead='F1',
           gain=5.6):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    # for exposure time weight and flux scaling
    expt_arr = []
    flux_scale_arr = []
    for i in range(len(inimg_arr)):

        # load original frame
        img = pyfits.open(inimg_arr[i])

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

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

    # preparing weighted variance map for each image
    inverse_var_list = tmp_prefix + '_obj.lst'
    if os.access(inverse_var_list, os.R_OK):
        os.remove(inverse_var_list)
    finverse_var = open(inverse_var_list, 'w')

    for i in range(len(inimg_arr)):

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

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

        # load original frame
        ffimg = pyfits.open(ffimg_arr[i])
        img = pyfits.open(inimg_arr[i])
        # object frame
        inverse_var = np.zeros((y_size, x_size))
        #print np.median(ffimg[0].data), ffimg_arr[i], gain, expt_arr[i], flux_scale_arr[i]
        #print np.median(np.sqrt(ffimg[0].data / (gain * expt_arr[i]))), flux_scale_arr[i]
        inverse_var[ymin - 1:ymax, xmin -
                    1:xmax] = (np.sqrt(ffimg[0].data / (gain * expt_arr[i])) *
                               flux_scale_arr[i])**-2
        hdu = pyfits.PrimaryHDU(inverse_var)
        inverse_var_img = pyfits.HDUList([hdu])
        inverse_var_img[0].header = img[0].header
        inverse_var_img[0].header['bpm'] = msktr_fits
        inverse_var_img[0].header.update('EXPTIME', expt_arr[i])
        #inverse_var_img[0].header.update('MASKSCAL', expt_arr[i])
        #inverse_var_img[0].header.update('MASKZERO', expt_arr[i])
        #print 'EXPT = %f' % (expt_arr[i])
        inverse_var_fits = tmp_prefix + 'var' + os.path.basename(inimg_arr[i])
        inverse_vartr_fits = tmp_prefix + 'vartr' + os.path.basename(
            inimg_arr[i])
        if os.access(inverse_var_fits, os.R_OK):
            os.remove(inverse_var_fits)
        if os.access(inverse_vartr_fits, os.R_OK):
            os.remove(inverse_vartr_fits)
        inverse_var_img.writeto(inverse_var_fits)
        inverse_var_img.close()
        iraf.geotran(inverse_var_fits,
                     inverse_vartr_fits,
                     dbs_arr[i],
                     gmp2_arr[i],
                     geometr='linear',
                     boundar='constant',
                     constant=0)
        finverse_var.write('%s\n' % inverse_vartr_fits)
        ffimg.close()

    # close file handlers
    finverse_var.close()

    # sum weighted variance images
    tmp_inverse_var_sum = tmp_prefix + 'inverse_var.fits'
    if os.access(tmp_inverse_var_sum, os.R_OK):
        os.remove(tmp_inverse_var_sum)
    tmp_sigma = tmp_prefix + 'sigma.fits'
    if os.access(tmp_sigma, os.R_OK):
        os.remove(tmp_sigma)
    tmp_exp = tmp_prefix + 'exp.fits'
    if os.access(tmp_exp, os.R_OK):
        os.remove(tmp_exp)

    if expmap != 'none':
        #iraf.hselect('@'+inverse_var_list,"$I,EXPTIME,MASKSCAL,MASKZERO","yes")
        iraf.imcombine('@' + inverse_var_list,
                       tmp_inverse_var_sum,
                       expmasks=tmp_exp,
                       combine='sum',
                       reject=reject,
                       masktype='!BPM',
                       maskvalue=0.0,
                       expname='EXPTIME')
    else:
        iraf.imcombine('@' + inverse_var_list,
                       tmp_inverse_var_sum,
                       combine='sum',
                       reject=reject,
                       masktype='!BPM',
                       maskvalue=0.0)

    # calculate sigma
    iraf.stsdas()
    iraf.imcalc(tmp_inverse_var_sum,
                tmp_sigma,
                'sqrt(1.0/im1)',
                pixtype='double')

    # cut image
    iraf.unlearn('imcopy')
    cut_sig = '%s[%d:%d,%d:%d]' % (tmp_sigma, xcmin, xcmax, ycmin, ycmax)
    iraf.imcopy(cut_sig, sigmap)
    if expmap != 'none':
        cut_exp = '%s[%d:%d,%d:%d]' % (tmp_exp, xcmin, xcmax, ycmin, ycmax)
        iraf.imcopy(cut_exp, expmap)

    # calc weight map
    if whtmap != 'none':
        cut_wht = '%s[%d:%d,%d:%d]' % (tmp_inverse_var_sum, xcmin, xcmax,
                                       ycmin, ycmax)
        iraf.imcopy(cut_wht, whtmap)

    # delete temporary object files
    os.remove(inverse_var_list)

    # remove all temporary files
    remove_temp_all(tmp_prefix)

    return 0
Exemple #20
0
def align_norm(fnlist,uncertlist=None):
    """Aligns a set of images to each other, as well as normalizing the images
    to the same average brightness.
    
    Both the alignment and normalization are accomplished through stellar
    photometry using the IRAF routine 'daophot'. The centroids of a handful
    of stars are found and used to run the IRAF routine 'imalign'. The
    instrumental magnitudes of the stars are used to determine by how much
    each image must be scaled for the photometry to match across images.
    
    The images are simply updated with their rescaled, shifted selves. This
    overwrites the previous images and adds the header keyword 'fpphot' to
    the images.
    
    A handful of temporary files are created during this process, which should
    all be deleted by the routine at the end. But if it is interrupted, they
    might not be.
    
    If the uncertainty images exist, this routine also shifts them by the same
    amounts as the intensity images, as well as updating the uncertainty values
    for both the new normalization and the uncertainties in normalizing the
    images.
    
    Inputs:
    fnlist -> List of strings, each the path to a fits image.
    uncertlist (optional) -> List of paths to uncertainty images.
    
    """
    
    #Fit for the sky background level
    _skyavg, skysig = fit_sky_level(fnlist)
    
    #Get image FWHMs
    fwhm = np.empty(len(fnlist))
    firstimage = openfits(fnlist[0])
    toggle = firstimage[0].header.get("fpfwhm")
    axcen = firstimage[0].header.get("fpaxcen")
    aycen = firstimage[0].header.get("fpaycen")
    arad = firstimage[0].header.get("fparad")
    firstimage.close()
    if axcen == None:
        print "Error! Images have not yet been aperture-masked! Do this first!"
        crash()
    if toggle == None:
        print "Warning: FWHMs have not been measured! Assuming 5 pixel FWHM for all images."
        for i in range(len(fnlist)): fwhm[i] = 5
    else:
        for i in range(len(fnlist)):
            image = openfits(fnlist[i])
            fwhm[i] = image[0].header["fpfwhm"]
            image.close()
    
    #Identify objects in the fields
    coolist = identify_objects(fnlist,skysig,fwhm)
    
    #Match objects between fields
    coofile = match_objects(coolist)
    
    #Do aperture photometry on the matched objects
    photlist = do_phot(fnlist,coofile,fwhm,skysig)
    
    #Read the photometry files
    x, y, mag, dmag = read_phot(photlist)
    
    #Calculate the normalizations
    norm, dnorm = calc_norm(mag,dmag)
    
    #Normalize the images (and optionally, the uncertainty images)
    for i in range(len(fnlist)):
        print "Normalizing image "+fnlist[i]
        image = openfits(fnlist[i],mode="update")
        if not (uncertlist is None):
            uncimage = openfits(uncertlist[i],mode="update")
            uncimage[0].data = np.sqrt(norm[i]**2*uncimage[0].data**2 + dnorm[i]**2*image[0].data**2)
            uncimage.close()
        image[0].data *= norm[i]
        image.close()
    
    #Calculate the shifts
    for i in range(x.shape[1]):
        x[:,i] = -(x[:,i] - x[0,i])
        y[:,i] = -(y[:,i] - y[0,i])
    xshifts = np.average(x,axis=1)
    yshifts = np.average(y,axis=1)
    
    #Shift the images (and optionally, the uncertainty images)
    iraf.images(_doprint=0)
    iraf.immatch(_doprint=0)
    for i in range(len(fnlist)):
        print "Shifting image "+fnlist[i]
        iraf.geotran(input=fnlist[i],
                     output=fnlist[i],
                     geometry="linear",
                     xshift=xshifts[i],
                     yshift=yshifts[i],
                     database="",
                     verbose="no")
        if not (uncertlist is None):
            iraf.geotran(input=uncertlist[i],
                         output=uncertlist[i],
                         geometry="linear",
                         xshift=xshifts[i],
                         yshift=yshifts[i],
                         database="",
                         verbose="no")
    
    #Update the image headers
    for i in range(len(fnlist)):
        image = openfits(fnlist[i],mode="update")
        image[0].header["fpphot"]="True"
        image[0].header["fpxcen"]+=xshifts[i]
        image[0].header["fpycen"]+=yshifts[i]
        image[0].header["fpaxcen"]+=xshifts[i]
        image[0].header["fpaycen"]+=yshifts[i]
        image.close()
    
    #Clean up the coordinate file list
    clean_files(fnlist)
    remove(coofile)
    for i in range(len(photlist)):
        remove(photlist[i])
    
    return
Exemple #21
0
def stacking(cllist,zpofflist,ref,zprefoff=0.0,stackname='stack',shiftsize=400):

    """
    """

    #Reset the IRAF tasks used in this routine.
    iraf.unlearn('imcalc')
    iraf.unlearn('imcombine')
    iraf.unlearn('imreplace')
    iraf.unlearn('xyxymatch')
    iraf.unlearn('geomap')
    iraf.unlearn('geotran')
    iraf.unlearn('imcopy')

    #Find reference image in reference directory. Check to make
    #sure that it is actually the image and not the mask file!
    #Grab the mask for adding to the mask list now.
    (refimg,refmask,expmap)=classify(ref+'/tu*.fits')
    zpref=pf.open(refimg)[0].header['MAGZERO']
#    zprefoff=NewfirmZPoffset[ref.split('/')[-1]]
    zprefoff=float(zprefoff)

    #Get 2MASS PSC positions for reference cluster image.
    catalog=get2masspsc(refimg)
    foo=file_check(ref+'/2mass_ref_stars.cdt',delete=True)
    foo=open(ref+'/2mass_ref_stars.cdt','w')
    for y in catalog:
        data=y.split()
        foo.write(data[6]+'\t'+data[7]+'\n')
    foo.close()

    #Create lists for files to be input into the stacking routine.
    foo=file_check('matchlist',delete=True)
    foo=file_check('scalelist',delete=True)
    foo=file_check('shiftlist',delete=True)
    foo=file_check('masklist',delete=True)
    foo=file_check('shiftmask',delete=True)
    foo=file_check('expmaplist',delete=True)
    (matchlist,scalelist,shiftlist,masklist,
     shiftmask,finalmasks,stacklist,stackmask,
     finalmasks2,expmaplist,shiftexp,expmaplist2)=(open('matchlist','w'),open('scalelist','w'),
                                        open('shiftlist','w'),open('masklist','w'),
                                        open('shiftmask','w'),open('finalmasks','w'),
                                        open('stacklist','w'),open('stackmask','w'),
                                        open('finalmasks2','w'),open('expmaplist','w'),
                                        open('shiftexp','w'),open('expmaplist2','w'))
    (xsize,ysize)=(np.array([]),np.array([]))
    
    
    #Step through all of the input cluster directories.
    i=0
    for x in cllist:
        #Find the image, mask, and exposure map files. Get zeropoints and
        #scale image to the reference image.
        scaleimg=x+'/scaled_to_'+ref.split('/')[-1]+'.fits'
        foo=file_check(scaleimg,delete=True)
        (img,mask,expmap)=classify(x+'/tu*.fits')
        imgzp=pf.open(img)[0].header['MAGZERO']
        (xs,ys)=(pf.open(img)[0].header['NAXIS1'],pf.open(img)[0].header['NAXIS2'])
        (xsize,ysize)=(np.append(xsize,xs),np.append(ysize,ys))

        imgzpoff=float(zpofflist[i])
#        imgzpoff=NewfirmZPoffset[x.split('/')[-1]]
        scale=scalecounts(imgzp+imgzpoff,zpref+zprefoff)
        iraf.imcalc(img,scaleimg,'im1*'+str(scale))

        #Get X,Y pixel positions of 2MASS sources from the 2MASS PSC
        #in the image. Use these to compute shifts relative to the
        #reference image using IRAF task geomap.
        foo=file_check(x+'/2mass_ref_stars.cdt',delete=True)
        foo=open(x+'/2mass_ref_stars.cdt','w')
        catalog=get2masspsc(scaleimg)
        for y in catalog:
            data=y.split()
            foo.write(data[6]+'\t'+data[7]+'\n')
        foo.close()
    
        #Match the 2MASS PSC positions with stars in the reference
        #image using xyxymatch. The matched source list is then fed
        #into geomap to get the X and Y shifts.
        foo=file_check(x+'/2mass_matched.cdt',delete=True)
        iraf.xyxymatch(x+'/2mass_ref_stars.cdt',ref+'/2mass_ref_stars.cdt',
                       x+'/2mass_matched.cdt','200.0',verbose='no')

        #Append all of the names of the files for the input and output filename
        #lists to be passed to IRAF tasks further down the line.
        matchlist.write(x+'/2mass_matched.cdt\n')
        scalelist.write(scaleimg+'\n')
        foo=file_check(x+'/scaled_and_shifted.fits',delete=True)
        shiftlist.write(x+'/scaled_and_shifted.fits['+str(shiftsize)+':'+\
                 str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\
                 str(int(np.max(ysize))+shiftsize)+']\n')
        stacklist.write(x+'/scaled_and_shifted.fits\n')
        file_check(x+'/mask_tmp.fits',delete=True)
        file_check(x+'/expmap_tmp.fits',delete=True)
        iraf.imarith(mask+'[1]','*',1000.0,x+'/mask_tmp.fits',pixtype='real')
        iraf.imarith(expmap+'[1]','*',1.0,x+'/expmap_tmp.fits',pixtype='real')
        offset=2.558435
        file_check(x+'/mask_tmp2.fits',delete=True)
        iraf.imcalc(x+'/mask_tmp.fits',x+'/mask_tmp2.fits','im1+'+str(offset))
        os.remove(x+'/mask_tmp.fits')
        masklist.write(x+'/mask_tmp2.fits\n')
        file_check(x+'/mask_shift.fits',delete=True)
        shiftmask.write(x+'/mask_shift.fits['+str(shiftsize)+':'+\
                    str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\
                    str(int(np.max(ysize))+shiftsize)+']\n')
        stackmask.write(x+'/mask_shift.fits\n')
        finalmasks.write(x+'/mask_final.fits\n')
        finalmasks2.write(x+'/mask_final.fits[0]\n')
        expmaplist.write(x+'/expmap_tmp.fits[0]\n')
        shiftexp.write(x+'/expmap_shift.fits['+str(shiftsize)+':'+\
                    str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\
                    str(int(np.max(ysize))+shiftsize)+']\n')
        expmaplist2.write(x+'/expmap_shift.fits\n')
        i += 1

    #Close all of the input and output filename lists to be passed to IRAF tasks.
    matchlist.close()
    scalelist.close()
    stacklist.close()
    masklist.close()
    shiftmask.close()
    finalmasks.close()
    shiftlist.close()
    stackmask.close()
    finalmasks2.close()
    expmaplist.close()
    expmaplist2.close()
    shiftexp.close()

    #Get the shifts between all input files (including the reference) and the
    #reference image itself.
    foo=file_check('shift.db',delete=True)
    iraf.geomap('@matchlist','shift.db',1.0,np.max(xsize),
                1.0,np.max(ysize),fitgeometry='shift',interactive='no',
                maxiter=2,function='legendre',verbose='no')

    #Shift the input images (including the reference) and associated mask files
    #to a common pixel grid. Add some padding around the individual frames (-99
    #in the images, 1 in the bad pixel masks) to ensure that the images will
    #combine properly.
    (maxx,maxy)=(np.max(xsize)+shiftsize+100.0,np.max(ysize)+shiftsize+100.0)
    iraf.geotran('@scalelist','@shiftlist','shift.db','@matchlist',geometry='linear',
                 boundary='constant',nlines=maxy,ncols=maxx,constant=-99.0)

    iraf.geotran('@masklist','@shiftmask','shift.db','@matchlist',geometry='linear',
                 boundary='constant',nlines=maxy,ncols=maxx,constant=1000.0,
                 nxblock=10000,nyblock=10000)
    
    iraf.geotran('@expmaplist','@shiftexp','shift.db','@matchlist',geometry='linear',
                 boundary='constant',nlines=maxy,ncols=maxx,constant=0.)

    for x in cllist:
        file_check(x+'/mask_final.fits',delete=True)
        shutil.copy(x+'/mask_shift.fits',x+'/mask_final.fits')
        iraf.hedit(x+'/scaled_and_shifted.fits[0]','BPM',x+'/mask_final.fits[0]',
                   add='yes',update='yes',verify='no')
    iraf.imreplace('@finalmasks2',0,upper=offset)
    iraf.imreplace('@finalmasks2',1,lower=offset)

    file_check(stackname,delete=True)
    file_check(stackname[:-5]+'_mask.pl',delete=True)
    file_check(stackname[:-5]+'_expmap.fits',delete=True)
    iraf.imcombine('@stacklist',stackname,bpmasks=stackname[:-5]+'_bpm',
                   masktype='goodval',reject='none',mclip='yes',lthresh='INDEF',hthresh='INDEF',
                   hsigma=10.0,lsigma='INDEF',nrejmasks=stackname[:-5]+'_nrej',
                   sigmas=stackname[:-5]+'_sigma',grow=2.5,nkeep=1,blank=-99.0,gain=8.0,rdnoise=35.0)
    iraf.imcombine('@expmaplist2',stackname[:-5]+'_expmap.fits',combine='sum')
    hdu=pf.open(stackname,mode='update')
    hdu[0].header['BPM']=stackname.split('/')[-1][:-5]+'_mask.pl'
    hdu[0].header['MAGZERO']=zpref+zprefoff
    hdu.close()

	#Fix the WCS information in the stacked image.
    copyhead(stackname,refimg,offset=shiftsize)
    applywcs(stackname,stackname[:-5]+'_wcs.fits')

    trash=['matchlist','scalelist','shiftlist','masklist','shiftmask','finalmasks',
            'shift.db','stacklist','finalmasks2','stackmask','tmp_wcs.fits','expmaplist',
            'expmaplist2','shiftexp']
    for x in trash:
        os.remove(x)
Exemple #22
0
def kaitsub(refimage,
            inlist,
            kernel=9,
            satval=50000.0,
            dspace=0,
            dbkg=0,
            nstamps=2,
            clobber=globclob):

    infiles = iraffiles(inlist)

    # RA and Dec of center pixel in reference image
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    xcenpix = (nx / 2.0)
    ycenpix = (ny / 2.0)
    [[ra, dec]] = impix2wcs(refimage, xcenpix, ycenpix)

    # Big loop
    for image in infiles:

        # Separate image rootname from extension
        root, ext = image.split('.')

        # Make sure WCS fit was successful
        if not check_head(image, 'IQWCS'):
            print 'Skipping image %s: IQWCS not successful' % image
            infiles.remove(image)
            continue

        # Remap image using objects detected on both frames and
        # create stamp list
        stars = Starlist(get_head(image, 'STARFILE'))
        refstars = Starlist(get_head(refimage, 'STARFILE'))
        refstars.pix2wcs(refimage)
        refstars.wcs2pix(image)
        match, refmatch = stars.match(refstars, useflags=yes, tol=10.0)
        nstars = len(match)
        if not (nstars > 2):
            print 'Could not find star matches between reference and %s' % image
            infiles.remove(image)
            continue
        refmatch.pix2wcs(image)
        refmatch.wcs2pix(refimage)
        matchfile = open('%s.match' % root, 'w')
        stampfile = open('%s.stamps' % root, 'w')
        for i in range(len(match)):
            matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' %
                            (refmatch[i].xval, refmatch[i].yval, match[i].xval,
                             match[i].yval))
            stampfile.write('%10.3f%10.3f\n' %
                            (refmatch[i].xval, refmatch[i].yval))
        matchfile.close()
        stampfile.close()
        check_exist('%s.geodb' % root, 'w', clobber=clobber)
        iraf.geomap('%s.match' % root,
                    '%s.geodb' % root,
                    1.0,
                    nx,
                    1.0,
                    ny,
                    fitgeom="general",
                    verbose=no,
                    interactive=no)
        check_exist('%s.shift.fits' % root, 'w', clobber=clobber)
        iraf.geotran(image,
                     '%s.shift' % root,
                     '%s.geodb' % root,
                     '%s.match' % root,
                     geometry="geometric",
                     boundary="constant",
                     verbose=no)

        # Run the subtraction
        check_exist('%s.sub.fits' % root, 'w', clobber=clobber)
        if os.path.exists("stamps.lis"):
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf stamps.lis -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (
                root, refimage, root, satval, AP_GAIN, AP_READN, satval,
                FL_GAIN, FL_READN, kernel, dspace, dbkg, root, nstamps,
                nstamps)
        else:
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf %s.stamps -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (
                root, refimage, root, satval, AP_GAIN, AP_READN, satval,
                FL_GAIN, FL_READN, kernel, root, dspace, dbkg, root, nstamps,
                nstamps)
            #cmd = '$REDUCTION/pois -k %i %i -s %.2f %.2f -S %s.stamps %s %s.shift.fits %s.sub.fits' % (kernel,kernel,satval,satval,root,refimage,root,root)
        fcmd = os.popen(cmd, 'r')
        sublines = fcmd.readlines()
        fcmd.close()

    # Return
    print 'Exiting successfully'
    return