Skip to content

Commit

Permalink
Merge pull request #11 from JannisHoch/adding_more_vars
Browse files Browse the repository at this point in the history
Adding more vars for ML
  • Loading branch information
JannisHoch authored Jun 17, 2020
2 parents 039a1ec + 84626f6 commit 3dd30b3
Show file tree
Hide file tree
Showing 6 changed files with 1,627 additions and 281 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"python.pythonPath": "C:\\Users\\hoch0001\\AppData\\Local\\Continuum\\anaconda3\\envs\\conflict_model\\python.exe",
"python.pythonPath": "C:\\Users\\hoch0001\\AppData\\Local\\Continuum\\anaconda3\\python.exe",
"restructuredtext.confPath": "${workspaceFolder}\\docs"
}
2 changes: 1 addition & 1 deletion conflict_model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def conflict_in_year_bool(conflict_gdf, extent_gdf, config, sim_year, out_dir, s
dataframe: dataframe containing column with boolean information about conflict for each year
"""

print('determining whether a conflict took place or not...')
print('determining whether a conflict took place or not')

out_df = extent_gdf.copy()

Expand Down
112 changes: 46 additions & 66 deletions conflict_model/env_vars_nc.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import xarray as xr
import rasterio as rio
import pandas as pd
import geopandas as gpd
import rasterstats as rstats
import numpy as np
import matplotlib.pyplot as plt
import os, sys

def rasterstats_GDP_PPP(gdf_in, config, sim_year, out_dir, saving_plots=False, showing_plots=False):
def rasterstats_GDP_PPP(gdf, config, sim_year, out_dir, saving_plots=False, showing_plots=False):

print('calculating zonal statistics per aggregation unit')
print('calculating GDP PPP mean per aggregation unit')

nc_fo = os.path.join(config.get('general', 'input_dir'),
config.get('env_vars', 'GDP_PPP'))
Expand All @@ -17,6 +18,46 @@ def rasterstats_GDP_PPP(gdf_in, config, sim_year, out_dir, saving_plots=False, s

nc_var = nc_ds['GDP_per_capita_PPP']

# years = pd.to_datetime(nc_ds.time.values).to_period(freq='Y').strftime('%Y').to_numpy(dtype=int)
# if sim_year not in years:
# raise ValueError('the simulation year {0} can not be found in file {1}'.format(sim_year, nc_fo))
# sim_year_idx = int(np.where(years == sim_year)[0])

affine = rio.open(nc_fo).transform

# gdf['zonal_stats_min_' + str(sim_year)] = np.nan
# gdf['zonal_stats_max_' + str(sim_year)] = np.nan
# gdf['GDP_PPP_mean_' + str(sim_year)] = np.nan

nc_arr = nc_var.sel(time=sim_year)
nc_arr_vals = nc_arr.values
if nc_arr_vals.size == 0:
raise ValueError('the data was found for this year in the nc-file {}, check if all is correct'.format(nc_fo))

list_GDP_PPP = []

for i in range(len(gdf)):
prov = gdf.iloc[i]
zonal_stats = rstats.zonal_stats(prov.geometry, nc_arr_vals, affine=affine, stats="mean")
# gdf.loc[i, 'zonal_stats_min_' + str(sim_year)] = zonal_stats[0]['min']
# gdf.loc[i, 'zonal_stats_max_' + str(sim_year)] = zonal_stats[0]['max']
list_GDP_PPP.append(zonal_stats[0]['mean'])

print('...DONE' + os.linesep)

return list_GDP_PPP

def rasterstats_totalEvap(gdf_in, config, sim_year, out_dir):

print('calculating evaporation mean per aggregation unit')

nc_fo = os.path.join(config.get('general', 'input_dir'),
config.get('env_vars', 'evaporation'))

nc_ds = xr.open_dataset(nc_fo)

nc_var = nc_ds['total_evaporation']

years = nc_ds['time'].values
years = years[years>=config.getint('settings', 'y_start')]
years = years[years<=config.getint('settings', 'y_end')]
Expand All @@ -25,9 +66,7 @@ def rasterstats_GDP_PPP(gdf_in, config, sim_year, out_dir, saving_plots=False, s

gdf = gdf_in.copy()

gdf['zonal_stats_min_' + str(sim_year)] = np.nan
gdf['zonal_stats_max_' + str(sim_year)] = np.nan
gdf['zonal_stats_mean_' + str(sim_year)] = np.nan
gdf['evap_mean_' + str(sim_year)] = np.nan

nc_arr = nc_var.sel(time=sim_year)
nc_arr_vals = nc_arr.values
Expand All @@ -36,68 +75,9 @@ def rasterstats_GDP_PPP(gdf_in, config, sim_year, out_dir, saving_plots=False, s

for i in range(len(gdf)):
prov = gdf.iloc[i]
zonal_stats = rstats.zonal_stats(prov.geometry, nc_arr_vals, affine=affine, stats="mean min max")
gdf.loc[i, 'zonal_stats_min_' + str(sim_year)] = zonal_stats[0]['min']
gdf.loc[i, 'zonal_stats_max_' + str(sim_year)] = zonal_stats[0]['max']
gdf.loc[i, 'zonal_stats_mean_' + str(sim_year)] = zonal_stats[0]['mean']
zonal_stats = rstats.zonal_stats(prov.geometry, nc_arr_vals, affine=affine, stats="mean")
gdf.loc[i, 'evap_mean_' + str(sim_year)] = zonal_stats[0]['mean']

print('...DONE' + os.linesep)

fig, axes = plt.subplots(1, 3 , figsize=(20, 10))

fig.suptitle(str(int(sim_year)), y=0.78)

gdf.plot(ax=axes[0],
column='zonal_stats_min_' + str(sim_year),
vmin=2000,
vmax=15000,
legend=True,
legend_kwds={'label': "min GDP_PPP",
'orientation': "vertical",
'shrink': 0.5,
'extend': 'both'})
gdf.boundary.plot(ax=axes[0],
color='0.5',
linestyle=':',
label='water province borders')

gdf.plot(ax=axes[1],
column='zonal_stats_max_' + str(sim_year),
vmin=2000,
vmax=15000,
legend=True,
legend_kwds={'label': "max GDP_PPP",
'orientation': "vertical",
'shrink': 0.5,
'extend': 'both'})
gdf.boundary.plot(ax=axes[1],
color='0.5',
linestyle=':',
label='water province borders')

gdf.plot(ax=axes[2],
column='zonal_stats_mean_' + str(sim_year),
vmin=2000,
vmax=15000,
legend=True,
legend_kwds={'label': "mean GDP_PPP",
'orientation': "vertical",
'shrink': 0.5,
'extend': 'both'})
gdf.boundary.plot(ax=axes[2],
color='0.5',
linestyle=':',
label='water province borders')

plt.tight_layout()

plt_name = 'GDP_PPP_zonal_stats_' + str(int(sim_year)) + '.png'
plt_name = os.path.join(out_dir, plt_name)

if saving_plots:
plt.savefig(plt_name, dpi=300)

if showing_plots == False:
plt.close()

return gdf
3 changes: 2 additions & 1 deletion data/run_setting.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ zones=BWh,BSh
code2class=KoeppenGeiger/classification_codes.txt

[env_vars]
GDP_PPP=GDP_HDI/GDP_per_capita_PPP_1990_2015_Africa.nc
GDP_PPP=GDP_HDI/GDP_per_capita_PPP_1990_2015_Africa.nc
evaporation=PCRGLOBWB/totalEvap/totalEvaporation_monthTot_output_2000_2015_Africa_yearmean.nc
Loading

0 comments on commit 3dd30b3

Please sign in to comment.