Пример #1
0
 def imgShift(self, fn_in, fn_out, dxdy):
     try:
         print("imshift %s %s\n" % (fn_in, fn_out))
         iraf.imshift(fn_in, fn_out, dxdy.x, dxdy.y, shifts_file="", \
                      interp_type="linear", boundary_typ="nearest")
         history_str = "Imshifted (%f, %f)" % (dxdy.x, dxdy.y)
         self.addFitsHistory(fn_out, history_str)
     except:
         sys.stderr.write("Error in imshift: %s\n" % (fn_in))
Пример #2
0
 def imshift(self, file, output, dx, dy, overwrite=True):
     """Shifts a given fits file with given dx and dy"""
     try:
         iraf.imshift.unlearn()
         if self.fop.is_file(output) and overwrite:
             self.logger.warning("Over Writing file({})".format(output))
             self.fop.rm(output)
         iraf.imshift(input=file, output=output, x=dx, y=dy)
         return True
     except Exception as e:
         self.logger.error(e)
         return False
Пример #3
0
def image_shift(targetdir):

    """
    Shift all B-A images by 48 pixels in y-direction
    """

    print 'Target directory is ' + targetdir
    print 'Shifting images...'

    innames, outnames = [], []

    for n in os.listdir(targetdir):
        if (n.endswith('.fit')) & (n.startswith('dfcr')):
            outnames.append(os.path.join(targetdir,'s' + n ) )
            innames.append(os.path.join(targetdir,n) )
            if os.path.exists( os.path.join(targetdir,'s' + n) ):
                os.remove(os.path.join(targetdir,'s' + n))
                print 'Removing file ' + os.path.join(targetdir,'s' + n)

    with open(os.path.join(targetdir,'input.list'), 'w') as f:
        for name in innames:
            print name.replace(targetdir + '/','')
            f.write( name + '\n' )

    with open(os.path.join(targetdir,'output.list'), 'w') as f:
        for name in outnames:
            f.write( name + '\n' )

    names = np.array([name.replace('dfcr','r') + '[1]' for name in innames])

    decoff = np.asarray([iraf.hselect(f , 'DECOFF', 'yes', Stdout=1)[0] for f in names],dtype='float')

    with open(os.path.join(targetdir,'shifts.lst'),'w') as f:
        for d in decoff:
            if d >= -1.0:
                yshift = '0.0'
            else:
                yshift = '-48.0'
            f.write('0.0 ' + yshift + '\n')

    iraf.images(_doprint=0)
    iraf.imgeom(_doprint=0)

    iraf.imshift.setParam('input', '@' + os.path.join(targetdir, 'input.list'))
    iraf.imshift.setParam('output', '@' + os.path.join(targetdir, 'output.list'))
    iraf.imshift.setParam('shifts_file', os.path.join(targetdir,'shifts.lst') )

    iraf.imshift()

    return None
Пример #4
0
def shift_align(filenames):
    if len(filenames) == 0:
        return ''
    fno = 1
    for i in filenames:
        iraf.display(image = i, frame = fno)
        fno += 1
    print 'The fits number is', len(filenames)
    if len(filenames) > 1:
        print 'Please check the fits pic and determine whether you need to shift the pic'
        print 'imexamine...'
        iraf.imexamine()
        valget = raw_input('Are you need to shift the fits picture?(y)')
        if valget == 'y' or valget == 'Y':
            nfilenames = [filenames[0]]
            for i2 in filenames:
                print 'implot ' + i2
                iraf.implot(image = i2)
            fno = 1
            iraf.immatch()
            while fno < len(filenames):
                valget = raw_input('Please input ' + filenames[fno] + ' xshift, yshift: ')
                valget = valget.split()
                fxshift = float(valget[0])
                fyshift = float(valget[1])
                ims_key = 1
                if os.path.isfile('shift_' + filenames[fno]):
                    valget = raw_input('shift_' + filenames[fno] + ' is already exist, are you want to delete the old one?(y):')
                    if valget == 'y' or valget == 'Y' or valget == 'yes' or valget == 'Yes':
                        os.remove('shift_' + filenames[fno])
                    else:
                        ims_key = 0
                if ims_key == 1:
                    print 'what happend????????????????'
                    iraf.imshift(input = filenames[fno]
                            , output = 'shift_' + filenames[fno], xshift = fxshift
                            , yshift = fyshift, shifts_file = '', interp_type = 'linear'
                            , boundary_type = 'nearest', constant = 0.0)
                nfilenames.append('shift_' + filenames[fno])
                fno += 1
            for i2 in xrange(len(nfilenames)):
                iraf.display(image = nfilenames[i2], frame = i2 + 1)
            iraf.imexamine()
            return cos_clear(nfilenames)
    return cos_clear(filenames)
