Skip to content

Commit

Permalink
improve gmu.return_requested_units() to allow any input units
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-simpson committed Jan 10, 2025
1 parent 19485aa commit 932ea0c
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 57 deletions.
8 changes: 4 additions & 4 deletions gemini_instruments/bhros/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ def _tag_instrument(self):
return TagSet({'BHROS', 'SPECT'}, ())

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units(input_units="AA")
def central_wavelength(self):
"""
Returns the central wavelength in nm
Returns the central wavelength
Returns
-------
float
The central wavelength setting in nm
The central wavelength setting
"""
# The central_wavelength keyword is in Angstroms
keyword = self._keyword_for('central_wavelength')
wave_in_angstroms = self.phu.get(keyword, -1)
if wave_in_angstroms < 0:
return None
return wave_in_angstroms * 0.1
return wave_in_angstroms

@astro_data_descriptor
def dec(self):
Expand Down
4 changes: 2 additions & 2 deletions gemini_instruments/f2/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def camera(self, stripID=False, pretty=False):
return self._may_remove_component(camera, stripID, pretty)

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def central_wavelength(self):
"""
Returns the central wavelength in nm
Expand Down Expand Up @@ -302,7 +302,7 @@ def detector_y_offset(self):
return -offset if self.phu.get('INPORT') == 1 else offset

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def dispersion(self):
"""
Returns the dispersion in nm per pixel as a list (one value per
Expand Down
6 changes: 3 additions & 3 deletions gemini_instruments/flamingos/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ def _tag_dark(self):
return TagSet(['DARK', 'CAL'])

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def central_wavelength(self):
"""
Returns the central wavelength in nm
Returns the central wavelength
Returns
-------
float
The central wavelength setting in nm
The central wavelength setting
"""
return 1500.0

Expand Down
13 changes: 7 additions & 6 deletions gemini_instruments/gemini/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,23 +610,23 @@ def cass_rotator_pa(self):
return crpa if abs(crpa) <= 360 else None

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units(input_units="um")
def central_wavelength(self):
"""
Returns the central wavelength in nm or the specified units
Returns the central wavelength
Returns
-------
float
The central wavelength setting in nm
The central wavelength setting
"""
# We assume that the central_wavelength keyword is in microns
keyword = self._keyword_for('central_wavelength')
wave_in_microns = float(self.phu.get(keyword, -1))

if wave_in_microns < 0:
return None
return 1000 * wave_in_microns
return wave_in_microns

@astro_data_descriptor
def coadds(self):
Expand Down Expand Up @@ -870,7 +870,7 @@ def disperser(self, stripID=False, pretty=False):
stripID, pretty)

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units(input_units="m")
def dispersion(self):
"""
Returns the dispersion in nm per pixel as a list (one value per
Expand All @@ -883,6 +883,7 @@ def dispersion(self):
The dispersion(s) in nm
"""
keyword = self._keyword_for('dispersion')
print(keyword)
if keyword in self.hdr:
dispersion = self.hdr[keyword]
elif self._keyword_for('dispersion') in self.phu:
Expand All @@ -892,7 +893,7 @@ def dispersion(self):
else:
return None

return 1e-9 * dispersion
return dispersion

@astro_data_descriptor
def dispersion_axis(self):
Expand Down
2 changes: 1 addition & 1 deletion gemini_instruments/ghost/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def calibration_key(self):

