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