def _get_routing_indices():
    """
    Used for the plot domain centering
    """
    from data import data_select
    i_indices, j_indices = data_select.get_indices_from_file(path = "data/streamflows/hydrosheds_euler9/aex_discharge_1970_01_01_00_00.nc")
    return i_indices, j_indices
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')
    m.drawcoastlines()
    draw_meridians_and_parallels(m, step_degrees = 30)

    int_ticker = LinearLocator()
    cb = plt.colorbar(ticks = int_ticker, orientation = colorbar_orientation)
    cb.ax.set_ylabel(units)

    override = {'fontsize': 20,
                  'verticalalignment': 'baseline',
                  'horizontalalignment': 'center'}


    plt.title(title if title != None else name, override)

    ymin, ymax = plt.ylim()
    plt.ylim(ymin + 0.12 * (ymax - ymin), ymax * 0.32)

    xmin, xmax = plt.xlim()
    plt.xlim(xmin + (xmax - xmin) * 0.65, 0.85*xmax)

    if name != None:
        plt.savefig(name + '.png', bbox_inches = 'tight')


if __name__ == "__main__":
    i_indices, j_indices = data_select.get_indices_from_file()
    data = data_select.get_field_from_file('data/test_data/divided.nc', 'water_discharge')
    data = np.max(data, axis = 0)
    plot_data(data, i_indices, j_indices, name = 'discharge_scaling', title = 'Discharge response \n to runoff doubling')
    print "Hello World"
def main():

    start_year = 1970
    end_year = 1999


    stations = cehq_station.read_station_data(folder="data/cehq_measure_data_all")
    stations = list( itertools.ifilter( lambda s: s.is_natural, stations) )
    for s in stations:
        s.delete_data_after_year(end_year)
        s.delete_data_before_year(start_year)
        pass


    stations = list( itertools.ifilter(lambda s: s.get_num_of_years_with_continuous_data() >= 10, stations) )
    s = stations[0]

    assert isinstance(s, Station)





    #stations = list( itertools.ifilter(lambda s: s.is_natural, stations) )

    x, y = polar_stereographic.lons, polar_stereographic.lats
    basemap = polar_stereographic.basemap
    x, y = basemap(x,y)

    sx = [s.longitude for s in stations]
    sy = [s.latitude for s in stations]

    sx, sy = basemap(sx, sy)

    #read model data
    model_file_path = "data/streamflows/hydrosheds_euler9/aex_discharge_1970_01_01_00_00.nc"
    acc_area = data_select.get_field_from_file(path=model_file_path,
        field_name="drainage")
    i_indices, j_indices = data_select.get_indices_from_file(path=model_file_path)
    lons_1d = data_select.get_field_from_file(path=model_file_path, field_name="longitude")
    lats_1d = data_select.get_field_from_file(path=model_file_path, field_name="latitude")

    x1d, y1d, z1d = lat_lon.lon_lat_to_cartesian(lons_1d, lats_1d)
    kdtree = KDTree(zip(x1d, y1d, z1d))

    print "Id: 4 DA (km2) <-> 4 dist (km) <-> 4 (i,j)"
    #basemap.scatter(sx, sy, c = "r", zorder = 5)
    for s, isx, isy in zip( stations, sx, sy ):
        assert isinstance(s, Station)
        plt.annotate(s.id, xy=(isx, isy),
                 bbox = dict(facecolor = 'white'), weight = "bold", font_properties = FontProperties(size=0.5))

        #get model drainaige areas for the four closest gridcells to the station
        x0, y0, z0 = lat_lon.lon_lat_to_cartesian(s.longitude, s.latitude)
        dists, indices = kdtree.query([x0, y0, z0], k = 4)
        dists /= 1000
        print("{0}: {1:.1f}; {2:.1f}; {3:.1f}; {4:.1f} <-> {5:.1f}; {6:.1f}; {7:.1f}; {8:.1f} <-> {9};{10};{11};{12}".format(
            "{0} (S_DA = {1:.1f})".format(s.id, s.drainage_km2),
            float(acc_area[indices[0]]),
            float(acc_area[indices[1]]),
            float(acc_area[indices[2]]),
            float(acc_area[indices[3]]),
            float( dists[0] ),
            float(dists[1]),
            float(dists[2]),
            float(dists[3]),
            "({0}, {1})".format(i_indices[indices[0]] + 1, j_indices[indices[0]] + 1),
            "({0}, {1})".format(i_indices[indices[1]] + 1, j_indices[indices[1]] + 1),
            "({0}, {1})".format(i_indices[indices[2]] + 1, j_indices[indices[2]] + 1),
            "({0}, {1})".format(i_indices[indices[3]] + 1, j_indices[indices[3]] + 1)
        ))

    basemap.drawcoastlines(linewidth=0.5)



    xmin, xmax = min(sx), max(sx)
    ymin, ymax = min(sy), max(sy)

    marginx = (xmax - xmin) * 0.1
    marginy = (ymax - ymin) * 0.1
    xmin -= marginx * 1.5
    xmax += marginx * 2
    ymin -= marginy
    ymax += marginy * 2

    plt.xlim(xmin, xmax)
    plt.ylim(ymin, ymax)
    plt.tight_layout()
    basin_boundaries.plot_basin_boundaries_from_shape(basemap=basemap, plotter=plt, linewidth=1)
    plt.savefig("10yr_cont_stations_natural_fs0.5.pdf")
    #plt.show()


    pass
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)