Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions devel/roman/make_sip_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
# and/or other materials provided with the distribution.
#

"""Make a TAN-SIP WCS from the WCS spreadsheet supplied by the project.

Run from devel/roman with the xlsx spreadsheet in this directory.

Note that the spreadsheet uses FPA coordinates rather than the natural science coordinates
used for the final image. The science coordiates have the opposite direction for x and the
same direction for y.
"""

import pandas
import string
import numpy as np
Expand Down Expand Up @@ -92,6 +101,8 @@

# We now make a new sip file in the same format as the one we got from Jeff Kruk,
# including the same conventions for signs and such.
# However, we don't want the image coordinates in terms of FPA x,y. We want them to use
# the regular science coordinates, which has science x = -FPA x and science y = FPA y.

def extract_params(params):
cr0, cr1, cd00, cd01, cd10, cd11, *ab = params
Expand Down Expand Up @@ -147,10 +158,8 @@ def sip_resid(params, x, y, u, v):
u2 *= -180. / np.pi # deproject returns ra, which is in -u direction
v2 *= 180. / np.pi

diff_sq = (u2 - u)**2 + (v2 - v)**2
#print('diffsq = ',diff_sq[:20])
#print(type(diff_sq))
return diff_sq
# Return the array of residuals.
return np.concatenate((u2 - u, v2 - v))

for isca in range(num_sca):
nsca, xan, yan, fpa_x, fpa_y = sca_data[isca]
Expand Down Expand Up @@ -200,27 +209,23 @@ def sip_resid(params, x, y, u, v):
first_row = 11 + 225 * isca
last_row = 236 + 225 * isca # one past the end
center_row = 123 + 225 * isca
x = data['I'][first_row:last_row].astype(float) * 100
# The spreadsheet positions use FPA coordinates. To convert to science coordinates,
# multiply x by -1.
x = -data['I'][first_row:last_row].astype(float) * 100
y = data['J'][first_row:last_row].astype(float) * 100
u = data['E'][first_row:last_row].astype(float)
v = data['F'][first_row:last_row].astype(float)

# The x,y values in the spreadsheed are correct for SCAs 3,6,9,12,15,18, but
# the rest of them are rotated 180 degrees relative to what is there.
# See the image mapping_v210503.pdf that Chris made to show the correct coordinates.
# These x,y values are from -2048..2048, so -x,-y is 180 degree rotation.
if nsca % 3 != 0:
x = -x
y = -y

fpa_center = coord.CelestialCoord(0*coord.degrees, field_bias_deg*coord.degrees)

guess = np.zeros(30)
guess[0:2] = xan, yan - field_bias_deg
guess[2] = guess[5] = 0.11 / 3600.
guess[2] = 0.11 / 3600.
guess[5] = -0.11 / 3600.

# Rescale the input x,y to be order unity. We'll rescale the SIP coefficients
# after the fact to compensate.
# after the fact to compensate. Note: the 2048 here is a numerical normalization,
# not the science-image center. It's just a convenient power of 2 to use.
x /= 2048
y /= 2048

Expand All @@ -229,12 +234,11 @@ def sip_resid(params, x, y, u, v):
print(result)
assert result.success
resid = sip_resid(result.x, x, y, u, v)
print('Final diffsq = ', resid)
rms = np.sqrt(np.sum(resid)/len(resid))
print('Final residuals = ', resid)
rms = np.sqrt(np.sum(resid**2)/len(u))
print(isca,': rms = ',rms)
assert rms < 3.e-4 # This isn't a requirement, but says the rms error is < ~1 arcsec,
# which seems pretty reasonable given the likely accuracy of the
# angle measurements in the spread sheet.
# The fit should be good to better than 0.1 arcsec rms.
assert rms*3600 < 0.1

crval, cd, a, b = extract_params(result.x)

