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

Fix types to allow float32 computations for SAR-C #2925

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 32 additions & 25 deletions satpy/readers/sar_c_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
return int(r.text)
except ValueError:
try:
return float(r.text)
return np.float32(r.text)

Check warning on line 81 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L81

Added line #L81 was not covered by tests
except ValueError:
return r.text
for x in r.findall("./*"):
Expand Down Expand Up @@ -186,7 +186,7 @@

def get_calibration_constant(self):
"""Load the calibration constant."""
return float(self.root.find(".//absoluteCalibrationConstant").text)
return np.float32(self.root.find(".//absoluteCalibrationConstant").text)

Check warning on line 189 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L189

Added line #L189 was not covered by tests

def _get_calibration_uncached(self, calibration, chunks=None):
"""Get the calibration array."""
Expand Down Expand Up @@ -341,7 +341,7 @@
current_blocks = self._find_blocks_covering_line(current_line)
current_blocks.sort(key=(lambda x: x.coords["x"][0]))
next_line = self._get_next_start_line(current_blocks, current_line)
current_y = np.arange(current_line, next_line)
current_y = np.arange(current_line, next_line, dtype=np.uint16)

Check warning on line 344 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L344

Added line #L344 was not covered by tests
pieces = [arr.sel(y=current_y) for arr in current_blocks]
return pieces

Expand Down Expand Up @@ -389,7 +389,7 @@
@staticmethod
def _fill_dask_pieces(dask_pieces, shape, chunks):
if shape[1] > 0:
new_piece = da.full(shape, np.nan, chunks=chunks)
new_piece = da.full(shape, np.nan, chunks=chunks, dtype=np.float32)

Check warning on line 392 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L392

Added line #L392 was not covered by tests
dask_pieces.append(new_piece)


Expand Down Expand Up @@ -425,11 +425,10 @@
# corr = 1.5
data = self.lut * corr

x_coord = np.arange(self.first_pixel, self.last_pixel + 1)
y_coord = np.arange(self.first_line, self.last_line + 1)

