Esempio n. 1
0
def main():

    start_year = 1980
    end_year = 2003

    months_of_obs = [12, 1, 2, 3, 4, 5]

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

    var_name = "LC"
    bmp_info = analysis.get_basemap_info(r_config=r_config)
    lkid_to_mask = get_lake_masks(bmp_info.lons, bmp_info.lats)


    cell_area_m2 = analysis.get_array_from_file(path=r_config.data_path, var_name="cell_area_m2")


    # read the model data
    lkid_to_ts_model = {}
    for lkid, the_mask in lkid_to_mask.items():
        lkid_to_ts_model[lkid] = analysis.get_area_mean_timeseries(r_config.data_path, var_name=var_name, the_mask=the_mask * cell_area_m2,
                                                                   start_year=start_year, end_year=end_year)

        df = lkid_to_ts_model[lkid]

        # remove the last December
        df = df.select(lambda d: not (d.year == end_year and d.month == 12))

        # remove the first Jan and Feb
        df = df.select(lambda d: not (d.year == start_year and d.month in [1, 2]))

        # remove the Feb 29th
        df = df.select(lambda d: not (d.month == 2 and d.day == 29))

        # select months of interest
        df = df.select(lambda d: d.month in months_of_obs)

        # calculate the climatology
        df = df.groupby(lambda d: datetime(2001 if d.month == 12 else 2002, d.month, d.day)).mean()
        df.sort_index(inplace=True)


        lkid_to_ts_model[lkid] = df * 100


    # read obs data and calculate climatology
    lkid_to_ts_obs = {}
    for lkid in LAKE_IDS:
        lkid_to_ts_obs[lkid] = GL_obs_timeseries.get_ts_from_file(path=os.path.join(OBS_DATA_FOLDER, "{}-30x.TXT".format(lkid)),
                                                                  start_year=start_year, end_year=end_year - 1)

        # get the climatology
        dfm = lkid_to_ts_obs[lkid].mean(axis=1)

        dfm.index = [datetime(2001, 1, 1) + timedelta(days=int(jd - 1)) for jd in dfm.index]

        lkid_to_ts_obs[lkid] = dfm


    # plotting
    plot_utils.apply_plot_params(font_size=10)
    fig = plt.figure()
    gs = GridSpec(nrows=len(lkid_to_ts_model), ncols=2)

    for row, lkid in enumerate(lkid_to_ts_model):

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

        mod = lkid_to_ts_model[lkid]
        obs = lkid_to_ts_obs[lkid]

        print(obs.index)
        print(obs.values)

        ax.plot(mod.index, mod.values, label=r_config.label, color="r", lw=2)
        ax.plot(obs.index, obs.values, label="NOAA NIC/CIS", color="k", lw=2)

        if row == 0:
            ax.legend()

        ax.set_title(lkid)

        ax.xaxis.set_major_formatter(DateFormatter("%b"))


    fig.tight_layout()
    fig.savefig(os.path.join(img_folder, "GL_ice-cover-validation.png"), bbox_inches="tight", dpi=common_plot_params.FIG_SAVE_DPI)
