コード例 #1
0
def cosmicray_lacosmic(im,sigclip=5,sigfrac=0.3,readnoise=1,gain=1,objlim=5,satlevel=65535.0,niter=5,verbose=False):

    if isinstance(im,CCDData):
        cosmic = cosmicsimage(im.data,gain=gain,readnoise=readnoise,sigclip=sigclip,sigfrac=sigfrac,objlim=objlim,satlevel=satlevel,verbose=verbose)
        cosmic.run(maxiter=niter,verbose=verbose)
        im.data = cosmic.cleanarray
        im.mask = cosmic.mask
        im = im.to_hdu()
        
    else:
        cosmic = cosmicsimage(im,gain=gain,readnoise=readnoise,sigclip=sigclip,sigfrac=sigfrac,objlim=objlim,satlevel=satlevel,verbose=verbose)
        cosmic.run(maxiter=niter,verbose=verbose)
        im = cosmic.cleanarray

    return im
コード例 #2
0
ファイル: pab.py プロジェクト: gbrammer/pabeta
def run_lacosmic(flc='j9cv13pcq_flc.fits', split=1024, pssl=0.0, sigclip=4.0, objlim=3.8, sigfrac=0.3):
    """
    
    drizzlepac.astrodrizzle.AstroDrizzle('ESO550-IG02-13-083-F814W_asn.fits', skysub=True, clean=True, final_wcs=True, final_scale=0.05, final_pixfrac=0.8, context=False, resetbits=0, final_bits=576, preserve=False)
    
    """
    
    import cosmics
    import threedhst
    
    im = pyfits.open(flc, mode='update')
    h = im[0].header
    
    nx, ny = 4096/split, 2048/split
    
    for ext in [1,2]:
        if flc.startswith('i'):
            dq = np.zeros((2051, 4096), dtype=int)
        else:
            dq = np.zeros((2048, 4096), dtype=int)
            
        for i in range(nx):
            for j in range(ny):
                threedhst.showMessage('ext: %d, (i,j)=%d,%d' %(ext, i,j))
                subim = im['sci',ext].data[j*split:(j+1)*split, i*split:(i+1)*split]
                c = cosmics.cosmicsimage(subim, pssl=pssl, gain=1, readnoise=h['READNSEA'], sigclip=objlim, sigfrac=sigfrac, objlim=objlim, satlevel=84700.0, verbose=True)
                c.run(maxiter=4)
                dq[j*split:(j+1)*split, i*split:(i+1)*split] += c.mask*1
        
        im['dq',ext].data |= dq*4096
        
        pyfits.writeto(im.filename().replace('.fits', '_lac_%d.fits' %(ext)), data=dq*4096, header=im['dq', ext].header, clobber=True)
        
    im.flush()
コード例 #3
0
ファイル: rcred.py プロジェクト: scizen9/kpy
def clean_cosmic(f):
    '''
    From lacosmic.
    '''
    import cosmics
    
    out = f.replace('.fits',  '_clean.fits')
    
    #If it does already exist, just return the name.
    if (os.path.isfile(out)):
        return out
    
    #Otherwise, run the cosmic ray rejection based on LA Cosmic.
    g = fitsutils.get_par(f, "GAIN")
    if (fitsutils.has_par(f, "RDNOISE")):
        rn = fitsutils.get_par(f, "RDNOISE")
    else:
        rn = 20
    array, header = cosmics.fromfits(f)
    
    try:
        c = cosmics.cosmicsimage(array, gain=g, readnoise=rn, sigclip = 8.0, sigfrac = 0.3, satlevel = 64000.0)
        c.run(maxiter = 3)
        out = f.replace('.fits',  '_clean.fits')
    
        cosmics.tofits(out, c.cleanarray, header)
        fitsutils.update_par(out, "CRREJ", 1)

        #os.remove(f)
    except:
        pass
    
    return out
コード例 #4
0
ファイル: MaskData.py プロジェクト: kif/Py2DeX
    def remove_cosmic(self, img):
        self.update_deque()
        test = cosmicsimage(img, sigclip=3.0, objlim=3.0)
        num = 2
        for i in xrange(num):
            test.lacosmiciteration(True)
            test.clean()
            self._mask_data = np.logical_or(self._mask_data, np.array(test.mask, dtype='bool'))

        print test.mask
コード例 #5
0
ファイル: LAcosmics.py プロジェクト: TravGrah/IRMOS-pipeline
def clean_data(data,maxiter=5,gain=2.2,readnoise=10.0,sigclip=5,
               sigfrac=0.3,objlim=5.0,satlevel=50000,getmask=False,verbose=False):

    # Perform Laplacian cosmic ray subtraction
    cosmic = cosmics.cosmicsimage(data,gain=gain,readnoise=readnoise,sigclip=sigclip,sigfrac=sigfrac,objlim=objlim,satlevel=satlevel,verbose=verbose)
    cosmic.run(maxiter=maxiter,verbose=verbose)

    if getmask:
        return cosmic.cleanarray,cosmic.mask
    else:
        return cosmic.cleanarray
コード例 #6
0
ファイル: vp_art_cosmicrays.py プロジェクト: boada/vp_art
def clean_cosmicrays( image, params ):

    imdat, imhead = cosmics.fromfits(image, verbose=False)

    # DEFAULT HEADER PARAMETERS FOR VIRUS-P AS OF 2012
    try: gain = imhead['GAIN1']
    except: gain = 2.0
    try: rdnoise = imhead['RDNOISE1']
    except: rdnoise = 2.5

    c = cosmics.cosmicsimage( imdat, gain=gain, readnoise=rdnoise, \
                              sigclip=5., sigfrac=0.3, objlim=5.0 )
    c.run( maxiter=4 )

    cosmics.tofits(image[:-5]+'c.fits', c.cleanarray, imhead)
    cosmics.tofits(image[:-5]+'_mask.fits', c.mask, imhead)
コード例 #7
0
ファイル: rcred.py プロジェクト: nblago/kpy
def clean_cosmic(f, name):
    '''
    From lacosmic.
    '''
    import cosmics
    
    
    g = fitsutils.get_par(f, "GAIN")
    rn = fitsutils.get_par(f, "RDNOISE")
    array, header = cosmics.fromfits(f)
    
    try:
        c = cosmics.cosmicsimage(array, gain=g, readnoise=rn, sigclip = 8.0, sigfrac = 0.3, satlevel = 64000.0)
        c.run(maxiter = 10)
        out = f.replace('.fits',  '_clean.fits')
    
        cosmics.tofits(out, c.cleanarray, header)
        
        os.remove(f)
    except:
        pass
