def main():
    ### Get Nino3.4 Index
    yrs= [2015,2020]  # Starting year and ending year
    #Nino3.4 (5N-5S, 170W-120W) [-170,-120,-5,5]
    nn34= vf.get_sst_areamean_from_HadISST([-170,-120,-5,5],yrs,remove_AC=True)
    ### And other region
    tio= vf.get_sst_areamean_from_HadISST([240,280,-10,0],yrs,remove_AC=True)
    spo= vf.get_sst_areamean_from_HadISST([-170,-120,-40,-30],yrs,remove_AC=True)

    ###---
    ### Plotting setup
    ###---
    fig=plt.figure()
    fig.set_size_inches(6.4,5)  ## (xsize,ysize)

    ###--- Suptitle
    suptit="Auto-correlation Example [HadISST, 2015-20]"
    fig.suptitle(suptit,fontsize=15,y=0.94,va='bottom',stretch='semi-condensed')

    ax1= fig.add_subplot(111)
    sub_tit= ''
    data=[nn34,tio,spo]
    vnames=['Ni{}o3.4'.format('\u00F1'),'Trop. IO','S. Pacific']
    autocorr_plot(ax1,data,vnames,sub_tit)

    ### Show or save
    outdir= '../Pics/'
    out_fig_nm= outdir+'V02.autocorr_example.SST_AM+Nino34.png'
    #fig.savefig(outfnm,dpi=100)   # dpi: pixels per inch
    fig.savefig(out_fig_nm,dpi=150,bbox_inches='tight')   # dpi: pixels per inch
    print(out_fig_nm)
    plt.show()
    return
Esempio n. 2
0
def main():
    ### Get Nino3.4 Index
    yrs = [2015, 2020]  # Starting year and ending year
    #Nino3.4 (5N-5S, 170W-120W) [-170,-120,-5,5]
    nn34 = vf.get_sst_areamean_from_HadISST([-170, -120, -5, 5],
                                            yrs,
                                            remove_AC=True)
    ### And other region
    tio = vf.get_sst_areamean_from_HadISST([240, 280, -10, 0],
                                           yrs,
                                           remove_AC=True)
    spo = vf.get_sst_areamean_from_HadISST([-170, -120, -40, -30],
                                           yrs,
                                           remove_AC=True)

    ###---
    ### Plotting setup
    ###---
    fig = plt.figure()
    fig.set_size_inches(6, 8.5)  ## (xsize,ysize)
    fig.subplots_adjust(hspace=0.3)
    ###--- Suptitle
    suptit = "Lead-Lag Correlation Example [HadISST,2015-20]"
    fig.suptitle(suptit,
                 fontsize=15,
                 y=0.95,
                 va='bottom',
                 stretch='semi-condensed')

    maxlag = 7

    ax1 = fig.add_subplot(211)
    sub_tit = '(a) Ni{}o3.4 vs. TIO'.format('\u00F1')
    data = [nn34, tio]
    vnames = ['Ni{}o3.4'.format('\u00F1'), 'TIO']
    corr_crt = estimate_significant_corr_coef(nn34, tio)[1]  # Choose 95% level
    llcorr_plot(ax1, data, vnames, sub_tit, maxlag=maxlag, corr_crt=corr_crt)

    ax1 = fig.add_subplot(212)
    sub_tit = '(b) Ni{}o3.4 vs. SPO'.format('\u00F1')
    data = [nn34, spo]
    vnames = ['Ni{}o3.4'.format('\u00F1'), 'SPO']
    corr_crt = estimate_significant_corr_coef(nn34, spo)[1]  # Choose 95% level
    llcorr_plot(ax1, data, vnames, sub_tit, maxlag=maxlag, corr_crt=corr_crt)

    ### Show or save
    outdir = '../Pics/'
    out_fig_nm = outdir + 'V04.estimate_corr_sig_level_example.SST_AM+Nino34.png'
    #fig.savefig(outfnm,dpi=100)   # dpi: pixels per inch
    fig.savefig(out_fig_nm, dpi=150,
                bbox_inches='tight')  # dpi: pixels per inch
    print(out_fig_nm)
    plt.show()
    return
def main():
    ### Get RMM index
    tgt_dates = (date(2010, 1, 1), date(2020, 12, 31))  # Recent 5 years
    time_info, pcs, phs, strs, miss_idx = vf.read_rmm_text(tgt_dates)

    ### Check missing
    if miss_idx.sum() > 0:
        print("There are missings:", miss_idx.sum())
        sys.exit()
    else:
        print("No missings")

    ### Data collected
    suptit = "Power Spectral Density of MJO Index [2010-20]"
    data = [pcs[:, 0], pcs[:, 1], strs]
    var_names = ['RMM1', 'RMM2', 'Amp']

    outdir = '../Pics/'
    out_fig_nm = outdir + 'V10.power_spectrum_RMM.png'
    plot_data = dict(data=data,
                     var_names=var_names,
                     out_fnm=out_fig_nm,
                     suptit=suptit)
    plot_PS(plot_data)

    return