Пример #5
0
def combine(doreduce=True, doshifts=True):

    if doreduce:
        ims = glob('oc01020[1-4]*_raw.fits')
        ims += glob('oc016*_raw.fits')
        # for each image
        for im in ims:
            print im
            stistools.basic2d.basic2d(im, im.replace('raw', 'flt'))
            im = im.replace('raw', 'flt')
            print im
            # Update the target position at 0.0
            for i in range(4):
                pyfits.setval(im, 'POSTARG2', value=0.0, ext=i)
            # reset the aperture table to the newer file (we maybe should check this)
            pyfits.setval(im, 'APERTAB', value='oref$y2r1559to_apt.fits')
            # Reset the wcs to have CRPIX2 along the trace

            # Run x2d on the flt frame

            stistools.x2d.x2d(input=im, output=im.replace('flt', 'x2d'))

            h = pyfits.open(im.replace('flt', 'x2d'), mode='update')

            # Replace all of the bad pixels in the image by -10000 based on the DQ array
            # save them to a new file
            # Throw away bad reference file pixels and saturated pixels. None of the other error codes
            # were in the first file so I haven't included them here, but we might want to
            d = h[3].data
            badpix = logical_and(
                bitwise_and(d, 256) == 256,
                bitwise_and(d, 512) == 512)
            h[1].data[badpix] = -10000
            h.flush()

            # Trim the image
            for i in range(1, 4):
                h[i].data = h[i].data[100:-100, 120:-100]
                h[i].header['CRPIX1'] -= 120
                h[i].header['CRPIX2'] -= 100
            h.close()

    ims = glob('oc01020[1-4]*_x2d.fits')
    ims += glob('oc01610[1-2]*_x2d.fits')
    if doshifts:
        init_guesses = [501, 542, 522, 523, 541, 524]
        centroids = []
        for i, im in enumerate(ims):
            print(im)
            h = pyfits.open(im)
            d = average(h[1].data[:, 915:925], axis=1)
            popt, _pcov = curve_fit(gauss,
                                    arange(len(d)),
                                    d,
                                    p0=[10, init_guesses[i], 1.5, 0])
            centroids.append(popt[1])
            shift = centroids[0] - popt[1]
            from matplotlib import pyplot as pl
            pl.ion()
            pl.clf()
            pl.plot(arange(len(d)), d)
            pl.plot(arange(len(d)),
                    gauss(arange(len(d)), popt[0], popt[1], popt[2], popt[3]))
            _w = raw_input('Press return to continue')
            # watch the sign convention
            # This gives what you need to shift the input image by to get to the reference image
            iraf.unlearn(iraf.imshift)
            iraf.imshift(im + '[1]',
                         im[:-8] + 'shift1.fits',
                         0.0,
                         shift,
                         interp_type='drizzle')

    # Run imcombine on the rectified (but not flux scaled) images with crreject
    iraf.unlearn(iraf.imcombine)
    imlist = ''
    for im in ims:
        imlist += im[:-8] + 'shift1.fits,'
    # drop the last comma
    imlist = imlist[:-1]
    iraf.imcombine(input=imlist,
                   output='13dh_uv_com.fits',
                   reject='none',
                   lthreshold=-20,
                   hthreshold=300)

    # run apall on the combined flux scaled image
    iraf.unlearn(iraf.apall)
    iraf.apall(input='13dh_uv_com.fits',
               output='13dh_uv',
               review='no',
               line=1024,
               nsum=-50,
               b_order=2,
               b_function='legendre',
               b_niterate=30,
               b_naverage=-21,
               nfind=1,
               t_order=2,
               background='fit',
               weights='variance')
Пример #6
0
def p60sub(refimage, inlist, ra=-1, dec=-1, dx=-1, dy=-1, \
 nsx=5, nsy=5, sx=1, sy=1, sat1=60000, sat2=60000, min=5, ms=130, dback=1, \
 dspace=2, hms=9, hss=15, thresh=20, seeing=5.0, scale=0.0, tol=0.005, \
 matchlist="", goodmatch=2.0, aorder=2, nstars=30, outim="", MAX=900, \
 order=3, clobber=globclob):
    """ subtraction pipeline for one object/filter P60 data """

    # Parse inputs
    infiles = iraffiles(inlist)

    # Find location (in pixels) of central object if specified
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    if not ((ra == -1) and (dec == -1)):
        [xcenpix, ycenpix] = imwcs2pix(refimage, ra, dec)
    else:
        xcenpix = (nx / 2.0)
        ycenpix = (ny / 2.0)
        [ra, dec] = impix2wcs(refimage, xcenpix, ycenpix)

    # Trim the reference image
    if (dx == -1):
        xl = 1
        xu = nx
    else:
        xl = int(xcenpix - (dx / 2.0))
        xu = int(xcenpix + (dx / 2.0))
    if (dy == -1):
        yl = 1
        yu = ny
    else:
        yl = int(ycenpix - (dy / 2.0))
        yu = int(ycenpix + (dy / 2.0))

    check_exist('t' + refimage, 'w', clobber)
    iraf.imcopy(refimage + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
  't' + refimage)

    # Big Loop
    for image in infiles:

        # Separate image rootname from extension
        imageroot = image.split('.')[0]

        # Check to make sure the WCS fit was successful
        if not check_head(image, 'IQWCS'):
            print 'Skipping image %s: IQWCS not successful' % image
            infiles.remove(image)
            continue
        # If so, then shift all the images appropriately
        else:
            xsh = xcenpix - imwcs2pix(image, ra, dec)[0]
            ysh = ycenpix - imwcs2pix(image, ra, dec)[1]
            check_exist('s' + image, 'w', clobber)
            iraf.imshift(image, 's' + image, xsh, ysh)

        # Make sure sky subtraction is not done
        [skysub, skybkg] = get_head(image, ['SKYSUB', 'SKYBKG'])
        if (skysub == 1):
            iraf.imarith('s' + image, '+', skybkg, 's' + image)

        # Trim the images
        check_exist('ts' + image, 'w', clobber)
        iraf.imcopy('s' + image + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
         'ts' + image)

        # Cosmic Ray Cleaning
        iraf.lacos_im('ts%s' % image, 'cts%s' % image, 'ts%s_crmask' % \
         imageroot, gain=2.2, readn=5.3, niter=3)

        # Register the images
        os.system(
            '$REDUCTION/imregister_new.pl t%s cts%s -sat %i -thresh %i -s %f -scale %f -tol %f %s -o %i -g %i -n %i -out cts%s.shift.fits -max %i'
            % (refimage, image, sat1, thresh, seeing, scale, tol, matchlist,
               order, goodmatch, nstars, imageroot, MAX))

        # Write the configuration parameters for subtraction
        pfile = open('default_config', 'w+')
        pfile.write('nstamps_x		%i\n' % nsx)
        pfile.write('nstamps_y		%i\n' % nsy)
        pfile.write('sub_x		%i\n' % sx)
        pfile.write('sub_y		%i\n' % sy)
        pfile.write('half_mesh_size	%i\n' % hms)
        pfile.write('half_stamp_size	%i\n' % hss)
        pfile.write('deg_bg		%i\n' % dback)
        pfile.write('saturation1	%f\n' % sat1)
        pfile.write('saturation2        %f\n' % sat2)
        pfile.write('pix_min		%i\n' % min)
        pfile.write('min_stamp_center  	%i\n' % ms)
        pfile.write('ngauss		3\n')
        pfile.write('deg_gauss1		6\n')
        pfile.write('deg_gauss2		4\n')
        pfile.write('deg_gauss3		2\n')
        pfile.write('sigma_gauss1	0.7\n')
        pfile.write('sigma_gauss2	1.5\n')
        pfile.write('sigma_gauss3	2.0\n')
        pfile.write('deg_spatial	%i\n' % dspace)
        pfile.write('reverse		0\n')
        pfile.write('stampsbyxy		0\n')
        pfile.close()

        # Run the subtraction
        os.system("$REDUCTION/ISIS/bin/mrj_phot t%s cts%s.shift.fits" \
         % (refimage, imageroot))

        # Clean up files
        os.system("mv conv.fits cts%s.shift.sub.fits" % imageroot)
        os.system("mv conv0.fits cts%s.shift.conv.fits" % imageroot)

    # Return
    print 'Exiting successfully'
    return
