Beispiel #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
Beispiel #2
0
 def updateMDRIZSKY(self,filename=None): 
     if (filename == None): 
         filename = self.name     
     try: 
         _handle = fileutil.openImage(filename,mode='update',memmap=0) 
     except IOError:
         raise IOError, "Unable to open %s for sky level computation"%filename 
     # Get the exposure time for the image.  If the exposure time of the image 
     # is 0, set the MDRIZSKY value to 0.  Otherwise update the MDRIZSKY value 
     # in units of counts per second. 
     if (self.getExpTime() == 0.0): 
         str =  "*******************************************\n" 
         str += "*                                         *\n" 
         str += "* ERROR: Image EXPTIME = 0.               *\n" 
         str += "* MDRIZSKY header value cannot be         *\n" 
         str += "* converted to units of 'counts/s'        *\n" 
         str += "* MDRIZSKY will be set to a value of '0'  *\n" 
         str += "*                                         *\n" 
         str =  "*******************************************\n" 
         _handle[0].header['MDRIZSKY'] = 0 
         print str 
     else:
         # Assume the MDRIZSKY keyword is in the primary header.  Try to update 
         # the header value
         if (_handle[0].header['UNITCORR'].strip() == 'PERFORM'): 
             skyvalue = self.getSubtractedSky()/self.getExpTime() 
         else: 
             skyvalue = self.getSubtractedSky()
         # We need to convert back to native units if computations were done in electrons
         if self.proc_unit != "native":
             skyvalue = skyvalue/self.getGain()    
         print "Updating MDRIZSKY keyword to primary header with value %f"%(skyvalue) 
         _handle[0].header.update('MDRIZSKY',skyvalue)  
     _handle.close() 
Beispiel #3
0
    def updateMDRIZSKY(self, filename=None):

        if (filename == None):
            filename = self.name

        try:
            _handle = fileutil.openImage(filename,
                                         mode='update',
                                         memmap=self.memmap)
        except:
            raise IOError, "Unable to open %s for sky level computation" % filename
        try:
            try:
                # Assume MDRIZSKY lives in primary header
                print "Updating MDRIZSKY in %s with %f" % (
                    filename, self.getSubtractedSky())
                _handle[0].header['MDRIZSKY'] = self.getSubtractedSky()
            except:
                print "Cannot find keyword MDRIZSKY in %s to update" % filename
                print "Adding MDRIZSKY keyword to primary header with value %f" % self.getSubtractedSky(
                )
                _handle[0].header.update(
                    'MDRIZSKY',
                    self.getSubtractedSky(),
                    comment="Sky value subtracted by Multidrizzle")
        finally:
            _handle.close()
Beispiel #4
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() 
Beispiel #5
0
    def updateMDRIZSKY(self, filename=None):
        if (filename == None):
            filename = self.name
        try:
            _handle = fileutil.openImage(filename, mode='update', memmap=0)
        except IOError:
            raise IOError, "Unable to open %s for sky level computation" % filename
        # Get the exposure time for the image.  If the exposure time of the image
        # is 0, set the MDRIZSKY value to 0.  Otherwise update the MDRIZSKY value
        # in units of electrons per second.
        if (self.getExpTime() == 0.0):
            str = "*******************************************\n"
            str += "*                                         *\n"
            str += "* ERROR: Image EXPTIME = 0.               *\n"
            str += "* MDRIZSKY header value cannot be         *\n"
            str += "* converted to units of 'electrons/s'     *\n"
            str += "* MDRIZSKY will be set to a value of '0'  *\n"
            str += "*                                         *\n"
            str = "*******************************************\n"
            _handle[0].header['MDRIZSKY'] = 0
            print str
        else:
            skyvalue = self.getSubtractedSky()
            if self.proc_unit == 'electrons':
                skyvalue = skyvalue / self.getExpTime()

            print "Updating MDRIZSKY keyword to primary header with value %f" % (
                skyvalue)
            _handle[0].header.update('MDRIZSKY', skyvalue)
        _handle.close()
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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
Beispiel #9
0
    def _createInputCopies(self, files):
        """
        Creates copies of all input images.

        If a previous execution of multidrizzle has failed and _OrIg
        files already exist, before removing the _OrIg files, we will
        copy the 'sci' extensions out of those files _OrIg files and
        use them to overwrite what is currently in the existing
        input files.  This protects us against crashes in the HST
        pipeline where Multidrizzle is restarted after the sky
        has already been subtracted from the input files.
        """

        for _img in files:
            # Only make copies of files that exist
            if os.path.exists(_img):
                # Create filename for copy
                _copy = manager.modifyRootname(_img)
                # Make sure we remove any previous copies first,
                # after we copy 'sci' extension into the
                # possibly corrupted input file.  This
                # ensures that Multidrizzle restarts will
                # always have pristine input to use.
                if os.path.exists(_copy):
                    fimage = fileutil.openImage(_img, mode='update')
                    fcopy = fileutil.openImage(_copy)
                    index = 0
                    for extn in fcopy:
                        if extn.name.upper() == 'SCI':
                            fimage[index].data = fcopy[index].data
                        index += 1
                    fimage.close()
                    fcopy.close()
                    os.remove(_copy)

                # Copy file into new filename
                shutil.copyfile(_img, _copy)

    #def _checkInputFiles(self, files, updatewcs):
        """ Checks input files before they are required later. """
        """ Checks that MAKEWCS is run on any ACS image in 'files' list. """
        """
Beispiel #10
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
Beispiel #11
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()
Beispiel #12
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() 
Beispiel #13
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
Beispiel #14
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
Beispiel #15
0
    def updateMDRIZSKY(self, filename=None):

        if (filename == None):
            filename = self.name

        try:
            _handle = fileutil.openImage(filename, mode='update', memmap=0)
        except:
            raise IOError, "Unable to open %s for sky level computation" % filename

        # Compute the sky level subtracted from all the WFPC2 detectors based upon the reference plate scale.
        skyvalue = (self.getSubtractedSky() *
                    (self.refplatescale / self.platescale)**2)
        if self.proc_unit == 'electrons':
            skyvalue = skyvalue / self.getGain()

        print "Updating MDRIZSKY keyword in primary header with value %f" % skyvalue
        _handle[0].header.update(
            'MDRIZSKY',
            skyvalue,
            comment="Sky value subtracted by Multidrizzle")
        _handle.close()
Beispiel #16
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
Beispiel #17
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