Created
November 28, 2026 16:41
-
-
Save johnarban/3c66598763ee71d2cf05dbdcf7e44cd2 to your computer and use it in GitHub Desktop.
Rotate / Flip a WCS to match an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Build the target WCS header, rotate/flip it to match the rendered TIFF, | |
| and write a .wcs file for toasty (toasty tile-study --fits-wcs ...). | |
| Pixel convention: y counts rows from the TOP of the image (PNG/TIFF, and how | |
| toasty reads the WCS), so "cw"/"ccw"/"flip_h"/"flip_v" mean what you see on | |
| screen. Pixels are 1-based (FITS). | |
| Each operation only swaps/negates PC columns and moves CRPIX: | |
| op CRPIX1' CRPIX2' PCi_1' PCi_2' NAXIS1/2 | |
| flip_h nx+1-CRPIX1 CRPIX2 -PCi_1 PCi_2 same | |
| flip_v CRPIX1 ny+1-CRPIX2 PCi_1 -PCi_2 same | |
| rot180 nx+1-CRPIX1 ny+1-CRPIX2 -PCi_1 -PCi_2 same | |
| ccw90 CRPIX2 nx+1-CRPIX1 PCi_2 -PCi_1 swapped | |
| cw90 ny+1-CRPIX2 CRPIX1 -PCi_2 PCi_1 swapped | |
| """ | |
| from astropy.io.fits import Header | |
| import tifffile | |
| TIFF = "./target.tif" | |
| OUT_FULL = "target_wcs.wcs" | |
| def base_header(): | |
| hdr = Header() | |
| hdr['SIMPLE'] = True | |
| hdr['BITPIX'] = -32 | |
| hdr['NAXIS'] = 2 | |
| hdr['NAXIS1'] = 28406 | |
| hdr['NAXIS2'] = 45680 | |
| hdr['WCSAXES'] = 2 | |
| hdr['CRPIX1'] = 15136 | |
| hdr['CRPIX2'] = 35343 | |
| hdr['PC1_1'] = 00000000000 | |
| hdr['PC1_2'] = 00000000000 | |
| hdr['PC2_1'] = 00000000000 | |
| hdr['PC2_2'] = 00000000000 | |
| hdr['CDELT1'] = 1.0 | |
| hdr['CDELT2'] = 1.0 | |
| hdr['CUNIT1'] = 'deg' | |
| hdr['CUNIT2'] = 'deg' | |
| hdr['CTYPE1'] = 'RA---TAN' | |
| hdr['CTYPE2'] = 'DEC--TAN' | |
| hdr['CRVAL1'] = 360.00 | |
| hdr['CRVAL2'] = -90.00 | |
| hdr['LONPOLE'] = 180.0 | |
| hdr['LATPOLE'] = -90.00 | |
| hdr['RADESYS'] = 'ICRS' | |
| hdr['BUNIT'] = 'MJy/sr' | |
| hdr['MOSNX'] = 28406 # original mosaic size (not updated by rotations) | |
| hdr['MOSNY'] = 45680 | |
| hdr['NCOMBINE'] = 18 | |
| return hdr | |
| def apply_op(hdr, op): | |
| """Return a copy of hdr with one rotation/flip applied (see table above).""" | |
| h = hdr.copy() # copy keeps card order: SIMPLE, BITPIX, NAXIS... first | |
| nx, ny = hdr['NAXIS1'], hdr['NAXIS2'] | |
| c1, c2 = hdr['CRPIX1'], hdr['CRPIX2'] | |
| p11, p12 = hdr['PC1_1'], hdr['PC1_2'] | |
| p21, p22 = hdr['PC2_1'], hdr['PC2_2'] | |
| if op == "flip_h": | |
| h['CRPIX1'] = nx + 1 - c1 | |
| h['PC1_1'], h['PC2_1'] = -p11, -p21 | |
| elif op == "flip_v": | |
| h['CRPIX2'] = ny + 1 - c2 | |
| h['PC1_2'], h['PC2_2'] = -p12, -p22 | |
| elif op == "rot180": | |
| h['CRPIX1'], h['CRPIX2'] = nx + 1 - c1, ny + 1 - c2 | |
| h['PC1_1'], h['PC2_1'] = -p11, -p21 | |
| h['PC1_2'], h['PC2_2'] = -p12, -p22 | |
| elif op == "ccw90": | |
| h['NAXIS1'], h['NAXIS2'] = ny, nx | |
| h['CRPIX1'], h['CRPIX2'] = c2, nx + 1 - c1 | |
| h['PC1_1'], h['PC2_1'] = p12, p22 # new column 1 = old column 2 | |
| h['PC1_2'], h['PC2_2'] = -p11, -p21 # new column 2 = -old column 1 | |
| elif op == "cw90": | |
| h['NAXIS1'], h['NAXIS2'] = ny, nx | |
| h['CRPIX1'], h['CRPIX2'] = ny + 1 - c2, c1 | |
| h['PC1_1'], h['PC2_1'] = -p12, -p22 # new column 1 = -old column 2 | |
| h['PC1_2'], h['PC2_2'] = p11, p21 # new column 2 = old column 1 | |
| else: | |
| raise ValueError(f"unknown op {op!r}") | |
| return h | |
| def transform_header(hdr, ops): | |
| for op in ops: | |
| hdr = apply_op(hdr, op) | |
| return hdr | |
| with tifffile.TiffFile(TIFF) as tif: | |
| height, width = tif.pages[0].shape[:2] | |
| print(f"TIFF: width {width}, height {height}") | |
| # ccw90 will rotate the WCS to match the TIFF orientation (WCS was portrait, TIFF is landscape) | |
| # and then we found it needt to be flipped along vertically, along the long axis | |
| operations = ["ccw90", "flip_v"] | |
| hdr_rotated = transform_header(base_header(), operations) | |
| if (hdr_rotated['NAXIS1'], hdr_rotated['NAXIS2']) != (width, height): | |
| raise SystemExit( | |
| f"rotated header is {hdr_rotated['NAXIS1']}x{hdr_rotated['NAXIS2']}, " | |
| f"TIFF is {width}x{height}: check operations" | |
| ) | |
| hdr_rotated.tofile(OUT_FULL, overwrite=True) | |
| print(f"wrote {OUT_FULL}") | |
| print(repr(hdr_rotated)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment