def get_cru_obs_mean_fields(target_lon_2d,
                            target_lat_2d,
                            nneighbours=1,
                            months=None):
    tmpCruDataManager = CRUDataManager(var_name="tmp", lazy=True)
    preCruDataManager = CRUDataManager(
        path="data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.pre.dat.nc",
        var_name="pre",
        lazy=True)

    #get mean annual temperature from CRU dataset
    tmpCru = tmpCruDataManager.get_mean(start_year=start_year,
                                        end_year=end_year,
                                        months=months)
    #same thing for the precip
    preCru = preCruDataManager.get_mean(start_year=start_year,
                                        end_year=end_year,
                                        months=months)  ##pre is in mm/month

    #convert to mm/day
    preCru *= 12.0 / 365.25

    tmpCruI = tmpCruDataManager.interpolate_data_to(tmpCru,
                                                    target_lon_2d,
                                                    target_lat_2d,
                                                    nneighbours=nneighbours)
    preCruI = preCruDataManager.interpolate_data_to(preCru,
                                                    target_lon_2d,
                                                    target_lat_2d,
                                                    nneighbours=nneighbours)

    return tmpCruI, preCruI
def get_seasonal_clim_obs_data(rconfig=None,
                               vname="TT",
                               bmp_info=None,
                               season_to_months=None,
                               obs_path=None,
                               nx_agg=None,
                               ny_agg=None):
    # Number of points for aggregation
    """
    return aggregated BasemapInfo object corresponding to the CRU resolution

    :param rconfig:
    :param vname:
    :param bmp_info: BasemapInfo object for the model field (will be upscaled to the CRU resolution)
    :param season_to_months:
    """

    if bmp_info is None:
        bmp_info = analysis.get_basemap_info_from_hdf(
            file_path=rconfig.data_path)

    if nx_agg is not None:
        bmp_info_agg = bmp_info.get_aggregated(nagg_x=nx_agg, nagg_y=ny_agg)
    else:
        bmp_info_agg = bmp_info

    # Validate temperature and precip
    model_vars = ["TT", "PR"]
    obs_vars = ["tmp", "pre"]

    obs_paths = [
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    ]

    model_var_to_obs_var = dict(zip(model_vars, obs_vars))
    model_var_to_obs_path = dict(zip(model_vars, obs_paths))

    if obs_path is None:
        obs_path = model_var_to_obs_path[vname]

    cru = CRUDataManager(var_name=model_var_to_obs_var[vname], path=obs_path)

    seasonal_clim_fields_obs = cru.get_seasonal_means(
        season_name_to_months=season_to_months,
        start_year=rconfig.start_year,
        end_year=rconfig.end_year)

    seasonal_clim_fields_obs_interp = OrderedDict()
    for season, cru_field in seasonal_clim_fields_obs.items():
        seasonal_clim_fields_obs_interp[season] = cru.interpolate_data_to(
            cru_field,
            lons2d=bmp_info_agg.lons,
            lats2d=bmp_info_agg.lats,
            nneighbours=1)

        # assert hasattr(seasonal_clim_fields_obs_interp[season], "mask")

    return bmp_info_agg, seasonal_clim_fields_obs_interp
Beispiel #3
0
def get_cru_obs_mean_fields(target_lon_2d, target_lat_2d, nneighbours = 1, months = None):
    tmpCruDataManager = CRUDataManager(var_name="tmp", lazy = True)
    preCruDataManager = CRUDataManager(path = "data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.pre.dat.nc", var_name="pre", lazy = True)


    #get mean annual temperature from CRU dataset
    tmpCru = tmpCruDataManager.get_mean(start_year=start_year, end_year = end_year, months = months)
    #same thing for the precip
    preCru = preCruDataManager.get_mean(start_year=start_year, end_year = end_year, months = months) ##pre is in mm/month

    #convert to mm/day
    preCru *= 12.0 / 365.25

    tmpCruI = tmpCruDataManager.interpolate_data_to(tmpCru, target_lon_2d, target_lat_2d, nneighbours=nneighbours)
    preCruI = preCruDataManager.interpolate_data_to(preCru, target_lon_2d, target_lat_2d, nneighbours=nneighbours)

    return tmpCruI, preCruI
Beispiel #4
0
def compare_vars(vname_model="TT",
                 vname_obs="tmp",
                 r_config=None,
                 season_to_months=None,
                 obs_path=None,
                 nx_agg=5,
                 ny_agg=5,
                 bmp_info_agg=None,
                 axes_list=None):
    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():
        print(field.shape)
        season_to_clim_fields_model_agg[season] = aggregate_array(
            field, nagg_x=nx_agg, nagg_y=ny_agg)
        if vname_model == "PR":
            season_to_clim_fields_model_agg[season] *= 1.0e3 * 24 * 3600

    if obs_path is None:
        cru = CRUDataManager(var_name=vname_obs)
    else:
        cru = CRUDataManager(var_name=vname_obs, path=obs_path)

    seasonal_clim_fields_obs = cru.get_seasonal_means(
        season_name_to_months=season_to_months,
        start_year=r_config.start_year,
        end_year=r_config.end_year)

    seasonal_clim_fields_obs_interp = OrderedDict()
    for season, cru_field in seasonal_clim_fields_obs.items():
        seasonal_clim_fields_obs_interp[season] = cru.interpolate_data_to(
            cru_field,
            lons2d=bmp_info_agg.lons,
            lats2d=bmp_info_agg.lats,
            nneighbours=1)

        assert hasattr(seasonal_clim_fields_obs_interp[season], "mask")

    season_to_err = OrderedDict()
    for season in seasonal_clim_fields_obs_interp:
        if vname_model == "PR":
            # * 10, because in the UDEL dataset, precipitation is in cm
            season_to_err[season] = season_to_clim_fields_model_agg[season] - \
                                    seasonal_clim_fields_obs_interp[season] * MM_PER_CM
        else:
            season_to_err[season] = season_to_clim_fields_model_agg[
                season] - seasonal_clim_fields_obs_interp[season]

    plot_performance_err_with_cru.plot_seasonal_mean_biases(
        season_to_error_field=season_to_err,
        varname=vname_model,
        basemap_info=bmp_info_agg,
        axes_list=axes_list)