Esempio n. 2
0
def main():


    vname_model = "I5"
    nx_agg = 2
    ny_agg = 2



    start_year = 1980
    end_year = 2006

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


    bmp_info = analysis.get_basemap_info(r_config=r_config)
    bmp_info_agg = bmp_info.get_aggregated(nagg_x=nx_agg, nagg_y=ny_agg)

    season_to_months = OrderedDict([
        ("Winter", [12, 1, 2]),
         ("Spring", [3, 4, 5])
    ])



    # Get the model data
    seasonal_clim_fields_model = analysis.get_seasonal_climatology_for_runconfig(run_config=r_config,
                                                                                 varname=vname_model, level=0,
                                                                                 season_to_months=season_to_months)

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



    # Get the EASE data
    obs_manager = EaseSweManager()
    season_to_clim_fields_obs = obs_manager.get_seasonal_clim_interpolated_to(target_lon2d=bmp_info_agg.lons, target_lat2d=bmp_info_agg.lats,
                                                                              season_to_months=season_to_months, start_year=start_year, end_year=end_year)


    # Do the plotting
    plot_utils.apply_plot_params(font_size=10, width_cm=16, height_cm=24)
    fig = plt.figure()
    xx, yy = bmp_info_agg.get_proj_xy()

    gs = GridSpec(3, len(season_to_clim_fields_model_agg) + 1, width_ratios=[1.0, ] * len(season_to_clim_fields_model_agg) + [0.05, ])
    clevs = [0, 50, 60, 70, 80, 90, 100, 150, 200, 250, 300, 350, 400, 500]
    norm = BoundaryNorm(clevs, 256)

    clevs_diff = np.arange(-100, 110, 10)

    cs_val = None
    cs_diff = None

    col = 0

    lons_agg_copy = bmp_info_agg.lons.copy()
    lons_agg_copy[lons_agg_copy > 180] -= 360

    lons_copy = bmp_info.lons.copy()
    lons_copy[lons_copy > 180] -= 360

    xx1, yy1 = bmp_info.get_proj_xy()

    for season, mod_field in seasonal_clim_fields_model.items():


        obs_field = season_to_clim_fields_obs[season]

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

        obs_field = maskoceans(lons_agg_copy, bmp_info_agg.lats, obs_field)
        cs_val = bmp_info_agg.basemap.contourf(xx, yy, obs_field, levels=clevs, norm=norm, ax=ax, extend="max")
        bmp_info_agg.basemap.drawcoastlines(linewidth=0.3, ax=ax)
        if col == 0:
            ax.set_ylabel("NSIDC")

        row += 1
        ax = fig.add_subplot(gs[row, col])
        mod_field = maskoceans(lons_copy, bmp_info.lats, mod_field)
        bmp_info.basemap.contourf(xx1, yy1, mod_field, levels=cs_val.levels, norm=cs_val.norm, ax=ax, extend="max")
        bmp_info.basemap.drawcoastlines(linewidth=0.3, ax=ax)
        if col == 0:
            ax.set_ylabel(r_config.label)

        row += 1
        ax = fig.add_subplot(gs[row, col])
        cs_diff = bmp_info_agg.basemap.contourf(xx, yy, season_to_clim_fields_model_agg[season] - obs_field, levels=clevs_diff, ax=ax, extend="both", cmap="seismic")
        bmp_info_agg.basemap.drawcoastlines(linewidth=0.3, ax=ax)

        if col == 0:
            ax.set_ylabel("{} minus {}".format(r_config.label, "NSIDC"))

        col += 1



    # Add values colorbar
    ax = fig.add_subplot(gs[0, -1])
    plt.colorbar(cs_val, cax=ax)
    ax.set_title("mm")


    # Add differences colorbaar
    ax = fig.add_subplot(gs[-1, -1])
    plt.colorbar(cs_diff, cax=ax)
    ax.set_title("mm")

    fig.tight_layout()
    fig.savefig(os.path.join(img_folder, "NSIDC_vs_CRCM_swe.png"), dpi=common_plot_params.FIG_SAVE_DPI, bbox_inches="tight")
