コード例 #1
0
ファイル: full_calibrate.py プロジェクト: bjanesh/odi-tools
def trim_img(img,x1,x2,y1,y2):
    """
    Trim a stacked image based on the coordinates given. The image is trimmed
    using ``imcopy`` through pyraf, so the x and y pixel ranges should be given
    in the correct ``imcopy`` format. ``[x1:x2,y1:y2]``

    Parameters
    ---------
    img : str
        String containing name of the image currently in use
    x1 : int
        Pixel coordinate of x1
    x2 : int
        Pixel coordinate of x2
    y1 : int
        Pixel coordinate of y1
    y2 : int
        Pixel coordinate of y2

    Returns
    -------
    img : str
        The new image is given the extension ``.trim.fits``.

    """
    x1,x2 = x1,x2
    y1,y2 = y1,y2
    input = img.nofits()+'['+repr(x1)+':'+repr(x2)+','+repr(y1)+':'+repr(y2)+']'
    output =  img.nofits()+'.trim.fits'
    if not os.path.isfile(output):
        print 'Trimming image: ' ,img
        iraf.unlearn(iraf.imcopy)
        iraf.imcopy(input = input,output = output,verbose='no',mode='h')
コード例 #2
0
ファイル: fitellifunc.py プロジェクト: svn2github/pymorph
def fit_elli(clus_id, line_s):
    #try:
    size = c.size
    values = line_s.split()
    mask_file = 'ell_mask_' + str(imagefile)[:6] + '_'  + str(clus_id) + '.fits'
    image_file = 'image_' + str(imagefile)[:6] + '_'  + str(clus_id) + '.fits' 
    print image_file
    xcntr_o  = float(values[1]) #x center of the object
    ycntr_o  = float(values[2]) #y center of the object
    xcntr = (size/2.0) + 1.0 + xcntr_o - int(xcntr_o)
    ycntr = (size/2.0) + 1.0 + ycntr_o - int(ycntr_o)
    mag    = float(values[7]) #Magnitude
    radius = float(values[9]) #Half light radius
    mag_zero = 25.256 #magnitude zero point
    sky	 = float(values[10]) #sky
    if(float(values[11])>=0 and float(values[11])<=180.0): 
        pos_ang = float(values[11]) - 90.0 #position angle
    if(float(values[11])<0 and float(values[11])>=-180.0):
        pos_ang = 90.0 - abs(float(values[11]))  #position angle
    if(float(values[11])>180 and float(values[11])<=360.0):
        pos_ang = float(values[11]) - 360.0 + 90.0 #position angle
    if(float(values[11])>=-360 and float(values[11])<-180.0):
        pos_ang = float(values[11]) + 360.0 - 90.0 #position angle	
    axis_rat = 1.0 / float(values[12]) #axis ration b/a
    eg = 1 - axis_rat
    if(eg<=0.05):
        eg = 0.07
    major_axis = float(values[14])#major axis of the object
    iraf.imcopy(mask_file, 'image'+str(mask_file)[8:]+'.pl')
    run_elli(image_file, xcntr, ycntr, eg, pos_ang, major_axis)
コード例 #3
0
ファイル: test_basic.py プロジェクト: spacetelescope/pyraf
 def test_imcopy(self):
     iraf.imcopy('dev$pix', 'image.short', verbose=False)
     with fits.open('image.short.fits') as f:
         assert len(f) == 1
         assert f[0].header['BITPIX'] == 16
         assert (f[0].header['ORIGIN'] ==
                 'NOAO-IRAF FITS Image Kernel July 2003')
         assert f[0].data.shape == (512, 512)
コード例 #4
0
ファイル: time_series.py プロジェクト: martindurant/astrobits
def custom1(filename): # for NACO timing mode cubes - removes horizontal banding
    #iraf.imarith(filename,'-','dark','temp')
    iraf.imarith(filename,'/','flatK','temp')
    im = pyfits.getdata('temp.fits')
    med = median(im.transpose())
    out = ((im).transpose()-med).transpose()
    (pyfits.ImageHDU(out)).writeto("temp2.fits",clobber=True)
    iraf.imdel('temp')
    iraf.imcopy('temp2[1]','temp')
コード例 #5
0
ファイル: full_phot.py プロジェクト: sjanowiecki/odi-tools
def trim_img(img):
    x1,x2 = 2508,15798
    y1,y2 = 2216,15506
    input = img[:-5]+'['+repr(x1)+':'+repr(x2)+','+repr(y1)+':'+repr(y2)+']'
    output =  img[:-5]+'.trim.fits'
    if not os.path.isfile(output):
	print 'Trimming image: ' ,img
        iraf.unlearn(iraf.imcopy)
        iraf.imcopy(input = input,output = output,verbose='no',mode='h')
コード例 #6
0
def shiftTrim(files, xi, xf, yi, yf, dx=None, dy=None, offset_x=0, offset_y=0, 
    fnew='', newdir=TRIMDIR, test=False):
    """
    Given initial and final x and y values of shifted stars, will compute shift in x and y and 
    trim files to compensate for shifting image.

    Warning:
    - Assumes that movement across CCD is uniform and predictable in x and y (can be zero)
    - Discard bad images after trim is complete (requires complete series to trim accurately)
    - Only use reduced science images 
    - Pixels are discretely counted

    Parameters:
    ----------------------
    parameter: (dtype) [default (if optional)], information

    xi: (int), initial x value(s) for star(s)
    xf: (int), final x values(s) for star(s)
    yi: (int), initial y value(s) for star(s)
    yx: (int), final y value(s) for star(s)
    dx: (int) [None], overwrite x shift value (will ignore xi,xf)
    dy: (int) [None], overwrite y shift value (will ignore yi,yf)
    fnew: (string) [None], add string to new file name
    newdir: (string) [TRIMDIR], trimmed image directory
    test: (boolean) [False], if True will only print pixel output and not trim files
    ----------------------
    """
    xi = np.array(xi)
    xf = np.array(xf)
    yi = np.array(yi)
    yf = np.array(yf)
    
    num = len(files) - 1
    
    if dx == None:
        dx = _dCalc(xi,xf,num,'x')
        
    if dy == None:
        dy = _dCalc(yi,yf,num,'y')
    
    print 'dx: %s, dy: %s' % (dx,dy)
    for i,f in enumerate(files):

        x = _pixelFinder(dx, i, abs(dx*num), 1, CCDx, offset_x)
        y = _pixelFinder(dy, i, abs(dy*num), 1, CCDy, offset_y)
        
        print "x: %s, y: %s, x*y: %s" % (x, y, ((x[1]-x[0])*(y[1]-y[0])))
        
        f_trim = f + '[%s:%s,%s:%s]' % (x[0],x[1],y[0],y[1])

        if fnew != None:
            f = f + '.' + fnew

        if test == False:
            iraf.imcopy(f_trim, newdir + f)
