lsst.jointcal  master-gc7bdbdc8f0
Associations.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 #include <iostream>
3 #include <limits>
4 #include <sstream>
5 
6 #include "lsst/log/Log.h"
12 #include "lsst/jointcal/Frame.h"
14 #include "lsst/jointcal/FatPoint.h"
15 #include "lsst/afw/image/Image.h"
18 
19 #include "lsst/pex/exceptions.h"
20 #include "lsst/afw/geom/Box.h"
21 #include "lsst/afw/geom/Point.h"
22 #include "lsst/afw/coord/Coord.h"
23 #include "lsst/afw/image/Calib.h"
24 
25 namespace jointcal = lsst::jointcal;
26 
27 namespace {
28 LOG_LOGGER _log = LOG_GET("jointcal.Associations");
29 }
30 
31 // TODO: Remove this once RFC-356 is implemented and all refcats give fluxes in Maggies.
32 const double JanskyToMaggy = 3631.0;
33 
34 namespace lsst {
35 namespace jointcal {
36 
40  lsst::afw::geom::Box2I const &bbox, std::string const &filter,
42  std::shared_ptr<afw::cameraGeom::Detector> detector, int visit, int ccd,
43  lsst::jointcal::JointcalControl const &control) {
44  auto ccdImage = std::make_shared<CcdImage>(catalog, wcs, visitInfo, bbox, filter, photoCalib, detector,
45  visit, ccd, control.sourceFluxField);
46  ccdImageList.push_back(ccdImage);
47  LOGLS_DEBUG(_log, "Catalog " << ccdImage->getName() << " has " << ccdImage->getWholeCatalog().size()
48  << " objects.");
49 }
50 
52  _commonTangentPoint = Point(commonTangentPoint.getX(), commonTangentPoint.getY()); // a jointcal::Point
53  for (auto &ccdImage : ccdImageList) ccdImage->setCommonTangentPoint(_commonTangentPoint);
54 }
55 
56 void Associations::associateCatalogs(const double matchCutInArcSec, const bool useFittedList,
57  const bool enlargeFittedList) {
58  // clear reference stars
59  refStarList.clear();
60 
61  // clear measurement counts and associations to refstars, but keep fittedStars themselves.
62  for (auto &item : fittedStarList) {
63  item->clearBeforeAssoc();
64  }
65  // clear fitted stars
66  if (!useFittedList) fittedStarList.clear();
67 
68  for (auto &ccdImage : ccdImageList) {
69  const Gtransfo *toCommonTangentPlane = ccdImage->getPix2CommonTangentPlane();
70 
71  /* clear the catalog to fit and copy the whole catalog into it.
72  this allows reassociating from scratch after a fit. */
73 
74  ccdImage->getCatalogForFit().clear();
75  ccdImage->getWholeCatalog().copyTo(ccdImage->getCatalogForFit());
76  MeasuredStarList &catalog = ccdImage->getCatalogForFit();
77 
78  // associate with previous lists
79  /* to speed up the match (more precisely the contruction of the
80  FastFinder), select in the fittedStarList the objects that
81  are within reach of the current ccdImage
82  */
83  Frame ccdImageFrameCPT = applyTransfo(ccdImage->getImageFrame(), *toCommonTangentPlane, LargeFrame);
84  ccdImageFrameCPT = ccdImageFrameCPT.rescale(1.10); // add 10 % margin.
85  /* we cannot use FittedStarList::ExtractInFrame, because it does an
86  actual copy, which we don't want here: we want the pointers in
87  the StarMatch to refer to fittedStarList elements. */
88  FittedStarList toMatch;
89 
90  for (auto const &fittedStar : fittedStarList) {
91  if (ccdImageFrameCPT.inFrame(*fittedStar)) {
92  toMatch.push_back(fittedStar);
93  }
94  }
95 
96  // divide by 3600 because coordinates in CTP are in degrees.
97  auto starMatchList = listMatchCollect(Measured2Base(catalog), Fitted2Base(toMatch),
98  toCommonTangentPlane, matchCutInArcSec / 3600.);
99 
100  /* should check what this removeAmbiguities does... */
101  LOGLS_DEBUG(_log, "Measured-to-Fitted matches before removing ambiguities " << starMatchList->size());
102  starMatchList->removeAmbiguities(*toCommonTangentPlane);
103  LOGLS_DEBUG(_log, "Measured-to-Fitted matches after removing ambiguities " << starMatchList->size());
104 
105  /* associate MeasuredStar -> FittedStar using the
106  surviving matches */
107 
108  int matchedCount = 0;
109  for (auto const &starMatch : *starMatchList) {
110  auto bs = starMatch.s1;
111  auto ms_const = std::dynamic_pointer_cast<const MeasuredStar>(bs);
112  auto ms = std::const_pointer_cast<MeasuredStar>(ms_const);
113  auto bs2 = starMatch.s2;
114  auto fs_const = std::dynamic_pointer_cast<const FittedStar>(bs2);
115  auto fs = std::const_pointer_cast<FittedStar>(fs_const);
116  ms->setFittedStar(fs);
117  matchedCount++;
118  }
119  LOGLS_INFO(_log, "Matched " << matchedCount << " objects in " << ccdImage->getName());
120 
121  // add unmatched objets to FittedStarList
122  int unMatchedCount = 0;
123  for (auto const &mstar : catalog) {
124  // to check if it was matched, just check if it has a fittedStar Pointer assigned
125  if (mstar->getFittedStar()) continue;
126  if (enlargeFittedList) {
127  auto fs = std::make_shared<FittedStar>(*mstar);
128  // transform coordinates to CommonTangentPlane
129  toCommonTangentPlane->transformPosAndErrors(*fs, *fs);
130  fittedStarList.push_back(fs);
131  mstar->setFittedStar(fs);
132  }
133  unMatchedCount++;
134  }
135  LOGLS_INFO(_log, "Unmatched objects: " << unMatchedCount);
136  } // end of loop on CcdImages
137 
138  assignMags();
139 }
140 
142  afw::geom::Angle matchCut, std::string const &fluxField,
143  std::map<std::string, std::vector<double>> const &refFluxMap,
144  std::map<std::string, std::vector<double>> const &refFluxErrMap) {
145  if (refCat.size() == 0) {
147  " reference catalog is empty : stop here "));
148  }
149 
150  afw::table::CoordKey coordKey = refCat.getSchema()["coord"];
151  auto fluxKey = refCat.getSchema().find<double>(fluxField).key;
152  // Don't blow up if the reference catalog doesn't contain errors.
153  afw::table::Key<double> fluxErrKey;
154  try {
155  fluxErrKey = refCat.getSchema().find<double>(fluxField + "Sigma").key;
156  } catch (pex::exceptions::NotFoundError &) {
157  LOGLS_WARN(_log, "Flux error field ("
158  << fluxField << "Sigma"
159  << ") not found in reference catalog. Not using ref flux errors.");
160  }
161  _filterMap.clear();
162  _filterMap.reserve(refFluxMap.size());
163  size_t nFilters = 0;
164  for (auto const &filter : refFluxMap) {
165  _filterMap[filter.first] = nFilters;
166  nFilters++;
167  }
168 
169  refStarList.clear();
170  for (size_t i = 0; i < refCat.size(); i++) {
171  auto const &record = refCat.get(i);
172 
173  afw::coord::Coord coord = record->get(coordKey);
174  double defaultFlux = record->get(fluxKey) / JanskyToMaggy;
175  double defaultFluxErr;
176  if (fluxErrKey.isValid()) {
177  defaultFluxErr = record->get(fluxErrKey) / JanskyToMaggy;
178  } else {
179  defaultFluxErr = std::numeric_limits<double>::quiet_NaN();
180  }
181  std::vector<double> fluxList(nFilters);
182  std::vector<double> fluxErrList(nFilters);
183  for (auto const &filter : _filterMap) {
184  fluxList[filter.second] = refFluxMap.at(filter.first).at(i) / JanskyToMaggy;
185  fluxErrList[filter.second] = refFluxErrMap.at(filter.first).at(i) / JanskyToMaggy;
186  }
187  double ra = lsst::afw::geom::radToDeg(coord.getLongitude());
188  double dec = lsst::afw::geom::radToDeg(coord.getLatitude());
189  auto star = std::make_shared<RefStar>(ra, dec, defaultFlux, defaultFluxErr, fluxList, fluxErrList);
190 
191  // TODO DM-10826: RefCats aren't guaranteed to have position errors.
192  // TODO: Need to devise a way to check whether the refCat has position errors
193  // TODO: and use them instead, if available.
194  // cook up errors: 100 mas per cooordinate
195  star->vx = std::pow(0.1 / 3600 / cos(coord.getLatitude()), 2);
196  star->vy = std::pow(0.1 / 3600, 2);
197  star->vxy = 0.;
198 
199  refStarList.push_back(star);
200  }
201 
202  // project on CTP (i.e. RaDec2CTP), in degrees
203  GtransfoLin identity;
204  TanRaDec2Pix raDec2CTP(identity, _commonTangentPoint);
205 
206  associateRefStars(matchCut.asArcseconds(), &raDec2CTP);
207 }
208 
210  // compute the frame on the CTP that contains all input images
211  Frame tangentPlaneFrame;
212 
213  for (auto const &ccdImage : ccdImageList) {
214  Frame CTPFrame =
215  applyTransfo(ccdImage->getImageFrame(), *(ccdImage->getPix2CommonTangentPlane()), LargeFrame);
216  if (tangentPlaneFrame.getArea() == 0)
217  tangentPlaneFrame = CTPFrame;
218  else
219  tangentPlaneFrame += CTPFrame;
220  }
221 
222  // convert tangent plane coordinates to RaDec:
223  GtransfoLin identity;
224  TanPix2RaDec CTP2RaDec(identity, _commonTangentPoint);
225  Frame raDecFrame = applyTransfo(tangentPlaneFrame, CTP2RaDec, LargeFrame);
226 
227  lsst::afw::geom::Point<double> min(raDecFrame.xMin, raDecFrame.yMin);
228  lsst::afw::geom::Point<double> max(raDecFrame.xMax, raDecFrame.yMax);
229  lsst::afw::geom::Box2D box(min, max);
230 
231  return box;
232 }
233 
234 void Associations::associateRefStars(double matchCutInArcSec, const Gtransfo *gtransfo) {
235  // associate with FittedStars
236  // 3600 because coordinates are in degrees (in CTP).
237  auto starMatchList = listMatchCollect(Ref2Base(refStarList), Fitted2Base(fittedStarList), gtransfo,
238  matchCutInArcSec / 3600.);
239 
240  LOGLS_DEBUG(_log, "Refcat matches before removing ambiguities " << starMatchList->size());
241  starMatchList->removeAmbiguities(*gtransfo);
242  LOGLS_DEBUG(_log, "Refcat matches after removing ambiguities " << starMatchList->size());
243 
244  // actually associate things
245  for (auto const &starMatch : *starMatchList) {
246  const BaseStar &bs = *starMatch.s1;
247  const RefStar &rs_const = dynamic_cast<const RefStar &>(bs);
248  RefStar &rs = const_cast<RefStar &>(rs_const);
249  const BaseStar &bs2 = *starMatch.s2;
250  const FittedStar &fs_const = dynamic_cast<const FittedStar &>(bs2);
251  FittedStar &fs = const_cast<FittedStar &>(fs_const);
252  // rs->setFittedStar(*fs);
253  fs.setRefStar(&rs);
254  }
255 
256  LOGLS_INFO(_log,
257  "Associated " << starMatchList->size() << " reference stars among " << refStarList.size());
258 }
259 
260 void Associations::selectFittedStars(int minMeasurements) {
261  LOGLS_INFO(_log, "Fitted stars before measurement # cut: " << fittedStarList.size());
262  /* first pass : remove objects that have less than a
263  certain number of measurements.
264  */
265  for (auto const &ccdImage : ccdImageList) {
266  MeasuredStarList &catalog = ccdImage->getCatalogForFit();
267  for (MeasuredStarIterator mi = catalog.begin(); mi != catalog.end();) {
268  MeasuredStar &mstar = **mi;
269 
270  auto fstar = mstar.getFittedStar();
271  if (!fstar) {
272  ++mi;
273  continue;
274  }
275 
276  /* keep FittedStar's which either have a minimum number of
277  measurements, or are matched to a RefStar
278  */
279  if (!fstar->getRefStar() && fstar->getMeasurementCount() < minMeasurements) {
280  auto f = std::const_pointer_cast<FittedStar>(fstar);
281  f->getMeasurementCount()--;
282  mi = catalog.erase(mi);
283  } else
284  ++mi;
285  } // end loop on objects in catalog
286  } // end loop on catalogs
287 
288  /* now FittedStars with less than minMeasurements should have
289  zero measurementCount; */
290 
291  for (FittedStarIterator fi = fittedStarList.begin(); fi != fittedStarList.end();) {
292  if ((*fi)->getMeasurementCount() == 0)
293  fi = fittedStarList.erase(fi);
294  else
295  ++fi;
296  }
297 
298  LOGLS_INFO(_log, "Fitted stars after measurement # cut: " << fittedStarList.size());
299 }
300 
301 void Associations::assignMags() {
302  for (auto const &ccdImage : ccdImageList) {
303  MeasuredStarList &catalog = ccdImage->getCatalogForFit();
304  for (auto const &mstar : catalog) {
305  auto fstar = mstar->getFittedStar();
306  if (!fstar) continue;
307  auto f = std::const_pointer_cast<FittedStar>(fstar);
308  f->addMagMeasurement(mstar->getMag(), mstar->getMagWeight());
309  }
310  }
311 }
312 
314  /* by default, Associations::fittedStarList is expressed on the
315  Associations::commonTangentPlane. For AstrometryFit, we need it on
316  the sky */
317  if (!fittedStarList.inTangentPlaneCoordinates) {
318  LOGLS_WARN(_log,
319  "DeprojectFittedStars: Fitted stars are already in sidereal coordinates, nothing done ");
320  return;
321  }
322 
323  TanPix2RaDec ctp2Sky(GtransfoLin(), getCommonTangentPoint());
324  fittedStarList.applyTransfo(ctp2Sky);
325  fittedStarList.inTangentPlaneCoordinates = false;
326 }
327 
328 #ifdef TODO
329 void Associations::collectMCStars(int realization) {
330  CcdImageIterator I;
331  StarMatchIterator smI;
332 
333  for (I = ccdImageList.begin(); I != ccdImageList.end(); I++) {
334  CcdImage &ccdImage = **I;
335  string dbimdir = ccdImage.Dir();
336  string mctruth = dbimdir + "/mc/mctruth.list";
337 
338  if (realization >= 0) {
339  stringstream sstrm;
340  sstrm << dbimdir << "/mc/mctruth_" << realization << ".list";
341  mctruth = sstrm.str();
342  }
343 
344  GtransfoIdentity gti;
345  MeasuredStarList &catalog = ccdImage.getCatalogForFit();
346 
347  // BaseStarWithErrorList mctruthlist(mctruth);
348  DicStarList mctruthlist(mctruth);
349  auto starMatchList =
350  listMatchCollect(Measured2Base(catalog), Dic2Base(mctruthlist), &gti, 1. /* pixel ? */);
351  if (starMatchList)
352  for (smI = starMatchList->begin(); smI != starMatchList->end(); smI++) {
353  StarMatch &sm = *smI;
354  BaseStar *bs = sm.s1;
355  MeasuredStar *mstar = dynamic_cast<MeasuredStar *>(bs);
356  bs = sm.s2;
357  DicStar *dstar = dynamic_cast<DicStar *>(bs);
358  std::unique_ptr<BaseStarWithError> mcstar(new BaseStarWithError(*bs));
359  mcstar->GetMCInfo().iflux = dstar->getval("iflux");
360  mcstar->GetMCInfo().tflux = dstar->getval("sflux");
361  /*
362  mstar->SetMCTruth(mcstar);
363  mstar->SetMCMeas(mcstar);
364  */
365  }
366  else
367  LOGLS_FATAL(_log, "CollectMCStars Unable to match MCTruth w/ catalog!");
368  }
369 }
370 
371 void Associations::setFittedStarColors(std::string dicStarListName, std::string color,
372  double matchCutArcSec) {
373  // decode color string in case it is x-y
374  size_t pos_minus = color.find('-');
375  bool compute_diff = (pos_minus != string::npos);
376  std::string c1, c2;
377  c1 = color.substr(0, pos_minus); // if pos_minus == npos, means "up to the end"
378  if (compute_diff) c2 = color.substr(pos_minus + 1, string::npos);
379  DicStarList cList(dicStarListName);
380  if (!cList.HasKey(c1))
381  throw(GastroException("Associations::SetFittedstarColors : " + dicStarListName +
382  " misses a key named \"" + c1 + "\""));
383  if (compute_diff && !cList.HasKey(c2))
384  throw(GastroException("Associations::SetFittedstarColors : " + dicStarListName +
385  " misses a key named \"" + c2 + "\""));
386  // we associate in some tangent plane. The reference catalog is expressed on the sky,
387  // but FittedStar's may be still in this tangent plane.
388  BaseStarList &l1 = (BaseStarList &)fittedStarList;
390  TanRaDec2Pix proj(GtransfoLin(), getCommonTangentPoint());
391  // project or not ?
392  Gtransfo *id_or_proj = &proj;
393  if (fittedStarList.inTangentPlaneCoordinates) id_or_proj = &id;
394  // The color List is to be projected:
395  TStarList projected_cList((BaseStarList &)cList, proj);
396  // Associate
397  auto starMatchList = listMatchCollect(Fitted2Base(fittedStarList), (const BaseStarList &)projected_cList,
398  id_or_proj, matchCutArcSec / 3600);
399 
400  LOGLS_INFO(_log, "Matched " << starMatchList->size() << '/' << fittedStarList.size()
401  << " FittedStars to color catalog");
402  // Evaluate and assign colors.
403  for (auto i = starMatchList->begin(); i != starMatchList->end(); ++i) {
404  BaseStar *s1 = i->s1;
405  FittedStar *fs = dynamic_cast<FittedStar *>(s1);
406  BaseStar *s2 = i->s2;
407  const TStar *ts = dynamic_cast<const TStar *>(s2);
408  const DicStar *ds = dynamic_cast<const DicStar *>(ts->get_original());
409  fs->color = ds->getval(c1);
410  if (compute_diff) fs->color -= ds->getval(c2);
411  }
412 }
413 
414 #endif /* STORAGE */
415 } // namespace jointcal
416 } // namespace lsst
#define LOGLS_WARN(logger, message)
Objects used as position anchors, typically USNO stars.
Definition: RefStar.h:16
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:292
lsst::afw::geom::Angle getLongitude() const
MeasuredStarList::iterator MeasuredStarIterator
Definition: MeasuredStar.h:119
A hanger for star associations.
Definition: StarMatch.h:31
A point in a plane.
Definition: Point.h:13
double getArea() const
Definition: Frame.cc:102
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
Definition: Gtransfo.h:443
void addMagMeasurement(double magValue, double magWeight)
this routine will hopefully soon disappear.
Definition: FittedStar.cc:41
int getMeasurementCount() const
Definition: FittedStar.h:82
int min
double xMin
coordinate of boundary.
Definition: Frame.h:22
T end(T... args)
constexpr double asArcseconds() const noexcept
STL class.
void associateCatalogs(const double matchCutInArcsec=0, const bool useFittedList=false, const bool enlargeFittedList=true)
incrementaly builds a merged catalog of all image catalogs
Definition: Associations.cc:56
A list of MeasuredStar. They are usually filled in Associations::AddImage.
Definition: MeasuredStar.h:111
lsst::afw::geom::Angle getLatitude() const
Frame rescale(const double factor) const
rescale it. The center does not move.
Definition: Frame.cc:94
STL class.
void selectFittedStars(int minMeasurements)
Set the color field of FittedStar &#39;s from a colored catalog.
pairs of points
The base class for handling stars. Used by all matching routines.
Definition: BaseStar.h:22
void collectRefStars(lsst::afw::table::SortedCatalogT< lsst::afw::table::SimpleRecord > &refCat, afw::geom::Angle matchCut, std::string const &fluxField, std::map< std::string, std::vector< double >> const &refFluxMap, std::map< std::string, std::vector< double >> const &refFluxErrMap)
Collect stars from an external reference catalog and associate them with fittedStars.
T at(T... args)
T push_back(T... args)
std::lists of Stars.
Definition: StarList.h:35
rectangle with sides parallel to axes.
Definition: Frame.h:19
#define LOGLS_DEBUG(logger, message)
Class for a simple mapping implementing a generic Gtransfo.
void setRefStar(const RefStar *_refStar)
Set the astrometric reference star associated with this star.
Definition: FittedStar.cc:30
FittedStarList::iterator FittedStarIterator
Definition: FittedStar.h:130
int max
T erase(T... args)
A list of FittedStar s. Such a list is typically constructed by Associations.
Definition: FittedStar.h:121
std::unique_ptr< StarMatchList > listMatchCollect(const BaseStarList &list1, const BaseStarList &list2, const Gtransfo *guess, const double maxDist)
assembles star matches.
Definition: ListMatch.cc:538
T str(T... args)
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:99
T cos(T... args)
objects measured on actual images.
Definition: MeasuredStar.h:18
T dynamic_pointer_cast(T... args)
::std::list< StarMatch >::iterator StarMatchIterator
Definition: StarMatch.h:110
void deprojectFittedStars()
Sends back the fitted stars coordinates on the sky FittedStarsList::inTangentPlaneCoordinates keeps t...
#define LOGLS_INFO(logger, message)
void setCommonTangentPoint(lsst::afw::geom::Point2D const &commonTangentPoint)
Sets a shared tangent point for all ccdImages.
Definition: Associations.cc:51
T find(T... args)
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
Definition: Gtransfo.h:150
#define LSST_EXCEPT(type,...)
std::shared_ptr< const FittedStar > getFittedStar() const
Definition: MeasuredStar.h:83
STL class.
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
Definition: Gtransfo.h:517
T begin(T... args)
T pow(T... args)
MeasuredStarList const & getCatalogForFit() const
Gets the catalog to be used for fitting, which may have been cleaned-up.
Definition: CcdImage.h:64
Combinatorial searches for linear transformations to go from list1 to list2.
BaseStarList & Fitted2Base(FittedStarList &This)
Definition: FittedStar.cc:48
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:39
std::shared_ptr< const BaseStar > s2
Definition: StarMatch.h:39
const lsst::afw::geom::Box2D getRaDecBBox()
BaseStarList & Measured2Base(MeasuredStarList &This)
Definition: MeasuredStar.cc:35
int id
constexpr double radToDeg(double x) noexcept
std::shared_ptr< RecordT > const get(size_type i) const
std::shared_ptr< const BaseStar > s1
Definition: StarMatch.h:39
T dec(T... args)
T substr(T... args)
Frame applyTransfo(const Frame &inputframe, const Gtransfo &gtransfo, const WhichTransformed which)
Transform a Frame through a Transfo.
Definition: AstroUtils.cc:15
std::string sourceFluxField
"name of flux field in source catalog" ;
T quiet_NaN(T... args)
#define LOGLS_FATAL(logger, message)
Handler of an actual image from a single CCD.
Definition: CcdImage.h:34
#define LOG_GET(logger)
const double JanskyToMaggy
Definition: Associations.cc:32
The objects which have been measured several times.
Definition: FittedStar.h:37
BaseStarList & Ref2Base(RefStarList &This)
Definition: RefStar.cc:11
void addImage(lsst::afw::table::SortedCatalogT< lsst::afw::table::SourceRecord > &catalog, std::shared_ptr< lsst::afw::image::TanWcs > 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.
Definition: Associations.cc:37