Пример #7
0
def psf_construct(img,
                  x,
                  y,
                  halosize,
                  norm=None,
                  magnify=1,
                  pixel_scale=0.168,
                  sub_indiv_bkg=False,
                  show_figure=False,
                  verbose=False):

    from pyraf import iraf
    from iraf import stsdas
    from iraf import analysis
    from iraf import isophote
    from iraf import bmodel
    from sep import Background

    from .image import save_to_fits, extract_obj, seg_remove_cen_obj

    if not os.path.isdir('./temp/'):
        os.mkdir('./temp/')
    # Delete `fits` file first
    iraf.imdel('./temp/_halo*.fits')

    x_int = x.astype(np.int)
    y_int = y.astype(np.int)
    dx = -1.0 * (x - x_int)
    dy = -1.0 * (y - y_int)

    halosize = int(halosize)

    # Note that stars near the boundary will have problems when making cutout. So we pad the image here.
    padsize = 40
    ny, nx = img.shape
    im_padded = np.zeros((ny + 2 * padsize, nx + 2 * padsize))
    # Making the left edge empty
    im_padded[padsize:ny + padsize, padsize:nx + padsize] = img

    halo_i = (im_padded[y_int + padsize - halosize:y_int + padsize + halosize +
                        1, x_int + padsize - halosize:x_int + padsize +
                        halosize + 1])
    if verbose:
        print('### Cuting out the star and masking out contaminations ###')
    # Build mask to move out contaminations
    psf_raw = halo_i.byteswap().newbyteorder()
    from astropy.convolution import convolve, Box2DKernel
    psf_blur = convolve(abs(psf_raw), Box2DKernel(1.5))
    psf_objects, psf_segmap = extract_obj(abs(psf_blur),
                                          b=5,
                                          f=4,
                                          sigma=5.5,
                                          minarea=2,
                                          pixel_scale=pixel_scale,
                                          deblend_nthresh=32,
                                          deblend_cont=0.0001,
                                          sky_subtract=False,
                                          show_fig=show_figure,
                                          verbose=verbose)

    # remove central object
    psf_segmap = seg_remove_cen_obj(psf_segmap)
    psf_mask = (psf_segmap != 0).astype(float)
    save_to_fits(psf_mask, './temp/_halo_mask.fits')
    # Here I subtract local sky, so that the final image will not be too negative.
    if sub_indiv_bkg is True:
        # Evaluate local sky backgroud within `halo_i`
        # Actually this should be estimated in larger cutuouts.
        # So make another cutout (larger)!
        psf_raw = psf_raw.byteswap().newbyteorder()
        bk = Background(psf_raw, psf_segmap != 0)
        glbbck = bk.globalback
        if verbose:
            print('# Global background: ', glbbck)
        save_to_fits(halo_i - glbbck, './temp/_halo_img.fits')  #
    else:
        save_to_fits(halo_i, './temp/_halo_img.fits')  #

    # Shift halo_i to integer grid
    iraf.imshift('./temp/_halo_img.fits',
                 './temp/_halo_img_shift.fits',
                 dx,
                 dy,
                 interp_type='poly3',
                 boundary='nearest')
    iraf.imshift('./temp/_halo_mask.fits',
                 './temp/_halo_mask_shift.fits',
                 dx,
                 dy,
                 interp_type='poly3',
                 boundary='nearest')

    if not isinstance(magnify, int):
        raise TypeError('# "magnify" must be positive integer less than 10!')
    elif magnify != 1 and magnify > 10:
        raise TypeError('# "magnify" must be positive integer less than 10!')
    elif magnify == 1:
        iraf.imcopy('./temp/_halo_img_shift.fits',
                    './temp/_halo_i_shift.fits',
                    verbose=verbose)
        iraf.imcopy('./temp/_halo_mask_shift.fits',
                    './temp/_halo_m_shift.fits',
                    verbose=verbose)
    else:
        iraf.magnify('./temp/_halo_img_shift.fits',
                     './temp/_halo_i_shift.fits',
                     magnify,
                     magnify,
                     interpo='poly3',
                     bound='refl',
                     const=0.)
        iraf.magnify('./temp/_halo_mask_shift.fits',
                     './temp/_halo_m_shift.fits',
                     magnify,
                     magnify,
                     interpo='poly3',
                     bound='refl',
                     const=0.)

    return psf_raw, psf_mask  #, x_int, y_int, dx, dy
