Example #1
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():
    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)
Example #3
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)
Example #4
0
def main():
    start_year = 1980
    end_year = 2010

    season_to_months = OrderedDict([
        ("Winter", [12, 1, 2]),
        ("Spring", [3, 4, 5]),
        ("Summer", [6, 7, 8]),
        ("Fall", [9, 10, 11]),
    ])

    model_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5"

    sim_config = RunConfig(data_path=model_path,
                           start_year=start_year,
                           end_year=end_year,
                           label="ERAI-CRCM5-L")

    plot_corr_fields_and_std(vname="TT",
                             season_to_months=season_to_months,
                             sim_config=sim_config)
    plot_corr_fields_and_std(vname="PR",
                             season_to_months=season_to_months,
                             sim_config=sim_config)
Example #5
0
def main():


    vname_model = "I5"
    nx_agg = 2
    ny_agg = 2



    start_year = 1980
    end_year = 2006

    r_config = RunConfig(
        data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        start_year=start_year, end_year=end_year, label="ERAI-CRCM5-L"
    )


    bmp_info = analysis.get_basemap_info(r_config=r_config)
    bmp_info_agg = bmp_info.get_aggregated(nagg_x=nx_agg, nagg_y=ny_agg)

    season_to_months = OrderedDict([
        ("Winter", [12, 1, 2]),
         ("Spring", [3, 4, 5])
    ])



    # Get the model data
    seasonal_clim_fields_model = analysis.get_seasonal_climatology_for_runconfig(run_config=r_config,
                                                                                 varname=vname_model, level=0,
                                                                                 season_to_months=season_to_months)

    season_to_clim_fields_model_agg = OrderedDict()
    for season, field in seasonal_clim_fields_model.items():
        season_to_clim_fields_model_agg[season] = aggregate_array(field, nagg_x=nx_agg, nagg_y=ny_agg)



    # Get the EASE data
    obs_manager = EaseSweManager()
    season_to_clim_fields_obs = obs_manager.get_seasonal_clim_interpolated_to(target_lon2d=bmp_info_agg.lons, target_lat2d=bmp_info_agg.lats,
                                                                              season_to_months=season_to_months, start_year=start_year, end_year=end_year)


    # Do the plotting
    plot_utils.apply_plot_params(font_size=10, width_cm=16, height_cm=24)
    fig = plt.figure()
    xx, yy = bmp_info_agg.get_proj_xy()

    gs = GridSpec(3, len(season_to_clim_fields_model_agg) + 1, width_ratios=[1.0, ] * len(season_to_clim_fields_model_agg) + [0.05, ])
    clevs = [0, 50, 60, 70, 80, 90, 100, 150, 200, 250, 300, 350, 400, 500]
    norm = BoundaryNorm(clevs, 256)

    clevs_diff = np.arange(-100, 110, 10)

    cs_val = None
    cs_diff = None

    col = 0

    lons_agg_copy = bmp_info_agg.lons.copy()
    lons_agg_copy[lons_agg_copy > 180] -= 360

    lons_copy = bmp_info.lons.copy()
    lons_copy[lons_copy > 180] -= 360

    xx1, yy1 = bmp_info.get_proj_xy()

    for season, mod_field in seasonal_clim_fields_model.items():


        obs_field = season_to_clim_fields_obs[season]

        row = 0
        ax = fig.add_subplot(gs[row, col])
        ax.set_title(season)

        obs_field = maskoceans(lons_agg_copy, bmp_info_agg.lats, obs_field)
        cs_val = bmp_info_agg.basemap.contourf(xx, yy, obs_field, levels=clevs, norm=norm, ax=ax, extend="max")
        bmp_info_agg.basemap.drawcoastlines(linewidth=0.3, ax=ax)
        if col == 0:
            ax.set_ylabel("NSIDC")

        row += 1
        ax = fig.add_subplot(gs[row, col])
        mod_field = maskoceans(lons_copy, bmp_info.lats, mod_field)
        bmp_info.basemap.contourf(xx1, yy1, mod_field, levels=cs_val.levels, norm=cs_val.norm, ax=ax, extend="max")
        bmp_info.basemap.drawcoastlines(linewidth=0.3, ax=ax)
        if col == 0:
            ax.set_ylabel(r_config.label)

        row += 1
        ax = fig.add_subplot(gs[row, col])
        cs_diff = bmp_info_agg.basemap.contourf(xx, yy, season_to_clim_fields_model_agg[season] - obs_field, levels=clevs_diff, ax=ax, extend="both", cmap="seismic")
        bmp_info_agg.basemap.drawcoastlines(linewidth=0.3, ax=ax)

        if col == 0:
            ax.set_ylabel("{} minus {}".format(r_config.label, "NSIDC"))

        col += 1



    # Add values colorbar
    ax = fig.add_subplot(gs[0, -1])
    plt.colorbar(cs_val, cax=ax)
    ax.set_title("mm")


    # Add differences colorbaar
    ax = fig.add_subplot(gs[-1, -1])
    plt.colorbar(cs_diff, cax=ax)
    ax.set_title("mm")

    fig.tight_layout()
    fig.savefig(os.path.join(img_folder, "NSIDC_vs_CRCM_swe.png"), dpi=common_plot_params.FIG_SAVE_DPI, bbox_inches="tight")
Example #6
0
def main_interflow():
    # Changes global plot properties mainly figure size and font size
    plot_utils.apply_plot_params(font_size=12, width_cm=20)

    # season_to_months = get_default_season_to_months_dict()
    season_to_months = OrderedDict([("January", [1, ]), ("February", [2, ]), ("March", [3, ]), ])

    var_names = ["TT", "HU", "PR", "AV", "TRAF", "I1", "STFL"]

    levels = [0, ] * len(var_names)

    plot_utils.apply_plot_params(font_size=10, width_pt=None, width_cm=20 * len(season_to_months) / 4.0,
                                 height_cm=20 * len(var_names) / 5.0)

    multipliers = {
        "PR": 1.,
        "TRAF": 1.,
        "I1": infovar.soil_layer_widths_26_to_60[0] * 1000.0,
        "TRAF+TDRA": 24 * 60 * 60
    }

    name_to_units = {
        "TRAF": "mm/day", "I1": "mm", "PR": "mm/day", "TRAF+TDRA": "mm/day"
    }

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

    modif_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
                         "quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5"
    modif_label = "CanESM2-CRCM5-LI"

    start_year_c = 1980
    end_year_c = 2010

    future_shift_years = 90

    params = dict(
        data_path=base_current_path, start_year=start_year_c, end_year=end_year_c, label=base_label
    )

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    params.update(
        dict(data_path=modif_current_path, label=modif_label)
    )

    modif_config_c = RunConfig(**params)
    modif_config_f = modif_config_c.get_shifted_config(future_shift_years)

    config_dict = OrderedDict([
        ("Current", OrderedDict([(base_label, base_config_c), (modif_label, modif_config_c)])),
        ("Future", OrderedDict([(base_label, base_config_f), (modif_label, modif_config_f)]))
    ])


    # Plot the differences
    fig = plt.figure()
    gs = GridSpec(len(var_names), len(season_to_months) + 1, width_ratios=[1., ] * len(season_to_months) + [0.05, ])
    config_dict.fig = fig
    config_dict.gs = gs
    config_dict.label_modif = modif_config_c.label
    config_dict.label_base = base_config_c.label
    config_dict.season_to_months = season_to_months
    config_dict.multipliers = multipliers

    lons, lats, bmp = analysis.get_basemap_from_hdf(base_current_path)
    config_dict.lons = lons
    config_dict.lats = lats
    config_dict.basemap = bmp
    config_dict.name_to_units = name_to_units

    # Calculate and plot seasonal means
    for vname, level, the_row in zip(var_names, levels, list(range(len(levels)))):
        config_dict.the_row = the_row

        _plot_row(vname=vname, level=level, config_dict=config_dict)

    # Save the image to the file
    img_path = get_image_path(base_config_c, base_config_f, modif_config_c, season_to_months=season_to_months)
    fig.savefig(img_path, bbox_inches="tight")
Example #7
0
def main():

    obs_data_path = Path("/RESCUE/skynet3_rech1/huziy/obs_data_for_HLES/interploated_to_the_same_grid/GL_0.1_452x260/anusplin+_interpolated_tt_pr.nc")

    start_year = 1980
    end_year = 2010

    HL_LABEL = "CRCM5_HL"
    NEMO_LABEL = "CRCM5_NEMO"


    vars_of_interest = [
        LAKE_ICE_FRACTION,
    ]

    sim_configs = {

        HL_LABEL: RunConfig(data_path="/RECH2/huziy/coupling/GL_440x260_0.1deg_GL_with_Hostetler/Samples_selected",
                  start_year=start_year, end_year=end_year, label=HL_LABEL),

        NEMO_LABEL: RunConfig(data_path="/RECH2/huziy/coupling/coupled-GL-NEMO1h_30min/selected_fields",
                  start_year=start_year, end_year=end_year, label=NEMO_LABEL),
    }

    sim_labels = [HL_LABEL, NEMO_LABEL]

    vname_to_level = {
        T_AIR_2M: VerticalLevel(1, level_kinds.HYBRID),
        U_WE: VerticalLevel(1, level_kinds.HYBRID),
        V_SN: VerticalLevel(1, level_kinds.HYBRID),
    }


    # Calculations

    # prepare params for interpolation
    lons_t, lats_t, bsmap = get_target_lons_lats_basemap(sim_configs[HL_LABEL])
    xt, yt, zt = lat_lon.lon_lat_to_cartesian(lons_t.flatten(), lats_t.flatten())


    vname_map = {}
    vname_map.update(default_varname_mappings.vname_map_CRCM5)



    # Read and calculate observed seasonal means
    store_config = {
            "base_folder": obs_data_path.parent,
            "data_source_type": data_source_types.ALL_VARS_IN_A_FOLDER_IN_NETCDF_FILES_OPEN_EACH_FILE_SEPARATELY,
            "varname_mapping": vname_map,
            "level_mapping": vname_to_level,
            "offset_mapping": default_varname_mappings.vname_to_offset_CRCM5,
            "multiplier_mapping": default_varname_mappings.vname_to_multiplier_CRCM5,
    }

    obs_dm = DataManager(store_config=store_config)
    obs_data = {}

    interp_indices = None
    for vname in vars_of_interest:
        # --
        end_year_for_current_var = end_year
        if vname == SWE:
            end_year_for_current_var = min(1996, end_year)

        # --
        seas_to_year_to_max = obs_dm.get_seasonal_maxima(varname_internal=vname,
                                                     start_year=start_year,
                                                     end_year=end_year_for_current_var,
                                                     season_to_months=season_to_months)

        seas_to_clim = {seas: np.array(list(y_to_means.values())).mean(axis=0) for seas, y_to_means in seas_to_year_to_max.items()}
        obs_data[vname] = seas_to_clim

        if interp_indices is None:
            _, interp_indices = obs_dm.get_kdtree().query(list(zip(xt, yt, zt)))

        for season in seas_to_clim:
            seas_to_clim[season] = seas_to_clim[season].flatten()[interp_indices].reshape(lons_t.shape)

    # Read and calculate simulated seasonal mean biases
    sim_data = defaultdict(dict)
    for label, r_config in sim_configs.items():

        store_config = {
                "base_folder": r_config.data_path,
                "data_source_type": data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT_VNAME_IN_FNAME,
                "varname_mapping": vname_map,
                "level_mapping": vname_to_level,
                "offset_mapping": default_varname_mappings.vname_to_offset_CRCM5,
                "multiplier_mapping": default_varname_mappings.vname_to_multiplier_CRCM5,
        }


        dm = DataManager(store_config=store_config)


        interp_indices = None
        for vname in vars_of_interest:

            # --
            end_year_for_current_var = end_year
            if vname == SWE:
                end_year_for_current_var = min(1996, end_year)

            # --
            seas_to_year_to_max = dm.get_seasonal_maxima(varname_internal=vname,
                                                           start_year=start_year,
                                                           end_year=end_year_for_current_var,
                                                           season_to_months=season_to_months)

            # get the climatology
            seas_to_clim = {seas: np.array(list(y_to_means.values())).mean(axis=0) for seas, y_to_means in seas_to_year_to_max.items()}

            sim_data[label][vname] = seas_to_clim

            if interp_indices is None:
                _, interp_indices = dm.get_kdtree().query(list(zip(xt, yt, zt)))

            for season in seas_to_clim:
                seas_to_clim[season] = seas_to_clim[season].flatten()[interp_indices].reshape(lons_t.shape) - obs_data[vname][season]







    # Plotting: interpolate to the same grid and plot obs and biases
    plot_utils.apply_plot_params(width_cm=32, height_cm=20, font_size=8)



    xx, yy = bsmap(lons_t, lats_t)
    lons_t[lons_t > 180] -= 360
    field_mask = ~maskoceans(lons_t, lats_t, np.zeros_like(lons_t)).mask

    for vname in vars_of_interest:

        fig = plt.figure()

        fig.suptitle(internal_name_to_title[vname] + "\n")

        nrows = len(sim_configs) + 2
        ncols = len(season_to_months)
        gs = GridSpec(nrows=nrows, ncols=ncols)



        # Plot the obs fields
        current_row = 0
        for col, season in enumerate(season_to_months):
            field = obs_data[vname][season]
            ax = fig.add_subplot(gs[current_row, col])
            ax.set_title(season)

            to_plot = np.ma.masked_where(field_mask, field) * internal_name_to_multiplier[vname]
            clevs = get_clevs(vname)

            if clevs is not None:
                bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                cmap = cm.get_cmap("jet", len(clevs) - 1)
            else:
                cmap = "jet"
                bnorm = None

            cs = bsmap.contourf(xx, yy, to_plot, ax=ax, levels=get_clevs(vname), norm=bnorm, cmap=cmap)
            bsmap.drawcoastlines()
            bsmap.colorbar(cs, ax=ax)

            if col == 0:
                ax.set_ylabel("Obs")



        # plot the biases
        for sim_label in sim_labels:
            current_row += 1
            for col, season in enumerate(season_to_months):

                field = sim_data[sim_label][vname][season]

                ax = fig.add_subplot(gs[current_row, col])

                clevs = get_clevs(vname + "bias")
                if clevs is not None:
                    bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                    cmap = cm.get_cmap("bwr", len(clevs) - 1)
                else:
                    cmap = "bwr"
                    bnorm = None

                to_plot = np.ma.masked_where(field_mask, field) * internal_name_to_multiplier[vname]
                cs = bsmap.contourf(xx, yy, to_plot, ax=ax, extend="both", levels=get_clevs(vname + "bias"), cmap=cmap, norm=bnorm)
                bsmap.drawcoastlines()
                bsmap.colorbar(cs, ax=ax)

                if col == 0:
                    ax.set_ylabel("{}\n-\nObs.".format(sim_label))


        # plot differences between the biases
        current_row += 1
        for col, season in enumerate(season_to_months):

            field = sim_data[NEMO_LABEL][vname][season] - sim_data[HL_LABEL][vname][season]

            ax = fig.add_subplot(gs[current_row, col])

            clevs = get_clevs(vname + "biasdiff")
            if clevs is not None:
                bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                cmap = cm.get_cmap("bwr", len(clevs) - 1)
            else:
                cmap = "bwr"
                bnorm = None

            to_plot = np.ma.masked_where(field_mask, field) * internal_name_to_multiplier[vname]
            cs = bsmap.contourf(xx, yy, to_plot, ax=ax, extend="both", levels=get_clevs(vname + "biasdiff"), cmap=cmap, norm=bnorm)
            bsmap.drawcoastlines()
            bsmap.colorbar(cs, ax=ax)

            if col == 0:
                ax.set_ylabel("{}\n-\n{}".format(NEMO_LABEL, HL_LABEL))


        fig.tight_layout()

        # save a figure per variable
        img_file = "seasonal_biases_{}_{}_{}-{}.png".format(vname,
                                                            "-".join([s for s in season_to_months]),
                                                            start_year, end_year)
        img_file = img_folder.joinpath(img_file)

        fig.savefig(str(img_file))

        plt.close(fig)
