示例#1
0
def calculate_seasonal_mean_winds(seasons=commons.default_seasons,
                                  level_hpa=None,
                                  samples_folder=None,
                                  file_prefix="dp"):

    cache_file_name = "seasonal_wind_components_{}_{}hPa_{}.npz".format(
        "-".join(seasons.keys()), level_hpa,
        hashlib.sha224(str(samples_folder).encode()).hexdigest())

    cache_file = Path(cache_file_name)

    if cache_file.is_file():
        print("Using cached winds from {}".format(cache_file))
        return np.load(cache_file_name)["arr_0"]

    u_season_to_mean = OrderedDict()
    v_season_to_mean = OrderedDict()
    r = None
    for sname, months in seasons.items():

        # find files for the season
        paths = []
        for m in months:
            paths.extend(
                glob.glob(
                    str(
                        samples_folder.joinpath("*{:02d}/{}*".format(
                            m, file_prefix)))))

        r = MultiRPN(paths)

        u_data = r.get_4d_field(varname="UU")
        v_data = r.get_4d_field(varname="VV")

        u_season_to_mean[sname] = np.array(
            [v[level_hpa] for k, v in u_data.items()]).mean(axis=0)
        v_season_to_mean[sname] = np.array(
            [v[level_hpa] for k, v in v_data.items()]).mean(axis=0)
        print("Processed: {}".format(sname))

    lons2d, lats2d = r.get_longitudes_and_latitudes_of_the_last_read_rec()
    np.savez_compressed(cache_file_name,
                        [lons2d, lats2d, u_season_to_mean, v_season_to_mean])
    return lons2d, lats2d, u_season_to_mean, v_season_to_mean
示例#2
0
def test_get_4d_field():
    r = None
    vname = "T"

    try:

        create_files_with_same_var_for_different_times(vname=vname)
        r = MultiRPN("test_?.rpn")

        recs = r.get_4d_field(vname)
        msg = "Not all records for {} were found".format(vname)
        print(recs.keys())
        tools.assert_equals(len(FILE_NAMES), len(recs), msg)
    finally:

        if r is not None:
            r.close()

        delete_files()
示例#3
0
def calculate_seasonal_mean_winds(seasons=commons.default_seasons, level_hpa=None, samples_folder=None, file_prefix="dp"):


    cache_file_name = "seasonal_wind_components_{}_{}hPa_{}.npz".format("-".join(seasons.keys()), level_hpa,
                                                                        hashlib.sha224(str(samples_folder).encode()).hexdigest())

    cache_file = Path(cache_file_name)


    if cache_file.is_file():
        print("Using cached winds from {}".format(cache_file))
        return np.load(cache_file_name)["arr_0"]


    u_season_to_mean = OrderedDict()
    v_season_to_mean = OrderedDict()
    r = None
    for sname, months in seasons.items():

        # find files for the season
        paths = []
        for m in months:
            paths.extend(glob.glob(str(samples_folder.joinpath("*{:02d}/{}*".format(m, file_prefix)))))


        r = MultiRPN(paths)

        u_data = r.get_4d_field(varname="UU")
        v_data = r.get_4d_field(varname="VV")

        u_season_to_mean[sname] = np.array([v[level_hpa] for k, v in u_data.items()]).mean(axis=0)
        v_season_to_mean[sname] = np.array([v[level_hpa] for k, v in v_data.items()]).mean(axis=0)
        print("Processed: {}".format(sname))

    lons2d, lats2d = r.get_longitudes_and_latitudes_of_the_last_read_rec()
    np.savez_compressed(cache_file_name, [lons2d, lats2d, u_season_to_mean, v_season_to_mean])
    return lons2d, lats2d, u_season_to_mean, v_season_to_mean
示例#4
0
def plot_seasonal_integrated_water_vapour(samples_folder, seasons=commons.default_seasons):

    # TODO: make the integrations
    vname = "HU"
    file_prefix = "dm"

    plot_units = "kg/kg"
    mult_coeff = 1
    add_offset = 0

    out_dx = 0.5



    season_to_mean = OrderedDict()
    r = None
    for sname, months in seasons.items():

        # find files for the season
        paths = []
        for m in months:
            paths.extend(glob.glob(str(samples_folder.joinpath("*{:02d}/{}*".format(m, file_prefix)))))


        r = MultiRPN(paths)

        data = r.get_4d_field(varname=vname)
        season_to_mean[sname] = np.array([list(v.items())[0][1] for k, v in data.items()]).mean(axis=0)
        print("Processed: {}".format(sname))

    lons2d, lats2d = r.get_longitudes_and_latitudes_of_the_last_read_rec()



    img_folder = samples_folder.joinpath("images/seasonal")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_file = img_folder.joinpath("{}.png".format(vname))

    plot_utils.apply_plot_params(width_cm=25, height_cm=15, font_size=18)
    fig = plt.figure()
    ncols = 2
    gs = GridSpec(len(season_to_mean) // ncols + int(not (len(season_to_mean) % ncols == 0)), ncols, wspace=0, hspace=0)
    xx, yy = None, None
    bmp = Basemap(projection="robin", lon_0=0)


    nlevs = 20
    clevs = None
    for i, (sname, field) in enumerate(season_to_mean.items()):
        row = i // ncols
        col = i % ncols

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

        lons, lats, data_out = commons.interpolate_to_uniform_global_grid(field, lons_in=lons2d, lats_in=lats2d, out_dx=out_dx)

        if xx is None:
            xx, yy = bmp(lons, lats)


        cs = bmp.contourf(xx, yy, data_out * mult_coeff, nlevs if clevs is None else clevs, cmap=cm_basemap.s3pcpn_l, extend="max")

        # save color levels for next subplots
        clevs = cs.levels

        print(np.max(data_out * mult_coeff))

        ax.set_title(sname)
        cb = plt.colorbar(cs, ax=ax)
        if not (row == 0 and col == ncols - 1):
            # cb.ax.set_title(plot_units)
            cb.ax.set_visible(False)
        bmp.drawcoastlines(ax=ax, linewidth=LINEWIDTH)

    fig.suptitle("Precipitation, {}".format(plot_units))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight")
    plt.close(fig)
示例#5
0
def plot_seasonal_vertical_velocity(samples_folder, seasons=commons.default_seasons):
    level = 850  # millibars
    vname = "WW"

    long_name = "Vertical compoent of wind velocity ({}mb)".format(level)
    file_prefix = "dp"

    plot_units = "Pa/s"
    mult_coeff = 1
    add_offset = 0

    out_dx = 0.5

    file_format = "png"



    season_to_mean = OrderedDict()
    r = None
    for sname, months in seasons.items():

        # find files for the season
        paths = []
        for m in months:
            paths.extend(glob.glob(str(samples_folder.joinpath("*{:02d}/{}*".format(m, file_prefix)))))


        r = MultiRPN(paths)

        data = r.get_4d_field(varname=vname)
        season_to_mean[sname] = np.array([v[level] for k, v in data.items()]).mean(axis=0)
        print("Processed: {}".format(sname))

    lons2d, lats2d = r.get_longitudes_and_latitudes_of_the_last_read_rec()



    img_folder = samples_folder.joinpath("images/seasonal")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)


    img_file = img_folder.joinpath("{}_{}hPa.{}".format(vname, level, file_format))

    plot_utils.apply_plot_params(width_cm=25, height_cm=15, font_size=18)
    fig = plt.figure()
    ncols = 2
    gs = GridSpec(len(season_to_mean) // ncols + int(not (len(season_to_mean) % ncols == 0)), ncols, wspace=0, hspace=0)
    xx, yy = None, None
    bmp = Basemap(projection="robin", lon_0=0)



    cmap = cm_basemap.GMT_polar

    clevs = np.arange(-0.1, 0.11, 0.01)


    for i, (sname, field) in enumerate(season_to_mean.items()):
        row = i // ncols
        col = i % ncols

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

        lons, lats, data_out = commons.interpolate_to_uniform_global_grid(field, lons_in=lons2d, lats_in=lats2d, out_dx=out_dx)

        if xx is None:
            xx, yy = bmp(lons, lats)


        cs = bmp.contourf(xx, yy, data_out * mult_coeff, 20 if clevs is None else clevs, cmap=cmap, extend="both")

        # save color levels for next subplots
        clevs = cs.levels

        print(np.max(data_out * mult_coeff))

        ax.set_title(sname)
        cb = plt.colorbar(cs, ax=ax)
        if not (row == 0 and col == ncols - 1):
            # cb.ax.set_title(plot_units)
            cb.ax.set_visible(False)
        bmp.drawcoastlines(ax=ax, linewidth=LINEWIDTH)

    fig.suptitle("{}, {}".format(long_name, plot_units))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight", format=file_format)

    plt.close(fig)
示例#6
0
def plot_seasonal_integrated_water_vapour(samples_folder,
                                          seasons=commons.default_seasons):

    # TODO: make the integrations
    vname = "HU"
    file_prefix = "dm"

    plot_units = "kg/kg"
    mult_coeff = 1
    add_offset = 0

    out_dx = 0.5

    season_to_mean = OrderedDict()
    r = None
    for sname, months in seasons.items():

        # find files for the season
        paths = []
        for m in months:
            paths.extend(
                glob.glob(
                    str(
                        samples_folder.joinpath("*{:02d}/{}*".format(
                            m, file_prefix)))))

        r = MultiRPN(paths)

        data = r.get_4d_field(varname=vname)
        season_to_mean[sname] = np.array(
            [list(v.items())[0][1] for k, v in data.items()]).mean(axis=0)
        print("Processed: {}".format(sname))

    lons2d, lats2d = r.get_longitudes_and_latitudes_of_the_last_read_rec()

    img_folder = samples_folder.joinpath("images/seasonal")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_file = img_folder.joinpath("{}.png".format(vname))

    plot_utils.apply_plot_params(width_cm=25, height_cm=15, font_size=18)
    fig = plt.figure()
    ncols = 2
    gs = GridSpec(len(season_to_mean) // ncols +
                  int(not (len(season_to_mean) % ncols == 0)),
                  ncols,
                  wspace=0,
                  hspace=0)
    xx, yy = None, None
    bmp = Basemap(projection="robin", lon_0=0)

    nlevs = 20
    clevs = None
    for i, (sname, field) in enumerate(season_to_mean.items()):
        row = i // ncols
        col = i % ncols

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

        lons, lats, data_out = commons.interpolate_to_uniform_global_grid(
            field, lons_in=lons2d, lats_in=lats2d, out_dx=out_dx)

        if xx is None:
            xx, yy = bmp(lons, lats)

        cs = bmp.contourf(xx,
                          yy,
                          data_out * mult_coeff,
                          nlevs if clevs is None else clevs,
                          cmap=cm_basemap.s3pcpn_l,
                          extend="max")

        # save color levels for next subplots
        clevs = cs.levels

        print(np.max(data_out * mult_coeff))

        ax.set_title(sname)
        cb = plt.colorbar(cs, ax=ax)
        if not (row == 0 and col == ncols - 1):
            # cb.ax.set_title(plot_units)
            cb.ax.set_visible(False)
        bmp.drawcoastlines(ax=ax, linewidth=LINEWIDTH)

    fig.suptitle("Precipitation, {}".format(plot_units))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight")
    plt.close(fig)
示例#7
0
def plot_seasonal_vertical_velocity(samples_folder,
                                    seasons=commons.default_seasons):
    level = 850  # millibars
    vname = "WW"

    long_name = "Vertical compoent of wind velocity ({}mb)".format(level)
    file_prefix = "dp"

    plot_units = "Pa/s"
    mult_coeff = 1
    add_offset = 0

    out_dx = 0.5

    file_format = "png"

    season_to_mean = OrderedDict()
    r = None
    for sname, months in seasons.items():

        # find files for the season
        paths = []
        for m in months:
            paths.extend(
                glob.glob(
                    str(
                        samples_folder.joinpath("*{:02d}/{}*".format(
                            m, file_prefix)))))

        r = MultiRPN(paths)

        data = r.get_4d_field(varname=vname)
        season_to_mean[sname] = np.array([v[level] for k, v in data.items()
                                          ]).mean(axis=0)
        print("Processed: {}".format(sname))

    lons2d, lats2d = r.get_longitudes_and_latitudes_of_the_last_read_rec()

    img_folder = samples_folder.joinpath("images/seasonal")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_file = img_folder.joinpath("{}_{}hPa.{}".format(
        vname, level, file_format))

    plot_utils.apply_plot_params(width_cm=25, height_cm=15, font_size=18)
    fig = plt.figure()
    ncols = 2
    gs = GridSpec(len(season_to_mean) // ncols +
                  int(not (len(season_to_mean) % ncols == 0)),
                  ncols,
                  wspace=0,
                  hspace=0)
    xx, yy = None, None
    bmp = Basemap(projection="robin", lon_0=0)

    cmap = cm_basemap.GMT_polar

    clevs = np.arange(-0.1, 0.11, 0.01)

    for i, (sname, field) in enumerate(season_to_mean.items()):
        row = i // ncols
        col = i % ncols

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

        lons, lats, data_out = commons.interpolate_to_uniform_global_grid(
            field, lons_in=lons2d, lats_in=lats2d, out_dx=out_dx)

        if xx is None:
            xx, yy = bmp(lons, lats)

        cs = bmp.contourf(xx,
                          yy,
                          data_out * mult_coeff,
                          20 if clevs is None else clevs,
                          cmap=cmap,
                          extend="both")

        # save color levels for next subplots
        clevs = cs.levels

        print(np.max(data_out * mult_coeff))

        ax.set_title(sname)
        cb = plt.colorbar(cs, ax=ax)
        if not (row == 0 and col == ncols - 1):
            # cb.ax.set_title(plot_units)
            cb.ax.set_visible(False)
        bmp.drawcoastlines(ax=ax, linewidth=LINEWIDTH)

    fig.suptitle("{}, {}".format(long_name, plot_units))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight", format=file_format)

    plt.close(fig)
示例#8
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)
示例#9
0
def get_daily_clim_profiles(samples_dir, start_year=-np.Inf, end_year=np.Inf, filename_prefix="dp", varname="", mask=None):

    """
    dates_2d, levels_2d, values - needed for plotting
    :param start_year:
    :param end_year:
    :param filename_prefix:
    :param varname:
    """


    dates_sorted = None
    levels_sorted = None
    yearly_fields = []

    day = timedelta(days=1)
    stamp_dates = [datetime(2001, 1, 1) + i * day for i in range(365)]

    for y in range(start_year, end_year + 1):
        files_for_year = []

        mfolders = [os.path.join(samples_dir, f) for f in os.listdir(samples_dir) if f[:-2].endswith(str(y))]

        for mfolder in mfolders:
            files_for_year += [os.path.join(mfolder, fn) for fn in os.listdir(mfolder) if fn.startswith(filename_prefix)]


        mrpn = MultiRPN(files_for_year)
        data = mrpn.get_4d_field(varname=varname)

        # calculate the area-average
        for d, lev_to_field in data.items():
            for lev in lev_to_field:
                lev_to_field[lev] = lev_to_field[lev][mask].mean()

        #
        dates_sorted = list(sorted([d for d in data if not (d.month == 2 and d.day == 29) and (d.year == y)]))
        levels_sorted = list(sorted([lev for lev in data[dates_sorted[0]] if lev >= 500]))

        #
        year_field = np.zeros((len(dates_sorted), len(levels_sorted)))
        for j, lev in enumerate(levels_sorted):
            for i, the_date in enumerate(dates_sorted):
                year_field[i, j] = data[the_date][lev]

        panel = pd.DataFrame(data=year_field, index=dates_sorted, columns=range(len(levels_sorted)))

        daily = panel.groupby(by=lambda d: datetime(d.year, d.month, d.day)).mean()


        yearly_fields.append(daily.values)

    values = np.mean(yearly_fields, axis=0)


    assert not hasattr(values, "mask")

    dates_num = date2num(stamp_dates)

    levs_2d, dates_num_2d = np.meshgrid(levels_sorted, dates_num)

    assert levs_2d.shape == values.shape, "levels and values shapes ({} and {}, respectively) are not consistent.".format(levs_2d.shape, values.shape)

    return dates_num_2d, levs_2d, values