Пример #8
0
def mkcombmask(combimg,
               inlist,
               sspref='sdfr',
               outpref='mc',
               minpix=250,
               hsigma=3,
               lsigma=10,
               conv='block 3 3',
               bpm='none',
               sigmap='none'):

    # load IRAF package
    iraf.nproto()

    # chceck combined image
    if not os.access(combimg, os.R_OK):
        print >> sys.stderr, 'cannot open combined image (%s)' % combimg
        return 1

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

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

    if lsigma <= 0:
        print >> sy.stderr, 'invalid lsigma value (%f)' % lsigma
        return 1

    if hsigma <= 0:
        print >> sys.stderr, 'invalid hsigma value (%f)' % hsigma
        return 1

    # check convolution kernel name
    ret = 1
    param = conv.split()
    if len(param) == 1:
        if os.access(param[0], os.R_OK):
            conv = '@' + conv
            ret = 0
        else:
            print >> sys.stderr, 'convolution kernel file (%s) does not exist' % param[
                0]
    elif param[0] == 'block' and len(param) == 3:
        if param[1].isdigit() and param[2].isdigit():
            ret = 0
    elif param[0] == 'bilinear' and len(param) == 3:
        if param[1].isdigit() and param[2].isdigit() and int(
                param[1]) > 0 and int(param[2]) > 0:
            ret = 0
    elif param[0] == 'gauss' and len(param) == 5:
        if param[1].isdigit() and param[2].isdigit() and int(
                param[1]) > 0 and int(param[2]) > 0:
            if isfloat(param[3]) and isfloat(
                    param[4]) and float(param[3]) > 0 and float(param[4]) > 0:
                ret = 0

    if ret > 0:
        print >> sys.stderr, 'invalid convolve parameter (%s)' % conv
        return 1

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

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

    # check sigma map
    if sigmap.lower() == 'none':
        tmp_sigma = ''
        tmp_mask = ''
    else:
        if os.access(sigmap, os.R_OK) == False:
            print >> sys.stderr, 'cannot read sigma map image (%s)' % sigmap
            return 1
        else:
            tmp_mask = tmp_prefix + 'mask' + os.path.basename(sigmap)
            tmp_sigma = tmp_prefix + 'sigma' + os.path.basename(sigmap)
            iraf.imcopy(sigmap, tmp_sigma, verbose='no')
            ret = iraf.imstat(sigmap,
                              nclip=50,
                              field='mode',
                              format='no',
                              Stdout=1)
            sigma_mode = float(ret[0])
            iraf.imreplace(tmp_sigma, sigma_mode, upper=0)
            iraf.imarith(sigmap, '*', -1, tmp_mask, verbose='no')
            iraf.imreplace(tmp_mask, 1, lower=0)
            iraf.imreplace(tmp_mask, 0, upper=0)
            iraf.imarith(tmp_mask, '*', -1, tmp_mask)
            iraf.imarith(tmp_mask, '+', 1, tmp_mask)
    #
    tmp_precombmask = tmp_prefix + 'premask' + os.path.basename(combimg)
    tmp_combmask = tmp_prefix + 'mask' + os.path.basename(combimg)
    iraf.unlearn('objmasks')
    try:
        iraf.objmasks(combimg,
                      tmp_precombmask,
                      omtype='boolean',
                      masks='',
                      convolve=conv,
                      lsigma=lsigma,
                      hsigma=hsigma,
                      hdetect='yes',
                      ldetect='yes',
                      minpix=minpix,
                      sigmas=tmp_sigma)
        iraf.objmasks(combimg,
                      tmp_combmask,
                      omtype='boolean',
                      masks=tmp_precombmask + '[pl]',
                      convolve=conv,
                      lsigma=lsigma,
                      hsigma=hsigma,
                      hdetect='yes',
                      ldetect='yes',
                      minpix=minpix,
                      sigmas=tmp_sigma)
        os.remove(tmp_precombmask)
    except:
        print >> sys.stderr, 'failed to execute iraf objmasks task for %s' % combimg
        remove_temp_all(tmp_prefix)
        return 1

    # remove the area with no sigma value
    tmp_combmask2 = tmp_prefix + 'mask2' + os.path.basename(combimg)
    if sigmap.lower() == 'none':
        iraf.imcopy(tmp_combmask + '[pl]', tmp_combmask2, verbose='no')
    else:
        iraf.imarith(tmp_combmask + '[pl]',
                     '*',
                     tmp_mask,
                     tmp_combmask2,
                     verbose='no')
    os.remove(tmp_combmask)

    # check if input images exist and fix bad pixel if requested
    iraf.unlearn('imcopy')
    iraf.unlearn('imshift')
    for i in range(len(inimg_arr)):

        # get offset values from header
        im = pyfits.open(inimg_arr[i])
        try:
            dx = float(im[0].header['dx'])
            dy = float(im[0].header['dy'])
            dxc = float(im[0].header['dxc'])
            dyc = float(im[0].header['dyc'])
            nx = int(im[0].header['NAXIS1'])
            ny = int(im[0].header['NAXIS2'])
        except:
            print >> sys.stderr, 'failed to get offset values from the header of %s' % inimg_arr[
                i]
            remove_temp_all(tmp_prefix)
            return 1
        im.close()

        # shift combined mask
        tmp_shiftimg = tmp_prefix + os.path.basename(outimg_arr[i])
        iraf.imshift(tmp_combmask2,
                     tmp_shiftimg,
                     dxc,
                     dyc,
                     boundary_typ='constant',
                     constant=0.0)
        tmp_shiftimg_reg = '%s[1:%d,1:%d]' % (tmp_shiftimg, nx, ny)
        #iraf.imcopy(tmp_shiftimg_reg, outimg_arr[i]+'[pl]',verbose='no')
        iraf.imcopy(tmp_shiftimg_reg, outimg_arr[i], verbose='no')
        if bpmex == 1:
            iraf.imarith(outimg_arr[i], '+', bpm, outimg_arr[i], verbose='no')
        convert_maskfits_int(outimg_arr[i], outimg_arr[i])
        os.remove(tmp_shiftimg)

    # remove all temporary files
    remove_temp_all(tmp_prefix)

    return 0
