Esempio n. 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)
Esempio n. 2
0
def main():
    import application_properties

    application_properties.set_current_directory()

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

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

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

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

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    nyears_to_future = 75

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

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

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=with_lakes_c_path)

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

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

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

    plot_comparison_std(configs, data, varname=varname)
def 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)
Esempio n. 4
0
def main():
    import application_properties

    application_properties.set_current_directory()

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

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

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

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

    start_year_c = 1980
    end_year_c = 2010

    varname = "STFL"

    nyears_to_future = 75

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

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

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=with_lakes_c_path)


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

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

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

    plot_comparison_std(configs, data, varname=varname)
def 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)
Esempio n. 6
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()
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():
    # 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()
Esempio n. 9
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)
Esempio n. 10
0
def main():
    import application_properties
    application_properties.set_current_directory()
    if not IMG_FOLDER.exists():
        IMG_FOLDER.mkdir(parents=True)

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

    varname = "TDRA"

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

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


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

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

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

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

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


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

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

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

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

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

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


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

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

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

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

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


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




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

    plt.show()
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()
Esempio n. 12
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()