def main(hdf_folder="/home/huziy/skynet3_rech1/hdf_store", start_year=1980, end_year=2010):
    prepare()

    all_markers = ["*", "s", "p", "+", "x", "d", "h"]

    excluded = ["white", "w", "aliceblue", "azure"]
    excluded.extend([ci for ci in colors.cnames if "yellow" in ci])

    all_colors = ["k", "b", "r", "g", "m"] + sorted([ci for ci in colors.cnames if ci not in excluded])

    # Station ids to get from the CEHQ database
    ids_with_lakes_upstream = [
        "104001", "093806", "093801", "081002", "081007", "080718"
    ]

    selected_ids = ids_with_lakes_upstream

    filedir = Path(hdf_folder)
    sim_name_to_file_path = OrderedDict([
        # ("CRCM5-LI", filedir.joinpath("quebec_0.1_crcm5-hcd-r.hdf5").as_posix()),

        ("ERAI-CRCM5-L", filedir.joinpath("quebec_0.1_crcm5-hcd-rl.hdf5").as_posix()),

        # ("CanESM2-CRCM5-NL", filedir.joinpath("cc-canesm2-driven/quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5").as_posix()),

        ("CanESM2-CRCM5-L",
         filedir.joinpath("cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5").as_posix()),

        # ("CanESM2-CRCM5-LI", filedir.joinpath("cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5").as_posix()),


    ])

    obs_label = "Obs."
    labels = [obs_label, ] + list(sim_name_to_file_path.keys())

    label_to_marker = dict(zip(labels, all_markers))
    label_to_color = dict(zip(labels, all_colors))

    # Get the list of stations to do the comparison with
    start_date = datetime(start_year, 1, 1)
    end_date = datetime(end_year, 12, 31)
    stations = cehq_station.read_station_data(
        start_date=start_date, end_date=end_date, selected_ids=selected_ids
    )

    # Get geophysical fields from one of the model simulations
    path0 = list(sim_name_to_file_path.values())[0]
    lons2d, lats2d, basemap = analysis.get_basemap_from_hdf(file_path=path0)
    flow_directions = analysis.get_array_from_file(path=path0, var_name=infovar.HDF_FLOW_DIRECTIONS_NAME)
    lake_fraction = analysis.get_array_from_file(path=path0, var_name=infovar.HDF_LAKE_FRACTION_NAME)

    accumulation_area_km2 = analysis.get_array_from_file(path=path0, var_name=infovar.HDF_ACCUMULATION_AREA_NAME)
    area_m2 = analysis.get_array_from_file(path=path0, var_name=infovar.HDF_CELL_AREA_NAME_M2)

    # Try to read cell areas im meters if it is not Ok then try in km2
    if area_m2 is not None:
        cell_area_km2 = area_m2 * 1.0e-6
    else:
        cell_area_km2 = analysis.get_array_from_file(path=path0, var_name=infovar.HDF_CELL_AREA_NAME_KM2)

    # Create a cell manager if it is not provided
    cell_manager = CellManager(flow_directions, accumulation_area_km2=accumulation_area_km2,
                               lons2d=lons2d, lats2d=lats2d)

    # Get the list of the corresponding model points
    station_to_modelpoint = cell_manager.get_model_points_for_stations(
        station_list=stations,
        lake_fraction=lake_fraction,
        drainaige_area_reldiff_limit=0.1)

    # plot_utils.apply_plot_params(font_size=10, width_cm=20, height_cm=18)
    fig = plt.figure()

    ncols = max([len(rp_list) for et, rp_list in ExtremeProperties.extreme_type_to_return_periods.items()])
    nrows = len(ExtremeProperties.extreme_types)
    gs = GridSpec(nrows, ncols)

    ext_type_to_rp_to_ax = OrderedDict()
    ax_with_legend = None

    label_to_ax_to_xdata = {}
    label_to_ax_to_ydata = {}
    for row, ext_type in enumerate(ExtremeProperties.extreme_types):
        ext_type_to_rp_to_ax[ext_type] = OrderedDict()
        for col, rperiod in enumerate(ExtremeProperties.extreme_type_to_return_periods[ext_type]):
            ax = fig.add_subplot(gs[row, col])
            ext_type_to_rp_to_ax[ext_type][rperiod] = ax

            if col == 0:
                ax.set_ylabel(ext_type)

            if row == nrows - 1 and col == ncols - 1:
                ax_with_legend = ax

            # Set axes labels
            if row == nrows - 1:
                ax.set_xlabel("Observations")

            if col == 0:
                ax.set_ylabel("Model")

            for label in sim_name_to_file_path:

                if label not in label_to_ax_to_xdata:
                    label_to_ax_to_xdata[label] = {ax: []}
                    label_to_ax_to_ydata[label] = {ax: []}
                else:
                    label_to_ax_to_xdata[label][ax] = []
                    label_to_ax_to_ydata[label][ax] = []

            ax.set_xscale("log")
            ax.set_yscale("log")

    print("Initial list of stations:")

    sim_label_to_handle = {}
    for s in stations:
        print("{0}".format(s))
        assert isinstance(s, Station)

        print(len([y for y in s.get_list_of_complete_years() if start_year <= y <= end_year]))
        df_ext_obs = extreme_commons.get_annual_extrema(ts_times=s.dates, ts_vals=s.values,
                                                        start_year=start_year, end_year=end_year)
        mp = station_to_modelpoint[s]

        assert isinstance(mp, ModelPoint)

        years_of_interest = df_ext_obs.index

        label_to_extrema_model = {}



        # label -> ext_type -> [return period -> ret level, return period -> std]
        label_to_return_levels = OrderedDict(
            [(obs_label, OrderedDict())]
        )
        for sim_label, sim_path in sim_name_to_file_path.items():
            label_to_return_levels[sim_label] = OrderedDict()
            label_to_extrema_model[sim_label] = OrderedDict()



        # Calculate the return levels and standard deviations
        for ext_type in ExtremeProperties.extreme_types:

            return_periods = ExtremeProperties.extreme_type_to_return_periods[ext_type]

            # fit GEV distribution and apply non-parametric bootstrap to get std
            label_to_return_levels[obs_label][ext_type] = gevfit.do_gevfit_for_a_point(df_ext_obs[ext_type].values,
                                                                                       extreme_type=ext_type,
                                                                                       return_periods=return_periods)
            return_levels_obs, rl_stds_obs = label_to_return_levels[obs_label][ext_type]


            # get annual extremas for the model output at the points colose to the stations
            for sim_label, sim_path in sim_name_to_file_path.items():
                label_to_return_levels[sim_label] = OrderedDict()

                ext_field = analysis.get_annual_extrema(
                    rconfig=RunConfig(data_path=sim_path, start_year=start_year, end_year=end_year),
                    varname="STFL", months_of_interest=ExtremeProperties.extreme_type_to_month_of_interest[ext_type],
                    n_avg_days=ExtremeProperties.extreme_type_to_n_agv_days[ext_type],
                    high_flow=ext_type == ExtremeProperties.high)

                # Select only those years when obs are available
                ts_data = [v for y, v in zip(range(start_year, end_year + 1), ext_field[:, mp.ix, mp.jy]) if
                           y in years_of_interest]
                ts_data = np.array(ts_data)
                return_levels, rl_stds = gevfit.do_gevfit_for_a_point(ts_data, extreme_type=ext_type,
                                                                      return_periods=return_periods)





                # Do the plotting
                for rp in return_periods:
                    ax = ext_type_to_rp_to_ax[ext_type][rp]
                    ax.set_title("T = {rp}-year".format(rp=rp))

                    # h = ax.errorbar(return_levels_obs[rp], return_levels[rp],
                    # marker=label_to_marker[sim_label], color=label_to_color[sim_label], label=sim_label,
                    #                 xerr=rl_stds_obs[rp] * 1.96, yerr=rl_stds[rp] * 1.96)

                    h = ax.scatter(return_levels_obs[rp], return_levels[rp],
                                   marker=label_to_marker[sim_label], color=label_to_color[sim_label], label=sim_label)



                    # save the data for maybe further calculation of the correlation coefficients
                    label_to_ax_to_xdata[sim_label][ax].append(return_levels_obs[rp])
                    label_to_ax_to_ydata[sim_label][ax].append(return_levels[rp])

                    sim_label_to_handle[sim_label] = h



    # Calculate the biases
    for sim_label in sim_name_to_file_path:
        for ext_type in ExtremeProperties.extreme_types:
            ret_periods = ExtremeProperties.extreme_type_to_return_periods[ext_type]
            for rp in ret_periods:

                ax = ext_type_to_rp_to_ax[ext_type][rp]
                mod = np.asarray(label_to_ax_to_ydata[sim_label][ax])
                obs = np.asarray(label_to_ax_to_xdata[sim_label][ax])

                bias = np.mean((mod - obs)/obs)
                corr, pv = stats.pearsonr(mod, obs)
                print("({sim_label}) Mean bias for {rp}-year {ext_type}-flow return level is: {bias}; corr={corr:.2f}; corr_pval={corr_pval:2g}".format(
                    sim_label=sim_label, rp=rp, bias=bias, corr=corr, corr_pval=pv,
                    ext_type=ext_type
                ))




    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))
    for et, rp_to_ax in ext_type_to_rp_to_ax.items():
        for rp, ax in rp_to_ax.items():
            xmin, xmax = ax.get_xlim()
            ymin, ymax = ax.get_ylim()
            x1 = min(xmin, ymin)
            x2 = min(xmax, ymax)
            ax.plot([x1, x2], [x1, x2], "k--")
            # ax.xaxis.set_major_locator(MaxNLocator(nbins=5))
            # ax.yaxis.set_major_locator(MaxNLocator(nbins=5))
            # ax.xaxis.set_major_formatter(sfmt)
            # ax.yaxis.set_major_formatter(sfmt)

    sim_labels = list(sim_name_to_file_path.keys())
    ax_with_legend.legend([sim_label_to_handle[sl] for sl in sim_labels], sim_labels,
                          bbox_to_anchor=(1, -0.25), borderaxespad=0.0, loc="upper right",
                          ncol=2, scatterpoints=1, numpoints=1)

    # Save the plot
    img_file = "{}.eps".format("_".join(sorted(label_to_marker.keys())))
    img_file = img_folder.joinpath(img_file)

    fig.tight_layout()
    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight")
Example #9
0
def main():

    start_year = 1980
    end_year = 2003

    months_of_obs = [12, 1, 2, 3, 4, 5]

    r_config = RunConfig(
        data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        start_year=start_year, end_year=end_year, label="ERAI-CRCM5-L"
    )

    var_name = "LC"
    bmp_info = analysis.get_basemap_info(r_config=r_config)
    lkid_to_mask = get_lake_masks(bmp_info.lons, bmp_info.lats)


    cell_area_m2 = analysis.get_array_from_file(path=r_config.data_path, var_name="cell_area_m2")


    # read the model data
    lkid_to_ts_model = {}
    for lkid, the_mask in lkid_to_mask.items():
        lkid_to_ts_model[lkid] = analysis.get_area_mean_timeseries(r_config.data_path, var_name=var_name, the_mask=the_mask * cell_area_m2,
                                                                   start_year=start_year, end_year=end_year)

        df = lkid_to_ts_model[lkid]

        # remove the last December
        df = df.select(lambda d: not (d.year == end_year and d.month == 12))

        # remove the first Jan and Feb
        df = df.select(lambda d: not (d.year == start_year and d.month in [1, 2]))

        # remove the Feb 29th
        df = df.select(lambda d: not (d.month == 2 and d.day == 29))

        # select months of interest
        df = df.select(lambda d: d.month in months_of_obs)

        # calculate the climatology
        df = df.groupby(lambda d: datetime(2001 if d.month == 12 else 2002, d.month, d.day)).mean()
        df.sort_index(inplace=True)


        lkid_to_ts_model[lkid] = df * 100


    # read obs data and calculate climatology
    lkid_to_ts_obs = {}
    for lkid in LAKE_IDS:
        lkid_to_ts_obs[lkid] = GL_obs_timeseries.get_ts_from_file(path=os.path.join(OBS_DATA_FOLDER, "{}-30x.TXT".format(lkid)),
                                                                  start_year=start_year, end_year=end_year - 1)

        # get the climatology
        dfm = lkid_to_ts_obs[lkid].mean(axis=1)

        dfm.index = [datetime(2001, 1, 1) + timedelta(days=int(jd - 1)) for jd in dfm.index]

        lkid_to_ts_obs[lkid] = dfm


    # plotting
    plot_utils.apply_plot_params(font_size=10)
    fig = plt.figure()
    gs = GridSpec(nrows=len(lkid_to_ts_model), ncols=2)

    for row, lkid in enumerate(lkid_to_ts_model):

        ax = fig.add_subplot(gs[row, 0])

        mod = lkid_to_ts_model[lkid]
        obs = lkid_to_ts_obs[lkid]

        print(obs.index)
        print(obs.values)

        ax.plot(mod.index, mod.values, label=r_config.label, color="r", lw=2)
        ax.plot(obs.index, obs.values, label="NOAA NIC/CIS", color="k", lw=2)

        if row == 0:
            ax.legend()

        ax.set_title(lkid)

        ax.xaxis.set_major_formatter(DateFormatter("%b"))


    fig.tight_layout()
    fig.savefig(os.path.join(img_folder, "GL_ice-cover-validation.png"), bbox_inches="tight", dpi=common_plot_params.FIG_SAVE_DPI)
def plot_hydrographs():
    plot_utils.apply_plot_params(font_size=14,
                                 width_pt=None,
                                 width_cm=20,
                                 height_cm=20)
    start_year = 1980
    end_year = 2010

    varname = "STFL"

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

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

    basin_name_to_out_indices_map, basin_name_to_basin_mask = get_basin_to_outlet_indices_map(
        lons=lons, lats=lats, accumulation_areas=facc, directions=fldr)
    # Calculate the daily mean fields
    dates, stf_base = analysis.get_daily_climatology_for_rconf(
        base_config, var_name=varname, level=0)
    _, stf_modif = analysis.get_daily_climatology_for_rconf(modif_config,
                                                            var_name=varname,
                                                            level=0)

    for bname, (i_out, j_out) in basin_name_to_out_indices_map.items():
        print(bname, i_out, j_out)
        fig = plt.figure()

        gs = GridSpec(2, 1, height_ratios=[1, 0.5], hspace=0.1)

        ax = fig.add_subplot(gs[0, 0])
        ax.plot(dates,
                stf_base[:, i_out, j_out],
                "b",
                lw=2,
                label=base_config.label)
        ax.plot(dates,
                stf_modif[:, i_out, j_out],
                "r",
                lw=2,
                label=modif_config.label)
        ax.set_title(bname)
        format_axis(ax)

        # Hide the tick labels from the x-axis of the upper plot
        for tl in ax.xaxis.get_ticklabels():
            tl.set_visible(False)

        ax = fig.add_subplot(gs[1, 0])
        ax.plot(dates,
                stf_modif[:, i_out, j_out] - stf_base[:, i_out, j_out],
                "k",
                lw=2,
                label="{}-{}".format(modif_config.label, base_config.label))
        format_axis(ax)

        fig.savefig(
            str(
                IMG_FOLDER.joinpath("{}_{}-{}.png".format(
                    bname, start_year, end_year))))
        plt.close(fig)
def main():
    start_year_c = 1980
    end_year_c = 2010

    img_folder = "cc_paper"

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

    # Need to read land fraction
    geo_file_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    r_obj = RPN(geo_file_path)
    mg_field = r_obj.get_first_record_for_name("MG")

    lons, lats = r_obj.get_longitudes_and_latitudes_for_the_last_read_rec()
    r_obj.close()

    future_shift_years = 75

    params = dict(data_path=current_path,
                  start_year=start_year_c,
                  end_year=end_year_c,
                  label=base_label)

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    data_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5"
    params.update(dict(data_path=data_path, label="CRCM5-LI"))

    modif_config_c = RunConfig(**params)
    modif_config_f = modif_config_c.get_shifted_config(future_shift_years)

    varnames = ["I1+I2", "I0", "PR", "TRAF", "AV", "I0_max", "I0_min"]
    levels = [0, 0, 0, 0, 0, 0, 0]
    var_labels = ["SM", "ST", "PR", "TRAF", "LHF", "STmin", "STmax"]

    # width of the first soil layer in mm
    multipliers = [100, 1, 1000 * 24 * 3600, 24 * 3600, 1, 1, 1]
    offsets = [
        0,
    ] * len(varnames)
    units = ["mm", "K", "mm/day", "mm/day", r"${\rm W/m^2}$", "K", "K"]

    SimData = collections.namedtuple("SimData",
                                     "base_c base_f modif_c modif_f")

    mg_fields = None

    fig = plt.figure()
    gs = GridSpec(len(varnames), 5, width_ratios=2 * [
        1,
    ] + [0.05, 1, 0.05])

    lats_agg = lats.mean(axis=0)

    diff_cmap = cm.get_cmap("RdBu_r", 10)
    the_zip = zip(varnames, levels, var_labels, multipliers, offsets, units)
    row = 0
    for vname, level, var_label, multiplier, offset, unit_label in the_zip:

        daily_dates, base_data_c = analysis.get_daily_climatology(
            path_to_hdf_file=base_config_c.data_path,
            var_name=vname,
            level=level,
            start_year=base_config_c.start_year,
            end_year=base_config_c.end_year)

        _, base_data_f = analysis.get_daily_climatology(
            path_to_hdf_file=base_config_f.data_path,
            var_name=vname,
            level=level,
            start_year=base_config_f.start_year,
            end_year=base_config_f.end_year)

        _, modif_data_c = analysis.get_daily_climatology(
            path_to_hdf_file=modif_config_c.data_path,
            var_name=vname,
            level=level,
            start_year=modif_config_c.start_year,
            end_year=modif_config_c.end_year)

        _, modif_data_f = analysis.get_daily_climatology(
            path_to_hdf_file=modif_config_f.data_path,
            var_name=vname,
            level=level,
            start_year=modif_config_f.start_year,
            end_year=modif_config_f.end_year)

        if mg_fields is None:
            mg_fields = np.asarray([mg_field for d in daily_dates])
            num_dates = date2num(daily_dates)

            # create 2d dates and latitudes for the contour plots
            lats_agg_2d, num_dates_2d = np.meshgrid(lats_agg, num_dates)

        sim_data = SimData(_avg_along_lon(base_data_c, mg_fields),
                           _avg_along_lon(base_data_f, mg_fields),
                           _avg_along_lon(modif_data_c, mg_fields),
                           _avg_along_lon(modif_data_f, mg_fields))

        # Unit conversion
        sim_data = SimData(*[multiplier * si + offset for si in sim_data])

        # Plot the row for the variable
        all_axes = []

        # Calculate nice color levels
        delta = np.percentile(
            np.abs([
                sim_data.modif_c - sim_data.base_c,
                sim_data.modif_f - sim_data.base_f
            ]), 99)
        vmin = -delta
        vmax = delta
        locator = MaxNLocator(nbins=10, symmetric=True)
        clevs = locator.tick_values(vmin=vmin, vmax=vmax)

        # Current
        ax = fig.add_subplot(gs[row, 0])
        cs = ax.contourf(num_dates_2d,
                         lats_agg_2d,
                         sim_data.modif_c - sim_data.base_c,
                         extend="both",
                         levels=clevs,
                         cmap=diff_cmap)
        if row == 0:
            ax.set_title("Current ({}-{})".format(base_config_c.start_year,
                                                  base_config_c.end_year))
        all_axes.append(ax)

        # Future
        ax = fig.add_subplot(gs[row, 1])
        if row == 0:
            ax.set_title("Future ({}-{})".format(base_config_f.start_year,
                                                 base_config_f.end_year))

        cs = ax.contourf(num_dates_2d,
                         lats_agg_2d,
                         sim_data.modif_f - sim_data.base_f,
                         levels=cs.levels,
                         extend="both",
                         cmap=diff_cmap)
        all_axes.append(ax)

        # Colorbar for value plots
        cax = fig.add_subplot(gs[row, 2])
        plt.colorbar(cs, cax=cax)
        cax.set_title("{} ({})\n".format(var_label, unit_label))

        diff = (sim_data.modif_f - sim_data.base_f) - (sim_data.modif_c -
                                                       sim_data.base_c)
        delta = np.percentile(np.abs(diff), 99)
        vmin = -delta
        vmax = delta
        locator = MaxNLocator(nbins=10, symmetric=True)
        clevs = locator.tick_values(vmin=vmin, vmax=vmax)

        ax = fig.add_subplot(gs[row, 3])
        cs = ax.contourf(num_dates_2d,
                         lats_agg_2d,
                         diff,
                         cmap=diff_cmap,
                         levels=clevs,
                         extend="both")
        all_axes.append(ax)
        cb = plt.colorbar(cs, cax=fig.add_subplot(gs[row, 4]))

        if row == 0:
            ax.set_title("Future - Current")

        cb.ax.set_title("{}\n".format(unit_label))

        for i, the_ax in enumerate(all_axes):
            the_ax.xaxis.set_major_formatter(DateFormatter("%b"))
            the_ax.xaxis.set_major_locator(MonthLocator(interval=2))

            the_ax.grid()
            if i == 0:
                the_ax.set_ylabel("Latitude")

        row += 1

    fig.tight_layout()
    img_path = Path(img_folder).joinpath(
        "{}_long_avg_intf_impact_{}-{}_vs_{}-{}.png".format(
            "_".join(varnames), base_config_f.start_year,
            base_config_f.end_year, base_config_c.start_year,
            base_config_c.end_year))
    fig.savefig(str(img_path), bbox_inches="tight")
Example #12
0
def main():
    start_year_c = 1980
    end_year_c = 2010

    img_folder = "cc_paper"

    current_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5"
    base_label = "CRCM5-LI"

    # Need to read land fraction
    geo_file_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    r_obj = RPN(geo_file_path)
    mg_field = r_obj.get_first_record_for_name("MG")

    lons, lats = r_obj.get_longitudes_and_latitudes_for_the_last_read_rec()
    r_obj.close()

    future_shift_years = 90

    params = dict(data_path=current_path,
                  start_year=start_year_c,
                  end_year=end_year_c,
                  label=base_label)

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    varname = "INTF"
    level = 0
    daily_dates, intf_c = analysis.get_daily_climatology(
        path_to_hdf_file=base_config_c.data_path,
        var_name=varname,
        level=level,
        start_year=base_config_c.start_year,
        end_year=base_config_c.end_year)

    _, intf_f = analysis.get_daily_climatology(
        path_to_hdf_file=base_config_f.data_path,
        var_name=varname,
        level=level,
        start_year=base_config_f.start_year,
        end_year=base_config_f.end_year)

    mg_fields = np.asarray([mg_field for d in daily_dates])

    mg_crit = 0.0001
    the_mask = mg_fields <= mg_crit
    # Convert to mm/day as well
    intf_c = _avg_along_lon(intf_c, the_mask) * 24 * 3600
    intf_f = _avg_along_lon(intf_f, the_mask) * 24 * 3600

    lats_agg = lats.mean(axis=0)
    num_dates = date2num(daily_dates)

    lats_agg_2d, num_dates_2d = np.meshgrid(lats_agg, num_dates)

    # Do the plotting
    fig = plt.figure()

    gs = GridSpec(2, 3, width_ratios=[1, 1, 0.05])
    norm = SymLogNorm(5e-5)

    all_axes = []
    # Current
    ax = fig.add_subplot(gs[0, 0])
    cs = ax.contourf(num_dates_2d, lats_agg_2d, intf_c[:], 60, norm=norm)
    ax.set_title("Current ({}-{})".format(base_config_c.start_year,
                                          base_config_c.end_year))
    all_axes.append(ax)

    # Future
    ax = fig.add_subplot(gs[0, 1])
    ax.set_title("Future ({}-{})".format(base_config_f.start_year,
                                         base_config_f.end_year))
    cs = ax.contourf(num_dates_2d,
                     lats_agg_2d,
                     intf_f[:],
                     levels=cs.levels,
                     norm=norm)
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[0, 2])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-1, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("mm/day")
    cax.yaxis.get_offset_text().set_position((-2, 10))

    # CC
    diff_cmap = cm.get_cmap("RdBu_r", 20)
    diff = (intf_f - intf_c) / (0.5 * (intf_c + intf_f)) * 100
    diff[(intf_f == 0) & (intf_c == 0)] = 0
    print(np.min(diff), np.max(diff))
    print(np.any(diff.mask))
    print(np.any(intf_c.mask))
    print(np.any(intf_f.mask))
    delta = 200
    vmin = -delta
    vmax = delta
    locator = MaxNLocator(nbins=20, symmetric=True)
    clevs = locator.tick_values(vmin=vmin, vmax=vmax)

    ax = fig.add_subplot(gs[1, 1])

    cs = ax.contourf(num_dates_2d,
                     lats_agg_2d,
                     diff,
                     cmap=diff_cmap,
                     levels=clevs,
                     extend="both")
    ax.set_title("Future - Current")
    # ax.set_aspect("auto")
    all_axes.append(ax)
    cb = plt.colorbar(cs, cax=fig.add_subplot(gs[1, -1]))
    cb.ax.set_xlabel(r"%")

    for i, the_ax in enumerate(all_axes):
        the_ax.xaxis.set_minor_formatter(
            FuncFormatter(lambda d, pos: num2date(d).strftime("%b")[0]))
        the_ax.xaxis.set_major_formatter(FuncFormatter(lambda d, pos: ""))
        the_ax.xaxis.set_minor_locator(MonthLocator(bymonthday=15))
        the_ax.xaxis.set_major_locator(MonthLocator())
        the_ax.grid()
        if i != 1:
            the_ax.set_ylabel(r"Latitude ${\rm \left(^\circ N \right)}$")

    # identify approximately the melting period and lower latitudes
    march1 = date2num(datetime(daily_dates[0].year, 3, 1))
    june1 = date2num(datetime(daily_dates[0].year, 6, 1))

    sel_mask = (num_dates_2d >= march1) & (num_dates_2d < june1) & (lats_agg_2d
                                                                    <= 50)
    print("Mean interflow decrease in the southern regions: {}%".format(
        diff[sel_mask].mean()))

    # identify the regions of max interflow rates in current and future climates
    lat_min = 55
    lat_max = 57.5

    may1 = date2num(datetime(daily_dates[0].year, 5, 1))
    july1 = date2num(datetime(daily_dates[0].year, 7, 1))

    mean_max_current = intf_c[(lats_agg_2d >= lat_min)
                              & (lats_agg_2d <= lat_max) &
                              (num_dates_2d <= july1) &
                              (num_dates_2d >= june1)].mean()
    mean_max_future = intf_f[(lats_agg_2d >= lat_min)
                             & (lats_agg_2d <= lat_max) &
                             (num_dates_2d <= june1) &
                             (num_dates_2d >= may1)].mean()

    print("Mean change in the maximum interflow rate: {} %".format(
        (mean_max_future - mean_max_current) * 100 / mean_max_current))

    img_file = Path(img_folder).joinpath("INTF_rate_longit_avg.png")
    fig.tight_layout()
    from crcm5.analyse_hdf import common_plot_params
    fig.savefig(str(img_file),
                bbox_inches="tight",
                transparent=True,
                dpi=common_plot_params.FIG_SAVE_DPI)
Example #13
0
def main():
    season_to_months = get_default_season_to_months_dict()

    # var_names = ["TT", "HU", "PR", "AV", "STFL"]
    # var_names = ["TRAF", "STFL", "TRAF+TDRA"]
    # var_names = ["TT", "PR", "STFL"]
    # var_names = ["TT", "PR", "I5", "STFL", "AV", "AH"]

    var_names = ["TT", ]

    levels = [0, ] * len(var_names)
    multipliers = {
        "PR": 1.0,
        "TRAF+TDRA": 24 * 60 * 60
    }
    name_to_units = {
        "TRAF": "mm/day", "I1": "mm", "PR": "mm/day", "TRAF+TDRA": "mm/day",
        "I5": "mm", "AV": r"${\rm W/m^2}$"
    }

    base_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
                        "quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    base_label = "CanESM2-CRCM5-NL"

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

    plot_cc_only_for = modif_label

    # plot_cc_only_for = None

    start_year_c = 1980
    end_year_c = 2010

    future_shift_years = 90

    params = dict(
        data_path=base_current_path, start_year=start_year_c, end_year=end_year_c, label=base_label
    )

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    params.update(
        dict(data_path=modif_current_path, label=modif_label)
    )

    modif_config_c = RunConfig(**params)
    modif_config_f = modif_config_c.get_shifted_config(future_shift_years)

    config_dict = OrderedDict([
        ("Current", OrderedDict([(base_label, base_config_c), (modif_label, modif_config_c)])),
        ("Future", OrderedDict([(base_label, base_config_f), (modif_label, modif_config_f)]))
    ])

    # Changes global plot properties mainly figure size and font size
    plot_utils.apply_plot_params(font_size=12, width_cm=25, height_cm=10)


    # Plot the differences
    fig = plt.figure()
    gs = GridSpec(len(var_names), len(season_to_months) + 1, width_ratios=[1., ] * len(season_to_months) + [0.05, ])

    config_dict.fig = fig
    config_dict.gs = gs
    config_dict.label_modif = modif_config_c.label
    config_dict.label_base = base_config_c.label
    config_dict.season_to_months = season_to_months
    config_dict.multipliers = multipliers

    lons, lats, bmp = analysis.get_basemap_from_hdf(base_current_path)
    config_dict.lons = lons
    config_dict.lats = lats
    config_dict.basemap = bmp

    config_dict.name_to_units = name_to_units

    # Calculate and plot seasonal means
    for vname, level, the_row in zip(var_names, levels, list(range(len(levels)))):
        config_dict.the_row = the_row

        _plot_row(vname=vname, level=level, config_dict=config_dict, plot_cc_only_for=plot_cc_only_for, mark_significance=False)

    # Save the image to the file
    if plot_cc_only_for is None:
        img_path = get_image_path(base_config_c, base_config_f, modif_config_c, season_to_months=season_to_months)
    else:

        config_c = base_config_c if base_config_c.label == plot_cc_only_for else modif_config_c
        config_f = base_config_f if base_config_f.label == plot_cc_only_for else modif_config_f

        img_path = get_image_path(config_c, config_f, config_c, season_to_months=season_to_months)
    fig.savefig(img_path, bbox_inches="tight", transparent=True, dpi=common_plot_params.FIG_SAVE_DPI)
    print("saving the plot to: {}".format(img_path))
    plt.close(fig)
Example #14
0
def main():
    # import seaborn as sns
    # sns.set_context("paper", font_scale=2)
    # sns.set_style("whitegrid")

    level_widths_mm = MM_PER_METER * infovar.soil_layer_widths_26_to_60

    avg_axis = "lon"
    start_year_c = 1990
    end_year_c = 2010

    img_folder = Path("impact_of_interflow")
    if not img_folder.exists():
        img_folder.mkdir(parents=True)

    # Configuration without interflow, to be compared with the one with intf.
    base_config = RunConfig(
        start_year=start_year_c,
        end_year=end_year_c,
        data_path=
        "/home/huziy/skynet3_rech1/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        label="NI")

    current_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl-intfl_ITFS.hdf5"

    # Need to read land fraction
    geo_file_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    r_obj = RPN(geo_file_path)
    mg_field = r_obj.get_first_record_for_name("MG")
    depth_to_bedrock_mm = r_obj.get_first_record_for_name("8L") * MM_PER_METER

    # recompute layer widths to account for the depth to bedrock
    layer_widths_3d = np.ones(depth_to_bedrock_mm.shape +
                              level_widths_mm.shape)
    layer_widths_3d *= level_widths_mm[np.newaxis, np.newaxis, :]

    layer_bottoms_3d = layer_widths_3d.cumsum(axis=2)

    corrections = depth_to_bedrock_mm[:, :, np.newaxis] - layer_bottoms_3d
    layer_widths_3d[corrections < 0] += corrections[corrections < 0]
    layer_widths_3d[layer_widths_3d < 0] = 0

    lons, lats = r_obj.get_longitudes_and_latitudes_for_the_last_read_rec()
    r_obj.close()

    modif_config = RunConfig(start_year=start_year_c,
                             end_year=end_year_c,
                             data_path=current_path,
                             label="WI")

    varname = "INTF"
    level = 0
    daily_dates, intf_c = analysis.get_daily_climatology_for_rconf(
        modif_config, var_name=varname, level=level)

    # Convert to mm/day as well
    intf_c = _avg_along(intf_c, axis=avg_axis,
                        land_fraction=mg_field) * 24 * 3600

    zagg = None
    ztitle = ""
    if avg_axis == "lon":
        zagg = lats.mean(axis=0)
        ztitle = "Latitude"
    elif avg_axis == "lat":
        zagg = lons.mean(axis=1)
        ztitle = "Longitude"

    num_dates = date2num(daily_dates)

    z_agg_2d, num_dates_2d = np.meshgrid(zagg, num_dates)

    # Do the plotting
    plot_utils.apply_plot_params(font_size=14,
                                 width_pt=None,
                                 width_cm=20,
                                 height_cm=10)
    fig = plt.figure()

    gs = GridSpec(1, 2, width_ratios=[1, 0.05])

    all_axes = []
    # ----------------------------------Interflow----------------------------------
    row = 0
    ax = fig.add_subplot(gs[row, 0])
    cs = ax.contourf(num_dates_2d,
                     z_agg_2d,
                     intf_c[:],
                     60,
                     cmap="jet",
                     norm=SymLogNorm(5 * 1.0e-7))
    ax.set_title("Interflow rate")
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("mm/day")
    cax.yaxis.get_offset_text().set_position((-0.5, 10))

    # ----------------------------------Soil moisture----------------------------------
    # layer_index = 0
    # _, sm_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="I1", level=layer_index)
    # _, sm_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="I1", level=layer_index)
    #
    # sm_mod = _avg_along(sm_mod, axis=avg_axis, land_fraction=mg_field, layer_depths=layer_widths_3d[:, :, layer_index])
    # sm_base = _avg_along(sm_base, axis=avg_axis, land_fraction=mg_field, layer_depths=layer_widths_3d[:, :, layer_index])
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, sm_mod - sm_base, cmap="jet")
    # ax.set_title("Soil moisture (liq., level={})".format(layer_index + 1))
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("mm")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Soil moisture (level=2)----------------------------------
    # layer_index = 1
    # _, sm_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="I1", level=layer_index)
    # _, sm_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="I1", level=layer_index)
    #
    # sm_mod = _avg_along(sm_mod, axis=avg_axis, land_fraction=mg_field, layer_depths=layer_widths_3d[:, :, layer_index])
    # sm_base = _avg_along(sm_base, axis=avg_axis, land_fraction=mg_field, layer_depths=layer_widths_3d[:, :, layer_index])
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, sm_mod - sm_base, cmap="jet")
    # ax.set_title("Soil moisture (liq., level={})".format(layer_index + 1))
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("mm")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Soil moisture (level=3)----------------------------------
    # layer_index = 2
    # _, sm_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="I1", level=layer_index)
    # _, sm_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="I1", level=layer_index)
    #
    # sm_mod = _avg_along(sm_mod, axis=avg_axis, land_fraction=mg_field, layer_depths=layer_widths_3d[:, :, layer_index])
    # sm_base = _avg_along(sm_base, axis=avg_axis, land_fraction=mg_field, layer_depths=layer_widths_3d[:, :, layer_index])
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, sm_mod - sm_base, cmap="jet")
    # ax.set_title("Soil moisture (liq., level={})".format(layer_index + 1))
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("mm")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Evaporation----------------------------------
    # _, av_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="AV", level=0)
    # _, av_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="AV", level=0)
    #
    # av_mod = _avg_along(av_mod, land_fraction=mg_field, axis=avg_axis)
    # av_base = _avg_along(av_base, land_fraction=mg_field, axis=avg_axis)
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, av_mod - av_base, cmap="jet")
    # ax.set_title("Evaporation (level=1)")
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("W/m**2")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Drainage----------------------------------
    # _, dr_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="TDRA", level=0)
    # _, dr_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="TDRA", level=0)
    #
    # dr_mod = _avg_along(dr_mod, the_mask, axis=avg_axis)
    # dr_base = _avg_along(dr_base, the_mask, axis=avg_axis)
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, (dr_mod - dr_base) * units.SECONDS_PER_DAY, cmap="jet")
    # ax.set_title("Drainage (level=1)")
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("mm/day")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Soil temperature (level_index=0)----------------------------------
    # layer_index = 0
    # _, t_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="I0", level=layer_index)
    # _, t_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="I0", level=layer_index)
    #
    # t_mod = _avg_along(t_mod, land_fraction=mg_field, axis=avg_axis)
    # t_base = _avg_along(t_base, land_fraction=mg_field, axis=avg_axis)
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, t_mod - t_base, cmap="jet")
    # ax.set_title("Soil temperature (level={})".format(layer_index + 1))
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("K")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Soil moisture total (I1 + I2, level_index=0)----------------------------------
    # _, sm_mod = analysis.get_daily_climatology_for_rconf(modif_config, var_name="I1+I2", level=0)
    # _, sm_base = analysis.get_daily_climatology_for_rconf(base_config, var_name="I1+I2", level=0)
    #
    # sm_mod = _avg_along(sm_mod, the_mask, axis=avg_axis)
    # sm_base = _avg_along(sm_base, the_mask, axis=avg_axis)
    #
    # row += 1
    # ax = fig.add_subplot(gs[row, 0])
    # cs = ax.contourf(num_dates_2d, z_agg_2d, (sm_mod - sm_base) * level_widths_mm[0], cmap="jet")
    # ax.set_title("Soil moisture (liq.+ice, level=1)")
    # all_axes.append(ax)
    #
    # # Colorbar for value plots
    # cax = fig.add_subplot(gs[row, -1])
    #
    # sfmt = ScalarFormatter(useMathText=True)
    # sfmt.set_powerlimits((-2, 2))
    #
    # plt.colorbar(cs, cax=cax, format=sfmt)
    # cax.set_xlabel("mm")
    # cax.yaxis.get_offset_text().set_position((-2, 10))

    delta = 200
    vmin = -delta
    vmax = delta

    for i, the_ax in enumerate(all_axes):
        the_ax.xaxis.set_major_formatter(
            FuncFormatter(lambda d, pos: num2date(d).strftime("%b")[0]))
        the_ax.xaxis.set_major_locator(MonthLocator(bymonthday=15))
        the_ax.xaxis.set_minor_locator(MonthLocator(bymonthday=1))
        the_ax.grid(which="minor")
        the_ax.set_ylabel(ztitle)

    img_file = Path(img_folder).joinpath("INTF_rate_{}_avg_{}-{}.png".format(
        avg_axis, start_year_c, end_year_c))
    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")

    plt.show()