コード例 #7
0
ファイル: deimos_pipe.py プロジェクト: cenko/python
def deimos_preproc(image):

    '''Take a MEF DEIMOS image, extract the relevant extensions, trim the
    LVM slit masks appropriately, and rename.'''

    # Needing grating
    graname = get_head(image, "GRATENAM", extn=0)
    
    # "Blue" chip
    iraf.imcopy("%s[%i]" % (image, BEXT), "d%s_B.fits" % image[6:10])
    iraf.ccdproc("d%s_B.fits" % image[6:10], overscan=yes, trim=yes,
                 fixpix=yes, biassec=BBIASSEC, trimsec=BTRIMSEC,
                 fixfile="%s_%i.fits" % (MASK, BEXT))
    iraf.imcopy("d%s_B.fits%s" % (image[6:10], DGRATINGS[graname]["blvmreg"]),
                "td%s_B.fits" % image[6:10])
    iraf.imtranspose("td%s_B.fits" % image[6:10], "rtd%s_B.fits" % image[6:10])

    # "Red" chip
    iraf.imcopy("%s[%i]" % (image, REXT), "d%s_R.fits" % image[6:10])
    iraf.ccdproc("d%s_R.fits" % image[6:10], overscan=yes, trim=yes,
                 fixpix=yes, biassec=RBIASSEC, trimsec=RTRIMSEC,
                 fixfile="%s_%i.fits" % (MASK, BEXT))
    iraf.imcopy("d%s_R.fits%s" % (image[6:10], DGRATINGS[graname]["rlvmreg"]),
                "td%s_R.fits" % image[6:10])
    iraf.imtranspose("td%s_R.fits" % image[6:10], "rtd%s_R.fits" % image[6:10])
    iraf.rotate("rtd%s_R.fits" % image[6:10], "rtd%s_R.fits" % image[6:10], 180.0)

    return
コード例 #8
0
def _iraf_dqbits_init(_data):
    """Initialize common IRAF tasks for dqbits tests
    """
    if not HAS_IRAF:
        return

    # imports & package loading
    iraf.stsdas(_doprint=0)
    iraf.imgtools(_doprint=0)
    iraf.artdata(_doprint=0)
    iraf.mstools(_doprint=0)

    # create two data files as input (dont care if appropriate to mscombine)
    iraf.imcopy('dev$pix', _data['dqbits']['input1'])
    iraf.imcopy('dev$pix', _data['dqbits']['input2'])
コード例 #9
0
ファイル: pymorphutils.py プロジェクト: vvinuv/pymorph
def HandleEllipseTask(cutimage, xcntr, ycntr, SizeX, SizeY, sky, out):
    """Running the ellipse task. SizeX, SizeY are the total size"""
    manual_profile = 0
    try:
        raise ImportError() #Temporarily kill this loop as the new flagging does not work with pyraf-functions, yet
        from pyraf import iraf
        from fitellifunc import run_elli
        use_pyraf = 1
    except ImportError:
        use_pyraf = 0
        print 'No pyraf installed!'
        WriteError('Cannot find pyraf installation! Trying manual 1d ' + \
                   'profile finder\n')
    if use_pyraf:
        if out:
            ell_mask_file = 'OEM_' + c.fstring + '.fits'
            ell_out = 'OE_' + c.fstring + '.txt'
        else:
            ell_mask_file = 'EM_' + c.fstring + '.fits'
            ell_out = 'E_' + c.fstring + '.txt'
        plfile = 'GalEllFit.fits.pl'
        CleanEllipse(ell_out, 0)
        try:
            iraf.imcopy(ell_mask_file, plfile, verbose='no')
            iraf.flpr()
        except:
            pass
        try:
            run_elli(cutimage, ell_out, xcntr, ycntr, c.eg, \
                     c.pos_ang, c.major_axis, sky)
            CleanEllipse(ell_out, 1)
            try:
                iraf.flpr()
            except:
                pass
            if exists(ell_out):
                pass
            else:
                manual_profile = 1
        except:
            manual_profile = 1
            WriteError('Error in ellipse task. Trying manual profile finder\n')
            try:
                c.Flag = SetFlag(c.Flag, GetFlag('ELLIPSE_FAIL'))
            except badflag:
                pass
    if use_pyraf == 0 or manual_profile:
        FitEllipseManual(cutimage, xcntr, ycntr, SizeX, SizeY, sky, out)
コード例 #10
0
def zeropadfits(smfits, bigfits, padfits):
    """Pads smfits with zeros to match size of bigfits.
       Result is padfits, centered as was smfits.
       Assumes smfits & bigfits are squares w/ odd # of pixels across.
    """
    NY, NX = fits.getheader(bigfits)["NAXIS2"], fits.getheader(bigfits)["NAXIS1"]
    ny, nx = fits.getheader(smfits)["NAXIS2"], fits.getheader(smfits)["NAXIS1"]
    print "\nPadding 'smfits' at %ix%i to match 'bigfits' at %ix%i\n" % (nx, ny, NX, NY)
    center = (NY + 1) / 2
    border = ny / 2
    lo = center - border
    hi = center + border
    croprange = "[%d:%d,%d:%d]" % (lo, hi, lo, hi)

    imarith(bigfits, "*", 0, padfits)
    imcopy(smfits, padfits + croprange)
コード例 #11
0
ファイル: image.py プロジェクト: jonnybazookatone/imclass
	def trimMyself(self, outname="remcut.fits", region="[400:500,400:500]", verbose=False):
		
                self._logger["trimMyself"] = []

		if self._Name == "None":
			self._logger["trimMyself"].append("No filename set, please check.")

		imCopy = imFits()
		imCopy._Name = outname
		iraf.imcopy("%s%s" % (self._Name,region), imCopy._Name)
		
		if verbose:
			self._logger["trimMyself"].append(iraf.imstat(self._Name))	

			for log in self._logger["trimMyself"]:
				print log

		return imCopy
