コード例 #1
0
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
コード例 #2
0
def main():
    var_name_liquid = "I1"
    var_name_solid = "I2"
    #peirod of interest
    start_year = 1979
    end_year = 1988

    #spatial averaging will be done over upstream points to the stations
    selected_ids = ["092715", "080101", "074903", "050304", "080104", "081007", "061905",
                         "041903", "040830", "093806", "090613", "081002", "093801", "080718"]

    selected_ids = ["074903"]


    #simulation names corresponding to the paths
    sim_names = ["crcm5-hcd-rl", "crcm5-hcd-rl-intfl"]

    sim_labels = [x.upper() for x in sim_names]

    colors = ["blue", "violet"]

    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]

    layer_depths = np.cumsum(layer_widths)


    paths = [
        "/home/huziy/skynet3_rech1/from_guillimin/new_outputs/quebec_0.1_crcm5-hcd-rl_spinup",
        "/home/huziy/skynet3_rech1/from_guillimin/new_outputs/quebec_0.1_crcm5-hcd-rl-intfl_spinup2/Samples_all_in_one"
    ]

    seasons = [
        [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]
    ]

    season_names = [
        "DJF", "MAM", "JJA", "SON"
    ]

    managers = [
        Crcm5ModelDataManager(samples_folder_path=path, file_name_prefix="pm",
            all_files_in_samples_folder=True, need_cell_manager= (i == 0)) for i, path in enumerate(paths)
    ]

    #share the cell manager
    a_data_manager = managers[0]
    assert isinstance(a_data_manager, Crcm5ModelDataManager)
    cell_manager = a_data_manager.cell_manager
    assert isinstance(cell_manager, CellManager)
    for m in managers[1:]:
        assert isinstance(m, Crcm5ModelDataManager)
        m.cell_manager = cell_manager

    #share the lake fraction field
    lake_fraction = a_data_manager.lake_fraction



    #selected_ids = ["092715", "080101", "074903", "050304", "080104", "081007", "061905",
    #                  "041903", "040830", "093806", "090613", "081002", "093801", "080718"]
    start_date = datetime(start_year, 1, 1)
    end_date = datetime(end_year, 12, 31)

    stations = cehq_station.read_station_data(selected_ids = selected_ids,
            start_date=start_date, end_date=end_date
    )

    #stations with corresponding model points
    station_to_mp = a_data_manager.get_dataless_model_points_for_stations(stations)

    #figure out levels in soil



    sim_label_to_profiles = {}
    for s, mp in station_to_mp.items():
        assert isinstance(mp, ModelPoint)
        mask = (mp.flow_in_mask == 1) & (lake_fraction < 0.6)
        fig = plt.figure()
        fmt = ScalarFormatter(useMathText=True)
        fmt.set_powerlimits([-2, 2])

        print(mp.ix, mp.jy, s.id)

        for m, label, color in zip(managers, sim_labels, colors):
            assert isinstance(m, Crcm5ModelDataManager)



            monthly_means_liquid = _get_cached_monthly_mean_fields(label, start_year, end_year, var_name_liquid)
            if monthly_means_liquid is None:
                monthly_means_liquid = m.get_monthly_climatology_of_3d_field(var_name=var_name_liquid, start_year=start_year, end_year=end_year)
                _cache_monthly_mean_fields(monthly_means_liquid, label, start_year, end_year, var_name_liquid)

            monthly_means_solid = _get_cached_monthly_mean_fields(label, start_year, end_year, var_name_solid)
            if monthly_means_solid is None:
                monthly_means_solid = m.get_monthly_climatology_of_3d_field(var_name=var_name_solid, start_year=start_year, end_year=end_year)
                _cache_monthly_mean_fields(monthly_means_solid, label, start_year, end_year, var_name_solid)


            profiles = [ monthly_means_liquid[i][mask,:].mean(axis = 0) + monthly_means_solid[i][mask,:].mean(axis = 0) for i in range(12) ]

            sim_label_to_profiles[label] = np.array( profiles )


        x = [ date2num( datetime(2001,month,1) ) for month in range(1,13)]
        y = layer_depths

        y2d, x2d = np.meshgrid(y, x)
        delta = (sim_label_to_profiles[sim_labels[1]] - sim_label_to_profiles[sim_labels[0]]) / sim_label_to_profiles[sim_labels[0]] * 100

        #delta = np.ma.masked_where(delta < 0.1, delta)

        cmap = my_colormaps.get_cmap_from_ncl_spec_file(path="colormap_files/BlueRed.rgb", ncolors=10)
        the_min = -6.0
        the_max = 6.0
        step = (the_max - the_min) / float(cmap.N)

        plt.pcolormesh(x2d[:,:8], y2d[:,:8], delta[:,:8], cmap = cmap, vmin = the_min, vmax = the_max) #, levels = np.arange(-6,7,1))
        plt.gca().invert_yaxis()
        plt.colorbar(ticks = np.arange(the_min, the_max + step, step))
        plt.gca().set_ylabel("Depth (m)")

        plt.gca().xaxis.set_major_formatter(DateFormatter("%b"))


        #fig.tight_layout()
        fig.savefig("soil_profile_upstream_of_{0}.pdf".format(s.id))




    pass