Beispiel #5
0
def get_seasonal_clim_obs_data(rconfig=None, vname="TT", bmp_info=None, season_to_months=None, obs_path=None):
    # Number of points for aggregation
    """
    return aggregated BasemapInfo object corresponding to the CRU resolution

    :param rconfig:
    :param vname:
    :param bmp_info: BasemapInfo object for the model field (will be upscaled to the CRU resolution)
    :param season_to_months:
    """
    nx_agg = 5
    ny_agg = 5

    if bmp_info is None:
        bmp_info = analysis.get_basemap_info_from_hdf(file_path=rconfig.data_path)

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

    # Validate temperature and precip
    model_vars = ["TT", "PR"]
    obs_vars = ["tmp", "pre"]

    obs_paths = [
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    ]

    model_var_to_obs_var = dict(zip(model_vars, obs_vars))
    model_var_to_obs_path = dict(zip(model_vars, obs_paths))

    if obs_path is None:
        obs_path = model_var_to_obs_path[vname]

    cru = CRUDataManager(var_name=model_var_to_obs_var[vname], path=obs_path)

    seasonal_clim_fields_obs = cru.get_seasonal_means(season_name_to_months=season_to_months,
                                                      start_year=rconfig.start_year,
                                                      end_year=rconfig.end_year)

    seasonal_clim_fields_obs_interp = OrderedDict()
    for season, cru_field in seasonal_clim_fields_obs.items():
        seasonal_clim_fields_obs_interp[season] = cru.interpolate_data_to(cru_field,
                                                                          lons2d=bmp_info_agg.lons,
                                                                          lats2d=bmp_info_agg.lats, nneighbours=1)

        # assert hasattr(seasonal_clim_fields_obs_interp[season], "mask")

    return bmp_info_agg, seasonal_clim_fields_obs_interp
def compare_vars(vname_model="TT", vname_obs="tmp", r_config=None,
                 season_to_months=None,
                 obs_path=None, nx_agg=5, ny_agg=5, bmp_info_agg=None, axes_list=None):
    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():
        print(field.shape)
        season_to_clim_fields_model_agg[season] = aggregate_array(field, nagg_x=nx_agg, nagg_y=ny_agg)
        if vname_model == "PR":
            season_to_clim_fields_model_agg[season] *= 1.0e3 * 24 * 3600

    if obs_path is None:
        cru = CRUDataManager(var_name=vname_obs)
    else:
        cru = CRUDataManager(var_name=vname_obs, path=obs_path)

    seasonal_clim_fields_obs = cru.get_seasonal_means(season_name_to_months=season_to_months,
                                                      start_year=r_config.start_year,
                                                      end_year=r_config.end_year)

    seasonal_clim_fields_obs_interp = OrderedDict()
    for season, cru_field in seasonal_clim_fields_obs.items():
        seasonal_clim_fields_obs_interp[season] = cru.interpolate_data_to(cru_field,
                                                                          lons2d=bmp_info_agg.lons,
                                                                          lats2d=bmp_info_agg.lats, nneighbours=1)

        assert hasattr(seasonal_clim_fields_obs_interp[season], "mask")

    season_to_err = OrderedDict()
    for season in seasonal_clim_fields_obs_interp:
        if vname_model == "PR":
            # * 10, because in the UDEL dataset, precipitation is in cm
            season_to_err[season] = season_to_clim_fields_model_agg[season] - \
                                    seasonal_clim_fields_obs_interp[season] * MM_PER_CM
        else:
            season_to_err[season] = season_to_clim_fields_model_agg[season] - seasonal_clim_fields_obs_interp[season]

    plot_performance_err_with_cru.plot_seasonal_mean_biases(season_to_error_field=season_to_err, varname=vname_model,
                                                            basemap_info=bmp_info_agg,
                                                            axes_list=axes_list)
