Beispiel #1
0
def test_select():
    import application_properties
    application_properties.set_current_directory()
    data_file = 'data/streamflows/hydrosheds_euler9/aet_discharge_1970_01_01_00_00.nc'
    #get streamflow data
    streamflow, times, xs, ys = get_data_from_file(data_file)

    
    print(streamflow.shape)

    #test maxima selection
    maxs = get_period_maxima(streamflow[:, 10], times, start_date = datetime(1970,1,1,0,0,0),
                                                       end_date = datetime(1999,12, 31,0,0,0),
                                                       start_month = 4,
                     end_month = 6, event_duration = timedelta(days = 1))

    print(maxs)

    #test minima selection
    maxs = get_period_minima(streamflow[:, 10], times, start_date = datetime(1970,1,1,0,0,0),
                            end_date = datetime(1999,12,31,0,0,0), start_month = 3,
                            end_month = 4, event_duration = timedelta(days = 15))
    print(maxs)

    #test get means
    means = get_annual_means(streamflow, times, start_date = datetime(1970,1,1,0,0,0), 
                                                 end_date = datetime(1999,12, 31,0,0,0))

    print(len(means))
    print(means[1980])
    print(means[1972].shape)
def main():
    import application_properties
    application_properties.set_current_directory()
    reproject_rivers_to_latlon_and_save_shape()
    #get_qc_rivers()

    #TODO: implement
    pass
def configure():
    plot_utils.apply_plot_params(font_size=10,
                                 width_pt=None,
                                 width_cm=17,
                                 height_cm=20)
    import application_properties

    application_properties.set_current_directory()
Beispiel #4
0
def main():
    import application_properties
    application_properties.set_current_directory()
    reproject_rivers_to_latlon_and_save_shape()
    #get_qc_rivers()

    #TODO: implement
    pass
Beispiel #5
0
def main():
    import application_properties
    application_properties.set_current_directory()


    # Create folder for output images
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)


    rea_driven_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5"
    rea_driven_label = "ERAI-CRCM5-L"

    gcm_driven_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    gcm_driven_label = "CanESM2-CRCM5-L"

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"


    params = dict(
        data_path=rea_driven_path, start_year=start_year_c, end_year=end_year_c, label=rea_driven_label)

    geo_data_file = "/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    rea_driven_config = RunConfig(**params)
    params.update(dict(data_path=gcm_driven_path, label=gcm_driven_label))

    gcm_driven_config = RunConfig(**params)

    r_obj = RPN(geo_data_file)
    facc = r_obj.get_first_record_for_name("FAA")
    fldr = r_obj.get_first_record_for_name("FLDR")
    lkfr = r_obj.get_first_record_for_name("ML")

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=rea_driven_path)

    basin_name_to_out_indices_map, basin_name_to_basin_mask = get_basin_to_outlet_indices_map(bmp_info=bmp_info,
                                                                                              accumulation_areas=facc,
                                                                                              directions=fldr,
                                                                                              lake_fraction_field=lkfr)



    # select lake rich basins
    sel_basins = ["ARN", "PYR", "LGR", "RDO", "SAG", "WAS"]
    basin_name_to_out_indices_map = {k: v for k, v in basin_name_to_out_indices_map.items() if k in sel_basins}


    rea_driven_daily = analysis.get_daily_climatology_for_rconf(rea_driven_config, var_name=varname, level=0)
    gcm_driven_daily = analysis.get_daily_climatology_for_rconf(gcm_driven_config, var_name=varname, level=0)

    rea_driven_config.data_daily = rea_driven_daily
    gcm_driven_config.data_daily = gcm_driven_daily

    plot_comparison_hydrographs(basin_name_to_out_indices_map, rea_config=rea_driven_config, gcm_config=gcm_driven_config)
def main_wrapper():
    import application_properties

    application_properties.set_current_directory()

    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    main()
Beispiel #7
0
def main():
    import application_properties

    application_properties.set_current_directory()
    import time

    t0 = time.clock()
    # study_interflow_effect()
    print("Execution time: {0} seconds".format(time.clock() - t0))