示例#10
0
def main():
    plot_utils.apply_plot_params(width_cm=40, height_cm=25, font_size=14)
    seasons = commons.default_seasons
    dx_plotting = 0.5  # degrees

    # units = "$^\circ$C"
    # long_name = "SST, {}".format(units)
    # var_name = "TM"
    # clevs_diff = np.arange(-5, 5.5, 0.5)

    units = ""
    long_name = "Sea ice {}".format(units)
    var_name = "LG"
    clevs_diff = [v for v in np.arange(-1, 1.1, 0.1) if abs(v) > 1.0e-6]

    label_to_folder = OrderedDict([
        ("ERA-Interim",
         "/home/huziy/skynet3_rech1/CNRCWP/Calgary_flood/SST_SeaIce/I_SST_SeaIce"
         ),
        ("PreI-CanESM2",
         "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/SST_SeaIce/PreI_SST_SeaIce"
         ),
        ("PreI-GFDL",
         "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/SST_SeaIce/PreI_SST_SeaIce"
         ),
        ("PreI-GISS",
         "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/SST_SeaIce/PreI_SST_SeaIce"
         )
    ])

    bmp = Basemap(projection="robin", lon_0=180)
    xx, yy = None, None

    year = 2013

    print(seasons)

    label_to_season_to_field = OrderedDict()
    for label, folder in label_to_folder.items():
        label_to_season_to_field[label] = OrderedDict()
        all_files = [
            f for f in os.listdir(folder) if label.lower() in f.lower()
            or label.split("-")[1].lower() in f.lower()
        ]

        for sname, months in seasons.items():
            season_files = [
                f for f in all_files
                if _get_month_and_year_from_file_name(f)[0] in months
                and _get_month_and_year_from_file_name(f)[1] == year
            ]

            season_files = [os.path.join(folder, f) for f in season_files]

            print(10 * "+" + sname + "+" * 10)
            print(season_files)

            r = MultiRPN(season_files)

            data = r.get_4d_field(var_name)

            the_mean = np.array(
                [list(v.items())[0][1] for d, v in data.items()]).mean(axis=0)

            lons, lats = r.get_longitudes_and_latitudes_of_the_last_read_rec()
            print("lon range", lons.min(), lons.max())
            print("lat range", lats.min(), lats.max())

            # lons, lats, the_mean = commons.interpolate_to_uniform_global_grid(the_mean, lons_in=lons, lats_in=lats, out_dx=dx_plotting)

            # mask land
            lons1 = lons.copy()
            lons1[lons1 > 180] -= 360
            ocean_mask = maskoceans(lons1, lats, np.zeros_like(the_mean))
            the_mean = np.ma.masked_where(~ocean_mask.mask, the_mean)

            if xx is None:
                xx, yy = bmp(lons, lats)

            label_to_season_to_field[label][sname] = the_mean

    # plotting------

    fig = plt.figure()

    gs = GridSpec(len(label_to_season_to_field),
                  len(seasons) + 1,
                  width_ratios=[
                      1.0,
                  ] * len(seasons) + [
                      0.05,
                  ])

    #
    base_fields = None
    base_label = None

    for row, (label,
              season_to_mean) in enumerate(label_to_season_to_field.items()):

        print(gs.get_geometry())

        axes = [
            fig.add_subplot(gs[row, col])
            for col in range(len(season_to_mean) + 1)
        ]

        common_params = dict(axes=axes, bmp=bmp, xx=xx, yy=yy)
        if base_fields is None:
            plot_row(row,
                     season_to_mean=season_to_mean,
                     label=label,
                     **common_params)
            base_fields = season_to_mean
            base_label = label
        else:
            plot_row(row,
                     season_to_mean=OrderedDict([
                         (k, v - base_fields[k])
                         for k, v in season_to_mean.items()
                     ]),
                     difference=True,
                     clevs=clevs_diff,
                     label="{}\n-\n{}".format(label, base_label),
                     **common_params)

    fig.suptitle(long_name)
    img_folder = Path("industrial_and_preindustrial_sst").joinpath("seasonal")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_format = "png"
    img_file = img_folder.joinpath("{}.{}".format(var_name, img_format))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight", format=img_format)