Esempio n. 4
0
def main():
    ### Years to read data
    yrs = [2015, 2020]  # Starting year and ending year

    ### Get SST anomaly
    area_range = [-180, 180, -60, 60]  # [lon_range, lat_range]
    sstano, lat_info, lon_info = vf.get_sst_ano_from_HadISST(area_range,
                                                             yrs,
                                                             remove_AC=True)

    ### Re-arange SST by 5deg x 5deg cells
    nlat, nlon = lat_info['nlat'], lon_info['nlon']
    scaler = 5
    nlat2, nlon2 = nlat // scaler, nlon // scaler
    sstano = sstano.reshape([-1, nlat2, scaler, nlon2, scaler]).swapaxes(2, 3)
    sstano = sstano.reshape([-1, nlat2, nlon2, scaler**2])
    ms_idx = np.isnan(sstano[0, :, :, :]).sum(
        axis=2) > scaler**2 / 2  # missing if more than half is missing

    data2test = np.nanmean(sstano[:, ~ms_idx, :], axis=2)  # Mean for 5x5 cells

    ### Calculate dependency_level
    dof_coef1 = np.apply_along_axis(get_dof_coef_log_r1, 0, data2test)
    dof_coef2 = np.apply_along_axis(get_dof_coef_e_folding, 0, data2test)
    print(dof_coef1.min(), np.median(dof_coef1))
    print(dof_coef2.min(), np.median(dof_coef2))

    ### Split data for t-test
    data_prv, data_post = data2test[:36, :], data2test[
        36:, :]  # First 3 years and later 3 years

    ### Calculate significance level of t-test by different dof setting
    p_vals1 = get_ttest_pval_2d(dof_coef1, data_prv, data_post)
    p_vals2 = get_ttest_pval_2d(dof_coef2, data_prv, data_post)
    p_vals3 = get_ttest_pval_2d(np.ones_like(dof_coef1), data_prv,
                                data_post)  # Assume no dependency

    ### Construct new map of p-values
    new_map = np.full(ms_idx.shape, np.nan, dtype=float)
    data = []
    for pp in [p_vals1, p_vals2, p_vals3]:
        new_map[~ms_idx] = pp
        data.append(np.copy(new_map))

    ### Map info for displaying
    lon0, dlon = lon_info['lon0'], lon_info['dlon']
    lat0, dlat = lat_info['lat0'], lat_info['dlat']
    img_bound = [
        lon0 - dlon / 2, lon0 + dlon * (nlon + 0.5), lat0 - dlat / 2,
        lat0 + dlat * (nlat + 0.5)
    ]  # Exact range of data, necessary for imshow()
    ##-- Above bound is based on previous resolution, but it's ok since no change on area_boundary

    ### Prepare for plotting
    suptit = "t-test for Mean Diff., 2015-17 vs. 2018-20 [HadISST]"
    var_names = ['DoF= -N*log(r1)', 'DoF= N/(2*Te)', 'DoF= N']

    outdir = '../Pics/'
    out_fig_nm = outdir + 'V08.t-test_example_5deg_box.png'
    plot_data = dict(data=data,
                     var_names=var_names,
                     out_fnm=out_fig_nm,
                     img_bound=img_bound,
                     suptit=suptit)
    plot_map(plot_data)

    return