コード例 #12
0
ファイル: lris_carpy.py プロジェクト: cenko/python
def lris_bpreproc(image):

    '''Take an LRIS blue image, combine into a single extension, subtract overscan,
    trim, and rotate.'''

    iraf.keck()
    iraf.lris()
    
    # Need grating name to start
    grism = get_head("../rawdata/%s" % image, "GRISNAME", extn=0)

    # Convert to single extension fits image
    iraf.multi2simple("../rawdata/%s" % image, "b%s.fits" % image[8:12],
                      overscan=yes, header=yes, trim=yes, verbose=no, debug=no)

    # Trim and rotate
    iraf.imcopy("b%s[%s]" % (image[8:12], LGRISMS[grism]["trimreg"]),
                "tb%s" % image[8:12])
    iraf.imtranspose("tb%s" % image[8:12], "rtb%s" % image[8:12])

    return
コード例 #13
0
ファイル: cubetoframes.py プロジェクト: josePhoenix/aotools
def split_frames(cubefile, range_pairs, target_dir):
    dirname, filename = os.path.split(cubefile)
    filebase = filename.rsplit('.', 1)[0]
    if len(filebase) > 8:
        warn("IRAF doesn't like long filenames. "
             "Consider shortening the cube filename ({0})".format(filebase))
    
    outfiles = []
    
    for fromidx, toidx in range_pairs:
        for i in range(fromidx, toidx+1):
            infile = cubefile + "[*,*,{0}]".format(i)
            outfile = '{0}/frame_{1:04}.fit'.format(target_dir, i)
            debug("imcopy", infile, outfile)
            iraf.imcopy( # easier to use imcopy and preserve headers than to use pyfits I think
                input=infile,
                output=outfile
            )
            outfiles.append(outfile)
        
        # f = open(inlst, 'w')
        # f.writelines(infiles)
        # f.write('\n')
        # f.close()
        
        # outfile = '{0}/{1}_{2}-{3}.fit'.format(target_dir, filebase, fromidx, toidx)
        # debug("imcombine input={input} output={output} combine=sum reject=none".format(
        #     input="@{0}".format(inlst), #','.join(infiles),
        #     output=outfile,
        # ))
        # outfiles.append(outfile)
        # iraf.imcombine(
        #     input=','.join(infiles),
        #     output=outfile,
        #     combine="sum",
        #     reject="none",
        #     # project='no', # IRAF wants bools specified / default is nonboolean?
        #     # mclip='no',
        # )
    return outfiles
コード例 #14
0
ファイル: go_3dhst.py プロジェクト: gbrammer/unicorn
def sn_marshall():
    """

    """
    from pyraf import iraf

    import unicorn.go_3dhst as go
    import threedhst.process_grism as proc
    import unicorn.analysis

    os.chdir(unicorn.GRISM_HOME+'SN-MARSHALL')

    #### Copy necessary files from PREP_FLT to DATA
    os.chdir('PREP_FLT')
    grism_asn  = glob.glob('MARSHALL-2??-G141_asn.fits')
    files=glob.glob('MARSHALL-2*-G141_shifts.txt')
    files.extend(grism_asn)
    for file in files:
        status = os.system('cp '+file+' ../DATA')
        #shutil.copy(file, '../DATA')

    try:
        iraf.imcopy('MARSHALL-F125W_drz.fits[1]', '../DATA/f125w_sci.fits')
    except:
        os.remove('../DATA/f125w_sci.fits')
        iraf.imcopy('MARSHALL-F125W_drz.fits[1]', '../DATA/f125w_sci.fits')
    
    os.chdir('../')

    #### Initialize parameters
    go.set_parameters(direct='F160W', LIMITING_MAGNITUDE=26)
    threedhst.options['PREFAB_DIRECT_IMAGE'] = '../PREP_FLT/MARSHALL-F160W_drz.fits'
    threedhst.options['OTHER_BANDS'] = [['f125w_sci.fits', 'F125W' , 1248.6, 26.25]]

    #### Main loop for reduction
    for i in range(len(grism_asn)):
        asn = grism_asn[i]
        proc.reduction_script(asn_grism_file=asn)
        unicorn.analysis.make_SED_plots(grism_root=asn.split('_asn.fits')[0])
        go.clean_up()
コード例 #15
0
def GetSkyMedian(gain,trim):
	
	# for this and subtracted images there is a new way to get the sky level.
	# this involves measuring the stddev in the subtracted images and then squaring it 
	# and multiplying by the gain to get the flux per pix which can then be used in the
	# sky calculations, see below to do so.
	
	# first imcopy the subtracted images into a new file
	# this is to stopp the truncation problem which is common. 
	
	t=cmd.getoutput('ls /Volumes/DATA/NITES/data/2011-08-12/reduced/CheckSkyInSubtracted/s_M71*.fits').split('\n')
	
	if trim == 1:
		for i in range(0,len(t)):
			image=str(t[i]+"[1:500,1:500]")
			image2=str(t[i])

			# clobber the old image
			iraf.imcopy(input=image,output=image2)
	
	# then define a box in each image (same box for bright and dark time) and 
	# get the standard deviation in it for each frame that night. Then get the average 
	# nightly stddev which is used to get the sky level.
	
	std_list=np.empty(len(t))
	
	for i in range(0,len(t)):
		h=pf.open(t[i])
		d=h[0].data[243:313,274:344]
		
		std_list[i]=np.std(d)
		#print "%s std: %.2f" % (t[i],std_list[i])
		
	av=pow(np.average(std_list),2)*gain
	print "Sky_bg_pp: %.2f e-" % (av)
	
	
	return av