Esempio n. 3
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():
    lkfr_limit = 0.05
    model_data_current_path = "/skynet3_rech1/huziy/hdf_store/cc-canesm2-driven/" \
                         "quebec_0.1_crcm5-hcd-rl-cc-canesm2-1980-2010.hdf5"


    modif_label = "CanESM2-CRCM5-L"

    start_year_c = 1980
    end_year_c = 2010

    future_shift_years = 90

    params = dict(
        start_year=start_year_c, end_year=end_year_c
    )

    params.update(
        dict(data_path=model_data_current_path, label=modif_label)
    )

    model_config_c = RunConfig(**params)
    model_config_f = model_config_c.get_shifted_config(future_shift_years)



    bmp_info = analysis.get_basemap_info(r_config=model_config_c)


    specific_cond_heat = 0.250100e7  # J/kg
    water_density = 1000.0  # kg/m**3

    season_to_months = OrderedDict([
        ("Summer", [6, 7, 8]),
    ])

    lkfr = analysis.get_array_from_file(path="/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5", var_name=infovar.HDF_LAKE_FRACTION_NAME)

    assert lkfr is not None, "Could not find lake fraction in the file"

    # Current climate
    traf_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="TRAF", level=5, season_to_months=season_to_months)
    pr_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="PR", level=0, season_to_months=season_to_months)

    lktemp_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="L1", level=0, season_to_months=season_to_months)
    airtemp_c = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="TT", level=0, season_to_months=season_to_months)

    lhc = OrderedDict([
        (s, specific_cond_heat * (pr_c[s] * water_density - traf_c[s])) for s, traf in traf_c.items()
    ])



    avc = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_c, varname="AV", level=0, season_to_months=season_to_months)


    plt.figure()
    lhc["Summer"] = np.ma.masked_where(lkfr < lkfr_limit, lhc["Summer"])
    print("min: {}, max: {}".format(lhc["Summer"].min(), lhc["Summer"].max()))
    cs = plt.contourf(lhc["Summer"].T)
    plt.title("lhc")
    plt.colorbar()

    plt.figure()
    cs = plt.contourf(avc["Summer"].T, levels=cs.levels, norm=cs.norm, cmap=cs.cmap)
    plt.title("avc")
    plt.colorbar()

    # Future climate
    traf_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="TRAF", level=5, season_to_months=season_to_months)
    pr_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="PR", level=0,
                                                           season_to_months=season_to_months)

    lktemp_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="L1", level=0,
                                                             season_to_months=season_to_months)
    airtemp_f = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="TT", level=0,
                                                              season_to_months=season_to_months)

    lhf = OrderedDict([
        (s, specific_cond_heat * (pr_f[s] * water_density - traf_f[s])) for s, traf in traf_f.items()
    ])

    plt.figure()
    plt.pcolormesh(traf_c["Summer"].T)
    plt.title("TRAF over lakes current")
    plt.colorbar()



    avf = analysis.get_seasonal_climatology_for_runconfig(run_config=model_config_f, varname="AV", level=0,
                                                          season_to_months=season_to_months)

    plt.figure()
    cs = plt.contourf(avf["Summer"].T)
    plt.title("avf")
    plt.colorbar()


    plt.figure()
    cs = plt.contourf(avf["Summer"].T - avc["Summer"].T, levels=np.arange(-40, 45, 5))
    plt.title("d(av)")
    plt.colorbar()


    plt.figure()
    plt.contourf(lhf["Summer"].T - lhc["Summer"].T, levels=cs.levels, cmap=cs.cmap, norm=cs.norm)
    plt.title("d(lh)")
    plt.colorbar()



    # plotting
    plot_utils.apply_plot_params(width_cm=15, height_cm=15, font_size=10)
    gs = GridSpec(2, 2)




    # tair_c_ts = analysis.get_area_mean_timeseries(model_config_c.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_c.start_year, end_year=model_config_c.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)
    #
    # tair_f_ts = analysis.get_area_mean_timeseries(model_config_f.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_f.start_year, end_year=model_config_f.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)
    #
    #
    # tlake_c_ts = analysis.get_area_mean_timeseries(model_config_c.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_c.start_year, end_year=model_config_c.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)
    #
    # tlake_f_ts = analysis.get_area_mean_timeseries(model_config_f.data_path, var_name="TT", level_index=0,
    #                                   start_year=model_config_f.start_year, end_year=model_config_f.end_year,
    #                                   the_mask=lkfr >= lkfr_limit)






    for season in season_to_months:
        fig = plt.figure()


        lktemp_c[season] -= 273.15
        dT_c = np.ma.masked_where(lkfr < lkfr_limit, lktemp_c[season] - airtemp_c[season])


        lktemp_f[season] -= 273.15
        dT_f = np.ma.masked_where(lkfr < lkfr_limit, lktemp_f[season] - airtemp_f[season])

        d = np.round(max(np.ma.abs(dT_c).max(), np.ma.abs(dT_f).max()))

        vmin = -d
        vmax = d

        clevs = np.arange(-12, 13, 1)
        ncolors = len(clevs) - 1
        bn = BoundaryNorm(clevs, ncolors=ncolors)
        cmap = cm.get_cmap("seismic", ncolors)




        ax_list = []

        fig.suptitle(season)

        xx, yy = bmp_info.get_proj_xy()

        # Current gradient
        ax = fig.add_subplot(gs[0, 0])
        ax.set_title(r"current: $T_{\rm lake} - T_{\rm atm}$")
        cs = bmp_info.basemap.pcolormesh(xx, yy, dT_c, ax=ax, norm=bn, cmap=cmap)
        bmp_info.basemap.colorbar(cs, ax=ax, extend="both")
        ax_list.append(ax)



        # Future Gradient
        ax = fig.add_subplot(gs[0, 1])
        ax.set_title(r"future: $T_{\rm lake} - T_{\rm atm}$")
        cs = bmp_info.basemap.pcolormesh(xx, yy, dT_f, ax=ax, norm=cs.norm, cmap=cs.cmap, vmin=vmin, vmax=vmax)
        bmp_info.basemap.colorbar(cs, ax=ax, extend="both")
        ax_list.append(ax)


        # Change in the gradient
        ax = fig.add_subplot(gs[1, 0])
        ax.set_title(r"$\Delta T_{\rm future} - \Delta T_{\rm current}$")

        ddT = dT_f - dT_c
        d = np.round(np.ma.abs(ddT).max())
        clevs = np.arange(-3, 3.1, 0.1)
        ncolors = len(clevs) - 1
        bn = BoundaryNorm(clevs, ncolors=ncolors)
        cmap = cm.get_cmap("seismic", ncolors)
        cs = bmp_info.basemap.pcolormesh(xx, yy, ddT, norm=bn, cmap=cmap)
        bmp_info.basemap.colorbar(cs, ax=ax, extend="both")
        ax_list.append(ax)



        # Change in the latent heat flux
        # ax = fig.add_subplot(gs[1, 1])
        # ax.set_title(r"$LE_{\rm future} - LE_{\rm current}$")
        # dlh = np.ma.masked_where(lkfr < lkfr_limit, lhf[season] - lhc[season])
        #
        # d = np.round(np.ma.abs(dlh).max() // 10) * 10
        # clevs = np.arange(0, 105, 5)
        # bn = BoundaryNorm(clevs, ncolors=ncolors)
        # cmap = cm.get_cmap("jet", ncolors)
        #
        # cs = bmp_info.basemap.pcolormesh(xx, yy, dlh, norm=bn, cmap=cmap)
        # bmp_info.basemap.colorbar(cs, ax=ax, extend="max")  # Change in the latent heat flux
        # ax_list.append(ax)


        for the_ax in ax_list:
            bmp_info.basemap.drawcoastlines(linewidth=0.3, ax=the_ax)


        fig.tight_layout()
        fig.savefig(os.path.join(img_folder, "lake_atm_gradients_and_fluxes_{}-{}_{}-{}.png".format(model_config_f.start_year, model_config_f.end_year, start_year_c, end_year_c)),
                    dpi=800,
                    bbox_inches="tight")
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)
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)