Beispiel #8
0
def main():
    import application_properties

    application_properties.set_current_directory()

    plot_utils.apply_plot_params(font_size=12, width_cm=25, height_cm=25)

    # Create folder for output images
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    with_lakes_c_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    with_lakes_label = "CRCM5-L"

    no_lakes_c_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    no_lakes_label = "CRCM5-NL"

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    nyears_to_future = 75

    with_lakes_config_c = RunConfig(data_path=with_lakes_c_path,
                                    start_year=start_year_c,
                                    end_year=end_year_c,
                                    label=with_lakes_label)
    with_lakes_config_f = with_lakes_config_c.get_shifted_config(
        nyears_to_future)

    no_lakes_config_c = RunConfig(data_path=no_lakes_c_path,
                                  start_year=start_year_c,
                                  end_year=end_year_c,
                                  label=no_lakes_label)
    no_lakes_config_f = no_lakes_config_c.get_shifted_config(nyears_to_future)

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=with_lakes_c_path)

    # Calculate daily climatologic fields
    with_lakes_c_daily = analysis.get_daily_climatology_for_rconf(
        with_lakes_config_c, var_name=varname, level=0)
    with_lakes_f_daily = analysis.get_daily_climatology_for_rconf(
        with_lakes_config_f, var_name=varname, level=0)

    no_lakes_c_daily = analysis.get_daily_climatology_for_rconf(
        no_lakes_config_c, var_name=varname, level=0)
    no_lakes_f_daily = analysis.get_daily_climatology_for_rconf(
        no_lakes_config_f, var_name=varname, level=0)

    configs = DataConfig(bmp_info, no_lakes_config_c, no_lakes_config_f,
                         with_lakes_config_c, with_lakes_config_f)
    args = (no_lakes_c_daily, no_lakes_f_daily, with_lakes_c_daily,
            with_lakes_f_daily)
    data = Data(*[arg[1] for arg in args])

    plot_comparison_std(configs, data, varname=varname)
def main():
    import application_properties

    application_properties.set_current_directory()
    import time

    t0 = time.clock()
    # study_interflow_effect()
    print("Execution time: {0} seconds".format(time.clock() - t0))
Beispiel #10
0
def main_wrapper():
    import application_properties

    application_properties.set_current_directory()

    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    main()
def prepare():
    import application_properties

    application_properties.set_current_directory()

    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    plot_utils.apply_plot_params(font_size=10, width_cm=20, height_cm=18)
Beispiel #12
0
def main_wrapper():
    print("Comparing with UDEL ...")
    import application_properties

    application_properties.set_current_directory()

    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    #
    main()
Beispiel #13
0
def main_wrapper():
    print("Comparing with CRU ...")
    import application_properties

    application_properties.set_current_directory()

    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    #
    main()
def main():
    print("start")
    import application_properties
    application_properties.set_current_directory()
    from util import plot_utils

    print("finished imports and set ups")
    plot_utils.apply_plot_params(width_pt=None, width_cm=15, height_cm=15, font_size=16)
    validate_daily_climatology()

    pass
Beispiel #15
0
def dem_cmap():

    import application_properties
    application_properties.set_current_directory()

    # Make a figure and axes with dimensions as desired.
    fig = plt.figure(figsize=(8,3))
    ax1 = fig.add_axes([0.05, 0.65, 0.9, 0.15])

    cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = get_dem_colormap(ncolors=10),
                                   orientation='horizontal')
    plt.show()
Beispiel #16
0
def test_ncl_map():

    import application_properties
    application_properties.set_current_directory()

    # Make a figure and axes with dimensions as desired.
    fig = plt.figure(figsize=(8,3))
    ax1 = fig.add_axes([0.05, 0.65, 0.9, 0.15])

    cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = get_cmap_from_ncl_spec_file(path="colormap_files/OceanLakeLandSnow.rgb",
                                            ncolors=  None),
                                   orientation='horizontal')
    plt.show()
Beispiel #17
0
def dem_cmap():

    import application_properties
    application_properties.set_current_directory()

    # Make a figure and axes with dimensions as desired.
    fig = plt.figure(figsize=(8, 3))
    ax1 = fig.add_axes([0.05, 0.65, 0.9, 0.15])

    cb1 = mpl.colorbar.ColorbarBase(ax1,
                                    cmap=get_dem_colormap(ncolors=10),
                                    orientation='horizontal')
    plt.show()
def main():
    from crcm5.analyse_hdf import do_analysis_using_pytables as analysis

    import application_properties
    application_properties.set_current_directory()

    # define the region of interest using indices
    sel = Selection(start_i=30, start_j=50, ni=40, nj=40)


    start_year = 1991
    end_year = 2010


    # Labels and paths to the data
    label_base = "CRCM5-NL"
    path_base = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-r.hdf5"


    labels = ["CRCM5-L1"]
    paths = ["/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-r.hdf5"]

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=path_base)


    # Do the calculations


    # Do the plotting

    fig = plt.figure()

    gs = GridSpec(2, 2)

    # Plot the domain
    ax = fig.add_subplot(gs[0, :])
    assert isinstance(ax, Axes)
    bmp_info.basemap.drawcoastlines(ax=ax)
    xx, yy = bmp_info.get_proj_xy()

    # add the region of interest to the map
    ll_i, ll_j = sel.ll_indices()
    ur_i, ur_j = sel.ur_indices()

    coords = ((ll_i, ll_j), (ll_i, ur_j), (ur_i, ur_j), (ur_i, ll_j))
    coords = [(xx[i, j], yy[i, j]) for (i, j) in coords]
    coords = np.array(coords)
    ax.add_patch(Polygon(coords, facecolor="none", linewidth=3))


    plt.show()
