Skip to content

Commit fc61dc2

Browse files
committed
Fix create_NC and model_SMBalance for invariant variable, problem with theta_sat.nc
1 parent d3c3996 commit fc61dc2

5 files changed

Lines changed: 145 additions & 25 deletions

File tree

modules/WA/create_NC.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ def main(nc_fn,basin,dataset,template,cutline,step='month',**kwargs):
4343
DTindex: object
4444
pandas DatetimeIndex range
4545
'''
46-
dims = {'time': None, 'latitude': None, 'longitude': None}
47-
dims['latitude'], dims['longitude'], optionsProj, optionsClip = _get_lats_lons(template, cutline)
48-
_init_nc(nc_fn, dims, dataset, attr = {"basin_name" : basin})
46+
4947
overview=_make_overview(dataset, step)
48+
if "invariant" in overview.keys():
49+
dims = {'latitude': None, 'longitude': None}
50+
else:
51+
dims = {'time': None, 'latitude': None, 'longitude': None}
52+
53+
dims['latitude'], dims['longitude'], optionsProj, optionsClip = _get_lats_lons(template, cutline)
54+
_init_nc(nc_fn, dims, dataset, attr = {"basin_name" : basin})
5055
succes = _fill_data_to_nc(nc_fn, overview, optionsProj, optionsClip, cutline)
5156
return succes
5257

@@ -156,7 +161,9 @@ def _fill_data_to_nc(nc_file, overview, optionsProj, optionsClip, shape):
156161
sourceds = None
157162
os.remove(temp_file)
158163
os.remove(temp_fileP)
159-
__fill_nc_one_timestep(nc_file, var, shape)
164+
print(var)
165+
print(shape)
166+
__fill_nc_one_timestep(nc_file, var, shape,time_val = None)
160167

161168
# Save time-variant data to nc-file.
162169
for date in tqdm(overview.keys()):
@@ -292,7 +299,9 @@ def __fill_nc_one_timestep(nc_file, var, shape, time_val = None):
292299
# Add invariant data to nc-file.
293300
else:
294301
for name, data in var.items():
295-
out_nc.variables[name][...] = data
302+
print(name)
303+
print(data.shape)
304+
out_nc.variables[name][0::] = data
296305

297306
# Close nc-file.
298307
out_nc.close()

modules/WA/model_SMBalance.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
def open_nc(nc,timechunk=1,chunksize=1000):
1818
dts=xr.open_dataset(nc)
1919
key=list(dts.keys())[0]
20-
var=dts[key].chunk({"time": timechunk, "latitude": chunksize, "longitude": chunksize}) #.ffill("time")
20+
if 'time' in list(dts.dims.keys()):
21+
var=dts[key].chunk({"time": timechunk, "latitude": chunksize, "longitude": chunksize}) #.ffill("time")
22+
else:
23+
var=dts[key].chunk({"latitude": chunksize, "longitude": chunksize}) #.ffill("time")
2124
return var,key
2225

2326
def SCS_calc_SRO(P,I,NRD,SMmax,SM, cf):
@@ -245,7 +248,7 @@ def run_SMBalance(MAIN_FOLDER,p_in,e_in,i_in,nrd_in,lu_in,smsat_file,
245248
mask = xr.where(((lu==80) | (lu==81) | (lu==70) | (lu==200)|(lu==90)), 1,0)
246249
#include flooded shrub?
247250
Rd = root_depth(lu)
248-
SMmax=thetasat[0]*Rd
251+
SMmax=thetasat*Rd
249252
f_consumed = Consumed_fraction(lu)
250253
for t in range(t1,t2):
251254
print('time: ', t)

notebooks/0_Collect_Prepare_data.ipynb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,41 @@
3737
},
3838
{
3939
"cell_type": "code",
40-
"execution_count": null,
40+
"execution_count": 2,
4141
"metadata": {},
42-
"outputs": [],
42+
"outputs": [
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"Loading WaPOR catalog...\n",
48+
"\n",
49+
"Download daily WaPOR precipitation data for the period 2009-01-01 till 2009-12-31\n",
50+
"Loading WaPOR catalog...\n",
51+
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
52+
"\n",
53+
"Download monthly WaPOR precipitation data for the period 2009-01-01 till 2009-12-31\n",
54+
"Loading WaPOR catalog...\n",
55+
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
56+
"\n",
57+
"Download monthly WaPOR Reference Evapotranspiration data for the period 2009-01-01 till 2009-12-31\n",
58+
"Loading WaPOR catalog...\n",
59+
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
60+
"\n",
61+
"Download monthly WaPOR Actual Evapotranspiration data for the period 2009-01-01 till 2009-12-31\n",
62+
"Loading WaPOR catalog...\n",
63+
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
64+
"\n",
65+
"Download yearly WaPOR Land Cover Class data for the period 2009-01-01 till 2009-12-31\n",
66+
"Loading WaPOR catalog...\n",
67+
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
68+
"\n",
69+
"Download dekadal WaPOR Interception data for the period 2009-01-01 till 2009-12-31\n",
70+
"Loading WaPOR catalog...\n",
71+
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n"
72+
]
73+
}
74+
],
4375
"source": [
4476
"WaPOR.API.version=2\n",
4577
"catalog=WaPOR.API.getCatalog()\n",

notebooks/1_Create_NetCDF.ipynb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"data": {
3636
"text/plain": [
37-
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200204_16h38.pickle'"
37+
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200513_08h50.pickle'"
3838
]
3939
},
4040
"execution_count": 2,
@@ -122,7 +122,9 @@
122122
{
123123
"cell_type": "code",
124124
"execution_count": 4,
125-
"metadata": {},
125+
"metadata": {
126+
"scrolled": false
127+
},
126128
"outputs": [
127129
{
128130
"name": "stdout",
@@ -136,7 +138,7 @@
136138
"name": "stderr",
137139
"output_type": "stream",
138140
"text": [
139-
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:07<00:00, 1.25it/s]\n"
141+
"100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:06<00:00, 1.65it/s]\n"
140142
]
141143
},
142144
{
@@ -152,7 +154,7 @@
152154
"name": "stderr",
153155
"output_type": "stream",
154156
"text": [
155-
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:07<00:00, 1.29it/s]\n"
157+
"100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:04<00:00, 2.14it/s]\n"
156158
]
157159
},
158160
{
@@ -168,7 +170,7 @@
168170
"name": "stderr",
169171
"output_type": "stream",
170172
"text": [
171-
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [00:39<00:00, 3.34it/s]\n"
173+
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:36<00:00, 3.20it/s]\n"
172174
]
173175
},
174176
{
@@ -184,7 +186,7 @@
184186
"name": "stderr",
185187
"output_type": "stream",
186188
"text": [
187-
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [01:22<00:00, 1.51it/s]\n"
189+
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [01:19<00:00, 1.34it/s]\n"
188190
]
189191
},
190192
{
@@ -200,7 +202,7 @@
200202
"name": "stderr",
201203
"output_type": "stream",
202204
"text": [
203-
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [00:40<00:00, 3.25it/s]\n"
205+
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:38<00:00, 3.15it/s]\n"
204206
]
205207
},
206208
{
@@ -216,7 +218,7 @@
216218
"name": "stderr",
217219
"output_type": "stream",
218220
"text": [
219-
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [01:20<00:00, 1.26it/s]\n"
221+
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:54<00:00, 1.49it/s]\n"
220222
]
221223
},
222224
{
@@ -232,7 +234,7 @@
232234
"name": "stderr",
233235
"output_type": "stream",
234236
"text": [
235-
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [00:37<00:00, 3.34it/s]\n"
237+
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:41<00:00, 3.41it/s]\n"
236238
]
237239
},
238240
{
@@ -241,7 +243,18 @@
241243
"text": [
242244
"Finished nRD_monthly.nc\n",
243245
"6.806621238863997e-16\n",
244-
"-6.806621238863997e-16\n"
246+
"-6.806621238863997e-16\n",
247+
"{'Top-soil Saturated Water Content': array([[-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
248+
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
249+
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
250+
" ...,\n",
251+
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
252+
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
253+
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.]],\n",
254+
" dtype=float32)}\n",
255+
"D:\\Github\\WAPORWA\\data\\Global\\Basins\\BasinLitani.shp\n",
256+
"Top-soil Saturated Water Content\n",
257+
"(958, 1181)\n"
245258
]
246259
},
247260
{
@@ -261,7 +274,7 @@
261274
{
262275
"data": {
263276
"text/plain": [
264-
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200204_16h45.pickle'"
277+
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200513_08h55.pickle'"
265278
]
266279
},
267280
"execution_count": 4,
@@ -303,7 +316,7 @@
303316
" if BASIN['input_data']['stat'][key] is not None: \n",
304317
" nc_fn=os.path.join(Dir_out,key+'_stat.nc') \n",
305318
" dataset={key:[BASIN['input_data']['stat'][key][0],\n",
306-
" ('time','latitude', 'longitude'), \n",
319+
" ('latitude', 'longitude'), \n",
307320
" {'units': BASIN['input_data']['stat'][key][1], \n",
308321
" 'quantity':BASIN['input_data']['stat'][key][2],\n",
309322
" 'source': 'WaPOR', 'period':'stat'}]}\n",

notebooks/3_RunSMBalance.ipynb

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"<td style=\"vertical-align: top; border: 0px solid white\">\n",
1414
"<h3>Client</h3>\n",
1515
"<ul>\n",
16-
" <li><b>Scheduler: </b>inproc://172.17.14.58/14392/1\n",
16+
" <li><b>Scheduler: </b>inproc://192.168.2.17/10332/1\n",
1717
" <li><b>Dashboard: </b><a href='http://localhost:8787/status' target='_blank'>http://localhost:8787/status</a>\n",
1818
"</ul>\n",
1919
"</td>\n",
@@ -29,7 +29,7 @@
2929
"</table>"
3030
],
3131
"text/plain": [
32-
"<Client: scheduler='inproc://172.17.14.58/14392/1' processes=2 cores=4>"
32+
"<Client: scheduler='inproc://192.168.2.17/10332/1' processes=2 cores=4>"
3333
]
3434
},
3535
"execution_count": 1,
@@ -65,6 +65,69 @@
6565
"cell_type": "code",
6666
"execution_count": 2,
6767
"metadata": {},
68+
"outputs": [
69+
{
70+
"data": {
71+
"text/plain": [
72+
"{'Name': 'Litani',\n",
73+
" 'Dir': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main',\n",
74+
" 'time_range': ['2009-01-01', '2018-12-31'],\n",
75+
" 'end_month': 'DEC',\n",
76+
" 'geo_data': {'basin': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\Basins\\\\BasinLitani.shp'},\n",
77+
" 'global_data': {'grace': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\GRACE\\\\GSFC.glb.200301_201607_v02.4-ICE6G',\n",
78+
" 'grand': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\GRanD\\\\GRanD_reservoirs_v1_1.shp',\n",
79+
" 'wdpa': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\WDPA\\\\WDPA_17countries.shp'},\n",
80+
" 'input_data': {'yearly': {'lcc': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L2_LCC_A',\n",
81+
" '-',\n",
82+
" 'Landcover Class'],\n",
83+
" 'lu': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\luwa',\n",
84+
" '-',\n",
85+
" 'WA+ Landuse Categories']},\n",
86+
" 'monthly': {'p': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L1_PCP_M',\n",
87+
" 'mm/month',\n",
88+
" 'Precipitation'],\n",
89+
" 'et': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L2_AETI_M',\n",
90+
" 'mm/month',\n",
91+
" 'Actual Evapotranspiration'],\n",
92+
" 'ret': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L1_RET_M',\n",
93+
" 'mm/month',\n",
94+
" 'Reference Evapotranspiration'],\n",
95+
" 'i': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L2_I_M',\n",
96+
" 'mm/month',\n",
97+
" 'Interception'],\n",
98+
" 'nRD': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\Rainy_Days',\n",
99+
" 'days/month',\n",
100+
" 'Number of Rainy Days']},\n",
101+
" 'stat': {'thetasat': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\HiHydroSoils\\\\thetasat_topsoil.tif',\n",
102+
" '%',\n",
103+
" 'Top-soil Saturated Water Content']}},\n",
104+
" 'input_ts': {'dS': None,\n",
105+
" 'Qoutlet': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\Qoutlet_litani_QASMIYE_SeaMouth.csv',\n",
106+
" 'Qswout': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\Qibt_litani_MarkabaTunnel.csv',\n",
107+
" 'Qgwout': None},\n",
108+
" 'main_data': {'yearly': {'lcc': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\lcc_yearly.nc',\n",
109+
" 'lu': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\lu_yearly.nc'},\n",
110+
" 'monthly': {'p': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\p_monthly.nc',\n",
111+
" 'et': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\et_monthly.nc',\n",
112+
" 'ret': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\ret_monthly.nc',\n",
113+
" 'i': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\i_monthly.nc',\n",
114+
" 'nRD': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\nRD_monthly.nc'},\n",
115+
" 'stat': {'thetasat': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\thetasat_stat.nc'}}}"
116+
]
117+
},
118+
"execution_count": 2,
119+
"metadata": {},
120+
"output_type": "execute_result"
121+
}
122+
],
123+
"source": [
124+
"BASIN"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 3,
130+
"metadata": {},
68131
"outputs": [
69132
{
70133
"name": "stdout",
@@ -205,10 +268,10 @@
205268
{
206269
"data": {
207270
"text/plain": [
208-
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200204_16h51.pickle'"
271+
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200513_09h00.pickle'"
209272
]
210273
},
211-
"execution_count": 2,
274+
"execution_count": 3,
212275
"metadata": {},
213276
"output_type": "execute_result"
214277
}

0 commit comments

Comments
 (0)