Skip to content

Commit

Permalink
reprojection works
Browse files Browse the repository at this point in the history
  • Loading branch information
konstntokas committed Jan 9, 2025
1 parent 99a362a commit da00868
Show file tree
Hide file tree
Showing 16 changed files with 9,438 additions and 237 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

92 changes: 29 additions & 63 deletions test/stac_extension/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,18 @@ def create_raster_stac_item_v1() -> pystac.Item:
stac_extensions=["https://stac-extensions.github.io/raster/v1.1.0/schema.json"],
)

for asset_name in ["B01", "B02"]:
asset_href = f"https://example.com/data/{asset_name}.tif"
asset = pystac.Asset(href=asset_href, media_type="image/tiff", roles=["data"])
asset.extra_fields["raster:bands"] = [
dict(
nodata=0,
scale=0.1,
offset=-0.05,
spatial_resolution=10.0,
unit="meters",
)
]
item.add_asset(asset_name, asset)
asset_href = f"https://example.com/data/B01.tif"
asset = pystac.Asset(href=asset_href, media_type="image/tiff", roles=["data"])
asset.extra_fields["raster:bands"] = [
dict(
nodata=0,
scale=0.1,
offset=-0.05,
spatial_resolution=10.0,
unit="meters",
)
]
item.add_asset("B01", asset)
return item


Expand All @@ -83,13 +82,12 @@ def create_raster_stac_item_v2() -> pystac.Item:
stac_extensions=["https://stac-extensions.github.io/raster/v2.0.0/schema.json"],
)

for asset_name in ["B01", "B02"]:
asset_href = f"https://example.com/data/{asset_name}.tif"
asset = pystac.Asset(href=asset_href, media_type="image/tiff", roles=["data"])
asset.extra_fields["nodata"] = 0
asset.extra_fields["raster:scale"] = 0.1
asset.extra_fields["raster:offset"] = -0.05
item.add_asset(asset_name, asset)
asset_href = f"https://example.com/data/B01.tif"
asset = pystac.Asset(href=asset_href, media_type="image/tiff", roles=["data"])
asset.extra_fields["nodata"] = 0
asset.extra_fields["raster:scale"] = 0.1
asset.extra_fields["raster:offset"] = -0.05
item.add_asset("B01", asset)
return item


