@@ -63,10 +63,6 @@ class Append:
6363 list of AbstractModule objects to execute result storage ops for
6464 MODULES_LIST: list
6565 list of string module names to create objects for
66- PRIORS_SUFFIX: str
67- string suffix for priors file name
68- RESULTS_SUFFIX: str
69- string suffix for output file name
7066 sos_nrids: nd.array
7167 array of SOS reach identifiers on the node-level
7268 sos_nids: nd.array
@@ -99,20 +95,22 @@ class Append:
9995 """
10096
10197
102- PRIORS_SUFFIX = "sword_v16_SOS_priors"
103- RESULTS_SUFFIX = "sword_v16_SOS_results"
104- # PRIORS_SUFFIX = "sword_v11_SOS_priors"
105- # RESULTS_SUFFIX = "sword_v11_SOS_results"
10698 VERS_LENGTH = 4
10799 INT_FILL_VALUE = - 999
108100
109101 def __init__ (self , cont_json , index , input_dir , output_dir , modules , logger ,
110- metadata_json ):
102+ metadata_json , sword_version = "17" ):
111103 """
112104 TODO: Remove "temp" from output_dir (self.sos_new)
113105
114106 Parameters
115107 ----------
108+ sword_version: str
109+ string arg input of SWORD version that built priors
110+ priors_suffix: str
111+ string suffix for priors file name
112+ results_suffix: str
113+ string suffix for output file name
116114 cont_json: Path
117115 path to continent JSON file
118116 index: int
@@ -125,12 +123,18 @@ def __init__(self, cont_json, index, input_dir, output_dir, modules, logger,
125123 list of module results to append to the SoS
126124 logger: Logger
127125 logger to use for logging state
126+ sword_version: str, optional
127+ SWORD version number (default: "16")
128128 """
129129
130+ self .sword_version = sword_version
131+ self .priors_suffix = f"sword_v{ sword_version } _SOS_priors"
132+ self .results_suffix = f"sword_v{ sword_version } _SOS_results"
133+
130134 self .cont = get_cont_data (cont_json , index )
131135 self .sos_cur = input_dir / "sos"
132- self .sos_file = output_dir / "sos" / f"{ list (self .cont .keys ())[0 ]} _{ self .RESULTS_SUFFIX } .nc"
133- sos_data = get_continent_sos_data (self .sos_cur , list (self .cont .keys ())[0 ], self .PRIORS_SUFFIX )
136+ self .sos_file = output_dir / "sos" / f"{ list (self .cont .keys ())[0 ]} _{ self .results_suffix } .nc"
137+ sos_data = get_continent_sos_data (self .sos_cur , list (self .cont .keys ())[0 ], self .priors_suffix )
134138 self .sos_rids = sos_data ["reaches" ]
135139 self .sos_nrids = sos_data ["node_reaches" ]
136140 self .sos_nids = sos_data ["nodes" ]
@@ -153,7 +157,7 @@ def create_new_version(self):
153157 # Create directory and file
154158 self .sos_file .parent .mkdir (parents = True , exist_ok = True )
155159 continent = self .sos_file .name .split ('_' )[0 ]
156- prior_sos = Dataset (self .sos_cur / f"{ continent } _{ self .PRIORS_SUFFIX } .nc" )
160+ prior_sos = Dataset (self .sos_cur / f"{ continent } _{ self .priors_suffix } .nc" )
157161 result_sos = Dataset (self .sos_file , 'w' )
158162
159163 # Global attributes
@@ -197,13 +201,14 @@ def create_new_version(self):
197201 # Variable length time steps
198202 self .vlen_f = result_sos .createVLType (np .float64 , "vlen_float" )
199203 self .vlen_i = result_sos .createVLType (np .int32 , "vlen_int" )
204+ self .vlen_i8 = result_sos .createVLType (np .int64 , "vlen_int64" )
200205 self .vlen_s = result_sos .createVLType ("S1" , "vlen_str" )
201206
202207 # Node and reach group
203208 write_reaches (prior_sos , result_sos , self .metadata_json )
204209
205210 # netCDF4 library is not reading in node_ids - use xarray
206- ds = xr .open_dataset (self .sos_cur / f"{ continent } _{ self .PRIORS_SUFFIX } .nc" ,
211+ ds = xr .open_dataset (self .sos_cur / f"{ continent } _{ self .priors_suffix } .nc" ,
207212 group = "nodes" , drop_variables = "river_name" )
208213 node_ids = ds ["node_id" ].data
209214 ds .close ()
@@ -217,9 +222,14 @@ def append_data(self):
217222 """Append data to the SoS by executing module storage operations."""
218223
219224 for module in self .modules :
220- module .append_module (self .metadata_json )
221- self .logger .info (f"Appended { module .__class__ .__name__ } data to { self .sos_file .name } ." )
222-
225+ try :
226+ module .append_module (self .metadata_json )
227+ self .logger .info (f"Appended { module .__class__ .__name__ } data to { self .sos_file .name } ." )
228+ except Exception as e :
229+ import traceback
230+ self .logger .error (f"Failed to append { module .__class__ .__name__ } : { e } " )
231+ self .logger .error (traceback .format_exc ())
232+
223233 def create_modules (self , run_type , input_dir , diag_dir , flpe_dir , moi_dir , \
224234 off_dir , val_dir , consensus_dir , lakeflow_dir , ssc_dir ):
225235
@@ -285,7 +295,7 @@ def create_modules(self, run_type, input_dir, diag_dir, flpe_dir, moi_dir, \
285295 self .sos_nids ))
286296 if module == "priors" and run_type == "constrained" :
287297 self .modules .append (Priors (list (self .cont .values ())[0 ], \
288- self .sos_cur , self .sos_file , self .logger , self .PRIORS_SUFFIX ))
298+ self .sos_cur , self .sos_file , self .logger , self .priors_suffix ))
289299 if module == "sad" :
290300 self .modules .append (Sad (list (self .cont .values ())[0 ], \
291301 flpe_dir , self .sos_file , self .logger , self .vlen_f , self .vlen_i , \
@@ -301,7 +311,7 @@ def create_modules(self, run_type, input_dir, diag_dir, flpe_dir, moi_dir, \
301311
302312 if module == "consensus" :
303313 self .modules .append (Consensus (list (self .cont .values ())[0 ], \
304- flpe_dir , self .sos_file , self .logger , self .vlen_f , self .vlen_i , \
314+ flpe_dir , self .sos_file , self .logger , self .vlen_f , self .vlen_i8 , \
305315 self .vlen_s , self .sos_rids ))
306316
307317 if module == "ssc" :
@@ -484,4 +494,4 @@ def set_variable_atts(variable, variable_dict):
484494 """Set the variable attribute metdata."""
485495
486496 for name , value in variable_dict .items ():
487- setattr (variable , name , value )
497+ setattr (variable , name , value )
0 commit comments