Пример #9
0
def combine(doreduce=True, doshifts=True):

    if doreduce:
        ims = glob('oc01020[1-4]*_raw.fits')
        ims += glob('oc016*_raw.fits')
        # for each image
        for im in ims:
            print im
            stistools.basic2d.basic2d(im, im.replace('raw','flt'))
            im = im.replace('raw', 'flt')
            print im
            # Update the target position at 0.0
            for i in range(4):
                pyfits.setval(im, 'POSTARG2', value=0.0, ext=i)
            # reset the aperture table to the newer file (we maybe should check this)
            pyfits.setval(im, 'APERTAB', value='oref$y2r1559to_apt.fits')
            # Reset the wcs to have CRPIX2 along the trace

            # Run x2d on the flt frame

            stistools.x2d.x2d(input=im, output=im.replace('flt','x2d') )

            h = pyfits.open(im.replace('flt','x2d'), mode='update')

            # Replace all of the bad pixels in the image by -10000 based on the DQ array
            # save them to a new file
            # Throw away bad reference file pixels and saturated pixels. None of the other error codes
            # were in the first file so I haven't included them here, but we might want to
            d = h[3].data
            badpix = logical_and(bitwise_and(d, 256) == 256,
                                 bitwise_and(d, 512) == 512)
            h[1].data[badpix] = -10000
            h.flush()

            # Trim the image
            for i in range(1,4):
                h[i].data = h[i].data[100:-100, 120:-100]
                h[i].header['CRPIX1'] -= 120
                h[i].header['CRPIX2'] -= 100
            h.close()

    ims = glob('oc01020[1-4]*_x2d.fits')
    ims += glob('oc01610[1-2]*_x2d.fits')
    if doshifts:
        init_guesses = [501, 542, 522, 523, 541, 524]
        centroids = []
        for i, im in enumerate(ims):
            print(im)
            h = pyfits.open(im)
            d = average(h[1].data[:, 915:925], axis=1)
            popt, _pcov = curve_fit(gauss, arange(len(d)), d, p0=[10, init_guesses[i], 1.5, 0])
            centroids.append(popt[1])
            shift = centroids[0] - popt[1]
            from matplotlib import pyplot as pl
            pl.ion()
            pl.clf()
            pl.plot(arange(len(d)), d)
            pl.plot(arange(len(d)), gauss(arange(len(d)), popt[0], popt[1], popt[2], popt[3]))
            _w = raw_input('Press return to continue')
            # watch the sign convention
            # This gives what you need to shift the input image by to get to the reference image
            iraf.unlearn(iraf.imshift)
            iraf.imshift(im + '[1]', im[:-8] + 'shift1.fits', 0.0, shift,
                         interp_type='drizzle')

    # Run imcombine on the rectified (but not flux scaled) images with crreject
    iraf.unlearn(iraf.imcombine)
    imlist = ''
    for im in ims:
        imlist += im[:-8] + 'shift1.fits,'
    # drop the last comma
    imlist = imlist[:-1]
    iraf.imcombine(input=imlist, output='13dh_uv_com.fits', reject='none',
                   lthreshold=-20, hthreshold=300)

    # run apall on the combined flux scaled image
    iraf.unlearn(iraf.apall)
    iraf.apall(input='13dh_uv_com.fits', output='13dh_uv', review='no',
               line=1024, nsum=-50, b_order=2, b_function='legendre',
               b_niterate=30, b_naverage=-21, nfind=1, t_order=2,
               background='fit', weights='variance')
