In [1]:
%matplotlib inline
import matplotlib.pylab as plt
import xray
import pandas as pd
import seaborn as sns
Open netCDF file with several variables inside:
In [2]:
f = xray.open_dataset('./air.sig995.2012.nc')
In [3]:
f
Out[3]:
Mean over time
In [4]:
mmean = f.mean('time')
In [5]:
mmean
Out[5]:
In [6]:
mmean.air.shape
Out[6]:
Plot mean air temperature:
In [8]:
plt.imshow(mmean.air, cmap=plt.cm.jet);
Create time series for northern and southernd hemispheres:
In [9]:
mmean.lat
Out[9]:
In [10]:
north = mmean.loc[dict(lat=slice(90,0))]
In [12]:
plt.imshow(north.air, cmap=plt.cm.jet)
Out[12]:
In [13]:
south = mmean.loc[dict(lat=slice(0,-90))]
In [15]:
plt.imshow(south.air, cmap=plt.cm.jet)
Out[15]:
In [16]:
north_tm = f.air.loc[dict(lat=slice(90,0))].mean(['lat','lon'])
south_tm = f.air.loc[dict(lat=slice(0,-90))].mean(['lat','lon'])
In [18]:
plt.plot(north_tm)
Out[18]:
In [19]:
north_tm
Out[19]:
In [20]:
dfn = north_tm.to_dataframe()
dfs = south_tm.to_dataframe()
In [21]:
df = pd.DataFrame(index=dfn.index)
df['north'] = dfn.air
df['south'] = dfs.air
In [22]:
df.plot()
Out[22]:
In [23]:
smoothed = pd.rolling_mean(df, 4)
smoothed.plot()
Out[23]:
Convert back to xray Dataset:
In [24]:
tr = xray.Dataset.from_dataframe(smoothed)
In [25]:
tr
Out[25]:
Save to netCDF file:
In [26]:
tr.to_netcdf('smoothed.nc')