def plot_all(folder_path="", limit_levels=1):
    if folder_path == "":
        folder_path = sys.argv[1]

    for f in os.listdir(folder_path):
        # skip files other than monthly
        if not "monthly_fields.rpn" in f.lower():
            continue

        r = RPN(os.path.join(folder_path, f))

        #Exclude coordinate variables
        vlist = [v for v in r.get_list_of_varnames() if v not in ["^^", ">>"]]

        #remove coordinates from the list
        for varname in vlist:
            data = r.get_4d_field(name=varname)
            img_folder = os.path.join(folder_path, "img")
            params = r.get_proj_parameters_for_the_last_read_rec()
            rll = RotatedLatLon(**params)
            lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
            plot_variable(varname, data, img_folder=img_folder,
                          lons=lons, lats=lats,
                          bmap=rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats),
                          limit_levels=limit_levels)
Exemplo n.º 2
0
def main():
    # path = "/skynet3_exec2/aganji/2GW_new/guziy-water_route_offline-d4627cd00b84/infocell.nc"

    path = "/skynet3_rech1/huziy/temp/directions_north_america_0.44deg_arman.v5.nc"

    # projection definition
    rll = RotatedLatLon(lon1=-97, lat1=47.5, lon2=-7.0, lat2=0.0)

    ds = Dataset(path)
    print(list(ds.variables.keys()))

    # read data from the infocell file
    fdv = ds.variables["flow_direction_value"][:]
    lons, lats = ds.variables["lon"][:], ds.variables["lat"][:]

    # get basemap object
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="c")
    x, y = bmp(lons, lats)

    colors = get_basin_map(fdv)

    save_matrix_to_netcdf(colors)


    im = bmp.pcolormesh(x, y, colors)
    bmp.colorbar(im)
    bmp.drawcoastlines()
    plt.show()
def get_target_lons_lats_basemap(run_config: RunConfig=None):

    base_dir = Path(run_config.data_path)

    for month_dir in base_dir.iterdir():
        if month_dir.is_dir():
            for f in month_dir.iterdir():

                if f.name.startswith("."):
                    continue

                with RPN(str(f)) as r:
                    assert isinstance(r, RPN)
                    vlist = r.get_list_of_varnames()

                    vname = [v for v in vlist if v not in [">>", "^^", "HY"]][0]

                    r.get_first_record_for_name(vname)

                    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()


                    rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())
                    basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats)
                    return lons, lats, basemap
Exemplo n.º 4
0
    def get_basemap(self, varname_internal, **bmap_kwargs):
        if self.data_source_type == data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT:
            for month_dir in self.base_folder.iterdir():
                if not month_dir.is_dir():
                    continue

                for data_file in month_dir.iterdir():

                    try:
                        # skip files that do not contain the variable
                        if varname_internal in self.varname_to_file_prefix:
                            if not data_file.name.startswith(self.varname_to_file_prefix[varname_internal]):
                                continue

                        # print(self.varname_mapping)
                        # print(data_file)

                        with RPN(str(data_file)) as r:
                            r.get_first_record_for_name(self.varname_mapping[varname_internal])
                            lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
                            rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())
                            return rll.get_basemap_object_for_lons_lats(lons, lats, **bmap_kwargs)
                    except Exception as exc:
                        # Try to look into several files before giving up
                        print(exc)

        else:
            raise NotImplementedError("Not impelmented for the data_source_type = {}".format(self.data_source_type))
def plot_all(folder_path="", limit_levels=1):
    if folder_path == "":
        folder_path = sys.argv[1]

    for f in os.listdir(folder_path):
        # skip files other than monthly
        if not "monthly_fields.rpn" in f.lower():
            continue

        r = RPN(os.path.join(folder_path, f))

        #Exclude coordinate variables
        vlist = [v for v in r.get_list_of_varnames() if v not in ["^^", ">>"]]

        #remove coordinates from the list
        for varname in vlist:
            data = r.get_4d_field(name=varname)
            img_folder = os.path.join(folder_path, "img")
            params = r.get_proj_parameters_for_the_last_read_rec()
            rll = RotatedLatLon(**params)
            lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
            plot_variable(varname,
                          data,
                          img_folder=img_folder,
                          lons=lons,
                          lats=lats,
                          bmap=rll.get_basemap_object_for_lons_lats(
                              lons2d=lons, lats2d=lats),
                          limit_levels=limit_levels)
Exemplo n.º 6
0
    def get_basemap(self, varname_internal, **bmap_kwargs):
        if self.data_source_type == data_source_types.SAMPLES_FOLDER_FROM_CRCM_OUTPUT:
            for month_dir in self.base_folder.iterdir():
                if not month_dir.is_dir():
                    continue

                for data_file in month_dir.iterdir():

                    try:
                        # skip files that do not contain the variable
                        if varname_internal in self.varname_to_file_prefix:
                            if not data_file.name.startswith(self.varname_to_file_prefix[varname_internal]):
                                continue

                        # print(self.varname_mapping)
                        # print(data_file)

                        with RPN(str(data_file)) as r:
                            r.get_first_record_for_name(self.varname_mapping[varname_internal])
                            lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
                            rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())
                            return rll.get_basemap_object_for_lons_lats(lons, lats, **bmap_kwargs)
                    except Exception as exc:
                        # Try to look into several files before giving up
                        print(exc)

        else:
            raise NotImplementedError("Not impelmented for the data_source_type = {}".format(self.data_source_type))
Exemplo n.º 7
0
    def get_basemap(self, **kwargs):
        from rpn.domains.rotated_lat_lon import RotatedLatLon

        rll = RotatedLatLon(**self.projection_params)
        return rll.get_basemap_object_for_lons_lats(lons2d=self.lons,
                                                    lats2d=self.lats,
                                                    **kwargs)
Exemplo n.º 8
0
def main():
    #data_folder = "/home/huziy/b2_fs2/sim_links_frm_Katja/Arctic_0.5deg_Peat_SC_26L_CanHR85_spn_Vspng_Diagnostics/1971-2000"

    data_folder = "/home/huziy/b2_fs2/sim_links_frm_Katja/Arctic_0.5deg_Peat_SC_26L_CanHR85_spn_Vspng_Diagnostics/2071-2100"
    var_name = "I0"

    # layer_widths = [0.1, 0.2, 0.3, 0.5, 0.9, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
    # 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
    #                 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5]


    layer_widths = [0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
                    1.0, 3.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, ]

    print(len(layer_widths))

    crcm_data_manager = CRCMDataManager(layer_widths=layer_widths, data_folder=data_folder)


    #get the mask
    r = RPN("/b1_fs2/winger/Arctic/land_sea_glacier_lake_mask_free")
    msk = r.get_first_record_for_name("FMSK")

    start_year = 2071
    end_year = 2100

    # alt - using globally max temperature profile
    #alt = crcm_data_manager.get_alt_using_files_in(data_folder, vname=var_name)




    #calculate alt for each year and then take mean
    alt_list = []
    for y in range(start_year, end_year + 1):
        tmax = crcm_data_manager.get_Tmax_profiles_for_year_using_monthly_means(y, var_name=var_name)
        alt1 = crcm_data_manager.get_alt(tmax)
        alt1[alt1 < 0] = np.nan
        alt_list.append(alt1)

    alt = np.mean(alt_list, axis=0)
    alt[np.isnan(alt)] = -1

    alt = np.ma.masked_where(alt < 0, alt)
    alt = np.ma.masked_where(msk < 0.1, alt)

    #get the coordinates
    fcoord = os.listdir(data_folder)[0]
    fcoord = os.path.join(data_folder, fcoord)
    r = RPN(fcoord)
    i0 = r.get_first_record_for_name(var_name)
    lons2d, lats2d = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())
    basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons2d, lats2d=lats2d)

    plot_values(basemap, lons2d, lats2d, alt, "{}-{}(Arctic-peat-26L)".format(start_year, end_year))
Exemplo n.º 9
0
def main():
    bathymetry_path = ""
    topo_path = "/RECH2/huziy/coupling/coupled-GL-NEMO1h_30min/geophys_452x260_directions_new_452x260_GL+NENA_0.1deg_SAND_CLAY_LDPT_DPTH.fst"




    plot_utils.apply_plot_params()

    with RPN(topo_path) as r:
        assert isinstance(r, RPN)
        topo = r.get_first_record_for_name("ME")
        lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()

        print(lons.shape)

        prj_params = r.get_proj_parameters_for_the_last_read_rec()
        rll = RotatedLatLon(**prj_params)
        bmap = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="i")


    xx, yy = bmap(lons, lats)

    plt.figure()
    ax = plt.gca()

    lons1 = np.where(lons <= 180, lons, lons - 360)
    topo = maskoceans(lons1, lats, topo)


    topo_clevs = [0, 100, 200, 300, 400, 500, 600, 800, 1000, 1200]
    # bn = BoundaryNorm(topo_clevs, len(topo_clevs) - 1)
    cmap = cm.get_cmap("terrain")

    ocean_color = cmap(0.18)




    cmap, norm = colors.from_levels_and_colors(topo_clevs, cmap(np.linspace(0.3, 1, len(topo_clevs) - 1)))


    add_rectangle(ax, xx, yy, margin=20, edge_style="solid")
    add_rectangle(ax, xx, yy, margin=10, edge_style="dashed")



    im = bmap.pcolormesh(xx, yy, topo, cmap=cmap, norm=norm)
    bmap.colorbar(im, ticks=topo_clevs)
    bmap.drawcoastlines(linewidth=0.3)
    bmap.drawmapboundary(fill_color=ocean_color)
    bmap.drawparallels(np.arange(-90, 90, 10), labels=[1, 0, 0, 1], color="0.3")
    bmap.drawmeridians(np.arange(-180, 190, 10), labels=[1, 0, 0, 1], color="0.3")
    plt.savefig("GL_452x260_0.1deg_domain.png", dpi=300, bbox_inches="tight")
