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
def calculate_forcing_differences(return_period = 10,
                                  prefix = 'gev_params_stationary',
                                  postfix = ''
                                  ):

    level_fields = []


    file = prefix + '_' + members.control_id + postfix
    pars_set = pickle.load(open(file))

    control_field = np.zeros((len(pars_set)))
    for pos, pars in enumerate(pars_set):
        if 'high' in postfix:
            control_field[pos] = gevfit.get_high_ret_level_stationary(pars, return_period)
        else:
            control_field[pos] = gevfit.get_low_ret_level_stationary(pars, return_period)


    for id in members.current_ids:
        file = prefix + '_' + id + postfix
        pars_set = pickle.load(open(file))

        level_field = np.zeros((len(pars_set)))
        for pos, pars in enumerate(pars_set):
            if 'high' in postfix:
                level_field[pos] = gevfit.get_high_ret_level_stationary(pars, return_period)
            else:
                level_field[pos] = gevfit.get_low_ret_level_stationary(pars, return_period)

        level_fields.append(level_field)


    diff = np.zeros((len(pars_set)))
    for level_field in level_fields:
        diff += (level_field - control_field) # / control_field * 100

    print(np.min(diff), np.max(diff))

    print('min(control_field): ', np.min(control_field))
    return diff / float( len(level_fields) )
Exemple #3
0
def function_for_each_process(args):
    sample_index, sampled_indices, extremes, \
    return_periods, high_flow, positions = args

    print('I work on sample %d' % sample_index)
    
    result = {}
    for the_period in return_periods:
        result[the_period] = []



    for pos in positions:
        pars = gevfit.optimize_stationary_for_period(extremes[sampled_indices, pos],
                                                    high_flow = high_flow)

        #calculate return levels for the current position and sample
        for the_period in return_periods:
            if high_flow:
                ret_level = gevfit.get_high_ret_level_stationary(pars, the_period)
            else:
                ret_level = gevfit.get_low_ret_level_stationary(pars, the_period)
            result[the_period].append(ret_level)
    return result
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')