From 5c492ca5827bdc049554f833980a5fcac79534ac Mon Sep 17 00:00:00 2001 From: Mike Jarvis Date: Tue, 10 Nov 2026 19:00:44 -0500 Subject: [PATCH 1/2] Fix how the Roman PSF is rotated to the right sky orientation --- galsim/roman/roman_config.py | 2 + galsim/roman/roman_psfs.py | 91 +++++++++++++++++++++++------ tests/test_roman.py | 109 ++++++++++++++++++++++++++++++++--- 3 files changed, 175 insertions(+), 27 deletions(-) diff --git a/galsim/roman/roman_config.py b/galsim/roman/roman_config.py index 9b1e522f50..fb050d41b4 100644 --- a/galsim/roman/roman_config.py +++ b/galsim/roman/roman_config.py @@ -42,6 +42,8 @@ def _BuildRomanPSF(config, base, ignore, gsparams, logger): 'wavelength' : float, 'bandpass' : str, 'use_SCA_pos': bool, + 'boresight': CelestialCoord, + 'telescope_PA': Angle, } ignore += ['extra_aberrations'] diff --git a/galsim/roman/roman_psfs.py b/galsim/roman/roman_psfs.py index a374f64aba..b5e1a2acc8 100644 --- a/galsim/roman/roman_psfs.py +++ b/galsim/roman/roman_psfs.py @@ -19,16 +19,17 @@ import numpy as np import os -from . import pixel_scale, n_pix, pixel_scale_mm +from . import pixel_scale_mm from . import n_pix, n_sca, longwave_bands, shortwave_bands from . import diameter, obscuration from .roman_bandpass import getBandpass from ..utilities import LRU_Cache from ..position import PositionD -from ..errors import GalSimValueError, GalSimRangeError +from ..errors import GalSimValueError, GalSimRangeError, GalSimIncompatibleValuesError from ..bandpass import Bandpass -from ..wcs import PixelScale +from ..angle import degrees +from ..celestial import CelestialCoord from ..gsparams import GSParams from ..phase_psf import Aperture from .. import OpticalPSF, ChromaticOpticalPSF @@ -59,7 +60,8 @@ def getPSF(SCA, bandpass, SCA_pos=None, pupil_bin=4, wcs=None, n_waves=None, extra_aberrations=None, wavelength=None, gsparams=None, - logger=None, high_accuracy=None, approximate_struts=None): + logger=None, high_accuracy=None, approximate_struts=None, + boresight=None, telescope_PA=None): """Get a single PSF for Roman ST observations. The user must provide the SCA and bandpass; the latter is used when setting up the pupil @@ -99,11 +101,22 @@ def getPSF(SCA, bandpass, The PSF that is returned by default will be oriented with respect to the SCA coordinates, not world coordinates as is typical in GalSim. The pupil plane has a fixed orientation - with respect to the focal plane, so the PSF rotates with the telescope. To obtain a - PSF in world coordinates, which can be convolved with galaxies (that are normally described - in world coordinates), you may pass in a ``wcs`` parameter to this function. This will - project the PSF into world coordinates according to that WCS before returning it. Otherwise, - the return value is equivalent to using ``wcs=galim.PixelScale(galsim.roman.pixel_scale)``. + with respect to the focal plane, so the PSF rotates with the telescope. + + To orient the PSF on the sky, supply a ``wcs``. The local rotation and (if present) reflection + will be applied, but not any distortion component of the WCS. This means that the distortion + is correctly applied when drawing the PSF onto an image with the same WCS. + + For real observations, the rotation direction can be improved further by passing + ``boresight`` and ``telescope_PA``. The latter in particular sets the field rotation + of the Roman v3 axis at the boresight position. These values are available in Roman + ASDF data files in ``roman.meta.pointing`` as the values ``ra_v1``, ``dec_v1``, and + ``pa_v3``:: + + boresight = galsim.CelestialCoord(ra_v1 * galsim.degrees, dec_v1 * galsim.degrees) + psf = galsim.roman.getPSF( + SCA, bandpass, SCA_pos=image_pos, wcs=wcs, boresight=boresight, + telescope_PA=pa_v3 * galsim.degrees) The calculation takes advantage of the fact that the diffraction limit and aberrations have a simple, understood wavelength-dependence. (The Roman abberation data for Cycle 9 does in fact @@ -163,8 +176,11 @@ def getPSF(SCA, bandpass, the SCA is chosen. [default: None] pupil_bin: The binning to apply to the pupil plane image. (See discussion above.) [default: 4] - wcs: The WCS to use to project the PSF into world coordinates. - [default: galsim.PixelScale(galsim.roman.pixel_scale)] + wcs: The image WCS used to orient the PSF in world coordinates. With no + attitude information, only its local rotation and reflection are + used. With ``boresight`` and ``telescope_PA``, it must be celestial + and is used only to find the sky position at ``SCA_pos``. + [default: None, which retains the native pupil orientation] n_waves: Number of wavelengths to use for setting up interpolation of the chromatic PSF objects, which can lead to much faster image rendering. If None, then no interpolation is used. Note that @@ -187,6 +203,9 @@ def getPSF(SCA, bandpass, bandpass. [default: False] gsparams: An optional GSParams argument. See the docstring for GSParams for details. [default: None] + boresight: Telescope boresight as a `CelestialCoord`. [default: None] + telescope_PA: Position angle of the telescope V3 axis at ``boresight``, measured + from celestial north toward east, as a `galsim.Angle`. [default: None] Returns: A single PSF object (either a ChromaticOpticalPSF or an OpticalPSF depending on the @@ -259,18 +278,54 @@ def getPSF(SCA, bandpass, if not isinstance(wavelength, (Bandpass, float, type(None))): raise TypeError("wavelength should either be a Bandpass, float, or None.") + # Attitude information must include both boresight and telescope_PA. + if (boresight is None) != (telescope_PA is None): + raise GalSimIncompatibleValuesError( + "boresight and telescope_PA must be supplied together", + boresight=boresight, telescope_PA=telescope_PA) + # Now call _get_single_PSF(). psf = _get_single_PSF(SCA, bandpass, SCA_pos, pupil_bin, n_waves, extra_aberrations, wavelength, pupil_plane_type, gsparams) - # Apply WCS. - # The current version is in arcsec units, but oriented parallel to the image coordinates. - # So to apply the right WCS, project to pixels using the Roman mean pixel_scale, then - # project back to world coordinates with the provided wcs. - if wcs is not None: - scale = PixelScale(pixel_scale) - psf = wcs.toWorld(scale.toImage(psf), image_pos=SCA_pos) + if boresight is not None: + # The boresight direction is what Roman defines as the V1 axis. + # V3 is a fixed direction perpendicular to this. It is normal to the Solar Array + # Sun Shield. Also called OBS +Z in the spacecraft coordinates. + # The telescope_PA is defined as the rotation angle between +V3 and North, where the + # sign is defined as sweeping from North towards East to the +V3 axis. + # The raw PSF we have now is oriented so "up" is -60 degrees from the V3 direction. + # So we need to rotate the PSF by approximately (telescope_PA - 60) degrees. + # Approximately, because the direction towards north varies across the focal plane. + # That is the angle to use at the boresight. In general, we need the direction of the + # V3 axis and find the local rotation angle at the given source position. + + # Determine the V3 direction. We need it to be 90 degrees away from boresight and + # telescope_PA degrees rotated from local north. + # The Postel projection has this cool property that it preserves the angular distance + # from the origin, so if we set a point at 90 degrees from the origin in (u,v) at the + # correct angle from North (+v direction), then deprojecting with Postel will give us + # the CelestialCoord with the correct direction at a right angle to the boresight. + # Projected u increases towards west, so its eastward component has a minus sign. + sin_pa, cos_pa = telescope_PA.sincos() + v3 = boresight.deproject(-90*degrees*sin_pa, 90*degrees*cos_pa, projection='postel') + + # Now measure the direction of V3 relative to local north at the source to get the + # correct rotation at the source position. + sky_pos = wcs.toWorld(SCA_pos) + north = CelestialCoord(0*degrees, 90*degrees) + theta = sky_pos.angleBetween(north, v3) + psf = psf.rotate(theta - 60*degrees) + elif wcs is not None: + # Without an explicit rotation angle from telescope_PA, we can get a good approximation + # to the correct rotation and possible reflection from the WCS directly. We don't want + # to apply the distortion, since that's real and needs to get applied when drawing, but + # we can use the orthogonal part of the jacobian to orient the PSF nearly correctly. + _, _, theta, flip = wcs.jacobian(image_pos=SCA_pos).getDecomposition() + if flip: + psf = psf.transform(0, 1, 1, 0) + psf = psf.rotate(theta) return psf diff --git a/tests/test_roman.py b/tests/test_roman.py index 60b5a90c74..d51a913786 100644 --- a/tests/test_roman.py +++ b/tests/test_roman.py @@ -899,7 +899,7 @@ def test_roman_basic_numbers(): @timer def test_roman_psf_wcs(): - """Test drawing the PSF with a provided WCS.""" + """Test the wcs and optional orientation parameters for the PSF.""" # Make a PSF without giving a wcs image_pos = galsim.PositionD(153, 921) @@ -919,14 +919,80 @@ def test_roman_psf_wcs(): wcs_dict = galsim.roman.getWCS(PA=PA, world_pos=world_pos, date=date) wcs = wcs_dict[5] - # Get the PSF in real world coordinates with this wcs - psf = galsim.roman.getPSF(SCA=5, bandpass='F184', wavelength=1950., pupil_bin=8, - SCA_pos=image_pos, wcs=wcs) - # Draw on an image with this wcs. - im_wcs = psf.drawImage(bounds=im_scale.bounds, wcs=wcs, center=image_pos) - - np.testing.assert_allclose(im_wcs.array, im_scale.array) - + # With a wcs parameter, the PSF will be in sky coordinates. Only the rotation/reflection + # part of the WCS will be used to project to sky coords. + sky_psf = galsim.roman.getPSF(SCA=5, bandpass='F184', wavelength=1950., pupil_bin=8, + SCA_pos=image_pos, wcs=wcs) + # Extract the orthogonal factor using J = U diag(s) V^T. + # Removing the singular values leaves Q = U V^T as the orthogonal component of J. + jacobian = wcs.jacobian(image_pos=image_pos).getMatrix() + U, _, Vt = np.linalg.svd(jacobian) + Q = U @ Vt + expected = psf.transform(*Q.ravel()) + + # Drawing onto the detector applies the distortion in the WCS. + im_wcs = sky_psf.drawImage(bounds=im_scale.bounds, wcs=wcs, center=image_pos) + im_expected = expected.drawImage(bounds=im_scale.bounds, wcs=wcs, center=image_pos) + np.testing.assert_allclose(im_wcs.array, im_expected.array) + assert not np.allclose(im_wcs.array, im_scale.array) + + # Supplying boresight and telescope_PA provides a means to specify the telescope's + # orientation directly. At the boresight, the rotation is telescope_PA - 60 degrees. + # This is artificial: Roman's camera is off-axis, so the boresight cannot actually be + # observed on an SCA, but the known rotation makes it a useful test case. + boresight = galsim.CelestialCoord(123*galsim.degrees, -35*galsim.degrees) + telescope_PA = 42 * galsim.degrees + affine = galsim.AffineTransform(0.11, 0, 0, 0.11, origin=image_pos) + wcs = galsim.TanWCS(affine, boresight) + oriented = galsim.roman.getPSF(5, 'F184', wavelength=1950., pupil_bin=8, + SCA_pos=image_pos, wcs=wcs, + boresight=boresight, telescope_PA=telescope_PA) + expected = psf.rotate(-18*galsim.degrees).drawImage(scale=0.05) + image = oriented.drawImage(bounds=expected.bounds, scale=0.05) + np.testing.assert_allclose(image.array, expected.array, atol=1.e-8) + + # In a more realistic geometry, the WCS projection point is the SCA center, off-axis + # from the boresight. The rotation needs to be with respect to local north at the source + # position rather than at the boresight. + sca_center = galsim.PositionD(galsim.roman.n_pix/2, galsim.roman.n_pix/2) + sky_center = galsim.CelestialCoord(123*galsim.degrees, -35.7*galsim.degrees) + affine = galsim.AffineTransform(0.11, 0, 0, 0.11, origin=sca_center) + wcs = galsim.TanWCS(affine, sky_center) + sky_pos = galsim.CelestialCoord(122.98*galsim.degrees, -35.65*galsim.degrees) + image_pos = wcs.toImage(sky_pos) + psf = galsim.roman.getPSF(5, 'F184', wavelength=1950., pupil_bin=8, + SCA_pos=image_pos) + + # Independently construct V3 in Cartesian coordinates: at the boresight it is + # cos(PA)*north + sin(PA)*east, where north and east are tangent unit vectors. + # (This is intentionally a different method than we use in the code.) + sin_ra, cos_ra = boresight.ra.sincos() + sin_dec, cos_dec = boresight.dec.sincos() + north = np.array([-sin_dec*cos_ra, -sin_dec*sin_ra, cos_dec]) + east = np.array([-sin_ra, cos_ra, 0.0]) + sin_pa, cos_pa = telescope_PA.sincos() + v3 = cos_pa*north + sin_pa*east + + # V3's components along the source's east and north give its local position angle. + # Subtract the pupil's 60-degree offset to get the expected PSF rotation. + sin_ra, cos_ra = sky_pos.ra.sincos() + sin_dec, cos_dec = sky_pos.dec.sincos() + north = np.array([-sin_dec*cos_ra, -sin_dec*sin_ra, cos_dec]) + east = np.array([-sin_ra, cos_ra, 0.0]) + theta = np.arctan2(v3.dot(east), v3.dot(north))*galsim.radians + assert np.isclose(theta.deg, 42.013387) # I.e. not just 42 degrees. + expected = psf.rotate(theta-60*galsim.degrees).drawImage(bounds=expected.bounds, scale=0.05) + oriented = galsim.roman.getPSF(5, 'F184', wavelength=1950., pupil_bin=8, + SCA_pos=image_pos, wcs=wcs, + boresight=boresight, telescope_PA=telescope_PA) + image = oriented.drawImage(bounds=expected.bounds, scale=0.05) + np.testing.assert_allclose(image.array, expected.array, atol=1.e-8) + + # Need both boresight and telescope_PA if either is provided. + with assert_raises(galsim.GalSimIncompatibleValuesError): + galsim.roman.getPSF(5, 'F184', boresight=boresight) + with assert_raises(galsim.GalSimIncompatibleValuesError): + galsim.roman.getPSF(5, 'F184', telescope_PA=telescope_PA) @timer def test_config_psf(): @@ -993,6 +1059,31 @@ def test_config_psf(): print('psf2 = ',str(psf2)) assert psf1 == psf2 + # Include boresight and telescope_PA + image_pos = galsim.PositionD(153, 921) + boresight = galsim.CelestialCoord(123*galsim.degrees, -35*galsim.degrees) + wcs = galsim.TanWCS(galsim.AffineTransform(0.11, 0, 0, 0.11, origin=image_pos), boresight) + config = { + 'modules': ['galsim.roman'], + 'psf': { + 'type': 'RomanPSF', + 'SCA': 5, + 'bandpass': 'F184', + 'pupil_bin': 8, + 'wavelength': 1950., + 'use_SCA_pos': True, + 'boresight': boresight, + 'telescope_PA': '42 degrees', + }, + 'image_pos': image_pos, + 'wcs': wcs, + } + psf = galsim.config.BuildGSObject(config, 'psf')[0] + expected = galsim.roman.getPSF(5, 'F184', wavelength=1950., pupil_bin=8, + SCA_pos=image_pos, wcs=wcs, boresight=boresight, + telescope_PA=42*galsim.degrees) + assert psf == expected + @timer def test_config_sca(): From 0a1c43de0b5f894cb18dfa82e4f259dd65121906 Mon Sep 17 00:00:00 2001 From: Mike Jarvis Date: Tue, 10 Nov 2026 20:50:36 -0500 Subject: [PATCH 2/2] Add a test of wcs=PixelScale --- tests/test_roman.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_roman.py b/tests/test_roman.py index d51a913786..32e3d34874 100644 --- a/tests/test_roman.py +++ b/tests/test_roman.py @@ -936,6 +936,13 @@ def test_roman_psf_wcs(): np.testing.assert_allclose(im_wcs.array, im_expected.array) assert not np.allclose(im_wcs.array, im_scale.array) + # Supplying a PixelScale WCS is equivalent to the default case. + scale_wcs = galsim.PixelScale(0.11) + scale_psf = galsim.roman.getPSF(5, 'F184', wavelength=1950., pupil_bin=8, + SCA_pos=image_pos, wcs=scale_wcs) + im_scale2 = scale_psf.drawImage(scale=galsim.roman.pixel_scale, center=image_pos) + np.testing.assert_allclose(im_scale2.array, im_scale.array, atol=1.e-8) + # Supplying boresight and telescope_PA provides a means to specify the telescope's # orientation directly. At the boresight, the rotation is telescope_PA - 60 degrees. # This is artificial: Roman's camera is off-axis, so the boresight cannot actually be