Пример #10
0
def subract(df_image,shifts=99.99,width_cfhtsm=0.,upperlim=0.04,medphotosc=True,numsources=100,sigmaclip=False,cutout=False):
    print "\n************ Running the subtraction steps ************\n"

    #iraf.imdel('_psf*.fits')
    #iraf.imdel('_model*.fits')
    #iraf.imdel('_df_sub')
    iraf.imdel('_res*.fits')
    iraf.imdel('cc_images')
    iraf.imdel('_model_sh')
    iraf.imdel('_model_sc')
    iraf.imdel('_model_mask')
    iraf.imdel('_model_maskb')
    
    'shift the images so they have the same physical coordinates'
    if shifts[0]==99.99:
        print('\n**** Calculating shifts... ****\n')
        iraf.stsdas.analysis.dither.crossdriz('_df_sub.fits','_model.fits','cc_images',dinp='no',dref='no')
        iraf.stsdas.analysis.dither.shiftfind('cc_images.fits','shift_values')
        x_shift=0
        y_shift=0
        with open('shift_values','r') as datafile:
            line = datafile.read().split()
            x_shift = float(line[2])
            y_shift = float(line[4])
        print('Shifting model image by x and y: '+str(x_shift)+','+str(y_shift))
         
        iraf.imshift('_model','_model_sh',0-x_shift,0-y_shift)
    else:
        print('Shifting model image by x and y: '+str(shifts[0])+','+str(shifts[1]))
        iraf.imshift('_model','_model_sh',shifts[0],shifts[1])

    'scale the model so that roughly the same as the _df_sub image'
#    if usemodelpsf:
#        iraf.imarith('_model_sh','/',16.,'_model_sc')
#    else:
    'the photometric step, matching the images to each other. '
    'in principle this comes from the headers - both datasets are calibrated,'
    'so this multiplication should be something like 10^((ZP_DF - ZP_CFHT)/-2.5)'
    '(perhaps with a correction for the difference in pixel size - depending'
    'on what wregister does - so another factor (PIX_SIZE_DF)^2/(PIX_SIZE_CFHT)^2'
    
    avgphotosc,medianphotosc = getphotosc('_model_sh.fits','_df_sub.fits',numsources=numsources,sigmaclip=sigmaclip,cutout=cutout)
    if medphotosc:
        photosc = medianphotosc
        print 'Using median calculated photosc: %s'%photosc
    else:
        photosc = avgphotosc
        print 'Using average calculated photosc: %s'%photosc
    iraf.imarith('_model_sh','*',photosc,'_model_sc')

    iraf.imdel('_df_ga.fits')
    
    'correction for the smoothing that was applied to the CFHT'
    if width_cfhtsm != 0:
        iraf.gauss('_df_sub','_df_ga',width_cfhtsm) # iraf.gauss('%s'%df_image,'_df_ga',width_cfhtsm)
    else:
        print 'WARNING: No smoothing applied to the dragonfly image before subtracting the model.'
        iraf.imcopy('_df_sub.fits','_df_ga.fits')

    'subtract the model from the dragonfly cutout'
    iraf.imarith('_df_ga','-','_model_sc','_res')

    'duplicate the residual so that we can redo the masking step later'
    iraf.imcopy('_res.fits','_res_org.fits')

    'mask the image to get block out stuff that didnt subtract nicely'
    mask('_res',upperlim=upperlim)

    return None
Пример #11
0
	if i == 0:
		x0 = g[0][0]
		y0 = g[0][1]
	f.write('%d %d\n' % (round(x0-g[0][0],0),round(y0-g[0][1],0)))
if i != len(images)-1:
	iraf.disp(images[-1][0:-1],1)
	os.system('rm tmpcoords')
	iraf.imexam(Stdout = 'tmpcoords')
	g = numpy.loadtxt('tmpcoords')
	f.write('%d %d\n' % (round(x0-g[0][0],0),round(y0-g[0][1],0)))
f.close()
os.system('rm tmpcoords')

print '\n Aligning... \n'

# Shift the images
os.system('rm aligned_list')
f = open('aligned_list','a')
for i in range(0,len(images)):
	indx = images[i][::-1].index('/') 
	image = images[i][-indx:-1]
	f.write('al_%s\n' % image)
f.close()
os.system('rm al_red*')
iraf.imshift('@imagelist','@aligned_list',shifts='shifts')
os.system('mv al_red_* ../SCIENCE/')




Пример #12
0
	print('                 X offset : {0:.3f} pix, Y offset : {1:.3f} pix'.format(offset_X, offset_Y))

	f.write("{0:.3f}  {1:.3f} \n".format(offset_X, offset_Y))
f.close()


# ----- Shifting & combining Ha sum images ----- #

# Shifting w/ IRAF/imshift task
iraf.images()
iraf.imgeom()
iraf.unlearn('imshift')

os.system('ls -1 Ha_sum-*.fits > input_Ha_sum.lis')
os.system('rm -rfv al1_Ha_sum*.fits')
iraf.imshift(input='@input_Ha_sum.lis', output='al1_//@input_Ha_sum.lis', shifts_file='offset.txt',
             interp_type='linear', boundary_type='nearest')

al1_list = sorted(glob.glob('al1_Ha_sum*.fits'))

f1 = open('al1_comb1.lis', 'w')
f2 = open('al1_comb2.lis', 'w')
for i in np.arange(len(ic.cube_list)):
	cb = ic.cube_list[i].split('/')[-1].split('cstxeqxbrg')[-1].split('_3D.fits')[0]
	# cb = ic.cube_list[i].split('_3D.fits')[0].split(ic.dir_redux+'cstxeqxbrg')[1]
	if (cb not in ic.cube_spa_off):
		f1.write(al1_list[i]+'\n')
	else:
		f2.write(al1_list[i]+'\n')
f1.close()
f2.close()
Пример #13
0
def p60sub(refimage, inlist, ra=-1, dec=-1, dx=-1, dy=-1, \
 nsx=5, nsy=5, sx=1, sy=1, sat1=60000, sat2=60000, min=5, ms=130, dback=1, \
 dspace=2, hms=9, hss=15, thresh=20, seeing=5.0, scale=0.0, tol=0.005, \
 matchlist="", goodmatch=2.0, aorder=2, nstars=30, outim="", MAX=900, \
 order=3, clobber=globclob):

    """ subtraction pipeline for one object/filter P60 data """

    # Parse inputs
    infiles = iraffiles(inlist)

    # Find location (in pixels) of central object if specified
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    if not ((ra == -1) and (dec == -1)):
	[xcenpix, ycenpix] = imwcs2pix(refimage, ra, dec)
    else:
	xcenpix = (nx / 2.0)
	ycenpix = (ny / 2.0)
	[ra, dec] = impix2wcs(refimage, xcenpix, ycenpix)

    # Trim the reference image 
    if (dx == -1):
	xl = 1
	xu = nx
    else:
        xl = int(xcenpix - (dx / 2.0))
        xu = int(xcenpix + (dx / 2.0))
    if (dy == -1):
	yl = 1
	yu = ny
    else:
	yl = int(ycenpix - (dy / 2.0))
        yu = int(ycenpix + (dy / 2.0))
        
    check_exist('t' + refimage, 'w', clobber)
    iraf.imcopy(refimage + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
	 't' + refimage)

    # Big Loop
    for image in infiles:

	# Separate image rootname from extension
	imageroot = image.split('.')[0]

     	# Check to make sure the WCS fit was successful
	if not check_head(image, 'IQWCS'):
	    print 'Skipping image %s: IQWCS not successful' % image
	    infiles.remove(image)
	    continue
	# If so, then shift all the images appropriately
	else:
	    xsh = xcenpix - imwcs2pix(image, ra, dec)[0]
	    ysh = ycenpix - imwcs2pix(image, ra, dec)[1]
	    check_exist('s' + image, 'w', clobber)
            iraf.imshift(image, 's' + image, xsh, ysh)
	
	# Make sure sky subtraction is not done
	[skysub, skybkg] = get_head(image, ['SKYSUB', 'SKYBKG'])
	if (skysub == 1):
	    iraf.imarith('s'+image,'+',skybkg,'s'+image)

	# Trim the images
	check_exist('ts' + image, 'w', clobber)
	iraf.imcopy('s' + image + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
	 'ts' + image)

	# Cosmic Ray Cleaning
	iraf.lacos_im('ts%s' % image, 'cts%s' % image, 'ts%s_crmask' % \
	 imageroot, gain=2.2, readn=5.3, niter=3)

	# Register the images
	os.system('$REDUCTION/imregister_new.pl t%s cts%s -sat %i -thresh %i -s %f -scale %f -tol %f %s -o %i -g %i -n %i -out cts%s.shift.fits -max %i' % (refimage, image, sat1, thresh, seeing, scale, tol, matchlist, order, goodmatch, nstars, imageroot, MAX))
	
	# Write the configuration parameters for subtraction
	pfile = open('default_config', 'w+')
	pfile.write('nstamps_x		%i\n' % nsx)
	pfile.write('nstamps_y		%i\n' % nsy)
	pfile.write('sub_x		%i\n' % sx)
	pfile.write('sub_y		%i\n' % sy)
	pfile.write('half_mesh_size	%i\n' % hms)
	pfile.write('half_stamp_size	%i\n' % hss)
	pfile.write('deg_bg		%i\n' % dback)
	pfile.write('saturation1	%f\n' % sat1)
	pfile.write('saturation2        %f\n' % sat2)
	pfile.write('pix_min		%i\n' % min)
	pfile.write('min_stamp_center  	%i\n' % ms)
	pfile.write('ngauss		3\n')
	pfile.write('deg_gauss1		6\n')
	pfile.write('deg_gauss2		4\n')
	pfile.write('deg_gauss3		2\n')
	pfile.write('sigma_gauss1	0.7\n')
	pfile.write('sigma_gauss2	1.5\n')
	pfile.write('sigma_gauss3	2.0\n')
	pfile.write('deg_spatial	%i\n' % dspace)
	pfile.write('reverse		0\n')
	pfile.write('stampsbyxy		0\n')
	pfile.close()

	# Run the subtraction
	os.system("$REDUCTION/ISIS/bin/mrj_phot t%s cts%s.shift.fits" \
	 % (refimage, imageroot))

	# Clean up files
	os.system("mv conv.fits cts%s.shift.sub.fits" % imageroot)
	os.system("mv conv0.fits cts%s.shift.conv.fits" % imageroot)

    # Return
    print 'Exiting successfully'
    return
Пример #14
0
list_temp = []
list_total = []
iraf.cd(path + galname + '/shifts')
for index in range(len(shift_fin)):
    arr1 = shift_fin[index]
    for index2 in range(len(arr1)):
        i_nam = arr1[index2][0]  # Initial name for imshift
        xs = arr1[index2][1]
        ys = arr1[index2][2]
        posa = arr1[index2][3]
        list_in = i_nam.replace('.fits', '.fits[1,overwrite+]')
        list_ot = list_in.replace('x2d', 'shift')
        list_out = list_ot.replace('.fits[1,overwrite+]', '.fits')
        list_temp.append(list_out)
        if not os.path.exists(list_out):
            iraf.imshift(input=list_in, output=list_out, xshift=xs, yshift=ys)
            print('File ' + str(i_nam) + ' changed to ' + str(list_out) + '!')
    list_total.append(list_temp)
    list_temp = []