Beispiel #19
0
def test_ncl_map():

    import application_properties
    application_properties.set_current_directory()

    # Make a figure and axes with dimensions as desired.
    fig = plt.figure(figsize=(8, 3))
    ax1 = fig.add_axes([0.05, 0.65, 0.9, 0.15])

    cb1 = mpl.colorbar.ColorbarBase(
        ax1,
        cmap=get_cmap_from_ncl_spec_file(
            path="colormap_files/OceanLakeLandSnow.rgb", ncolors=None),
        orientation='horizontal')
    plt.show()
Beispiel #20
0
def main():
    from crcm5.analyse_hdf import do_analysis_using_pytables as analysis

    import application_properties
    application_properties.set_current_directory()

    # define the region of interest using indices
    sel = Selection(start_i=30, start_j=50, ni=40, nj=40)

    start_year = 1991
    end_year = 2010

    # Labels and paths to the data
    label_base = "CRCM5-NL"
    path_base = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-r.hdf5"

    labels = ["CRCM5-L1"]
    paths = [
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-r.hdf5"
    ]

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=path_base)

    # Do the calculations

    # Do the plotting

    fig = plt.figure()

    gs = GridSpec(2, 2)

    # Plot the domain
    ax = fig.add_subplot(gs[0, :])
    assert isinstance(ax, Axes)
    bmp_info.basemap.drawcoastlines(ax=ax)
    xx, yy = bmp_info.get_proj_xy()

    # add the region of interest to the map
    ll_i, ll_j = sel.ll_indices()
    ur_i, ur_j = sel.ur_indices()

    coords = ((ll_i, ll_j), (ll_i, ur_j), (ur_i, ur_j), (ur_i, ll_j))
    coords = [(xx[i, j], yy[i, j]) for (i, j) in coords]
    coords = np.array(coords)
    ax.add_patch(Polygon(coords, facecolor="none", linewidth=3))

    plt.show()
def main():
    import application_properties

    application_properties.set_current_directory()

    plot_utils.apply_plot_params(font_size=12, width_cm=25, height_cm=25)

    # Create folder for output images
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    with_lakes_c_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    with_lakes_label = "CRCM5-L"

    no_lakes_c_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    no_lakes_label = "CRCM5-NL"

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    nyears_to_future = 75

    with_lakes_config_c = RunConfig(data_path=with_lakes_c_path, start_year=start_year_c, end_year=end_year_c,
                                    label=with_lakes_label)
    with_lakes_config_f = with_lakes_config_c.get_shifted_config(nyears_to_future)

    no_lakes_config_c = RunConfig(data_path=no_lakes_c_path, start_year=start_year_c, end_year=end_year_c,
                                  label=no_lakes_label)
    no_lakes_config_f = no_lakes_config_c.get_shifted_config(nyears_to_future)

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=with_lakes_c_path)


    # Calculate daily climatologic fields
    with_lakes_c_daily = analysis.get_daily_climatology_for_rconf(with_lakes_config_c, var_name=varname, level=0)
    with_lakes_f_daily = analysis.get_daily_climatology_for_rconf(with_lakes_config_f, var_name=varname, level=0)

    no_lakes_c_daily = analysis.get_daily_climatology_for_rconf(no_lakes_config_c, var_name=varname, level=0)
    no_lakes_f_daily = analysis.get_daily_climatology_for_rconf(no_lakes_config_f, var_name=varname, level=0)

    configs = DataConfig(bmp_info, no_lakes_config_c, no_lakes_config_f, with_lakes_config_c, with_lakes_config_f)
    args = (no_lakes_c_daily, no_lakes_f_daily, with_lakes_c_daily, with_lakes_f_daily)
    data = Data(*[arg[1] for arg in args])

    plot_comparison_std(configs, data, varname=varname)
Beispiel #22
0
def demo():
    import application_properties

    application_properties.set_current_directory()
    am = AnuSplinManager()
    t0 = time.clock()
    df = am.get_daily_climatology_fields()
    print("Execution time: {0} seconds".format(time.clock() - t0))
    # x,t,y - the order of the axes



    print(dir(df))
    df = df.fillna(value=np.nan)

    annual_mean = np.mean(df.values[:, :, :], axis=1)

    annual_mean = np.ma.masked_where(~(annual_mean == annual_mean), annual_mean)
    import matplotlib.pyplot as plt

    b = Basemap(resolution="l")

    x, y = b(am.lons2d, am.lats2d)

    plt.figure()
    b.pcolormesh(x, y, annual_mean.transpose())
    b.drawcoastlines()
    b.colorbar()

    monthly_means = df.groupby(lambda d: d.month).mean()

    for the_month in range(1, 13):
        plt.figure()
        plt.title("{0}".format(the_month))

        v = monthly_means.values[:, the_month - 1, :].transpose()
        v = np.ma.masked_invalid(v)
        b.pcolormesh(x, y, v)
        b.drawcoastlines()
        b.colorbar()

    plt.show()
Beispiel #23
0
    def setUp(self):
        application_properties.set_current_directory()
        #the files with dumped parameters using pickle.dump()
        self.gml_result_folder = 'old_rl/rl_14_GML_half_and_half'
        self.lm_result_folder = 'old_rl/rl_7_LM'

        self.gml_parameters = {}
        self.lm_parameters = {}


        types = ['high', 'low']
        prefix = 'gev_params_stationary'

        name_pattern = prefix + '_%s_%s'

        for the_type in types:
            lm = []
            gml = []

            for the_id in members.all_members:
                #read lm and gml params
                path = os.path.join(self.lm_result_folder, name_pattern % (the_id, the_type))
                lm_pars_list = pickle.load(open(path))
                
                path = os.path.join(self.gml_result_folder, name_pattern % (the_id, the_type))
                gml_pars_list = pickle.load(open(path))
                
                for lm_pars, gml_pars in zip(lm_pars_list, gml_pars_list):
                    if lm_pars[0] == None or gml_pars[0] == None:
                        continue
                    lm.append(lm_pars[0:3])
                    gml.append(gml_pars[0:3])


            ##save to the object's fields
            print(the_type)
            assert len(gml) == len(lm), 'Not None: gml = %d, lm = %d' % (len(gml), len(lm))
            self.gml_parameters[the_type] = np.array(gml)
            self.lm_parameters[the_type] = np.array(lm)

            pass
Beispiel #24
0
def demo():
    import application_properties

    application_properties.set_current_directory()
    am = AnuSplinManager()
    t0 = time.clock()
    df = am.get_daily_climatology_fields()
    print("Execution time: {0} seconds".format(time.clock() - t0))
    # x,t,y - the order of the axes

    print(dir(df))
    df = df.fillna(value=np.nan)

    annual_mean = np.mean(df.values[:, :, :], axis=1)

    annual_mean = np.ma.masked_where(~(annual_mean == annual_mean),
                                     annual_mean)
    import matplotlib.pyplot as plt

    b = Basemap(resolution="l")

    x, y = b(am.lons2d, am.lats2d)

    plt.figure()
    b.pcolormesh(x, y, annual_mean.transpose())
    b.drawcoastlines()
    b.colorbar()

    monthly_means = df.groupby(lambda d: d.month).mean()

    for the_month in range(1, 13):
        plt.figure()
        plt.title("{0}".format(the_month))

        v = monthly_means.values[:, the_month - 1, :].transpose()
        v = np.ma.masked_invalid(v)
        b.pcolormesh(x, y, v)
        b.drawcoastlines()
        b.colorbar()

    plt.show()
Beispiel #25
0
def test():
    application_properties.set_current_directory()
    server = ECMWFDataServer(
        "http://data-portal.ecmwf.int/data/d/dataserver/", "b940ef6b2c284ed4c13c2eb15d6e7cdf", "*****@*****.**"
    )

    surf_and_subsurf_runoff_code = "9.128"

    server.retrieve(
        {
            "dataset": "interim_full_daily",
            "date": "20090101/to/20090110",
            "time": "00:00:00",
            "step": "6",
            "levtype": "sfc",
            "type": "fc",
            "param": surf_and_subsurf_runoff_code,  # "8.128",
            "class": "ei",
            "levelist": "/".join(map(lambda x: str(x), range(1, 61))),
            #'area'    : "60/-120/30/-60",
            "target": "data/era_interim/data_sro.nc",
        }
    )
def main():
    import application_properties
    application_properties.set_current_directory()
    stations = get_station_objects(db_path="/RESCUE/skynet3_rech1/huziy/hydat_db/Hydat.sqlite")
    """:type : list[data.cehq_station.Station] """

    directions_file = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/directions_north_america_0.11deg_floods.nc"
    fig = plt.figure()
    ax = fig.add_subplot(111)
    bmp = Basemap()


    ds = Dataset("calg_flood_stations_upstream_masks_na_0.11deg.nc", "w")
    """
    :type : netCDF4.Dataset
    """
    cell_manager = get_cell_manager_from_directions_file(path=directions_file)


    station_to_modelpoint = cell_manager.get_model_points_for_stations(station_list=stations, drainaige_area_reldiff_limit=0.15)


    for i, s in enumerate(stations):
        the_mask = _plot_station_position(ax=ax, the_station=s, basemap=bmp, cell_manager=cell_manager,
                                          the_model_point=station_to_modelpoint[s])

        # save to file
        if i == 0:
            # create dimensions
            ds.createDimension("x", the_mask.shape[0])
            ds.createDimension("y", the_mask.shape[1])

        v = ds.createVariable("station_{}".format(s.id), "i4", ("x", "y"))
        v[:] = the_mask

    ds.close()
