def plot_potential_skillgain():

    res = pd.read_csv('/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv', index_col=0)

    modes = ['abs', 'anom_lst', 'anom_lt', 'anom_st']

    f = plt.figure(figsize=(15,8))

    for i, mode in enumerate(modes):

        R = res[f'ubrmse_grid_{mode}_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
        P = res[f'ubrmse_grid_{mode}_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

        R2 = res[f'r2_grid_{mode}_m_CLSM_tc_ASCAT_SMAP_CLSM']

        K = P / (R + P)
        NSR = (1 - R2) / R2

        R2upd = 1 / (1 + (1 - K) * NSR)

        res['gain_pot'] = R2upd - R2

        plt.subplot(2, 2, i+1)
        plot_ease_img(res, 'gain_pot', fontsize=12, cbrange=[-0.2,0.2], cmap=cc.cm.bjy, log_scale=False, title=mode, plot_cb=True)


    fout = f'/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/plots/skillgain_pot_Pcorr_simple.png'
    f.savefig(fout, dpi=300, bbox_inches='tight')
    plt.close()
def plot_kalman_gains():

    res = pd.read_csv('/Users/u0116961/Documents/work/validation_good_practice/CI80/ASCAT_SMAP_CLSM/result.csv', index_col=0)

    # k_repr_abs = [(1,0), (2,0), (3,0), (1,0.01), (1,0.02)]
    # k_repr_lt = [(1,0), (2,0), (3,0), (1,0.01), (1,0.02)]
    # k_repr_lst = [(1,0), (2,0), (3,0), (1,0.01), (1,0.02)]
    # k_repr_st = [(1,0), (1.5,0), (2.5,0), (1,0.005), (1,0.01)]
    k_repr_abs = [(0.8,0), (1,0), (1.5,0)]
    k_repr_lt = [(0.8,0), (1,0), (1.5,0)]
    k_repr_lst = [(0.8,0), (1,0), (1.5,0)]
    k_repr_st = [(0.8,0), (1,0), (1.5,0)]

    modes = ['abs', 'anom_lt', 'anom_lst', 'anom_st']

    f = plt.figure(figsize=(23,10))

    for j, (mode, k_repr) in enumerate(zip(modes,[k_repr_abs,k_repr_lt,k_repr_lst,k_repr_st])):
        for i, k in enumerate(k_repr):

            k_m, k_a = k
            tag_r = f'ubrmse_grid_{mode}_m_SMAP_tc_ASCAT_SMAP_CLSM'
            tag_p = f'ubrmse_grid_{mode}_m_CLSM_tc_ASCAT_SMAP_CLSM'

            res['tmp_tag'] = 1 / (1 + (res[tag_r] * k_m + k_a)**2 / res[tag_p] ** 2)

            res.loc[res['tmp_tag']<0.5,'tmp_tag'] **= 1.75

            plt.subplot(3, 4, i*4 + 1 + j)
            plot_ease_img(res, 'tmp_tag', fontsize=12, cbrange=[0,1], cmap='seismic_r', log_scale=False, title=f'{mode}: k_m = {k_m} / k_a = {k_a}', plot_cb=True)


    fout = f'/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/plots/obs_weight.png'
    f.savefig(fout, dpi=300, bbox_inches='tight')
    plt.close()
def plot_TCA_reliability():

    res = pd.read_csv('/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv', index_col=0)

    modes = ['abs', 'anom_lst', 'anom_lt', 'anom_st']

    f = plt.figure(figsize=(15,8))

    cmap = mpl.cm.get_cmap("magma", 8)

    for i, mode in enumerate(modes):

        p_a_s = res[f'p_grid_{mode}_p_ASCAT_SMAP']
        p_a_c = res[f'p_grid_{mode}_p_ASCAT_CLSM']
        p_s_c = res[f'p_grid_{mode}_p_SMAP_CLSM']
        r_a_s = res[f'r_grid_{mode}_p_ASCAT_SMAP']
        r_a_c = res[f'r_grid_{mode}_p_ASCAT_CLSM']
        r_s_c = res[f'r_grid_{mode}_p_SMAP_CLSM']

        res[f'p_mask_{mode}'] = ((p_a_s <= 0.05)&(r_a_s > 0.2)) * 1 + \
                                ((p_a_c <= 0.05)&(r_a_c > 0.2)) * 2 + \
                                ((p_s_c <= 0.05)&(r_s_c > 0.2)) * 4

        plt.subplot(2, 2, i+1)
        plot_ease_img(res, f'p_mask_{mode}', fontsize=12, cbrange=[0,8], cmap=cmap, log_scale=False, title=mode, plot_cb=True)



    plt.tight_layout()
    plt.show()
Esempio n. 4
0
def plot_orthogonality_check(dir_out):

    sensors = ['ASCAT', 'SMAP', 'CLSM']

    res = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)

    figsize = (11, 3)
    fontsize = 10
    # cb = [-0.02, 0.02]
    cb = [-0.02, 0.02]

    f = plt.figure(figsize=figsize)

    valid = pd.Series(True, index=res.index)
    for mode in ['anom_lt', 'anom_st', 'anom_lst']:
        r_asc_smap = res[f'r_grid_{mode}_p_ASCAT_SMAP']
        r_asc_clsm = res[f'r_grid_{mode}_p_ASCAT_CLSM']
        r_smap_clsm = res[f'r_grid_{mode}_p_SMAP_CLSM']
        thres = 0.2
        valid &= ((r_asc_smap > thres) & (r_asc_smap > thres) &
                  (r_asc_smap > thres))

    ind_valid = res[valid].index

    for i, s in enumerate(sensors[1::]):

        plt.subplot(1, 2, i + 1)

        tag_lt = 'ubrmse_grid_anom_lt_m_' + s + '_tc_ASCAT_SMAP_CLSM'
        tag_st = 'ubrmse_grid_anom_st_m_' + s + '_tc_ASCAT_SMAP_CLSM'
        tag_lst = 'ubrmse_grid_anom_lst_m_' + s + '_tc_ASCAT_SMAP_CLSM'

        res['diff'] = (res[tag_lst] - np.sqrt(res[tag_lt]**2 + res[tag_st]**2))

        im_r = plot_ease_img(res.reindex(ind_valid),
                             'diff',
                             fontsize=fontsize + 2,
                             cbrange=cb,
                             cmap=cc.cm.bjy,
                             print_meanstd=True)

        plt.title(s, fontsize=fontsize)

    plot_centered_cbar(f,
                       im_r,
                       2,
                       fontsize=fontsize - 2,
                       bottom=0.00,
                       hspace=0.030,
                       pad=0.02,
                       wdth=0.04)

    fout = dir_out / 'orthogonality_verification.png'
    f.savefig(fout, dpi=300, bbox_inches='tight')
    plt.close()
Esempio n. 5
0
def plot_tca_uncertainties(dir_out):

    sensors = ['ASCAT', 'SMAP', 'CLSM']

    res = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)

    figsize = (15, 5)
    fontsize = 12
    cb = [0, 0.04]

    modes = ['anom_lst', 'anom_lt', 'anom_st']
    titles = ['Anomalies', 'LF signal', 'HF signal']
    labels = [
        '$\widehat{std}(\epsilon_{\Theta,smap})$',
        '$\widehat{std}(\epsilon_{\Theta,clsm})$'
    ]

    f = plt.figure(figsize=figsize)

    n = 0
    pos = []
    for s, l in zip(sensors[1::], labels):
        for mode, title in zip(modes, titles):
            n += 1

            plt.subplot(2, 3, n)

            tag = 'ubrmse_grid_' + mode + '_m_' + s + '_tc_ASCAT_SMAP_CLSM'

            r_asc_smap = res[f'r_grid_{mode}_p_ASCAT_SMAP']
            r_asc_clsm = res[f'r_grid_{mode}_p_ASCAT_CLSM']
            r_smap_clsm = res[f'r_grid_{mode}_p_SMAP_CLSM']
            thres = 0.2
            ind_valid = res[(r_asc_smap > thres) & (r_asc_smap > thres) &
                            (r_asc_smap > thres)].index

            im_r = plot_ease_img(res.reindex(ind_valid),
                                 tag,
                                 fontsize=fontsize,
                                 cbrange=cb,
                                 cmap='viridis',
                                 print_mean=True)
            # if (n == 6) | (n == 7):
            #     pos += [im_r.axes.get_position()]

            if s == 'SMAP':
                plt.title(title, fontsize=fontsize)
            if mode == 'anom_lst':
                plt.ylabel(l, fontsize=fontsize)

    plot_centered_cbar(f, im_r, 3, fontsize=fontsize - 2, bottom=0.07)

    fout = dir_out / 'tca_uncertainties.png'
    f.savefig(fout, dpi=300, bbox_inches='tight')
    plt.close()