def main():
    erainterim_075_folder = "/HOME/data/Validation/ERA-Interim_0.75/Offline_driving_data/3h_Forecast"

    vname = "PR"
    start_year = 1980
    end_year = 2010


    season_key = "summer"
    season_labels = {season_key: "Summer"}
    season_to_months = OrderedDict([
        (season_key, [6, 7, 8])
    ])


    # Validate temperature and precip
    model_vars = ["TT", "PR"]
    obs_vars = ["tmp", "pre"]

    obs_paths = [
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc"
    ]

    model_var_to_obs_var = dict(zip(model_vars, obs_vars))
    model_var_to_obs_path = dict(zip(model_vars, obs_paths))

    obs_path = model_var_to_obs_path[vname]

    cru = CRUDataManager(var_name=model_var_to_obs_var[vname], path=obs_path)

    # Calculate seasonal means for CRU
    seasonal_clim_fields_cru = cru.get_seasonal_means(season_name_to_months=season_to_months,
                                                      start_year=start_year,
                                                      end_year=end_year)



    # Calculate seasonal mean for erai
    flist = get_files_for_season(erainterim_075_folder, start_year=start_year, end_year=end_year, months=season_to_months[season_key])

    rpf = MultiRPN(flist)
    date_to_field_erai075 = rpf.get_all_time_records_for_name_and_level(varname=vname, level=-1)

    # Convert to mm/day
    era075 = np.mean([field for field in date_to_field_erai075.values()], axis=0) * 24 * 3600 * 1000
    lons_era, lats_era = rpf.get_longitudes_and_latitudes_of_the_last_read_rec()



    seasonal_clim_fields_cru_interp = OrderedDict()

    # Calculate biases
    for season, cru_field in seasonal_clim_fields_cru.items():
        seasonal_clim_fields_cru_interp[season] = cru.interpolate_data_to(cru_field,
                                                                          lons2d=lons_era,
                                                                          lats2d=lats_era,
                                                                          nneighbours=1)




    # Do the plotting ------------------------------------------------------------------------------
    plot_utils.apply_plot_params()
    fig = plt.figure()


    b = Basemap()
    gs = gridspec.GridSpec(nrows=3, ncols=1)


    ax = fig.add_subplot(gs[0, 0])
    xx, yy = b(cru.lons2d, cru.lats2d)
    cs = b.contourf(xx, yy, seasonal_clim_fields_cru[season_key], 20)
    b.drawcoastlines(ax=ax)
    ax.set_title("CRU")
    plt.colorbar(cs, ax=ax)


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



    lons_era[lons_era > 180] -= 360
    lons_era, era075 = b.shiftdata(lons_era, datain=era075, lon_0=0)
    xx, yy = b(lons_era, lats_era)

    # mask oceans in the era plot as well
    era075 = maskoceans(lons_era, lats_era, era075)

    cs = b.contourf(xx, yy, era075, levels=cs.levels, norm=cs.norm, cmap=cs.cmap, ax=ax)
    b.drawcoastlines(ax=ax)
    ax.set_title("ERA-Interim 0.75")
    plt.colorbar(cs, ax=ax)


    # differences
    ax = fig.add_subplot(gs[2, 0])
    diff = era075 - seasonal_clim_fields_cru_interp[season_key]
    delta = np.percentile(np.abs(diff)[~diff.mask], 90)
    clevs = np.linspace(-delta, delta, 20)

    cs = b.contourf(xx, yy, diff, levels=clevs, cmap="RdBu_r", extend="both")
    b.drawcoastlines(ax=ax)
    ax.set_title("ERA-Interim 0.75 - CRU")
    plt.colorbar(cs, ax=ax)

    plt.show()

    fig.savefig(os.path.join(img_folder, "erai0.75_vs_cru_precip.png"), bbox_inches="tight")