Beispiel #27
0
def main():
    import application_properties
    application_properties.set_current_directory()

    # Create folder for output images
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    rea_driven_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5"
    rea_driven_label = "ERAI-CRCM5-L"

    gcm_driven_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    gcm_driven_label = "CanESM2-CRCM5-L"

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    params = dict(data_path=rea_driven_path,
                  start_year=start_year_c,
                  end_year=end_year_c,
                  label=rea_driven_label)

    geo_data_file = "/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    rea_driven_config = RunConfig(**params)
    params.update(dict(data_path=gcm_driven_path, label=gcm_driven_label))

    gcm_driven_config = RunConfig(**params)

    r_obj = RPN(geo_data_file)
    facc = r_obj.get_first_record_for_name("FAA")
    fldr = r_obj.get_first_record_for_name("FLDR")
    lkfr = r_obj.get_first_record_for_name("ML")

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=rea_driven_path)

    basin_name_to_out_indices_map, basin_name_to_basin_mask = get_basin_to_outlet_indices_map(
        bmp_info=bmp_info,
        accumulation_areas=facc,
        directions=fldr,
        lake_fraction_field=lkfr)

    # select lake rich basins
    sel_basins = ["ARN", "PYR", "LGR", "RDO", "SAG", "WAS"]
    basin_name_to_out_indices_map = {
        k: v
        for k, v in basin_name_to_out_indices_map.items() if k in sel_basins
    }

    rea_driven_daily = analysis.get_daily_climatology_for_rconf(
        rea_driven_config, var_name=varname, level=0)
    gcm_driven_daily = analysis.get_daily_climatology_for_rconf(
        gcm_driven_config, var_name=varname, level=0)

    rea_driven_config.data_daily = rea_driven_daily
    gcm_driven_config.data_daily = gcm_driven_daily

    plot_comparison_hydrographs(basin_name_to_out_indices_map,
                                rea_config=rea_driven_config,
                                gcm_config=gcm_driven_config)
    hours.sort()
    stamp_dates = toStampDates(hours, startDate)

    t1, v1 = get_mean_for_day_of_year(stamp_dates, [ hourToValues[h][0] for h in hours])
    t2, v2 = get_mean_for_day_of_year(stamp_dates, [ hourToValues[h][1] for h in hours])

    plt.figure()
    plt.plot(t1, v1, label = 'surface runoff', lw = 3)
    plt.plot(t2, v2, label = 'total runoff', lw = 3)
    plt.title(title)
    ax = plt.gca()
    ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%b'))
    plt.legend()
    plt.savefig(outfile)





def main():
    withFix = get_mean_time_series(path = 'data/CORDEX/NA_fix')
    withoutFix = get_mean_time_series(path = 'data/CORDEX/NA')
    plot_timeseries(withFix, 'runoff_with_fix.png', 'with fix')
    plot_timeseries(withoutFix, 'runoff_without_fix.png', 'without fix')


if __name__ == "__main__":
    application_properties.set_current_directory()
    main()
    print("Hello World")
Beispiel #29
0
def setup():
    import application_properties

    application_properties.set_current_directory()
    print("setting up the test suite")
