Skip to content

Commit

Permalink
feat: add support for netcdf qlat files
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed Aug 16, 2023
1 parent 97a4df7 commit 5a48b05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/troute-network/troute/AbstractNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import pyarrow as pa
import pyarrow.parquet as pq
import xarray as xr

from troute.nhd_network import extract_connections, replace_waterbodies_connections, reverse_network, reachable_network, split_at_waterbodies_and_junctions, split_at_junction, dfs_decomposition
from troute.nhd_network_utilities_v02 import organize_independent_networks
Expand Down Expand Up @@ -741,6 +742,11 @@ def build_forcing_sets(self,):
t1 = datetime.strptime(t1, "%Y-%m-%d_%H:%M:%S")
t2 = nhd_io.get_param_str(second_file, "model_output_valid_time")
t2 = datetime.strptime(t2, "%Y-%m-%d_%H:%M:%S")
elif forcing_glob_filter=='*.nc':
t1_str = first_file.name.removeprefix('qlat_').removesuffix('.nc')
t1 = datetime.strptime(t1_str, '%Y-%m-%d %H:%M:%S')
t2_str = second_file.name.removeprefix('qlat_').removesuffix('.nc')
t2 = datetime.strptime(t2_str, '%Y-%m-%d %H:%M:%S')
else:
df = read_file(first_file)
t1_str = pd.to_datetime(df.columns[1]).strftime("%Y-%m-%d_%H:%M:%S")
Expand Down Expand Up @@ -810,6 +816,8 @@ def build_forcing_sets(self,):
final_qlat = qlat_input_folder.joinpath(run_sets[j]['qlat_files'][-1])
if forcing_glob_filter=="*.CHRTOUT_DOMAIN1":
final_timestamp_str = nhd_io.get_param_str(final_qlat,'model_output_valid_time')
elif forcing_glob_filter=='*.nc':
final_timestamp_str = final_qlat.name.removeprefix('qlat_').removesuffix('.nc').replace(' ', '_')
else:
df = read_file(final_qlat)
final_timestamp_str = pd.to_datetime(df.columns[1]).strftime("%Y-%m-%d_%H:%M:%S")
Expand Down Expand Up @@ -863,5 +871,8 @@ def read_file(file_name):
elif extension=='.parquet':
df = pq.read_table(file_name).to_pandas().reset_index()
df.index.name = None
elif extension=='.nc':
df = xr.open_dataset(file_name).to_pandas().reset_index()
df.index.name = None

return df
return df
6 changes: 5 additions & 1 deletion src/troute-network/troute/HYFeaturesNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from itertools import chain
from joblib import delayed, Parallel
from collections import defaultdict
import xarray as xr

import troute.nhd_io as nhd_io #FIXME
from troute.nhd_network import reverse_dict, extract_connections, reverse_network, reachable
Expand Down Expand Up @@ -602,6 +603,9 @@ def read_file(file_name):
elif extension=='.parquet':
df = pq.read_table(file_name).to_pandas().reset_index()
df.index.name = None
elif extension=='.nc':
df = xr.open_dataset(file_name).to_pandas().reset_index()
df.index.name = None

return df

Expand Down Expand Up @@ -717,4 +721,4 @@ def replace_waterbodies_connections(connections, waterbodies):
# copy to new network unchanged
new_conn[n] = connections[n]

return new_conn, link_lake
return new_conn, link_lake

0 comments on commit 5a48b05

Please sign in to comment.