From 648ae7929c17a3e9cb1da5bc8572be433bf2697f Mon Sep 17 00:00:00 2001 From: "Adam W. Sisco" Date: Fri, 15 Sep 2023 09:06:17 -0500 Subject: [PATCH 1/3] Changes sorting in write_kfac_file and fixes documentation typo --- rapid_utils/rapid_input_csv.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/rapid_utils/rapid_input_csv.py b/rapid_utils/rapid_input_csv.py index 9b06abe..9a4f6be 100644 --- a/rapid_utils/rapid_input_csv.py +++ b/rapid_utils/rapid_input_csv.py @@ -101,7 +101,7 @@ def write_kfac_file(flowline_file, """ Write a Muskingum kfac file containing first guesses (in seconds) of the - Muskingum k parameter. Three formula types are available, corresponing to + Muskingum k parameter. Three formula types are available, corresponding to equations (5)–(7) in Tavakoly et al. 2017 (https://doi.org/10.1111/1752-1688.12456). @@ -147,17 +147,15 @@ def write_kfac_file(flowline_file, usecols=0, dtype=int) # The kfac file must be ordered the same as the connectivity file - # Rewrite the length and slope arrays to ensure this requirement is met + # Reorder flowline_gdf to match the order of connect_rivid_array + flowline_gdf = flowline_gdf.set_index('COMID') \ + .reindex(connect_rivid_array).reset_index() + + # Read the length and slope arrays that are ordered as in the connectivity + # file length_array = flowline_gdf[length_field_name].values slope_array = flowline_gdf[slope_field_name].values - sort_idx = [] - for connect_rivid in connect_rivid_array: - sort_idx.append(flowline_id_list.index(connect_rivid)) - - length_array = length_array[sort_idx] - slope_array = slope_array[sort_idx] - if input_length_units == 'km': length_array *= 1000. if input_slope_percent: From 729f642d90764cd5a64e19e410bf3a8690cc04e9 Mon Sep 17 00:00:00 2001 From: "Adam W. Sisco" Date: Wed, 27 Sep 2023 11:23:33 -0500 Subject: [PATCH 2/3] Remove hard coded COMID reference in write_kfac_file --- rapid_utils/rapid_input_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rapid_utils/rapid_input_csv.py b/rapid_utils/rapid_input_csv.py index 9a4f6be..4ea4238 100644 --- a/rapid_utils/rapid_input_csv.py +++ b/rapid_utils/rapid_input_csv.py @@ -148,7 +148,7 @@ def write_kfac_file(flowline_file, # The kfac file must be ordered the same as the connectivity file # Reorder flowline_gdf to match the order of connect_rivid_array - flowline_gdf = flowline_gdf.set_index('COMID') \ + flowline_gdf = flowline_gdf.set_index(flowline_id_field_name) \ .reindex(connect_rivid_array).reset_index() # Read the length and slope arrays that are ordered as in the connectivity From 4ca1d07d33553b17659d328c03a44e94fe584bb0 Mon Sep 17 00:00:00 2001 From: "Adam W. Sisco" Date: Fri, 1 Nov 2024 10:40:49 -0500 Subject: [PATCH 3/3] Adds line break arguments when writing connectivity file --- rapid_utils/rapid_input_csv.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rapid_utils/rapid_input_csv.py b/rapid_utils/rapid_input_csv.py index 4ea4238..d3b0abd 100644 --- a/rapid_utils/rapid_input_csv.py +++ b/rapid_utils/rapid_input_csv.py @@ -3,7 +3,7 @@ from csv import writer as csv_writer import numpy as np import geopandas as gpd - +import os def write_connectivity_file(flowline_file, flowline_id_field_name, @@ -55,8 +55,8 @@ def write_connectivity_file(flowline_file, np.array([flowline_id, downstream_id, count_upstream]), list_upstream_id]).astype(int)) - with open(out_csv_file, 'w', encoding='utf-8') as csv_file: - connectivity_writer = csv_writer(csv_file) + with open(out_csv_file, 'w', encoding='utf-8', newline='') as csv_file: + connectivity_writer = csv_writer(csv_file, lineterminator=os.linesep) for row_list in list_write: out = np.concatenate([row_list, np.repeat(0, max_count_upstream - row_list[2])])