-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scenarios_design_final.py
247 lines (199 loc) · 11 KB
/
Scenarios_design_final.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
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
import pandas as pd
import csv
import numpy as np
import scipy.io as scio
def read_csv_data_SSP(filename, encode='ANSI'): # get the socioeconomic data and ISIMIP data from csv
pd_reader = pd.read_csv(filename, encoding=encode, header=None)
data = np.array(pd_reader)
return data[:, 1:]
#This is the socio data after PCA and Norm
socio_data = np.loadtxt(open("C:\Research2\Socioeconomic\Socio_data.csv", "rb"), delimiter=",", skiprows=0)
Map_code_proj = {}
with open('C:\Research2\surf_data\country_index.csv',
encoding='ANSI') as inp: # read csv as a dictionary
reader = csv.reader(inp)
Map_code_proj = {rows[0]: rows[1] for rows in reader}
Socio_code_file = {}
with open('C:\Research2\Socioeconomic\Socioeconomic_codes.csv',
encoding='ANSI') as inp: # read csv as a dictionary
reader = csv.reader(inp)
Socio_code_file = {rows[0]: rows[1] for rows in reader}
# AREA data from the CLM surface dataset
hist_AREA_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\AREA.mat')
hist_AREA = hist_AREA_dict['AREA'] # grid area data
hist_PCT_CROP_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\PCT_CROP.mat')
hist_PCT_CROP = hist_PCT_CROP_dict['PCT_CROP'] # percentage of crop land
# irrigation area and frac
basic_drip_area_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\act_drip_area.mat')
basic_drip_area = basic_drip_area_dict['act_drip_area']
basic_drip_frac_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\act_drip_frac.mat')
basic_drip_frac = basic_drip_frac_dict['act_drip_frac']
basic_spri_area_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\act_spri_area.mat')
basic_spri_area = basic_spri_area_dict['act_spri_area']
basic_spri_frac_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\act_spri_frac.mat')
basic_spri_frac = basic_spri_frac_dict['act_spri_frac']
basic_floo_area_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\act_floo_area.mat')
basic_floo_area = basic_floo_area_dict['act_floo_area']
basic_floo_frac_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\act_floo_frac.mat')
basic_floo_frac = basic_floo_frac_dict['act_floo_frac']
all_CROP_AREA_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\CROP_AREA.mat')
all_CROP_AREA = all_CROP_AREA_dict['CROP_AREA']
opt_drip_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\opt_drip.mat')
opt_drip = opt_drip_dict['opt_drip']
opt_floo_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\opt_floo.mat')
opt_floo = opt_floo_dict['opt_floo']
opt_spri_dict = scio.loadmat('C:\Research2\PCT_CFT_AREA\SSP126\\opt_spri.mat')
opt_spri = opt_spri_dict['opt_spri']
# basic fraction data
# basic_drip_dict = scio.loadmat('C:\Research2\\basic_fraction\drip.mat')
# basic_sprinkler_dict = scio.loadmat('C:\Research2\\basic_fraction\sprinkler.mat')
# basic_flood_dict = scio.loadmat('C:\Research2\\basic_fraction\\flood.mat')
# basic_drip = basic_drip_dict['drip_fraction']
# basic_sprinkler = basic_sprinkler_dict['sprinkler_fraction']
# basic_flood = basic_flood_dict['flood_fraction']
# strings for P data
str_P_begin = 'C:\Research2\ISIMIP3a\\P_data\ssp126\\'
str_year_list = ['1996_2015', '2001_2020', '2006_2025', '2011_2030', '2016_2035', '2021_2040', '2026_2045', '2031_2050',
'2036_2055', '2041_2060', '2046_2065', '2051_2070', '2056_2075', '2061_2080', '2066_2085', '2071_2090',
'2076_2095', '2081_2100']
str_P_end = '_after_normalization.csv'
#OUTPUT data
grid_frac = np.zeros([288, 192, 18, 3])
grid_area = np.zeros([288, 192, 18, 3])
country_frac = np.zeros([257, 18, 3])
country_area = np.zeros([257, 18, 3])
#for i_country in range(1, 257):
for i_country in range(11, 12):
try:
# get the pixels of the country
str_file = 'C:\Research2\surf_data\countries\\' + str(i_country) + '.txt'
row_col = pd.read_csv(str_file, header=None, delimiter=',')
all_str = row_col[0]
print(i_country)
try:
code = Map_code_proj[str(i_country)]
print(code)
socio_code = int(Socio_code_file[code])
all_area = np.zeros([18])
drip_area = np.zeros([18])
spri_area = np.zeros([18])
floo_area = np.zeros([18])
for line in range(len(all_str)):
str_split = all_str[line].split()
x = int(str_split[0]) - 1
y = int(str_split[1]) - 1
print('x = ' + str(x))
print('y = ' + str(y))
flood = basic_floo_frac[x, y]
sprinkler = basic_spri_frac[x, y]
drip = basic_drip_frac[x, y]
print('Basic flood frac = ' + str(flood))
print('Basic sprinkler frac = ' + str(sprinkler))
print('Basic drip frac = ' + str(drip))
if np.isnan(flood): # needs to be checked
try:
if not (np.isnan(basic_floo_frac[x-1, y])):
flood = basic_floo_frac[x-1, y]
sprinkler = basic_spri_frac[x-1, y]
drip = basic_drip_frac[x-1, y]
elif not (np.isnan(basic_floo_frac[x+1, y])):
flood = basic_floo_frac[x + 1, y]
sprinkler = basic_spri_frac[x + 1, y]
drip = basic_drip_frac[x + 1, y]
elif not (np.isnan(basic_floo_frac[x, y-1])):
flood = basic_floo_frac[x, y-1]
sprinkler = basic_spri_frac[x, y-1]
drip = basic_drip_frac[x, y-1]
elif not (np.isnan(basic_floo_frac[x, y+1])):
flood = basic_floo_frac[x, y+1]
sprinkler = basic_spri_frac[x, y+1]
drip = basic_drip_frac[x, y+1]
except (IndexError):
flood = 1
sprinkler = 0
drip = 0
for time in range(18):
real_year = (time) * 5 + 14
if all_CROP_AREA[x, y, real_year] == 0:
continue
else:
str_year = str_year_list[time-1]
str_P_file = str_P_begin + str_year + str_P_end
optimal_flood = opt_floo[x, y, real_year]
optimal_sprinkler = opt_spri[x, y, real_year]
optimal_drip = opt_drip[x, y, real_year]
print('Optimal flood frac = ' + str(optimal_flood))
print('Optimal sprinkler frac = ' + str(optimal_sprinkler))
print('Optimal drip frac = ' + str(optimal_drip))
pd_reader = pd.read_csv(str_P_file, header=None)
var_P = np.array(pd_reader)
P = var_P[x, y]
hydro_climate = 1 - P
socio_eco = socio_data[socio_code, time + 2] # socio_data needs to be created and loaded
speed = 1
if socio_eco < 0.25:
speed = speed - 0.4
elif socio_eco < 0.5:
speed = speed - 0.2
elif socio_eco < 0.75:
speed = speed
elif socio_eco < 1:
speed = speed + 0.2
else:
speed = speed + 0.4
if hydro_climate < 0.25:
speed = speed - 0.4
elif hydro_climate < 0.5:
speed = speed - 0.2
elif hydro_climate < 0.75:
speed = speed
elif hydro_climate < 1:
speed = speed + 0.2
else:
speed = speed + 0.4
speed = speed / 100
flood = flood - 5 * speed
# if (sprinkler + drip) != 0:
# sprinkler = sprinkler + 5 * speed * (sprinkler) / (sprinkler + drip)
# drip = drip + 5 * speed * (drip) / (sprinkler + drip)
# else:
sprinkler = sprinkler + 5 * speed * (optimal_sprinkler) / (optimal_sprinkler + optimal_drip)
drip = drip + 5 * speed * (optimal_drip) / (optimal_sprinkler + optimal_drip)
print('Updated flood frac = ' + str(flood))
print('Updated sprinkler frac = ' + str(sprinkler))
print('Updated drip frac = ' + str(drip))
if drip > optimal_drip:
drip = optimal_drip
if sprinkler > optimal_sprinkler:
sprinkler = optimal_sprinkler
if flood < optimal_flood:
flood = optimal_flood
print('Adjusted flood frac = ' + str(flood))
print('Adjusted sprinkler frac = ' + str(sprinkler))
print('Adjusted drip frac = ' + str(drip))
grid_frac[x, y, time, 0] = flood
grid_frac[x, y, time, 1] = sprinkler
grid_frac[x, y, time, 2] = drip
grid_area[x, y, time, 0] = all_CROP_AREA[x,y,real_year] * flood
grid_area[x, y, time, 1] = all_CROP_AREA[x,y,real_year] * sprinkler
grid_area[x, y, time, 2] = all_CROP_AREA[x,y,real_year] * drip
all_area[time] = all_area[time] + all_CROP_AREA[x,y,real_year]
drip_area[time] = drip_area[time] + all_CROP_AREA[x,y,real_year] * drip
spri_area[time] = spri_area[time] + all_CROP_AREA[x,y,real_year] * sprinkler
floo_area[time] = floo_area[time] + all_CROP_AREA[x,y,real_year] * flood
for t in range(18):
country_area[i_country, t, 0] = floo_area[t]
country_area[i_country, t, 1] = spri_area[t]
country_area[i_country, t, 2] = drip_area[t]
if all_area[t] != 0:
country_frac[i_country, t, 0] = floo_area[t] / all_area[t]
country_frac[i_country, t, 1] = spri_area[t] / all_area[t]
country_frac[i_country, t, 2] = drip_area[t] / all_area[t]
except (KeyError):
error = 1
except (FileNotFoundError):
error = 1
scio.savemat('C:\Research2\Output_data\grid_frac.mat', {'grid_frac': grid_frac})
scio.savemat('C:\Research2\Output_data\grid_area.mat', {'grid_area': grid_area})
scio.savemat('C:\Research2\Output_data\country_frac.mat', {'country_frac': country_frac})
scio.savemat('C:\Research2\Output_data\country_area.mat', {'country_area': country_area})