Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subnetwork final #17

Draft
wants to merge 54 commits into
base: subnetwork_creation_wScript
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
2bd1b39
Include short time step arg in compute_network call
jameshalgren Dec 3, 2020
6a4e1d3
qlat read and minor change to install file (#176)
jhrehanoaa Dec 3, 2020
0a5f4af
Update ngen script to work with new hydro fabric (#169)
donaldwj Dec 7, 2020
6faf3e3
network merging utility functions
awlostowski-noaa Nov 30, 2020
13ef191
utilities and main function to prune, snap and merge network. modifie…
awlostowski-noaa Dec 4, 2020
15dcf56
add CapeFear test basin to supernetwork options
awlostowski-noaa Dec 4, 2020
35ed6ee
new mask for Cape Fear river basin, full resolution
awlostowski-noaa Dec 4, 2020
8816fcf
fixed SettingWithCopyWarning in merge_parameters
awlostowski-noaa Dec 4, 2020
4f03a1c
export RouteLinks as shapefiles
awlostowski-noaa Dec 11, 2020
2d2a273
added Florence Test domain, catchment above Falls Lake, as supernetwo…
awlostowski-noaa Dec 11, 2020
a2cda66
mask for catchment above Falls Lake, Florence Test domain
awlostowski-noaa Dec 11, 2020
b3c5804
Add new network options to main compute script
jameshalgren Dec 11, 2020
9976b84
adding warm states
jhrehanoaa Dec 3, 2020
15a0601
hooked up warm states inputs
jhrehanoaa Dec 3, 2020
0d3d2ce
modify initial state variables
awlostowski-noaa Dec 11, 2020
947e456
add test strings
jameshalgren Dec 14, 2020
a29f6a5
refine qlateral timestep handling
jameshalgren Dec 14, 2020
0f23432
add test cases
jameshalgren Dec 14, 2020
e7586d0
Cython to Fortran bindings to Reservoir module and Python unit tests …
Nov 17, 2020
853b86e
updated python tests and instructions for multi-threaded test
Dec 18, 2020
7bc918b
added todo in setup.py
Dec 18, 2020
72b0610
correct bad use of dict keyword
jameshalgren Dec 14, 2020
1fb03fa
change function to get_supernetwork_parameters
jameshalgren Dec 14, 2020
2c98c9a
change call to set_supernetwork_parameters
jameshalgren Dec 14, 2020
7182913
change return to supernetwork_parameters
jameshalgren Dec 14, 2020
88f9bd0
turn on custom supernetwork parameters
jameshalgren Dec 14, 2020
f63a4c1
update comment headers
jameshalgren Dec 14, 2020
80666bf
reorganize param_df edits into one place
jameshalgren Dec 14, 2020
fa6e0cd
organize functions above main.
jameshalgren Dec 14, 2020
da4e34c
move build_connections into nhd_network_utilities_v02
jameshalgren Dec 14, 2020
f988c41
move reach organization to utility function
jameshalgren Dec 15, 2020
b3c4f3a
add TODO to remove dependence on dt in build_connections
jameshalgren Dec 15, 2020
cdacb43
make function for q0
jameshalgren Dec 15, 2020
86cfc7b
add function for qlats
jameshalgren Dec 15, 2020
bdddd93
separate _input_handler function
jameshalgren Dec 15, 2020
2b8dfa9
fix q0 indent
jameshalgren Dec 15, 2020
0603993
import glob
jameshalgren Dec 15, 2020
0030a7c
fix test-full-pocono
jameshalgren Dec 15, 2020
6851f95
update yaml/json CustomInput files for v02
jameshalgren Dec 16, 2020
f411b04
nhd test file (#15)
awlostowski-noaa Dec 16, 2020
1a5ef5a
change tail/headwaters from yield to return
jameshalgren Dec 18, 2020
b0d9fc1
fix Bug in disjoint check for unbranched networks
jameshalgren Dec 18, 2020
918cc75
Add Build Subnetworks Function
jameshalgren Dec 18, 2020
802c58a
add TODOs
jameshalgren Dec 18, 2020
9d42d84
add by-subnetwork parallel computation capability
jameshalgren Dec 18, 2020
7869e91
move to --inplace setup.py
jameshalgren Dec 18, 2020
f6036eb
use xarray.open_mfdataset() to load qlat
jameshalgren Jan 6, 2021
8c25635
move processing out of parity_parameters
jameshalgren Jan 6, 2021
2d250d7
allow filter
jameshalgren Jan 6, 2021
04da1b6
black
jameshalgren Jan 6, 2021
87996bc
update mf_dataset load to optimize for serial loading
jameshalgren Jan 9, 2021
cd9f62c
Add parity check parameters to json/yaml
jameshalgren Jan 11, 2021
d0a0fe8
add comment placeholder
jameshalgren Jan 13, 2021
7ddd2a3
remove square brackets from compute_network return
jameshalgren Jan 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions install.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

This file provides more detailed instructions on how to install, configure, and get the project running. Summary instructions are provided in readme.md.

#Step 1 - Clone the repository
#Step 1 -- Activate a python environment on your local machine

#Step 2 - Clone the repository
git clone [email protected]:NOAA-OWP/t-route.git

#Step 2 - Compile necessary files
#Step 3 - Compile necessary files
cd t-route/src/python_routing_v02/

./compiler.sh

#Step 3 - Run the model
#Step 4 - Run the model

python3 compute_nhd_routing_SingleSeg_v02.py

#Step 3 - Alternative v01 model (defaults can be overwritten with custom input yaml)
#Step 4 - Alternative v01 model (defaults can be overwritten with custom input yaml)
cd ..
cd python_routing_v01
python3 compute_nhd_routing_SingleSeg_v01.py
Expand Down
3 changes: 2 additions & 1 deletion src/external_connections/next_gen_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def read_catchment_lateral_flows(path):
catchment_id = int(file_name_str_list[0][4 :])
catchment_id_list.append(catchment_id)
# Read the second column of a csv file and return a series. The index will be an autoincrementing range.
catchment_qlats = pd.read_csv(os.path.join(path, file_name), names=[catchment_id], usecols=[1], squeeze=True)
catchment_qlats = pd.read_csv(os.path.join(path, file_name), usecols=['Flow'], squeeze=True)
catchment_qlats.rename(catchment_id, inplace=True)
ql.append(catchment_qlats)

qlats = pd.concat(ql, axis='columns').T
Expand Down
111 changes: 80 additions & 31 deletions src/external_connections/next_gen_network.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# coding: utf-8
# example usage: python next_gen_network.py -n sugar_creek_waterbody_data_subset.geojson
# example usage: python next_gen_network.py -n ./data/coarse/flowpath_data.geojson -s cat-71 cat-42 cat-34 cat-26 cat-68 cat-67 cat-27 cat-52 -i ./data

"""This program reads in and modifies lateral flows from the next generation water modeling framework and feeds them into mc_reach.compute_network.
This program presupposes that the mc_reach has been compiled prior to running."""
Expand All @@ -14,6 +14,7 @@
from functools import partial
from itertools import chain, islice
import next_gen_io
import json

def _handle_args():
parser = argparse.ArgumentParser(
Expand All @@ -26,73 +27,118 @@ def _handle_args():
help="Input catchment network",
dest="supernetwork"
)

parser.add_argument(
"-s",
"--subset",
help="Subset of catchments to process",
dest="subset",
nargs='+'
)
parser.add_argument(
"-i",
"--input",
help="path to lateral flow inputs",
dest="input"
)
return parser.parse_args()

root = pathlib.Path('../..').resolve()
root = pathlib.Path('.').resolve()
sys.path.append(r"../python_framework_v02")
sys.path.append(r"../python_routing_v02")
sys.path.append(r"../../test/input/next_gen")

test_folder = pathlib.Path(root, "test")
next_gen_input_folder = test_folder.joinpath("input", "next_gen")

import nhd_network
import nhd_io
import troute.nhd_network as nhd_network
import troute.nhd_io as nhd_io
import mc_reach

def main():

args = _handle_args()

next_gen_input_folder = test_folder.joinpath("input", "next_gen")
if args.input:
next_gen_input_folder = pathlib.Path(args.input)

#The following 2 values are currently hard coded for this test domain
# The following 2 values are currently hard coded for this test domain
nts = 720 # number of timestep = 1140 * 60(model timestep) = 86400 = day
dt_mc = 300.0 # time interval for MC

#Currently tested on the Sugar Creek domain
ngen_network_df = nhd_io.read_geopandas(os.path.join(next_gen_input_folder, args.supernetwork))

#Create dictionary mapping each connection ID
ngen_network_dict = dict(zip(ngen_network_df.ID, ngen_network_df.toID))
# Currently tested on the Sugar Creek domain
ngen_network_df = nhd_io.read_geopandas( args.supernetwork )
if args.subset:
ngen_network_df = ngen_network_df[ ngen_network_df['realized_catchment'].isin(args.subset) ]

# Create dictionary mapping each connection ID
ngen_network_dict = dict(zip(ngen_network_df.id, ngen_network_df.toid))
#ngen_network_dict = dict(zip(ngen_network_df.ID, ngen_network_df.toID))

def node_key_func(x):
return int(x[4:])
return int(x[3:])

#Extract the ID integer values
# Extract the ID integer values
waterbody_connections = {node_key_func(k): node_key_func(v) for k, v in ngen_network_dict.items()}

#Convert dictionary connections to data frame and make ID column the index
# Convert dictionary connections to data frame and make ID column the index
waterbody_df = pd.DataFrame.from_dict(waterbody_connections, orient='index', columns=['to'])

#Sort ID index column
# Sort ID index column
waterbody_df = waterbody_df.sort_index()

waterbody_df = nhd_io.replace_downstreams(waterbody_df, "to", 0)

connections = nhd_network.extract_connections(waterbody_df, "to")

#Read and convert catchment lateral flows to format that can be processed by compute_network
# Read and convert catchment lateral flows to format that can be processed by compute_network
qlats = next_gen_io.read_catchment_lateral_flows(next_gen_input_folder)

print(qlats)
rconn = nhd_network.reverse_network(connections)

subnets = nhd_network.reachable_network(rconn, check_disjoint=False)

# read the routelink file
nhd_routelink = nhd_io.read_netcdf("data/RouteLink_NHDPLUS.nc")
nhd_routelink['dt'] = 300.0

nhd_routelink.set_index("link", inplace=True)

waterbody_df['dt'] = 300.0
routelink_cols = {
"downstream": "to",
"dx": "Length",
"n": "n",
"ncc": "nCC",
"s0": "So",
"bw": "BtmWdth",
"tw": "TopWdth",
"twcc": "TopWdthCC",
"waterbody": "NHDWaterbodyComID",
"musk": "MusK",
"musx": "MusX",
"cs": "ChSlp",
}

#Setting all below to 1.0 until we can get the appropriate parameters
waterbody_df['bw'] = 1.0
waterbody_df['tw'] = 1.0
waterbody_df['twcc'] = 1.0
waterbody_df['dx'] = 1.0
waterbody_df['n'] = 1.0
waterbody_df['ncc'] = 1.0
waterbody_df['cs'] = 1.0
waterbody_df['s0'] = 1.0
routelink_cols = dict([(value,key) for key, value in routelink_cols.items() ])

nhd_routelink.rename(columns=routelink_cols, inplace=True)

with open(next_gen_input_folder/'coarse/crosswalk.json') as f:
crosswalk_data = json.load(f)
waterbody_df['comid'] = waterbody_df.apply(lambda x: crosswalk_data['cat-' + str(x.name)]['outlet_COMID'], axis=1)

waterbody_df = waterbody_df.join(nhd_routelink, on='comid', how='left')

del nhd_routelink


# initial conditions, assume to be zero
# TO DO: Allow optional reading of initial conditions from WRF
q0 = pd.DataFrame(
0, index=waterbody_df.index, columns=["qu0", "qd0", "h0"], dtype="float32"
)

#Set types as float32
waterbody_df = waterbody_df.astype({"dt": "float32", "bw": "float32", "tw": "float32", "twcc": "float32", "dx": "float32", "n": "float32", "ncc": "float32", "cs": "float32", "s0": "float32"})

subreaches = {}

for tw, net in subnets.items():
Expand All @@ -104,9 +150,12 @@ def node_key_func(x):
for twi, (tw, reach) in enumerate(subreaches.items(), 1):
r = list(chain.from_iterable(reach))
data_sub = waterbody_df.loc[r, ['dt', 'bw', 'tw', 'twcc', 'dx', 'n', 'ncc', 'cs', 's0']].sort_index()
#data_sub = waterbody_df.loc[r, ['dt', 'bw', 'tw', 'twcc', 'dx', 'n', 'ncc', 'cs', 's0']]
qlat_sub = qlats.loc[r].sort_index()
q0_sub = q0.loc[r].sort_index()

results.append(mc_reach.compute_network(
nts, reach, subnets[tw], data_sub.index.values, data_sub.columns.values, data_sub.values, qlat_sub.values
nts, reach, subnets[tw], data_sub.index.values, data_sub.columns.values, data_sub.values, qlat_sub.values, q0_sub.values
)
)

Expand Down
Loading