Example #15
0
def main():
    """
    Everything is aggregated to the CRU resolution before calculating biases
    """
    season_to_months = DEFAULT_SEASON_TO_MONTHS
    varnames = ["PR", "TT"]

    plot_utils.apply_plot_params(font_size=5,
                                 width_pt=None,
                                 width_cm=15,
                                 height_cm=4)

    # reanalysis_driven_config = RunConfig(data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
    #                                      start_year=1980, end_year=2010, label="ERAI-CRCM5-L")

    reanalysis_driven_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.4_crcm5-hcd-rl.hdf5",
        start_year=1980,
        end_year=2010,
        label="ERAI-CRCM5-RL_0.4deg")

    bmp_info = analysis.get_basemap_info(r_config=reanalysis_driven_config)

    field_cmap = cm.get_cmap("jet", 10)

    vname_to_clevels = {
        "TT": np.arange(-30, 32, 2),
        "PR": np.arange(0, 6.5, 0.5)
    }

    vname_to_anusplin_path = {
        "TT": "/home/huziy/skynet3_rech1/anusplin_links",
        "PR": "/home/huziy/skynet3_rech1/anusplin_links"
    }

    vname_to_cru_path = {
        "TT":
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "PR":
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    }

    xx_agg = None
    yy_agg = None

    for vname in varnames:

        # get anusplin obs climatology
        season_to_obs_anusplin = plot_performance_err_with_anusplin.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config,
            vname=vname,
            season_to_months=season_to_months,
            bmp_info=bmp_info)

        # get CRU obs values-------------------------
        bmp_info_agg, season_to_obs_cru = plot_performance_err_with_cru.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config,
            bmp_info=bmp_info,
            season_to_months=season_to_months,
            obs_path=vname_to_cru_path[vname],
            vname=vname)

        if xx_agg is None:
            xx_agg, yy_agg = bmp_info_agg.get_proj_xy()

        # get model data
        seasonal_clim_fields_model = analysis.get_seasonal_climatology_for_runconfig(
            run_config=reanalysis_driven_config,
            varname=vname,
            level=0,
            season_to_months=season_to_months)

        ###
        biases_with_anusplin = OrderedDict()
        biases_with_cru = OrderedDict()

        nx_agg_anusplin = 4
        ny_agg_anusplin = 4

        nx_agg_model = 1
        ny_agg_model = 1

        season_to_clim_fields_model_agg = OrderedDict()
        for season, field in seasonal_clim_fields_model.items():
            print(field.shape)
            season_to_clim_fields_model_agg[season] = aggregate_array(
                field, nagg_x=nx_agg_model, nagg_y=ny_agg_model)

            if vname == "PR":
                season_to_clim_fields_model_agg[season] *= 1.0e3 * 24 * 3600

            biases_with_cru[season] = season_to_clim_fields_model_agg[
                season] - season_to_obs_cru[season]

            biases_with_anusplin[season] = season_to_clim_fields_model_agg[
                season] - aggregate_array(season_to_obs_anusplin[season],
                                          nagg_x=nx_agg_anusplin,
                                          nagg_y=ny_agg_anusplin)

        # Do the plotting
        fig = plt.figure()
        clevs = [c for c in np.arange(-0.5, 0.55, 0.05)
                 ] if vname == "PR" else np.arange(-2, 2.2, 0.2)

        gs = GridSpec(1,
                      len(biases_with_cru) + 1,
                      width_ratios=len(biases_with_cru) * [
                          1.,
                      ] + [
                          0.05,
                      ])

        col = 0
        cs = None
        cmap = "seismic"

        fig.suptitle(
            r"$\left| \delta_{\rm Hopkinson} \right| - \left| \delta_{\rm CRU} \right|$"
        )

        for season, cru_err in biases_with_cru.items():
            anu_err = biases_with_anusplin[season]

            ax = fig.add_subplot(gs[0, col])

            diff = np.abs(anu_err) - np.abs(cru_err)
            cs = bmp_info_agg.basemap.contourf(xx_agg,
                                               yy_agg,
                                               diff,
                                               levels=clevs,
                                               ax=ax,
                                               extend="both",
                                               cmap=cmap)
            bmp_info_agg.basemap.drawcoastlines(ax=ax, linewidth=0.3)

            good = diff[~diff.mask & ~np.isnan(diff)]
            n_neg = sum(good < 0) / sum(good > 0)

            print("season: {}, n-/n+ = {}".format(season, n_neg))

            ax.set_title(season)
            ax.set_xlabel(r"$n_{-}/n_{+} = $" + "{:.1f}".format(n_neg) + "\n" +
                          r"$\overline{\varepsilon} = $" +
                          "{:.2f}".format(good.mean()))

            col += 1

        ax = fig.add_subplot(gs[0, -1])
        plt.colorbar(cs, cax=ax)
        ax.set_title("mm/day" if vname == "PR" else r"${\rm ^\circ C}$")

        fig.savefig(os.path.join(
            img_folder, "comp_anu_and_cru_biases_for_{}_{}.png".format(
                vname, reanalysis_driven_config.label)),
                    bbox_inches="tight",
                    dpi=common_plot_params.FIG_SAVE_DPI)
def main():
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    season_to_months = OrderedDict([
        ("Winter (DJF)", (1, 2, 12)),
        ("Spring (MAM)", range(3, 6)),
        ("Summer (JJA)", range(6, 9)),
        ("Fall (SON)", range(9, 12)),
    ])

    varnames = ["TT", "PR"]

    plot_utils.apply_plot_params(font_size=10,
                                 width_pt=None,
                                 width_cm=20,
                                 height_cm=17)

    # reanalysis_driven_config = RunConfig(data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
    #                                      start_year=1980, end_year=2010, label="ERAI-CRCM5-L")
    #

    reanalysis_driven_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.4_crcm5-hcd-rl.hdf5",
        start_year=1980,
        end_year=2010,
        label="ERAI-CRCM5-L(0.4)")

    nx_agg_model = 1
    ny_agg_model = 1

    nx_agg_anusplin = 4
    ny_agg_anusplin = 4

    gcm_driven_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5",
        start_year=1980,
        end_year=2010,
        label="CanESM2-CRCM5-L")

    bmp_info = analysis.get_basemap_info(r_config=reanalysis_driven_config)
    xx, yy = bmp_info.get_proj_xy()

    field_cmap = cm.get_cmap("jet", 10)

    vname_to_clevels = {
        "TT": np.arange(-30, 32, 2),
        "PR": np.arange(0, 6.5, 0.5)
    }

    vname_to_anusplin_path = {
        "TT": "/home/huziy/skynet3_rech1/anusplin_links",
        "PR": "/home/huziy/skynet3_rech1/anusplin_links"
    }

    vname_to_cru_path = {
        "TT":
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "PR":
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    }

    for vname in varnames:
        fig = plt.figure()
        ncols = len(season_to_months)
        gs = GridSpec(4, ncols + 1, width_ratios=ncols * [
            1.,
        ] + [
            0.09,
        ])

        clevels = vname_to_clevels[vname]

        # get anusplin obs climatology
        season_to_obs_anusplin = plot_performance_err_with_anusplin.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config,
            vname=vname,
            season_to_months=season_to_months,
            bmp_info=bmp_info,
            n_agg_x=nx_agg_anusplin,
            n_agg_y=ny_agg_anusplin)

        row = 0

        # Plot CRU values-------------------------
        bmp_info_agg, season_to_obs_cru = plot_performance_err_with_cru.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config,
            bmp_info=bmp_info,
            season_to_months=season_to_months,
            obs_path=vname_to_cru_path[vname],
            vname=vname)

        # Mask out the Great Lakes
        cru_mask = get_mask(bmp_info_agg.lons,
                            bmp_info_agg.lats,
                            shp_path=os.path.join(GL_SHP_FOLDER, "gl_cst.shp"))
        for season in season_to_obs_cru:
            season_to_obs_cru[season] = np.ma.masked_where(
                cru_mask > 0.5, season_to_obs_cru[season])

        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        cs = None
        xx_agg, yy_agg = bmp_info_agg.get_proj_xy()
        for j, (season, obs_field) in enumerate(season_to_obs_cru.items()):
            ax = ax_list[j]
            cs = bmp_info_agg.basemap.contourf(xx_agg,
                                               yy_agg,
                                               obs_field.copy(),
                                               levels=clevels,
                                               ax=ax)
            bmp_info.basemap.drawcoastlines(ax=ax)
            bmp_info.basemap.readshapefile(BASIN_BOUNDARIES_SHP[:-4],
                                           "basin",
                                           ax=ax)
            ax.set_title(season)

        ax_list[0].set_ylabel("CRU")
        # plt.colorbar(cs, caax=ax_list[-1])
        row += 1

        # Plot ANUSPLIN values-------------------------
        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        cs = None
        for j, (season,
                obs_field) in enumerate(season_to_obs_anusplin.items()):
            ax = ax_list[j]
            cs = bmp_info.basemap.contourf(xx,
                                           yy,
                                           obs_field,
                                           levels=clevels,
                                           ax=ax)
            bmp_info.basemap.drawcoastlines(ax=ax)
            bmp_info.basemap.readshapefile(BASIN_BOUNDARIES_SHP[:-4],
                                           "basin",
                                           ax=ax)
            ax.set_title(season)

        ax_list[0].set_ylabel("Hopkinson")
        cb = plt.colorbar(cs, cax=fig.add_subplot(gs[:2, -1]))
        cb.ax.set_xlabel(infovar.get_units(vname))
        _format_axes(ax_list, vname=vname)
        row += 1

        # Plot model (CRCM) values-------------------------
        # ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        # cs = None
        #
        # season_to_field_crcm = analysis.get_seasonal_climatology_for_runconfig(run_config=reanalysis_driven_config,
        #                                                                        varname=vname, level=0,
        #                                                                        season_to_months=season_to_months)
        #
        # for j, (season, crcm_field) in enumerate(season_to_field_crcm.items()):
        #     ax = ax_list[j]
        #     cs = bmp_info.basemap.contourf(xx, yy, crcm_field * 1000 * 24 * 3600, levels=clevels, ax=ax)
        #     bmp_info.basemap.drawcoastlines(ax=ax)
        #     bmp_info.basemap.readshapefile(BASIN_BOUNDARIES_SHP[:-4], "basin", ax=ax)
        #     ax.set_title(season)
        #
        # ax_list[0].set_ylabel(reanalysis_driven_config.label)
        # cb = plt.colorbar(cs, cax=fig.add_subplot(gs[:2, -1]))
        # cb.ax.set_xlabel(infovar.get_units(vname))
        # _format_axes(ax_list, vname=vname)
        # row += 1

        # Plot (Model - CRU) Performance biases-------------------------
        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        cs = plot_performance_err_with_cru.compare_vars(
            vname_model=vname,
            vname_obs=None,
            r_config=reanalysis_driven_config,
            season_to_months=season_to_months,
            obs_path=vname_to_cru_path[vname],
            bmp_info_agg=bmp_info_agg,
            diff_axes_list=ax_list,
            mask_shape_file=os.path.join(GL_SHP_FOLDER, "gl_cst.shp"),
            nx_agg_model=nx_agg_model,
            ny_agg_model=ny_agg_model)

        ax_list[0].set_ylabel(
            "{label}\n--\nCRU".format(label=reanalysis_driven_config.label))
        _format_axes(ax_list, vname=vname)
        row += 1

        # Plot performance+BFE errors with respect to CRU (Model - CRU)-------------------------
        # ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        # plot_performance_err_with_cru.compare_vars(vname, vname_obs=None, obs_path=vname_to_cru_path[vname],
        #                                            r_config=gcm_driven_config,
        #                                            bmp_info_agg=bmp_info_agg, season_to_months=season_to_months,
        #                                            axes_list=ax_list)
        # _format_axes(ax_list, vname=vname)
        # ax_list[0].set_ylabel("{label}\nvs\nCRU".format(label=gcm_driven_config.label))
        # row += 1

        # Plot performance errors with respect to ANUSPLIN (Model - ANUSPLIN)-------------------------
        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        plot_performance_err_with_anusplin.compare_vars(
            vname, {vname: season_to_obs_anusplin},
            r_config=reanalysis_driven_config,
            bmp_info_agg=bmp_info,
            season_to_months=season_to_months,
            axes_list=ax_list)
        _format_axes(ax_list, vname=vname)
        ax_list[0].set_ylabel("{label}\n--\nHopkinson".format(
            label=reanalysis_driven_config.label))
        row += 1

        # Plot performance+BFE errors with respect to ANUSPLIN (Model - ANUSPLIN)-------------------------
        # ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        # plot_performance_err_with_anusplin.compare_vars(vname, {vname: season_to_obs_anusplin},
        #                                                 r_config=gcm_driven_config,
        #                                                 bmp_info_agg=bmp_info, season_to_months=season_to_months,
        #                                                 axes_list=ax_list)
        # _format_axes(ax_list, vname=vname)
        # ax_list[0].set_ylabel("{label}\nvs\nHopkinson".format(label=gcm_driven_config.label))

        cb = plt.colorbar(cs, cax=fig.add_subplot(gs[-2:, -1]))
        cb.ax.set_xlabel(infovar.get_units(vname))

        # Save the plot
        img_file = "{vname}_{sy}-{ey}_{sim_label}.png".format(
            vname=vname,
            sy=reanalysis_driven_config.start_year,
            ey=reanalysis_driven_config.end_year,
            sim_label=reanalysis_driven_config.label)

        img_file = img_folder.joinpath(img_file)
        with img_file.open("wb") as f:
            fig.savefig(f, bbox_inches="tight")
        plt.close(fig)
Example #17
0
def main():
    plot_utils.apply_plot_params(font_size=12,
                                 width_pt=None,
                                 width_cm=25,
                                 height_cm=25)

    season_to_months = DEFAULT_SEASON_TO_MONTHS

    r_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        start_year=1980,
        end_year=2009,
        label="CRCM5-L")

    # Number of points for aggregation
    nx_agg = 5
    ny_agg = 5

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=r_config.data_path)

    bmp_info_agg = bmp_info.get_aggregated(nagg_x=nx_agg, nagg_y=ny_agg)

    # Validate temperature and precip
    model_vars = ["TT", "PR"]
    obs_vars = ["air", "precip"]
    obs_paths = [
        "/RECH/data/Validation/Willmott_Matsuura/NetCDF/air.mon.mean.v301.nc",
        "/RECH/data/Validation/Willmott_Matsuura/NetCDF/precip.mon.total.v301.nc"
    ]

    plot_all_vars_in_one_fig = True

    fig = None
    gs = None
    row_axes = None
    ncols = None
    if plot_all_vars_in_one_fig:
        plot_utils.apply_plot_params(font_size=12,
                                     width_pt=None,
                                     width_cm=25,
                                     height_cm=12)
        fig = plt.figure()
        ncols = len(season_to_months) + 1
        gs = GridSpec(len(model_vars),
                      ncols,
                      width_ratios=(ncols - 1) * [
                          1.,
                      ] + [
                          0.05,
                      ])
    else:
        plot_utils.apply_plot_params(font_size=12,
                                     width_pt=None,
                                     width_cm=25,
                                     height_cm=25)

    row = 0
    for mname, oname, opath in zip(model_vars, obs_vars, obs_paths):

        if plot_all_vars_in_one_fig:
            row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols)]

        compare_vars(vname_model=mname,
                     vname_obs=oname,
                     r_config=r_config,
                     season_to_months=season_to_months,
                     nx_agg=nx_agg,
                     ny_agg=ny_agg,
                     bmp_info_agg=bmp_info_agg,
                     obs_path=opath,
                     axes_list=row_axes)

        row += 1

    # Save the figure if necessary
    if plot_all_vars_in_one_fig:
        fig_path = img_folder.joinpath("{}.png".format("_".join(model_vars)))
        with fig_path.open("wb") as figfile:
            fig.savefig(figfile, format="png", bbox_inches="tight")

        plt.close(fig)