Beispiel #8
0
def compare_for_season(start_year=1958,
                       end_year=1974,
                       the_months=None,
                       period_str="djf"):
    """
    Compare CRU, ERA40-driven and GCM-driven s
    """
    #b, lons2d, lats2d = draw_regions.get_basemap_and_coords(llcrnrlat=40.0, llcrnrlon=-145, urcrnrlon=-10)
    b, lons2d, lats2d = draw_regions.get_basemap_and_coords()
    lons2d[lons2d > 180] -= 360
    x, y = b(lons2d, lats2d)

    cru = CRUDataManager()
    cru_data = cru.get_mean(start_year, end_year, months=the_months)
    cru_data_interp = cru.interpolate_data_to(cru_data, lons2d, lats2d)

    temp_levels = np.arange(-40, 40, 5)
    diff_levels = np.arange(-10, 12, 2)
    gs = gridspec.GridSpec(3, 2)
    #plot_utils.apply_plot_params(width_pt=None, height_cm=20, width_cm=20, font_size=12)
    fig = plt.figure()
    coast_line_width = 0.25
    axes_list = []

    #plot CRU data
    ax = fig.add_subplot(gs[0, :])
    axes_list.append(ax)
    cru_data_interp = maskoceans(lons2d, lats2d, cru_data_interp)
    img = b.contourf(x, y, cru_data_interp, ax=ax, levels=temp_levels)
    ax.set_title("CRU")
    plot_utils.draw_colorbar(fig, img, ax=ax)

    #era40 driven
    file_path = None
    era40_folder = "data/CORDEX/na/era40_1"
    file_prefix = "dm"
    for file_name in os.listdir(era40_folder):
        if period_str.upper() in file_name and file_name.startswith(
                file_prefix):
            file_path = os.path.join(era40_folder, file_name)
            break
    #get the temperature
    rpn_obj = RPN(file_path)
    t2m_era40 = rpn_obj.get_first_record_for_name_and_level(
        varname="TT", level=1, level_kind=level_kinds.HYBRID)
    t2m_era40 = maskoceans(lons2d, lats2d, t2m_era40)
    ax = fig.add_subplot(gs[1, 0])
    axes_list.append(ax)
    img = b.contourf(x, y, t2m_era40, ax=ax, levels=temp_levels)
    ax.set_title("ERA40 driven 1 (1958-1961)")
    plot_utils.draw_colorbar(fig, img, ax=ax)
    rpn_obj.close()

    #era40 - cru
    ax = fig.add_subplot(gs[1, 1])
    axes_list.append(ax)
    img = b.contourf(x,
                     y,
                     t2m_era40 - cru_data_interp,
                     ax=ax,
                     levels=diff_levels)
    ax.set_title("ERA40 driven 1 - CRU")
    plot_utils.draw_colorbar(fig, img, ax=ax)

    plot_e2_data = False
    if plot_e2_data:
        ##get and plot E2 data
        file_path = None
        e2_folder = "data/CORDEX/na/e2"
        prefix = "dm"
        #get file path
        for file_name in os.listdir(e2_folder):
            if file_name.endswith(period_str) and file_name.startswith(prefix):
                file_path = os.path.join(e2_folder, file_name)
                break
            pass
        #get the temperature
        rpn_obj = RPN(file_path)
        t2m = rpn_obj.get_first_record_for_name_and_level(
            varname="TT", level=1, level_kind=level_kinds.HYBRID)
        t2m = maskoceans(lons2d, lats2d, t2m)
        ax = fig.add_subplot(gs[2, 0])
        axes_list.append(ax)
        img = b.contourf(x, y, t2m, ax=ax, levels=temp_levels)
        ax.set_title("E2, GCM driven")
        plot_utils.draw_colorbar(fig, img, ax=ax)

        #e2 - cru
        ax = fig.add_subplot(gs[2, 1])
        axes_list.append(ax)
        img = b.contourf(x,
                         y,
                         t2m - cru_data_interp,
                         ax=ax,
                         levels=diff_levels)
        ax.set_title("E2, GCM driven - CRU")
        plot_utils.draw_colorbar(fig, img, ax=ax)

    ####Draw common elements
    pf_kinds = draw_regions.get_permafrost_mask(lons2d, lats2d)
    for the_ax in axes_list:
        b.drawcoastlines(ax=the_ax, linewidth=coast_line_width)
        b.contour(x, y, pf_kinds, ax=the_ax, colors="k")

    gs.tight_layout(fig, h_pad=5, w_pad=5, pad=2)
    fig.suptitle(period_str.upper(), y=0.03, x=0.5)
    fig.savefig("temperature_validation_{0}.png".format(period_str))

    fig = plt.figure()
    ax = plt.gca()
    img = b.contourf(x,
                     y,
                     t2m_era40 - cru_data_interp,
                     ax=ax,
                     levels=diff_levels)
    ax.set_title("ERA40 driven 1 - CRU")
    plot_utils.draw_colorbar(fig, img, ax=ax)
    b.drawcoastlines(ax=ax, linewidth=coast_line_width)
    b.contour(x, y, pf_kinds, ax=ax, colors="k")
    fig.savefig("temperature_diff_{0}.png".format(period_str))

    pass
Beispiel #9
0
def validate_using_monthly_diagnostics():
    start_year = 1980
    end_year = 1996

    sim_data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1"

    sim_names = ["ERA40", "MPI", "CanESM"]
    simname_to_path = {
        "ERA40":
        "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1_dm",
        "MPI":
        "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_MPI_B1_dm",
        "CanESM":
        "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_CanESM_B1_dm"
    }

    coord_file = os.path.join(
        sim_data_folder, "pmNorthAmerica_0.44deg_ERA40-Int_B1_200812_moyenne")
    basemap, lons2d, lats2d = draw_regions.get_basemap_and_coords(
        resolution="c",
        file_path=coord_file,
        llcrnrlat=45.0,
        llcrnrlon=-145,
        urcrnrlon=-20,
        urcrnrlat=74,
        anchor="W")
    assert isinstance(basemap, Basemap)

    lons2d[lons2d > 180] -= 360

    obs_manager = CRUDataManager()
    obs = obs_manager.get_mean(start_year, end_year, months=[6, 7, 8])
    obs = obs_manager.interpolate_data_to(obs, lons2d, lats2d, nneighbours=1)

    x, y = basemap(lons2d, lats2d)

    #x = (x[1:,1:] + x[:-1, :-1]) /2.0

    permafrost_mask = draw_regions.get_permafrost_mask(lons2d, lats2d)
    mask_cond = (permafrost_mask <= 0) | (permafrost_mask >= 2)

    #plot_utils.apply_plot_params(width_pt=None, width_cm=35,height_cm=55, font_size=35)
    fig = plt.figure()
    assert isinstance(fig, Figure)

    cmap = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=True)
    gs = gridspec.GridSpec(3,
                           2,
                           width_ratios=[1, 0.1],
                           hspace=0,
                           wspace=0,
                           left=0.05,
                           bottom=0.01,
                           top=0.95)

    all_axes = []
    all_img = []

    i = 0
    for name in sim_names:
        path = simname_to_path[name]
        dm = CRCMDataManager(data_folder=path)
        mod = dm.get_mean_over_months_of_2d_var(start_year,
                                                end_year,
                                                months=[6, 7, 8],
                                                var_name="TT",
                                                level=1,
                                                level_kind=level_kinds.HYBRID)

        delta = mod - obs
        ax = fig.add_subplot(gs[i, 0])
        assert isinstance(ax, Axes)
        delta = np.ma.masked_where(mask_cond, delta)
        img = basemap.pcolormesh(x, y, delta, cmap=cmap, vmin=-5.0, vmax=5.0)
        if not i:
            ax.set_title("T2m, Mod - Obs, ({0} - {1}), JJA \n".format(
                start_year, end_year))
        i += 1
        #ax.set_ylabel(name)
        all_axes.append(ax)
        all_img.append(img)

    i = 0
    axs_to_hide = []
    #zones and coastlines
    for the_ax, the_img in zip(all_axes, all_img):
        #        divider = make_axes_locatable(the_ax)
        #        cax = divider.append_axes("right", "5%", pad="3%")
        #cax.set_title("%\n")
        assert isinstance(the_ax, Axes)
        basemap.drawcoastlines(ax=the_ax, linewidth=0.5)
        basemap.readshapefile("data/pf_4/permafrost8_wgs84/permaice",
                              name="zone",
                              ax=the_ax,
                              linewidth=1.5,
                              drawbounds=False)

        for nshape, seg in enumerate(basemap.zone):
            if basemap.zone_info[nshape]["EXTENT"] != "C": continue
            poly = mpl.patches.Polygon(seg,
                                       edgecolor="k",
                                       facecolor="none",
                                       zorder=10,
                                       lw=1.5)
            the_ax.add_patch(poly)

        i += 1

    cax = fig.add_subplot(gs[:, 1])
    cax.set_anchor("W")
    cax.set_aspect(30)
    formatter = FuncFormatter(lambda x, pos: "{0: <6}".format(str(x)))
    cb = fig.colorbar(all_img[0],
                      ax=cax,
                      cax=cax,
                      extend="both",
                      format=formatter)
    cax.set_title("$^{\\circ} {\\rm C}$")

    #fig.tight_layout(h_pad=0)

    #    for the_ax in axs_to_hide:
    #        the_ax.set_visible(False)

    fig.savefig("tmp_validation_era_mpi_canesm.png")
