|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """Surface and global-atmospheric temperatures on 2020-01-01, in SQL. |
3 | 3 |
|
4 | | -Two queries against ARCO-ERA5 on the morning of January 1, 2020: |
| 4 | +Three queries against ARCO-ERA5 on the morning of January 1, 2020: |
5 | 5 |
|
6 | 6 | * **Surface (local).** Average 2m-temperature over a small grid covering the |
7 | 7 | New York City area for the first six hours. |
8 | 8 | * **Atmosphere (global).** Average temperature per pressure level, computed |
9 | 9 | over the entire planet for the same six hours — a classic atmospheric |
10 | 10 | temperature profile (surface around 1000 hPa is warmest, tropopause near |
11 | 11 | 100 hPa is coldest). |
| 12 | + * **Surface (global, gridded).** Average 2m-temperature per (lat, lon) cell |
| 13 | + for the same six hours, returned as an xarray Dataset. |
12 | 14 |
|
13 | | -Both queries express their filters entirely in SQL: ``xr.open_zarr`` is given |
14 | | -a single calendar year and no spatial slicing. The library's table provider |
15 | | -prunes time partitions for ``WHERE time …`` filters, and pushes ``WHERE |
| 15 | +All filters live in SQL: the dataset is opened with no time or spatial |
| 16 | +slicing on the xarray side. The library's table provider prunes time |
| 17 | +partitions for ``WHERE time …`` filters, and pushes ``WHERE |
16 | 18 | latitude/longitude …`` down to dimension columns. |
17 | 19 |
|
18 | 20 | ARCO-ERA5's atmospheric variables are stored in native Zarr chunks of shape |
19 | 21 | ``(1, 37, 721, 1440)`` — about 150 MB per hour. We align Dask chunks to that |
20 | | -shape with ``chunks={'time': 1}`` so chunks fetch from GCS concurrently. The |
21 | | -global atmospheric query scans ~230M rows after pruning. |
| 22 | +shape with ``chunks=dict(time=1)`` so chunks fetch from GCS concurrently. |
22 | 23 |
|
23 | 24 | The Zarr is read anonymously from the public GCS bucket — no auth required. |
24 | 25 | """ |
|
34 | 35 |
|
35 | 36 |
|
36 | 37 | def main() -> None: |
37 | | - full = xr.open_zarr(URL, chunks=None, storage_options={"token": "anon"}) |
38 | | - |
39 | | - # Open a full calendar year — all 273 variables. No spatial slicing on |
40 | | - # the xarray side; SQL WHERE clauses below express the filters. |
41 | | - # |
42 | | - # Heads up: the library pushes column projection down to Zarr, so SELECT |
43 | | - # only fetches what you ask for — but `SELECT * FROM era5.surface` would |
44 | | - # try to read every variable across the year (terabytes from GCS). |
45 | | - # Always SELECT specific columns. |
46 | | - ds = full.sel(time="2020").chunk({"time": 1}) |
| 38 | + # Open the full ARCO-ERA5 archive — all 273 variables since 1940. No |
| 39 | + # time or spatial slicing on the xarray side; SQL WHERE clauses below |
| 40 | + # express the filters. Turning dask off (chunks=None) skips task-graph |
| 41 | + # construction at open time. |
| 42 | + ds = xr.open_zarr(URL, chunks=None, storage_options={"token": "anon"}) |
47 | 43 | print( |
48 | | - "ARCO-ERA5 opened: year 2020, " |
| 44 | + "ARCO-ERA5 opened: " |
49 | 45 | f"{ds.sizes['time']:,} hourly time steps, " |
50 | | - f"{len(ds.data_vars)} variables (no spatial pre-slicing)." |
| 46 | + f"{len(ds.data_vars)} variables (no pre-slicing)." |
51 | 47 | ) |
52 | 48 |
|
| 49 | + # Heads up: ARCO-ERA5 has 262 surface + 11 atmospheric variables. The |
| 50 | + # library pushes column projection down to Zarr, so SELECT only fetches |
| 51 | + # what you ask for — but `SELECT * FROM era5.surface` would try to pull |
| 52 | + # every variable across the archive (terabytes from GCS). |
| 53 | + # ---> Always SELECT specific columns. <--- |
53 | 54 | ctx = xql.XarrayContext() |
54 | 55 | t0 = time.perf_counter() |
| 56 | + # Make sure to pass `chunks`! |
55 | 57 | ctx.from_dataset( |
56 | 58 | "era5", |
57 | 59 | ds, |
| 60 | + chunks=dict(time=1), |
58 | 61 | table_names={ |
59 | 62 | ("time", "latitude", "longitude"): "surface", |
60 | 63 | ("time", "level", "latitude", "longitude"): "atmosphere", |
@@ -94,7 +97,25 @@ def main() -> None: |
94 | 97 | """ |
95 | 98 | ).to_pandas() |
96 | 99 | print(profile.to_string(index=False)) |
97 | | - print(f" ({time.perf_counter() - t0:.2f}s, ~230M rows scanned)") |
| 100 | + print(f" ({time.perf_counter() - t0:.2f}s)") |
| 101 | + |
| 102 | + print( |
| 103 | + "\nAverage 2m-temperature per (lat, lon) cell, globally, " |
| 104 | + "2020-01-01 00:00-05:00 UTC (°C):" |
| 105 | + ) |
| 106 | + t0 = time.perf_counter() |
| 107 | + gridded = ctx.sql( |
| 108 | + """ |
| 109 | + SELECT latitude, longitude, AVG("2m_temperature") - 273.15 AS avg_c |
| 110 | + FROM era5.surface |
| 111 | + WHERE time BETWEEN TIMESTAMP '2020-01-01' |
| 112 | + AND TIMESTAMP '2020-01-01 05:00:00' |
| 113 | + GROUP BY latitude, longitude |
| 114 | + ORDER BY latitude DESC, longitude |
| 115 | + """ |
| 116 | + ).to_dataset(dims=["latitude", "longitude"], template=ds) |
| 117 | + print(gridded) |
| 118 | + print(f" ({time.perf_counter() - t0:.2f}s)") |
98 | 119 |
|
99 | 120 |
|
100 | 121 | if __name__ == "__main__": |
|
0 commit comments