Exemplo n.º 10
0
def get_basemap_obj_and_coords_from_rpn_file(path=""):

    assert len(path) > 0, "The path should not be empty."
    r = RPN(path)
    vname = [v for v in r.get_list_of_varnames() if v.strip() not in [">>", "^^"]][0]
    r.get_first_record_for_name(vname)

    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    projparams = r.get_proj_parameters_for_the_last_read_rec()

    rll = RotatedLatLon(**projparams)
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="l")
    return bmp, lons, lats
Exemplo n.º 11
0
def get_basemap_obj_and_coords_from_rpn_file(path=""):

    assert len(path) > 0, "The path should not be empty."
    r = RPN(path)
    vname = [v for v in r.get_list_of_varnames() if v.strip() not in [">>", "^^"]][0]
    r.get_first_record_for_name(vname)

    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    projparams = r.get_proj_parameters_for_the_last_read_rec()

    rll = RotatedLatLon(**projparams)
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="l")
    return bmp, lons, lats
Exemplo n.º 12
0
def main():
    bathymetry_path = ""
    topo_path = "/RECH2/huziy/coupling/coupled-GL-NEMO1h_30min/geophys_452x260_directions_new_452x260_GL+NENA_0.1deg_SAND_CLAY_LDPT_DPTH.fst"

    plot_utils.apply_plot_params()

    with RPN(topo_path) as r:
        assert isinstance(r, RPN)
        topo = r.get_first_record_for_name("ME")
        lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()

        print(lons.shape)

        prj_params = r.get_proj_parameters_for_the_last_read_rec()
        rll = RotatedLatLon(**prj_params)
        bmap = rll.get_basemap_object_for_lons_lats(lons2d=lons,
                                                    lats2d=lats,
                                                    resolution="i")

    xx, yy = bmap(lons, lats)

    plt.figure()
    ax = plt.gca()

    lons1 = np.where(lons <= 180, lons, lons - 360)
    topo = maskoceans(lons1, lats, topo)

    topo_clevs = [0, 100, 200, 300, 400, 500, 600, 800, 1000, 1200]
    # bn = BoundaryNorm(topo_clevs, len(topo_clevs) - 1)
    cmap = cm.get_cmap("terrain")

    ocean_color = cmap(0.18)

    cmap, norm = colors.from_levels_and_colors(
        topo_clevs, cmap(np.linspace(0.3, 1,
                                     len(topo_clevs) - 1)))

    add_rectangle(ax, xx, yy, margin=20, edge_style="solid")
    add_rectangle(ax, xx, yy, margin=10, edge_style="dashed")

    im = bmap.pcolormesh(xx, yy, topo, cmap=cmap, norm=norm)
    bmap.colorbar(im, ticks=topo_clevs)
    bmap.drawcoastlines(linewidth=0.3)
    bmap.drawmapboundary(fill_color=ocean_color)
    bmap.drawparallels(np.arange(-90, 90, 10),
                       labels=[1, 0, 0, 1],
                       color="0.3")
    bmap.drawmeridians(np.arange(-180, 190, 10),
                       labels=[1, 0, 0, 1],
                       color="0.3")
    plt.savefig("GL_452x260_0.1deg_domain.png", dpi=300, bbox_inches="tight")
Exemplo n.º 13
0
def read_and_plot_ts_cross(path="", exp_name=""):
    var_interest = "ADD"


    path_to_dpth_to_bedrock = "/skynet1_rech3/huziy/CLASS_offline_VG/GEOPHYSICAL_FIELDS/test_analysis.rpn"

    # read depth to bedrock
    r = RPN(path_to_dpth_to_bedrock)
    _ = r.get_first_record_for_name("DPTH")

    lons2d, lats2d = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())
    b = rll.get_basemap_object_for_lons_lats(lons2d=lons2d, lats2d=lats2d, resolution="c")
    r.close()

    layer_widths = [0.1, 0.2, 0.3, 0.5, 0.9, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
                    1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
                    1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5]


    nlayers = 7
    nt = 200*12
    layer_widths = layer_widths[:nlayers]

    print(len(layer_widths))


    #calculate depths of soil layer centers
    soil_lev_tops = np.cumsum([0, ] + layer_widths[:-1])
    soil_lev_bottoms = np.cumsum(layer_widths)
    soil_levs = 0.5 * (soil_lev_tops + soil_lev_bottoms)


    i_interest_list, j_interest_list = [120, 120, 160, 170], [50, 60, 60, 60]

    r = RPN(path)

    data = r.get_4d_field_fc_hour_as_time(name=var_interest)
    lev_sorted = list(sorted(list(data.items())[0][1].keys()))[:nlayers]
    fc_sorted = list(sorted(data.keys()))[:nt]

    for i_interest, j_interest in zip(i_interest_list, j_interest_list):
        data1 = np.asarray(
            [[data[fc][lev][i_interest, j_interest] for lev in lev_sorted] for fc in fc_sorted])

        plot_time_series(data=data1, soil_levels=soil_levs, basemap=b, i_interest=i_interest,
                         j_interest=j_interest,
                         longitude=lons2d[i_interest, j_interest],
                         latitude=lats2d[i_interest, j_interest], exp_name=exp_name)
Exemplo n.º 14
0
    def __update_bmp_info_from_rpnfile_obj(self, r):
        # save projection paarams for a possible re-use in the future
        proj_params = r.get_proj_parameters_for_the_last_read_rec()
        self.lons, self.lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
        rlons, rlats = r.get_tictacs_for_the_last_read_record()

        rll = RotatedLatLon(**proj_params)
        bmp = rll.get_basemap_object_for_lons_lats(self.lons, self.lats)

        assert isinstance(bmp, Basemap)

        self.basemap_info_of_the_last_imported_field = {
            "rlon": rlons,
            "rlat": rlats,
        }
        self.basemap_info_of_the_last_imported_field.update(bmp.projparams)
Exemplo n.º 15
0
    def __update_bmp_info_from_rpnfile_obj(self, r):
        # save projection paarams for a possible re-use in the future
        proj_params = r.get_proj_parameters_for_the_last_read_rec()
        self.lons, self.lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
        rlons, rlats = r.get_tictacs_for_the_last_read_record()

        rll = RotatedLatLon(**proj_params)
        bmp = rll.get_basemap_object_for_lons_lats(self.lons, self.lats)

        assert isinstance(bmp, Basemap)

        self.basemap_info_of_the_last_imported_field = {
            "rlon": rlons,
            "rlat": rlats,
        }
        self.basemap_info_of_the_last_imported_field.update(bmp.projparams)
Exemplo n.º 16
0
def get_data_and_coords():
    """
    :rtype: (dict, np.ndarray, np.ndarray, Basemap)
    :return: dict with {level: field} data
    """
    path = "/skynet3_rech1/huziy/geofields_interflow_exp/pm1979010100_00000000p"
    vname = "D9"

    r = RPN(path)
    data = list(r.get_4d_field(name=vname).items())[0][1]

    params = r.get_proj_parameters_for_the_last_read_rec()
    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()

    rll = RotatedLatLon(**params)
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats)

    r.close()
    return data, lons, lats, bmp
Exemplo n.º 17
0
def get_data_and_coords():
    """
    :rtype: (dict, np.ndarray, np.ndarray, Basemap)
    :return: dict with {level: field} data
    """
    path = "/skynet3_rech1/huziy/geofields_interflow_exp/pm1979010100_00000000p"
    vname = "D9"

    r = RPN(path)
    data = list(r.get_4d_field(name=vname).items())[0][1]

    params = r.get_proj_parameters_for_the_last_read_rec()
    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()

    rll = RotatedLatLon(**params)
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats)

    r.close()
    return data, lons, lats, bmp
