-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
"""Demo of calculating global average sea surface temperature (SST) with SQL. | ||
Please run the following to access the ERA5 dataset: | ||
``` | ||
gcloud auth application-default login | ||
``` | ||
""" | ||
import xarray as xr | ||
import xarray_sql as qr | ||
|
||
# TODO(alxmrs): Add coiled or dask cluster code. | ||
|
||
era5_ds = xr.open_zarr( | ||
'gs://gcp-public-data-arco-era5/ar/' | ||
'1959-2022-full_37-1h-0p25deg-chunk-1.zarr-v2', | ||
chunks={'time': 240, 'level': 1} | ||
) | ||
print('dataset opened.') | ||
# TODO(alxmrs): Slice to small time range based on script args. | ||
era5_sst_ds = era5_ds[['sea_surface_temperature']].sel( | ||
level=1000, # surface level only. | ||
) | ||
|
||
# chunk sizes determined from VM memory limit of 16 GB. | ||
c = qr.Context() | ||
c.create_table('era5', era5_sst_ds, chunks=dict(time=24)) | ||
|
||
print('beginning query.') | ||
df = c.sql(""" | ||
SELECT | ||
DATE("time") as date, | ||
AVG("sea_surface_temperature") as daily_avg_sst | ||
FROM | ||
"era5" | ||
GROUP BY | ||
DATE("time") | ||
""") | ||
|
||
# TODO(alxmrs): time slice should be in file name. | ||
df.to_csv('global_avg_sst_*.cvs') |