Exemple #1
0
from thesis import *
import geomappy as mp

M_p = p = mp.Raster("data/precipitation/precipitation.tif")

pet = mp.Raster("data/pet/et0_yr.tif", fill_value=0)[M_p.bounds]
p = M_p[0]

M_p_pet = mp.Raster("data/pet/p_pet.tif",
                    mode='w',
                    ref_map="data/precipitation/precipitation.tif",
                    overwrite=True)
M_p_pet[0] = p / pet

mp.Raster.close()
CORR_WTD_T_15 = "data/correlation_wtd_t/correlation_wtd_t_15.tif"
CORR_P_T_15 = "/Volumes/Elements SE/Thesis/Data/correlation_p_t/correlation_p_t_15.tif"
CORR_T_FAPAR_15 = "data/correlation_t_fapar/correlation_t_fapar_ge3_15.tif"
CORR_P_PET_FAPAR_15 = "data/correlation_p_pet_fapar/correlation_p_pet_fapar_ge3_15.tif"

CORR_WTD_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_wtd_fapar/correlation_wtd_fapar_ge3_15_downsampled_10.tif"
CORR_P_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_p_fapar/correlation_p_fapar_ge3_15_downsampled_10.tif"
CORR_P_WTD_15_DOWNSAMPLED_10 = "data/correlation_p_wtd/correlation_p_wtd_15_downsampled_10.tif"
CORR_P_PET_WTD_15_DOWNSAMPLED_10 = "data/correlation_p_pet_wtd/correlation_p_pet_wtd_15_downsampled_10.tif"
CORR_WTD_T_15_DOWNSAMPLED_10 = "data/correlation_wtd_t/correlation_wtd_t_15_downsampled_10.tif"
CORR_P_T_15_DOWNSAMPLED_10 = "/Volumes/Elements SE/Thesis/Data/correlation_p_t/correlation_p_t_15_downsampled_10.tif"
CORR_T_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_t_fapar/correlation_t_fapar_ge3_15_downsampled_10.tif"
CORR_P_PET_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_p_pet_fapar/correlation_p_pet_fapar_ge3_15_downsampled_10.tif"

### Correlate WTD and fPAR
M_wtd = mp.Raster(WTD, tiles=(10, 10), window_size=15)
M_th = mp.Raster(TH, tiles=(10, 10), window_size=15)
M_fapar = mp.Raster(FAPAR, tiles=(10, 10), window_size=15)

with mp.Raster(CORR_WTD_FAPAR_15, mode='w', ref_map=WTD, tiles=(10, 10), window_size=15) as M_corr_wtd_fapar_15:
    for i in M_wtd:
        progress_bar((i + 1) / M_wtd.c_tiles)
        wtd = M_wtd[i]
        th = M_th[i]
        fapar = M_fapar[i]
        fapar[th < 3] = np.nan
        M_corr_wtd_fapar_15[i] = mp.correlate_maps(wtd, fapar, window_size=15, fraction_accepted=0.25)

M_corr_wtd_fapar_15 = mp.Raster(CORR_WTD_FAPAR_15, tiles=(10, 10))
M_corr_wtd_fapar_15.focal_mean(output_file=CORR_WTD_FAPAR_15_DOWNSAMPLED_10, window_size=10, reduce=True,
                               fraction_accepted=0)
Exemple #3
0
"""
Convert water table depth dataset to be defined negatively
"""
from thesis import *
import geomappy as mp
import numpy as np

WTD_ORIGINAL = "data/wtd/wtd_original.tif"
WTD_CONVERTED = "data/wtd/wtd_2.tif"

with mp.Raster(WTD_ORIGINAL) as src:
    with mp.Raster(WTD_CONVERTED, ref_map=WTD_ORIGINAL, mode='w', nodata=np.nan, dtype=np.float32, overwrite=True) \
            as dst:
        dst[0] = -src[0]
Exemple #4
0
    return corr_t, corr_bs, corr_pe


def t_test_threshold_table(offset_fraction=0, window_size=15):
    n_max = window_size**2
    sign_threshold = np.full(n_max + 1, np.nan)
    for n in range(0, n_max + 1):
        df = n - 2 - (offset_fraction * n)
        x = t.ppf(0.95, df=df)
        r = x / np.sqrt(df + x**2)
        sign_threshold[n] = x / np.sqrt(df + x**2)
    return sign_threshold


M_wtd = mp.Raster(
    "/Volumes/Elements SE/Thesis/Data_raw/wtd/wtd_South_America_CF.tif",
    epsg=4326)