def plot_basin_outlets(shape_file=BASIN_BOUNDARIES_FILE,
                       bmp_info=None,
                       directions=None,
                       accumulation_areas=None,
                       lake_fraction_field=None):
    assert isinstance(bmp_info, BasemapInfo)

    driver = ogr.GetDriverByName("ESRI Shapefile")
    print(driver)
    ds = driver.Open(shape_file, 0)

    assert isinstance(ds, ogr.DataSource)
    layer = ds.GetLayer()

    assert isinstance(layer, ogr.Layer)
    print(layer.GetFeatureCount())

    latlong_proj = osr.SpatialReference()
    latlong_proj.ImportFromEPSG(4326)

    utm_proj = layer.GetSpatialRef()

    # create Coordinate Transformation
    coord_transform = osr.CoordinateTransformation(latlong_proj, utm_proj)

    utm_coords = coord_transform.TransformPoints(
        list(zip(bmp_info.lons.flatten(), bmp_info.lats.flatten())))
    utm_coords = np.asarray(utm_coords)
    x_utm = utm_coords[:, 0].reshape(bmp_info.lons.shape)
    y_utm = utm_coords[:, 1].reshape(bmp_info.lons.shape)

    basin_mask = np.zeros_like(bmp_info.lons)
    cell_manager = CellManager(directions,
                               accumulation_area_km2=accumulation_areas,
                               lons2d=bmp_info.lons,
                               lats2d=bmp_info.lats)

    index = 1
    basins = []
    basin_names = []
    basin_name_to_mask = {}
    for feature in layer:
        assert isinstance(feature, ogr.Feature)
        # print feature["FID"]

        geom = feature.GetGeometryRef()
        assert isinstance(geom, ogr.Geometry)

        basins.append(ogr.CreateGeometryFromWkb(geom.ExportToWkb()))
        basin_names.append(feature["abr"])

    accumulation_areas_temp = accumulation_areas.copy()
    lons_out, lats_out = [], []
    basin_names_out = []
    name_to_ij_out = OrderedDict()

    min_basin_area = min(b.GetArea() * 1.0e-6 for b in basins)

    while len(basins):
        fm = np.max(accumulation_areas_temp)

        i, j = np.where(fm == accumulation_areas_temp)
        i, j = i[0], j[0]
        p = ogr.CreateGeometryFromWkt("POINT ({} {})".format(
            x_utm[i, j], y_utm[i, j]))
        b_selected = None
        name_selected = None
        for name, b in zip(basin_names, basins):

            assert isinstance(b, ogr.Geometry)
            assert isinstance(p, ogr.Geometry)
            if b.Contains(p.Buffer(2000 * 2**0.5)):
                # Check if there is an upstream cell from the same basin
                the_mask = cell_manager.get_mask_of_upstream_cells_connected_with_by_indices(
                    i, j)

                # Save the mask of the basin for future use
                basin_name_to_mask[name] = the_mask

                # if is_part_of_points_in(b, x_utm[the_mask == 1], y_utm[the_mask == 1]):
                # continue

                b_selected = b
                name_selected = name
                # basin_names_out.append(name)

                lons_out.append(bmp_info.lons[i, j])
                lats_out.append(bmp_info.lats[i, j])
                name_to_ij_out[name] = (i, j)

                basin_mask[the_mask == 1] = index
                index += 1
                break

        if b_selected is not None:
            basins.remove(b_selected)
            basin_names.remove(name_selected)
            outlet_index_in_basin = 1
            current_basin_name = name_selected
            while current_basin_name in basin_names_out:
                current_basin_name = name_selected + str(outlet_index_in_basin)
                outlet_index_in_basin += 1

            basin_names_out.append(current_basin_name)
            print(len(basins), basin_names_out)

        accumulation_areas_temp[i, j] = -1

    plot_utils.apply_plot_params(font_size=12,
                                 width_pt=None,
                                 width_cm=20,
                                 height_cm=20)
    gs = GridSpec(2, 2, width_ratios=[1.0, 0.5], wspace=0.01)
    fig = plt.figure()

    ax = fig.add_subplot(gs[1, 0])
    xx, yy = bmp_info.get_proj_xy()
    bmp_info.basemap.drawcoastlines(linewidth=0.5, ax=ax)
    bmp_info.basemap.drawrivers(zorder=5, color="0.5", ax=ax)

    upstream_edges = cell_manager.get_upstream_polygons_for_points(
        model_point_list=[
            ModelPoint(ix=i, jy=j) for (i, j) in name_to_ij_out.values()
        ],
        xx=xx,
        yy=yy)

    upstream_edges_latlon = cell_manager.get_upstream_polygons_for_points(
        model_point_list=[
            ModelPoint(ix=i, jy=j) for (i, j) in name_to_ij_out.values()
        ],
        xx=bmp_info.lons,
        yy=bmp_info.lats)

    plot_utils.draw_upstream_area_bounds(ax,
                                         upstream_edges=upstream_edges,
                                         color="r",
                                         linewidth=0.6)
    plot_utils.save_to_shape_file(upstream_edges_latlon, in_proj=None)

    xs, ys = bmp_info.basemap(lons_out, lats_out)
    bmp_info.basemap.scatter(xs, ys, c="0.75", s=30, zorder=10)
    bmp_info.basemap.drawparallels(np.arange(-90, 90, 5),
                                   labels=[True, False, False, False],
                                   linewidth=0.5)

    bmp_info.basemap.drawmeridians(np.arange(-180, 180, 5),
                                   labels=[False, False, False, True],
                                   linewidth=0.5)

    cmap = cm.get_cmap("rainbow", index - 1)
    bn = BoundaryNorm(list(range(index + 1)), index - 1)

    # basin_mask = np.ma.masked_where(basin_mask < 0.5, basin_mask)
    # bmp_info.basemap.pcolormesh(xx, yy, basin_mask, norm=bn, cmap=cmap, ax=ax)

    xmin, xmax = ax.get_xlim()
    ymin, ymax = ax.get_ylim()

    print(xmin, xmax, ymin, ymax)
    dx = xmax - xmin
    dy = ymax - ymin
    step_y = 0.1
    step_x = 0.12
    y0_frac = 0.75
    y0_frac_bottom = 0.02
    x0_frac = 0.35
    bname_to_text_coords = {
        "RDO": (xmin + x0_frac * dx, ymin + y0_frac_bottom * dy),
        "STM": (xmin + (x0_frac + step_x) * dx, ymin + y0_frac_bottom * dy),
        "SAG":
        (xmin + (x0_frac + 2 * step_x) * dx, ymin + y0_frac_bottom * dy),
        "BOM":
        (xmin + (x0_frac + 3 * step_x) * dx, ymin + y0_frac_bottom * dy),
        "MAN":
        (xmin + (x0_frac + 4 * step_x) * dx, ymin + y0_frac_bottom * dy),
        "MOI":
        (xmin + (x0_frac + 5 * step_x) * dx, ymin + y0_frac_bottom * dy),
        "ROM": (xmin + (x0_frac + 5 * step_x) * dx,
                ymin + (y0_frac_bottom + step_y) * dy),
        "NAT": (xmin + (x0_frac + 5 * step_x) * dx,
                ymin + (y0_frac_bottom + 2 * step_y) * dy),

        ######
        "CHU": (xmin + (x0_frac + 5 * step_x) * dx, ymin + y0_frac * dy),
        "GEO": (xmin + (x0_frac + 5 * step_x) * dx,
                ymin + (y0_frac + step_y) * dy),
        "BAL": (xmin + (x0_frac + 5 * step_x) * dx,
                ymin + (y0_frac + 2 * step_y) * dy),
        "PYR": (xmin + (x0_frac + 4 * step_x) * dx,
                ymin + (y0_frac + 2 * step_y) * dy),
        "MEL": (xmin + (x0_frac + 3 * step_x) * dx,
                ymin + (y0_frac + 2 * step_y) * dy),
        "FEU": (xmin + (x0_frac + 2 * step_x) * dx,
                ymin + (y0_frac + 2 * step_y) * dy),
        "ARN": (xmin + (x0_frac + 1 * step_x) * dx,
                ymin + (y0_frac + 2 * step_y) * dy),

        ######
        "CAN": (xmin + 0.1 * dx, ymin + 0.80 * dy),
        "GRB": (xmin + 0.1 * dx, ymin + (0.80 - step_y) * dy),
        "LGR": (xmin + 0.1 * dx, ymin + (0.80 - 2 * step_y) * dy),
        "RUP": (xmin + 0.1 * dx, ymin + (0.80 - 3 * step_y) * dy),
        "WAS": (xmin + 0.1 * dx, ymin + (0.80 - 4 * step_y) * dy),
        "BEL": (xmin + 0.1 * dx, ymin + (0.80 - 5 * step_y) * dy),
    }

    # bmp_info.basemap.readshapefile(".".join(BASIN_BOUNDARIES_FILE.split(".")[:-1]).replace("utm18", "latlon"), "basin",
    #                                linewidth=1.2, ax=ax, zorder=9)

    for name, xa, ya, lona, lata in zip(basin_names_out, xs, ys, lons_out,
                                        lats_out):
        ax.annotate(name,
                    xy=(xa, ya),
                    xytext=bname_to_text_coords[name],
                    textcoords='data',
                    ha='right',
                    va='bottom',
                    bbox=dict(boxstyle='round,pad=0.4', fc='white'),
                    arrowprops=dict(arrowstyle='->',
                                    connectionstyle='arc3,rad=0',
                                    linewidth=0.25),
                    font_properties=FontProperties(size=8),
                    zorder=20)

        print(r"{} & {:.0f} \\".format(
            name, accumulation_areas[name_to_ij_out[name]]))

    # Plot zonally averaged lake fraction
    ax = fig.add_subplot(gs[1, 1])
    ydata = range(lake_fraction_field.shape[1])
    ax.plot(lake_fraction_field.mean(axis=0) * 100, ydata, lw=2)

    ax.fill_betweenx(ydata, lake_fraction_field.mean(axis=0) * 100, alpha=0.5)

    ax.set_xlabel("Lake fraction (%)")
    ax.set_ylim(min(ydata), max(ydata))
    ax.xaxis.set_tick_params(direction='out', width=1)
    ax.yaxis.set_tick_params(direction='out', width=1)
    ax.xaxis.set_ticks_position("bottom")
    ax.yaxis.set_ticks_position("none")

    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)

    for tl in ax.yaxis.get_ticklabels():
        tl.set_visible(False)

    # plot elevation, buffer zone, big lakes, grid cells
    ax = fig.add_subplot(gs[0, :])
    geophy_file = "/RESCUE/skynet3_rech1/huziy/from_guillimin/geophys_Quebec_0.1deg_260x260_with_dd_v6"

    r = RPN(geophy_file)
    elev = r.get_first_record_for_name("ME")
    lkfr = r.get_first_record_for_name("LKFR")
    fldr = r.get_first_record_for_name("FLDR")

    params = r.get_proj_parameters_for_the_last_read_rec()
    lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**params)

    bsmp = rll.get_basemap_object_for_lons_lats(lons2d=lons,
                                                lats2d=lats,
                                                resolution="l")
    xx, yy = bsmp(lons, lats)

    dx = (xx[0, 0] - xx[-1, 0]) / xx.shape[0]
    dy = (yy[0, 0] - yy[0, -1]) / yy.shape[1]

    xx_ll_crnrs = xx - dx / 2
    yy_ll_crnrs = yy - dy / 2

    xx_ur_crnrs = xx + dx / 2
    yy_ur_crnrs = yy + dy / 2

    ll_lon, ll_lat = bsmp(xx_ll_crnrs[0, 0], yy_ll_crnrs[0, 0], inverse=True)
    ur_lon, ur_lat = bsmp(xx_ur_crnrs[-1, -1],
                          yy_ur_crnrs[-1, -1],
                          inverse=True)

    crnr_lons = np.array([[ll_lon, ll_lon], [ur_lon, ur_lon]])

    crnr_lats = np.array([[ll_lat, ll_lat], [ur_lat, ur_lat]])

    bsmp = rll.get_basemap_object_for_lons_lats(lons2d=crnr_lons,
                                                lats2d=crnr_lats)

    # plot elevation
    levs = [0, 100, 200, 300, 500, 700, 1000, 1500, 2000, 2800]
    norm = BoundaryNorm(levs, len(levs) - 1)
    the_cmap = my_colormaps.get_cmap_from_ncl_spec_file(
        path="colormap_files/OceanLakeLandSnow.rgb", ncolors=len(levs) - 1)

    lons[lons > 180] -= 360
    me_to_plot = maskoceans(lons, lats, elev, resolution="l")
    im = bsmp.contourf(xx,
                       yy,
                       me_to_plot,
                       cmap=the_cmap,
                       levels=levs,
                       norm=norm,
                       ax=ax)
    bsmp.colorbar(im)

    bsmp.drawcoastlines(linewidth=0.5, ax=ax)

    # show large lake points
    gl_lakes = np.ma.masked_where((lkfr < 0.6) | (fldr <= 0) | (fldr > 128),
                                  lkfr)
    gl_lakes[~gl_lakes.mask] = 1.0
    bsmp.pcolormesh(xx,
                    yy,
                    gl_lakes,
                    cmap=cm.get_cmap("Blues"),
                    ax=ax,
                    vmin=0,
                    vmax=1,
                    zorder=3)

    # show free zone border
    margin = 20
    x1 = xx_ll_crnrs[margin, margin]
    x2 = xx_ur_crnrs[-margin, margin]
    y1 = yy_ll_crnrs[margin, margin]
    y2 = yy_ur_crnrs[margin, -margin]
    pol_corners = ((x1, y1), (x2, y1), (x2, y2), (x1, y2))
    ax.add_patch(Polygon(xy=pol_corners, fc="none", ls="solid", lw=3,
                         zorder=5))

    # show blending zone border (with halo zone)
    margin = 10
    x1 = xx_ll_crnrs[margin, margin]
    x2 = xx_ur_crnrs[-margin, margin]
    y1 = yy_ll_crnrs[margin, margin]
    y2 = yy_ur_crnrs[margin, -margin]
    pol_corners = ((x1, y1), (x2, y1), (x2, y2), (x1, y2))
    ax.add_patch(
        Polygon(xy=pol_corners, fc="none", ls="dashed", lw=3, zorder=5))

    # show the grid
    step = 20
    xx_ll_crnrs_ext = np.zeros([n + 1 for n in xx_ll_crnrs.shape])
    yy_ll_crnrs_ext = np.zeros([n + 1 for n in yy_ll_crnrs.shape])

    xx_ll_crnrs_ext[:-1, :-1] = xx_ll_crnrs
    yy_ll_crnrs_ext[:-1, :-1] = yy_ll_crnrs
    xx_ll_crnrs_ext[:-1, -1] = xx_ll_crnrs[:, -1]
    yy_ll_crnrs_ext[-1, :-1] = yy_ll_crnrs[-1, :]

    xx_ll_crnrs_ext[-1, :] = xx_ur_crnrs[-1, -1]
    yy_ll_crnrs_ext[:, -1] = yy_ur_crnrs[-1, -1]

    bsmp.pcolormesh(xx_ll_crnrs_ext[::step, ::step],
                    yy_ll_crnrs_ext[::step, ::step],
                    np.ma.masked_all_like(xx_ll_crnrs_ext)[::step, ::step],
                    edgecolors="0.6",
                    ax=ax,
                    linewidth=0.05,
                    zorder=4,
                    alpha=0.5)

    ax.set_title("Elevation (m)")

    # plt.show()
    fig.savefig("qc_basin_outlets_points.png", bbox_inches="tight")
    # plt.show()
    plt.close(fig)

    return name_to_ij_out, basin_name_to_mask
