Пример #1
0
def validate_yearmax_ice_cover_from_hostetler_with_glerl(path="/home/huziy/skynet3_rech1/CRCM_GL_simulation/all_files",
                                                         start_year=2003, end_year=2009):
    model_manager = Crcm5ModelDataManager(samples_folder_path=path, all_files_in_samples_folder=True)

    varname = "LC"

    hl_icecov_yearly_max = model_manager.get_yearmax_fields(start_year=start_year, end_year=end_year, var_name=varname)

    hl_icecov_yearly_max_clim = np.mean(list(hl_icecov_yearly_max.values()), axis=0)




    # Get Nemo manager here only for coordinates and mask
    nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
                                          suffix="icemod.nc")

    lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap()

    # Interpolate hostetler's lake fraction to the model's grid
    hl_icecov_yearly_max_clim = model_manager.interpolate_data_to(hl_icecov_yearly_max_clim, lon2d, lat2d)
    hl_icecov_yearly_max_clim = np.ma.masked_where(~nemo_manager.lake_mask, hl_icecov_yearly_max_clim)

    model_lake_avg_ts = []
    for the_year in range(start_year, end_year + 1):
        model_lake_avg_ts.append(hl_icecov_yearly_max[the_year][nemo_manager.lake_mask].mean())



    # plt.figure()
    xx, yy = bmp(lon2d.copy(), lat2d.copy())
    # im = bmp.pcolormesh(xx, yy, model_yearmax_ice_conc)
    # bmp.colorbar(im)

    # Read and interpolate obs
    path_to_obs = "/RESCUE/skynet3_rech1/huziy/nemo_obs_for_validation/glerl_icecov.nc"

    obs_varname = "ice_cover"
    obs_lake_avg_ts = []
    with Dataset(path_to_obs) as ds:
        time_var = ds.variables["time"]

        lons_obs = ds.variables["lon"][:]
        lats_obs = ds.variables["lat"][:]

        dates = num2date(time_var[:], time_var.units)
        nx, ny = lons_obs.shape

        data = ds.variables[obs_varname][:]
        data = np.ma.masked_where((data > 100) | (data < 0), data)
        print(data.min(), data.max())
        panel = pd.Panel(data=data, items=dates, major_axis=range(nx), minor_axis=range(ny))

        panel = panel.select(lambda d: start_year <= d.year <= end_year)
        the_max_list = []
        for key, g in panel.groupby(lambda d: d.year, axis="items"):
            the_max_field = np.ma.max(np.ma.masked_where((g.values > 100) | (g.values < 0), g.values), axis=0)
            obs_lake_avg_ts.append(the_max_field.mean())
            the_max_list.append(the_max_field)

        obs_yearmax_ice_conc = np.ma.mean(the_max_list, axis=0) / 100.0

        xs, ys, zs = lat_lon.lon_lat_to_cartesian(lons_obs.flatten(), lats_obs.flatten())
        ktree = cKDTree(list(zip(xs, ys, zs)))

        xt, yt, zt = lat_lon.lon_lat_to_cartesian(lon2d.flatten(), lat2d.flatten())
        dists, inds = ktree.query(list(zip(xt, yt, zt)))

        obs_yearmax_ice_conc_interp = obs_yearmax_ice_conc.flatten()[inds].reshape(lon2d.shape)
        obs_yearmax_ice_conc_interp = np.ma.masked_where(~nemo_manager.lake_mask, obs_yearmax_ice_conc_interp)


    # plt.figure()
    # b = Basemap()
    # xx, yy = b(lons_obs, lats_obs)
    # im = b.pcolormesh(xx, yy, obs_yearmax_ice_conc)
    # b.colorbar(im)
    # b.drawcoastlines()

    # Plot as usual: model, obs, model - obs
    img_folder = Path("nemo/hostetler")
    if not img_folder.is_dir():
        img_folder.mkdir()
    img_file = img_folder.joinpath("validate_yearmax_icecov_hl_vs_glerl_{}-{}.pdf".format(start_year, end_year))

    fig = plt.figure()
    gs = GridSpec(2, 4, width_ratios=[1, 1, 1, 0.05])
    all_axes = []

    cmap = cm.get_cmap("jet", 10)
    diff_cmap = cm.get_cmap("RdBu_r", 10)

    # Model
    ax = fig.add_subplot(gs[0, 0])
    ax.set_title("Hostetler")
    bmp.pcolormesh(xx, yy, hl_icecov_yearly_max_clim, cmap=cmap, vmin=0, vmax=1)
    all_axes.append(ax)



    # Obs
    ax = fig.add_subplot(gs[0, 2])
    ax.set_title("GLERL")
    im = bmp.pcolormesh(xx, yy, obs_yearmax_ice_conc_interp, cmap=cmap, vmin=0, vmax=1)
    all_axes.append(ax)

    plt.colorbar(im, cax=fig.add_subplot(gs[0, -1]))



    # Biases
    ax = fig.add_subplot(gs[1, :])
    ax.set_title("Hostetler - GLERL")
    im = bmp.pcolormesh(xx, yy, hl_icecov_yearly_max_clim - obs_yearmax_ice_conc_interp,
                        cmap=diff_cmap, vmin=-1, vmax=1)
    bmp.colorbar(im, ax=ax)
    all_axes.append(ax)

    for the_ax in all_axes:
        bmp.drawcoastlines(ax=the_ax)

    fig.savefig(str(img_file), bbox_inches="tight")
    plt.close(fig)


    # Plot lake aversged ice concentrations
    fig = plt.figure()
    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
    plt.plot(range(start_year, end_year + 1), model_lake_avg_ts, "b", lw=2, label="Hostetler")
    plt.plot(range(start_year, end_year + 1), np.asarray(obs_lake_avg_ts) / 100.0, "r", lw=2, label="GLERL")

    plt.grid()
    plt.legend(loc=3)
    fig.savefig(str(img_folder.joinpath("lake_avg_iceconc_hostetler_offline_vs_GLERL.pdf")), bbox_inches="tight")
