Пример #1
0
    def runDrizCR(self, blotted_array, mask_array, drizcrpars, skypars,
                  corr_file, cr_file):
        """ Run 'deriv' and 'driz_cr' to create cosmic-ray mask for this image. """

        _deriv_array = None

        print "Working on image ", self.datafile, "..."
        _deriv_array = quickDeriv.qderiv(blotted_array)

        # Open input image and get pointer to SCI data
        handle = fileutil.openImage(self.name,
                                    mode='readonly',
                                    memmap=self.memmap)
        scihdu = fileutil.getExtn(handle, extn=self.extn)

        tmpDriz_cr = driz_cr.DrizCR(scihdu.data,
                                    scihdu.header,
                                    blotted_array,
                                    _deriv_array,
                                    mask_array,
                                    gain=self.getEffGain(),
                                    grow=drizcrpars['driz_cr_grow'],
                                    ctegrow=drizcrpars['driz_cr_ctegrow'],
                                    ctedir=self.cte_dir,
                                    amp=self.amp,
                                    rn=self.getReadNoise(),
                                    SNR=drizcrpars['driz_cr_snr'],
                                    backg=self.getSubtractedSky(),
                                    scale=drizcrpars['driz_cr_scale'])

        # If the user provided a None value for the cr bit, don't
        # update the dqarray
        if (self.getCRbit() != 0):
            # Update the dq information if there is a dq array to update.
            # In the case of WFPC2 input, no DQ file may have been provided.
            # For ACS, there will always be DQ array information in the FLT file.
            if fileutil.findFile(self.dqfile_fullname):
                dqhandle = fileutil.openImage(self.dqfile_name,
                                              mode='update',
                                              memmap=self.memmap)
                dqarray = fileutil.getExtn(dqhandle, extn=self.dqfile_extn)
                tmpDriz_cr.updatedqarray(dqarray.data, self.getCRbit())
                dqhandle.close()
        else:
            print "  CR bit value of 0 specified.  Skipping DQ array updates."

        if (corr_file != None):
            tmpDriz_cr.createcorrfile(corr_file, self.header)
        if (cr_file != None):
            tmpDriz_cr.createcrmaskfile(cr_file, self.header)

        del tmpDriz_cr

        # Close input image filehandle
        if handle:
            del scihdu
            handle.close()
            del handle
        if _deriv_array != None:
            del _deriv_array
Пример #2
0
    def getdarkimg(self):
        """
        
        Purpose
        =======
        Return an array representing the dark image for the detector.
        
        :units: cps
        
        """

        # First attempt to get the dark image specified by the "DARKFILE"
        # keyword in the primary keyword of the science data.
        try:
            filename = self.header["DARKFILE"]
            handle = fileutil.openImage(filename, mode='readonly', memmap=0)
            hdu = fileutil.getExtn(handle, extn="sci")
            darkobj = hdu.data[self.ltv2:self.size2, self.ltv1:self.size1]
        # If the darkfile cannot be located, create the dark image from
        # what we know about the detector dark current and assume a
        # constant dark current for the whole image.
        except:
            darkobj = np.ones(self.image_shape,
                              dtype=self.image_dtype) * self.getdarkcurrent()
        return darkobj
Пример #3
0
    def doUnitConversions(self): 
        # Image information        
        _handle = fileutil.openImage(self.name,mode='update',memmap=0) 
        _sciext = fileutil.getExtn(_handle,extn=self.extn)         

        # Determine if Multidrizzle is in units of counts/second or counts 
        # 
        # Counts per second case 
        if (_handle[0].header['UNITCORR'].strip() == 'PERFORM'):         
            # Multiply the values of the sci extension pixels by the gain. 
            print "Converting %s from COUNTS/S to ELECTRONS"%(self.name) 
            # If the exptime is 0 the science image will be zeroed out. 
            conversionFactor = (self.getExpTime() * self.getGain())

        # Counts case 
        else:
            # Multiply the values of the sci extension pixels by the gain. 
            print "Converting %s from COUNTS to ELECTRONS"%(self.name) 
            # If the exptime is 0 the science image will be zeroed out. 
            conversionFactor = (self.getGain())  

        np.multiply(_sciext.data,conversionFactor,_sciext.data)
        
        # Set the BUNIT keyword to 'electrons'
        _sciext.header.update('BUNIT','ELECTRONS')

        # Update the PHOTFLAM value
        photflam = _handle[0].header['PHOTFLAM']
        _handle[0].header.update('PHOTFLAM',(photflam/self.getGain()))
        
        # Close the files and clean-up
        _handle.close() 
Пример #4
0
    def computeSky(self, skypars):
        """ Compute the sky value based upon the sci array of the chip"""

        # Open input image and get pointer to SCI data
        #
        #
        try:
            _handle = fileutil.openImage(self.name,
                                         mode='update',
                                         memmap=self.memmap)
            _sciext = fileutil.getExtn(_handle, extn=self.extn)
        except:
            raise IOError, "Unable to open %s for sky level computation" % self.name

        _tmp = ImageStats(_sciext.data,
                          fields=skypars['skystat'],
                          lower=skypars['skylower'],
                          upper=skypars['skyupper'],
                          nclip=skypars['skyclip'],
                          lsig=skypars['skylsigma'],
                          usig=skypars['skyusigma'],
                          binwidth=skypars['skywidth'])

        self._computedsky = self._extractSkyValue(_tmp,
                                                  skypars['skystat'].lower())
        print "Computed sky value for ", self.name, " : ", self._computedsky

        # Close input image filehandle
        _handle.close()
        del _sciext, _handle