Exemplo n.º 19
0
def main(plot_vals=False):
    varname = "RFAC"
    multiplier = 24 * 3600
    data_folder = Path(
        "/home/huziy/skynet3_rech1/CNRCWP/Calgary_flood/atm_data_for_Arman_simulations"
    )
    day_range = range(19, 22)
    dates_of_interest = [datetime(2013, 6, d) for d in day_range]

    img_folder = Path("calgary_flood/2D")

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

    lons, lats, bmp = None, None, None

    the_mask = get_bow_river_basin_mask()

    i_list, j_list = np.where(the_mask > 0.5)
    imin, imax = i_list.min() - 2, i_list.max() + 5
    jmin, jmax = j_list.min() - 2, j_list.max() + 5

    # Calculate daily means
    sim_label_to_date_to_mean = OrderedDict()
    for sim_dir in data_folder.iterdir():
        mr = MultiRPN(str(sim_dir.joinpath("pm*")))
        print(str(sim_dir))
        print(mr.get_number_of_records())

        label = sim_dir.name.split("_")[-2].replace("NoDrain", "").replace(
            "frozen", "Frozen").replace("Bow", "")
        sim_label_to_date_to_mean[label] = OrderedDict()

        data = mr.get_4d_field(varname)
        data = {
            d: list(v.items())[0][1]
            for d, v in data.items() if d.day in day_range
        }

        for d in dates_of_interest:
            sim_label_to_date_to_mean[label][d] = np.array([
                field for d1, field in data.items() if d1.day == d.day
            ]).mean(axis=0) * multiplier

        if lons is None:
            lons, lats = mr.get_longitudes_and_latitudes_of_the_last_read_rec()
            for f in sim_dir.iterdir():
                if f.name.startswith("pm"):
                    r = RPN(str(f))
                    r.get_first_record_for_name(varname=varname)
                    rll = RotatedLatLon(
                        **r.get_proj_parameters_for_the_last_read_rec())
                    bmp = rll.get_basemap_object_for_lons_lats(
                        lons2d=lons[imin:imax, jmin:jmax],
                        lats2d=lats[imin:imax, jmin:jmax],
                        resolution="i")
                    r.close()
                    break

        mr.close()

    # reorder simulations
    sim_label_to_date_to_mean = OrderedDict([
        (k, sim_label_to_date_to_mean[k]) for k in sorted(
            sim_label_to_date_to_mean, key=lambda z: len(z), reverse=True)
    ])

    key_list = [k for k in sim_label_to_date_to_mean]
    key_list[-2], key_list[-1] = key_list[-1], key_list[-2]
    sim_label_to_date_to_mean = OrderedDict([(k, sim_label_to_date_to_mean[k])
                                             for k in key_list])

    # do the plots (subplots: vertically - simulations, horizontally - days)
    plot_utils.apply_plot_params(width_cm=24, height_cm=28, font_size=10)
    fig = plt.figure()
    nrows = len(sim_label_to_date_to_mean)
    ncols = len(day_range)
    gs = GridSpec(nrows, ncols, wspace=0, hspace=0.1)

    clevs_vals = [0, 0.1, 20, 50, 100, 150, 200, 300]

    base_label = None
    clevs_diff = [1, 5, 10, 20, 50, 100]
    clevs_diff = [-c for c in reversed(clevs_diff)] + [0] + clevs_diff
    cmap_diff = cm.get_cmap("bwr", len(clevs_diff) - 1)
    cmap_vals = get_cmap_from_ncl_spec_file(
        path="colormap_files/precip3_16lev.rgb", ncolors=len(clevs_vals) - 1)
    xx, yy = bmp(lons, lats)
    title = "Total runoff ({}, mm/day)".format(varname)
    fig.suptitle(title)

    for row, (sim_label,
              date_to_field) in enumerate(sim_label_to_date_to_mean.items()):

        if row == 0 or plot_vals:
            base_sim = OrderedDict([(k, 0) for k, v in date_to_field.items()])
            base_label = sim_label
            plot_label = sim_label
            clevs = clevs_vals
            cmap = cmap_vals
            extend = "max"
        else:
            base_sim = list(sim_label_to_date_to_mean.items())[0][1]
            plot_label = "{}\n-\n{}".format(sim_label, base_label)
            clevs = clevs_diff
            cmap = cmap_diff
            extend = "both"

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

        for col, (the_date, field) in enumerate(date_to_field.items()):

            ax = fig.add_subplot(gs[row, col])
            to_plot = np.ma.masked_where(the_mask < 0.5,
                                         field - base_sim[the_date])
            # cs = bmp.contourf(xx[~to_plot.mask], yy[~to_plot.mask], to_plot[~to_plot.mask], levels=clevs, extend="max", tri=True)
            cs = bmp.pcolormesh(xx,
                                yy,
                                to_plot[:-1, :-1],
                                norm=bn,
                                vmin=clevs[0],
                                vmax=clevs[-1],
                                cmap=cmap)

            bmp.drawcoastlines(ax=ax, linewidth=0.3)
            assert isinstance(bmp, Basemap)
            bmp.readshapefile(BOW_RIVER_SHP[:-4], "basin", zorder=5)
            cb = bmp.colorbar(cs,
                              ax=ax,
                              ticks=clevs,
                              extend=extend,
                              pad="4%",
                              size="10%")

            if plot_vals:
                cb.ax.set_visible(col == ncols - 1 and row == 0)
            else:
                cb.ax.set_visible(col == ncols - 1 and row in (0, 1))

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

            if row == 0:
                ax.set_title(the_date.strftime("%b %d"))

    if plot_vals:
        img_file = img_folder.joinpath("{}.png".format(varname))
    else:
        img_file = img_folder.joinpath("{}_diff.png".format(varname))

    fig.savefig(str(img_file))
    plt.close(fig)