コード例 #3
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)
コード例 #4
0
def main():
    # plot_utils.apply_plot_params(20)
    folder = "/home/huziy/skynet3_rech1/from_guillimin"
    fname = "geophys_Quebec_0.1deg_260x260_with_dd_v6"
    path = os.path.join(folder, fname)

    rObj = RPN(path)

    mg = rObj.get_first_record_for_name_and_level(
        "MG", level=0, level_kind=level_kinds.PRESSURE)
    # j2 = rObj.get_first_record_for_name("J2")

    levs = [0, 100, 200, 300, 500, 700, 1000, 1500, 2000, 2800]
    norm = BoundaryNorm(levs, len(levs) - 1)

    me = rObj.get_first_record_for_name_and_level(
        "ME", level=0, level_kind=level_kinds.ARBITRARY)

    proj_params = rObj.get_proj_parameters_for_the_last_read_rec()

    print(me.shape)
    lons2d, lats2d = rObj.get_longitudes_and_latitudes_for_the_last_read_rec()

    lons2d[lons2d > 180] -= 360

    # me_to_plot = np.ma.masked_where(mg < 0.4, me)
    me_to_plot = me
    # print me_to_plot.min(), me_to_plot.max()

    rll = RotatedLatLon(**proj_params)
    basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons2d,
                                                   lats2d=lats2d)
    rObj.close()

    x, y = basemap(lons2d, lats2d)

    plt.figure()
    ax = plt.gca()
    # the_cmap = cm.get_cmap(name = "gist_earth", lut=len(levs) -1)
    # the_cmap = my_colormaps.get_cmap_from_ncl_spec_file(path="colormap_files/topo_15lev.rgb")
    the_cmap = my_colormaps.get_cmap_from_ncl_spec_file(
        path="colormap_files/OceanLakeLandSnow.rgb", ncolors=len(levs) - 1)

    # new_cm = matplotlib.colors.LinearSegmentedColormap('colormap',new_dict,len(levs) - 1)

    me_to_plot = maskoceans(lons2d, lats2d, me_to_plot, resolution="l")
    basemap.contourf(x, y, me_to_plot, cmap=the_cmap, levels=levs, norm=norm)

    # basemap.fillcontinents(color = "none", lake_color="aqua")
    basemap.drawmapboundary(fill_color='#479BF9')
    basemap.drawcoastlines(linewidth=0.5)
    basemap.drawmeridians(np.arange(-180, 180, 20), labels=[1, 0, 0, 1])
    basemap.drawparallels(np.arange(45, 75, 15), labels=[1, 0, 0, 1])

    basemap.readshapefile("data/shape/contour_bv_MRCC/Bassins_MRCC_latlon",
                          name="basin",
                          linewidth=1)

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "5%", pad="3%")
    plt.colorbar(ticks=levs, cax=cax)

    # basemap.scatter(x, y, color="k", s=1, linewidths=0, ax=ax, zorder=2)

    margin = 20
    x1 = x[margin, margin]
    x2 = x[-margin, margin]
    y1 = y[margin, margin]
    y2 = y[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))

    # plt.tight_layout()
    # plt.show()
    plt.savefig("free_domain_260x260.png", dpi=cpp.FIG_SAVE_DPI)

    pass