Expand All @@ -245,8 +249,10 @@ def sip_resid(params, x, y, u, v):
[2,3,4,0,0],
[3,4,0,0,0],
[4,0,0,0,0]])
a /= 2048**powers
b /= 2048**powers
# CD already contributes a factor of 1/2048. A degree-n SIP term therefore
# needs 2048**(1-n), so CD times the polynomial reproduces the fitted map.
a *= 2048.0**(1-powers)
b *= 2048.0**(1-powers)

# The fitted crval is pretty close to our initial guess, but might as well save the
# best fit value in our positions file, rather than do the calculation in code
Expand All @@ -263,10 +269,12 @@ def sip_resid(params, x, y, u, v):

with open(pos_output_file, 'w') as fout:
for isca in range(num_sca):
fout.write(("%3d" + 6*"\t%10.4f" + "\n")%(sca_data[isca][:7]))
fout.write(("%3d" + 2*"\t%10.5f" + 2*"\t%10.2f" + 2*"\t%10.6f" + "\n")
%(sca_data[isca][:7]))


with open(sip_output_file, 'w') as fout:
fout.write('# Generated by devel/roman/make_sip_file.py from the 20210204 spreadsheet.\n')
for isca in range(num_sca):
# Write A matrix along with C00, C01
_, _, _, _, _, _, _, cd, a, b = sca_data[isca]
Expand Down
14 changes: 10 additions & 4 deletions galsim/chromatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3507,6 +3507,8 @@ class ChromaticOpticalPSF(ChromaticObject):
optics approximation where the photon angles are derived from the
phase screen gradient. If False, then first draw using Fourier
optics and then shoot from the derived InterpolatedImage. [default: False]
interpolant: Interpolant instance or string specifying the real-space reconstruction
of the sampled PSF image, as for `OpticalPSF`. [default: None]
**kwargs: Any other keyword arguments to be passed to OpticalPSF, for example,
related to struts, obscuration, oversampling, etc. See OpticalPSF
docstring for a complete list of options.
Expand All @@ -3516,7 +3518,7 @@ class ChromaticOpticalPSF(ChromaticObject):
_single_params = [ {'diam' : float, 'lam_over_diam' : float} ]

def __init__(self, lam, diam=None, lam_over_diam=None, aberrations=None,
scale_unit=None, gsparams=None, **kwargs):
scale_unit=None, gsparams=None, interpolant=None, **kwargs):
# First, take the basic info.
if scale_unit is None:
scale_unit = arcsec
Expand Down Expand Up @@ -3558,6 +3560,7 @@ def __init__(self, lam, diam=None, lam_over_diam=None, aberrations=None,
raise GalSimValueError("Invalid fft_sign", self.fft_sign, allowed_values=['+','-'])

self.geometric_shooting = kwargs.pop('geometric_shooting', False)
self.interpolant = interpolant

# All kwargs left should be relevant for the Aperture.
# If the pupil plane image is given, then we can make the aperture now to use at all
Expand Down Expand Up @@ -3613,13 +3616,14 @@ def __eq__(self, other):
self.gsparams == other.gsparams and
self.fft_sign == other.fft_sign and
self.geometric_shooting == other.geometric_shooting and
self.interpolant == other.interpolant and
self._aper == other._aper and
self._kwargs == other._kwargs))

def __hash__(self):
return hash(("galsim.ChromaticOpticalPSF", self.lam, self.lam_over_diam,
tuple(self.aberrations), self.scale_unit, self.gsparams,
self.fft_sign, self.geometric_shooting, self._aper,
self.fft_sign, self.geometric_shooting, self.interpolant, self._aper,
frozenset(self._kwargs.items())))