def main():
    import application_properties
    application_properties.set_current_directory()
    if not IMG_FOLDER.exists():
        IMG_FOLDER.mkdir(parents=True)

    plot_utils.apply_plot_params(font_size=14,
                                 width_pt=None,
                                 width_cm=20,
                                 height_cm=20)
    start_year = 1980
    end_year = 2010

    varname = "TDRA"

    base_config = RunConfig(
        start_year=start_year,
        end_year=end_year,
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        label="NI")

    modif_config = RunConfig(
        start_year=start_year,
        end_year=end_year,
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl-intfl_ITFS.hdf5",
        label="WI")

    r_obj = RPN(GEO_DATA_FILE)
    facc = r_obj.get_first_record_for_name("FAA")
    fldr = r_obj.get_first_record_for_name("FLDR")
    mg = r_obj.get_first_record_for_name("MG")

    lons, lats, bmp = analysis.get_basemap_from_hdf(
        file_path=base_config.data_path)

    # Calculate the daily mean fields
    dates, daily_clim_base = analysis.get_daily_climatology_for_rconf(
        base_config, var_name=varname, level=0)
    _, daily_clim_modif = analysis.get_daily_climatology_for_rconf(
        modif_config, var_name=varname, level=0)

    _, pr_base = analysis.get_daily_climatology_for_rconf(base_config,
                                                          var_name="PR",
                                                          level=0)
    _, pr_modif = analysis.get_daily_climatology_for_rconf(modif_config,
                                                           var_name="PR",
                                                           level=0)

    _, av_base = analysis.get_daily_climatology_for_rconf(base_config,
                                                          var_name="AV",
                                                          level=0)
    _, av_modif = analysis.get_daily_climatology_for_rconf(modif_config,
                                                           var_name="AV",
                                                           level=0)

    _, intf_rates = analysis.get_daily_climatology_for_rconf(modif_config,
                                                             var_name="INTF",
                                                             level=0)

    # Plot the difference
    fig = plt.figure()
    xx, yy = bmp(lons, lats)

    # daily_clim_base = np.array([f for d, f in zip(dates, daily_clim_base) if d.month not in range(3, 12)])
    # daily_clim_modif = np.array([f for d, f in zip(dates, daily_clim_modif) if d.month not in range(3, 12)])

    mean_base = np.mean(daily_clim_base, axis=0)
    diff = (np.mean(daily_clim_modif, axis=0) - mean_base) * 24 * 3600
    dpr = (pr_modif.sum(axis=0) - pr_base.sum(axis=0)) / pr_base.sum(axis=0)
    dav = (av_modif.sum(axis=0) - av_base.sum(axis=0)) / av_base.sum(axis=0)
    diff = np.ma.masked_where(
        (mg <= 1.0e-3) | (dpr < 0) | (dav > 0) | (diff > 0), diff)

    print("{}-ranges: {}, {}".format(varname, daily_clim_base.min(),
                                     daily_clim_base.max()))
    print("{}-ranges: {}, {}".format(varname, daily_clim_modif.min(),
                                     daily_clim_modif.max()))

    limit_base = np.percentile(daily_clim_base, 90, axis=0)
    limit_modif = np.percentile(daily_clim_modif, 50, axis=0)
    limit_lower_intf = 1.0e-4 / (24.0 * 60.0 * 60.0)

    ndays_base = daily_clim_base > limit_base[np.newaxis, :, :]
    ndays_base = ndays_base.sum(axis=0)

    ndays_modif = daily_clim_modif > limit_base[np.newaxis, :, :]
    ndays_modif = ndays_modif.sum(axis=0)

    diff_days = np.ma.masked_where(
        (mg <= 1.0e-4) | (intf_rates.max() < limit_lower_intf),
        ndays_modif - ndays_base)

    diff_days = np.ma.masked_where(diff_days > 0, diff_days)

    print("diff_days ranges: {} to {}".format(diff_days.min(),
                                              diff_days.max()))

    im = bmp.pcolormesh(xx, yy, diff)
    bmp.colorbar(im)
    bmp.drawcoastlines()

    img_file = IMG_FOLDER.joinpath("{}_{}-{}.png".format(
        varname, start_year, end_year))
    with img_file.open("wb") as imf:
        fig.savefig(imf, bbox_inches="tight")

    plt.show()
Beispiel #31
0
def configure():
    plot_utils.apply_plot_params(font_size=10, width_pt=None, width_cm=17, height_cm=20)
    import application_properties

    application_properties.set_current_directory()
Beispiel #32
0
 def setUp(self):
     super(MyTestCase, self).setUp()
     import application_properties
     application_properties.set_current_directory()
Beispiel #33
0
def main():
    import application_properties
    application_properties.set_current_directory()
Beispiel #34
0
def setup():
    import application_properties

    application_properties.set_current_directory()
    print("setting up the test suite")
Beispiel #35
0
 def setUp(self):
     super(MyTestCase, self).setUp()
     import application_properties
     application_properties.set_current_directory()
def test():
    application_properties.set_current_directory()
    swe = SweHolder(path="data/swe_ross_brown/B2003_daily_swe")
    swe.convertAllDataToNetcdf()