コード例 #5
0
def main(months=None):
    start_year = 1979
    end_year = 1988

    import matplotlib.pyplot as plt
    fig = plt.figure()

    var_name_to_ij = {
        "TT": (1, 0),
        "PR": (2, 0),
        "AH": (1, 1),
        "AV": (2, 1),
        "STFL": (0, 1),
        "TRAF": (1, 2),
        "TDRA": (2, 2)
    }

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

    dmManager = Crcm5ModelDataManager(samples_folder_path=rpn_folder,
                                      file_name_prefix="dm",
                                      all_files_in_samples_folder=True)
    basemap = dmManager.get_rotpole_basemap()
    lons, lats = dmManager.lons2D, dmManager.lats2D
    x, y = basemap(lons, lats)

    lkfr = dmManager.lake_fraction

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

    #fig.suptitle("{0} minus {1}".format(sim_name2, sim_name1))
    month_dates = [datetime(2001, m, 1) for m in months]

    ax = None
    for var_name in field_names:

        coef = 1
        if var_name == "PR":
            coef = 24 * 60 * 60 * 1000
        elif var_name in ["TDRA", "TRAF"]:
            coef = 24 * 60 * 60
        elif var_name == "AV":
            coef = 3 * 60 * 60
        #cmap.set_bad("0.6")
        print(var_name)
        v1 = _get_values(sim_name1,
                         var_name,
                         start_year,
                         end_year,
                         months=months)
        v2 = _get_values(sim_name2,
                         var_name,
                         start_year,
                         end_year,
                         months=months)

        #calculate annual means, for each year
        v1 = v1.mean(axis=1)
        v1m = v1.mean(axis=0)

        v2 = v2.mean(axis=1)
        v2m = v2.mean(axis=0)

        i, j = var_name_to_ij[var_name]

        ax = fig.add_subplot(gs[i, j])
        dv = (v2m - v1m) * coef

        t, p = stats.ttest_ind(v1, v2)

        if var_name in ["STFL"]:
            dv = np.ma.masked_where((lkfr >= 0.6), dv)

            print(months)

            i_interest, j_interest = np.where(dv > 53)
            print(i_interest, j_interest)
            dv = maskoceans(lons, lats, dv)
            print(10 * "*")
        elif var_name in ["TDRA", "TRAF"]:
            dv = maskoceans(lons, lats, dv)
        else:
            pass

        dv = np.ma.masked_where(
            p > 1, dv)  #mask changes not significant to the 10 % level

        if not np.all(dv.mask):
            print("{0}: min = {1}; max = {2}".format(var_name, dv.min(),
                                                     dv.max()))
            print("")
            max_abs = np.ma.max(np.abs(dv))
            delta = max_abs
            the_power = np.log10(delta)

            the_power = np.ceil(the_power) if the_power >= 0 else np.floor(
                the_power)

            delta = np.ceil(delta / 10.0**the_power) * 10.0**the_power
            while delta > 2 * max_abs:
                delta /= 2.0

            while 0.8 * delta > max_abs:
                delta *= 0.8

            step = delta / 5.0  #10 ** (the_power - 1)  * 2 if the_power >= 0 else 10 ** the_power * 2

            if var_name == "TT":
                step = 0.06
                delta = 0.66

            levels = np.arange(-delta, delta + step, step)

            cmap = my_colormaps.get_cmap_from_ncl_spec_file(
                path="colormap_files/BlueRed.rgb", ncolors=len(levels) - 1)

            bn = BoundaryNorm(levels, cmap.N) if len(levels) > 0 else None

            print("delta={0}; step={1}; the_power={2}".format(
                delta, step, the_power))

            the_img = basemap.pcolormesh(x,
                                         y,
                                         dv,
                                         cmap=cmap,
                                         vmin=-delta,
                                         vmax=delta,
                                         norm=bn)

            #add colorbar
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", "5%", pad="3%")
            cb = plt.colorbar(the_img, cax=cax, format=fmt)

        #coast lines
        basemap.drawcoastlines(ax=ax, linewidth=0.5)
        assert isinstance(ax, Axes)
        ax.set_title("$\Delta$ " + field_name_to_long_name[var_name] + "\n" +
                     "({0})".format(field_name_to_units[var_name]))

    bbox_props = dict(boxstyle="rarrow,pad=0.3", fc="wheat", ec="b", lw=2)
    label = "-".join([d.strftime("%b") for d in month_dates
                      ]) + "\n({0}-{1})".format(start_year, end_year)
    ax.annotate(label,
                xy=(0.1, 0.85),
                xycoords="figure fraction",
                bbox=bbox_props,
                font_properties=FontProperties(size=15, weight="bold"))

    #fig.tight_layout()
    if months is None:
        fig.savefig("annual_mean_diffs_{0}_minus_{1}.jpeg".format(
            sim_name2, sim_name1))
    else:
        print(month_dates, months)
        fig.savefig("seasonal_mean_{0}_diffs_{1}_minus_{2}.png".format(
            "_".join(map(str, months)), sim_name2, sim_name1))
    pass
