Пример #1
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
Пример #2
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")
def main():

    sim_names = ["MPI", "CanESM" ]
    data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/from_Leo"
    current_paths = [
         os.path.join(data_folder, "c_MPI-ESM-LR_pr_JJA_1981-2010_rg"),
         os.path.join(data_folder, "c_CanESM2_pr_JJA_1981-2010_rg")
    ]

    future_paths = [

         os.path.join(data_folder, "c_MPI-ESM-LR_pr_JJA_2071-2100_rg"),
         os.path.join(data_folder, "c_CanESM2_pr_JJA_2071-2100_rg")

    ]



    fig = plt.figure()
    assert isinstance(fig, Figure)


    gs = gridspec.GridSpec(2,2, width_ratios=[1, 0.07], bottom=0.01, left=0.01, top=0.96,
        hspace=0.1, wspace=0)
    cmap = my_colormaps.get_red_blue_colormap(ncolors=10)

    basemap, lons2d, lats2d = draw_regions.get_basemap_and_coords()
    lons2d[lons2d > 180] -= 360

    x, y = basemap(lons2d, lats2d)
    i = 0
    img = None
    for the_name, cur_path, fut_path in zip(sim_names, current_paths, future_paths):

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

        rc = RPN(cur_path)
        data_c = rc.get_first_record_for_name("PR")

        rf = RPN(fut_path)
        data_f = rf.get_first_record_for_name("PR")

        img = basemap.pcolormesh(x, y, (data_f - data_c) / data_c * 100.0,
            ax = ax, cmap = cmap, vmax = 100, vmin = -100)
        basemap.drawcoastlines(ax = ax)
        ax.set_title(the_name)
        i += 1


    ax = fig.add_subplot(gs[:,1])
    ax.set_aspect(40)
    fig.colorbar(img, cax = ax, extend = "both")

    #fig.tight_layout()

    #plt.show()
    fig.savefig("clim_change_pr_mpi_canesm.png")


    #TODO: implement
    pass
Пример #4
0
def plot_climate_change():

    start_year_current = 1981
    end_year_current = 2010

    start_year_future = 2071
    end_year_future = 2100

    periods = "F2 - C"

    #plot changes
    sim_data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1"
    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,
        anchor="W")
    assert isinstance(basemap, Basemap)

    #basemap.transform_scalar()

    #basemap = Basemap()
    lons2d[lons2d > 180] -= 360

    x, y = basemap(lons2d, lats2d)

    cmap = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=True)

    sim_names = ["MPI", "CanESM"]
    simname_to_path = {
        "MPI":
        "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_MPI_B1",
        "CanESM":
        "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_CanESM_B1"
    }
    permafrost_mask = draw_regions.get_permafrost_mask(lons2d, lats2d)
    mask_cond = (permafrost_mask <= 0) | (permafrost_mask >= 2)

    #alt
    #    folder = "/home/huziy/skynet1_rech3/cordex/for_Samira"
    #    alt_sim_name_to_path = {
    #        "MPI": os.path.join(folder, "alt_mpi_b1_yearly.nc"),
    #        "CanESM": os.path.join(folder, "alt_canesm_b1_yearly.nc")
    #    }
    alt_sim_name_to_path = simname_to_path

    name_to_delta = {}
    for name in sim_names:
        path = alt_sim_name_to_path[name]

        #select data and needed alt
        #ds = Dataset(path)
        #years = ds.variables["year"][:]
        #hct = ds.variables["alt"][(years >= start_year_current) & (years <= end_year_current),:,:]

        #print "hct.shape = ", hct.shape
        #hc = alt_mod.get_alt_using_nyear_rule(hct, nyears = 2)

        #hft = ds.variables["alt"][(years >= start_year_future) & (years <= end_year_future),:,:]
        #hf = alt_mod.get_alt_using_nyear_rule(hft, nyears = 2)

        dm = CRCMDataManager(data_folder=path)
        hc = dm.get_alt_using_monthly_mean_climatology(
            range(start_year_current, end_year_current + 1))
        hf = dm.get_alt_using_monthly_mean_climatology(
            range(start_year_future, end_year_future + 1))

        good_points = (hf >= 0) & (hc > 0) & (~mask_cond)
        d = np.ma.masked_all(hf.shape)
        d[good_points] = (hf[good_points] -
                          hc[good_points]) / hc[good_points] * 100.0
        name_to_delta[name] = d

        d = (hf - hc) / hc * 100.0
        print(name)
        the_is, the_js = np.where((hf < hc) & good_points)


