def main():

    high_ret_periods = [10]
    low_ret_periods = [2]

    #plot_utils.apply_plot_params(font_size=15, width_pt=900, aspect_ratio=2.5)
    plot_utils.apply_plot_params(width_pt=None, font_size=9, aspect_ratio=2.5)



    fig = plt.figure()
    assert isinstance(fig, Figure)
    gs = gridspec.GridSpec(3,2)

    ax1 = fig.add_subplot(gs[0,0])
    ax2 = fig.add_subplot(gs[0,1])

    for ret_period in high_ret_periods:
        calculate_and_plot(ret_period, gevfit.get_high_ret_level_stationary, ax = ax1)

    for ret_period in low_ret_periods:
        calculate_and_plot(ret_period, gevfit.get_low_ret_level_stationary, ax = ax2)



    fig.tight_layout()
    fig.savefig("rl_changes.png")


    pass
Exemple #2
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_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')
Exemple #4
0
def stationary():

    #data_folder = 'data/streamflows/hydrosheds_euler9'
    data_folder = "data/streamflows/narccap_ccsm-crcm"

    current_data_path_pattern = '%s_discharge_1970_01_01_00_00.nc'
    future_data_path_pattern = '%s_discharge_2041_01_01_00_00.nc'

    i_list, j_list = data_select.get_indices_from_file()

    current_ids = ["ccsm-crcm-current"]
    future_ids = ["ccsm-crcm-future"]
    current2future = dict(list(zip(current_ids, future_ids)))

    current_start_date = datetime(1970, 1, 1, 0, 0)
    current_end_date = datetime(1999, 11, 23,0, 0)

    future_start_date = datetime(2041, 1, 1, 0, 0)
    future_end_date = datetime(2070, 11, 23,0, 0)


    high_return_periods = [10]
    high_start_month = 3
    high_end_month = 7
    high_event_duration = timedelta(days = 1)


    low_return_periods = [2]
    low_start_month = 1
    low_end_month = 5
    low_event_duration = timedelta(days = 15)





    all_return_periods = []
    all_return_periods.extend(high_return_periods)
    all_return_periods.extend(low_return_periods)

    plot_return_levels = False

    extreme_types = ['low', 'high']

    #calculate parameters of the gev distriution for each member
    #calculate and plot return levels
    for extreme_type in extreme_types:
        start_month = low_start_month if extreme_type == 'low' else high_start_month
        end_month = low_end_month if extreme_type == 'low' else high_end_month
        event_duration = low_event_duration if extreme_type == 'low' else high_event_duration

        for current_id in current_ids:
            param_file = 'gev_params_stationary'
            param_file += '_' + current_id + '_' + extreme_type
            data_file = current_data_path_pattern % current_id
            data_file = os.path.join(data_folder, data_file)
            pars_set = optimize_stationary_for_period_and_all_cells(data_file,
                    paramfile = param_file ,
                    high_flow = (extreme_type == 'high'),
                    start_month = start_month,
                    end_month = end_month,
                    start_date = current_start_date,
                    end_date = current_end_date,
                    event_duration = event_duration)

            if plot_return_levels:
                for period in low_return_periods:
                    plot_low_flows(period = period,
                           imagefile = '%drlevel_%s_stationary_%s.png' % (period, extreme_type, current_id),
                           pars_set = pars_set)
                           

        print('Finished optimizing for current climate')

    
        for future_id in future_ids:
            param_file = 'gev_params_stationary'
            param_file += '_' + future_id + '_' + extreme_type
            data_file = future_data_path_pattern % future_id
            data_file = os.path.join(data_folder, data_file)
            pars_set = optimize_stationary_for_period_and_all_cells(data_file,
                    paramfile = param_file ,
                    high_flow = (extreme_type == 'high'),
                    start_month = start_month,
                    end_month = end_month,
                    start_date = future_start_date,
                    end_date = future_end_date,
                    event_duration = event_duration)


            if plot_return_levels:
                for period in low_return_periods:
                    plot_low_flows(period = period, imagefile = '%drlevel_%s_stationary_%s.png' % (period, extreme_type ,future_id),
                           pars_set = pars_set)



    print('Finished optimizing for future climate')
    print('Finished calculating return levels !!!')

    plot_mean_changes = True
    if not plot_mean_changes:
        return