# FIXME Remove once headers corrected
@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def central_wavelength(self): # pragma: no cover
"""
Dummy to work around current Gemini cal_mgr
Expand Down
6 changes: 3 additions & 3 deletions gemini_instruments/gmos/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def array_name(self):
return self.hdr.get('AMPNAME')

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def central_wavelength(self, pretty=False):
"""
Returns the central wavelength in nm
Expand Down Expand Up @@ -430,7 +430,7 @@ def disperser(self, stripID=False, pretty=False):
return disperser

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def dispersion(self):
"""
Returns the dispersion in nm per binned pixel as a list (one value per
Expand Down Expand Up @@ -474,7 +474,7 @@ def dispersion(self):

if dispersion is not None:
grating_order = self.phu.get('GRORDER', 1)
dispersion = 1e-9 * dispersion / grating_order
dispersion = 1e9 * dispersion / grating_order

if not self.is_single:
dispersion = [dispersion] * len(self)
Expand Down
56 changes: 29 additions & 27 deletions gemini_instruments/gmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,38 +106,40 @@ def convert_units(input_units, input_value, output_units):
return input_value * factor


def return_requested_units(fn):
def return_requested_units(input_units='nm'):
"""
Decorator that replaces the repeated code for asMicrometers,
asNanometers, asAngstroms. Should be replaced by a "units='nm'"
parameter, but time is limited. Keeping current coding to avoid
"""
@wraps(fn)
def gn(instance, asMicrometers=False, asNanometers=False, asAngstroms=False,
**kwargs):
unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
output_units = u.m # By default
if unit_arg_list.count(True) == 1:
# Just one of the unit arguments was set to True. Return the
# central wavelength in these units
if asMicrometers:
output_units = u.um
elif asNanometers:
output_units = u.nm
else:
output_units = u.AA

# Ensure we return a list, not an array
# nm are the "standard" DRAGONS wavelength unit
retval = fn(instance, **kwargs)
if retval is None:
return retval
if isinstance(retval, list):
return [None if v is None else (v * u.nm).to(output_units).value
for v in retval]
return (fn(instance) * u.nm).to(output_units).value

return gn
def inner_decorator(fn):
@wraps(fn)
def gn(instance, asMicrometers=False, asNanometers=False, asAngstroms=False,
**kwargs):
unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
output_units = u.m # By default
if unit_arg_list.count(True) == 1:
# Just one of the unit arguments was set to True. Return the
# central wavelength in these units
if asMicrometers:
output_units = u.um
elif asNanometers:
output_units = u.nm
else:
output_units = u.AA

# Ensure we return a list, not an array
# nm are the "standard" DRAGONS wavelength unit
retval = fn(instance, **kwargs)
print("RETVAL", retval)
if retval is None:
return retval
if isinstance(retval, list):
return [None if v is None else (v * u.Unit(input_units)).to(output_units).value
for v in retval]
return (retval * u.Unit(input_units)).to(output_units).value
return gn
return inner_decorator


def toicrs(frame, ra, dec, equinox=2000.0, ut_datetime=None):
Expand Down
4 changes: 2 additions & 2 deletions gemini_instruments/gnirs/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def array_name(self):
return self.phu.get(self._keyword_for('array_name'))

@astro_data_descriptor
@gmu.return_requested_units
def dispersion(self,):
@gmu.return_requested_units()
def dispersion(self):
"""
Returns the dispersion in nm per pixel as a list (one value per
extension) or a float if used on a single-extension slice. It is
Expand Down
4 changes: 2 additions & 2 deletions gemini_instruments/graces/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def _tag_bias(self):
return TagSet(['BIAS', 'CAL'])

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def central_wavelength(self):
"""
Returns the central wavelength in nm
Returns the central wavelength
Returns
-------
Expand Down
4 changes: 2 additions & 2 deletions gemini_instruments/niri/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def array_section(self, pretty=False):
return build_ir_section(self, pretty)

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def central_wavelength(self):
"""
Returns the central wavelength in nm
Expand Down Expand Up @@ -251,7 +251,7 @@ def disperser(self, stripID=False, pretty=False):
return 'MIRROR'

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units()
def dispersion(self):
"""
Returns the dispersion in nm per pixel as a list (one value per
Expand Down
2 changes: 1 addition & 1 deletion gemini_instruments/test/lut_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@
('detector_x_bin', 1),
('detector_y_bin', 1),
('disperser', 'MIRROR'),
('dispersion', [-3.88e-10]),
('dispersion', [-3.8800000000000006e-10]),
('dispersion_axis', [2]),
('effective_wavelength', 1.25e-06),
('elevation', 60.1851833333),
Expand Down
8 changes: 4 additions & 4 deletions gemini_instruments/trecs/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _type_mode(self):
return TagSet(['SPECT'])

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units(input_units="um")
def central_wavelength(self):
"""
Returns the central wavelength in microns
Expand All @@ -49,7 +49,7 @@ def central_wavelength(self):
wave_in_microns = self.phu.get('HRCENWL')
else:
return None
return wave_in_microns * 1000
return wave_in_microns

@astro_data_descriptor
def detector_x_offset(self):
Expand Down Expand Up @@ -84,7 +84,7 @@ def detector_y_offset(self):
return None

@astro_data_descriptor
@gmu.return_requested_units
@gmu.return_requested_units(input_units="um")
def dispersion(self):
"""
Returns the dispersion in microns per pixel as a list (one value per
Expand All @@ -105,7 +105,7 @@ def dispersion(self):
dispersion = 0.0019
else:
return None
return dispersion * 1000
return dispersion

@returns_list
@astro_data_descriptor
Expand Down

0 comments on commit 932ea0c

Please sign in to comment.