Пример #5
0
    def getflat(self):
        """

        Purpose
        =======
        Method for retrieving a detector's flat field.
        
        This method will return an array the same shape as the
        image.
        

        """

        # The keyword for WFPC2 flat fields in the primary header of the flt
        # file is FLATFILE.  This flat file is *not* already in the required
        # units of electrons.

        filename = self.header['FLATFILE']

        try:
            handle = fileutil.openImage(filename,
                                        mode='readonly',
                                        writefits=False,
                                        memmap=0)
            hdu = fileutil.getExtn(handle, extn=self.grp)
            data = hdu.data[self.ltv2:self.size2, self.ltv1:self.size1]
        except:
            try:
                handle = fileutil.openImage(filename[5:],
                                            mode='readonly',
                                            writefits=False,
                                            memmap=0)
                hdu = fileutil.getExtn(handle, extn=self.grp)
                data = hdu.data[self.ltv2:self.size2, self.ltv1:self.size1]
            except:
                data = np.ones(self.image_shape, dtype=self.image_dtype)
                str = "Cannot find file " + filename + ".  Treating flatfield constant value of '1'.\n"
                print str
        # For the WFPC2 flat we need to invert
        # for use in Multidrizzle
        flat = (1.0 / data)
        return flat
Пример #6
0
    def getflat(self):
        """

        Purpose
        =======
        Method for retrieving a detector's flat field.
        
        This method will return an array the same shape as the
        image.

        :units: electrons

        """

        # The keyword for WFC3 IR flat fields in the primary header of the flt
        # file is PFLTFILE.  This flat file is not already in the required
        # units of electrons.

        filename = self.header['PFLTFILE']

        try:
            handle = fileutil.openImage(filename, mode='readonly', memmap=0)
            hdu = fileutil.getExtn(handle, extn=self.grp)
            flat = hdu.data[self.ltv2:self.size2, self.ltv1:self.size1]
        except:
            try:
                handle = fileutil.openImage(filename[5:],
                                            mode='readonly',
                                            memmap=0)
                hdu = fileutil.getExtn(handle, extn=self.grp)
                flat = hdu.data[self.ltv2:self.size2, self.ltv1:self.size1]
            except:
                flat = np.ones(self.image_shape, dtype=self.image_dtype)
                str = "Cannot find file " + filename + ".  Treating flatfield constant value of '1'.\n"
                print str

        #flat = (1.0/data) # The flat field is normalized to unity.

        return flat
Пример #7
0
    def doUnitConversions(self):
        # Image information
        _handle = fileutil.openImage(self.name, mode='update', memmap=0)
        _sciext = fileutil.getExtn(_handle, extn=self.extn)

        # Set the BUNIT keyword to 'electrons'
        _sciext.header.update('BUNIT', 'ELECTRONS')
        self.header.update('BUNIT', 'ELECTRONS')

        # Counts case
        np.multiply(_sciext.data, self.getExpTime(), _sciext.data)

        # Close the files and clean-up
        _handle.close()
Пример #8
0
    def doUnitConversions(self): 
        # Image information 
        _handle = fileutil.openImage(self.name,mode='update',memmap=0) 
        _sciext = fileutil.getExtn(_handle,extn=self.extn)         

        # Multiply the values of the sci extension pixels by the gain. 
        print "Converting %s from COUNTS to ELECTRONS"%(self.name) 
        # If the exptime is 0 the science image will be zeroed out. 
        np.multiply(_sciext.data,self.getGain(),_sciext.data)

        # Set the BUNIT keyword to 'electrons'
        _sciext.header.update('BUNIT','ELECTRONS')
        
        # Close the files and clean-up
        _handle.close() 
Пример #9
0
 def subtractSky(self):
     try:
         try:
             _handle = fileutil.openImage(self.name,
                                          mode='update',
                                          memmap=self.memmap)
             _sciext = fileutil.getExtn(_handle, extn=self.extn)
             print "%s (computed sky,subtracted sky) : (%f,%f)" % (
                 self.name, self.getComputedSky(), self.getSubtractedSky())
             np.subtract(_sciext.data, self.getSubtractedSky(),
                         _sciext.data)
         except:
             raise IOError, "Unable to open %s for sky subtraction" % self.name
     finally:
         _handle.close()
         del _sciext, _handle
