def plot_temperature_biases():

    seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"]
    months =  [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]  ]
    season_to_months = dict(zip(seasons, months))

    cru_var_name = "tmp"
    cru_data_store, crcm4_data_store = _get_comparison_data(cru_var_name= cru_var_name,
        season_to_months=season_to_months)

    x, y = polar_stereographic.xs, polar_stereographic.ys
    i_array, j_array = _get_routing_indices()
    x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array])



    plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5)
    fig = plt.figure()
    assert isinstance(fig, Figure)

    basemap = polar_stereographic.basemap
    assert isinstance(basemap, Basemap)
    gs = gridspec.GridSpec(3,2)

    color_map = my_cm.get_red_blue_colormap(ncolors = 14, reversed=True)
    clevels = xrange(-8, 9, 2)
    all_plot_axes = []
    for i, season in enumerate(seasons):
        if not i:
            ax = fig.add_subplot(gs[0,:])
        else:
            row, col = (i - 1)  // 2 + 1, (i - 1) % 2
            ax = fig.add_subplot(gs[row, col])
        all_plot_axes.append(ax)
        assert isinstance(ax, Axes)
        delta = crcm4_data_store[season] - cru_data_store[season]
        if cru_var_name == "tmp": delta -= 273.15
        #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta)
        save = delta[i_array, j_array]
        delta[:, :] = np.ma.masked
        delta[i_array, j_array] = save
        img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = -7, vmax = 7)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", "8%", pad="3%")
        int_ticker = LinearLocator(numticks = color_map.N + 1)
        fig.colorbar(img, cax = cax, ticks = MultipleLocator(base = 2))
        ax.set_title(season)

    for the_ax in all_plot_axes:
        the_ax.set_xlim(x_min, x_max)
        the_ax.set_ylim(y_min, y_max)
        basemap.drawcoastlines(ax = the_ax, linewidth = 0.1)
        plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax)
        plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4)
        put_selected_stations(the_ax, basemap, i_array, j_array)

    #gs.tight_layout(fig)
    fig.suptitle("T(2m), degrees, CRCM4 - CRU")
    fig.savefig("seasonal_{0}_ccc.png".format(cru_var_name))
def plot_diff(folder = "data/streamflows/hydrosheds_euler9",
              plot_f_and_c_means_separately = False):
    """
    Plot difference between the means for future and current climate
    """


    file_name = None
    for f_name in os.listdir(folder):
        if f_name.startswith( "aex" ):
            file_name = f_name

    #get indices of interest
    x_indices, y_indices = data_select.get_indices_from_file(path=os.path.join(folder, file_name))

    ##get significance and percentage changes
    #signific_vector, change = bootstrap_for_mean.get_significance_for_change_in_mean_over_months()
    #signific_vector, change = ttest_for_mean_of_merged.get_significance_and_changes_for_months()
    signific_vector, change = bootstrap_for_mean_merged.get_significance_for_change_in_mean_of_merged_over_months()

    signific_vector = signific_vector.astype(int)

    #start plotting (f-c)/c * 100

    plt.subplots_adjust(hspace = 0.2)

    #significance level 5%
    plot_axes = plt.subplot(1,1,1)
    plot_data(  change,
                x_indices, y_indices, name = None,
                color_map = my_cm.get_red_blue_colormap(ncolors = 8),
                #mpl.cm.get_cmap('RdBu', 16),
                minmax = (-40, 40),
                title = '', axes=plot_axes
                )

    color_map = mpl.cm.get_cmap(name="gray", lut=3)

    signific_vector = np.ma.masked_where(signific_vector == 1, signific_vector)
    plot_data(signific_vector, x_indices, y_indices, name = None, title="",
            minmax = (-1, 1), color_map=color_map, draw_colorbar=False, axes=plot_axes)

    plt.tight_layout()

    plt.savefig('future-current(sign).png')
