In [1]:
import xarray as xr
from matplotlib import pyplot as plt
plt.rcParams['figure.figsize'] = (12,7)
%matplotlib inline
In [13]:
! wget http://ldeo.columbia.edu/~rpa/NOAA_NCDC_ERSST_v3b_SST.nc
In [2]:
ds = xr.open_dataset('NOAA_NCDC_ERSST_v3b_SST.nc')
ds
Out[2]:
In [3]:
ds.sst[0].plot(vmin=-2, vmax=30)
Out[3]:
In [4]:
ds.sst.sel(lon=300, lat=50).plot()
Out[4]:
In [5]:
ds_mm = ds.groupby('time.month').mean(dim='time')
ds_mm
Out[5]:
In [6]:
ds_mm.sst.sel(lon=300, lat=50).plot()
Out[6]:
In [7]:
ds_mm.sst.mean(dim='lon').transpose().plot.contourf(levels=12, vmin=-2, vmax=30)
Out[7]:
In [8]:
(ds_mm.sst.sel(month=1) - ds_mm.sst.sel(month=7)).plot(vmax=10)
Out[8]:
In [10]:
def remove_time_mean(x):
return x - x.mean(dim='time')
ds_anom = ds.groupby('time.month').apply(remove_time_mean)
ds_anom
Out[10]:
In [11]:
ds_anom.sst.sel(lon=300, lat=50).plot()
Out[11]:
In [12]:
ds_anom_resample = ds_anom.resample(time='5Y').mean(dim='time')
ds_anom_resample
Out[12]:
In [13]:
ds_anom.sst.sel(lon=300, lat=50).plot()
ds_anom_resample.sst.sel(lon=300, lat=50).plot(marker='o')
Out[13]:
In [14]:
ds_anom_rolling = ds_anom.rolling(time=12, center=True).mean()
ds_anom_rolling
Out[14]:
In [15]:
ds_anom.sst.sel(lon=300, lat=50).plot(label='monthly anom')
ds_anom_resample.sst.sel(lon=300, lat=50).plot(marker='o', label='5 year resample')
ds_anom_rolling.sst.sel(lon=300, lat=50).plot(label='12 month rolling mean')
plt.legend()
Out[15]:
In [16]:
basin = xr.open_dataset('http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NODC/.WOA09/.Masks/.basin/dods')
basin
Out[16]:
In [17]:
basin = basin.rename({'X': 'lon', 'Y': 'lat'})
basin
Out[17]:
In [18]:
basin_surf = basin.basin[0]
basin_surf
Out[18]:
In [19]:
basin_surf.plot(vmax=10)
Out[19]:
In [20]:
basin_surf_interp = basin_surf.interp_like(ds.sst, method='nearest')
basin_surf_interp.plot(vmax=10)
Out[20]:
In [21]:
ds.sst.groupby(basin_surf_interp).first()
Out[21]:
In [23]:
basin_mean_sst = ds_mm.sst.groupby(basin_surf_interp).mean()
basin_mean_sst
Out[23]:
In [24]:
df = basin_mean_sst.to_dataframe()
df
Out[24]:
In [25]:
import pandas as pd
import numpy as np
basin_names = basin_surf.attrs['CLIST'].split('\n')
basin_df = pd.Series(basin_names, index=np.arange(1, len(basin_names)+1))
basin_df
Out[25]:
In [26]:
df = df.join(basin_df.rename('basin_name'))
In [27]:
df.plot.bar(x='basin_name')
Out[27]: