Package multidrizzle :: Module nicmos_input
[hide private]
[frames] | no frames]

Source Code for Module multidrizzle.nicmos_input

  1  # 
  2  #   Authors: Christopher Hanley, David Grumm 
  3  #   Program: nicmos_input.py 
  4  #   Purpose: Class used to model NICMOS specific instrument data. 
  5  from __future__ import division # confidence high 
  6   
  7  from pytools import fileutil 
  8  from nictools import readTDD 
  9  import numpy as np 
 10   
 11  from input_image import InputImage 
 12   
 13   
14 -class NICMOSInputImage(InputImage):
15 16 SEPARATOR = '_' 17
18 - def __init__(self, input,dqname,platescale,memmap=0,proc_unit="native"):
19 InputImage.__init__(self,input,dqname,platescale,memmap=0,proc_unit=proc_unit) 20 21 # define the cosmic ray bits value to use in the dq array 22 self.cr_bits_value = 4096 23 24 # Detector parameters 25 self.platescale = platescale 26 self.full_shape = (256,256) 27 self.native_units = "COUNTS/S" 28 29 # no cte correction for NICMOS so set cte_dir=0. 30 self.cte_dir = 0 31 32 # All data is converted to electrons so use effective gain of 1 33 self._effGain = 1
34 35
36 - def updateMDRIZSKY(self,filename=None):
37 if (filename == None): 38 filename = self.name 39 try: 40 _handle = fileutil.openImage(filename,mode='update',memmap=0) 41 except IOError: 42 raise IOError, "Unable to open %s for sky level computation"%filename 43 # Get the exposure time for the image. If the exposure time of the image 44 # is 0, set the MDRIZSKY value to 0. Otherwise update the MDRIZSKY value 45 # in units of counts per second. 46 if (self.getExpTime() == 0.0): 47 str = "*******************************************\n" 48 str += "* *\n" 49 str += "* ERROR: Image EXPTIME = 0. *\n" 50 str += "* MDRIZSKY header value cannot be *\n" 51 str += "* converted to units of 'counts/s' *\n" 52 str += "* MDRIZSKY will be set to a value of '0' *\n" 53 str += "* *\n" 54 str = "*******************************************\n" 55 _handle[0].header['MDRIZSKY'] = 0 56 print str 57 else: 58 # Assume the MDRIZSKY keyword is in the primary header. Try to update 59 # the header value 60 if (_handle[0].header['UNITCORR'].strip() == 'PERFORM'): 61 skyvalue = self.getSubtractedSky()/self.getExpTime() 62 else: 63 skyvalue = self.getSubtractedSky() 64 # We need to convert back to native units if computations were done in electrons 65 if self.proc_unit != "native": 66 skyvalue = skyvalue/self.getGain() 67 print "Updating MDRIZSKY keyword to primary header with value %f"%(skyvalue) 68 _handle[0].header.update('MDRIZSKY',skyvalue) 69 _handle.close()
70
71 - def doUnitConversions(self):
72 # Image information 73 _handle = fileutil.openImage(self.name,mode='update',memmap=0) 74 _sciext = fileutil.getExtn(_handle,extn=self.extn) 75 76 # Determine if Multidrizzle is in units of counts/second or counts 77 # 78 # Counts per second case 79 if (_handle[0].header['UNITCORR'].strip() == 'PERFORM'): 80 # Multiply the values of the sci extension pixels by the gain. 81 print "Converting %s from COUNTS/S to ELECTRONS"%(self.name) 82 # If the exptime is 0 the science image will be zeroed out. 83 conversionFactor = (self.getExpTime() * self.getGain()) 84 85 # Counts case 86 else: 87 # Multiply the values of the sci extension pixels by the gain. 88 print "Converting %s from COUNTS to ELECTRONS"%(self.name) 89 # If the exptime is 0 the science image will be zeroed out. 90 conversionFactor = (self.getGain()) 91 92 np.multiply(_sciext.data,conversionFactor,_sciext.data) 93 94 # Set the BUNIT keyword to 'electrons' 95 _sciext.header.update('BUNIT','ELECTRONS') 96 97 # Update the PHOTFLAM value 98 photflam = _handle[0].header['PHOTFLAM'] 99 _handle[0].header.update('PHOTFLAM',(photflam/self.getGain())) 100 101 # Close the files and clean-up 102 _handle.close() 103
104 - def setInstrumentParameters(self, instrpars, pri_header):
105 """ This method overrides the superclass to set default values into 106 the parameter dictionary, in case empty entries are provided. 107 """ 108 if self._isNotValid (instrpars['gain'], instrpars['gnkeyword']): 109 instrpars['gnkeyword'] = 'ADCGAIN' 110 if self._isNotValid (instrpars['rdnoise'], instrpars['rnkeyword']): 111 instrpars['rnkeyword'] = None 112 if self._isNotValid (instrpars['exptime'], instrpars['expkeyword']): 113 instrpars['expkeyword'] = 'EXPTIME' 114 if instrpars['crbit'] == None: 115 instrpars['crbit'] = self.cr_bits_value 116 117 self._gain = self.getInstrParameter(instrpars['gain'], pri_header, 118 instrpars['gnkeyword']) 119 self._rdnoise = self.getInstrParameter(instrpars['rdnoise'], pri_header, 120 instrpars['rnkeyword']) 121 self._exptime = self.getInstrParameter(instrpars['exptime'], pri_header, 122 instrpars['expkeyword']) 123 self._crbit = instrpars['crbit'] 124 125 if self._gain == None or self._exptime == None: 126 print 'ERROR: invalid instrument task parameter' 127 raise ValueError 128 129 # We need to treat Read Noise as a special case since it is 130 # not populated in the NICMOS primary header 131 if (instrpars['rnkeyword'] != None): 132 self._rdnoise = self.getInstrParameter(instrpars['rdnoise'], pri_header, 133 instrpars['rnkeyword']) 134 else: 135 self._rdnoise = None 136 137 138 # We need to determine if the user has used the default readnoise/gain value 139 # since if not, they will need to supply a gain/readnoise value as well 140 141 usingDefaultReadnoise = False 142 if (instrpars['rnkeyword'] == None): 143 usingDefaultReadnoise = True 144 145 # Set the default readnoise values based upon the amount of user input given. 146 147 # User supplied no readnoise information 148 if usingDefaultReadnoise: 149 # Set the default gain and readnoise values 150 self._setchippars() 151 152 # Set the darkrate for the chips 153 self._setDarkRate() 154 155 # Convert the science data to electrons. 156 self.doUnitConversions()
157
158 - def _setchippars(self):
159 self._setDefaultReadnoise()
160
161 - def getflat(self):
162 """ 163 164 Purpose 165 ======= 166 Method for retrieving a detector's flat field. 167 168 This method will return an array the same shape as the 169 image. 170 171 :units: cps 172 173 """ 174 175 # The keyword for NICMOS flat fields in the primary header of the flt 176 # file is FLATFILE. This flat file is not already in the required 177 # units of electrons. 178 179 filename = self.header['FLATFILE'] 180 181 try: 182 handle = fileutil.openImage(filename,mode='readonly',memmap=0) 183 hdu = fileutil.getExtn(handle,extn=self.grp) 184 data = hdu.data[self.ltv2:self.size2,self.ltv1:self.size1] 185 except: 186 try: 187 handle = fileutil.openImage(filename[5:],mode='readonly',memmap=0) 188 hdu = fileutil.getExtn(handle,extn=self.grp) 189 data = hdu.data[self.ltv2:self.size2,self.ltv1:self.size1] 190 except: 191 data = np.ones(self.image_shape,dtype=self.image_dtype) 192 str = "Cannot find file "+filename+". Treating flatfield constant value of '1'.\n" 193 print str 194 195 flat = (1.0/data) # The flat field is normalized to unity. 196 197 return flat
198 199
200 - def getdarkcurrent(self):
201 """ 202 203 Purpose 204 ======= 205 Return the dark current for the NICMOS detectors. 206 207 :units: cps 208 209 """ 210 211 try: 212 darkcurrent = self.header['exptime'] * self.darkrate 213 214 except: 215 str = "#############################################\n" 216 str += "# #\n" 217 str += "# Error: #\n" 218 str += "# Cannot find the value for 'EXPTIME' #\n" 219 str += "# in the image header. NICMOS input #\n" 220 str += "# images are expected to have this header #\n" 221 str += "# keyword. #\n" 222 str += "# #\n" 223 str += "#Error occured in the NICMOSInputImage class#\n" 224 str += "# #\n" 225 str += "#############################################\n" 226 raise ValueError, str 227 228 229 return darkcurrent
230
231 - def getdarkimg(self):
232 """ 233 234 Purpose 235 ======= 236 Return an array representing the dark image for the detector. 237 238 :units: cps 239 240 """ 241 242 # Read the temperature dependeant dark file. The name for the file is taken from 243 # the TEMPFILE keyword in the primary header. 244 tddobj = readTDD.fromcalfile(self.name) 245 246 if tddobj == None: 247 return np.ones(self.image_shape,dtype=self.image_dtype)*self.getdarkcurrent() 248 else: 249 # Create Dark Object from AMPGLOW and Lineark Dark components 250 darkobj = tddobj.getampglow() + tddobj.getlindark() 251 252 # Return the darkimage taking into account an subarray information available 253 return darkobj[self.ltv2:self.size2,self.ltv1:self.size1]
254 255
256 -class NIC1InputImage(NICMOSInputImage):
257
258 - def __init__(self, input, dqname, platescale, memmap=0,proc_unit="native"):
259 NICMOSInputImage.__init__(self,input,dqname,platescale,memmap=0,proc_unit=proc_unit) 260 self.instrument = 'NICMOS/1'
261
262 - def _setDarkRate(self):
263 self.darkrate = 0.08 #electrons/s
264
265 - def _setDefaultReadnoise(self):
266 self._rdnoise = 26.0 # electrons
267
268 -class NIC2InputImage(NICMOSInputImage):
269 - def __init__(self, input, dqname, platescale, memmap=0,proc_unit="native"):
270 NICMOSInputImage.__init__(self,input,dqname,platescale,memmap=0,proc_unit=proc_unit) 271 self.instrument = 'NICMOS/2'
272
273 - def _setDarkRate(self):
274 self.darkrate = 0.08 #electrons/s
275
276 - def _setDefaultReadnoise(self):
277 self._rdnoise = 26.0 #electrons
278
279 -class NIC3InputImage(NICMOSInputImage):
280 - def __init__(self, input, dqname, platescale, memmap=0,proc_unit="native"):
281 NICMOSInputImage.__init__(self,input,dqname,platescale,memmap=0,proc_unit=proc_unit) 282 self.instrument = 'NICMOS/3'
283
284 - def _setDarkRate(self):
285 self.darkrate = 0.15 #electrons/s
286
287 - def _setDefaultReadnoise(self):
288 self._rdnoise = 29.0 # electrons
289