new_arr = (da.ones((len(y_coord), len(x_coord)), chunks=chunks) *
np.interp(y_coord, self.lines, data)[:, np.newaxis])
x_coord = np.arange(self.first_pixel, self.last_pixel + 1, dtype=np.uint16)
y_coord = np.arange(self.first_line, self.last_line + 1, dtype=np.uint16)
new_arr = (da.ones((len(y_coord), len(x_coord)), dtype=np.float32, chunks=chunks) *

Check warning on line 430 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L428-L430

Added lines #L428 - L430 were not covered by tests
np.interp(y_coord, self.lines, data)[:, np.newaxis].astype(np.float32))
new_arr = xr.DataArray(new_arr,
dims=["y", "x"],
coords={"x": x_coord,
Expand All @@ -438,29 +437,29 @@

@property
def first_pixel(self):
return int(self.element.find("firstRangeSample").text)
return np.uint16(self.element.find("firstRangeSample").text)

Check warning on line 440 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L440

Added line #L440 was not covered by tests

@property
def last_pixel(self):
return int(self.element.find("lastRangeSample").text)
return np.uint16(self.element.find("lastRangeSample").text)

Check warning on line 444 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L444

Added line #L444 was not covered by tests

@property
def first_line(self):
return int(self.element.find("firstAzimuthLine").text)
return np.uint16(self.element.find("firstAzimuthLine").text)

Check warning on line 448 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L448

Added line #L448 was not covered by tests

@property
def last_line(self):
return int(self.element.find("lastAzimuthLine").text)
return np.uint16(self.element.find("lastAzimuthLine").text)

Check warning on line 452 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L452

Added line #L452 was not covered by tests

@property
def lines(self):
lines = self.element.find("line").text.split()
return np.array(lines).astype(int)
return np.array(lines).astype(np.uint16)

Check warning on line 457 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L457

Added line #L457 was not covered by tests

@property
def lut(self):
lut = self.element.find("noiseAzimuthLut").text.split()
return np.array(lut).astype(float)
return np.array(lut, dtype=np.float32)

Check warning on line 462 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L462

Added line #L462 was not covered by tests


class XMLArray:
Expand All @@ -487,7 +486,7 @@
new_x = elt.find("pixel").text.split()
y += [int(elt.find("line").text)] * len(new_x)
x += [int(val) for val in new_x]
data += [float(val)
data += [np.float32(val)

Check warning on line 489 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L489

Added line #L489 was not covered by tests
for val in elt.find(self.element_tag).text.split()]

return np.asarray(data), (x, y)
Expand Down Expand Up @@ -519,17 +518,17 @@
else:
vchunks, hchunks = chunks, chunks

points = _ndim_coords_from_arrays(np.vstack((np.asarray(ypoints),
np.asarray(xpoints))).T)
points = _ndim_coords_from_arrays(np.vstack((np.asarray(ypoints, dtype=np.uint16),

Check warning on line 521 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L521

Added line #L521 was not covered by tests
np.asarray(xpoints, dtype=np.uint16))).T)

interpolator = LinearNDInterpolator(points, values)

grid_x, grid_y = da.meshgrid(da.arange(shape[1], chunks=hchunks),
da.arange(shape[0], chunks=vchunks))
grid_x, grid_y = da.meshgrid(da.arange(shape[1], chunks=hchunks, dtype=np.uint16),

Check warning on line 526 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L526

Added line #L526 was not covered by tests
da.arange(shape[0], chunks=vchunks, dtype=np.uint16))

# workaround for non-thread-safe first call of the interpolator:
interpolator((0, 0))
res = da.map_blocks(intp, grid_x, grid_y, interpolator=interpolator)
res = da.map_blocks(intp, grid_x, grid_y, interpolator=interpolator).astype(values.dtype)

Check warning on line 531 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L531

Added line #L531 was not covered by tests

return DataArray(res, dims=("y", "x"))

Expand Down Expand Up @@ -617,7 +616,7 @@
def _get_digital_number(self, data):
"""Get the digital numbers (uncalibrated data)."""
data = data.where(data > 0)
data = data.astype(np.float64)
data = data.astype(np.float32)

Check warning on line 619 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L619

Added line #L619 was not covered by tests
dn = data * data
return dn

Expand Down Expand Up @@ -675,8 +674,8 @@
for feature in gcps["features"]]
gcp_array = np.array(gcp_list)

ypoints = np.unique(gcp_array[:, 0])
xpoints = np.unique(gcp_array[:, 1])
ypoints = np.unique(gcp_array[:, 0]).astype(np.uint16)
xpoints = np.unique(gcp_array[:, 1]).astype(np.uint16)

Check warning on line 678 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L677-L678

Added lines #L677 - L678 were not covered by tests

gcp_lons = gcp_array[:, 2].reshape(ypoints.shape[0], xpoints.shape[0])
gcp_lats = gcp_array[:, 3].reshape(ypoints.shape[0], xpoints.shape[0])
Expand All @@ -686,6 +685,13 @@

return (xpoints, ypoints), (gcp_lons, gcp_lats, gcp_alts), (rio_gcps, crs)

def get_bounding_box(self):

Check warning on line 688 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L688

Added line #L688 was not covered by tests
"""Get the bounding box for the data coverage."""
(xpoints, ypoints), (gcp_lons, gcp_lats, gcp_alts), (rio_gcps, crs) = self.get_gcps()
bblons = np.hstack((gcp_lons[0, :-1], gcp_lons[:-1, -1], gcp_lons[-1, :1:-1], gcp_lons[:1:-1, 0]))
bblats = np.hstack((gcp_lats[0, :-1], gcp_lats[:-1, -1], gcp_lats[-1, :1:-1], gcp_lats[:1:-1, 0]))
return bblons.tolist(), bblats.tolist()

Check warning on line 693 in satpy/readers/sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/readers/sar_c_safe.py#L690-L693

Added lines #L690 - L693 were not covered by tests

@property
def start_time(self):
"""Get the start time."""
Expand Down Expand Up @@ -733,7 +739,8 @@
gcps = get_gcps_from_array(val)
from pyresample.future.geometry import SwathDefinition
val.attrs["area"] = SwathDefinition(lonlats["longitude"], lonlats["latitude"],
attrs=dict(gcps=gcps))
attrs=dict(gcps=gcps,
bounding_box=handler.get_bounding_box()))
datasets[key] = val
continue
return datasets
Expand Down
35 changes: 29 additions & 6 deletions satpy/tests/reader_tests/test_sar_c_safe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python

Check notice on line 1 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Code Duplication

The module no longer contains too many functions with similar structure
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Satpy developers
#
Expand Down Expand Up @@ -215,22 +215,28 @@
calibration = Calibration.sigma_nought
xarr = measurement_filehandler.get_dataset(DataQuery(name="measurement", polarization="vv",
calibration=calibration, quantity="natural"), info=dict())
expected = np.array([[np.nan, 0.02707529], [2.55858416, 3.27611055]])
expected = np.array([[np.nan, 0.02707529], [2.55858416, 3.27611055]], dtype=np.float32)

Check warning on line 218 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L218

Added line #L218 was not covered by tests
np.testing.assert_allclose(xarr.values[:2, :2], expected, rtol=2e-7)
assert xarr.dtype == np.float32
assert xarr.compute().dtype == np.float32

Check warning on line 221 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L220-L221

Added lines #L220 - L221 were not covered by tests

def test_read_calibrated_dB(self, measurement_filehandler):
"""Test the calibration routines."""
calibration = Calibration.sigma_nought
xarr = measurement_filehandler.get_dataset(DataQuery(name="measurement", polarization="vv",
calibration=calibration, quantity="dB"), info=dict())
expected = np.array([[np.nan, -15.674268], [4.079997, 5.153585]])
np.testing.assert_allclose(xarr.values[:2, :2], expected)
expected = np.array([[np.nan, -15.674268], [4.079997, 5.153585]], dtype=np.float32)
np.testing.assert_allclose(xarr.values[:2, :2], expected, rtol=1e-6)
assert xarr.dtype == np.float32
assert xarr.compute().dtype == np.float32

Check warning on line 231 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L228-L231

Added lines #L228 - L231 were not covered by tests

def test_read_lon_lats(self, measurement_filehandler):
"""Test reading lons and lats."""
query = DataQuery(name="longitude", polarization="vv")
xarr = measurement_filehandler.get_dataset(query, info=dict())
np.testing.assert_allclose(xarr.values, expected_longitudes)
assert xarr.dtype == np.float64
assert xarr.compute().dtype == np.float64

Check warning on line 239 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L238-L239

Added lines #L238 - L239 were not covered by tests


annotation_xml = b"""<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -702,6 +708,8 @@
query = DataQuery(name="noise", polarization="vv")
res = noise_filehandler.get_dataset(query, {})
np.testing.assert_allclose(res, self.expected_azimuth_noise * self.expected_range_noise)
assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 712 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L711-L712

Added lines #L711 - L712 were not covered by tests

def test_get_noise_dataset_has_right_chunk_size(self, noise_filehandler):
"""Test using get_dataset for the noise has right chunk size in result."""
Expand All @@ -724,31 +732,40 @@
expected_dn = np.ones((10, 10)) * 1087
res = calibration_filehandler.get_calibration(Calibration.dn, chunks=5)
np.testing.assert_allclose(res, expected_dn)
assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 736 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L735-L736

Added lines #L735 - L736 were not covered by tests

def test_beta_calibration_array(self, calibration_filehandler):
"""Test reading the beta calibration array."""
expected_beta = np.ones((10, 10)) * 1087
res = calibration_filehandler.get_calibration(Calibration.beta_nought, chunks=5)
np.testing.assert_allclose(res, expected_beta)
assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 744 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L743-L744

Added lines #L743 - L744 were not covered by tests

def test_sigma_calibration_array(self, calibration_filehandler):
"""Test reading the sigma calibration array."""
expected_sigma = np.array([[1894.274, 1841.4335, 1788.593, 1554.4165, 1320.24, 1299.104,
1277.968, 1277.968, 1277.968, 1277.968]]) * np.ones((10, 1))
res = calibration_filehandler.get_calibration(Calibration.sigma_nought, chunks=5)
np.testing.assert_allclose(res, expected_sigma)

assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 753 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L752-L753

Added lines #L752 - L753 were not covered by tests

def test_gamma_calibration_array(self, calibration_filehandler):
"""Test reading the gamma calibration array."""
res = calibration_filehandler.get_calibration(Calibration.gamma, chunks=5)
np.testing.assert_allclose(res, self.expected_gamma)
assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 760 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L759-L760

Added lines #L759 - L760 were not covered by tests

def test_get_calibration_dataset(self, calibration_filehandler):
"""Test using get_dataset for the calibration."""
query = DataQuery(name="gamma", polarization="vv")
res = calibration_filehandler.get_dataset(query, {})
np.testing.assert_allclose(res, self.expected_gamma)
assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 768 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L767-L768

Added lines #L767 - L768 were not covered by tests

def test_get_calibration_dataset_has_right_chunk_size(self, calibration_filehandler):
"""Test using get_dataset for the calibration yields array with right chunksize."""
Expand All @@ -762,13 +779,16 @@
query = DataQuery(name="calibration_constant", polarization="vv")
res = calibration_filehandler.get_dataset(query, {})
assert res == 1
assert type(res) is np.float32

Check warning on line 782 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L782

Added line #L782 was not covered by tests


def test_incidence_angle(annotation_filehandler):
"""Test reading the incidence angle in an annotation file."""
query = DataQuery(name="incidence_angle", polarization="vv")
res = annotation_filehandler.get_dataset(query, {})
np.testing.assert_allclose(res, 19.18318046)
assert res.dtype == np.float32
assert res.compute().dtype == np.float32

Check warning on line 791 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L790-L791

Added lines #L790 - L791 were not covered by tests


def test_reading_from_reader(measurement_file, calibration_file, noise_file, annotation_file):
Expand All @@ -787,7 +807,9 @@
array = dataset_dict["measurement"]
np.testing.assert_allclose(array.attrs["area"].lons, expected_longitudes)
expected_db = np.array([[np.nan, -15.674268], [4.079997, 5.153585]])
np.testing.assert_allclose(array.values[:2, :2], expected_db)
np.testing.assert_allclose(array.values[:2, :2], expected_db, rtol=1e-6)
assert array.dtype == np.float32
assert array.compute().dtype == np.float32

Check warning on line 812 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L810-L812

Added lines #L810 - L812 were not covered by tests


def test_filename_filtering_from_reader(measurement_file, calibration_file, noise_file, annotation_file, tmp_path):
Expand All @@ -814,7 +836,7 @@
pytest.fail(str(err))


def test_swath_def_contains_gcps(measurement_file, calibration_file, noise_file, annotation_file):
def test_swath_def_contains_gcps_and_bounding_box(measurement_file, calibration_file, noise_file, annotation_file):

Check warning on line 839 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L839

Added line #L839 was not covered by tests
"""Test reading using the reader defined in the config."""
with open(Path(PACKAGE_CONFIG_PATH) / "readers" / "sar-c_safe.yaml") as fd:
config = yaml.load(fd, Loader=yaml.UnsafeLoader)
Expand All @@ -829,3 +851,4 @@
dataset_dict = reader.load([query])
array = dataset_dict["measurement"]
assert array.attrs["area"].attrs["gcps"] is not None
assert array.attrs["area"].attrs["bounding_box"] is not None

Check warning on line 854 in satpy/tests/reader_tests/test_sar_c_safe.py

View check run for this annotation

Codecov / codecov/patch

satpy/tests/reader_tests/test_sar_c_safe.py#L854

Added line #L854 was not covered by tests
Loading