コード例 #6
0
ファイル: geophys_file_manip.py プロジェクト: guziy/RPN
def main():
    # plot_utils.apply_plot_params(20)
    folder = "/home/huziy/skynet3_rech1/from_guillimin"
    fname = "geophys_Quebec_0.1deg_260x260_with_dd_v6"
    path = os.path.join(folder, fname)

    rObj = RPN(path)

    mg = rObj.get_first_record_for_name_and_level("MG", level=0, level_kind=level_kinds.PRESSURE)
    # j2 = rObj.get_first_record_for_name("J2")

    levs = [0, 100, 200, 300, 500, 700, 1000, 1500, 2000, 2800]
    norm = BoundaryNorm(levs, len(levs) - 1)

    me = rObj.get_first_record_for_name_and_level("ME", level=0, level_kind=level_kinds.ARBITRARY)

    proj_params = rObj.get_proj_parameters_for_the_last_read_rec()

    print(me.shape)
    lons2d, lats2d = rObj.get_longitudes_and_latitudes_for_the_last_read_rec()

    lons2d[lons2d > 180] -= 360

    # me_to_plot = np.ma.masked_where(mg < 0.4, me)
    me_to_plot = me
    # print me_to_plot.min(), me_to_plot.max()

    rll = RotatedLatLon(**proj_params)
    basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons2d, lats2d=lats2d)
    rObj.close()

    x, y = basemap(lons2d, lats2d)

    plt.figure()
    ax = plt.gca()
    # the_cmap = cm.get_cmap(name = "gist_earth", lut=len(levs) -1)
    # the_cmap = my_colormaps.get_cmap_from_ncl_spec_file(path="colormap_files/topo_15lev.rgb")
    the_cmap = my_colormaps.get_cmap_from_ncl_spec_file(
        path="colormap_files/OceanLakeLandSnow.rgb", ncolors=len(levs) - 1
    )

    # new_cm = matplotlib.colors.LinearSegmentedColormap('colormap',new_dict,len(levs) - 1)

    me_to_plot = maskoceans(lons2d, lats2d, me_to_plot, resolution="l")
    basemap.contourf(x, y, me_to_plot, cmap=the_cmap, levels=levs, norm=norm)

    # basemap.fillcontinents(color = "none", lake_color="aqua")
    basemap.drawmapboundary(fill_color="#479BF9")
    basemap.drawcoastlines(linewidth=0.5)
    basemap.drawmeridians(np.arange(-180, 180, 20), labels=[1, 0, 0, 1])
    basemap.drawparallels(np.arange(45, 75, 15), labels=[1, 0, 0, 1])

    basemap.readshapefile("data/shape/contour_bv_MRCC/Bassins_MRCC_latlon", name="basin", linewidth=1)

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "5%", pad="3%")
    plt.colorbar(ticks=levs, cax=cax)

    # basemap.scatter(x, y, color="k", s=1, linewidths=0, ax=ax, zorder=2)

    margin = 20
    x1 = x[margin, margin]
    x2 = x[-margin, margin]
    y1 = y[margin, margin]
    y2 = y[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))

    # plt.tight_layout()
    # plt.show()
    plt.savefig("free_domain_260x260.png", dpi=cpp.FIG_SAVE_DPI)

    pass