Esempio n. 5
0
def plot_map(pdata):
    '''
    Draw global map where dateline is on the center
    '''
    abc = 'abcdefgh'

    ###--- Create a figure
    fig = plt.figure()
    fig.set_size_inches(7, 8.5)  ## (xsize,ysize)

    ###--- Suptitle
    suptit = pdata['suptit']
    fig.suptitle(suptit,
                 fontsize=16,
                 y=0.97,
                 va='bottom',
                 stretch='semi-condensed')

    ###--- Map Projection
    center = 180  # Want to draw a map where dateline is on the center
    proj = ccrs.PlateCarree(central_longitude=center)
    data_crs = ccrs.PlateCarree()

    map_extent = [0., 359.9, -60, 60]  # Range to be shown
    img_range = pdata['img_bound']

    ###--- Set range of values to be shown
    #val_min, val_max= 0,0.5   <-- it is unnecessary for BoundaryNorm
    p_val_levels = [0, 0.01, 0.02, 0.05, 0.1, 0.2]

    ###--- Color map
    colors = ['darkred', '#FA325A', 'darkorange', 'forestgreen', '#C9CD71']
    cm = cls.LinearSegmentedColormap.from_list("cm5", colors)
    cm.set_under('1.')
    cm.set_over('1.')
    cm.set_bad('0.8')  # For the gridcell of NaN
    norm = cls.BoundaryNorm(p_val_levels, ncolors=cm.N, clip=False)

    left, right, top, bottom = 0.07, 0.97, 0.93, 0.12
    npnx, gapx, npny, gapy = 1, 0.05, len(pdata['data']), 0.06
    lx = (right - left - gapx * (npnx - 1)) / npnx
    ly = (top - bottom - gapy * (npny - 1)) / npny
    ix, iy = left, top

    props_imshow = dict(norm=norm,
                        origin='lower',
                        cmap=cm,
                        extent=img_range,
                        transform=data_crs)

    for i, (data, vnm) in enumerate(zip(pdata['data'], pdata['var_names'])):
        ax1 = fig.add_axes([ix, iy - ly, lx, ly], projection=proj)
        ax1.set_extent(map_extent, crs=data_crs)
        map1 = ax1.imshow(data, **props_imshow)

        subtit = '({}) {}'.format(abc[i], vnm)
        vf.map_common(ax1, subtit, data_crs, xloc=60, yloc=20)

        iy = iy - ly - gapy
    cb = vf.draw_colorbar(fig,
                          ax1,
                          map1,
                          type='horizontal',
                          size='panel',
                          gap=0.06,
                          extend='max')
    cb.set_label('Significance level', fontsize=11)
    cb.ax.set_xticklabels(
        ['{:.0f}%'.format((1 - val) * 100) for val in p_val_levels])
    cb.ax.tick_params(labelsize=10)

    ##-- Seeing or Saving Pic --##
    outfnm = pdata['out_fnm']
    print(outfnm)
    #fig.savefig(outfnm,dpi=100)   # dpi: pixels per inch
    fig.savefig(outfnm, dpi=150, bbox_inches='tight')  # dpi: pixels per inch
    # Defalut: facecolor='w', edgecolor='w', transparent=False
    plt.show()
    return
def main():
    ### Years to read data
    yrs = [2015, 2020]  # Starting year and ending year

    ### Get SST anomaly
    area_range = [-180, 180, -60, 60]  # [lon_range, lat_range]
    sstano, lat_info, lon_info = vf.get_sst_ano_from_HadISST(area_range,
                                                             yrs,
                                                             remove_AC=True)
    lat0, dlat, nlat = lat_info.values()
    lon0, dlon, nlon = lon_info.values()

    ### Weight by latitude
    lat_deg = np.arange(nlat) * dlat + lat0
    lat_wt = np.sqrt(np.cos(lat_deg / 180 * np.pi))  # Or use np.deg2rad()
    sstano = sstano * lat_wt[None, :, None]

    ### This is only for reducing computing time!!!
    ### Degrading data resolution (for convenience)
    scaler = 2
    nlat2, nlon2 = nlat // scaler, nlon // scaler
    sstano = sstano.reshape([-1, nlat2, scaler, nlon2,
                             scaler]).mean(axis=(2, 4))

    ### Find location of missing values
    ms_idx = np.isnan(sstano.mean(axis=0))  # one missing becomes missing
    print("Missing ratio= {:.2f}%".format(ms_idx.sum() /
                                          ms_idx.reshape(-1).shape[0] * 100))

    ### Get PCs and Eigenvectors
    maxnum = 10
    pc, evec, eval = PC_analysis(sstano[:, ~ms_idx], maxnum=maxnum)
    '''### Check if it works
    n1= sstano[:,~ms_idx][:,3600]
    n2= np.dot(pc,evec)[:,3600]
    plt.plot(n1); plt.plot(n2)
    plt.show() ; sys.exit()'''

    ### Restore to lat-lon format
    ev_map = np.full([maxnum, *ms_idx.shape], np.nan)
    ev_map[:, ~ms_idx] = evec
    img_bound = [
        lon0 - dlon / 2, lon0 + dlon * (nlon + 0.5), lat0 - dlat / 2,
        lat0 + dlat * (nlat + 0.5)
    ]  # Exact range of data, necessary for imshow()
    ##-- Above bound is based on previous resolution, but it's ok since no change on area_boundary

    ### Prepare for plotting
    suptit = 'PC and EOF of Global SST [HadISST, 2015-20]'
    tgt_nums = [1, 2, 3]
    mon_list = vf.get_monthly_dates(date(yrs[0], 1, 1),
                                    date(yrs[1], 12, 31),
                                    day=15,
                                    include_date2=True)

    outdir = '../Pics/'
    out_fig_nm = outdir + 'V09.EOF_example_HadISST.png'
    plot_data = dict(ev_map=ev_map,
                     pc=pc,
                     suptit=suptit,
                     out_fnm=out_fig_nm,
                     img_bound=img_bound,
                     mon_list=mon_list,
                     tgt_nums=tgt_nums)
    plot_map(plot_data)

    return
