6 #include "lsst/log/Log.h"
15 #include "lsst/afw/image/Image.h"
16 #include "lsst/afw/image/VisitInfo.h"
17 #include "lsst/daf/base/PropertySet.h"
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"
25 namespace jointcal = lsst::jointcal;
27 static double sqr(
double x) {
return x * x; }
30 LOG_LOGGER _log = LOG_GET(
"jointcal.Associations");
39 std::shared_ptr<lsst::afw::image::TanWcs> wcs,
40 std::shared_ptr<lsst::afw::image::VisitInfo> visitInfo,
41 lsst::afw::geom::Box2I
const &bbox, std::string
const &filter,
42 std::shared_ptr<lsst::afw::image::Calib> calib,
int visit,
int ccd,
43 std::shared_ptr<lsst::jointcal::JointcalControl> control) {
44 auto ccdImage = std::make_shared<CcdImage>(catalog, wcs, visitInfo, bbox, filter, calib, visit, ccd,
45 control->sourceFluxField);
47 LOGLS_DEBUG(_log,
"Catalog " << ccdImage->getName() <<
" has " << ccdImage->getWholeCatalog().size()
57 const bool enlargeFittedList) {
63 item->clearBeforeAssoc();
66 if (!useFittedList) fittedStarList.clear();
69 const Gtransfo *toCommonTangentPlane = ccdImage->getPix2CommonTangentPlane();
74 ccdImage->getCatalogForFit().clear();
75 ccdImage->getWholeCatalog().copyTo(ccdImage->getCatalogForFit());
84 ccdImageFrameCPT = ccdImageFrameCPT.
rescale(1.10);
90 for (
auto const &fittedStar : fittedStarList) {
91 if (ccdImageFrameCPT.inFrame(*fittedStar)) {
92 toMatch.push_back(fittedStar);
98 toCommonTangentPlane, matchCutInArcSec / 3600.);
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());
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);
119 LOGLS_INFO(_log,
"Matched " << matchedCount <<
" objects in " << ccdImage->getName());
122 int unMatchedCount = 0;
123 for (
auto const &mstar : catalog) {
125 if (mstar->getFittedStar())
continue;
126 if (enlargeFittedList) {
127 auto fs = std::make_shared<FittedStar>(*mstar);
130 fittedStarList.push_back(fs);
131 mstar->setFittedStar(fs);
135 LOGLS_INFO(_log,
"Unmatched objects: " << unMatchedCount);
142 afw::geom::Angle matchCut, std::string
const &fluxField) {
143 if (refCat.size() == 0) {
144 throw(LSST_EXCEPT(pex::exceptions::InvalidParameterError,
145 " reference catalog is empty : stop here "));
151 afw::table::CoordKey coordKey = refCat.getSchema()[
"coord"];
152 auto fluxKey = refCat.getSchema().find<
double>(fluxField).key;
154 for (
auto const &record : refCat) {
155 lsst::afw::coord::Coord coord = record.get(coordKey);
156 double flux = record.get(fluxKey);
157 double mag = lsst::afw::image::abMagFromFlux(flux);
158 double ra = lsst::afw::geom::radToDeg(coord.getLongitude());
159 double dec = lsst::afw::geom::radToDeg(coord.getLatitude());
166 star.vx = sqr(0.1 / 3600 / cos(coord.getLatitude()));
167 star.vy = sqr(0.1 / 3600);
169 refStarList.push_back(std::make_shared<RefStar>(star));
176 associateRefStars(matchCut.asArcseconds(), &raDec2CTP);
181 Frame tangentPlaneFrame;
186 if (tangentPlaneFrame.
getArea() == 0)
187 tangentPlaneFrame = CTPFrame;
189 tangentPlaneFrame += CTPFrame;
197 lsst::afw::geom::Point<double> min(raDecFrame.
xMin, raDecFrame.
yMin);
198 lsst::afw::geom::Point<double> max(raDecFrame.
xMax, raDecFrame.
yMax);
199 lsst::afw::geom::Box2D box(min, max);
204 void Associations::associateRefStars(
double matchCutInArcSec,
const Gtransfo *gtransfo) {
208 matchCutInArcSec / 3600.);
210 LOGLS_DEBUG(_log,
"Refcat matches before removing ambiguities " << starMatchList->size());
211 starMatchList->removeAmbiguities(*gtransfo);
212 LOGLS_DEBUG(_log,
"Refcat matches after removing ambiguities " << starMatchList->size());
215 for (
auto const &starMatch : *starMatchList) {
219 const BaseStar &bs2 = *starMatch.s2;
227 "Associated " << starMatchList->size() <<
" reference stars among " <<
refStarList.size());
231 LOGLS_INFO(_log,
"Fitted stars before measurement # cut: " <<
fittedStarList.size());
249 if (!fstar->getRefStar() && fstar->getMeasurementCount() < minMeasurements) {
250 auto f = std::const_pointer_cast<
FittedStar>(fstar);
252 mi = catalog.erase(mi);
262 if ((*fi)->getMeasurementCount() == 0)
268 LOGLS_INFO(_log,
"Fitted stars after measurement # cut: " <<
fittedStarList.size());
271 void Associations::assignMags() {
274 for (
auto const &mstar : catalog) {
275 auto fstar = mstar->getFittedStar();
276 if (!fstar)
continue;
277 auto f = std::const_pointer_cast<
FittedStar>(fstar);
289 "DeprojectFittedStars: Fitted stars are already in sidereal coordinates, nothing done ");
299 void Associations::collectMCStars(
int realization) {
303 for (I = ccdImageList.begin(); I != ccdImageList.end(); I++) {
305 string dbimdir = ccdImage.Dir();
306 string mctruth = dbimdir +
"/mc/mctruth.list";
308 if (realization >= 0) {
310 sstrm << dbimdir <<
"/mc/mctruth_" << realization <<
".list";
311 mctruth = sstrm.str();
314 GtransfoIdentity gti;
318 DicStarList mctruthlist(mctruth);
322 for (smI = starMatchList->begin(); smI != starMatchList->end(); smI++) {
323 StarMatch &sm = *smI;
324 BaseStar *bs = sm.s1;
325 MeasuredStar *mstar =
dynamic_cast<MeasuredStar *
>(bs);
327 DicStar *dstar =
dynamic_cast<DicStar *
>(bs);
328 std::unique_ptr<BaseStarWithError> mcstar(
new BaseStarWithError(*bs));
329 mcstar->GetMCInfo().iflux = dstar->getval(
"iflux");
330 mcstar->GetMCInfo().tflux = dstar->getval(
"sflux");
337 LOGLS_FATAL(_log,
"CollectMCStars Unable to match MCTruth w/ catalog!");
341 void Associations::setFittedStarColors(std::string dicStarListName, std::string color,
342 double matchCutArcSec) {
344 size_t pos_minus = color.find(
'-');
345 bool compute_diff = (pos_minus != string::npos);
347 c1 = color.substr(0, pos_minus);
348 if (compute_diff) c2 = color.substr(pos_minus + 1, string::npos);
349 DicStarList cList(dicStarListName);
350 if (!cList.HasKey(c1))
351 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
352 " misses a key named \"" + c1 +
"\""));
353 if (compute_diff && !cList.HasKey(c2))
354 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
355 " misses a key named \"" + c2 +
"\""));
362 Gtransfo *id_or_proj = &proj;
368 id_or_proj, matchCutArcSec / 3600);
370 LOGLS_INFO(_log,
"Matched " << starMatchList->size() <<
'/' <<
fittedStarList.size()
371 <<
" FittedStars to color catalog");
373 for (
auto i = starMatchList->begin(); i != starMatchList->end(); ++i) {
374 BaseStar *s1 = i->s1;
375 FittedStar *fs =
dynamic_cast<FittedStar *
>(s1);
376 BaseStar *s2 = i->s2;
377 const TStar *ts =
dynamic_cast<const TStar *
>(s2);
378 const DicStar *ds =
dynamic_cast<const DicStar *
>(ts->get_original());
379 fs->
color = ds->getval(c1);
380 if (compute_diff) fs->color -= ds->getval(c2);
Point getCommonTangentPoint() const
can be used to project sidereal coordinates related to the image set on a plane.
Objects used as position anchors, typically USNO stars.
implements the linear transformations (6 real coefficients).
MeasuredStarList::iterator MeasuredStarIterator
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
StarList< BaseStar > BaseStarList
void addMagMeasurement(double magValue, double magWeight)
this routine will hopefully soon disappear.
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< lsst::afw::image::Calib > calib, 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.
std::shared_ptr< const FittedStar > getFittedStar() const
double xMin
coordinate of boundary.
CcdImageList ccdImageList
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::AddImage.
void selectFittedStars(int minMeasurements)
Set the color field of FittedStar 's from a colored catalog.
The base class for handling stars. Used by all matching routines.
rectangle with sides parallel to axes.
void setRefStar(const RefStar *_refStar)
Set the astrometric reference star associated with this star.
FittedStarList::iterator FittedStarIterator
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
A list of FittedStar s. Such a list is typically constructed by Associations.
std::unique_ptr< StarMatchList > listMatchCollect(const BaseStarList &list1, const BaseStarList &list2, const Gtransfo *guess, const double maxDist)
assembles star matches.
objects measured on actual images.
::std::list< StarMatch >::iterator StarMatchIterator
void deprojectFittedStars()
Sends back the fitted stars coordinates on the sky FittedStarsList::inTangentPlaneCoordinates keeps t...
const MeasuredStarList & getCatalogForFit() const
Gets the catalog to be used for fitting, which may have been cleaned-up.
bool inTangentPlaneCoordinates
void setCommonTangentPoint(lsst::afw::geom::Point2D const &commonTangentPoint)
Sets a shared tangent point for all ccdImages.
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
Combinatorial searches for linear transformations to go from list1 to list2.
BaseStarList & Fitted2Base(FittedStarList &This)
a virtual (interface) class for geometric transformations.
const lsst::afw::geom::Box2D getRaDecBBox()
BaseStarList & Measured2Base(MeasuredStarList &This)
int getMeasurementCount() const
Associations()
Source selection is performed in python, so Associations' constructor only initializes a couple of va...
void collectRefStars(lsst::afw::table::SortedCatalogT< lsst::afw::table::SimpleRecord > &refCat, afw::geom::Angle matchCut, std::string const &fluxField)
Collect stars from an external reference catalog and associate them with fittedStars.
Frame rescale(const double factor) const
rescale it. The center does not move.
Frame applyTransfo(const Frame &inputframe, const Gtransfo >ransfo, const WhichTransformed which)
Transform a Frame through a Transfo.
Point _commonTangentPoint
Handler of an actual image from a single CCD.
void applyTransfo(const Operator &op)
enables to apply a geometrical transfo if Star is Basestar or derives from it.
The objects which have been measured several times.
FittedStarList fittedStarList
BaseStarList & Ref2Base(RefStarList &This)