lsst.jointcal  16.0-16-g925333c+6
CcdImage.cc
Go to the documentation of this file.
1 #include <assert.h>
2 #include <string>
3 #include <sstream>
4 #include <cmath>
5 
7 #include "lsst/pex/exceptions.h"
8 #include "lsst/afw/image/Image.h"
10 #include "lsst/afw/geom/Angle.h"
11 #include "lsst/afw/geom/Point.h"
12 
13 #include "lsst/log/Log.h"
14 #include "lsst/jointcal/CcdImage.h"
15 #include "lsst/jointcal/Gtransfo.h"
16 #include "lsst/jointcal/Point.h"
17 
18 namespace jointcal = lsst::jointcal;
19 namespace afwImg = lsst::afw::image;
20 
21 namespace {
22 LOG_LOGGER _log = LOG_GET("jointcal.CcdImage");
23 }
24 
25 namespace lsst {
26 namespace jointcal {
27 
29  out << "(visit: " << key.visit << ", ccd: " << key.ccd << ")";
30  return out;
31 }
32 
33 void CcdImage::loadCatalog(afw::table::SourceCatalog const &catalog, std::string const &fluxField) {
34  auto xKey = catalog.getSchema().find<double>("slot_Centroid_x").key;
35  auto yKey = catalog.getSchema().find<double>("slot_Centroid_y").key;
36  auto xsKey = catalog.getSchema().find<float>("slot_Centroid_xErr").key;
37  auto ysKey = catalog.getSchema().find<float>("slot_Centroid_yErr").key;
38  auto mxxKey = catalog.getSchema().find<double>("slot_Shape_xx").key;
39  auto myyKey = catalog.getSchema().find<double>("slot_Shape_yy").key;
40  auto mxyKey = catalog.getSchema().find<double>("slot_Shape_xy").key;
41  auto instFluxKey = catalog.getSchema().find<double>(fluxField + "_instFlux").key;
42  auto instFluxErrKey = catalog.getSchema().find<double>(fluxField + "_instFluxErr").key;
43 
44  auto transform = _detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
45 
46  _wholeCatalog.clear();
47  for (auto const &record : catalog) {
48  auto ms = std::make_shared<MeasuredStar>();
49  ms->setId(record.getId());
50  ms->x = record.get(xKey);
51  ms->y = record.get(yKey);
52  ms->vx = std::pow(record.get(xsKey), 2);
53  ms->vy = std::pow(record.get(ysKey), 2);
54  auto pointFocal = transform->applyForward(record.getCentroid());
55  ms->setXFocal(pointFocal.getX());
56  ms->setYFocal(pointFocal.getY());
57  /* the xy covariance is not provided in the input catalog: we
58  cook it up from the x and y position variance and the shape
59  measurements: */
60  double mxx = record.get(mxxKey);
61  double myy = record.get(myyKey);
62  double mxy = record.get(mxyKey);
63  ms->vxy = mxy * (ms->vx + ms->vy) / (mxx + myy);
64  if (std::isnan(ms->vxy) || ms->vx < 0 || ms->vy < 0 || (ms->vxy * ms->vxy) > (ms->vx * ms->vy)) {
65  LOGLS_WARN(_log, "Bad source detected during loadCatalog id: "
66  << ms->getId() << " with vx,vy: " << ms->vx << "," << ms->vy
67  << " vxy^2: " << ms->vxy * ms->vxy << " vx*vy: " << ms->vx * ms->vy);
68  continue;
69  }
70  ms->setInstFluxAndErr(record.get(instFluxKey), record.get(instFluxErrKey));
71  // TODO: the below lines will be less clumsy once DM-4044 is cleaned up and we can say:
72  // TODO: instFluxToMaggies(ms->getInstFlux(), ms) (because ms will be derived from afw::geom::Point).
73  afw::geom::Point<double, 2> point(ms->x, ms->y);
74  auto flux = _photoCalib->instFluxToMaggies(ms->getInstFlux(), ms->getInstFluxErr(), point);
75  ms->setFlux(flux.value);
76  ms->setFluxErr(flux.err);
77  auto mag = _photoCalib->instFluxToMagnitude(ms->getInstFlux(), ms->getInstFluxErr(), point);
78  ms->getMag() = mag.value;
79  ms->setMagErr(mag.err);
80  ms->setCcdImage(this);
81  _wholeCatalog.push_back(std::move(ms));
82  }
83  _wholeCatalog.setCcdImage(this);
84 }
85 
87  std::shared_ptr<lsst::afw::image::VisitInfo> visitInfo, afw::geom::Box2I const &bbox,
88  std::string const &filter, std::shared_ptr<afw::image::PhotoCalib> photoCalib,
89  std::shared_ptr<afw::cameraGeom::Detector> detector, int visit, int ccdId,
90  std::string const &fluxField)
91  : _ccdId(ccdId), _visit(visit), _photoCalib(photoCalib), _detector(detector), _filter(filter) {
92  loadCatalog(catalog, fluxField);
93 
94  Point lowerLeft(bbox.getMinX(), bbox.getMinY());
95  Point upperRight(bbox.getMaxX(), bbox.getMaxY());
96  _imageFrame = Frame(lowerLeft, upperRight);
97 
98  _readWcs = std::make_shared<GtransfoSkyWcs>(wcs);
99 
100  std::stringstream out;
101  out << visit << "_" << ccdId;
102  _name = out.str();
103 
104  _boresightRaDec = visitInfo->getBoresightRaDec();
105  _airMass = visitInfo->getBoresightAirmass();
106  _mjd = visitInfo->getDate().get(lsst::daf::base::DateTime::MJD);
107  double latitude = visitInfo->getObservatory().getLatitude();
108  _lstObs = visitInfo->getEra();
109  _hourAngle = visitInfo->getBoresightHourAngle();
110 
111  // lsstSim doesn't manage ERA (and thus Hour Angle) properly, so it's going to be NaN.
112  // Because we need the refraction vector later, go with 0 HA to prevent crashes on that NaN.
113  if (std::isnan(_hourAngle) == true) {
114  _hourAngle = 0;
115  }
116 
117  if (_airMass == 1)
118  _sineta = _coseta = _tgz = 0;
119  else {
120  double cosz = 1. / _airMass;
121  double sinz = std::sqrt(1 - cosz * cosz); // astronomers usually observe above the horizon
122  _tgz = sinz / cosz;
123  // TODO: as part of DM-12473, we can remove all of this and just call _visitInfo.getParallacticAngle()
124  double dec = _boresightRaDec.getLatitude();
125  // x/y components of refraction angle, eta.]
126  double yEta = std::sin(_hourAngle);
127  double xEta = std::cos(dec) * std::tan(latitude) - std::sin(dec) * std::cos(_hourAngle);
128  double eta = std::atan2(yEta, xEta);
129  _sineta = std::sin(eta);
130  _coseta = std::cos(eta);
131  }
132 }
133 
135  int measuredStars = 0;
136  int refStars = 0;
137  for (auto const &measuredStar : _catalogForFit) {
138  if (measuredStar->isValid()) {
139  measuredStars++;
140  }
141  if ((measuredStar->getFittedStar() != nullptr) &&
142  (measuredStar->getFittedStar()->getRefStar() != nullptr)) {
143  refStars++;
144  }
145  }
146  return std::make_pair(measuredStars, refStars);
147 }
148 
149 void CcdImage::setCommonTangentPoint(Point const &commonTangentPoint) {
150  _commonTangentPoint = commonTangentPoint;
151 
152  auto const crval = _readWcs->getSkyWcs()->getSkyOrigin();
153  jointcal::Point tangentPoint(crval[0].asDegrees(), crval[1].asDegrees());
154 
155  /* we don't assume here that we know the internals of TanPix2RaDec:
156  to construct pix->TP, we do pix->sky->TP, although pix->sky
157  actually goes through TP */
158  GtransfoLin identity;
159  TanRaDec2Pix raDec2TP(identity, tangentPoint);
160  _pix2TP = gtransfoCompose(raDec2TP, *_readWcs);
161  TanPix2RaDec CTP2RaDec(identity, commonTangentPoint);
162  _CTP2TP = gtransfoCompose(raDec2TP, CTP2RaDec);
163 
164  // jump from one TP to an other:
165  TanRaDec2Pix raDec2CTP(identity, commonTangentPoint);
166  TanPix2RaDec TP2RaDec(identity, tangentPoint);
167  _TP2CTP = gtransfoCompose(raDec2CTP, TP2RaDec);
168  _sky2TP.reset(new TanRaDec2Pix(identity, tangentPoint));
169 
170  // this one is needed for matches :
171  _pix2CommonTangentPlane = gtransfoCompose(raDec2CTP, *_readWcs);
172 }
173 } // namespace jointcal
174 } // namespace lsst
#define LOGLS_WARN(logger, message)
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:391
double dec
std::ostream & operator<<(std::ostream &out, CcdImageKey const &key)
Definition: CcdImage.cc:28
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:577
For hashing a ccdImage: the pair of (visit, ccd) IDs should be unique to each ccdImage.
Definition: CcdImage.h:28
table::PointKey< double > crval
T atan2(T... args)
STL class.
T sin(T... args)
table::Key< lsst::geom::Angle > latitude
rectangle with sides parallel to axes.
Definition: Frame.h:15
SchemaItem< T > find(std::string const &name) const
Class for a simple mapping implementing a generic Gtransfo.
Key< U > key
T make_pair(T... args)
T cos(T... args)
T move(T... args)
T get(T... args)
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
Definition: Gtransfo.h:653
T pow(T... args)
T tan(T... args)
T isnan(T... args)
T sqrt(T... args)
#define LOG_GET(logger)
table::Key< int > transform
STL class.
void setCommonTangentPoint(Point const &commonTangentPoint)
Sets the common tangent point and computes necessary transforms.
Definition: CcdImage.cc:149
std::pair< int, int > countStars() const
Count the number of valid measured and reference stars that fall within this ccdImage.
Definition: CcdImage.cc:134
std::unique_ptr< Gtransfo > gtransfoCompose(Gtransfo const &left, Gtransfo const &right)
Returns a pointer to a composition of gtransfos, representing left(right()).
Definition: Gtransfo.cc:384