#        for i,j in zip(the_is, the_js):
#            print(i,j,lons2d[i,j], lats2d[i,j], hc[i,j], hf[i,j], d[i,j])
#        if name == "CanESM":
#            raise Exception

    plot_column(names=sim_names,
                name_to_data=name_to_delta,
                title="ALT, {0}".format(periods),
                x=x,
                y=y,
                basemap=basemap,
                img_file="alt_b1_cc1.png",
                cmap=cmap,
                vminmax=(-100, 100),
                mask=mask_cond,
                units="%",
                extend="both")

    #swe
    cmap_swe = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=False)

    name_to_delta = {}
    for name in sim_names:
        path = simname_to_path[name]
        name_to_delta[name] = get_delta("PR",
                                        start_year_current,
                                        end_year_current,
                                        start_year_future,
                                        end_year_future,
                                        data_folder=path,
                                        months=[6, 7, 8],
                                        percentage=True)

    plot_column(names=sim_names,
                name_to_data=name_to_delta,
                title="PREC, {0}, JJA".format(periods),
                x=x,
                y=y,
                basemap=basemap,
                img_file="pr_b1_cc1.png",
                cmap=cmap_swe,
                vminmax=(-100, 100),
                mask=mask_cond,
                units="%",
                extend="both")

    #temperature
    sim_names = ["MPI", "CanESM"]
    simname_to_path = {
        "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"
    }

    name_to_delta = {}
    for name in sim_names:
        path = simname_to_path[name]
        name_to_delta[name] = get_delta("TT",
                                        start_year_current,
                                        end_year_current,
                                        start_year_future,
                                        end_year_future,
                                        data_folder=path,
                                        months=[6, 7, 8],
                                        level=1,
                                        level_kind=level_kinds.HYBRID)

    plot_column(names=sim_names,
                name_to_data=name_to_delta,
                title="2m Tempreature, {0}, JJA".format(periods),
                x=x,
                y=y,
                basemap=basemap,
                img_file="tt_b1_cc1.png",
                cmap=my_colormaps.get_red_colormap(ncolors=8),
                vminmax=(1, 5),
                mask=mask_cond,
                units="$^{\\circ} {\\rm C}$",
                extend="max")
Пример #5
0
def plot_climate_change():

    start_year_current = 1981
    end_year_current = 2010

    start_year_future = 2071
    end_year_future = 2100

    periods = "F2 - C"

    #plot changes
    sim_data_folder = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/era40_driven_b1"
    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, anchor="W"
    )
    assert isinstance(basemap, Basemap)

    #basemap.transform_scalar()

    #basemap = Basemap()
    lons2d[lons2d > 180] -= 360

    x, y = basemap(lons2d, lats2d)


    cmap = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=True)

    sim_names = ["MPI",
        "CanESM"
    ]
    simname_to_path = {
        "MPI": "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_MPI_B1",
        "CanESM": "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_CanESM_B1"


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



    #alt
#    folder = "/home/huziy/skynet1_rech3/cordex/for_Samira"
#    alt_sim_name_to_path = {
#        "MPI": os.path.join(folder, "alt_mpi_b1_yearly.nc"),
#        "CanESM": os.path.join(folder, "alt_canesm_b1_yearly.nc")
#    }
    alt_sim_name_to_path = simname_to_path


    name_to_delta = {}
    for name in sim_names:
        path = alt_sim_name_to_path[name]

        #select data and needed alt
        #ds = Dataset(path)
        #years = ds.variables["year"][:]
        #hct = ds.variables["alt"][(years >= start_year_current) & (years <= end_year_current),:,:]

        #print "hct.shape = ", hct.shape
        #hc = alt_mod.get_alt_using_nyear_rule(hct, nyears = 2)

        #hft = ds.variables["alt"][(years >= start_year_future) & (years <= end_year_future),:,:]
        #hf = alt_mod.get_alt_using_nyear_rule(hft, nyears = 2)

        dm = CRCMDataManager(data_folder=path)
        hc = dm.get_alt_using_monthly_mean_climatology(range(start_year_current,end_year_current+1))
        hf = dm.get_alt_using_monthly_mean_climatology(range(start_year_future,end_year_future+1))

        good_points = (hf >= 0) & (hc > 0) & (~mask_cond)
        d = np.ma.masked_all(hf.shape)
        d[good_points] = (hf[good_points] - hc[good_points]) / hc[good_points] * 100.0
        name_to_delta[name] = d

        d = (hf - hc) / hc * 100.0
        print(name)
        the_is, the_js = np.where((hf < hc) & good_points)

#        for i,j in zip(the_is, the_js):
#            print(i,j,lons2d[i,j], lats2d[i,j], hc[i,j], hf[i,j], d[i,j])
#        if name == "CanESM":
#            raise Exception

    plot_column(names=sim_names, name_to_data=name_to_delta,title="ALT, {0}".format(periods), x = x, y = y, basemap=basemap,
        img_file="alt_b1_cc1.png", cmap=cmap, vminmax=(-100, 100), mask=mask_cond, units = "%", extend = "both"
    )


    #swe
    cmap_swe = my_colormaps.get_red_blue_colormap(ncolors=10, reversed=False)

    name_to_delta = {}
    for name in sim_names:
        path = simname_to_path[name]
        name_to_delta[name] = get_delta("PR", start_year_current, end_year_current,
            start_year_future, end_year_future, data_folder = path, months=[6,7,8], percentage=True)

    plot_column(names=sim_names, name_to_data=name_to_delta,title="PREC, {0}, JJA".format(periods), x = x, y = y, basemap=basemap,
        img_file="pr_b1_cc1.png", cmap=cmap_swe, vminmax=(-100, 100), mask=mask_cond,
        units="%", extend = "both"
    )



    #temperature
    sim_names = ["MPI",
                 "CanESM"]
    simname_to_path = {
         "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"
    }

    name_to_delta = {}
    for name in sim_names:
        path = simname_to_path[name]
        name_to_delta[name] = get_delta("TT", start_year_current, end_year_current,
            start_year_future, end_year_future, data_folder = path, months=[6,7,8],
            level=1, level_kind=level_kinds.HYBRID)


    plot_column(names=sim_names, name_to_data=name_to_delta,title="2m Tempreature, {0}, JJA".format(periods), x = x, y = y, basemap=basemap,
        img_file="tt_b1_cc1.png", cmap=my_colormaps.get_red_colormap(ncolors = 8), vminmax=(1, 5), mask=mask_cond, units="$^{\\circ} {\\rm C}$", extend = "max"
    )
Пример #6
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")
Пример #7
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