def plot_eval():

    analysis_only = True
    relative = True
    snr = False

    pc = 'noPcorr'

    ref_exp = f'OL_{pc}'
    # ref_exp = f'DA_{pc}_4K'

    ana = 'ana_' if analysis_only else ''
    rel = 'rel_' if relative else ''
    res = pd.read_csv(Path(
        f'/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/validation/ascat_eval.csv'
    ),
                      index_col=0)
    fbase = Path(
        f'~/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/plots/ascat_eval/ref_{ref_exp}'
    ).expanduser()
    if not fbase.exists():
        Path.mkdir(fbase, parents=True)

    # topo = pd.read_csv('/Users/u0116961/data_sets/ASCAT/static_layer/topographic_complexity_conus.csv', index_col=0, squeeze=True, header=None)

    cbr_r = [0.6, 1.4] if relative else [0.2, 0.7]
    cbr_r = [-3, 3] if snr else [-0.2, 0.2] if relative else [0.2, 0.7]
    cbr_len = [2e2, 2e3]

    runs = [f'OL_{pc}'] + [
        f'DA_{pc}_{err}'
        for err in ['4K', 'abs', 'anom_lt', 'anom_lst', 'anom_st']
    ]

    modes = ['abs', 'anom_st', 'anom_lt', 'anom_lst']

    # params = ['len',]
    # cbranges = [cbr_len]

    params = ['r_corr']
    cbranges = [cbr_r, cbr_r]

    # r_tca_
    # 'len', 'r',  'r_corr', 'ana_len', 'ana_r', 'ana_r_corr'

    fontsize = 12

    for p, cbr in zip(params, cbranges):

        # cmap = 'seismic_r' if ('r' in p) & relative else 'YlGn'
        cmap = cc.cm.bjy

        for i, m in enumerate(modes):

            f = plt.figure(figsize=(25, 9))

            for cnt, r in enumerate(runs):

                ncols = 3
                plt.subplot(2, ncols, cnt + 1)

                col = f'{ana}{p}_{r}_{m}'
                ref_col = f'{ana}{p}_{ref_exp}_{m}'

                if relative and (p != 'len'):

                    if snr:
                        SNR1 = 10**np.log10(res[col]**2 / (1 - res[col]**2))
                        SNR2 = 10**np.log10(res[ref_col]**2 /
                                            (1.01 - res[ref_col]**2))
                        res[f'{col}_diff'] = SNR1 - SNR2
                    else:
                        res[f'{col}_diff'] = res[col]**2 - res[ref_col]**2
                        # res[f'{col}_diff'] = (res[col]**2 - res[ref_col]**2) / res[ref_col]**2

                    # res.loc[topo.reindex(res.index) > 15, f'{col}_diff'] = np.nan

                    ext = '_diff'
                else:
                    ext = ''

                log_scale = True if 'len' in p else False
                im = plot_ease_img(res,
                                   f'{col}{ext}',
                                   title=f'{r}',
                                   cmap=cmap,
                                   cbrange=cbr,
                                   fontsize=fontsize,
                                   print_median=True,
                                   log_scale=log_scale)
                # im = plot_ease_img(res, f'{col}{ext}' , title=f'{e} / {m}', cmap=cmap, cbrange=cbr, fontsize=fontsize, print_median=True, log_scale=log_scale)
                # if (i == 2) & (j == 1):
                #     ax = im.axes

            plot_centered_cbar(f, im, ncols, fontsize=fontsize)

            fname = f'{ana}rel_snr_{m}' if snr else f'{ana}{rel}{p}_{m}.png'

            f.savefig(fbase / fname, dpi=300, bbox_inches='tight')
            plt.close()
