ras-commander 0.51.0__py3-none-any.whl → 0.53.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ras_commander/RasPrj.py CHANGED
@@ -1,879 +1,934 @@
1
- """
2
- RasPrj.py - Manages HEC-RAS projects within the ras-commander library
3
-
4
- This module provides a class for managing HEC-RAS projects.
5
-
6
- Classes:
7
- RasPrj: A class for managing HEC-RAS projects.
8
-
9
- Functions:
10
- init_ras_project: Initialize a RAS project.
11
- get_ras_exe: Determine the HEC-RAS executable path based on the input.
12
-
13
- DEVELOPER NOTE:
14
- This class is used to initialize a RAS project and is used in conjunction with the RasCmdr class to manage the execution of RAS plans.
15
- By default, the RasPrj class is initialized with the global 'ras' object.
16
- However, you can create multiple RasPrj instances to manage multiple projects.
17
- Do not mix and match global 'ras' object instances and custom instances of RasPrj - it will cause errors.
18
-
19
- This module is part of the ras-commander library and uses a centralized logging configuration.
20
-
21
- Logging Configuration:
22
- - The logging is set up in the logging_config.py file.
23
- - A @log_call decorator is available to automatically log function calls.
24
- - Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
25
- - Logs are written to both console and a rotating file handler.
26
- - The default log file is 'ras_commander.log' in the 'logs' directory.
27
- - The default log level is INFO.
28
-
29
- To use logging in this module:
30
- 1. Use the @log_call decorator for automatic function call logging.
31
- 2. For additional logging, use logger.[level]() calls (e.g., logger.info(), logger.debug()).
32
-
33
-
34
- Example:
35
- @log_call
36
- def my_function():
37
-
38
- logger.debug("Additional debug information")
39
- # Function logic here
40
-
41
- -----
42
-
43
- All of the methods in this class are class methods and are designed to be used with instances of the class.
44
-
45
- List of Functions in RasPrj:
46
- - initialize()
47
- - _load_project_data()
48
- - _get_geom_file_for_plan()
49
- - _parse_plan_file()
50
- - _parse_unsteady_file()
51
- - _get_prj_entries()
52
- - _parse_boundary_condition()
53
- - is_initialized (property)
54
- - check_initialized()
55
- - find_ras_prj()
56
- - get_project_name()
57
- - get_prj_entries()
58
- - get_plan_entries()
59
- - get_flow_entries()
60
- - get_unsteady_entries()
61
- - get_geom_entries()
62
- - get_hdf_entries()
63
- - print_data()
64
- - get_plan_value()
65
- - get_boundary_conditions()
66
-
67
- Functions in RasPrj that are not part of the class:
68
- - init_ras_project()
69
- - get_ras_exe()
70
-
71
-
72
-
73
-
74
- """
75
- import os
76
- import re
77
- from pathlib import Path
78
- import pandas as pd
79
- from typing import Union, Any, List, Dict, Tuple
80
- import logging
81
- from ras_commander.LoggingConfig import get_logger
82
- from ras_commander.Decorators import log_call
83
-
84
- logger = get_logger(__name__)
85
-
86
- class RasPrj:
87
-
88
- def __init__(self):
89
- self.initialized = False
90
- self.boundaries_df = None # New attribute to store boundary conditions
91
-
92
- @log_call
93
- def initialize(self, project_folder, ras_exe_path):
94
- """
95
- Initialize a RasPrj instance.
96
-
97
- This method sets up the RasPrj instance with the given project folder and RAS executable path.
98
- It finds the project file, loads project data, sets the initialization flag, and now also
99
- extracts boundary conditions.
100
-
101
- Args:
102
- project_folder (str or Path): Path to the HEC-RAS project folder.
103
- ras_exe_path (str or Path): Path to the HEC-RAS executable.
104
-
105
- Raises:
106
- ValueError: If no HEC-RAS project file is found in the specified folder.
107
-
108
- Note:
109
- This method is intended for internal use. External users should use the init_ras_project function instead.
110
- """
111
- self.project_folder = Path(project_folder)
112
- self.prj_file = self.find_ras_prj(self.project_folder)
113
- if self.prj_file is None:
114
- logger.error(f"No HEC-RAS project file found in {self.project_folder}")
115
- raise ValueError(f"No HEC-RAS project file found in {self.project_folder}")
116
- self.project_name = Path(self.prj_file).stem
117
- self.ras_exe_path = ras_exe_path
118
- self._load_project_data()
119
- self.boundaries_df = self.get_boundary_conditions() # Extract boundary conditions
120
- self.initialized = True
121
- logger.info(f"Initialization complete for project: {self.project_name}")
122
- logger.info(f"Plan entries: {len(self.plan_df)}, Flow entries: {len(self.flow_df)}, "
123
- f"Unsteady entries: {len(self.unsteady_df)}, Geometry entries: {len(self.geom_df)}, "
124
- f"Boundary conditions: {len(self.boundaries_df)}")
125
- logger.info(f"Geometry HDF files found: {self.plan_df['Geom_File'].notna().sum()}")
126
-
127
- @log_call
128
- def _load_project_data(self):
129
- """
130
- Load project data from the HEC-RAS project file.
131
-
132
- This method initializes DataFrames for plan, flow, unsteady, and geometry entries
133
- by calling the _get_prj_entries method for each entry type.
134
- """
135
- # Initialize DataFrames
136
- self.plan_df = self._get_prj_entries('Plan')
137
- self.flow_df = self._get_prj_entries('Flow')
138
- self.unsteady_df = self._get_prj_entries('Unsteady')
139
- self.geom_df = self.get_geom_entries() # Use get_geom_entries instead of _get_prj_entries
140
-
141
- # Add Geom_File to plan_df
142
- self.plan_df['Geom_File'] = self.plan_df.apply(lambda row: self._get_geom_file_for_plan(row['plan_number']), axis=1)
143
-
144
-
145
- def _get_geom_file_for_plan(self, plan_number):
146
- """
147
- Get the geometry file path for a given plan number.
148
-
149
- Args:
150
- plan_number (str): The plan number to find the geometry file for.
151
-
152
- Returns:
153
- str: The full path to the geometry HDF file, or None if not found.
154
- """
155
- plan_file_path = self.project_folder / f"{self.project_name}.p{plan_number}"
156
- try:
157
- with open(plan_file_path, 'r') as plan_file:
158
- for line in plan_file:
159
- if line.startswith("Geom File="):
160
- geom_file = line.strip().split('=')[1]
161
- geom_hdf_path = self.project_folder / f"{self.project_name}.{geom_file}.hdf"
162
- if geom_hdf_path.exists():
163
- return str(geom_hdf_path)
164
- else:
165
- return None
166
- except Exception as e:
167
- logger.error(f"Error reading plan file for geometry: {e}")
168
- return None
169
-
170
-
171
- def _parse_plan_file(self, plan_file_path):
172
- """
173
- Parse a plan file and extract critical information.
174
-
175
- Args:
176
- plan_file_path (Path): Path to the plan file.
177
-
178
- Returns:
179
- dict: Dictionary containing extracted plan information.
180
- """
181
- plan_info = {}
182
- try:
183
- with open(plan_file_path, 'r') as file:
184
- content = file.read()
185
-
186
- # Extract description
187
- description_match = re.search(r'Begin DESCRIPTION(.*?)END DESCRIPTION', content, re.DOTALL)
188
- if description_match:
189
- plan_info['description'] = description_match.group(1).strip()
190
-
191
- # BEGIN Exception to Style Guide, this is needed to keep the key names consistent with the plan file keys.
192
-
193
- # Extract other critical information
194
- supported_plan_keys = {
195
- 'Computation Interval': r'Computation Interval=(.+)',
196
- 'DSS File': r'DSS File=(.+)',
197
- 'Flow File': r'Flow File=(.+)',
198
- 'Friction Slope Method': r'Friction Slope Method=(.+)',
199
- 'Geom File': r'Geom File=(.+)',
200
- 'Mapping Interval': r'Mapping Interval=(.+)',
201
- 'Plan Title': r'Plan Title=(.+)',
202
- 'Program Version': r'Program Version=(.+)',
203
- 'Run HTab': r'Run HTab=(.+)',
204
- 'Run PostProcess': r'Run PostProcess=(.+)',
205
- 'Run Sediment': r'Run Sediment=(.+)',
206
- 'Run UNet': r'Run UNet=(.+)',
207
- 'Run WQNet': r'Run WQNet=(.+)',
208
- 'Short Identifier': r'Short Identifier=(.+)',
209
- 'Simulation Date': r'Simulation Date=(.+)',
210
- 'UNET D1 Cores': r'UNET D1 Cores=(.+)',
211
- 'UNET Use Existing IB Tables': r'UNET Use Existing IB Tables=(.+)',
212
- 'UNET 1D Methodology': r'UNET 1D Methodology=(.+)',
213
- 'UNET D2 SolverType': r'UNET D2 SolverType=(.+)',
214
- 'UNET D2 Name': r'UNET D2 Name=(.+)'
215
- }
216
-
217
- # END Exception to Style Guide
218
-
219
- for key, pattern in supported_plan_keys.items():
220
- match = re.search(pattern, content)
221
- if match:
222
- plan_info[key] = match.group(1).strip()
223
-
224
- logger.debug(f"Parsed plan file: {plan_file_path}")
225
- except Exception as e:
226
- logger.exception(f"Error parsing plan file {plan_file_path}: {e}")
227
-
228
- return plan_info
229
-
230
- def _get_prj_entries(self, entry_type):
231
- """
232
- Extract entries of a specific type from the HEC-RAS project file.
233
-
234
- Args:
235
- entry_type (str): The type of entry to extract (e.g., 'Plan', 'Flow', 'Unsteady', 'Geom').
236
-
237
- Returns:
238
- pd.DataFrame: A DataFrame containing the extracted entries.
239
-
240
- Note:
241
- This method reads the project file and extracts entries matching the specified type.
242
- For 'Unsteady' entries, it parses additional information from the unsteady file.
243
- """
244
- entries = []
245
- pattern = re.compile(rf"{entry_type} File=(\w+)")
246
-
247
- try:
248
- with open(self.prj_file, 'r') as file:
249
- for line in file:
250
- match = pattern.match(line.strip())
251
- if match:
252
- file_name = match.group(1)
253
- full_path = str(self.project_folder / f"{self.project_name}.{file_name}")
254
- entry = {
255
- f'{entry_type.lower()}_number': file_name[1:],
256
- 'full_path': full_path
257
- }
258
-
259
- if entry_type == 'Plan':
260
- plan_info = self._parse_plan_file(Path(full_path))
261
- entry.update(plan_info)
262
-
263
- hdf_results_path = self.project_folder / f"{self.project_name}.p{file_name[1:]}.hdf"
264
- entry['HDF_Results_Path'] = str(hdf_results_path) if hdf_results_path.exists() else None
265
-
266
- if entry_type == 'Unsteady':
267
- unsteady_info = self._parse_unsteady_file(Path(full_path))
268
- entry.update(unsteady_info)
269
-
270
- entries.append(entry)
271
- except Exception as e:
272
- raise
273
-
274
- return pd.DataFrame(entries)
275
-
276
- def _parse_unsteady_file(self, unsteady_file_path):
277
- """
278
- Parse an unsteady flow file and extract critical information.
279
-
280
- Args:
281
- unsteady_file_path (Path): Path to the unsteady flow file.
282
-
283
- Returns:
284
- dict: Dictionary containing extracted unsteady flow information.
285
- """
286
- unsteady_info = {}
287
- with open(unsteady_file_path, 'r') as file:
288
- content = file.read()
289
-
290
- # BEGIN Exception to Style Guide, this is needed to keep the key names consistent with the unsteady file keys.
291
-
292
- supported_unsteady_keys = {
293
- 'Flow Title': r'Flow Title=(.+)',
294
- 'Program Version': r'Program Version=(.+)',
295
- 'Use Restart': r'Use Restart=(.+)',
296
- 'Precipitation Mode': r'Precipitation Mode=(.+)',
297
- 'Wind Mode': r'Wind Mode=(.+)',
298
- 'Met BC=Precipitation|Mode': r'Met BC=Precipitation\|Mode=(.+)',
299
- 'Met BC=Evapotranspiration|Mode': r'Met BC=Evapotranspiration\|Mode=(.+)',
300
- 'Met BC=Precipitation|Expanded View': r'Met BC=Precipitation\|Expanded View=(.+)',
301
- 'Met BC=Precipitation|Constant Units': r'Met BC=Precipitation\|Constant Units=(.+)',
302
- 'Met BC=Precipitation|Gridded Source': r'Met BC=Precipitation\|Gridded Source=(.+)'
303
- }
304
-
305
- # END Exception to Style Guide
306
-
307
- for key, pattern in supported_unsteady_keys.items():
308
- match = re.search(pattern, content)
309
- if match:
310
- unsteady_info[key] = match.group(1).strip()
311
-
312
- return unsteady_info
313
-
314
- @property
315
- def is_initialized(self):
316
- """
317
- Check if the RasPrj instance has been initialized.
318
-
319
- Returns:
320
- bool: True if the instance has been initialized, False otherwise.
321
- """
322
- return self.initialized
323
-
324
- @log_call
325
- def check_initialized(self):
326
- """
327
- Ensure that the RasPrj instance has been initialized.
328
-
329
- Raises:
330
- RuntimeError: If the project has not been initialized.
331
- """
332
- if not self.initialized:
333
- raise RuntimeError("Project not initialized. Call init_ras_project() first.")
334
-
335
- @staticmethod
336
- @log_call
337
- def find_ras_prj(folder_path):
338
- """
339
- Find the appropriate HEC-RAS project file (.prj) in the given folder.
340
-
341
- Parameters:
342
- folder_path (str or Path): Path to the folder containing HEC-RAS files.
343
-
344
- Returns:
345
- Path: The full path of the selected .prj file or None if no suitable file is found.
346
- """
347
- folder_path = Path(folder_path)
348
- prj_files = list(folder_path.glob("*.prj"))
349
- rasmap_files = list(folder_path.glob("*.rasmap"))
350
- if len(prj_files) == 1:
351
- return prj_files[0].resolve()
352
- if len(prj_files) > 1:
353
- if len(rasmap_files) == 1:
354
- base_filename = rasmap_files[0].stem
355
- prj_file = folder_path / f"{base_filename}.prj"
356
- if prj_file.exists():
357
- return prj_file.resolve()
358
- for prj_file in prj_files:
359
- try:
360
- with open(prj_file, 'r') as file:
361
- content = file.read()
362
- if "Proj Title=" in content:
363
- return prj_file.resolve()
364
- except Exception:
365
- continue
366
- return None
367
-
368
-
369
- @log_call
370
- def get_project_name(self):
371
- """
372
- Get the name of the HEC-RAS project.
373
-
374
- Returns:
375
- str: The name of the project.
376
-
377
- Raises:
378
- RuntimeError: If the project has not been initialized.
379
- """
380
- self.check_initialized()
381
- return self.project_name
382
-
383
- @log_call
384
- def get_prj_entries(self, entry_type):
385
- """
386
- Get entries of a specific type from the HEC-RAS project.
387
-
388
- Args:
389
- entry_type (str): The type of entry to retrieve (e.g., 'Plan', 'Flow', 'Unsteady', 'Geom').
390
-
391
- Returns:
392
- pd.DataFrame: A DataFrame containing the requested entries.
393
-
394
- Raises:
395
- RuntimeError: If the project has not been initialized.
396
- """
397
- self.check_initialized()
398
- return self._get_prj_entries(entry_type)
399
-
400
- @log_call
401
- def get_plan_entries(self):
402
- """
403
- Get all plan entries from the HEC-RAS project.
404
-
405
- Returns:
406
- pd.DataFrame: A DataFrame containing all plan entries.
407
-
408
- Raises:
409
- RuntimeError: If the project has not been initialized.
410
- """
411
- self.check_initialized()
412
- return self._get_prj_entries('Plan')
413
-
414
- @log_call
415
- def get_flow_entries(self):
416
- """
417
- Get all flow entries from the HEC-RAS project.
418
-
419
- Returns:
420
- pd.DataFrame: A DataFrame containing all flow entries.
421
-
422
- Raises:
423
- RuntimeError: If the project has not been initialized.
424
- """
425
- self.check_initialized()
426
- return self._get_prj_entries('Flow')
427
-
428
- @log_call
429
- def get_unsteady_entries(self):
430
- """
431
- Get all unsteady flow entries from the HEC-RAS project.
432
-
433
- Returns:
434
- pd.DataFrame: A DataFrame containing all unsteady flow entries.
435
-
436
- Raises:
437
- RuntimeError: If the project has not been initialized.
438
- """
439
- self.check_initialized()
440
- return self._get_prj_entries('Unsteady')
441
-
442
- @log_call
443
- def get_geom_entries(self):
444
- """
445
- Get geometry entries from the project file.
446
-
447
- Returns:
448
- pd.DataFrame: DataFrame containing geometry entries.
449
- """
450
- geom_pattern = re.compile(r'Geom File=(\w+)')
451
- geom_entries = []
452
-
453
- try:
454
- with open(self.prj_file, 'r') as f:
455
- for line in f:
456
- match = geom_pattern.search(line)
457
- if match:
458
- geom_entries.append(match.group(1))
459
-
460
- geom_df = pd.DataFrame({'geom_file': geom_entries})
461
- geom_df['geom_number'] = geom_df['geom_file'].str.extract(r'(\d+)$')
462
- geom_df['full_path'] = geom_df['geom_file'].apply(lambda x: str(self.project_folder / f"{self.project_name}.{x}"))
463
- geom_df['hdf_path'] = geom_df['full_path'] + ".hdf"
464
-
465
- logger.info(f"Found {len(geom_df)} geometry entries")
466
- return geom_df
467
- except Exception as e:
468
- logger.error(f"Error reading geometry entries from project file: {e}")
469
- raise
470
-
471
- @log_call
472
- def get_hdf_entries(self):
473
- """
474
- Get HDF entries for plans that have results.
475
-
476
- Returns:
477
- pd.DataFrame: A DataFrame containing plan entries with HDF results.
478
- Returns an empty DataFrame if no HDF entries are found.
479
- """
480
- self.check_initialized()
481
-
482
- hdf_entries = self.plan_df[self.plan_df['HDF_Results_Path'].notna()].copy()
483
-
484
- if hdf_entries.empty:
485
- return pd.DataFrame(columns=self.plan_df.columns)
486
-
487
- return hdf_entries
488
-
489
-
490
- @log_call
491
- def print_data(self):
492
- """Print all RAS Object data for this instance."""
493
- self.check_initialized()
494
- logger.info(f"--- Data for {self.project_name} ---")
495
- logger.info(f"Project folder: {self.project_folder}")
496
- logger.info(f"PRJ file: {self.prj_file}")
497
- logger.info(f"HEC-RAS executable: {self.ras_exe_path}")
498
- logger.info("Plan files:")
499
- logger.info(f"\n{self.plan_df}")
500
- logger.info("Flow files:")
501
- logger.info(f"\n{self.flow_df}")
502
- logger.info("Unsteady flow files:")
503
- logger.info(f"\n{self.unsteady_df}")
504
- logger.info("Geometry files:")
505
- logger.info(f"\n{self.geom_df}")
506
- logger.info("HDF entries:")
507
- logger.info(f"\n{self.get_hdf_entries()}")
508
- logger.info("Boundary conditions:")
509
- logger.info(f"\n{self.boundaries_df}")
510
- logger.info("----------------------------")
511
-
512
- @staticmethod
513
- @log_call
514
- def get_plan_value(
515
- plan_number_or_path: Union[str, Path],
516
- key: str,
517
- ras_object=None
518
- ) -> Any:
519
- """
520
- Retrieve a specific value from a HEC-RAS plan file.
521
-
522
- Parameters:
523
- plan_number_or_path (Union[str, Path]): The plan number (1 to 99) or full path to the plan file
524
- key (str): The key to retrieve from the plan file
525
- ras_object (RasPrj, optional): Specific RAS object to use. If None, uses the global ras instance.
526
-
527
- Returns:
528
- Any: The value associated with the specified key
529
-
530
- Raises:
531
- ValueError: If an invalid key is provided or if the plan file is not found
532
- IOError: If there's an error reading the plan file
533
-
534
- Note: See the docstring of update_plan_file for a full list of available keys and their types.
535
-
536
- Example:
537
- >>> computation_interval = RasUtils.get_plan_value("01", "computation_interval")
538
- >>> print(f"Computation interval: {computation_interval}")
539
- """
540
- logger = getLogger(__name__)
541
- ras_obj = ras_object or ras
542
- ras_obj.check_initialized()
543
-
544
- valid_keys = {
545
- 'description', 'computation_interval', 'dss_file', 'flow_file', 'friction_slope_method',
546
- 'geom_file', 'mapping_interval', 'plan_file', 'plan_title', 'program_version',
547
- 'run_htab', 'run_post_process', 'run_sediment', 'run_unet', 'run_wqnet',
548
- 'short_identifier', 'simulation_date', 'unet_d1_cores', 'unet_use_existing_ib_tables',
549
- 'unet_1d_methodology', 'unet_d2_solver_type', 'unet_d2_name'
550
- }
551
-
552
- if key not in valid_keys:
553
- logger.error(f"Invalid key: {key}")
554
- raise ValueError(f"Invalid key: {key}. Valid keys are: {', '.join(valid_keys)}")
555
-
556
- plan_file_path = Path(plan_number_or_path)
557
- if not plan_file_path.is_file():
558
- plan_file_path = RasUtils.get_plan_path(plan_number_or_path, ras_object)
559
- if not plan_file_path.exists():
560
- logger.error(f"Plan file not found: {plan_file_path}")
561
- raise ValueError(f"Plan file not found: {plan_file_path}")
562
-
563
- try:
564
- with open(plan_file_path, 'r') as file:
565
- content = file.read()
566
- except IOError as e:
567
- logger.error(f"Error reading plan file {plan_file_path}: {e}")
568
- raise
569
-
570
- if key == 'description':
571
- import re
572
- match = re.search(r'Begin DESCRIPTION(.*?)END DESCRIPTION', content, re.DOTALL)
573
- return match.group(1).strip() if match else None
574
- else:
575
- pattern = f"{key.replace('_', ' ').title()}=(.*)"
576
- import re
577
- match = re.search(pattern, content)
578
- return match.group(1).strip() if match else None
579
-
580
- @log_call
581
- def get_boundary_conditions(self) -> pd.DataFrame:
582
- """
583
- Extract boundary conditions from unsteady flow files and create a DataFrame.
584
-
585
- This method parses unsteady flow files to extract boundary condition information.
586
- It creates a DataFrame with structured data for known boundary condition types
587
- and parameters, and associates this information with the corresponding unsteady flow file.
588
-
589
- Note:
590
- Any lines in the boundary condition blocks that are not explicitly parsed and
591
- incorporated into the DataFrame are captured in a multi-line string. This string
592
- is logged at the DEBUG level for each boundary condition. This feature is crucial
593
- for developers incorporating new boundary condition types or parameters, as it
594
- allows them to see what information might be missing from the current parsing logic.
595
- If no unsteady flow files are present, it returns an empty DataFrame.
596
-
597
- Returns:
598
- pd.DataFrame: A DataFrame containing detailed boundary condition information,
599
- linked to the unsteady flow files.
600
-
601
- Usage:
602
- To see the unparsed lines, set the logging level to DEBUG before calling this method:
603
-
604
- import logging
605
- getLogger().setLevel(logging.DEBUG)
606
-
607
- boundaries_df = ras_project.get_boundary_conditions()
608
- linked to the unsteady flow files. Returns an empty DataFrame if
609
- no unsteady flow files are present.
610
- """
611
- boundary_data = []
612
-
613
- # Check if unsteady_df is empty
614
- if self.unsteady_df.empty:
615
- logger.info("No unsteady flow files found in the project.")
616
- return pd.DataFrame() # Return an empty DataFrame
617
-
618
- for _, row in self.unsteady_df.iterrows():
619
- unsteady_file_path = row['full_path']
620
- unsteady_number = row['unsteady_number']
621
-
622
- try:
623
- with open(unsteady_file_path, 'r') as file:
624
- content = file.read()
625
- except IOError as e:
626
- logger.error(f"Error reading unsteady file {unsteady_file_path}: {e}")
627
- continue
628
-
629
- bc_blocks = re.split(r'(?=Boundary Location=)', content)[1:]
630
-
631
- for i, block in enumerate(bc_blocks, 1):
632
- bc_info, unparsed_lines = self._parse_boundary_condition(block, unsteady_number, i)
633
- boundary_data.append(bc_info)
634
-
635
- if unparsed_lines:
636
- logger.debug(f"Unparsed lines for boundary condition {i} in unsteady file {unsteady_number}:\n{unparsed_lines}")
637
-
638
- if not boundary_data:
639
- logger.info("No boundary conditions found in unsteady flow files.")
640
- return pd.DataFrame() # Return an empty DataFrame if no boundary conditions were found
641
-
642
- boundaries_df = pd.DataFrame(boundary_data)
643
-
644
- # Merge with unsteady_df to get relevant unsteady flow file information
645
- merged_df = pd.merge(boundaries_df, self.unsteady_df,
646
- left_on='unsteady_number', right_on='unsteady_number', how='left')
647
-
648
- return merged_df
649
-
650
- def _parse_boundary_condition(self, block: str, unsteady_number: str, bc_number: int) -> Tuple[Dict, str]:
651
- lines = block.split('\n')
652
- bc_info = {
653
- 'unsteady_number': unsteady_number,
654
- 'boundary_condition_number': bc_number
655
- }
656
-
657
- parsed_lines = set()
658
-
659
- # Parse Boundary Location
660
- boundary_location = lines[0].split('=')[1].strip()
661
- fields = [field.strip() for field in boundary_location.split(',')]
662
- bc_info.update({
663
- 'river_reach_name': fields[0] if len(fields) > 0 else '',
664
- 'river_station': fields[1] if len(fields) > 1 else '',
665
- 'storage_area_name': fields[2] if len(fields) > 2 else '',
666
- 'pump_station_name': fields[3] if len(fields) > 3 else ''
667
- })
668
- parsed_lines.add(0)
669
-
670
- # Determine BC Type
671
- bc_types = {
672
- 'Flow Hydrograph=': 'Flow Hydrograph',
673
- 'Lateral Inflow Hydrograph=': 'Lateral Inflow Hydrograph',
674
- 'Uniform Lateral Inflow Hydrograph=': 'Uniform Lateral Inflow Hydrograph',
675
- 'Stage Hydrograph=': 'Stage Hydrograph',
676
- 'Friction Slope=': 'Normal Depth',
677
- 'Gate Name=': 'Gate Opening'
678
- }
679
-
680
- bc_info['bc_type'] = 'Unknown'
681
- bc_info['hydrograph_type'] = None
682
- for i, line in enumerate(lines[1:], 1):
683
- for key, bc_type in bc_types.items():
684
- if line.startswith(key):
685
- bc_info['bc_type'] = bc_type
686
- if 'Hydrograph' in bc_type:
687
- bc_info['hydrograph_type'] = bc_type
688
- parsed_lines.add(i)
689
- break
690
- if bc_info['bc_type'] != 'Unknown':
691
- break
692
-
693
- # Parse other fields
694
- known_fields = ['Interval', 'DSS Path', 'Use DSS', 'Use Fixed Start Time', 'Fixed Start Date/Time',
695
- 'Is Critical Boundary', 'Critical Boundary Flow', 'DSS File']
696
- for i, line in enumerate(lines):
697
- if '=' in line:
698
- key, value = line.split('=', 1)
699
- key = key.strip()
700
- if key in known_fields:
701
- bc_info[key] = value.strip()
702
- parsed_lines.add(i)
703
-
704
- # Handle hydrograph values
705
- bc_info['hydrograph_num_values'] = 0
706
- if bc_info['hydrograph_type']:
707
- hydrograph_key = f"{bc_info['hydrograph_type']}="
708
- hydrograph_line = next((line for i, line in enumerate(lines) if line.startswith(hydrograph_key)), None)
709
- if hydrograph_line:
710
- hydrograph_index = lines.index(hydrograph_line)
711
- values_count = int(hydrograph_line.split('=')[1].strip())
712
- bc_info['hydrograph_num_values'] = values_count
713
- if values_count > 0:
714
- values = ' '.join(lines[hydrograph_index + 1:]).split()[:values_count]
715
- bc_info['hydrograph_values'] = values
716
- parsed_lines.update(range(hydrograph_index, hydrograph_index + (values_count // 5) + 2))
717
-
718
- # Collect unparsed lines
719
- unparsed_lines = '\n'.join(line for i, line in enumerate(lines) if i not in parsed_lines and line.strip())
720
-
721
- if unparsed_lines:
722
- logger.debug(f"Unparsed lines for boundary condition {bc_number} in unsteady file {unsteady_number}:\n{unparsed_lines}")
723
-
724
- return bc_info, unparsed_lines
725
-
726
-
727
- # Create a global instance named 'ras'
728
- # Defining the global instance allows the init_ras_project function to initialize the project.
729
- # This only happens on the library initialization, not when the user calls init_ras_project.
730
- ras = RasPrj()
731
-
732
- # END OF CLASS DEFINITION
733
-
734
-
735
- # START OF FUNCTION DEFINITIONS
736
-
737
-
738
- @log_call
739
- def init_ras_project(ras_project_folder, ras_version=None, ras_instance=None):
740
- """
741
- Initialize a RAS project.
742
-
743
- USE THIS FUNCTION TO INITIALIZE A RAS PROJECT, NOT THE INITIALIZE METHOD OF THE RasPrj CLASS.
744
- The initialize method of the RasPrj class only modifies the global 'ras' object.
745
-
746
- This function creates or initializes a RasPrj instance, providing a safer and more
747
- flexible interface than directly using the 'initialize' method.
748
-
749
- Parameters:
750
- -----------
751
- ras_project_folder : str
752
- The path to the RAS project folder.
753
- ras_version : str, optional
754
- The version of RAS to use (e.g., "6.6").
755
- The version can also be a full path to the Ras.exe file. (Useful when calling ras objects for folder copies.)
756
- If None, the function will attempt to use the version from the global 'ras' object or a default path.
757
- You MUST specify a version number via init at some point or ras will not run.
758
- Once the ras_version is specified once it should auto-fill from the global 'ras' object.
759
- The RAS Commander Library Assistant can ignore this argument since it does not have Ras.exe present, but all of other operations are fully working.
760
- ras_instance : RasPrj, optional
761
- An instance of RasPrj to initialize. If None, the global 'ras' instance is used.
762
-
763
- Returns:
764
- --------
765
- RasPrj
766
- An initialized RasPrj instance.
767
-
768
- Usage:
769
- ------
770
- 1. For general use with a single project:
771
- init_ras_project("/path/to/project")
772
- # Use the global 'ras' object after initialization
773
-
774
- 2. For managing multiple projects:
775
- project1 = init_ras_project("/path/to/project1", "6.6", ras_instance=RasPrj())
776
- project2 = init_ras_project("/path/to/project2", ras_instance=RasPrj())
777
-
778
- Notes:
779
- ------
780
- - This function is preferred over directly calling the 'initialize' method.
781
- - It supports both the global 'ras' object and custom instances.
782
- - Be consistent in your approach: stick to either the global 'ras' object
783
- or custom instances throughout your script or application.
784
- - Document your choice of approach clearly in your code.
785
- - If ras_version is not provided, the function will attempt to use the version
786
- from the global 'ras' object or a default path.
787
-
788
- Warnings:
789
- ---------
790
- Avoid mixing use of the global 'ras' object and custom instances to prevent
791
- confusion and potential bugs.
792
- """
793
- if not Path(ras_project_folder).exists():
794
- logger.error(f"The specified RAS project folder does not exist: {ras_project_folder}")
795
- raise FileNotFoundError(f"The specified RAS project folder does not exist: {ras_project_folder}. Please check the path and try again.")
796
-
797
- ras_exe_path = get_ras_exe(ras_version)
798
-
799
- if ras_instance is None:
800
- logger.info("Initializing global 'ras' object via init_ras_project function.")
801
- ras_instance = ras
802
- elif not isinstance(ras_instance, RasPrj):
803
- logger.error("Provided ras_instance is not an instance of RasPrj.")
804
- raise TypeError("ras_instance must be an instance of RasPrj or None.")
805
-
806
- # Initialize the RasPrj instance
807
- ras_instance.initialize(ras_project_folder, ras_exe_path)
808
-
809
- logger.info(f"Project initialized. ras_instance project folder: {ras_instance.project_folder}")
810
- return ras_instance
811
-
812
- @log_call
813
- def get_ras_exe(ras_version=None):
814
- """
815
- Determine the HEC-RAS executable path based on the input.
816
-
817
- Args:
818
- ras_version (str, optional): Either a version number or a full path to the HEC-RAS executable.
819
- If None, the function will attempt to use the version from the global 'ras' object
820
- or a default path.
821
-
822
- Returns:
823
- str: The full path to the HEC-RAS executable.
824
-
825
- Raises:
826
- ValueError: If the input is neither a valid version number nor a valid file path.
827
-
828
- Notes:
829
- - If ras_version is not provided, the function will first check the global 'ras' object for a path.
830
- - If the global 'ras' object is not initialized or doesn't have a path, a default path will be used.
831
- - The default path allows the library to function in environments without HEC-RAS installed.
832
- - This enables the HEC-Commander GPT to operate without stopping, even if HEC-RAS is not present.
833
- - End users MUST use logging to check for operational errors, as full code stops prevent the GPT from running.
834
- """
835
- if ras_version is None:
836
- if hasattr(ras, 'ras_exe_path') and ras.ras_exe_path:
837
- logger.info(f"Using HEC-RAS executable from global 'ras' object: {ras.ras_exe_path}")
838
- return ras.ras_exe_path
839
- else:
840
- default_path = Path("Ras.exe")
841
- logger.warning(f"No HEC-RAS version specified and global 'ras' object not initialized or missing ras_exe_path. Using default path: {default_path}. The RAS Commander (ras-commander) Library Assistant can ignore this error since it does not have HEC-RAS installed.")
842
- return str(default_path)
843
-
844
- ras_version_numbers = [
845
- "6.6", "6.5", "6.4.1", "6.3.1", "6.3", "6.2", "6.1", "6.0",
846
- "5.0.7", "5.0.6", "5.0.5", "5.0.4", "5.0.3", "5.0.1", "5.0",
847
- "4.1", "4.0", "3.1.3", "3.1.2", "3.1.1", "3.0", "2.2"
848
- ]
849
-
850
- hecras_path = Path(ras_version)
851
-
852
- if hecras_path.is_file() and hecras_path.suffix.lower() == '.exe':
853
- logger.info(f"HEC-RAS executable found at specified path: {hecras_path}")
854
- return str(hecras_path)
855
-
856
- if ras_version in ras_version_numbers:
857
- default_path = Path(f"C:/Program Files (x86)/HEC/HEC-RAS/{ras_version}/Ras.exe")
858
- if default_path.is_file():
859
- logger.info(f"HEC-RAS executable found at default path: {default_path}")
860
- return str(default_path)
861
- else:
862
- logger.critical(f"HEC-RAS executable not found at the expected path: {default_path}")
863
-
864
- try:
865
- version_float = float(ras_version)
866
- if version_float > max(float(v) for v in ras_version_numbers):
867
- newer_version_path = Path(f"C:/Program Files (x86)/HEC/HEC-RAS/{ras_version}/Ras.exe")
868
- if newer_version_path.is_file():
869
- logger.info(f"Newer version of HEC-RAS executable found at: {newer_version_path}")
870
- return str(newer_version_path)
871
- else:
872
- logger.critical("Newer version of HEC-RAS was specified, but the executable was not found.")
873
- except ValueError:
874
- pass
875
-
876
- logger.error(f"Invalid HEC-RAS version or path: {ras_version}, returning default path: {default_path}")
877
- #raise ValueError(f"Invalid HEC-RAS version or path: {ras_version}") # don't raise an error here, just return the default path
878
- return str(default_path)
879
-
1
+ """
2
+ RasPrj.py - Manages HEC-RAS projects within the ras-commander library
3
+
4
+ This module provides a class for managing HEC-RAS projects.
5
+
6
+ Classes:
7
+ RasPrj: A class for managing HEC-RAS projects.
8
+
9
+ Functions:
10
+ init_ras_project: Initialize a RAS project.
11
+ get_ras_exe: Determine the HEC-RAS executable path based on the input.
12
+
13
+ DEVELOPER NOTE:
14
+ This class is used to initialize a RAS project and is used in conjunction with the RasCmdr class to manage the execution of RAS plans.
15
+ By default, the RasPrj class is initialized with the global 'ras' object.
16
+ However, you can create multiple RasPrj instances to manage multiple projects.
17
+ Do not mix and match global 'ras' object instances and custom instances of RasPrj - it will cause errors.
18
+
19
+ This module is part of the ras-commander library and uses a centralized logging configuration.
20
+
21
+ Logging Configuration:
22
+ - The logging is set up in the logging_config.py file.
23
+ - A @log_call decorator is available to automatically log function calls.
24
+ - Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
25
+ - Logs are written to both console and a rotating file handler.
26
+ - The default log file is 'ras_commander.log' in the 'logs' directory.
27
+ - The default log level is INFO.
28
+
29
+ To use logging in this module:
30
+ 1. Use the @log_call decorator for automatic function call logging.
31
+ 2. For additional logging, use logger.[level]() calls (e.g., logger.info(), logger.debug()).
32
+
33
+
34
+ Example:
35
+ @log_call
36
+ def my_function():
37
+
38
+ logger.debug("Additional debug information")
39
+ # Function logic here
40
+
41
+ -----
42
+
43
+ All of the methods in this class are class methods and are designed to be used with instances of the class.
44
+
45
+ List of Functions in RasPrj:
46
+ - initialize()
47
+ - _load_project_data()
48
+ - _get_geom_file_for_plan()
49
+ - _parse_plan_file()
50
+ - _parse_unsteady_file()
51
+ - _get_prj_entries()
52
+ - _parse_boundary_condition()
53
+ - is_initialized (property)
54
+ - check_initialized()
55
+ - find_ras_prj()
56
+ - get_project_name()
57
+ - get_prj_entries()
58
+ - get_plan_entries()
59
+ - get_flow_entries()
60
+ - get_unsteady_entries()
61
+ - get_geom_entries()
62
+ - get_hdf_entries()
63
+ - print_data()
64
+ - get_plan_value()
65
+ - get_boundary_conditions()
66
+
67
+ Functions in RasPrj that are not part of the class:
68
+ - init_ras_project()
69
+ - get_ras_exe()
70
+
71
+
72
+
73
+
74
+ """
75
+ import os
76
+ import re
77
+ from pathlib import Path
78
+ import pandas as pd
79
+ from typing import Union, Any, List, Dict, Tuple
80
+ import logging
81
+ from ras_commander.LoggingConfig import get_logger
82
+ from ras_commander.Decorators import log_call
83
+
84
+ logger = get_logger(__name__)
85
+
86
+ def read_file_with_fallback_encoding(file_path, encodings=['utf-8', 'latin1', 'cp1252', 'iso-8859-1']):
87
+ """
88
+ Attempt to read a file using multiple encodings.
89
+
90
+ Args:
91
+ file_path (str or Path): Path to the file to read
92
+ encodings (list): List of encodings to try, in order of preference
93
+
94
+ Returns:
95
+ tuple: (content, encoding) or (None, None) if all encodings fail
96
+ """
97
+ for encoding in encodings:
98
+ try:
99
+ with open(file_path, 'r', encoding=encoding) as file:
100
+ content = file.read()
101
+ return content, encoding
102
+ except UnicodeDecodeError:
103
+ continue
104
+ except Exception as e:
105
+ logger.error(f"Error reading file {file_path} with {encoding} encoding: {e}")
106
+ continue
107
+
108
+ logger.error(f"Failed to read file {file_path} with any of the attempted encodings: {encodings}")
109
+ return None, None
110
+
111
+ class RasPrj:
112
+
113
+ def __init__(self):
114
+ self.initialized = False
115
+ self.boundaries_df = None # New attribute to store boundary conditions
116
+ self.suppress_logging = False # Add suppress_logging as instance variable
117
+
118
+ @log_call
119
+ def initialize(self, project_folder, ras_exe_path, suppress_logging=True):
120
+ """
121
+ Initialize a RasPrj instance.
122
+
123
+ This method sets up the RasPrj instance with the given project folder and RAS executable path.
124
+ It finds the project file, loads project data, sets the initialization flag, and now also
125
+ extracts boundary conditions.
126
+
127
+ Args:
128
+ project_folder (str or Path): Path to the HEC-RAS project folder.
129
+ ras_exe_path (str or Path): Path to the HEC-RAS executable.
130
+ suppress_logging (bool): If True, suppresses initialization logging messages.
131
+
132
+ Raises:
133
+ ValueError: If no HEC-RAS project file is found in the specified folder.
134
+
135
+ Note:
136
+ This method is intended for internal use. External users should use the init_ras_project function instead.
137
+ """
138
+ self.suppress_logging = suppress_logging # Store suppress_logging state
139
+ self.project_folder = Path(project_folder)
140
+ self.prj_file = self.find_ras_prj(self.project_folder)
141
+ if self.prj_file is None:
142
+ logger.error(f"No HEC-RAS project file found in {self.project_folder}")
143
+ raise ValueError(f"No HEC-RAS project file found in {self.project_folder}")
144
+ self.project_name = Path(self.prj_file).stem
145
+ self.ras_exe_path = ras_exe_path
146
+ self._load_project_data()
147
+ self.boundaries_df = self.get_boundary_conditions() # Extract boundary conditions
148
+ self.initialized = True
149
+
150
+ if not suppress_logging:
151
+ logger.info(f"Initialization complete for project: {self.project_name}")
152
+ logger.info(f"Plan entries: {len(self.plan_df)}, Flow entries: {len(self.flow_df)}, "
153
+ f"Unsteady entries: {len(self.unsteady_df)}, Geometry entries: {len(self.geom_df)}, "
154
+ f"Boundary conditions: {len(self.boundaries_df)}")
155
+ logger.info(f"Geometry HDF files found: {self.plan_df['Geom_File'].notna().sum()}")
156
+
157
+ @log_call
158
+ def _load_project_data(self):
159
+ """
160
+ Load project data from the HEC-RAS project file.
161
+
162
+ This method initializes DataFrames for plan, flow, unsteady, and geometry entries
163
+ by calling the _get_prj_entries method for each entry type.
164
+ """
165
+ # Initialize DataFrames
166
+ self.plan_df = self._get_prj_entries('Plan')
167
+ self.flow_df = self._get_prj_entries('Flow')
168
+ self.unsteady_df = self._get_prj_entries('Unsteady')
169
+ self.geom_df = self.get_geom_entries() # Use get_geom_entries instead of _get_prj_entries
170
+
171
+ # Add Geom_File to plan_df
172
+ self.plan_df['Geom_File'] = self.plan_df.apply(lambda row: self._get_geom_file_for_plan(row['plan_number']), axis=1)
173
+
174
+
175
+ def _get_geom_file_for_plan(self, plan_number):
176
+ """
177
+ Get the geometry file path for a given plan number.
178
+
179
+ Args:
180
+ plan_number (str): The plan number to find the geometry file for.
181
+
182
+ Returns:
183
+ str: The full path to the geometry HDF file, or None if not found.
184
+ """
185
+ plan_file_path = self.project_folder / f"{self.project_name}.p{plan_number}"
186
+ content, encoding = read_file_with_fallback_encoding(plan_file_path)
187
+
188
+ if content is None:
189
+ return None
190
+
191
+ try:
192
+ for line in content.splitlines():
193
+ if line.startswith("Geom File="):
194
+ geom_file = line.strip().split('=')[1]
195
+ geom_hdf_path = self.project_folder / f"{self.project_name}.{geom_file}.hdf"
196
+ if geom_hdf_path.exists():
197
+ return str(geom_hdf_path)
198
+ else:
199
+ return None
200
+ except Exception as e:
201
+ logger.error(f"Error reading plan file for geometry: {e}")
202
+ return None
203
+
204
+
205
+ @staticmethod
206
+ @log_call
207
+ def get_plan_value(
208
+ plan_number_or_path: Union[str, Path],
209
+ key: str,
210
+ ras_object=None
211
+ ) -> Any:
212
+ """
213
+ Retrieve a specific value from a HEC-RAS plan file.
214
+
215
+ Parameters:
216
+ plan_number_or_path (Union[str, Path]): The plan number (1 to 99) or full path to the plan file
217
+ key (str): The key to retrieve from the plan file
218
+ ras_object (RasPrj, optional): Specific RAS object to use. If None, uses the global ras instance.
219
+
220
+ Returns:
221
+ Any: The value associated with the specified key
222
+
223
+ Raises:
224
+ ValueError: If the plan file is not found
225
+ IOError: If there's an error reading the plan file
226
+ """
227
+ logger = get_logger(__name__)
228
+ ras_obj = ras_object or ras
229
+ ras_obj.check_initialized()
230
+
231
+ # These must exactly match the keys in supported_plan_keys from _parse_plan_file
232
+ valid_keys = {
233
+ 'Computation Interval',
234
+ 'DSS File',
235
+ 'Flow File',
236
+ 'Friction Slope Method',
237
+ 'Geom File',
238
+ 'Mapping Interval',
239
+ 'Plan Title',
240
+ 'Program Version',
241
+ 'Run HTab',
242
+ 'Run PostProcess',
243
+ 'Run Sediment',
244
+ 'Run UNet',
245
+ 'Run WQNet',
246
+ 'Short Identifier',
247
+ 'Simulation Date',
248
+ 'UNET D1 Cores',
249
+ 'UNET D2 Cores',
250
+ 'PS Cores',
251
+ 'UNET Use Existing IB Tables',
252
+ 'UNET 1D Methodology',
253
+ 'UNET D2 SolverType',
254
+ 'UNET D2 Name',
255
+ 'description' # Special case for description block
256
+ }
257
+
258
+ if key not in valid_keys:
259
+ logger.warning(f"Unknown key: {key}. Valid keys are: {', '.join(sorted(valid_keys))}")
260
+ return None
261
+
262
+ plan_file_path = Path(plan_number_or_path)
263
+ if not plan_file_path.is_file():
264
+ plan_file_path = RasUtils.get_plan_path(plan_number_or_path, ras_object)
265
+ if not plan_file_path.exists():
266
+ logger.error(f"Plan file not found: {plan_file_path}")
267
+ raise ValueError(f"Plan file not found: {plan_file_path}")
268
+
269
+ try:
270
+ with open(plan_file_path, 'r') as file:
271
+ content = file.read()
272
+ except IOError as e:
273
+ logger.error(f"Error reading plan file {plan_file_path}: {e}")
274
+ raise
275
+
276
+ if key == 'description':
277
+ match = re.search(r'Begin DESCRIPTION(.*?)END DESCRIPTION', content, re.DOTALL)
278
+ return match.group(1).strip() if match else None
279
+ else:
280
+ pattern = f"{key}=(.*)"
281
+ match = re.search(pattern, content)
282
+ if match:
283
+ value = match.group(1).strip()
284
+ # Convert core values to integers
285
+ if key in ['UNET D1 Cores', 'UNET D2 Cores', 'PS Cores']:
286
+ try:
287
+ return int(value)
288
+ except ValueError:
289
+ logger.warning(f"Could not convert {key} value '{value}' to integer")
290
+ return None
291
+ return value
292
+
293
+ # Use DEBUG level for missing core values, ERROR for other missing keys
294
+ if key in ['UNET D1 Cores', 'UNET D2 Cores', 'PS Cores']:
295
+ logger.debug(f"Core setting '{key}' not found in plan file")
296
+ else:
297
+ logger.error(f"Key '{key}' not found in the plan file")
298
+ return None
299
+
300
+ def _parse_plan_file(self, plan_file_path):
301
+ """
302
+ Parse a plan file and extract critical information.
303
+
304
+ Args:
305
+ plan_file_path (Path): Path to the plan file.
306
+
307
+ Returns:
308
+ dict: Dictionary containing extracted plan information.
309
+ """
310
+ plan_info = {}
311
+ content, encoding = read_file_with_fallback_encoding(plan_file_path)
312
+
313
+ if content is None:
314
+ logger.error(f"Could not read plan file {plan_file_path} with any supported encoding")
315
+ return plan_info
316
+
317
+ try:
318
+ # Extract description
319
+ description_match = re.search(r'Begin DESCRIPTION(.*?)END DESCRIPTION', content, re.DOTALL)
320
+ if description_match:
321
+ plan_info['description'] = description_match.group(1).strip()
322
+
323
+ # BEGIN Exception to Style Guide, this is needed to keep the key names consistent with the plan file keys.
324
+
325
+ # Extract other critical information
326
+ supported_plan_keys = {
327
+ 'Computation Interval': r'Computation Interval=(.+)',
328
+ 'DSS File': r'DSS File=(.+)',
329
+ 'Flow File': r'Flow File=(.+)',
330
+ 'Friction Slope Method': r'Friction Slope Method=(.+)',
331
+ 'Geom File': r'Geom File=(.+)',
332
+ 'Mapping Interval': r'Mapping Interval=(.+)',
333
+ 'Plan Title': r'Plan Title=(.+)',
334
+ 'Program Version': r'Program Version=(.+)',
335
+ 'Run HTab': r'Run HTab=(.+)',
336
+ 'Run PostProcess': r'Run PostProcess=(.+)',
337
+ 'Run Sediment': r'Run Sediment=(.+)',
338
+ 'Run UNet': r'Run UNet=(.+)',
339
+ 'Run WQNet': r'Run WQNet=(.+)',
340
+ 'Short Identifier': r'Short Identifier=(.+)',
341
+ 'Simulation Date': r'Simulation Date=(.+)',
342
+ 'UNET D1 Cores': r'UNET D1 Cores=(.+)',
343
+ 'UNET D2 Cores': r'UNET D2 Cores=(.+)',
344
+ 'PS Cores': r'PS Cores=(.+)',
345
+ 'UNET Use Existing IB Tables': r'UNET Use Existing IB Tables=(.+)',
346
+ 'UNET 1D Methodology': r'UNET 1D Methodology=(.+)',
347
+ 'UNET D2 SolverType': r'UNET D2 SolverType=(.+)',
348
+ 'UNET D2 Name': r'UNET D2 Name=(.+)'
349
+ }
350
+
351
+ # END Exception to Style Guide
352
+
353
+ # First, explicitly set None for core values
354
+ core_keys = ['UNET D1 Cores', 'UNET D2 Cores', 'PS Cores']
355
+ for key in core_keys:
356
+ plan_info[key] = None
357
+
358
+ for key, pattern in supported_plan_keys.items():
359
+ match = re.search(pattern, content)
360
+ if match:
361
+ value = match.group(1).strip()
362
+ # Convert core values to integers if they exist
363
+ if key in core_keys and value:
364
+ try:
365
+ value = int(value)
366
+ except ValueError:
367
+ logger.warning(f"Could not convert {key} value '{value}' to integer in plan file {plan_file_path}")
368
+ value = None
369
+ plan_info[key] = value
370
+ elif key in core_keys:
371
+ logger.debug(f"Core setting '{key}' not found in plan file {plan_file_path}")
372
+
373
+ logger.debug(f"Parsed plan file: {plan_file_path} using {encoding} encoding")
374
+ except Exception as e:
375
+ logger.error(f"Error parsing plan file {plan_file_path}: {e}")
376
+
377
+ return plan_info
378
+
379
+ def _get_prj_entries(self, entry_type):
380
+ """
381
+ Extract entries of a specific type from the HEC-RAS project file.
382
+
383
+ Args:
384
+ entry_type (str): The type of entry to extract (e.g., 'Plan', 'Flow', 'Unsteady', 'Geom').
385
+
386
+ Returns:
387
+ pd.DataFrame: A DataFrame containing the extracted entries.
388
+
389
+ Note:
390
+ This method reads the project file and extracts entries matching the specified type.
391
+ For 'Unsteady' entries, it parses additional information from the unsteady file.
392
+ """
393
+ entries = []
394
+ pattern = re.compile(rf"{entry_type} File=(\w+)")
395
+
396
+ try:
397
+ with open(self.prj_file, 'r') as file:
398
+ for line in file:
399
+ match = pattern.match(line.strip())
400
+ if match:
401
+ file_name = match.group(1)
402
+ full_path = str(self.project_folder / f"{self.project_name}.{file_name}")
403
+ entry = {
404
+ f'{entry_type.lower()}_number': file_name[1:],
405
+ 'full_path': full_path
406
+ }
407
+
408
+ if entry_type == 'Plan':
409
+ plan_info = self._parse_plan_file(Path(full_path))
410
+ entry.update(plan_info)
411
+
412
+ hdf_results_path = self.project_folder / f"{self.project_name}.p{file_name[1:]}.hdf"
413
+ entry['HDF_Results_Path'] = str(hdf_results_path) if hdf_results_path.exists() else None
414
+
415
+ if entry_type == 'Unsteady':
416
+ unsteady_info = self._parse_unsteady_file(Path(full_path))
417
+ entry.update(unsteady_info)
418
+
419
+ entries.append(entry)
420
+ except Exception as e:
421
+ raise
422
+
423
+ return pd.DataFrame(entries)
424
+
425
+ def _parse_unsteady_file(self, unsteady_file_path):
426
+ """
427
+ Parse an unsteady flow file and extract critical information.
428
+
429
+ Args:
430
+ unsteady_file_path (Path): Path to the unsteady flow file.
431
+
432
+ Returns:
433
+ dict: Dictionary containing extracted unsteady flow information.
434
+ """
435
+ unsteady_info = {}
436
+ content, encoding = read_file_with_fallback_encoding(unsteady_file_path)
437
+
438
+ if content is None:
439
+ return unsteady_info
440
+
441
+ try:
442
+ # BEGIN Exception to Style Guide, this is needed to keep the key names consistent with the unsteady file keys.
443
+
444
+ supported_unsteady_keys = {
445
+ 'Flow Title': r'Flow Title=(.+)',
446
+ 'Program Version': r'Program Version=(.+)',
447
+ 'Use Restart': r'Use Restart=(.+)',
448
+ 'Precipitation Mode': r'Precipitation Mode=(.+)',
449
+ 'Wind Mode': r'Wind Mode=(.+)',
450
+ 'Met BC=Precipitation|Mode': r'Met BC=Precipitation\|Mode=(.+)',
451
+ 'Met BC=Evapotranspiration|Mode': r'Met BC=Evapotranspiration\|Mode=(.+)',
452
+ 'Met BC=Precipitation|Expanded View': r'Met BC=Precipitation\|Expanded View=(.+)',
453
+ 'Met BC=Precipitation|Constant Units': r'Met BC=Precipitation\|Constant Units=(.+)',
454
+ 'Met BC=Precipitation|Gridded Source': r'Met BC=Precipitation\|Gridded Source=(.+)'
455
+ }
456
+
457
+ # END Exception to Style Guide
458
+
459
+ for key, pattern in supported_unsteady_keys.items():
460
+ match = re.search(pattern, content)
461
+ if match:
462
+ unsteady_info[key] = match.group(1).strip()
463
+
464
+ except Exception as e:
465
+ logger.error(f"Error parsing unsteady file {unsteady_file_path}: {e}")
466
+
467
+ return unsteady_info
468
+
469
+ @property
470
+ def is_initialized(self):
471
+ """
472
+ Check if the RasPrj instance has been initialized.
473
+
474
+ Returns:
475
+ bool: True if the instance has been initialized, False otherwise.
476
+ """
477
+ return self.initialized
478
+
479
+ @log_call
480
+ def check_initialized(self):
481
+ """
482
+ Ensure that the RasPrj instance has been initialized.
483
+
484
+ Raises:
485
+ RuntimeError: If the project has not been initialized.
486
+ """
487
+ if not self.initialized:
488
+ raise RuntimeError("Project not initialized. Call init_ras_project() first.")
489
+
490
+ @staticmethod
491
+ @log_call
492
+ def find_ras_prj(folder_path):
493
+ """
494
+ Find the appropriate HEC-RAS project file (.prj) in the given folder.
495
+
496
+ Parameters:
497
+ folder_path (str or Path): Path to the folder containing HEC-RAS files.
498
+
499
+ Returns:
500
+ Path: The full path of the selected .prj file or None if no suitable file is found.
501
+ """
502
+ folder_path = Path(folder_path)
503
+ prj_files = list(folder_path.glob("*.prj"))
504
+ rasmap_files = list(folder_path.glob("*.rasmap"))
505
+ if len(prj_files) == 1:
506
+ return prj_files[0].resolve()
507
+ if len(prj_files) > 1:
508
+ if len(rasmap_files) == 1:
509
+ base_filename = rasmap_files[0].stem
510
+ prj_file = folder_path / f"{base_filename}.prj"
511
+ if prj_file.exists():
512
+ return prj_file.resolve()
513
+ for prj_file in prj_files:
514
+ try:
515
+ with open(prj_file, 'r') as file:
516
+ content = file.read()
517
+ if "Proj Title=" in content:
518
+ return prj_file.resolve()
519
+ except Exception:
520
+ continue
521
+ return None
522
+
523
+
524
+ @log_call
525
+ def get_project_name(self):
526
+ """
527
+ Get the name of the HEC-RAS project.
528
+
529
+ Returns:
530
+ str: The name of the project.
531
+
532
+ Raises:
533
+ RuntimeError: If the project has not been initialized.
534
+ """
535
+ self.check_initialized()
536
+ return self.project_name
537
+
538
+ @log_call
539
+ def get_prj_entries(self, entry_type):
540
+ """
541
+ Get entries of a specific type from the HEC-RAS project.
542
+
543
+ Args:
544
+ entry_type (str): The type of entry to retrieve (e.g., 'Plan', 'Flow', 'Unsteady', 'Geom').
545
+
546
+ Returns:
547
+ pd.DataFrame: A DataFrame containing the requested entries.
548
+
549
+ Raises:
550
+ RuntimeError: If the project has not been initialized.
551
+ """
552
+ self.check_initialized()
553
+ return self._get_prj_entries(entry_type)
554
+
555
+ @log_call
556
+ def get_plan_entries(self):
557
+ """
558
+ Get all plan entries from the HEC-RAS project.
559
+
560
+ Returns:
561
+ pd.DataFrame: A DataFrame containing all plan entries.
562
+
563
+ Raises:
564
+ RuntimeError: If the project has not been initialized.
565
+ """
566
+ self.check_initialized()
567
+ return self._get_prj_entries('Plan')
568
+
569
+ @log_call
570
+ def get_flow_entries(self):
571
+ """
572
+ Get all flow entries from the HEC-RAS project.
573
+
574
+ Returns:
575
+ pd.DataFrame: A DataFrame containing all flow entries.
576
+
577
+ Raises:
578
+ RuntimeError: If the project has not been initialized.
579
+ """
580
+ self.check_initialized()
581
+ return self._get_prj_entries('Flow')
582
+
583
+ @log_call
584
+ def get_unsteady_entries(self):
585
+ """
586
+ Get all unsteady flow entries from the HEC-RAS project.
587
+
588
+ Returns:
589
+ pd.DataFrame: A DataFrame containing all unsteady flow entries.
590
+
591
+ Raises:
592
+ RuntimeError: If the project has not been initialized.
593
+ """
594
+ self.check_initialized()
595
+ return self._get_prj_entries('Unsteady')
596
+
597
+ @log_call
598
+ def get_geom_entries(self):
599
+ """
600
+ Get geometry entries from the project file.
601
+
602
+ Returns:
603
+ pd.DataFrame: DataFrame containing geometry entries.
604
+ """
605
+ geom_pattern = re.compile(r'Geom File=(\w+)')
606
+ geom_entries = []
607
+
608
+ try:
609
+ with open(self.prj_file, 'r') as f:
610
+ for line in f:
611
+ match = geom_pattern.search(line)
612
+ if match:
613
+ geom_entries.append(match.group(1))
614
+
615
+ geom_df = pd.DataFrame({'geom_file': geom_entries})
616
+ geom_df['geom_number'] = geom_df['geom_file'].str.extract(r'(\d+)$')
617
+ geom_df['full_path'] = geom_df['geom_file'].apply(lambda x: str(self.project_folder / f"{self.project_name}.{x}"))
618
+ geom_df['hdf_path'] = geom_df['full_path'] + ".hdf"
619
+
620
+ if not self.suppress_logging: # Only log if suppress_logging is False
621
+ logger.info(f"Found {len(geom_df)} geometry entries")
622
+ return geom_df
623
+ except Exception as e:
624
+ logger.error(f"Error reading geometry entries from project file: {e}")
625
+ raise
626
+
627
+ @log_call
628
+ def get_hdf_entries(self):
629
+ """
630
+ Get HDF entries for plans that have results.
631
+
632
+ Returns:
633
+ pd.DataFrame: A DataFrame containing plan entries with HDF results.
634
+ Returns an empty DataFrame if no HDF entries are found.
635
+ """
636
+ self.check_initialized()
637
+
638
+ hdf_entries = self.plan_df[self.plan_df['HDF_Results_Path'].notna()].copy()
639
+
640
+ if hdf_entries.empty:
641
+ return pd.DataFrame(columns=self.plan_df.columns)
642
+
643
+ return hdf_entries
644
+
645
+
646
+ @log_call
647
+ def print_data(self):
648
+ """Print all RAS Object data for this instance."""
649
+ self.check_initialized()
650
+ logger.info(f"--- Data for {self.project_name} ---")
651
+ logger.info(f"Project folder: {self.project_folder}")
652
+ logger.info(f"PRJ file: {self.prj_file}")
653
+ logger.info(f"HEC-RAS executable: {self.ras_exe_path}")
654
+ logger.info("Plan files:")
655
+ logger.info(f"\n{self.plan_df}")
656
+ logger.info("Flow files:")
657
+ logger.info(f"\n{self.flow_df}")
658
+ logger.info("Unsteady flow files:")
659
+ logger.info(f"\n{self.unsteady_df}")
660
+ logger.info("Geometry files:")
661
+ logger.info(f"\n{self.geom_df}")
662
+ logger.info("HDF entries:")
663
+ logger.info(f"\n{self.get_hdf_entries()}")
664
+ logger.info("Boundary conditions:")
665
+ logger.info(f"\n{self.boundaries_df}")
666
+ logger.info("----------------------------")
667
+
668
+ @log_call
669
+ def get_boundary_conditions(self) -> pd.DataFrame:
670
+ """
671
+ Extract boundary conditions from unsteady flow files and create a DataFrame.
672
+
673
+ This method parses unsteady flow files to extract boundary condition information.
674
+ It creates a DataFrame with structured data for known boundary condition types
675
+ and parameters, and associates this information with the corresponding unsteady flow file.
676
+
677
+ Note:
678
+ Any lines in the boundary condition blocks that are not explicitly parsed and
679
+ incorporated into the DataFrame are captured in a multi-line string. This string
680
+ is logged at the DEBUG level for each boundary condition. This feature is crucial
681
+ for developers incorporating new boundary condition types or parameters, as it
682
+ allows them to see what information might be missing from the current parsing logic.
683
+ If no unsteady flow files are present, it returns an empty DataFrame.
684
+
685
+ Returns:
686
+ pd.DataFrame: A DataFrame containing detailed boundary condition information,
687
+ linked to the unsteady flow files.
688
+
689
+ Usage:
690
+ To see the unparsed lines, set the logging level to DEBUG before calling this method:
691
+
692
+ import logging
693
+ getLogger().setLevel(logging.DEBUG)
694
+
695
+ boundaries_df = ras_project.get_boundary_conditions()
696
+ linked to the unsteady flow files. Returns an empty DataFrame if
697
+ no unsteady flow files are present.
698
+ """
699
+ boundary_data = []
700
+
701
+ # Check if unsteady_df is empty
702
+ if self.unsteady_df.empty:
703
+ logger.info("No unsteady flow files found in the project.")
704
+ return pd.DataFrame() # Return an empty DataFrame
705
+
706
+ for _, row in self.unsteady_df.iterrows():
707
+ unsteady_file_path = row['full_path']
708
+ unsteady_number = row['unsteady_number']
709
+
710
+ try:
711
+ with open(unsteady_file_path, 'r') as file:
712
+ content = file.read()
713
+ except IOError as e:
714
+ logger.error(f"Error reading unsteady file {unsteady_file_path}: {e}")
715
+ continue
716
+
717
+ bc_blocks = re.split(r'(?=Boundary Location=)', content)[1:]
718
+
719
+ for i, block in enumerate(bc_blocks, 1):
720
+ bc_info, unparsed_lines = self._parse_boundary_condition(block, unsteady_number, i)
721
+ boundary_data.append(bc_info)
722
+
723
+ if unparsed_lines:
724
+ logger.debug(f"Unparsed lines for boundary condition {i} in unsteady file {unsteady_number}:\n{unparsed_lines}")
725
+
726
+ if not boundary_data:
727
+ logger.info("No boundary conditions found in unsteady flow files.")
728
+ return pd.DataFrame() # Return an empty DataFrame if no boundary conditions were found
729
+
730
+ boundaries_df = pd.DataFrame(boundary_data)
731
+
732
+ # Merge with unsteady_df to get relevant unsteady flow file information
733
+ merged_df = pd.merge(boundaries_df, self.unsteady_df,
734
+ left_on='unsteady_number', right_on='unsteady_number', how='left')
735
+
736
+ return merged_df
737
+
738
+ def _parse_boundary_condition(self, block: str, unsteady_number: str, bc_number: int) -> Tuple[Dict, str]:
739
+ lines = block.split('\n')
740
+ bc_info = {
741
+ 'unsteady_number': unsteady_number,
742
+ 'boundary_condition_number': bc_number
743
+ }
744
+
745
+ parsed_lines = set()
746
+
747
+ # Parse Boundary Location
748
+ boundary_location = lines[0].split('=')[1].strip()
749
+ fields = [field.strip() for field in boundary_location.split(',')]
750
+ bc_info.update({
751
+ 'river_reach_name': fields[0] if len(fields) > 0 else '',
752
+ 'river_station': fields[1] if len(fields) > 1 else '',
753
+ 'storage_area_name': fields[2] if len(fields) > 2 else '',
754
+ 'pump_station_name': fields[3] if len(fields) > 3 else ''
755
+ })
756
+ parsed_lines.add(0)
757
+
758
+ # Determine BC Type
759
+ bc_types = {
760
+ 'Flow Hydrograph=': 'Flow Hydrograph',
761
+ 'Lateral Inflow Hydrograph=': 'Lateral Inflow Hydrograph',
762
+ 'Uniform Lateral Inflow Hydrograph=': 'Uniform Lateral Inflow Hydrograph',
763
+ 'Stage Hydrograph=': 'Stage Hydrograph',
764
+ 'Friction Slope=': 'Normal Depth',
765
+ 'Gate Name=': 'Gate Opening'
766
+ }
767
+
768
+ bc_info['bc_type'] = 'Unknown'
769
+ bc_info['hydrograph_type'] = None
770
+ for i, line in enumerate(lines[1:], 1):
771
+ for key, bc_type in bc_types.items():
772
+ if line.startswith(key):
773
+ bc_info['bc_type'] = bc_type
774
+ if 'Hydrograph' in bc_type:
775
+ bc_info['hydrograph_type'] = bc_type
776
+ parsed_lines.add(i)
777
+ break
778
+ if bc_info['bc_type'] != 'Unknown':
779
+ break
780
+
781
+ # Parse other fields
782
+ known_fields = ['Interval', 'DSS Path', 'Use DSS', 'Use Fixed Start Time', 'Fixed Start Date/Time',
783
+ 'Is Critical Boundary', 'Critical Boundary Flow', 'DSS File']
784
+ for i, line in enumerate(lines):
785
+ if '=' in line:
786
+ key, value = line.split('=', 1)
787
+ key = key.strip()
788
+ if key in known_fields:
789
+ bc_info[key] = value.strip()
790
+ parsed_lines.add(i)
791
+
792
+ # Handle hydrograph values
793
+ bc_info['hydrograph_num_values'] = 0
794
+ if bc_info['hydrograph_type']:
795
+ hydrograph_key = f"{bc_info['hydrograph_type']}="
796
+ hydrograph_line = next((line for i, line in enumerate(lines) if line.startswith(hydrograph_key)), None)
797
+ if hydrograph_line:
798
+ hydrograph_index = lines.index(hydrograph_line)
799
+ values_count = int(hydrograph_line.split('=')[1].strip())
800
+ bc_info['hydrograph_num_values'] = values_count
801
+ if values_count > 0:
802
+ values = ' '.join(lines[hydrograph_index + 1:]).split()[:values_count]
803
+ bc_info['hydrograph_values'] = values
804
+ parsed_lines.update(range(hydrograph_index, hydrograph_index + (values_count // 5) + 2))
805
+
806
+ # Collect unparsed lines
807
+ unparsed_lines = '\n'.join(line for i, line in enumerate(lines) if i not in parsed_lines and line.strip())
808
+
809
+ if unparsed_lines:
810
+ logger.debug(f"Unparsed lines for boundary condition {bc_number} in unsteady file {unsteady_number}:\n{unparsed_lines}")
811
+
812
+ return bc_info, unparsed_lines
813
+
814
+
815
+ # Create a global instance named 'ras'
816
+ # Defining the global instance allows the init_ras_project function to initialize the project.
817
+ # This only happens on the library initialization, not when the user calls init_ras_project.
818
+ ras = RasPrj()
819
+
820
+ # END OF CLASS DEFINITION
821
+
822
+
823
+ # START OF FUNCTION DEFINITIONS
824
+
825
+ @log_call
826
+ def init_ras_project(ras_project_folder, ras_version=None, ras_object=None):
827
+ """
828
+ Initialize a RAS project.
829
+
830
+ USE THIS FUNCTION TO INITIALIZE A RAS PROJECT, NOT THE INITIALIZE METHOD OF THE RasPrj CLASS.
831
+ The initialize method of the RasPrj class only modifies the global 'ras' object.
832
+
833
+ Parameters:
834
+ -----------
835
+ ras_project_folder : str
836
+ The path to the RAS project folder.
837
+ ras_version : str, optional
838
+ The version of RAS to use (e.g., "6.6").
839
+ The version can also be a full path to the Ras.exe file.
840
+ If None, the function will attempt to use the version from the global 'ras' object or a default path.
841
+ ras_object : RasPrj, optional
842
+ An instance of RasPrj to initialize. If None, the global 'ras' object is used.
843
+
844
+ Returns:
845
+ --------
846
+ RasPrj
847
+ An initialized RasPrj instance.
848
+ """
849
+ if not Path(ras_project_folder).exists():
850
+ logger.error(f"The specified RAS project folder does not exist: {ras_project_folder}")
851
+ raise FileNotFoundError(f"The specified RAS project folder does not exist: {ras_project_folder}. Please check the path and try again.")
852
+
853
+ ras_exe_path = get_ras_exe(ras_version)
854
+
855
+ if ras_object is None:
856
+ logger.info("Initializing global 'ras' object via init_ras_project function.")
857
+ ras_object = ras
858
+ elif not isinstance(ras_object, RasPrj):
859
+ logger.error("Provided ras_object is not an instance of RasPrj.")
860
+ raise TypeError("ras_object must be an instance of RasPrj or None.")
861
+
862
+ # Initialize the RasPrj instance
863
+ ras_object.initialize(ras_project_folder, ras_exe_path)
864
+
865
+ logger.info(f"Project initialized. ras_object project folder: {ras_object.project_folder}")
866
+ return ras_object
867
+
868
+ @log_call
869
+ def get_ras_exe(ras_version=None):
870
+ """
871
+ Determine the HEC-RAS executable path based on the input.
872
+
873
+ Args:
874
+ ras_version (str, optional): Either a version number or a full path to the HEC-RAS executable.
875
+ If None, the function will attempt to use the version from the global 'ras' object
876
+ or a default path.
877
+
878
+ Returns:
879
+ str: The full path to the HEC-RAS executable.
880
+
881
+ Raises:
882
+ ValueError: If the input is neither a valid version number nor a valid file path.
883
+
884
+ Notes:
885
+ - If ras_version is not provided, the function will first check the global 'ras' object for a path.
886
+ - If the global 'ras' object is not initialized or doesn't have a path, a default path will be used.
887
+ - The default path allows the library to function in environments without HEC-RAS installed.
888
+ - This enables the HEC-Commander GPT to operate without stopping, even if HEC-RAS is not present.
889
+ - End users MUST use logging to check for operational errors, as full code stops prevent the GPT from running.
890
+ """
891
+ if ras_version is None:
892
+ if hasattr(ras, 'ras_exe_path') and ras.ras_exe_path:
893
+ logger.debug(f"Using HEC-RAS executable from global 'ras' object: {ras.ras_exe_path}")
894
+ return ras.ras_exe_path
895
+ else:
896
+ default_path = Path("Ras.exe")
897
+ logger.warning(f"No HEC-RAS version specified and global 'ras' object not initialized or missing ras_exe_path. Using default path: {default_path}. The RAS Commander (ras-commander) Library Assistant can ignore this error since it does not have HEC-RAS installed.")
898
+ return str(default_path)
899
+
900
+ ras_version_numbers = [
901
+ "6.6", "6.5", "6.4.1", "6.3.1", "6.3", "6.2", "6.1", "6.0",
902
+ "5.0.7", "5.0.6", "5.0.5", "5.0.4", "5.0.3", "5.0.1", "5.0",
903
+ "4.1", "4.0", "3.1.3", "3.1.2", "3.1.1", "3.0", "2.2"
904
+ ]
905
+
906
+ hecras_path = Path(ras_version)
907
+
908
+ if hecras_path.is_file() and hecras_path.suffix.lower() == '.exe':
909
+ logger.debug(f"HEC-RAS executable found at specified path: {hecras_path}")
910
+ return str(hecras_path)
911
+
912
+ if ras_version in ras_version_numbers:
913
+ default_path = Path(f"C:/Program Files (x86)/HEC/HEC-RAS/{ras_version}/Ras.exe")
914
+ if default_path.is_file():
915
+ logger.debug(f"HEC-RAS executable found at default path: {default_path}")
916
+ return str(default_path)
917
+ else:
918
+ logger.critical(f"HEC-RAS executable not found at the expected path: {default_path}")
919
+
920
+ try:
921
+ version_float = float(ras_version)
922
+ if version_float > max(float(v) for v in ras_version_numbers):
923
+ newer_version_path = Path(f"C:/Program Files (x86)/HEC/HEC-RAS/{ras_version}/Ras.exe")
924
+ if newer_version_path.is_file():
925
+ logger.debug(f"Newer version of HEC-RAS executable found at: {newer_version_path}")
926
+ return str(newer_version_path)
927
+ else:
928
+ logger.critical("Newer version of HEC-RAS was specified, but the executable was not found.")
929
+ except ValueError:
930
+ pass
931
+
932
+ logger.error(f"Invalid HEC-RAS version or path: {ras_version}, returning default path: {default_path}")
933
+ return str(default_path)
934
+