Exemplo n.º 1
0
def main():
    season_to_months = DEFAULT_SEASON_TO_MONTHS
    varnames = ["PR", "TT"]

    plot_utils.apply_plot_params(font_size=5, width_pt=None, width_cm=15, height_cm=4)

    reanalysis_driven_config = RunConfig(data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
                                         start_year=1980, end_year=2010, label="ERAI-CRCM5-L")


    bmp_info = analysis.get_basemap_info(r_config=reanalysis_driven_config)

    field_cmap = cm.get_cmap("jet", 10)

    vname_to_clevels = {
        "TT": np.arange(-30, 32, 2), "PR": np.arange(0, 6.5, 0.5)
    }

    vname_to_anusplin_path = {
        "TT": "/home/huziy/skynet3_rech1/anusplin_links",
        "PR": "/home/huziy/skynet3_rech1/anusplin_links"
    }

    vname_to_cru_path = {
        "TT": "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "PR": "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    }

    xx_agg = None
    yy_agg = None


    for vname in varnames:

        # get anusplin obs climatology
        season_to_obs_anusplin = plot_performance_err_with_anusplin.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config,
            vname=vname, season_to_months=season_to_months, bmp_info=bmp_info)


        # get CRU obs values-------------------------
        bmp_info_agg, season_to_obs_cru = plot_performance_err_with_cru.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config, bmp_info=bmp_info, season_to_months=season_to_months,
            obs_path=vname_to_cru_path[vname], vname=vname
        )


        if xx_agg is None:
            xx_agg, yy_agg = bmp_info_agg.get_proj_xy()



        # get model data
        seasonal_clim_fields_model = analysis.get_seasonal_climatology_for_runconfig(run_config=reanalysis_driven_config,
                                                                                     varname=vname,
                                                                                     level=0,
                                                                                     season_to_months=season_to_months)


        ###
        biases_with_anusplin = OrderedDict()
        biases_with_cru = OrderedDict()


        nx_agg = 5
        ny_agg = 5
        season_to_clim_fields_model_agg = OrderedDict()
        for season, field in seasonal_clim_fields_model.items():
            print(field.shape)
            season_to_clim_fields_model_agg[season] = aggregate_array(field, nagg_x=nx_agg, nagg_y=ny_agg)

            if vname == "PR":
                season_to_clim_fields_model_agg[season] *= 1.0e3 * 24 * 3600


            biases_with_cru[season] = season_to_clim_fields_model_agg[season] - season_to_obs_cru[season]

            biases_with_anusplin[season] = season_to_clim_fields_model_agg[season] - aggregate_array(season_to_obs_anusplin[season], nagg_x=nx_agg, nagg_y=ny_agg)


        # Do the plotting
        fig = plt.figure()
        clevs = [c for c in np.arange(-0.5, 0.55, 0.05)] if vname == "PR" else np.arange(-2, 2.2, 0.2)

        gs = GridSpec(1, len(biases_with_cru) + 1, width_ratios=len(biases_with_cru) * [1., ] + [0.05, ])

        col = 0
        cs = None
        cmap = "seismic"

        fig.suptitle(r"$\left| \delta_{\rm Hopkinson} \right| - \left| \delta_{\rm CRU} \right|$")

        for season, cru_err in biases_with_cru.items():
            anu_err = biases_with_anusplin[season]

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

            diff = np.abs(anu_err) - np.abs(cru_err)
            cs = bmp_info_agg.basemap.contourf(xx_agg, yy_agg, diff, levels=clevs, ax=ax, extend="both", cmap=cmap)
            bmp_info_agg.basemap.drawcoastlines(ax=ax, linewidth=0.3)


            good = diff[~diff.mask & ~np.isnan(diff)]
            n_neg = sum(good < 0) / sum(good > 0)

            print("season: {}, n-/n+ = {}".format(season, n_neg))

            ax.set_title(season)
            ax.set_xlabel(r"$n_{-}/n_{+} = $" + "{:.1f}".format(n_neg) + "\n" + r"$\overline{\varepsilon} = $" + "{:.2f}".format(good.mean()))

            col += 1


        ax = fig.add_subplot(gs[0, -1])
        plt.colorbar(cs, cax=ax)
        ax.set_title("mm/day" if vname == "PR" else r"${\rm ^\circ C}$")


        fig.savefig(os.path.join(img_folder, "comp_anu_and_cru_biases_for_{}.png".format(vname)), bbox_inches="tight", dpi=common_plot_params.FIG_SAVE_DPI)
