|
6 | 6 |
|
7 | 7 |
|
8 | 8 | def rand_wx(start: str, end: str) -> xr.Dataset: |
9 | | - np.random.seed(42) |
10 | | - lat = np.linspace(-90, 90, num=720) |
11 | | - lon = np.linspace(-180, 180, num=1440) |
12 | | - time = pd.date_range(start, end, freq="h") |
13 | | - level = np.array([1000, 500], dtype=np.int32) |
14 | | - reference_time = pd.Timestamp(start) |
15 | | - temperature = 15 + 8 * np.random.randn(720, 1440, len(time), len(level)) |
16 | | - precipitation = 10 * np.random.rand(720, 1440, len(time), len(level)) |
17 | | - return xr.Dataset( |
18 | | - data_vars=dict( |
19 | | - temperature=(["lat", "lon", "time", "level"], temperature), |
20 | | - precipitation=(["lat", "lon", "time", "level"], precipitation), |
21 | | - ), |
22 | | - coords=dict( |
23 | | - lat=lat, |
24 | | - lon=lon, |
25 | | - time=time, |
26 | | - level=level, |
27 | | - reference_time=reference_time, |
28 | | - ), |
29 | | - attrs=dict(description="Random weather."), |
30 | | - ) |
| 9 | + np.random.seed(42) |
| 10 | + lat = np.linspace(-90, 90, num=720) |
| 11 | + lon = np.linspace(-180, 180, num=1440) |
| 12 | + time = pd.date_range(start, end, freq="h") |
| 13 | + level = np.array([1000, 500], dtype=np.int32) |
| 14 | + reference_time = pd.Timestamp(start) |
| 15 | + temperature = 15 + 8 * np.random.randn(720, 1440, len(time), len(level)) |
| 16 | + precipitation = 10 * np.random.rand(720, 1440, len(time), len(level)) |
| 17 | + return xr.Dataset( |
| 18 | + data_vars=dict( |
| 19 | + temperature=(["lat", "lon", "time", "level"], temperature), |
| 20 | + precipitation=(["lat", "lon", "time", "level"], precipitation), |
| 21 | + ), |
| 22 | + coords=dict( |
| 23 | + lat=lat, |
| 24 | + lon=lon, |
| 25 | + time=time, |
| 26 | + level=level, |
| 27 | + reference_time=reference_time, |
| 28 | + ), |
| 29 | + attrs=dict(description="Random weather."), |
| 30 | + ) |
31 | 31 |
|
32 | 32 |
|
33 | 33 | def create_large_dataset(time_steps=1000, lat_points=100, lon_points=100): |
34 | | - """Create a large xarray dataset for memory testing.""" |
35 | | - np.random.seed(42) |
| 34 | + """Create a large xarray dataset for memory testing.""" |
| 35 | + np.random.seed(42) |
36 | 36 |
|
37 | | - time = pd.date_range("2020-01-01", periods=time_steps, freq="h") |
38 | | - lat = np.linspace(-90, 90, lat_points) |
39 | | - lon = np.linspace(-180, 180, lon_points) |
| 37 | + time = pd.date_range("2020-01-01", periods=time_steps, freq="h") |
| 38 | + lat = np.linspace(-90, 90, lat_points) |
| 39 | + lon = np.linspace(-180, 180, lon_points) |
40 | 40 |
|
41 | | - temp_data = np.random.rand(time_steps, lat_points, lon_points) * 40 - 10 |
42 | | - precip_data = np.random.rand(time_steps, lat_points, lon_points) * 100 |
| 41 | + temp_data = np.random.rand(time_steps, lat_points, lon_points) * 40 - 10 |
| 42 | + precip_data = np.random.rand(time_steps, lat_points, lon_points) * 100 |
43 | 43 |
|
44 | | - return xr.Dataset( |
45 | | - { |
46 | | - "temperature": (["time", "lat", "lon"], temp_data), |
47 | | - "precipitation": (["time", "lat", "lon"], precip_data), |
48 | | - }, |
49 | | - coords={"time": time, "lat": lat, "lon": lon}, |
50 | | - ) |
| 44 | + return xr.Dataset( |
| 45 | + { |
| 46 | + "temperature": (["time", "lat", "lon"], temp_data), |
| 47 | + "precipitation": (["time", "lat", "lon"], precip_data), |
| 48 | + }, |
| 49 | + coords={"time": time, "lat": lat, "lon": lon}, |
| 50 | + ) |
51 | 51 |
|
52 | 52 |
|
53 | 53 | @pytest.fixture |
54 | 54 | def air(): |
55 | | - ds = xr.tutorial.open_dataset("air_temperature") |
56 | | - chunks = {"time": 240} |
57 | | - return ds.chunk(chunks) |
| 55 | + ds = xr.tutorial.open_dataset("air_temperature") |
| 56 | + chunks = {"time": 240} |
| 57 | + return ds.chunk(chunks) |
58 | 58 |
|
59 | 59 |
|
60 | 60 | @pytest.fixture |
61 | 61 | def air_small(air): |
62 | | - return air.isel(time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)).chunk( |
63 | | - {"time": 240} |
64 | | - ) |
| 62 | + return air.isel( |
| 63 | + time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10) |
| 64 | + ).chunk({"time": 240}) |
65 | 65 |
|
66 | 66 |
|
67 | 67 | @pytest.fixture |
68 | 68 | def randwx(): |
69 | | - return rand_wx("1995-01-13T00", "1995-01-13T01") |
| 69 | + return rand_wx("1995-01-13T00", "1995-01-13T01") |
70 | 70 |
|
71 | 71 |
|
72 | 72 | @pytest.fixture |
73 | 73 | def large_ds(): |
74 | | - return create_large_dataset().chunk({"time": 25}) |
| 74 | + return create_large_dataset().chunk({"time": 25}) |
75 | 75 |
|
76 | 76 |
|
77 | 77 | @pytest.fixture |
78 | 78 | def air_dataset_small(): |
79 | | - ds = xr.tutorial.open_dataset("air_temperature").chunk({"time": 240}) |
80 | | - return ds.isel(time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)) |
| 79 | + ds = xr.tutorial.open_dataset("air_temperature").chunk({"time": 240}) |
| 80 | + return ds.isel(time=slice(0, 12), lat=slice(0, 11), lon=slice(0, 10)) |
81 | 81 |
|
82 | 82 |
|
83 | 83 | @pytest.fixture |
84 | 84 | def air_dataset_large(): |
85 | | - return xr.tutorial.open_dataset("air_temperature").chunk({"time": 240}) |
| 85 | + return xr.tutorial.open_dataset("air_temperature").chunk({"time": 240}) |
86 | 86 |
|
87 | 87 |
|
88 | 88 | @pytest.fixture |
89 | 89 | def rasm_ds(): |
90 | | - """rasm uses cftime.DatetimeNoLeap (noleap / 365_day) for time.""" |
91 | | - return xr.tutorial.open_dataset("rasm") |
| 90 | + """rasm uses cftime.DatetimeNoLeap (noleap / 365_day) for time.""" |
| 91 | + return xr.tutorial.open_dataset("rasm") |
92 | 92 |
|
93 | 93 |
|
94 | 94 | @pytest.fixture |
95 | 95 | def weather_dataset(): |
96 | | - ds = rand_wx("2023-01-01T00", "2023-01-01T12") |
97 | | - return ds.isel(time=slice(0, 6), lat=slice(0, 10), lon=slice(0, 10)).chunk( |
98 | | - {"time": 3} |
99 | | - ) |
| 96 | + ds = rand_wx("2023-01-01T00", "2023-01-01T12") |
| 97 | + return ds.isel(time=slice(0, 6), lat=slice(0, 10), lon=slice(0, 10)).chunk( |
| 98 | + {"time": 3} |
| 99 | + ) |
100 | 100 |
|
101 | 101 |
|
102 | 102 | @pytest.fixture |
103 | 103 | def synthetic_dataset(): |
104 | | - return create_large_dataset( |
105 | | - time_steps=50, lat_points=20, lon_points=20 |
106 | | - ).chunk({"time": 25}) |
| 104 | + return create_large_dataset( |
| 105 | + time_steps=50, lat_points=20, lon_points=20 |
| 106 | + ).chunk({"time": 25}) |
107 | 107 |
|
108 | 108 |
|
109 | 109 | @pytest.fixture |
110 | 110 | def station_dataset(): |
111 | | - return xr.Dataset( |
112 | | - { |
113 | | - "station_id": (["station"], [1, 2, 3, 4, 5]), |
114 | | - "elevation": (["station"], [100, 250, 500, 750, 1000]), |
115 | | - "name": ( |
116 | | - ["station"], |
117 | | - ["Station_A", "Station_B", "Station_C", "Station_D", "Station_E"], |
118 | | - ), |
119 | | - } |
120 | | - ).chunk({"station": 5}) |
| 111 | + return xr.Dataset( |
| 112 | + { |
| 113 | + "station_id": (["station"], [1, 2, 3, 4, 5]), |
| 114 | + "elevation": (["station"], [100, 250, 500, 750, 1000]), |
| 115 | + "name": ( |
| 116 | + ["station"], |
| 117 | + [ |
| 118 | + "Station_A", |
| 119 | + "Station_B", |
| 120 | + "Station_C", |
| 121 | + "Station_D", |
| 122 | + "Station_E", |
| 123 | + ], |
| 124 | + ), |
| 125 | + } |
| 126 | + ).chunk({"station": 5}) |
121 | 127 |
|
122 | 128 |
|
123 | 129 | @pytest.fixture |
124 | 130 | def air_and_stations(): |
125 | | - air = ( |
126 | | - xr.tutorial.open_dataset("air_temperature") |
127 | | - .isel(time=slice(0, 12), lat=slice(0, 5), lon=slice(0, 8)) |
128 | | - .chunk({"time": 6}) |
129 | | - ) |
130 | | - stations = xr.Dataset( |
131 | | - { |
132 | | - "station_id": (["station"], [101, 102, 103]), |
133 | | - "lat": ( |
134 | | - ["station"], |
135 | | - [air.lat.values[0], air.lat.values[2], air.lat.values[4]], |
136 | | - ), |
137 | | - "lon": ( |
138 | | - ["station"], |
139 | | - [air.lon.values[1], air.lon.values[3], air.lon.values[5]], |
140 | | - ), |
141 | | - "elevation": (["station"], [100, 250, 500]), |
142 | | - } |
143 | | - ).chunk({"station": 3}) |
144 | | - return air, stations |
| 131 | + air = ( |
| 132 | + xr.tutorial.open_dataset("air_temperature") |
| 133 | + .isel(time=slice(0, 12), lat=slice(0, 5), lon=slice(0, 8)) |
| 134 | + .chunk({"time": 6}) |
| 135 | + ) |
| 136 | + stations = xr.Dataset( |
| 137 | + { |
| 138 | + "station_id": (["station"], [101, 102, 103]), |
| 139 | + "lat": ( |
| 140 | + ["station"], |
| 141 | + [air.lat.values[0], air.lat.values[2], air.lat.values[4]], |
| 142 | + ), |
| 143 | + "lon": ( |
| 144 | + ["station"], |
| 145 | + [air.lon.values[1], air.lon.values[3], air.lon.values[5]], |
| 146 | + ), |
| 147 | + "elevation": (["station"], [100, 250, 500]), |
| 148 | + } |
| 149 | + ).chunk({"station": 3}) |
| 150 | + return air, stations |
0 commit comments