def _loadThroughputs(self, butler, camera): """Internal method to load throughput data for filters Parameters ---------- butler: `lsst.daf.persistence.butler.Butler` A butler with the transmission info camera: `lsst.afw.cameraGeom.Camera` """ self._opticsTransmission = butler.get('transmission_optics') self._sensorsTransmission = {} for detector in camera: self._sensorsTransmission[detector.getId()] = butler.get('transmission_sensor', dataId={'ccd': detector.getId()}) self._filtersTransmission = {} for filterName in self.config.filterNames: f = Filter(filterName) foundTrans = False # Get all possible aliases, and also try the short filterName aliases = f.getAliases() aliases.extend(filterName) for alias in f.getAliases(): try: self._filtersTransmission[filterName] = butler.get('transmission_filter', dataId={'filter': alias}) foundTrans = True break except NoResults: pass if not foundTrans: raise ValueError("Could not find transmission for filter %s via any alias." % (filterName))
def getColorterm(self, filterName, photoCatName, doRaise=True): """!Get the appropriate Colorterm from the library Use dict of color terms in the library that matches the photoCatName. If the photoCatName exactly matches an entry in the library, that dict is used; otherwise if the photoCatName matches a single glob (shell syntax, e.g., "sdss-*" will match "sdss-dr8"), then that is used. If there is no exact match and no unique match to the globs, raise an exception. @param filterName name of filter @param photoCatName name of photometric reference catalog from which to retrieve the data. This argument is not glob-expanded (but the catalog names in the library are, if no exact match is found). @param[in] doRaise if True then raise ColortermNotFoundError if no suitable Colorterm found; if False then return a null Colorterm with filterName as the primary and secondary filter @return the appropriate Colorterm @throw ColortermNotFoundError if no suitable Colorterm found and doRaise true; other exceptions may be raised for unexpected errors, regardless of the value of doRaise """ try: trueRefCatName = None ctDictConfig = self.data.get(photoCatName) if ctDictConfig is None: # try glob expression matchList = [ libRefNameGlob for libRefNameGlob in self.data if fnmatch.fnmatch(photoCatName, libRefNameGlob) ] if len(matchList) == 1: trueRefCatName = matchList[0] ctDictConfig = self.data[trueRefCatName] elif len(matchList) > 1: raise ColortermNotFoundError( "Multiple library globs match photoCatName %r: %s" % (photoCatName, matchList)) else: raise ColortermNotFoundError( "No colorterm dict found with photoCatName %r" % photoCatName) ctDict = ctDictConfig.data if filterName not in ctDict: # Perhaps it's an alias try: filterName = Filter(Filter(filterName).getId()).getName() except pexExcept.NotFoundError: pass # this will be handled shortly if filterName not in ctDict: errMsg = "No colorterm found for filter %r with photoCatName %r" % ( filterName, photoCatName) if trueRefCatName is not None: errMsg += " = catalog %r" % (trueRefCatName, ) raise ColortermNotFoundError(errMsg) return ctDict[filterName] except ColortermNotFoundError: if doRaise: raise else: return Colorterm(filterName, filterName)
def getFilterNames(): """Return a list of filter names in increasing ID order. This assumes that an lsst.daf.persistence.Mapper which sets up filter definitions has been created, or that filters have been manually defined with e.g. lsst.afw.image.utils.defineFilter(). """ names = list(Filter.getNames()) names.sort(key=lambda name: Filter(name, False).getId()) return names
def makeFilter(self, metadata): obsInfo = ObservationInfo(metadata, translator_class=HscTranslator) # For historical reasons we need to return a short, lowercase filter # name that is neither a physical_filter nor an abstract_filter in Gen3 # or a filter data ID value in Gen2. # We'll suck that out of the definitions used to construct filters # for HSC in Gen2. This should all get cleaned up in RFC-541. for d in HSC_FILTER_DEFINITIONS: if obsInfo.physical_filter == d["name"] or obsInfo.physical_filter in d["alias"]: return Filter(d["name"], force=True) return Filter(obsInfo.physical_filter, force=True)
def splitCoaddId(oid, asDict=True, hasFilter=True): """Split an ObjectId (maybe an numpy array) into tract, patch, [filter], and objId. See obs/subaru/python/lsst/obs/hscSim/hscMapper.py """ oid = np.array(oid, dtype='int64') objId = np.bitwise_and(oid, 2**HscMapper._nbit_id - 1) oid >>= HscMapper._nbit_id if hasFilter: filterId = np.bitwise_and(oid, 2**HscMapper._nbit_filter - 1).astype('int32') oid >>= HscMapper._nbit_filter filterName = np.empty(oid.size, "a6") if filterId.size == 1: filterId = [int(filterId)] # as you can't iterate over a length-1 np array for fid in set(filterId): name = Filter(int(fid)).getName() filesystemName = "HSC-%s" % name.upper() # name mapper needs try: Filter(filesystemName) name = filesystemName except: pass filterName[filterId == fid] = name else: filterName = None patchY = np.bitwise_and(oid, 2**HscMapper._nbit_patch - 1).astype('int32') oid >>= HscMapper._nbit_patch patchX = np.bitwise_and(oid, 2**HscMapper._nbit_patch - 1).astype('int32') oid >>= HscMapper._nbit_patch add = np.core.defchararray.add # why isn't this easier to find? patch = add(add(patchX.astype(str), ","), patchY.astype(str)) #patch.shape = filterName.shape # why do I have to do this? tract = oid.astype('int32') if oid.size == 1: # sqlite doesn't like numpy types filterName = str(filterName[0]) tract = int(tract) patch = str(patch[0]) objId = int(objId) if asDict: return {"filter" : filterName, "tract" : tract, "patch" : patch, "objId" : objId} else: return filterName, tract, patch, objId
def getLongFilterName(short): """Return a long HSC filter name (e.g. 'HSC-R') that's usable as a data ID value from the short one (e.g. 'r') declared canonical in afw.image.Filter. """ HscMapper.addFilters() if short.startswith("HSC"): return short if short.startswith("NB") or short.startswith("IB"): num = int(short[2:].lstrip("0")) return "%s%04d" % (short[:2], num) f = Filter(short) for a in f.getAliases(): if a.startswith("HSC") or a.startswith("NB") or a.startswith("IB"): return a return short
def testMultiPlaneFitsReaders(self): """Run tests for MaskedImageFitsReader and ExposureFitsReader. """ metadata = PropertyList() metadata.add("FIVE", 5) metadata.add("SIX", 6.0) wcs = makeSkyWcs(Point2D(2.5, 3.75), SpherePoint(40.0 * degrees, 50.0 * degrees), np.array([[1E-5, 0.0], [0.0, -1E-5]])) defineFilter("test_readers_filter", lambdaEff=470.0) calib = PhotoCalib(2.5E4) psf = GaussianPsf(21, 21, 8.0) polygon = Polygon(Box2D(self.bbox)) apCorrMap = ApCorrMap() visitInfo = VisitInfo(exposureTime=5.0) transmissionCurve = TransmissionCurve.makeIdentity() coaddInputs = CoaddInputs(ExposureTable.makeMinimalSchema(), ExposureTable.makeMinimalSchema()) detector = DetectorWrapper().detector record = coaddInputs.ccds.addNew() record.setWcs(wcs) record.setPhotoCalib(calib) record.setPsf(psf) record.setValidPolygon(polygon) record.setApCorrMap(apCorrMap) record.setVisitInfo(visitInfo) record.setTransmissionCurve(transmissionCurve) record.setDetector(detector) for n, dtypeIn in enumerate(self.dtypes): with self.subTest(dtypeIn=dtypeIn): exposureIn = Exposure(self.bbox, dtype=dtypeIn) shape = exposureIn.image.array.shape exposureIn.image.array[:, :] = np.random.randint(low=1, high=5, size=shape) exposureIn.mask.array[:, :] = np.random.randint(low=1, high=5, size=shape) exposureIn.variance.array[:, :] = np.random.randint(low=1, high=5, size=shape) exposureIn.setMetadata(metadata) exposureIn.setWcs(wcs) exposureIn.setFilter(Filter("test_readers_filter")) exposureIn.setFilterLabel( FilterLabel(physical="test_readers_filter")) exposureIn.setPhotoCalib(calib) exposureIn.setPsf(psf) exposureIn.getInfo().setValidPolygon(polygon) exposureIn.getInfo().setApCorrMap(apCorrMap) exposureIn.getInfo().setVisitInfo(visitInfo) exposureIn.getInfo().setTransmissionCurve(transmissionCurve) exposureIn.getInfo().setCoaddInputs(coaddInputs) exposureIn.setDetector(detector) with lsst.utils.tests.getTempFilePath(".fits") as fileName: exposureIn.writeFits(fileName) self.checkMaskedImageFitsReader(exposureIn, fileName, self.dtypes[n:]) self.checkExposureFitsReader(exposureIn, fileName, self.dtypes[n:])
def getComponent(self, label, derivedName): """Derive a Filter from a FilterLabel. Parameters ---------- label : `~lsst.afw.image.FilterLabel` The object to convert. derivedName : `str` Name of type to convert to. Only "filter" is supported. Returns ------- derived : `object` The converted type. Can be `None`. Raises ------ AttributeError An unknown component was requested. """ if derivedName == "filter": # Port of backwards-compatibility code in afw; don't want to # expose it as API. # Filters still have standard aliases, so can use almost any name # to define them. Prefer afw_name or band because that's what most # code assumes is Filter.getName(). if label == FilterLabel(band="r", physical="HSC-R2"): return Filter("r2", force=True) elif label == FilterLabel(band="i", physical="HSC-I2"): return Filter("i2", force=True) elif label == FilterLabel(physical="solid plate 0.0 0.0"): return Filter("SOLID", force=True) elif label.hasBandLabel(): return Filter(label.bandLabel, force=True) else: # FilterLabel guarantees at least one of band or physical # is defined. return Filter(label.physicalLabel, force=True) else: raise AttributeError( f"Do not know how to convert {type(label)} to {derivedName}")
def testFilters(self): # Check that the mapper has defined some standard filters. # Note that this list is not intended to be comprehensive -- we # anticipate that more filters can be added without causing the test # to break -- but captures the standard HSC broad-band filters. FilterName = namedtuple("FilterName", ["alias", "canonical"]) filterNames = ( FilterName(alias="HSC-G", canonical="g"), FilterName(alias="HSC-R", canonical="r"), FilterName(alias="HSC-I", canonical="i"), FilterName(alias="HSC-Z", canonical="z"), FilterName(alias="HSC-Y", canonical="y"), FilterName(alias="NONE", canonical="UNRECOGNISED") ) for filterName in filterNames: self.assertIn(filterName.alias, self.mapper.filters) self.assertEqual(Filter(filterName.alias).getCanonicalName(), filterName.canonical)