###############################current climate return levels
    print('Calculating mean high flow return levels for current climate ...')
    current_rl_means = {}
    future_rl_means = {}
    the_type_to_periods = {'high': high_return_periods, 'low': low_return_periods}



    keys = []
    for the_type, periods in the_type_to_periods.items():
        for period in periods:
            k = TypePeriodKey()
            k.type = the_type
            k.return_period = period
            keys.append(k)


    for key in keys:
        current_rl_means[key] = []
        future_rl_means[key] = []






    #collect return levels for corresponding type(high , low) and return period for each member and then take mean
    for current_id in current_ids:
        for key in keys:
            future_id = current2future[current_id]
            the_field_current = get_levels_for_type_and_id(current_id, return_period = key.return_period, type = key.type)
            the_field_future = get_levels_for_type_and_id(future_id, return_period = key.return_period, type = key.type)

            assert isinstance(the_field_current, np.ndarray)
            assert isinstance(the_field_future, np.ndarray)

            indices = np.where((the_field_current > 0) & (the_field_future >= 0) )
            to_plot = np.ma.masked_all(the_field_current.shape)
            to_plot[indices] = (the_field_future[indices] - the_field_current[indices]) / the_field_current[indices] * 100.0


            file_name = '{0}-{1}_{2}_{3}yr_change.png'.format(current_id, future_id, key.type, key.return_period)

            delta = np.max(np.abs(to_plot[indices]))
            delta = min(100.0, delta)
            plot_data(data = to_plot, imagefile = file_name,
                  units = '%', minmax = (-125, 125), color_map = my_cm.get_red_blue_colormap(ncolors = 16),
                  title = '{0}-{1}, change, {2}, return period: {3}'.format(current_id,
                  future_id, key.type, key.return_period)
            )


            future_rl_means[key].append(the_field_future)
            current_rl_means[key].append(the_field_current)



            
    for key in keys:

        current_rl_means[key] = np.array(current_rl_means[key])
        current_rl_means[key] = np.ma.masked_where(current_rl_means[key] < 0, current_rl_means[key])

        future_rl_means[key] = np.array(future_rl_means[key])
        future_rl_means[key] = np.ma.masked_where(future_rl_means[key] < 0, future_rl_means[key])


        current_rl_means[key] = np.ma.mean(  current_rl_means[key]  , axis = 0)
        future_rl_means[key] = np.ma.mean(  future_rl_means[key]  , axis = 0)


#        plt.figure()
#        plt.subplot(2,1,1)
#        plt.title('current mean')
#        plot_data(current_rl_means[key], imagefile = None)
#
#        plt.subplot(2,1,2)
#        plt.title('future mean')
#        plot_data(future_rl_means[key], imagefile = None)
#
#        plt.savefig('means_%s_%dyr_rl.png' % (key.type, key.return_period))


###################################################
####Calculate differences between future and current return levels for 10 and 30 year
#### return period.
####

    plot_utils.apply_plot_params(width_pt=None, font_size=9, aspect_ratio=2.5)
    fig = plt.figure()
    assert isinstance(fig, Figure)
    gs = GridSpec(3,2, height_ratios=[1,1,1])
    for i, key in enumerate(keys):
        current = current_rl_means[key]
        future = future_rl_means[key]
        indices = np.where((current > 0) & (future >= 0))
        #to_plot = np.ma.masked_all(current.shape)
        to_plot = (future[indices] - current[indices]) / current[indices] * 100.0

        delta = np.max(np.abs(to_plot))

        min_change = np.min(to_plot)

        if not key.type == "high":
            delta = 100
            lower_limit = 0 if min_change >= 0 else np.floor(min_change / 10.0) * 10
        else:
            delta = 50
            lower_limit = np.floor(min_change / 10.0 ) * 10

        fig.add_subplot(gs[i // 2, i % 2])
        csfb.plot(to_plot,  i_list, j_list, xs, ys,
              imagefile = None, #'%s_%dyr_mean_change.png' % (key.type, key.return_period),
              units = '%', minmax = (-delta, delta),
              color_map = my_cm.get_red_blue_colormap(ncolors = 20), #mpl.cm.get_cmap('RdYlBu',20),
              title = '{0}-year {1} flow'.format( key.return_period, key.type),
              colorbar_tick_locator = LinearLocator(numticks = 11), upper_limited=True,
              impose_lower_limit= lower_limit, basemap=polar_stereographic.basemap
              )

    #gs.update()
    plt.tight_layout(h_pad = 2)
    fig.savefig("ccsm-crcm-rl-changes.png")
                    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

if __name__ == "__main__":
    application_properties.set_current_directory()
    plot_utils.apply_plot_params(width_pt=None, font_size=9)
    calculate = False
    if calculate:
        main(n_samples = 1000)
    else:
        plot_results()
    print("Hello World")