Esempio n. 1
0
def main():
    plot_utils.apply_plot_params(width_pt=None, font_size=9, aspect_ratio=2.5)
    gs = mpl.gridspec.GridSpec(3,2)
    key_data_list = get_key_data_list()
    i_list, j_list = data_select.get_indices_from_file()
    subplot_count = 0

    for the_type in ["high", "low"]:
        for time_window in ["current", "future"]:
            selected_data = None

            for data in key_data_list:
                if data.time_window == time_window and data.type == the_type:
                    selected_data = data
                    break

            row = subplot_count // 2
            col = subplot_count % 2
            plt.subplot(gs[row, col])
            csfb.plot(selected_data.p_values, i_list, j_list,
                      polar_stereographic.xs, polar_stereographic.ys,
                      units = "", basemap = polar_stereographic.basemap,
                      minmax = (0, 0.25), title = "", # "{0} climate, {1} flow".format(selected_data.time_window, selected_data.type),
                      colorbar_label_format="%.2f", color_map = mpl.cm.get_cmap("jet", 5), upper_limited=True
            )
            subplot_count += 1

    #TODO:add 2 subplots for mean values
    pc = kw_test_for_means()
    plt.subplot(gs[2,0])
    csfb.plot(pc, i_list, j_list,
              polar_stereographic.xs, polar_stereographic.ys,
              units = "", basemap = polar_stereographic.basemap,
              minmax = (0, 0.25), #title = "{0} climate, {1} flow".format("current", "mean"),
              colorbar_label_format="%.2f", color_map = mpl.cm.get_cmap("jet", 5), upper_limited=True
    )

    pf = kw_test_for_means(current_climate=False)
    plt.subplot(gs[2, 1])
    csfb.plot(pf, i_list, j_list,
              polar_stereographic.xs, polar_stereographic.ys,
              units = "", basemap = polar_stereographic.basemap,
              minmax = (0, 0.25), #title = "{0} climate, {1} flow".format("future", "mean"),
              colorbar_label_format="%.2f", color_map = mpl.cm.get_cmap("jet", 5), upper_limited=True
    )


    plt.tight_layout()
    plt.savefig("p_values_kruskalwallis.png")
def plot_results():

    cc = "current"
    fc = "future"
    climate_types = [cc, fc]


    folder_path = 'data/streamflows/hydrosheds_euler9/'
    file_path = os.path.join(folder_path, "aex_discharge_1970_01_01_00_00.nc")
    i_indices, j_indices = data_select.get_indices_from_file(file_path)
    xs = polar_stereographic.xs
    ys = polar_stereographic.ys
    basemap = polar_stereographic.basemap
    
    hi = "high"
    lo = "low"
    extreme_types = [hi, lo]

    base_std_name = "{0}_std_dev_{1}"
    base_par_name = {
        cc : "_".join(["{0}"] + members.current_ids),
        fc : "_".join(["{0}"] + members.future_ids)
    }

    extreme_to_return_periods = {
        hi : get_high_return_periods(),
        lo : get_low_return_periods()
    }


    sig_coefs = [1.96
    #    , 1.645
    ]
    sig_levels = ["95 %"
    #    , "90 %"
    ]
    gs = gridspec.GridSpec(3,2)

    for extreme in extreme_types:
        pars_path_current = base_par_name[cc].format(extreme)
        std_path_current = base_std_name.format(extreme, cc)

        pars_path_future = base_par_name[fc].format(extreme)
        std_path_future = base_std_name.format(extreme, fc)

        pars_current = pickle.load(open(pars_path_current))
        stds_current = pickle.load(open(std_path_current))

        pars_future = pickle.load(open(pars_path_future))
        stds_future = pickle.load(open(std_path_future))

        return_periods = extreme_to_return_periods[extreme]
        #plot_utils.apply_plot_params(font_size=15, width_pt=900, aspect_ratio=2.5)
        #plt.figure()


        delta = 50 if extreme == hi else 100
        for row, ret_period in enumerate( return_periods ):
            #calculate changes in return levels
            func = lambda x: gevfit.get_high_ret_level_stationary(x, ret_period)
            rl_c = list(map(func, pars_current))
            rl_f = list(map(func, pars_future))
            
            rl_c = np.array(rl_c)
            rl_f = np.array(rl_f)

            std_c = stds_current[ret_period]
            std_f = stds_future[ret_period]

            in_odf = (std_c > 0) & (std_f > 0) & (rl_c > 0) & (rl_f >= 0)
            change = np.ma.masked_all(rl_c.shape)
            change[in_odf] = (rl_f[in_odf] - rl_c[in_odf]) / rl_c[in_odf] * 100.0


            min_change = np.min((rl_f - rl_c) / rl_c * 100.0)
            if min_change >= 0:
               low_limit = 0
            elif min_change > -10:
               low_limit = -10
            else:
                low_limit = np.floor(min_change / 10.0) * 10

            print("min change = {0}, low limit = {1}".format(min_change, low_limit))


            for sig_coef, sig_name in zip(sig_coefs, sig_levels):
                significance = np.ma.masked_all(rl_c.shape)
                sig_cond = (sig_coef * (std_c + std_f) < np.abs(rl_f - rl_c)) & in_odf
                significance[~sig_cond] = 0#fill with gray non signifcant areas
                change1 = np.ma.masked_where(~sig_cond, change)
                if extreme == hi:
                    plt.subplot(gs[row, 0])
                else:
                    plt.subplot(gs[row, 1])

                csfb.plot(change1 , i_indices, j_indices, xs, ys,
                        title = 'T = {0}-year'.format( ret_period ),
                        color_map = mycolors.get_red_blue_colormap(ncolors = 10),
                        units = '%',
                        basemap = basemap, minmax = (-delta, delta),
                        colorbar_label_format = '%d',
                        upper_limited = True,
                        colorbar_tick_locator = LinearLocator(numticks = 11),
                        not_significant_mask = significance,
                        show_colorbar = True, impose_lower_limit=low_limit
                        )
                #subplot_count += 1
    plt.tight_layout()
    plt.savefig("rl_of_merged_change.png")

    pass