Skip to content

Latest commit

 

History

History
118 lines (74 loc) · 2.78 KB

weather-data.rst

File metadata and controls

118 lines (74 loc) · 2.78 KB

Toy weather data

Here is an example of how to easily manipulate a toy weather dataset using xray and other recommended Python libraries:

Shared setup:

_code/weather_data_setup.py

python

execfile("examples/_code/weather_data_setup.py")

Examine a dataset with pandas and seaborn

python

ds

df = ds.to_dataframe()

df.head()

df.describe()

@savefig examples_tmin_tmax_plot.png ds.mean(dim='location').to_dataframe().plot()

In [6]: sns.pairplot(df.reset_index(), vars=ds.data_vars) Out[6]: <seaborn.axisgrid.PairGrid at 0x7f0fd2368a10>

image

Probability of freeze by calendar month

python

freeze = (ds['tmin'] <= 0).groupby('time.month').mean('time') freeze

@savefig examples_freeze_prob.png freeze.to_pandas().plot()

Monthly averaging

python

monthly_avg = ds.resample('1MS', dim='time', how='mean')

@savefig examples_tmin_tmax_plot_mean.png monthly_avg.sel(location='IA').to_dataframe().plot(style='s-')

Note that MS here refers to Month-Start; M labels Month-End (the last day of the month).

Calculate monthly anomalies

In climatology, "anomalies" refer to the difference between observations and typical weather for a particular season. Unlike observations, anomalies should not show any seasonal cycle.

python

climatology = ds.groupby('time.month').mean('time') anomalies = ds.groupby('time.month') - climatology

@savefig examples_anomalies_plot.png anomalies.mean('location').to_dataframe()[['tmin', 'tmax']].plot()

Fill missing values with climatology

The :py~xray.Dataset.fillna method on grouped objects lets you easily fill missing values by group:

python

# throw away the first half of every month some_missing = ds.tmin.sel(time=ds['time.day'] > 15).reindex_like(ds) filled = some_missing.groupby('time.month').fillna(climatology.tmin)

both = xray.Dataset({'some_missing': some_missing, 'filled': filled}) both

df = both.sel(time='2000').mean('location').reset_coords(drop=True).to_dataframe()

@savefig examples_filled.png df[['filled', 'some_missing']].plot()