Skip to content

Commit

Permalink
Add Quantity support to DCR config layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeyers314 committed Sep 16, 2024
1 parent 771d45b commit 9b37ea3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion galsim/config/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def ParseValue(config, key, base, value_type):
for vt in value_type:
try:
return ParseValue(config, key, base, vt)
except GalSimConfigError:
except (GalSimConfigError, TypeError):
pass
else:
raise GalSimConfigError(
Expand Down
24 changes: 20 additions & 4 deletions galsim/photon_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
# and/or other materials provided with the distribution.
#

__all__ = [ 'PhotonArray', 'PhotonOp', 'WavelengthSampler', 'FRatioAngles',
__all__ = [ 'PhotonArray', 'PhotonOp', 'WavelengthSampler', 'FRatioAngles',
'PhotonDCR', 'Refraction', 'FocusDepth',
'PupilImageSampler', 'PupilAnnulusSampler', 'TimeSampler',
'ScaleFlux', 'ScaleWavelength' ]

import numpy as np
import astropy.units as u

from . import _galsim
from .random import BaseDeviate
Expand Down Expand Up @@ -1017,9 +1018,14 @@ class PhotonDCR(PhotonOp):
H2O_pressure: Water vapor pressure in kiloPascals. [default: 1.067 kPa]
"""
_req_params = { 'base_wavelength' : float }
_opt_params = { 'scale_unit' : str, 'alpha' : float,
'parallactic_angle' : Angle, 'latitude' : Angle,
'pressure' : float, 'temperature' : float, 'H2O_pressure' : float }
_opt_params = { 'scale_unit' : str,
'alpha' : float,
'parallactic_angle' : Angle,
'latitude' : Angle,
'pressure' : (float, u.Quantity),
'temperature' : (float, u.Quantity),
'H2O_pressure' : (float, u.Quantity)
}
_single_params = [ { 'zenith_angle' : Angle, 'HA' : Angle, 'zenith_coord' : CelestialCoord } ]

def __init__(self, base_wavelength, scale_unit=arcsec, **kwargs):
Expand All @@ -1032,6 +1038,16 @@ def __init__(self, base_wavelength, scale_unit=arcsec, **kwargs):
self.alpha = kwargs.pop('alpha', 0.)

self.zenith_angle, self.parallactic_angle, self.kw = dcr.parse_dcr_angles(**kwargs)
# Convert any weather data to the appropriate units
if (p := self.kw.get('pressure')) is not None:
if isinstance(p, u.Quantity):
self.kw['pressure'] = p.to_value(u.kPa)
if (t := self.kw.get('temperature')) is not None:
if isinstance(t, u.Quantity):
self.kw['temperature'] = t.to_value(u.K)
if (h := self.kw.get('H2O_pressure')) is not None:
if isinstance(h, u.Quantity):
self.kw['H2O_pressure'] = h.to_value(u.kPa)

# Any remaining kwargs will get forwarded to galsim.dcr.get_refraction
# Check that they're valid
Expand Down
7 changes: 4 additions & 3 deletions tests/test_photon_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import unittest
import numpy as np
import astropy.units as u
import os
import warnings

Expand Down Expand Up @@ -841,9 +842,9 @@ def test_dcr():
'base_wavelength': base_wavelength,
'HA': local_sidereal_time-obj_coord.ra,
'latitude': '-30:14:23.76 deg',
'pressure': 72,
'temperature': 290,
'H2O_pressure': 0.9,
'pressure': 72*u.kPa,
'temperature': 290, #'290 K',
'H2O_pressure': '$900*u.Pa',
}
im5c = galsim.config.BuildImage(config)
assert im5c == im5
Expand Down

0 comments on commit 9b37ea3

Please sign in to comment.