Skip to content

Commit

Permalink
replace repeated code with gmu.return_requested_units() descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-simpson committed Jan 9, 2025
1 parent a9c483d commit 7fddb56
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 504 deletions.
36 changes: 5 additions & 31 deletions gemini_instruments/bhros/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,22 @@ def _tag_instrument(self):
return TagSet({'BHROS', 'SPECT'}, ())

@astro_data_descriptor
def central_wavelength(self, asMicrometers=False, asNanometers=False,
asAngstroms=False):
@gmu.return_requested_units
def central_wavelength(self):
"""
Returns the central wavelength in meters or the specified units
Parameters
----------
asMicrometers : bool
If True, return the wavelength in microns
asNanometers : bool
If True, return the wavelength in nanometers
asAngstroms : bool
If True, return the wavelength in Angstroms
Returns the central wavelength in microns
Returns
-------
float
The central wavelength setting
The central wavelength setting in nm
"""
unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
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 = "micrometers"
if asNanometers:
output_units = "nanometers"
if asAngstroms:
output_units = "angstroms"
else:
# Either none of the unit arguments were set to True or more than
# one of the unit arguments was set to True. In either case,
# return the central wavelength in the default units of meters.
output_units = "meters"

# 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 gmu.convert_units('angstroms', wave_in_angstroms,
output_units)
return wave_in_angstroms * 0.1

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

@astro_data_descriptor
def central_wavelength(self, asMicrometers=False, asNanometers=False,
asAngstroms=False):
@gmu.return_requested_units
def central_wavelength(self):
"""
Returns the central wavelength in meters or the specified units
For F2, central wavelength is specified for the middle of the
grism + filter transmission window, not for the central row.
Parameters
----------
asMicrometers : bool
If True, return the wavelength in microns
asNanometers : bool
If True, return the wavelength in nanometers
asAngstroms : bool
If True, return the wavelength in Angstroms
Returns the central wavelength in microns
Returns
-------
float
The central wavelength setting
"""
unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
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 = "micrometers"
if asNanometers:
output_units = "nanometers"
if asAngstroms:
output_units = "angstroms"
else:
# Either none of the unit arguments were set to True or more than
# one of the unit arguments was set to True. In either case,
# return the central wavelength in the default units of meters.
output_units = "meters"

central_wavelength = float(self.phu['WAVELENG'])
central_wavelength = float(self.phu['WAVELENG']) * 0.1

filter = self.filter_name(keepID=True)
# Header value for this filter in early data is incorrect:
if filter == 'K-long_G0812':
central_wavelength = 22000
central_wavelength = 2200.0
# The new JH_G0816 and HK_G0817 filters were installed in 2022, but their
# WAVELENG header keywords weren't simultaneously updated, thus the correction.
if filter == "JH_G0816":
central_wavelength = 13385
central_wavelength = 1338.5
if filter == "HK_G0817":
central_wavelength = 19000
central_wavelength = 1900.0

if central_wavelength < 0.0:
return None

else:
return gmu.convert_units('angstroms', central_wavelength,
output_units)
return central_wavelength

@astro_data_descriptor
def data_section(self, pretty=False):
Expand Down Expand Up @@ -333,21 +302,13 @@ def detector_y_offset(self):
return -offset if self.phu.get('INPORT') == 1 else offset