def main():
    import application_properties

    application_properties.set_current_directory()

    # Create folder for output images
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    rea_driven_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5"
    rea_driven_label = "CRCM5-L-ERAI"

    # gcm_driven_path_c = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    gcm_driven_path_c = "/home/huziy/skynet3_rech1/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    gcm_driven_label_c = "CRCM5-L"

    gcm_driven_path_f = "/home/huziy/skynet3_rech1/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-2070-2100.hdf5"


    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    future_shift_years = 90

    params = dict(
        data_path=rea_driven_path, start_year=start_year_c, end_year=end_year_c, label=rea_driven_label)

    geo_data_file = "/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    rea_driven_config = RunConfig(**params)
    params.update(dict(data_path=gcm_driven_path_c, label=gcm_driven_label_c))

    gcm_driven_config_c = RunConfig(**params)
    gcm_driven_config_f = gcm_driven_config_c.get_shifted_config(shift_years=future_shift_years, data_path=gcm_driven_path_f)

    r_obj = RPN(geo_data_file)
    facc = r_obj.get_first_record_for_name("FAA")
    fldr = r_obj.get_first_record_for_name("FLDR")
    lkfr = r_obj.get_first_record_for_name("ML")


    # get basemap information
    bmp_info = analysis.get_basemap_info_from_hdf(file_path=rea_driven_path)


    # get basin mask
    _, bname_to_mask = plot_basin_outlets(bmp_info=bmp_info, directions=fldr, accumulation_areas=facc, lake_fraction_field=lkfr)

    all_basins_mask = np.zeros(fldr.shape)
    for bname, the_mask in bname_to_mask.items():
        all_basins_mask += the_mask



    rs_gcm_c = get_return_levels_and_unc_using_bootstrap(gcm_driven_config_c, varname=varname)

    rs_gcm_f = get_return_levels_and_unc_using_bootstrap(gcm_driven_config_f, varname=varname)

    plot_utils.apply_plot_params(font_size=10, width_cm=20, height_cm=18)

    # Plot return level changes
    fig = plt.figure()
    nplots = 0
    for the_type, rp_to_rl in rs_gcm_c.return_lev_dict.items():
        nplots += len(rp_to_rl)
    ncols = 2
    nrows = nplots // ncols + int(nplots % ncols != 0)
    gs = GridSpec(nrows, ncols + 1, width_ratios=[1.0, ] * ncols + [0.05, ])

    xx, yy = bmp_info.get_proj_xy()

    cmap = cm.get_cmap("bwr", 20)

    limits = {
        "high": (-50, 50),
        "low": (-150, 150)
    }

    for row, (the_type, rp_to_rl) in enumerate(sorted(rs_gcm_c.return_lev_dict.items(), key=lambda itm: itm[0])):

        for col, rp in enumerate(sorted(rp_to_rl)):

            ax = fig.add_subplot(gs[row, col])
            rl = rp_to_rl[rp]

            # Ignore 0 return levels in the current climate for percentage calculations
            # rl = np.ma.masked_where(rl <= 0, rl)


            rl_future = rs_gcm_f.return_lev_dict[the_type][rp]

            # Calculate climate change signal
            diff = (rl_future - rl) / (np.abs(rl + rl_future) * 0.5) * 100


            diff[(rl_future <= 0) & (rl <= 0)] = 0

            # diff = rl


            diff = maskoceans(bmp_info.lons, bmp_info.lats, diff)
            print(diff.min(), diff.max())

            std_c = rs_gcm_c.std_dict[the_type][rp]
            std_f = rs_gcm_f.std_dict[the_type][rp]

            significance = (np.ma.abs(diff) >= 1.96 * (std_c + std_f)) & (~diff.mask)
            significance = significance.astype(int)

            vmin, vmax = limits[the_type]
            diff = np.ma.masked_where(all_basins_mask < 0.5, diff)
            im = bmp_info.basemap.pcolormesh(xx, yy, diff, vmin=vmin, vmax=vmax, cmap=cmap)

            significance = np.ma.masked_where(all_basins_mask < 0.5, significance)
            cs = bmp_info.basemap.contourf(xx, yy, significance,
                                           levels=[0, 0.5, 1],
                                           hatches=["////", None, None],
                                           colors="none")

            if row == nrows - 1 and col == ncols - 1:
                # create a legend for the contour set
                artists, labels = cs.legend_elements()
                ax.legend([artists[0], ], ["not significant", ], handleheight=0.5,
                          bbox_to_anchor=(1, -0.1), loc="upper right", borderaxespad=0.)

            ax.set_title("T = {}-year".format(rp))

            if col == 0:
                ax.set_ylabel("{} flow".format(the_type))

            bmp_info.basemap.drawcoastlines(ax=ax)
            bmp_info.basemap.drawmapboundary(fill_color="0.75")
            bmp_info.basemap.readshapefile(".".join(quebec_info.BASIN_BOUNDARIES_DERIVED_10km.split(".")[:-1]).replace("utm18", "latlon"),
                                           "basin",
                                           linewidth=1.2, ax=ax)

            if col == ncols - 1:
                cax = fig.add_subplot(gs[row, -1])
                plt.colorbar(im, cax=cax, extend="both")
                cax.set_title("%")



            # Print basin average changes
            print("--- start ----------------{rp}-year {ext_type} flow ----------------".format(rp=rp, ext_type=the_type))

            bname_to_diff = [(bname, diff[the_mask > 0.5].mean()) for bname, the_mask in bname_to_mask.items()]
            bname_to_diff = list(sorted(bname_to_diff, key=lambda item: item[1]))

            for bname, the_diff in bname_to_diff:
                print("{bname}: cc = {cc:.2f}%".format(bname=bname, cc=the_diff))

            print("--- end ----------------{rp}-year {ext_type} flow ----------------".format(rp=rp, ext_type=the_type))

    img_file = img_folder.joinpath("rl_cc_{}.png".format(gcm_driven_config_c.label))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight")

    plt.close(fig)