def plot_swe_biases():

    seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"]
    months =  [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]  ]
    season_to_months = dict(zip(seasons, months))

    swe_obs_name = "swe"
    cru_data_store, crcm4_data_store = _get_comparison_data_swe(swe_var_name= swe_obs_name,
        season_to_months=season_to_months, start_date=datetime(1980,1,1), end_date=datetime(1996, 12, 31))

    x, y = polar_stereographic.xs, polar_stereographic.ys
    i_array, j_array = _get_routing_indices()
    x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array])



    plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5)
    fig = plt.figure()
    assert isinstance(fig, Figure)

    basemap = polar_stereographic.basemap
    assert isinstance(basemap, Basemap)
    gs = gridspec.GridSpec(3,2)

    color_map = my_cm.get_red_blue_colormap(ncolors = 14, reversed=True)
    clevels = xrange(-8, 9, 2)
    all_plot_axes = []
    for i, season in enumerate(seasons):
        if not i:
            ax = fig.add_subplot(gs[0,:])
        else:
            row, col = (i - 1)  // 2 + 1, (i -1) % 2
            ax = fig.add_subplot(gs[row, col])
        all_plot_axes.append(ax)
        assert isinstance(ax, Axes)
        delta = crcm4_data_store[season] - cru_data_store[season]

        #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta)
        save = delta[i_array, j_array]
        delta = np.ma.masked_all(delta.shape)
        delta[i_array, j_array] = save


        vmax = np.ceil( np.max(save) / 10.0) * 10
        vmin = np.floor( np.min(save) / 10.0) * 10

        bounds = plot_utils.get_boundaries_for_colobar(vmin, vmax, color_map.N, lambda x: np.round(x, decimals = 1))
        bn = BoundaryNorm(bounds, color_map.N)
        img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = vmin, vmax = vmax, norm = bn)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", "8%", pad="3%")
        int_ticker = LinearLocator(numticks = color_map.N + 1)





        fig.colorbar(img, cax = cax, ticks = bounds, boundaries = bounds)
        ax.set_title(season)

    for the_ax in all_plot_axes:
        the_ax.set_xlim(x_min, x_max)
        the_ax.set_ylim(y_min, y_max)
        basemap.drawcoastlines(ax = the_ax, linewidth = 0.1)
        plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax)
        plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4)
#        put_selected_stations(the_ax, basemap, i_array, j_array)


    #gs.tight_layout(fig)
    fig.suptitle("SWE (mm), CRCM4 - Ross Brown dataset (1981-1997)")
    fig.savefig("seasonal_{0}_ccc.png".format(swe_obs_name))
def plot_precip_biases():
    seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"]
    months =  [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]  ]
    season_to_months = dict(zip(seasons, months))
    cru_var_name = "pre"
    cru_data_store, crcm4_data_store = _get_comparison_data(
        crcm4_data_folder="/home/huziy/skynet1_rech3/crcm4_data/aex_p1pcp",
        cru_data_path = "data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.pre.dat.nc",
        cru_var_name=cru_var_name, season_to_months=season_to_months
    )

    x, y = polar_stereographic.xs, polar_stereographic.ys
    i_array, j_array = _get_routing_indices()
    x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array])



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

    fig = plt.figure()
    assert isinstance(fig, Figure)

    basemap = polar_stereographic.basemap
    assert isinstance(basemap, Basemap)
    gs = gridspec.GridSpec(3,2, width_ratios=[1,1], height_ratios=[1, 1, 1])

    color_map = my_cm.get_red_blue_colormap(ncolors = 16, reversed=False)
    color_map.set_over("k")
    color_map.set_under("k")
    all_plot_axes = []
    img = None
    for i, season in enumerate(seasons):
        if not i:
            ax = fig.add_subplot(gs[0,:])
        else:
            row, col = (i - 1)  // 2 + 1, (i - 1) % 2
            ax = fig.add_subplot(gs[row, col])
        all_plot_axes.append(ax)
        assert isinstance(ax, Axes)

        delta = crcm4_data_store[season] - cru_data_store[season]
        save = delta[i_array, j_array]
        delta[:, :] = np.ma.masked
        delta[i_array, j_array] = save
        img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = -2, vmax = 2)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", "8%", pad="3%")
        int_ticker = LinearLocator(numticks = color_map.N + 1)
        fig.colorbar(img, cax = cax, ticks = MultipleLocator(base = 0.5))
        ax.set_title(season)

    for the_ax in all_plot_axes:
        assert isinstance(the_ax, Axes)
        the_ax.set_xlim(x_min, x_max)
        the_ax.set_ylim(y_min, y_max)
        the_ax.set_xmargin(0)
        the_ax.set_ymargin(0)
        basemap.drawcoastlines(ax = the_ax, linewidth = 0.1)
        plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax)
        plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4)
#        put_selected_stations(the_ax, basemap, i_array, j_array)

#    ax = fig.add_subplot(gs[3,:])
#    assert isinstance(ax, Axes)
#    fig.colorbar(img, cax = ax, orientation = "horizontal", ticks = MultipleLocator(base = 0.5))

    #gs.tight_layout(fig)
    fig.suptitle("Total precip, mm/day, CRCM4 - CRU")
    fig.savefig("seasonal_{0}_ccc.png".format(cru_var_name))
