From 99c8391632399179a4233592a0aae45796fa621e Mon Sep 17 00:00:00 2001 From: Josh Meyers Date: Mon, 16 Sep 2024 10:28:58 -0700 Subject: [PATCH] Add Quantity support to DCR config layer --- galsim/config/value.py | 2 +- galsim/photon_array.py | 24 ++++++++++++++++++++---- tests/test_photon_array.py | 7 ++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/galsim/config/value.py b/galsim/config/value.py index 919774dac2..ce26f92e2c 100644 --- a/galsim/config/value.py +++ b/galsim/config/value.py @@ -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( diff --git a/galsim/photon_array.py b/galsim/photon_array.py index 48f1bcbbc9..e1edd9c11c 100644 --- a/galsim/photon_array.py +++ b/galsim/photon_array.py @@ -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 @@ -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): @@ -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 + p = self.kw.get('pressure', None) + if p is not None and isinstance(p, u.Quantity): + self.kw['pressure'] = p.to_value(u.kPa) + t = self.kw.get('temperature', None) + if t is not None and isinstance(t, u.Quantity): + self.kw['temperature'] = t.to_value(u.K) + h = self.kw.get('H2O_pressure', None) + if h is not None and 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 diff --git a/tests/test_photon_array.py b/tests/test_photon_array.py index f17cf409ea..fd7bd5fa5b 100644 --- a/tests/test_photon_array.py +++ b/tests/test_photon_array.py @@ -18,6 +18,7 @@ import unittest import numpy as np +import astropy.units as u import os import warnings @@ -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