|
21 | 21 | from pathlib import Path |
22 | 22 |
|
23 | 23 | import numpy as np |
24 | | -import netCDF4 |
25 | 24 |
|
26 | 25 | from pysteps.cascade import get_method as cascade_get_method |
27 | 26 | from pysteps.cascade.bandpass_filters import filter_gaussian |
| 27 | +from pysteps.exceptions import MissingOptionalDependency |
28 | 28 | from pysteps.utils import get_method as utils_get_method |
29 | 29 |
|
| 30 | +try: |
| 31 | + import netCDF4 |
| 32 | + |
| 33 | + NETCDF4_IMPORTED = True |
| 34 | +except ImportError: |
| 35 | + NETCDF4_IMPORTED = False |
| 36 | + |
30 | 37 |
|
31 | 38 | def stack_cascades(R_d, donorm=True): |
32 | 39 | """Stack the given cascades into a larger array. |
@@ -298,9 +305,15 @@ def decompose_NWP( |
298 | 305 |
|
299 | 306 | Returns |
300 | 307 | ------- |
301 | | - Nothing |
| 308 | + None |
302 | 309 | """ |
303 | 310 |
|
| 311 | + if not NETCDF4_IMPORTED: |
| 312 | + raise MissingOptionalDependency( |
| 313 | + "netCDF4 package is required to save the decomposed NWP data, " |
| 314 | + "but it is not installed" |
| 315 | + ) |
| 316 | + |
304 | 317 | # Make a NetCDF file |
305 | 318 | output_date = f"{analysis_time.astype(datetime.datetime):%Y%m%d%H%M%S}" |
306 | 319 | outfn = Path(output_path) / f"cascade_{NWP_model}_{output_date}.nc" |
@@ -443,6 +456,12 @@ def load_NWP(input_nc_path_decomp, input_path_velocities, start_time, n_timestep |
443 | 456 | of the advection field for the (NWP) model field per forecast lead time. |
444 | 457 | """ |
445 | 458 |
|
| 459 | + if not NETCDF4_IMPORTED: |
| 460 | + raise MissingOptionalDependency( |
| 461 | + "netCDF4 package is required to load the decomposed NWP data, " |
| 462 | + "but it is not installed" |
| 463 | + ) |
| 464 | + |
446 | 465 | # Open the file |
447 | 466 | ncf_decomp = netCDF4.Dataset(input_nc_path_decomp, "r", format="NETCDF4") |
448 | 467 | velocities = np.load(input_path_velocities) |
|
0 commit comments