Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 19b02a5

Browse files
authored
Pad Satellite data with zeros (#653)
* Pad data with zeros * Change padding * Fix checks and add test * Fix path
1 parent d21e31b commit 19b02a5

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

nowcasting_dataset/data_sources/satellite/satellite_data_source.py

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -162,67 +162,47 @@ def get_spatial_region_of_interest(
162162
max_x_and_y_index_height = x_and_y_index_at_center + half_image_size_pixels_height
163163

164164
# Check whether the requested region of interest steps outside of the available data:
165-
suggested_reduction_of_image_size_pixels_width = (
166-
max(
167-
(-min_x_and_y_index_width.min() if (min_x_and_y_index_width < 0).any() else 0),
168-
(min_x_and_y_index_width.x_index_at_center - len(data_array.x_geostationary)),
169-
)
170-
* 2
165+
# Need to know how much to pad the outputs, so can do that here
166+
min_width_padding = max(-min_x_and_y_index_width.x_index_at_center, 0)
167+
max_width_padding = max(
168+
max_x_and_y_index_width.x_index_at_center - len(data_array.x_geostationary), 0
171169
)
172-
suggested_reduction_of_image_size_pixels_height = (
173-
max(
174-
(-max_x_and_y_index_height.min() if (max_x_and_y_index_height < 0).any() else 0),
175-
(max_x_and_y_index_height.y_index_at_center - len(data_array.y_geostationary)),
176-
)
177-
* 2
170+
min_height_padding = max(-min_x_and_y_index_height.y_index_at_center, 0)
171+
max_height_padding = max(
172+
max_x_and_y_index_height.y_index_at_center - len(data_array.y_geostationary), 0
178173
)
179-
# If the requested region does step outside the available data then raise an exception
180-
# with a helpful message:
181-
if (
182-
suggested_reduction_of_image_size_pixels_width > 0
183-
or suggested_reduction_of_image_size_pixels_height > 0
184-
):
185-
new_suggested_image_size_pixels_width = (
186-
self._rectangle.size_pixels_width - suggested_reduction_of_image_size_pixels_width
187-
)
188-
new_suggested_image_size_pixels_height = (
189-
self._rectangle.size_pixels_height - suggested_reduction_of_image_size_pixels_height
190-
)
191-
raise RuntimeError(
192-
"Requested region of interest of satellite data steps outside of the available"
193-
" geographical extent of the Zarr data. The requested region of interest extends"
194-
f" from pixel indicies"
195-
f" x={min_x_and_y_index_width.x_index_at_center} to"
196-
f" x={min_x_and_y_index_width.x_index_at_center},"
197-
f" y={min_x_and_y_index_width.y_index_at_center} to"
198-
f" y={min_x_and_y_index_width.y_index_at_center}."
199-
f" In the Zarr data, len(x)={len(data_array.x_geostationary)},"
200-
f" len(y)={len(data_array.y_geostationary)}."
201-
f" Try reducing image_size_pixels_height from {self._rectangle.size_pixels_height}"
202-
f" to {new_suggested_image_size_pixels_height} pixels."
203-
f" Try reducing image_size_pixels_width from {self._rectangle.size_pixels_width}"
204-
f" to {new_suggested_image_size_pixels_width} pixels."
205-
f" {self.history_length=}; {self.forecast_length=}; {x_center_osgb=};"
206-
f" {y_center_osgb=}; {x_center_geostationary=}; {y_center_geostationary=};"
207-
f" {min(data_array.x_geostationary.values)=};"
208-
f" {max(data_array.x_geostationary.values)=};"
209-
f" {min(data_array.y_geostationary.values)=};"
210-
f" {max(data_array.y_geostationary.values)=};\n"
211-
f" {data_array=}"
212-
)
213174

214175
# Select the geographical region of interest.
215176
# Note that isel is *exclusive* of the end of the slice.
216177
# e.g. isel(x=slice(0, 3)) will return the first, second, and third values.
217178
data_array = data_array.isel(
218179
x_geostationary=slice(
219-
min_x_and_y_index_width.x_index_at_center, max_x_and_y_index_width.x_index_at_center
180+
max(min_x_and_y_index_width.x_index_at_center, 0),
181+
min(max_x_and_y_index_width.x_index_at_center, len(data_array.x_geostationary)),
220182
),
221183
y_geostationary=slice(
222-
min_x_and_y_index_height.y_index_at_center,
223-
max_x_and_y_index_height.y_index_at_center,
184+
max(min_x_and_y_index_height.y_index_at_center, 0),
185+
min(max_x_and_y_index_height.y_index_at_center, len(data_array.y_geostationary)),
224186
),
225187
)
188+
189+
# isel is wrong if padding before, so add padding after to be the correct size
190+
# Get the difference for each direction and use that
191+
if (
192+
min_height_padding > 0
193+
or min_width_padding > 0
194+
or max_height_padding > 0
195+
or max_width_padding > 0
196+
):
197+
data_array = data_array.pad(
198+
pad_width={
199+
"x_geostationary": (min_width_padding, max_width_padding),
200+
"y_geostationary": (min_height_padding, max_height_padding),
201+
},
202+
mode="constant",
203+
constant_values=0,
204+
)
205+
226206
return data_array
227207

228208
def get_example(self, location: SpaceTimeLocation) -> xr.Dataset:

tests/data_sources/satellite/test_satellite_data_source.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ def _test_get_example(
5555
assert len(sat_data.x_geostationary.shape) == 1
5656

5757

58+
def test_padding(sat_filename): # noqa: D103
59+
data_source = SatelliteDataSource(
60+
image_size_pixels_height=1024,
61+
image_size_pixels_width=1024,
62+
zarr_path=sat_filename,
63+
history_minutes=0,
64+
forecast_minutes=15,
65+
channels=("IR_016",),
66+
meters_per_pixel=6000,
67+
)
68+
data_source.open()
69+
t0_dt = pd.Timestamp("2020-04-01T13:00")
70+
sat_data = data_source.get_example(
71+
SpaceTimeLocation(t0_datetime_utc=t0_dt, x_center_osgb=0, y_center_osgb=0)
72+
)
73+
74+
assert len(sat_data.x_geostationary) == 1024
75+
assert len(sat_data.y_geostationary) == 1024
76+
assert len(sat_data.x_geostationary.shape) == 1
77+
78+
5879
@pytest.mark.parametrize(
5980
"x_center_osgb, y_center_osgb, left_geostationary, right_geostationary,"
6081
" top_geostationary, bottom_geostationary",

0 commit comments

Comments
 (0)