コード例 #1
0
ファイル: plot_means.py プロジェクト: guziy/PlotWatrouteData
def plot_mean_extreme_flow(folder_path = "data/streamflows/hydrosheds_euler9",
                                 member_ids = None,
                                 file_name_pattern = "%s_discharge_1970_01_01_00_00.nc",
                                 out_file_name = "annual_means.png",
                                 high = True,
                                 start_month = 1, end_month = 12
                                 ):
    """
    Plot mean extreme (1-day high or 15-day low) flow over time
    """
    if member_ids is None:
        return


    i_indices = None
    j_indices = None
    times = None
    x_min, x_max = None, None
    y_min, y_max = None, None
    the_extreme_list = []
    for i, the_id in enumerate( member_ids ):
        fName = file_name_pattern % the_id
        fPath = os.path.join(folder_path, fName)
        if not i:
            data, times, i_indices, j_indices = data_select.get_data_from_file(fPath)
            interest_x = x[i_indices, j_indices]
            interest_y = y[i_indices, j_indices]
            x_min, x_max, y_min, y_max = _get_limits(interest_x = interest_x, interest_y = interest_y)
        else:
            data = data_select.get_field_from_file(path = fPath)


        assert data is not None, "i = %d " % i


        if high:
            extremes = data_select.get_list_of_annual_maximums_for_domain(data, times,
                                                                          start_month = start_month,
                                                                          end_month = end_month)
        else:
            extremes = data_select.get_list_of_annual_minimums_for_domain(data, times,
                                                                          event_duration = timedelta(days = 15),
                                                                          start_month = start_month,
                                                                          end_month = end_month
                                                                          )

        the_extreme_list.append(np.mean(extremes, axis = 0))


    print "shape of extremes list ", np.array(the_extreme_list).shape

    plot_utils.apply_plot_params(aspect_ratio=0.8)
    plt.figure()
    plt.subplots_adjust(hspace = 0.1, wspace = 0.3)

    for k, the_extreme_mean in enumerate(the_extreme_list):
        plt.subplot( 2, len(member_ids) // 2 + 1 , k + 1)
        to_plot = np.ma.masked_all(x.shape)
        for the_index, i, j in zip( xrange(len(i_indices)), i_indices, j_indices):
            to_plot[i, j] = the_extreme_mean[the_index]

        basemap.pcolormesh(x, y, to_plot.copy(), cmap = mpl.cm.get_cmap(name = "jet_r", lut = 18), vmin = 0,
                           vmax = 1.5)
        basemap.drawcoastlines(linewidth = 0.5)
        plt.colorbar(ticks = LinearLocator(numticks = 7), format = "%.2e")
        plt.xlim(x_min, x_max)
        plt.ylim(y_min, y_max)

    plt.savefig(out_file_name)

    #plot cv for the extremes     (here for performance, no need to again fetch the extremes)
    max_value = 0.1
    plot_utils.apply_plot_params(width_pt=600)
    plt.figure()
    extreme_means = np.array( the_extreme_list )
    mu = np.mean(extreme_means, axis=0)
    sigma = np.std(extreme_means,axis=0)
    cv = sigma / mu

    to_plot = np.ma.masked_all(x.shape)
    for the_index, i, j in zip( xrange(len(i_indices)), i_indices, j_indices):
        to_plot[i, j] = cv[the_index]

    basemap.pcolormesh(x, y, to_plot.copy(), cmap = mpl.cm.get_cmap(name = "jet_r", lut = 30), vmin = 0,
                       vmax = max_value)
    basemap.drawcoastlines(linewidth = 0.5)
    plt.colorbar(ticks = LinearLocator(numticks = 11), format = "%.1e")
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.savefig("cv_" + out_file_name)
    pass
コード例 #2
0
def compare_means(member = 'aet', my_data_path = '',
                  start_date = datetime(1961, 1, 1, 0, 0),
                  end_date = datetime(1990, 12, 31, 0, 0)):
    streamflows, times, i_array, j_array = data_select.get_data_from_file(my_data_path)

    
    event_duration = timedelta(days = 1)

    my_data = data_select.get_list_of_annual_maximums_for_domain(streamflows, times,
                                    start_date = start_date, end_date = end_date,
                                    start_month = 1, end_month = 12,
                                    event_duration = event_duration)


    data_path = 'data/streamflows/Vincent_annual_max/mapHIGH_{0}.txt'.format(member)
    v = VincentMaximumsReader(data_path = data_path)

    the_format = '{0}: i = {1}, j = {2}, min = {3}, max = {4}, mean = {5}'
    vmeans = []
    vmins = []
    vmaxs = []
   # my_data = 500 * np.ones((10,547))
    for i, j, the_index in zip(i_array, j_array, range(my_data.shape[1])):
        data = my_data[:, the_index]
        print the_format.format('Sasha', i, j, np.min(data), np.max(data), np.mean(data))
        data = v.get_data_at(i + 1, j + 1)
        print the_format.format('Vincent', i, j, np.min(data), np.max(data), np.mean(data))
        vmeans.append(np.mean(data))
        vmins.append(np.min(data))
        vmaxs.append(np.max(data))
        print '=' * 30


    #scatter plot for means
    plt.subplots_adjust(hspace = 0.5)

    plt.subplot(2,2,1)
    plt.title('annual maximums, \n average for each grid point', override)
    plt.scatter( vmeans , np.mean(my_data, axis = 0), linewidth = 0)
    plt.xlabel('Vincent')
    plt.ylabel('Sasha')

    x = plt.xlim()
    plt.plot(x,x, color = 'k')
    plt.grid(True)
    
    #scatter plot for minimums
    plt.subplot(2,2,2)
    plt.title('annual maximums, \n minimum for each grid point', override)

    plt.scatter( vmins , np.min(my_data, axis = 0), linewidth = 0)
    plt.xlabel('Vincent')
    plt.ylabel('Sasha')

    x = plt.xlim()
    plt.plot(x,x, color = 'k')
    plt.grid(True)
    
    #scatter plot for minimums
    plt.subplot(2,2,3)
    plt.title('annual maximums, \n maximum for each grid point', override)
    plt.scatter( vmaxs , np.max(my_data, axis = 0), linewidth = 0)
    plt.xlabel('Vincent')
    plt.ylabel('Sasha')

    x = plt.xlim()
    plt.plot(x,x, color = 'k')
    plt.grid(True)
    plt.savefig('{0}_scatter_max.png'.format(member), bbox_inches = 'tight')