Expand All @@ -98,59 +96,27 @@ class RasterTest(unittest.TestCase):
def test_apply_offset_scaling(self):
item_v1 = create_raster_stac_item_v1()
item_v2 = create_raster_stac_item_v2()
ds_v1 = xr.Dataset()
ds_v1["B01"] = xr.DataArray(
da_v1 = xr.DataArray(
data=np.array([[0, 3, 3], [1, 1, 1], [2, 2, 2]]),
dims=("y", "x"),
coords=dict(y=[5000, 5010, 5020], x=[7430, 7440, 7450]),
)
ds_v1["B02"] = xr.DataArray(
data=np.array([[3, 3, 3], [1, 1, 1], [2, 0, 2]]),
dims=("y", "x"),
coords=dict(y=[5000, 5010, 5020], x=[7430, 7440, 7450]),
)
ds_v2 = ds_v1.copy()
ds_mod_v1 = apply_offset_scaling(ds_v1, item_v1, "B01")
ds_mod_v2 = apply_offset_scaling(ds_v2, item_v2, "B01")
ds_mod_expected = xr.Dataset()
ds_mod_expected["B01"] = xr.DataArray(
da_v2 = da_v1.copy()
da_mod_v1 = apply_offset_scaling(da_v1, item_v1, "B01")
da_mod_v2 = apply_offset_scaling(da_v2, item_v2, "B01")
da_mod_expected = xr.DataArray(
data=np.array(
[[np.nan, 0.25, 0.25], [0.05, 0.05, 0.05], [0.15, 0.15, 0.15]]
),
dims=("y", "x"),
coords=dict(y=[5000, 5010, 5020], x=[7430, 7440, 7450]),
)
ds_mod_expected["B02"] = xr.DataArray(
data=np.array([[3, 3, 3], [1, 1, 1], [2, 0, 2]]),
dims=("y", "x"),
coords=dict(y=[5000, 5010, 5020], x=[7430, 7440, 7450]),
)
xr.testing.assert_allclose(ds_mod_expected, ds_mod_v1)
xr.testing.assert_allclose(ds_mod_expected, ds_mod_v2)

ds_mod2_v1 = apply_offset_scaling(ds_mod_v1, item_v1, "B02")
ds_mod2_v2 = apply_offset_scaling(ds_mod_v2, item_v2, "B02")
ds_mod_expected2 = xr.Dataset()
ds_mod_expected2["B01"] = xr.DataArray(
data=np.array(
[[np.nan, 0.25, 0.25], [0.05, 0.05, 0.05], [0.15, 0.15, 0.15]]
),
dims=("y", "x"),
coords=dict(y=[5000, 5010, 5020], x=[7430, 7440, 7450]),
)
ds_mod_expected2["B02"] = xr.DataArray(
data=np.array(
[[0.25, 0.25, 0.25], [0.05, 0.05, 0.05], [0.15, np.nan, 0.15]]
),
dims=("y", "x"),
coords=dict(y=[5000, 5010, 5020], x=[7430, 7440, 7450]),
)
xr.testing.assert_allclose(ds_mod_expected2, ds_mod2_v1)
xr.testing.assert_allclose(ds_mod_expected2, ds_mod2_v2)
xr.testing.assert_allclose(da_mod_v1, da_mod_expected)
xr.testing.assert_allclose(da_mod_v2, da_mod_expected)

with self.assertLogs("xcube.stac", level="WARNING") as cm:
ds_mod_v1 = apply_offset_scaling(ds_v1, item_v1, "B01", raster_version="v3")
xr.testing.assert_allclose(ds_v1, ds_mod_v1)
ds_mod_v1 = apply_offset_scaling(da_v1, item_v1, "B01", raster_version="v3")
xr.testing.assert_allclose(ds_mod_v1, da_v1)
self.assertEqual(1, len(cm.output))
msg = (
"WARNING:xcube.stac:Stac extension raster exists only for version 'v1' "
Expand All @@ -160,8 +126,8 @@ def test_apply_offset_scaling(self):

item_v1.stac_extensions = []
with self.assertLogs("xcube.stac", level="WARNING") as cm:
ds__mod_v1 = apply_offset_scaling(ds_v1, item_v1, asset_name="B01")
xr.testing.assert_allclose(ds_v1, ds__mod_v1)
ds_mod_v1 = apply_offset_scaling(da_v1, item_v1, asset_name="B01")
xr.testing.assert_allclose(ds_mod_v1, da_v1)
self.assertEqual(1, len(cm.output))
msg = (
"WARNING:xcube.stac:The item 'example-item' is not conform to "
Expand Down
60 changes: 0 additions & 60 deletions test/test_helper.py

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_href_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_assert_aws_s3_region_name(self):
self.assertEqual(
(
f"Region name {region_name!r} extracted from the href {href!r} "
f"is not supported by AWS S3"
"is not supported by AWS S3"
),
f"{cm.exception}",
)
102 changes: 102 additions & 0 deletions test/test_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# The MIT License (MIT)
# Copyright (c) 2024 by the xcube development team and contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import unittest

import numpy as np
import xarray as xr

from xcube_stac.stack import mosaic_take_first


class UtilsTest(unittest.TestCase):

def test_mosaic_take_first(self):
list_ds = []
# first tile
data = np.array(
[
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, np.nan], [np.nan, np.nan, np.nan]],
[[19, 20, 21], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]],
],
dtype=float,
)
dims = ("time", "lat", "lon")
coords = {
"time": np.array(
["2025-01-01", "2025-01-02", "2025-01-03"], dtype="datetime64"
),
"lat": [10.0, 20.0, 30.0],
"lon": [100.0, 110.0, 120.0],
}
da = xr.DataArray(data, dims=dims, coords=coords)
crs = xr.DataArray(np.array(0), attrs=dict(crs_wkt="testing"))
list_ds.append(xr.Dataset({"B01": da, "crs": crs}))
# second tile
data = np.array(
[
[[np.nan, np.nan, np.nan], [np.nan, np.nan, 106], [107, 108, 109]],
[[np.nan, np.nan, np.nan], [113, 114, 115], [116, 117, 118]],
[[np.nan, np.nan, 120], [121, 122, 123], [124, 125, 126]],
],
dtype=float,
)
dims = ("time", "lat", "lon")
coords = {
"time": np.array(
["2025-01-01", "2025-01-02", "2025-01-04"], dtype="datetime64"
),
"lat": [10.0, 20.0, 30.0],
"lon": [100.0, 110.0, 120.0],
}
da = xr.DataArray(data, dims=dims, coords=coords)
crs = xr.DataArray(np.array(0), attrs=dict(crs_wkt="testing"))
list_ds.append(xr.Dataset({"B01": da, "crs": crs}))

# test only one tile
dts = np.array(["2025-01-01", "2025-01-02", "2025-01-03"], dtype="datetime64")
ds_test = mosaic_take_first(list_ds[:1], dts)
xr.testing.assert_allclose(ds_test, list_ds[0])

# test two tiles
dts = np.array(
["2025-01-01", "2025-01-02", "2025-01-03", "2025-01-04"], dtype="datetime64"
)
ds_test = mosaic_take_first(list_ds, dts)
data = np.array(
[
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 115], [116, 117, 118]],
[[19, 20, 21], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]],
[[np.nan, np.nan, 120], [121, 122, 123], [124, 125, 126]],
],
dtype=float,
)
dims = ("time", "lat", "lon")
coords = {
"time": dts,
"lat": [10.0, 20.0, 30.0],
"lon": [100.0, 110.0, 120.0],
}
da = xr.DataArray(data, dims=dims, coords=coords)
crs = xr.DataArray(np.array(0), attrs=dict(crs_wkt="testing"))
ds_expected = xr.Dataset({"B01": da, "crs": crs})
xr.testing.assert_allclose(ds_test, ds_expected)
Loading

0 comments on commit da00868

Please sign in to comment.