def main():
    # import seaborn as sns
    # sns.set_context("paper", font_scale=2)
    # sns.set_style("whitegrid")

    level_widths_mm = MM_PER_METER * infovar.soil_layer_widths_26_to_60

    avg_axis = "lon"
    start_year_c = 1980
    end_year_c = 2010

    img_folder = Path("cc_paper/lake_props")
    if not img_folder.exists():
        img_folder.mkdir(parents=True)

    # Configuration without interflow, to be compared with the one with intf.
    base_config = RunConfig(
        start_year=start_year_c,
        end_year=end_year_c,
        data_path=
        "/home/huziy/skynet3_rech1/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        label="CRCM5-L")

    current_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"

    # Need to read land fraction
    geo_file_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    r_obj = RPN(geo_file_path)
    mg_field = r_obj.get_first_record_for_name("MG")
    depth_to_bedrock_mm = r_obj.get_first_record_for_name("8L") * MM_PER_METER
    lake_fraction = r_obj.get_first_record_for_name("ML")

    # recompute layer widths to account for the depth to bedrock
    layer_widths_3d = np.ones(depth_to_bedrock_mm.shape +
                              level_widths_mm.shape)
    layer_widths_3d *= level_widths_mm[np.newaxis, np.newaxis, :]

    layer_bottoms_3d = layer_widths_3d.cumsum(axis=2)

    corrections = depth_to_bedrock_mm[:, :, np.newaxis] - layer_bottoms_3d
    layer_widths_3d[corrections < 0] += corrections[corrections < 0]
    layer_widths_3d[layer_widths_3d < 0] = 0

    lons, lats = r_obj.get_longitudes_and_latitudes_for_the_last_read_rec()
    r_obj.close()

    # Current and future configurations
    current_config = RunConfig(start_year=start_year_c,
                               end_year=end_year_c,
                               data_path=current_path,
                               label="CRCM5-L")

    n_shift_years = 90
    future_config = current_config.get_shifted_config(n_shift_years)
    print(future_config)

    varname = "L1"
    level = 0
    daily_dates, lake_temp_c = analysis.get_daily_climatology_for_rconf(
        current_config, var_name=varname, level=level)

    _, lake_temp_f = analysis.get_daily_climatology_for_rconf(future_config,
                                                              var_name=varname,
                                                              level=level)

    # average along a dim
    lake_temp_c = _avg_along(lake_temp_c,
                             axis=avg_axis,
                             lake_fraction=lake_fraction)
    lake_temp_f = _avg_along(lake_temp_f,
                             axis=avg_axis,
                             lake_fraction=lake_fraction)

    zagg = None
    ztitle = ""
    if avg_axis == "lon":
        zagg = lats.mean(axis=0)
        ztitle = "Latitude"
    elif avg_axis == "lat":
        zagg = lons.mean(axis=1)
        ztitle = "Longitude"

    num_dates = date2num(daily_dates)

    z_agg_2d, num_dates_2d = np.meshgrid(zagg, num_dates)

    # Do the plotting
    plot_utils.apply_plot_params(font_size=14,
                                 width_pt=None,
                                 width_cm=20,
                                 height_cm=30)
    fig = plt.figure()

    gs = GridSpec(4, 2, width_ratios=[1, 0.02])

    all_axes = []
    # ----------------------------------Lake temperature----------------------------------
    row = 0
    ax = fig.add_subplot(gs[row, 0])
    cmap = cm.get_cmap("bwr", 15)
    to_plot = lake_temp_f - lake_temp_c[:, :]
    vmax = max(to_plot.max(), 0)
    vmin = min(to_plot.min(), 0)
    vmax = max(abs(vmin), abs(vmax))
    # norm = MidpointNormalize(vmin=vmin, vmax=vmax)
    clevs = MaxNLocator(cmap.N + 1).tick_values(-vmax, vmax)

    cs = ax.contourf(num_dates_2d,
                     z_agg_2d,
                     to_plot,
                     cmap=cmap,
                     levels=clevs,
                     extend="both")
    ax.set_title("Lake surface water temperature")
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 3))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel(r"${\rm ^\circ C}$")
    cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Lake ice thickness----------------------------------
    level = 0
    varname = "LD"
    _, lake_ice_th_c = analysis.get_daily_climatology_for_rconf(
        current_config, var_name=varname, level=level)

    _, lake_ice_th_f = analysis.get_daily_climatology_for_rconf(
        future_config, var_name=varname, level=level)

    lake_ice_th = _avg_along(lake_ice_th_f - lake_ice_th_c,
                             axis=avg_axis,
                             lake_fraction=lake_fraction)

    # get the points for which the average should be calculated
    winter_points = np.asarray(
        [i for i, d in enumerate(daily_dates) if d.month in [1, 2, 12]])

    row += 1
    ax = fig.add_subplot(gs[row, 0])

    vmax = max(lake_ice_th.max(), 0)
    vmin = min(lake_ice_th.min(), 0)
    vmax = max(abs(vmin), abs(vmax))
    # norm = MidpointNormalize(vmin=vmin, vmax=vmax)
    clevs = MaxNLocator(cmap.N + 1).tick_values(-vmax, vmax)

    cs = ax.contourf(num_dates_2d,
                     z_agg_2d,
                     lake_ice_th,
                     cmap=cmap,
                     levels=clevs,
                     extend="both")
    ax.set_title("Lake ice thickness")

    print("Mean change in ice thickness: {} m".format(
        lake_ice_th.mean(axis=1)[winter_points].mean()))
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("m")
    cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Lake ice fraction----------------------------------
    level = 0
    varname = "LC"
    _, lake_ice_fraction_c = analysis.get_daily_climatology_for_rconf(
        current_config, var_name=varname, level=level)

    _, lake_ice_fraction_f = analysis.get_daily_climatology_for_rconf(
        future_config, var_name=varname, level=level)

    lake_ice_fraction = _avg_along(lake_ice_fraction_f - lake_ice_fraction_c,
                                   axis=avg_axis,
                                   lake_fraction=lake_fraction)

    row += 1
    ax = fig.add_subplot(gs[row, 0])

    vmax = max(lake_ice_fraction.max(), 0)
    vmin = min(lake_ice_fraction.min(), 0)
    vmax = max(abs(vmin), abs(vmax))
    # norm = MidpointNormalize(vmin=vmin, vmax=vmax)
    clevs = MaxNLocator(cmap.N + 1).tick_values(-vmax, vmax)

    cs = ax.contourf(num_dates_2d,
                     z_agg_2d,
                     lake_ice_fraction,
                     cmap=cmap,
                     levels=clevs,
                     extend="both")
    ax.set_title("Lake ice fraction")
    print("Mean change in ice fraction: {}".format(
        lake_ice_fraction.mean(axis=1)[winter_points].mean()))
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("")
    cax.yaxis.get_offset_text().set_position((-2, 10))
    # ----------------------------------Lake depth----------------------------------
    level = 0
    varname = "CLDP"
    _, lake_depth_c = analysis.get_daily_climatology_for_rconf(
        current_config, var_name=varname, level=level)

    _, lake_depth_f = analysis.get_daily_climatology_for_rconf(
        future_config, var_name=varname, level=level)

    lake_depth_cc = _avg_along(lake_depth_f - lake_depth_c,
                               axis=avg_axis,
                               lake_fraction=lake_fraction)

    row += 1
    ax = fig.add_subplot(gs[row, 0])

    clevs = [0.01, 0.1, 0.5, 0.75, 1, 2, 3]
    clevs = [-c for c in reversed(clevs)] + clevs
    cmap = cm.get_cmap("bwr", len(clevs) - 1)

    norm = BoundaryNorm(boundaries=clevs, ncolors=len(clevs) - 1)

    cs = ax.contourf(num_dates_2d,
                     z_agg_2d,
                     lake_depth_cc,
                     cmap=cmap,
                     levels=clevs,
                     norm=norm,
                     extend="both")
    ax.set_title("Lake level")
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt, ticks=clevs)
    cax.set_xlabel("m")
    cax.yaxis.get_offset_text().set_position((-2, 10))

    for i, the_ax in enumerate(all_axes):
        the_ax.xaxis.set_major_formatter(
            FuncFormatter(lambda d, pos: num2date(d).strftime("%b")[0]))
        the_ax.xaxis.set_major_locator(MonthLocator(bymonthday=15))
        the_ax.xaxis.set_minor_locator(MonthLocator(bymonthday=1))
        the_ax.grid(which="minor")
        the_ax.set_ylabel(ztitle)

    img_file = Path(img_folder).joinpath(
        "cc_{}_Lake_props_current_{}_avg_{}-{}_vs_{fsy}-{fey}.png".format(
            base_config.label,
            avg_axis,
            start_year_c,
            end_year_c,
            fsy=future_config.start_year,
            fey=future_config.end_year))

    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")

    plt.show()
Example #19
0
def main():
    # import seaborn as sns
    # sns.set_context("paper", font_scale=2)
    # sns.set_style("whitegrid")

    level_widths_mm = MM_PER_METER * infovar.soil_layer_widths_26_to_60

    avg_axis = "lon"
    start_year_c = 1980
    end_year_c = 2010

    img_folder = Path("cc_paper/lake_props")
    if not img_folder.exists():
        img_folder.mkdir(parents=True)


    # Configuration without interflow, to be compared with the one with intf.
    base_config = RunConfig(start_year=start_year_c, end_year=end_year_c,
                            data_path="/home/huziy/skynet3_rech1/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
                            label="CRCM5-L")

    current_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"

    # Need to read land fraction
    geo_file_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    r_obj = RPN(geo_file_path)
    mg_field = r_obj.get_first_record_for_name("MG")
    depth_to_bedrock_mm = r_obj.get_first_record_for_name("8L") * MM_PER_METER
    lake_fraction = r_obj.get_first_record_for_name("ML")


    # recompute layer widths to account for the depth to bedrock
    layer_widths_3d = np.ones(depth_to_bedrock_mm.shape + level_widths_mm.shape)
    layer_widths_3d *= level_widths_mm[np.newaxis, np.newaxis, :]

    layer_bottoms_3d = layer_widths_3d.cumsum(axis=2)

    corrections = depth_to_bedrock_mm[:, :, np.newaxis] - layer_bottoms_3d
    layer_widths_3d[corrections < 0] += corrections[corrections < 0]
    layer_widths_3d[layer_widths_3d < 0] = 0

    lons, lats = r_obj.get_longitudes_and_latitudes_for_the_last_read_rec()
    r_obj.close()

    # Current and future configurations
    current_config = RunConfig(start_year=start_year_c, end_year=end_year_c,
                               data_path=current_path,
                               label="CRCM5-L")

    n_shift_years = 90
    future_config = current_config.get_shifted_config(n_shift_years)
    print(future_config)

    varname = "L1"
    level = 0
    daily_dates, lake_temp_c = analysis.get_daily_climatology_for_rconf(current_config,
                                                                        var_name=varname, level=level)

    _, lake_temp_f = analysis.get_daily_climatology_for_rconf(future_config,
                                                              var_name=varname, level=level)

    # average along a dim
    lake_temp_c = _avg_along(lake_temp_c, axis=avg_axis, lake_fraction=lake_fraction)
    lake_temp_f = _avg_along(lake_temp_f, axis=avg_axis, lake_fraction=lake_fraction)

    zagg = None
    ztitle = ""
    if avg_axis == "lon":
        zagg = lats.mean(axis=0)
        ztitle = "Latitude"
    elif avg_axis == "lat":
        zagg = lons.mean(axis=1)
        ztitle = "Longitude"

    num_dates = date2num(daily_dates)

    z_agg_2d, num_dates_2d = np.meshgrid(zagg, num_dates)


    # Do the plotting
    plot_utils.apply_plot_params(font_size=14, width_pt=None, width_cm=20, height_cm=30)
    fig = plt.figure()

    gs = GridSpec(4, 2, width_ratios=[1, 0.02])

    all_axes = []
    # ----------------------------------Lake temperature----------------------------------
    row = 0
    ax = fig.add_subplot(gs[row, 0])
    cmap = cm.get_cmap("bwr", 15)
    to_plot = lake_temp_f - lake_temp_c[:, :]
    vmax = max(to_plot.max(), 0)
    vmin = min(to_plot.min(), 0)
    vmax = max(abs(vmin), abs(vmax))
    # norm = MidpointNormalize(vmin=vmin, vmax=vmax)
    clevs = MaxNLocator(cmap.N + 1).tick_values(-vmax, vmax)

    cs = ax.contourf(num_dates_2d, z_agg_2d, to_plot, cmap=cmap, levels=clevs, extend="both")
    ax.set_title("Lake surface water temperature")
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 3))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel(r"${\rm ^\circ C}$")
    cax.yaxis.get_offset_text().set_position((-2, 10))


    # ----------------------------------Lake ice thickness----------------------------------
    level = 0
    varname = "LD"
    _, lake_ice_th_c = analysis.get_daily_climatology_for_rconf(current_config,
                                                                var_name=varname, level=level)

    _, lake_ice_th_f = analysis.get_daily_climatology_for_rconf(future_config,
                                                                var_name=varname, level=level)

    lake_ice_th = _avg_along(lake_ice_th_f - lake_ice_th_c, axis=avg_axis, lake_fraction=lake_fraction)

    # get the points for which the average should be calculated
    winter_points = np.asarray([i for i, d in enumerate(daily_dates) if d.month in [1, 2, 12]])



    row += 1
    ax = fig.add_subplot(gs[row, 0])

    vmax = max(lake_ice_th.max(), 0)
    vmin = min(lake_ice_th.min(), 0)
    vmax = max(abs(vmin), abs(vmax))
    # norm = MidpointNormalize(vmin=vmin, vmax=vmax)
    clevs = MaxNLocator(cmap.N + 1).tick_values(-vmax, vmax)

    cs = ax.contourf(num_dates_2d, z_agg_2d, lake_ice_th, cmap=cmap, levels=clevs, extend="both")
    ax.set_title("Lake ice thickness")



    print("Mean change in ice thickness: {} m".format(lake_ice_th.mean(axis=1)[winter_points].mean()))
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("m")
    cax.yaxis.get_offset_text().set_position((-2, 10))

    # ----------------------------------Lake ice fraction----------------------------------
    level = 0
    varname = "LC"
    _, lake_ice_fraction_c = analysis.get_daily_climatology_for_rconf(current_config,
                                                                      var_name=varname, level=level)

    _, lake_ice_fraction_f = analysis.get_daily_climatology_for_rconf(future_config,
                                                                      var_name=varname, level=level)

    lake_ice_fraction = _avg_along(lake_ice_fraction_f - lake_ice_fraction_c, axis=avg_axis,
                                   lake_fraction=lake_fraction)

    row += 1
    ax = fig.add_subplot(gs[row, 0])

    vmax = max(lake_ice_fraction.max(), 0)
    vmin = min(lake_ice_fraction.min(), 0)
    vmax = max(abs(vmin), abs(vmax))
    # norm = MidpointNormalize(vmin=vmin, vmax=vmax)
    clevs = MaxNLocator(cmap.N + 1).tick_values(-vmax, vmax)

    cs = ax.contourf(num_dates_2d, z_agg_2d, lake_ice_fraction, cmap=cmap, levels=clevs, extend="both")
    ax.set_title("Lake ice fraction")
    print("Mean change in ice fraction: {}".format(lake_ice_fraction.mean(axis=1)[winter_points].mean()))
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("")
    cax.yaxis.get_offset_text().set_position((-2, 10))
    # ----------------------------------Lake depth----------------------------------
    level = 0
    varname = "CLDP"
    _, lake_depth_c = analysis.get_daily_climatology_for_rconf(current_config,
                                                               var_name=varname, level=level)

    _, lake_depth_f = analysis.get_daily_climatology_for_rconf(future_config,
                                                               var_name=varname, level=level)

    lake_depth_cc = _avg_along(lake_depth_f - lake_depth_c, axis=avg_axis,
                               lake_fraction=lake_fraction)

    row += 1
    ax = fig.add_subplot(gs[row, 0])

    clevs = [0.01, 0.1, 0.5, 0.75, 1, 2, 3]
    clevs = [-c for c in reversed(clevs)] + clevs
    cmap = cm.get_cmap("bwr", len(clevs) - 1)

    norm = BoundaryNorm(boundaries=clevs, ncolors=len(clevs) - 1)

    cs = ax.contourf(num_dates_2d, z_agg_2d, lake_depth_cc, cmap=cmap, levels=clevs, norm=norm, extend="both")
    ax.set_title("Lake level")
    all_axes.append(ax)

    # Colorbar for value plots
    cax = fig.add_subplot(gs[row, -1])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-2, 2))

    plt.colorbar(cs, cax=cax, format=sfmt, ticks=clevs)
    cax.set_xlabel("m")
    cax.yaxis.get_offset_text().set_position((-2, 10))

    for i, the_ax in enumerate(all_axes):
        the_ax.xaxis.set_major_formatter(FuncFormatter(lambda d, pos: num2date(d).strftime("%b")[0]))
        the_ax.xaxis.set_major_locator(MonthLocator(bymonthday=15))
        the_ax.xaxis.set_minor_locator(MonthLocator(bymonthday=1))
        the_ax.grid(which="minor")
        the_ax.set_ylabel(ztitle)

    img_file = Path(img_folder).joinpath("cc_{}_Lake_props_current_{}_avg_{}-{}_vs_{fsy}-{fey}.png".format(
        base_config.label, avg_axis, start_year_c, end_year_c,
        fsy=future_config.start_year, fey=future_config.end_year))

    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")

    plt.show()
Example #20
0
def main():
    season_to_months = OrderedDict([(s, months) for s, months in DEFAULT_SEASON_TO_MONTHS.items()
                                    if s.lower() not in ["summer", "fall"]])

    r_config_01 = RunConfig(
        data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        start_year=1980, end_year=1996, label="CRCM5-L"
    )

    r_config_04 = RunConfig(
        data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.4_crcm5-hcd-rl.hdf5",
        start_year=1980, end_year=1996, label="CRCM5-L(0.4)"
    )

    r_config = r_config_04

    # Number of points for aggregation
    nx_agg_model = 1
    ny_agg_model = 1

    nx_agg_obs = 2
    ny_agg_obs = 2




    r_config_cc = RunConfig(
        data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5",
        start_year=1980, end_year=1996, label="CRCM5-L"
    )


    bmp_info = analysis.get_basemap_info_from_hdf(file_path=r_config.data_path)

    if nx_agg_model * ny_agg_model > 1:
        bmp_info_agg = bmp_info.get_aggregated(nagg_x=nx_agg_model, nagg_y=ny_agg_model)
    else:
        bmp_info_agg = bmp_info

    # Validate temperature and precip
    model_vars = ["I5", ]
    obs_vars = ["SWE", ]
    obs_paths = [
        "/RESCUE/skynet3_rech1/huziy/swe_ross_brown/swe.nc4",
    ]

    plot_all_vars_in_one_fig = True

    fig = None
    gs = None
    row_axes = None
    obs_row_axes = None
    ncols = None
    if plot_all_vars_in_one_fig:
        plot_utils.apply_plot_params(font_size=12, width_pt=None, width_cm=12, height_cm=18)
        fig = plt.figure()
        ncols = len(season_to_months) + 1
        gs = GridSpec(len(model_vars) * 3, ncols, width_ratios=(ncols - 1) * [1., ] + [0.05, ])

    else:
        plot_utils.apply_plot_params(font_size=12, width_pt=None, width_cm=25, height_cm=25)

    row = 0
    for mname, oname, opath in zip(model_vars, obs_vars, obs_paths):

        if plot_all_vars_in_one_fig:
            # Obs row is for observed values
            obs_row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols)]
            obs_row_axes[-1].set_title("mm")
            row += 1

            model_row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols - 1)]
            row += 1

            row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols)]
            row += 1

        compare_vars(vname_model=mname, vname_obs=oname, r_config=r_config,
                     season_to_months=season_to_months,
                     nx_agg_model=nx_agg_model, ny_agg_model=ny_agg_model,
                     nx_agg_obs=nx_agg_obs, ny_agg_obs=ny_agg_obs,
                     bmp_info_agg=bmp_info_agg, bmp_info_model=bmp_info,
                     obs_path=opath,
                     diff_axes_list=row_axes, obs_axes_list=obs_row_axes, model_axes_list=model_row_axes)

        if plot_all_vars_in_one_fig:
            obs_row_axes[0].set_ylabel("(a)", rotation="horizontal", labelpad=10)
            model_row_axes[0].set_ylabel("(b)", rotation="horizontal", labelpad=10)

            row_axes[0].set_ylabel("(c)", rotation="horizontal", labelpad=10)
            for ax in row_axes[:-1]:
                ax.set_title("")

    # row_axes[0].annotate("(a)", (-0.2, 0.5), font_properties=FontProperties(weight="bold"), xycoords="axes fraction")



    # Plot bfe errs
    # row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols)]
    # plot_swe_bfes(r_config, r_config_cc, vname_model="I5", season_to_months=season_to_months,
    #               bmp_info=bmp_info, axes_list=row_axes)
    #
    # row_axes[-1].set_visible(False)


    # row_axes[0].annotate("(b)", (0, 1.05), font_properties=FontProperties(weight="bold"), xycoords="axes fraction")
    # row_axes[0].set_ylabel("(b)", rotation="horizontal", labelpad=10)
    # for ax in row_axes[:-1]:
    #     ax.set_title("")


    # Save the figure if necessary
    if plot_all_vars_in_one_fig:

        from crcm5.analyse_hdf import common_plot_params

        fig_path = img_folder.joinpath("{}_{}.png".format("_".join(model_vars), r_config.label))
        with fig_path.open("wb") as figfile:
            fig.savefig(figfile, format="png", bbox_inches="tight", dpi=common_plot_params.FIG_SAVE_DPI)

        plt.close(fig)
