-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
202 lines (167 loc) · 6.53 KB
/
functions.py
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
# -*- coding: utf-8 -*-
"""
Authors: Elga Salvadore
IHE Delft 2019
Contact: [email protected]
Repository:
Module: wa_wb
Simplified version of WaterPix
"""
from __future__ import division
import numpy as np
from scipy.optimize import fsolve
import netCDF4
import get_dictionaries as gd
import becgis
import os
from wa_wb import davgis
def lai_and_soil_calculations(thetasat, lai, swi, swio, swix, rootdepth):
'''
Calculate thetasat, lai, and soil moisture parameters
'''
# Soil moisture calculations
theta0 = thetasat * swi/100.0
thetao= thetasat * swio/100.0
thetax = thetasat * swix/100.0
# Argument for exponential function
exp_arg = exp_arg_func(theta0,thetasat,lai)
exp_argo = exp_arg_func(thetao,thetasat,lai)
exp_argx = exp_arg_func(thetax,thetasat,lai)
# Soil moisture values - root zone
thetarz = (0.1*lai +(1-0.1*lai) * (exp_arg))*thetasat
thetarzo = (0.1*lai +(1-0.1*lai) * (exp_argo))*thetasat
thetarzx = (0.1*lai +(1-0.1*lai) * (exp_argx))*thetasat
# Change in storage
dsm = rootdepth*(thetarzx - thetarzo)
return thetarz, thetarzo, thetarzx, dsm
def exp_arg_func(theta0, thetasat, lai):
'''
Compute the exponential argument in the calculation of the root depth
soil moisture.
'''
value = 1 - np.exp((theta0/thetasat) * (-0.5*lai - 1))
return value
def SCS_surface_runoff(p, supply, interception, rootdepth, thetasat, thetarz):
P_I_supply = p-interception+supply
Qsw = P_I_supply**2 / (P_I_supply + rootdepth * (thetasat - thetarz))
return Qsw
def SCS_surface_runoff_gr(p, interception, rootdepth, thetasat, thetarz):
P_I = p-interception
Qsw_gr = P_I**2 / (P_I + rootdepth * (thetasat - thetarz))
return Qsw_gr
def baseflow_calculation(Qsw, filter_par, qratio_y):
'''
Calculate the baseflow using the runoff ratio and the surface runoff
'''
Qgw_tot = np.nansum(Qsw/qratio_y-Qsw)
q0 = fsolve(baseflow_function, 0.0, [Qsw, filter_par,
qratio_y, Qgw_tot, True])
Qgw = baseflow_function(q0, [Qsw, filter_par,
qratio_y, Qgw_tot, False])
# fixing negative values when Qsw is 0
Qgw[Qgw<0]=0
return Qgw
def baseflow_function(q0, args):
'''
Balance baseflow values in a yearly basis
'''
Qsw, filter_par, qratio, Qgw_tot, return_error = args
q_temp = np.zeros(np.shape(Qsw)) * np.nan
q_temp[0] = filter_par*q0 + 0.5*(1 + filter_par)*(
Qsw[0] - Qsw[-1])
for i in range(1, 12):
q_temp[i] = filter_par*q_temp[i-1] + 0.5*(1 + filter_par)*(
Qsw[i] - Qsw[i-1])
# Baseflow
Qgw = (1-qratio)/qratio*(Qsw - q_temp)
# Error calculation
error = Qgw_tot - np.nansum(Qgw)
# Return error or vector
if return_error:
return error
else:
return Qgw
def total_supply(etb, lu):
"""
Apply a consumed fraction to groups of landuse classes (categories) to aqcuire
maps of blue water supplies.
"""
consumed_fractions, sw_supply_fractions, sw_return_fractions=gd.get_sheet4_6_fractions(version = '1.0')
lu_categories = gd.get_sheet4_6_classes(version = '1.0')
supply = np.copy(etb)
for key in consumed_fractions.keys():
classes = lu_categories[key]
mask = np.logical_or.reduce([lu == value for value in classes])
consumed_fraction = consumed_fractions[key]
for i in range(np.shape(supply)[0]):
supply[i,mask] /= consumed_fraction
return supply
def output_nc_to_tiffs(output_nc, output_path):
"""
Create raster files from the variables in the output netcdf file
"""
# Output folders
if not os.path.isdir(output_path):
os.mkdir(output_path)
path_y = os.path.join(output_path, 'yearly')
path_m = os.path.join(output_path, 'monthly')
path_a = os.path.join(output_path, 'additional')
if not os.path.isdir(path_y):
os.mkdir(path_y)
if not os.path.isdir(path_m):
os.mkdir(path_m)
if not os.path.isdir(path_a):
os.mkdir(path_a)
# Read netcdf file
nc_file = netCDF4.Dataset(output_nc, 'r')
variables_ls = nc_file.variables.keys()
time_y = nc_file.variables['time_yyyy'][:]
time_m = nc_file.variables['time_yyyymm'][:]
nc_file.close()
# Remove variables
for variable in ['latitude', 'longitude', 'time_yyyy', 'time_yyyymm', 'crs']:
variables_ls.remove(variable)
# Add sub-folders
for variable in variables_ls:
if '_Y' in variable:
if not os.path.exists(os.path.join(path_y, variable)):
os.mkdir(os.path.join(path_y, variable))
elif '_M' in variable:
if not os.path.exists(os.path.join(path_m, variable)):
os.mkdir(os.path.join(path_m, variable))
else:
if not os.path.exists(os.path.join(path_a, variable)):
os.mkdir(os.path.join(path_a, variable))
# Main Loop
for variable in variables_ls:
# Yearly rasters
if '_Y' in variable:
for time in time_y:
print '{0}\t{1}'.format(variable, time)
file_name = variable[:-1] + '{0}.tif'.format(time)
output_tiff = os.path.join(path_y, variable, file_name)
davgis.NetCDF_to_Raster(output_nc, output_tiff, variable,
x_variable='longitude',
y_variable='latitude',
time={'variable': 'time_yyyy',
'value': time})
# Monthly rasters
elif '_M' in variable:
for time in time_m:
print '{0}\t{1}'.format(variable, time)
file_name = variable[:-1] + '{0}.tif'.format(time)
output_tiff = os.path.join(path_m, variable, file_name)
print output_tiff
davgis.NetCDF_to_Raster(output_nc, output_tiff, variable,
x_variable='longitude',
y_variable='latitude',
time={'variable': 'time_yyyymm',
'value': time})
# Additional rasters
else:
print '{0}'.format(variable)
file_name = variable[:-1] + '.tif'
output_tiff = os.path.join(path_a, variable, file_name)
davgis.NetCDF_to_Raster(output_nc, output_tiff, variable,
x_variable='longitude',
y_variable='latitude')