def plot_smos_eval_diffs():

    difference = True

    sub = 'skillgain' if difference else ''

    res = pd.read_csv(Path(
        f'~/Documents/work/MadKF/CLSM/SMOS40/validation/multiperiod/ascat/ascat_eval_smos.csv'
    ).expanduser(),
                      index_col=0)
    fbase = Path(f'~/Documents/work/MadKF/CLSM/SMOS40/plots/multiperiod/ascat'
                 ).expanduser() / sub

    n_subits = 4
    exps = ['SMOS40_it621', 'SMOS40_it622', 'SMOS40_it623', 'SMOS40_it624']
    modes = ['absolute', 'shortterm', 'longterm']

    per = [
        '2010-2020', '2010-2015', '2015-2020', '2010-2012', '2012-2015',
        '2015-2017', '2017-2020'
    ]

    p = 'r'
    cbr = [-0.2, 0.2]

    fontsize = 10

    cmap = 'seismic_r' if ('r' in p) else 'YlGn'

    combs = list(combinations(np.arange(7), 2))
    combs = [combs[i] for i in [0, 1, 6, 7, 8, 13, 14, 15, 18, 20]]

    for j, e in enumerate(exps):

        froot = fbase / e
        if not froot.exists():
            Path.mkdir(froot, parents=True)

        for i, m in enumerate(modes):

            f = plt.figure(figsize=(26, 6))

            for k, comb in enumerate(combs):

                plt.subplot(2, 5, k + 1)

                if difference:
                    res[f'diff_{k}'] = (res[f'p{comb[0]}_{p}_{e}_{m}'] - res[f'p{comb[0]}_{p}_open_loop_{m}']) \
                                       - (res[f'p{comb[1]}_{p}_{e}_{m}'] - res[f'p{comb[1]}_{p}_open_loop_{m}'])
                else:
                    res[f'diff_{k}'] = res[f'p{comb[0]}_{p}_{e}_{m}'] - res[
                        f'p{comb[1]}_{p}_{e}_{m}']

                title = f'{per[comb[0]]} minus {per[comb[1]]}'

                im = plot_ease_img(res,
                                   f'diff_{k}',
                                   title=title,
                                   cmap=cmap,
                                   cbrange=cbr,
                                   fontsize=fontsize,
                                   print_median=True)

            plot_centered_cbar(f, im, 5, fontsize=fontsize, col_offs=0)

            f.savefig(froot / f'{m}.png', dpi=300, bbox_inches='tight')
            plt.close()
def plot_smos_eval():

    res = pd.read_csv(Path(
        f'~/Documents/work/MadKF/CLSM/SMOS40/validation/multiperiod/ascat_eval_smos.csv'
    ).expanduser(),
                      index_col=0)
    fbase = Path(
        f'~/Documents/work/MadKF/CLSM/SMOS40/plots/multiperiod/per_period'
    ).expanduser()
    if not fbase.exists():
        Path.mkdir(fbase, parents=True)

    cbr_r = [0, 0.8]
    # cbr_r = [-0.2,0.2]
    # cbr_len = [2e2, 2e3]
    cbr_len = [0, 250]

    n_subits = 4
    exps = ['SMOS40_it621', 'SMOS40_it622', 'SMOS40_it623', 'SMOS40_it624']
    modes = ['absolute', 'shortterm', 'longterm']

    # ref_exp = 'SMOS40_it622'

    params = ['r', 'len']
    cbranges = [cbr_r, cbr_len]

    # params = ['r',]
    # cbranges = [cbr_r,]

    fontsize = 12

    for p, cbr in zip(params, cbranges):

        cmap = 'seismic_r' if ('r' in p) else 'YlGn'

        for per in np.arange(7):

            f = plt.figure(figsize=(25, 10))

            for i, m in enumerate(modes):
                for j, e in enumerate(exps):

                    plt.subplot(len(modes), len(exps), len(exps) * i + j + 1)

                    col = f'p{per}_{p}_{e}_{m}'

                    im = plot_ease_img(res,
                                       f'{col}',
                                       title=f'{e} / {m}',
                                       cmap=cmap,
                                       cbrange=cbr,
                                       fontsize=fontsize,
                                       print_median=True,
                                       log_scale=False)

            plot_centered_cbar(f, im, len(exps), fontsize=fontsize)

            f.savefig(fbase / f'{p}_per{per}.png',
                      dpi=300,
                      bbox_inches='tight')
            plt.close()
