Skip to content

Commit

Permalink
Fix create_NC and model_SMBalance for invariant variable, problem wit…
Browse files Browse the repository at this point in the history
…h theta_sat.nc
  • Loading branch information
trngbich committed May 13, 2020
1 parent d3c3996 commit fc61dc2
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 25 deletions.
19 changes: 14 additions & 5 deletions modules/WA/create_NC.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ def main(nc_fn,basin,dataset,template,cutline,step='month',**kwargs):
DTindex: object
pandas DatetimeIndex range
'''
dims = {'time': None, 'latitude': None, 'longitude': None}
dims['latitude'], dims['longitude'], optionsProj, optionsClip = _get_lats_lons(template, cutline)
_init_nc(nc_fn, dims, dataset, attr = {"basin_name" : basin})

overview=_make_overview(dataset, step)
if "invariant" in overview.keys():
dims = {'latitude': None, 'longitude': None}
else:
dims = {'time': None, 'latitude': None, 'longitude': None}

dims['latitude'], dims['longitude'], optionsProj, optionsClip = _get_lats_lons(template, cutline)
_init_nc(nc_fn, dims, dataset, attr = {"basin_name" : basin})
succes = _fill_data_to_nc(nc_fn, overview, optionsProj, optionsClip, cutline)
return succes

Expand Down Expand Up @@ -156,7 +161,9 @@ def _fill_data_to_nc(nc_file, overview, optionsProj, optionsClip, shape):
sourceds = None
os.remove(temp_file)
os.remove(temp_fileP)
__fill_nc_one_timestep(nc_file, var, shape)
print(var)
print(shape)
__fill_nc_one_timestep(nc_file, var, shape,time_val = None)

# Save time-variant data to nc-file.
for date in tqdm(overview.keys()):
Expand Down Expand Up @@ -292,7 +299,9 @@ def __fill_nc_one_timestep(nc_file, var, shape, time_val = None):
# Add invariant data to nc-file.
else:
for name, data in var.items():
out_nc.variables[name][...] = data
print(name)
print(data.shape)
out_nc.variables[name][0::] = data

# Close nc-file.
out_nc.close()
Expand Down
7 changes: 5 additions & 2 deletions modules/WA/model_SMBalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
def open_nc(nc,timechunk=1,chunksize=1000):
dts=xr.open_dataset(nc)
key=list(dts.keys())[0]
var=dts[key].chunk({"time": timechunk, "latitude": chunksize, "longitude": chunksize}) #.ffill("time")
if 'time' in list(dts.dims.keys()):
var=dts[key].chunk({"time": timechunk, "latitude": chunksize, "longitude": chunksize}) #.ffill("time")
else:
var=dts[key].chunk({"latitude": chunksize, "longitude": chunksize}) #.ffill("time")
return var,key

def SCS_calc_SRO(P,I,NRD,SMmax,SM, cf):
Expand Down Expand Up @@ -245,7 +248,7 @@ def run_SMBalance(MAIN_FOLDER,p_in,e_in,i_in,nrd_in,lu_in,smsat_file,
mask = xr.where(((lu==80) | (lu==81) | (lu==70) | (lu==200)|(lu==90)), 1,0)
#include flooded shrub?
Rd = root_depth(lu)
SMmax=thetasat[0]*Rd
SMmax=thetasat*Rd
f_consumed = Consumed_fraction(lu)
for t in range(t1,t2):
print('time: ', t)
Expand Down
36 changes: 34 additions & 2 deletions notebooks/0_Collect_Prepare_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading WaPOR catalog...\n",
"\n",
"Download daily WaPOR precipitation data for the period 2009-01-01 till 2009-12-31\n",
"Loading WaPOR catalog...\n",
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
"\n",
"Download monthly WaPOR precipitation data for the period 2009-01-01 till 2009-12-31\n",
"Loading WaPOR catalog...\n",
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
"\n",
"Download monthly WaPOR Reference Evapotranspiration data for the period 2009-01-01 till 2009-12-31\n",
"Loading WaPOR catalog...\n",
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
"\n",
"Download monthly WaPOR Actual Evapotranspiration data for the period 2009-01-01 till 2009-12-31\n",
"Loading WaPOR catalog...\n",
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
"\n",
"Download yearly WaPOR Land Cover Class data for the period 2009-01-01 till 2009-12-31\n",
"Loading WaPOR catalog...\n",
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n",
"\n",
"Download dekadal WaPOR Interception data for the period 2009-01-01 till 2009-12-31\n",
"Loading WaPOR catalog...\n",
"Progress: |██████████████████████████████████████████████████| 100.0% Complete\n"
]
}
],
"source": [
"WaPOR.API.version=2\n",
"catalog=WaPOR.API.getCatalog()\n",
Expand Down
37 changes: 25 additions & 12 deletions notebooks/1_Create_NetCDF.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{
"data": {
"text/plain": [
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200204_16h38.pickle'"
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200513_08h50.pickle'"
]
},
"execution_count": 2,
Expand Down Expand Up @@ -122,7 +122,9 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
Expand All @@ -136,7 +138,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:07<00:00, 1.25it/s]\n"
"100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:06<00:00, 1.65it/s]\n"
]
},
{
Expand All @@ -152,7 +154,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:07<00:00, 1.29it/s]\n"
"100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:04<00:00, 2.14it/s]\n"
]
},
{
Expand All @@ -168,7 +170,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [00:39<00:00, 3.34it/s]\n"
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:36<00:00, 3.20it/s]\n"
]
},
{
Expand All @@ -184,7 +186,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [01:22<00:00, 1.51it/s]\n"
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [01:19<00:00, 1.34it/s]\n"
]
},
{
Expand All @@ -200,7 +202,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [00:40<00:00, 3.25it/s]\n"
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:38<00:00, 3.15it/s]\n"
]
},
{
Expand All @@ -216,7 +218,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [01:20<00:00, 1.26it/s]\n"
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:54<00:00, 1.49it/s]\n"
]
},
{
Expand All @@ -232,7 +234,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████| 120/120 [00:37<00:00, 3.34it/s]\n"
"100%|████████████████████████████████████████████████████████████████████████████████| 120/120 [00:41<00:00, 3.41it/s]\n"
]
},
{
Expand All @@ -241,7 +243,18 @@
"text": [
"Finished nRD_monthly.nc\n",
"6.806621238863997e-16\n",
"-6.806621238863997e-16\n"
"-6.806621238863997e-16\n",
"{'Top-soil Saturated Water Content': array([[-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
" ...,\n",
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.],\n",
" [-9999., -9999., -9999., ..., -9999., -9999., -9999.]],\n",
" dtype=float32)}\n",
"D:\\Github\\WAPORWA\\data\\Global\\Basins\\BasinLitani.shp\n",
"Top-soil Saturated Water Content\n",
"(958, 1181)\n"
]
},
{
Expand All @@ -261,7 +274,7 @@
{
"data": {
"text/plain": [
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200204_16h45.pickle'"
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200513_08h55.pickle'"
]
},
"execution_count": 4,
Expand Down Expand Up @@ -303,7 +316,7 @@
" if BASIN['input_data']['stat'][key] is not None: \n",
" nc_fn=os.path.join(Dir_out,key+'_stat.nc') \n",
" dataset={key:[BASIN['input_data']['stat'][key][0],\n",
" ('time','latitude', 'longitude'), \n",
" ('latitude', 'longitude'), \n",
" {'units': BASIN['input_data']['stat'][key][1], \n",
" 'quantity':BASIN['input_data']['stat'][key][2],\n",
" 'source': 'WaPOR', 'period':'stat'}]}\n",
Expand Down
71 changes: 67 additions & 4 deletions notebooks/3_RunSMBalance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"<td style=\"vertical-align: top; border: 0px solid white\">\n",
"<h3>Client</h3>\n",
"<ul>\n",
" <li><b>Scheduler: </b>inproc://172.17.14.58/14392/1\n",
" <li><b>Scheduler: </b>inproc://192.168.2.17/10332/1\n",
" <li><b>Dashboard: </b><a href='http://localhost:8787/status' target='_blank'>http://localhost:8787/status</a>\n",
"</ul>\n",
"</td>\n",
Expand All @@ -29,7 +29,7 @@
"</table>"
],
"text/plain": [
"<Client: scheduler='inproc://172.17.14.58/14392/1' processes=2 cores=4>"
"<Client: scheduler='inproc://192.168.2.17/10332/1' processes=2 cores=4>"
]
},
"execution_count": 1,
Expand Down Expand Up @@ -65,6 +65,69 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Name': 'Litani',\n",
" 'Dir': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main',\n",
" 'time_range': ['2009-01-01', '2018-12-31'],\n",
" 'end_month': 'DEC',\n",
" 'geo_data': {'basin': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\Basins\\\\BasinLitani.shp'},\n",
" 'global_data': {'grace': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\GRACE\\\\GSFC.glb.200301_201607_v02.4-ICE6G',\n",
" 'grand': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\GRanD\\\\GRanD_reservoirs_v1_1.shp',\n",
" 'wdpa': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\WDPA\\\\WDPA_17countries.shp'},\n",
" 'input_data': {'yearly': {'lcc': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L2_LCC_A',\n",
" '-',\n",
" 'Landcover Class'],\n",
" 'lu': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\luwa',\n",
" '-',\n",
" 'WA+ Landuse Categories']},\n",
" 'monthly': {'p': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L1_PCP_M',\n",
" 'mm/month',\n",
" 'Precipitation'],\n",
" 'et': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L2_AETI_M',\n",
" 'mm/month',\n",
" 'Actual Evapotranspiration'],\n",
" 'ret': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L1_RET_M',\n",
" 'mm/month',\n",
" 'Reference Evapotranspiration'],\n",
" 'i': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\L2_I_M',\n",
" 'mm/month',\n",
" 'Interception'],\n",
" 'nRD': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\Rainy_Days',\n",
" 'days/month',\n",
" 'Number of Rainy Days']},\n",
" 'stat': {'thetasat': ['D:\\\\Github\\\\WAPORWA\\\\data\\\\Global\\\\HiHydroSoils\\\\thetasat_topsoil.tif',\n",
" '%',\n",
" 'Top-soil Saturated Water Content']}},\n",
" 'input_ts': {'dS': None,\n",
" 'Qoutlet': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\Qoutlet_litani_QASMIYE_SeaMouth.csv',\n",
" 'Qswout': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Input\\\\Qibt_litani_MarkabaTunnel.csv',\n",
" 'Qgwout': None},\n",
" 'main_data': {'yearly': {'lcc': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\lcc_yearly.nc',\n",
" 'lu': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\lu_yearly.nc'},\n",
" 'monthly': {'p': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\p_monthly.nc',\n",
" 'et': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\et_monthly.nc',\n",
" 'ret': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\ret_monthly.nc',\n",
" 'i': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\i_monthly.nc',\n",
" 'nRD': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\nRD_monthly.nc'},\n",
" 'stat': {'thetasat': 'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\data\\\\nc\\\\thetasat_stat.nc'}}}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"BASIN"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -205,10 +268,10 @@
{
"data": {
"text/plain": [
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200204_16h51.pickle'"
"'D:\\\\Github\\\\WAPORWA\\\\data\\\\Litani\\\\Main\\\\Info_20200513_09h00.pickle'"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand Down

0 comments on commit fc61dc2

Please sign in to comment.