コード例 #16
0
ファイル: photometry.py プロジェクト: YSOVAR/PAIRITEL
def apply_rectangle_mask(image, mask, threshold, min_width, min_height):
    #finds rectangular middle part with good exposure and trims images to that part.
    hdulist = pyfits.open(image)
    imdata = hdulist[0].data
    #print len(imdata)
    hdulist.close()
    hdulist = pyfits.open(mask)
    immask = hdulist[0].data
    #print len(immask)
    hdulist.close()
    
    good_row = np.zeros(immask.shape[1])
    good_col = np.zeros(immask.shape[0])
    for i in np.arange(0, immask.shape[1]):
        good_col[i] = len(np.where(immask[i,:] > threshold*immask.max())[0])
    
    #print good_row
    for i in np.arange(0, immask.shape[0]):
        good_row[i] = len(np.where(immask[:,i] > threshold*immask.max())[0])
    
    # if sufficient exposure exists (i.e. not a bad frame), trim the image and the mask accordingly:
    if (len(np.where(good_row > min_width)[0]) > min_height) & (len(np.where(good_col > min_height)[0]) > min_width):
        good_width = 0.8 * max(good_row)
        good_height = 0.8 * max(good_col)
	ind_row = [np.where(good_row >= good_width)[0].min(), np.where(good_row >= good_width)[0].max()]
	ind_col = [np.where(good_col >= good_height)[0].min(), np.where(good_col >= good_height)[0].max()]
	
	print ind_row
	print ind_col
	
	outdata = deepcopy(imdata[ind_row[0]:ind_row[1], ind_col[0]:ind_col[1]])
	outmask = deepcopy(immask[ind_row[0]:ind_row[1], ind_col[0]:ind_col[1]])
	
	outimage = image.replace('.fits', '_trimmed.fits')
	outmask = mask.replace('.fits', '_trimmed.fits')
	
	iraf.imcopy(image + '[' + str(ind_row[0]+1) + ':' + str(ind_row[1]+1) + ',' + str(ind_col[0]+1) + ':' + str(ind_col[1]+1) + ']', outimage)
	iraf.imcopy(mask + '[' + str(ind_row[0]+1) + ':' + str(ind_row[1]+1) + ',' + str(ind_col[0]+1) + ':' + str(ind_col[1]+1) + ']', outmask)
コード例 #17
0
ファイル: rcred.py プロジェクト: nblago/kpy
def slice_rc(img):
    '''
    Slices the Rainbow Camera into 4 different images and adds the 'filter' keyword in the fits file.
    '''
    fname = os.path.basename(img)
    fdir = os.path.dirname(img)    
    
    # Running IRAF
    iraf.noao(_doprint=0)
    
    '''f = pf.open(img)
    h = f[0].header
    i = f[0].data[1000:-1,0:920]
    g = f[0].data[0:910,0:900]
    r = f[0].data[1035:2046, 1050:2046]
    u = f[0].data[0:910,1050:2046]'''
    
    
    corners = {
    "i" : [1, 910, 1, 900],
    "g" : [1, 910, 1060, 2045],
    "r" : [1040, 2045, 1015, 2045],
    "u" : [1030, 2045, 1, 900]
    }
    
    
    filenames = []
        
    for i, b in enumerate(corners.keys()):
        name = fname.replace(".fits", "_%s.fits"%b)
        iraf.imcopy("%s[%d:%d,%d:%d]"%(img, corners[b][0], corners[b][1], corners[b][2], corners[b][3]), name)
         
        fitsutils.update_par(name, 'filter', b)

        filenames.append(name)
    
    return filenames
コード例 #18
0
ファイル: go_3dhst.py プロジェクト: gbrammer/unicorn
def sn_primo():
    from pyraf import iraf

    import unicorn.go_3dhst as go
    import threedhst.process_grism as proc
    import unicorn.analysis
    
    os.chdir(unicorn.GRISM_HOME+'SN-PRIMO')
    
    #### Copy necessary files from PREP_FLT to DATA
    os.chdir('PREP_FLT')
    grism_asn  = glob.glob('PRIMO-1???-G141_asn.fits')
    files=glob.glob('PRIMO-1???-G141_shifts.txt')
    files.extend(grism_asn)
    files.append('PRIMO-1026-F160W_tweak.fits')
    for file in files:
        status = os.system('cp '+file+' ../DATA')
        #shutil.copy(file, '../DATA')
    
    try:
        iraf.imcopy('/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F125W_drz.fits[1]', '../DATA/f125w.fits')
    except:
        os.remove('../DATA/f125w.fits')
        iraf.imcopy('/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F125W_drz.fits[1]', '../DATA/f125w.fits')
    
    os.chdir('../')
    
    #### Initialize parameters
    go.set_parameters(direct='F160W', LIMITING_MAGNITUDE=26)
    
    #### Main loop for reduction
    for i, asn in enumerate(grism_asn):
        threedhst.options['PREFAB_DIRECT_IMAGE'] = '/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F160W_drz.fits'
        threedhst.options['OTHER_BANDS'] = [['f125w.fits', 'F125W' , 1248.6, 26.25]]
        proc.reduction_script(asn_grism_file=asn)
        unicorn.analysis.make_SED_plots(grism_root=asn.split('_asn.fits')[0])
        go.clean_up()
コード例 #19
0
ファイル: iraf.py プロジェクト: CarlosBacigalupo/ipn
def shift_master(ref, master):
    """
    Find the shift between two images.
    returns the shift in y direction (along columns)
    """
    #make a narow copy of the reference flat and masterflat 
    iraf.imcopy(input="../"+ref+"[1990:2010,*]", output="tmp/masterflat_ref.fitscut",Stdout="/dev/null")
    iraf.imcopy(input="tmp/masterflat.fits[1990:2010,*]", output="tmp/masterflat.fitscut",Stdout="/dev/null")

    #find the shift with correlation
    iraf.images(_doprint=0,Stdout="/dev/null")
    iraf.immatch(_doprint=0,Stdout="/dev/null")

    shift=iraf.xregister(input="tmp/masterflat_ref.fitscut, tmp/masterflat.fitscut", reference="tmp/masterflat_ref.fitscut", regions='[*,*]', shifts="tmp/mastershift", output="", databasefmt="no", correlation="fourier", xwindow=3, ywindow=51, xcbox=21, ycbox=21, Stdout=1)

    shift=float(shift[-2].split()[-2])

    #Shift the list of apertures
    f=open(master, "r")
    o=open("database/aptmp_masterflat_s", "w")

    for line in f:
        l=line.split()
        if len(l)>0 and l[0]=='begin':
            l[-1]=str(float(l[-1])-shift)
            o.write(l[0]+"\t"+l[1]+"\t"+"tmp/masterflat_s"+"\t"+l[3]+"\t"+l[4]+"\t"+l[5]+"\n")
        elif len(l)>0 and l[0]=='center':
            l[-1]=str(float(l[-1])-shift)
            o.write("\t"+l[0]+"\t"+l[1]+"\t"+l[2]+"\n")
        elif len(l)>0 and l[0]=='image':
            o.write("\t"+"image"+"\t"+"tmp/masterflat_s"+"\n")
        else:
            o.write(line)

    f.close()
    o.close()
