Пример #1
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)
Пример #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)
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)