Beispiel #10
0
def validate_thawing_index():
    start_year = 1980
    end_year = 1996

    sim_data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1"

    sim_names = ["ERA40", "MPI", "CanESM"]
    fold = "/home/samira/skynet/DailyTempData"
    simname_to_path = {
        "ERA40": os.path.join(fold, "TempERA40_b1_1981-2008.nc"),
        "MPI": os.path.join(fold, "TempMPI1981-2010.nc"),
        "CanESM": os.path.join(fold, "TempCanESM1981-2010.nc"),
    }

    coord_file = os.path.join(
        sim_data_folder, "pmNorthAmerica_0.44deg_ERA40-Int_B1_200812_moyenne")
    basemap, lons2d, lats2d = draw_regions.get_basemap_and_coords(
        resolution="c",
        file_path=coord_file,
        llcrnrlat=40.0,
        llcrnrlon=-145,
        urcrnrlon=-20,
        urcrnrlat=74)
    assert isinstance(basemap, Basemap)

    lons2d[lons2d > 180] -= 360

    om = CRUDataManager()
    clim = om.get_daily_climatology(start_year, end_year)
    obs = om.get_thawing_index_from_climatology(clim)
    obs = om.interpolate_data_to(
        obs, lons2d, lats2d, nneighbours=1)  #interpolatee to the model grid

    x, y = basemap(lons2d, lats2d)
    #x = (x[1:,1:] + x[:-1, :-1]) /2.0

    permafrost_mask = draw_regions.get_permafrost_mask(lons2d, lats2d)
    mask_cond = (permafrost_mask <= 0) | (permafrost_mask >= 3)

    #plot_utils.apply_plot_params(width_pt=None, width_cm=35,height_cm=55, font_size=35)
    fig = plt.figure()
    assert isinstance(fig, Figure)

    cmap = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=True)
    gs = gridspec.GridSpec(3, 1)

    all_axes = []
    all_img = []

    i = 0
    for name in sim_names:
        path = simname_to_path[name]

        ds = Dataset(path)
        data_mod = ds.variables["air"][:]
        mod = _get_thawing_index()

        delta = mod - obs
        ax = fig.add_subplot(gs[i, 0])
        assert isinstance(ax, Axes)
        delta = np.ma.masked_where(mask_cond, delta)
        img = basemap.pcolormesh(x, y, delta, cmap=cmap, vmin=None, vmax=None)
        if not i:
            ax.set_title("Thawing index, Mod - Obs, ({0} - {1}) \n".format(
                start_year, end_year))
        i += 1
        ax.set_ylabel(name)
        all_axes.append(ax)
        all_img.append(img)

    i = 0
    axs_to_hide = []
    #zones and coastlines
    for the_ax, the_img in zip(all_axes, all_img):
        divider = make_axes_locatable(the_ax)
        cax = divider.append_axes("right", "5%", pad="3%")
        cb = fig.colorbar(the_img, cax=cax, extend="both")
        cax.set_title("$^{\\circ} {\\rm C}$\n")
        #cax.set_title("%\n")
        assert isinstance(the_ax, Axes)
        basemap.drawcoastlines(ax=the_ax, linewidth=1.5)
        basemap.readshapefile("data/pf_4/permafrost8_wgs84/permaice",
                              name="zone",
                              ax=the_ax,
                              linewidth=3)

        if i != 1:
            axs_to_hide.append(cax)
        i += 1

    fig.tight_layout()

    for the_ax in axs_to_hide:
        the_ax.set_visible(False)

    fig.savefig("tmp_validation_era_mpi_canesm.png")

    pass