Пример #10
0
    def updateStaticMask(self, static_mask):
        """ This method updates a static mask passed as a parameter,
            with mask info derived from the [SCI] array. It also
            keeps a private static mask array appropriate for
            use with the [SCI] array when doing sky processing
            later on.
        """
        # Open input image and get pointer to SCI data
        handle = fileutil.openImage(self.name,
                                    mode='readonly',
                                    memmap=self.memmap)
        sciext = fileutil.getExtn(handle, extn=self.extn)

        # Add SCI array to static mask
        static_mask.addMember(sciext.data, self.signature())
        self.static_mask = static_mask

        # Close input image filehandle
        handle.close()
        del sciext, handle
Пример #11
0
    def getflat(self):
        """

        Purpose
        =======
        Method for retrieving a detector's flat field.  For STIS there are three 
        
        
        This method will return an array the same shape as the
        image.
        
        """

        # The keyword for STIS flat fields in the primary header of the flt
        
        lflatfile = self.header['LFLTFILE']
        pflatfile = self.header['PFLTFILE']
        
        # Try to open the file in the location specified by LFLTFILE.
        try:
            handle = fileutil.openImage(lflatfile,mode='readonly',memmap=0)
            hdu = fileutil.getExtn(handle,extn=self.extn)
            lfltdata = hdu.data
            if lfltdata.shape != self.image_shape:
                lfltdata = interp2d.expand2d(lfltdata,self.image_shape)
        except:
            # If the user forgot to specifiy oref try looking for the reference
            # file in the current directory
            try:
                handle = fileutil.openImage(lfltfile[5:],mode='readonly',memmap=0)
                hdu = fileutil.getExtn(handle,extn=self.extn)
                lfltdata = hdu.data
            # No flat field was found.  Assume the flat field is a constant value of 1.
            except:
                lfltdata = np.ones(self.image_shape,dtype=self.image_dtype)
                str = "Cannot find file "+filename+".  Treating flatfield constant value of '1'.\n"
                print str
        
        # Try to open the file in the location specified by PFLTFILE.
        try:
            handle = fileutil.openImage(pflatfile,mode='readonly',memmap=0)
            hdu = fileutil.getExtn(handle,extn=self.extn)
            pfltdata = hdu.data
        except:
            # If the user forgot to specifiy oref try looking for the reference
            # file in the current directory
            try:
                handle = fileutil.openImage(pfltfile[5:],mode='readonly',memmap=0)
                hdu = fileutil.getExtn(handle,extn=self.extn)
                pfltdata = hdu.data
            # No flat field was found.  Assume the flat field is a constant value of 1.
            except:
                pfltdata = np.ones(self.image_shape,dtype=self.image_dtype)
                str = "Cannot find file "+filename+".  Treating flatfield constant value of '1'.\n"
                print str
        
        print "lfltdata shape: ",lfltdata.shape
        print "pfltdata shape: ",pfltdata.shape
        flat = lfltdata * pfltdata
        
        return flat
Пример #12
0
    def __init__(self,
                 input,
                 dqname,
                 platescale,
                 memmap=0,
                 proc_unit="native"):
        # These will always be populated by the appropriate
        # sub-class, however, this insures that these attributes
        # are not overlooked/forgotten.
        self.name = input
        self.memmap = memmap
        if not self.SEPARATOR:
            self.sep = DEFAULT_SEPARATOR
        else:
            self.sep = self.SEPARATOR

        self.instrument = None
        _fname, _extn = fileutil.parseFilename(input)
        self.dqfile_fullname = dqname
        self.dqfile_name, self.dqfile_extn = fileutil.parseFilename(dqname)
        self.extn = _extn
        self.grp = fileutil.parseExtn(_extn)[1]
        self.rootname = self.getRootname(_fname)
        self.datafile = _fname
        self.cr_bits_value = None
        self._effGain = None
        self.static_badval = 64
        self.static_mask = None
        self.cte_dir = 1

        # read amplifier to be used for HRC or STIS/CCD.
        try:
            self.amp = fileutil.getKeyword(input, 'CCDAMP')
        # set default if keyword missing
        except KeyError:
            # STIS default should be 'D' but 'C' and 'D' have the same readout direction so it's okay
            self.amp = 'C'

        # Define the platescale and reference plate scale for the detector.
        self.platescale = platescale
        self.refplatescale = platescale  # Default is to make each chip it's own reference value

        # Image information
        handle = fileutil.openImage(self.name,
                                    mode='readonly',
                                    memmap=self.memmap)
        sciext = fileutil.getExtn(handle, extn=self.extn)
        self.image_shape = sciext.data.shape
        self.image_type = sciext.data.dtype.name
        self.image_dtype = sciext.data.dtype.name

        # Retrieve a combined primary and extension header
        self.header = fileutil.getHeader(input, handle=handle)
        del sciext
        handle.close()
        del handle

        # Initialize sky background information keywords
        self._subtractedsky = 0.
        self._computedsky = None

        # Get image size information for possible subarray use
        try:
            self.ltv1 = self.header['LTV1'] * -1
            self.ltv2 = self.header['LTV2'] * -1
        except KeyError:
            self.ltv1 = 0
            self.ltv2 = 0
        self.size1 = self.header['NAXIS1'] + self.ltv1
        self.size2 = self.header['NAXIS2'] + self.ltv2

        # Set Units used for processing.  Options are "native" or "electrons"
        self.proc_unit = proc_unit