Skip to content

Commit

Permalink
add tests for area and area density layout
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianHofmann committed Oct 24, 2023
1 parent 831f37a commit f595422
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
11 changes: 6 additions & 5 deletions atlite/cutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,15 @@ def area(self, crs=3035):
Parameters
----------
crs : int, optional
CRS to calculate the total area of grid cells in m^2.
The default is 3035 which is suitable for the European area.
The coordinate reference system (CRS) to use for the calculation.
The default value is 3035, which is a suitable projection for Europe and returns the area in square meters.
Returns
-------
None.
xr.DataArray
A DataArray containing the area per grid cell with coordinates (x,y).
"""
area = self.grid.to_crs(crs).area.div(1e6)
area = self.grid.to_crs(crs).area
return xr.DataArray(
area.values.reshape(self.shape),
[self.coords["y"], self.coords["x"]],
Expand Down Expand Up @@ -660,7 +661,7 @@ def layout_from_area_density(self, capacity_density, crs=3035):
capacity placed within one grid cell.
"""

return capacity_density * self.area(crs)
return capacity_density * self.area(crs) / 1e6

availabilitymatrix = compute_availabilitymatrix

Expand Down
8 changes: 8 additions & 0 deletions test/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ def test_sel(ref):
cutout = ref.sel(x=slice(X0 + 2, X1 - 1), y=slice(Y0 + 1, Y1 - 2))
assert cutout.coords["x"][0] - ref.coords["x"][0] == 2
assert cutout.coords["y"][-1] - ref.coords["y"][-1] == -2


def test_layout_from_area_density(ref):
density = 0.1
layout = ref.layout_from_area_density(density)
assert layout.dims == ("y", "x")
assert layout.shape == (ref.data.y.size, ref.data.x.size)
assert ref.area().sum() * density / 1e6 == layout.sum()
15 changes: 15 additions & 0 deletions test/test_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ def test_open_closed_checks(ref, geometry, raster):
assert not excluder.all_closed and excluder.all_open


def test_area(ref):
"""
Test the area of the cutout.
"""
area = ref.area()
assert isinstance(area, xr.DataArray)
assert area.dims == ("y", "x")

# now test with a different crs
with pytest.warns(UserWarning):
assert ref.area(crs=ref.crs).sum().item() == (X1 - X0 + ref.dx) * (
Y1 - Y0 + ref.dy
)


def test_transform():
"""
Test the affine transform.
Expand Down

0 comments on commit f595422

Please sign in to comment.