52 LOG_LOGGER _log =
LOG_GET(
"jointcal.Associations");
64 lsst::afw::geom::Box2I
const &bbox,
std::string const &filter,
68 auto ccdImage = std::make_shared<CcdImage>(catalog, wcs, visitInfo, bbox, filter, photoCalib, detector,
77 centers.
reserve(ccdImageList.size());
78 for (
auto const &
ccdImage : ccdImageList) {
81 auto commonTangentPoint = afw::geom::averageSpherePoint(centers);
82 LOGLS_DEBUG(_log,
"Using common tangent point: " << commonTangentPoint.getPosition(afw::geom::degrees));
83 setCommonTangentPoint(commonTangentPoint.getPosition(afw::geom::degrees));
87 _commonTangentPoint =
Point(commonTangentPoint.getX(), commonTangentPoint.getY());
88 for (
auto &
ccdImage : ccdImageList)
ccdImage->setCommonTangentPoint(_commonTangentPoint);
92 const bool enlargeFittedList) {
97 for (
auto &item : fittedStarList) {
98 item->clearBeforeAssoc();
101 if (!useFittedList) fittedStarList.clear();
103 for (
auto &
ccdImage : ccdImageList) {
114 Frame ccdImageFrameCPT = toCommonTangentPlane->apply(
ccdImage->getImageFrame(),
false);
115 ccdImageFrameCPT = ccdImageFrameCPT.
rescale(1.10);
120 for (
auto const &fittedStar : fittedStarList) {
121 if (ccdImageFrameCPT.
inFrame(*fittedStar)) {
128 toCommonTangentPlane.
get(), matchCutInArcSec / 3600.);
131 LOGLS_DEBUG(_log,
"Measured-to-Fitted matches before removing ambiguities " << starMatchList->size());
132 starMatchList->removeAmbiguities(*toCommonTangentPlane);
133 LOGLS_DEBUG(_log,
"Measured-to-Fitted matches after removing ambiguities " << starMatchList->size());
137 int matchedCount = 0;
138 for (
auto const &starMatch : *starMatchList) {
139 auto bs = starMatch.s1;
142 auto bs2 = starMatch.s2;
145 ms->setFittedStar(fs);
148 LOGLS_INFO(_log,
"Matched " << matchedCount <<
" objects in " <<
ccdImage->getName());
151 int unMatchedCount = 0;
152 for (
auto const &mstar : catalog) {
154 if (mstar->getFittedStar())
continue;
155 if (enlargeFittedList) {
156 auto fs = std::make_shared<FittedStar>(*mstar);
158 toCommonTangentPlane->transformPosAndErrors(*fs, *fs);
159 fittedStarList.push_back(fs);
160 mstar->setFittedStar(fs);
164 LOGLS_INFO(_log,
"Unmatched objects: " << unMatchedCount);
179 bool rejectBadFluxes) {
180 if (refCat.
size() == 0) {
182 " reference catalog is empty : stop here "));
193 << fluxField <<
"Err" 194 <<
") not found in reference catalog. Not using ref flux errors.");
197 _filterMap.reserve(refFluxMap.size());
199 for (
auto const &filter : refFluxMap) {
200 _filterMap[filter.first] = nFilters;
205 for (
size_t i = 0; i < refCat.
size(); i++) {
206 auto const &record = refCat.
get(i);
208 auto coord = record->get(coordKey);
210 double defaultFluxErr;
211 if (fluxErrKey.isValid()) {
218 for (
auto const &filter : _filterMap) {
219 fluxList[filter.second] = refFluxMap.
at(filter.first).at(i) /
JanskyToMaggy;
220 fluxErrList[filter.second] = refFluxErrMap.
at(filter.first).at(i) /
JanskyToMaggy;
222 double ra = lsst::afw::geom::radToDeg(coord.getLongitude());
223 double dec = lsst::afw::geom::radToDeg(coord.getLatitude());
224 auto star = std::make_shared<RefStar>(ra, dec, defaultFlux, defaultFluxErr, fluxList, fluxErrList);
230 star->vx =
std::pow(0.1 / 3600 /
cos(coord.getLatitude()), 2);
235 if (rejectBadFluxes &&
238 refStarList.push_back(star);
243 TanRaDecToPixel raDecToCommonTangentPlane(identity, _commonTangentPoint);
245 associateRefStars(matchCut.asArcseconds(), &raDecToCommonTangentPlane);
250 Frame tangentPlaneFrame;
252 for (
auto const &
ccdImage : ccdImageList) {
254 if (tangentPlaneFrame.
getArea() == 0)
255 tangentPlaneFrame = CTPFrame;
257 tangentPlaneFrame += CTPFrame;
262 TanPixelToRaDec commonTangentPlaneToRaDec(identity, _commonTangentPoint);
263 Frame raDecFrame = commonTangentPlaneToRaDec.
apply(tangentPlaneFrame,
false);
265 lsst::afw::geom::Point<double>
min(raDecFrame.
xMin, raDecFrame.
yMin);
266 lsst::afw::geom::Point<double>
max(raDecFrame.
xMax, raDecFrame.
yMax);
267 lsst::afw::geom::Box2D box(min, max);
276 matchCutInArcSec / 3600.);
278 LOGLS_DEBUG(_log,
"Refcat matches before removing ambiguities " << starMatchList->size());
279 starMatchList->removeAmbiguities(*transform);
280 LOGLS_DEBUG(_log,
"Refcat matches after removing ambiguities " << starMatchList->size());
283 for (
auto const &starMatch : *starMatchList) {
287 const BaseStar &bs2 = *starMatch.s2;
295 "Associated " << starMatchList->size() <<
" reference stars among " << refStarList.size());
299 selectFittedStars(minMeasurements);
300 normalizeFittedStars();
303 void Associations::selectFittedStars(
int minMeasurements) {
304 LOGLS_INFO(_log,
"Fitted stars before measurement # cut: " << fittedStarList.size());
307 for (
auto const &
ccdImage : ccdImageList) {
315 if (fittedStar ==
nullptr) {
322 if (!fittedStar->getRefStar() && fittedStar->getMeasurementCount() < minMeasurements) {
323 fittedStar->getMeasurementCount()--;
324 mi = catalog.
erase(mi);
333 if ((*fi)->getMeasurementCount() == 0) {
334 fi = fittedStarList.erase(fi);
340 LOGLS_INFO(_log,
"Fitted stars after measurement # cut: " << fittedStarList.size());
343 void Associations::normalizeFittedStars()
const {
345 for (
auto &fittedStar : fittedStarList) {
348 fittedStar->setFlux(0.0);
349 fittedStar->getMag() = 0.0;
353 for (
auto const &
ccdImage : ccdImageList) {
356 for (
auto &mi : catalog) {
357 auto fittedStar = mi->getFittedStar();
358 if (fittedStar ==
nullptr)
361 "All measuredStars must have a fittedStar: did you call selectFittedStars()?"));
362 auto point = toCommonTangentPlane->apply(*mi);
363 fittedStar->x += point.x;
364 fittedStar->y += point.y;
365 fittedStar->getFlux() += mi->getFlux();
369 for (
auto &fi : fittedStarList) {
370 auto measurementCount = fi->getMeasurementCount();
371 fi->x /= measurementCount;
372 fi->y /= measurementCount;
373 fi->getFlux() /= measurementCount;
374 fi->getMag() = magFromFlux(fi->getFlux());
378 void Associations::assignMags() {
379 for (
auto const &
ccdImage : ccdImageList) {
381 for (
auto const &mstar : catalog) {
382 auto fstar = mstar->getFittedStar();
383 if (!fstar)
continue;
384 fstar->addMagMeasurement(mstar->getMag(), mstar->getMagWeight());
392 if (!fittedStarList.inTangentPlaneCoordinates) {
394 "DeprojectFittedStars: Fitted stars are already in sidereal coordinates, nothing done ");
399 fittedStarList.applyTransform(ctp2Sky);
400 fittedStarList.inTangentPlaneCoordinates =
false;
405 return item->getCatalogForFit().size() > 0;
411 for (
auto const &fittedStar : fittedStarList) {
412 if ((fittedStar !=
nullptr) & (fittedStar->getRefStar() !=
nullptr)) count++;
418 void Associations::collectMCStars(
int realization) {
422 for (I = ccdImageList.begin(); I != ccdImageList.end(); I++) {
424 string dbimdir = ccdImage.Dir();
425 string mctruth = dbimdir +
"/mc/mctruth.list";
427 if (realization >= 0) {
429 sstrm << dbimdir <<
"/mc/mctruth_" << realization <<
".list";
430 mctruth = sstrm.
str();
437 DicStarList mctruthlist(mctruth);
441 for (smI = starMatchList->begin(); smI != starMatchList->end(); smI++) {
446 DicStar *dstar =
dynamic_cast<DicStar *
>(bs);
448 mcstar->GetMCInfo().iflux = dstar->getval(
"iflux");
449 mcstar->GetMCInfo().tflux = dstar->getval(
"sflux");
456 LOGLS_FATAL(_log,
"CollectMCStars Unable to match MCTruth w/ catalog!");
461 double matchCutArcSec) {
463 size_t pos_minus = color.
find(
'-');
464 bool compute_diff = (pos_minus != string::npos);
466 c1 = color.
substr(0, pos_minus);
467 if (compute_diff) c2 = color.
substr(pos_minus + 1, string::npos);
468 DicStarList cList(dicStarListName);
469 if (!cList.HasKey(c1))
470 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
471 " misses a key named \"" + c1 +
"\""));
472 if (compute_diff && !cList.HasKey(c2))
473 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
474 " misses a key named \"" + c2 +
"\""));
482 if (fittedStarList.inTangentPlaneCoordinates) id_or_proj = &id;
487 id_or_proj, matchCutArcSec / 3600);
489 LOGLS_INFO(_log,
"Matched " << starMatchList->size() <<
'/' << fittedStarList.size()
490 <<
" FittedStars to color catalog");
492 for (
auto i = starMatchList->begin(); i != starMatchList->end(); ++i) {
496 const TStar *ts =
dynamic_cast<const TStar *
>(s2);
497 const DicStar *ds =
dynamic_cast<const DicStar *
>(ts->get_original());
498 fs->
color = ds->getval(c1);
499 if (compute_diff) fs->
color -= ds->getval(c2);
Objects used as position anchors, typically USNO stars.
The transformation that handles pixels to sideral transformations (Gnomonic, possibly with polynomial...
int nCcdImagesValidForFit() const
return the number of CcdImages with non-empty catalogs to-be-fit.
MeasuredStarList::iterator MeasuredStarIterator
A hanger for star associations.
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
Transform pixels to ICRS RA, Dec in degrees.
double xMin
coordinate of boundary.
std::unique_ptr< StarMatchList > listMatchCollect(const BaseStarList &list1, const BaseStarList &list2, const AstrometryTransform *guess, const double maxDist)
assembles star matches.
#define LOGLS_FATAL(logger, message)
void associateCatalogs(const double matchCutInArcsec=0, const bool useFittedList=false, const bool enlargeFittedList=true)
incrementaly builds a merged catalog of all image catalogs
A list of MeasuredStar. They are usually filled in Associations::createCcdImage.
Frame rescale(const double factor) const
rescale it. The center does not move.
#define LOGLS_INFO(logger, message)
The base class for handling stars. Used by all matching routines.
void prepareFittedStars(int minMeasurements)
Set the color field of FittedStar 's from a colored catalog.
size_t nFittedStarsWithAssociatedRefStar() const
Return the number of fittedStars that have an associated refStar.
void collectRefStars(afw::table::SimpleCatalog &refCat, afw::geom::Angle matchCut, std::string const &fluxField, RefFluxMapType const &refFluxMap=RefFluxMapType(), RefFluxMapType const &refFluxErrMap=RefFluxMapType(), bool rejectBadFluxes=false)
Collect stars from an external reference catalog and associate them with fittedStars.
rectangle with sides parallel to axes.
SchemaItem< T > find(std::string const &name) const
Class for a simple mapping implementing a generic AstrometryTransform.
void setRefStar(const RefStar *_refStar)
Set the astrometric reference star associated with this star.
void computeCommonTangentPoint()
Sets a shared tangent point for all ccdImages, using the mean of the centers of all ccdImages...
FittedStarList::iterator FittedStarIterator
A list of FittedStar s. Such a list is typically constructed by Associations.
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
objects measured on actual images.
T dynamic_pointer_cast(T... args)
::std::list< StarMatch >::iterator StarMatchIterator
void deprojectFittedStars()
Sends back the fitted stars coordinates on the sky FittedStarsList::inTangentPlaneCoordinates keeps t...
void setCommonTangentPoint(lsst::afw::geom::Point2D const &commonTangentPoint)
Sets a shared tangent point for all ccdImages.
bool inFrame(double x, double y) const
inside?
#define LSST_EXCEPT(type,...)
void createCcdImage(afw::table::SourceCatalog &catalog, std::shared_ptr< lsst::afw::geom::SkyWcs > wcs, std::shared_ptr< lsst::afw::image::VisitInfo > visitInfo, lsst::afw::geom::Box2I const &bbox, std::string const &filter, std::shared_ptr< afw::image::PhotoCalib > photoCalib, std::shared_ptr< afw::cameraGeom::Detector > detector, int visit, int ccd, lsst::jointcal::JointcalControl const &control)
Create a ccdImage from an exposure catalog and metadata, and add it to the list.
MeasuredStarList const & getCatalogForFit() const
Gets the catalog to be used for fitting, which may have been cleaned-up.
Combinatorial searches for linear transformations to go from list1 to list2.
BaseStarList & Fitted2Base(FittedStarList &This)
std::shared_ptr< const BaseStar > s2
const lsst::afw::geom::Box2D getRaDecBBox()
BaseStarList & Measured2Base(MeasuredStarList &This)
#define LOGLS_DEBUG(logger, message)
std::shared_ptr< RecordT > const get(size_type i) const
std::shared_ptr< const BaseStar > s1
std::string sourceFluxField
"name of flux field in source catalog" ;
Handler of an actual image from a single CCD.
const double JanskyToMaggy
std::shared_ptr< FittedStar > getFittedStar() const
The objects which have been measured several times.
#define LOGLS_WARN(logger, message)
BaseStarList & Ref2Base(RefStarList &This)