Skip to content

Commit 1f994fd

Browse files
finozzifaFabrizio Finozzilkstrp
authored
Additional unit tests (#406)
* code: unit tests for name, module and crs * code: fix unit test on name * code: add unit test on shape and extent * code:add unit tests for extent, bounds, transform and transform_r * code: reformat code * code: restore cutout.py * code: override __eq__ method * code: add equals method * code: add unit test for equals method * Update atlite/cutout.py * Update atlite/cutout.py * Update atlite/cutout.py --------- Co-authored-by: Fabrizio Finozzi <[email protected]> Co-authored-by: Lukas Trippe <[email protected]>
1 parent cf3a07b commit 1f994fd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

atlite/cutout.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,15 @@ def uniform_density_layout(self, capacity_density, crs=None):
576576
"""
577577
return capacity_density * self.area(crs)
578578

579+
def equals(self, other):
580+
"""
581+
It overrides xarray.Dataset.equals and ignores the path attribute in the comparison
582+
"""
583+
if not isinstance(other, Cutout):
584+
return NotImplemented
585+
# Compare cutouts data attributes
586+
return self.data.equals(other.data)
587+
579588
def layout_from_capacity_list(self, data, col="Capacity"):
580589
"""
581590
Get a capacity layout aligned to the cutout based on a capacity list.

test/test_creation.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import numpy as np
1515
import pytest
16+
import rasterio as rio
1617
from xarray.testing import assert_equal
1718

1819
from atlite import Cutout
@@ -31,6 +32,53 @@ def ref():
3132
)
3233

3334

35+
def test_name(ref):
36+
assert ref.name == "creation_ref"
37+
38+
39+
def test_module(ref):
40+
assert ref.module == "era5"
41+
42+
43+
def test_crs(ref):
44+
assert ref.crs == "EPSG:4326"
45+
46+
47+
def test_shape(ref):
48+
assert ref.shape == (21, 23)
49+
50+
51+
def test_extent(ref):
52+
reference_extent = [-4.125, 1.625, 55.875, 61.125]
53+
assert all([x == y for x, y in zip(ref.extent, reference_extent)])
54+
55+
56+
def test_bounds(ref):
57+
reference_extent = [-4.125, 55.875, 1.625, 61.125]
58+
assert all([x == y for x, y in zip(ref.bounds, reference_extent)])
59+
60+
61+
def test_transform(ref):
62+
reference_affine = rio.Affine(
63+
0.25, 0.00, -4.125, 0.00, 0.25, 55.875, 0.00, 0.00, 1.00
64+
)
65+
assert reference_affine == ref.transform
66+
67+
68+
def test_equals(ref):
69+
test_cutout = Cutout(
70+
path="creation_ref", module="era5", bounds=(X0, Y0, X1, Y1), time=TIME
71+
)
72+
assert ref.equals(test_cutout)
73+
74+
75+
def test_transform_r(ref):
76+
reference_affine = rio.Affine(
77+
0.25, 0.00, -4.125, 0.00, -0.25, 61.125, 0.00, 0.00, 1.00
78+
)
79+
assert reference_affine == ref.transform_r
80+
81+
3482
def test_odd_bounds_coords(ref):
3583
cutout = Cutout(
3684
path="odd_bounds",

0 commit comments

Comments
 (0)