コード例 #8
0
def origRemoval(fitsFile):
	"""
	Runs the original cosmics.py script on the input FITS file.
	Outputs: origClean.fits, origMask.Fits
	"""
	# Read the FITS :
	array, header = origCosmics.fromfits(fitsFile)
	# array is a 2D numpy array

	# Build the object :
	c = origCosmics.cosmicsimage(array, gain=2.2, readnoise=10.0, sigclip = 5.0,
	 sigfrac = 0.3, objlim = 5.0)
	# There are other options, check the manual...

	# Run the full artillery :
	c.run(maxiter = 4)

	# Write the cleaned image into a new FITS file, conserving the original header :
	origCosmics.tofits("origClean.fits", c.cleanarray, header)

	# If you want the mask, here it is :
	origCosmics.tofits("origMask.fits", c.mask, header)
コード例 #9
0
ファイル: autoproc_depend.py プロジェクト: cenko/RATIR-GSFC
def cosmiczap(filename, outname, sigclip=6.0, maxiter=3, verbose=True):

    """
    NAME:
        cosmiczap
    PURPOSE:
        Removes cosmic rays using Laplacian cosmic ray identification written in cosmics.py 
    INPUTS:
    	filename - file or list of files to be cosmic ray zapped
    	outfile  - name of output file
    OPTIONAL KEYWORDS:
        sigclip  - sigma to clip
        maxiter  - maximum number of times to iterate loop
        verbose  - quiet?
    EXAMPLE:
        cosmiczap(filename, outname)
    DEPENDENCIES:
        cosmic.py (described in http://arxiv.org/pdf/1506.07791v3.pdf)  
    FUTURE IMPROVEMENTS:
        Read readnoise from header?    
    """
    
    data, head = cosmics.fromfits(filename, verbose=False)
    
    gain = head['GAIN']
    c = cosmics.cosmicsimage(data, gain=gain, readnoise=18, sigclip=sigclip,
        sigfrac = 0.5, objlim = 5.0, verbose=False)
    
    tot = c.run(maxiter=maxiter, verbose=False)
    
    head['NPZAP'] = (tot, "Num. of pixels zapped by cosmiczap")
    date = datetime.datetime.now().isoformat()
    head.add_history('Processed by cosmiczap ' + date)    
    
    if verbose: print '  Zapped %d total affected pixels (%.3f%% of total)' \
                      %(tot,tot*100.0/np.size(data))
    
    cosmics.tofits(outname, c.cleanarray, head, verbose=False)
コード例 #10
0
ファイル: ph_tasks.py プロジェクト: npadmana/py_hydra
  def process_single_image(self, infn, nlacosmic=2):
    """ Do all the basic clean up steps for a single image 
    
    Imfix
    Bias subtract.
    Clean up cosmic rays
    Generate a noise map

    nlacosmic -- number of LA Cosmic iterations to do
      -- A small number means that a smaller number of cosmics get removed, but the code
         runs faster.
    """

    # Read in the data
    ff = pyfits.open(infn)
    arr = ff[0].data
    ff.close()

    # Image fix and bias subtract
    arr = self._imfix(arr) - self.get_bias()

  
    # Cosmic ray cleanup
    # The parameters here are hardcoded... in general, we should clean these up somewhat
    cc = cosmics.cosmicsimage(arr, gain=self.gain, readnoise=self.readnoise, objlim=1.5)
    cc.findsatstars() # Mark the saturated stars
    cc.run(nlacosmic)
    arr = cc.cleanarray.copy()

    # Now generate a median filtered image
    m5 = ndimage.filters.median_filter(arr, size=5, mode='mirror')
    m5clipped = m5.clip(min=0.00001) # As we will take the sqrt
    ivar = self.gain**2/(self.gain*m5clipped + self.readnoise*self.readnoise)
    # Now set the bad pixels to zero ivar
    ivar[cc.mask == 1] = 0.0

    return arr, ivar
コード例 #11
0
def run_cosmics(filename):
    '''
    The main controller.
    '''
    
    output = filename.split('_')[0] + "_cr_" + filename.split('_')[1]

    # Assert the input file exists
    error = filename + ' input for run_cosmics in '
    error += 'run_cosmics.py does not exist.'
    assert os.access(filename, os.F_OK), error
    
    # Define the output name and delete if exists.
    query = os.access(output, os.F_OK)
    if query == True:
        os.remove(output)

        # Find the number of extentions.
    header = pyfits.open(filename)
    header_count = len(header)
    header.close()

    for ext in range(0, header_count):
        print filename + '[' + str(ext) + ']'

        if ext in [0, 5]:
            hdu = pyfits.open(filename)
            array = hdu[ext].data
            header = hdu[ext].header
            pyfits.append(
                output,
                array,
                header)
            hdu.close()
        else:
            # Read the FITS :
            array, header = cosmics.fromfits(filename, hdu = ext)
            # array is a 2D numpy array

            # Build the object
            # WFPC2 PC Chip Settings
            if ext == 1:
                c = cosmics.cosmicsimage(
                        array, 
                        pssl = 0.0, 
                        gain = 1.0, 
                        readnoise = 5.0,
                        sigclip = 3.0, 
                        sigfrac = 0.01, 
                        objlim = 4.0,
                        satlevel = 4095.0,
                        verbose = True)
            # Build the object
            # WFPC2 WF Chip Settings
            elif ext in [2, 3, 4]:
                c = cosmics.cosmicsimage(
                        array, 
                        pssl = 0.0, 
                        gain=1.0, 
                        readnoise=5.0,
                        sigclip = 2.5, 
                        sigfrac = 0.001, 
                        objlim = 5.0,
                        satlevel = 4095.0, 
                        verbose = True)

            # Run the full artillery
            c.run(maxiter = 7)

            # Write the cleaned image into a new FITS file
            # conserving the original header :
            #cosmics.tofits(output, c.cleanarray, header)

            pyfits.append(
                output,
                c.cleanarray.transpose(),
                header,
                ext = 'SCI')

            # If you want the mask, here it is :
            # cosmics.tofits("mask.fits", c.mask, header)
            # (c.mask is a boolean numpy array, that gets 
            # converted here to an integer array)

    assert os.path.isfile(output) == True, 'run_cosmics did not create: ' + output

    # Create a symbolic link to the original c1m files that matches the 
    # Output filename.
    make_c1m_link(os.path.abspath(filename))
コード例 #12
0
ファイル: CRR.py プロジェクト: nblago/kpy
import cosmics
import pyfits as pf
import sys




fn = sys.argv[1]
FF = pf.open(fn)
if FF[0].header['ADCSPEED'] == 2: RN = 21
else: RN = 5

c = cosmics.cosmicsimage(FF[0].data, gain=1, readnoise=RN, sigclip = 5,
    sigfrac=0.5, objlim=4.0)

