ds = ds.stack(realiz=['year', 'number']) #compute seasonal means: ASO and SON ninio3_aso = ds.sel(**{'month': slice(8, 10)}).mean(dim='month') ninio3_son = ds.sel(**{'month': slice(9, 11)}).mean(dim='month') ninio3_seasonal = xr.concat([ninio3_aso, ninio3_son], dim='season') #select upper and lower quartile ninio3_season_lower = ninio3_aso.quantile(0.25, dim='realiz', interpolation='linear') ninio3_season_upper = ninio3_aso.quantile(0.75, dim='realiz', interpolation='linear') ninio3_monthly_lower = ds.quantile(0.25, dim='realiz', interpolation='linear') ninio3_monthly_upper = ds.quantile(0.75, dim='realiz', interpolation='linear') [lamb, v, PC] = eofdata.eofdata(ds.sst.values, 4) print(v[:, 0]) sst_monthly_index = -PC[0, :] [lamb, v, PC] = eofdata.eofdata(ninio3_seasonal.sst.values, 8) sst_seasonal_index = PC[0, :] ds_new = xr.Dataset({ 'ninio3_mon': xr.DataArray(sst_monthly_index), 'ninio3_seas': xr.DataArray(sst_seasonal_index) }) ds_new.to_netcdf('~/datos/data/ninio3_index.nc') ds.reset_index('realiz').to_netcdf('~/datos/data/ninio3_monthly.nc') ninio3_seasonal.reset_index('realiz').to_netcdf( '~/datos/data/ninio3_seasonal.nc')
RUTA = '~/datos/ERAI/' hgt = [] for Y in np.arange(1981, 2019): if Y != 2002: ds = xr.open_dataset(RUTA + 'HGT50_erai_' + str(Y) + '.grib', engine='cfgrib', backend_kwargs={'indexpath': ''}) ds = ds.drop(['number', 'isobaricInhPa', 'step']) hgt.append(ds) hgt = xr.concat(hgt, dim='time') hgt.time.values = np.arange(0, len(hgt.time.values)) hgt.to_netcdf('~/datos/data/hgt_erai_50.nc4') hgt = hgt.sel(**{ 'latitude': slice(-60, -90) }).mean(dim=['longitude', 'latitude']) hgt.to_netcdf('~/datos/data/PV_monthly_erai.nc4') hgt.time.values = hgt.valid_time.values hgt = hgt.sel(**{'time': slice('1981-01-01', '2017-12-31')}) PV_ason = hgt.sel( time=np.logical_and(hgt['time.month'] >= 8, hgt['time.month'] <= 11)) PV = np.reshape(PV_ason.z.values, [36, 4]) PV = (PV - np.mean(PV, axis=0)) [lamb, v, PC] = eofdata.eofdata(PV.T, 3) print(v[:, 0]) PV_monthly_index = -1 * PC[0, :] ds_new = xr.Dataset({'PV_mon': xr.DataArray(PV_monthly_index)}) ds_new.to_netcdf('~/datos/data/PV_index_erai.nc4')
#compute monthly el ninio 3.4 index import numpy as np import xarray as xr import eofdata import os os.environ['HDF5_USE_FILE_LOCKING'] = 'FALSE' RUTA = '~/datos/data/' ds = xr.open_dataset(RUTA + 'monthly_hgt50_aug_feb.nc') ds = ds.sel(**{ 'latitude': slice(-60, -90) }).mean(dim=['longitude', 'latitude']).compute() [lamb, v, PC] = eofdata.eofdata(ds.z.values, 3) print(v[:, 0]) SPV_monthly_index = -1 * PC[0, :] ds_new = xr.Dataset({'SPV_index': xr.DataArray(SPV_monthly_index)}) ds_new.to_netcdf(RUTA + 'fogt/SPV_index.nc')
#plt.savefig('./figures/scatter_PCs_SSTs.jpg') # ##open era intreim data a compute el ninio index # sst_erai = xr.open_dataset('~/datos/data/fogt/sst_erai.nc4') sst_erai.time.values = sst_erai.valid_time.values #compute ninio 3.4 index ninio34_erai = sst_erai.sel( **{ 'time': slice('1981-08-01', '2018-02-01'), 'latitude': slice(5, -5), 'longitude': slice(190, 240) }).mean(dim=['longitude', 'latitude']) ninio34_erai = ninio34_erai.sel(time=np.logical_or( ninio34_erai.time.values <= np.datetime64('2002-07-31'), ninio34_erai.time.values >= np.datetime64('2003-08-01'))) ninio34_erai = ninio34_erai.sel(time=np.logical_or( ninio34_erai['time.month'] >= 8, ninio34_erai['time.month'] <= 2)) ninio34_erai = np.reshape(ninio34_erai.sst.values, [36, 7]) [lamb, v, PC] = eofdata.eofdata(np.transpose(ninio34_erai), 4) print(v[:, 0]) sst_monthly_index = -PC[0, :] ds = xr.Dataset({'ninio34_index': xr.DataArray(sst_monthly_index)}) ds.to_netcdf('~/datos/data/fogt/ninio34_erai_index.nc4')
#compute seasona means: ASO and SON PV_aso = ds.sel(**{'month':slice(8,10)}).mean(dim='month') PV_son = ds.sel(**{'month':slice(9,11)}).mean(dim='month') PV_seasonal = xr.concat([PV_aso, PV_son], dim='season') #select upper and lower quartile PV_aso_lower = PV_aso.quantile(0.25, dim='realiz', interpolation='linear') PV_aso_upper = PV_aso.quantile(0.75, dim='realiz', interpolation='linear') PV_son_lower = PV_aso.quantile(0.25, dim='realiz', interpolation='linear') PV_son_upper = PV_aso.quantile(0.75, dim='realiz', interpolation='linear') PV_monthly_lower = ds.quantile(0.25, dim='realiz', interpolation='linear') PV_monthly_upper = ds.quantile(0.75, dim='realiz', interpolation='linear') ds.reset_index('realiz').to_netcdf('./data/PV_monthly2.nc') [lamb, v, PC] = eofdata.eofdata(ds.z.values[0:4, :], 3) print(v[:, 0]) PV_monthly_index = PC[0, :] [lamb, v, PC] = eofdata.eofdata(PV_seasonal.z.values, 2) print(v[0,:]) PV_seasonal_index = -1 * PC[0, :] ds_new = xr.Dataset({'PV_mon': xr.DataArray(PV_monthly_index), 'PV_seas': xr.DataArray(PV_seasonal_index)}) ds_new.to_netcdf('./data/PV_index.nc') PV_aso.reset_index('realiz').to_netcdf('./data/PV_aso.nc') PV_son.reset_index('realiz').to_netcdf('./data/PV_son.nc') PV_aso_lower.to_netcdf('./data/PV_aso_lower.nc')
#compute ninio indexes for erai and define ninio and ninia years import numpy as np import xarray as xr import matplotlib.pyplot as plt import pandas as pd import eofdata import matplotlib.pyplot as plt import os os.environ['HDF5_USE_FILE_LOCKING'] = 'FALSE' RUTA = '~/datos/data/fogt/' ds = xr.open_dataset(RUTA + 'sst_ninio34_aug_feb.nc4') ninio34_monthly_lower = ds.quantile(0.25, dim='realiz', interpolation='linear') ninio34_monthly_upper = ds.quantile(0.75, dim='realiz', interpolation='linear') [lamb, v, PC] = eofdata.eofdata(ds.sst.values, 4) print(v[:, 0]) ninio34_monthly_index = -PC[0, :] ds_new = xr.Dataset({'ninio34_index': xr.DataArray(ninio34_monthly_index)}) ds_new.to_netcdf('~/datos/data/fogt/ninio34_monthly.nc4') #plot scatter PC against SST. This is done to check how representative this index is plt.figure(1) plt.scatter(sst_monthly_index, sst_seasonal_index, s=80) plt.xlabel('PC monthly') plt.ylabel('PC seasonal') plt.savefig('./figures/scatter_PCs.jpg') plt.figure(2, (12, 16)) plt.subplot(421) plt.scatter(sst_monthly_index, ds.sst.values[0, :], s=80)