Skip to content

Commit a505463

Browse files
committed
Merge pull request #465 from jhamman/update/month_mean_example
update monthly-means.rst using example data from xray-data repo
2 parents 91090e0 + 3f9b5d4 commit a505463

File tree

1 file changed

+29
-62
lines changed

1 file changed

+29
-62
lines changed

doc/examples/monthly-means.rst

+29-62
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Calculating Seasonal Averages from Timeseries of Monthly Means
44
==============================================================
55

6-
Author: `Joe Hamman <http://www.hydro.washington.edu/~jhamman/>`_
6+
Author: `Joe Hamman <http://uw-hydro.github.io/current_member/joe_hamman/>`_
7+
8+
The data for this example can be found in the `xray-data <https://github.com/xray/xray-data>`_ repository. This example is also available in an IPython Notebook that is available `here <https://github.com/xray/xray/tree/master/examples/xray_seasonal_means.ipynb>`_.
79

810
Suppose we have a netCDF or xray Dataset of monthly mean data and we
911
want to calculate the seasonal average. To do this properly, we need to
@@ -12,6 +14,7 @@ different number of days.
1214

1315
.. code:: python
1416
17+
%matplotlib inline
1518
import numpy as np
1619
import pandas as pd
1720
import xray
@@ -24,9 +27,9 @@ different number of days.
2427
2528
.. parsed-literal::
2629
27-
numpy version : 1.9.1
28-
pandas version : 0.15.2
29-
xray version : 0.4rc1-20-g52bbca3
30+
numpy version : 1.9.2
31+
pandas version : 0.16.2
32+
xray version : 0.5.1
3033
3134
3235
Some calendar information so we can support any netCDF calendar.
@@ -42,6 +45,7 @@ Some calendar information so we can support any netCDF calendar.
4245
'all_leap': [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
4346
'366_day': [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
4447
'360_day': [0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]}
48+
4549
A few calendar functions to determine the number of days in each month
4650
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4751

@@ -80,59 +84,39 @@ the ``calendar.month_range`` function.
8084
if leap_year(year, calendar=calendar):
8185
month_length[i] += 1
8286
return month_length
87+
8388
Open the ``Dataset``
8489
^^^^^^^^^^^^^^^^^^^^
8590

8691
.. code:: python
8792
88-
monthly_mean_file = '/raid2/jhamman/projects/RASM/data/processed/R1002RBRxaaa01a/lnd/monthly_mean_timeseries/R1002RBRxaaa01a.vic.hmm.197909-201212.nc'
93+
monthly_mean_file = 'RASM_example_data.nc'
8994
ds = xray.open_dataset(monthly_mean_file, decode_coords=False)
90-
ds.attrs['history'] = '' # get rid of the history attribute because its obnoxiously long
9195
print(ds)
9296
97+
9398
.. parsed-literal::
9499
95100
<xray.Dataset>
96-
Dimensions: (depth: 3, time: 400, x: 275, y: 205)
101+
Dimensions: (time: 36, x: 275, y: 205)
97102
Coordinates:
98-
* time (time) datetime64[ns] 1979-09-16T12:00:00 1979-10-17 ...
99-
* depth (depth) int64 0 1 2
100-
* x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
101-
* y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
103+
* time (time) datetime64[ns] 1980-09-16T12:00:00 1980-10-17 ...
104+
* x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
105+
* y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
102106
Data variables:
103-
Precipitation (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
104-
Evap (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
105-
Runoff (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
106-
Baseflow (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
107-
Soilw (time, depth, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan ...
108-
Swq (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
109-
Swd (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
110-
Swnet (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
111-
Lwnet (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
112-
Lwin (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
113-
Netrad (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
114-
Swin (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
115-
Latht (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
116-
Senht (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
117-
Grdht (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
118-
Albedo (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
119-
Radt (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
120-
Surft (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
121-
Relhum (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
122-
Tair (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
123-
Tsoil (time, depth, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan ...
124-
Wind (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan nan nan nan ...
107+
Tair (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan ...
125108
Attributes:
126109
title: /workspace/jhamman/processed/R1002RBRxaaa01a/lnd/temp/R1002RBRxaaa01a.vic.ha.1979-09-01.nc
127110
institution: U.W.
128111
source: RACM R1002RBRxaaa01a
129112
output_frequency: daily
130113
output_mode: averaged
131114
convention: CF-1.4
132-
history:
133115
references: Based on the initial model of Liang et al., 1994, JGR, 99, 14,415- 14,429.
134116
comment: Output from the Variable Infiltration Capacity (VIC) model.
135117
nco_openmp_thread_number: 1
118+
NCO: 4.3.7
119+
history: history deleted for brevity
136120
137121
138122
Now for the heavy lifting:
@@ -148,8 +132,10 @@ allong the time dimension.
148132
.. code:: python
149133
150134
# Make a DataArray with the number of days in each month, size = len(time)
151-
month_length = xray.DataArray(get_dpm(ds.time.to_index(), calendar='noleap'),
135+
month_length = xray.DataArray(get_dpm(ds.time.to_index(),
136+
calendar='noleap'),
152137
coords=[ds.time], name='month_length')
138+
153139
# Calculate the weights by grouping by 'time.season'
154140
weights = month_length.groupby('time.season') / month_length.groupby('time.season').sum()
155141
@@ -158,49 +144,30 @@ allong the time dimension.
158144
159145
# Calculate the weighted average
160146
ds_weighted = (ds * weights).groupby('time.season').sum(dim='time')
147+
161148
.. code:: python
162149
163150
print(ds_weighted)
164151
152+
165153
.. parsed-literal::
166154
167155
<xray.Dataset>
168-
Dimensions: (depth: 3, season: 4, x: 275, y: 205)
156+
Dimensions: (season: 4, x: 275, y: 205)
169157
Coordinates:
170-
* y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
171-
* x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
172-
* depth (depth) int64 0 1 2
173-
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
158+
* y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
159+
* x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
160+
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
174161
Data variables:
175-
Baseflow (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
176-
Tsoil (season, depth, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
177-
Wind (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
178-
Swin (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
179-
Swq (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
180-
Netrad (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
181-
Albedo (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
182-
Evap (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
183-
Swd (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
184-
Radt (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
185-
Lwin (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
186-
Relhum (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
187-
Soilw (season, depth, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
188-
Lwnet (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
189-
Senht (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
190-
Surft (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
191-
Latht (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
192-
Runoff (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
193-
Tair (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
194-
Grdht (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
195-
Swnet (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
196-
Precipitation (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
162+
Tair (season, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
197163
198164
199165
.. code:: python
200166
201167
# only used for comparisons
202168
ds_unweighted = ds.groupby('time.season').mean('time')
203169
ds_diff = ds_weighted - ds_unweighted
170+
204171
.. code:: python
205172
206173
# Quick plot to show the results

0 commit comments

Comments
 (0)