c.run(maxiter=4, verbose=True)

FF[0].data = c.cleanarray
FF[0].header["CRR"] = ("Processed", "With cosmics.py 0.4")

import os

outn = os.path.join(os.path.dirname(fn), "crr_" + os.path.basename(fn))

FF.writeto(outn)
    
コード例 #13
0
	# We read array and header of that fits file :
	(a, h) = cosmics.fromfits("g.fits", verbose=False)
	
	# We gather some parameters :
	
	pssl = image['skylevel'] # The Previously Subtracted Sky Level
	print "PSSL (TM): %.2f" % pssl
	gain = image['gain']
	readnoise = image['readnoise']
	
	sigclip = cosmicssigclip
	sigfrac = 0.3
	objlim = 3.0
	
	# Creating the object :
	c = cosmics.cosmicsimage(a, pssl=pssl, gain=gain, readnoise=readnoise, sigclip=sigclip, sigfrac=sigfrac, objlim=objlim, satlevel=-1.0, verbose=False)
	# negative satlevel : we do not look for saturated stars
	
	
	# Ok let's try a full run :
	c.run(maxiter=3)
	
	ncosmics = np.sum(c.mask)
	
	if ncosmics != 0:
		print "--- %i pixels ---" % ncosmics
	
	# We do the rest anyway (easier): 
	
	# We write the mask :
	cosmics.tofits("cosmicsmask.fits", c.getdilatedmask(size=5), verbose=False)
コード例 #14
0
ファイル: clean_image.py プロジェクト: b1quint/btfi
# The following two lines are only needed as cosmic.py is not in this directory nor in the python path.
# They would not be required if you copy cosmics.py in this directory.
import sys
import os
import pyfits
sys.path.append("../.") # The directory that contains cosmic.py

import cosmics

# File information
path = '/home/bquint/Data/20151203/012'
name = 'xjfp_sami_C001.602.fits'
filename = os.path.join(path, name)

# Read the FITS :
array = pyfits.getdata(filename)

# Build the object :
c = cosmics.cosmicsimage(array, gain=2.1, readnoise=10.0, sigclip = 3.0, sigfrac = 0.3, objlim = 5.0)
# There are other options, check the manual...

# Run the full artillery :
c.run(maxiter = 4)

array = c.cleanarray
pyfits.writeto(filename.replace('.fits', '_crclean.fits'), array, clobber=True)
コード例 #15
0
ファイル: crclean.py プロジェクト: kasperschmidt/python
def cr_clean(fits_fn):
    det = pyfits.getval(fits_fn, 'DETECTOR')

    if det == 'IR':
        print('CR-cleaning IR FLT file: %s' % fits_fn)

        readnoise = 20.0     # for WFC3/IR
        #sigclip = 5.0
        sigclip = 5.0
        sigfrac = 0.3     # small means crs are grown more  0.3 is good!
        #objlim = 5.0
        objlim = 12.0
        #maxiter = 4
        maxiter = 3
        exptime = pyfits.getval(fits_fn, 'EXPTIME')
        gain = exptime

        (image, header) = cosmics.fromfits(fits_fn)
        c = cosmics.cosmicsimage(image, gain=gain, readnoise=readnoise, sigclip=sigclip, sigfrac=sigfrac, objlim=objlim)
        c.run(maxiter=maxiter)

        clean_fn = fits_fn.replace('.fits', '_crclean.fits')
        mask_fn = fits_fn.replace('.fits', '_crmask.fits')

        fo = pyfits.open(fits_fn)
        fo[1].data = c.cleanarray
        fo.writeto(clean_fn)
        fo.close()

        #cosmics.tofits(clean_fn, c.cleanarray, header)
        cosmics.tofits(mask_fn, c.mask, header)

    if det == 'UVIS':
        print('CR-cleaning UVIS FLT file: %s' % fits_fn)
        readnoise = 3.105     # for WFC3/UVIS

        # good
        #sigclip = 5.0
        #sigfrac = 0.1     # small means crs are grown more  0.3 is good!
        #objlim = 5.0

        # some residuals
        #sigclip = 4.5
        #sigfrac = 0.1     # small means crs are grown more  0.3 is good!
        #objlim = 4.0

        sigclip = 4.5
        sigfrac = 0.1     # small means crs are grown more  0.3 is good!
        objlim = 2.0

        maxiter = 4
        exptime = pyfits.getval(fits_fn, 'EXPTIME')
        gain = 1.0
        ff = pyfits.open(fits_fn)
        header = ff[0].header
        image = ff[1].data

        #(image, header) = cosmics.fromfits(fits_fn)
        c = cosmics.cosmicsimage(image, gain=gain, readnoise=readnoise, sigclip=sigclip, sigfrac=sigfrac, objlim=objlim)
        c.run(maxiter=maxiter)

        image2 = ff[4].data
        d = cosmics.cosmicsimage(image2, gain=gain, readnoise=readnoise, sigclip=sigclip, sigfrac=sigfrac, objlim=objlim)
        d.run(maxiter=maxiter)

        clean_fn = fits_fn.replace('.fits', '_crclean.fits')
        mask_fn = fits_fn.replace('.fits', '_crmask.fits')

        fo = pyfits.open(fits_fn)
        fo[1].data = c.cleanarray
        fo[4].data = d.cleanarray
        fo.writeto(clean_fn)
        fo.close()

        #cosmics.tofits(clean_fn, c.cleanarray, header)
        cosmics.tofits(mask_fn, c.mask, header)