wtd = -M_wtd[0]
th = mp.Raster("data/tree_height/tree_height_global.tif")[M_wtd.bounds]
fapar = mp.Raster("data/fapar/mean_fpar_reprojected.tif")[M_wtd.bounds]
p = mp.Raster("data/pet/p_pet.tif")[M_wtd.bounds]
fapar[th < 3] = np.nan

# offset_fraction value from previous file (06_global_threshold_table.py)
WTD_corr_t, WTD_corr_bs, WTD_corr_pe = correlation_significance_inference(
    wtd,
    fapar,
    window_size=15,
    fraction_accepted=0.25,
    t_test_thresholds=t_test_threshold_table(offset_fraction=0.178))
P_corr_t, P_corr_bs, P_corr_pe = correlation_significance_inference(
import geopandas as gpd
import geomappy as mp
import numpy as np
import pandas as pd
from functions import calc_emissions

M_extent_1 = mp.Raster("data/River_network/global_chans_30arcs.tif")
M_water_bodies = mp.Raster("data/reprojected/water_bodies.tif")
M_extent_10 = mp.Raster("data/reprojected/extent_10.tif")
M_extent_20 = mp.Raster("data/reprojected/extent_20.tif")
M_extent_50 = mp.Raster("data/reprojected/extent_50.tif")
M_extent_100 = mp.Raster("data/reprojected/extent_100.tif")
M_extent_200 = mp.Raster("data/reprojected/extent_200.tif")
M_extent_500 = mp.Raster("data/reprojected/extent_500.tif")
M_mpw = mp.Raster("data/reprojected/mpw.tif")

env = {
    "M_extent_1": M_extent_1,
    "M_extent_10": M_extent_10,
    "M_extent_20": M_extent_20,
    "M_extent_50": M_extent_50,
    "M_extent_100": M_extent_100,
    "M_extent_200": M_extent_200,
    "M_extent_500": M_extent_500,
    "M_water_bodies": M_water_bodies,
    "M_mpw": M_mpw
}

continents = ['eu', 'as', 'af', 'au', 'ca', 'na', 'sa']
for continent in continents:
    print(continent)
import thesis
import geomappy as mp
from geomappy.utils import progress_bar
import numpy as np
np.warnings.filterwarnings('ignore')

M_corr_wtd = mp.Raster("data/cmap_2d_significant/wtd_fapar_sign.tif")
M_corr_p_pet = mp.Raster("data/cmap_2d_significant/p_pet_fapar_sign.tif")

CLASSES = "data/cmap_2d_significant/classes_serial.tif"
CLASSES_FLOAT = "data/cmap_2d_significant/classes_serial_float.tif"
CLASSES_DOWNSAMPLED_10 = "data/cmap_2d_significant/classes_serial_downsampled_10.tif"

profile = M_corr_p_pet.profile
profile['dtype'] = np.int8
profile['nodata'] = -1
M_classes = mp.Raster(CLASSES, mode='w', profile=profile, overwrite=True)

profile['dtype'] = np.float64
M_classes_float = mp.Raster(CLASSES_FLOAT,
                            mode='w',
                            profile=profile,
                            overwrite=True)

mp.Raster.set_tiles((4, 4))

classes = np.arange(9, dtype=np.int8).reshape((3, 3))
classes_float = np.arange(9, dtype=np.float64).reshape((3, 3))

for i in M_corr_wtd:
    progress_bar((i + 1) / M_corr_wtd.c_tiles)
Exemple #7
0
import thesis
import geomappy as mp
from geomappy.rolling import rolling_sum
from geomappy.utils import progress_bar
from scipy.stats import t
import numpy as np
np.warnings.filterwarnings(
    'ignore')  # comparison with nans that can be ignored

M_fapar = mp.Raster("data/fapar/mean_fpar_reprojected.tif")
M_wtd = mp.Raster("data/wtd/wtd.tif")
M_corr = mp.Raster(
    "data/correlation_wtd_fapar/correlation_wtd_fapar_ge3_15.tif")
M_th = mp.Raster("data/tree_height/tree_height_global.tif")

M_sig = mp.Raster("data/cmap_2d_significant/wtd_fapar_sign.tif",
                  mode="w",
                  ref_map="data/wtd/wtd.tif")

window_size = 15
fringe = window_size // 2
n_max = window_size**2

sign_threshold = np.full(n_max + 1, np.nan)

for n in range(0, n_max + 1):
    # The 0.178 value was obtained from 06_global_threshold_table.py
    df = n - 2 - (0.178 * n)
    x = t.ppf(0.95, df=df)
    r = x / np.sqrt(df + x**2)
    sign_threshold[n] = x / np.sqrt(df + x**2)
Exemple #8
0
warnings.filterwarnings("once")


def update_class_map(ind, val):
    global classified_map, accessed_map
    ind = np.logical_and(~accessed_map, ind)
    classified_map[ind] = val
    accessed_map[ind] = True


wtd = "data/wtd/wtd_original.tif"
wtd_mean_5 = "data/wtd/wtd_mean_5.tif"
wtd_std_5 = "data/wtd/wtd_std_5.tif"
output_map = "data/landscape_classes/landscape_classes.tif"

M_wtd = mp.Raster(wtd, tiles=(20, 20))
M_wtd.focal_mean(output_file=wtd_mean_5, window_size=5)
M_wtd.focal_std(output_file=wtd_std_5, window_size=5)

M_wtd_mean_5 = mp.Raster(wtd_mean_5, tiles=(20, 20))
M_wtd_std_5 = mp.Raster(wtd_std_5, tiles=(20, 20))

M_output = mp.Raster(output_map,
                     ref_map=wtd,
                     tiles=(20, 20),
                     mode="w",
                     dtype=np.int8,
                     nodata=0,
                     overwrite=True)

for i in M_wtd:
LANDSCAPE_DOWNSAMPLED_10 = "data/landscape_classes/landscape_downsampled_10.tif"
LANDSCAPE_DOWNSAMPLED_10_DISPLAY = "data/landscape_classes/landscape_downsampled_10_display.tif"

P_DOWNSAMPLED_10 = "data/precipitation/precipitation_downsampled_10.tif"
PET_DOWNSAMPLED_10 = "data/pet/pet_downsampled_10.tif"
P_PET_DOWNSAMPLED_10 = "data/pet/p_pet_downsampled_10.tif"

WTD_DOWNSAMPLED_10 = "data/wtd/wtd_downsampled_10.tif"
WTD_STD_DOWNSAMPLED_10 = "data/wtd/wtd_std_5_downsampled_10.tif"

TH_DOWNSAMPLED_10 = "data/tree_height/tree_height_global_downsampled_10.tif"

FAPAR_DOWNSAMPLED_10 = "data/fapar/mean_fpar_downsampled_10.tif"

M_clim = mp.Raster(CLIMATE, tiles=(10, 20))
M_p = mp.Raster(P, tiles=(10, 20))
M_pet = mp.Raster(PET, tiles=(10, 20), fill_value=0)
M_p_pet = mp.Raster(P_PET, tiles=(10, 20))
M_wtd = mp.Raster(WTD, tiles=(10, 20))
M_wtd_std = mp.Raster(WTD_STD, tiles=(10, 20))
M_th = mp.Raster(TH, tiles=(10, 20))
M_fapar = mp.Raster(FAPAR, tiles=(10, 20))
M_landscape = mp.Raster(LANDSCAPE, tiles=(20, 20))

M_clim.focal_majority(output_file=CLIMATE_DOWNSAMPLED_10,
                      window_size=10,
                      reduce=True,
                      fraction_accepted=0,
                      majority_mode='nan')
M_clim.focal_majority(output_file=CLIMATE_DOWNSAMPLED_10_DISPLAY,
def draw_thesis_location_zoom(ind):
    """
    Drawing array of nine maps, displaying the input data and results. Not meant for custom use.

    Parameters
    ----------
    ind : .
        look at mp.RasterBase.get_pointer()
    """
    classes = mp.Raster(
        "data/cmap_2d_significant/classes_serial_downsampled_10.tif")
    climate = mp.Raster("data/climate/climate_downsampled_10_display.tif")
    landscape = mp.Raster(
        "data/landscape_classes/landscape_downsampled_10_display.tif")
    p_pet = mp.Raster("data/pet/p_pet_downsampled_10.tif")
    fapar = mp.Raster("data/fapar/mean_fpar_downsampled_10.tif")
    corr_wtd_fapar = mp.Raster(
        "data/correlation_wtd_fapar/correlation_wtd_fapar_ge3_15_downsampled_10.tif"
    )
    corr_p_pet_fapar = mp.Raster(
        "data/correlation_p_pet_fapar/correlation_p_pet_fapar_ge3_15_downsampled_10.tif"
    )
    corr_p_pet_wtd = mp.Raster(
        "data/correlation_p_pet_wtd/correlation_p_pet_wtd_15_downsampled_10.tif"
    )
    wtd = mp.Raster("data/wtd/wtd_downsampled_10.tif")

    f, ax = plt.subplots(3, 3, figsize=(20, 20))
    plt.tight_layout(h_pad=-15, w_pad=7)
    ax = [mp.basemap(ax=cax, coastlines=False) for cax in ax.flatten()]

    basemap_kwargs = {
        'coastline_linewidth': 0.3,
        'xticks': 5,
        'yticks': 5,
        'resolution': '10m'
    }
    fontsize = 14

    # Classes
    cmap = cmap_2d((3, 3), alpha=0.5, diverging_alpha=0.25)
    cmap[1, 1, :] = (0.9, 0.9, 0.9)
    cmap = cmap.reshape((9, 3))
    classes.plot_classified_map(ind,
                                basemap=True,
                                basemap_kwargs=basemap_kwargs,
                                legend=None,
                                colors=cmap,
                                force_equal_figsize=True,
                                ax=ax[0],
                                fontsize=fontsize)
    ax[0].set_title("Ecohydrological classes", fontsize=fontsize)

    # P/PET
    bins = np.arange(0, 3.1, 0.2)
    p_pet.plot_map(ind,
                   bins=bins,
                   basemap=True,
                   basemap_kwargs=basemap_kwargs,
                   cmap="Blues",
                   legend='colorbar',
                   ax=ax[1],
                   fontsize=fontsize,
                   legend_kwargs={
                       'title': "[-]",
                       'title_font': {
                           'pad': 10
                       }
                   })
    ax[1].set_title("P/PET", fontsize=fontsize)

    # WTD
    bins = [0, 1, 2, 5, 10, 15, 20, 25, 35, 50, 100, 300]
    bins = bins[::-1]
    bins = [-i for i in bins]
    wtd.plot_map(ind,
                 bins=bins,
                 basemap=True,
                 basemap_kwargs=basemap_kwargs,
                 cmap="Blues",
                 legend='colorbar',
                 ax=ax[2],
                 fontsize=fontsize,
                 legend_kwargs={
                     'title': "[m]",
                     'title_font': {
                         'pad': 10
                     }
                 })
    ax[2].set_title("WTD", fontsize=fontsize)

    # FAPAR
    bins = [0, 10, 20, 30, 40, 50, 60, 70, 100]
    fapar.plot_map(ind,
                   bins=bins,
                   basemap=True,
                   basemap_kwargs=basemap_kwargs,
                   cmap="Greens",
                   legend='colorbar',
                   ax=ax[3],
                   fontsize=fontsize,
                   legend_kwargs={
                       'title': "[%]",
                       'title_font': {
                           'pad': 10
                       }
                   })
    ax[3].set_title("fAPAR", fontsize=fontsize)

    # Climate
    cmap = [(1, 1, 1)]
    bins = [0]
    labels = ["Water"]
    with open("data/climate/koppen_legend.txt") as f:
        for line in f:
            line = line.strip()
            try:
                int(line[0])
                rgb = [
                    int(c) / 255 for c in line[line.find('[') + 1:-1].split()
                ]
                cmap.append(rgb)
                labels.append(line.split()[1])
                bins.append(line[:line.find(':')])
            except:
                pass

    bins = np.array(bins, dtype=np.int)
    climate.plot_classified_map(ind,
                                bins=bins,
                                basemap=True,
                                basemap_kwargs=basemap_kwargs,
                                colors=cmap,
                                labels=labels,
                                legend='colorbar',
                                ax=ax[4],
                                fontsize=fontsize,
                                suppress_warnings=True,
                                clip_legend=True)

    ax[4].set_title("Climate", fontsize=fontsize)

    # Landscape
    cmap = [
        '#004dac', '#729116', '#b1bc1d', '#e7de23', '#af9a15', '#785707',
        '#fff9f2'
    ]
    labels = [
        'Wetland and open water', 'Lowland', 'Undulating', 'Hilly',
        'Low mountainous', 'Mountainous', 'High mountainous'
    ]
    bins = [1, 2, 3, 4, 5, 6, 7]
    landscape.plot_classified_map(ind,
                                  bins=bins,
                                  basemap=True,
                                  basemap_kwargs=basemap_kwargs,
                                  colors=cmap,
                                  labels=labels,
                                  legend='colorbar',
                                  ax=ax[5],
                                  fontsize=fontsize,
                                  suppress_warnings=True)
    ax[5].set_title("Landscape", fontsize=fontsize)

    # Correlation WTD and fAPAR
    bins = [-1, -0.5, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.5, 1]
    corr_wtd_fapar.plot_map(ind,
                            bins=bins,
                            basemap=True,
                            basemap_kwargs=basemap_kwargs,
                            legend='colorbar',
                            ax=ax[6],
                            fontsize=fontsize,
                            cmap="RdYlBu_r")

    ax[6].set_title("Correlation WTD & fAPAR", fontsize=fontsize)

    # Correlation P and fAPAR
    corr_p_pet_fapar.plot_map(ind,
                              bins=bins,
                              basemap=True,
                              basemap_kwargs=basemap_kwargs,
                              legend='colorbar',
                              ax=ax[7],
                              fontsize=fontsize,
                              cmap="RdYlBu_r")

    ax[7].set_title("Correlation P/PET & fAPAR", fontsize=fontsize)

    # Correlation P/PET and fAPAR
    corr_p_pet_wtd.plot_map(ind,
                            bins=bins,
                            basemap=True,
                            basemap_kwargs=basemap_kwargs,
                            legend='colorbar',
                            ax=ax[8],
                            fontsize=fontsize,
                            cmap="RdYlBu_r")
    ax[8].set_title("Correlation P/PET & WTD", fontsize=fontsize)
    mp.Raster.close(verbose=False)
def draw_thesis_map(loc,
                    loc_small=None,
                    classified=False,
                    legend_2d=False,
                    legend=None,
                    fontsize=28,
                    **kwargs):
    """
    Plot map for thesis. Not intended for reuse.
    """
    fontdict = {'fontsize': fontsize, 'va': 'center', 'ha': 'center'}

    M_thesis = mp.Raster(loc)
    if isinstance(loc_small, type(None)):
        loc_small = loc
    M_thesis_small = mp.Raster(loc_small)

    inset_coords = (
        (-93, 29, -78, 44),  # Mississippi
        (7, 35, 22, 50),  # Italy
        (15, -7, 30, 8),  # Congo
        (142, -35, 157, -20))  # Australia

    bounds = [-180, -89, 180, 90]
    basemap_kwargs = {'coastline_linewidth': 0.25}
    ax = mp.basemap(*bounds, figsize=(40, 40), **basemap_kwargs)

    bounds = (-180, -60, 180, 90)
    extent = (bounds[0], bounds[2], bounds[1], bounds[3])
    if classified:
        ax, _legend = M_thesis.plot_classified_map(bounds,
                                                   ax=ax,
                                                   suppress_warnings=True,
                                                   legend=legend,
                                                   extent=extent,
                                                   transform=ax.projection,
                                                   **kwargs)
    else:
        ax, _legend = M_thesis.plot_map(bounds,
                                        ax=ax,
                                        legend=legend,
                                        extent=extent,
                                        transform=ax.projection,
                                        **kwargs)

    inset_locations = [(-55, -88, 44, 44), (-5, -88, 44, 44),
                       (45, -88, 44, 44), (95, -88, 44, 44)]

    inset_locations = [((loc[0] + 180) / 360, (loc[1] + 90) / 180,
                        loc[2] / 360, loc[3] / 180) for loc in inset_locations]

    labels = [chr(65 + i) for i in range(len(inset_locations))]

    for i in range(len(inset_locations)):
        left, bottom, right, top = inset_coords[i]

        ax_inset = plt.gcf().add_subplot(projection=ccrs.PlateCarree(),
                                         label=f"{np.random.rand()}")

        basemap_kwargs = {'grid': False, 'coastline_linewidth': 0.5}
        if classified:
            ax_inset, _ = M_thesis_small.plot_classified_map(
                inset_coords[i],
                basemap=True,
                fontsize=fontsize,
                resolution='10m',
                ax=ax_inset,
                xticks=[],
                yticks=[],
                basemap_kwargs=basemap_kwargs,
                suppress_warnings=True,
                legend=None,
                **kwargs)
        else:
            ax_inset, _ = M_thesis_small.plot_map(
                inset_coords[i],
                basemap=True,
                fontsize=fontsize,
                resolution='10m',
                ax=ax_inset,
                xticks=[],
                yticks=[],
                basemap_kwargs=basemap_kwargs,
                legend=None,
                **kwargs)

        ip = InsetPosition(ax, inset_locations[i])
        ax_inset.set_axes_locator(ip)

        bounds_to_polygons([inset_coords[i]]).plot_shapes(ax=ax,
                                                          facecolor=(1, 1, 1,
                                                                     0.5))

        text_location_x = (left + right) / 2
        text_location_y = (top + bottom) / 2
        t = ax.text(text_location_x,
                    text_location_y,
                    labels[i],
                    fontdict=fontdict)
        t.set_bbox(dict(facecolor='white', alpha=0.8, edgecolor='grey'))

        text_location_x = inset_locations[i][0] + inset_locations[i][2] / 2
        text_location_y = inset_locations[i][1] + inset_locations[i][3] + 0.015
        t = ax.text(text_location_x,
                    text_location_y,
                    labels[i],
                    fontdict=fontdict,
                    transform=ax.transAxes,
                    zorder=5)
        t.set_bbox(dict(facecolor='white', alpha=0.6, edgecolor='grey'))

    if legend_2d:
        data = mp.Raster(
            "data/cmap_2d_significant/classes_serial_downsampled_10.tif")[0]
        data[np.isnan(data)] = -1
        bins, vals = np.unique(data, return_counts=True)
        bins = bins[1:]
        vals = vals[1:]
        vals = vals / vals.sum() * 100

        axins = inset_axes(ax,
                           width="100%",
                           height="100%",
                           bbox_to_anchor=(-0.01, 0.25, 0.26, 0.26),
                           bbox_transform=ax.transAxes)

        cmap = cmap_2d((3, 3), alpha=0.5, diverging_alpha=0.25)
        cmap[1, 1, :] = (0.9, 0.9, 0.9)

        axins.imshow(cmap, origin='lower')
        axins.set_xticks([0, 1, 2], minor=False)
        axins.set_yticks([0, 1, 2], minor=False)
        axins.set_xticklabels([u'\u2014', "0", "+"],
                              minor=False,
                              fontdict=fontdict)
        axins.set_yticklabels([u'\u2014', "0", "+"],
                              minor=False,
                              fontsize=fontsize)
        axins.set_xlabel("Correlation WTD and fAPAR",
                         fontdict=fontdict,
                         labelpad=20)
        axins.set_ylabel("Correlation P/PET and fAPAR",
                         fontdict=fontdict,
                         labelpad=20)
        axins.tick_params(axis='both', which='both', length=0, pad=20)

        for i in range(3):
            for j in range(3):
                t = axins.text(i,
                               j,
                               f'{vals[i + 3 * j]:1.0f}',
                               fontdict=fontdict)
                t.set_bbox(dict(facecolor='white', alpha=0.6,
                                edgecolor='grey'))

    ax.tick_params(axis='both',
                   which='both',
                   length=0,
                   labelsize=fontsize,
                   pad=12)
    if isinstance(_legend, ColorbarBase):
        _legend.ax.tick_params(labelsize=fontsize, pad=12)

    mp.Raster.close(verbose=False)

    return ax, _legend
def draw_thesis_location_zoom_full(ind, ticks=1):
    """
    Drawing array of nine maps, displaying the input data and results. Not meant for custom use.

    Parameters
    ----------
    ind : .
        look at mp.RasterBase.get_pointer()
    """
    classes = mp.Raster("data/cmap_2d_significant/classes_serial.tif")
    climate = mp.Raster("data/climate/climate.tif")
    landscape = mp.Raster("data/wtd/wtd_std_5.tif")
    p_pet = mp.Raster("data/pet/p_pet.tif")
    fapar = mp.Raster("data/fapar/mean_fpar_reprojected.tif")
    corr_wtd_fapar = mp.Raster(
        "data/correlation_wtd_fapar/correlation_wtd_fapar_ge3_15.tif")
    corr_p_pet_fapar = mp.Raster(
        "data/correlation_p_pet_fapar/correlation_p_pet_fapar_ge3_15.tif")
    corr_p_pet_wtd = mp.Raster(
        "data/correlation_p_pet_wtd/correlation_p_pet_wtd_15.tif")
    wtd = mp.Raster("data/wtd/wtd.tif")

    f, ax = plt.subplots(3, 3, figsize=(20, 20))
    plt.tight_layout(h_pad=-15, w_pad=7)
    ax = [mp.basemap(ax=cax, coastlines=False) for cax in ax.flatten()]

    basemap_kwargs = {
        'coastline_linewidth': 0.3,
        'xticks': ticks,
        'yticks': ticks,
        'resolution': '10m'
    }
    fontsize = 14

    # Classes
    cmap = cmap_2d((3, 3), alpha=0.5, diverging_alpha=0.25)
    cmap[1, 1, :] = (0.9, 0.9, 0.9)
    cmap = cmap.reshape((9, 3))
    cmap = np.vstack(((1, 1, 1), cmap))
    classes.plot_classified_map(ind,
                                basemap=True,
                                basemap_kwargs=basemap_kwargs,
                                legend=None,
                                colors=cmap,
                                force_equal_figsize=True,
                                ax=ax[0],
                                fontsize=fontsize,
                                suppress_warnings=True)
    ax[0].set_title("Ecohydrological classes", fontsize=fontsize)

    # P/PET
    bins = np.arange(0, 3.1, 0.2)
    p_pet.plot_map(ind,
                   bins=bins,
                   basemap=True,
                   basemap_kwargs=basemap_kwargs,
                   cmap="Blues",
                   legend='colorbar',
                   ax=ax[1],
                   fontsize=fontsize,
                   legend_kwargs={
                       'title': "[-]",
                       'title_font': {
                           'pad': 10
                       }
                   })
    ax[1].set_title("P/PET", fontsize=fontsize)

    # WTD
    bins = [0, 1, 2, 5, 10, 15, 20, 25, 35, 50, 100, 200, 300, 1000]
    bins = bins[::-1]
    bins = [-i for i in bins]
    wtd.plot_map(ind,
                 bins=bins,
                 basemap=True,
                 basemap_kwargs=basemap_kwargs,
                 cmap="Blues",
                 legend='colorbar',
                 ax=ax[2],
                 fontsize=fontsize,
                 legend_kwargs={
                     'title': "[m]",
                     'title_font': {
                         'pad': 10
                     }
                 })
    ax[2].set_title("WTD", fontsize=fontsize)

    # FAPAR
    bins = [0, 10, 20, 30, 40, 50, 60, 70, 100]
    fapar.plot_map(ind,
                   bins=bins,
                   basemap=True,
                   basemap_kwargs=basemap_kwargs,
                   cmap="Greens",
                   legend='colorbar',
                   ax=ax[3],
                   fontsize=fontsize,
                   legend_kwargs={
                       'title': "[%]",
                       'title_font': {
                           'pad': 10
                       }
                   })
    ax[3].set_title("fAPAR", fontsize=fontsize)

    # Climate
    cmap = [(1, 1, 1)]
    bins = [0]
    labels = ["Water"]
    with open("data/climate/koppen_legend.txt") as f:
        for line in f:
            line = line.strip()
            try:
                int(line[0])
                rgb = [
                    int(c) / 255 for c in line[line.find('[') + 1:-1].split()
                ]
                cmap.append(rgb)
                labels.append(line.split()[1])
                bins.append(line[:line.find(':')])
            except:
                pass

    bins = np.array(bins, dtype=np.int)
    climate.plot_classified_map(ind,
                                bins=bins,
                                basemap=True,
                                basemap_kwargs=basemap_kwargs,
                                colors=cmap,
                                labels=labels,
                                legend='colorbar',
                                ax=ax[4],
                                fontsize=fontsize,
                                suppress_warnings=True,
                                clip_legend=True)

    ax[4].set_title("Climate", fontsize=fontsize)

    # Landscape
    dem_cmap = np.vstack((cmap_from_borders(("#0000b8", "#006310"),
                                            n=2,
                                            return_type="list"),
                          cmap_from_borders(("#006310", "#faea00"),
                                            n=7,
                                            return_type="list"),
                          cmap_from_borders(("#faea00", "#504100"),
                                            n=8,
                                            return_type="list"),
                          cmap_from_borders(("#504100", "#ffffff"),
                                            n=2,
                                            return_type="list")))
    bins = np.round(np.hstack(((0, ), np.logspace(0, np.log10(400), num=18))),
                    1)
    landscape.plot_map(ind,
                       bins=bins,
                       basemap=True,
                       basemap_kwargs=basemap_kwargs,
                       legend='colorbar',
                       cmap=ListedColormap(dem_cmap),
                       ax=ax[5],
                       fontsize=fontsize)
    ax[5].set_title("Landscape", fontsize=fontsize)

    # Correlation WTD and fAPAR
    bins = [-1, -0.5, -0.25, -0.15, -0.05, 0.05, 0.15, 0.25, 0.5, 1]
    corr_wtd_fapar.plot_map(ind,
                            bins=bins,
                            basemap=True,
                            basemap_kwargs=basemap_kwargs,
                            legend='colorbar',
                            ax=ax[6],
                            fontsize=fontsize,
                            cmap="RdYlBu_r")

    ax[6].set_title("Correlation WTD & fAPAR", fontsize=fontsize)

    # Correlation P and fAPAR
    corr_p_pet_fapar.plot_map(ind,
                              bins=bins,
                              basemap=True,
                              basemap_kwargs=basemap_kwargs,
                              legend='colorbar',
                              ax=ax[7],
                              fontsize=fontsize,
                              cmap="RdYlBu_r")

    ax[7].set_title("Correlation P/PET & fAPAR", fontsize=fontsize)

    # Correlation P/PET and fAPAR
    corr_p_pet_wtd.plot_map(ind,
                            bins=bins,
                            basemap=True,
                            basemap_kwargs=basemap_kwargs,
                            legend='colorbar',
                            ax=ax[8],
                            fontsize=fontsize,
                            cmap="RdYlBu_r")
    ax[8].set_title("Correlation P/PET & WTD", fontsize=fontsize)
    mp.Raster.close(verbose=False)
import geomappy as mp
from geomappy.focal_statistics.focal_correlation import correlate_maps as correlate_maps_njit
from geomappy.focal_statistics.c_focal_correlation import correlate_maps as correlate_maps_cython
from geomappy.focal_statistics.focal_correlation import correlate_maps_base as correlate_maps_numpy

import matplotlib.pyplot as plt
import numpy as np

map1 = mp.Raster("data/wtd.tif").values
map2 = mp.Raster("data/tree_height.asc").values
print("data loaded")

# %timeit correlate_maps_cython(map1, map2, window_size=5, fraction_accepted=0.25, verbose=True, reduce=False)
# %timeit correlate_maps_cython(map1, map2, window_size=5, fraction_accepted=0.25, verbose=True, reduce=True)

for f in [0, 0.25, 1]:
    for reduce in [False, True]:
        c1 = correlate_maps_njit(map1,
                                 map2,
                                 window_size=5,
                                 fraction_accepted=f,
                                 verbose=True,
                                 reduce=reduce)
        c2 = correlate_maps_cython(map1,
                                   map2,
                                   window_size=5,
                                   fraction_accepted=f,
                                   verbose=True,
                                   reduce=reduce)
        if not reduce:
            c3 = correlate_maps_numpy(map1,
import thesis
import geomappy as mp
import numpy as np

CLASSES = "data/cmap_2d_significant/classes_serial.tif"
M_classes = mp.Raster(CLASSES)
classes_data = M_classes[0].astype(np.int8) + 1
profile = M_classes.profile
profile['dtype'] = np.int8
profile['nodata'] = 0
profile['compress'] = 'LZW'
mp.Raster("data/export/classes.tif", mode='w',
          profile=profile)[0] = classes_data

LANDSCAPE = "data/landscape_classes/landscape_classes.tif"
M_landscape = mp.Raster(LANDSCAPE)
landscape_data = M_landscape[0].astype(np.int8)
profile = M_landscape.profile
profile['dtype'] = np.int8
profile['nodata'] = 0
profile['compress'] = 'LZW'
mp.Raster("data/export/landscape.tif", mode='w',
          profile=profile)[0] = landscape_data

mp.Raster.close()
import numpy as np
import pandas as pd

CORR_WTD_FAPAR = "data/correlation_wtd_fapar/correlation_wtd_fapar_ge3_15_downsampled_10.tif"
CORR_P_PET_FAPAR = "data/correlation_p_pet_fapar/correlation_p_pet_fapar_ge3_15_downsampled_10.tif"
CORR_P_PET_WTD = "data/correlation_p_pet_wtd/correlation_p_pet_wtd_15_downsampled_10.tif"
LANDSCAPE = "data/landscape_classes/landscape_downsampled_10.tif"
CLIMATE = "data/climate/climate_downsampled_10.tif"
CLASSES = "data/cmap_2d_significant/classes_serial_downsampled_10.tif"
P_PET = "data/pet/p_pet_downsampled_10.tif"
WTD = "data/wtd/wtd_downsampled_10.tif"
WTD_STD = "data/wtd/wtd_std_5_downsampled_10.tif"
TH = "data/tree_height/tree_height_global_downsampled_10.tif"
FAPAR = "data/fapar/mean_fpar_downsampled_10.tif"

M_corr_wtd_fapar = mp.Raster(CORR_WTD_FAPAR)
M_corr_p_pet_fapar = mp.Raster(CORR_P_PET_FAPAR)
M_corr_p_pet_wtd = mp.Raster(CORR_P_PET_WTD)
M_climate = mp.Raster(CLIMATE)
M_p_pet = mp.Raster(P_PET)
M_wtd = mp.Raster(WTD)
M_wtd_std = mp.Raster(WTD_STD)
M_landscape = mp.Raster(LANDSCAPE)
M_classes = mp.Raster(CLASSES, fill_value=-1)
M_fapar = mp.Raster(FAPAR)
M_th = mp.Raster(TH)

mp.Raster.set_tiles((10, 20))

df = None
for i in M_wtd:
Exemple #16
0
    return bs, pe


def calc_df_offset(value, n=225):
    df_offset = 0
    while True:
        df = n - 2 - df_offset
        t_value = t.ppf(0.95, df=df)
        threshold = t_value / np.sqrt(df + t_value ** 2)
        if threshold > value:
            return df_offset
        df_offset = df_offset + 1


M_th = mp.Raster("data/tree_height/tree_height_global.tif")
M_fapar = mp.Raster("data/fapar/mean_fpar_reprojected.tif")
M_p_pet = mp.Raster("data/pet/p_pet.tif")
M_wtd = mp.Raster("data/wtd/wtd.tif")

mp.Raster.set_window_size(15)
mp.Raster.set_tiles((20, 20))

d_p_pet = {}
d_wtd = {}
for i in M_th:
    progress_bar((i+1) / M_th.c_tiles)
    th = M_th[i]
    fapar = M_fapar[i]
    fapar[th < 3] = np.nan
    p_pet = M_p_pet[i]