Example #1
0
def main():

    pf_mask_file = "pf_mask_qc0.1deg.220x220.nc"
    if not os.path.isfile(pf_mask_file):
        path_to_rpn_with_targ_grid = "/RESCUE/skynet3_rech1/huziy/from_guillimin/new_outputs/current_climate_30_yr_sims/quebec_0.1_crcm5-hcd-rl-intfl_ITFS/Samples/quebec_crcm5-hcd-rl-intfl_197901/pm1979010100_00008928p"
        save_pf_mask_to_netcdf(pf_mask_file, path_to_rpn_with_target_grid=path_to_rpn_with_targ_grid)


    with Dataset(pf_mask_file) as ds:
        pf_mask = ds.variables["pf_type"][:]
        print(ds.variables.keys())

    lay_widths = qc01deg_sim_layer_widths

    data_path = "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl-intfl_ITFS.hdf5"
    start_year = 1990
    end_year = 1990

    alt_field = do_calculations(data_path=data_path, lay_widths=lay_widths, start_year=start_year, end_year=end_year)

    alt_field = np.ma.masked_where((alt_field < 0) | (alt_field > 8), alt_field)


    # do the plotting
    plot_utils.apply_plot_params()
    fig = plt.figure()

    bmp_info = analysis.get_basemap_info_from_hdf(file_path=data_path)

    # Mask oceans
    alt_field = maskoceans(bmp_info.lons, bmp_info.lats, alt_field)
    alt_field = np.ma.masked_where((pf_mask > 2) | (pf_mask < 1), alt_field)
    alt_field[(pf_mask == 2) & (~alt_field.mask)] = 5.5


    xx, yy = bmp_info.get_proj_xy()

    img = bmp_info.basemap.contourf(xx, yy, alt_field, extend="both")
    bmp_info.basemap.colorbar(img)
    bmp_info.basemap.drawcoastlines()
    plt.contour(xx, yy, pf_mask, levels=[1, 2, 3], colors="k", linewidth=2)
    fig_path = "{}_alt_{}-{}.png".format(data_path[:-5], start_year, end_year)
    fig.savefig(fig_path, transparent=True, dpi=common_plot_params.FIG_SAVE_DPI, bbox_inches="tight")
    print("Saving the plot to {}".format(fig_path))
    plt.show()
Example #2
0
def main():
    plot_permafrost = False
    plot_glaciers = False

    if not os.path.isdir(img_folder):
        print(os.mkdir(img_folder))


    geophy_file = "/BIG1/skynet1_exec1/winger/Floods/Geophys/geophys_CORDEX_NA_0.11deg_695x680_filled_grDes_barBor_Crop2Gras_peat"

    # Save the permafrost mask to file
    pfmask_file = os.path.join(img_folder, "pf_mask_na_0.11deg.nc")

    if not os.path.isfile(pfmask_file):
        save_pf_mask_to_netcdf(rpn_field_name_with_target_grid="VF", path_to_rpn_with_target_grid=geophy_file, path=pfmask_file)

    with Dataset(pfmask_file) as ds:
        pf_mask = ds.variables["pf_type"][:]



    r = RPN(geophy_file)
    lkfr = r.get_record_for_name_and_level(varname="VF", level=3)


    glac = r.get_record_for_name_and_level(varname="VF", level=2)

    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()

    proj_params = r.get_proj_parameters_for_the_last_read_rec()
    rll = RotatedLatLon(**proj_params)



    ill, jll = 0, 250


    right_margin = 50
    lkfr = lkfr[ill:-right_margin, jll:]
    lons = lons[ill:-right_margin, jll:]
    lats = lats[ill:-right_margin, jll:]
    glac = glac[ill:-right_margin, jll:]
    pf_mask = pf_mask[ill:-right_margin, jll:]

    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="l")

    xx, yy = bmp(lons, lats)


    plot_utils.apply_plot_params(font_size=8, width_cm=10, height_cm=10)

    fig = plt.figure()

    # Plot lakes
    clevs = np.arange(0, 1.1, 0.1)
    lkfr = np.ma.masked_where(lkfr <= 0, lkfr)
    cs = bmp.contourf(xx, yy, lkfr, levels=clevs, cmap="Blues")
    # bmp.colorbar(cs)
    bmp.drawcoastlines(linewidth=0.1)
    bmp.drawcountries(linewidth=0.1)



    # plot glaciers
    glval = 0.65
    glac = (glac > 0.001) * glval
    glac = np.ma.masked_where(glac < 0.5, glac)
    cmap = cm.get_cmap("Greys")

    if plot_glaciers:
        cs = bmp.pcolormesh(xx, yy, glac, cmap="Greys", alpha=0.7, vmin=0, vmax=1)
        #bmp.readshapefile(os.path.join(img_folder, "sasha_glaciers/sasha_glaciers"), "glacier_poly", color=cmap(glval))
        plt.legend([Rectangle((0, 0), 5, 5, fc=cmap(glval)), ], ["Glaciers", ], loc=3)



    # Plot permafrost boundaries
    with Dataset("permafrost_types_arctic_using_contains.nc") as ds:
        # pf_mask = ds.variables["pf_type"][:]
        # lons_s, lats_s = ds.variables["longitude"][:], ds.variables["latitude"][:]
        #
        # x, y, z = lat_lon.lon_lat_to_cartesian(lons_s.flatten(), lats_s.flatten())
        # ktree = KDTree(list(zip(x, y, z)))
        #
        # lons_copy = lons.copy()
        # lons_copy[lons > 180] -= 360
        # tmp = np.zeros_like(lons)
        # tmp = maskoceans(lons_copy, lats, tmp, inlands=False)
        #
        # xt, yt, zt = lat_lon.lon_lat_to_cartesian(lons[~tmp.mask], lats[~tmp.mask])
        # dists, inds = ktree.query(list(zip(xt, yt, zt)))
        #

        # pf_mask1[~tmp.mask] = pf_mask.flatten()[inds]

        pf_mask1 = np.ma.masked_where((pf_mask > 2) | (pf_mask < 1), pf_mask)
        pf_mask1[~pf_mask1.mask] = 1



        # bmp.contourf(xx, yy, pf_mask, levels=[2.5, 3.5, 4.5], cmap=ListedColormap(["yellow", "orange", "red"]), alpha=0.7)
        #bmp.contour(xx, yy, pf_mask, levels=[2.5, 3.5, 4.5], colors="k", linewidths=1)

        # bmp.drawrivers(color=cm.get_cmap("Blues")(0.8))

        ncolors = 2
        norm = BoundaryNorm([0.5, 1.5, 2.5], ncolors=ncolors)
        cmap = ListedColormap(["c", "violet"])
        alpha = 0.7

        if plot_permafrost:
            bmp.pcolormesh(xx, yy, pf_mask1, norm=norm, cmap=cmap, alpha=alpha)
            plt.legend([Rectangle((0, 0), 5, 5, fc=cmap(0), alpha=alpha), ], ["Permafrost",], loc=3)




    # tree line
    # bmp.readshapefile(os.path.join(img_folder, "Bernardo/treeline_current"), "treeline", linewidth=0.5, color="orange")
    # bmp.readshapefile(os.path.join(img_folder, "Bernardo/treeline_future"), "treeline", linewidth=0.5, color="orange")


    print(os.path.join(img_folder, "lakes_glaciers_pf_veg.png"))
    fig.savefig(os.path.join(img_folder, "lakes_glaciers_pf_veg.png"), dpi=400, bbox_inches="tight", transparent=True)