def plot_map(pdata):
    '''
    Draw PC time series on the top, and
       draw global map where dateline is on the center
    '''

    ###--- Create a figure
    fig = plt.figure()
    fig.set_size_inches(6, 10)  ## (xsize,ysize)

    ###--- Suptitle
    fig.suptitle(pdata['suptit'],
                 fontsize=16,
                 y=0.97,
                 va='bottom',
                 stretch='semi-condensed')

    ###--- Axes setting
    nk = len(pdata['tgt_nums'])  # Number of data to show
    left, right, top, bottom = 0.07, 0.93, 0.925, 0.1
    npnx, gapx, npny, gapy = 1, 0.05, nk + 1, 0.064
    lx = (right - left - gapx * (npnx - 1)) / npnx
    ly = (top - bottom - gapy * (npny - 1)) / npny
    ix, iy = left, top

    ###--- Top panel: PC time series
    ax1 = fig.add_axes([ix, iy - ly, lx, ly])
    colors = plt.cm.tab10(np.linspace(0.05, 0.95, 10))
    for i, k in enumerate(pdata['tgt_nums']):
        ax1.plot_date(pdata['mon_list'],
                      pdata['pc'][:, k],
                      fmt='-',
                      c=colors[i],
                      lw=2.5,
                      alpha=0.85,
                      label='PC{}'.format(k))
    iy = iy - ly - gapy
    subtit = '(a) '
    ax1.set_title(subtit, fontsize=12, ha='left', x=0.0)
    ax1.legend(bbox_to_anchor=(0.08, 1.02, .92, .10),
               loc='lower left',
               ncol=nk,
               mode="expand",
               borderaxespad=0.,
               fontsize=10)
    ax1.axhline(y=0., c='k', lw=0.8, ls='--')
    ax1.grid(ls=':')
    ax1.xaxis.set_major_formatter(DateFormatter('%b%Y'))
    ax1.yaxis.set_minor_locator(AutoMinorLocator(2))
    ax1.yaxis.set_ticks_position('both')
    ax1.tick_params(axis='both', labelsize=10)

    ###--- Next, draw global maps
    ###--- Map Projection
    center = 180  # Want to draw a map where dateline is on the center
    proj = ccrs.PlateCarree(central_longitude=center)
    data_crs = ccrs.PlateCarree()

    map_extent = [0., 359.9, -61, 61]  # Range to be shown
    img_range = pdata['img_bound']

    val_max = max(np.nanmin(pdata['ev_map']) * -1, np.nanmax(pdata['ev_map']))
    val_min, val_max = val_max * -0.9, val_max * 0.9
    abc = 'abcdefgh'

    ###--- Color map
    cm = plt.cm.get_cmap('RdBu_r').copy()
    cm.set_bad('0.9')  # For the gridcell of NaN

    props = dict(vmin=val_min,
                 vmax=val_max,
                 origin='lower',
                 extent=img_range,
                 cmap=cm,
                 transform=data_crs)

    for i, (data, k) in enumerate(zip(pdata['ev_map'], pdata['tgt_nums'])):
        ax2 = fig.add_axes([ix, iy - ly, lx, ly], projection=proj)
        ax2.set_extent(map_extent, crs=data_crs)
        map1 = ax2.imshow(data, **props)

        subtit = '({}) EOF{}'.format(abc[i + 1], k)
        vf.map_common(ax2,
                      subtit,
                      data_crs,
                      xloc=60,
                      yloc=20,
                      gl_lab_locator=[True, True, False, True])

        iy = iy - ly - gapy
    vf.draw_colorbar(fig,
                     ax2,
                     map1,
                     type='horizontal',
                     size='panel',
                     gap=0.06,
                     extend='both',
                     width=0.02)

    ##-- Seeing or Saving Pic --##
    outfnm = pdata['out_fnm']
    print(outfnm)
    #fig.savefig(outfnm,dpi=100)   # dpi: pixels per inch
    fig.savefig(outfnm, dpi=150, bbox_inches='tight')  # dpi: pixels per inch
    # Defalut: facecolor='w', edgecolor='w', transparent=False
    plt.show()
    return