def testRaw(self): """Test retrieval of raw image""" frame = 0 for expId, mit in [(22657, True), (127073, False)]: for ccdNum in [2, 5]: butler = getButler(self.datadir, mit) raw = butler.get("raw", visit=expId, ccd=ccdNum) print "Visit: ", expId print "MIT detector: ", mit print "width: ", raw.getWidth() print "height: ", raw.getHeight() print "detector(amp) name: ", raw.getDetector().getId().getName() self.assertEqual(raw.getWidth(), self.sizes[mit][0]) # untrimmed self.assertEqual(raw.getHeight(), self.sizes[mit][1]) # untrimmed self.assertEqual(raw.getFilter().getFilterProperty().getName(), "i") self.assertEqual(raw.getDetector().getId().getName(), self.names[mit][ccdNum]) if display: ccd = raw.getDetector() for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame) frame += 1
def assertExposure(self, exp, ccd, checkFilter=True): print("dataId: ", self.dataId) print("ccd: ", ccd) print("width: ", exp.getWidth()) print("height: ", exp.getHeight()) print("detector name: ", exp.getDetector().getName()) print("filter name: ", exp.getFilter().getFilterProperty().getName()) self.assertEqual(exp.getWidth(), self.size[0]) self.assertEqual(exp.getHeight(), self.size[1]) self.assertEqual(exp.getDetector().getName(), "ccd%02d" % ccd) if checkFilter: self.assertEqual(exp.getFilter().getFilterProperty().getName(), self.filter) if display and ccd % 18 == 0: global frame frame += 1 ccd = cameraGeom.cast_Ccd(exp.getDetector()) for amp in ccd: amp = cameraGeom.cast_Amp(amp) print(ccd.getId(), amp.getId(), amp.getDataSec().toString(), amp.getBiasSec().toString(), amp.getElectronicParams().getGain()) cameraGeomUtils.showCcd(ccd, ccdImage=exp, frame=frame)
def testRaw(self): """Test retrieval of raw image""" frame = 0 for expId, mit in [(22657, True), (127073, False)]: for ccdNum in [2, 5]: butler = getButler(self.datadir, mit) raw = butler.get("raw", visit=expId, ccd=ccdNum) print "Visit: ", expId print "MIT detector: ", mit print "width: ", raw.getWidth() print "height: ", raw.getHeight() print "detector(amp) name: ", raw.getDetector().getId( ).getName() self.assertEqual(raw.getWidth(), self.sizes[mit][0]) # untrimmed self.assertEqual(raw.getHeight(), self.sizes[mit][1]) # untrimmed self.assertEqual(raw.getFilter().getFilterProperty().getName(), "i") self.assertEqual(raw.getDetector().getId().getName(), self.names[mit][ccdNum]) if display: ccd = raw.getDetector() for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame) frame += 1
def getAmp(exposure): """Get the Amp referred to by an exposure @param exposure Exposure to inspect @returns Amp """ det = exposure.getDetector() amp = cameraGeom.cast_Amp(det) # None if detector is not an Amp return amp
def detectorIsAmp(exposure): """Is the detector referred to by the exposure an Amp? @param exposure Exposure to inspect @returns True if exposure's detector is an Amp """ det = exposure.getDetector() amp = cameraGeom.cast_Amp(det) return False if amp is None else True
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing Saturation correction.") #grab exposure from clipboard exposure = clipboard.get(self.policy.getString("inputKeys.exposure")) exposure = ipIsr.convertImageForIsr(exposure) fwhm = self.policy.getDouble("parameters.defaultFwhm") amp = cameraGeom.cast_Amp(exposure.getDetector()) dataBbox = amp.getDataSec(True) x = dataBbox.getMinX() y = dataBbox.getMinY() height = dataBbox.getMaxY() - dataBbox.getMinY() width = dataBbox.getMaxY() - dataBbox.getMinX() dl = measAlg.DefectListT() defectList = cameraGeom.cast_Ccd(amp.getParent()).getDefects() if amp.getId().getIndex()[1] == 1: for d in defectList: d1 = measAlg.Defect(d.getBBox()) d1.shift(-x, -y) bbox = d1.getBBox() if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \ height - bbox.getMinY() - 1 > height or height - bbox.getMaxY() - 1 < 0: pass else: nd = measAlg.Defect(afwGeom.Box2I( afwGeom.Point2I(bbox.getMinX() + 4, height - bbox.getMinY() + 1), bbox.getDimensions())) dl.append(nd) else: for d in defectList: d1 = measAlg.Defect(d.getBBox()) d1.shift(-x, -y) bbox = d1.getBBox() if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \ bbox.getMinY() - 1 > height or bbox.getMaxY() - 1 < 0: pass else: nd = measAlg.Defect(afwGeom.Box2I( bbox.getMin() + afwGeom.Extent2I(4, 1), bbox.getDimensions())) dl.append(nd) saturation = amp.getElectronicParams().getSaturationLevel() ipIsr.maskBadPixelsDef(exposure, dl, fwhm, interpolate=True, maskName='BAD') ipIsr.saturationCorrection(exposure, int(saturation), fwhm, growSaturated=False, interpolate=True) #ds9.mtv(exposure, frame = 0, title = "my Amp") #exposure.writeFits("Amp.fits") #output products clipboard.put(self.policy.get("outputKeys.saturationCorrectedExposure"), exposure)
def haveAmp(exposure, amp): """Does the detector referred to by the exposure contain this particular amp? @param exposure Exposure to inspect @param amp Amp for comparison @returns True if exposure contains this amp """ det = exposure.getDetector() testAmp = cameraGeom.cast_Amp(det) if testAmp is None: # Exposure contains a CCD, which contains all its amps return True return True if testAmp.getId() == amp.getId() else False
def __init__(self, figure, camera, rect=(0.08, 0.08, 0.84, 0.84)): self.figure = figure self.camera = camera self.rect = rect self.detectors = {} self.amps = {} # use same rect as figure.add_axes(rect) to avoid confusion l, b, w, h = self.rect self.fig_left = l self.fig_bottom = b self.fig_width = w self.fig_height = h l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6 for r in self.camera: raft = camGeom.cast_Raft(r) for c in raft: ccd = camGeom.cast_Ccd(c) serial = ccd.getId().getSerial() self.detectors[serial] = ccd cc = ccd.getCenter().getPixels(ccd.getPixelSize()) cxc, cyc = cc.getX(), cc.getY() cbbox = ccd.getAllPixels(True) cwidth = cbbox.getMaxX() - cbbox.getMinX() cheight = cbbox.getMaxY() - cbbox.getMinY() l_cam = min(l_cam, cxc - cwidth/2) b_cam = min(b_cam, cyc - cheight/2) r_cam = max(r_cam, cxc + cwidth/2) t_cam = max(t_cam, cyc + cheight/2) self.amps[serial] = [] for a in ccd: amp = camGeom.cast_Amp(a) self.amps[serial].append(amp) self.camera_width = r_cam - l_cam self.camera_height = t_cam - b_cam extent = afwGeom.Extent2D(self.camera_width, self.camera_height) self.camera_bbox = afwGeom.Box2D(afwGeom.Point2D(0.0, 0.0), extent) #print "Camera", self.camera_bbox self.axes = {}
def testRaw(self): """Test retrieval of raw image""" for ccd in range(12): raw = self.butler.get("raw", self.dataId, ccd=ccd) self.assertExposure(raw, ccd) if display: ccd = cameraGeom.cast_Ccd(raw.getDetector()) for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame) frame += 1
def testRaw(self): """Test retrieval of raw image""" ccd = 'VIRGO1' raw = self.butler.get("raw", self.dataId, ccd=ccd) self.assertExposure(raw, ccd) if display: ccd = cameraGeom.cast_Ccd(raw.getDetector()) for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame) frame += 1
def getCcd(exposure): """Get the Ccd referred to by an exposure @param exposure Exposure to inspect @returns Ccd """ det = exposure.getDetector() ccd = cameraGeom.cast_Ccd(det) if ccd is not None: return ccd amp = cameraGeom.cast_Amp(det) if amp is not None: det = amp.getParent() ccd = cameraGeom.cast_Ccd(det) return ccd raise RuntimeError("Can't find Ccd from detector.")
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing CCD assembly.") #grab exposure from clipboard exposureList = clipboard.get(self.policy.getString("inputKeys.exposureList")) rmKeys = self.policy.getArray("parameters.deleteFieldsList") amp = cameraGeom.cast_Amp(exposureList[0].getDetector()) ccdId = amp.getParent().getId() ccd = cameraGeom.cast_Ccd(amp.getParent()) exposure = ipIsr.ccdAssemble.assembleCcd(exposureList, ccd, keysToRemove=rmKeys) #output products clipboard.put(self.policy.get("outputKeys.assembledCcdExposure"), exposure)
def __init__(self, figure, camera, rect=(0.08, 0.08, 0.84, 0.84)): self.figure = figure self.camera = camera self.rect = rect self.detectors = {} self.amps = {} # use same rect as figure.add_axes(rect) to avoid confusion l, b, w, h = self.rect self.fig_left = l self.fig_bottom = b self.fig_width = w self.fig_height = h l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6 for r in self.camera: raft = camGeom.cast_Raft(r) for c in raft: ccd = camGeom.cast_Ccd(c) serial = ccd.getId().getSerial() self.detectors[serial] = ccd cc = ccd.getCenter().getPixels(ccd.getPixelSize()) cxc, cyc = cc.getX(), cc.getY() cbbox = ccd.getAllPixels(True) cwidth = cbbox.getMaxX() - cbbox.getMinX() cheight = cbbox.getMaxY() - cbbox.getMinY() l_cam = min(l_cam, cxc - cwidth / 2) b_cam = min(b_cam, cyc - cheight / 2) r_cam = max(r_cam, cxc + cwidth / 2) t_cam = max(t_cam, cyc + cheight / 2) self.amps[serial] = [] for a in ccd: amp = camGeom.cast_Amp(a) self.amps[serial].append(amp) self.camera_width = r_cam - l_cam self.camera_height = t_cam - b_cam extent = afwGeom.Extent2D(self.camera_width, self.camera_height) self.camera_bbox = afwGeom.Box2D(afwGeom.Point2D(0.0, 0.0), extent) #print "Camera", self.camera_bbox self.axes = {}
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing Saturation correction.") #grab exposure from clipboard exposure = clipboard.get(self.policy.getString("inputKeys.exposure")) metadata = exposure.getMetadata() exposure = ipIsr.convertImageForIsr(exposure) fwhm = self.policy.getDouble("parameters.defaultFwhm") amp = cameraGeom.cast_Amp(exposure.getDetector()) saturation = amp.getElectronicParams().getSaturationLevel() bboxes = ipIsr.saturationDetection(exposure, int(saturation), doMask = True) self.log.log(Log.INFO, "Found %i saturated regions."%(len(bboxes))) #output products clipboard.put(self.policy.get("outputKeys.saturationMaskedExposure"), exposure)
def variance(self, exposure): """Set variance from gain @param exposure Exposure to process """ assert exposure, "No exposure provided" mi = exposure.getMaskedImage() if pipUtil.detectorIsCcd(exposure): ccd = pipUtil.getCcd(exposure) MaskedImage = type(mi) for amp in ccd: miAmp = MaskedImage(mi, amp.getDataSec(True), afwImage.LOCAL) self._varianceAmp(miAmp, amp) else: amp = cameraGeom.cast_Amp(exposure.getDetector()) self._varianceAmp(mi, amp) return
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing CCD assembly.") #grab exposure from clipboard exposureList = clipboard.get( self.policy.getString("inputKeys.exposureList")) rmKeys = self.policy.getArray("parameters.deleteFieldsList") amp = cameraGeom.cast_Amp(exposureList[0].getDetector()) ccdId = amp.getParent().getId() ccd = cameraGeom.cast_Ccd(amp.getParent()) exposure = ipIsr.ccdAssemble.assembleCcd(exposureList, ccd, keysToRemove=rmKeys) #output products clipboard.put(self.policy.get("outputKeys.assembledCcdExposure"), exposure)
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing Saturation correction.") #grab exposure from clipboard exposure = clipboard.get(self.policy.getString("inputKeys.exposure")) metadata = exposure.getMetadata() exposure = ipIsr.convertImageForIsr(exposure) fwhm = self.policy.getDouble("parameters.defaultFwhm") amp = cameraGeom.cast_Amp(exposure.getDetector()) saturation = amp.getElectronicParams().getSaturationLevel() bboxes = ipIsr.saturationDetection(exposure, int(saturation), doMask=True) self.log.log(Log.INFO, "Found %i saturated regions." % (len(bboxes))) #output products clipboard.put(self.policy.get("outputKeys.saturationMaskedExposure"), exposure)
def testRaw(self): """Test retrieval of raw image""" frame = 0 if display: cameraGeomUtils.showCamera(self.butler.mapper.camera, frame=frame) for side in ("N", "S"): for ccd in range(1, 32, 1): raw = self.butler.get("raw", self.dataId, side=side, ccd=ccd) self.assertExposure(raw, side, ccd) if display: frame += 1 ccd = cameraGeom.cast_Ccd(raw.getDetector()) for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame)
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing overscan subtraction.") #grab exposure and overscan bbox from clipboard exposure = clipboard.get(self.policy.getString("inputKeys.exposure")) fittype = self.policy.getString("parameters.overscanFitType") amp = cameraGeom.cast_Amp(exposure.getDetector()) overscanBbox = amp.getDiskBiasSec() dataBbox = amp.getDiskDataSec() #It just so happens that this is an o.k. place to put the SDQA #calculation because the ratings requested at the moment can all be #calculated here. If, for example, an Amp rating an the flat fielded #amp were requested, it would have to be calculated separately. ipIsr.calculateSdqaAmpRatings(exposure, overscanBbox, dataBbox) ipIsr.overscanCorrection(exposure, overscanBbox, fittype) #TODO optionally trim #output products clipboard.put(self.policy.get("outputKeys.overscanCorrectedExposure"), exposure)
def getCcd(exposure, allowRaise=True): """Get the Ccd referred to by an exposure @param exposure Exposure to inspect @param allowRaise If False, return None if the CCD can't be found rather than raising an exception @returns Ccd """ det = exposure.getDetector() ccd = cameraGeom.cast_Ccd(det) if ccd is not None: return ccd amp = cameraGeom.cast_Amp(det) if amp is not None: det = amp.getParent() ccd = cameraGeom.cast_Ccd(det) return ccd if allowRaise: raise RuntimeError("Can't find Ccd from detector.") else: return None
def trim(self, exposure): """Trim overscan out of exposure @param exposure Exposure to process """ assert exposure, "No exposure provided" mi = exposure.getMaskedImage() MaskedImage = type(mi) if pipUtil.detectorIsCcd(exposure): # Effectively doing CCD assembly since we have all amplifiers ccd = pipUtil.getCcd(exposure) miCcd = MaskedImage(ccd.getAllPixels(True).getDimensions()) for amp in ccd: diskDataSec = amp.getDiskDataSec() trimDataSec = amp.getElectronicDataSec(True) miTrim = MaskedImage(mi, diskDataSec, afwImage.LOCAL) miTrim = MaskedImage(amp.prepareAmpData(miTrim.getImage()), amp.prepareAmpData(miTrim.getMask()), amp.prepareAmpData(miTrim.getVariance())) miAmp = MaskedImage(miCcd, trimDataSec, afwImage.LOCAL) self.log.log( self.log.INFO, "Trimming amp %s: %s --> %s" % (amp.getId(), diskDataSec, trimDataSec)) miAmp <<= miTrim amp.setTrimmed(True) exposure.setMaskedImage(miCcd) else: # AFW doesn't provide a useful target datasec, so we just make an image that has the useful pixels amp = cameraGeom.cast_Amp(exposure.getDetector()) diskDataSec = amp.getDiskDataSec() self.log.log(self.log.INFO, "Trimming amp %s: %s" % (amp.getId(), diskDataSec)) miTrim = MaskedImage(mi, diskDataSec, afwImage.LOCAL) amp.setTrimmed(True) miAmp = MaskedImage(amp.prepareAmpData(miTrim.getImage()), amp.prepareAmpData(miTrim.getMask()), amp.prepareAmpData(miTrim.getVariance())) exposure.setMaskedImage(miAmp) return
def assertExposure(self, exp, ccd, checkFilter=True): print "dataId: ", self.dataId print "ccd: ", ccd print "width: ", exp.getWidth() print "height: ", exp.getHeight() print "detector name: ", exp.getDetector().getName() print "filter name: ", exp.getFilter().getFilterProperty().getName() self.assertEqual(exp.getWidth(), self.size[0]) self.assertEqual(exp.getHeight(), self.size[1]) self.assertEqual(exp.getDetector().getName(), "ccd%02d" % ccd) if checkFilter: self.assertEqual(exp.getFilter().getFilterProperty().getName(), self.filter) if display and ccd % 18 == 0: global frame frame += 1 ccd = cameraGeom.cast_Ccd(exp.getDetector()) for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() cameraGeomUtils.showCcd(ccd, ccdImage=exp, frame=frame)
def main(infile, ccds=None, camera="hsc", cmap="copper", cols=[0,1], nsig=3.0, percent=False, textcolor='k', out=None): ########################### # build the camera policy_file = { 'hsc': os.path.join(os.getenv("OBS_SUBARU_DIR"), "hsc", "hsc_geom.paf"), 'sc' : os.path.join(os.getenv("OBS_SUBARU_DIR"), "suprimecam", "Full_Suprimecam_geom.paf") } geomPolicy = afwCGU.getGeomPolicy(policy_file[camera.lower()]) camera = afwCGU.makeCamera(geomPolicy) ########################### # load the data data = {} if infile: load = numpy.loadtxt(infile) vals = load[:,cols[1]] for i in range(len(vals)): ccd = int(load[i,cols[0]]) if len(cols) == 3: amp = int(load[i,cols[2]]) else: amp = 0 if ccd not in data: data[ccd] = {} data[ccd][amp] = vals[i] else: vals = [] for r in camera: for c in afwCG.cast_Raft(r): ccd = afwCG.cast_Ccd(c) ccdId = ccd.getId().getSerial() data[ccdId] = {} val = 1.0 if ccdId > 103: val = 0.8 for a in ccd: amp = afwCG.cast_Amp(a) ampId = amp.getId().getSerial() - 1 data[ccdId][ampId] = val vals.append(val) if len(data[ccdId]) == 0: data[ccdId][0] = val vals.append(val) vals = numpy.array(vals) mean = vals.mean() med = numpy.median(vals) std = vals.std() vmin, vmax = med - nsig*std, med+nsig*std ########################### # make the plot fig = figure.Figure(figsize=(7,7)) canvas = FigCanvas(fig) if infile: rect = (0.06, 0.12, 0.76, 0.76) else: rect = (0.06, 0.06, 0.88, 0.88) fpa_fig = hscUtil.FpaFigure(fig, camera, rect=rect) for i_ccd, amplist in data.items(): hide = False if ccds and (i_ccd not in ccds): hide = True ax = fpa_fig.getAxes(i_ccd) fpa_fig.highlightAmp(i_ccd, 0) nq = fpa_fig.detectors[i_ccd].getOrientation().getNQuarter() nx, ny = 4, 8 if nq % 2: ny, nx = nx, ny firstAmp = sorted(amplist.keys())[0] im = numpy.zeros((ny, nx)) + amplist[firstAmp] print i_ccd for amp, val in amplist.items(): useVal = val if hide: useVal = 0.0 if nq == 0: im[:,amp] = useVal if nq == 1 or nq == -3: im[3-amp,:] = useVal if nq == 2: im[:,3-amp] = useVal if nq == -1 or nq == 3: im[amp,:] = useVal im_ax = ax.imshow(im, cmap=cmap, vmax=vmax, vmin=vmin, interpolation='nearest') fpa_fig.addLabel(i_ccd, [str(i_ccd)], color=textcolor) ############################# # the colorbar ylo, yhi = vmin, vmax if infile: rect = (0.91, 0.25, 0.02, 0.6) # real units cax = fig.add_axes(rect) cax.get_yaxis().get_major_formatter().set_useOffset(False) cax.set_ylim([ylo, yhi]) cax.get_xaxis().set_ticks([]) cbar = fig.colorbar(im_ax, cax=cax) # mirror the values. std or in percent (fractional) if requested caxp = cax.twinx() caxp.get_xaxis().set_ticks([]) caxp.get_yaxis().get_major_formatter().set_useOffset(False) if percent: caxp.set_ylim([(med-ylo)/(yhi-ylo), (yhi-med)/(yhi-ylo)]) else: caxp.set_ylim([-nsig*std, nsig*std]) for t in cax.get_yticklabels(): t.set_size('small') for t in caxp.get_yticklabels(): t.set_size("small") ################################# # add stats if infile: stats = {"Mean":mean, "Med":med, "Std":std, "Max":vals.max(), "Min":vals.min()} order = ["Mean", "Med", "Std", "Min", "Max"] i = 0 for k in order: v = stats[k] ax.text(0.8, 0.03+0.02*i, "%-10s %.3g"%(k+":",v), fontsize=9, horizontalalignment='left', verticalalignment='center', transform=fig.transFigure) i += 1 ################################# # title and write it date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") title = "File: %s Cols: %s %s" % (infile, ":".join([str(x+1) for x in cols]), date) if infile else "" fig.suptitle(title) outfile = out if out else "camview-%s.png" % infile fig.savefig(outfile)
def run(self, sensorRefList, calibType): """Process a calibration frame. @param sensorRef: sensor-level butler data reference @return pipe_base Struct containing these fields: - masterExpList: amp exposures of master calibration products """ referenceAmps = sensorRefList[0].subItems(level="channel") masterExpList = [] dataIdList = [] expmeta = None for amp in referenceAmps: if amp.dataId['snap'] == 1: continue self.log.info("Amp: Processing %s", amp.dataId) print "dataid %s" % (amp.dataId) butler = amp.butlerSubset.butler ampMIList = afwImage.vectorMaskedImageF() for sRef in sensorRefList: self.log.info("Sensor: Processing %s", sRef.dataId) ampSnapMIList = afwImage.vectorMaskedImageF() dataId = eval(amp.dataId.__repr__()) dataId['visit'] = sRef.dataId['visit'] for snap in (0, 1): dataId['snap'] = snap ampExposure = sRef.butlerSubset.butler.get('raw', dataId) if expmeta is None: expmeta = ampExposure.getMetadata() expfilter = ampExposure.getFilter() expcalib = ampExposure.getCalib() ampDetector = cameraGeom.cast_Amp( ampExposure.getDetector()) ampExposure = self.convertIntToFloat(ampExposure) ampExpDataView = ampExposure.Factory( ampExposure, ampDetector.getDiskDataSec()) self.saturationDetection(ampExposure, ampDetector) self.overscanCorrection(ampExposure, ampDetector) if calibType in ('flat', 'dark'): self.biasCorrection(ampExpDataView, amp) if False: self.darkCorrection(ampExpDataView, amp) self.updateVariance(ampExpDataView, ampDetector) ampSnapMIList.append(ampExpDataView.getMaskedImage()) ampMIList.append(self.combineMIList(ampSnapMIList)) masterFrame = self.combineMIList(ampMIList) # Fix saturation too??? self.fixDefectsAndSat(masterFrame, ampDetector) exp = afwImage.ExposureF(masterFrame) self.copyMetadata(exp, expmeta, calibType) exp.setDetector(ampDetector) exp.setWcs(afwImage.Wcs()) exp.setCalib(expcalib) if calibType is 'flat': exp.setFilter(expfilter) if self.config.doWrite and calibType is not 'flat': print "writing file %s" % dataId butler.put(exp, calibType, dataId=amp.dataId) masterExpList.append(exp) dataIdList.append(amp.dataId) if self.config.doWrite and calibType is 'flat': self.normChipAmps(masterExpList) for exp, dataId in zip(masterExpList, dataIdList): print "writing flat file %s" % dataId butler.put(exp, calibType, dataId) return pipeBase.Struct(masterFrameList=masterExpList, )
def testRaw(self): """Test retrieval of raw image""" frame = 1 for ccdNum, rotated in zip(self.ccdList, self.rotated): butler = getButler(self.datadir) raw = butler.get("raw", visit=self.expId, ccd=ccdNum) ccd = raw.getDetector() print "Visit: ", self.expId print "width: ", raw.getWidth() print "height: ", raw.getHeight() ccdName = ccd.getId().getName() print "detector name: ", ccdName self.assertEqual(raw.getWidth(), self.untrimmedSize[0]) self.assertEqual(raw.getHeight(), self.untrimmedSize[1]) self.assertEqual(raw.getFilter().getFilterProperty().getName(), "g") self.assertEqual(ccd.getId().getName(), "hsc%03d" % ccdNum) # CCD size trimmed = ccd.getAllPixelsNoRotation(True).getDimensions() self.assertEqual(trimmed.getX(), self.trimmedSize[0]) self.assertEqual(trimmed.getY(), self.trimmedSize[1]) # Size in camera coordinates camera = ccd.getAllPixels(True).getDimensions() self.assertEqual(camera.getX(), self.trimmedSize[0] if not rotated else self.trimmedSize[1]) self.assertEqual(camera.getY(), self.trimmedSize[1] if not rotated else self.trimmedSize[0]) for i, amp in enumerate(ccd): amp = cameraGeom.cast_Amp(amp) print "amp: %s %i"%(amp.getId(), i) # Amplifier on CCD datasec = amp.getAllPixels(True) self.assertEqual(datasec.getWidth(), self.ampSize[0] if not rotated else self.ampSize[1]) self.assertEqual(datasec.getHeight(), self.ampSize[1] if not rotated else self.ampSize[0]) self.assertEqual(datasec.getMinX(), i*self.ampSize[0] if not rotated else 0) self.assertEqual(datasec.getMinY(), 0 if not rotated else i*self.ampSize[0]) # Amplifier on disk datasec = amp.getRawDataBBox() self.assertEqual(datasec.getWidth(), self.ampSize[0]) self.assertEqual(datasec.getHeight(), self.ampSize[1]) if display: ds9.mtv(raw, frame=frame, title="Raw %s" % ccdName) frame += 1 for amp in ccd: amp = cameraGeom.cast_Amp(amp) print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \ amp.getBiasSec().toString(), amp.getElectronicParams().getGain() ccdIm = afwMath.rotateImageBy90(raw.getMaskedImage().getImage(), ccd.getOrientation().getNQuarter()) cameraGeomUtils.showCcd(ccd, ccdImage=ccdIm, frame=frame) frame += 1 if assemble: ccdIm = ca.assembleCcd([raw], ccd, reNorm=False, imageFactory=afwImage.ImageU) if display: cameraGeomUtils.showCcd(ccd, ccdImage=ccdIm, frame=frame) frame += 1 # If you'd like the image in un-rotated coordinates... rotCcdIm = afwMath.rotateImageBy90(ccdIm.getMaskedImage().getImage(), -ccd.getOrientation().getNQuarter()) ds9.mtv(rotCcdIm, frame=frame, title="Assembled derotated %s" % ccdName) frame += 1
def process(self, clipboard): """ """ self.log.log(Log.INFO, "Doing Saturation correction.") #grab exposure from clipboard exposure = clipboard.get(self.policy.getString("inputKeys.exposure")) exposure = ipIsr.convertImageForIsr(exposure) fwhm = self.policy.getDouble("parameters.defaultFwhm") amp = cameraGeom.cast_Amp(exposure.getDetector()) dataBbox = amp.getDataSec(True) x = dataBbox.getMinX() y = dataBbox.getMinY() height = dataBbox.getMaxY() - dataBbox.getMinY() width = dataBbox.getMaxY() - dataBbox.getMinX() dl = measAlg.DefectListT() defectList = cameraGeom.cast_Ccd(amp.getParent()).getDefects() if amp.getId().getIndex()[1] == 1: for d in defectList: d1 = measAlg.Defect(d.getBBox()) d1.shift(-x, -y) bbox = d1.getBBox() if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \ height - bbox.getMinY() - 1 > height or height - bbox.getMaxY() - 1 < 0: pass else: nd = measAlg.Defect( afwGeom.Box2I( afwGeom.Point2I(bbox.getMinX() + 4, height - bbox.getMinY() + 1), bbox.getDimensions())) dl.append(nd) else: for d in defectList: d1 = measAlg.Defect(d.getBBox()) d1.shift(-x, -y) bbox = d1.getBBox() if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \ bbox.getMinY() - 1 > height or bbox.getMaxY() - 1 < 0: pass else: nd = measAlg.Defect( afwGeom.Box2I(bbox.getMin() + afwGeom.Extent2I(4, 1), bbox.getDimensions())) dl.append(nd) saturation = amp.getElectronicParams().getSaturationLevel() ipIsr.maskBadPixelsDef(exposure, dl, fwhm, interpolate=True, maskName='BAD') ipIsr.saturationCorrection(exposure, int(saturation), fwhm, growSaturated=False, interpolate=True) #ds9.mtv(exposure, frame = 0, title = "my Amp") #exposure.writeFits("Amp.fits") #output products clipboard.put( self.policy.get("outputKeys.saturationCorrectedExposure"), exposure)
def run(self, sensorRefList, calibType): """Process a calibration frame. @param sensorRef: sensor-level butler data reference @return pipe_base Struct containing these fields: - masterExpList: amp exposures of master calibration products """ referenceAmps = sensorRefList[0].subItems(level="channel") masterExpList = [] dataIdList = [] expmeta = None for amp in referenceAmps: if amp.dataId['snap'] == 1: continue self.log.log(self.log.INFO, "Amp: Processing %s" % (amp.dataId)) print "dataid %s"%(amp.dataId) butler = amp.butlerSubset.butler ampMIList = afwImage.vectorMaskedImageF() for sRef in sensorRefList: self.log.log(self.log.INFO, "Sensor: Processing %s" % (sRef.dataId)) ampSnapMIList = afwImage.vectorMaskedImageF() dataId = eval(amp.dataId.__repr__()) dataId['visit'] = sRef.dataId['visit'] for snap in (0,1): dataId['snap'] = snap ampExposure = sRef.butlerSubset.butler.get('raw', dataId) if expmeta is None: expmeta = ampExposure.getMetadata() expfilter = ampExposure.getFilter() expcalib = ampExposure.getCalib() ampDetector = cameraGeom.cast_Amp(ampExposure.getDetector()) ampExposure = self.convertIntToFloat(ampExposure) ampExpDataView = ampExposure.Factory(ampExposure, ampDetector.getDiskDataSec()) self.saturationDetection(ampExposure, ampDetector) self.overscanCorrection(ampExposure, ampDetector) if calibType in ('flat', 'dark'): self.biasCorrection(ampExpDataView, amp) if False: self.darkCorrection(ampExpDataView, amp) self.updateVariance(ampExpDataView, ampDetector) ampSnapMIList.append(ampExpDataView.getMaskedImage()) ampMIList.append(self.combineMIList(ampSnapMIList)) masterFrame = self.combineMIList(ampMIList) #Fix saturation too??? self.fixDefectsAndSat(masterFrame, ampDetector) exp = afwImage.ExposureF(masterFrame) self.copyMetadata(exp, expmeta, calibType) exp.setDetector(ampDetector) exp.setWcs(afwImage.Wcs()) exp.setCalib(expcalib) if calibType is 'flat': exp.setFilter(expfilter) if self.config.doWrite and calibType is not 'flat': print "writing file %s"%dataId butler.put(exp, calibType, dataId = amp.dataId) masterExpList.append(exp) dataIdList.append(amp.dataId) if self.config.doWrite and calibType is 'flat': self.normChipAmps(masterExpList) for exp, dataId in zip(masterExpList, dataIdList): print "writing flat file %s"%dataId butler.put(exp, calibType, dataId) return pipeBase.Struct( masterFrameList = masterExpList, )