@@ -36,26 +36,23 @@ ds = xr.tutorial.open_dataset('air_temperature')
3636ctx = xql.XarrayContext()
3737ctx.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.
4041clim = 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 .
5956clim_ds[" air" ].plot() # in a script, call matplotlib.pyplot.show() to display
6057```
6158
0 commit comments