Skip to content

Commit e4ee966

Browse files
authored
Sample dataarray (#53)
* add sample dataarray * add changelog
1 parent ac7fd12 commit e4ee966

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mplotutils now uses the MIT license instead of GPL-3.0 ([#51](https://github.com
1717
* Allow passing list of axes to ``set_map_layout``, renamed the files and extended
1818
the test coverage ([#42](https://github.com/mathause/mplotutils/pull/42)
1919
and [#43](https://github.com/mathause/mplotutils/pull/43)).
20+
* Add function to create `xr.DataArray` sample data ([#53](https://github.com/mathause/mplotutils/pull/53)).
2021

2122
### Bug fixes
2223

mplotutils/cartopy_utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@
1414
def sample_data_map(nlons, nlats):
1515
"""Returns `lons`, `lats`, and fake `data`
1616
17+
Parameters
18+
----------
19+
nlons : int
20+
Number of longitude grid cells.
21+
nlats : int
22+
Number of latitude grid cells.
23+
24+
Returns
25+
-------
26+
lon : ndarray
27+
Array of the longitude coordiantes.
28+
lat : ndarray
29+
Array of the latitude coordiantes.
30+
data : ndarray
31+
Sample data.
32+
33+
Notes
34+
-----
1735
adapted from:
1836
http://scitools.org.uk/cartopy/docs/v0.15/examples/axes_grid_basic.html
1937
"""
@@ -32,6 +50,34 @@ def sample_data_map(nlons, nlats):
3250
return lon, lat, data
3351

3452

53+
def sample_dataarray(nlon, nlat):
54+
"""Returns `lons`, `lats`, and fake `data`
55+
56+
Parameters
57+
----------
58+
nlons : int
59+
Number of longitude grid cells.
60+
nlats : int
61+
Number of latitude grid cells.
62+
63+
Returns
64+
-------
65+
data : xr.DataArray
66+
Sample data with coordiantes.
67+
68+
Notes
69+
-----
70+
adapted from:
71+
http://scitools.org.uk/cartopy/docs/v0.15/examples/axes_grid_basic.html
72+
"""
73+
74+
import xarray as xr
75+
76+
lon, lat, data = sample_data_map(nlons=nlon, nlats=nlat)
77+
78+
return xr.DataArray(data, dims=("lat", "lon"), coords={"lon": lon, "lat": lat})
79+
80+
3581
def cyclic_dataarray(obj, coord="lon"):
3682
"""Add a cyclic coordinate point to a DataArray or Dataset along a dimension.
3783

mplotutils/tests/test_sample_data_map.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import numpy as np
2+
import pytest
3+
import xarray as xr
24

3-
from mplotutils import sample_data_map
5+
from mplotutils import sample_data_map, sample_dataarray
46

57

6-
def test_data_shape():
7-
8-
nlons = 10
9-
nlats = 20
10-
11-
lon, lat, data = sample_data_map(nlons, nlats)
12-
13-
assert len(lon) == nlons
14-
assert len(lat) == nlats
15-
16-
assert data.shape == (nlats, nlons)
17-
18-
nlons = 5
19-
nlats = 10
8+
@pytest.mark.parametrize("nlons", [5, 10])
9+
@pytest.mark.parametrize("nlats", [10, 20])
10+
def test_data_shape(nlons, nlats):
2011

2112
lon, lat, data = sample_data_map(nlons, nlats)
2213

@@ -40,3 +31,13 @@ def test_lon():
4031

4132
expected_lon = np.arange(0, 351, 10)
4233
assert np.allclose(lon, expected_lon)
34+
35+
36+
@pytest.mark.parametrize("nlon", [5, 10])
37+
@pytest.mark.parametrize("nlat", [10, 20])
38+
def test_sample_dataarray(nlon, nlat):
39+
40+
data = sample_dataarray(nlon, nlat)
41+
42+
assert isinstance(data, xr.DataArray)
43+
assert data.shape == (nlat, nlon)

0 commit comments

Comments
 (0)