def main():
    season_to_months = OrderedDict([("Dec", [
        12,
    ]), ("Jan", [
        1,
    ]), ("Feb", [
        2,
    ]), ("Mar", [
        3,
    ]), ("Apr", [
        4,
    ])])

    start_year = 1995
    end_year = 2010

    # nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")
    # nemo_manager = NemoYearlyFilesManager(folder="/Users/san/NEMO/outputs")
    nemo_manager = NemoYearlyFilesManager(
        folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")

    # glerl_manager = GLERLIceCoverManager(data_folder="/Users/san/NEMO/validation/glerl_ice_data")
    glerl_manager = GLERLIceCoverManager()
    glerl_manager.get_data_for_day(the_date=datetime(2005, 1, 3))
    obs_ice_cover_interp = glerl_manager.get_icecover_interpolated_to(
        lons2d_target=nemo_manager.lons,
        lats2d_target=nemo_manager.lats,
        the_date=datetime(2013, 4, 3))

    fig = plt.figure()
    xx, yy = nemo_manager.basemap(nemo_manager.lons, nemo_manager.lats)
    im = nemo_manager.basemap.pcolormesh(xx, yy, obs_ice_cover_interp)
    nemo_manager.basemap.colorbar(im)
    nemo_manager.basemap.drawcoastlines()
    plt.show()
Пример #2
0
def main():
    season_to_months = OrderedDict([
        ("Dec", [12, ]),
        ("Jan", [1, ]),
        ("Feb", [2, ]),
        ("Mar", [3, ]),
        ("Apr", [4, ])])

    start_year = 2003
    end_year = 2012


    nemo_manager = NemoYearlyFilesManager(folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")
    # nemo_manager = NemoYearlyFilesManager(folder="/Users/san/NEMO/outputs")

    # glerl_manager = GLERLIceCoverManager(data_folder="/Users/san/NEMO/validation/glerl_ice_data")
    glerl_manager = GLERLIceCoverManager()
    glerl_manager.get_data_for_day(the_date=datetime(2005, 1, 3))
    obs_ice_cover_interp = glerl_manager.get_icecover_interpolated_to(
        lons2d_target=nemo_manager.lons, lats2d_target=nemo_manager.lats, the_date=datetime(2013, 4, 3))


    fig = plt.figure()
    xx, yy = nemo_manager.basemap(nemo_manager.lons, nemo_manager.lats)
    im = nemo_manager.basemap.pcolormesh(xx, yy, obs_ice_cover_interp)
    nemo_manager.basemap.colorbar(im)
    nemo_manager.basemap.drawcoastlines()
    plt.show()
Пример #3
0
def get_seasonal_flows(data_dir="", start_year=1980, end_year=2010, season_to_months=None, level=0):


    """

    :param data_dir: 
    :param start_year: 
    :param end_year: 
    :param season_to_months: 
    :param level: 
    :return: (lons, lats, {season: (uu, vv, flow_speed)}) 
    """

    nemo_manager_U = NemoYearlyFilesManager(data_dir, suffix="grid_U.nc")
    nemo_manager_V = NemoYearlyFilesManager(data_dir, suffix="grid_V.nc")

    u_comp_name = "vozocrtx"
    v_comp_name = "vomecrty"


    dirpath = Path(data_dir)

    cache_file = Path("nemo_seasonal_wind_{}-{}_{}_{}_lev{}.bin".format(
        start_year, end_year, dirpath.name, "_".join([s for s in season_to_months]), level))


    if cache_file.exists():
        return pickle.load(cache_file.open("rb"))


    uu = nemo_manager_U.get_seasonal_clim_field(start_year=start_year, end_year=end_year, season_to_months=season_to_months, varname=u_comp_name, level_index=level)
    vv = nemo_manager_V.get_seasonal_clim_field(start_year=start_year, end_year=end_year, season_to_months=season_to_months, varname=v_comp_name, level_index=level)


    # Cache to reuse in future
    pickle.dump([nemo_manager_U.lons, nemo_manager_U.lats, {season: (uu[season], vv[season]) for season in season_to_months}], cache_file.open("wb"))

    return nemo_manager_U.lons, nemo_manager_U.lats, {season: (uu[season], vv[season]) for season in season_to_months}
Пример #4
0
def get_seasonal_flows(data_dir="",
                       start_year=1980,
                       end_year=2010,
                       season_to_months=None,
                       level=0):
    """

    :param data_dir: 
    :param start_year: 
    :param end_year: 
    :param season_to_months: 
    :param level: 
    :return: (lons, lats, {season: (uu, vv, flow_speed)}) 
    """

    nemo_manager_U = NemoYearlyFilesManager(data_dir, suffix="grid_U.nc")
    nemo_manager_V = NemoYearlyFilesManager(data_dir, suffix="grid_V.nc")

    u_comp_name = "vozocrtx"
    v_comp_name = "vomecrty"

    dirpath = Path(data_dir)

    cache_file = Path("nemo_seasonal_wind_{}-{}_{}_{}_lev{}.bin".format(
        start_year, end_year, dirpath.name,
        "_".join([s for s in season_to_months]), level))

    if cache_file.exists():
        return pickle.load(cache_file.open("rb"))

    uu = nemo_manager_U.get_seasonal_clim_field(
        start_year=start_year,
        end_year=end_year,
        season_to_months=season_to_months,
        varname=u_comp_name,
        level_index=level)
    vv = nemo_manager_V.get_seasonal_clim_field(
        start_year=start_year,
        end_year=end_year,
        season_to_months=season_to_months,
        varname=v_comp_name,
        level_index=level)

    # Cache to reuse in future
    pickle.dump([
        nemo_manager_U.lons, nemo_manager_U.lats,
        {season: (uu[season], vv[season])
         for season in season_to_months}
    ], cache_file.open("wb"))

    return nemo_manager_U.lons, nemo_manager_U.lats, {
        season: (uu[season], vv[season])
        for season in season_to_months
    }
Пример #5
0
def main_bak():
    dirpath = "/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"
    dirpath = Path(dirpath)

    nemo_manager = NemoYearlyFilesManager(folder=str(dirpath))

    plot_utils.apply_plot_params(width_cm=20, font_size=10)

    lons, lats, uu1, vv1, flow_speed = get_annual_mean_flow(nemo_manager,
                                                            start_year=1998,
                                                            end_year=1998,
                                                            level=0)

    lons[lons < 0] += 360
    plot_flow_vectors_basemap(lons, lats, uu1, vv1, flow_speed)

    lons[lons > 180] -= 360
Пример #6
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)
Пример #7
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")
Пример #8
0
def plot_profiles():
    obs_base_dir = Path("/home/huziy/skynet3_rech1/nemo_obs_for_validation/data_from_Ram_Yerubandi/ADCP-profiles")
    obs_dir_list = [
        str(obs_base_dir.joinpath("105.317")),
        str(obs_base_dir.joinpath("155.289"))
    ]

    obs_var_col = AdcpProfileObs.vmag_col
    model_var_name = FLOW_SPEED

    model_folder = "/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012"

    manager_nemo_u = NemoYearlyFilesManager(folder=model_folder, suffix="_U.nc")
    manager_nemo_v = NemoYearlyFilesManager(folder=model_folder, suffix="_V.nc")
    manager_nemo_w = NemoYearlyFilesManager(folder=model_folder, suffix="_W.nc")



    fig = plt.figure()
    gs = GridSpec(len(obs_dir_list), 5, width_ratios=[1, 1, 0.05, 1, 0.05])


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

    for i, obs_dir in enumerate(obs_dir_list):

        adcp = AdcpProfileObs()

        dates, levels, obs_data = adcp.get_acdp_profiles(folder=obs_dir, data_column=obs_var_col)

        dates_m, levs_m, u_cs = manager_nemo_u.get_tz_crosssection_for_the_point(
            lon=adcp.longitude, lat=adcp.latitude,
            start_date=dates[0], end_date=dates[-1],
            var_name="vozocrtx", zlist=levels
        )

        dates_m, levs_m, v_cs = manager_nemo_v.get_tz_crosssection_for_the_point(
            lon=adcp.longitude, lat=adcp.latitude,
            start_date=dates[0], end_date=dates[-1],
            var_name="vomecrty", zlist=levels
        )

        dates_m, levs_m, w_cs = manager_nemo_w.get_tz_crosssection_for_the_point(
            lon=adcp.longitude, lat=adcp.latitude,
            start_date=dates[0], end_date=dates[-1],
            var_name="vovecrtz", zlist=levels
        )




        numdates = date2num(dates.tolist())
        print("Obs dates are: {} ... {}".format(dates[0], dates[-1]))
        print([num2date([n for n in numdates if n not in dates_m])])
        zz, tt = np.meshgrid(levels, numdates)

        umag_mod_cs = (u_cs ** 2 + v_cs ** 2 + w_cs ** 2) ** 0.5 * 100.0

        all_axes = []
        # Obs
        ax = fig.add_subplot(gs[i, 0])
        ax.set_title("Obs")
        cs = ax.contourf(tt, zz, obs_data, cmap=cmap)
        ax.set_ylabel("({:.1f}, {:.1f})".format(adcp.longitude, adcp.latitude))
        all_axes.append(ax)


        # Model
        ax = fig.add_subplot(gs[i, 1])
        ax.set_title("NEMO-offline")
        cs = ax.contourf(tt, zz, umag_mod_cs, levels=cs.levels, cmap=cmap)
        all_axes.append(ax)

        plt.colorbar(cs, cax=fig.add_subplot(gs[i, 2]))

        # Bias
        ax = fig.add_subplot(gs[i, 3])
        ax.set_title("Model - Obs.")
        delta = umag_mod_cs - obs_data

        vmax = np.abs(delta).max()
        vmin = -vmax
        locator = MaxNLocator(nbins=diff_cmap.N, symmetric=True)

        cs = ax.contourf(tt, zz, delta, levels=locator.tick_values(vmin, vmax), cmap=diff_cmap)
        plt.colorbar(cs, cax=fig.add_subplot(gs[i, 4]))
        all_axes.append(ax)


        for the_ax in all_axes:
            the_ax.xaxis.set_major_formatter(DateFormatter("%b"))
            the_ax.xaxis.set_major_locator(MonthLocator())
            the_ax.invert_yaxis()

    img_folder = Path("nemo/adcp")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_file = img_folder.joinpath("adcp_profiles.pdf")
    fig.tight_layout()
    fig.savefig(str(img_file), bbox_inches="tight")
Пример #9
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)
Пример #10
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)
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)
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)
Пример #13
0
def main():
    model_data_manager = NemoYearlyFilesManager(
        folder="/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012")
