forked from wateraccounting/WA_Hyperloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindicators.py
More file actions
280 lines (221 loc) · 12.4 KB
/
indicators.py
File metadata and controls
280 lines (221 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 19 09:54:17 2017
@author: cmi001
"""
import os
import csv
import glob
import numpy as np
import pandas as pd
import datetime
class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)()
return value
def sheet1_indicators(dir1):
exploitable_water_fractions = np.array([])
storage_change_fractions = np.array([])
available_water_fractions = np.array([])
basin_closure_fractions = np.array([])
rainfall_dependency_fractions = np.array([])
utilizable_outflow_fractions = np.array([])
reserved_outflows_fractions = np.array([])
dates = np.array([])
file_list = glob.glob(dir1+'\sheet*.csv')
for f in file_list:
explod = ''.join(os.path.split(f)[1].split('.')[:-1]).split('_')
try:
date = datetime.date(int(explod[-2]), int(explod[-1]), 1)
except:
date = datetime.date(int(explod[-1]), 1, 1)
with open(f) as f1:
data = csv.reader(f1)
DATA = Vividict()
next(data)
for row in data:
splitr= row[0].split(';')
DATA[splitr[0]][splitr[1]][splitr[2]]=float(splitr[3])
gross_inflow = 0
for k1 in DATA['INFLOW'].keys():
for k2 in DATA['INFLOW'][k1]:
gross_inflow += DATA['INFLOW'][k1][k2]
total_storage_change = 0
for k1 in DATA['STORAGE'].keys():
for k2 in DATA['STORAGE'][k1]:
total_storage_change += DATA['STORAGE'][k1][k2]
net_inflow = gross_inflow + total_storage_change
landscape_et = sum([DATA['OUTFLOW']['ET LANDSCAPE'][k1] for k1 in DATA['OUTFLOW']['ET LANDSCAPE'].keys()])
exploitable_water = net_inflow - landscape_et
DeltaS_fresh_water = total_storage_change
utilised_flow = sum([DATA['OUTFLOW']['ET UTILIZED FLOW'][k1] for k1 in DATA['OUTFLOW']['ET UTILIZED FLOW'].keys()])
reserved_outflows = np.max([DATA['OUTFLOW']['RESERVED'][k1] for k1 in DATA['OUTFLOW']['RESERVED'].keys()])
non_utlisiable_outflow = DATA['OUTFLOW']['OTHER']['Non-utilizable']
available_water = exploitable_water - reserved_outflows - non_utlisiable_outflow
QSWout = sum([DATA['OUTFLOW']['SURFACE WATER'][k1] for k1 in DATA['OUTFLOW']['SURFACE WATER'].keys()])
QGWout = sum([DATA['OUTFLOW']['GROUNDWATER'][k1] for k1 in DATA['OUTFLOW']['GROUNDWATER'].keys()])
# Calculate Indicators for sheet1 from Karimi et al
exploitable_water_fraction = exploitable_water/net_inflow
storage_change_fraction = DeltaS_fresh_water/exploitable_water
available_water_fraction = available_water/exploitable_water
basin_closure_fraction = utilised_flow/available_water
# Added indicators from Wim
total_precip = sum([DATA['INFLOW']['PRECIPITATION'][k1] for k1 in DATA['INFLOW']['PRECIPITATION'].keys()])
rainfall_dependency_fraction = total_precip / net_inflow
utilizable_outflow_fraction = 1-basin_closure_fraction
if (QSWout + QGWout)!=0:
reserved_outflows_fraction = reserved_outflows/(QSWout + QGWout)
else:
reserved_outflows_fraction = np.nan
exploitable_water_fractions = np.append(exploitable_water_fractions, exploitable_water_fraction)
storage_change_fractions = np.append(storage_change_fractions, storage_change_fraction)
available_water_fractions = np.append(available_water_fractions, available_water_fraction)
basin_closure_fractions = np.append(basin_closure_fractions, basin_closure_fraction)
rainfall_dependency_fractions = np.append(rainfall_dependency_fractions, rainfall_dependency_fraction)
utilizable_outflow_fractions = np.append(utilizable_outflow_fractions, utilizable_outflow_fraction)
reserved_outflows_fractions = np.append(reserved_outflows_fractions, reserved_outflows_fraction)
dates = np.append(dates, date)
sheet1_indicators = {
'expl._wat.': exploitable_water_fractions,
'strg_chng.': storage_change_fractions,
'avlb._wat.': available_water_fractions,
'bsn._clsr.': basin_closure_fractions,
#'rainfall_dependency': rainfall_dependency_fractions,
#'utilizable_outflow': utilizable_outflow_fractions,
'rsrvd._of.': reserved_outflows_fractions,
'dates': dates,
}
return sheet1_indicators
def sheet2_indicators(dir1):
file_list = glob.glob(os.path.join(dir1, "*.csv"))
transpiration_fractions = np.array([])
beneficial_fractions = np.array([])
managed_fractions = np.array([])
agricultural_ET_fractions = np.array([])
irrigated_agricultural_ET_fractions = np.array([])
dates = np.array([])
for f in file_list:
explod = ''.join(os.path.split(f)[1].split('.')[:-1]).split('_')
try:
date = datetime.date(int(explod[-2]), int(explod[-1]), 1)
except:
date = datetime.date(int(explod[-1]), 1, 1)
with open(f) as f1:
data = csv.reader(f1)
DATA = Vividict()
next(data)
mline = []
for row in data:
splitr= row[0].split(';')
DATA[splitr[0]][splitr[1]]=np.array(splitr[2:]).astype('float')
line = np.array(splitr[2:]).astype('float')
mline = np.append(mline, line)
m = mline.reshape(len(mline)/len(line),len(line))
mt = np.transpose(m)
T = sum(mt[0])
ET = sum(mt[0])+sum(mt[1])+sum(mt[2])+sum(mt[3])
ET_benef = ET - sum(mt[9])
ET_managed_nc = sum(mt[0][13:19])+sum(mt[1][13:19])+sum(mt[2][13:19])+sum(mt[3][13:19])
ET_managed_c = sum(mt[0][23:28])+sum(mt[1][23:28])+sum(mt[2][23:28])+sum(mt[3][23:28])
agricultural_ET = sum(m[22][0:4])+sum(m[25][0:4])
irrigated_agricultural_ET = sum(m[25][0:4])
transpiration_fractions = np.append(transpiration_fractions, T/ET)
beneficial_fractions = np.append(beneficial_fractions, (ET_benef)/ET)
managed_fractions = np.append(managed_fractions, (ET_managed_nc + ET_managed_c)/ET)
agricultural_ET_fractions = np.append(agricultural_ET_fractions, agricultural_ET/ET)
irrigated_agricultural_ET_fractions = np.append(irrigated_agricultural_ET_fractions, irrigated_agricultural_ET/agricultural_ET)
dates = np.append(dates, date)
sheet2_indicators = {
't_fraction': transpiration_fractions,
'benefi_ET': beneficial_fractions,
'mngd_ET': managed_fractions,
'agr_ET': agricultural_ET_fractions,
'irr_agr_ET': irrigated_agricultural_ET_fractions,
'dates': dates,
}
return sheet2_indicators
def sheet3_indicators(dir1):
a_files = glob.glob(dir1+'\sheet3a*.csv')
b_files = glob.glob(dir1+'\sheet3b*.csv')
files = zip(a_files,b_files)
land_productivity_cropss = np.array([])
water_productivity_r_cropss = np.array([])
water_productivity_i_cropss = np.array([])
food_irrigation_dependencys = np.array([])
years = np.array([])
for (fa, fb) in files:
years = np.append(years, int(fa[-8:-4]))
dfa= pd.read_csv(fa,delimiter=';')
dfa2 = dfa.loc[~np.isnan(dfa.WATER_CONSUMPTION)]
dfb = pd.read_csv(fb,delimiter=";")
dfb2 = dfb.loc[~np.isnan(dfb.LAND_PRODUCTIVITY)]
crop_TYPE = ['Cereals','Beverage crops','Feed crops','Fruit & vegetables','Non-cereals','Oilseeds','Other crops']
production_1 = []
area_1 =[]
wat_cons = {'RAINFED':[] ,
'IRRIGATED':[] }
wat_prod = {'RAINFED':[] ,
'IRRIGATED':[] }
production_totals = {'RAINFED':[] ,
'IRRIGATED':[] }
for crop in crop_TYPE:
dfa_crop = dfa2.loc[dfa2.TYPE == crop]
df_crop = dfb2.loc[dfb2.TYPE == crop]
for cl in ['IRRIGATED','RAINFED']:
dfa_cropclass = dfa_crop.loc[(dfa_crop.CLASS == cl)]
if cl == 'IRRIGATED':
df_cropclass = df_crop.loc[(df_crop.CLASS == cl) & (df_crop.SUBCLASS == 'Total yield')]
else:
df_cropclass = df_crop.loc[(df_crop.CLASS == cl) & (df_crop.SUBCLASS == 'Yield')]
if not df_cropclass.empty:
lp = np.array(df_cropclass.LAND_PRODUCTIVITY)
area = np.array(df_cropclass.Crop_Area)
ratio = np.array(df_cropclass.Area_ratio_for_DBLCROP)
subtype = df_cropclass.SUBTYPE.tolist()
for ti in range(len(subtype)):
t = subtype[ti]
wat_cons[cl].append(np.sum((dfa_cropclass.loc[dfa_cropclass.SUBTYPE == t]).WATER_CONSUMPTION))
wat_prod[cl].append(float((df_cropclass.loc[df_cropclass.SUBTYPE == t]).Crop_Area) * float((df_cropclass.loc[df_cropclass.SUBTYPE == t]).LAND_PRODUCTIVITY))
production_1.append(np.sum(lp*area))
area_1.append(area[0]/ratio[0])
production_totals[cl].append(np.sum(lp*area))
land_productivity_crops = np.sum(production_1)/np.sum(area_1)
water_productivity_r_crops = np.sum(wat_prod['RAINFED'])/np.sum(wat_cons['RAINFED'])/10000000
water_productivity_i_crops = np.sum(wat_prod['IRRIGATED'])/np.sum(wat_cons['IRRIGATED'])/10000000
food_irrigation_dependency = np.sum(production_totals['IRRIGATED'])/(np.sum(production_totals['IRRIGATED'])+np.sum(production_totals['RAINFED']))*100
land_productivity_cropss = np.append(land_productivity_cropss, land_productivity_crops)
water_productivity_r_cropss = np.append(water_productivity_r_cropss, water_productivity_r_crops)
water_productivity_i_cropss = np.append(water_productivity_i_cropss, water_productivity_i_crops)
food_irrigation_dependencys = np.append(food_irrigation_dependencys, food_irrigation_dependency)
return land_productivity_cropss, water_productivity_r_cropss, water_productivity_i_cropss, food_irrigation_dependencys, years
def sheet4_indicators(dir1):
files = glob.glob(dir1+'\sheet*.csv')
groundwater_withdrawl_fractions = np.array([])
irrigation_efficiencys = np.array([])
recoverable_fractions = np.array([])
dates = np.array([])
for f in files:
year = int(f[-11:-7])
month = int(f[-6:-4])
dfa = pd.read_csv(f,delimiter=';')
recoverable = (np.nansum(dfa.RECOVERABLE_SURFACEWATER)+np.nansum(dfa.RECOVERABLE_GROUNDWATER))
tot_withdrawl = (np.nansum(dfa.SUPPLY_GROUNDWATER)+np.nansum(dfa.SUPPLY_SURFACEWATER))
df_irrcrop = dfa.loc[(dfa.LANDUSE_TYPE == "Irrigated crops")]
et_consumption = float(df_irrcrop.CONSUMED_ET)
groundwater_withdrawl_fraction = np.nansum(dfa.SUPPLY_GROUNDWATER)/tot_withdrawl
if et_consumption != 0.0:
irrigation_efficiency = float(df_irrcrop.CONSUMED_ET) / (float(df_irrcrop.SUPPLY_GROUNDWATER)+float(df_irrcrop.SUPPLY_SURFACEWATER))
else:
irrigation_efficiency = np.nan
recoverable_fraction = recoverable / tot_withdrawl
groundwater_withdrawl_fractions = np.append(groundwater_withdrawl_fractions, groundwater_withdrawl_fraction)
irrigation_efficiencys = np.append(irrigation_efficiencys, irrigation_efficiency)
recoverable_fractions = np.append(recoverable_fractions, recoverable_fraction)
dates = np.append(dates, datetime.date(year, month, 1))
sheet4_indicators = {
'gw_wthdrwl': groundwater_withdrawl_fractions,
'irr._fcncy': irrigation_efficiencys,
'recovarble': recoverable_fractions,
'dates': dates,
}
return sheet4_indicators