ras-commander 0.76.0__py3-none-any.whl → 0.77.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.
@@ -1,424 +1,424 @@
1
- """
2
- RasExamples - Manage and load HEC-RAS example projects for testing and development
3
-
4
- This module is part of the ras-commander library and uses a centralized logging configuration.
5
-
6
- Logging Configuration:
7
- - The logging is set up in the logging_config.py file.
8
- - A @log_call decorator is available to automatically log function calls.
9
- - Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
10
- - Logs are written to both console and a rotating file handler.
11
- - The default log file is 'ras_commander.log' in the 'logs' directory.
12
- - The default log level is INFO.
13
-
14
- To use logging in this module:
15
- 1. Use the @log_call decorator for automatic function call logging.
16
- 2. For additional logging, use logger.[level]() calls (e.g., logger.info(), logger.debug()).
17
- 3. Obtain the logger using: logger = logging.getLogger(__name__)
18
-
19
- Example:
20
- @log_call
21
- def my_function():
22
- logger = logging.getLogger(__name__)
23
- logger.debug("Additional debug information")
24
- # Function logic here
25
-
26
-
27
- -----
28
-
29
- All of the methods in this class are static and are designed to be used without instantiation.
30
-
31
- List of Functions in RasExamples:
32
- - get_example_projects()
33
- - list_categories()
34
- - list_projects()
35
- - extract_project()
36
- - is_project_extracted()
37
- - clean_projects_directory()
38
-
39
- """
40
- import os
41
- import requests
42
- import zipfile
43
- import pandas as pd
44
- from pathlib import Path
45
- import shutil
46
- from typing import Union, List
47
- import csv
48
- from datetime import datetime
49
- import logging
50
- import re
51
- from tqdm import tqdm
52
- from ras_commander import get_logger
53
- from ras_commander.LoggingConfig import log_call
54
-
55
- logger = get_logger(__name__)
56
-
57
- class RasExamples:
58
- """
59
- A class for quickly loading HEC-RAS example projects for testing and development of ras-commander.
60
- All methods are class methods, so no initialization is required.
61
- """
62
- base_url = 'https://github.com/HydrologicEngineeringCenter/hec-downloads/releases/download/'
63
- valid_versions = [
64
- "6.6", "6.5", "6.4.1", "6.3.1", "6.3", "6.2", "6.1", "6.0",
65
- "5.0.7", "5.0.6", "5.0.5", "5.0.4", "5.0.3", "5.0.1", "5.0",
66
- "4.1", "4.0", "3.1.3", "3.1.2", "3.1.1", "3.0", "2.2"
67
- ]
68
- base_dir = Path.cwd()
69
- examples_dir = base_dir
70
- projects_dir = examples_dir / 'example_projects'
71
- csv_file_path = examples_dir / 'example_projects.csv'
72
-
73
- _folder_df = None
74
- _zip_file_path = None
75
-
76
- def __init__(self):
77
- """Initialize RasExamples and ensure data is loaded"""
78
- self._ensure_initialized()
79
-
80
- @property
81
- def folder_df(self):
82
- """Access the folder DataFrame"""
83
- self._ensure_initialized()
84
- return self._folder_df
85
-
86
- def _ensure_initialized(self):
87
- """Ensure the class is initialized with required data"""
88
- self.projects_dir.mkdir(parents=True, exist_ok=True)
89
- if self._folder_df is None:
90
- self._load_project_data()
91
-
92
- def _load_project_data(self):
93
- """Load project data from CSV if up-to-date, otherwise extract from zip."""
94
- logger.debug("Loading project data")
95
- self._find_zip_file()
96
-
97
- if not self._zip_file_path:
98
- logger.info("No example projects zip file found. Downloading...")
99
- self.get_example_projects()
100
-
101
- try:
102
- zip_modified_time = os.path.getmtime(self._zip_file_path)
103
- except FileNotFoundError:
104
- logger.error(f"Zip file not found at {self._zip_file_path}.")
105
- return
106
-
107
- if self.csv_file_path.exists():
108
- csv_modified_time = os.path.getmtime(self.csv_file_path)
109
-
110
- if csv_modified_time >= zip_modified_time:
111
- logger.info("Loading project data from CSV...")
112
- try:
113
- self._folder_df = pd.read_csv(self.csv_file_path)
114
- logger.info(f"Loaded {len(self._folder_df)} projects from CSV.")
115
- return
116
- except Exception as e:
117
- logger.error(f"Failed to read CSV file: {e}")
118
- self._folder_df = None
119
-
120
- logger.info("Extracting folder structure from zip file...")
121
- self._extract_folder_structure()
122
- self._save_to_csv()
123
-
124
- @classmethod
125
- def extract_project(cls, project_names: Union[str, List[str]]) -> Union[Path, List[Path]]:
126
- """Extract one or more specific HEC-RAS projects from the zip file.
127
-
128
- Args:
129
- project_names: Single project name as string or list of project names
130
-
131
- Returns:
132
- Path: Single Path object if one project extracted
133
- List[Path]: List of Path objects if multiple projects extracted
134
- """
135
- logger.debug(f"Extracting projects: {project_names}")
136
-
137
- # Initialize if needed
138
- if cls._folder_df is None:
139
- cls._find_zip_file()
140
- if not cls._zip_file_path:
141
- logger.info("No example projects zip file found. Downloading...")
142
- cls.get_example_projects()
143
- cls._load_project_data()
144
-
145
- if isinstance(project_names, str):
146
- project_names = [project_names]
147
-
148
- extracted_paths = []
149
-
150
- for project_name in project_names:
151
- logger.info("----- RasExamples Extracting Project -----")
152
- logger.info(f"Extracting project '{project_name}'")
153
- project_path = cls.projects_dir
154
-
155
- if (project_path / project_name).exists():
156
- logger.info(f"Project '{project_name}' already exists. Deleting existing folder...")
157
- try:
158
- shutil.rmtree(project_path / project_name)
159
- logger.info(f"Existing folder for project '{project_name}' has been deleted.")
160
- except Exception as e:
161
- logger.error(f"Failed to delete existing project folder '{project_name}': {e}")
162
- continue
163
-
164
- project_info = cls._folder_df[cls._folder_df['Project'] == project_name]
165
- if project_info.empty:
166
- error_msg = f"Project '{project_name}' not found in the zip file."
167
- logger.error(error_msg)
168
- raise ValueError(error_msg)
169
-
170
- try:
171
- with zipfile.ZipFile(cls._zip_file_path, 'r') as zip_ref:
172
- for file in zip_ref.namelist():
173
- parts = Path(file).parts
174
- if len(parts) > 1 and parts[1] == project_name:
175
- relative_path = Path(*parts[1:])
176
- extract_path = project_path / relative_path
177
- if file.endswith('/'):
178
- extract_path.mkdir(parents=True, exist_ok=True)
179
- else:
180
- extract_path.parent.mkdir(parents=True, exist_ok=True)
181
- with zip_ref.open(file) as source, open(extract_path, "wb") as target:
182
- shutil.copyfileobj(source, target)
183
-
184
- logger.info(f"Successfully extracted project '{project_name}' to {project_path / project_name}")
185
- extracted_paths.append(project_path / project_name)
186
- except Exception as e:
187
- logger.error(f"An error occurred while extracting project '{project_name}': {str(e)}")
188
-
189
- # Return single path if only one project was extracted, otherwise return list
190
- return extracted_paths[0] if len(project_names) == 1 else extracted_paths
191
-
192
- @classmethod
193
- def _find_zip_file(cls):
194
- """Locate the example projects zip file in the examples directory."""
195
- for version in cls.valid_versions:
196
- potential_zip = cls.examples_dir / f"Example_Projects_{version.replace('.', '_')}.zip"
197
- if potential_zip.exists():
198
- cls._zip_file_path = potential_zip
199
- logger.info(f"Found zip file: {cls._zip_file_path}")
200
- break
201
- else:
202
- logger.warning("No existing example projects zip file found.")
203
-
204
- @classmethod
205
- def get_example_projects(cls, version_number='6.6'):
206
- """
207
- Download and extract HEC-RAS example projects for a specified version.
208
- """
209
- logger.info(f"Getting example projects for version {version_number}")
210
- if version_number not in cls.valid_versions:
211
- error_msg = f"Invalid version number. Valid versions are: {', '.join(cls.valid_versions)}"
212
- logger.error(error_msg)
213
- raise ValueError(error_msg)
214
-
215
- zip_url = f"{cls.base_url}1.0.33/Example_Projects_{version_number.replace('.', '_')}.zip"
216
-
217
- cls.examples_dir.mkdir(parents=True, exist_ok=True)
218
-
219
- cls._zip_file_path = cls.examples_dir / f"Example_Projects_{version_number.replace('.', '_')}.zip"
220
-
221
- if not cls._zip_file_path.exists():
222
- logger.info(f"Downloading HEC-RAS Example Projects from {zip_url}. \nThe file is over 400 MB, so it may take a few minutes to download....")
223
- try:
224
- response = requests.get(zip_url, stream=True)
225
- response.raise_for_status()
226
- with open(cls._zip_file_path, 'wb') as file:
227
- shutil.copyfileobj(response.raw, file)
228
- logger.info(f"Downloaded to {cls._zip_file_path}")
229
- except requests.exceptions.RequestException as e:
230
- logger.error(f"Failed to download the zip file: {e}")
231
- raise
232
- else:
233
- logger.info("HEC-RAS Example Projects zip file already exists. Skipping download.")
234
-
235
- cls._load_project_data()
236
- return cls.projects_dir
237
-
238
- @classmethod
239
- def _load_project_data(cls):
240
- """Load project data from CSV if up-to-date, otherwise extract from zip."""
241
- logger.debug("Loading project data")
242
-
243
- try:
244
- zip_modified_time = os.path.getmtime(cls._zip_file_path)
245
- except FileNotFoundError:
246
- logger.error(f"Zip file not found at {cls._zip_file_path}.")
247
- return
248
-
249
- if cls.csv_file_path.exists():
250
- csv_modified_time = os.path.getmtime(cls.csv_file_path)
251
-
252
- if csv_modified_time >= zip_modified_time:
253
- logger.info("Loading project data from CSV...")
254
- try:
255
- cls._folder_df = pd.read_csv(cls.csv_file_path)
256
- logger.info(f"Loaded {len(cls._folder_df)} projects from CSV.")
257
- return
258
- except Exception as e:
259
- logger.error(f"Failed to read CSV file: {e}")
260
- cls._folder_df = None
261
-
262
- logger.info("Extracting folder structure from zip file...")
263
- cls._extract_folder_structure()
264
- cls._save_to_csv()
265
-
266
- @classmethod
267
- def _extract_folder_structure(cls):
268
- """
269
- Extract folder structure from the zip file.
270
-
271
- Populates folder_df with category and project information.
272
- """
273
- folder_data = []
274
- try:
275
- with zipfile.ZipFile(cls._zip_file_path, 'r') as zip_ref:
276
- for file in zip_ref.namelist():
277
- parts = Path(file).parts
278
- if len(parts) > 1:
279
- folder_data.append({
280
- 'Category': parts[0],
281
- 'Project': parts[1]
282
- })
283
-
284
- cls._folder_df = pd.DataFrame(folder_data).drop_duplicates()
285
- logger.info(f"Extracted {len(cls._folder_df)} projects.")
286
- logger.debug(f"folder_df:\n{cls._folder_df}")
287
- except zipfile.BadZipFile:
288
- logger.error(f"The file {cls._zip_file_path} is not a valid zip file.")
289
- cls._folder_df = pd.DataFrame(columns=['Category', 'Project'])
290
- except Exception as e:
291
- logger.error(f"An error occurred while extracting the folder structure: {str(e)}")
292
- cls._folder_df = pd.DataFrame(columns=['Category', 'Project'])
293
-
294
- @classmethod
295
- def _save_to_csv(cls):
296
- """Save the extracted folder structure to CSV file."""
297
- if cls._folder_df is not None and not cls._folder_df.empty:
298
- try:
299
- cls._folder_df.to_csv(cls.csv_file_path, index=False)
300
- logger.info(f"Saved project data to {cls.csv_file_path}")
301
- except Exception as e:
302
- logger.error(f"Failed to save project data to CSV: {e}")
303
- else:
304
- logger.warning("No folder data to save to CSV.")
305
-
306
- @classmethod
307
- def list_categories(cls):
308
- """
309
- List all categories of example projects.
310
- """
311
- if cls._folder_df is None or 'Category' not in cls._folder_df.columns:
312
- logger.warning("No categories available. Make sure the zip file is properly loaded.")
313
- return []
314
- categories = cls._folder_df['Category'].unique()
315
- logger.info(f"Available categories: {', '.join(categories)}")
316
- return categories.tolist()
317
-
318
- @classmethod
319
- def list_projects(cls, category=None):
320
- """
321
- List all projects or projects in a specific category.
322
- """
323
- if cls._folder_df is None:
324
- logger.warning("No projects available. Make sure the zip file is properly loaded.")
325
- return []
326
- if category:
327
- projects = cls._folder_df[cls._folder_df['Category'] == category]['Project'].unique()
328
- logger.info(f"Projects in category '{category}': {', '.join(projects)}")
329
- else:
330
- projects = cls._folder_df['Project'].unique()
331
- logger.info(f"All available projects: {', '.join(projects)}")
332
- return projects.tolist()
333
-
334
- @classmethod
335
- def is_project_extracted(cls, project_name):
336
- """
337
- Check if a specific project is already extracted.
338
- """
339
- project_path = cls.projects_dir / project_name
340
- is_extracted = project_path.exists()
341
- logger.info(f"Project '{project_name}' extracted: {is_extracted}")
342
- return is_extracted
343
-
344
- @classmethod
345
- def clean_projects_directory(cls):
346
- """Remove all extracted projects from the example_projects directory."""
347
- logger.info(f"Cleaning projects directory: {cls.projects_dir}")
348
- if cls.projects_dir.exists():
349
- try:
350
- shutil.rmtree(cls.projects_dir)
351
- logger.info("All projects have been removed.")
352
- except Exception as e:
353
- logger.error(f"Failed to remove projects directory: {e}")
354
- else:
355
- logger.warning("Projects directory does not exist.")
356
- cls.projects_dir.mkdir(parents=True, exist_ok=True)
357
- logger.info("Projects directory cleaned and recreated.")
358
-
359
- @classmethod
360
- def download_fema_ble_model(cls, huc8, output_dir=None):
361
- """
362
- Download a FEMA Base Level Engineering (BLE) model for a given HUC8.
363
-
364
- Args:
365
- huc8 (str): The 8-digit Hydrologic Unit Code (HUC) for the desired watershed.
366
- output_dir (str, optional): The directory to save the downloaded files. If None, uses the current working directory.
367
-
368
- Returns:
369
- str: The path to the downloaded and extracted model directory.
370
-
371
- Note:
372
- This method downloads the BLE model from the FEMA website and extracts it to the specified directory.
373
- """
374
- # Method implementation...
375
-
376
- @classmethod
377
- def _make_safe_folder_name(cls, name: str) -> str:
378
- """
379
- Convert a string to a safe folder name by replacing unsafe characters with underscores.
380
- """
381
- safe_name = re.sub(r'[^a-zA-Z0-9_\-]', '_', name)
382
- logger.debug(f"Converted '{name}' to safe folder name '{safe_name}'")
383
- return safe_name
384
-
385
- @classmethod
386
- def _download_file_with_progress(cls, url: str, dest_folder: Path, file_size: int) -> Path:
387
- """
388
- Download a file from a URL to a specified destination folder with progress bar.
389
- """
390
- local_filename = dest_folder / url.split('/')[-1]
391
- try:
392
- with requests.get(url, stream=True) as r:
393
- r.raise_for_status()
394
- with open(local_filename, 'wb') as f, tqdm(
395
- desc=local_filename.name,
396
- total=file_size,
397
- unit='iB',
398
- unit_scale=True,
399
- unit_divisor=1024,
400
- ) as progress_bar:
401
- for chunk in r.iter_content(chunk_size=8192):
402
- size = f.write(chunk)
403
- progress_bar.update(size)
404
- logger.info(f"Successfully downloaded {url} to {local_filename}")
405
- return local_filename
406
- except requests.exceptions.RequestException as e:
407
- logger.error(f"Request failed for {url}: {e}")
408
- raise
409
- except Exception as e:
410
- logger.error(f"Failed to write file {local_filename}: {e}")
411
- raise
412
-
413
- @classmethod
414
- def _convert_size_to_bytes(cls, size_str: str) -> int:
415
- """
416
- Convert a human-readable file size to bytes.
417
- """
418
- units = {'B': 1, 'KB': 1024, 'MB': 1024**2, 'GB': 1024**3, 'TB': 1024**4}
419
- size_str = size_str.upper().replace(' ', '')
420
- if not re.match(r'^\d+(\.\d+)?[BKMGT]B?$', size_str):
421
- raise ValueError(f"Invalid size string: {size_str}")
422
-
423
- number, unit = float(re.findall(r'[\d\.]+', size_str)[0]), re.findall(r'[BKMGT]B?', size_str)[0]
1
+ """
2
+ RasExamples - Manage and load HEC-RAS example projects for testing and development
3
+
4
+ This module is part of the ras-commander library and uses a centralized logging configuration.
5
+
6
+ Logging Configuration:
7
+ - The logging is set up in the logging_config.py file.
8
+ - A @log_call decorator is available to automatically log function calls.
9
+ - Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
10
+ - Logs are written to both console and a rotating file handler.
11
+ - The default log file is 'ras_commander.log' in the 'logs' directory.
12
+ - The default log level is INFO.
13
+
14
+ To use logging in this module:
15
+ 1. Use the @log_call decorator for automatic function call logging.
16
+ 2. For additional logging, use logger.[level]() calls (e.g., logger.info(), logger.debug()).
17
+ 3. Obtain the logger using: logger = logging.getLogger(__name__)
18
+
19
+ Example:
20
+ @log_call
21
+ def my_function():
22
+ logger = logging.getLogger(__name__)
23
+ logger.debug("Additional debug information")
24
+ # Function logic here
25
+
26
+
27
+ -----
28
+
29
+ All of the methods in this class are static and are designed to be used without instantiation.
30
+
31
+ List of Functions in RasExamples:
32
+ - get_example_projects()
33
+ - list_categories()
34
+ - list_projects()
35
+ - extract_project()
36
+ - is_project_extracted()
37
+ - clean_projects_directory()
38
+
39
+ """
40
+ import os
41
+ import requests
42
+ import zipfile
43
+ import pandas as pd
44
+ from pathlib import Path
45
+ import shutil
46
+ from typing import Union, List
47
+ import csv
48
+ from datetime import datetime
49
+ import logging
50
+ import re
51
+ from tqdm import tqdm
52
+ from ras_commander import get_logger
53
+ from ras_commander.LoggingConfig import log_call
54
+
55
+ logger = get_logger(__name__)
56
+
57
+ class RasExamples:
58
+ """
59
+ A class for quickly loading HEC-RAS example projects for testing and development of ras-commander.
60
+ All methods are class methods, so no initialization is required.
61
+ """
62
+ base_url = 'https://github.com/HydrologicEngineeringCenter/hec-downloads/releases/download/'
63
+ valid_versions = [
64
+ "6.6", "6.5", "6.4.1", "6.3.1", "6.3", "6.2", "6.1", "6.0",
65
+ "5.0.7", "5.0.6", "5.0.5", "5.0.4", "5.0.3", "5.0.1", "5.0",
66
+ "4.1", "4.0", "3.1.3", "3.1.2", "3.1.1", "3.0", "2.2"
67
+ ]
68
+ base_dir = Path.cwd()
69
+ examples_dir = base_dir
70
+ projects_dir = examples_dir / 'example_projects'
71
+ csv_file_path = examples_dir / 'example_projects.csv'
72
+
73
+ _folder_df = None
74
+ _zip_file_path = None
75
+
76
+ def __init__(self):
77
+ """Initialize RasExamples and ensure data is loaded"""
78
+ self._ensure_initialized()
79
+
80
+ @property
81
+ def folder_df(self):
82
+ """Access the folder DataFrame"""
83
+ self._ensure_initialized()
84
+ return self._folder_df
85
+
86
+ def _ensure_initialized(self):
87
+ """Ensure the class is initialized with required data"""
88
+ self.projects_dir.mkdir(parents=True, exist_ok=True)
89
+ if self._folder_df is None:
90
+ self._load_project_data()
91
+
92
+ def _load_project_data(self):
93
+ """Load project data from CSV if up-to-date, otherwise extract from zip."""
94
+ logger.debug("Loading project data")
95
+ self._find_zip_file()
96
+
97
+ if not self._zip_file_path:
98
+ logger.info("No example projects zip file found. Downloading...")
99
+ self.get_example_projects()
100
+
101
+ try:
102
+ zip_modified_time = os.path.getmtime(self._zip_file_path)
103
+ except FileNotFoundError:
104
+ logger.error(f"Zip file not found at {self._zip_file_path}.")
105
+ return
106
+
107
+ if self.csv_file_path.exists():
108
+ csv_modified_time = os.path.getmtime(self.csv_file_path)
109
+
110
+ if csv_modified_time >= zip_modified_time:
111
+ logger.info("Loading project data from CSV...")
112
+ try:
113
+ self._folder_df = pd.read_csv(self.csv_file_path)
114
+ logger.info(f"Loaded {len(self._folder_df)} projects from CSV.")
115
+ return
116
+ except Exception as e:
117
+ logger.error(f"Failed to read CSV file: {e}")
118
+ self._folder_df = None
119
+
120
+ logger.info("Extracting folder structure from zip file...")
121
+ self._extract_folder_structure()
122
+ self._save_to_csv()
123
+
124
+ @classmethod
125
+ def extract_project(cls, project_names: Union[str, List[str]]) -> Union[Path, List[Path]]:
126
+ """Extract one or more specific HEC-RAS projects from the zip file.
127
+
128
+ Args:
129
+ project_names: Single project name as string or list of project names
130
+
131
+ Returns:
132
+ Path: Single Path object if one project extracted
133
+ List[Path]: List of Path objects if multiple projects extracted
134
+ """
135
+ logger.debug(f"Extracting projects: {project_names}")
136
+
137
+ # Initialize if needed
138
+ if cls._folder_df is None:
139
+ cls._find_zip_file()
140
+ if not cls._zip_file_path:
141
+ logger.info("No example projects zip file found. Downloading...")
142
+ cls.get_example_projects()
143
+ cls._load_project_data()
144
+
145
+ if isinstance(project_names, str):
146
+ project_names = [project_names]
147
+
148
+ extracted_paths = []
149
+
150
+ for project_name in project_names:
151
+ logger.info("----- RasExamples Extracting Project -----")
152
+ logger.info(f"Extracting project '{project_name}'")
153
+ project_path = cls.projects_dir
154
+
155
+ if (project_path / project_name).exists():
156
+ logger.info(f"Project '{project_name}' already exists. Deleting existing folder...")
157
+ try:
158
+ shutil.rmtree(project_path / project_name)
159
+ logger.info(f"Existing folder for project '{project_name}' has been deleted.")
160
+ except Exception as e:
161
+ logger.error(f"Failed to delete existing project folder '{project_name}': {e}")
162
+ continue
163
+
164
+ project_info = cls._folder_df[cls._folder_df['Project'] == project_name]
165
+ if project_info.empty:
166
+ error_msg = f"Project '{project_name}' not found in the zip file."
167
+ logger.error(error_msg)
168
+ raise ValueError(error_msg)
169
+
170
+ try:
171
+ with zipfile.ZipFile(cls._zip_file_path, 'r') as zip_ref:
172
+ for file in zip_ref.namelist():
173
+ parts = Path(file).parts
174
+ if len(parts) > 1 and parts[1] == project_name:
175
+ relative_path = Path(*parts[1:])
176
+ extract_path = project_path / relative_path
177
+ if file.endswith('/'):
178
+ extract_path.mkdir(parents=True, exist_ok=True)
179
+ else:
180
+ extract_path.parent.mkdir(parents=True, exist_ok=True)
181
+ with zip_ref.open(file) as source, open(extract_path, "wb") as target:
182
+ shutil.copyfileobj(source, target)
183
+
184
+ logger.info(f"Successfully extracted project '{project_name}' to {project_path / project_name}")
185
+ extracted_paths.append(project_path / project_name)
186
+ except Exception as e:
187
+ logger.error(f"An error occurred while extracting project '{project_name}': {str(e)}")
188
+
189
+ # Return single path if only one project was extracted, otherwise return list
190
+ return extracted_paths[0] if len(project_names) == 1 else extracted_paths
191
+
192
+ @classmethod
193
+ def _find_zip_file(cls):
194
+ """Locate the example projects zip file in the examples directory."""
195
+ for version in cls.valid_versions:
196
+ potential_zip = cls.examples_dir / f"Example_Projects_{version.replace('.', '_')}.zip"
197
+ if potential_zip.exists():
198
+ cls._zip_file_path = potential_zip
199
+ logger.info(f"Found zip file: {cls._zip_file_path}")
200
+ break
201
+ else:
202
+ logger.warning("No existing example projects zip file found.")
203
+
204
+ @classmethod
205
+ def get_example_projects(cls, version_number='6.6'):
206
+ """
207
+ Download and extract HEC-RAS example projects for a specified version.
208
+ """
209
+ logger.info(f"Getting example projects for version {version_number}")
210
+ if version_number not in cls.valid_versions:
211
+ error_msg = f"Invalid version number. Valid versions are: {', '.join(cls.valid_versions)}"
212
+ logger.error(error_msg)
213
+ raise ValueError(error_msg)
214
+
215
+ zip_url = f"{cls.base_url}1.0.33/Example_Projects_{version_number.replace('.', '_')}.zip"
216
+
217
+ cls.examples_dir.mkdir(parents=True, exist_ok=True)
218
+
219
+ cls._zip_file_path = cls.examples_dir / f"Example_Projects_{version_number.replace('.', '_')}.zip"
220
+
221
+ if not cls._zip_file_path.exists():
222
+ logger.info(f"Downloading HEC-RAS Example Projects from {zip_url}. \nThe file is over 400 MB, so it may take a few minutes to download....")
223
+ try:
224
+ response = requests.get(zip_url, stream=True)
225
+ response.raise_for_status()
226
+ with open(cls._zip_file_path, 'wb') as file:
227
+ shutil.copyfileobj(response.raw, file)
228
+ logger.info(f"Downloaded to {cls._zip_file_path}")
229
+ except requests.exceptions.RequestException as e:
230
+ logger.error(f"Failed to download the zip file: {e}")
231
+ raise
232
+ else:
233
+ logger.info("HEC-RAS Example Projects zip file already exists. Skipping download.")
234
+
235
+ cls._load_project_data()
236
+ return cls.projects_dir
237
+
238
+ @classmethod
239
+ def _load_project_data(cls):
240
+ """Load project data from CSV if up-to-date, otherwise extract from zip."""
241
+ logger.debug("Loading project data")
242
+
243
+ try:
244
+ zip_modified_time = os.path.getmtime(cls._zip_file_path)
245
+ except FileNotFoundError:
246
+ logger.error(f"Zip file not found at {cls._zip_file_path}.")
247
+ return
248
+
249
+ if cls.csv_file_path.exists():
250
+ csv_modified_time = os.path.getmtime(cls.csv_file_path)
251
+
252
+ if csv_modified_time >= zip_modified_time:
253
+ logger.info("Loading project data from CSV...")
254
+ try:
255
+ cls._folder_df = pd.read_csv(cls.csv_file_path)
256
+ logger.info(f"Loaded {len(cls._folder_df)} projects from CSV.")
257
+ return
258
+ except Exception as e:
259
+ logger.error(f"Failed to read CSV file: {e}")
260
+ cls._folder_df = None
261
+
262
+ logger.info("Extracting folder structure from zip file...")
263
+ cls._extract_folder_structure()
264
+ cls._save_to_csv()
265
+
266
+ @classmethod
267
+ def _extract_folder_structure(cls):
268
+ """
269
+ Extract folder structure from the zip file.
270
+
271
+ Populates folder_df with category and project information.
272
+ """
273
+ folder_data = []
274
+ try:
275
+ with zipfile.ZipFile(cls._zip_file_path, 'r') as zip_ref:
276
+ for file in zip_ref.namelist():
277
+ parts = Path(file).parts
278
+ if len(parts) > 1:
279
+ folder_data.append({
280
+ 'Category': parts[0],
281
+ 'Project': parts[1]
282
+ })
283
+
284
+ cls._folder_df = pd.DataFrame(folder_data).drop_duplicates()
285
+ logger.info(f"Extracted {len(cls._folder_df)} projects.")
286
+ logger.debug(f"folder_df:\n{cls._folder_df}")
287
+ except zipfile.BadZipFile:
288
+ logger.error(f"The file {cls._zip_file_path} is not a valid zip file.")
289
+ cls._folder_df = pd.DataFrame(columns=['Category', 'Project'])
290
+ except Exception as e:
291
+ logger.error(f"An error occurred while extracting the folder structure: {str(e)}")
292
+ cls._folder_df = pd.DataFrame(columns=['Category', 'Project'])
293
+
294
+ @classmethod
295
+ def _save_to_csv(cls):
296
+ """Save the extracted folder structure to CSV file."""
297
+ if cls._folder_df is not None and not cls._folder_df.empty:
298
+ try:
299
+ cls._folder_df.to_csv(cls.csv_file_path, index=False)
300
+ logger.info(f"Saved project data to {cls.csv_file_path}")
301
+ except Exception as e:
302
+ logger.error(f"Failed to save project data to CSV: {e}")
303
+ else:
304
+ logger.warning("No folder data to save to CSV.")
305
+
306
+ @classmethod
307
+ def list_categories(cls):
308
+ """
309
+ List all categories of example projects.
310
+ """
311
+ if cls._folder_df is None or 'Category' not in cls._folder_df.columns:
312
+ logger.warning("No categories available. Make sure the zip file is properly loaded.")
313
+ return []
314
+ categories = cls._folder_df['Category'].unique()
315
+ logger.info(f"Available categories: {', '.join(categories)}")
316
+ return categories.tolist()
317
+
318
+ @classmethod
319
+ def list_projects(cls, category=None):
320
+ """
321
+ List all projects or projects in a specific category.
322
+ """
323
+ if cls._folder_df is None:
324
+ logger.warning("No projects available. Make sure the zip file is properly loaded.")
325
+ return []
326
+ if category:
327
+ projects = cls._folder_df[cls._folder_df['Category'] == category]['Project'].unique()
328
+ logger.info(f"Projects in category '{category}': {', '.join(projects)}")
329
+ else:
330
+ projects = cls._folder_df['Project'].unique()
331
+ logger.info(f"All available projects: {', '.join(projects)}")
332
+ return projects.tolist()
333
+
334
+ @classmethod
335
+ def is_project_extracted(cls, project_name):
336
+ """
337
+ Check if a specific project is already extracted.
338
+ """
339
+ project_path = cls.projects_dir / project_name
340
+ is_extracted = project_path.exists()
341
+ logger.info(f"Project '{project_name}' extracted: {is_extracted}")
342
+ return is_extracted
343
+
344
+ @classmethod
345
+ def clean_projects_directory(cls):
346
+ """Remove all extracted projects from the example_projects directory."""
347
+ logger.info(f"Cleaning projects directory: {cls.projects_dir}")
348
+ if cls.projects_dir.exists():
349
+ try:
350
+ shutil.rmtree(cls.projects_dir)
351
+ logger.info("All projects have been removed.")
352
+ except Exception as e:
353
+ logger.error(f"Failed to remove projects directory: {e}")
354
+ else:
355
+ logger.warning("Projects directory does not exist.")
356
+ cls.projects_dir.mkdir(parents=True, exist_ok=True)
357
+ logger.info("Projects directory cleaned and recreated.")
358
+
359
+ @classmethod
360
+ def download_fema_ble_model(cls, huc8, output_dir=None):
361
+ """
362
+ Download a FEMA Base Level Engineering (BLE) model for a given HUC8.
363
+
364
+ Args:
365
+ huc8 (str): The 8-digit Hydrologic Unit Code (HUC) for the desired watershed.
366
+ output_dir (str, optional): The directory to save the downloaded files. If None, uses the current working directory.
367
+
368
+ Returns:
369
+ str: The path to the downloaded and extracted model directory.
370
+
371
+ Note:
372
+ This method downloads the BLE model from the FEMA website and extracts it to the specified directory.
373
+ """
374
+ # Method implementation...
375
+
376
+ @classmethod
377
+ def _make_safe_folder_name(cls, name: str) -> str:
378
+ """
379
+ Convert a string to a safe folder name by replacing unsafe characters with underscores.
380
+ """
381
+ safe_name = re.sub(r'[^a-zA-Z0-9_\-]', '_', name)
382
+ logger.debug(f"Converted '{name}' to safe folder name '{safe_name}'")
383
+ return safe_name
384
+
385
+ @classmethod
386
+ def _download_file_with_progress(cls, url: str, dest_folder: Path, file_size: int) -> Path:
387
+ """
388
+ Download a file from a URL to a specified destination folder with progress bar.
389
+ """
390
+ local_filename = dest_folder / url.split('/')[-1]
391
+ try:
392
+ with requests.get(url, stream=True) as r:
393
+ r.raise_for_status()
394
+ with open(local_filename, 'wb') as f, tqdm(
395
+ desc=local_filename.name,
396
+ total=file_size,
397
+ unit='iB',
398
+ unit_scale=True,
399
+ unit_divisor=1024,
400
+ ) as progress_bar:
401
+ for chunk in r.iter_content(chunk_size=8192):
402
+ size = f.write(chunk)
403
+ progress_bar.update(size)
404
+ logger.info(f"Successfully downloaded {url} to {local_filename}")
405
+ return local_filename
406
+ except requests.exceptions.RequestException as e:
407
+ logger.error(f"Request failed for {url}: {e}")
408
+ raise
409
+ except Exception as e:
410
+ logger.error(f"Failed to write file {local_filename}: {e}")
411
+ raise
412
+
413
+ @classmethod
414
+ def _convert_size_to_bytes(cls, size_str: str) -> int:
415
+ """
416
+ Convert a human-readable file size to bytes.
417
+ """
418
+ units = {'B': 1, 'KB': 1024, 'MB': 1024**2, 'GB': 1024**3, 'TB': 1024**4}
419
+ size_str = size_str.upper().replace(' ', '')
420
+ if not re.match(r'^\d+(\.\d+)?[BKMGT]B?$', size_str):
421
+ raise ValueError(f"Invalid size string: {size_str}")
422
+
423
+ number, unit = float(re.findall(r'[\d\.]+', size_str)[0]), re.findall(r'[BKMGT]B?', size_str)[0]
424
424
  return int(number * units[unit])