コード例 #16
0
ファイル: whirc_reduce.py プロジェクト: marlageha/dataredux
def whirc_reduce(obj_id):
    
    import pyfits
    import numpy as np
    import matplotlib.pyplot as plt
    import cosmics 
    import astropy.coordinates as coord
    import astropy.units as u
    
    #READ IN DATA AND OBSERVING LOG
    #Read in the bias file to be subtracted from flats
    bias = pyfits.getdata("Calibs/bias.fits")
    bias = bias[0:2048,0:2048]
    
    #Read in the dark file to be subtracted from flats
    dark = pyfits.getdata("Calibs/dark.fits")
    dark = dark[0:2048,0:2048]
    
    #Read in flats
    J_flat = pyfits.getdata('Calibs/New_J_Flat.fits')
    K_flat = pyfits.getdata('Calibs/New_K_Flat.fits')
    
    #Read in data from "whirc_info.dat" (the observation log)
    im1 = open('whirc_info.dat','r')
    data1 = im1.readlines()
    im1.close()
    
    filename = []
    dateobs = []
    objname = []
    imgtype = []
    ra = []
    dec = []
    exptime = []
    usable = []
    rotangle = []
    raoffset = []
    decoffset = []
    
    for line in data1:
        p = line.split()
        filename.append(p[0])
        dateobs.append(p[1])
        objname.append(p[2])
        imgtype.append(p[3])
        ra.append(p[4])
        dec.append(p[5])
        exptime.append(p[6])
        usable.append(p[7])
        rotangle.append(p[8])
        raoffset.append(p[9])
        decoffset.append(p[10])
        
    
    #Rewrite in a more convenient array with format array[line][element]    
    alldata = []
    
    for line in range(len(usable)):
        alldata.append([filename[line],dateobs[line],objname[line],imgtype[line],ra[line],dec[line],exptime[line],usable[line],rotangle[line],raoffset[line],decoffset[line]])
    
    #Find junk files and good files:
    junk = []
    good = []
    for line in range(len(alldata)):
       if "no" in alldata[line][7]:
           junk.append(alldata[line])
       if "yes" in alldata[line][7]:
           good.append(alldata[line])
    
    #Find files related to obj_id in good files
    aobj_id = []
    for line in range(len(good)):
        if str(obj_id) in good[line][2]:
            aobj_id.append(good[line])
    print str(len(aobj_id)),"files for "+str(obj_id)
    
    #Find files through J_filter
    aobj_idJ = []
    for line in range(len(aobj_id)):
        if "J" in aobj_id[line][2]:
            aobj_idJ.append(aobj_id[line])
    print str(len(aobj_idJ)), "J files for "+str(obj_id)
    
    #Find files through K_filter
    aobj_idK = []
    for line in range(len(aobj_id)):
        if "K" in aobj_id[line][2]:
            aobj_idK.append(aobj_id[line])
    print str(len(aobj_idK)), "K files for "+str(obj_id)
    
    
    #Find J files from Night 1
    aobj_idJN1 = []
    for line in range(len(aobj_idJ)):
        if "N1" in aobj_idJ[line][0]:
            aobj_idJN1.append(aobj_idJ[line])
    print str(len(aobj_idJN1)), "J files in N1"
    
    #Fink K files from Night 1
    aobj_idKN1 = []
    for line in range(len(aobj_idK)):
        if "N1" in aobj_idK[line][0]:
            aobj_idKN1.append(aobj_idK[line])
    print str(len(aobj_idKN1)), "K files in N1"
    
    #Find J files from Night 2
    aobj_idJN2 = []
    for line in range(len(aobj_idJ)):
        if "N2" in aobj_idJ[line][0]:
            aobj_idJN2.append(aobj_idJ[line])
    print str(len(aobj_idJN2)), "J files in N2"
    
    #Find K files from Night 2
    aobj_idKN2 = []
    for line in range(len(aobj_idK)):
        if "N2" in aobj_idK[line][0]:
            aobj_idKN2.append(aobj_idK[line])
    print str(len(aobj_idKN2)), "K files in N2"
    
    #Find J files from Night 3
    aobj_idJN3 = []
    for line in range(len(aobj_idJ)):
        if "N3" in aobj_idJ[line][0]:
            aobj_idJN3.append(aobj_idJ[line])
    print str(len(aobj_idJN3)), "J files in N3"
    
    #Find K files from Night 3
    aobj_idKN3 = []
    for line in range(len(aobj_idK)):
        if "N3" in aobj_idK[line][0]:
            aobj_idKN3.append(aobj_idK[line])
    print str(len(aobj_idKN3)), "K files in N3"
    
    #WRITE PATH TO J_FILES_N1
    Jobj_idN1_locs = []
    for line in range(len(aobj_idJN1)):
        Jobj_idN1_locs.append("Raw/"+str(aobj_idJN1[line][0]))
        
    #WRITE PATH TO K_FILES_N1
    Kobj_idN1_locs = []
    for line in range(len(aobj_idKN1)):
        Kobj_idN1_locs.append("Raw/"+str(aobj_idKN1[line][0]))
        
    #WRITE PATH OF J_FILES_N2
    Jobj_idN2_locs = []
    for line in range(len(aobj_idJN2)):
        Jobj_idN2_locs.append("Raw/"+str(aobj_idJN2[line][0]))
    
    #WRITE PATH OF K_FILES_N2
    Kobj_idN2_locs = []
    for line in range(len(aobj_idKN2)):
        Kobj_idN2_locs.append("Raw/"+str(aobj_idKN2[line][0]))
        
    #WRITE PATH OF J_FILES_N3
    Jobj_idN3_locs = []
    for line in range(len(aobj_idJN3)):
        Jobj_idN3_locs.append("Raw/"+str(aobj_idJN3[line][0]))
    
    #WRITE PATH OF K_FILES_N3
    Kobj_idN3_locs = []
    for line in range(len(aobj_idKN3)):
        Kobj_idN3_locs.append("Raw/"+str(aobj_idKN3[line][0]))
        
    #READ IN SKY FILES
    #READ IN SCIENCE EXPOSURES
    
    #J_filter, Night 1
    print "reading in JN1..."
    if len(aobj_idJN1) > 0:
        skyJN1 = pyfits.getdata('Calibs/sky/sky_'+str(obj_id)+'J_N1.fits')
        
        for line in range(len(aobj_idJN1)):
            sci = pyfits.getdata(Jobj_idN1_locs[line])[0:2048,0:2048]
            reduced_sci = (sci - bias - dark - skyJN1)/J_flat
            file = pyfits.PrimaryHDU(reduced_sci)
            file.writeto('Calibs/reduced/'+str(obj_id)+'_'+'JN1_'+str(line + 1)+'.fits', clobber = True)
            array, header = cosmics.fromfits('Calibs/reduced/'+str(obj_id)+'_'+'JN1_'+str(line + 1)+'.fits')
            c = cosmics.cosmicsimage(array)
            c.run(maxiter = 1) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits('Calibs/reduced/'+str(obj_id)+'_'+'JN1_cos'+str(line + 1)+'.fits', c.cleanarray, header)
            
            
    #K_filter, Night 1
    print "reading in KN1..."    
    if len(aobj_idKN1) > 0:
        skyKN1 = pyfits.getdata('Calibs/sky/sky_'+str(obj_id)+'K_N1.fits')
        
        for line in range(len(aobj_idKN1)):
           sci = pyfits.getdata(Kobj_idN1_locs[line])[0:2048,0:2048]
           reduced_sci = (sci - bias - dark - skyKN1)/K_flat
           file = pyfits.PrimaryHDU(reduced_sci)
           file.writeto('Calibs/reduced/'+str(obj_id)+'_'+'KN1_'+str(line + 1)+'.fits', clobber = True)
           array, header = cosmics.fromfits('Calibs/reduced/'+str(obj_id)+'_'+'KN1_'+str(line + 1)+'.fits')
           c = cosmics.cosmicsimage(array)
           c.run(maxiter = 1) # can increase up to 4 to improve precision, but takes longer
           cosmics.tofits('Calibs/reduced/'+str(obj_id)+'_'+'KN1_cos'+str(line + 1)+'.fits', c.cleanarray, header)
        
    #J_filter, Night 2
    print "reading in JN2..."    
    if len(aobj_idJN2) > 0:
        skyJN2 = pyfits.getdata('Calibs/sky/sky_'+str(obj_id)+'J_N2.fits')
        
        for line in range(len(aobj_idJN2)):
            sci = pyfits.getdata(Jobj_idN2_locs[line])[0:2048,0:2048]
            reduced_sci = (sci - bias - dark - skyJN2)/J_flat
            file = pyfits.PrimaryHDU(reduced_sci)
            file.writeto('Calibs/reduced/'+str(obj_id)+'_'+'JN2_'+str(line + 1)+'.fits', clobber = True)
            array, header = cosmics.fromfits('Calibs/reduced/'+str(obj_id)+'_'+'JN2_'+str(line + 1)+'.fits')
            c = cosmics.cosmicsimage(array)
            c.run(maxiter = 1) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits('Calibs/reduced/'+str(obj_id)+'_'+'JN2_cos'+str(line + 1)+'.fits', c.cleanarray, header)

    #K_filter, Night 2
    print "reading in KN2..."
    if len(aobj_idKN2) > 0:
        skyKN2 = pyfits.getdata('Calibs/sky/sky_'+str(obj_id)+'K_N2.fits')
        
        for line in range(len(aobj_idKN2)):
            sci = pyfits.getdata(Kobj_idN2_locs[line])[0:2048,0:2048]
            reduced_sci = (sci - bias - dark - skyKN2)/K_flat
            file = pyfits.PrimaryHDU(reduced_sci)
            file.writeto('Calibs/reduced/'+str(obj_id)+'_'+'KN2_'+str(line + 1)+'.fits', clobber = True)
            array, header = cosmics.fromfits('Calibs/reduced/'+str(obj_id)+'_'+'KN2_'+str(line + 1)+'.fits')
            c = cosmics.cosmicsimage(array)
            c.run(maxiter = 1) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits('Calibs/reduced/'+str(obj_id)+'_'+'KN2_cos'+str(line + 1)+'.fits', c.cleanarray, header)
            
    #J_filter, Night 3
    print "reading in JN3..."  
    if len(aobj_idJN3) > 0:
        skyJN3 = pyfits.getdata('Calibs/sky/sky_'+str(obj_id)+'J_N3.fits')
        
        for line in range(len(aobj_idJN3)):
           sci = pyfits.getdata(Jobj_idN3_locs[line])[0:2048,0:2048]
           reduced_sci = (sci - bias - dark - skyJN3)/J_flat
           file = pyfits.PrimaryHDU(reduced_sci)
           file.writeto('Calibs/reduced/'+str(obj_id)+'_'+'JN3_'+str(line + 1)+'.fits', clobber = True)
           array, header = cosmics.fromfits('Calibs/reduced/'+str(obj_id)+'_'+'JN3_'+str(line + 1)+'.fits')
           c = cosmics.cosmicsimage(array)
           c.run(maxiter = 1) # can increase up to 4 to improve precision, but takes longer
           cosmics.tofits('Calibs/reduced/'+str(obj_id)+'_'+'JN3_cos'+str(line + 1)+'.fits', c.cleanarray, header)
        
    if len(aobj_idKN3) > 0:
        skyKN3 = pyfits.getdata('Calibs/sky/sky_'+str(obj_id)+'K_N3.fits')
        
        for line in range(len(aobj_idKN3)):
            sci = pyfits.getdata(Kobj_idN3_locs[line])[0:2048,0:2048]
            reduced_sci = (sci - bias - dark - skyKN3)/K_flat
            file = pyfits.PrimaryHDU(reduced_sci)
            file.writeto('Calibs/reduced/'+str(obj_id)+'_'+'KN3_'+str(line + 1)+'.fits', clobber = True)
            array, header = cosmics.fromfits('Calibs/reduced/'+str(obj_id)+'_'+'KN3_'+str(line + 1)+'.fits')
            c = cosmics.cosmicsimage(array)
            c.run(maxiter = 1) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits('Calibs/reduced/'+str(obj_id)+'_'+'KN3_cos'+str(line + 1)+'.fits', c.cleanarray, header)