def main(save_to_nc=True):
    """
    Read geotiff file with the field capacity field in mm,
    interpolate it to model grid, divide by the depth to bedrock,
    and save to netcdf file
    """

    # Read model data
    obs_fields_path = "/skynet3_rech1/huziy/geofields_interflow_exp/pm1979010100_00000000p"

    robj = RPN(obs_fields_path)

    thfc = robj.get_first_record_for_name_and_level(varname="D9", level=1)

    bdrck_depth = _get_depth_to_bedrock(obs_fields_path)

    proj_params = robj.get_proj_parameters_for_the_last_read_rec()

    rll = RotatedLatLon(**proj_params)

    t_lons, t_lats = robj.get_longitudes_and_latitudes_for_the_last_read_rec()

    t_lons[t_lons > 180] -= 360.0
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=t_lons, lats2d=t_lats)

    robj.close()



    intp_img = read_observed_bfc().resample(
        SwathDefinition(t_lons.flatten(), t_lats.flatten())
    )
    fig = plt.figure(figsize=(10, 6))

    assert isinstance(fig, Figure)


    gs = GridSpec(1, 3, width_ratios=[0.95, 1, 1])


    # Observed bulk field capacity
    ax = fig.add_subplot(gs[0, 0])
    x, y = bmp(t_lons, t_lats)
    to_plot = intp_img.image_data.reshape(t_lons.shape)
    to_plot = np.ma.masked_where(to_plot < 0, to_plot) / (1.0e3 * bdrck_depth)

    im = bmp.pcolormesh(x, y, to_plot, vmin=0, vmax=0.5)
    bmp.drawcoastlines()
    ax.set_title("ORNL DAAC")

    # Currently used in the model
    ax = fig.add_subplot(gs[0, 1])
    thfc_to_plot = np.ma.masked_where(to_plot.mask, thfc)
    im = bmp.pcolormesh(x, y, thfc_to_plot, vmin=0, vmax=0.5)
    bmp.colorbar(im, ax=ax)
    bmp.drawcoastlines(ax=ax)
    ax.set_title("Current")
    bmp.colorbar(im, ax=ax)


    # Current - (ORNL DAAC)
    ax = fig.add_subplot(gs[0, 2])
    x, y = bmp(t_lons, t_lats)
    cmap = cm.get_cmap("RdBu_r", 21)
    im = bmp.pcolormesh(x, y, thfc_to_plot - to_plot, vmin=-0.5, vmax=0.5, cmap=cmap)
    bmp.colorbar(im, ax=ax)
    bmp.drawcoastlines(ax=ax)
    ax.set_title("Current - (ORNL-DAAC)")
    bmp.colorbar(im, ax=ax)



    plt.show()

    pass
Exemplo n.º 21
0
    def get_basemap(self, **kwargs):
        from rpn.domains.rotated_lat_lon import RotatedLatLon

        rll = RotatedLatLon(**self.projection_params)
        return rll.get_basemap_object_for_lons_lats(lons2d=self.lons, lats2d=self.lats, **kwargs)
Exemplo n.º 22
0
def main():
    plot_permafrost = True
    plot_glaciers = True

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

    geophy_file = "misc_plots/geophys_CORDEX_NA_0.11deg_695x680_filled_grDes_barBor_Crop2Gras_peat_with_directions"

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

        #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="red")

    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)
Exemplo n.º 23
0
def main():

    path = "/snow3/huziy/geophysics_files/geophys_452x260_0.1deg_GL_NENA_and_directions_452x260_GL+NENA_0.1deg_SAND_CLAY_LDPT_DPTH.fst"

    # get rotated latlon projection and grid parameters
    with RPN(path) as r:

        assert isinstance(r, RPN)
        lkfr = r.variables["LKFR"][:].squeeze()
        print(r.variables)

        lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()
        rll_obj = RotatedLatLon(
            **r.get_proj_parameters_for_the_last_read_rec())

        rlon, rlat = r.get_tictacs_for_the_last_read_record()

    plot_utils.apply_plot_params()

    lons[lons > 180] -= 360

    crs_map = ccrs.Orthographic(central_latitude=50, central_longitude=-80)
    ax = plt.axes(projection=crs_map)
    # b = Basemap(projection="ortho", lat_0=50, lon_0=-80)

    #
    # b.drawcoastlines(linewidth=0.5)
    # b.shadedrelief()
    # b.warpimage()

    # b.drawmapboundary(fill_color="#94e3e8")

    # aximg = b.etopo()
    # assert isinstance(aximg, AxesImage)
    # aximg.set_cmap("bwr")

    topo, lonst, latst = get_etopo()

    lonst[lonst >= 180] -= 360

    oc_mask = maskoceans(lonst, latst, topo)

    clevs = [
        -500, 0, 100, 200, 300, 400, 500, 600, 700, 800, 1000, 1200, 1400,
        1600, 1800, 2000, 2200, 2400, 2600, 2800, 3000, 3500, 4000, 4500, 5000
    ]
    cmap = get_cmap(clevs)
    topo[topo < 0] = -600
    topo[oc_mask.mask] = -600

    cs = ax.contourf(lonst,
                     latst,
                     topo,
                     cmap=cmap,
                     levels=clevs,
                     transform=ccrs.Geodetic())
    ax.colorbar(cs)

    # show_coast_lines(b, resolution="h")

    ax = plt.gca()
    for gc in get_gridcell_polygons(rll_obj, rlon, rlat, step=20):
        ax.add_patch(gc)

    plt.savefig("carrtopy_GL_NENA_ortho0.1deg.png",
                bbox_inches="tight",
                dpi=400)