def __repr__(self):
Expand All @@ -3631,6 +3635,8 @@ def __repr__(self):
s += ', fft_sign="-"'
if self.geometric_shooting:
s += ', geometric_shooting=True'
if self.interpolant is not None:
s += ', interpolant=%r'%self.interpolant
if self._aper is not None:
s += ', aper=%r'%self._aper
else:
Expand Down Expand Up @@ -3678,12 +3684,12 @@ def evaluateAtWavelength(self, wave):
lam=wave, diam=self.diam,
aberrations=self.aberrations*wave_factor, scale_unit=self.scale_unit,
_force_stepk=self._stepk*wave_factor, _force_maxk=self._maxk*wave_factor,
gsparams=self.gsparams, aper=aper)
gsparams=self.gsparams, aper=aper, interpolant=self.interpolant)
else:
ret = OpticalPSF(
lam=wave, diam=self.diam,
aberrations=self.aberrations*wave_factor, scale_unit=self.scale_unit,
gsparams=self.gsparams, aper=aper)
gsparams=self.gsparams, aper=aper, interpolant=self.interpolant)
self._stepk = ret.stepk / wave_factor
self._maxk = ret.maxk / wave_factor
return ret
Expand Down
30 changes: 12 additions & 18 deletions galsim/image.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for these changes - I thought when I was using the functions last time that I would take all the meta information with it, didn't realize that wasn't explicitly happening

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a reasonable assumption. Which is why I changed the default. In my review it didn't occur to me that it wouldn't keep the pixel scale. That's a sign of bad API design.

Original file line number Diff line number Diff line change
Expand Up @@ -1863,53 +1863,47 @@ def __ior__(self, other):
def transpose(self):
"""Return the tranpose of the image.

Note: The returned image will have an undefined wcs.
If you care about the wcs, you will need to set it yourself.
Note: The returned image will keep the original wcs.
"""
bT = _BoundsI(self.ymin, self.ymax, self.xmin, self.xmax)
return _Image(self.array.T, bT, None)
return _Image(self.array.T, bT, wcs=self.wcs)

def flip_lr(self):
"""Return a version of the image flipped left to right.

Note: The returned image will have an undefined wcs.
If you care about the wcs, you will need to set it yourself.
Note: The returned image will keep the original wcs.
"""
return _Image(self.array[:,::-1], self._bounds, None)
return _Image(self.array[:,::-1], self._bounds, wcs=self.wcs)

def flip_ud(self):
"""Return a version of the image flipped top to bottom.

Note: The returned image will have an undefined wcs.
If you care about the wcs, you will need to set it yourself.
Note: The returned image will keep the original wcs.
"""
return _Image(self.array[::-1,:], self._bounds, None)
return _Image(self.array[::-1,:], self._bounds, wcs=self.wcs)

def rot_cw(self):
"""Return a version of the image rotated 90 degrees clockwise.

Note: The returned image will have an undefined wcs.
If you care about the wcs, you will need to set it yourself.
Note: The returned image will keep the original wcs.
"""
bT = _BoundsI(self.ymin, self.ymax, self.xmin, self.xmax)
return _Image(self.array.T[::-1,:], bT, None)
return _Image(self.array.T[::-1,:], bT, wcs=self.wcs)

def rot_ccw(self):
"""Return a version of the image rotated 90 degrees counter-clockwise.

Note: The returned image will have an undefined wcs.
If you care about the wcs, you will need to set it yourself.
Note: The returned image will keep the original wcs.
"""
bT = _BoundsI(self.ymin, self.ymax, self.xmin, self.xmax)
return _Image(self.array.T[:,::-1], bT, None)
return _Image(self.array.T[:,::-1], bT, wcs=self.wcs)

def rot_180(self):
"""Return a version of the image rotated 180 degrees.

Note: The returned image will have an undefined wcs.
If you care about the wcs, you will need to set it yourself.
Note: The returned image will keep the original wcs.
"""
return _Image(self.array[::-1,::-1], self._bounds, None)
return _Image(self.array[::-1,::-1], self._bounds, wcs=self.wcs)

def depixelize(self, x_interpolant):
"""Return a depixelized version of the image.
Expand Down
3 changes: 3 additions & 0 deletions galsim/roman/roman_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def _BuildRomanPSF(config, base, ignore, gsparams, logger):
'wavelength' : float,
'bandpass' : str,
'use_SCA_pos': bool,
'boresight': CelestialCoord,
'telescope_PA': Angle,
'interpolant': str,
}
ignore += ['extra_aberrations']

Expand Down
Loading
Loading