Beispiel #1
0
def plot_cv_for_return_levels_current_and_future_and_change():
    low_periods = [2, 5]
    high_periods = [10, 30]


    extreme_types = ['high', 'low']
    extreme_type_to_periods = dict(list(zip(extreme_types, [high_periods, low_periods])))


    i_indices, j_indices = data_select.get_indices_from_file()
    current_cvs = {}
    future_cvs = {}
    for extreme_type in extreme_types:
        current_cvs[extreme_type] = {} #to save return level fields for a given extreme type
        future_cvs[extreme_type] = {}

        for return_period in extreme_type_to_periods[extreme_type]:
            c_rl_fields = np.zeros((len(members.current_ids), len(i_indices)))
            f_rl_fields = np.zeros((len(members.current_ids), len(i_indices)))
            for member_num, the_member in enumerate(members.current_ids):
                pars_current = csfb.get_pars_for_member_and_type(the_member, level_type = extreme_type)
                pars_future = csfb.get_pars_for_member_and_type(members.current2future[the_member], level_type = extreme_type)

                #calculate changes for each pair of members and each position
                npos = len(pars_current)
                for pos, pc, pf in zip(range(npos), pars_current, pars_future):
                    if extreme_type == 'high':
                        c = gevfit.get_high_ret_level_stationary(pc, return_period)
                        f = gevfit.get_high_ret_level_stationary(pf, return_period)
                    else:
                        c = gevfit.get_low_ret_level_stationary(pc, return_period)
                        f = gevfit.get_low_ret_level_stationary(pf, return_period)

                    c_rl_fields[member_num, pos] = c
                    f_rl_fields[member_num, pos] = f

                    pass


            current_cvs[extreme_type][return_period] = np.std( c_rl_fields, axis = 0 ) / np.mean( c_rl_fields, axis = 0 )
            future_cvs[extreme_type][return_period] = np.std( f_rl_fields, axis = 0 ) / np.mean( f_rl_fields, axis = 0 )


    #do plotting
    _plot_map_as_subplots(i_indices, j_indices, current_cvs, title = " Current Climate, CV ")
    plt.savefig('cv_for_return_levels_current.png')
    _plot_map_as_subplots(i_indices, j_indices, future_cvs, title = "Future Climate, CV")
    plt.savefig('cv_for_return_levels_future.png')
    pass
Beispiel #2
0
def plot_signal_to_noise_ratio():
    low_periods = [2, 5]
    high_periods = [10, 30]
    plot_utils.apply_plot_params(width_pt=None, font_size=9, aspect_ratio=2.5)

    extreme_types = ['high', 'low']
    extreme_type_to_periods = dict(list(zip(extreme_types, [high_periods, low_periods])))

    i_indices, j_indices = data_select.get_indices_from_file()
    i_subplot = 0

    #plt.subplots_adjust(wspace = 0.1)
    cMap = mpl.cm.get_cmap('jet', 3)
    cMap.set_over(color = '#FF8C00')

    gs = gridspec.GridSpec(3,2)

    for extreme_type in extreme_types:
        for return_period in extreme_type_to_periods[extreme_type]:

            changes = np.zeros((len(members.current_ids), len(i_indices)))
            for member_num, the_member in enumerate(members.current_ids):

                pars_current = csfb.get_pars_for_member_and_type(the_member, level_type = extreme_type)
                pars_future = csfb.get_pars_for_member_and_type(members.current2future[the_member], level_type = extreme_type)

                
                #calculate changes for each pair of members and each position
                npos = len(pars_current)
                for pos, pc, pf in zip(range(npos), pars_current, pars_future):
                    if extreme_type == 'high':
                        c = gevfit.get_high_ret_level_stationary(pc, return_period)
                        f = gevfit.get_high_ret_level_stationary(pf, return_period)
                    else:
                        c = gevfit.get_low_ret_level_stationary(pc, return_period)
                        f = gevfit.get_low_ret_level_stationary(pf, return_period)

                    changes[member_num, pos] = f - c
                    pass

            #calculate mean and stdev of the obtained changes
            the_mean = np.mean(changes, axis = 0)
            the_std = np.std(changes, axis = 0)

            #change if you want signal to noise ratio, currently it is cv (coefficient of variation 1/(signal-to-noise-ratio))
            the_values = the_std / np.abs(the_mean)
            print(the_values.min(), the_values.max())
            #the_values = the_mean
            to_plot = np.ma.masked_all(csfb.xs.shape)

            max_value = 1.5
            for i, j, value, dev in zip(i_indices, j_indices, the_values, the_std):
                to_plot[i, j] = value


            #shaded = np.ma.masked_where(to_plot != 0, shaded)
            #to_plot = np.ma.masked_where(to_plot == 0, to_plot)


            plot_axes = plt.subplot(gs[i_subplot // 2, i_subplot % 2])
            i_subplot += 1
            print('just before plotting')

            
            basin_boundaries.plot_basin_boundaries_from_shape(csfb.basemap, plotter = plt, linewidth = 1.)
            image = csfb.basemap.pcolormesh(csfb.xs, csfb.ys, to_plot, vmin = 0, vmax = max_value, cmap = cMap,
                                            ax = plot_axes)
            csfb.basemap.drawcoastlines(linewidth = 0.2)

            plot_axes.set_title('T = {0}-year'.format(return_period))
            x_min, x_max, y_min, y_max = plot_utils.get_ranges(csfb.xs[i_indices, j_indices], csfb.ys[i_indices, j_indices])
            plot_axes.set_xlim(x_min, x_max)
            plot_axes.set_ylim(y_min, y_max)




            divider = make_axes_locatable(plot_axes)
            cax = divider.append_axes("right", "8%", pad="3%")


            # extend choices are "both", "min", "max" and "neither"
            cb = plt.colorbar(image, extend = 'max',
                              format = "%.1f", drawedges = True, cax = cax)

            cb.set_ticks(LinearLocator(numticks = 4))
            cb.outline.set_visible(False)



    #plt.show()
    plt.tight_layout()
    plt.savefig('cv_for_changes.png')