def main():
    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    season_to_months = OrderedDict([
        ("Winter (DJF)", (1, 2, 12)),
        ("Spring (MAM)", range(3, 6)),
        ("Summer (JJA)", range(6, 9)),
        ("Fall (SON)", range(9, 12)),
    ])

    varnames = ["TT", "PR"]

    plot_utils.apply_plot_params(font_size=10, width_pt=None, width_cm=20, height_cm=17)

    # reanalysis_driven_config = RunConfig(data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5",
    #                                      start_year=1980, end_year=2010, label="ERAI-CRCM5-L")
    #

    reanalysis_driven_config = RunConfig(data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.4_crcm5-hcd-rl.hdf5",
                                         start_year=1980, end_year=2010, label="ERAI-CRCM5-L(0.4)")

    nx_agg_model = 1
    ny_agg_model = 1

    nx_agg_anusplin = 4
    ny_agg_anusplin = 4





    gcm_driven_config = RunConfig(
        data_path="/RESCUE/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5",
        start_year=1980, end_year=2010, label="CanESM2-CRCM5-L")

    bmp_info = analysis.get_basemap_info(r_config=reanalysis_driven_config)
    xx, yy = bmp_info.get_proj_xy()

    field_cmap = cm.get_cmap("jet", 10)

    vname_to_clevels = {
        "TT": np.arange(-30, 32, 2), "PR": np.arange(0, 6.5, 0.5)
    }

    vname_to_anusplin_path = {
        "TT": "/home/huziy/skynet3_rech1/anusplin_links",
        "PR": "/home/huziy/skynet3_rech1/anusplin_links"
    }

    vname_to_cru_path = {
        "TT": "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "PR": "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    }

    for vname in varnames:
        fig = plt.figure()
        ncols = len(season_to_months)
        gs = GridSpec(4, ncols + 1, width_ratios=ncols * [1., ] + [0.09, ])

        clevels = vname_to_clevels[vname]

        # get anusplin obs climatology
        season_to_obs_anusplin = plot_performance_err_with_anusplin.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config,
            vname=vname, season_to_months=season_to_months, bmp_info=bmp_info,
            n_agg_x=nx_agg_anusplin, n_agg_y=ny_agg_anusplin)

        row = 0

        # Plot CRU values-------------------------
        bmp_info_agg, season_to_obs_cru = plot_performance_err_with_cru.get_seasonal_clim_obs_data(
            rconfig=reanalysis_driven_config, bmp_info=bmp_info, season_to_months=season_to_months,
            obs_path=vname_to_cru_path[vname], vname=vname
        )

        # Mask out the Great Lakes
        cru_mask = get_mask(bmp_info_agg.lons, bmp_info_agg.lats, shp_path=os.path.join(GL_SHP_FOLDER, "gl_cst.shp"))
        for season in season_to_obs_cru:
            season_to_obs_cru[season] = np.ma.masked_where(cru_mask > 0.5, season_to_obs_cru[season])

        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        cs = None
        xx_agg, yy_agg = bmp_info_agg.get_proj_xy()
        for j, (season, obs_field) in enumerate(season_to_obs_cru.items()):
            ax = ax_list[j]
            cs = bmp_info_agg.basemap.contourf(xx_agg, yy_agg, obs_field.copy(), levels=clevels, ax=ax)
            bmp_info.basemap.drawcoastlines(ax=ax)
            bmp_info.basemap.readshapefile(BASIN_BOUNDARIES_SHP[:-4], "basin", ax=ax)
            ax.set_title(season)

        ax_list[0].set_ylabel("CRU")
        # plt.colorbar(cs, caax=ax_list[-1])
        row += 1

        # Plot ANUSPLIN values-------------------------
        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        cs = None
        for j, (season, obs_field) in enumerate(season_to_obs_anusplin.items()):
            ax = ax_list[j]
            cs = bmp_info.basemap.contourf(xx, yy, obs_field, levels=clevels, ax=ax)
            bmp_info.basemap.drawcoastlines(ax=ax)
            bmp_info.basemap.readshapefile(BASIN_BOUNDARIES_SHP[:-4], "basin", ax=ax)
            ax.set_title(season)

        ax_list[0].set_ylabel("Hopkinson")
        cb = plt.colorbar(cs, cax=fig.add_subplot(gs[:2, -1]))
        cb.ax.set_xlabel(infovar.get_units(vname))
        _format_axes(ax_list, vname=vname)
        row += 1

        # Plot model (CRCM) values-------------------------
        # ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        # cs = None
        #
        # season_to_field_crcm = analysis.get_seasonal_climatology_for_runconfig(run_config=reanalysis_driven_config,
        #                                                                        varname=vname, level=0,
        #                                                                        season_to_months=season_to_months)
        #
        # for j, (season, crcm_field) in enumerate(season_to_field_crcm.items()):
        #     ax = ax_list[j]
        #     cs = bmp_info.basemap.contourf(xx, yy, crcm_field * 1000 * 24 * 3600, levels=clevels, ax=ax)
        #     bmp_info.basemap.drawcoastlines(ax=ax)
        #     bmp_info.basemap.readshapefile(BASIN_BOUNDARIES_SHP[:-4], "basin", ax=ax)
        #     ax.set_title(season)
        #
        # ax_list[0].set_ylabel(reanalysis_driven_config.label)
        # cb = plt.colorbar(cs, cax=fig.add_subplot(gs[:2, -1]))
        # cb.ax.set_xlabel(infovar.get_units(vname))
        # _format_axes(ax_list, vname=vname)
        # row += 1


        # Plot (Model - CRU) Performance biases-------------------------
        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        cs = plot_performance_err_with_cru.compare_vars(vname_model=vname, vname_obs=None,
                                                        r_config=reanalysis_driven_config,
                                                        season_to_months=season_to_months,
                                                        obs_path=vname_to_cru_path[vname],
                                                        bmp_info_agg=bmp_info_agg, diff_axes_list=ax_list,
                                                        mask_shape_file=os.path.join(GL_SHP_FOLDER, "gl_cst.shp"),
                                                        nx_agg_model=nx_agg_model, ny_agg_model=ny_agg_model)

        ax_list[0].set_ylabel("{label}\n--\nCRU".format(label=reanalysis_driven_config.label))
        _format_axes(ax_list, vname=vname)
        row += 1

        # Plot performance+BFE errors with respect to CRU (Model - CRU)-------------------------
        # ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        # plot_performance_err_with_cru.compare_vars(vname, vname_obs=None, obs_path=vname_to_cru_path[vname],
        #                                            r_config=gcm_driven_config,
        #                                            bmp_info_agg=bmp_info_agg, season_to_months=season_to_months,
        #                                            axes_list=ax_list)
        # _format_axes(ax_list, vname=vname)
        # ax_list[0].set_ylabel("{label}\nvs\nCRU".format(label=gcm_driven_config.label))
        # row += 1


        # Plot performance errors with respect to ANUSPLIN (Model - ANUSPLIN)-------------------------
        ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        plot_performance_err_with_anusplin.compare_vars(vname, {vname: season_to_obs_anusplin},
                                                        r_config=reanalysis_driven_config,
                                                        bmp_info_agg=bmp_info, season_to_months=season_to_months,
                                                        axes_list=ax_list)
        _format_axes(ax_list, vname=vname)
        ax_list[0].set_ylabel("{label}\n--\nHopkinson".format(label=reanalysis_driven_config.label))
        row += 1

        # Plot performance+BFE errors with respect to ANUSPLIN (Model - ANUSPLIN)-------------------------
        # ax_list = [fig.add_subplot(gs[row, j]) for j in range(ncols)]
        # plot_performance_err_with_anusplin.compare_vars(vname, {vname: season_to_obs_anusplin},
        #                                                 r_config=gcm_driven_config,
        #                                                 bmp_info_agg=bmp_info, season_to_months=season_to_months,
        #                                                 axes_list=ax_list)
        # _format_axes(ax_list, vname=vname)
        # ax_list[0].set_ylabel("{label}\nvs\nHopkinson".format(label=gcm_driven_config.label))


        cb = plt.colorbar(cs, cax=fig.add_subplot(gs[-2:, -1]))
        cb.ax.set_xlabel(infovar.get_units(vname))

        # Save the plot
        img_file = "{vname}_{sy}-{ey}_{sim_label}.png".format(
            vname=vname, sy=reanalysis_driven_config.start_year, ey=reanalysis_driven_config.end_year,
            sim_label=reanalysis_driven_config.label)

        img_file = img_folder.joinpath(img_file)
        with img_file.open("wb") as f:
            fig.savefig(f, bbox_inches="tight")
        plt.close(fig)