Пример #14
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")
Пример #16
0
def main():

    # obs_data_path = "/RESCUE/skynet3_rech1/huziy/obs_data_for_HLES/interploated_to_the_same_grid/GL_0.1_452x260/cis_nic_glerl_interpolated_lc.nc"
    obs_data_path = "/home/huziy/skynet3_rech1/nemo_obs_for_validation/glerl_icecov1_fix.nc"


    obs_manager = CisNicIceManager(nc_file_path=obs_data_path, ice_varname="ice_cover")


    start_year = 1973
    end_year = 2013


    do_spatial_plots = False
    do_areaavg_plots = True




    season_to_months = OrderedDict()


    selected_months = [1, 2, 3, 4]

    for i, m in enumerate(calendar.month_name[1:], 1):
        if i in selected_months:
            season_to_months[m] = [i, ]

    print(season_to_months)

    nemo_icefrac_vname = "soicecov"
    nemo_managers = OrderedDict([
        ("CRCM5_NEMO", NemoYearlyFilesManager(folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO", suffix="grid_T.nc")),
    ])




    # calculate and plot
    map = Basemap(llcrnrlon=-93, llcrnrlat=41, urcrnrlon=-73,
                  urcrnrlat=48.5, projection='lcc', lat_1=33, lat_2=45,
                  lon_0=-90, resolution='i', area_thresh=10000)



    if do_spatial_plots:
        validate_2d_maps(nemo_managers, obs_manager=obs_manager, start_year=start_year, end_year=end_year,
                         season_to_months=season_to_months, nemo_icecover_name=nemo_icefrac_vname,
                         nemo_field_level_index=0, basemap=map)


    if do_areaavg_plots:

        nemo_managers = OrderedDict()

        # starting December until April
        season_month_start = 12
        season_month_count = 5


        mask_shp_file = "data/shp/Great_lakes_coast_shape/gl_cst.shp"

        validate_areaavg_annual_max(nemo_managers, obs_manager, start_year=start_year, end_year=end_year,
                                    season_month_start=season_month_start,
                                    season_month_count=season_month_count,
                                    mask_shape_file=mask_shp_file)
Пример #17
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)
Пример #18
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")
Пример #19
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")
Пример #20
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")
Пример #21
0
def main():
    current_start_year = 1981
    current_end_year = 2010

    future_start_year = 2070
    future_end_year = 2099

    LABEL_CURRENT = "Current"
    LABEL_FUTURE = "Future"

    pval_crit = 0.05

    label_to_period = {
        LABEL_CURRENT: (current_start_year, current_end_year),
        LABEL_FUTURE: (future_start_year, future_end_year)
    }

    season_to_months = OrderedDict()

    selected_months = [11, 12, 1]

    for i in selected_months:
        season_to_months[calendar.month_name[i]] = [
            i,
        ]

    print(season_to_months)

    nemo_icefrac_vname = "soicecov"
    nemo_sst_vname = "sosstsst"

    vname = nemo_sst_vname

    exp_label = "cc_canesm2_nemo_offline"

    nemo_managers_coupled_cc_slices_canesm2_rcp85 = OrderedDict([
        (LABEL_CURRENT,
         NemoYearlyFilesManager(
             folder=
             "/HOME/huziy/skynet3_rech1/CRCM5_outputs/cc_canesm2_rcp85_gl/coupled-GL-current_CanESM2/CRCMNEMO_GL_CanESM2_RCP85",
             suffix="grid_T.nc")),
        (LABEL_FUTURE,
         NemoYearlyFilesManager(
             folder=
             "/HOME/huziy/skynet3_rech1/CRCM5_outputs/cc_canesm2_rcp85_gl/coupled-GL-future_CanESM2/CRCMNEMO_GL_CanESM2_RCP85_future",
             suffix="grid_T.nc"))
    ])

    nemo_managers_offline_cc_canesm2_rcp85 = OrderedDict([
        (LABEL_CURRENT,
         NemoYearlyFilesManager(
             folder=
             "/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/Simulations/cc_canesm2_nemo_offline_gathered_corrected_from_guillimin",
             suffix="grid_T.nc")),
        (LABEL_FUTURE,
         NemoYearlyFilesManager(
             folder=
             "/HOME/huziy/skynet3_rech1/NEMO_OFFICIAL/Simulations/cc_canesm2_nemo_offline_gathered_corrected_from_guillimin",
             suffix="grid_T.nc"))
    ])

    # nemo_managers = OrderedDict([
    #     (LABEL_CURRENT, NemoYearlyFilesManager(folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO", suffix="grid_T.nc")),
    #     (LABEL_FUTURE, NemoYearlyFilesManager(folder="/BIG1/huziy/CRCM5_NEMO_coupled_sim_nemo_outputs/NEMO", suffix="grid_T.nc")),
    # ])

    nemo_managers = nemo_managers_offline_cc_canesm2_rcp85

    # calculate cc for LSWT and ice cover

    # Calculate seasonal mean projected changes
    label_to_data = OrderedDict()

    lons, lats = None, None

    for label, manager in nemo_managers.items():
        assert isinstance(manager, NemoYearlyFilesManager)

        start_year, end_year = label_to_period[label]
        label_to_data[
            label] = manager.get_seasonal_clim_fields_with_ttest_data(
                start_year=start_year,
                end_year=end_year,
                season_to_months=season_to_months,
                varname=vname)

        if lons is None:
            lons, lats = manager.lons, manager.lats

    # ----------- plot the plots
    #
    plot_utils.apply_plot_params(font_size=10,
                                 width_cm=8 * len(season_to_months),
                                 height_cm=5)

    map = Basemap(llcrnrlon=-93,
                  llcrnrlat=41,
                  urcrnrlon=-73,
                  urcrnrlat=48.5,
                  projection='lcc',
                  lat_1=33,
                  lat_2=45,
                  lon_0=-90,
                  resolution='i',
                  area_thresh=10000)

    xx, yy = map(lons, lats)

    fig = plt.figure()
    gs = GridSpec(nrows=1,
                  ncols=len(season_to_months),
                  wspace=0.02,
                  hspace=0.02)

    for col, season in enumerate(season_to_months):
        mean_c, std_c, nobs_c = label_to_data[LABEL_CURRENT][season]
        mean_f, std_f, nobs_f = label_to_data[LABEL_FUTURE][season]

        cc = mean_f - mean_c

        tval, pval = ttest_ind_from_stats(mean_c,
                                          std_c,
                                          nobs_c,
                                          mean_f,
                                          std_f,
                                          nobs_f,
                                          equal_var=False)

        cc = np.ma.masked_where(pval > pval_crit, cc)

        clevs = vname_to_clevs_diff[vname]
        cmap = cm.get_cmap("bwr", len(clevs) - 1)
        bn = BoundaryNorm(clevs, len(clevs) - 1)

        ax = fig.add_subplot(gs[0, col])
        im = map.pcolormesh(xx, yy, cc, cmap=cmap, norm=bn, ax=ax)
        cb = map.colorbar(im, location="bottom")

        cb.ax.set_visible(col == 0)

        map.drawcoastlines(linewidth=0.3)

        ax.set_frame_on(False)
        ax.set_title(season)

        if col == 0:
            ax.set_ylabel("F - C")

    # create the image folder if it does not exist yet
    if not img_folder.exists():
        img_folder.mkdir()

    fname = "{}_{}_{}vs{}.png".format(exp_label, vname, future_start_year,
                                      future_end_year, current_start_year,
                                      current_end_year)
    fig.savefig(str(img_folder / fname), bbox_inches="tight", dpi=300)
def plot_profiles():
    obs_base_dir = Path("/home/huziy/skynet3_rech1/nemo_obs_for_validation/data_from_Ram_Yerubandi/ADCP-profiles")
    obs_dir_list = [
        str(obs_base_dir.joinpath("105.317")),
        str(obs_base_dir.joinpath("155.289"))
    ]

    obs_var_col = AdcpProfileObs.vmag_col
    model_var_name = FLOW_SPEED

    # model_folder = "/home/huziy/skynet3_rech1/offline_glk_output_daily_1979-2012"
    model_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"

    manager_nemo_u = NemoYearlyFilesManager(folder=model_folder, suffix="_U.nc")
    manager_nemo_v = NemoYearlyFilesManager(folder=model_folder, suffix="_V.nc")
    manager_nemo_w = NemoYearlyFilesManager(folder=model_folder, suffix="_W.nc")



    fig = plt.figure()
    gs = GridSpec(len(obs_dir_list), 5, width_ratios=[1, 1, 0.05, 1, 0.05])


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

    for i, obs_dir in enumerate(obs_dir_list):

        adcp = AdcpProfileObs()

        dates, levels, obs_data = adcp.get_acdp_profiles(folder=obs_dir, data_column=obs_var_col)

        dates_m, levs_m, u_cs = manager_nemo_u.get_tz_crosssection_for_the_point(
            lon=adcp.longitude, lat=adcp.latitude,
            start_date=dates[0], end_date=dates[-1],
            var_name="vozocrtx", zlist=levels
        )

        dates_m, levs_m, v_cs = manager_nemo_v.get_tz_crosssection_for_the_point(
            lon=adcp.longitude, lat=adcp.latitude,
            start_date=dates[0], end_date=dates[-1],
            var_name="vomecrty", zlist=levels
        )

        dates_m, levs_m, w_cs = manager_nemo_w.get_tz_crosssection_for_the_point(
            lon=adcp.longitude, lat=adcp.latitude,
            start_date=dates[0], end_date=dates[-1],
            var_name="vovecrtz", zlist=levels
        )




        numdates = date2num(dates.tolist())
        print("Obs dates are: {} ... {}".format(dates[0], dates[-1]))
        print([num2date([n for n in numdates if n not in dates_m])])
        zz, tt = np.meshgrid(levels, numdates)

        umag_mod_cs = (u_cs ** 2 + v_cs ** 2 + w_cs ** 2) ** 0.5 * 100.0

        all_axes = []
        # Obs
        ax = fig.add_subplot(gs[i, 0])
        ax.set_title("Obs")
        cs = ax.contourf(tt, zz, obs_data, cmap=cmap)
        ax.set_ylabel("({:.1f}, {:.1f})".format(adcp.longitude, adcp.latitude))
        all_axes.append(ax)


        # Model
        ax = fig.add_subplot(gs[i, 1])
        ax.set_title("NEMO-offline")
        cs = ax.contourf(tt, zz, umag_mod_cs, levels=cs.levels, cmap=cmap)
        all_axes.append(ax)

        plt.colorbar(cs, cax=fig.add_subplot(gs[i, 2]))

        # Bias
        ax = fig.add_subplot(gs[i, 3])
        ax.set_title("Model - Obs.")
        delta = umag_mod_cs - obs_data

        vmax = np.abs(delta).max()
        vmin = -vmax
        locator = MaxNLocator(nbins=diff_cmap.N, symmetric=True)

        cs = ax.contourf(tt, zz, delta, levels=locator.tick_values(vmin, vmax), cmap=diff_cmap)
        plt.colorbar(cs, cax=fig.add_subplot(gs[i, 4]))
        all_axes.append(ax)


        for the_ax in all_axes:
            the_ax.xaxis.set_major_formatter(DateFormatter("%b"))
            the_ax.xaxis.set_major_locator(MonthLocator())
            the_ax.invert_yaxis()

    img_folder = Path("nemo/adcp/zdf_gls_dt_and_sbc_5min")
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    img_file = img_folder.joinpath("adcp_profiles.pdf")
    fig.tight_layout()

    print("Saving plots to {}".format(img_file))
    fig.savefig(str(img_file), bbox_inches="tight")