Example #21
0
def main():
    lkfr_limit = 0.05
    model_data_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
                         "quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"


    modif_label = "CanESM2-CRCM5-L"

    start_year_c = 1980
    end_year_c = 2010

    future_shift_years = 90

    params = dict(
        start_year=start_year_c, end_year=end_year_c
    )

    params.update(
        dict(data_path=model_data_current_path, label=modif_label)
    )

    model_config_c = RunConfig(**params)
    model_config_f = model_config_c.get_shifted_config(future_shift_years)



    bmp_info = analysis.get_basemap_info(r_config=model_config_c)


    specific_cond_heat = 0.250100e7  # J/kg
    water_density = 1000.0  # kg/m**3

    season_to_months = OrderedDict([
        ("Summer", [6, 7, 8]),
    ])

    lkfr = analysis.get_array_from_file(path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5", var_name=infovar.HDF_LAKE_FRACTION_NAME)

    assert lkfr is not None, "Could not find lake fraction in the file"

    # Current climate
    traf_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="TRAF", level=5, season_to_months=season_to_months)
    pr_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="PR", level=0, season_to_months=season_to_months)

    lktemp_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="L1", level=0, season_to_months=season_to_months)
    airtemp_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="TT", level=0, season_to_months=season_to_months)

    lhc = OrderedDict([
        (s, specific_cond_heat * (pr_c[s] * water_density - traf_c[s])) for s, traf in traf_c.items()
    ])



    avc = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="AV", level=0, season_to_months=season_to_months)


    plt.figure()
    lhc["Summer"] = np.ma.masked_where(lkfr < lkfr_limit, lhc["Summer"])
    print("min: {}, max: {}".format(lhc["Summer"].min(), lhc["Summer"].max()))
    cs = plt.contourf(lhc["Summer"].T)
    plt.title("lhc")
    plt.colorbar()

    plt.figure()
    cs = plt.contourf(avc["Summer"].T, levels=cs.levels, norm=cs.norm, cmap=cs.cmap)
    plt.title("avc")
    plt.colorbar()

    # Future climate
    traf_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="TRAF", level=5, season_to_months=season_to_months)
    pr_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="PR", level=0,
                                                           season_to_months=season_to_months)

    lktemp_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="L1", level=0,
                                                             season_to_months=season_to_months)
    airtemp_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="TT", level=0,
                                                              season_to_months=season_to_months)

    lhf = OrderedDict([
        (s, specific_cond_heat * (pr_f[s] * water_density - traf_f[s])) for s, traf in traf_f.items()
    ])

    plt.figure()
    plt.pcolormesh(traf_c["Summer"].T)
    plt.title("TRAF over lakes current")
    plt.colorbar()



    avf = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="AV", level=0,
                                                          season_to_months=season_to_months)

    plt.figure()
    cs = plt.contourf(avf["Summer"].T)
    plt.title("avf")
    plt.colorbar()


    plt.figure()
    cs = plt.contourf(avf["Summer"].T - avc["Summer"].T, levels=np.arange(-40, 45, 5))
    plt.title("d(av)")
    plt.colorbar()


    plt.figure()
    plt.contourf(lhf["Summer"].T - lhc["Summer"].T, levels=cs.levels, cmap=cs.cmap, norm=cs.norm)
    plt.title("d(lh)")
    plt.colorbar()



    # plotting
    plot_utils.apply_plot_params(width_cm=15, height_cm=15, font_size=10)
    gs = GridSpec(2, 2)




    # tair_c_ts = analysis.get_area_mean_timeseries(model_config_c.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_c.start_year, end_year=model_config_c.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)
    #
    # tair_f_ts = analysis.get_area_mean_timeseries(model_config_f.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_f.start_year, end_year=model_config_f.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)
    #
    #
    # tlake_c_ts = analysis.get_area_mean_timeseries(model_config_c.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_c.start_year, end_year=model_config_c.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)
    #
    # tlake_f_ts = analysis.get_area_mean_timeseries(model_config_f.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_f.start_year, end_year=model_config_f.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)






    for season in season_to_months:
        fig = plt.figure()


        lktemp_c[season] -= 273.15
        dT_c = np.ma.masked_where(lkfr < lkfr_limit, lktemp_c[season] - airtemp_c[season])


        lktemp_f[season] -= 273.15
        dT_f = np.ma.masked_where(lkfr < lkfr_limit, lktemp_f[season] - airtemp_f[season])

        d = np.round(max(np.ma.abs(dT_c).max(), np.ma.abs(dT_f).max()))

        vmin = -d
        vmax = d

        clevs = np.arange(-12, 13, 1)
        ncolors = len(clevs) - 1
        bn = BoundaryNorm(clevs, ncolors=ncolors)
        cmap = cm.get_cmap("seismic", ncolors)




        ax_list = []

        fig.suptitle(season)

        xx, yy = bmp_info.get_proj_xy()

        # Current gradient
        ax = fig.add_subplot(gs[0, 0])
        ax.set_title(r"current: $T_{\rm lake} - T_{\rm atm}$")
        cs = bmp_info.basemap.pcolormesh(xx, yy, dT_c, ax=ax, norm=bn, cmap=cmap)
        bmp_info.basemap.colorbar(cs, ax=ax, extend="both")
        ax_list.append(ax)



        # Future Gradient
        ax = fig.add_subplot(gs[0, 1])
        ax.set_title(r"future: $T_{\rm lake} - T_{\rm atm}$")
        cs = bmp_info.basemap.pcolormesh(xx, yy, dT_f, ax=ax, norm=cs.norm, cmap=cs.cmap, vmin=vmin, vmax=vmax)
        bmp_info.basemap.colorbar(cs, ax=ax, extend="both")
        ax_list.append(ax)


        # Change in the gradient
        ax = fig.add_subplot(gs[1, 0])
        ax.set_title(r"$\Delta T_{\rm future} - \Delta T_{\rm current}$")

        ddT = dT_f - dT_c
        d = np.round(np.ma.abs(ddT).max())
        clevs = np.arange(-3, 3.1, 0.1)
        ncolors = len(clevs) - 1
        bn = BoundaryNorm(clevs, ncolors=ncolors)
        cmap = cm.get_cmap("seismic", ncolors)
        cs = bmp_info.basemap.pcolormesh(xx, yy, ddT, norm=bn, cmap=cmap)
        bmp_info.basemap.colorbar(cs, ax=ax, extend="both")
        ax_list.append(ax)



        # Change in the latent heat flux
        # ax = fig.add_subplot(gs[1, 1])
        # ax.set_title(r"$LE_{\rm future} - LE_{\rm current}$")
        # dlh = np.ma.masked_where(lkfr < lkfr_limit, lhf[season] - lhc[season])
        #
        # d = np.round(np.ma.abs(dlh).max() // 10) * 10
        # clevs = np.arange(0, 105, 5)
        # bn = BoundaryNorm(clevs, ncolors=ncolors)
        # cmap = cm.get_cmap("jet", ncolors)
        #
        # cs = bmp_info.basemap.pcolormesh(xx, yy, dlh, norm=bn, cmap=cmap)
        # bmp_info.basemap.colorbar(cs, ax=ax, extend="max")  # Change in the latent heat flux
        # ax_list.append(ax)


        for the_ax in ax_list:
            bmp_info.basemap.drawcoastlines(linewidth=0.3, ax=the_ax)


        fig.tight_layout()
        fig.savefig(os.path.join(img_folder, "lake_atm_gradients_and_fluxes_{}-{}_{}-{}.png".format(model_config_f.start_year, model_config_f.end_year, start_year_c, end_year_c)),
                    dpi=800,
                    bbox_inches="tight")
def main():
    base_current_path = \
        "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    base_label = "CanESM2-CRCM5-NL"

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

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    future_shift_years = 90

    params = dict(data_path=base_current_path,
                  start_year=start_year_c,
                  end_year=end_year_c,
                  label=base_label)

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

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    base_configs = [base_config_c, base_config_f]

    params.update(dict(data_path=modif_current_path, label=modif_label))

    modif_config_c = RunConfig(**params)
    modif_config_f = modif_config_c.get_shifted_config(future_shift_years)
    modif_configs = [modif_config_c, modif_config_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")
    lake_fraction = r_obj.get_first_record_for_name_and_level("ML")
    lake_fraction = np.ma.masked_where((fldr <= 0) | (fldr > 128),
                                       lake_fraction)

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=base_current_path)

    # plots basin boundaries and names on the domain plot
    basin_name_to_out_indices_map, basin_name_to_basin_mask = plot_basin_outlets(
        bmp_info=bmp_info,
        accumulation_areas=facc,
        directions=fldr,
        lake_fraction_field=lake_fraction)

    # 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=lake_fraction)

    data_to_plot = read_cc_and_cc_diff(
        base_configs,
        modif_configs,
        name_to_indices=basin_name_to_out_indices_map,
        varname=varname)

    basin_name_to_basin_mask = basin_name_to_basin_mask if varname not in [
        "STFA", "STFL"
    ] else None

    img_path = get_image_path(base_config_c, base_config_f, modif_config_c,
                              modif_config_f, varname)

    # 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
    }

    calculate_and_plot_climate_change_hydrographs(
        data_to_plot,
        name_to_out_indices=basin_name_to_out_indices_map,
        months=list(range(1, 13)),
        varname=varname,
        img_path=img_path)
def main():
    """

    """

    season_to_months = get_default_season_to_months_dict()

    # season_to_months = OrderedDict([("April", [4, ]), ("May", [5, ]), ("June", [6, ]), ("July", [7, ])])

    var_names = ["TT", "HU", "PR", "AV", "AH", "STFL", "TRAF", "I5", "I0"]

    # var_names = ["TT", "PR"]

    levels = [0, ] * len(var_names)
    multipliers = {
        "PR": 1.0e3 * 24.0 * 3600.,
        "TRAF": 24 * 3600.0
    }

    base_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
                        "quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    base_label = "CanESM2-CRCM5-NL"

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

    # base_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
    # "quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    # base_label = "CRCM5-L"
    #
    # modif_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
    # "quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5"
    # modif_label = "CRCM5-LI"

    start_year_c = 1980
    end_year_c = 2010

    future_shift_years = 90

    params = dict(
        data_path=base_current_path, start_year=start_year_c, end_year=end_year_c, label=base_label
    )

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    params.update(
        dict(data_path=modif_current_path, label=modif_label)
    )

    modif_config_c = RunConfig(**params)
    modif_config_f = modif_config_c.get_shifted_config(future_shift_years)

    config_dict = OrderedDict([
        ("Current", OrderedDict([(base_label, base_config_c), (modif_label, modif_config_c)])),
        ("Future", OrderedDict([(base_label, base_config_f), (modif_label, modif_config_f)]))
    ])

    # Plot the differences
    config_dict.label_modif = modif_config_c.label
    config_dict.label_base = base_config_c.label
    config_dict.season_to_months = season_to_months
    config_dict.multipliers = multipliers

    lons, lats, bmp = analysis.get_basemap_from_hdf(base_current_path)
    config_dict.lons = lons
    config_dict.lats = lats
    config_dict.basemap = bmp

    # Calculate and plot seasonal means
    for vname, level in zip(var_names, levels):
        data = get_data(vname=vname, level=level, config_dict=config_dict)
        _plot_var(vname=vname, level=level, config_dict=config_dict, data_dict=data)