def plot_ascat_smap_eval(iteration):

    analysis_only = True
    relative = True
    ref_exp = 'open_loop'

    sub = 'ana_' if analysis_only else ''
    fext = '_rel' if relative else '_abs'

    res_ascat = pd.read_csv(
        f'/Users/u0116961/Documents/work/MadKF/CLSM/SMAP/validation/iter_{iteration}/ascat_eval.csv',
        index_col=0)
    res_smap = pd.read_csv(
        f'/Users/u0116961/Documents/work/MadKF/CLSM/SMAP/validation/iter_{iteration}/smap_eval.csv',
        index_col=0)
    fbase = Path(
        f'/Users/u0116961/Documents/work/MadKF/CLSM/SMAP/plots/iter_{iteration}'
    )
    if not fbase.exists():
        Path.mkdir(fbase, parents=True)

    cbr_r = [0.6, 1.4] if relative else [0.2, 0.7]
    cbr_r = [-0.4, 0.4] if relative else [0.2, 0.7]
    cbr_len = [500, 1500]

    exps = ['SMOSSMAP_short', 'SMAP_it11']
    modes = ['absolute', 'longterm', 'shortterm']

    # params = ['len', 'r']
    # cbranges = [cbr_len, cbr_r]

    params = ['r']
    cbranges = [cbr_r]

    fontsize = 12

    for p, cbr in zip(params, cbranges):

        cmap = 'seismic_r' if (p == 'r') & relative else 'YlGn'

        f = plt.figure(figsize=(22, 8))

        for j, e in enumerate(exps):
            for i, m in enumerate(modes):

                plt.subplot(len(exps), len(modes), len(modes) * j + i + 1)

                col = f'{sub}{p}_{e}_{m}'
                if relative and (p != 'len'):
                    ref_col = f'{sub}{p}_{ref_exp}_{m}'
                    res_ascat[f'{col}_diff'] = (res_ascat[col] -
                                                res_ascat[ref_col]) / (
                                                    1 - res_ascat[ref_col])
                    res_smap[f'{col}_diff'] = (res_smap[col] -
                                               res_smap[ref_col]) / (
                                                   1 - res_smap[ref_col])
                    ext = '_diff'
                else:
                    ext = ''

                if j == 0:
                    im = plot_ease_img(res_ascat,
                                       f'{col}{ext}',
                                       title=f'Skill gain {m} (ref: ASCAT)',
                                       cmap=cmap,
                                       cbrange=cbr,
                                       fontsize=fontsize,
                                       print_median=True)
                else:
                    im = plot_ease_img(res_smap,
                                       f'{col}{ext}',
                                       title=f'Skill gain {m} (ref: SMAP)',
                                       cmap=cmap,
                                       cbrange=cbr,
                                       fontsize=fontsize,
                                       print_median=True)

        f.subplots_adjust(wspace=0.04, hspace=0.025, bottom=0.06)
        # pos1 = f.axes[-3].get_position()
        # pos2 = f.axes[-4].get_position()
        # x1 = (pos1.x0 + pos1.x1) / 2
        # x2 = (pos2.x0 + pos2.x1) / 2
        pos = f.axes[-2].get_position()
        x1 = pos.x0
        x2 = pos.x1
        cbar_ax = f.add_axes([x1, 0.03, x2 - x1, 0.03])
        cbar = f.colorbar(im, orientation='horizontal', cax=cbar_ax)
        for t in cbar.ax.get_xticklabels():
            t.set_fontsize(fontsize)

        f.savefig(fbase / f'ascat_smap_eval.png', dpi=300, bbox_inches='tight')
        plt.close()
Esempio n. 10
0
def plot_ascat_eval():

    analysis_only = True
    relative = True
    ref_exp = 'open_loop'

    sub = 'ana_' if analysis_only else ''
    fext = '_rel' if relative else '_abs'

    res = pd.read_csv(
        '/Users/u0116961/Documents/work/LDAS/2020-03_scaling/validation/ascat_eval.csv',
        index_col=0)

    cbr_r = [0.6, 1.4] if relative else [0.2, 0.7]
    cbr_r = [-0.3, 0.3] if relative else [0.2, 0.7]
    cbr_len = [500, 1500]

    exps = ['SMOSSMAP_short', 'MadKF_SMOS40']
    # exps = ['SMOS_long', 'SMOSSMAP_long', 'SMOS_short', 'SMOSSMAP_short', 'SMOSSMAP_short_PCA', 'SMAP_short']
    modes = ['absolute', 'longterm', 'shortterm']

    params = ['len', 'r']
    cbranges = [cbr_len, cbr_r]

    # params = ['r']
    # cbranges = [cbr_r]

    fontsize = 12

    for p, cbr in zip(params, cbranges):

        cmap = 'seismic_r' if (p == 'r') & relative else 'YlGn'

        f = plt.figure(figsize=(10, 8))

        for i, m in enumerate(modes):
            for j, e in enumerate(exps):

                plt.subplot(len(modes), len(exps), len(exps) * i + j + 1)

                col = f'{sub}{p}_{e}_{m}' if p != 'len' else f'{sub}{p}_{e}{m}'
                if relative and (p != 'len'):
                    ref_col = f'{sub}{p}_{ref_exp}_{m}'
                    res[f'{col}_diff'] = (res[col] -
                                          res[ref_col]) / (1 - res[ref_col])
                    ext = '_diff'
                else:
                    ext = ''

                im = plot_ease_img(res,
                                   f'{col}{ext}',
                                   title=f'{e} / {m}',
                                   cmap=cmap,
                                   cbrange=cbr,
                                   fontsize=fontsize,
                                   print_median=True)
                # if (i == 2) & (j == 1):
                #     ax = im.axes

        f.subplots_adjust(wspace=0.04, hspace=0.025, bottom=0.06)
        pos1 = f.axes[-2].get_position()
        pos2 = f.axes[-1].get_position()
        x1 = (pos1.x0 + pos1.x1) / 2
        x2 = (pos2.x0 + pos2.x1) / 2
        cbar_ax = f.add_axes([x1, 0.03, x2 - x1, 0.03])
        cbar = f.colorbar(im, orientation='horizontal', cax=cbar_ax)
        for t in cbar.ax.get_xticklabels():
            t.set_fontsize(fontsize)

        fout = f'/Users/u0116961/Documents/work/LDAS/2020-03_scaling/plots/ascat_eval_{p}{sub}{fext}.png'
        f.savefig(fout, dpi=300, bbox_inches='tight')
        plt.close()
