LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
CcdImage.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of jointcal.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #include <assert.h>
26 #include <string>
27 #include <sstream>
28 #include <cmath>
29 
31 #include "lsst/pex/exceptions.h"
32 #include "lsst/afw/image/Image.h"
34 #include "lsst/geom/Angle.h"
35 #include "lsst/geom/Point.h"
36 
37 #include "lsst/log/Log.h"
38 #include "lsst/jointcal/CcdImage.h"
40 #include "lsst/jointcal/Point.h"
41 
42 namespace jointcal = lsst::jointcal;
43 namespace afwImg = lsst::afw::image;
44 
45 namespace {
46 LOG_LOGGER _log = LOG_GET("jointcal.CcdImage");
47 }
48 
49 namespace lsst {
50 namespace jointcal {
51 
53  out << "(visit: " << key.visit << ", detector: " << key.ccd << ")";
54  return out;
55 }
56 
57 void CcdImage::loadCatalog(afw::table::SourceCatalog const &catalog, std::string const &fluxField) {
58  auto xKey = catalog.getSchema().find<double>("slot_Centroid_x").key;
59  auto yKey = catalog.getSchema().find<double>("slot_Centroid_y").key;
60  auto xsKey = catalog.getSchema().find<float>("slot_Centroid_xErr").key;
61  auto ysKey = catalog.getSchema().find<float>("slot_Centroid_yErr").key;
62  auto mxxKey = catalog.getSchema().find<double>("slot_Shape_xx").key;
63  auto myyKey = catalog.getSchema().find<double>("slot_Shape_yy").key;
64  auto mxyKey = catalog.getSchema().find<double>("slot_Shape_xy").key;
65  auto instFluxKey = catalog.getSchema().find<double>(fluxField + "_instFlux").key;
66  auto instFluxErrKey = catalog.getSchema().find<double>(fluxField + "_instFluxErr").key;
67 
68  auto transform = _detector->getTransform(afw::cameraGeom::PIXELS, afw::cameraGeom::FOCAL_PLANE);
69 
70  _wholeCatalog.clear();
71  for (auto const &record : catalog) {
72  auto ms = std::make_shared<MeasuredStar>();
73  ms->setId(record.getId());
74  ms->x = record.get(xKey);
75  ms->y = record.get(yKey);
76  ms->vx = std::pow(record.get(xsKey), 2);
77  ms->vy = std::pow(record.get(ysKey), 2);
78  auto pointFocal = transform->applyForward(record.getCentroid());
79  ms->setXFocal(pointFocal.getX());
80  ms->setYFocal(pointFocal.getY());
81  /* the xy covariance is not provided in the input catalog: we
82  cook it up from the x and y position variance and the shape
83  measurements: */
84  double mxx = record.get(mxxKey);
85  double myy = record.get(myyKey);
86  double mxy = record.get(mxyKey);
87  ms->vxy = mxy * (ms->vx + ms->vy) / (mxx + myy);
88  if (std::isnan(ms->vxy) || ms->vx < 0 || ms->vy < 0 || (ms->vxy * ms->vxy) > (ms->vx * ms->vy)) {
89  LOGLS_WARN(_log, "Bad source detected during loadCatalog id: "
90  << ms->getId() << " with vx,vy: " << ms->vx << "," << ms->vy
91  << " vxy^2: " << ms->vxy * ms->vxy << " vx*vy: " << ms->vx * ms->vy);
92  continue;
93  }
94  ms->setInstFluxAndErr(record.get(instFluxKey), record.get(instFluxErrKey));
95  // TODO: the below lines will be less clumsy once DM-4044 is cleaned up and we can say:
96  // TODO: instFluxToNanojansky(ms->getInstFlux(), ms) (because ms will be derived from
97  // geom::Point).
98  geom::Point<double, 2> point(ms->x, ms->y);
99  auto flux = _photoCalib->instFluxToNanojansky(ms->getInstFlux(), ms->getInstFluxErr(), point);
100  ms->setFlux(flux.value);
101  ms->setFluxErr(flux.error);
102  auto mag = _photoCalib->instFluxToMagnitude(ms->getInstFlux(), ms->getInstFluxErr(), point);
103  ms->getMag() = mag.value;
104  ms->setMagErr(mag.error);
105  ms->setCcdImage(this);
106  _wholeCatalog.push_back(std::move(ms));
107  }
108  _wholeCatalog.setCcdImage(this);
109 }
110 
115  std::string const &fluxField)
116  : _ccdId(ccdId), _visit(visit), _photoCalib(photoCalib), _detector(detector), _filter(filter) {
117  loadCatalog(catalog, fluxField);
118 
119  Point lowerLeft(bbox.getMinX(), bbox.getMinY());
120  Point upperRight(bbox.getMaxX(), bbox.getMaxY());
121  _imageFrame = Frame(lowerLeft, upperRight);
122 
123  _readWcs = std::make_shared<AstrometryTransformSkyWcs>(wcs);
124 
125  std::stringstream out;
126  out << visit << "_" << ccdId;
127  _name = out.str();
128 
129  _boresightRaDec = visitInfo->getBoresightRaDec();
130  _airMass = visitInfo->getBoresightAirmass();
131  _mjd = visitInfo->getDate().get(lsst::daf::base::DateTime::MJD);
132  double latitude = visitInfo->getObservatory().getLatitude();
133  _lstObs = visitInfo->getEra();
134  _hourAngle = visitInfo->getBoresightHourAngle();
135 
136  // Some cameras don't manage ERA (and thus Hour Angle) properly, so it's going to be NaN.
137  // Because we need the refraction vector later, go with 0 HA to prevent crashes on that NaN.
138  if (std::isnan(_hourAngle) == true) {
139  _hourAngle = 0;
140  }
141 
142  if (_airMass == 1)
143  _sinEta = _cosEta = _tanZ = 0;
144  else {
145  double cosz = 1. / _airMass;
146  double sinz = std::sqrt(1 - cosz * cosz); // astronomers usually observe above the horizon
147  _tanZ = sinz / cosz;
148  // TODO: as part of DM-12473, we can remove all of this and just call _visitInfo.getParallacticAngle()
149  double dec = _boresightRaDec.getLatitude();
150  // x/y components of refraction angle, eta.]
151  double yEta = std::sin(_hourAngle);
152  double xEta = std::cos(dec) * std::tan(latitude) - std::sin(dec) * std::cos(_hourAngle);
153  double eta = std::atan2(yEta, xEta);
154  _sinEta = std::sin(eta);
155  _cosEta = std::cos(eta);
156  }
157 }
158 
160  int measuredStars = 0;
161  int refStars = 0;
162  for (auto const &measuredStar : _catalogForFit) {
163  if (measuredStar->isValid()) {
164  measuredStars++;
165  }
166  if ((measuredStar->getFittedStar() != nullptr) &&
167  (measuredStar->getFittedStar()->getRefStar() != nullptr)) {
168  refStars++;
169  }
170  }
171  return std::make_pair(measuredStars, refStars);
172 }
173 
174 void CcdImage::setCommonTangentPoint(Point const &commonTangentPoint) {
175  _commonTangentPoint = commonTangentPoint;
176 
177  auto const crval = _readWcs->getSkyWcs()->getSkyOrigin();
178  jointcal::Point tangentPoint(crval[0].asDegrees(), crval[1].asDegrees());
179 
180  /* we don't assume here that we know the internals of TanPixelToRaDec:
181  to construct pix->TP, we do pix->sky->TP, although pix->sky
182  actually goes through TP */
183  AstrometryTransformLinear identity;
184  TanRaDecToPixel raDecToTangentPlane(identity, tangentPoint);
185  _pixelToTangentPlane = compose(raDecToTangentPlane, *_readWcs);
186  TanPixelToRaDec CommonTangentPlane2RaDec(identity, commonTangentPoint);
187  _commonTangentPlaneToTangentPlane = compose(raDecToTangentPlane, CommonTangentPlane2RaDec);
188 
189  // jump from one TP to an other:
190  TanRaDecToPixel raDecToCommonTangentPlane(identity, commonTangentPoint);
191  TanPixelToRaDec TangentPlaneToRaDec(identity, tangentPoint);
192  _tangentPlaneToCommonTangentPlane = compose(raDecToCommonTangentPlane, TangentPlaneToRaDec);
193  _skyToTangentPlane.reset(new TanRaDecToPixel(identity, tangentPoint));
194 
195  // this one is needed for matches :
196  _pixelToCommonTangentPlane = compose(raDecToCommonTangentPlane, *_readWcs);
197 }
198 } // namespace jointcal
199 } // namespace lsst
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
table::Key< int > detector
afw::table::Key< afw::table::Array< ImagePixelT > > image
LSST DM logging module built on log4cxx.
#define LOGLS_WARN(logger, message)
Log a warn-level message using an iostream-based interface.
Definition: Log.h:648
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:75
#define LOG_LOGGER
Definition: Log.h:703
double dec
Definition: Match.cc:41
table::PointKey< double > crval
Definition: OldWcs.cc:130
Implementation of the Photometric Calibration class.
Key< U > key
Definition: Schema.cc:281
table::Key< table::Array< std::uint8_t > > wcs
Definition: SkyWcs.cc:71
table::Key< int > transform
table::Key< lsst::geom::Angle > latitude
Definition: VisitInfo.cc:205
T atan2(T... args)
An integer coordinate rectangle.
Definition: Box.h:55
Angle getLatitude() const noexcept
The latitude of this point.
Definition: SpherePoint.h:190
implements the linear transformations (6 real coefficients).
void setCommonTangentPoint(Point const &commonTangentPoint)
Sets the common tangent point and computes necessary transforms.
Definition: CcdImage.cc:174
CcdImage(afw::table::SourceCatalog &record, std::shared_ptr< lsst::afw::geom::SkyWcs > wcs, std::shared_ptr< lsst::afw::image::VisitInfo > visitInfo, lsst::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, std::string const &fluxField)
Definition: CcdImage.cc:111
std::pair< int, int > countStars() const
Count the number of valid measured and reference stars that fall within this ccdImage.
Definition: CcdImage.cc:159
rectangle with sides parallel to axes.
Definition: Frame.h:38
void setCcdImage(const CcdImage *_ccdImage)
Definition: MeasuredStar.cc:68
A point in a plane.
Definition: Point.h:36
The transformation that handles pixels to sideral transformations (Gnomonic, possibly with polynomial...
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane)
T clear(T... args)
T cos(T... args)
T isnan(T... args)
T make_pair(T... args)
T move(T... args)
CameraSys const FOCAL_PLANE
Focal plane coordinates: Position on a 2-d planar approximation to the focal plane (x,...
Definition: CameraSys.cc:30
CameraSysPrefix const PIXELS
Pixel coordinates: Nominal position on the entry surface of a given detector (x, y unbinned pixels).
Definition: CameraSys.cc:34
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
std::unique_ptr< AstrometryTransform > compose(AstrometryTransform const &left, AstrometryTransform const &right)
Returns a pointer to a composition of transforms, representing left(right()).
std::ostream & operator<<(std::ostream &stream, AstrometryMapping const &mapping)
A base class for image defects.
T pow(T... args)
T push_back(T... args)
T sin(T... args)
T sqrt(T... args)
T str(T... args)
For hashing a ccdImage: the pair of (visit, ccd) IDs should be unique to each ccdImage.
Definition: CcdImage.h:51
Key< int > visitInfo
Definition: Exposure.cc:70
Key< int > photoCalib
Definition: Exposure.cc:67
T tan(T... args)