Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 3.x um reflectance with the same dtype as the NIR data #206

Merged
merged 16 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pyspectral/near_infrared_reflectance.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ def reflectance_from_tbs(self, sun_zenith, tb_near_ir, tb_thermal, **kwargs):

# Assume rsr is in microns!!!
# FIXME!
self._rad3x_t11 = self.tb2radiance(tb_therm, lut=lut)['radiance']
thermal_emiss_one = self._rad3x_t11 * self.rsr_integral

l_nir = self.tb2radiance(tb_nir, lut=lut)['radiance']
self._rad3x_t11 = self.tb2radiance(tb_therm, lut=lut)['radiance'].astype(tb_nir.dtype)
rsr_integral = tb_therm.dtype.type(self.rsr_integral)
thermal_emiss_one = self._rad3x_t11 * rsr_integral
l_nir = self.tb2radiance(tb_nir, lut=lut)['radiance'].astype(tb_nir.dtype)
self._rad3x = l_nir.copy()
l_nir *= self.rsr_integral
l_nir *= rsr_integral

if thermal_emiss_one.ravel().shape[0] < 10:
LOG.info('thermal_emiss_one = %s', str(thermal_emiss_one))
Expand All @@ -268,7 +268,8 @@ def reflectance_from_tbs(self, sun_zenith, tb_near_ir, tb_thermal, **kwargs):
self.derive_rad39_corr(tb_therm, tbco2)
LOG.info("CO2 correction applied...")
else:
self._rad3x_correction = 1.0
self._rad3x_correction = np.float64(1.0)
self._rad3x_correction = self._rad3x_correction.astype(tb_nir.dtype)

corrected_thermal_emiss_one = thermal_emiss_one * self._rad3x_correction
nomin = l_nir - corrected_thermal_emiss_one
Expand Down
22 changes: 17 additions & 5 deletions pyspectral/tests/test_reflectance.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,25 @@ def test_reflectance(self):
refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
self.assertTrue(hasattr(refl, 'mask'))

sunz = np.array([80.], dtype=np.float32)
tb3 = np.array([295.], dtype=np.float32)
tb4 = np.array([282.], dtype=np.float32)
refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
np.testing.assert_allclose(refl, np.array([0.45249779], np.float32))
assert refl.dtype == np.float32

try:
import dask.array as da
sunz = da.from_array([50.], chunks=10)
tb3 = da.from_array([300.], chunks=10)
tb4 = da.from_array([285.], chunks=10)
refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
self.assertTrue(hasattr(refl, 'compute'))
import dask.config

from pyspectral.tests.unittest_helpers import CustomScheduler

with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
sunz = da.from_array([50.], chunks=10)
tb3 = da.from_array([300.], chunks=10)
tb4 = da.from_array([285.], chunks=10)
refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
self.assertTrue(hasattr(refl, 'compute'))
except ImportError:
pass

Expand Down
18 changes: 18 additions & 0 deletions pyspectral/tests/unittest_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@
raise AssertionError("Shapes don't match")
if not np.allclose(self, other):
raise AssertionError("Elements don't match!")


class CustomScheduler(object):
pnuu marked this conversation as resolved.
Show resolved Hide resolved
mraspaud marked this conversation as resolved.
Show resolved Hide resolved
"""Scheduler raising an exception if data are computed too many times."""

def __init__(self, max_computes=1):
"""Set starting and maximum compute counts."""
self.max_computes = max_computes
self.total_computes = 0

def __call__(self, dsk, keys, **kwargs):
"""Compute dask task and keep track of number of times we do so."""
import dask
self.total_computes += 1
if self.total_computes > self.max_computes:
raise RuntimeError("Too many dask computations were scheduled: "

Check warning on line 46 in pyspectral/tests/unittest_helpers.py

View check run for this annotation

Codecov / codecov/patch

pyspectral/tests/unittest_helpers.py#L43-L46

Added lines #L43 - L46 were not covered by tests
"{}".format(self.total_computes))
return dask.get(dsk, keys, **kwargs)

Check warning on line 48 in pyspectral/tests/unittest_helpers.py

View check run for this annotation

Codecov / codecov/patch

pyspectral/tests/unittest_helpers.py#L48

Added line #L48 was not covered by tests
Loading