Skip to content

Commit 513f85d

Browse files
committed
update
1 parent 5fbaf8d commit 513f85d

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,23 @@ ds = xr.tutorial.open_dataset('air_temperature')
3636
ctx = xql.XarrayContext()
3737
ctx.from_dataset('air', ds, chunks=dict(time=100))
3838

39-
# A climatology — the long-term mean at each grid cell — computed in SQL.
39+
# A climatology — the mean annual cycle — computed in SQL: average air
40+
# temperature for each month of the year, over all grid cells and years.
4041
clim = ctx.sql('''
41-
SELECT "lat", "lon", AVG("air") AS air
42+
SELECT
43+
CAST(date_part('month', "time") AS INTEGER) AS month,
44+
AVG("air") AS air
4245
FROM "air"
43-
GROUP BY "lat", "lon"
46+
GROUP BY CAST(date_part('month', "time") AS INTEGER)
47+
ORDER BY month
4448
''')
4549

46-
# Write the SQL result back to an Xarray Dataset. Because `time` was
47-
# aggregated away, name the remaining dimensions explicitly. The variable's
48-
# source metadata (e.g. its units) is recovered from the registered table.
49-
clim_ds = clim.to_dataset(dims=["lat", "lon"])
50-
# <xarray.Dataset>
51-
# Dimensions: (lat: 25, lon: 53)
52-
# Coordinates:
53-
# * lat (lat) float32 75.0 72.5 70.0 ... 20.0 17.5 15.0
54-
# * lon (lon) float32 200.0 202.5 205.0 ... 325.0 327.5 330.0
55-
# Data variables:
56-
# air (lat, lon) float64 ...
50+
# Write the SQL result back to an Xarray Dataset. `month` is a derived
51+
# column, so name it as the dimension; the variable's units are recovered
52+
# from the registered table. The result is one value per month: air(month).
53+
clim_ds = clim.to_dataset(dims=["month"])
5754

58-
# Plot the climatology like any other Dataset.
55+
# Plot the annual cycle as a time series.
5956
clim_ds["air"].plot() # in a script, call matplotlib.pyplot.show() to display
6057
```
6158

docs/examples.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
A query result can be consumed two ways: as a flat pandas DataFrame
44
(`to_pandas`) or written back to an Xarray Dataset (`to_dataset`). This computes
5-
a climatology — the long-term mean at each grid cell — and shows both.
5+
a climatology — the mean annual cycle, one value per month of the year — and
6+
shows both.
67

78
> **Note:** this example also needs `pooch` and a netCDF backend (for the
89
> tutorial download) and `matplotlib` (for the plot):
@@ -19,20 +20,23 @@ ctx.from_dataset('air', ds, chunks=dict(time=100))
1920

2021
clim = ctx.sql('''
2122
SELECT
22-
"lat", "lon", AVG("air") AS air
23+
CAST(date_part('month', "time") AS INTEGER) AS month,
24+
AVG("air") AS air
2325
FROM
2426
"air"
2527
GROUP BY
26-
"lat", "lon"
28+
CAST(date_part('month', "time") AS INTEGER)
29+
ORDER BY
30+
month
2731
''')
2832

2933
# Option 1: a flat pandas DataFrame.
3034
clim.to_pandas().head()
3135

32-
# Option 2: round-trip back to an Xarray Dataset and plot it. `time` was
33-
# aggregated away, so name the remaining dimensions explicitly; the variable's
34-
# units are recovered from the registered table.
35-
clim_ds = clim.to_dataset(dims=["lat", "lon"])
36+
# Option 2: round-trip back to an Xarray Dataset and plot the annual cycle as
37+
# a time series. `month` is a derived column, so name it as the dimension; the
38+
# variable's units are recovered from the registered table.
39+
clim_ds = clim.to_dataset(dims=["month"])
3640
clim_ds["air"].plot()
3741
```
3842

0 commit comments

Comments
 (0)