示例#11
0
def get_daily_clim_profiles(samples_dir,
                            start_year=-np.Inf,
                            end_year=np.Inf,
                            filename_prefix="dp",
                            varname="",
                            mask=None):
    """
    dates_2d, levels_2d, values - needed for plotting
    :param start_year:
    :param end_year:
    :param filename_prefix:
    :param varname:
    """

    dates_sorted = None
    levels_sorted = None
    yearly_fields = []

    day = timedelta(days=1)
    stamp_dates = [datetime(2001, 1, 1) + i * day for i in range(365)]

    for y in range(start_year, end_year + 1):
        files_for_year = []

        mfolders = [
            os.path.join(samples_dir, f) for f in os.listdir(samples_dir)
            if f[:-2].endswith(str(y))
        ]

        for mfolder in mfolders:
            files_for_year += [
                os.path.join(mfolder, fn) for fn in os.listdir(mfolder)
                if fn.startswith(filename_prefix)
            ]

        mrpn = MultiRPN(files_for_year)
        data = mrpn.get_4d_field(varname=varname)

        # calculate the area-average
        for d, lev_to_field in data.items():
            for lev in lev_to_field:
                lev_to_field[lev] = lev_to_field[lev][mask].mean()

        #
        dates_sorted = list(
            sorted([
                d for d in data
                if not (d.month == 2 and d.day == 29) and (d.year == y)
            ]))
        levels_sorted = list(
            sorted([lev for lev in data[dates_sorted[0]] if lev >= 500]))

        #
        year_field = np.zeros((len(dates_sorted), len(levels_sorted)))
        for j, lev in enumerate(levels_sorted):
            for i, the_date in enumerate(dates_sorted):
                year_field[i, j] = data[the_date][lev]

        panel = pd.DataFrame(data=year_field,
                             index=dates_sorted,
                             columns=range(len(levels_sorted)))

        daily = panel.groupby(
            by=lambda d: datetime(d.year, d.month, d.day)).mean()

        yearly_fields.append(daily.values)

    values = np.mean(yearly_fields, axis=0)

    assert not hasattr(values, "mask")

    dates_num = date2num(stamp_dates)

    levs_2d, dates_num_2d = np.meshgrid(levels_sorted, dates_num)

    assert levs_2d.shape == values.shape, "levels and values shapes ({} and {}, respectively) are not consistent.".format(
        levs_2d.shape, values.shape)

    return dates_num_2d, levs_2d, values