Beispiel #38
0
def main():
    import application_properties
    application_properties.set_current_directory()
    if not IMG_FOLDER.exists():
        IMG_FOLDER.mkdir(parents=True)

    plot_utils.apply_plot_params(font_size=14, width_pt=None, width_cm=20, height_cm=20)
    start_year = 1980
    end_year = 2010

    varname = "TDRA"

    base_config = RunConfig(start_year=start_year, end_year=end_year,
                            data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
                            label="NI")

    modif_config = RunConfig(start_year=start_year, end_year=end_year,
                             data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl-intfl_ITFS.hdf5",
                             label="WI")


    r_obj = RPN(GEO_DATA_FILE)
    facc = r_obj.get_first_record_for_name("FAA")
    fldr = r_obj.get_first_record_for_name("FLDR")
    mg = r_obj.get_first_record_for_name("MG")

    lons, lats, bmp = analysis.get_basemap_from_hdf(file_path=base_config.data_path)

    # Calculate the daily mean fields
    dates, daily_clim_base = analysis.get_daily_climatology_for_rconf(base_config, var_name=varname, level=0)
    _, daily_clim_modif = analysis.get_daily_climatology_for_rconf(modif_config, var_name=varname, level=0)

    _, pr_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="PR", level=0)
    _, pr_modif = analysis.get_daily_climatology_for_rconf(modif_config, var_name="PR", level=0)

    _, av_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="AV", level=0)
    _, av_modif = analysis.get_daily_climatology_for_rconf(modif_config, var_name="AV", level=0)


    _, intf_rates = analysis.get_daily_climatology_for_rconf(modif_config, var_name="INTF", level=0)

    # Plot the difference
    fig = plt.figure()
    xx, yy = bmp(lons, lats)

    # daily_clim_base = np.array([f for d, f in zip(dates, daily_clim_base) if d.month not in range(3, 12)])
    # daily_clim_modif = np.array([f for d, f in zip(dates, daily_clim_modif) if d.month not in range(3, 12)])

    mean_base = np.mean(daily_clim_base, axis=0)
    diff = (np.mean(daily_clim_modif, axis=0) - mean_base) * 24 * 3600
    dpr = (pr_modif.sum(axis=0) - pr_base.sum(axis=0)) / pr_base.sum(axis=0)
    dav = (av_modif.sum(axis=0) - av_base.sum(axis=0)) / av_base.sum(axis=0)
    diff = np.ma.masked_where((mg <= 1.0e-3) | (dpr < 0) | (dav > 0) | (diff > 0), diff)

    print("{}-ranges: {}, {}".format(varname, daily_clim_base.min(), daily_clim_base.max()))
    print("{}-ranges: {}, {}".format(varname, daily_clim_modif.min(), daily_clim_modif.max()))

    limit_base = np.percentile(daily_clim_base, 90, axis=0)
    limit_modif = np.percentile(daily_clim_modif, 50, axis=0)
    limit_lower_intf = 1.0e-4 / (24.0 * 60.0 * 60.0)


    ndays_base = daily_clim_base > limit_base[np.newaxis, :, :]
    ndays_base = ndays_base.sum(axis=0)

    ndays_modif = daily_clim_modif > limit_base[np.newaxis, :, :]
    ndays_modif = ndays_modif.sum(axis=0)

    diff_days = np.ma.masked_where((mg <= 1.0e-4) | (intf_rates.max() < limit_lower_intf),
                                   ndays_modif - ndays_base)

    diff_days = np.ma.masked_where(diff_days > 0, diff_days)

    print("diff_days ranges: {} to {}".format(diff_days.min(), diff_days.max()))


    im = bmp.pcolormesh(xx, yy, diff)
    bmp.colorbar(im)
    bmp.drawcoastlines()




    img_file = IMG_FOLDER.joinpath("{}_{}-{}.png".format(varname, start_year, end_year))
    with img_file.open("wb") as imf:
        fig.savefig(imf, bbox_inches="tight")

    plt.show()
Beispiel #39
0
 def setUp(self):
     import application_properties
     application_properties.set_current_directory()
Beispiel #40
0
Datei: test.py Projekt: guziy/RPN
 def setUp(self):
     import application_properties
     application_properties.set_current_directory()
    plt.savefig("offline_validation.png", dpi=400)
    plt.close(fig)

    r = RPN(
        "/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/Depth_to_bedrock_WestNA_0.25")
    r.get_first_record_for_name("8L")
    proj_params = r.get_proj_parameters_for_the_last_read_rec()
    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    bsmp = RotatedLatLon(**proj_params).get_basemap_object_for_lons_lats(
        lons2d=lons, lats2d=lats)
    plot_utils.apply_plot_params(width_pt=None,
                                 width_cm=19,
                                 height_cm=19,
                                 font_size=12)
    plot_station_positions(manager=None, station_list=stations, bsmp=bsmp)


if __name__ == "__main__":
    import application_properties

    application_properties.set_current_directory()

    from util import plot_utils

    plot_utils.apply_plot_params(width_pt=None,
                                 width_cm=19,
                                 height_cm=40,
                                 font_size=22)

    main()
    print("Hello world")
Beispiel #42
0
def test():
    application_properties.set_current_directory()
    main()