コード例 #20
0
ファイル: rcred.py プロジェクト: scizen9/kpy
def slice_rc(img):
    '''
    Slices the Rainbow Camera into 4 different images and adds the 'filter' keyword in the fits file.
    '''
    fname = os.path.basename(img)
    fdir = os.path.dirname(img)    
    
    # Running IRAF
    iraf.noao(_doprint=0)    
    
    corners = {
    "g" : [1, 910, 1, 900],
    "i" : [1, 910, 1060, 2045],
    "r" : [1040, 2045, 1015, 2045],
    "u" : [1030, 2045, 1, 900]
    }
    
    
    filenames = []
        
    for i, b in enumerate(corners.keys()):
        logger.info( "Slicing for filter %s"% b)
        name = fname.replace(".fits", "_%s.fits"%b)
        
        #Clean first
        if (os.path.isfile(name)):
            os.remove(name)
            
        iraf.imcopy("%s[%d:%d,%d:%d]"%(img, corners[b][0], corners[b][1], corners[b][2], corners[b][3]), name)
         
        fitsutils.update_par(name, 'filter', b)
        is_on_target(name)
        
        filenames.append(name)
    
    return filenames
コード例 #21
0
iraf.images()
iraf.images.imfit()
iraf.images.imutil()
#iraf.crutil()
if mode == 'MMIRS':
    flipme = True
    headers = iraf.imhead(fitsfile, long='yes', Stdout=1)
    for line in headers:
        if re.match('WAT', line):
            flipme = False

    if flipme:
        print "not flipped"
        iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2, upper=2, lower=2, ngrow=15)
        iraf.imarith(fitsfile, '-', 'back.fits', fitsfile)
        iraf.imcopy(fitsfile + '[*,*]', fitsfile)
#        iraf.cosmicrays(fitsfile, fitsfile, interactive='no')
        
    else:
        print "flipped"

else:
    iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2)
    iraf.imarith(fitsfile, '-', 'back.fits', fitsfile)

hdu = rfits(fitsfile)
image = hdu.data
hdr = hdu.header
try:
    rot = hdr['ROT']
except KeyError:
コード例 #22
0
		slit_piece3 = '%s[%d][4170:6218, *]' % (slitted_fits[i], ext_num)

		#for some reason, if I did specify the y-dimensions, then I get some corruption error, not sure why; code wrong?
		#slit_piece1 = '%s[%d][1:2048,%d:%d]' % (slitted_fits[i], ext_num, secy1[j], secy2[j])
		#slit_piece2 = '%s[%d][2067:4114,%d:%d]' % (slitted_fits[i], ext_num, secy1[j], secy2[j])
		#slit_piece3 = '%s[%d][4133:6180,%d:%d]' % (slitted_fits[i], ext_num, secy1[j], secy2[j])

		#Now when I copy it into the 3 CCD extension image the coordinates aren't the same as the slit dimensions. That's because each 			extension starts over at 1 for the x-dimension; therefore each CCD extension goes from 1:2048 and not the x-dimensions of the 			slitted-image

		ext1 = '%s[1][1:2048,%d:%d]' % (new_mosaics[i], secy1[j], secy2[j])
		ext2 = '%s[2][1:2048,%d:%d]' % (new_mosaics[i], secy1[j], secy2[j])
		ext3 = '%s[3][1:2048,%d:%d]' % (new_mosaics[i], secy1[j], secy2[j])


		#now running the actual copying of the image
		iraf.imcopy(slit_piece1, ext1)
		iraf.imcopy(slit_piece2, ext2)
		iraf.imcopy(slit_piece3, ext3)


os.system('rm frost_files.list')
os.system('rm retilted_optimum_corrected.list')

end = time.strftime("%H:%M:%S")
print "Start time: ", start
print "End time: ", end	