Beispiel #11
0
def main():
    erainterim_075_folder = "/HOME/data/Validation/ERA-Interim_0.75/Offline_driving_data/3h_Forecast"

    vname = "PR"
    start_year = 1980
    end_year = 2010

    season_key = "summer"
    season_labels = {season_key: "Summer"}
    season_to_months = OrderedDict([(season_key, [6, 7, 8])])

    # Validate temperature and precip
    model_vars = ["TT", "PR"]
    obs_vars = ["tmp", "pre"]

    obs_paths = [
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.tmp.dat.nc",
        "/HOME/data/Validation/CRU_TS_3.1/Original_files_gzipped/cru_ts_3_10.1901.2009.pre.dat.nc",
    ]

    model_var_to_obs_var = dict(zip(model_vars, obs_vars))
    model_var_to_obs_path = dict(zip(model_vars, obs_paths))

    obs_path = model_var_to_obs_path[vname]

    cru = CRUDataManager(var_name=model_var_to_obs_var[vname], path=obs_path)

    # Calculate seasonal means for CRU
    seasonal_clim_fields_cru = cru.get_seasonal_means(
        season_name_to_months=season_to_months, start_year=start_year, end_year=end_year
    )

    # Calculate seasonal mean for erai
    flist = get_files_for_season(
        erainterim_075_folder, start_year=start_year, end_year=end_year, months=season_to_months[season_key]
    )

    rpf = MultiRPN(flist)
    date_to_field_erai075 = rpf.get_all_time_records_for_name_and_level(varname=vname, level=-1)

    # Convert to mm/day
    era075 = np.mean([field for field in date_to_field_erai075.values()], axis=0) * 24 * 3600 * 1000
    lons_era, lats_era = rpf.get_longitudes_and_latitudes_of_the_last_read_rec()

    seasonal_clim_fields_cru_interp = OrderedDict()

    # Calculate biases
    for season, cru_field in seasonal_clim_fields_cru.items():
        seasonal_clim_fields_cru_interp[season] = cru.interpolate_data_to(
            cru_field, lons2d=lons_era, lats2d=lats_era, nneighbours=1
        )

    # Do the plotting ------------------------------------------------------------------------------
    plot_utils.apply_plot_params()
    fig = plt.figure()

    b = Basemap()
    gs = gridspec.GridSpec(nrows=3, ncols=1)

    ax = fig.add_subplot(gs[0, 0])
    xx, yy = b(cru.lons2d, cru.lats2d)
    cs = b.contourf(xx, yy, seasonal_clim_fields_cru[season_key], 20)
    b.drawcoastlines(ax=ax)
    ax.set_title("CRU")
    plt.colorbar(cs, ax=ax)

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

    lons_era[lons_era > 180] -= 360
    lons_era, era075 = b.shiftdata(lons_era, datain=era075, lon_0=0)
    xx, yy = b(lons_era, lats_era)

    # mask oceans in the era plot as well
    era075 = maskoceans(lons_era, lats_era, era075)

    cs = b.contourf(xx, yy, era075, levels=cs.levels, norm=cs.norm, cmap=cs.cmap, ax=ax)
    b.drawcoastlines(ax=ax)
    ax.set_title("ERA-Interim 0.75")
    plt.colorbar(cs, ax=ax)

    # differences
    ax = fig.add_subplot(gs[2, 0])
    diff = era075 - seasonal_clim_fields_cru_interp[season_key]
    delta = np.percentile(np.abs(diff)[~diff.mask], 90)
    clevs = np.linspace(-delta, delta, 20)

    cs = b.contourf(xx, yy, diff, levels=clevs, cmap="RdBu_r", extend="both")
    b.drawcoastlines(ax=ax)
    ax.set_title("ERA-Interim 0.75 - CRU")
    plt.colorbar(cs, ax=ax)

    plt.show()

    fig.savefig(os.path.join(img_folder, "erai0.75_vs_cru_precip.png"), bbox_inches="tight")