def plot_domain_and_interest_region(ax: Axes,
                                    topo_nc_file_path: Path,
                                    focus_region_lonlat_nc_file: Path = None):
    """
    :param focus_region_lonlat_nc_file: Path to the file containing focus region lons and lats
    :param region_mask_lats: latitudes corresponding to the region mask
    :param region_mask_lons:
    :param ax:
    :param topo_nc_file_path:
    :param region_mask:

    Note: below is the expected structure of the input netcdf file

    $ ncdump -h geophys_452x260_me.nc
    netcdf geophys_452x260_me {
    dimensions:
        x = 452 ;
        y = 260 ;
    variables:
        float ME(x, y) ;
        float lon(x, y) ;
        float lat(x, y) ;
        int proj_params ;
            proj_params:grid_type = "E" ;
            proj_params:lat1 = 0. ;
            proj_params:lon1 = 180. ;
            proj_params:lat2 = 1. ;
            proj_params:lon2 = 276. ;
    }
    """

    # read the model topography from the file
    with xarray.open_dataset(topo_nc_file_path) as topo_ds:
        topo_lons, topo_lats, topo = [
            topo_ds[k].values for k in ["lon", "lat", "ME"]
        ]

        prj_params = topo_ds["proj_params"]

        rll = RotatedLatLon(lon1=prj_params.lon1,
                            lat1=prj_params.lat1,
                            lon2=prj_params.lon2,
                            lat2=prj_params.lat2)

        rot_pole_cpy = rll.get_cartopy_projection_obj()

    ax.set_visible(False)
    ax = ax.figure.add_axes(ax.get_position().bounds, projection=rot_pole_cpy)
    # ax.coastlines()

    gl_mask = get_gl_mask(topo_nc_file_path)
    region_mask = get_mask_of_points_near_lakes(gl_mask, npoints_radius=20)
    topo_lons[topo_lons > 180] -= 360

    xll, yll = rot_pole_cpy.transform_point(topo_lons[0, 0], topo_lats[0, 0],
                                            cartopy.crs.PlateCarree())
    xur, yur = rot_pole_cpy.transform_point(topo_lons[-1, -1], topo_lats[-1,
                                                                         -1],
                                            cartopy.crs.PlateCarree())
    map_extent = [xll, xur, yll, yur]
    print("Map extent: ", map_extent)

    topo_clevs = [0, 100, 200, 300, 400, 500, 600, 800, 1000, 1200]
    # bn = BoundaryNorm(topo_clevs, len(topo_clevs) - 1)
    cmap = cm.get_cmap("terrain")
    ocean_color = cmap(0.18)
    cmap, norm = colors.from_levels_and_colors(
        topo_clevs, cmap(np.linspace(0.3, 1,
                                     len(topo_clevs) - 1)))

    xyz_coords = rot_pole_cpy.transform_points(cartopy.crs.PlateCarree(),
                                               topo_lons, topo_lats)
    xx = xyz_coords[:, :, 0]
    yy = xyz_coords[:, :, 1]

    add_rectangle(ax,
                  xx,
                  yy,
                  margin=20,
                  edge_style="solid",
                  zorder=10,
                  linewidth=0.5)
    add_rectangle(ax,
                  xx,
                  yy,
                  margin=10,
                  edge_style="dashed",
                  zorder=10,
                  linewidth=0.5)

    # plot a rectangle for the focus region
    if focus_region_lonlat_nc_file is not None:
        with xarray.open_dataset(focus_region_lonlat_nc_file) as fr:
            focus_lons, focus_lats = fr["lon"].data, fr["lat"].data

            xyz_coords = rot_pole_cpy.transform_points(
                cartopy.crs.PlateCarree(), focus_lons, focus_lats)
            xxf, yyf = xyz_coords[..., 0], xyz_coords[..., 1]

            add_rectangle(ax,
                          xxf,
                          yyf,
                          edge_style="solid",
                          margin=0,
                          edgecolor="magenta",
                          zorder=10,
                          linewidth=1)

    cs = ax.pcolormesh(topo_lons[:, :],
                       topo_lats[:, :],
                       topo[:, :],
                       transform=cartopy.crs.PlateCarree(),
                       cmap=cmap,
                       norm=norm)

    to_plot = np.ma.masked_where(region_mask < 0.5, region_mask)
    ax.scatter(topo_lons,
               topo_lats,
               to_plot * 0.01,
               c="cyan",
               transform=cartopy.crs.PlateCarree(),
               alpha=0.5)

    # Add geographic features
    line_color = "k"
    ax.add_feature(common_params.LAKES_50m,
                   facecolor=cartopy.feature.COLORS["water"],
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.add_feature(common_params.OCEAN_50m,
                   facecolor=cartopy.feature.COLORS["water"],
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.add_feature(common_params.COASTLINE_50m,
                   facecolor="none",
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.add_feature(common_params.RIVERS_50m,
                   facecolor="none",
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.set_extent(map_extent, crs=rot_pole_cpy)

    # improve colorbar
    divider = make_axes_locatable(ax)
    ax_cb = divider.new_vertical(size="5%",
                                 pad=0.1,
                                 axes_class=plt.Axes,
                                 pack_start=True)
    ax.figure.add_axes(ax_cb)
    cb = plt.colorbar(cs, cax=ax_cb, orientation="horizontal")
    cb.ax.set_xticklabels(topo_clevs, rotation=45)
    return ax
Exemplo n.º 25
0
    def to_gridconfig(self):
        return GridConfig(rll=self.rll,
                          ni=self.nx,
                          nj=self.ny,
                          iref=self.iref,
                          jref=self.jref,
                          dx=self.dx,
                          dy=self.dy,
                          xref=self.lonref,
                          yref=self.latref)


known_projections = {
    "GLK_210x130_0.1deg": RotatedLatLon(lon1=180.,
                                        lat1=0.,
                                        lon2=-84.,
                                        lat2=1.0)
}

known_domains = {
    "GLK_210x130_0.1deg":
    Grid(rll=RotatedLatLon(lon1=180., lat1=0., lon2=-84., lat2=1.0),
         nx=210,
         ny=130,
         iref=105,
         jref=100,
         lonref=-84,
         latref=48,
         dx=0.1,
         dy=0.1),
    "GLK_440x260_0.1deg":
Exemplo n.º 26
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)
Exemplo n.º 27
0
from rpn.domains.rotated_lat_lon import RotatedLatLon
from domains.grid_config import GridConfig

# The projection used in the last 2 papers of Huziy's thesis
qc_rll_projection_huziy_thesis = RotatedLatLon(lon1=360 - 68,
                                               lat1=52,
                                               lon2=16.65,
                                               lat2=0)
qc_rll_domain_huziy_thesis = GridConfig(rll=qc_rll_projection_huziy_thesis,
                                        iref=142,
                                        jref=122,
                                        dx=0.1,
                                        dy=0.1,
                                        xref=180,
                                        yref=0,
                                        ni=260,
                                        nj=260)

qc_rll_domain_huziy_thesis_04 = qc_rll_domain_huziy_thesis.decrease_resolution_keep_free_domain_same(
    4)

if __name__ == "__main__":
    print(qc_rll_domain_huziy_thesis_04)
Exemplo n.º 28
0
def main():
    soiltemp_var_name = "TBAR"
    path1 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_var/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_var_1980-2009/TBAR.rpn"
    label1 = "Variable (real) depth to bedrock"

    path2 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_3.6m/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_constant_1980-2009/TBAR.rpn"
    label2 = "Fixed depth to bedrock (3.6 m)"

    path_to_dpth_to_bedrock = "/skynet1_rech3/huziy/CLASS_offline_VG/GEOPHYSICAL_FIELDS/test_analysis.rpn"

    # read depth to bedrock
    r = RPN(path_to_dpth_to_bedrock)
    dpth = r.get_first_record_for_name("DPTH")
    r.close()

    layer_widths = [0.1, 0.2, 0.3, 0.5, 0.9, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
                    1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
                    1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5]

    print(len(layer_widths))

    crcm_data_manager = CRCMDataManager(layer_widths=layer_widths)

    # Read in the temperature profiles
    r1 = RPN(path1)
    soiltemp1 = r1.get_4d_field(name=soiltemp_var_name)
    lons2d, lats2d = r1.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**r1.get_proj_parameters_for_the_last_read_rec())
    b = rll.get_basemap_object_for_lons_lats(lons2d=lons2d, lats2d=lats2d, resolution="c")
    r1.close()

    r2 = RPN(path2)
    soiltemp2 = r2.get_4d_field(name=soiltemp_var_name)
    r2.close()

    dates_sorted = list(sorted(soiltemp1.keys()))
    levels_sorted = list(sorted(list(soiltemp1.items())[0][1].keys()))

    # group dates for each year
    alt1_list = []
    alt2_list = []
    for year, dates_group in itertools.groupby(dates_sorted, lambda par: par.year):
        t1 = []
        t2 = []
        for d in dates_group:
            t1.append([soiltemp1[d][lev] for lev in levels_sorted])
            t2.append([soiltemp2[d][lev] for lev in levels_sorted])

        # Get maximum temperature profiles
        t1max = np.max(t1, axis=0).transpose(1, 2, 0)
        t2max = np.max(t2, axis=0).transpose(1, 2, 0)

        print(t1max.shape, t2max.shape)

        #calculate and save alts
        h1 = crcm_data_manager.get_alt(t1max)
        h2 = crcm_data_manager.get_alt(t2max)

        h1[h1 < 0] = np.nan
        h2[h2 < 0] = np.nan

        alt1_list.append(h1)
        alt2_list.append(h2)

    #take into account permafrost
    alt1_list_pf = []
    alt2_list_pf = []
    n_years_for_pf = 3
    for i in range(len(alt1_list) - n_years_for_pf):
        alt1_list_pf.append(np.max(alt1_list[i:i+n_years_for_pf], axis=0))
        alt2_list_pf.append(np.max(alt2_list[i:i+n_years_for_pf], axis=0))



    # calculate climatological mean
    alt1 = np.mean(alt1_list_pf, axis=0)
    alt2 = np.mean(alt2_list_pf, axis=0)

    print(np.isnan(alt1).any(), np.isnan(alt2).any())

    #mask nans
    alt1 = np.ma.masked_where(np.isnan(alt1), alt1)
    alt2 = np.ma.masked_where(np.isnan(alt2), alt2)

    #calculate change due to fixed depth to bedrock
    delta = alt2 - alt1

    #
    for i in range(len(alt1_list)):
        alt1_list[i] = np.ma.masked_where(np.isnan(alt1_list[i]), alt1_list[i])
        alt2_list[i] = np.ma.masked_where(np.isnan(alt2_list[i]), alt2_list[i])

    #tval, pval = ttest_ind(alt1_list, alt2_list)

    #mask oceans
    lons2d[lons2d > 180] -= 360
    dpth = maskoceans(lons2d, lats2d, dpth)
    delta = np.ma.masked_where(dpth.mask, delta)


    #calculate differences in SWE
    path_swe_1 = path1.replace("TBAR.rpn", "SNO.rpn")
    r1 = RPN(path_swe_1)
    swe1 = r1.get_all_time_records_for_name("SNO")
    r1.close()
    swe1_winter_clim = np.mean(
        [field for key, field in swe1.items() if key.month in [1, 2, 12]], axis=0)
    swe1_winter_clim = np.ma.masked_where((swe1_winter_clim >= 999) | dpth.mask, swe1_winter_clim)

    path_swe_2 = path2.replace("TBAR.rpn", "SNO.rpn")
    r2 = RPN(path_swe_2)
    swe2 = r2.get_all_time_records_for_name("SNO")
    r2.close()
    swe2_winter_clim = np.mean(
        [field for key, field in swe2.items() if key.month in [1, 2, 12]], axis=0)
    swe2_winter_clim = np.ma.masked_where((swe2_winter_clim >= 999) | dpth.mask, swe2_winter_clim)




    #plotting
    print("Start plotting ...")
    plot_differences(b, lons2d, lats2d, dpth, delta, label="ALT(DPTH=3.6m) - ALT(DPTH~)", pvalue=None,
                     swe_diff=swe2_winter_clim - swe1_winter_clim)
Exemplo n.º 29
0
def main(plot_vals=False):
    varname = "RFAC"
    multiplier = 24 * 3600
    data_folder = Path("/home/huziy/skynet3_rech1/CNRCWP/Calgary_flood/atm_data_for_Arman_simulations")
    day_range = range(19, 22)
    dates_of_interest = [datetime(2013, 6, d) for d in day_range]

    img_folder = Path("calgary_flood/2D")

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

    lons, lats, bmp = None, None, None

    the_mask = get_bow_river_basin_mask()

    i_list, j_list = np.where(the_mask > 0.5)
    imin, imax = i_list.min() - 2, i_list.max() + 5
    jmin, jmax = j_list.min() - 2, j_list.max() + 5

    # Calculate daily means
    sim_label_to_date_to_mean = OrderedDict()
    for sim_dir in data_folder.iterdir():
        mr = MultiRPN(str(sim_dir.joinpath("pm*")))
        print(str(sim_dir))
        print(mr.get_number_of_records())

        label = sim_dir.name.split("_")[-2].replace("NoDrain", "").replace("frozen", "Frozen").replace("Bow", "")
        sim_label_to_date_to_mean[label] = OrderedDict()

        data = mr.get_4d_field(varname)
        data = {d: list(v.items())[0][1] for d, v in data.items() if d.day in day_range}

        for d in dates_of_interest:
            sim_label_to_date_to_mean[label][d] = (
                np.array([field for d1, field in data.items() if d1.day == d.day]).mean(axis=0) * multiplier
            )

        if lons is None:
            lons, lats = mr.get_longitudes_and_latitudes_of_the_last_read_rec()
            for f in sim_dir.iterdir():
                if f.name.startswith("pm"):
                    r = RPN(str(f))
                    r.get_first_record_for_name(varname=varname)
                    rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())
                    bmp = rll.get_basemap_object_for_lons_lats(
                        lons2d=lons[imin:imax, jmin:jmax], lats2d=lats[imin:imax, jmin:jmax], resolution="i"
                    )
                    r.close()
                    break

        mr.close()

    # reorder simulations
    sim_label_to_date_to_mean = OrderedDict(
        [
            (k, sim_label_to_date_to_mean[k])
            for k in sorted(sim_label_to_date_to_mean, key=lambda z: len(z), reverse=True)
        ]
    )

    key_list = [k for k in sim_label_to_date_to_mean]
    key_list[-2], key_list[-1] = key_list[-1], key_list[-2]
    sim_label_to_date_to_mean = OrderedDict([(k, sim_label_to_date_to_mean[k]) for k in key_list])

    # do the plots (subplots: vertically - simulations, horizontally - days)
    plot_utils.apply_plot_params(width_cm=24, height_cm=28, font_size=10)
    fig = plt.figure()
    nrows = len(sim_label_to_date_to_mean)
    ncols = len(day_range)
    gs = GridSpec(nrows, ncols, wspace=0, hspace=0.1)

    clevs_vals = [0, 0.1, 20, 50, 100, 150, 200, 300]

    base_label = None
    clevs_diff = [1, 5, 10, 20, 50, 100]
    clevs_diff = [-c for c in reversed(clevs_diff)] + [0] + clevs_diff
    cmap_diff = cm.get_cmap("bwr", len(clevs_diff) - 1)
    cmap_vals = get_cmap_from_ncl_spec_file(path="colormap_files/precip3_16lev.rgb", ncolors=len(clevs_vals) - 1)
    xx, yy = bmp(lons, lats)
    title = "Total runoff ({}, mm/day)".format(varname)
    fig.suptitle(title)

    for row, (sim_label, date_to_field) in enumerate(sim_label_to_date_to_mean.items()):

        if row == 0 or plot_vals:
            base_sim = OrderedDict([(k, 0) for k, v in date_to_field.items()])
            base_label = sim_label
            plot_label = sim_label
            clevs = clevs_vals
            cmap = cmap_vals
            extend = "max"
        else:
            base_sim = list(sim_label_to_date_to_mean.items())[0][1]
            plot_label = "{}\n-\n{}".format(sim_label, base_label)
            clevs = clevs_diff
            cmap = cmap_diff
            extend = "both"

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

        for col, (the_date, field) in enumerate(date_to_field.items()):

            ax = fig.add_subplot(gs[row, col])
            to_plot = np.ma.masked_where(the_mask < 0.5, field - base_sim[the_date])
            # cs = bmp.contourf(xx[~to_plot.mask], yy[~to_plot.mask], to_plot[~to_plot.mask], levels=clevs, extend="max", tri=True)
            cs = bmp.pcolormesh(xx, yy, to_plot[:-1, :-1], norm=bn, vmin=clevs[0], vmax=clevs[-1], cmap=cmap)

            bmp.drawcoastlines(ax=ax, linewidth=0.3)
            assert isinstance(bmp, Basemap)
            bmp.readshapefile(BOW_RIVER_SHP[:-4], "basin", zorder=5)
            cb = bmp.colorbar(cs, ax=ax, ticks=clevs, extend=extend, pad="4%", size="10%")

            if plot_vals:
                cb.ax.set_visible(col == ncols - 1 and row == 0)
            else:
                cb.ax.set_visible(col == ncols - 1 and row in (0, 1))

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

            if row == 0:
                ax.set_title(the_date.strftime("%b %d"))

    if plot_vals:
        img_file = img_folder.joinpath("{}.png".format(varname))
    else:
        img_file = img_folder.joinpath("{}_diff.png".format(varname))

    fig.savefig(str(img_file))
    plt.close(fig)
Exemplo n.º 30
0
def main():
    soiltemp_var_name = "TBAR"
    path1 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_var/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_var_1980-2009/TBAR.rpn"
    label1 = "Variable (real) depth to bedrock"

    path2 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_3.6m/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_constant_1980-2009/TBAR.rpn"
    label2 = "Fixed depth to bedrock (3.6 m)"

    path_to_dpth_to_bedrock = "/skynet1_rech3/huziy/CLASS_offline_VG/GEOPHYSICAL_FIELDS/test_analysis.rpn"

    # read depth to bedrock
    r = RPN(path_to_dpth_to_bedrock)
    dpth = r.get_first_record_for_name("DPTH")
    r.close()

    layer_widths = [
        0.1, 0.2, 0.3, 0.5, 0.9, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
        1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
        1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
        1.5, 1.5, 1.5, 1.5, 1.5
    ]

    print(len(layer_widths))

    crcm_data_manager = CRCMDataManager(layer_widths=layer_widths)

    # Read in the temperature profiles
    r1 = RPN(path1)
    soiltemp1 = r1.get_4d_field(name=soiltemp_var_name)
    lons2d, lats2d = r1.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**r1.get_proj_parameters_for_the_last_read_rec())
    b = rll.get_basemap_object_for_lons_lats(lons2d=lons2d,
                                             lats2d=lats2d,
                                             resolution="c")
    r1.close()

    r2 = RPN(path2)
    soiltemp2 = r2.get_4d_field(name=soiltemp_var_name)
    r2.close()

    dates_sorted = list(sorted(soiltemp1.keys()))
    levels_sorted = list(sorted(list(soiltemp1.items())[0][1].keys()))

    # group dates for each year
    alt1_list = []
    alt2_list = []
    for year, dates_group in itertools.groupby(dates_sorted,
                                               lambda par: par.year):
        t1 = []
        t2 = []
        for d in dates_group:
            t1.append([soiltemp1[d][lev] for lev in levels_sorted])
            t2.append([soiltemp2[d][lev] for lev in levels_sorted])

        # Get maximum temperature profiles
        t1max = np.max(t1, axis=0).transpose(1, 2, 0)
        t2max = np.max(t2, axis=0).transpose(1, 2, 0)

        print(t1max.shape, t2max.shape)

        #calculate and save alts
        h1 = crcm_data_manager.get_alt(t1max)
        h2 = crcm_data_manager.get_alt(t2max)

        h1[h1 < 0] = np.nan
        h2[h2 < 0] = np.nan

        alt1_list.append(h1)
        alt2_list.append(h2)

    #take into account permafrost
    alt1_list_pf = []
    alt2_list_pf = []
    n_years_for_pf = 3
    for i in range(len(alt1_list) - n_years_for_pf):
        alt1_list_pf.append(np.max(alt1_list[i:i + n_years_for_pf], axis=0))
        alt2_list_pf.append(np.max(alt2_list[i:i + n_years_for_pf], axis=0))

    # calculate climatological mean
    alt1 = np.mean(alt1_list_pf, axis=0)
    alt2 = np.mean(alt2_list_pf, axis=0)

    print(np.isnan(alt1).any(), np.isnan(alt2).any())

    #mask nans
    alt1 = np.ma.masked_where(np.isnan(alt1), alt1)
    alt2 = np.ma.masked_where(np.isnan(alt2), alt2)

    #calculate change due to fixed depth to bedrock
    delta = alt2 - alt1

    #
    for i in range(len(alt1_list)):
        alt1_list[i] = np.ma.masked_where(np.isnan(alt1_list[i]), alt1_list[i])
        alt2_list[i] = np.ma.masked_where(np.isnan(alt2_list[i]), alt2_list[i])

    #tval, pval = ttest_ind(alt1_list, alt2_list)

    #mask oceans
    lons2d[lons2d > 180] -= 360
    dpth = maskoceans(lons2d, lats2d, dpth)
    delta = np.ma.masked_where(dpth.mask, delta)

    #calculate differences in SWE
    path_swe_1 = path1.replace("TBAR.rpn", "SNO.rpn")
    r1 = RPN(path_swe_1)
    swe1 = r1.get_all_time_records_for_name("SNO")
    r1.close()
    swe1_winter_clim = np.mean(
        [field for key, field in swe1.items() if key.month in [1, 2, 12]],
        axis=0)
    swe1_winter_clim = np.ma.masked_where(
        (swe1_winter_clim >= 999) | dpth.mask, swe1_winter_clim)

    path_swe_2 = path2.replace("TBAR.rpn", "SNO.rpn")
    r2 = RPN(path_swe_2)
    swe2 = r2.get_all_time_records_for_name("SNO")
    r2.close()
    swe2_winter_clim = np.mean(
        [field for key, field in swe2.items() if key.month in [1, 2, 12]],
        axis=0)
    swe2_winter_clim = np.ma.masked_where(
        (swe2_winter_clim >= 999) | dpth.mask, swe2_winter_clim)

    #plotting
    print("Start plotting ...")
    plot_differences(b,
                     lons2d,
                     lats2d,
                     dpth,
                     delta,
                     label="ALT(DPTH=3.6m) - ALT(DPTH~)",
                     pvalue=None,
                     swe_diff=swe2_winter_clim - swe1_winter_clim)
def main():
    path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/Global_NA_v1/Samples/Global_NA_v1_201306/pm2013010100_00017280p"
    varname = "PR"
    plot_units = "mm/day"
    mult_coeff = 1000 * 24 * 3600
    add_offset = 0

    img_folder = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/glob_sim/{}/monthly/{}".format(varname, os.path.basename(path))
    img_folder = Path(img_folder)
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    r = RPN(path=path)

    pr = r.get_all_time_records_for_name(varname=varname)

    lons2d, lats2d = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())



    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons2d,
                                               lats2d=lats2d,
                                               resolution="c", no_rot=True)

