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" 27 static double sqr(
double x) {
return x * x; }
30 LOG_LOGGER _log = LOG_GET(
"jointcal.Associations");
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);
48 LOGLS_DEBUG(_log,
"Catalog " <<
ccdImage->getName() <<
" has " <<
ccdImage->getWholeCatalog().size()
53 _commonTangentPoint =
Point(commonTangentPoint.getX(), commonTangentPoint.getY());
54 for (
auto &
ccdImage : ccdImageList)
ccdImage->setCommonTangentPoint(_commonTangentPoint);
58 const bool enlargeFittedList) {
63 for (
auto &item : fittedStarList) {
64 item->clearBeforeAssoc();
67 if (!useFittedList) fittedStarList.clear();
69 for (
auto &
ccdImage : ccdImageList) {
70 const Gtransfo *toCommonTangentPlane =
ccdImage->getPix2CommonTangentPlane();
75 ccdImage->getCatalogForFit().clear();
85 ccdImageFrameCPT = ccdImageFrameCPT.
rescale(1.10);
91 for (
auto const &fittedStar : fittedStarList) {
92 if (ccdImageFrameCPT.inFrame(*fittedStar)) {
93 toMatch.push_back(fittedStar);
99 toCommonTangentPlane, matchCutInArcSec / 3600.);
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());
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);
120 LOGLS_INFO(_log,
"Matched " << matchedCount <<
" objects in " <<
ccdImage->getName());
123 int unMatchedCount = 0;
124 for (
auto const &mstar : catalog) {
126 if (mstar->getFittedStar())
continue;
127 if (enlargeFittedList) {
128 auto fs = std::make_shared<FittedStar>(*mstar);
131 fittedStarList.push_back(fs);
132 mstar->setFittedStar(fs);
136 LOGLS_INFO(_log,
"Unmatched objects: " << unMatchedCount);
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 "));
151 afw::table::CoordKey coordKey = refCat.getSchema()[
"coord"];
152 auto fluxKey = refCat.getSchema().find<
double>(fluxField).key;
154 afw::table::Key<double> fluxErrKey;
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.");
162 std::cout <<
"Error key: " << fluxErrKey <<
" valid: " << fluxErrKey.isValid() << std::endl;
164 _filterMap.reserve(refFluxMap.size());
166 for (
auto const &filter : refFluxMap) {
167 _filterMap[filter.first] = nFilters;
172 for (
size_t i = 0; i < refCat.size(); i++) {
173 auto const &record = refCat.get(i);
175 afw::coord::Coord coord = record->get(coordKey);
177 double defaultFluxErr;
178 if (fluxErrKey.isValid()) {
181 defaultFluxErr = std::numeric_limits<double>::quiet_NaN();
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;
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);
197 star->vx = sqr(0.1 / 3600 / cos(coord.getLatitude()));
198 star->vy = sqr(0.1 / 3600);
201 refStarList.push_back(star);
208 associateRefStars(matchCut.asArcseconds(), &raDec2CTP);
213 Frame tangentPlaneFrame;
215 for (
auto const &
ccdImage : ccdImageList) {
218 if (tangentPlaneFrame.
getArea() == 0)
219 tangentPlaneFrame = CTPFrame;
221 tangentPlaneFrame += CTPFrame;
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);
236 void Associations::associateRefStars(
double matchCutInArcSec,
const Gtransfo *
gtransfo) {
240 matchCutInArcSec / 3600.);
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());
247 for (
auto const &starMatch : *starMatchList) {
251 const BaseStar &bs2 = *starMatch.s2;
259 "Associated " << starMatchList->size() <<
" reference stars among " << refStarList.size());
263 LOGLS_INFO(_log,
"Fitted stars before measurement # cut: " << fittedStarList.size());
267 for (
auto const &
ccdImage : ccdImageList) {
281 if (!fstar->getRefStar() && fstar->getMeasurementCount() < minMeasurements) {
282 auto f = std::const_pointer_cast<
FittedStar>(fstar);
284 mi = catalog.erase(mi);
294 if ((*fi)->getMeasurementCount() == 0)
295 fi = fittedStarList.erase(fi);
300 LOGLS_INFO(_log,
"Fitted stars after measurement # cut: " << fittedStarList.size());
303 void Associations::assignMags() {
304 for (
auto const &
ccdImage : ccdImageList) {
306 for (
auto const &mstar : catalog) {
307 auto fstar = mstar->getFittedStar();
308 if (!fstar)
continue;
309 auto f = std::const_pointer_cast<
FittedStar>(fstar);
319 if (!fittedStarList.inTangentPlaneCoordinates) {
321 "DeprojectFittedStars: Fitted stars are already in sidereal coordinates, nothing done ");
326 fittedStarList.applyTransfo(ctp2Sky);
327 fittedStarList.inTangentPlaneCoordinates =
false;
331 void Associations::collectMCStars(
int realization) {
335 for (I = ccdImageList.begin(); I != ccdImageList.end(); I++) {
337 string dbimdir = ccdImage.Dir();
338 string mctruth = dbimdir +
"/mc/mctruth.list";
340 if (realization >= 0) {
342 sstrm << dbimdir <<
"/mc/mctruth_" << realization <<
".list";
343 mctruth = sstrm.str();
350 DicStarList mctruthlist(mctruth);
354 for (smI = starMatchList->begin(); smI != starMatchList->end(); smI++) {
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");
369 LOGLS_FATAL(_log,
"CollectMCStars Unable to match MCTruth w/ catalog!");
373 void Associations::setFittedStarColors(std::string dicStarListName, std::string color,
374 double matchCutArcSec) {
376 size_t pos_minus = color.find(
'-');
377 bool compute_diff = (pos_minus != string::npos);
379 c1 = color.substr(0, pos_minus);
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 +
"\""));
395 if (fittedStarList.inTangentPlaneCoordinates) id_or_proj = &id;
400 id_or_proj, matchCutArcSec / 3600);
402 LOGLS_INFO(_log,
"Matched " << starMatchList->size() <<
'/' << fittedStarList.size()
403 <<
" FittedStars to color catalog");
405 for (
auto i = starMatchList->begin(); i != starMatchList->end(); ++i) {
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);
Objects used as position anchors, typically USNO stars.
implements the linear transformations (6 real coefficients).
MeasuredStarList::iterator MeasuredStarIterator
A hanger for star associations.
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
void addMagMeasurement(double magValue, double magWeight)
this routine will hopefully soon disappear.
int getMeasurementCount() const
double xMin
coordinate of boundary.
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.
Frame rescale(const double factor) const
rescale it. The center does not move.
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.
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.
Class for a simple mapping implementing a generic Gtransfo.
void setRefStar(const RefStar *_refStar)
Set the astrometric reference star associated with this star.
FittedStarList::iterator FittedStarIterator
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.
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
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...
void setCommonTangentPoint(lsst::afw::geom::Point2D const &commonTangentPoint)
Sets a shared tangent point for all ccdImages.
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.
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
std::shared_ptr< const FittedStar > getFittedStar() const
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
MeasuredStarList const & getCatalogForFit() const
Gets the catalog to be used for fitting, which may have been cleaned-up.
Combinatorial searches for linear transformations to go from list1 to list2.
BaseStarList & Fitted2Base(FittedStarList &This)
a virtual (interface) class for geometric transformations.
std::shared_ptr< const BaseStar > s2
const lsst::afw::geom::Box2D getRaDecBBox()
BaseStarList & Measured2Base(MeasuredStarList &This)
std::shared_ptr< const BaseStar > s1
Frame applyTransfo(const Frame &inputframe, const Gtransfo >ransfo, const WhichTransformed which)
Transform a Frame through a Transfo.
Handler of an actual image from a single CCD.
const double JanskyToMaggy
The objects which have been measured several times.
BaseStarList & Ref2Base(RefStarList &This)