Skip to content

Commit 6d39925

Browse files
committed
Netcdf is an optional dependency
1 parent 9185a5d commit 6d39925

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

pysteps/blending/utils.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@
2121
from pathlib import Path
2222

2323
import numpy as np
24-
import netCDF4
2524

2625
from pysteps.cascade import get_method as cascade_get_method
2726
from pysteps.cascade.bandpass_filters import filter_gaussian
27+
from pysteps.exceptions import MissingOptionalDependency
2828
from pysteps.utils import get_method as utils_get_method
2929

30+
try:
31+
import netCDF4
32+
33+
NETCDF4_IMPORTED = True
34+
except ImportError:
35+
NETCDF4_IMPORTED = False
36+
3037

3138
def stack_cascades(R_d, donorm=True):
3239
"""Stack the given cascades into a larger array.
@@ -298,9 +305,15 @@ def decompose_NWP(
298305
299306
Returns
300307
-------
301-
Nothing
308+
None
302309
"""
303310

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+
304317
# Make a NetCDF file
305318
output_date = f"{analysis_time.astype(datetime.datetime):%Y%m%d%H%M%S}"
306319
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
443456
of the advection field for the (NWP) model field per forecast lead time.
444457
"""
445458

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+
446465
# Open the file
447466
ncf_decomp = netCDF4.Dataset(input_nc_path_decomp, "r", format="NETCDF4")
448467
velocities = np.load(input_path_velocities)

0 commit comments

Comments
 (0)