コード例 #7
0
def main(months = None):
    start_year = 1979
    end_year = 1988


    import matplotlib.pyplot as plt
    fig = plt.figure()


    var_name_to_ij = {
        "TT": (1,0),
        "PR": (2,0),
        "AH": (1,1),
        "AV": (2,1),
        "STFL": (0,1),
        "TRAF": (1, 2),
        "TDRA" : (2, 2)
    }

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

    dmManager = Crcm5ModelDataManager(samples_folder_path=rpn_folder, file_name_prefix="dm", all_files_in_samples_folder=True)
    basemap = dmManager.get_rotpole_basemap()
    lons, lats = dmManager.lons2D, dmManager.lats2D
    x, y = basemap(lons, lats)

    lkfr = dmManager.lake_fraction



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

    #fig.suptitle("{0} minus {1}".format(sim_name2, sim_name1))
    month_dates = [datetime(2001, m, 1) for m in months]

    ax = None
    for var_name in field_names:


        coef = 1
        if var_name == "PR":
            coef = 24 * 60 * 60 * 1000
        elif var_name in ["TDRA", "TRAF"]:
            coef = 24 * 60 * 60
        elif var_name == "AV":
            coef = 3 * 60 * 60
        #cmap.set_bad("0.6")
        print(var_name)
        v1 = _get_values(sim_name1, var_name, start_year, end_year, months = months)
        v2 = _get_values(sim_name2, var_name, start_year, end_year, months = months)

        #calculate annual means, for each year
        v1 = v1.mean(axis=1)
        v1m = v1.mean(axis = 0)

        v2 = v2.mean(axis = 1)
        v2m = v2.mean(axis = 0)

        i,j = var_name_to_ij[var_name]

        ax = fig.add_subplot(gs[i,j])
        dv = (v2m -  v1m) * coef

        t, p = stats.ttest_ind(v1, v2)


        if var_name in ["STFL"]:
            dv = np.ma.masked_where((lkfr >= 0.6), dv)

            print(months)

            i_interest, j_interest = np.where(dv > 53)
            print(i_interest, j_interest)
            dv = maskoceans(lons, lats, dv)
            print(10 * "*")
        elif var_name in ["TDRA", "TRAF"]:
            dv = maskoceans(lons, lats, dv)
        else:
            pass

        dv = np.ma.masked_where(p > 1, dv) #mask changes not significant to the 10 % level

        if not np.all(dv.mask):
            print("{0}: min = {1}; max = {2}".format(var_name, dv.min(), dv.max()))
            print("")
            max_abs = np.ma.max(np.abs(dv))
            delta = max_abs
            the_power = np.log10(delta)

            the_power = np.ceil(the_power) if the_power >= 0 else np.floor(the_power)




            delta = np.ceil( delta / 10.0 ** the_power ) * 10.0 ** the_power
            while delta > 2 * max_abs:
                delta /= 2.0


            while 0.8 * delta > max_abs:
                delta *= 0.8



            step = delta / 5.0 #10 ** (the_power - 1)  * 2 if the_power >= 0 else 10 ** the_power * 2



            if var_name == "TT":
                step = 0.06
                delta = 0.66


            levels = np.arange(-delta, delta + step, step)

            cmap = my_colormaps.get_cmap_from_ncl_spec_file(path="colormap_files/BlueRed.rgb", ncolors=len(levels) - 1)



            bn = BoundaryNorm(levels, cmap.N) if len(levels) > 0 else None

            print("delta={0}; step={1}; the_power={2}".format(delta, step, the_power))

            the_img = basemap.pcolormesh(x, y, dv, cmap=cmap, vmin = -delta, vmax = delta, norm = bn)



            #add colorbar
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", "5%", pad="3%")
            cb = plt.colorbar(the_img,cax = cax, format = fmt)


        #coast lines
        basemap.drawcoastlines(ax = ax, linewidth=0.5)
        assert isinstance(ax, Axes)
        ax.set_title( "$\Delta$ " + field_name_to_long_name[var_name] + "\n" + "({0})".format(field_name_to_units[var_name]))



    bbox_props = dict(boxstyle="rarrow,pad=0.3", fc="wheat", ec="b", lw=2)
    label = "-".join([d.strftime("%b") for d in month_dates]) + "\n({0}-{1})".format(start_year, end_year)
    ax.annotate(label, xy = (0.1, 0.85), xycoords = "figure fraction", bbox = bbox_props,
        font_properties = FontProperties(size=15, weight="bold"))


    #fig.tight_layout()
    if months is None:
        fig.savefig("annual_mean_diffs_{0}_minus_{1}.jpeg".format(sim_name2, sim_name1))
    else:
        print(month_dates, months)
        fig.savefig("seasonal_mean_{0}_diffs_{1}_minus_{2}.png".format("_".join(map(str, months)),
            sim_name2, sim_name1) )
    pass
