29 LOG_LOGGER _log =
LOG_GET(
"jointcal.Associations");
41 lsst::afw::geom::Box2I
const &bbox,
std::string const &filter,
45 auto ccdImage = std::make_shared<CcdImage>(catalog, wcs, visitInfo, bbox, filter, photoCalib, detector,
54 centers.
reserve(ccdImageList.size());
55 for (
auto const &
ccdImage : ccdImageList) {
58 auto commonTangentPoint = afw::geom::averageSpherePoint(centers);
59 LOGLS_DEBUG(_log,
"Using common tangent point: " << commonTangentPoint.getPosition(afw::geom::degrees));
60 setCommonTangentPoint(commonTangentPoint.getPosition(afw::geom::degrees));
64 _commonTangentPoint =
Point(commonTangentPoint.getX(), commonTangentPoint.getY());
65 for (
auto &
ccdImage : ccdImageList)
ccdImage->setCommonTangentPoint(_commonTangentPoint);
69 const bool enlargeFittedList) {
74 for (
auto &item : fittedStarList) {
75 item->clearBeforeAssoc();
78 if (!useFittedList) fittedStarList.clear();
80 for (
auto &
ccdImage : ccdImageList) {
81 const Gtransfo *toCommonTangentPlane =
ccdImage->getPix2CommonTangentPlane();
91 Frame ccdImageFrameCPT = toCommonTangentPlane->
apply(
ccdImage->getImageFrame(),
false);
92 ccdImageFrameCPT = ccdImageFrameCPT.
rescale(1.10);
97 for (
auto const &fittedStar : fittedStarList) {
98 if (ccdImageFrameCPT.
inFrame(*fittedStar)) {
105 toCommonTangentPlane, matchCutInArcSec / 3600.);
108 LOGLS_DEBUG(_log,
"Measured-to-Fitted matches before removing ambiguities " << starMatchList->size());
109 starMatchList->removeAmbiguities(*toCommonTangentPlane);
110 LOGLS_DEBUG(_log,
"Measured-to-Fitted matches after removing ambiguities " << starMatchList->size());
114 int matchedCount = 0;
115 for (
auto const &starMatch : *starMatchList) {
116 auto bs = starMatch.s1;
119 auto bs2 = starMatch.s2;
122 ms->setFittedStar(fs);
125 LOGLS_INFO(_log,
"Matched " << matchedCount <<
" objects in " <<
ccdImage->getName());
128 int unMatchedCount = 0;
129 for (
auto const &mstar : catalog) {
131 if (mstar->getFittedStar())
continue;
132 if (enlargeFittedList) {
133 auto fs = std::make_shared<FittedStar>(*mstar);
136 fittedStarList.push_back(fs);
137 mstar->setFittedStar(fs);
141 LOGLS_INFO(_log,
"Unmatched objects: " << unMatchedCount);
156 bool rejectBadFluxes) {
157 if (refCat.
size() == 0) {
159 " reference catalog is empty : stop here "));
170 << fluxField <<
"Sigma" 171 <<
") not found in reference catalog. Not using ref flux errors.");
174 _filterMap.reserve(refFluxMap.size());
176 for (
auto const &filter : refFluxMap) {
177 _filterMap[filter.first] = nFilters;
182 for (
size_t i = 0; i < refCat.
size(); i++) {
183 auto const &record = refCat.
get(i);
185 auto coord = record->get(coordKey);
187 double defaultFluxErr;
188 if (fluxErrKey.isValid()) {
195 for (
auto const &filter : _filterMap) {
196 fluxList[filter.second] = refFluxMap.
at(filter.first).at(i) /
JanskyToMaggy;
197 fluxErrList[filter.second] = refFluxErrMap.
at(filter.first).at(i) /
JanskyToMaggy;
201 auto star = std::make_shared<RefStar>(ra, dec, defaultFlux, defaultFluxErr, fluxList, fluxErrList);
207 star->vx =
std::pow(0.1 / 3600 /
cos(coord.getLatitude()), 2);
212 if (rejectBadFluxes &&
215 refStarList.push_back(star);
222 associateRefStars(matchCut.asArcseconds(), &raDec2CTP);
227 Frame tangentPlaneFrame;
229 for (
auto const &
ccdImage : ccdImageList) {
231 if (tangentPlaneFrame.
getArea() == 0)
232 tangentPlaneFrame = CTPFrame;
234 tangentPlaneFrame += CTPFrame;
240 Frame raDecFrame = CTP2RaDec.
apply(tangentPlaneFrame,
false);
242 lsst::afw::geom::Point<double>
min(raDecFrame.
xMin, raDecFrame.
yMin);
243 lsst::afw::geom::Point<double>
max(raDecFrame.
xMax, raDecFrame.
yMax);
244 lsst::afw::geom::Box2D box(min, max);
249 void Associations::associateRefStars(
double matchCutInArcSec,
const Gtransfo *
gtransfo) {
253 matchCutInArcSec / 3600.);
255 LOGLS_DEBUG(_log,
"Refcat matches before removing ambiguities " << starMatchList->size());
256 starMatchList->removeAmbiguities(*gtransfo);
257 LOGLS_DEBUG(_log,
"Refcat matches after removing ambiguities " << starMatchList->size());
260 for (
auto const &starMatch : *starMatchList) {
264 const BaseStar &bs2 = *starMatch.s2;
272 "Associated " << starMatchList->size() <<
" reference stars among " << refStarList.size());
276 selectFittedStars(minMeasurements);
277 normalizeFittedStars();
280 void Associations::selectFittedStars(
int minMeasurements) {
281 LOGLS_INFO(_log,
"Fitted stars before measurement # cut: " << fittedStarList.size());
284 for (
auto const &
ccdImage : ccdImageList) {
292 if (fittedStar ==
nullptr) {
299 if (!fittedStar->getRefStar() && fittedStar->getMeasurementCount() < minMeasurements) {
300 fittedStar->getMeasurementCount()--;
301 mi = catalog.
erase(mi);
310 if ((*fi)->getMeasurementCount() == 0) {
311 fi = fittedStarList.erase(fi);
317 LOGLS_INFO(_log,
"Fitted stars after measurement # cut: " << fittedStarList.size());
320 void Associations::normalizeFittedStars()
const {
322 for (
auto &fittedStar : fittedStarList) {
325 fittedStar->setFlux(0.0);
326 fittedStar->getMag() = 0.0;
330 for (
auto const &
ccdImage : ccdImageList) {
331 const Gtransfo *toCommonTangentPlane =
ccdImage->getPix2CommonTangentPlane();
333 for (
auto &mi : catalog) {
334 auto fittedStar = mi->getFittedStar();
335 if (fittedStar ==
nullptr)
338 "All measuredStars must have a fittedStar: did you call selectFittedStars()?"));
339 auto point = toCommonTangentPlane->
apply(*mi);
340 fittedStar->x += point.x;
341 fittedStar->y += point.y;
342 fittedStar->getFlux() += mi->getFlux();
346 for (
auto &fi : fittedStarList) {
347 auto measurementCount = fi->getMeasurementCount();
348 fi->x /= measurementCount;
349 fi->y /= measurementCount;
350 fi->getFlux() /= measurementCount;
351 fi->getMag() = magFromFlux(fi->getFlux());
355 void Associations::assignMags() {
356 for (
auto const &
ccdImage : ccdImageList) {
358 for (
auto const &mstar : catalog) {
359 auto fstar = mstar->getFittedStar();
360 if (!fstar)
continue;
361 fstar->addMagMeasurement(mstar->getMag(), mstar->getMagWeight());
369 if (!fittedStarList.inTangentPlaneCoordinates) {
371 "DeprojectFittedStars: Fitted stars are already in sidereal coordinates, nothing done ");
376 fittedStarList.applyTransfo(ctp2Sky);
377 fittedStarList.inTangentPlaneCoordinates =
false;
382 return item->getCatalogForFit().size() > 0;
388 for (
auto const &fittedStar : fittedStarList) {
389 if ((fittedStar !=
nullptr) & (fittedStar->getRefStar() !=
nullptr)) count++;
395 void Associations::collectMCStars(
int realization) {
399 for (I = ccdImageList.begin(); I != ccdImageList.end(); I++) {
401 string dbimdir = ccdImage.Dir();
402 string mctruth = dbimdir +
"/mc/mctruth.list";
404 if (realization >= 0) {
406 sstrm << dbimdir <<
"/mc/mctruth_" << realization <<
".list";
407 mctruth = sstrm.
str();
414 DicStarList mctruthlist(mctruth);
418 for (smI = starMatchList->begin(); smI != starMatchList->end(); smI++) {
423 DicStar *dstar =
dynamic_cast<DicStar *
>(bs);
425 mcstar->GetMCInfo().iflux = dstar->getval(
"iflux");
426 mcstar->GetMCInfo().tflux = dstar->getval(
"sflux");
433 LOGLS_FATAL(_log,
"CollectMCStars Unable to match MCTruth w/ catalog!");
438 double matchCutArcSec) {
440 size_t pos_minus = color.
find(
'-');
441 bool compute_diff = (pos_minus != string::npos);
443 c1 = color.
substr(0, pos_minus);
444 if (compute_diff) c2 = color.
substr(pos_minus + 1, string::npos);
445 DicStarList cList(dicStarListName);
446 if (!cList.HasKey(c1))
447 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
448 " misses a key named \"" + c1 +
"\""));
449 if (compute_diff && !cList.HasKey(c2))
450 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
451 " misses a key named \"" + c2 +
"\""));
459 if (fittedStarList.inTangentPlaneCoordinates) id_or_proj = &id;
464 id_or_proj, matchCutArcSec / 3600);
466 LOGLS_INFO(_log,
"Matched " << starMatchList->size() <<
'/' << fittedStarList.size()
467 <<
" FittedStars to color catalog");
469 for (
auto i = starMatchList->begin(); i != starMatchList->end(); ++i) {
473 const TStar *ts =
dynamic_cast<const TStar *
>(s2);
474 const DicStar *ds =
dynamic_cast<const DicStar *
>(ts->get_original());
475 fs->
color = ds->getval(c1);
476 if (compute_diff) fs->
color -= ds->getval(c2);
#define LOGLS_WARN(logger, message)
Objects used as position anchors, typically USNO stars.
implements the linear transformations (6 real coefficients).
int nCcdImagesValidForFit() const
return the number of CcdImages with non-empty catalogs to-be-fit.
MeasuredStarList::iterator MeasuredStarIterator
A hanger for star associations.
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
Transform pixels to ICRS RA, Dec in degrees.
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::createCcdImage.
Frame rescale(const double factor) const
rescale it. The center does not move.
double radToDeg(long double angleInRadians)
The base class for handling stars. Used by all matching routines.
void prepareFittedStars(int minMeasurements)
Set the color field of FittedStar 's from a colored catalog.
size_t nFittedStarsWithAssociatedRefStar() const
Return the number of fittedStars that have an associated refStar.
void collectRefStars(afw::table::SimpleCatalog &refCat, afw::geom::Angle matchCut, std::string const &fluxField, RefFluxMapType const &refFluxMap=RefFluxMapType(), RefFluxMapType const &refFluxErrMap=RefFluxMapType(), bool rejectBadFluxes=false)
Collect stars from an external reference catalog and associate them with fittedStars.
rectangle with sides parallel to axes.
#define LOGLS_DEBUG(logger, message)
SchemaItem< T > find(std::string const &name) const
Class for a simple mapping implementing a generic Gtransfo.
void setRefStar(const RefStar *_refStar)
Set the astrometric reference star associated with this star.
void computeCommonTangentPoint()
Sets a shared tangent point for all ccdImages, using the mean of the centers of all ccdImages...
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.
T dynamic_pointer_cast(T... args)
::std::list< StarMatch >::iterator StarMatchIterator
void deprojectFittedStars()
Sends back the fitted stars coordinates on the sky FittedStarsList::inTangentPlaneCoordinates keeps t...
#define LOGLS_INFO(logger, message)
void setCommonTangentPoint(lsst::afw::geom::Point2D const &commonTangentPoint)
Sets a shared tangent point for all ccdImages.
bool inFrame(double x, double y) const
inside?
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
#define LSST_EXCEPT(type,...)
void createCcdImage(afw::table::SourceCatalog &catalog, std::shared_ptr< lsst::afw::geom::SkyWcs > 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, std::shared_ptr< afw::cameraGeom::Detector > detector, int visit, int ccd, lsst::jointcal::JointcalControl const &control)
Create a ccdImage from an exposure catalog and metadata, and add it to the list.
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< RecordT > const get(size_type i) const
std::shared_ptr< const BaseStar > s1
std::string sourceFluxField
"name of flux field in source catalog" ;
#define LOGLS_FATAL(logger, message)
Handler of an actual image from a single CCD.
const double JanskyToMaggy
std::shared_ptr< FittedStar > getFittedStar() const
The objects which have been measured several times.
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0
BaseStarList & Ref2Base(RefStarList &This)