def plot_potential_skillgain_decomposed():

    res = pd.read_csv('/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv', index_col=0)

    R_abs = res[f'ubrmse_grid_abs_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_abs = res[f'ubrmse_grid_abs_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_anom_lt = res[f'ubrmse_grid_anom_lt_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_anom_lt = res[f'ubrmse_grid_anom_lt_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_anom_lst = res[f'ubrmse_grid_anom_lst_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_anom_lst = res[f'ubrmse_grid_anom_lst_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_anom_st = res[f'ubrmse_grid_anom_st_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_anom_st = res[f'ubrmse_grid_anom_st_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_clim = (R_abs - R_anom_lt - R_anom_st).abs()
    P_clim = (P_abs - P_anom_lt - P_anom_st).abs()


    # Baseline estimates
    R2 = res[f'r2_grid_abs_m_CLSM_tc_ASCAT_SMAP_CLSM']
    SNR = R2 / (1 - R2)
    SIG = SNR * P_abs

    modes = ['abs', 'anom_lt', 'anom_lst', 'anom_st']
    result = pd.DataFrame(index=res.index, columns=modes)
    result['row'] = res.row
    result['col'] = res.col

    for cnt, i in enumerate(res.index):
        print(f'{cnt} / {len(res)}')

        R11 = R_clim.loc[i]
        R22 = R_anom_lt.loc[i]
        R33 = R_anom_st.loc[i]
        P11 = P_clim.loc[i]
        P22 = P_anom_lt.loc[i]
        P33 = P_anom_st.loc[i]

        r_c_l = 0.2
        r_c_s = 0.8
        r_l_s = 0.2

        R12 = r_c_l * np.sqrt(R11 * R22)
        R13 = r_c_s * np.sqrt(R11 * R33)
        R23 = r_l_s * np.sqrt(R22 * R33)
        P12 = r_c_l * np.sqrt(P11 * P22)
        P13 = r_c_s * np.sqrt(P11 * P33)
        P23 = r_l_s * np.sqrt(P22 * P33)

        S = np.matrix([[R11, R12, R13, 0,   0,   0  ],
                       [R12, R22, R23, 0,   0,   0  ],
                       [R13, R23, R33, 0,   0,   0  ],
                       [0,   0,   0,   P11, P12, P13],
                       [0,   0,   0,   P12, P22, P23],
                       [0,   0,   0,   P13, P23, P33]])

        for mode in modes:

            P = res.loc[i, f'ubrmse_grid_{mode}_m_CLSM_tc_ASCAT_SMAP_CLSM'] ** 2
            R = res.loc[i, f'ubrmse_grid_{mode}_m_SMAP_tc_ASCAT_SMAP_CLSM'] ** 2
            K = P / (R + P)

            A = np.matrix([K, K, K, 1-K, 1-K, 1-K])
            P_upd = (A * S * A.T)[0,0]

            NSR_upd = P_upd / SIG.loc[i]
            R2upd = 1 / (1 + NSR_upd)

            result.loc[i, mode] = R2upd - R2.loc[i]

    f = plt.figure(figsize=(15,8))
    for i, mode in enumerate(modes):
        plt.subplot(2, 2, i+1)
        plot_ease_img(result, mode, fontsize=12, cbrange=[-0.2,0.2], cmap=cc.cm.bjy, log_scale=False, title=mode, plot_cb=True)

    fout = f'/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/plots/skillgain_pot_Pcorr_corr_asym.png'
    f.savefig(fout, dpi=300, bbox_inches='tight')
    plt.close()
Esempio n. 12
0
def plot_uncertainty_ratios(dir_out):

    sensors = ['ASCAT', 'SMAP', 'CLSM']

    res = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)
    res_tc = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)

    tg = GEOSldas_io().grid.tilegrids
    res_cols = res.col.values - tg.loc['domain', 'i_offg']
    res_rows = res.row.values - tg.loc['domain', 'j_offg']

    figsize = (16, 4)
    fontsize = 10
    cb = [-10, 10]
    cmap = cc.cm.bjy

    modes = ['4K', 'anom_lst', 'anom_lt', 'anom_st']
    titles = ['4K benchmark', 'Anomalies', 'LF signal', 'HF signal']

    ios = [
        GEOSldas_io('ObsFcstAna',
                    exp=f'NLv4_M36_US_DA_SMAP_Pcorr_{mode}').timeseries
        for mode in modes
    ]
    io_ol = GEOSldas_io('ObsFcstAna', exp=f'NLv4_M36_US_OL_Pcorr').timeseries

    grid = GEOSldas_io().grid

    f = plt.figure(figsize=figsize)

    for n, (mode, title, io_da) in enumerate(zip(modes, titles, ios)):

        if n > 0:
            plt.subplot(2, 4, n + 1)
            tagP = 'ubrmse_grid_' + mode + '_m_CLSM_tc_ASCAT_SMAP_CLSM'
            tagR = 'ubrmse_grid_' + mode + '_m_SMAP_tc_ASCAT_SMAP_CLSM'
            res['tmp'] = 10 * np.log10(res[tagP]**2 / res[tagR]**2)

            r_asc_smap = res_tc[f'r_grid_{mode}_p_ASCAT_SMAP']
            r_asc_clsm = res_tc[f'r_grid_{mode}_p_ASCAT_CLSM']
            r_smap_clsm = res_tc[f'r_grid_{mode}_p_SMAP_CLSM']
            thres = 0.2
            ind_valid = res_tc[(r_asc_smap > thres) & (r_asc_smap > thres) &
                               (r_asc_smap > thres)].index

            img = plot_ease_img(res.reindex(ind_valid),
                                'tmp',
                                fontsize=fontsize,
                                cbrange=cb,
                                cmap=cmap,
                                plot_cb=False)
            plt.title(title, fontsize=fontsize)
            if n == 1:
                plt.ylabel('TCA unc. ratio', fontsize=fontsize)

    for n, (mode, title, io_da) in enumerate(zip(modes, titles, ios)):

        if mode != '4K':
            r_asc_smap = res_tc[f'r_grid_{mode}_p_ASCAT_SMAP']
            r_asc_clsm = res_tc[f'r_grid_{mode}_p_ASCAT_CLSM']
            r_smap_clsm = res_tc[f'r_grid_{mode}_p_SMAP_CLSM']
            thres = 0.2
            ind_valid = res_tc[(r_asc_smap > thres) & (r_asc_smap > thres) &
                               (r_asc_smap > thres)].index
        else:
            ind_valid = res.index

        avg = np.full(io_da['obs_obsvar'].shape[1::], np.nan)
        for spc in range(4):
            tmp1 = io_da['obs_obsvar'][:, spc, :, :].values
            tmp2 = io_ol['obs_fcstvar'][:, spc, :, :].values
            avg[spc, :, :] = np.nanmean(tmp2 / tmp1, axis=0)
        avg = np.nanmean(avg, axis=0)

        res['avg'] = 10 * np.log10(avg[res_rows, res_cols])

        plt.subplot(2, 4, n + 5)
        img = plot_ease_img(res.reindex(ind_valid),
                            'avg',
                            fontsize=fontsize,
                            cbrange=cb,
                            cmap=cmap,
                            plot_cb=False)
        if n == 0:
            plt.title(title, fontsize=fontsize)
            plt.ylabel('Ens. var. ratio', fontsize=fontsize)

    plot_centered_cbar(f, img, 4, fontsize=fontsize, bottom=0.07)

    fout = dir_out / 'uncertainty_ratio.png'
    f.savefig(fout, dpi=300, bbox_inches='tight')
    plt.close()
Esempio n. 13
0
def plot_predicted_skillgain(dir_out):

    res = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)

    R_anom_lst = res[f'ubrmse_grid_anom_lst_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_anom_lst = res[f'ubrmse_grid_anom_lst_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_anom_lt = res[f'ubrmse_grid_anom_lt_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_anom_lt = res[f'ubrmse_grid_anom_lt_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_anom_st = res[f'ubrmse_grid_anom_st_m_SMAP_tc_ASCAT_SMAP_CLSM']**2
    P_anom_st = res[f'ubrmse_grid_anom_st_m_CLSM_tc_ASCAT_SMAP_CLSM']**2

    R_lt_st_arr = (R_anom_lst - (R_anom_lt + R_anom_st)) / 2
    P_lt_st_arr = (P_anom_lst - (P_anom_lt + P_anom_st)) / 2

    P_lt_st_arr_rho = P_lt_st_arr / np.sqrt(P_anom_lt * P_anom_st)

    # Baseline estimates
    # R2 = res[f'r2_grid_abs_m_CLSM_tc_ASCAT_SMAP_CLSM']
    R2 = res[f'r2_grid_anom_lst_m_CLSM_tc_ASCAT_SMAP_CLSM']
    SNR = R2 / (1 - R2)
    SIG = SNR * P_anom_lst

    modes = ['anom_lt', 'anom_st', 'anom_lst', 'anom_lst']
    titles = [
        'LF signal', 'HF signal', 'Anomalies (lumped)', 'Anomalies (joint)'
    ]
    result = pd.DataFrame(index=res.index, columns=modes)
    result['row'] = res.row
    result['col'] = res.col

    for i, mode in enumerate(modes):

        P = res[f'ubrmse_grid_{mode}_m_CLSM_tc_ASCAT_SMAP_CLSM']**2
        R = res[f'ubrmse_grid_{mode}_m_SMAP_tc_ASCAT_SMAP_CLSM']**2

        K = P / (R + P)

        if i < 3:
            K_4K = P / (4**2 + P)

            P_upd = K * R + (1 - K) * P
            NSR_upd = P_upd / SIG
            R2upd = 1 / (1 + NSR_upd)

            result[f'{i}_4K'] = np.sqrt(R2upd) - np.sqrt(R2)

        if i < 2:
            # Single signal assimilation
            # R2 = res[f'r2_grid_{mode}_m_CLSM_tc_ASCAT_SMAP_CLSM']
            # NSR = (1 - R2) / R2
            # R2upd = 1 / (1 + (1 - K) * NSR)

            P_upd = (1 - K) * P
            NSR_upd = P_upd / SIG
            R2upd = 1 / (1 + NSR_upd)

            result[i] = np.sqrt(R2upd) - np.sqrt(R2)
            result[f'P_upd_{i}'] = P_upd

        elif i == 2:
            # Joint signal assimilation
            result[i] = np.nan

            for cnt, idx in enumerate(res.index):
                print(f'{cnt} / {len(res)}')

                R11 = R_anom_lt.loc[idx]
                R22 = R_anom_st.loc[idx]
                R12 = R_lt_st_arr.loc[idx]

                P11 = P_anom_lt.loc[idx]
                P22 = P_anom_st.loc[idx]
                P12 = P_lt_st_arr.loc[idx]

                S = np.matrix([[R11, R12, 0, 0], [R12, R22, 0, 0],
                               [0, 0, P11, P12], [0, 0, P12, P22]])

                A = np.matrix(
                    [K.loc[idx], K.loc[idx], 1 - K.loc[idx], 1 - K.loc[idx]])
                P_upd = (A * S * A.T)[0, 0]
                NSR_upd = P_upd / SIG.loc[idx]
                R2upd = 1 / (1 + NSR_upd)

                result.loc[idx, i] = np.sqrt(R2upd) - np.sqrt(R2.loc[idx])

        else:
            P_upd = result[f'P_upd_0'] + result[
                f'P_upd_1'] + 2 * P_lt_st_arr_rho * np.sqrt(
                    result[f'P_upd_0'] * result[f'P_upd_1'])
            NSR_upd = P_upd / SIG
            R2upd = 1 / (1 + NSR_upd)
            result[i] = np.sqrt(R2upd) - np.sqrt(R2)

    f = plt.figure(figsize=(23, 7))

    for i, title in enumerate(titles):
        plt.subplot(2, 4, i + 1)
        im = plot_ease_img(result,
                           i,
                           fontsize=12,
                           cbrange=[-0.2, 0.2],
                           cmap=cc.cm.bjy,
                           log_scale=False,
                           title=title,
                           plot_cb=False)

        if i < 3:
            plt.subplot(2, 4, i + 5)
            im = plot_ease_img(result,
                               f'{i}_4K',
                               fontsize=12,
                               cbrange=[-0.2, 0.2],
                               cmap=cc.cm.bjy,
                               log_scale=False,
                               title=title + ' (4K)',
                               plot_cb=False)

    plot_centered_cbar(f, im, 3, fontsize=10)

    plt.tight_layout()
    plt.show()
Esempio n. 14
0
def plot_ascat_eval_relative(res_path, dir_out):

    refs = ['Pcorr_OL', 'Pcorr_4K']
    runs = [['Pcorr_anom_lst', 'Pcorr_anom_lt_ScDY', 'Pcorr_anom_st_ScYH'],
            'Pcorr_LTST']

    titles = [
        'R$_{TC}$ - R$_{OL}$ (Individual)', 'R$_{TC}$ - R$_{OL}$ (Joint)',
        'R$_{TC}$ - R$_{4K}$ (Individual)', 'R$_{TC}$ - R$_{4K}$ (Joint)'
    ]

    res = pd.read_csv(res_path / 'ascat_eval.csv', index_col=0)

    res_tc = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)

    modes = ['anom_lst', 'anom_lt', 'anom_st']
    labels = [f'Anomaly skill', f'LF skill', f'HF skill']

    cb_r = [-0.2, 0.2]

    fontsize = 14

    f = plt.figure(figsize=(22, 8))

    for i, (m, label) in enumerate(zip(modes, labels)):

        for j, ref in enumerate(refs):

            ref_col = f'ana_r_corr_{ref}_{m}'

            res[ref_col][res[ref_col] < 0] = 0
            res[ref_col][res[ref_col] > 1] = 1

            for k, run in enumerate(runs):

                r = run if k == 1 else run[i]

                ax = plt.subplot(3, 4, i * 4 + j * 2 + k + 1)

                col = f'ana_r_corr_{r}_{m}'

                res[col][res[col] < 0] = 0
                res[col][res[col] > 1] = 1

                res['diff'] = res[col] - res[ref_col]

                if i == 0:
                    title = titles[j * 2 + k]
                else:
                    title = ''

                if (j == 0) & (k == 0):
                    ylabel = label
                else:
                    ylabel = ''

                r_asc_smap = res_tc[f'r_grid_{m}_p_ASCAT_SMAP']
                r_asc_clsm = res_tc[f'r_grid_{m}_p_ASCAT_CLSM']
                r_smap_clsm = res_tc[f'r_grid_{m}_p_SMAP_CLSM']
                thres = 0.2
                ind_valid = res_tc[(r_asc_smap > thres) & (r_asc_smap > thres)
                                   & (r_asc_smap > thres)].index

                im = plot_ease_img(res.reindex(ind_valid),
                                   'diff',
                                   title=title,
                                   cmap=cc.cm.bjy,
                                   cbrange=cb_r,
                                   fontsize=fontsize,
                                   print_mean=True,
                                   plot_cb=False)
                ax.set_ylabel(ylabel)

        plot_centered_cbar(f, im, 4, fontsize=fontsize - 2, pad=0.02)

    f.savefig(dir_out / f'ascat_eval_rel.png', dpi=300, bbox_inches='tight')
    plt.close()
Esempio n. 15
0
def plot_ascat_eval_absolute(res_path, dir_out):

    runs = [
        'Pcorr_OL', 'Pcorr_4K',
        ['Pcorr_anom_lst', 'Pcorr_anom_lt_ScDY', 'Pcorr_anom_st_ScYH'],
        'Pcorr_LTST'
    ]
    titles = [
        'Open-loop', '4K Benchmark', 'Individual assimilation',
        'Joint assimilation'
    ]

    res = pd.read_csv(res_path / 'ascat_eval.csv', index_col=0)
    res_tc = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)

    modes = ['anom_lst', 'anom_lt', 'anom_st']

    cb_r = [0.6, 1]

    fontsize = 14

    f = plt.figure(figsize=(22, 8))

    for i, m in enumerate(modes):

        for cnt, (run, tit) in enumerate(zip(runs, titles)):

            r = run if cnt != 2 else run[i]

            ax = plt.subplot(3, 4, i * 4 + cnt + 1)

            col = f'ana_r_corr_{r}_{m}'

            res[col][res[col] < 0] = 0
            res[col][res[col] > 1] = 1

            if i == 0:
                title = tit
            else:
                title = ''

            if cnt == 0:
                if i == 0:
                    ylabel = 'Anomaly skill'
                elif i == 1:
                    ylabel = 'LF skill'
                else:
                    ylabel = 'HF skill'
            else:
                ylabel = ''

            r_asc_smap = res_tc[f'r_grid_{m}_p_ASCAT_SMAP']
            r_asc_clsm = res_tc[f'r_grid_{m}_p_ASCAT_CLSM']
            r_smap_clsm = res_tc[f'r_grid_{m}_p_SMAP_CLSM']
            thres = 0.2
            ind_valid = res_tc[(r_asc_smap > thres) & (r_asc_smap > thres) &
                               (r_asc_smap > thres)].index

            im = plot_ease_img(res.reindex(ind_valid),
                               col,
                               title=title,
                               cmap='viridis',
                               cbrange=cb_r,
                               fontsize=fontsize,
                               print_mean=True,
                               plot_cb=False)
            ax.set_ylabel(ylabel)

        plot_centered_cbar(f, im, 4, fontsize=fontsize - 2, pad=0.02)

    f.savefig(dir_out / f'ascat_eval_abs.png', dpi=300, bbox_inches='tight')
    plt.close()
Esempio n. 16
0
def plot_filter_diagnostics(res_path, dir_out):

    fname = res_path / 'filter_diagnostics.nc'

    res = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)
    res_tc = pd.read_csv(
        '/Users/u0116961/Documents/work/MadKF/CLSM/SM_err_ratio/GEOSldas/sm_validation/Pcorr/result.csv',
        index_col=0)
    tg = GEOSldas_io().grid.tilegrids
    res_cols = res.col.values - tg.loc['domain', 'i_offg']
    res_rows = res.row.values - tg.loc['domain', 'j_offg']
    r_asc_smap = res_tc[f'r_grid_anom_lst_p_ASCAT_SMAP']
    r_asc_clsm = res_tc[f'r_grid_anom_lst_p_ASCAT_CLSM']
    r_smap_clsm = res_tc[f'r_grid_anom_lst_p_SMAP_CLSM']
    thres = 0.2
    ind_valid = res_tc[(r_asc_smap > thres) & (r_asc_smap > thres) &
                       (r_asc_smap > thres)].index

    fontsize = 14

    root = Path('/Users/u0116961/data_sets/GEOSldas_runs')
    runs = [run.name for run in root.glob('*_DA_SMAP_*')]
    runs += ['NLv4_M36_US_OL_Pcorr', 'NLv4_M36_US_OL_noPcorr']

    tags = ['OL_Pcorr', 'Pcorr_4K', f'Pcorr_anom_lst', 'Pcorr_LTST']
    iters = [np.where([tag in run for run in runs])[0][0] for tag in tags]

    titles = ['Open-loop', '4K constant', 'Anomalies']
    labels = ['H pol. / Asc.', 'V pol. / Asc.']

    with Dataset(fname) as ds:

        lons = ds.variables['lon'][:]
        lats = ds.variables['lat'][:]
        lons, lats = np.meshgrid(lons, lats)

        var = 'innov_autocorr'
        cbrange = [0, 0.7]
        step = 0.2
        cmap = 'viridis'

        f = plt.figure(figsize=(19, 6))

        for j, (spc, label) in enumerate(zip([0, 2], labels)):
            for i, (it_tit, it) in enumerate(zip(titles, iters)):

                title = it_tit if j == 0 else ''

                plt.subplot(2, 3, j * 3 + i + 1)
                data = ds.variables[var][:, :, it, spc]

                res['tmp'] = data[res_rows, res_cols]
                im = plot_ease_img(res.reindex(ind_valid),
                                   'tmp',
                                   fontsize=fontsize,
                                   cbrange=cbrange,
                                   cmap=cmap,
                                   title=title,
                                   plot_cb=False,
                                   print_meanstd=True)
                if i == 0:
                    plt.ylabel(label, fontsize=fontsize)

        plot_centered_cbar(f, im, 3, fontsize=fontsize - 2, bottom=0.07)
        fout = dir_out / f'{var}.png'
        f.savefig(fout, dpi=300, bbox_inches='tight')
        plt.close()