Skip to content

Commit

Permalink
solve compatibility issue with recent python versions and solved warn…
Browse files Browse the repository at this point in the history
…ings in the tests
  • Loading branch information
ghislainp committed Jun 28, 2024
1 parent 1aef62f commit 1f3872b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 36 deletions.
37 changes: 14 additions & 23 deletions smrt/core/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,17 @@ def __init__(self, frequency=None, theta_inc_deg=None, theta_deg=None, phi_deg=N
"""
super().__init__()

if frequency is not None and wavelength is not None:
smrt_warn("Sensor requires either frequency or wavelength argument, not both")
if wavelength is not None:
self.wavelength = wavelength
if frequency is not None:
if wavelength is not None:
smrt_warn("Sensor requires either frequency or wavelength argument, not both")

self.frequency = np.asarray(frequency).squeeze() if isinstance(frequency, Sequence) else frequency
self.wavelength = C_SPEED / self.frequency
elif wavelength is not None:
self.wavelength = np.asarray(wavelength).squeeze() if isinstance(wavelength, Sequence) else wavelength
self.frequency = C_SPEED / self.wavelength
else:
self.frequency = frequency

if isinstance(self.frequency, Sequence):
self.frequency = np.array(self.frequency).squeeze()
raise SMRTError("Either frequency or wavelength is required")

self.channel_map = channel_map or dict()

Expand Down Expand Up @@ -240,18 +242,6 @@ def __init__(self, frequency=None, theta_inc_deg=None, theta_deg=None, phi_deg=N
self.theta_inc = np.radians(self.theta_inc_deg)
self.mu_s = np.cos(self.theta_inc)

@property
def wavelength(self):
if hasattr(self, "_wls"):
return self._wls # avoid calculation and numerical rounding error when wavelength has been explicitely set
else:
return C_SPEED / self.frequency

@wavelength.setter
def wavelength(self, wls):
self._wls = wls
self.frequency = C_SPEED / wls

@property
def wavenumber(self):
return 2 * np.pi / self.wavelength
Expand All @@ -272,7 +262,7 @@ def basic_checks(self):
# Check frequency range. Below 300 MHz is an indication the units may be wrong
# Not documented as it will not be called by the user.

frequency_min = min(np.atleast_1d(self.frequency))
frequency_min = np.min(np.atleast_1d(self.frequency))

if frequency_min < 300e6:
# Checks frequency is above 300 MHz
Expand Down Expand Up @@ -350,8 +340,9 @@ class Altimeter(Sensor):
"""

def __init__(self, frequency, altitude, beamwidth, pulse_bandwidth, sigma_p=None, off_nadir_angle=0, beam_asymmetry=0,
ngate=1024, nominal_gate=40, theta_inc_deg=0., polarization_inc=None, polarization=None, channel=None):
def __init__(self, frequency, altitude, beamwidth, pulse_bandwidth, sigma_p=None, antenna_gain=1, off_nadir_angle=0,
beam_asymmetry=0, ngate=1024, nominal_gate=40, theta_inc_deg=0., polarization_inc=None, polarization=None,
channel=None):

channel_map = {channel: dict()} if channel is not None else dict()

Expand Down
2 changes: 1 addition & 1 deletion smrt/core/snowpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def mid_layer_depths(self):

@property
def z(self):
"""return the depth of each interface, that is, 0 and the depth of the bottom of each layer
"""return the depth of each interface, that is, 0 and the depths of the bottom of each layer
"""
return np.insert(self.bottom_layer_depths, 0, 0)
Expand Down
2 changes: 1 addition & 1 deletion smrt/emmodel/commontest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_energy_conservation(em, tolerance_pc, npol=None, subset=16):
#p_sum, p_err = scipy.integrate.quad(phase_fn_int, 0, 2. * np.pi, args=(inc, pol, em, mu, npol)) # not needed as we already use m=0

p11_12 = np.sum(ft_even_phase[:, pol, 0, :, inc], axis=0)
p_sum = 2 * np.pi * scipy.integrate.simps(p11_12, mu)
p_sum = 2 * np.pi * scipy.integrate.simpson(p11_12, x=mu)
phase_integral = p_sum / (4. * np.pi)
print ('Test failed for incidence__polarization_angle_number: ', inc, pol) # Only prints if test fails
print ('Integrated phase function is: ', phase_integral) # Only prints if test fails
Expand Down
4 changes: 2 additions & 2 deletions smrt/rtsolver/dort.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import numpy as np
from pandas._libs import properties
import xarray as xr
import scipy.special.orthogonal
import scipy.special
import scipy.linalg
import scipy.interpolate

Expand Down Expand Up @@ -1190,7 +1190,7 @@ def gaussquad(n):
# """
assert n >= 2

mu, weight = scipy.special.orthogonal.p_roots(2 * n)
mu, weight = scipy.special.p_roots(2 * n)

mu = mu[-1:n - 1:-1]
weight = weight[-1:n - 1:-1]
Expand Down
3 changes: 1 addition & 2 deletions smrt/rtsolver/nadir_lrm_altimetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ def gate_depth(self, eps=None):

def combined_depth_grid(self):

z_lay = self.snowpack.layer_depths
z_lay = np.insert(z_lay, 0, 0) # include the top layer
z_lay = self.snowpack.z

# merge both depth array (layer and gate)
z = np.concatenate((z_lay, self.z_gate))
Expand Down
4 changes: 3 additions & 1 deletion smrt/test/test_coherent_layer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

import warnings
import numpy as np

# local import
from smrt import make_snowpack, make_model, sensor_list
from smrt.core.error import SMRTWarning


def test_snowpack_with_coherent_layer():
Expand All @@ -23,7 +25,7 @@ def test_snowpack_with_coherent_layer():

# create the EM Model - Equivalent DMRTML
m = make_model("iba", "dort", rtsolver_options={'n_max_stream': 64, 'process_coherent_layers': True})

warnings.simplefilter("ignore", category=SMRTWarning)
res = m.run(radiometer, sp)

print(res.TbV(), res.TbH())
Expand Down
6 changes: 6 additions & 0 deletions smrt/test/test_dmrtdort.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# coding: utf-8

from re import sub
import warnings

import numpy as np

# local import
from smrt import make_snowpack, make_model, make_snow_layer, make_soil
from smrt.inputs.make_medium import make_transparent_volume
from smrt.inputs.sensor_list import amsre, active, passive
from smrt.substrate.reflector import make_reflector
from smrt.core.error import SMRTWarning

#
# Ghi: rapid hack, should be splitted in different functions
Expand Down Expand Up @@ -84,6 +87,7 @@ def test_less_refringent_bottom_layer():
# this test fails with some version of scipy if not using the shur method
m = make_model("dmrt_qcacp_shortrange", "dort", rtsolver_options=dict(diagonalization_method='shur_forcedtriu'))
scat = active(10e9, 45)
warnings.simplefilter("ignore", category=SMRTWarning)
res = m.run(scat, snowpack)
print(res.sigmaVV_dB(), res.sigmaHH_dB())
assert abs(res.sigmaVV_dB() - -50.25547167709486) < 1e-1
Expand All @@ -97,6 +101,7 @@ def test_less_refringent_bottom_layer_VV():
snowpack = make_snowpack([0.2, 0.3], "sticky_hard_spheres", density = [290.0, 250.0], radius = 1e-4, stickiness=0.2)
m = make_model("dmrt_qcacp_shortrange", "dort", rtsolver_options=dict(diagonalization_method='shur_forcedtriu'))
scat = active(10e9, 45)
warnings.simplefilter("ignore", category=SMRTWarning)
res = m.run(scat, snowpack)
print(res.sigmaVV())
assert abs(res.sigmaVV() - 7.54253344e-05) < 1e-7
Expand All @@ -107,6 +112,7 @@ def test_less_refringent_bottom_layer_HH():
snowpack = make_snowpack([0.2, 0.3], "sticky_hard_spheres", density = [290.0, 250.0], radius = 1e-4, stickiness=0.2)
m = make_model("dmrt_qcacp_shortrange", "dort", rtsolver_options=dict(diagonalization_method='shur_forcedtriu'))
scat = active(10e9, 45)
warnings.simplefilter("ignore", category=SMRTWarning)
res = m.run(scat, snowpack)
print(res.sigmaHH())
assert abs(res.sigmaHH() - 7.09606407e-05) < 1e-7
Expand Down
11 changes: 5 additions & 6 deletions smrt/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
This packages contain various utilities that works with/for SMRT.
Expand All @@ -7,17 +6,17 @@
"""


import numpy as np

LOG10 = np.log(10)
LOG10 = np.log(10.)


def dB(x):
"""computes the ratio x in dB."""
return 10*np.log(x)/LOG10
"""Compute the ratio x in dB. Any small value is converted to -200dB.
"""
return 10 * np.log(np.maximum(x, 1e-20)) / LOG10


def invdB(x):
"""computes the dB value x in natural value."""
return 10**(x/10.0)
return 10 ** (x / 10.0)

0 comments on commit 1f3872b

Please sign in to comment.