Beispiel #12
0
def compare_for_season(   start_year = 1958,
    end_year = 1974,
    the_months = None,
    period_str = "djf"):

    """
    Compare CRU, ERA40-driven and GCM-driven s
    """
    #b, lons2d, lats2d = draw_regions.get_basemap_and_coords(llcrnrlat=40.0, llcrnrlon=-145, urcrnrlon=-10)
    b, lons2d, lats2d = draw_regions.get_basemap_and_coords()
    lons2d[lons2d > 180] -= 360
    x, y = b(lons2d, lats2d)


    cru = CRUDataManager()
    cru_data = cru.get_mean(start_year,end_year, months = the_months)
    cru_data_interp = cru.interpolate_data_to(cru_data, lons2d, lats2d)


    temp_levels = np.arange(-40, 40, 5)
    diff_levels = np.arange(-10, 12, 2)
    gs = gridspec.GridSpec(3,2)
    #plot_utils.apply_plot_params(width_pt=None, height_cm=20, width_cm=20, font_size=12)
    fig = plt.figure()
    coast_line_width = 0.25
    axes_list = []



    #plot CRU data
    ax = fig.add_subplot(gs[0,:])
    axes_list.append(ax)
    cru_data_interp = maskoceans(lons2d, lats2d, cru_data_interp)
    img = b.contourf(x, y, cru_data_interp, ax = ax, levels = temp_levels)
    ax.set_title("CRU")
    plot_utils.draw_colorbar(fig, img, ax = ax)


    #era40 driven
    file_path = None
    era40_folder = "data/CORDEX/na/era40_1"
    file_prefix = "dm"
    for file_name in os.listdir(era40_folder):
        if period_str.upper() in file_name and file_name.startswith(file_prefix):
            file_path = os.path.join(era40_folder, file_name)
            break
    #get the temperature
    rpn_obj = RPN(file_path)
    t2m_era40 = rpn_obj.get_first_record_for_name_and_level(varname="TT",
            level=1, level_kind=level_kinds.HYBRID)
    t2m_era40 = maskoceans(lons2d, lats2d, t2m_era40)
    ax = fig.add_subplot(gs[1,0])
    axes_list.append(ax)
    img = b.contourf(x, y, t2m_era40, ax = ax, levels = temp_levels)
    ax.set_title("ERA40 driven 1 (1958-1961)")
    plot_utils.draw_colorbar(fig, img, ax = ax)
    rpn_obj.close()

    #era40 - cru
    ax = fig.add_subplot(gs[1,1])
    axes_list.append(ax)
    img = b.contourf(x, y, t2m_era40 - cru_data_interp, ax = ax, levels = diff_levels)
    ax.set_title("ERA40 driven 1 - CRU")
    plot_utils.draw_colorbar(fig, img, ax = ax)


    plot_e2_data = False
    if plot_e2_data:
        ##get and plot E2 data
        file_path = None
        e2_folder = "data/CORDEX/na/e2"
        prefix = "dm"
        #get file path
        for file_name in os.listdir(e2_folder):
            if file_name.endswith(period_str) and file_name.startswith(prefix):
                file_path = os.path.join(e2_folder, file_name)
                break
            pass
        #get the temperature
        rpn_obj = RPN(file_path)
        t2m = rpn_obj.get_first_record_for_name_and_level(varname="TT",
                level=1, level_kind=level_kinds.HYBRID)
        t2m = maskoceans(lons2d, lats2d, t2m)
        ax = fig.add_subplot(gs[2,0])
        axes_list.append(ax)
        img = b.contourf(x, y, t2m, ax = ax, levels = temp_levels)
        ax.set_title("E2, GCM driven")
        plot_utils.draw_colorbar(fig, img, ax = ax)

        #e2 - cru
        ax = fig.add_subplot(gs[2,1])
        axes_list.append(ax)
        img = b.contourf(x, y, t2m - cru_data_interp, ax = ax, levels = diff_levels)
        ax.set_title("E2, GCM driven - CRU")
        plot_utils.draw_colorbar(fig, img, ax = ax)


    ####Draw common elements
    pf_kinds = draw_regions.get_permafrost_mask(lons2d, lats2d)
    for the_ax in axes_list:
        b.drawcoastlines(ax = the_ax, linewidth = coast_line_width)
        b.contour(x, y, pf_kinds, ax = the_ax, colors = "k")



    gs.tight_layout(fig, h_pad = 5, w_pad = 5, pad=2)
    fig.suptitle(period_str.upper(), y = 0.03, x = 0.5)
    fig.savefig("temperature_validation_{0}.png".format(period_str))





    fig = plt.figure()
    ax = plt.gca()
    img = b.contourf(x, y, t2m_era40 - cru_data_interp, ax = ax, levels = diff_levels)
    ax.set_title("ERA40 driven 1 - CRU")
    plot_utils.draw_colorbar(fig, img, ax = ax)
    b.drawcoastlines(ax = ax, linewidth = coast_line_width)
    b.contour(x, y, pf_kinds, ax = ax, colors = "k")
    fig.savefig("temperature_diff_{0}.png".format(period_str))

    pass
Beispiel #13
0
def validate_using_monthly_diagnostics():
    start_year = 1980
    end_year = 1996




    sim_data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1"

    sim_names = ["ERA40","MPI","CanESM"]
    simname_to_path = {
        "ERA40": "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1_dm",
        "MPI": "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_MPI_B1_dm",
        "CanESM": "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_CanESM_B1_dm"
    }


    coord_file = os.path.join(sim_data_folder, "pmNorthAmerica_0.44deg_ERA40-Int_B1_200812_moyenne")
    basemap, lons2d, lats2d = draw_regions.get_basemap_and_coords(resolution="c",
        file_path = coord_file, llcrnrlat=45.0, llcrnrlon=-145, urcrnrlon=-20, urcrnrlat=74,
        anchor = "W"
    )
    assert isinstance(basemap, Basemap)

    lons2d[lons2d > 180] -= 360

    obs_manager = CRUDataManager()
    obs = obs_manager.get_mean(start_year, end_year, months=[6,7,8])
    obs = obs_manager.interpolate_data_to(obs, lons2d, lats2d, nneighbours=1)



    x, y = basemap(lons2d, lats2d)

    #x = (x[1:,1:] + x[:-1, :-1]) /2.0


    permafrost_mask = draw_regions.get_permafrost_mask(lons2d, lats2d)
    mask_cond = (permafrost_mask <= 0) | (permafrost_mask >= 2)

    #plot_utils.apply_plot_params(width_pt=None, width_cm=35,height_cm=55, font_size=35)
    fig = plt.figure()
    assert isinstance(fig, Figure)


    cmap = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=True)
    gs = gridspec.GridSpec(3,2, width_ratios=[1,0.1], hspace=0, wspace=0,
        left=0.05, bottom = 0.01, top=0.95)

    all_axes = []
    all_img = []


    i = 0
    for name in sim_names:
        path = simname_to_path[name]
        dm = CRCMDataManager(data_folder=path)
        mod = dm.get_mean_over_months_of_2d_var(start_year, end_year, months = [6,7,8],
            var_name="TT", level=1, level_kind=level_kinds.HYBRID)

        delta = mod - obs
        ax = fig.add_subplot(gs[i,0])
        assert isinstance(ax, Axes)
        delta = np.ma.masked_where(mask_cond, delta)
        img = basemap.pcolormesh(x, y, delta, cmap = cmap, vmin=-5.0, vmax = 5.0)
        if not i:
            ax.set_title("T2m, Mod - Obs, ({0} - {1}), JJA \n".format(start_year, end_year))
        i += 1
        #ax.set_ylabel(name)
        all_axes.append(ax)
        all_img.append(img)



    i = 0
    axs_to_hide = []
    #zones and coastlines
    for the_ax, the_img in zip(all_axes, all_img):
