|
| 1 | +"""Datasets focussing on grid geometry""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import xarray as xr |
| 5 | + |
| 6 | +N = 30 |
| 7 | +T = 10 |
| 8 | + |
| 9 | + |
| 10 | +def rotated_curvilinear_grid(): |
| 11 | + XG = np.arange(N) |
| 12 | + YG = np.arange(2 * N) |
| 13 | + LON, LAT = np.meshgrid(XG, YG) |
| 14 | + |
| 15 | + angle = -np.pi / 24 |
| 16 | + rotation = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]) |
| 17 | + |
| 18 | + # rotate the LON and LAT grids |
| 19 | + LON, LAT = np.einsum("ji, mni -> jmn", rotation, np.dstack([LON, LAT])) |
| 20 | + |
| 21 | + return xr.Dataset( |
| 22 | + { |
| 23 | + "data_g": (["ZG", "YG", "XG"], np.random.rand(3 * N, 2 * N, N)), |
| 24 | + "data_c": (["ZC", "YC", "XC"], np.random.rand(3 * N, 2 * N, N)), |
| 25 | + }, |
| 26 | + coords={ |
| 27 | + "XG": (["XG"], XG, {"axis": "X", "c_grid_axis_shift": -0.5}), |
| 28 | + "YG": (["YG"], YG, {"axis": "Y", "c_grid_axis_shift": -0.5}), |
| 29 | + "XC": (["XC"], XG + 0.5, {"axis": "X"}), |
| 30 | + "YC": (["YC"], YG + 0.5, {"axis": "Y"}), |
| 31 | + "ZG": ( |
| 32 | + ["ZG"], |
| 33 | + np.arange(3 * N), |
| 34 | + {"axis": "Z", "c_grid_axis_shift": -0.5}, |
| 35 | + ), |
| 36 | + "ZC": ( |
| 37 | + ["ZC"], |
| 38 | + np.arange(3 * N) + 0.5, |
| 39 | + {"axis": "Z"}, |
| 40 | + ), |
| 41 | + "depth": (["ZG"], np.arange(3 * N), {"axis": "Z"}), |
| 42 | + "time": (["time"], np.arange(T), {"axis": "T"}), |
| 43 | + "lon": ( |
| 44 | + ["YG", "XG"], |
| 45 | + LON, |
| 46 | + {"axis": "X", "c_grid_axis_shift": -0.5}, # ? Needed? |
| 47 | + ), |
| 48 | + "lat": ( |
| 49 | + ["YG", "XG"], |
| 50 | + LAT, |
| 51 | + {"axis": "Y", "c_grid_axis_shift": -0.5}, # ? Needed? |
| 52 | + ), |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _cartesion_to_polar(x, y): |
| 58 | + r = np.sqrt(x**2 + y**2) |
| 59 | + theta = np.arctan2(y, x) |
| 60 | + return r, theta |
| 61 | + |
| 62 | + |
| 63 | +def _polar_to_cartesian(r, theta): |
| 64 | + x = r * np.cos(theta) |
| 65 | + y = r * np.sin(theta) |
| 66 | + return x, y |
| 67 | + |
| 68 | + |
| 69 | +def unrolled_cone_curvilinear_grid(): |
| 70 | + # Not a great unrolled cone, but this is good enough for testing |
| 71 | + # you can use matplotlib pcolormesh to plot |
| 72 | + XG = np.arange(N) |
| 73 | + YG = np.arange(2 * N) * 0.25 |
| 74 | + |
| 75 | + pivot = -10, 0 |
| 76 | + LON, LAT = np.meshgrid(XG, YG) |
| 77 | + |
| 78 | + new_lon_lat = [] |
| 79 | + |
| 80 | + min_lon = np.min(XG) |
| 81 | + for lon, lat in zip(LON.flatten(), LAT.flatten(), strict=True): |
| 82 | + r, _ = _cartesion_to_polar(lon - pivot[0], lat - pivot[1]) |
| 83 | + _, theta = _cartesion_to_polar(min_lon - pivot[0], lat - pivot[1]) |
| 84 | + theta *= 1.2 |
| 85 | + r *= 1.2 |
| 86 | + lon, lat = _polar_to_cartesian(r, theta) |
| 87 | + new_lon_lat.append((lon + pivot[0], lat + pivot[1])) |
| 88 | + |
| 89 | + new_lon, new_lat = zip(*new_lon_lat, strict=True) |
| 90 | + LON, LAT = np.array(new_lon).reshape(LON.shape), np.array(new_lat).reshape(LAT.shape) |
| 91 | + |
| 92 | + return xr.Dataset( |
| 93 | + { |
| 94 | + "data_g": (["ZG", "YG", "XG"], np.random.rand(3 * N, 2 * N, N)), |
| 95 | + "data_c": (["ZC", "YC", "XC"], np.random.rand(3 * N, 2 * N, N)), |
| 96 | + }, |
| 97 | + coords={ |
| 98 | + "XG": (["XG"], XG, {"axis": "X", "c_grid_axis_shift": -0.5}), |
| 99 | + "YG": (["YG"], YG, {"axis": "Y", "c_grid_axis_shift": -0.5}), |
| 100 | + "XC": (["XC"], XG + 0.5, {"axis": "X"}), |
| 101 | + "YC": (["YC"], YG + 0.5, {"axis": "Y"}), |
| 102 | + "ZG": ( |
| 103 | + ["ZG"], |
| 104 | + np.arange(3 * N), |
| 105 | + {"axis": "Z", "c_grid_axis_shift": -0.5}, |
| 106 | + ), |
| 107 | + "ZC": ( |
| 108 | + ["ZC"], |
| 109 | + np.arange(3 * N) + 0.5, |
| 110 | + {"axis": "Z"}, |
| 111 | + ), |
| 112 | + "depth": (["ZG"], np.arange(3 * N), {"axis": "Z"}), |
| 113 | + "time": (["time"], np.arange(T), {"axis": "T"}), |
| 114 | + "lon": ( |
| 115 | + ["YG", "XG"], |
| 116 | + LON, |
| 117 | + {"axis": "X", "c_grid_axis_shift": -0.5}, # ? Needed? |
| 118 | + ), |
| 119 | + "lat": ( |
| 120 | + ["YG", "XG"], |
| 121 | + LAT, |
| 122 | + {"axis": "Y", "c_grid_axis_shift": -0.5}, # ? Needed? |
| 123 | + ), |
| 124 | + }, |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +datasets = { |
| 129 | + "2d_left_rotated": rotated_curvilinear_grid(), |
| 130 | + "ds_2d_left": xr.Dataset( |
| 131 | + { |
| 132 | + "data_g": (["time", "ZG", "YG", "XG"], np.random.rand(T, 3 * N, 2 * N, N)), |
| 133 | + "data_c": (["time", "ZC", "YC", "XC"], np.random.rand(T, 3 * N, 2 * N, N)), |
| 134 | + }, |
| 135 | + coords={ |
| 136 | + "XG": ( |
| 137 | + ["XG"], |
| 138 | + 2 * np.pi / N * np.arange(0, N), |
| 139 | + {"axis": "X", "c_grid_axis_shift": -0.5}, |
| 140 | + ), |
| 141 | + "XC": (["XC"], 2 * np.pi / N * (np.arange(0, N) + 0.5), {"axis": "X"}), |
| 142 | + "YG": ( |
| 143 | + ["YG"], |
| 144 | + 2 * np.pi / (2 * N) * np.arange(0, 2 * N), |
| 145 | + {"axis": "Y", "c_grid_axis_shift": -0.5}, |
| 146 | + ), |
| 147 | + "YC": ( |
| 148 | + ["YC"], |
| 149 | + 2 * np.pi / (2 * N) * (np.arange(0, 2 * N) + 0.5), |
| 150 | + {"axis": "Y"}, |
| 151 | + ), |
| 152 | + "ZG": ( |
| 153 | + ["ZG"], |
| 154 | + np.arange(3 * N), |
| 155 | + {"axis": "Z", "c_grid_axis_shift": -0.5}, |
| 156 | + ), |
| 157 | + "ZC": ( |
| 158 | + ["ZC"], |
| 159 | + np.arange(3 * N) + 0.5, |
| 160 | + {"axis": "Z"}, |
| 161 | + ), |
| 162 | + "lon": (["XG"], 2 * np.pi / N * np.arange(0, N)), |
| 163 | + "lat": (["YG"], 2 * np.pi / (2 * N) * np.arange(0, 2 * N)), |
| 164 | + "depth": (["ZG"], np.arange(3 * N)), |
| 165 | + "time": (["time"], np.arange(T), {"axis": "T"}), |
| 166 | + }, |
| 167 | + ), |
| 168 | + "2d_left_unrolled_cone": unrolled_cone_curvilinear_grid(), |
| 169 | +} |
0 commit comments