Пример #2
0
def validate_seas_mean_lswt_from_hostetler_and_nemo_with_homa(
        hl_data_path="/home/huziy/skynet3_rech1/CRCM_GL_simulation/all_files",
        start_year=2003,
        end_year=2003,
        season_to_months=None):
    """
    Note: degrees plotted are in C
    :param hl_data_path:
    :param start_year:
    :param end_year:
    """

    # crcm5_model_manager = Crcm5ModelDataManager(samples_folder_path=hl_data_path, all_files_in_samples_folder=True)

    model_label1 = "CRCM5_HL"
    plot_hl_biases = True

    use_noaa_oisst = True
    obs_label = "NOAA OISST" if use_noaa_oisst else "MODIS"

    clevs = np.arange(0, 22, 1)
    norm = BoundaryNorm(clevs, len(clevs) - 1)
    cmap = cm.get_cmap("viridis", len(clevs) - 1)

    clevs_bias = np.arange(-5.5, 6.5, 1)
    norm_bias = BoundaryNorm(clevs_bias, len(clevs_bias) - 1)
    cmap_bias = cm.get_cmap("bwr", len(clevs_bias) - 1)

    if season_to_months is None:
        season_to_months = OrderedDict([
            ("Winter", [1, 2, 12]),
            ("Spring", [3, 4, 5]),
            ("Summer", range(6, 9)),
            ("Fall", range(9, 11)),
        ])

    # hl_lake_temp_clim = crcm5_model_manager.get_mean_field(start_year=start_year, end_year=end_year, var_name=varname,
    #                                                        level=1.1, months=season_months)

    # Get Nemo manager here only for coordinates and mask
    # nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
    #                                       suffix="icemod.nc")

    # nemo_manager = NemoYearlyFilesManager(folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_5min",
    #                                       suffix="grid_T.nc")

    model_label2 = "CRCM5_NEMO"
    nemo_manager = NemoYearlyFilesManager(
        folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO",
        suffix="grid_T.nc")

    # model_label2 = "NEMO_offline_CRCM5_CanESM2"
    # nemo_manager = NemoYearlyFilesManager(folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3_CC_drivenby_CRCM5_CanESM2_RCP85/EXP00/cc_canesm2_outputs",
    #                                       suffix="grid_T.nc")

    # model_label2 = "NEMO_offline"

    # nemo_manager = NemoYearlyFilesManager(folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_30min",
    #                                       suffix="grid_T.nc")

    img_folder = Path("nemo/{}".format(model_label2))

    #lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap(resolution="l", subregion=[0.06, 0.5, 0.06, 0.5])
    lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap(
        resolution="l", subregion=[0.1, 0.5, 0.15, 0.5], area_thresh=2000)

    xx, yy = bmp(lon2d, lat2d)

    # get nemo and observed sst
    nemo_sst, obs_sst, _, _, _ = nemo_manager.get_nemo_and_homa_seasonal_mean_sst(
        start_year=start_year,
        end_year=end_year,
        season_to_months=season_to_months,
        use_noaa_oisst=use_noaa_oisst)

    obs_sst_clim = {}

    if use_noaa_oisst:
        manager = OISSTManager(thredds_baseurl="/BIG1/huziy/noaa_oisst_daily")
        obs_sst_clim = manager.get_seasonal_clim_interpolate_to(
            lons=lon2d,
            lats=lat2d,
            start_year=start_year,
            end_year=end_year,
            season_to_months=season_to_months,
            vname="sst")

        for season in season_to_months:
            obs_sst_clim[season] = np.ma.masked_where(
                np.isnan(obs_sst_clim[season]), obs_sst_clim[season])

    else:
        # Convert to Celsius
        for season in season_to_months:
            obs_sst_clim[season] = np.ma.mean(
                [obs_sst[y][season] for y in range(start_year, end_year + 1)],
                axis=0) - 273.15

    obs_sst_clim = {
        season: np.ma.masked_where(~nemo_manager.lake_mask,
                                   obs_sst_clim[season])
        for season in obs_sst_clim
    }
    nemo_sst_clim = {
        season: np.ma.mean(
            [nemo_sst[y][season] for y in range(start_year, end_year + 1)],
            axis=0)
        for season in season_to_months
    }
    nemo_sst_clim = {
        season: np.ma.masked_where(~nemo_manager.lake_mask,
                                   nemo_sst_clim[season])
        for season in season_to_months
    }

    hl_sst_clim = {}
    if plot_hl_biases:
        hl_sst_clim = get_seasonal_sst_from_crcm5_outputs(
            model_label1,
            start_year=start_year,
            end_year=end_year,
            season_to_months=season_to_months,
            lons_target=lon2d,
            lats_target=lat2d)

        hl_sst_clim = {
            season: np.ma.masked_where(~nemo_manager.lake_mask,
                                       hl_sst_clim[season])
            for season in season_to_months
        }

        # Convert to C
        hl_sst_clim = {
            season: hl_sst_clim[season] - 273.15
            for season in season_to_months
        }

    # plt.figure()

    # im = bmp.pcolormesh(xx, yy, model_yearmax_ice_conc)
    # bmp.colorbar(im)

    # plt.figure()
    # b = Basemap()
    # xx, yy = b(lons_obs, lats_obs)
    # im = b.pcolormesh(xx, yy, obs_yearmax_ice_conc)
    # b.colorbar(im)
    # b.drawcoastlines()

    # Plot as usual: model, obs, model - obs

    if not img_folder.is_dir():
        img_folder.mkdir()

    img_file = img_folder.joinpath(
        "validate_{}_lswt_{}_vs_{}_{}-{}.png".format(
            "_".join([season for season in season_to_months]),
            "_".join([model_label1 if plot_hl_biases else "",
                      model_label2]), obs_label, start_year, end_year))

    nrows = 3
    plot_utils.apply_plot_params(font_size=8,
                                 width_cm=8 * len(season_to_months),
                                 height_cm=4.5 * nrows)

    fig = plt.figure()

    gs = GridSpec(nrows=nrows,
                  ncols=len(season_to_months),
                  hspace=0.15,
                  wspace=0.000)
    all_axes = []

    # Model, Hostetler
    # ax = fig.add_subplot(gs[0, 0])
    # ax.set_title("Hostetler+CRCM5")
    # bmp.pcolormesh(xx, yy, hl_lake_temp_clim, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    # col += 1
    # all_axes.append(ax)

    for col, season in enumerate(season_to_months):

        row = 0

        # Obs: MODIS or NOAA OISST
        ax = fig.add_subplot(gs[row, col])
        im_obs = bmp.pcolormesh(xx,
                                yy,
                                obs_sst_clim[season],
                                cmap=cmap,
                                norm=norm)
        # obs values
        cb = bmp.colorbar(im_obs, location="bottom")
        cb.ax.set_visible(col == 0)

        ax.text(0.99,
                0.99,
                season,
                va="top",
                ha="right",
                fontsize=16,
                transform=ax.transAxes)
        all_axes.append(ax)
        if col == 0:
            ax.set_ylabel(obs_label)

        row += 1

        if plot_hl_biases:
            #plot CRCM5_HL biases (for poster)
            ax = fig.add_subplot(gs[row, col])

            if col == 0:
                ax.set_ylabel("{}\n-\n{}".format(model_label1, obs_label))

            im_bias = bmp.pcolormesh(xx,
                                     yy,
                                     hl_sst_clim[season] -
                                     obs_sst_clim[season],
                                     cmap=cmap_bias,
                                     norm=norm_bias,
                                     ax=ax)
            cb = bmp.colorbar(im_bias, location="bottom", ax=ax)
            cb.ax.set_visible(False)

            all_axes.append(ax)
            row += 1

        ax = fig.add_subplot(gs[row, col])
        if col == 0:
            ax.set_ylabel("{}\n-\n{}".format(model_label2, obs_label))
        im_bias = bmp.pcolormesh(xx,
                                 yy,
                                 nemo_sst_clim[season] - obs_sst_clim[season],
                                 cmap=cmap_bias,
                                 norm=norm_bias)
        # common for all bias values
        cb = bmp.colorbar(im_bias, location="bottom", ax=ax)
        cb.ax.set_visible(col == 0)

        all_axes.append(ax)
        row += 1

        print_arr_limits(nemo_sst_clim[season],
                         "NEMO_sst, for {}".format(season))
        print_arr_limits(obs_sst_clim[season],
                         "Obs_sst, for {}".format(season))

    for the_ax in all_axes:
        bmp.drawcoastlines(ax=the_ax, linewidth=0.3)
        the_ax.set_frame_on(False)

    print("Saving {}".format(img_file))
    fig.savefig(str(img_file), bbox_inches="tight", dpi=300)
    plt.close(fig)
Пример #3
0
def main_compare_max_yearly_ice_conc():
    """
    ice concentration
    """
    var_name = ""

    start_year = 1979
    end_year = 1985

    SimConfig = namedtuple("SimConfig", "path label")

    base_config = SimConfig("/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012", "ERAI-driven")
    modif_config = SimConfig("/home/huziy/skynet3_rech1/one_way_coupled_nemo_outputs_1979_1985", "CRCM5")

    nemo_manager_base = NemoYearlyFilesManager(folder=base_config.path, suffix="icemod.nc")
    nemo_manager_modif = NemoYearlyFilesManager(folder=modif_config.path, suffix="icemod.nc")

    icecov_base, icecov_ts_base = nemo_manager_base.get_max_yearly_ice_fraction(start_year=start_year,
                                                                                end_year=end_year)

    icecov_modif, icecov_ts_modif = nemo_manager_modif.get_max_yearly_ice_fraction(start_year=start_year,
                                                                                   end_year=end_year)


    lons, lats, bmp = nemo_manager_base.get_coords_and_basemap()
    xx, yy = bmp(lons.copy(), lats.copy())

    # Plot as usual: model, obs, model - obs
    img_folder = Path("nemo/{}vs{}".format(modif_config.label, base_config.label))
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)
    img_file = img_folder.joinpath("compare_yearmax_icecov_{}_vs_{}_{}-{}.pdf".format(
        modif_config.label, base_config.label, start_year, end_year))


    fig = plt.figure()
    gs = GridSpec(2, 3, width_ratios=[1, 1, 0.05])

    cmap = cm.get_cmap("jet", 10)
    diff_cmap = cm.get_cmap("RdBu_r", 10)

    # base
    ax = fig.add_subplot(gs[0, 0])
    cs = bmp.contourf(xx, yy, icecov_base, cmap=cmap)
    bmp.drawcoastlines(ax=ax)
    ax.set_title(base_config.label)

    # modif
    ax = fig.add_subplot(gs[0, 1])
    cs = bmp.contourf(xx, yy, icecov_modif, cmap=cmap, levels=cs.levels)
    plt.colorbar(cs, cax=fig.add_subplot(gs[0, -1]))
    bmp.drawcoastlines(ax=ax)
    ax.set_title(modif_config.label)



    # difference
    ax = fig.add_subplot(gs[1, :])
    cs = bmp.contourf(xx, yy, icecov_modif - icecov_base, cmap=diff_cmap, levels=np.arange(-1, 1.2, 0.2))
    bmp.colorbar(cs, ax=ax)
    bmp.drawcoastlines(ax=ax)


    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")

    ax.set_title("{}-{}".format(modif_config.label, base_config.label))

    plt.close(fig)


    # Plot time series
    img_file = img_folder.joinpath("ts_compare_yearmax_icecov_{}_vs_{}_{}-{}.pdf".format(
        modif_config.label, base_config.label, start_year, end_year))
    fig = plt.figure()


    plt.plot(range(start_year, end_year + 1), icecov_ts_base, "b", lw=2, label=base_config.label)
    plt.plot(range(start_year, end_year + 1), icecov_ts_modif, "r", lw=2, label=modif_config.label)
    plt.legend()
    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
    plt.grid()
    plt.xlabel("Year")

    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")