@astro_data_descriptor
def dispersion(self, asMicrometers=False, asNanometers=False, asAngstroms=False):
@gmu.return_requested_units
def dispersion(self):
"""
Returns the dispersion in meters per pixel as a list (one value per
Returns the dispersion in microns per pixel as a list (one value per
extension) or a float if used on a single-extension slice. It is
possible to control the units of wavelength using the input arguments.
Parameters
----------
asMicrometers : bool
If True, return the wavelength in microns
asNanometers : bool
If True, return the wavelength in nanometers
asAngstroms : bool
If True, return the wavelength in Angstroms
Returns
-------
list/float
Expand All @@ -359,22 +320,7 @@ def dispersion(self, asMicrometers=False, asNanometers=False, asAngstroms=False)
mask = dispersion_offset_mask.get(config, None)
dispersion = float(mask.dispersion if mask else None)

unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
output_units = "meters" # 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 = "micrometers"
if asNanometers:
output_units = "nanometers"
if asAngstroms:
output_units = "angstroms"

if dispersion is not None:
dispersion = gmu.convert_units('nanometers', dispersion, output_units)

if not self.is_single:
if dispersion is not None and not self.is_single:
dispersion = [dispersion] * len(self)

return dispersion
Expand Down
36 changes: 5 additions & 31 deletions gemini_instruments/flamingos/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,17 @@ def _tag_dark(self):
return TagSet(['DARK', 'CAL'])

@astro_data_descriptor
def central_wavelength(self, asMicrometers=False, asNanometers=False,
asAngstroms=False):
@gmu.return_requested_units
def central_wavelength(self):
"""
Returns the central wavelength in meters or the specified units
Parameters
----------
asMicrometers : bool
If True, return the wavelength in microns
asNanometers : bool
If True, return the wavelength in nanometers
asAngstroms : bool
If True, return the wavelength in Angstroms
Returns the central wavelength in microns
Returns
-------
float
The central wavelength setting
The central wavelength setting in um
"""
unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
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 = "micrometers"
if asNanometers:
output_units = "nanometers"
if asAngstroms:
output_units = "angstroms"
else:
# Either none of the unit arguments were set to True or more than
# one of the unit arguments was set to True. In either case,
# return the central wavelength in the default units of meters.
output_units = "meters"

return gmu.convert_units('micrometers', 1.5, output_units)
return 1500.0

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

@astro_data_descriptor
def central_wavelength(self, asMicrometers=False, asNanometers=False,
asAngstroms=False):
@gmu.return_requested_units
def central_wavelength(self):
"""
Returns the central wavelength in meters or the specified units
Parameters
----------
asMicrometers : bool
If True, return the wavelength in microns
asNanometers : bool
If True, return the wavelength in nanometers
asAngstroms : bool
If True, return the wavelength in Angstroms
Returns
-------
float
The central wavelength setting
"""
unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
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 = "micrometers"
if asNanometers:
output_units = "nanometers"
if asAngstroms:
output_units = "angstroms"
else:
# Either none of the unit arguments were set to True or more than
# one of the unit arguments was set to True. In either case,
# return the central wavelength in the default units of meters.
output_units = "meters"

The central wavelength setting in nm
"""
# We assume that the central_wavelength keyword is in microns
keyword = self._keyword_for('central_wavelength')
wave_in_microns = self.phu.get(keyword, -1)
# Convert to float if they saved it as a string
wave_in_microns = float(wave_in_microns) if isinstance(wave_in_microns, str) else wave_in_microns
wave_in_microns = float(self.phu.get(keyword, -1))

if wave_in_microns < 0:
return None
return gmu.convert_units('micrometers', wave_in_microns,
output_units)
return wave_in_microns

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

@astro_data_descriptor
def dispersion(self, asMicrometers=False, asNanometers=False, asAngstroms=False):
@gmu.return_requested_units
def dispersion(self):
"""
Returns the dispersion in meters per pixel as a list (one value per
extension) or a float if used on a single-extension slice. It is
possible to control the units of wavelength using the input arguments.
Parameters
----------
asMicrometers : bool
If True, return the wavelength in microns
asNanometers : bool
If True, return the wavelength in nanometers
asAngstroms : bool
If True, return the wavelength in Angstroms
Returns
-------
list/float
The dispersion(s)
The dispersion(s) in nm
"""
if self._keyword_for('dispersion') in self.hdr:
dispersion = self.hdr[self._keyword_for('dispersion')]

keyword = self._keyword_for('dispersion')
if keyword in self.hdr:
dispersion = self.hdr[keyword]
elif self._keyword_for('dispersion') in self.phu:
dispersion = self.phu[self._keyword_for('dispersion')]

else:
dispersion = None

unit_arg_list = [asMicrometers, asNanometers, asAngstroms]
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 = "micrometers"
if asNanometers:
output_units = "nanometers"
if asAngstroms:
output_units = "angstroms"
else:
# Either none of the unit arguments were set to True or more than
# one of the unit arguments was set to True. In either case,
# return the central wavelength in the default units of meters.
output_units = "meters"

if dispersion is not None:

dispersion = gmu.convert_units('meters', dispersion, output_units)

dispersion = self.phu[keyword]
if not self.is_single:
dispersion = [dispersion] * len(self)
else:
return None

return dispersion

Expand Down
20 changes: 7 additions & 13 deletions gemini_instruments/ghost/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from gemini_instruments.gemini import AstroDataGemini
from gemini_instruments.common import build_group_id

from .. import gmu


def return_dict_for_bundle(desc_fn):
"""
Expand Down Expand Up @@ -393,8 +395,8 @@ def calibration_key(self):

# FIXME Remove once headers corrected
@astro_data_descriptor
def central_wavelength(self, asMicrometers=False, asNanometers=False,
asAngstroms=False): # pragma: no cover
@gmu.return_selected_units
def central_wavelength(self): # pragma: no cover
"""
Dummy to work around current Gemini cal_mgr
"""
Expand All @@ -403,21 +405,13 @@ def central_wavelength(self, asMicrometers=False, asNanometers=False,

if val is None:
if self.arm() == 'red':
val = 7500. * 10**-10
val = 0.75
elif self.arm() == 'blue':
val = 4400. * 10**-10
val = 0.44
else:
return None


if asMicrometers:
val *= 10**6
elif asNanometers:
val *= 10**9
elif asAngstroms:
val *= 10**10

return float(val)
return val

@astro_data_descriptor
@return_dict_for_bundle
Expand Down
Loading

0 comments on commit 7fddb56

Please sign in to comment.