Skip to content

Fix MOSAIC_TYPE SAME_CRVAL for multi-detector mosaics - #38

Closed
smeingast wants to merge 1 commit into
astromatic:masterfrom
smeingast:fix/same-crval-mosaic
Closed

smeingast wants to merge 1 commit into
astromatic:masterfrom
smeingast:fix/same-crval-mosaic

Conversation

@smeingast

@smeingast smeingast commented Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • mosaic.c: MOSAIC_TYPE SAME_CRVAL overwrote all detectors' CRVALs with the field mean without adjusting CRPIX or CD. This collapsed all detectors to the same sky position, breaking the virtual-image matching for multi-detector instruments (e.g. VIRCAM). Now uses crval_to_crpix() like the other mosaic types, preserving each detector's sky footprint while sharing a common tangent point.
  • astrsolve.c: In the loop computing findex2 adjustments per instrument (line 275), the code used a stale instru variable from the previous loop instead of the current loop variable i. Harmless for single-instrument setups but would corrupt the normal equation matrix for multi-instrument configurations.

Test results (VIRCAM, 16 detectors, 30 exposures)

MOSAIC_TYPE Internal dAXIS1/2 (before) Internal dAXIS1/2 (after)
UNCHANGED 0.284" / 0.281" (not affected)
SAME_CRVAL 1.46" / 1.45" 0.018" / 0.023"
FIX_FOCALPLANE 0.091" / 0.085" (not affected)
LOOSE 0.041" / 0.044" (not affected)

All modes now produce equivalent results with STABILITY_TYPE INSTRUMENT.

Test plan

  • Verified SAME_CRVAL with VIRCAM 16-detector mosaic data (30 exposures, ZEA output projection, Gaia-EDR3 reference)
  • Verified FIX_FOCALPLANE and LOOSE modes are unaffected
  • Verified single-instrument astrsolve indexing is unchanged
  • Multi-instrument astrsolve fix not tested (no multi-instrument data available)

…variable in astrsolve

Two bugs fixed:

1. mosaic.c: SAME_CRVAL overwrote all detectors' CRVALs with the field
   mean without adjusting CRPIX or CD. This collapsed all detectors to
   the same sky position, breaking the virtual-image matching step for
   multi-detector instruments. Now uses crval_to_crpix() like the other
   mosaic types, which preserves each detector's sky footprint while
   sharing a common tangent point.

   Tested with VIRCAM 16-detector data (30 exposures): internal
   astrometric residuals went from 1.46" (broken) to 0.018" (fixed),
   matching the performance of LOOSE and FIX_FOCALPLANE modes.

2. astrsolve.c: In the loop computing findex2 adjustments per
   instrument, the code used a stale `instru` variable (set by the
   previous loop's last field) instead of the loop variable `i`. This is
   harmless for single-instrument setups but would corrupt the normal
   equation matrix dimensions for multi-instrument configurations.

@ebertin ebertin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR! You will find my comments inline.

Comment thread src/mosaic.c
Comment on lines -113 to +114
if (prefs.mosaic_type[field->astromlabel] == MOSAIC_SAMECRVAL)
for (s=0; s<field->nset; s++)
{
for (d=0; d<naxis; d++)
sets[s]->wcs->crval[d] = wcsmean[d];
init_wcs(sets[s]->wcs);
range_wcs(sets[s]->wcs);
invert_wcs(sets[s]->wcs);
}
else
for (s=0; s<field->nset; s++)
crval_to_crpix(sets[s]->wcs, wcsmean);
for (s=0; s<field->nset; s++)
crval_to_crpix(sets[s]->wcs, wcsmean);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you misunderstood the goal of the MOSAIC_TYPE SAME_CRVAL option, which is to bring the CRVALs of all extensions to a common set of coordinates without compensating the possible shifts using the CDs and CRPIXs. It is generally used in conjunction with a .ahead file defining the focal plane of a mosaic instrument (where the CRPIXs and CDs are known to be correct), when the original CRVALs have been re-adjusted individually (common among simple astrometry calibration pipelines). What you actually want is what the MOSAIC_TYPE SHARE_PROJAXIS option already provides (the "else" code path here). So, in short, no code change is required here.

Comment thread src/astrsolve.c
for (i=0; i<ninstru; i++)
{
if (prefs.stability_type[instru]!=STABILITY_PREDISTORTED)
if (prefs.stability_type[i]!=STABILITY_PREDISTORTED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

smeingast added a commit to smeingast/scamp that referenced this pull request Apr 9, 2026
In astrsolve_fgroups() the loop at src/astrsolve.c:273 iterates `i` from
0 to ninstru, but the stability_type[] check inside used the variable
`instru` (set at line 193 to field->astromlabel from the previous loop)
instead of the current loop variable `i`. For single-instrument setups
the two indices match and this has no effect. For multi-instrument
configurations with mixed STABILITY_TYPE values per instrument, the
check would consult the wrong instrument and potentially mis-size the
normal equation matrix.

Fix: use `i` as the index into prefs.stability_type.

Split out from astromatic#38; the mosaic.c portion of that PR is being dropped
(SHARE_PROJAXIS already provided the intended behaviour).
@smeingast

Copy link
Copy Markdown
Contributor Author

Thank you for the detailed review @ebertin. You're right on both counts:

  • On mosaic.c: I misread the purpose of MOSAIC_TYPE SAME_CRVAL. I re-ran the test on an unmodified build with MOSAIC_TYPE SHARE_PROJAXIS and confirmed it produces bit-identical residuals to my "patched" SAME_CRVAL on VIRCAM data, so the existing SHARE_PROJAXIS option was what I wanted all along. No code change is needed here.
  • On astrsolve.c: thanks for confirming. I've split that one-line fix into a dedicated PR: Fix wrong loop index in astrsolve.c stability_type check #39.

Closing this PR in favour of #39.

@smeingast smeingast closed this Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants