Skip to content

Commit f9c57ba

Browse files
committed
Added script arguments.
1 parent e6bfffe commit f9c57ba

File tree

3 files changed

+66
-50
lines changed

3 files changed

+66
-50
lines changed

demo/sst

-50
This file was deleted.

demo/sst.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
"""Demo of calculating global average sea surface temperature (SST) with SQL.
3+
4+
Please run the following to set up cloud resources:
5+
```
6+
gcloud auth application-default login
7+
coiled setup
8+
```
9+
"""
10+
import argparse
11+
import xarray as xr
12+
import xarray_sql as qr
13+
14+
parser = argparse.ArgumentParser()
15+
parser.add_argument('--start', type=str, default='2020-01-01', help='start time ISO string')
16+
parser.add_argument('--end', type=str, default='2020-01-02', help='end time ISO string')
17+
parser.add_argument('--cluster', action='store_true', help='deploy on coiled cluster')
18+
19+
args = parser.parse_args()
20+
21+
if args.cluster:
22+
from coiled import Cluster
23+
24+
cluster = Cluster(
25+
region='us-central1',
26+
worker_memory='16 GiB',
27+
spot_policy='spot_with_fallback',
28+
arm=True,
29+
)
30+
client = cluster.get_client()
31+
cluster.adapt(minimum=1, maximum=100)
32+
else:
33+
from dask.distributed import LocalCluster
34+
cluster = LocalCluster(processes=False)
35+
client = cluster.get_client()
36+
37+
era5_ds = xr.open_zarr(
38+
'gs://gcp-public-data-arco-era5/ar/'
39+
'1959-2022-full_37-1h-0p25deg-chunk-1.zarr-v2',
40+
chunks={'time': 240, 'level': 1}
41+
)
42+
print('dataset opened.')
43+
era5_sst_ds = era5_ds[['sea_surface_temperature']].sel(
44+
time=slice(args.start, args.end),
45+
level=1000, # surface level only.
46+
)
47+
48+
c = qr.Context()
49+
# chunk sizes determined from VM memory limit of 16 GiB.
50+
c.create_table('era5', era5_sst_ds, chunks=dict(time=24))
51+
52+
print('beginning query.')
53+
df = c.sql("""
54+
SELECT
55+
DATE("time") as date,
56+
AVG("sea_surface_temperature") as daily_avg_sst
57+
FROM
58+
"era5"
59+
GROUP BY
60+
DATE("time")
61+
""")
62+
63+
df.to_csv(f'global_avg_sst_{args.start}-{args.end}_*.cvs')

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ dev = [
4141
"pyink",
4242
"py-spy"
4343
]
44+
demo = [
45+
"coiled"
46+
]
4447

4548
[project.urls]
4649
Homepage = "https://github.com/alxmrs/xarray-sql"

0 commit comments

Comments
 (0)