Example #24
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(vars_of_interest=None):
    # Validation with CRU (temp, precip) and CMC SWE

    # obs_data_path = Path("/RESCUE/skynet3_rech1/huziy/obs_data_for_HLES/interploated_to_the_same_grid/GL_0.1_452x260/anusplin+_interpolated_tt_pr.nc")
    obs_data_path = Path("/HOME/huziy/skynet3_rech1/obs_data/mh_churchill_nelson_obs_fields")
    CRU_PRECIP = True

    sim_id = "mh_0.44"
    add_shp_files = [
        default_domains.MH_BASINS_PATH,
        constants.upstream_station_boundaries_shp_path[sim_id]
    ]


    start_year = 1981
    end_year = 2009

    MODEL_LABEL =  "CRCM5 (0.44)"
    # critical p-value for the ttest aka significance level
    # p_crit = 0.05
    p_crit = 1

    coastlines_width = 0.3

    vars_of_interest_default = [
        # T_AIR_2M,
        TOTAL_PREC,
        # SWE,
        # LAKE_ICE_FRACTION
    ]

    if vars_of_interest is None:
        vars_of_interest = vars_of_interest_default


    vname_to_seasonmonths_map = {
        SWE: OrderedDict([("DJF", [12, 1, 2])]),
        T_AIR_2M: season_to_months,
        TOTAL_PREC: OrderedDict([("Annual", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])]) # season_to_months,

    }

    sim_configs = {

        MODEL_LABEL: RunConfig(data_path="/RECH2/huziy/BC-MH/bc_mh_044deg/Samples",
                  start_year=start_year, end_year=end_year, label=MODEL_LABEL),

    }


    grid_config = default_domains.bc_mh_044




    sim_labels = [MODEL_LABEL, ]

    vname_to_level = {
        T_AIR_2M: VerticalLevel(1, level_kinds.HYBRID),
        U_WE: VerticalLevel(1, level_kinds.HYBRID),
        V_SN: VerticalLevel(1, level_kinds.HYBRID),
        SWE: VerticalLevel(-1, level_kinds.ARBITRARY)
    }

    vname_map = {
        default_varname_mappings.TOTAL_PREC: "pre",
        default_varname_mappings.T_AIR_2M: "tmp",
        default_varname_mappings.SWE: "SWE"
    }

    filename_prefix_mapping = {
        default_varname_mappings.SWE: "pm",
        default_varname_mappings.TOTAL_PREC: "pm",
        default_varname_mappings.T_AIR_2M: "dm"
    }


    # Try to get the land_fraction for masking if necessary
    land_fraction = None
    try:
        land_fraction = get_land_fraction(sim_configs[MODEL_LABEL])
    except Exception:
        pass



    # Calculations

    # prepare params for interpolation
    lons_t, lats_t, bsmap = get_target_lons_lats_basemap(sim_configs[MODEL_LABEL])

    bsmap, reg_of_interest_mask = grid_config.get_basemap_using_shape_with_polygons_of_interest(lons=lons_t, lats=lats_t,
                                                                                                shp_path=default_domains.MH_BASINS_PATH,
                                                                                                mask_margin=2, resolution="i")

    xt, yt, zt = lat_lon.lon_lat_to_cartesian(lons_t.flatten(), lats_t.flatten())










    obs_multipliers = default_varname_mappings.vname_to_multiplier_CRCM5.copy()

    # Read and calculate observed seasonal means
    store_config = {
            "base_folder": obs_data_path.parent if not obs_data_path.is_dir() else obs_data_path,
            "data_source_type": data_source_types.ALL_VARS_IN_A_FOLDER_IN_NETCDF_FILES_OPEN_EACH_FILE_SEPARATELY,
            "varname_mapping": vname_map,
            "level_mapping": vname_to_level,
            "offset_mapping": default_varname_mappings.vname_to_offset_CRCM5,
            "multiplier_mapping": obs_multipliers,
    }

    obs_dm = DataManager(store_config=store_config)
    obs_data = {}


    # need to save it for ttesting
    obs_vname_to_season_to_std = {}
    obs_vname_to_season_to_nobs = {}

    interp_indices = None
    for vname in vars_of_interest:
        # --
        end_year_for_current_var = end_year
        if vname == SWE:
            end_year_for_current_var = min(1996, end_year)

        # --
        seas_to_year_to_mean = obs_dm.get_seasonal_means(varname_internal=vname,
                                                     start_year=start_year,
                                                     end_year=end_year_for_current_var,
                                                     season_to_months=vname_to_seasonmonths_map[vname])





        seas_to_clim = {seas: np.array(list(y_to_means.values())).mean(axis=0) for seas, y_to_means in seas_to_year_to_mean.items()}

        # convert precip from mm/month (CRU) to mm/day
        if vname in [TOTAL_PREC] and CRU_PRECIP:
            for seas in seas_to_clim:
                seas_to_clim[seas] *= 1. / (365.25 / 12)
                seas_to_clim[seas] = np.ma.masked_where(np.isnan(seas_to_clim[seas]), seas_to_clim[seas])


                print("{}: min={}, max={}".format(seas, seas_to_clim[seas].min(), seas_to_clim[seas].max()))


        obs_data[vname] = seas_to_clim

        if interp_indices is None:
            _, interp_indices = obs_dm.get_kdtree().query(list(zip(xt, yt, zt)))




        # need for ttests
        season_to_std = {}
        obs_vname_to_season_to_std[vname] = season_to_std

        season_to_nobs = {}
        obs_vname_to_season_to_nobs[vname] = season_to_nobs

        for season in seas_to_clim:
            seas_to_clim[season] = seas_to_clim[season].flatten()[interp_indices].reshape(lons_t.shape)



            # save the yearly means for ttesting
            season_to_std[season] = np.asarray([field.flatten()[interp_indices].reshape(lons_t.shape)
                                                         for field in seas_to_year_to_mean[season].values()]).std(axis=0)


            season_to_nobs[season] = np.ones_like(lons_t) * len(seas_to_year_to_mean[season])


        plt.show()



    # Read and calculate simulated seasonal mean biases
    mod_label_to_vname_to_season_to_std = {}
    mod_label_to_vname_to_season_to_nobs = {}

    model_data_multipliers = defaultdict(lambda: 1)
    model_data_multipliers[TOTAL_PREC] = 1000 * 24 * 3600

    sim_data = defaultdict(dict)
    for label, r_config in sim_configs.items():

        store_config = {
                "base_folder": r_config.data_path,
                "data_source_type": data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT,
                "varname_mapping": default_varname_mappings.vname_map_CRCM5,
                "level_mapping": vname_to_level,
                "offset_mapping": default_varname_mappings.vname_to_offset_CRCM5,
                "multiplier_mapping": model_data_multipliers,
                "filename_prefix_mapping": filename_prefix_mapping
        }


        dm = DataManager(store_config=store_config)

        mod_label_to_vname_to_season_to_std[label] = {}
        mod_label_to_vname_to_season_to_nobs[label] = {}


        interp_indices = None
        for vname in vars_of_interest:

            # --
            end_year_for_current_var = end_year
            if vname == SWE:
                end_year_for_current_var = min(1996, end_year)

            # --
            seas_to_year_to_mean = dm.get_seasonal_means(varname_internal=vname,
                                                         start_year=start_year,
                                                         end_year=end_year_for_current_var,
                                                         season_to_months=vname_to_seasonmonths_map[vname])


            # get the climatology
            seas_to_clim = {seas: np.array(list(y_to_means.values())).mean(axis=0) for seas, y_to_means in seas_to_year_to_mean.items()}

            sim_data[label][vname] = seas_to_clim



            if interp_indices is None:
                _, interp_indices = dm.get_kdtree().query(list(zip(xt, yt, zt)))


            season_to_std = {}
            mod_label_to_vname_to_season_to_std[label][vname] = season_to_std

            season_to_nobs = {}
            mod_label_to_vname_to_season_to_nobs[label][vname] = season_to_nobs

            for season in seas_to_clim:
                interpolated_field = seas_to_clim[season].flatten()[interp_indices].reshape(lons_t.shape)
                seas_to_clim[season] = interpolated_field - obs_data[vname][season]

                # calculate standard deviations of the interpolated fields
                season_to_std[season] = np.asarray([field.flatten()[interp_indices].reshape(lons_t.shape) for field in seas_to_year_to_mean[season].values()]).std(axis=0)

                # calculate numobs for the ttest
                season_to_nobs[season] = np.ones_like(lons_t) * len(seas_to_year_to_mean[season])






    xx, yy = bsmap(lons_t, lats_t)
    lons_t[lons_t > 180] -= 360

    field_mask = maskoceans(lons_t, lats_t, np.zeros_like(lons_t)).mask


    for vname in vars_of_interest:

        if vname not in [SWE]:
            field_mask = np.zeros_like(field_mask, dtype=bool)


        # Plotting: interpolate to the same grid and plot obs and biases
        plot_utils.apply_plot_params(width_cm=32 / 4 * (len(vname_to_seasonmonths_map[vname])),
                                     height_cm=25 / 3.0 * (len(sim_configs) + 1), font_size=8 * len(vname_to_seasonmonths_map[vname]))

        fig = plt.figure()

        # fig.suptitle(internal_name_to_title[vname] + "\n")

        nrows = len(sim_configs) + 2
        ncols = len(vname_to_seasonmonths_map[vname])
        gs = GridSpec(nrows=nrows, ncols=ncols)



        # Plot the obs fields
        current_row = 0
        for col, season in enumerate(vname_to_seasonmonths_map[vname]):
            field = obs_data[vname][season]
            ax = fig.add_subplot(gs[current_row, col])
            ax.set_title(season)

            to_plot = np.ma.masked_where(field_mask, field) * internal_name_to_multiplier[vname]
            clevs = get_clevs(vname)

            to_plot = np.ma.masked_where(~reg_of_interest_mask, to_plot)

            if clevs is not None:
                bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                cmap = cm.get_cmap("Blues", len(clevs) - 1)
            else:
                cmap = "jet"
                bnorm = None

            bsmap.drawmapboundary(fill_color="0.75")

            # cs = bsmap.contourf(xx, yy, to_plot, ax=ax, levels=get_clevs(vname), norm=bnorm, cmap=cmap)
            cs = bsmap.pcolormesh(xx, yy, to_plot, ax=ax, norm=bnorm, cmap=internal_name_to_cmap[vname])

            bsmap.drawcoastlines(linewidth=coastlines_width)
            # bsmap.drawstates(linewidth=0.1)
            # bsmap.drawcountries(linewidth=0.2)
            bsmap.colorbar(cs, ax=ax)

            i = 0
            bsmap.readshapefile(str(add_shp_files[i])[:-4], "field_{}".format(i), linewidth=0.5, color="m")


            if col == 0:
                ax.set_ylabel("Obs")



        # plot the biases
        for sim_label in sim_labels:
            current_row += 1
            for col, season in enumerate(vname_to_seasonmonths_map[vname]):

                field = sim_data[sim_label][vname][season]

                ax = fig.add_subplot(gs[current_row, col])

                clevs = get_clevs(vname + "bias")
                if clevs is not None:
                    bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                    cmap = cm.get_cmap("bwr", len(clevs) - 1)
                else:
                    cmap = "bwr"
                    bnorm = None

                to_plot = np.ma.masked_where(field_mask, field) * internal_name_to_multiplier[vname]


                # ttest
                a = sim_data[sim_label][vname][season] + obs_data[vname][season]  # Calculate the simulation data back from biases
                std_a = mod_label_to_vname_to_season_to_std[sim_label][vname][season]
                nobs_a = mod_label_to_vname_to_season_to_nobs[sim_label][vname][season]

                b = obs_data[vname][season]
                std_b =  obs_vname_to_season_to_std[vname][season]
                nobs_b = obs_vname_to_season_to_nobs[vname][season]



                t, p = ttest_ind_from_stats(mean1=a, std1=std_a, nobs1=nobs_a,
                                            mean2=b, std2=std_b, nobs2=nobs_b, equal_var=False)

                # Mask non-significant differences as given by the ttest
                to_plot = np.ma.masked_where(p > p_crit, to_plot)

                # only focus on the basins of interest
                to_plot = np.ma.masked_where(~reg_of_interest_mask, to_plot)


                # cs = bsmap.contourf(xx, yy, to_plot, ax=ax, extend="both", levels=get_clevs(vname + "bias"), cmap=cmap, norm=bnorm)

                bsmap.drawmapboundary(fill_color="0.75")


                cs = bsmap.pcolormesh(xx, yy, to_plot, ax=ax, cmap=cmap, norm=bnorm)
                bsmap.drawcoastlines(linewidth=coastlines_width)
                bsmap.colorbar(cs, ax=ax, extend="both")





                for i, shp in enumerate(add_shp_files[1:], start=1):
                    bsmap.readshapefile(str(shp)[:-4], "field_{}".format(i), linewidth=0.5, color="k")

                if col == 0:
                    ax.set_ylabel("{}\n-\nObs.".format(sim_label))




        fig.tight_layout()



        # save a figure per variable
        img_file = "seasonal_biases_{}_{}_{}-{}.png".format(vname,
                                                            "-".join([s for s in vname_to_seasonmonths_map[vname]]),
                                                            start_year, end_year)


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

        img_file = img_folder / img_file
        fig.savefig(str(img_file), bbox_inches="tight", dpi=300)

        plt.close(fig)
Example #26
0
def main():
    season_to_months = DEFAULT_SEASON_TO_MONTHS

    r_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
        start_year=1980,
        end_year=2010,
        label="CRCM5-L")

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=r_config.data_path)

    # Validate temperature and precip
    model_vars = ["TT", "PR"]

    # Get Anusplin data managers
    obs_path = "/home/huziy/skynet3_rech1/anusplin_links"
    pcp_obs_manager = AnuSplinManager(variable="pcp", folder_path=obs_path)
    tmax_obs_manager = AnuSplinManager(variable="stmx", folder_path=obs_path)
    tmin_obs_manager = AnuSplinManager(variable="stmn", folder_path=obs_path)

    vname_to_obs_data = {}

    for vname in model_vars:
        if vname == "TT":
            dates, vals_max = tmax_obs_manager.get_daily_clim_fields_interpolated_to(
                start_year=r_config.start_year,
                end_year=r_config.end_year,
                lons_target=bmp_info.lons,
                lats_target=bmp_info.lats)

            _, vals_min = tmin_obs_manager.get_daily_clim_fields_interpolated_to(
                start_year=r_config.start_year,
                end_year=r_config.end_year,
                lons_target=bmp_info.lons,
                lats_target=bmp_info.lats)

            daily_obs = (dates, (vals_min + vals_max) * 0.5)
        elif vname == "PR":
            daily_obs = pcp_obs_manager.get_daily_clim_fields_interpolated_to(
                start_year=r_config.start_year,
                end_year=r_config.end_year,
                lons_target=bmp_info.lons,
                lats_target=bmp_info.lats)
        else:
            raise Exception("Unknown variable: {}".format(vname))

        season_to_obs_data = OrderedDict()
        for season, months in season_to_months.items():
            season_to_obs_data[season] = np.mean(
                [f for d, f in zip(*daily_obs) if d.month in months], axis=0)

        vname_to_obs_data[vname] = season_to_obs_data

    plot_all_vars_in_one_fig = True

    fig = None
    gs = None
    row_axes = None
    ncols = None
    if plot_all_vars_in_one_fig:
        plot_utils.apply_plot_params(font_size=12,
                                     width_pt=None,
                                     width_cm=25,
                                     height_cm=12)
        fig = plt.figure()
        ncols = len(season_to_months) + 1
        gs = GridSpec(len(model_vars),
                      ncols,
                      width_ratios=(ncols - 1) * [
                          1.,
                      ] + [
                          0.05,
                      ])
    else:
        plot_utils.apply_plot_params(font_size=12,
                                     width_pt=None,
                                     width_cm=25,
                                     height_cm=25)

    row = 0
    for mname in model_vars:

        if plot_all_vars_in_one_fig:
            row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols)]

        compare_vars(vname_model=mname,
                     vname_to_obs=vname_to_obs_data,
                     r_config=r_config,
                     season_to_months=season_to_months,
                     bmp_info_agg=bmp_info,
                     axes_list=row_axes)

        row += 1

    # Save the figure if necessary
    if plot_all_vars_in_one_fig:
        fig_path = img_folder.joinpath("{}.png".format("_".join(model_vars)))
        with fig_path.open("wb") as figfile:
            fig.savefig(figfile, format="png", bbox_inches="tight")

        plt.close(fig)
