|
| 1 | +import pytest |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | + |
| 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 | + ) |
| 31 | + |
| 32 | + |
| 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) |
| 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) |
| 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 |
| 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 | + ) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def air(): |
| 55 | + ds = xr.tutorial.open_dataset("air_temperature") |
| 56 | + chunks = {"time": 240} |
| 57 | + return ds.chunk(chunks) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 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 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def randwx(): |
| 69 | + return rand_wx("1995-01-13T00", "1995-01-13T01") |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def large_ds(): |
| 74 | + return create_large_dataset().chunk({"time": 25}) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 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)) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def air_dataset_large(): |
| 85 | + return xr.tutorial.open_dataset("air_temperature").chunk({"time": 240}) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def weather_dataset(): |
| 90 | + ds = rand_wx("2023-01-01T00", "2023-01-01T12") |
| 91 | + return ds.isel(time=slice(0, 6), lat=slice(0, 10), lon=slice(0, 10)).chunk( |
| 92 | + {"time": 3} |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture |
| 97 | +def synthetic_dataset(): |
| 98 | + return create_large_dataset( |
| 99 | + time_steps=50, lat_points=20, lon_points=20 |
| 100 | + ).chunk({"time": 25}) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture |
| 104 | +def station_dataset(): |
| 105 | + return xr.Dataset( |
| 106 | + { |
| 107 | + "station_id": (["station"], [1, 2, 3, 4, 5]), |
| 108 | + "elevation": (["station"], [100, 250, 500, 750, 1000]), |
| 109 | + "name": ( |
| 110 | + ["station"], |
| 111 | + ["Station_A", "Station_B", "Station_C", "Station_D", "Station_E"], |
| 112 | + ), |
| 113 | + } |
| 114 | + ).chunk({"station": 5}) |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +def air_and_stations(): |
| 119 | + air = ( |
| 120 | + xr.tutorial.open_dataset("air_temperature") |
| 121 | + .isel(time=slice(0, 12), lat=slice(0, 5), lon=slice(0, 8)) |
| 122 | + .chunk({"time": 6}) |
| 123 | + ) |
| 124 | + stations = xr.Dataset( |
| 125 | + { |
| 126 | + "station_id": (["station"], [101, 102, 103]), |
| 127 | + "lat": ( |
| 128 | + ["station"], |
| 129 | + [air.lat.values[0], air.lat.values[2], air.lat.values[4]], |
| 130 | + ), |
| 131 | + "lon": ( |
| 132 | + ["station"], |
| 133 | + [air.lon.values[1], air.lon.values[3], air.lon.values[5]], |
| 134 | + ), |
| 135 | + "elevation": (["station"], [100, 250, 500]), |
| 136 | + } |
| 137 | + ).chunk({"station": 3}) |
| 138 | + return air, stations |
0 commit comments