|
| 1 | +import xarray as xr |
| 2 | +import cf_xarray as cfxr |
| 3 | + |
| 4 | + |
| 5 | +def print_section(title): |
| 6 | + print(f"\n{title}") |
| 7 | + print("=" * len(title)) |
| 8 | + |
| 9 | + |
| 10 | +def print_bounds_and_vertices(bounds, vertices): |
| 11 | + print("Bounds:") |
| 12 | + print(bounds) |
| 13 | + print("Vertices:") |
| 14 | + print(vertices) |
| 15 | + print("-" * 40) |
| 16 | + |
| 17 | + |
| 18 | +# 0a. Strictly monotonic bounds (increasing) |
| 19 | +print_section("0a. Strictly monotonic bounds (increasing)") |
| 20 | +bounds = xr.DataArray( |
| 21 | + [[10.0, 10.5], [10.5, 11.0], [11.0, 11.5], [11.5, 12.0], [12.0, 12.5]], |
| 22 | + dims=("lat", "bounds"), |
| 23 | +) |
| 24 | +vertices = cfxr.bounds_to_vertices(bounds, "bounds") |
| 25 | +print_bounds_and_vertices(bounds, vertices) |
| 26 | + |
| 27 | +# 0b. Strictly monotonic bounds (decreasing) |
| 28 | +print_section("0b. Strictly monotonic bounds (decreasing)") |
| 29 | +bounds = xr.DataArray( |
| 30 | + [[12.5, 12.0], [12.0, 11.5], [11.5, 11.0], [11.0, 10.5], [10.5, 10.0]], |
| 31 | + dims=("lat", "bounds"), |
| 32 | +) |
| 33 | +vertices = cfxr.bounds_to_vertices(bounds, "bounds") |
| 34 | +print_bounds_and_vertices(bounds, vertices) |
| 35 | + |
| 36 | +# 1. Descending coordinates |
| 37 | +print_section("1. Descending coordinates") |
| 38 | +bounds = xr.DataArray( |
| 39 | + [[50.5, 50.0], [51.0, 50.5], [51.5, 51.0], [52.0, 51.5], [52.5, 52.0]], |
| 40 | + dims=("lat", "bounds"), |
| 41 | +) |
| 42 | +vertices = cfxr.bounds_to_vertices(bounds, "bounds") |
| 43 | +print_bounds_and_vertices(bounds, vertices) |
| 44 | + |
| 45 | +# 2. Descending coordinates with duplicated values |
| 46 | +print_section("2. Descending coordinates with duplicated values") |
| 47 | +bounds = xr.DataArray( |
| 48 | + [[50.5, 50.0], [51.0, 50.5], [51.0, 50.5], [52.0, 51.5], [52.5, 52.0]], |
| 49 | + dims=("lat", "bounds"), |
| 50 | +) |
| 51 | +vertices = cfxr.bounds_to_vertices(bounds, "bounds") |
| 52 | +print_bounds_and_vertices(bounds, vertices) |
| 53 | + |
| 54 | +# 3. Ascending coordinates |
| 55 | +print_section("3. Ascending coordinates") |
| 56 | +bounds = xr.DataArray( |
| 57 | + [[50.0, 50.5], [50.5, 51.0], [51.0, 51.5], [51.5, 52.0], [52.0, 52.5]], |
| 58 | + dims=("lat", "bounds"), |
| 59 | +) |
| 60 | +vertices = cfxr.bounds_to_vertices(bounds, "bounds") |
| 61 | +print_bounds_and_vertices(bounds, vertices) |
| 62 | + |
| 63 | +# 4. Ascending coordinates with duplicated values |
| 64 | +print_section("4. Ascending coordinates with duplicated values") |
| 65 | +bounds = xr.DataArray( |
| 66 | + [[50.0, 50.5], [50.5, 51.0], [50.5, 51.0], [51.0, 51.5], [51.5, 52.0]], |
| 67 | + dims=("lat", "bounds"), |
| 68 | +) |
| 69 | +vertices = cfxr.bounds_to_vertices(bounds, "bounds") |
| 70 | +print_bounds_and_vertices(bounds, vertices) |
| 71 | + |
| 72 | +# 5. 3D array (extra non-core dim) |
| 73 | +print_section("5. 3D array (extra non-core dim)") |
| 74 | +bounds_3d = xr.DataArray( |
| 75 | + [ |
| 76 | + [ |
| 77 | + [50.0, 50.5], |
| 78 | + [50.5, 51.0], |
| 79 | + [51.0, 51.5], |
| 80 | + [51.5, 52.0], |
| 81 | + [52.0, 52.5], |
| 82 | + ], |
| 83 | + [ |
| 84 | + [60.0, 60.5], |
| 85 | + [60.5, 61.0], |
| 86 | + [61.0, 61.5], |
| 87 | + [61.5, 62.0], |
| 88 | + [62.0, 62.5], |
| 89 | + ], |
| 90 | + ], |
| 91 | + dims=("extra", "lat", "bounds"), |
| 92 | +) |
| 93 | +vertices_3d = cfxr.bounds_to_vertices(bounds_3d, "bounds", core_dims=["lat"]) |
| 94 | +print_bounds_and_vertices(bounds_3d, vertices_3d) |
| 95 | + |
| 96 | +# 6. 4D array (time, extra, lat, bounds) |
| 97 | +print_section("6. 4D array (time, extra, lat, bounds)") |
| 98 | +bounds_4d = xr.DataArray( |
| 99 | + [ |
| 100 | + [ |
| 101 | + [ |
| 102 | + [50.0, 50.5], |
| 103 | + [50.5, 51.0], |
| 104 | + [51.0, 51.5], |
| 105 | + [51.5, 52.0], |
| 106 | + [52.0, 52.5], |
| 107 | + ], |
| 108 | + [ |
| 109 | + [60.0, 60.5], |
| 110 | + [60.5, 61.0], |
| 111 | + [61.0, 61.5], |
| 112 | + [61.5, 62.0], |
| 113 | + [62.0, 62.5], |
| 114 | + ], |
| 115 | + ], |
| 116 | + [ |
| 117 | + [ |
| 118 | + [70.0, 70.5], |
| 119 | + [70.5, 71.0], |
| 120 | + [71.0, 71.5], |
| 121 | + [71.5, 72.0], |
| 122 | + [72.0, 72.5], |
| 123 | + ], |
| 124 | + [ |
| 125 | + [80.0, 80.5], |
| 126 | + [80.5, 81.0], |
| 127 | + [81.0, 81.5], |
| 128 | + [81.5, 82.0], |
| 129 | + [82.0, 82.5], |
| 130 | + ], |
| 131 | + ], |
| 132 | + ], |
| 133 | + dims=("time", "extra", "lat", "bounds"), |
| 134 | +) |
| 135 | +vertices_4d = cfxr.bounds_to_vertices(bounds_4d, "bounds", core_dims=["lat"]) |
| 136 | +print_bounds_and_vertices(bounds_4d, vertices_4d) |
0 commit comments