Пример #4
0
def validate_seas_mean_lswt_from_hostetler_and_nemo_with_homa(
        hl_data_path="/home/huziy/skynet3_rech1/CRCM_GL_simulation/all_files",
        start_year=2003, end_year=2009):
    crcm5_model_manager = Crcm5ModelDataManager(samples_folder_path=hl_data_path, all_files_in_samples_folder=True)

    varname = "sohefldo"
    season = "Summer"

    season_to_months = {season: range(6, 9)}
    season_months = list(season_to_months[season])




    # Get Nemo manager here only for coordinates and mask
    nemo_offline_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
                                                  suffix="grid_T.nc")

    nemo_crcm5_manager = NemoYearlyFilesManager(
        folder="/home/huziy/skynet3_rech1/one_way_coupled_nemo_outputs_1979_1985",
        suffix="grid_T.nc")

    lon2d, lat2d, bmp = nemo_offline_manager.get_coords_and_basemap()

    # Interpolate


    # get nemo sst
    nemo_offline_sst = nemo_offline_manager.get_seasonal_clim_field(start_year=start_year,
                                                                    end_year=end_year,
                                                                    season_to_months=season_to_months,
                                                                    varname=varname)

    nemo_crcm5_sst = nemo_crcm5_manager.get_seasonal_clim_field(start_year=start_year,
                                                                end_year=end_year,
                                                                season_to_months=season_to_months,
                                                                varname=varname)

    nemo_offline_sst = np.ma.masked_where(~nemo_offline_manager.lake_mask, nemo_offline_sst[season])
    nemo_crcm5_sst = np.ma.masked_where(~nemo_offline_manager.lake_mask, nemo_crcm5_sst[season])

    # plt.figure()
    xx, yy = bmp(lon2d.copy(), lat2d.copy())
    # im = bmp.pcolormesh(xx, yy, model_yearmax_ice_conc)
    # bmp.colorbar(im)

    vmin = min(nemo_offline_sst.min(), nemo_crcm5_sst.min())
    vmax = max(nemo_offline_sst.max(), nemo_crcm5_sst.max())

    # plt.figure()
    # b = Basemap()
    # xx, yy = b(lons_obs, lats_obs)
    # im = b.pcolormesh(xx, yy, obs_yearmax_ice_conc)
    # b.colorbar(im)
    # b.drawcoastlines()

    # Plot as usual: model, obs, model - obs
    img_folder = Path("nemo")
    if not img_folder.is_dir():
        img_folder.mkdir()
    img_file = img_folder.joinpath("{}_{}_nemo-offline_vs_nemo-crcm5_{}-{}.png".format(season.lower(),
                                                                                       varname, start_year, end_year))

    fig = plt.figure()
    gs = GridSpec(1, 3, width_ratios=[1, 1, 0.05])
    all_axes = []

    cmap = cm.get_cmap("jet", 10)
    locator = MaxNLocator(nbins=10)
    ticks = locator.tick_values(vmin=vmin, vmax=vmax)
    norm = BoundaryNorm(boundaries=ticks, ncolors=len(ticks) - 1)

    # Model, Hostetler
    ax = fig.add_subplot(gs[0, 0])
    ax.set_title("NEMO+CRCM5")
    bmp.pcolormesh(xx, yy, nemo_crcm5_sst, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    all_axes.append(ax)

    #
    ax = fig.add_subplot(gs[0, 1])
    ax.set_title("NEMO-offline")
    im = bmp.pcolormesh(xx, yy, nemo_offline_sst, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    all_axes.append(ax)


    # Obs: Homa
    # ax = fig.add_subplot(gs[0, 2])
    # ax.set_title("MODIS")
    # im = bmp.pcolormesh(xx, yy, obs_sst_clim, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    # all_axes.append(ax)

    plt.colorbar(im, cax=fig.add_subplot(gs[0, -1]), ticks=ticks)

    for the_ax in all_axes:
        bmp.drawcoastlines(ax=the_ax)

    fig.savefig(str(img_file), bbox_inches="tight")
    plt.close(fig)
Пример #5
0
def validate_yearmax_ice_cover_from_hostetler_with_glerl(
        path="/home/huziy/skynet3_rech1/CRCM_GL_simulation/all_files",
        start_year=2003,
        end_year=2009):
    model_manager = Crcm5ModelDataManager(samples_folder_path=path,
                                          all_files_in_samples_folder=True)

    varname = "LC"

    hl_icecov_yearly_max = model_manager.get_yearmax_fields(
        start_year=start_year, end_year=end_year, var_name=varname)

    hl_icecov_yearly_max_clim = np.mean(list(hl_icecov_yearly_max.values()),
                                        axis=0)

    # Get Nemo manager here only for coordinates and mask
    nemo_manager = NemoYearlyFilesManager(
        folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
        suffix="icemod.nc")

    lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap()

    # Interpolate hostetler's lake fraction to the model's grid
    hl_icecov_yearly_max_clim = model_manager.interpolate_data_to(
        hl_icecov_yearly_max_clim, lon2d, lat2d)
    hl_icecov_yearly_max_clim = np.ma.masked_where(~nemo_manager.lake_mask,
                                                   hl_icecov_yearly_max_clim)

    model_lake_avg_ts = []
    for the_year in range(start_year, end_year + 1):
        model_lake_avg_ts.append(
            hl_icecov_yearly_max[the_year][nemo_manager.lake_mask].mean())

    # plt.figure()
    xx, yy = bmp(lon2d.copy(), lat2d.copy())
    # im = bmp.pcolormesh(xx, yy, model_yearmax_ice_conc)
    # bmp.colorbar(im)

    # Read and interpolate obs
    path_to_obs = "/RESCUE/skynet3_rech1/huziy/nemo_obs_for_validation/glerl_icecov.nc"

    obs_varname = "ice_cover"
    obs_lake_avg_ts = []
    with Dataset(path_to_obs) as ds:
        time_var = ds.variables["time"]

        lons_obs = ds.variables["lon"][:]
        lats_obs = ds.variables["lat"][:]

        dates = num2date(time_var[:], time_var.units)
        nx, ny = lons_obs.shape

        data = ds.variables[obs_varname][:]
        data = np.ma.masked_where((data > 100) | (data < 0), data)
        print(data.min(), data.max())
        panel = pd.Panel(data=data,
                         items=dates,
                         major_axis=range(nx),
                         minor_axis=range(ny))

        panel = panel.select(lambda d: start_year <= d.year <= end_year)
        the_max_list = []
        for key, g in panel.groupby(lambda d: d.year, axis="items"):
            the_max_field = np.ma.max(np.ma.masked_where(
                (g.values > 100) | (g.values < 0), g.values),
                                      axis=0)
            obs_lake_avg_ts.append(the_max_field.mean())
            the_max_list.append(the_max_field)

        obs_yearmax_ice_conc = np.ma.mean(the_max_list, axis=0) / 100.0

        xs, ys, zs = lat_lon.lon_lat_to_cartesian(lons_obs.flatten(),
                                                  lats_obs.flatten())
        ktree = cKDTree(list(zip(xs, ys, zs)))

        xt, yt, zt = lat_lon.lon_lat_to_cartesian(lon2d.flatten(),
                                                  lat2d.flatten())
        dists, inds = ktree.query(list(zip(xt, yt, zt)))

        obs_yearmax_ice_conc_interp = obs_yearmax_ice_conc.flatten(
        )[inds].reshape(lon2d.shape)
        obs_yearmax_ice_conc_interp = np.ma.masked_where(
            ~nemo_manager.lake_mask, obs_yearmax_ice_conc_interp)

    # plt.figure()
    # b = Basemap()
    # xx, yy = b(lons_obs, lats_obs)
    # im = b.pcolormesh(xx, yy, obs_yearmax_ice_conc)
    # b.colorbar(im)
    # b.drawcoastlines()

    # Plot as usual: model, obs, model - obs
    img_folder = Path("nemo/hostetler")
    if not img_folder.is_dir():
        img_folder.mkdir()
    img_file = img_folder.joinpath(
        "validate_yearmax_icecov_hl_vs_glerl_{}-{}.pdf".format(
            start_year, end_year))

    fig = plt.figure()
    gs = GridSpec(2, 4, width_ratios=[1, 1, 1, 0.05])
    all_axes = []

    cmap = cm.get_cmap("jet", 10)
    diff_cmap = cm.get_cmap("RdBu_r", 10)

    # Model
    ax = fig.add_subplot(gs[0, 0])
    ax.set_title("Hostetler")
    bmp.pcolormesh(xx,
                   yy,
                   hl_icecov_yearly_max_clim,
                   cmap=cmap,
                   vmin=0,
                   vmax=1)
    all_axes.append(ax)

    # Obs
    ax = fig.add_subplot(gs[0, 2])
    ax.set_title("GLERL")
    im = bmp.pcolormesh(xx,
                        yy,
                        obs_yearmax_ice_conc_interp,
                        cmap=cmap,
                        vmin=0,
                        vmax=1)
    all_axes.append(ax)

    plt.colorbar(im, cax=fig.add_subplot(gs[0, -1]))

    # Biases
    ax = fig.add_subplot(gs[1, :])
    ax.set_title("Hostetler - GLERL")
    im = bmp.pcolormesh(xx,
                        yy,
                        hl_icecov_yearly_max_clim -
                        obs_yearmax_ice_conc_interp,
                        cmap=diff_cmap,
                        vmin=-1,
                        vmax=1)
    bmp.colorbar(im, ax=ax)
    all_axes.append(ax)

    for the_ax in all_axes:
        bmp.drawcoastlines(ax=the_ax)

    fig.savefig(str(img_file), bbox_inches="tight")
    plt.close(fig)

    # Plot lake aversged ice concentrations
    fig = plt.figure()
    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
    plt.plot(range(start_year, end_year + 1),
             model_lake_avg_ts,
             "b",
             lw=2,
             label="Hostetler")
    plt.plot(range(start_year, end_year + 1),
             np.asarray(obs_lake_avg_ts) / 100.0,
             "r",
             lw=2,
             label="GLERL")

    plt.grid()
    plt.legend(loc=3)
    fig.savefig(str(
        img_folder.joinpath(
            "lake_avg_iceconc_hostetler_offline_vs_GLERL.pdf")),
                bbox_inches="tight")
def validate_seas_mean_lswt_from_hostetler_and_nemo_with_homa(
        hl_data_path="/home/huziy/skynet3_rech1/CRCM_GL_simulation/all_files",
        start_year=2003, end_year=2003, season_to_months=None):
    """
    Note: degrees plotted are in C
    :param hl_data_path:
    :param start_year:
    :param end_year:
    """


    # crcm5_model_manager = Crcm5ModelDataManager(samples_folder_path=hl_data_path, all_files_in_samples_folder=True)

    model_label1 = "CRCM5_HL"
    plot_hl_biases = True



    use_noaa_oisst = True
    obs_label = "NOAA OISST" if use_noaa_oisst else "MODIS"

    clevs = np.arange(0, 22, 1)
    norm = BoundaryNorm(clevs, len(clevs) - 1)
    cmap = cm.get_cmap("viridis", len(clevs) - 1)


    clevs_bias = np.arange(-5.5, 6.5, 1)
    norm_bias = BoundaryNorm(clevs_bias, len(clevs_bias) - 1)
    cmap_bias = cm.get_cmap("bwr", len(clevs_bias) - 1)




    if season_to_months is None:
        season_to_months = OrderedDict([
            ("Winter", [1, 2, 12]),
            ("Spring", [3, 4, 5]),
            ("Summer", range(6, 9)),
            ("Fall", range(9, 11)),
        ])



    # hl_lake_temp_clim = crcm5_model_manager.get_mean_field(start_year=start_year, end_year=end_year, var_name=varname,
    #                                                        level=1.1, months=season_months)




    # Get Nemo manager here only for coordinates and mask
    # nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
    #                                       suffix="icemod.nc")

    # nemo_manager = NemoYearlyFilesManager(folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_5min",
    #                                       suffix="grid_T.nc")

    model_label2 = "CRCM5_NEMO"
    nemo_manager = NemoYearlyFilesManager(folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO",
                                          suffix="grid_T.nc")


    # model_label2 = "NEMO_offline_CRCM5_CanESM2"
    # nemo_manager = NemoYearlyFilesManager(folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3_CC_drivenby_CRCM5_CanESM2_RCP85/EXP00/cc_canesm2_outputs",
    #                                       suffix="grid_T.nc")

    # model_label2 = "NEMO_offline"

    # nemo_manager = NemoYearlyFilesManager(folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_30min",
    #                                       suffix="grid_T.nc")

    img_folder = Path("nemo/{}".format(model_label2))



    #lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap(resolution="l", subregion=[0.06, 0.5, 0.06, 0.5])
    lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap(resolution="l", subregion=[0.1, 0.5, 0.15, 0.5], area_thresh=2000)

    xx, yy = bmp(lon2d, lat2d)


    # get nemo and observed sst
    nemo_sst, obs_sst, _, _, _ = nemo_manager.get_nemo_and_homa_seasonal_mean_sst(start_year=start_year,
                                                                                  end_year=end_year,
                                                                                  season_to_months=season_to_months,
                                                                                  use_noaa_oisst=use_noaa_oisst)


    obs_sst_clim = {}



    if use_noaa_oisst:
        manager = OISSTManager(thredds_baseurl="/BIG1/huziy/noaa_oisst_daily")
        obs_sst_clim = manager.get_seasonal_clim_interpolate_to(lons=lon2d, lats=lat2d,
                                                                start_year=start_year, end_year=end_year,
                                                                season_to_months=season_to_months, vname="sst")

        for season in season_to_months:
            obs_sst_clim[season] = np.ma.masked_where(np.isnan(obs_sst_clim[season]), obs_sst_clim[season])

    else:
        # Convert to Celsius
        for season in season_to_months:
            obs_sst_clim[season] = np.ma.mean([obs_sst[y][season] for y in range(start_year, end_year + 1)], axis=0) - 273.15


    obs_sst_clim = {season: np.ma.masked_where(~nemo_manager.lake_mask, obs_sst_clim[season]) for season in obs_sst_clim}
    nemo_sst_clim = {season: np.ma.mean([nemo_sst[y][season] for y in range(start_year, end_year + 1)], axis=0) for season in season_to_months}
    nemo_sst_clim = {season: np.ma.masked_where(~nemo_manager.lake_mask, nemo_sst_clim[season]) for season in season_to_months}




    hl_sst_clim = {}
    if plot_hl_biases:
        hl_sst_clim = get_seasonal_sst_from_crcm5_outputs(model_label1, start_year=start_year, end_year=end_year,
                                                          season_to_months=season_to_months,
                                                          lons_target=lon2d, lats_target=lat2d)

        hl_sst_clim = {season: np.ma.masked_where(~nemo_manager.lake_mask, hl_sst_clim[season]) for season in season_to_months}

        # Convert to C
        hl_sst_clim = {season: hl_sst_clim[season] - 273.15 for season in season_to_months}






    # plt.figure()

    # im = bmp.pcolormesh(xx, yy, model_yearmax_ice_conc)
    # bmp.colorbar(im)



    # plt.figure()
    # b = Basemap()
    # xx, yy = b(lons_obs, lats_obs)
    # im = b.pcolormesh(xx, yy, obs_yearmax_ice_conc)
    # b.colorbar(im)
    # b.drawcoastlines()

    # Plot as usual: model, obs, model - obs

    if not img_folder.is_dir():
        img_folder.mkdir()

    img_file = img_folder.joinpath("validate_{}_lswt_{}_vs_{}_{}-{}.png".format(
        "_".join([season for season in season_to_months]),
        "_".join([model_label1 if plot_hl_biases else "", model_label2]),
        obs_label,
        start_year, end_year))




    nrows = 3
    plot_utils.apply_plot_params(font_size=8, width_cm=8 * len(season_to_months), height_cm=4.5 * nrows)


    fig = plt.figure()



    gs = GridSpec(nrows=nrows, ncols=len(season_to_months), hspace=0.15, wspace=0.000)
    all_axes = []



    # Model, Hostetler
    # ax = fig.add_subplot(gs[0, 0])
    # ax.set_title("Hostetler+CRCM5")
    # bmp.pcolormesh(xx, yy, hl_lake_temp_clim, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    # col += 1
    # all_axes.append(ax)



    for col, season in enumerate(season_to_months):

        row = 0

        # Obs: MODIS or NOAA OISST
        ax = fig.add_subplot(gs[row, col])
        im_obs = bmp.pcolormesh(xx, yy, obs_sst_clim[season], cmap=cmap, norm=norm)
        # obs values
        cb = bmp.colorbar(im_obs, location="bottom")
        cb.ax.set_visible(col == 0)



        ax.text(0.99, 0.99, season, va="top", ha="right", fontsize=16, transform=ax.transAxes)
        all_axes.append(ax)
        if col == 0:
            ax.set_ylabel(obs_label)

        row += 1


        if plot_hl_biases:
            #plot CRCM5_HL biases (for poster)
            ax = fig.add_subplot(gs[row, col])

            if col == 0:
                ax.set_ylabel("{}\n-\n{}".format(model_label1, obs_label))

            im_bias = bmp.pcolormesh(xx, yy, hl_sst_clim[season] - obs_sst_clim[season], cmap=cmap_bias, norm=norm_bias, ax=ax)
            cb = bmp.colorbar(im_bias, location="bottom", ax=ax)
            cb.ax.set_visible(False)

            all_axes.append(ax)
            row += 1


        ax = fig.add_subplot(gs[row, col])
        if col == 0:
            ax.set_ylabel("{}\n-\n{}".format(model_label2, obs_label))
        im_bias = bmp.pcolormesh(xx, yy, nemo_sst_clim[season] - obs_sst_clim[season], cmap=cmap_bias, norm=norm_bias)
        # common for all bias values
        cb = bmp.colorbar(im_bias, location="bottom", ax=ax)
        cb.ax.set_visible(col == 0)


        all_axes.append(ax)
        row += 1


        print_arr_limits(nemo_sst_clim[season], "NEMO_sst, for {}".format(season))
        print_arr_limits(obs_sst_clim[season], "Obs_sst, for {}".format(season))


    for the_ax in all_axes:
        bmp.drawcoastlines(ax=the_ax, linewidth=0.3)
        the_ax.set_frame_on(False)

    print("Saving {}".format(img_file))
    fig.savefig(str(img_file), bbox_inches="tight", dpi=300)
    plt.close(fig)
Пример #7
0
def validate_seas_mean_lswt_from_hostetler_and_nemo_with_homa(
        hl_data_path="/home/huziy/skynet3_rech1/CRCM_GL_simulation/all_files",
        start_year=2003, end_year=2006):
    crcm5_model_manager = Crcm5ModelDataManager(samples_folder_path=hl_data_path, all_files_in_samples_folder=True)

    varname = "L1"
    season = "Fall"

    season_to_months = {season: range(9, 11)}
    season_months = list(season_to_months[season])

    print(season_months, season_to_months)

    hl_lake_temp_clim = crcm5_model_manager.get_mean_field(start_year=start_year, end_year=end_year, var_name=varname,
                                                           level=1.1, months=season_months)

    hl_lake_temp_clim = hl_lake_temp_clim[13:-13, 13:-13]

    print("hl_lake_temp_clim.shape = ", hl_lake_temp_clim.shape)




    lon_hl, lat_hl = crcm5_model_manager.lons2D, crcm5_model_manager.lats2D



    print("lon_hl.shape = ", lon_hl.shape)

    print(lon_hl.min(), lon_hl.max())
    print(lat_hl.min(), lat_hl.max())

    # Get Nemo manager here only for coordinates and mask
    nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
                                          suffix="icemod.nc")

    lon2d, lat2d, bmp = nemo_manager.get_coords_and_basemap()

    # Interpolate hostetler's lake fraction to the model's grid
    hl_lake_temp_clim -= 273.15
    xs, ys, zs = lat_lon.lon_lat_to_cartesian(lon_hl.flatten(), lat_hl.flatten())
    print(xs.shape)
    ktree = cKDTree(data=list(zip(xs, ys, zs)))

    xt, yt, zt = lat_lon.lon_lat_to_cartesian(lon2d.flatten(), lat2d.flatten())
    dists, inds = ktree.query(np.asarray(list(zip(xt, yt, zt))))
    print(inds[:20])
    print(type(inds))
    print(inds.min(), inds.max())
    hl_lake_temp_clim = hl_lake_temp_clim.flatten()[inds].reshape(lon2d.shape)





    # get nemo and observed sst
    obs_sst, nemo_sst, _, _, _ = nemo_manager.get_nemo_and_homa_seasonal_mean_sst(start_year=start_year,
                                                                                  end_year=end_year,
                                                                                  season_to_months=season_to_months)

    obs_sst_clim = np.ma.mean([obs_sst[y][season] for y in range(start_year, end_year + 1)], axis=0)
    nemo_sst_clim = np.ma.mean([nemo_sst[y][season] for y in range(start_year, end_year + 1)], axis=0)


    obs_sst_clim = np.ma.masked_where(~nemo_manager.lake_mask, obs_sst_clim)
    nemo_sst_clim = np.ma.masked_where(~nemo_manager.lake_mask, nemo_sst_clim)
    hl_lake_temp_clim = np.ma.masked_where(~nemo_manager.lake_mask, hl_lake_temp_clim)

    # plt.figure()
    xx, yy = bmp(lon2d.copy(), lat2d.copy())
    # im = bmp.pcolormesh(xx, yy, model_yearmax_ice_conc)
    # bmp.colorbar(im)

    vmin = min(obs_sst_clim.min(), nemo_sst_clim.min(), hl_lake_temp_clim.min())
    vmax = max(obs_sst_clim.max(), nemo_sst_clim.max(), hl_lake_temp_clim.max())

    print("vmin={}; vmax={}".format(vmin, vmax))

    # plt.figure()
    # b = Basemap()
    # xx, yy = b(lons_obs, lats_obs)
    # im = b.pcolormesh(xx, yy, obs_yearmax_ice_conc)
    # b.colorbar(im)
    # b.drawcoastlines()

    # Plot as usual: model, obs, model - obs
    img_folder = Path("nemo/hostetler")
    if not img_folder.is_dir():
        img_folder.mkdir()
    img_file = img_folder.joinpath("validate_{}_lswt_hostetler_nemo_vs_homa_{}-{}.png".format(
        season, start_year, end_year))

    fig = plt.figure()
    gs = GridSpec(1, 4, width_ratios=[1, 1, 1, 0.05])
    all_axes = []

    cmap = cm.get_cmap("jet", 10)
    locator = MaxNLocator(nbins=10)
    ticks = locator.tick_values(vmin=vmin, vmax=vmax)
    norm = BoundaryNorm(boundaries=ticks, ncolors=len(ticks) - 1)

    # Model, Hostetler
    ax = fig.add_subplot(gs[0, 0])
    ax.set_title("Hostetler+CRCM5")
    bmp.pcolormesh(xx, yy, hl_lake_temp_clim, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    all_axes.append(ax)

    #
    ax = fig.add_subplot(gs[0, 1])
    ax.set_title("NEMO-offline")
    im = bmp.pcolormesh(xx, yy, nemo_sst_clim, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    all_axes.append(ax)


    # Obs: Homa
    ax = fig.add_subplot(gs[0, 2])
    ax.set_title("MODIS")
    im = bmp.pcolormesh(xx, yy, obs_sst_clim, cmap=cmap, vmin=vmin, vmax=vmax, norm=norm)
    all_axes.append(ax)

    plt.colorbar(im, cax=fig.add_subplot(gs[0, -1]), ticks=ticks)

    for the_ax in all_axes:
        bmp.drawcoastlines(ax=the_ax)

    fig.savefig(str(img_file), bbox_inches="tight")
    plt.close(fig)
Пример #8
0
def main_compare_max_yearly_ice_conc():
    """
    ice concentration
    """
    var_name = ""

    start_year = 1979
    end_year = 1985

    SimConfig = namedtuple("SimConfig", "path label")

    base_config = SimConfig(
        "/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012",
        "ERAI-driven")
    modif_config = SimConfig(
        "/home/huziy/skynet3_rech1/one_way_coupled_nemo_outputs_1979_1985",
        "CRCM5")

    nemo_manager_base = NemoYearlyFilesManager(folder=base_config.path,
                                               suffix="icemod.nc")
    nemo_manager_modif = NemoYearlyFilesManager(folder=modif_config.path,
                                                suffix="icemod.nc")

    icecov_base, icecov_ts_base = nemo_manager_base.get_max_yearly_ice_fraction(
        start_year=start_year, end_year=end_year)

    icecov_modif, icecov_ts_modif = nemo_manager_modif.get_max_yearly_ice_fraction(
        start_year=start_year, end_year=end_year)

    lons, lats, bmp = nemo_manager_base.get_coords_and_basemap()
    xx, yy = bmp(lons.copy(), lats.copy())

    # Plot as usual: model, obs, model - obs
    img_folder = Path("nemo/{}vs{}".format(modif_config.label,
                                           base_config.label))
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)
    img_file = img_folder.joinpath(
        "compare_yearmax_icecov_{}_vs_{}_{}-{}.pdf".format(
            modif_config.label, base_config.label, start_year, end_year))

    fig = plt.figure()
    gs = GridSpec(2, 3, width_ratios=[1, 1, 0.05])

    cmap = cm.get_cmap("jet", 10)
    diff_cmap = cm.get_cmap("RdBu_r", 10)

    # base
    ax = fig.add_subplot(gs[0, 0])
    cs = bmp.contourf(xx, yy, icecov_base, cmap=cmap)
    bmp.drawcoastlines(ax=ax)
    ax.set_title(base_config.label)

    # modif
    ax = fig.add_subplot(gs[0, 1])
    cs = bmp.contourf(xx, yy, icecov_modif, cmap=cmap, levels=cs.levels)
    plt.colorbar(cs, cax=fig.add_subplot(gs[0, -1]))
    bmp.drawcoastlines(ax=ax)
    ax.set_title(modif_config.label)

    # difference
    ax = fig.add_subplot(gs[1, :])
    cs = bmp.contourf(xx,
                      yy,
                      icecov_modif - icecov_base,
                      cmap=diff_cmap,
                      levels=np.arange(-1, 1.2, 0.2))
    bmp.colorbar(cs, ax=ax)
    bmp.drawcoastlines(ax=ax)

    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")

    ax.set_title("{}-{}".format(modif_config.label, base_config.label))

    plt.close(fig)

    # Plot time series
    img_file = img_folder.joinpath(
        "ts_compare_yearmax_icecov_{}_vs_{}_{}-{}.pdf".format(
            modif_config.label, base_config.label, start_year, end_year))
    fig = plt.figure()

    plt.plot(range(start_year, end_year + 1),
             icecov_ts_base,
             "b",
             lw=2,
             label=base_config.label)
    plt.plot(range(start_year, end_year + 1),
             icecov_ts_modif,
             "r",
             lw=2,
             label=modif_config.label)
    plt.legend()
    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
    plt.grid()
    plt.xlabel("Year")

    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")
def main():
    # path_to_folder = "/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012"

    path_to_folder = "/home/huziy/skynet3_rech1/one_way_coupled_nemo_outputs_1979_1985"

    season_to_months = OrderedDict([
    #    ("Winter", (12, 1, 2)),
    #    ("Spring", (3, 4, 5)),
    #    ("Summer", (6, 7, 8)),
        ("Fall", (9, 10, 11))
    ])

    start_year = 1990
    end_year = 2010

    u_manager = NemoYearlyFilesManager(folder=path_to_folder, suffix="_U.nc")
    v_manager = NemoYearlyFilesManager(folder=path_to_folder, suffix="_V.nc")

    u_clim = u_manager.get_seasonal_clim_field(start_year=start_year, end_year=end_year,
                                               season_to_months=season_to_months, varname="vozocrtx")
    v_clim = v_manager.get_seasonal_clim_field(start_year=start_year, end_year=end_year,
                                               season_to_months=season_to_months, varname="vomecrty")

    lons, lats, bmp = u_manager.get_coords_and_basemap()
    lons, lats, rpole = u_manager.get_cartopy_proj_and_coords()

    img_folder = Path("nemo/circulation")
    if not img_folder.is_dir():
        img_folder.mkdir()

    fig = plt.figure()

    gs = GridSpec(len(season_to_months), 1, width_ratios=[1, 0.05])

    xx, yy = bmp(lons.copy(), lats.copy())
    lons_1d = lons.mean(axis=0)
    lats_1d = lats.mean(axis=1)

    import cartopy.crs as ccrs

    tr_xy = rpole.transform_points(ccrs.Geodetic(), lons, lats)

    xx = tr_xy[:, :, 0]
    yy = tr_xy[:, :, 1]

    for i, season in enumerate(season_to_months):
        ax = fig.add_subplot(gs[i, 0], projection=rpole)
        u = u_clim[season]
        v = v_clim[season]
        # im = bmp.contourf(xx, yy, (u ** 2 + v ** 2) ** 0.5)

        # bmp.colorbar(im)

        # bmp.quiver(xx, yy, u, v, ax=ax)

        # uproj, vproj, xx, yy = bmp.transform_vector(u, v, lons_1d, lats_1d, lons.shape[0], lons.shape[1],
        # returnxy=True, masked=True)

        # uproj, vproj = bmp.rotate_vector(u, v, lons.copy(), lats.copy())


        c = (u ** 2 + v ** 2) ** 0.5 * 100

        print(c.shape, u.shape, v.shape)

        # Convert to cm
        u *= 100
        v *= 100

        # u = np.ma.masked_where(u > 10, u)
        # v = np.ma.masked_where(v > 10, v)


        step = 2
        q = ax.quiver(xx[::step, ::step], yy[::step, ::step], u[::step, ::step], v[::step, ::step], width=0.001,
                      pivot="middle", headwidth=5, headlength=5, scale=100)

        qk = ax.quiverkey(q, 0.1, 0.1, 10, r'$10 \frac{cm}{s}$', fontproperties={'weight': 'bold'}, linewidth=1)

        # ax.streamplot(xx, yy, u, v, linewidth=2, density=5, color="k")
        # im = ax.pcolormesh(xx, yy, c)
        # plt.colorbar(im, ax=ax)
        ax.set_extent([xx[0, 0], xx[-1, -1], yy[0, 0], yy[-1, -1]], crs=rpole)
        ax.coastlines("10m")
        ax.add_feature(cfeature.NaturalEarthFeature('physical', 'lakes', '50m',
                                                    edgecolor='k',
                                                    facecolor="none"), linewidth=0.5)

        ax.add_feature(cfeature.RIVERS, edgecolor="k", facecolor="none", linewidth=0.5)

        ax.grid()
        ax.set_title(season)

    fig.savefig(str(img_folder.joinpath(
        "NEMO-CRCM5-seasonal_surf_circulation_{}_arrows.pdf".format("_".join(season_to_months.keys())))),
                bbox_inches="tight")
Пример #10
0
def main_plot_all_temp_profiles_in_one_figure(
    folder_path="/home/huziy/skynet3_rech1/nemo_obs_for_validation/data_from_Ram_Yerubandi/T-profiles"
):
    """

    figure layout:
        Obs     Model  (Model - Obs)
    p1

    p2

    ...

    pn

            Point Positions map

    :param folder_path: Path to the text files containing observed temperature profiles
    """
    folder_path = os.path.expanduser(folder_path)

    temperature_profile_file_prefixes = [
        "08-01T-004A024.120.290",
        # "08-01T-013A054.120.290", "08-00T-012A017.105.317",
        "08-00T-004A177.106.286"
    ]

    # nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")
    # nemo_manager = NemoYearlyFilesManager(folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_5min")
    # nemo_manager = NemoYearlyFilesManager(folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO")
    #nemo_manager = NemoYearlyFilesManager(folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3_CC_drivenby_CRCM5_CanESM2_RCP85/EXP00/cc_canesm2_outputs")
    # nemo_manager = NemoYearlyFilesManager(
    #     folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_30min")

    # nemo_manager = NemoYearlyFilesManager(
    #     folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_30min_1980-2010")

    nemo_manager = NemoYearlyFilesManager(
        folder=
        "/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/Simulations/cc_canesm2_nemo_offline_gathered_corrected_from_guillimin"
    )

    nemo_manager.get_coords_and_basemap(resolution="i")

    plot_utils.apply_plot_params(font_size=12,
                                 width_pt=None,
                                 width_cm=35,
                                 height_cm=30)
    fig = plt.figure()
    gs = GridSpec(len(temperature_profile_file_prefixes) + 2,
                  4,
                  width_ratios=[1, 1, 1, 0.05],
                  height_ratios=len(temperature_profile_file_prefixes) * [
                      1.0,
                  ] + [
                      0.05,
                      2,
                  ],
                  top=0.90,
                  wspace=0.2,
                  hspace=0.2)

    color_levels = np.arange(0, 30, 0.5)
    diff_levels = np.arange(-7, 8, 2)
    diff_cmap = cm.get_cmap("bwr", len(diff_levels) - 1)
    axes_list = []
    imvalues = None
    imdiff = None
    titles = ["Obs", "Model", "Model - Obs"]
    labels = [
        "P{}".format(p) for p in range(len(temperature_profile_file_prefixes))
    ]

    nupper_levs_to_plot = 5

    obs_point_list = []
    start_date, end_date = None, None
    for row, prefix in enumerate(temperature_profile_file_prefixes):
        # Get the data for plots
        po = obs.get_profile_for_prefix(prefix, folder=folder_path)
        obs_point_list.append(po)

        tto, zzo, obs_profile = po.get_tz_section_data()

        start_date = po.get_start_date()
        end_date = po.get_end_date()

        tt, zz, model_profile_interp = nemo_manager.get_tz_crosssection_for_the_point(
            lon=po.longitude,
            lat=po.latitude,
            zlist=po.levels,
            var_name="votemper",
            start_date=start_date,
            end_date=end_date)

        ttm, zzm, model_profile = nemo_manager.get_tz_crosssection_for_the_point(
            lon=po.longitude,
            lat=po.latitude,
            zlist=None,
            var_name="votemper",
            start_date=start_date,
            end_date=end_date)

        print("Unique model levels: ", np.unique(zzm))

        # obs
        ax = fig.add_subplot(gs[row, 0])
        ax.contourf(tto, zzo, obs_profile, levels=color_levels)
        axes_list.append(ax)
        ax.set_ylabel(labels[row])
        common_ylims = ax.get_ylim()

        # model (not interpolated)
        ax = fig.add_subplot(gs[row, 1])
        imvalues = ax.contourf(ttm, zzm, model_profile, levels=color_levels)
        ax.set_ylim(common_ylims)
        ax.yaxis.set_ticklabels([])
        axes_list.append(ax)

        # model profile (interpolated to the observation levels) - obs profile
        ax = fig.add_subplot(gs[row, 2])
        diff = model_profile_interp - obs_profile
        imdiff = ax.contourf(tt[:, :nupper_levs_to_plot],
                             zz[:, :nupper_levs_to_plot],
                             diff[:, :nupper_levs_to_plot],
                             levels=diff_levels,
                             cmap=diff_cmap,
                             extend="both")
        # ax.yaxis.set_ticklabels([])
        axes_list.append(ax)

        if not row:
            for axi, title in zip(axes_list, titles):
                axi.set_title(title)


    title_suffix = " {}-{}".format(start_date.year, end_date.year) if start_date.year < end_date.year \
        else " {}".format(start_date.year)
    fig.suptitle("Temperature, " + title_suffix,
                 font_properties=FontProperties(weight="bold"))

    # plot colorbars
    cb = plt.colorbar(imvalues,
                      cax=fig.add_subplot(
                          gs[len(temperature_profile_file_prefixes), :2]),
                      orientation="horizontal")

    plt.colorbar(imdiff,
                 cax=fig.add_subplot(
                     gs[:len(temperature_profile_file_prefixes), 3]))

    # Format dates
    dfmt = DateFormatter("%b")
    for i, ax in enumerate(axes_list):
        ax.xaxis.set_major_formatter(dfmt)
        ax.yaxis.set_major_locator(MaxNLocator(nbins=5))
        ax.invert_yaxis()

    for ax in axes_list[:-3]:
        ax.xaxis.set_ticklabels([])

    # Plot station positions
    lons, lats, bmp = nemo_manager.get_coords_and_basemap(resolution="i")
    ax = fig.add_subplot(gs[len(temperature_profile_file_prefixes) + 1, :-2])

    for i, po, label in zip(list(range(len(obs_point_list))), obs_point_list,
                            labels):
        xx, yy = bmp(po.longitude, po.latitude)
        bmp.scatter(xx, yy, c="r")

        multiplier = 0.5 if i % 2 else 1
        if i > 1:
            multiplier *= -4

        ax.annotate(label,
                    xy=(xx, yy),
                    xytext=(-20 * multiplier, 20 * multiplier),
                    textcoords='offset points',
                    ha='right',
                    va='bottom',
                    font_properties=FontProperties(size=14),
                    bbox=dict(boxstyle='round,pad=0.5', fc='yellow'),
                    arrowprops=dict(arrowstyle='->',
                                    connectionstyle='arc3,rad=0'))

    bmp.drawcoastlines(linewidth=0.5, ax=ax)
    # fig.tight_layout()

    if not img_dir.exists():
        img_dir.mkdir(parents=True)

    img_path = img_dir / "T-profiles.pdf"

    print("Saving plots to {}".format(img_path))
    fig.savefig(str(img_path), transparent=True, bbox_inches="tight", dpi=300)
def main_plot_all_temp_profiles_in_one_figure(
        folder_path="/home/huziy/skynet3_rech1/nemo_obs_for_validation/data_from_Ram_Yerubandi/T-profiles"):
    """

    figure layout:
        Obs     Model  (Model - Obs)
    p1

    p2

    ...

    pn

            Point Positions map

    :param folder_path: Path to the text files containing observed temperature profiles
    """
    folder_path = os.path.expanduser(folder_path)

    temperature_profile_file_prefixes = [
        "08-01T-004A024.120.290",
        # "08-01T-013A054.120.290", "08-00T-012A017.105.317",
        "08-00T-004A177.106.286"
    ]

    # nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")
    # nemo_manager = NemoYearlyFilesManager(folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_5min")
    # nemo_manager = NemoYearlyFilesManager(folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO")
    #nemo_manager = NemoYearlyFilesManager(folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3_CC_drivenby_CRCM5_CanESM2_RCP85/EXP00/cc_canesm2_outputs")
    # nemo_manager = NemoYearlyFilesManager(
    #     folder="/RESCUE/skynet3_rech1/huziy/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_30min")

    # nemo_manager = NemoYearlyFilesManager(
    #     folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/dev_v3_4_STABLE_2012/NEMOGCM/CONFIG/GLK_LIM3/EXP_GLK_LIM3_1980/zdf_gls_dt_and_sbc_30min_1980-2010")


    nemo_manager = NemoYearlyFilesManager(
        folder="/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/Simulations/cc_canesm2_nemo_offline_gathered_corrected_from_guillimin")

    nemo_manager.get_coords_and_basemap(resolution="i")



    plot_utils.apply_plot_params(font_size=12, width_pt=None, width_cm=35, height_cm=30)
    fig = plt.figure()
    gs = GridSpec(len(temperature_profile_file_prefixes) + 2, 4, width_ratios=[1, 1, 1, 0.05],
                  height_ratios=len(temperature_profile_file_prefixes) * [1.0, ] + [0.05, 2, ], top=0.90,
                  wspace=0.2, hspace=0.2)

    color_levels = np.arange(0, 30, 0.5)
    diff_levels = np.arange(-7, 8, 2)
    diff_cmap = cm.get_cmap("bwr", len(diff_levels) - 1)
    axes_list = []
    imvalues = None
    imdiff = None
    titles = ["Obs", "Model", "Model - Obs"]
    labels = ["P{}".format(p) for p in range(len(temperature_profile_file_prefixes))]


    nupper_levs_to_plot = 5

    obs_point_list = []
    start_date, end_date = None, None
    for row, prefix in enumerate(temperature_profile_file_prefixes):
        # Get the data for plots
        po = obs.get_profile_for_prefix(prefix, folder=folder_path)
        obs_point_list.append(po)

        tto, zzo, obs_profile = po.get_tz_section_data()

        start_date = po.get_start_date()
        end_date = po.get_end_date()

        tt, zz, model_profile_interp = nemo_manager.get_tz_crosssection_for_the_point(lon=po.longitude, lat=po.latitude,
                                                                                      zlist=po.levels,
                                                                                      var_name="votemper",
                                                                                      start_date=start_date,
                                                                                      end_date=end_date)

        ttm, zzm, model_profile = nemo_manager.get_tz_crosssection_for_the_point(lon=po.longitude, lat=po.latitude,
                                                                                 zlist=None,
                                                                                 var_name="votemper",
                                                                                 start_date=start_date,
                                                                                 end_date=end_date)

        print("Unique model levels: ", np.unique(zzm))

        # obs
        ax = fig.add_subplot(gs[row, 0])
        ax.contourf(tto, zzo, obs_profile, levels=color_levels)
        axes_list.append(ax)
        ax.set_ylabel(labels[row])
        common_ylims = ax.get_ylim()

        # model (not interpolated)
        ax = fig.add_subplot(gs[row, 1])
        imvalues = ax.contourf(ttm, zzm, model_profile, levels=color_levels)
        ax.set_ylim(common_ylims)
        ax.yaxis.set_ticklabels([])
        axes_list.append(ax)



        # model profile (interpolated to the observation levels) - obs profile
        ax = fig.add_subplot(gs[row, 2])
        diff = model_profile_interp - obs_profile
        imdiff = ax.contourf(tt[:, :nupper_levs_to_plot], zz[:, :nupper_levs_to_plot], diff[:, :nupper_levs_to_plot], levels=diff_levels, cmap=diff_cmap, extend="both")
        # ax.yaxis.set_ticklabels([])
        axes_list.append(ax)

        if not row:
            for axi, title in zip(axes_list, titles):
                axi.set_title(title)


    title_suffix = " {}-{}".format(start_date.year, end_date.year) if start_date.year < end_date.year \
        else " {}".format(start_date.year)
    fig.suptitle("Temperature, " + title_suffix, font_properties=FontProperties(weight="bold"))


    # plot colorbars
    cb = plt.colorbar(imvalues, cax=fig.add_subplot(gs[len(temperature_profile_file_prefixes), :2]), orientation="horizontal")

    plt.colorbar(imdiff, cax=fig.add_subplot(gs[:len(temperature_profile_file_prefixes), 3]))

    # Format dates
    dfmt = DateFormatter("%b")
    for i, ax in enumerate(axes_list):
        ax.xaxis.set_major_formatter(dfmt)
        ax.yaxis.set_major_locator(MaxNLocator(nbins=5))
        ax.invert_yaxis()


    for ax in axes_list[:-3]:
        ax.xaxis.set_ticklabels([])


    # Plot station positions
    lons, lats, bmp = nemo_manager.get_coords_and_basemap(resolution="i")
    ax = fig.add_subplot(gs[len(temperature_profile_file_prefixes) + 1, :-2])

    for i, po, label in zip(list(range(len(obs_point_list))), obs_point_list, labels):
        xx, yy = bmp(po.longitude, po.latitude)
        bmp.scatter(xx, yy, c="r")

        multiplier = 0.5 if i % 2 else 1
        if i > 1:
            multiplier *= -4

        ax.annotate(label, xy=(xx, yy), xytext=(-20 * multiplier, 20 * multiplier),
                    textcoords='offset points', ha='right', va='bottom',
                    font_properties=FontProperties(size=14),
                    bbox=dict(boxstyle='round,pad=0.5', fc='yellow'),
                    arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))

    bmp.drawcoastlines(linewidth=0.5, ax=ax)
    # fig.tight_layout()


    if not img_dir.exists():
        img_dir.mkdir(parents=True)

    img_path = img_dir / "T-profiles.pdf"

    print("Saving plots to {}".format(img_path))
    fig.savefig(str(img_path), transparent=True, bbox_inches="tight", dpi=300)
Пример #12
0
def main_plot_all_temp_profiles_in_one_figure(
        folder_path="/home/huziy/skynet3_rech1/nemo_obs_for_validation/data_from_Ram_Yerubandi/T-profiles"):
    """

    figure layout:
        Obs     Model  (Model - Obs)
    p1

    p2

    ...

    pn

            Point Positions map

    :param folder_path: Path to the text files containing observed temperature profiles
    """
    folder_path = os.path.expanduser(folder_path)

    temperature_profile_file_prefixes = [
        "08-01T-004A024.120.290",
        # "08-01T-013A054.120.290", "08-00T-012A017.105.317",
        "08-00T-004A177.106.286"
    ]

    nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")

    plot_utils.apply_plot_params(font_size=16, width_pt=None, width_cm=40, height_cm=30)
    fig = plt.figure()
    gs = GridSpec(len(temperature_profile_file_prefixes) + 1, 5, width_ratios=[1, 1, 0.05, 1, 0.05],
                  height_ratios=len(temperature_profile_file_prefixes) * [1.0, ] + [3, ], top=0.90,
                  wspace=0.4, hspace=0.2)

    color_levels = np.arange(-1, 30, 1)
    diff_levels = np.arange(-10, 10.5, 0.5)
    diff_cmap = cm.get_cmap("RdBu_r", len(diff_levels) - 1)
    axes_list = []
    imvalues = None
    imdiff = None
    titles = ["Obs", "Model", "Model - Obs"]
    labels = ["P{}".format(p) for p in range(len(temperature_profile_file_prefixes))]

    obs_point_list = []
    start_date, end_date = None, None
    for row, prefix in enumerate(temperature_profile_file_prefixes):
        # Get the data for plots
        po = obs.get_profile_for_prefix(prefix, folder=folder_path)
        obs_point_list.append(po)

        tto, zzo, obs_profile = po.get_tz_section_data()

        start_date = po.get_start_date()
        end_date = po.get_end_date()

        tt, zz, model_profile_interp = nemo_manager.get_tz_crosssection_for_the_point(lon=po.longitude, lat=po.latitude,
                                                                                      zlist=po.levels,
                                                                                      var_name="votemper",
                                                                                      start_date=start_date,
                                                                                      end_date=end_date)

        ttm, zzm, model_profile = nemo_manager.get_tz_crosssection_for_the_point(lon=po.longitude, lat=po.latitude,
                                                                                 zlist=None,
                                                                                 var_name="votemper",
                                                                                 start_date=start_date,
                                                                                 end_date=end_date)

        print("Unique model levels: ", np.unique(zzm))

        # obs
        ax = fig.add_subplot(gs[row, 0])
        ax.contourf(tto, zzo, obs_profile, levels=color_levels)
        axes_list.append(ax)
        ax.set_ylabel(labels[row])
        common_ylims = ax.get_ylim()

        # model (not interpolated)
        ax = fig.add_subplot(gs[row, 1])
        imvalues = ax.contourf(ttm, zzm, model_profile, levels=color_levels)
        ax.set_ylim(common_ylims)
        ax.yaxis.set_ticklabels([])
        axes_list.append(ax)



        # model profile (interpolated to the observation levels) - obs profile
        ax = fig.add_subplot(gs[row, 3])
        diff = model_profile_interp - obs_profile
        imdiff = ax.contourf(tt, zz, diff, levels=diff_levels, cmap=diff_cmap, extend="both")
        ax.yaxis.set_ticklabels([])
        axes_list.append(ax)

        if not row:
            for axi, title in zip(axes_list, titles):
                axi.set_title(title)


    title_suffix = " {}-{}".format(start_date.year, end_date.year) if start_date.year < end_date.year \
        else " {}".format(start_date.year)
    fig.suptitle("Temperature, " + title_suffix, font_properties=FontProperties(weight="bold"))


    # plot colorbars
    cb = plt.colorbar(imvalues, cax=fig.add_subplot(gs[:-1, 2]))

    plt.colorbar(imdiff, cax=fig.add_subplot(gs[:-1, 4]))

    # Format dates
    dfmt = DateFormatter("%b")
    for i, ax in enumerate(axes_list):
        ax.xaxis.set_major_formatter(dfmt)
        ax.yaxis.set_major_locator(MaxNLocator(nbins=5))
        ax.invert_yaxis()


    for ax in axes_list[:-3]:
        ax.xaxis.set_ticklabels([])


    # Plot station positions
    lons, lats, bmp = nemo_manager.get_coords_and_basemap()
    ax = fig.add_subplot(gs[len(temperature_profile_file_prefixes), :-2])

    for i, po, label in zip(list(range(len(obs_point_list))), obs_point_list, labels):
        xx, yy = bmp(po.longitude, po.latitude)
        bmp.scatter(xx, yy, c="r")

        multiplier = 0.5 if i % 2 else 1
        if i > 1:
            multiplier *= -4

        ax.annotate(label, xy=(xx, yy), xytext=(-20 * multiplier, 20 * multiplier),
                    textcoords='offset points', ha='right', va='bottom',
                    font_properties=FontProperties(size=14),
                    bbox=dict(boxstyle='round,pad=0.5', fc='yellow'),
                    arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))

    bmp.drawcoastlines(linewidth=0.5, ax=ax)
    # plt.tight_layout()
    img_path = "nemo/T-profiles.pdf"

    fig.savefig(img_path, transparent=True, bbox_inches="tight")