In [17]:
%matplotlib inline
from matplotlib import pyplot as plt
In [18]:
import os, sys
import numpy as np
from numpy import ma
import xray
In [19]:
dpath = os.path.join(os.environ['HOME'], 'data/NCEP1')
In [20]:
dset_hgt = xray.open_dataset(os.path.join(dpath, 'hgt/hgt.mon.mean.nc'))
In [21]:
dset_hgt
Out[21]:
In [22]:
lat = dset_hgt['lat'].data
lon = dset_hgt['lon'].data
In [23]:
dset_hgt = dset_hgt.sel(time=slice('1948','2014'))
In [24]:
dates = dset_hgt['time'].data
In [25]:
hgt_700 = dset_hgt.sel(level=700)
In [26]:
hgt_700
Out[26]:
In [27]:
hgt_700 = hgt_700.sel(lat=slice(-20,-90.))
In [40]:
lat = hgt_700['lat'].data
lon = hgt_700['lon'].data
In [29]:
# hgt_700.close()
In [30]:
hgt_700
Out[30]:
In [32]:
def demean(x):
return x - x.sel(time=slice('1981-1-1','2010-12-1')).mean('time')
In [33]:
hgt_700_anoms = hgt_700.groupby('time.month').apply(demean)
In [34]:
Out[34]:
In [35]:
from eofs.standard import Eof
In [42]:
coslat = np.cos(np.deg2rad(lat))
wgts = np.sqrt(coslat)[..., np.newaxis]
In [46]:
X = hgt_700_anoms['hgt'].data
In [47]:
X = ma.masked_array(X)
In [48]:
solver = Eof(X, weights=wgts)
In [49]:
eof1 = solver.eofsAsCorrelation(neofs=1)
In [50]:
pc1 = solver.pcs(npcs=1, pcscaling=1)
In [51]:
plt.plot(pc1)
Out[51]:
In [52]:
eof1.shape
Out[52]:
In [53]:
plt.imshow(eof1.squeeze())
Out[53]:
In [54]:
from matplotlib.mlab import detrend_linear
In [57]:
dpc1 = detrend_linear(pc1.squeeze())
In [58]:
plt.plot(dpc1)
Out[58]:
In [61]:
time = hgt_700_anoms.time.to_index()
In [62]:
import pandas as pd
In [68]:
SAM = pd.DataFrame(dpc1, index=time, columns=['SAM'])
In [71]:
SAM.to_csv('../data/SAM_index_1948_2014_1981_2010_Clim.csv')