コード例 #8
0
def main():
    var_name_liquid = "I1"
    var_name_solid = "I2"
    #peirod of interest
    start_year = 1979
    end_year = 1988

    #spatial averaging will be done over upstream points to the stations
    selected_ids = [
        "092715", "080101", "074903", "050304", "080104", "081007", "061905",
        "041903", "040830", "093806", "090613", "081002", "093801", "080718"
    ]

    selected_ids = ["074903"]

    #simulation names corresponding to the paths
    sim_names = ["crcm5-hcd-rl", "crcm5-hcd-rl-intfl"]

    sim_labels = [x.upper() for x in sim_names]

    colors = ["blue", "violet"]

    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
    ]

    layer_depths = np.cumsum(layer_widths)

    paths = [
        "/home/huziy/skynet3_rech1/from_guillimin/new_outputs/quebec_0.1_crcm5-hcd-rl_spinup",
        "/home/huziy/skynet3_rech1/from_guillimin/new_outputs/quebec_0.1_crcm5-hcd-rl-intfl_spinup2/Samples_all_in_one"
    ]

    seasons = [[12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]

    season_names = ["DJF", "MAM", "JJA", "SON"]

    managers = [
        Crcm5ModelDataManager(samples_folder_path=path,
                              file_name_prefix="pm",
                              all_files_in_samples_folder=True,
                              need_cell_manager=(i == 0))
        for i, path in enumerate(paths)
    ]

    #share the cell manager
    a_data_manager = managers[0]
    assert isinstance(a_data_manager, Crcm5ModelDataManager)
    cell_manager = a_data_manager.cell_manager
    assert isinstance(cell_manager, CellManager)
    for m in managers[1:]:
        assert isinstance(m, Crcm5ModelDataManager)
        m.cell_manager = cell_manager

    #share the lake fraction field
    lake_fraction = a_data_manager.lake_fraction

    #selected_ids = ["092715", "080101", "074903", "050304", "080104", "081007", "061905",
    #                  "041903", "040830", "093806", "090613", "081002", "093801", "080718"]
    start_date = datetime(start_year, 1, 1)
    end_date = datetime(end_year, 12, 31)

    stations = cehq_station.read_station_data(selected_ids=selected_ids,
                                              start_date=start_date,
                                              end_date=end_date)

    #stations with corresponding model points
    station_to_mp = a_data_manager.get_dataless_model_points_for_stations(
        stations)

    #figure out levels in soil

    sim_label_to_profiles = {}
    for s, mp in station_to_mp.items():
        assert isinstance(mp, ModelPoint)
        mask = (mp.flow_in_mask == 1) & (lake_fraction < 0.6)
        fig = plt.figure()
        fmt = ScalarFormatter(useMathText=True)
        fmt.set_powerlimits([-2, 2])

        print(mp.ix, mp.jy, s.id)

        for m, label, color in zip(managers, sim_labels, colors):
            assert isinstance(m, Crcm5ModelDataManager)

            monthly_means_liquid = _get_cached_monthly_mean_fields(
                label, start_year, end_year, var_name_liquid)
            if monthly_means_liquid is None:
                monthly_means_liquid = m.get_monthly_climatology_of_3d_field(
                    var_name=var_name_liquid,
                    start_year=start_year,
                    end_year=end_year)
                _cache_monthly_mean_fields(monthly_means_liquid, label,
                                           start_year, end_year,
                                           var_name_liquid)

            monthly_means_solid = _get_cached_monthly_mean_fields(
                label, start_year, end_year, var_name_solid)
            if monthly_means_solid is None:
                monthly_means_solid = m.get_monthly_climatology_of_3d_field(
                    var_name=var_name_solid,
                    start_year=start_year,
                    end_year=end_year)
                _cache_monthly_mean_fields(monthly_means_solid, label,
                                           start_year, end_year,
                                           var_name_solid)

            profiles = [
                monthly_means_liquid[i][mask, :].mean(axis=0) +
                monthly_means_solid[i][mask, :].mean(axis=0) for i in range(12)
            ]

            sim_label_to_profiles[label] = np.array(profiles)

        x = [date2num(datetime(2001, month, 1)) for month in range(1, 13)]
        y = layer_depths

        y2d, x2d = np.meshgrid(y, x)
        delta = (sim_label_to_profiles[sim_labels[1]] -
                 sim_label_to_profiles[sim_labels[0]]
                 ) / sim_label_to_profiles[sim_labels[0]] * 100

        #delta = np.ma.masked_where(delta < 0.1, delta)

        cmap = my_colormaps.get_cmap_from_ncl_spec_file(
            path="colormap_files/BlueRed.rgb", ncolors=10)
        the_min = -6.0
        the_max = 6.0
        step = (the_max - the_min) / float(cmap.N)

        plt.pcolormesh(x2d[:, :8],
                       y2d[:, :8],
                       delta[:, :8],
                       cmap=cmap,
                       vmin=the_min,
                       vmax=the_max)  #, levels = np.arange(-6,7,1))
        plt.gca().invert_yaxis()
        plt.colorbar(ticks=np.arange(the_min, the_max + step, step))
        plt.gca().set_ylabel("Depth (m)")

        plt.gca().xaxis.set_major_formatter(DateFormatter("%b"))

        #fig.tight_layout()
        fig.savefig("soil_profile_upstream_of_{0}.pdf".format(s.id))

    pass
コード例 #9
0
ファイル: plot_mean_runoff.py プロジェクト: guziy/RPN
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)