示例#12
0
def main():
    plot_utils.apply_plot_params(width_cm=40, height_cm=25, font_size=14)
    seasons = commons.default_seasons
    dx_plotting = 0.5  # degrees

    # units = "$^\circ$C"
    # long_name = "SST, {}".format(units)
    # var_name = "TM"
    # clevs_diff = np.arange(-5, 5.5, 0.5)


    units = ""
    long_name = "Sea ice {}".format(units)
    var_name = "LG"
    clevs_diff = [v for v in np.arange(-1, 1.1, 0.1) if abs(v) > 1.0e-6]


    label_to_folder = OrderedDict([
        ("ERA-Interim", "/home/huziy/skynet3_rech1/CNRCWP/Calgary_flood/SST_SeaIce/I_SST_SeaIce"),
        ("PreI-CanESM2", "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/SST_SeaIce/PreI_SST_SeaIce"),
        ("PreI-GFDL", "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/SST_SeaIce/PreI_SST_SeaIce"),
        ("PreI-GISS", "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/SST_SeaIce/PreI_SST_SeaIce")
    ])




    bmp = Basemap(projection="robin", lon_0=180)
    xx, yy = None, None

    year = 2013

    print(seasons)

    label_to_season_to_field = OrderedDict()
    for label, folder in label_to_folder.items():
        label_to_season_to_field[label] = OrderedDict()
        all_files = [f for f in os.listdir(folder) if label.lower() in f.lower() or label.split("-")[1].lower() in f.lower()]

        for sname, months in seasons.items():
            season_files = [f for f in all_files if _get_month_and_year_from_file_name(f)[0] in months and _get_month_and_year_from_file_name(f)[1] == year]

            season_files = [os.path.join(folder, f) for f in season_files]

            print(10 * "+" + sname + "+" * 10)
            print(season_files)

            r = MultiRPN(season_files)

            data = r.get_4d_field(var_name)

            the_mean = np.array([list(v.items())[0][1] for d, v in data.items()]).mean(axis=0)


            lons, lats = r.get_longitudes_and_latitudes_of_the_last_read_rec()
            print("lon range", lons.min(), lons.max())
            print("lat range", lats.min(), lats.max())

            # lons, lats, the_mean = commons.interpolate_to_uniform_global_grid(the_mean, lons_in=lons, lats_in=lats, out_dx=dx_plotting)

            # mask land
            lons1 = lons.copy()
            lons1[lons1 > 180] -= 360
            ocean_mask = maskoceans(lons1, lats, np.zeros_like(the_mean))
            the_mean = np.ma.masked_where(~ocean_mask.mask, the_mean)

            if xx is None:
                xx, yy = bmp(lons, lats)


            label_to_season_to_field[label][sname] = the_mean



    # plotting------

    fig = plt.figure()

    gs = GridSpec(len(label_to_season_to_field), len(seasons) + 1, width_ratios=[1.0, ] * len(seasons) + [0.05, ])

    #
    base_fields = None
    base_label = None

    for row, (label, season_to_mean) in enumerate(label_to_season_to_field.items()):

        print(gs.get_geometry())

        axes = [fig.add_subplot(gs[row, col]) for col in range(len(season_to_mean) + 1)]

        common_params = dict(axes=axes, bmp=bmp, xx=xx, yy=yy)
        if base_fields is None:
            plot_row(row, season_to_mean=season_to_mean, label=label, **common_params)
            base_fields = season_to_mean
            base_label = label
        else:
            plot_row(row, season_to_mean=OrderedDict([(k, v - base_fields[k]) for k, v in season_to_mean.items()]),
                     difference=True, clevs=clevs_diff, label="{}\n-\n{}".format(label, base_label),
                     **common_params)


    fig.suptitle(long_name)
    img_folder = Path("industrial_and_preindustrial_sst").joinpath("seasonal")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_format = "png"
    img_file = img_folder.joinpath("{}.{}".format(var_name, img_format))

    with img_file.open("wb") as f:
        fig.savefig(f, bbox_inches="tight", format=img_format)
示例#13
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)