'''
http://www.stsci.edu/hst/HST_overview/documents/datahandbook/intro_ch23.html

Task Parameters:
コード例 #23
0
ファイル: agndiff.py プロジェクト: svalenti/agnkey
                                            yxor=num, yyor=num, yxterms="half", calctype="real", inter='No',verbose='no')

                        imgtemp = temp_file0+'_temp.fits'
                        imgtarg = temp_file0+'_targ.fits'
                        imgout = temp_file0+'_out.fits'
                        tempmask = temp_file0+'_tempmask.fits'
                        targmask = temp_file0+'_targmask.fits'
                        agnkey.util.delete(imgtemp)
                        agnkey.util.delete(imgtarg)
                        agnkey.util.delete(imgout)
                        agnkey.util.delete(tempmask)
                        agnkey.util.delete(targmask)
                        agnkey.util.delete(temp_file0+'_tempmask3.fits')
                        if os.path.isfile(re.sub('.fits','.clean.fits',_dir+imgtarg0)):
                            print 'use clean'
                            iraf.imcopy(re.sub('.fits','.clean.fits', _dir+imgtarg0)+'[0]', imgtarg, verbose='yes')
                        else:
                            iraf.imcopy(_dir + imgtarg0+'[0]', imgtarg, verbose='no')

                        iraf.imcopy(_dir + targmask0, targmask, verbose='no')
#                        try:
                        iraf.immatch.gregister(_dirtemp + imgtemp0, imgtemp, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric",
                                               interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')
                        try:
                            iraf.immatch.gregister(_dirtemp + tempmask0, tempmask, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric",
                                               interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')
                        except:
                            # this is strange, sometime the registration of the msk fail the first time, but not the second time
                            iraf.immatch.gregister(_dirtemp + tempmask0, tempmask, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric",
                                               interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')
コード例 #24
0
ファイル: SMS.py プロジェクト: cinserra/S3
	    wavefilter.append(float(p[0]))
	    transmission.append(float(p[1]))
	
	wavefilterv = array(wavefilter)
	transmissionv = array(transmission)
	fil_obs_min= min(wavefilterv)
	fil_obs_max= int(max(wavefilterv)) #integer is needed for a sharper cut-off
	#############################################################
	

	#############################################################
	##### Mananging the spectra
	#############################################################

	spec = sn + "[*,1,1]"            # generally multidimension
	iraf.imcopy(sn+'[*,1,1]','sn.fits',verbose='no')             # to create a onedimension fit to use during the script
	
	
	spectrum=iraf.wspec("sn.fits","sn_xbbody.txt", header='no')
	lcf = open('sn_xbbody.txt','r')     
	riga = lcf.readlines()             
	lcf.close()
	wave,flux= [],[]
	for line in riga:
	    p = line.split()
	    wave.append(float(p[0]))
	    flux.append(float(p[1]))
	
	wavev = array(wave)
	fluxv = array(flux)
	waveobs_min= min(wavev)
コード例 #25
0
ファイル: agnmaketempl.py プロジェクト: svalenti/agnkey
         z11 = float(_z11)
         z22 = float(_z22)
 else:
     z11, z22 = '', ''
 answ = 'n'
 while answ == 'n':
     coordlist = str(_xpos) + '   ' + str(_ypos) + '    ' + str(_mag)
     os.system('echo ' + coordlist + ' > ddd')
     iraf.imarith(img0, '-', img0, '_tmp.fits', verbose='no')
     if float(_mag) != 0.0:
         iraf.daophot.addstar("_tmp.fits", 'ddd', psfimg, "_tmp2.fits", nstar=1, veri='no', simple='yes',
                              verb='no')
         iraf.imarith(img0, '-', '_tmp2.fits', imgout, verbose='yes')
     else:
         print '\####  copy file '
         iraf.imcopy(img0, imgout, verbose='yes')
     if _show:
         _z11, _z22, goon = agnkey.util.display_image(imgout, 2, z11, z22, False)
         answ = raw_input('ok  ? [[y]/n]')
         if not answ:
             answ = 'y'
     else:
         answ = 'y'
     agnkey.util.delete('_tmp.fits,_tmp2.fits,_tmp2.fits.art,ddd')
     if answ == 'n':
         agnkey.util.delete(imgout)
         _mag0 = raw_input('which magnitude  ' + str(_mag) + ' ?')
         if _mag0: _mag = _mag0
 print 'insert in the archive'
 _targid = agnkey.agnsqldef.targimg(imgout)
 hd = agnkey.util.readhdr(imgout)
コード例 #26
0
ファイル: chop_image.py プロジェクト: georgezhou/hsfu23
image_slices = functions.read_table(image_slices)

### Loop through and cut out each slice
### save in individual files

print "Chopping image into its image slices"

os.chdir(file_path_temp)

slices_file_list = ""
for i in range(len(image_slices)):
    start_column = int(image_slices[i][0])
    end_column = int(image_slices[i][1])
    region = '[1:4093,' + str(start_column) + ':'+str(end_column)+']'
    print region
    os.system("rm " + str(i) +"_"+ file_name)

    iraf.imcopy(
        input = biassubtracted_file_name + region,\
        output = str(i) +'_'+ file_name,\
        verbose =1)

    ### Save the names of the output files into a list
    slices_file_list = slices_file_list + str(i)+"_" + file_name + "\n"

### Save this list of output files into an ascii file
### This is called on by future programs
image_slices_file_list = open("slice_"+file_name + ".txt","w")
image_slices_file_list.write(slices_file_list)
image_slices_file_list.close()
コード例 #27
0
ファイル: iqwirc.py プロジェクト: cenko/python
    def defringe(self,qombos):

        if type(qombos) is StringType:
            qombo=qombos
            qombos=[qombo]

        # Loop over input list of qombos
        for qombo in qombos:

            # Corresponding files
            offiles2=self.science_images(qombo,pfx=self.flatpfx)
            if len(offiles2)==0:
                continue
            oflist2=','.join(offiles2)

            # Check for existence of fringe image
            offrg=self.fringepre+qombo+'.fits'
            if not os.path.exists(offrg):
                self.mkfringe(qombo)

            # Defringe images
            iraf.iqdefringe(oflist2,offrg,outpfx=self.defrgpfx,
                            skykey="SKYBKG",minlim=no,
                            clobber=self.clobber,verbose=no)

            # List of defringed images
            offiles3=pfx_list(offiles2,self.defrgpfx)
            oflist3=','.join(offiles3)

            # Clipping of defringed images
            clipreg=self.clipreg
            if len(clipreg)>0:

                re1=re.search('(\d+):(\d+),(\d+):(\d+)',clipreg)

                if re1:
                    szx=int(re1.group(2))-int(re1.group(1))+1
                    szy=int(re1.group(4))-int(re1.group(3))+1
                
                    for image in offiles3:

                        # Check that it's not already clipped
                        naxis1,naxis2=get_head(image,['NAXIS1','NAXIS2'])
                        if naxis1<=szx and naxis2<=szy:
                            continue

                        # Clip the defringed images
                        iraf.imcopy(image+clipreg,image,verbose=no)

                        # Clip the corresponding BPM
                        oldbpm=get_head(image,'BPM')
                        newbpm=oldbpm.replace('.pl',self.clpsfx+'.pl')
                        if os.path.exists(oldbpm) and \
                               not os.path.exists(newbpm):
                            iraf.imcopy(oldbpm+clipreg,newbpm,verbose=yes)

                        # Update the image headers
                        update_head(image,'BPM',newbpm)

                else:
                    print "Failed to parse clipping region... not clipping"

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

            # Attempt WCS refinement
            for image in offiles3:

                # Skip images with good WCS
                #if check_head(image,'IQWCS'):
                    #if int(get_head(image,'IQWCS')):
                        #continue

                # Object-detection
                iraf.iqobjs(image,self.sigma,self.satval,skyval="!SKYBKG",
                            masksfx=self.masksfx,wtimage="",minlim=no,
                            clobber=yes,verbose=no)

                # WCS refinement
                wirc_anet(image)
                #iraf.iqwcs(image,objkey=self.objkey,rakey='RA',
                           #deckey='DEC',pixscl=self.pix,pixtol=0.05,
                           #starfile='!STARFILE',catalog='ir',
                           #nstar=60,nstarmax=60,
                           #diffuse=yes,clobber=yes,verbose=self.verbose)

            # Figure out which have good WCS
            wcsdict=keysplit(offiles3,'IQWCS')

            # Interpolate WCS onto files without good WCS so far
            if wcsdict.has_key(""):
                wcsbadfiles=wcsdict[""]
                if len(wcsbadfiles)==len(offiles3):
                    print "No images with good WCS in this set"
                else:
                    wcsgoodfiles=wcsdict[1]
                    wcsinterp(wcsbadfiles,wcsgoodfiles)

            # Done with Step #5
            update_head(offiles3,'IQWRCSTP',5,
                        "Stage of IQWIRC processing")
コード例 #28
0
ファイル: iqwirc.py プロジェクト: cenko/python
    def flatten(self,qombos,flat=None,flatscale=no):

        if type(qombos) is StringType:
            qombo=qombos
            qombos=[qombo]

        # Loop over input list of qombos
        for qombo in qombos:

            # Check for presence of calibration files
            obj,filt=qombo_split(qombo)
            if not self.check_calib(filt):
                self.calib()

            # Corresponding files
            offiles=self.science_images(qombo)
            if len(offiles)==0:
                continue

            # Make a text ".lis" file to record this
            putlines(qombo+'.lis',offiles,clobber=yes)

            # Create the renormalized flatfield
            inflat=self.flatpre+filt+'.fits'
            outflat=self.flatpre+qombo+'.fits'
            check_exist(outflat,"w",clobber=self.clobber)

            # Rescale flatfield if requested
            if flatscale:
                wirc_flatscale(offiles,inflat,outflat,clipfrac=0.02,
                               clobber=yes)
            else:
                iraf.imcopy(inflat,outflat,verbose=no)
            check_exist(outflat,"r")

            # Further preprocessing
            for image in offiles:

                # Flatfielding
                update_head(image,self.filtset,filt)
                iraf.iqflatten(image,outflat,outpfx=self.flatpfx,
                               normflat=yes,statsec=self.statsec,
                               subsky=yes,vignflat=no,
                               clobber=yes,verbose=no)

                # Set bad pixels to zero
                image2=self.flatpfx+image
                iraf.iqmask(image2,mask='!BPM',method='constant',value=0.0,
                            clobber=yes,verbose=no)
                
                # Reject cosmic rays / bad pixels
                #iraf.lacos_im(image2, "tempcr.fits", "tempcrmask.fits",
                              #gain=5.5, readn=12.0, statsec="*,*",
                              #skyval=get_head(image2,"SKYBKG"), sigclip=6.0,
                              #sigfrac=0.5, objlim=1.0, niter=1, verbose=no)
                #shutil.move("tempcr.fits", image2)
                #os.remove("tempcrmask.fits")

                # Write Useful Keywords
                update_head(image2,'PROCESSD',1,
                            "Image has been processed by iqwirc")
                update_head(image2,'PROCVER',version,
                            "Version number of iqwirc used")
                update_head(image2,'ZEROPT','INDEF',
                            "Zero-point relative to 2MASS or INDEF")
                update_head(image2,'PROCPROB','None',
                            "Problems encountered in iqwirc processing")

            # List of flatfielded images
            offiles2=pfx_list(offiles,self.flatpfx)
            oflist2=','.join(offiles2)

            # Object-detection
            iraf.iqobjs(oflist2,self.sigma,self.satval,
                        skyval="!SKYBKG",masksfx=self.masksfx,
                        wtimage="",minlim=no,
                        clobber=yes,verbose=no)

            # Attempt WCS refinement
            iraf.iqwcs(oflist2,objkey=self.objkey,rakey='RA',
                       deckey='DEC',pixscl=self.pix,pixtol=0.05,
                       starfile='!STARFILE',catalog='ir',
                       nstar=60,nstarmax=60,
                       diffuse=yes,clobber=yes,verbose=self.verbose)

            # Done with Step #3
            update_head(offiles2,'IQWRCSTP',3,
                        "Stage of IQWIRC processing")
コード例 #29
0
ファイル: iqwirc.py プロジェクト: cenko/python
    def calib(self):

        dolamps=no
        lampfilts=[]
        lampfiles=[]
        lamponfiles={}
        lampofffiles={}

        allfiles=glob.glob(self.inpat)

        # Look for flatfield images
        for image in allfiles:

            # Criteria for flatfield images
            lampval=get_head(image,self.lampkey)
            if re.search(self.lampon,lampval,re.I) or \
               re.search(self.lampoff,lampval,re.I):
                dolamps=yes
                filter=wirc_filter(image)
                if filter not in lampfilts:
                    lampfilts.append(filter)
                if re.search(self.lampon,lampval,re.I):
                    update_head(image,self.lampfkey,'On-'+filter,
                                "Lamp status + Filter setting")
                    lampfiles.append(image)
                    if lamponfiles.has_key(filter):
                        lamponfiles[filter].append(image)
                    else:
                        lamponfiles[filter]=[image]
                elif re.search(self.lampoff,lampval,re.I):
                    update_head(image,self.lampfkey,'Off-'+filter,
                                "Lamp status + Filter setting")
                    lampfiles.append(image)
                    if lampofffiles.has_key(filter):
                        lampofffiles[filter].append(image)
                    else:
                        lampofffiles[filter]=[image]

        lamplist=','.join(lampfiles)

        # Use flatfield images to make our calibration files
        if dolamps:

            # Make lamp-on and lamp-off flats & bad-pixel mask
            iraf.iqcals(lamplist,dobias=no,biasproc=no,
                        doflats=yes,flatproc=no,flatkey=self.flatkey,
                        flatre=self.flatre,filtkey=self.lampfkey,
                        flatpre=self.flatpre,flatscale="none",
                        statsec=self.statsec,normflat=no,
                        dobpm=yes,bpmmethod='flatratio',
                        bpmroot=self.bpmroot,
                        bpmffilt=self.bpmffilt,classkey="",
                        mosaic=no,clobber=self.clobber,
                        verbose=self.verbose)

            # Subtract lamp-off flat from lamp-on flat, if necessary
            for filter in lamponfiles.keys():

                onflat=self.flatpre+'On-'+filter+'.fits'
                offflat=self.flatpre+'Off-'+filter+'.fits'
                outflat=self.flatpre+filter+'.fits'
                check_exist(outflat,'w',self.clobber)

                if filter in self.onofflist:

                    if not lampofffiles.has_key(filter):
                        print "Need lamp-off flats for filter '%s'" % filter
                        continue

                    iraf.imarith(onflat,'-',offflat,outflat,
                                 verbose=self.verbose,noact=no)

                else:

                    # For some filters "lampsoff" counts are negligible
                    iraf.imcopy(onflat,outflat,verbose=yes)

                update_head(outflat,self.filtset,filter)
                medflat=wirc_sky(outflat,"")
                #update_head(outflat,"NORMFLAT",1,
                update_head(outflat,"NORMFLAT",0,
                            "Has flatfield been normalized?")
                update_head(outflat,"MEDFLAT",medflat,
                            "Median value of original flatfield")
コード例 #30
0
ファイル: iqwirc.py プロジェクト: cenko/python
def wirc_flatscale(infiles,inflat,outflat,clipfrac=0.03,
                   clobber=globclob):

    # "infiles" is a list of filenames
    inlist=','.join(infiles)
    bpmfile=get_head(infiles[0],'BPM')

    # Make a median combination of input files
    imcmb1=iraf.mktemp("iqwircc")+".fits"
    iraf.imcombine(inlist,imcmb1,combine="median",reject="none",
                   project=no,outtype="real",outlimits="",offsets="",
                   masktype="none",blank=0.0,scale="!SKYBKG",zero="none",
                   weight="none",statsec="",lthreshold="INDEF",
                   hthreshold="INDEF",nkeep=1)
    [naxis1,naxis2]=get_head(imcmb1,['NAXIS1','NAXIS2'])
    npixall=float(naxis1)*float(naxis2)

    # Calculate sky background & divide, subtract 1
    skybkg=wirc_sky(imcmb1,bpmfile)
    imcmb2=iraf.mktemp("iqwircc")+".fits"
    iraf.imarith(imcmb1,'/',skybkg,imcmb2,verbose=no,noact=no)
    iraf.imdel(imcmb1,verify=no,go_ahead=yes)
    iraf.imarith(imcmb2,'-',1.0,imcmb1,verbose=no,noact=no)

    # Surface fit to median image
    imsurf1=iraf.mktemp("iqwircs")+".fits"
    iraf.imsurfit(imcmb1,imsurf1,xorder=6,yorder=6,type_output="fit",
                  function="chebyshev",cross_terms=yes,
                  xmedian=21,ymedian=21,median_percent=50.0,
                  lower=0.0,upper=0.0,ngrow=0,niter=0,
                  regions="all",rows="*",columns="*",border=50,
                  sections="",circle="",div_min="INDEF")
    # Corresponding bad pixel mask
    imbpm1=iraf.mktemp("iqwircb")+".pl"
    iraf.imexpr("abs(x)>%.3f ? 0 : 1" % clipfrac,imbpm1,x=imsurf1)

    # Subtract 1.0 from flatfield
    imflat1=iraf.mktemp("iqwircf")+".fits"
    iraf.imarith(inflat,'-',1.0,imflat1,verbose=no,noact=no)

    # Surface fit to the flatfield
    imsurf2=iraf.mktemp("iqwircs")+".fits"
    iraf.imsurfit(imflat1,imsurf2,xorder=6,yorder=6,type_output="fit",
                  function="chebyshev",cross_terms=yes,
                  xmedian=21,ymedian=21,median_percent=50.0,
                  lower=0.0,upper=0.0,ngrow=0,niter=0,
                  regions="all",rows="*",columns="*",border=50,
                  sections="",circle="",div_min="INDEF")
    # Corresponding bad pixel mask
    imbpm2=iraf.mktemp("iqwircb")+".pl"
    iraf.imexpr("abs(x)>%.3f ? 0 : 1" % clipfrac,imbpm2,x=imsurf2)

    # Combine bad pixel masks for median + flat
    imbpm3=iraf.mktemp("iqwircb")+".pl"
    iraf.imexpr("(x>0 || y>0) ? 1 : 0",imbpm3,x=imbpm1,y=imbpm2)

    # Calculate the ratio image
    imratio=iraf.mktemp("iqwircr")+".fits"
    iraf.imexpr("z>0 ? 0 : x/y",imratio,x=imsurf1,y=imsurf2,z=imbpm3)

    # Mimstat on the ratio image
    mimstat=iraf.mimstatistics(imratio,imasks=imbpm3,omasks="",
                     fields='image,npix,mean,stddev,min,max,mode',
                     lower='INDEF',upper='INDEF',nclip=4,
                     lsigma=5.0,usigma=5.0,binwidth=0.1,
                     format=no,Stdout=1,Stderr=1)
    mimels=mimstat[0].split()
    npix=float(mimels[1])
    xmult=float(mimels[2])

    # Check that a reasonable number of pixels have made the grade
    check_exist(outflat,'w',clobber=clobber)
    if npix<0.05*npixall:
        print "Less than 5% of pixels passed the cut... preserving flatfield"
        iraf.imcopy(inflat,outflat,verbose=no)
        xmult=1.0
    else:
        # Create the final flatfield image
        iraf.imexpr("x*%.3f + 1" % xmult,outflat,x=imflat1)

    # Update header keywords
    update_head(outflat,'RESCALE',1,"Flatfield has been rescaled")
    update_head(outflat,'ORIGFLAT',inflat,
                "Input flatfield name (before rescaling)")
    update_head(outflat,'XMULT',xmult,
                "Multiplied ORIGFLAT by this factor to rescale")

    # Clean up
    iraf.imdel(imcmb1,verify=no,go_ahead=yes)
    iraf.imdel(imcmb2,verify=no,go_ahead=yes)
    iraf.imdel(imsurf1,verify=no,go_ahead=yes)
    iraf.imdel(imsurf2,verify=no,go_ahead=yes)
    iraf.imdel(imbpm1,verify=no,go_ahead=yes)
    iraf.imdel(imbpm2,verify=no,go_ahead=yes)
    iraf.imdel(imbpm3,verify=no,go_ahead=yes)
    iraf.imdel(imflat1,verify=no,go_ahead=yes)
    iraf.imdel(imratio,verify=no,go_ahead=yes)