def plot_swe_and_temp_on_one_plot():
    seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"]
    months =  [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]  ]
    season_to_months = dict(zip(seasons, months))
    cru_var_name = "tmp"

    temp_obs_data_store, temp_crcm4_data_store = _get_comparison_data(cru_var_name= cru_var_name,
        season_to_months=season_to_months)

    x, y = polar_stereographic.xs, polar_stereographic.ys
    i_array, j_array = _get_routing_indices()
    x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array])

    #get swe data
    swe_obs_name = "swe"
    swe_obs_data_store, swe_crcm4_data_store = _get_comparison_data_swe(swe_var_name= swe_obs_name,
        season_to_months=season_to_months, start_date=datetime(1980,1,1), end_date=datetime(1997, 1, 1))



    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(2, 1)

    basemap = polar_stereographic.basemap
    swe_season = seasons[1]
    temp_season = seasons[2]

    var_names = [ "swe", cru_var_name]
    the_seasons = [ swe_season, temp_season ]

    labels = [ "(a) Winter (DJF)", "(b) Sping (MAM)"]
    units = [ "mm", "$^{\\circ}{\\rm C}$" ]

    data_stores = [
        [swe_obs_data_store, swe_crcm4_data_store],
        [temp_obs_data_store, temp_crcm4_data_store]
    ]



    all_plot_axes = []
    for i, season, var_name, store, label, unit in zip(xrange(len(seasons)), the_seasons, var_names, data_stores,
            labels, units):
        ax = fig.add_subplot(gs[i, 0])
        all_plot_axes.append(ax)
        assert isinstance(ax, Axes)

        crcm4 = store[1][season]
        obs = store[0][season]

        delta = crcm4 - obs
        if var_name == "tmp": delta -= 273.15

        if var_name == "swe":
            ax.annotate("(1979-1997)", (0.1, 0.1), xycoords = "axes fraction",
                font_properties = FontProperties(weight = "bold"))
        elif var_name == "tmp":
            ax.annotate("(1970-1999)", (0.1, 0.1), xycoords = "axes fraction",
                font_properties = FontProperties(weight = "bold"))


        color_map = my_cm.get_red_blue_colormap(ncolors = 16, reversed=(var_name == "tmp"))
        color_map.set_over("k")
        color_map.set_under("k")



        #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta)
        save = delta[i_array, j_array]
        delta = np.ma.masked_all(delta.shape)
        delta[i_array, j_array] = save



        vmin = np.floor( np.min(save)  )
        vmax = np.ceil( np.max(save)  )

        decimals = 0 if var_name == "swe" else 1
        round_func = lambda x: np.round(x, decimals= decimals)
        bounds = plot_utils.get_boundaries_for_colobar(vmin, vmax, color_map.N, round_func= round_func)
        bn = BoundaryNorm( bounds, color_map.N )

        img = basemap.pcolormesh(x, y, delta, cmap = color_map, norm = bn)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", "8%", pad="3%")
        fig.colorbar(img, cax = cax, boundaries = bounds, ticks = bounds)
        ax.set_title(label)
        cax.set_title(unit)




    for the_ax in all_plot_axes:
        the_ax.set_xlim(x_min, x_max)
        the_ax.set_ylim(y_min, y_max)
        basemap.drawcoastlines(ax = the_ax, linewidth = 0.1)
        plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax)
        plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4)
        put_selected_stations(the_ax, basemap, i_array, j_array)

    fig.tight_layout()
    fig.savefig("swe_temp_biases.png")
def calculate_seasonal_changes_in_mean_stfl_and_plot(folder_path = 'data/streamflows/hydrosheds_euler9',
                                                     months = None, label = "",
                                                     cb_axes = None,
                                                     plot_axes = None,
                                                     impose_lower_limit = None,
                                                     upper_limited = False,
                                                     minmax = None
                                                     ):

    """

    """
    if months is None:
        print "please specify months"
        return

    fileName = None
    for fName in os.listdir(folder_path):
        if fName.startswith( "aex" ):
            fileName = fName


    i_indices, j_indices = data_select.get_indices_from_file(path = os.path.join(folder_path, fileName))

    xs = polar_stereographic.xs
    ys = polar_stereographic.ys
    basemap = polar_stereographic.basemap

    #get mean changes along with its significance
    #ensemble mean
    significance, change = bootstrap_for_mean.get_significance_for_change_in_mean_over_months(months=months)
    #significance, change = ttest_for_mean_of_merged.get_significance_and_changes_for_months(months=months)
    #merged
    #significance, change = bootstrap_for_mean_merged.get_significance_for_change_in_mean_of_merged_over_months(months= months)




    #plot mean change

    print "plotting"
    plot_axes.set_title('{0}'.format(label))

    calculate_mean_map.plot_data(change, i_indices, j_indices, minmax = minmax, title=label, name = None,
                                 color_map = my_cm.get_red_blue_colormap(ncolors = 10),
                                 draw_colorbar=True, basemap=basemap, axes=plot_axes,
                                 impose_lower_limit = impose_lower_limit,
                                 upper_limited = upper_limited
                                 )



    plot_significance = True
    if plot_significance:
        to_plot = np.ma.masked_all(xs.shape)
        significance = significance.astype(int)
        significance = np.ma.masked_where(significance == 1, significance)
        for the_significance, i, j in zip(significance, i_indices, j_indices):
            to_plot[i, j] = the_significance

        basemap.pcolormesh( xs, ys, to_plot.copy(), cmap = mpl.cm.get_cmap(name = "gray", lut = 3),
                           vmin = -1, vmax = 1, ax = plot_axes)