72 LOGLS_DEBUG(_log,
"Catalog " << ccdImage->getName() <<
" has " << ccdImage->getWholeCatalog().size()
80 centers.
push_back(ccdImage->getBoresightRaDec());
88 _commonTangentPoint =
Point(commonTangentPoint.getX(), commonTangentPoint.getY());
89 for (
auto &ccdImage :
ccdImageList) ccdImage->setCommonTangentPoint(_commonTangentPoint);
94 Frame tangentPlaneFrame;
97 Frame CTPFrame = ccdImage->getPixelToCommonTangentPlane()->apply(ccdImage->getImageFrame(),
false);
98 if (tangentPlaneFrame.
getArea() == 0)
99 tangentPlaneFrame = CTPFrame;
101 tangentPlaneFrame += CTPFrame;
106 TanPixelToRaDec commonTangentPlaneToRaDec(identity, _commonTangentPoint);
107 Frame raDecFrame = commonTangentPlaneToRaDec.
apply(tangentPlaneFrame,
false);
122 const bool enlargeFittedList) {
128 item->clearBeforeAssoc();
138 ccdImage->resetCatalogForFit();
144 Frame ccdImageFrameCPT = toCommonTangentPlane->apply(ccdImage->getImageFrame(),
false);
145 ccdImageFrameCPT = ccdImageFrameCPT.
rescale(1.10);
151 if (ccdImageFrameCPT.
inFrame(*fittedStar)) {
158 toCommonTangentPlane.
get(), matchCutInArcSec / 3600.);
161 LOGLS_DEBUG(_log,
"Measured-to-Fitted matches before removing ambiguities " << starMatchList->size());
162 starMatchList->removeAmbiguities(*toCommonTangentPlane);
163 LOGLS_DEBUG(_log,
"Measured-to-Fitted matches after removing ambiguities " << starMatchList->size());
167 int matchedCount = 0;
168 for (
auto const &starMatch : *starMatchList) {
169 auto bs = starMatch.s1;
170 auto ms_const = std::dynamic_pointer_cast<const MeasuredStar>(bs);
171 auto ms = std::const_pointer_cast<MeasuredStar>(ms_const);
172 auto bs2 = starMatch.s2;
173 auto fs_const = std::dynamic_pointer_cast<const FittedStar>(bs2);
174 auto fs = std::const_pointer_cast<FittedStar>(fs_const);
175 ms->setFittedStar(
fs);
178 LOGLS_INFO(_log,
"Matched " << matchedCount <<
" objects in " << ccdImage->getName());
181 int unMatchedCount = 0;
182 for (
auto const &mstar : catalog) {
184 if (mstar->getFittedStar())
continue;
185 if (enlargeFittedList) {
186 auto fs = std::make_shared<FittedStar>(*mstar);
188 toCommonTangentPlane->transformPosAndErrors(*
fs, *
fs);
190 mstar->setFittedStar(
fs);
194 LOGLS_INFO(_log,
"Unmatched objects: " << unMatchedCount);
206 std::string const &fluxField,
float refCoordinateErr,
207 bool rejectBadFluxes) {
208 if (refCat.size() == 0) {
210 " reference catalog is empty : stop here "));
218 raErrKey = refCat.getSchema()[
"coord_raErr"];
219 decErrKey = refCat.getSchema()[
"coord_decErr"];
222 auto fluxKey = refCat.getSchema().
find<
double>(fluxField).
key;
226 fluxErrKey = refCat.getSchema().
find<
double>(fluxField +
"Err").
key;
229 << fluxField <<
"Err"
230 <<
") not found in reference catalog. Not using ref flux errors.");
234 for (
size_t i = 0; i < refCat.size(); i++) {
235 auto const &record = refCat.get(i);
237 auto coord = record->get(coordKey);
238 double flux = record->get(fluxKey);
240 if (fluxErrKey.isValid()) {
241 fluxErr = record->get(fluxErrKey);
247 auto star = std::make_shared<RefStar>(ra,
dec, flux, fluxErr);
250 star->vx = record->get(raErrKey);
251 star->vy = record->get(decErrKey);
254 star->vx =
std::pow(refCoordinateErr / 1000. / 3600. /
std::cos(coord.getLatitude()), 2);
255 star->vy =
std::pow(refCoordinateErr / 1000. / 3600., 2);
267 TanRaDecToPixel raDecToCommonTangentPlane(identity, _commonTangentPoint);
269 associateRefStars(matchCut.
asArcseconds(), &raDecToCommonTangentPlane);
276 matchCutInArcSec / 3600.);
278 LOGLS_DEBUG(_log,
"Refcat matches before removing ambiguities " << starMatchList->size());
279 starMatchList->removeAmbiguities(*
transform);
280 LOGLS_DEBUG(_log,
"Refcat matches after removing ambiguities " << starMatchList->size());
283 for (
auto const &starMatch : *starMatchList) {
287 const BaseStar &bs2 = *starMatch.s2;
295 "Associated " << starMatchList->size() <<
" reference stars among " <<
refStarList.
size());
299 selectFittedStars(minMeasurements);
300 normalizeFittedStars();
303 void Associations::selectFittedStars(
int minMeasurements) {
306 int totalMeasured = 0, validMeasured = 0;
318 if (fittedStar ==
nullptr) {
325 if (!fittedStar->getRefStar() && fittedStar->getMeasurementCount() < minMeasurements) {
326 fittedStar->getMeasurementCount()--;
327 mi = catalog.
erase(mi);
337 if ((*fi)->getMeasurementCount() == 0) {
345 LOGLS_INFO(_log,
"Total, valid number of Measured stars: " << totalMeasured <<
", " << validMeasured);
348 void Associations::normalizeFittedStars()
const {
353 fittedStar->setFlux(0.0);
354 fittedStar->getMag() = 0.0;
360 MeasuredStarList &catalog = ccdImage->getCatalogForFit();
361 for (
auto &mi : catalog) {
362 auto fittedStar = mi->getFittedStar();
363 if (fittedStar ==
nullptr)
365 pex::exceptions::RuntimeError,
366 "All measuredStars must have a fittedStar: did you call selectFittedStars()?"));
367 auto point = toCommonTangentPlane->apply(*mi);
368 fittedStar->x += point.x;
369 fittedStar->y += point.y;
370 fittedStar->getFlux() += mi->getFlux();
375 auto measurementCount = fi->getMeasurementCount();
376 fi->x /= measurementCount;
377 fi->y /= measurementCount;
378 fi->getFlux() /= measurementCount;
383 void Associations::assignMags() {
385 MeasuredStarList &catalog = ccdImage->getCatalogForFit();
386 for (
auto const &mstar : catalog) {
388 if (!fstar)
continue;
399 "DeprojectFittedStars: Fitted stars are already in sidereal coordinates, nothing done ");
410 return item->getCatalogForFit().size() > 0;
417 if ((fittedStar !=
nullptr) & (fittedStar->getRefStar() !=
nullptr))
count++;
423 void Associations::collectMCStars(
int realization) {
429 string dbimdir = ccdImage.Dir();
430 string mctruth = dbimdir +
"/mc/mctruth.list";
432 if (realization >= 0) {
434 sstrm << dbimdir <<
"/mc/mctruth_" << realization <<
".list";
435 mctruth = sstrm.
str();
438 AstrometryTransformIdentity gti;
442 DicStarList mctruthlist(mctruth);
446 for (smI = starMatchList->begin(); smI != starMatchList->end(); smI++) {
447 StarMatch &sm = *smI;
448 BaseStar *bs = sm.s1;
449 MeasuredStar *mstar =
dynamic_cast<MeasuredStar *
>(bs);
451 DicStar *dstar =
dynamic_cast<DicStar *
>(bs);
453 mcstar->GetMCInfo().iflux = dstar->getval(
"iflux");
454 mcstar->GetMCInfo().tflux = dstar->getval(
"sflux");
461 LOGLS_FATAL(_log,
"CollectMCStars Unable to match MCTruth w/ catalog!");
466 double matchCutArcSec) {
468 size_t pos_minus = color.
find(
'-');
469 bool compute_diff = (pos_minus != string::npos);
471 c1 = color.
substr(0, pos_minus);
472 if (compute_diff) c2 = color.
substr(pos_minus + 1, string::npos);
473 DicStarList cList(dicStarListName);
474 if (!cList.HasKey(c1))
475 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
476 " misses a key named \"" + c1 +
"\""));
477 if (compute_diff && !cList.HasKey(c2))
478 throw(GastroException(
"Associations::SetFittedstarColors : " + dicStarListName +
479 " misses a key named \"" + c2 +
"\""));
483 AstrometryTransformIdentity
id;
486 AstrometryTransform *id_or_proj = &proj;
492 id_or_proj, matchCutArcSec / 3600);
495 <<
" FittedStars to color catalog");
497 for (
auto i = starMatchList->begin(); i != starMatchList->end(); ++i) {
498 BaseStar *s1 = i->s1;
499 FittedStar *
fs =
dynamic_cast<FittedStar *
>(s1);
500 BaseStar *s2 = i->s2;
501 const TStar *ts =
dynamic_cast<const TStar *
>(s2);
502 const DicStar *ds =
dynamic_cast<const DicStar *
>(ts->get_original());
503 fs->color = ds->getval(c1);
504 if (compute_diff)
fs->color -= ds->getval(c2);