コード例 #17
0
ファイル: find_cr.py プロジェクト: DmB/CosmicRayExtractor
#!/usr/bin/python

from dbauth import *
import cosmics
import os

file_name = 'a.fits'

# some comments on params are here http://obswww.unige.ch/~tewes/cosmics_dot_py/
pssl = 0.0
ccd_gain = 2.687
ccd_readnoise = 8.14
sigclip = 5.0
sigfrac = 0.2
objlim = 3.0
satlevel = 30000
maxiter = 10
verbose = True

(array, header) = cosmics.fromfits(file_name, verbose=verbose)
c = cosmics.cosmicsimage(array, pssl=pssl, gain=ccd_gain, readnoise=ccd_readnoise, 
                         sigclip=sigclip, sigfrac=sigfrac, objlim=objlim,
                         satlevel=satlevel, verbose=verbose)

c.run(maxiter=maxiter, verbose=verbose)

#cleaned_file_name = "a_crcl.fits"
#cosmics.tofits(cleaned_file_name, c.cleanarray, header, verbose=verbose)

#c.make_plots('/disk/Work/Cosmic_rays/Code/CosmicRayExtractor/RP','a_crcl.png')
コード例 #18
0
ファイル: pab.py プロジェクト: gbrammer/pabeta
def prep():

    import glob
    import os

    import threedhst
    import threedhst.prep_flt_astrodrizzle as init
    from threedhst import catIO

    import unicorn
    import research.hawkiff

    ### Make ACS associations
    unicorn.candels.make_asn_files(uniquename=True, translate={'-ROT': ''})
    threedhst.options['FLT_PERSISTENCE_PATH'] = '../Persistence/'

    #### IR
    files = glob.glob('*-F1*asn.fits')
    radec = None
    for asn_file in files:
        init.prep_direct_grism_pair(
            direct_asn=asn_file,
            grism_asn=None,
            radec=radec,
            ACS=False,
            align_threshold=5,
            raw_path='../RAW/',
            order=0)

    ###
    for asn_file in files:
        drizzlepac.astrodrizzle.AstroDrizzle(
            asn_file,
            static=False,
            skysub=False,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            final_wht_type='IVM',
            clean=True,
            final_wcs=True,
            final_refimage='IRAS23436+5257-87-304-F814W_drc_sci.fits',
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

    ### Match WCS of NB observations
    init.copy_adriz_headerlets(
        glob.glob('*-F1*[MW]*asn.fits')[0],
        glob.glob('*-F1*[N]*asn.fits')[0],
        order=[1, 0])

    ### Flag negative pixels
    for asn_file in files:
        asn = threedhst.utils.ASNFile(asn_file)
        for exp in asn.exposures:
            im = pyfits.open('%s_flt.fits' % (exp), mode='update')
            neg = im['SCI'].data < -3 * im['ERR'].data
            print '%s #negative = %d' % (exp, neg.sum())
            im['DQ'].data[neg] |= 4096
            im.flush()
        #
        drizzlepac.astrodrizzle.AstroDrizzle(
            asn_file,
            skysub=True,
            static=True,
            driz_separate=True,
            driz_sep_wcs=True,
            median=True,
            blot=True,
            driz_cr=True,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_scale=0.1,
            final_rot=0,
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=64,
            preserve=False)
        sci = pyfits.open(asn_file.replace('_asn', '_drz_sci'), mode='update')
        wht = pyfits.open(asn_file.replace('_asn', '_drz_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### ACS / UVIS
    files = glob.glob('*-F6*asn.fits')
    files = glob.glob('*-F[48]*asn.fits')
    files.sort()
    for asn_file in files[::-1]:
        asn = threedhst.utils.ASNFile(asn_file)
        for i in range(len(asn.exposures)):
            asn.exposures[i] = asn.exposures[i].split('_flc')[0]

        print asn_file, asn.exposures
        asn.write(asn_file)

        if ('814' in asn_file) | ('625' in asn_file):
            radec = None
        else:
            root = glob.glob('*-F[86]*W*sci.fits')[0].split('_sci')[0]
            if not os.path.exists('%s_sci.cat' % (root)):
                os.system(
                    'cp /user/brammer/software/share/gauss_5.0_9x9.conv .')
                research.hawkiff.make_catalog(
                    root=root,
                    sci_ext='sci',
                    threshold=3,
                    use_rms=False,
                    subtract_background=True)
                cat = catIO.Table('%s_sci.cat' % (root))
                cat['X_WORLD', 'Y_WORLD'].write(
                    'ref_opt.radec', format='ascii.commented_header')

            radec = 'ref_opt.radec'

        radec = None
        init.prep_direct_grism_pair(
            direct_asn=asn_file,
            grism_asn=None,
            radec=radec,
            ACS=True,
            align_threshold=5,
            raw_path='../RAW/')
    #
    for asn_file in files:
        radec = None
        init.prep_direct_grism_pair(
            direct_asn=asn_file,
            grism_asn=None,
            radec=radec,
            ACS=True,
            align_threshold=5,
            raw_path='../RAW/')

    files = glob.glob('*sci.fits')
    ims = {}
    for file in files:
        im = pyfits.open(file)
        filter = file.split('-')[-1][:5]
        pix = np.sqrt(im[0].header['CD1_1']**2 +
                      im[0].header['CD1_2']**2) * 3600
        scl = im[0].header['PHOTFLAM'] / 1.e-19 * (0.1 / pix)**2
        im[0].data *= scl
        ims[filter] = im

    ### Try Multidrizzle CR with combined image
    import drizzlepac

    asn_files = glob.glob('*-F[4-8]*asn.fits')
    exposures = []
    for file in asn_files:
        asn = threedhst.utils.ASNFile(file)
        exposures.extend(asn.exposures)

    target = '-'.join(file.split('-')[:-1])
    asn.exposures = exposures
    asn.product = target
    asn_file = '%s_asn.fits' % (asn.product)
    asn.write(asn_file, clobber=True)

    ### modify:  make copy of FLCs, make new mosaic and then copy FLCs back
    ##xxx

    drizzlepac.astrodrizzle.AstroDrizzle(
        asn_file,
        skysub=False,
        clean=True,
        final_wcs=True,
        final_scale=0.05,
        final_pixfrac=0.8,
        context=False,
        resetbits=4096,
        final_bits=576,
        preserve=False)

    #
    bits = 576

    drizzlepac.astrodrizzle.AstroDrizzle(
        asn_file,
        static=False,
        skysub=False,
        driz_separate=False,
        driz_sep_wcs=False,
        median=False,
        blot=False,
        driz_cr=False,
        driz_combine=True,
        final_wht_type='IVM',
        clean=True,
        final_wcs=True,
        final_rot=0,
        final_scale=0.1,
        final_pixfrac=1,
        context=False,
        resetbits=0,
        final_bits=bits,
        preserve=False)

    #### rerun final mosaics
    refimage = '%s_drz_sci.fits' % (target)

    for file in asn_files:
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            static=False,
            skysub=False,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            final_wht_type='IVM',
            clean=True,
            final_wcs=True,
            final_refimage=refimage,
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=bits,
            preserve=False)

    for file in asn_files:
        print file
        sci = pyfits.open(
            glob.glob(file.replace('_asn', '_dr?_sci'))[0], mode='update')
        wht = pyfits.open(sci.filename().replace('_sci', '_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### Try own driz CR
    import stwcs
    from drizzlepac import astrodrizzle, quickDeriv
    import scipy.ndimage as nd

    ref_file = 'ESO550-IG02-13-083-F435W_drc_sci.fits'
    ref_file = 'ESO550-IG02-13-083-F814W_drc_sci.fits'
    ref_file = 'ESO550-IG02-13-083_drc_sci.fits'
    ref_ext = 0
    ref = pyfits.open(ref_file)
    ref_wcs = stwcs.wcsutil.HSTWCS(ref, ext=ref_ext)

    asn_file = 'ESO550-IG02-13-083-F814W_asn.fits'
    asn = threedhst.utils.ASNFile(asn_file)
    for exp in asn.exposures:
        flt = pyfits.open('%s_flc.fits' % (exp))  #, mode='update')
        for ext in [1, 2]:
            flt_wcs = stwcs.wcsutil.HSTWCS(flt, ext=('sci', ext))
            blotted_ref = astrodrizzle.ablot.do_blot(
                ref[ref_ext].data,
                ref_wcs,
                flt_wcs,
                1,
                coeffs=True,
                interp='nearest',
                sinscl=1.0,
                stepsize=10,
                wcsmap=None)
            #
            blotDeriv = quickDeriv.qderiv(blotted_ref)

            scl = flt['sci', ext].header['PHOTFLAM'] / ref[0].header[
                'PHOTFLAM'] / flt[0].header['EXPTIME']
            noise = flt['err', ext].data

            driz_scale = 1.2
            driz_snr = 3.5

            driz_scale = 1.0
            driz_snr = 6
            driz_f = 2

            # |data_image - blotted_image| > scale x deriv + SNR x noise
            abs = np.abs(flt['sci', ext].data * scl - blotted_ref * driz_f)
            xderiv = driz_scale * blotDeriv
            xsnr = driz_snr * scl * noise

            mask = abs > (xderiv + xsnr)
            new_dq = mask & ((flt['dq', ext].data & 4096) == 0)

    ### Try "cosmics" LA Cosmic
    im = pyfits.open('ESO550-IG02-13-083-F814W_drc_sci.fits')
    slx, sly = slice(1500, 2600), slice(1800, 2800)
    ext = 0
    subim = im[ext].data[sly, slx]
    h = im[ext].header
    subim *= im[0].header['EXPTIME']

    #### FLT
    im = pyfits.open('j9cv13pcq_flc.fits')
    ext = 0
    slx, sly = slice(1500, 2600), slice(0, 400)
    subim = im[4].data[sly, slx]
    h = im[0].header

    import cosmics
    c = cosmics.cosmicsimage(
        subim,
        pssl=20.0,
        gain=1,
        readnoise=h['READNSEA'],
        sigclip=4.0,
        sigfrac=0.3,
        objlim=3.8,
        satlevel=84700.0,
        verbose=True)
    c.run(maxiter=4)

    crflux = subim * c.mask
    plt.hist(np.log10(crflux[c.mask]), range=[1, 6], bins=100, alpha=0.5)

    ####
    import research.pab.pab
    files = glob.glob('*-F*asn.fits')
    #files=glob.glob('IC*-F814*asn.fits')
    for file in files:
        asn = threedhst.utils.ASNFile(file)
        for exp in asn.exposures:
            flc = pyfits.open('%s_flc.fits' % (exp))
            research.pab.pab.run_lacosmic(
                '%s_flc.fits' % (exp),
                split=2048,
                sigclip=5,
                pssl=flc[1].header['MDRIZSK0'])

        #
        drizzlepac.updatehdr.update_from_shiftfile(
            file.replace('asn.fits', 'shifts.txt'))
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            skysub=True,
            clean=True,
            final_wcs=True,
            final_scale=0.05,
            final_pixfrac=0.8,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            output='sub_' + file.split('_asn')[0],
            skysub=True,
            static=True,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_scale=0.08,
            final_ra=60.635233,
            final_dec=60.344597,
            final_outnx=800,
            final_outny=800,
            final_rot=0,
            final_pixfrac=0.8,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

    ### Final mosaics
    f110w = glob.glob('*110W*asn.fits')[0]
    drizzlepac.astrodrizzle.AstroDrizzle(
        f110w,
        output='sub_' + f110w.split('_asn')[0],
        skysub=True,
        static=True,
        driz_separate=False,
        driz_sep_wcs=False,
        median=False,
        blot=False,
        driz_cr=False,
        driz_combine=True,
        clean=True,
        final_wcs=True,
        final_scale=0.1,
        final_rot=0,
        final_pixfrac=1,
        context=False,
        resetbits=0,
        final_bits=576 - 512,
        preserve=False)

    files = glob.glob('*-F*asn.fits')
    for file in files:
        if 'F110W' in file:
            continue
        #
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            output='sub_' + file.split('_asn')[0],
            skysub=True,
            static=True,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_refimage='sub_' + f110w.replace('_asn', '_drz_sci'),
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=576 - 512 * ('-F1' in file),
            preserve=False)

    for sci_im in glob.glob('sub*sci.fits'):
        print sci_im
        sci = pyfits.open(sci_im, mode='update')
        wht = pyfits.open(sci_im.replace('_sci', '_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### Test alignment
    import align
    ra_list, de_list = np.loadtxt('f814w.radec', unpack=True)
    for geom in ['shift', 'shift', 'rxyscale', 'shift', 'shift']:
        align.get_align_to_subaru(
            sci='sub_ESO550-IG025-41-115-F673N_drc_sci.fits',
            wht='sub_ESO550-IG025-41-115-F673N_drc_wht.fits',
            verbose=False,
            fitgeometry=geom,
            align_data=(ra_list, de_list, ra_list * 0),
            THRESH=1.2)

    #drizzlepac.tweakback.tweakback('ESO550-IG025-41-115-F673N_drc_sci.fits', force=True, verbose=True) #, origwcs='DRZWCS')

    ra_list, de_list = np.loadtxt('f110w.radec', unpack=True)
    for geom in ['shift', 'shift', 'rxyscale', 'shift', 'shift']:
        align.get_align_to_subaru(
            sci='sub_ESO550-IG025-41-115-F132N_drz_sci.fits',
            wht='sub_ESO550-IG025-41-115-F132N_drz_wht.fits',
            verbose=False,
            fitgeometry=geom,
            align_data=(ra_list, de_list, ra_list * 0),
            THRESH=1.2)
コード例 #19
0
ファイル: esi_cosmic.py プロジェクト: marlageha/dataredux
def esi_cosmic(date):
    

    #Get edge masks from file
    orders_mask = pickle.load(open(str(date)+'/Calibs/orders_mask_'+str(date)+'.p', 'rb'))
    background_mask = pickle.load(open(str(date)+'/Calibs/background_mask_'+str(date)+'.p', 'rb'))

    #Bias
    bias = pyfits.getdata(str(date)+'/Calibs/bias_'+str(date)+'.fits')

    #Normalized Flat
    flat = pyfits.getdata(str(date)+'/Calibs/norm_flat_'+str(date)+'.fits')

    #READ LOG
    im1 = open(str(date)+'/Logs/esi_info_'+str(date)+'.dat','r')
    data1 = im1.readlines()
    im1.close()

    filename = []
    dateobs = []
    objname = []
    imgtype = []
    ra = []
    dec = []
    exptime = []
    usable = []

    for line in data1:
        p = line.split()
        filename.append(p[0])
        dateobs.append(p[1])
        objname.append(p[2])
        imgtype.append(p[3])
        ra.append(p[4])
        dec.append(p[5])
        exptime.append(p[6])
        usable.append(p[7])

    #Rewrite in a more convenient array with format array[line][element]    
    alldata = []

    for line in range(len(usable)):
        alldata.append([filename[line],dateobs[line],objname[line],imgtype[line],
                        ra[line],dec[line],exptime[line],usable[line]])
        
    #Find good files:
    good = []
    for line in range(len(alldata)):
       if "yes" in alldata[line][ 7]:
           good.append(alldata[line])
       
    #Find list of objects:
    names = []
    for line in range(len(alldata)):
        if ("Object" in alldata[line][3] and float(alldata[line][6]) > 600) or "*" in alldata[line][2]:
            names.append(alldata[line][2])
    objects = np.array(list(set(names)))
    objects.sort() #ascending order, modify in place 
    
    #Find list of lines
    names = []
    for line in range(len(alldata)):
        if "Line" in alldata[line][3]:
            names.append(alldata[line][2])
    lines = np.array(list(set(names)))
    lines.sort() #ascending order, modify in place
    
    #Find list of stars:
    names = []
    for line in range(len(alldata)):
        if "*" in alldata[line][2]:
            names.append(alldata[line][2])
    stars = np.array(list(set(names)))
    stars.sort() #ascending order, modify in place 
    
    #Make directory to hold decosmicified files
    if not os.path.exists(str(date)+'/Calibs/cosmicless/'):
        os.makedirs(str(date)+'/Calibs/cosmicless/')
    
    #Remove cosmics from each lamp
    for obj_id in lines:
        print "reducing " + str(obj_id) + '...'
    
        #Find files related to this object
        aobj_id = []
        for line in range(len(good)):
            if str(obj_id) in good[line][2]:
                aobj_id.append(good[line])
        print str(len(aobj_id)), "files for "+ str(obj_id)
    
        #Write path to each objects files:
        obj_locs = [str(date)+'/Raw/' + str(aobj_id[line][0]) for line in range(len(aobj_id))]
    
        #Read in and de-cosmify
        for line in range(len(obj_locs)):
        
            array, header = cosmics.fromfits(obj_locs[line])
            #array = array - bias #backwards for some reason
            c = cosmics.cosmicsimage(array, gain = 1.29, readnoise = 2.2, sigclip = 6, objlim = 5.0, sigfrac = 0.7, satlevel = 1e4)
            c.run(maxiter = 3) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits', c.cleanarray, header)
            
            #now zip it (to save space)
            f = pyfits.getdata(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits')
            hdu = pyfits.CompImageHDU(f)
            hdu.writeto(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits', clobber=True)

    #Remove cosmics from each object
    for obj_id in objects:
        print "reducing " + str(obj_id) + '...'
    
        #Find files related to this object
        aobj_id = []
        for line in range(len(good)):
            if str(obj_id) in good[line][2]:
                aobj_id.append(good[line])
        print str(len(aobj_id)), "files for "+ str(obj_id)
    
        #Write path to each objects files:
        obj_locs = [str(date)+'/Raw/' + str(aobj_id[line][0]) for line in range(len(aobj_id))]
    
        #Read in and de-cosmify
        for line in range(len(obj_locs)):
        
            array, header = cosmics.fromfits(obj_locs[line])
            #array = array - bias #backwards for some reason
            c = cosmics.cosmicsimage(array, gain = 1.29, readnoise = 2.2, sigclip = 3.8, objlim = 3.0, sigfrac = 0.7, satlevel = -1)
            c.run(maxiter = 3) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits', c.cleanarray, header)
            
            #now zip it (to save space)
            f = pyfits.getdata(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits')
            hdu = pyfits.CompImageHDU(f)
            hdu.writeto(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits', clobber=True)
            
    #Remove cosmics from each star
    for obj_id in stars:
        print "reducing " + str(obj_id) + '...'
    
        #Find files related to this object
        aobj_id = []
        for line in range(len(good)):
            if str(obj_id) == good[line][2]:
                aobj_id.append(good[line])
        print str(len(aobj_id)), "files for "+ str(obj_id)
    
        #Write path to each objects files:
        obj_locs = [str(date)+'/Raw/' + str(aobj_id[line][0]) for line in range(len(aobj_id))]
    
        #Read in and de-cosmify
        for line in range(len(obj_locs)):
        
            array, header = cosmics.fromfits(obj_locs[line])
            #array = array - bias #backwards for some reason
            c = cosmics.cosmicsimage(array, gain = 1.29, readnoise = 2.2, sigclip = 10, objlim = 7.0, sigfrac = 0.7, satlevel = 15000)
            c.run(maxiter = 3) # can increase up to 4 to improve precision, but takes longer
            cosmics.tofits(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits', c.cleanarray, header)
            
            #now zip it (to save space)
            f = pyfits.getdata(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits')
            hdu = pyfits.CompImageHDU(f)
            hdu.writeto(str(date)+'/Calibs/cosmicless/' + str(obj_id)+'_'+str(line+1)+'decos.fits', clobber=True)
コード例 #20
0
ファイル: pointing.py プロジェクト: adrn/MDM
    def reduce(self, overwrite=False):
        """ Reduce data from this pointing.

        """

        # solve for wavelength at every pixel
        science_wvln = self.wavelength_image
        ccd = self.night.observing_run.ccd

        n_flat = self.night.make_normalized_flat()
        path = self.night.redux_path

        for fn in self.file_paths:
            _data_fn = os.path.splitext(os.path.split(fn)[1])[0]
            data_fn = os.path.join(path, "{0}_2d.fit".format(_data_fn))
            self._data_file_paths[fn] = data_fn

            if os.path.exists(data_fn) and overwrite:
                os.remove(data_fn)

            if os.path.exists(data_fn):
                continue

            # subtract bias
            hdu = fits.open(fn)[0]
            frame_data = hdu.data
            hdr = hdu.header
            frame_data = ccd.zero_correct_frame(frame_data,
                                                self.night.master_zero)

            #TODO: frame.inverse_variance # automatically created?
            variance_image = frame_data*ccd.gain + ccd.read_noise**2
            inv_var = 1./variance_image

            # divide by master normalized flat
            frame_data /= n_flat
            inv_var *= n_flat**2

            # if the exposure was more than 60 seconds, run this cosmic
            #   ray flagger on the CCD data
            if hdr['EXPTIME'] > 60:
                c = cosmics.cosmicsimage(frame_data, gain=ccd.gain,
                                         readnoise=ccd.read_noise,
                                         sigclip=8.0, sigfrac=0.5,
                                         objlim=10.0)
                #c.run(maxiter=6)
                #frame_data = c.cleanarray

            # if exposure time > 60 seconds, do sky subtraction
            if hdr['EXPTIME'] > 60:
                pass

            # extract only the science data region
            science_data = frame_data[ccd.regions["science"]]
            science_inv_var = inv_var[ccd.regions["science"]]

            # write this back out as a 2D image
            new_hdu0 = fits.PrimaryHDU(science_data, header=hdr)
            new_hdu1 = fits.ImageHDU(science_wvln,
                            header=fits.Header([("NAME","wavelength")]))
            new_hdu2 = fits.ImageHDU(science_inv_var,
                            header=fits.Header([("NAME","inverse variance")]))
            hdul = fits.HDUList([new_hdu0,new_hdu1,new_hdu2])
            hdul.writeto(data_fn)