average (DJF)
以下のようにしてmonthly dataからDJF(12月~2月)平均データを求めることができる.
import numpy as np
import xarray as xr
ds = xr.open_dataset(fn) # fn is file name of netcdf data of geopotential height (zg).
# Read data at 500 hPa
z500=ds['zg'].sel(plev=50000, \
time=slice('1950-12-01','1952-12-31'), \
lon=slice(0.4, 0.6), lat=slice(-89.6, -89.4))
# Seasonal average (DJF
# Use time.season, which returns DJF, MAM, JJA, or SON,
# and mask (set as nan) non-DJF seasons
z500_djf1 = z500.where(z500['time.season'] == 'DJF')
# 3-month running average, which results in averaged data is located only
# time position of January.
z500_djf2 = z500_djf1.rolling(min_periods=3, center=True, time=3).mean()
# Take yearly averaged, which just extract January data (since other month data
# is nan, and convert time coordinate into year.
z500_djf3 = z500_djf2.groupby('time.year').mean('time')
# Remove first year data, which is nan. Note that if you take MAM average,
# first year dat is not nan and should be kept.
z500_djf4=z500_djf3[1:,:,:]
# In the above example, for the convenience of readers, different names
# are used for z500_djf1, ...4, but in real application it is better
# to use the same name for the efficient use of memory.