ras-commander 0.66.0__tar.gz → 0.67.0__tar.gz

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.
Files changed (39) hide show
  1. {ras_commander-0.66.0/ras_commander.egg-info → ras_commander-0.67.0}/PKG-INFO +1 -1
  2. ras_commander-0.67.0/ras_commander/Decorators.py +184 -0
  3. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfPlan.py +1 -19
  4. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasPrj.py +79 -29
  5. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/__init__.py +1 -1
  6. {ras_commander-0.66.0 → ras_commander-0.67.0/ras_commander.egg-info}/PKG-INFO +1 -1
  7. {ras_commander-0.66.0 → ras_commander-0.67.0}/setup.py +1 -1
  8. ras_commander-0.66.0/ras_commander/Decorators.py +0 -144
  9. {ras_commander-0.66.0 → ras_commander-0.67.0}/LICENSE +0 -0
  10. {ras_commander-0.66.0 → ras_commander-0.67.0}/README.md +0 -0
  11. {ras_commander-0.66.0 → ras_commander-0.67.0}/pyproject.toml +0 -0
  12. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfBase.py +0 -0
  13. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfBndry.py +0 -0
  14. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfFluvialPluvial.py +0 -0
  15. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfInfiltration.py +0 -0
  16. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfMesh.py +0 -0
  17. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfPipe.py +0 -0
  18. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfPlot.py +0 -0
  19. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfPump.py +0 -0
  20. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfResultsMesh.py +0 -0
  21. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfResultsPlan.py +0 -0
  22. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfResultsPlot.py +0 -0
  23. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfResultsXsec.py +0 -0
  24. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfStruc.py +0 -0
  25. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfUtils.py +0 -0
  26. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/HdfXsec.py +0 -0
  27. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/LoggingConfig.py +0 -0
  28. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasCmdr.py +0 -0
  29. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasExamples.py +0 -0
  30. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasGeo.py +0 -0
  31. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasMapper.py +0 -0
  32. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasPlan.py +0 -0
  33. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasUnsteady.py +0 -0
  34. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander/RasUtils.py +0 -0
  35. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander.egg-info/SOURCES.txt +0 -0
  36. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander.egg-info/dependency_links.txt +0 -0
  37. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander.egg-info/requires.txt +0 -0
  38. {ras_commander-0.66.0 → ras_commander-0.67.0}/ras_commander.egg-info/top_level.txt +0 -0
  39. {ras_commander-0.66.0 → ras_commander-0.67.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: ras-commander
3
- Version: 0.66.0
3
+ Version: 0.67.0
4
4
  Summary: A Python library for automating HEC-RAS 6.x operations
5
5
  Home-page: https://github.com/gpt-cmdr/ras-commander
6
6
  Author: William M. Katzenmeyer, P.E., C.F.M.
@@ -0,0 +1,184 @@
1
+ from functools import wraps
2
+ from pathlib import Path
3
+ from typing import Union
4
+ import logging
5
+ import h5py
6
+ import inspect
7
+ import pandas as pd
8
+
9
+
10
+ def log_call(func):
11
+ @wraps(func)
12
+ def wrapper(*args, **kwargs):
13
+ logger = logging.getLogger(func.__module__)
14
+ logger.debug(f"Calling {func.__name__}")
15
+ result = func(*args, **kwargs)
16
+ logger.debug(f"Finished {func.__name__}")
17
+ return result
18
+ return wrapper
19
+
20
+ def standardize_input(file_type: str = 'plan_hdf'):
21
+ """
22
+ Decorator to standardize input for HDF file operations.
23
+
24
+ This decorator processes various input types and converts them to a Path object
25
+ pointing to the correct HDF file. It handles the following input types:
26
+ - h5py.File objects
27
+ - pathlib.Path objects
28
+ - Strings (file paths or plan/geom numbers)
29
+ - Integers (interpreted as plan/geom numbers)
30
+
31
+ The decorator also manages RAS object references and logging.
32
+
33
+ Args:
34
+ file_type (str): Specifies whether to look for 'plan_hdf' or 'geom_hdf' files.
35
+
36
+ Returns:
37
+ A decorator that wraps the function to standardize its input to a Path object.
38
+ """
39
+ def decorator(func):
40
+ @wraps(func)
41
+ def wrapper(*args, **kwargs):
42
+ logger = logging.getLogger(func.__module__)
43
+
44
+ # Check if the function expects an hdf_path parameter
45
+ sig = inspect.signature(func)
46
+ param_names = list(sig.parameters.keys())
47
+
48
+ # Handle both static method calls and regular function calls
49
+ if args and isinstance(args[0], type):
50
+ # Static method call, remove the class argument
51
+ args = args[1:]
52
+
53
+ hdf_input = kwargs.pop('hdf_path', None) or (args[0] if args else None)
54
+
55
+ # Import ras here to ensure we get the most current instance
56
+ from .RasPrj import ras as ras
57
+ ras_object = kwargs.pop('ras_object', None) or (args[1] if len(args) > 1 else None)
58
+ ras_obj = ras_object or ras
59
+
60
+ # If no hdf_input provided, return the function unmodified
61
+ if hdf_input is None:
62
+ return func(*args, **kwargs)
63
+
64
+ hdf_path = None
65
+
66
+ # Clean and normalize string inputs
67
+ if isinstance(hdf_input, str):
68
+ # Clean the string (remove extra whitespace, normalize path separators)
69
+ hdf_input = hdf_input.strip()
70
+
71
+ # Check if it's a raw file path that exists
72
+ try:
73
+ test_path = Path(hdf_input)
74
+ if test_path.is_file():
75
+ hdf_path = test_path
76
+ logger.info(f"Using HDF file from direct string path: {hdf_path}")
77
+ except Exception as e:
78
+ logger.debug(f"Error converting string to path: {str(e)}")
79
+
80
+ # If a valid path wasn't created from string processing, continue with normal flow
81
+ if hdf_path is None:
82
+ # If hdf_input is already a Path and exists, use it directly
83
+ if isinstance(hdf_input, Path) and hdf_input.is_file():
84
+ hdf_path = hdf_input
85
+ logger.info(f"Using existing Path object HDF file: {hdf_path}")
86
+ # If hdf_input is an h5py.File object, use its filename
87
+ elif isinstance(hdf_input, h5py.File):
88
+ hdf_path = Path(hdf_input.filename)
89
+ logger.info(f"Using HDF file from h5py.File object: {hdf_path}")
90
+ # Handle Path objects that might not be verified yet
91
+ elif isinstance(hdf_input, Path):
92
+ if hdf_input.is_file():
93
+ hdf_path = hdf_input
94
+ logger.info(f"Using verified Path object HDF file: {hdf_path}")
95
+ # Handle string inputs that are plan/geom numbers
96
+ elif isinstance(hdf_input, str) and (hdf_input.isdigit() or (len(hdf_input) > 1 and hdf_input[0] == 'p' and hdf_input[1:].isdigit())):
97
+ try:
98
+ ras_obj.check_initialized()
99
+ except Exception as e:
100
+ raise ValueError(f"RAS object is not initialized: {str(e)}")
101
+
102
+ number_str = hdf_input if hdf_input.isdigit() else hdf_input[1:]
103
+ number_int = int(number_str)
104
+
105
+ if file_type == 'plan_hdf':
106
+ try:
107
+ # Convert plan_number column to integers for comparison
108
+ plan_info = ras_obj.plan_df[ras_obj.plan_df['plan_number'].astype(int) == number_int]
109
+ if not plan_info.empty:
110
+ # Make sure HDF_Results_Path is a string and not None
111
+ hdf_path_str = plan_info.iloc[0]['HDF_Results_Path']
112
+ if pd.notna(hdf_path_str):
113
+ hdf_path = Path(str(hdf_path_str))
114
+ except Exception as e:
115
+ logger.warning(f"Error retrieving plan HDF path: {str(e)}")
116
+ elif file_type == 'geom_hdf':
117
+ try:
118
+ # Convert geom_number column to integers for comparison
119
+ geom_info = ras_obj.geom_df[ras_obj.geom_df['geom_number'].astype(int) == number_int]
120
+ if not geom_info.empty:
121
+ hdf_path_str = geom_info.iloc[0]['hdf_path']
122
+ if pd.notna(hdf_path_str):
123
+ hdf_path = Path(str(hdf_path_str))
124
+ except Exception as e:
125
+ logger.warning(f"Error retrieving geometry HDF path: {str(e)}")
126
+ else:
127
+ raise ValueError(f"Invalid file type: {file_type}")
128
+
129
+ # Handle integer inputs (assuming they're plan or geom numbers)
130
+ elif isinstance(hdf_input, int):
131
+ try:
132
+ ras_obj.check_initialized()
133
+ except Exception as e:
134
+ raise ValueError(f"RAS object is not initialized: {str(e)}")
135
+
136
+ number_int = hdf_input
137
+
138
+ if file_type == 'plan_hdf':
139
+ try:
140
+ # Convert plan_number column to integers for comparison
141
+ plan_info = ras_obj.plan_df[ras_obj.plan_df['plan_number'].astype(int) == number_int]
142
+ if not plan_info.empty:
143
+ # Make sure HDF_Results_Path is a string and not None
144
+ hdf_path_str = plan_info.iloc[0]['HDF_Results_Path']
145
+ if pd.notna(hdf_path_str):
146
+ hdf_path = Path(str(hdf_path_str))
147
+ except Exception as e:
148
+ logger.warning(f"Error retrieving plan HDF path: {str(e)}")
149
+ elif file_type == 'geom_hdf':
150
+ try:
151
+ # Convert geom_number column to integers for comparison
152
+ geom_info = ras_obj.geom_df[ras_obj.geom_df['geom_number'].astype(int) == number_int]
153
+ if not geom_info.empty:
154
+ hdf_path_str = geom_info.iloc[0]['hdf_path']
155
+ if pd.notna(hdf_path_str):
156
+ hdf_path = Path(str(hdf_path_str))
157
+ except Exception as e:
158
+ logger.warning(f"Error retrieving geometry HDF path: {str(e)}")
159
+ else:
160
+ raise ValueError(f"Invalid file type: {file_type}")
161
+
162
+ # Final verification that the path exists
163
+ if hdf_path is None or not hdf_path.exists():
164
+ error_msg = f"HDF file not found: {hdf_input}"
165
+ logger.error(error_msg)
166
+ raise FileNotFoundError(error_msg)
167
+
168
+ logger.info(f"Final validated HDF file path: {hdf_path}")
169
+
170
+ # Now try to validate the HDF file structure (but don't fail if validation fails)
171
+ try:
172
+ with h5py.File(hdf_path, 'r') as test_file:
173
+ # Just open to verify it's a valid HDF5 file
174
+ logger.debug(f"Successfully opened HDF file for validation: {hdf_path}")
175
+ except Exception as e:
176
+ logger.warning(f"Warning: Could not validate HDF file: {str(e)}")
177
+ # Continue anyway, let the function handle detailed validation
178
+
179
+ # Pass all original arguments and keywords, replacing hdf_input with standardized hdf_path
180
+ new_args = (hdf_path,) + args[1:]
181
+ return func(*new_args, **kwargs)
182
+
183
+ return wrapper
184
+ return decorator
@@ -11,25 +11,7 @@ The file has been forked and modified for use in RAS Commander.
11
11
 
12
12
  All of the methods in this class are static and are designed to be used without instantiation.
13
13
 
14
- List of Functions in HdfPlan:
15
- - get_simulation_start_time()
16
- - get_simulation_end_time()
17
- - get_unsteady_datetimes()
18
- - get_plan_info_attrs()
19
- - get_plan_parameters()
20
- - get_meteorology_precip_attrs()
21
- - get_geom_attrs()
22
-
23
-
24
- REVISIONS NEEDED:
25
14
 
26
- Use get_ prefix for functions that return data.
27
- Since we are extracting plan data, we should use get_plan_...
28
- BUT, we will never set results data, so we should use results_
29
-
30
- We need to shorten names where possible.
31
-
32
- List of Revised Functions in HdfPlan:
33
15
  - get_plan_start_time()
34
16
  - get_plan_end_time()
35
17
  - get_plan_timestamps_list()
@@ -283,7 +265,7 @@ class HdfPlan:
283
265
 
284
266
  @staticmethod
285
267
  @log_call
286
- @standardize_input(file_type='plan_hdf')
268
+ @standardize_input(file_type='geom_hdf')
287
269
  def get_geometry_information(hdf_path: Path) -> pd.DataFrame:
288
270
  """
289
271
  Get root level geometry attributes from the HDF plan file.
@@ -175,6 +175,9 @@ class RasPrj:
175
175
  # Set paths for geometry and flow files
176
176
  self._set_file_paths()
177
177
 
178
+ # Make sure all plan paths are properly set
179
+ self._set_plan_paths()
180
+
178
181
  except Exception as e:
179
182
  logger.error(f"Error loading project data: {e}")
180
183
  raise
@@ -220,6 +223,44 @@ class RasPrj:
220
223
  flow_path = self.project_folder / f"{self.project_name}.{prefix}{row['Flow File']}"
221
224
  self.plan_df.at[idx, 'Flow Path'] = str(flow_path)
222
225
 
226
+ def _set_plan_paths(self):
227
+ """Set full path information for plan files and their associated geometry and flow files."""
228
+ if self.plan_df.empty:
229
+ logger.debug("Plan DataFrame is empty, no paths to set")
230
+ return
231
+
232
+ # Ensure full path is set for all plan entries
233
+ if 'full_path' not in self.plan_df.columns or self.plan_df['full_path'].isna().any():
234
+ self.plan_df['full_path'] = self.plan_df['plan_number'].apply(
235
+ lambda x: str(self.project_folder / f"{self.project_name}.p{x}")
236
+ )
237
+
238
+ # Create the Geom Path and Flow Path columns if they don't exist
239
+ if 'Geom Path' not in self.plan_df.columns:
240
+ self.plan_df['Geom Path'] = None
241
+ if 'Flow Path' not in self.plan_df.columns:
242
+ self.plan_df['Flow Path'] = None
243
+
244
+ # Update paths for each plan entry
245
+ for idx, row in self.plan_df.iterrows():
246
+ try:
247
+ # Set geometry path if Geom File exists and Geom Path is missing or invalid
248
+ if pd.notna(row['Geom File']):
249
+ geom_path = self.project_folder / f"{self.project_name}.g{row['Geom File']}"
250
+ self.plan_df.at[idx, 'Geom Path'] = str(geom_path)
251
+
252
+ # Set flow path if Flow File exists and Flow Path is missing or invalid
253
+ if pd.notna(row['Flow File']):
254
+ # Determine the prefix (u for unsteady, f for steady flow)
255
+ prefix = 'u' if pd.notna(row['unsteady_number']) else 'f'
256
+ flow_path = self.project_folder / f"{self.project_name}.{prefix}{row['Flow File']}"
257
+ self.plan_df.at[idx, 'Flow Path'] = str(flow_path)
258
+
259
+ if not self.suppress_logging:
260
+ logger.debug(f"Plan {row['plan_number']} paths set up")
261
+ except Exception as e:
262
+ logger.error(f"Error setting paths for plan {row.get('plan_number', idx)}: {e}")
263
+
223
264
  def _get_geom_file_for_plan(self, plan_number):
224
265
  """
225
266
  Get the geometry file path for a given plan number.
@@ -939,33 +980,60 @@ class RasPrj:
939
980
  return df
940
981
 
941
982
  if entry_type == 'Plan':
942
- # Define column groups
983
+ # Set required column order
943
984
  first_cols = ['plan_number', 'unsteady_number', 'geometry_number']
985
+
986
+ # Standard plan key columns in the exact order specified
944
987
  plan_key_cols = [
945
988
  'Plan Title', 'Program Version', 'Short Identifier', 'Simulation Date',
946
- 'Computation Interval', 'Mapping Interval', 'Run HTab', 'Run UNet',
947
- 'Run Sediment', 'Run PostProcess', 'Run WQNet', 'UNET Use Existing IB Tables',
948
- 'HDF_Results_Path', 'UNET 1D Methodology'
989
+ 'Std Step Tol', 'Computation Interval', 'Output Interval', 'Instantaneous Interval',
990
+ 'Mapping Interval', 'Run HTab', 'Run UNet', 'Run Sediment', 'Run PostProcess',
991
+ 'Run WQNet', 'Run RASMapper', 'UNET Use Existing IB Tables', 'HDF_Results_Path',
992
+ 'UNET 1D Methodology', 'Write IC File', 'Write IC File at Fixed DateTime',
993
+ 'IC Time', 'Write IC File Reoccurance', 'Write IC File at Sim End'
949
994
  ]
995
+
996
+ # Additional convenience columns
950
997
  file_path_cols = ['Geom File', 'Geom Path', 'Flow File', 'Flow Path']
951
998
 
952
- # Build column list
999
+ # Special columns that must be preserved
1000
+ special_cols = ['HDF_Results_Path']
1001
+
1002
+ # Build the final column list
953
1003
  all_cols = first_cols.copy()
954
- all_cols.extend([col for col in plan_key_cols if col in df.columns])
955
- all_cols.extend([col for col in df.columns if col not in all_cols + file_path_cols + ['full_path']])
1004
+
1005
+ # Add plan key columns if they exist
1006
+ for col in plan_key_cols:
1007
+ if col in df.columns and col not in all_cols and col not in special_cols:
1008
+ all_cols.append(col)
1009
+
1010
+ # Add any remaining columns not explicitly specified
1011
+ other_cols = [col for col in df.columns if col not in all_cols + file_path_cols + special_cols + ['full_path']]
1012
+ all_cols.extend(other_cols)
1013
+
1014
+ # Add HDF_Results_Path if it exists (ensure it comes before file paths)
1015
+ for special_col in special_cols:
1016
+ if special_col in df.columns and special_col not in all_cols:
1017
+ all_cols.append(special_col)
1018
+
1019
+ # Add file path columns at the end
956
1020
  all_cols.extend(file_path_cols)
957
1021
 
958
- # Rename and fill missing columns
1022
+ # Rename plan_number column
959
1023
  df = df.rename(columns={f'{entry_type.lower()}_number': 'plan_number'})
1024
+
1025
+ # Fill in missing columns with None
960
1026
  for col in all_cols:
961
1027
  if col not in df.columns:
962
1028
  df[col] = None
963
1029
 
964
- # Add full_path if present
965
- if 'full_path' in df.columns:
1030
+ # Make sure full_path column is preserved and included
1031
+ if 'full_path' in df.columns and 'full_path' not in all_cols:
966
1032
  all_cols.append('full_path')
967
1033
 
968
- return df[[col for col in all_cols if col in df.columns]]
1034
+ # Return DataFrame with specified column order
1035
+ cols_to_return = [col for col in all_cols if col in df.columns]
1036
+ return df[cols_to_return]
969
1037
 
970
1038
  return df
971
1039
 
@@ -1035,24 +1103,6 @@ class RasPrj:
1035
1103
  hdf_path = self.project_folder / f"{self.project_name}.p{entry_number}.hdf"
1036
1104
  entry['HDF_Results_Path'] = str(hdf_path) if hdf_path.exists() else None
1037
1105
 
1038
- def _set_plan_paths(self):
1039
- """Helper method to set paths in plan_df."""
1040
- if not self.plan_df['full_path'].any():
1041
- self.plan_df['full_path'] = self.plan_df['plan_number'].apply(
1042
- lambda x: str(self.project_folder / f"{self.project_name}.p{x}")
1043
- )
1044
-
1045
- for idx, row in self.plan_df.iterrows():
1046
- if pd.notna(row['Geom File']) and pd.isna(row.get('Geom Path')):
1047
- self.plan_df.at[idx, 'Geom Path'] = str(self.project_folder / f"{self.project_name}.g{row['Geom File']}")
1048
-
1049
- if pd.notna(row['Flow File']) and pd.isna(row.get('Flow Path')):
1050
- prefix = 'u' if pd.notna(row['unsteady_number']) else 'f'
1051
- self.plan_df.at[idx, 'Flow Path'] = str(self.project_folder / f"{self.project_name}.{prefix}{row['Flow File']}")
1052
-
1053
- if not self.suppress_logging:
1054
- logger.info(f"Plan {row['plan_number']} paths set up")
1055
-
1056
1106
 
1057
1107
  # Create a global instance named 'ras'
1058
1108
  # Defining the global instance allows the init_ras_project function to initialize the project.
@@ -10,7 +10,7 @@ try:
10
10
  __version__ = version("ras-commander")
11
11
  except PackageNotFoundError:
12
12
  # package is not installed
13
- __version__ = "0.66.0"
13
+ __version__ = "0.67.0"
14
14
 
15
15
  # Set up logging
16
16
  setup_logging()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: ras-commander
3
- Version: 0.66.0
3
+ Version: 0.67.0
4
4
  Summary: A Python library for automating HEC-RAS 6.x operations
5
5
  Home-page: https://github.com/gpt-cmdr/ras-commander
6
6
  Author: William M. Katzenmeyer, P.E., C.F.M.
@@ -28,7 +28,7 @@ class CustomBuildPy(build_py):
28
28
 
29
29
  setup(
30
30
  name="ras-commander",
31
- version="0.66.0",
31
+ version="0.67.0",
32
32
  packages=find_packages(),
33
33
  include_package_data=True,
34
34
  python_requires='>=3.10',
@@ -1,144 +0,0 @@
1
- from functools import wraps
2
- from pathlib import Path
3
- from typing import Union
4
- import logging
5
- import h5py
6
- import inspect
7
-
8
-
9
- def log_call(func):
10
- @wraps(func)
11
- def wrapper(*args, **kwargs):
12
- logger = logging.getLogger(func.__module__)
13
- logger.debug(f"Calling {func.__name__}")
14
- result = func(*args, **kwargs)
15
- logger.debug(f"Finished {func.__name__}")
16
- return result
17
- return wrapper
18
-
19
- def standardize_input(file_type: str = 'plan_hdf'):
20
- """
21
- Decorator to standardize input for HDF file operations.
22
-
23
- This decorator processes various input types and converts them to a Path object
24
- pointing to the correct HDF file. It handles the following input types:
25
- - h5py.File objects
26
- - pathlib.Path objects
27
- - Strings (file paths or plan/geom numbers)
28
- - Integers (interpreted as plan/geom numbers)
29
-
30
- The decorator also manages RAS object references and logging.
31
-
32
- Args:
33
- file_type (str): Specifies whether to look for 'plan_hdf' or 'geom_hdf' files.
34
-
35
- Returns:
36
- A decorator that wraps the function to standardize its input to a Path object.
37
- """
38
- def decorator(func):
39
- @wraps(func)
40
- def wrapper(*args, **kwargs):
41
- logger = logging.getLogger(func.__module__)
42
-
43
- # Check if the function expects an hdf_path parameter
44
- sig = inspect.signature(func)
45
- param_names = list(sig.parameters.keys())
46
-
47
- # If first parameter is 'hdf_file', skip path processing
48
- if param_names and param_names[0] == 'hdf_file':
49
- return func(*args, **kwargs)
50
-
51
- # Handle both static method calls and regular function calls
52
- if args and isinstance(args[0], type):
53
- # Static method call, remove the class argument
54
- args = args[1:]
55
-
56
- hdf_input = kwargs.pop('hdf_path', None) or kwargs.pop('hdf_input', None) or (args[0] if args else None)
57
-
58
- # Import ras here to ensure we get the most current instance
59
- from .RasPrj import ras as ras
60
- ras_object = kwargs.pop('ras_object', None) or (args[1] if len(args) > 1 else None)
61
- ras_obj = ras_object or ras
62
-
63
- # If no hdf_input provided, return the function unmodified
64
- if hdf_input is None:
65
- return func(*args, **kwargs)
66
-
67
- # NEW: If input is already a Path and exists, use it directly regardless of file_type
68
- if isinstance(hdf_input, Path) and hdf_input.is_file():
69
- logger.info(f"Using existing HDF file: {hdf_input}")
70
- new_args = (hdf_input,) + args[1:]
71
- return func(*new_args, **kwargs)
72
-
73
- hdf_path = None
74
-
75
- # If hdf_input is already an h5py.File object, use its filename
76
- if isinstance(hdf_input, h5py.File):
77
- hdf_path = Path(hdf_input.filename)
78
- # Handle Path objects
79
- elif isinstance(hdf_input, Path):
80
- if hdf_input.is_file():
81
- hdf_path = hdf_input
82
- # Handle string inputs
83
- elif isinstance(hdf_input, str):
84
- # Check if it's a file path
85
- if Path(hdf_input).is_file():
86
- hdf_path = Path(hdf_input)
87
- # Check if it's a number (with or without 'p' prefix)
88
- elif hdf_input.isdigit() or (len(hdf_input) > 1 and hdf_input[0] == 'p' and hdf_input[1:].isdigit()):
89
- try:
90
- ras_obj.check_initialized()
91
- except Exception as e:
92
- raise ValueError(f"RAS object is not initialized: {str(e)}")
93
-
94
- # Extract the numeric part and convert to integer for comparison
95
- number_str = hdf_input if hdf_input.isdigit() else hdf_input[1:]
96
- number_int = int(number_str)
97
-
98
- if file_type == 'plan_hdf':
99
- # Convert plan_number column to integers for comparison
100
- plan_info = ras_obj.plan_df[ras_obj.plan_df['plan_number'].astype(int) == number_int]
101
- if not plan_info.empty:
102
- hdf_path = Path(plan_info.iloc[0]['HDF_Results_Path'])
103
- elif file_type == 'geom_hdf':
104
- # Convert geom_number column to integers for comparison
105
- geom_info = ras_obj.geom_df[ras_obj.geom_df['geom_number'].astype(int) == number_int]
106
- if not geom_info.empty:
107
- hdf_path = Path(geom_info.iloc[0]['HDF_Path'])
108
- else:
109
- raise ValueError(f"Invalid file type: {file_type}")
110
- # Handle integer inputs (assuming they're plan or geom numbers)
111
- elif isinstance(hdf_input, int):
112
- try:
113
- ras_obj.check_initialized()
114
- except Exception as e:
115
- raise ValueError(f"RAS object is not initialized: {str(e)}")
116
-
117
- number_int = hdf_input
118
-
119
- if file_type == 'plan_hdf':
120
- # Convert plan_number column to integers for comparison
121
- plan_info = ras_obj.plan_df[ras_obj.plan_df['plan_number'].astype(int) == number_int]
122
- if not plan_info.empty:
123
- hdf_path = Path(plan_info.iloc[0]['HDF_Results_Path'])
124
- elif file_type == 'geom_hdf':
125
- # Convert geom_number column to integers for comparison
126
- geom_info = ras_obj.geom_df[ras_obj.geom_df['geom_number'].astype(int) == number_int]
127
- if not geom_info.empty:
128
- hdf_path = Path(geom_info.iloc[0]['HDF_Path'])
129
- else:
130
- raise ValueError(f"Invalid file type: {file_type}")
131
-
132
- if hdf_path is None or not hdf_path.is_file():
133
- error_msg = f"HDF file not found: {hdf_input}"
134
- logger.error(error_msg)
135
- raise FileNotFoundError(error_msg)
136
-
137
- logger.info(f"Using HDF file: {hdf_path}")
138
-
139
- # Pass all original arguments and keywords, replacing hdf_input with standardized hdf_path
140
- new_args = (hdf_path,) + args[1:]
141
- return func(*new_args, **kwargs)
142
-
143
- return wrapper
144
- return decorator
File without changes
File without changes
File without changes