hydro_file¶
The hydro_file module provides utilities for file I/O operations, including reading and writing various data formats commonly used in hydrological applications.
Core Functions¶
read_ts_xrdataset¶
1 2 3 4 5 6 7 | |
Reads time series data from NetCDF files into xarray Dataset format.
Example:
1 2 3 4 5 | |
write_ts_xrdataset¶
1 2 3 4 5 6 | |
Writes xarray Dataset to NetCDF file.
read_csv¶
1 | |
Reads CSV files with enhanced error handling and encoding detection.
write_csv¶
1 | |
Writes DataFrame to CSV with proper encoding and error handling.
JSON Functions¶
serialize_json¶
1 | |
Saves a dictionary to a JSON file.
unserialize_json¶
1 | |
Loads a JSON file into a dictionary.
serialize_json_np¶
1 | |
Saves a dictionary containing NumPy arrays to a JSON file.
Pickle Functions¶
serialize_pickle¶
1 | |
Saves an object to a pickle file.
unserialize_pickle¶
1 | |
Loads an object from a pickle file.
NumPy Array Functions¶
serialize_numpy¶
1 | |
Saves a NumPy array to a .npy file.
unserialize_numpy¶
1 | |
Loads a NumPy array from a .npy file.
File Management Functions¶
get_lastest_file_in_a_dir¶
1 | |
Gets the most recently modified .pth file in a directory.
get_cache_dir¶
1 | |
Gets the appropriate cache directory for the current platform.
Classes¶
NumpyArrayEncoder¶
1 | |
JSON encoder that handles NumPy arrays and scalars.
API Reference¶
Author: Wenyu Ouyang Date: 2024-08-15 10:08:59 LastEditTime: 2025-02-02 06:27:44 LastEditors: Wenyu Ouyang Description: some methods for file operations FilePath: \hydroutils\hydroutils\hydro_file.py Copyright (c) 2023-2024 Wenyu Ouyang. All rights reserved.
NumpyArrayEncoder
¶
Bases: JSONEncoder
JSON encoder that handles NumPy arrays and scalar types.
This encoder converts NumPy arrays and scalar types to Python native types that can be serialized by the standard JSON encoder.
Source code in hydroutils/hydro_file.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
convert_ndarray(array)
¶
Convert a NumPy array to a nested list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
ndarray
|
NumPy array to convert. |
required |
Returns:
| Type | Description |
|---|---|
|
list or scalar: Python native type equivalent of the array. |
Source code in hydroutils/hydro_file.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
default(obj)
¶
Convert NumPy types to JSON serializable objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Object to encode. |
required |
Returns:
| Type | Description |
|---|---|
|
JSON serializable object. |
Source code in hydroutils/hydro_file.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
download_a_file_from_google_drive(drive, dir_id, download_dir)
¶
Download files from Google Drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drive
|
Google Drive API instance. |
required | |
dir_id
|
str
|
ID of the Google Drive directory. |
required |
download_dir
|
str
|
Local directory to save downloaded files. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
Handles both files and folders recursively. Skips already downloaded files.
Source code in hydroutils/hydro_file.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
download_excel(data_url, temp_file)
¶
Download an Excel file from URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_url
|
str
|
URL of the Excel file to download. |
required |
temp_file
|
str
|
Path where the Excel file will be saved. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
Only downloads if the file doesn't already exist locally.
Source code in hydroutils/hydro_file.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
download_one_zip(data_url, data_dir)
¶
Download one zip file from URL and extract it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_url
|
str
|
The URL of the file to download. |
required |
data_dir
|
str
|
Directory where the file will be downloaded and extracted. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
The function will create the target directory if it doesn't exist.
Source code in hydroutils/hydro_file.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
download_small_file(data_url, temp_file)
¶
Download a small file from URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_url
|
str
|
URL of the file to download. |
required |
temp_file
|
str
|
Path where the downloaded file will be saved. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
Uses requests library for downloading.
Source code in hydroutils/hydro_file.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
download_small_zip(data_url, data_dir)
¶
Download a small zip file and extract it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_url
|
str
|
URL of the zip file to download. |
required |
data_dir
|
str
|
Directory where the file will be downloaded and extracted. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
Uses urllib.request for downloading small files.
Source code in hydroutils/hydro_file.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
download_zip_files(urls, the_dir)
¶
Download multiple files from multiple URLs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urls
|
list
|
List of URLs to download files from. |
required |
the_dir
|
Path
|
Directory where downloaded files will be stored. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
Uses a temporary directory for caching during download.
Source code in hydroutils/hydro_file.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
get_cache_dir(app_name='hydro')
¶
Get the appropriate cache directory for the current operating system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str
|
Name of the application. Defaults to "hydro". |
'hydro'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
Path to the cache directory. |
Note
Creates the directory if it doesn't exist. Follows OS-specific conventions: - Windows: %LOCALAPPDATA%/app_name/Cache - macOS: ~/Library/Caches/app_name - Linux: ~/.cache/app_name
Source code in hydroutils/hydro_file.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
get_lastest_file_in_a_dir(dir_path)
¶
Get the last file in a directory
Parameters¶
dir_path : str the directory
Returns¶
str the path of the weight file
Source code in hydroutils/hydro_file.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
get_latest_file_in_a_lst(lst)
¶
Get the most recently modified file from a list of files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lst
|
list
|
List of file paths. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
Path of the most recently modified file. |
Source code in hydroutils/hydro_file.py
421 422 423 424 425 426 427 428 429 430 431 432 | |
is_there_file(zipfile_path, unzip_dir)
¶
if a file has existed
Source code in hydroutils/hydro_file.py
95 96 97 98 99 100 101 | |
serialize_json(my_dict, my_file, encoding='utf-8', ensure_ascii=True)
¶
Serialize a dictionary to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_dict
|
dict
|
Dictionary to serialize. |
required |
my_file
|
str
|
Path to the output JSON file. |
required |
encoding
|
str
|
File encoding. Defaults to "utf-8". |
'utf-8'
|
ensure_ascii
|
bool
|
If True, ensure ASCII output. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in hydroutils/hydro_file.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
serialize_json_np(my_dict, my_file)
¶
Serialize a dictionary containing NumPy arrays to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_dict
|
dict
|
Dictionary containing NumPy arrays to serialize. |
required |
my_file
|
str
|
Path to the output JSON file. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Note
Uses NumpyArrayEncoder to handle NumPy types.
Source code in hydroutils/hydro_file.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
serialize_numpy(my_array, my_file)
¶
Save a NumPy array to a binary file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_array
|
ndarray
|
NumPy array to save. |
required |
my_file
|
str
|
Path to the output file. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in hydroutils/hydro_file.py
375 376 377 378 379 380 381 382 383 384 385 | |
serialize_pickle(my_object, my_file)
¶
Serialize an object to a pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_object
|
object
|
Python object to serialize. |
required |
my_file
|
str
|
Path to the output pickle file. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in hydroutils/hydro_file.py
347 348 349 350 351 352 353 354 355 356 357 358 | |
unserialize_json(my_file)
¶
Load a JSON file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_file
|
str
|
Path to the JSON file to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
Python object (typically dict or list) loaded from the JSON file. |
Source code in hydroutils/hydro_file.py
273 274 275 276 277 278 279 280 281 282 283 284 | |
unserialize_json_ordered(my_file)
¶
Load a JSON file into an OrderedDict, preserving key order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_file
|
str
|
Path to the JSON file to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OrderedDict |
Dictionary with preserved key order from the JSON file. |
Source code in hydroutils/hydro_file.py
259 260 261 262 263 264 265 266 267 268 269 270 | |
unserialize_numpy(my_file)
¶
Load a NumPy array from a binary file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_file
|
str
|
Path to the NumPy array file. |
required |
Returns:
| Type | Description |
|---|---|
|
np.ndarray: NumPy array loaded from the file. |
Source code in hydroutils/hydro_file.py
388 389 390 391 392 393 394 395 396 397 | |
unserialize_pickle(my_file)
¶
Load an object from a pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
my_file
|
str
|
Path to the pickle file to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
Python object loaded from the pickle file. |
Source code in hydroutils/hydro_file.py
361 362 363 364 365 366 367 368 369 370 371 372 | |
unzip_file(data_zip, path_unzip)
¶
Extract a zip file to the specified directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_zip
|
str
|
Path to the zip file to extract. |
required |
path_unzip
|
str
|
Directory where the contents will be extracted. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in hydroutils/hydro_file.py
45 46 47 48 49 50 51 52 53 54 55 56 | |
unzip_nested_zip(dataset_zip, path_unzip)
¶
Extract a zip file including any nested zip files If a file's name is "xxx_", it seems the "extractall" function in the "zipfile" lib will throw an OSError, so please check the unzipped files manually when this occurs. Parameters
dataset_zip: the zip file path_unzip: where it is unzipped
Source code in hydroutils/hydro_file.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
zip_extract(the_dir)
¶
Extract the downloaded zip files in the specified directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
the_dir
|
Path
|
The directory containing zip files to extract. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in hydroutils/hydro_file.py
30 31 32 33 34 35 36 37 38 39 40 41 42 | |