def mk_rean_sam_index(datapath='./'): # list in the pre-defined list of files to use. rean = ['R1', 'R2', '20CR', 'ERA-Int', 'CFSR', 'MERRA', 'HadSLP2r'] rean2 = ['R1', 'R2', '20CR', 'ERA', 'CFSR', 'MERRA', 'HadSLP2r'] tail = '_slp.mon.mean.nc' names = [ 'zonal-mean_remap_' + r + tail for r in rean ] df_sam = pd.DataFrame() for i, name in enumerate(names): print name # load the data and make dataframes ifile = os.path.join(datapath, name) dims = cd.get_dimensions(ifile, 'slp', toDatetime=True) dims['time'] = [pd.datetime(d.year, d.month, 1) for d in dims['time']] sami = calc_sam(ifile, 'slp') samdf = pd.DataFrame(sami, index=dims['time']) df_sam = pd.concat([df_sam, samdf], axis=1) df_sam.columns = rean2 # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_reanalysis.h5') store = pd.HDFStore(out_file, 'a') store['zonmean_sam'] = df_sam store.close()
def mk_rean_sam_index(datapath='./'): # list in the pre-defined list of files to use. rean = ['R1', 'R2', '20CR', 'ERA-Int', 'CFSR', 'MERRA', 'HadSLP2r'] rean2 = ['R1', 'R2', '20CR', 'ERA', 'CFSR', 'MERRA', 'HadSLP2r'] tail = '_slp.mon.mean.nc' names = ['zonal-mean_remap_' + r + tail for r in rean] df_sam = pd.DataFrame() for i, name in enumerate(names): print name # load the data and make dataframes ifile = os.path.join(datapath, name) dims = cd.get_dimensions(ifile, 'slp', toDatetime=True) dims['time'] = [pd.datetime(d.year, d.month, 1) for d in dims['time']] sami = calc_sam(ifile, 'slp') samdf = pd.DataFrame(sami, index=dims['time']) df_sam = pd.concat([df_sam, samdf], axis=1) df_sam.columns = rean2 # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_reanalysis.h5') store = pd.HDFStore(out_file, 'a') store['zonmean_sam'] = df_sam store.close()
def calc_marshall_sam(psl_file, varname, start_date='1871-01-01', end_date='2013-12-31'): """ Compute the SAM index as the pressure difference between 40 and 65S only using data from the 12 Marshall (2003) locations. Parameters: ----------- psl_file : str The name of the **zonal meaned** SLP netcdf file to compute the SAM from. Can be a full path. varname : str The name of the Sea Level Pressure variable in psl_file. Returns: ------- dft : pandas DataFrame The calculated SAM index, presure at 40 and 65S. """ # Marshall locations mlat40s = np.array([46.9, 37.8, 42.9, 43.5, 39.6, 40.4])*-1 mlon40s = np.array([37.9, 77.5, 147.3, 172.6, -73.1, -9.9]) mlat65s = np.array([70.8, 67.6, 66.6, 66.3, 66.7, 65.2])*-1 mlon65s = np.array([11.8, 62.9, 93.0, 110.5, 140.0, -64.3]) # load the data dims = cd.get_dimensions(psl_file, varname, toDatetime=True) #dims['time'] = [pd.datetime(d.year, d.month, 1) for d in dims['time']] tmin = dims['time'].min() ds = pd.datetime(tmin.year, tmin.month, 1) dates = pd.date_range(ds, periods=(len(dims['time'])) , freq='MS') p40 = np.zeros((len(dims['time']), 6)) p65 = np.zeros((len(dims['time']), 6)) ncvar = Dataset(psl_file).variables[varname] for k in range(6): # loop over the six stations at each lat and get data at each one. var = cdo.remapnn('lon=' + str( mlon40s[k] ) + '/lat='\ + str( mlat40s[k] ) , input=('-selvar,' + varname + ' ' + psl_file) , returnMaArray=varname).squeeze() p40[:,k] = scale(ncvar, var) var2 = cdo.remapnn('lon=' + str( mlon65s[k] ) + '/lat='\ + str( mlat65s[k] ) , input=('-selvar,' + varname + ' ' + psl_file) , returnMaArray=varname).squeeze() p65[:,k] = scale(ncvar, var2) # Now create the mean pressure at 40S and 65S and return s40s = pd.Series(p40.mean(axis=1), index=dates) s65s = pd.Series(p65.mean(axis=1), index=dates) return s40s, s65s
def mk_20cr_ens_jetprop(datapath='../data_retrieval/data/'): """Calculated the kinematic properties of the jet for the 20CR ensemble """ #The pre-computed zonal mean u10m file zmfn = os.path.join(datapath, 'zonal-mean_remap_20CR_ens_u10m.mon.mean.nc') # load the data and make dataframes dims = cd.get_dimensions(zmfn, 'u10m', toDatetime=True) nc = Dataset(zmfn) u_20cr = nc.variables['u10m'][:].squeeze() lat = dims['lat'] # initialize some empty arrays for the jet props. width = np.zeros((len(dims['time']), 56)) umax = np.zeros((len(dims['time']), 56)) uloc = np.zeros((len(dims['time']), 56)) # Calculate properties for each ensemble member for i in np.arange(56): uas = np.squeeze(u_20cr[:,i,:]) jetmax, latofmax, latn, lats, jetwidth = jetprop(uas, lat) umax[:,i] = jetmax uloc[:,i] = latofmax width[:,i] = jetwidth # make a plot at a random time index ri, to see that the defs are working. #ri = 10 #plt.close() #plt.plot( lat, uas[ri,:], 'k-o', linewidth=2) #plt.plot( lat, lat*0, 'k--') #plt.plot( latofmax[ri], jetmax[ri], 'rx', markersize=8, markeredgewidth=1) #plt.plot( [-90, 90], [ jetmax[ri],jetmax[ri] ], 'r--') #plt.plot( [-90, 90], [ jetmax[ri]*0.5, jetmax[ri]*0.5], 'r--') #print latn[ri], lats[ri] #plt.plot( [latn[ri], latn[ri]], [-10, 10], 'r--') #plt.plot( [lats[ri], lats[ri]], [-10, 10], 'r--') #raw_input('go?') # Create Pandas DataFrames from the arrays df_umax = pd.DataFrame(umax, index=dims['time'], columns=np.arange(1,57)) df_uloc = pd.DataFrame(uloc, index=dims['time'], columns=np.arange(1,57)) df_width = pd.DataFrame(width, index=dims['time'], columns=np.arange(1,57)) # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_20CR_ensemble.h5') store = pd.HDFStore(out_file, 'a') store['width'] = df_width store['maxspd'] = df_umax store['locmax'] = df_uloc store.close()
def mk_cmip5_jetprop(datapath='./'): # List of models to get data for model_list = [ 'ACCESS1-0', 'ACCESS1-3', 'BNU-ESM', 'CMCC-CMS', 'CMCC-CM', 'CNRM-CM5', 'CSIRO-Mk3-6-0', 'CanESM2', 'GISS-E2-H-CC', 'GISS-E2-H', 'GISS-E2-R-CC', 'GISS-E2-R', 'HadCM3', 'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'IPSL-CM5A-LR', 'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC-ESM-CHEM', 'MIROC-ESM', 'MIROC5', 'MPI-ESM-LR', 'MPI-ESM-MR', 'MRI-CGCM3', 'NorESM1-ME', 'NorESM1-M', 'bcc-csm1-1-m', 'bcc-csm1-1', 'inmcm4' ] tail = '_historical-rcp45_r1i1p1_188101-201212.nc' names = ['zonal-mean_remap_uas_Amon_' + m + tail for m in model_list] # Initialize some arrays width = np.zeros((1584, 30)) umax = np.zeros((1584, 30)) uloc = np.zeros((1584, 30)) for i, name in enumerate(names): # define and load the file dimensions and uas data ifile = os.path.join(datapath, name) print ifile dims = cd.get_dimensions(ifile, 'uas', toDatetime=True) nc = Dataset(ifile) uas = nc.variables['uas'][:].squeeze() lat = dims['lat'] # compute jet props jetmax, latofmax, latn, lats, jetwidth = jetprop(uas, lat) umax[:, i] = jetmax uloc[:, i] = latofmax width[:, i] = jetwidth # Assign to pd DataFrames df_umax = pd.DataFrame(umax, index=dims['time'], columns=np.arange(1, 31)) df_uloc = pd.DataFrame(uloc, index=dims['time'], columns=np.arange(1, 31)) df_width = pd.DataFrame(width, index=dims['time'], columns=np.arange(1, 31)) # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_cmip5.h5') store = pd.HDFStore(out_file, 'a') store['width'] = df_width store['maxspd'] = df_umax store['locmax'] = df_uloc store.close()
def mk_cmip5_jetprop(datapath='./'): # List of models to get data for model_list = ['ACCESS1-0', 'ACCESS1-3', 'BNU-ESM', 'CMCC-CMS', 'CMCC-CM', 'CNRM-CM5', 'CSIRO-Mk3-6-0', 'CanESM2', 'GISS-E2-H-CC', 'GISS-E2-H', 'GISS-E2-R-CC', 'GISS-E2-R', 'HadCM3', 'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'IPSL-CM5A-LR', 'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC-ESM-CHEM', 'MIROC-ESM', 'MIROC5', 'MPI-ESM-LR','MPI-ESM-MR','MRI-CGCM3', 'NorESM1-ME', 'NorESM1-M', 'bcc-csm1-1-m', 'bcc-csm1-1','inmcm4' ] tail = '_historical-rcp45_r1i1p1_188101-201212.nc' names = [ 'zonal-mean_remap_uas_Amon_' + m + tail for m in model_list ] # Initialize some arrays width = np.zeros((1584, 30)) umax = np.zeros((1584, 30)) uloc = np.zeros((1584, 30)) for i, name in enumerate(names): # define and load the file dimensions and uas data ifile = os.path.join(datapath, name) print ifile dims = cd.get_dimensions(ifile, 'uas', toDatetime=True) nc = Dataset(ifile) uas = nc.variables['uas'][:].squeeze() lat = dims['lat'] # compute jet props jetmax, latofmax, latn, lats, jetwidth = jetprop(uas, lat) umax[:,i] = jetmax uloc[:,i] = latofmax width[:,i] = jetwidth # Assign to pd DataFrames df_umax = pd.DataFrame(umax, index=dims['time'], columns=np.arange(1,31)) df_uloc = pd.DataFrame(uloc, index=dims['time'], columns=np.arange(1,31)) df_width = pd.DataFrame(width, index=dims['time'], columns=np.arange(1,31)) # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_cmip5.h5') store = pd.HDFStore(out_file, 'a') store['width'] = df_width store['maxspd'] = df_umax store['locmax'] = df_uloc store.close()
def mk_20cr_ens_sam_index(datapath='.'): """Calculates the SAM index for the 20CR ensemble """ #The pre-computed zonal mean SLP file zmfn = os.path.join(datapath, 'zonal-mean_remap_20CR_ens_slp.mon.mean.nc') # load the data and make dataframes dims = cd.get_dimensions(zmfn, 'slp', toDatetime=True) sam_20cr = calc_sam(zmfn, 'slp', start_date='1871-01-01', end_date='2013-12-31') df_sam = pd.DataFrame(sam_20cr, index=dims['time']) # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_20CR_ensemble.h5') store = pd.HDFStore(out_file, 'a') store['zonmean_sam'] = df_sam store.close()
def mk_cmip5_sam_index(datapath='./'): # List of models to get data for model_list = ['ACCESS1-0', 'ACCESS1-3', 'BNU-ESM', 'CMCC-CMS', 'CMCC-CM', 'CNRM-CM5', 'CSIRO-Mk3-6-0', 'CanESM2', 'GISS-E2-H-CC', 'GISS-E2-H', 'GISS-E2-R-CC', 'GISS-E2-R', 'HadCM3', 'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'IPSL-CM5A-LR', 'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC-ESM-CHEM', 'MIROC-ESM', 'MIROC5', 'MPI-ESM-LR','MPI-ESM-MR','MRI-CGCM3', 'NorESM1-ME', 'NorESM1-M', 'bcc-csm1-1-m', 'bcc-csm1-1','inmcm4' ] tail = '_historical-rcp45_r1i1p1_188101-201212.nc' names = [ 'zonal-mean_remap_psl_Amon_' + m + tail for m in model_list ] cnames = [ n.replace('-', '_') for n in model_list ] sam = np.zeros((1584, 30)) for i, name in enumerate(names): ifile = os.path.join(datapath, name) # load the data and make dataframes sam[:,i] = calc_sam(ifile, 'psl', start_date='1881-01-01', end_date='2013-12-31') # Get the time axis dims = cd.get_dimensions(ifile, 'psl', toDatetime=True) t = dims['time'] a = ( t <= pd.datetime(2013,12,31) ) & ( t >= pd.datetime(1881,1,1) ) dims['time'] = t[a] df_sam = pd.DataFrame(sam, index=dims['time'], columns=cnames) # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_cmip5.h5') store = pd.HDFStore(out_file, 'a') store['zonmean_sam'] = df_sam store.close()
def plot_trend_maps(datapath): dims = {'lat' : np.arange(-89.5,89.6,1), 'lon' : np.arange(0,360,1) } # load the data for all 20CR ense members ifile_20CR_slopes_all = datapath + 'slope_remap_20CR_ens_slp.mon.mean.nc' nc = Dataset(ifile_20CR_slopes_all) psl_slopes_all_20cr = nc.variables['slp'][:].squeeze()*120 dims20cr = cd.get_dimensions(ifile_20CR_slopes_all, 'slp') ifile_20CR_slopes_all_u10m = datapath + 'slope_remap_20CR_ens_u10m.mon.mean.nc' nc = Dataset(ifile_20CR_slopes_all_u10m) u10m_slopes_all_20cr = nc.variables['u10m'][:].squeeze()*120 dims20cr2 = cd.get_dimensions(ifile_20CR_slopes_all_u10m, 'u10m') # Look at the uncertainty in the trends fig, (axt, axm, axb) = plt.subplots(3,1, sharex=True, figsize=(7,7)) fig.subplots_adjust(right=0.75, top=0.8, hspace=0.2) # For SLP vmin = 0 vmax = 20 ncols = 9 # For u10m vmin2 = 0 vmax2 = 0.1*100 cmap = brewer2mpl.get_map('Reds', 'sequential', ncols, reverse=False).mpl_colormap cmap = discrete_cmap(ncols, cmap) m =\ Basemap(llcrnrlon=0,llcrnrlat=-90,urcrnrlon=360,urcrnrlat=0,projection='mill', suppress_ticks=True) lons, lats = np.meshgrid(dims20cr['lon'], dims20cr['lat']) lons2, lats2 = np.meshgrid(dims20cr2['lon'], dims20cr2['lat']) x, y = m(lons, lats) x2, y2 = m(lons2, lats2) xpt, ypt = m(20,-86) cot = m.pcolor(x, y, psl_slopes_all_20cr.std(axis=0)*2,vmin=vmin, vmax=vmax, cmap=cmap, ax=axt, rasterized=True, zorder=0) #CS = m.contour(x, y, psl_slopes_all_20cr.std(axis=0)*2,np.arange(0,20,2), #ax=axt, colors='k', zorder=0) #plt.clabel(CS, CS.levels, inline=True, fmt='%d', fontsize=10, zorder=1) axt.text(xpt, ypt, r'20CR SLP trend $2\sigma$', zorder=5) com = m.pcolor(x2, y2, u10m_slopes_all_20cr.std(axis=0)*200,vmin=vmin2, vmax=vmax2, cmap=cmap, ax=axm, rasterized=True, zorder=0) axm.text(xpt, ypt, r'20CR u10m trend $2\sigma$', zorder=5) #CS2 = m.contour(x2, y2, u10m_slopes_all_20cr.std(axis=0)*2,np.arange(0,0.1,0.01), #ax=axm, colors='k', zorder=0) #plt.clabel(CS2, CS2.levels, inline=True, fmt='%1.2f', fontsize=10, zorder=1) xtp = np.arange(0,360,60) m.drawmeridians(xtp,labels=[0,0,0,1], linewidth=0,yoffset=-0.4e6 , ax=axm) xt, yt = m(np.arange(0,360,60), np.repeat(-90,6)) axm.set_xticks(xt) for ax in fig.axes: ax.autoscale(enable=True, axis='both', tight=True) m.drawcoastlines(linewidth=1.25, ax=ax) m.fillcontinents(color='0.8',ax=ax, zorder=4) m.drawparallels(np.arange(-80,81,20),labels=[1,0,0,0], linewidth=0, ax=ax) for k, spine in ax.spines.items(): spine.set_zorder(10) box = axt.get_position() tl = fig.add_axes([box.x0*1.1 + box.width * 1., box.y0, 0.02, box.height]) bounds = np.linspace(vmin, vmax, ncols) plt.colorbar(cot, cax=tl, label='Pa decade$^{-1}$',spacing='proportional', boundaries=bounds) box = axm.get_position() tl = fig.add_axes([box.x0*1.1 + box.width * 1., box.y0, 0.02, box.height]) bounds = np.linspace(vmin2, vmax2, ncols) plt.colorbar(com, cax=tl, label=r'$\times 10^{-2}$ m s$^{-1}$ decade$^{-1}$', spacing='proportional', boundaries=bounds) #fig.delaxes(axm) fig.delaxes(axb) plt.savefig('../plots/psl_maps_20CR_trends-uncertainty_1951-2011.pdf', bbox_inches='tight', dpi=300)
def mk_rean_jetprop(datapath='./'): # list in the pre-defined list of files to use. rean = ['R1', 'R2', '20CR', 'ERA-Int', 'CFSR', 'MERRA'] rean2 = ['R1', 'R2', '20CR', 'ERA', 'CFSR', 'MERRA'] tail = '_u10m.mon.mean.nc' names = [ 'zonal-mean_remap_' + r + tail for r in rean ] # Initialize some jetprop arrays df_umax = pd.DataFrame() df_uloc = pd.DataFrame() df_width = pd.DataFrame() # Loop over the reanalyses for i, name in enumerate(names): # define the input ifile = os.path.join(datapath, name) print ifile # Get the dimensions dims = cd.get_dimensions(ifile, 'u10m', toDatetime=True) dims['time'] = [pd.datetime(d.year, d.month, 1) for d in dims['time']] # load the data nc = Dataset(ifile) u10m = nc.variables['u10m'][:].squeeze() lat = dims['lat'] # Compute the properties jetmax, latofmax, latn, lats, jetwidth = jetprop(u10m, lat) # Assign to pd DataFrames df = pd.DataFrame(jetmax, index=dims['time']) df_umax = pd.concat([df_umax, df], axis=1) df = pd.DataFrame(latofmax, index=dims['time']) df_uloc = pd.concat([df_uloc, df], axis=1) df = pd.DataFrame(jetwidth, index=dims['time']) df_width = pd.concat([df_width, df], axis=1) # Test plots to make sure props were calculated right #ri = 49 #plt.close() #plt.plot( lat, uwnd[ri,:], 'k-o', linewidth=2) #plt.plot( lat, lat*0, 'k--') #plt.plot( latofmax[ri], jetmax[ri], 'rx', markersize=8, markeredgewidth=1) #plt.plot( [-90, 90], [ jetmax[ri],jetmax[ri] ], 'r--') #plt.plot( [-90, 90], [ jetmax[ri]*0.5, jetmax[ri]*0.5], 'r--') ##print latn[ri], lats[ri] #plt.plot( [latn[ri], latn[ri]], [-10, 10], 'r--') #plt.plot( [lats[ri], lats[ri]], [-10, 10], 'r--') #raw_input('go?') df_umax.columns = rean2 df_uloc.columns = rean2 df_width.columns = rean2 # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_reanalysis.h5') store = pd.HDFStore(out_file, 'a') store['width'] = df_width store['maxspd'] = df_umax store['locmax'] = df_uloc store.close()
def mk_rean_jetprop(datapath='./'): # list in the pre-defined list of files to use. rean = ['R1', 'R2', '20CR', 'ERA-Int', 'CFSR', 'MERRA'] rean2 = ['R1', 'R2', '20CR', 'ERA', 'CFSR', 'MERRA'] tail = '_u10m.mon.mean.nc' names = ['zonal-mean_remap_' + r + tail for r in rean] # Initialize some jetprop arrays df_umax = pd.DataFrame() df_uloc = pd.DataFrame() df_width = pd.DataFrame() # Loop over the reanalyses for i, name in enumerate(names): # define the input ifile = os.path.join(datapath, name) print ifile # Get the dimensions dims = cd.get_dimensions(ifile, 'u10m', toDatetime=True) dims['time'] = [pd.datetime(d.year, d.month, 1) for d in dims['time']] # load the data nc = Dataset(ifile) u10m = nc.variables['u10m'][:].squeeze() lat = dims['lat'] # Compute the properties jetmax, latofmax, latn, lats, jetwidth = jetprop(u10m, lat) # Assign to pd DataFrames df = pd.DataFrame(jetmax, index=dims['time']) df_umax = pd.concat([df_umax, df], axis=1) df = pd.DataFrame(latofmax, index=dims['time']) df_uloc = pd.concat([df_uloc, df], axis=1) df = pd.DataFrame(jetwidth, index=dims['time']) df_width = pd.concat([df_width, df], axis=1) # Test plots to make sure props were calculated right #ri = 49 #plt.close() #plt.plot( lat, uwnd[ri,:], 'k-o', linewidth=2) #plt.plot( lat, lat*0, 'k--') #plt.plot( latofmax[ri], jetmax[ri], 'rx', markersize=8, markeredgewidth=1) #plt.plot( [-90, 90], [ jetmax[ri],jetmax[ri] ], 'r--') #plt.plot( [-90, 90], [ jetmax[ri]*0.5, jetmax[ri]*0.5], 'r--') ##print latn[ri], lats[ri] #plt.plot( [latn[ri], latn[ri]], [-10, 10], 'r--') #plt.plot( [lats[ri], lats[ri]], [-10, 10], 'r--') #raw_input('go?') df_umax.columns = rean2 df_uloc.columns = rean2 df_width.columns = rean2 # Store the DataFrame in HDF5 out_file = os.path.join(datapath, 'zonmean_sam-jet_analysis_reanalysis.h5') store = pd.HDFStore(out_file, 'a') store['width'] = df_width store['maxspd'] = df_umax store['locmax'] = df_uloc store.close()