Skip to content

Commit b79a88a

Browse files
authored
JP-3285: Allow negative angles in engineering format. Add support for angle units (#7683)
1 parent 68daafa commit b79a88a

File tree

3 files changed

+96
-19
lines changed

3 files changed

+96
-19
lines changed

CHANGES.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ ramp_fitting
5050
group, the timing for these ramps is not group time. These adjusted times
5151
are now used. [#7612, spacetelescope/stcal#173]
5252

53+
tweakreg
54+
--------
55+
56+
- Fixed a bug in the ``adjust_wcs`` *script* that was preventing passing
57+
negative angular arguments in the engineering format. Exposed ``adjust_wcs``
58+
function's docstring to be used in building ``jwst`` documentation. [#7683]
59+
60+
- Added support for units for angular arguments to both ``adjust_wcs`` script
61+
and function. [#7683]
62+
5363

5464
1.11.0 (2023-06-21)
5565
===================
@@ -83,7 +93,7 @@ cube_build
8393
----------
8494

8595
- Remove deleting the ``spaxel_dq`` array twice when using a weighting method of either msm or emsm. [#7586]
86-
96+
8797
- Updated to read wavelength range for NIRSpec IFU cubes from the cubepars reference file,
8898
instead of setting it based on the data. This makes use of new NIRSpec IFU cubepars reference
8999
files with wavelength arrays for the drizzle method. [#7559]
@@ -134,7 +144,7 @@ flat_field
134144
----------
135145

136146
- Added log messages for reporting flat reference file(s) used. [#7606]
137-
147+
138148
other
139149
-----
140150

@@ -216,7 +226,7 @@ tweakreg
216226
another. It is an analog of the ``tweakback`` task in the
217227
``drizzlepac``. [#7573, #7591]
218228

219-
- Added the 'GAIADR3' catalog to the available options for alignment;
229+
- Added the 'GAIADR3' catalog to the available options for alignment;
220230
this has been enabled as the default option [#7611].
221231

222232

jwst/scripts/adjust_wcs.py

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
4040
From command line::
4141
42-
% adjust_wcs [-h] (-u | --suffix SUFFIX | -f FILE) [--overwrite] [-w]
43-
[-r RA_DELTA] [-d DEC_DELTA] [-o ROLL_DELTA] [-s SCALE_FACTOR]
42+
% adjust_wcs [-h] (-u | --suffix SUFFIX | -f FILE) [--overwrite]
43+
[-r RA_DELTA [units]] [-d DEC_DELTA [units]]
44+
[-o ROLL_DELTA [units]] [-s SCALE_FACTOR]
4445
[-v] [input ...]
4546
4647
% adjust_wcs data_model_*_cal.fits -u -s 1.002
4748
48-
% adjust_wcs data_model_*_cal.fits --suffix wcsadj -s 1.002 -o 0.0023
49+
% adjust_wcs data_model_*_cal.fits --suffix wcsadj -s 1.002 -r 0.2 arcsec -o 0.0023 deg
4950
"""
5051
import argparse
5152
import glob
@@ -56,6 +57,10 @@
5657
import jwst
5758
from jwst.tweakreg.utils import adjust_wcs
5859
from jwst.assign_wcs.util import update_fits_wcsinfo
60+
from astropy import units
61+
62+
63+
_ANGLE_PARS = ["-r", "--ra_delta", "-d", "--dec_delta", "-o", "--roll_delta"]
5964

6065

6166
# Configure logging
@@ -80,6 +85,36 @@ def _replace_suffix(file, new_suffix):
8085
return new_file_name
8186

8287

88+
def _is_float(arg):
89+
try:
90+
float(arg)
91+
return True
92+
except ValueError:
93+
pass
94+
return False
95+
96+
97+
def _is_unit(arg):
98+
if arg.startswith("-"):
99+
return False
100+
try:
101+
units.Unit(arg)
102+
return True
103+
except ValueError:
104+
pass
105+
return False
106+
107+
108+
def angle(arg):
109+
args = arg.strip().split(' ')
110+
if len(args) == 1:
111+
return float(args[0])
112+
elif len(args) == 2:
113+
return units.Quantity(float(args[0]), units.Unit(args[1]), dtype=float)
114+
else:
115+
raise argparse.ArgumentTypeError()
116+
117+
83118
def main():
84119
if len(sys.argv) <= 1:
85120
raise ValueError("Missing required arguments.")
@@ -129,21 +164,21 @@ def main():
129164
group_wcs.add_argument(
130165
"-r",
131166
"--ra_delta",
132-
type=float,
167+
type=angle,
133168
default=0.0,
134169
help="Delta RA (longitude), degrees."
135170
)
136171
group_wcs.add_argument(
137172
"-d",
138173
"--dec_delta",
139-
type=float,
174+
type=angle,
140175
default=0.0,
141176
help="Delta DEC (latitude), degrees."
142177
)
143178
group_wcs.add_argument(
144179
"-o",
145180
"--roll_delta",
146-
type=float,
181+
type=angle,
147182
default=0.0,
148183
help="Delta roll angle, degrees."
149184
)
@@ -162,7 +197,25 @@ def main():
162197
version="v{0}".format(jwst.__version__)
163198
)
164199

165-
options = parser.parse_args()
200+
# pre-process argv for units and negative floats:
201+
argv = sys.argv[1:]
202+
argv_new = [os.path.basename(sys.argv[0])]
203+
while argv:
204+
argv_new.append(argv.pop(0))
205+
206+
# check whether next argument is a float:
207+
if (argv_new[-1] in _ANGLE_PARS and len(argv) >= 1 and
208+
_is_float(argv[0])):
209+
angle_value = argv.pop(0)
210+
# check whether next argument is a unit:
211+
if len(argv) >= 1 and _is_unit(argv[0]):
212+
angle_unit = argv.pop(0)
213+
argv_new.append(f" {angle_value} {angle_unit}")
214+
else:
215+
# assume angle units are degrees
216+
argv_new.append(f" {angle_value}")
217+
218+
options = parser.parse_args(argv_new)
166219

167220
files = []
168221
for f in options.arg0:

jwst/tweakreg/utils.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from copy import deepcopy
22

33
from astropy.modeling.rotations import RotationSequence3D
4+
from astropy import units
45
from gwcs.wcs import WCS
56
import numpy as np
67
from tweakwcs.correctors import JWSTWCSCorrector
@@ -14,6 +15,9 @@
1415
_RAD2ARCSEC = 3600.0 * np.rad2deg(1.0)
1516

1617

18+
__all__ = ["adjust_wcs", "transfer_wcs_correction"]
19+
20+
1721
def _wcsinfo_from_wcs_transform(wcs):
1822
frames = wcs.available_frames
1923
if 'v2v3' not in frames or 'world' not in frames or frames[-1] != 'world':
@@ -70,17 +74,17 @@ def adjust_wcs(wcs, delta_ra=0.0, delta_dec=0.0, delta_roll=0.0,
7074
WCS object to be adjusted. Must be an imaging JWST WCS of a calibrated
7175
data model.
7276
73-
delta_ra : float, optional
74-
Additional rotation (in degrees) to be applied along the longitude
75-
direction.
77+
delta_ra : float, astropy.units.Quantity, optional
78+
Additional rotation (in degrees if units not provided) to be applied
79+
along the longitude direction.
7680
77-
delta_dec : float, optional
78-
Additional rotation (in degrees) to be applied along the latitude
79-
direction.
81+
delta_dec : float, astropy.units.Quantity, optional
82+
Additional rotation (in degrees if units not provided) to be applied
83+
along the latitude direction.
8084
81-
delta_roll : float, optional
82-
Additional rotation (in degrees) to be applied to the telescope roll
83-
angle (rotation about V1 axis).
85+
delta_roll : float, astropy.units.Quantity, optional
86+
Additional rotation (in degrees if units not provided) to be applied
87+
to the telescope roll angle (rotation about V1 axis).
8488
8589
scale_factor : float, optional
8690
A multiplicative scale factor to be applied to the current scale
@@ -94,6 +98,16 @@ def adjust_wcs(wcs, delta_ra=0.0, delta_dec=0.0, delta_roll=0.0,
9498
wcs : `gwcs.WCS`
9599
Adjusted WCS object.
96100
"""
101+
# convert input angles to degrees:
102+
u_deg = units.Unit('deg')
103+
104+
if isinstance(delta_ra, units.Quantity):
105+
delta_ra = delta_ra.to(u_deg).value
106+
if isinstance(delta_dec, units.Quantity):
107+
delta_dec = delta_dec.to(u_deg).value
108+
if isinstance(delta_roll, units.Quantity):
109+
delta_roll = delta_roll.to(u_deg).value
110+
97111
# find the last frame in the pipeline that starts with 'v2v3':
98112
pipeline = deepcopy(wcs.pipeline)
99113
for step in pipeline[::-1]:

0 commit comments

Comments
 (0)