#        divider = make_axes_locatable(the_ax)
#        cax = divider.append_axes("right", "5%", pad="3%")
        #cax.set_title("%\n")
        assert isinstance(the_ax, Axes)
        basemap.drawcoastlines(ax = the_ax, linewidth=0.5)
        basemap.readshapefile("data/pf_4/permafrost8_wgs84/permaice", name="zone",
                ax=the_ax, linewidth=1.5, drawbounds=False)

        for nshape,seg in enumerate(basemap.zone):
            if basemap.zone_info[nshape]["EXTENT"] != "C": continue
            poly = mpl.patches.Polygon(seg,edgecolor = "k", facecolor="none", zorder = 10, lw = 1.5)
            the_ax.add_patch(poly)

        i += 1



    cax = fig.add_subplot(gs[:,1])
    cax.set_anchor("W")
    cax.set_aspect(30)
    formatter = FuncFormatter(
        lambda x, pos: "{0: <6}".format(str(x))
    )
    cb = fig.colorbar(all_img[0], ax = cax, cax = cax,
        extend = "both", format = formatter)
    cax.set_title("$^{\\circ} {\\rm C}$")

    #fig.tight_layout(h_pad=0)



#    for the_ax in axs_to_hide:
#        the_ax.set_visible(False)

    fig.savefig("tmp_validation_era_mpi_canesm.png")
Beispiel #14
0
def validate_thawing_index():
    start_year = 1980
    end_year = 1996

    sim_data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1"

    sim_names = ["ERA40","MPI","CanESM"]
    fold = "/home/samira/skynet/DailyTempData"
    simname_to_path = {
        "ERA40": os.path.join(fold, "TempERA40_b1_1981-2008.nc"),
        "MPI": os.path.join(fold, "TempMPI1981-2010.nc"),
        "CanESM":os.path.join(fold, "TempCanESM1981-2010.nc"),
    }


    coord_file = os.path.join(sim_data_folder, "pmNorthAmerica_0.44deg_ERA40-Int_B1_200812_moyenne")
    basemap, lons2d, lats2d = draw_regions.get_basemap_and_coords(resolution="c",
        file_path = coord_file, llcrnrlat=40.0, llcrnrlon=-145, urcrnrlon=-20, urcrnrlat=74
    )
    assert isinstance(basemap, Basemap)

    lons2d[lons2d > 180] -= 360

    om = CRUDataManager()
    clim = om.get_daily_climatology(start_year, end_year)
    obs = om.get_thawing_index_from_climatology(clim)
    obs = om.interpolate_data_to(obs, lons2d, lats2d, nneighbours=1) #interpolatee to the model grid




    x, y = basemap(lons2d, lats2d)
    #x = (x[1:,1:] + x[:-1, :-1]) /2.0


    permafrost_mask = draw_regions.get_permafrost_mask(lons2d, lats2d)
    mask_cond = (permafrost_mask <= 0) | (permafrost_mask >= 3)

    #plot_utils.apply_plot_params(width_pt=None, width_cm=35,height_cm=55, font_size=35)
    fig = plt.figure()
    assert isinstance(fig, Figure)


    cmap = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=True)
    gs = gridspec.GridSpec(3,1)

    all_axes = []
    all_img = []


    i = 0
    for name in sim_names:
        path = simname_to_path[name]

        ds = Dataset(path)
        data_mod = ds.variables["air"][:]
        mod = _get_thawing_index()



        delta = mod - obs
        ax = fig.add_subplot(gs[i,0])
        assert isinstance(ax, Axes)
        delta = np.ma.masked_where(mask_cond, delta)
        img = basemap.pcolormesh(x, y, delta, cmap = cmap, vmin=None, vmax = None)
        if not i:
            ax.set_title("Thawing index, Mod - Obs, ({0} - {1}) \n".format(start_year, end_year))
        i += 1
        ax.set_ylabel(name)
        all_axes.append(ax)
        all_img.append(img)



    i = 0
    axs_to_hide = []
    #zones and coastlines
    for the_ax, the_img in zip(all_axes, all_img):
        divider = make_axes_locatable(the_ax)
        cax = divider.append_axes("right", "5%", pad="3%")
        cb = fig.colorbar(the_img,  cax = cax, extend = "both")
        cax.set_title("$^{\\circ} {\\rm C}$\n")
        #cax.set_title("%\n")
        assert isinstance(the_ax, Axes)
        basemap.drawcoastlines(ax = the_ax, linewidth=1.5)
        basemap.readshapefile("data/pf_4/permafrost8_wgs84/permaice", name="zone",
                ax=the_ax, linewidth=3)

        if i != 1:
            axs_to_hide.append(cax)
        i += 1

    fig.tight_layout()

    for the_ax in axs_to_hide:
        the_ax.set_visible(False)

    fig.savefig("tmp_validation_era_mpi_canesm.png")

    pass