Example #27
0
def main():
    """

    """

    season_to_months = get_default_season_to_months_dict()

    # season_to_months = OrderedDict([("April", [4, ]), ("May", [5, ]), ("June", [6, ]), ("July", [7, ])])

    var_names = ["TT", "HU", "PR", "AV", "AH", "STFL", "TRAF", "I5", "I0"]

    # var_names = ["TT", "PR"]

    levels = [
        0,
    ] * len(var_names)
    multipliers = {"PR": 1.0e3 * 24.0 * 3600., "TRAF": 24 * 3600.0}

    base_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
                        "quebec_0.1_crcm5-r-cc-canesm2-1980-2010.hdf5"
    base_label = "CanESM2-CRCM5-NL"

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

    # base_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
    # "quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"
    # base_label = "CRCM5-L"
    #
    # modif_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
    # "quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5"
    # modif_label = "CRCM5-LI"

    start_year_c = 1980
    end_year_c = 2010

    future_shift_years = 90

    params = dict(data_path=base_current_path,
                  start_year=start_year_c,
                  end_year=end_year_c,
                  label=base_label)

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    params.update(dict(data_path=modif_current_path, label=modif_label))

    modif_config_c = RunConfig(**params)
    modif_config_f = modif_config_c.get_shifted_config(future_shift_years)

    config_dict = OrderedDict([("Current",
                                OrderedDict([(base_label, base_config_c),
                                             (modif_label, modif_config_c)])),
                               ("Future",
                                OrderedDict([(base_label, base_config_f),
                                             (modif_label, modif_config_f)]))])

    # Plot the differences
    config_dict.label_modif = modif_config_c.label
    config_dict.label_base = base_config_c.label
    config_dict.season_to_months = season_to_months
    config_dict.multipliers = multipliers

    lons, lats, bmp = analysis.get_basemap_from_hdf(base_current_path)
    config_dict.lons = lons
    config_dict.lats = lats
    config_dict.basemap = bmp

    # Calculate and plot seasonal means
    for vname, level in zip(var_names, levels):
        data = get_data(vname=vname, level=level, config_dict=config_dict)
        _plot_var(vname=vname,
                  level=level,
                  config_dict=config_dict,
                  data_dict=data)
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)
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()
def main():
    start_year = 1980
    end_year = 2009

    HL_LABEL = "CRCM5_HL"
    NEMO_LABEL = "CRCM5_NEMO"

    # critical p-value for the ttest aka significance level
    p_crit = 1

    vars_of_interest = [
        # T_AIR_2M,
        # TOTAL_PREC,
        # SWE,
        default_varname_mappings.LATENT_HF,
        default_varname_mappings.SENSIBLE_HF,
        default_varname_mappings.LWRAD_DOWN,
        default_varname_mappings.SWRAD_DOWN
        #       LAKE_ICE_FRACTION
    ]

    coastline_width = 0.3

    vname_to_seasonmonths_map = {
        SWE: OrderedDict([("November", [11]),
                          ("December", [12]),
                          ("January", [1, ])]),
        LAKE_ICE_FRACTION: OrderedDict([
            ("December", [12]),
            ("January", [1, ]),
            ("February", [2, ]),
            ("March", [3, ]),
            ("April", [4, ])]),
        T_AIR_2M: season_to_months,
        TOTAL_PREC: season_to_months,
    }


    # set season to months mappings
    for vname in vars_of_interest:
        if vname not in vname_to_seasonmonths_map:
            vname_to_seasonmonths_map[vname] = season_to_months


    sim_configs = {
        HL_LABEL: RunConfig(data_path="/RECH2/huziy/coupling/GL_440x260_0.1deg_GL_with_Hostetler/Samples_selected",
                            start_year=start_year, end_year=end_year, label=HL_LABEL),

        NEMO_LABEL: RunConfig(data_path="/RECH2/huziy/coupling/coupled-GL-NEMO1h_30min/selected_fields",
                              start_year=start_year, end_year=end_year, label=NEMO_LABEL),
    }

    sim_labels = [HL_LABEL, NEMO_LABEL]

    vname_to_level = {
        T_AIR_2M: VerticalLevel(1, level_kinds.HYBRID),
        U_WE: VerticalLevel(1, level_kinds.HYBRID),
        V_SN: VerticalLevel(1, level_kinds.HYBRID),
        default_varname_mappings.LATENT_HF: VerticalLevel(5, level_kinds.ARBITRARY),
        default_varname_mappings.SENSIBLE_HF: VerticalLevel(5, level_kinds.ARBITRARY),
    }

    # Try to get the land_fraction for masking if necessary
    land_fraction = None
    try:
        first_ts_file = Path(sim_configs[HL_LABEL].data_path).parent / "pm1979010100_00000000p"

        land_fraction = get_land_fraction(first_timestep_file=first_ts_file)
    except Exception as err:
        raise err
        pass

    # Calculations

    # prepare params for interpolation
    lons_t, lats_t, bsmap = get_target_lons_lats_basemap(sim_configs[HL_LABEL])

    # get a subdomain of the simulation domain
    nx, ny = lons_t.shape
    iss = IndexSubspace(i_start=20, j_start=10, i_end=nx // 1.5, j_end=ny / 1.8)
    # just to change basemap limits
    lons_t, lats_t, bsmap = get_target_lons_lats_basemap(sim_configs[HL_LABEL], sub_space=iss)

    xt, yt, zt = lat_lon.lon_lat_to_cartesian(lons_t.flatten(), lats_t.flatten())

    vname_map = {}
    vname_map.update(default_varname_mappings.vname_map_CRCM5)

    # Read and calculate simulated seasonal means
    mod_label_to_vname_to_season_to_std = {}
    mod_label_to_vname_to_season_to_nobs = {}

    sim_data = defaultdict(dict)
    for label, r_config in sim_configs.items():

        store_config = {
            "base_folder": r_config.data_path,
            "data_source_type": data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT_VNAME_IN_FNAME,
            "varname_mapping": vname_map,
            "level_mapping": vname_to_level,
            "offset_mapping": default_varname_mappings.vname_to_offset_CRCM5,
            "multiplier_mapping": default_varname_mappings.vname_to_multiplier_CRCM5,
        }

        dm = DataManager(store_config=store_config)

        mod_label_to_vname_to_season_to_std[label] = {}
        mod_label_to_vname_to_season_to_nobs[label] = {}

        interp_indices = None
        for vname in vars_of_interest:

            # --
            end_year_for_current_var = end_year
            if vname == SWE:
                end_year_for_current_var = min(1996, end_year)

            # --
            seas_to_year_to_mean = dm.get_seasonal_means(varname_internal=vname,
                                                         start_year=start_year,
                                                         end_year=end_year_for_current_var,
                                                         season_to_months=vname_to_seasonmonths_map[vname])

            # get the climatology
            seas_to_clim = {seas: np.array(list(y_to_means.values())).mean(axis=0) for seas, y_to_means in
                            seas_to_year_to_mean.items()}

            sim_data[label][vname] = seas_to_clim

            if interp_indices is None:
                _, interp_indices = dm.get_kdtree().query(list(zip(xt, yt, zt)))

            season_to_std = {}
            mod_label_to_vname_to_season_to_std[label][vname] = season_to_std

            season_to_nobs = {}
            mod_label_to_vname_to_season_to_nobs[label][vname] = season_to_nobs

            for season in seas_to_clim:
                interpolated_field = seas_to_clim[season].flatten()[interp_indices].reshape(lons_t.shape)
                seas_to_clim[season] = interpolated_field

                # calculate standard deviations of the interpolated fields
                season_to_std[season] = np.asarray([field.flatten()[interp_indices].reshape(lons_t.shape) for field in
                                                    seas_to_year_to_mean[season].values()]).std(axis=0)

                # calculate numobs for the ttest
                season_to_nobs[season] = np.ones_like(lons_t) * len(seas_to_year_to_mean[season])



    # Plotting: interpolate to the same grid and plot obs and biases
    xx, yy = bsmap(lons_t, lats_t)
    lons_t[lons_t > 180] -= 360


    for vname in vars_of_interest:

        field_mask = maskoceans(lons_t, lats_t, np.zeros_like(lons_t), inlands=vname in [SWE]).mask
        field_mask_lakes = maskoceans(lons_t, lats_t, np.zeros_like(lons_t), inlands=True).mask

        plot_utils.apply_plot_params(width_cm=11 * len(vname_to_seasonmonths_map[vname]), height_cm=20, font_size=8)

        fig = plt.figure()



        nrows = len(sim_configs) + 1
        ncols = len(vname_to_seasonmonths_map[vname])
        gs = GridSpec(nrows=nrows, ncols=ncols)




        # plot the fields
        for current_row, sim_label in enumerate(sim_labels):
            for col, season in enumerate(vname_to_seasonmonths_map[vname]):

                field = sim_data[sim_label][vname][season]

                ax = fig.add_subplot(gs[current_row, col])

                if current_row == 0:
                    ax.set_title(season)

                clevs = get_clevs(vname)
                if clevs is not None:
                    bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                    cmap = cm.get_cmap("viridis", len(clevs) - 1)
                else:
                    cmap = "viridis"
                    bnorm = None

                the_mask = field_mask_lakes if vname in [T_AIR_2M, TOTAL_PREC, SWE] else field_mask
                to_plot = np.ma.masked_where(the_mask, field) * internal_name_to_multiplier[vname]



                # temporary plot the actual values
                cs = bsmap.contourf(xx, yy, to_plot, ax=ax, levels=get_clevs(vname), cmap=cmap, norm=bnorm, extend="both")
                bsmap.drawcoastlines(linewidth=coastline_width)
                bsmap.colorbar(cs, ax=ax)

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





        # plot differences between the fields
        for col, season in enumerate(vname_to_seasonmonths_map[vname]):

            field = sim_data[NEMO_LABEL][vname][season] - sim_data[HL_LABEL][vname][season]

            ax = fig.add_subplot(gs[-1, col])

            clevs = get_clevs(vname + "biasdiff")
            if clevs is not None:
                bnorm = BoundaryNorm(clevs, len(clevs) - 1)
                cmap = cm.get_cmap("bwr", len(clevs) - 1)
            else:
                cmap = "bwr"
                bnorm = None


            to_plot = field * internal_name_to_multiplier[vname]
            # to_plot = np.ma.masked_where(field_mask, field) * internal_name_to_multiplier[vname]



            # ttest
            a = sim_data[NEMO_LABEL][vname][season]  # Calculate the simulation data back from biases
            std_a = mod_label_to_vname_to_season_to_std[NEMO_LABEL][vname][season]
            nobs_a = mod_label_to_vname_to_season_to_nobs[NEMO_LABEL][vname][season]

            b = sim_data[HL_LABEL][vname][season]  # Calculate the simulation data back from biases
            std_b = mod_label_to_vname_to_season_to_std[HL_LABEL][vname][season]
            nobs_b = mod_label_to_vname_to_season_to_nobs[HL_LABEL][vname][season]


            t, p = ttest_ind_from_stats(mean1=a, std1=std_a, nobs1=nobs_a,
                                        mean2=b, std2=std_b, nobs2=nobs_b, equal_var=False)

            # Mask non-significant differences as given by the ttest
            to_plot = np.ma.masked_where(p > p_crit, to_plot)


            # mask the points with not sufficient land fraction
            if land_fraction is not None and vname in [SWE, ]:
                to_plot = np.ma.masked_where(land_fraction < 0.05, to_plot)


            # print("land fractions for large differences ", land_fraction[to_plot > 30])


            cs = bsmap.contourf(xx, yy, to_plot, ax=ax, extend="both", levels=get_clevs(vname + "biasdiff"), cmap=cmap, norm=bnorm)
            bsmap.drawcoastlines(linewidth=coastline_width)
            bsmap.colorbar(cs, ax=ax)

            if col == 0:
                ax.set_ylabel("{}\n-\n{}".format(NEMO_LABEL, HL_LABEL))


        fig.tight_layout()

        # save a figure per variable
        img_file = "seasonal_differences_noobs_{}_{}_{}-{}.png".format(vname,
                                                            "-".join([s for s in vname_to_seasonmonths_map[vname]]),
                                                            start_year, end_year)
        img_file = img_folder.joinpath(img_file)

        fig.savefig(str(img_file), dpi=300)

        plt.close(fig)
Example #31
0
def get_seasonal_sst_from_crcm5_outputs(sim_label,
                                        start_year=1980,
                                        end_year=2010,
                                        season_to_months=None,
                                        lons_target=None,
                                        lats_target=None):

    from lake_effect_snow.default_varname_mappings import T_AIR_2M
    from lake_effect_snow.default_varname_mappings import U_WE
    from lake_effect_snow.default_varname_mappings import V_SN
    from lake_effect_snow.base_utils import VerticalLevel
    from rpn import level_kinds
    from lake_effect_snow import default_varname_mappings
    from data.robust import data_source_types

    from data.robust.data_manager import DataManager

    sim_configs = {
        sim_label:
        RunConfig(
            data_path=
            "/RECH2/huziy/coupling/GL_440x260_0.1deg_GL_with_Hostetler/Samples_selected",
            start_year=start_year,
            end_year=end_year,
            label=sim_label),
    }

    r_config = sim_configs[sim_label]

    vname_to_level = {
        T_AIR_2M:
        VerticalLevel(1, level_kinds.HYBRID),
        U_WE:
        VerticalLevel(1, level_kinds.HYBRID),
        V_SN:
        VerticalLevel(1, level_kinds.HYBRID),
        default_varname_mappings.LAKE_WATER_TEMP:
        VerticalLevel(1, level_kinds.ARBITRARY)
    }

    vname_map = {}

    vname_map.update(default_varname_mappings.vname_map_CRCM5)

    store_config = {
        "base_folder": r_config.data_path,
        "data_source_type":
        data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT_VNAME_IN_FNAME,
        "varname_mapping": vname_map,
        "level_mapping": vname_to_level,
        "offset_mapping": default_varname_mappings.vname_to_offset_CRCM5,
        "multiplier_mapping":
        default_varname_mappings.vname_to_multiplier_CRCM5,
    }

    dm = DataManager(store_config=store_config)

    season_to_year_to_mean = dm.get_seasonal_means(
        start_year=start_year,
        end_year=end_year,
        season_to_months=season_to_months,
        varname_internal=default_varname_mappings.LAKE_WATER_TEMP)

    result = {}

    # fill in the result dictionary with seasonal means
    for season in season_to_months:
        result[season] = np.array([
            field for field in season_to_year_to_mean[season].values()
        ]).mean(axis=0)

    # interpolate the data
    if lons_target is not None:
        xt, yt, zt = lat_lon.lon_lat_to_cartesian(lons_target.flatten(),
                                                  lats_target.flatten())

        dists, inds = dm.get_kdtree().query(list(zip(xt, yt, zt)))
        for season in season_to_months:
            result[season] = result[season].flatten()[inds].reshape(
                lons_target.shape)

    return result
Example #32
0
def main():
    # Define the simulations to be validated
    r_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-r.hdf5",
        start_year=1990,
        end_year=2010,
        label="CRCM5-L1")
    r_config_list = [r_config]

    r_config = RunConfig(
        data_path=
        "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-r.hdf5",
        start_year=1990,
        end_year=2010,
        label="CRCM5-NL")
    r_config_list.append(r_config)

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=r_config.data_path)
    bmp_info.should_draw_grey_map_background = True
    bmp_info.should_draw_basin_boundaries = False
    bmp_info.map_bg_color = "0.75"

    station_ids = ["104001", "093806", "093801", "081002", "081007", "080718"]

    # get river network information used in the model
    flow_directions = analysis.get_array_from_file(
        r_config.data_path, var_name=infovar.HDF_FLOW_DIRECTIONS_NAME)
    accumulation_area_km2 = analysis.get_array_from_file(
        path=r_config.data_path, var_name=infovar.HDF_ACCUMULATION_AREA_NAME)
    cell_manager = CellManager(flow_dirs=flow_directions,
                               lons2d=bmp_info.lons,
                               lats2d=bmp_info.lats,
                               accumulation_area_km2=accumulation_area_km2)

    # Get the list of stations to indicate on the bias map
    stations = cehq_station.read_station_data(start_date=None,
                                              end_date=None,
                                              selected_ids=station_ids)
    """:type : list[Station]"""

    xx, yy = bmp_info.get_proj_xy()
    station_to_modelpoint = cell_manager.get_model_points_for_stations(
        station_list=stations)
    upstream_edges = cell_manager.get_upstream_polygons_for_points(
        model_point_list=station_to_modelpoint.values(), xx=xx, yy=yy)

    bmp_info.draw_colorbar_for_each_subplot = True

    # Validate temperature, precip and swe
    obs_path_anusplin = "/home/huziy/skynet3_rech1/anusplin_links"
    obs_path_swe = "data/swe_ross_brown/swe.nc"
    model_var_to_obs_path = OrderedDict([("TT", obs_path_anusplin),
                                         ("I5", obs_path_swe)])

    model_var_to_season = OrderedDict([
        ("TT", OrderedDict([("Spring", range(3, 6))])),
        ("I5", OrderedDict([("Winter", [1, 2, 12])]))
    ])

    vname_to_obs_data = {}

    # parameters that won't change in the loop over variable names
    params_const = dict(rconfig=r_config, bmp_info=bmp_info)

    for vname, obs_path in model_var_to_obs_path.items():
        season_to_obs_data = get_seasonal_clim_obs_data(
            vname=vname,
            obs_path=obs_path,
            season_to_months=model_var_to_season[vname],
            **params_const)

        # Comment swe over lakes, since I5 calculated only for land
        if vname in [
                "I5",
        ]:
            for season in season_to_obs_data:
                season_to_obs_data[season] = maskoceans(
                    bmp_info.lons,
                    bmp_info.lats,
                    season_to_obs_data[season],
                    inlands=True)

        vname_to_obs_data[vname] = season_to_obs_data

    # Plotting
    plot_all_vars_in_one_fig = True

    fig = None
    gs = None
    if plot_all_vars_in_one_fig:
        plot_utils.apply_plot_params(font_size=12,
                                     width_pt=None,
                                     width_cm=25,
                                     height_cm=20)
        fig = plt.figure()
        ncols = len(model_var_to_obs_path) + 1
        gs = GridSpec(len(r_config_list),
                      ncols,
                      width_ratios=(ncols - 1) * [
                          1.,
                      ] + [
                          0.05,
                      ])
    else:
        plot_utils.apply_plot_params(font_size=12,
                                     width_pt=None,
                                     width_cm=25,
                                     height_cm=25)

    station_x_list = []
    station_y_list = []

    mvarname_to_cs = {}
    for row, r_config in enumerate(r_config_list):
        for col, mname in enumerate(model_var_to_obs_path):

            row_axes = [
                fig.add_subplot(gs[row, col]),
            ]

            mvarname_to_cs[mname] = compare_vars(
                vname_model=mname,
                vname_to_obs=vname_to_obs_data,
                r_config=r_config,
                season_to_months=model_var_to_season[mname],
                bmp_info_agg=bmp_info,
                axes_list=row_axes)

            # -1 in order to exclude colorbars
            for the_ax in row_axes:

                the_ax.set_title(the_ax.get_title() + ", {}".format(
                    infovar.get_long_display_label_for_var(mname)))
                # Need titles only for the first row
                if row > 0:
                    the_ax.set_title("")

                if col == 0:
                    the_ax.set_ylabel(r_config.label)
                else:
                    the_ax.set_ylabel("")

                draw_upstream_area_bounds(the_ax, upstream_edges, color="g")

                if len(station_x_list) == 0:
                    for the_station in stations:
                        xst, yst = bmp_info.basemap(the_station.longitude,
                                                    the_station.latitude)
                        station_x_list.append(xst)
                        station_y_list.append(yst)

                bmp_info.basemap.scatter(station_x_list,
                                         station_y_list,
                                         c="g",
                                         ax=the_ax,
                                         s=20,
                                         zorder=10,
                                         alpha=0.5)

    # Save the figure if necessary
    if plot_all_vars_in_one_fig:

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

        fig_path = img_folder.joinpath("{}.png".format(
            "_".join(model_var_to_obs_path)))
        with fig_path.open("wb") as figfile:
            fig.savefig(figfile, format="png", bbox_inches="tight")

        plt.close(fig)
Example #33
0
def main():
    start_year_c = 1980
    end_year_c = 2010

    img_folder = "cc_paper"

    current_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-intfl-cc-canesm2-1980-2010.hdf5"
    base_label = "CRCM5-LI"

    # Need to read land fraction
    geo_file_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/pm1979010100_00000000p"

    r_obj = RPN(geo_file_path)
    mg_field = r_obj.get_first_record_for_name("MG")

    lons, lats = r_obj.get_longitudes_and_latitudes_for_the_last_read_rec()
    r_obj.close()

    future_shift_years = 90

    params = dict(
        data_path=current_path,
        start_year=start_year_c, end_year=end_year_c,
        label=base_label
    )

    base_config_c = RunConfig(**params)
    base_config_f = base_config_c.get_shifted_config(future_shift_years)

    varname = "INTF"
    level = 0
    daily_dates, intf_c = analysis.get_daily_climatology(path_to_hdf_file=base_config_c.data_path,
                                                         var_name=varname, level=level,
                                                         start_year=base_config_c.start_year,
                                                         end_year=base_config_c.end_year)

    _, intf_f = analysis.get_daily_climatology(path_to_hdf_file=base_config_f.data_path,
                                               var_name=varname, level=level,
                                               start_year=base_config_f.start_year,
                                               end_year=base_config_f.end_year)


    mg_fields = np.asarray([mg_field for d in daily_dates])

    mg_crit = 0.0001
    the_mask = mg_fields <= mg_crit
    # Convert to mm/day as well
    intf_c = _avg_along_lon(intf_c, the_mask) * 24 * 3600
    intf_f = _avg_along_lon(intf_f, the_mask) * 24 * 3600



    lats_agg = lats.mean(axis=0)
    num_dates = date2num(daily_dates)

    lats_agg_2d, num_dates_2d = np.meshgrid(lats_agg, num_dates)


    # Do the plotting
    fig = plt.figure()

    gs = GridSpec(2, 3, width_ratios=[1, 1, 0.05])
    norm = SymLogNorm(5e-5)

    all_axes = []
    # Current
    ax = fig.add_subplot(gs[0, 0])
    cs = ax.contourf(num_dates_2d, lats_agg_2d, intf_c[:], 60, norm=norm)
    ax.set_title("Current ({}-{})".format(
        base_config_c.start_year, base_config_c.end_year))
    all_axes.append(ax)

    # Future
    ax = fig.add_subplot(gs[0, 1])
    ax.set_title("Future ({}-{})".format(
        base_config_f.start_year, base_config_f.end_year))
    cs = ax.contourf(num_dates_2d, lats_agg_2d, intf_f[:], levels=cs.levels, norm=norm)
    all_axes.append(ax)





    # Colorbar for value plots
    cax = fig.add_subplot(gs[0, 2])

    sfmt = ScalarFormatter(useMathText=True)
    sfmt.set_powerlimits((-1, 2))

    plt.colorbar(cs, cax=cax, format=sfmt)
    cax.set_xlabel("mm/day")
    cax.yaxis.get_offset_text().set_position((-2, 10))



    # CC
    diff_cmap = cm.get_cmap("RdBu_r", 20)
    diff = (intf_f - intf_c) / (0.5 * (intf_c + intf_f)) * 100
    diff[(intf_f == 0) & (intf_c == 0)] = 0
    print(np.min(diff), np.max(diff))
    print(np.any(diff.mask))
    print(np.any(intf_c.mask))
    print(np.any(intf_f.mask))
    delta = 200
    vmin = -delta
    vmax = delta
    locator = MaxNLocator(nbins=20, symmetric=True)
    clevs = locator.tick_values(vmin=vmin, vmax=vmax)

    ax = fig.add_subplot(gs[1, 1])

    cs = ax.contourf(num_dates_2d, lats_agg_2d, diff, cmap=diff_cmap,
                     levels=clevs, extend="both")
    ax.set_title("Future - Current")
    # ax.set_aspect("auto")
    all_axes.append(ax)
    cb = plt.colorbar(cs, cax=fig.add_subplot(gs[1, -1]))
    cb.ax.set_xlabel(r"%")


    for i, the_ax in enumerate(all_axes):
        the_ax.xaxis.set_minor_formatter(FuncFormatter(lambda d, pos: num2date(d).strftime("%b")[0]))
        the_ax.xaxis.set_major_formatter(FuncFormatter(lambda d, pos: ""))
        the_ax.xaxis.set_minor_locator(MonthLocator(bymonthday=15))
        the_ax.xaxis.set_major_locator(MonthLocator())
        the_ax.grid()
        if i != 1:
            the_ax.set_ylabel(r"Latitude ${\rm \left(^\circ N \right)}$")



    # identify approximately the melting period and lower latitudes
    march1 = date2num(datetime(daily_dates[0].year, 3, 1))
    june1 = date2num(datetime(daily_dates[0].year, 6, 1))

    sel_mask = (num_dates_2d >= march1) & (num_dates_2d < june1) & (lats_agg_2d <= 50)
    print("Mean interflow decrease in the southern regions: {}%".format(diff[sel_mask].mean()))


    # identify the regions of max interflow rates in current and future climates
    lat_min = 55
    lat_max = 57.5

    may1 = date2num(datetime(daily_dates[0].year, 5, 1))
    july1 = date2num(datetime(daily_dates[0].year, 7, 1))

    mean_max_current = intf_c[(lats_agg_2d >= lat_min) & (lats_agg_2d <= lat_max) & (num_dates_2d <= july1) & (num_dates_2d >= june1)].mean()
    mean_max_future = intf_f[(lats_agg_2d >= lat_min) & (lats_agg_2d <= lat_max) & (num_dates_2d <= june1) & (num_dates_2d >= may1)].mean()

    print("Mean change in the maximum interflow rate: {} %".format((mean_max_future - mean_max_current) * 100 / mean_max_current))

    img_file = Path(img_folder).joinpath("INTF_rate_longit_avg.png")
    fig.tight_layout()
    from crcm5.analyse_hdf import common_plot_params
    fig.savefig(str(img_file), bbox_inches="tight", transparent=True, dpi=common_plot_params.FIG_SAVE_DPI)