#    bmp = Basemap(projection="robin", lon_0=180)

    xx, yy = bmp(lons2d, lats2d)


    dates = list(sorted(pr.keys()))
    data = np.array([pr[d] for d in dates])

    p = pd.Panel(data=data, items=dates, major_axis=range(data.shape[1]), minor_axis=range(data.shape[2]))

    # p_daily = p.groupby(lambda d: d.day, axis="items").mean()
    p_daily = p.apply(np.mean, axis="items")

    print(p_daily.head())

    lons2d[lons2d > 180] -= 360

    bmap_params = bmp.projparams
    bmap_params.update({
        'llcrnrlon': lons2d[0, 0], 'urcrnrlon': lons2d[-1, -1], 'llcrnrlat': lats2d[0, 0], 'urcrnrlat': lats2d[-1, -1]
    })
    rpole_crs = ccrs.RotatedPole(pole_longitude=bmap_params["lon_0"] + 180,
                                 pole_latitude=bmap_params["o_lat_p"])



    clevs = [0, 0.01, 0.1, 1, 1.5, 2, 5, 10, 20, 40, 60, 80]
    norm = BoundaryNorm(clevs, ncolors=len(clevs) - 1)
    cmap = cm.get_cmap(cm_basemap.s3pcpn, len(clevs) - 1)
    field = p_daily.values * mult_coeff + add_offset
    fig = plt.figure()
    plt.title("{}, {}/{}/{}, {}".format(varname, 1, dates[0].month, dates[0].year, plot_units))




    ax = plt.axes(projection=rpole_crs)
    ax.coastlines(resolution='110m')

    ax.gridlines()
    ax.gridlines()


    # cs = bmp.contourf(xx, yy, field, clevs, norm=norm, extend="max", cmap=cmap)
    cs = ax.pcolormesh(lons2d[:-1, :-1], lats2d[:-1, :-1], field[:-1, :-1], norm=norm, cmap=cmap, transform=rpole_crs)
    plt.colorbar(cs, ticks=clevs, extend="max", ax=ax)
    img_file = img_folder.joinpath("{:02d}-{:02d}-{}.png".format(1, dates[0].month, dates[0].year))
    # bmp.drawcoastlines()
    # bmp.drawstates()
    # bmp.drawcounties()
    # bmp.drawcountries()

    plt.savefig(img_file.open("wb"))
    plt.close(fig)
    print(pr[dates[0]].mean())