#
# Now we have to combine the images as long as they have not been created yet
# We use the IRAF task imcombine
# The rejection algorithm would be ccdclip, with weights for combining (TEXPTIME in headers), readnoise = 4.32 and gain = 1.
#
if os.path.exists('*comb.fits'):
    print(
        'Combined images present in this folder. Imcombine will not be applied. Please, delete them if needed! '
    )
if not os.path.exists('*comb.fits'):
    for index3 in range(len(list_total)):
def subract(df_image, psf):
    iraf.imdel('_model*.fits')
    iraf.imdel('_res*.fits')
    iraf.imdel('_df_sub')

    'subtract the sky value from the dragonfly image header'
    df_backval = fits.getheader(df_image)['BACKVAL']
    iraf.imarith('%s' % df_image, '-', '%s' % df_backval, '_df_sub')

    'convolve the model with the Dragonfly PSF'
    if usemodelpsf:
        makeallisonspsf()
        psf = './psf/psf_static_fullframe.fits'
    ### XXX resample the PSF by a factor of 4

    if verbose:
        print 'VERBOSE:  Using %s for the psf convolution.' % psf
    iraf.stsdas.analysis.fourier.fconvolve('_fluxmod_dragonfly', '%s' % psf,
                                           '_model')

    'shift the images so they have the same physical coordinates'
    iraf.stsdas.analysis.dither.crossdriz('_df_sub.fits',
                                          '_model.fits',
                                          'cc_images',
                                          dinp='no',
                                          dref='no')
    iraf.stsdas.analysis.dither.shiftfind('cc_images.fits', 'shift_values')
    x_shift = 0
    y_shift = 0
    with open('shift_values', 'r') as datafile:
        line = datafile.read().split()
        x_shift = float(line[2])
        y_shift = float(line[4])
    print('The shift in x and y are: ' + str(x_shift) + ',' + str(y_shift))
    iraf.imshift('_model', '_model_sh', 0 - x_shift, 0 - y_shift)

    'scale the model so that roughly the same as the _df_sub image'
    if usemodelpsf:
        iraf.imarith(
            '_model_sh', '/', 16., '_model_sc'
        )  ## just trying to match it to the original (below) approximately
    else:
        iraf.imarith(
            '_model_sh', '/', 2422535.2, '_model_sc'
        )  ## difference between the flux of the star used to make the psf in both frames

    iraf.imarith('_model_sc', '*', 1.5, '_model_sc')

    'subtract the model frm the dragonfly cutout'
    iraf.imarith('_df_sub', '-', '_model_sc', '_res')

    '????'
    iraf.imcopy('_model_sc', '_model_mask')
    iraf.imreplace('_model_mask.fits', 0, upper=50)
    iraf.imreplace('_model_mask.fits', 1, lower=0.01)
    iraf.boxcar('_model_mask', '_model_maskb', 5, 5)
    iraf.imreplace('_model_maskb.fits', 1, lower=0.1)
    iraf.imreplace('_model_maskb.fits', 0, upper=0.9)
    iraf.imarith(1, '-', '_model_maskb', '_model_maskb')
    iraf.imarith('_model_maskb', '*', '_res', '_res_final')

    return None
Пример #16
0
shimlst22.close()

xave = sum(x) / len(x)
yave = sum(y) / len(y)
#print x,y sum(x)/len(x), sum(y)/len(y), xave,yave

# shifting of images
print "> shifting images"
iraf.lintran("imexamine.irafl",
             x1=xave,
             y1=yave,
             angle=180,
             Stdout="shiftlis.irafl")  # angle set for SKinakas IR
iraf.imshift("@images_to_shift.irafl",
             output="@shifted_images.irafl",
             shifts_file="shiftlis.irafl",
             boundary_type="constant",
             constant=0.0)

# creating the final image
print "> creating the final combined image,", rname + '_comb.fits'
iraf.imcombine("@shifted_images.irafl",
               output=rname + '_comb.fit',
               combine="median")

# cleaning ...
rmvoption = raw_input(
    'Would you like to remove all intermediate steps? [press "n" for no, else "enter"]\t'
)
if rmvoption == "n" or rmvoption == "N":
    print "\n Enjoy! ;)\n"
Пример #17
0
	os.chdir(working_dir)


# Stop point #1
sys.exit('Split images generated')


# Shifting cubes with offset.txt (IRAF/imshift task)
os.chdir(split_dir)
iraf.unlearn('imshift')
for j in np.arange(np.shape(d_sci_cut)[0]):
	print('Shifting frame {0:04d}'.format(j+1))
	os.system('rm -rfv al1_N*_2D_{0:04d}.fits'.format(j+1))
	os.system('ls -1 N*_2D_{0:04d}.fits > split_{1:04d}.lis'.format(j+1, j+1))
	iraf.sleep(1.0)
	iraf.imshift(input='@split_{0:04d}.lis'.format(j+1), output='al1_//@split_{0:04d}.lis'.format(j+1),
		         shifts_file='../offset.txt', interp_type='linear', boundary_type='nearest')

	os.system('rm -rfv al1_N*_VAR_{0:04d}.fits'.format(j+1))
	os.system('ls -1 N*_VAR_{0:04d}.fits > variance_{1:04d}.lis'.format(j+1, j+1))
	iraf.sleep(1.0)
	iraf.imshift(input='@variance_{0:04d}.lis'.format(j+1), output='al1_//@variance_{0:04d}.lis'.format(j+1),
		         shifts_file='../offset.txt', interp_type='linear', boundary_type='nearest')

os.chdir(working_dir)


# Stop point #2
sys.exit('Shifted images generated.')