Exemplo n.º 32
0
def main():
    path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/Global_NA_v1/Samples/Global_NA_v1_201306/pm2013010100_00017280p"
    varname = "PR"
    plot_units = "mm/day"
    mult_coeff = 1000 * 24 * 3600
    add_offset = 0

    img_folder = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/glob_sim/{}/monthly/{}".format(
        varname, os.path.basename(path))
    img_folder = Path(img_folder)
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    r = RPN(path=path)

    pr = r.get_all_time_records_for_name(varname=varname)

    lons2d, lats2d = r.get_longitudes_and_latitudes_for_the_last_read_rec()
    rll = RotatedLatLon(**r.get_proj_parameters_for_the_last_read_rec())

    bmp = rll.get_basemap_object_for_lons_lats(lons2d=lons2d,
                                               lats2d=lats2d,
                                               resolution="c",
                                               no_rot=True)

    #    bmp = Basemap(projection="robin", lon_0=180)

    xx, yy = bmp(lons2d, lats2d)

    dates = list(sorted(pr.keys()))
    data = np.array([pr[d] for d in dates])

    p = pd.Panel(data=data,
                 items=dates,
                 major_axis=range(data.shape[1]),
                 minor_axis=range(data.shape[2]))

    # p_daily = p.groupby(lambda d: d.day, axis="items").mean()
    p_daily = p.apply(np.mean, axis="items")

    print(p_daily.head())

    lons2d[lons2d > 180] -= 360

    bmap_params = bmp.projparams
    bmap_params.update({
        'llcrnrlon': lons2d[0, 0],
        'urcrnrlon': lons2d[-1, -1],
        'llcrnrlat': lats2d[0, 0],
        'urcrnrlat': lats2d[-1, -1]
    })
    rpole_crs = ccrs.RotatedPole(pole_longitude=bmap_params["lon_0"] + 180,
                                 pole_latitude=bmap_params["o_lat_p"])

    clevs = [0, 0.01, 0.1, 1, 1.5, 2, 5, 10, 20, 40, 60, 80]
    norm = BoundaryNorm(clevs, ncolors=len(clevs) - 1)
    cmap = cm.get_cmap(cm_basemap.s3pcpn, len(clevs) - 1)
    field = p_daily.values * mult_coeff + add_offset
    fig = plt.figure()
    plt.title("{}, {}/{}/{}, {}".format(varname, 1, dates[0].month,
                                        dates[0].year, plot_units))

    ax = plt.axes(projection=rpole_crs)
    ax.coastlines(resolution='110m')

    ax.gridlines()
    ax.gridlines()

    # cs = bmp.contourf(xx, yy, field, clevs, norm=norm, extend="max", cmap=cmap)
    cs = ax.pcolormesh(lons2d[:-1, :-1],
                       lats2d[:-1, :-1],
                       field[:-1, :-1],
                       norm=norm,
                       cmap=cmap,
                       transform=rpole_crs)
    plt.colorbar(cs, ticks=clevs, extend="max", ax=ax)
    img_file = img_folder.joinpath("{:02d}-{:02d}-{}.png".format(
        1, dates[0].month, dates[0].year))
    # bmp.drawcoastlines()
    # bmp.drawstates()
    # bmp.drawcounties()
    # bmp.drawcountries()

    plt.savefig(img_file.open("wb"))
    plt.close(fig)
    print(pr[dates[0]].mean())
Exemplo n.º 33
0
def main(save_to_nc=True):
    """
    Read geotiff file with the field capacity field in mm,
    interpolate it to model grid, divide by the depth to bedrock,
    and save to netcdf file
    """

    # Read model data
    obs_fields_path = "/skynet3_rech1/huziy/geofields_interflow_exp/pm1979010100_00000000p"

    robj = RPN(obs_fields_path)

    thfc = robj.get_first_record_for_name_and_level(varname="D9", level=1)

    bdrck_depth = _get_depth_to_bedrock(obs_fields_path)

    proj_params = robj.get_proj_parameters_for_the_last_read_rec()

    rll = RotatedLatLon(**proj_params)

    t_lons, t_lats = robj.get_longitudes_and_latitudes_for_the_last_read_rec()

    t_lons[t_lons > 180] -= 360.0
    bmp = rll.get_basemap_object_for_lons_lats(lons2d=t_lons, lats2d=t_lats)

    robj.close()

    intp_img = read_observed_bfc().resample(
        SwathDefinition(t_lons.flatten(), t_lats.flatten()))
    fig = plt.figure(figsize=(10, 6))

    assert isinstance(fig, Figure)

    gs = GridSpec(1, 3, width_ratios=[0.95, 1, 1])

    # Observed bulk field capacity
    ax = fig.add_subplot(gs[0, 0])
    x, y = bmp(t_lons, t_lats)
    to_plot = intp_img.image_data.reshape(t_lons.shape)
    to_plot = np.ma.masked_where(to_plot < 0, to_plot) / (1.0e3 * bdrck_depth)

    im = bmp.pcolormesh(x, y, to_plot, vmin=0, vmax=0.5)
    bmp.drawcoastlines()
    ax.set_title("ORNL DAAC")

    # Currently used in the model
    ax = fig.add_subplot(gs[0, 1])
    thfc_to_plot = np.ma.masked_where(to_plot.mask, thfc)
    im = bmp.pcolormesh(x, y, thfc_to_plot, vmin=0, vmax=0.5)
    bmp.colorbar(im, ax=ax)
    bmp.drawcoastlines(ax=ax)
    ax.set_title("Current")
    bmp.colorbar(im, ax=ax)

    # Current - (ORNL DAAC)
    ax = fig.add_subplot(gs[0, 2])
    x, y = bmp(t_lons, t_lats)
    cmap = cm.get_cmap("RdBu_r", 21)
    im = bmp.pcolormesh(x,
                        y,
                        thfc_to_plot - to_plot,
                        vmin=-0.5,
                        vmax=0.5,
                        cmap=cmap)
    bmp.colorbar(im, ax=ax)
    bmp.drawcoastlines(ax=ax)
    ax.set_title("Current - (ORNL-DAAC)")
    bmp.colorbar(im, ax=ax)

    plt.show()

    pass