Skip to content

Commit

Permalink
Netcdf is an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dnerini committed Feb 18, 2022
1 parent 9185a5d commit 6d39925
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pysteps/blending/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
from pathlib import Path

import numpy as np
import netCDF4

from pysteps.cascade import get_method as cascade_get_method
from pysteps.cascade.bandpass_filters import filter_gaussian
from pysteps.exceptions import MissingOptionalDependency
from pysteps.utils import get_method as utils_get_method

try:
import netCDF4

NETCDF4_IMPORTED = True
except ImportError:
NETCDF4_IMPORTED = False


def stack_cascades(R_d, donorm=True):
"""Stack the given cascades into a larger array.
Expand Down Expand Up @@ -298,9 +305,15 @@ def decompose_NWP(
Returns
-------
Nothing
None
"""

if not NETCDF4_IMPORTED:
raise MissingOptionalDependency(
"netCDF4 package is required to save the decomposed NWP data, "
"but it is not installed"
)

# Make a NetCDF file
output_date = f"{analysis_time.astype(datetime.datetime):%Y%m%d%H%M%S}"
outfn = Path(output_path) / f"cascade_{NWP_model}_{output_date}.nc"
Expand Down Expand Up @@ -443,6 +456,12 @@ def load_NWP(input_nc_path_decomp, input_path_velocities, start_time, n_timestep
of the advection field for the (NWP) model field per forecast lead time.
"""

if not NETCDF4_IMPORTED:
raise MissingOptionalDependency(
"netCDF4 package is required to load the decomposed NWP data, "
"but it is not installed"
)

# Open the file
ncf_decomp = netCDF4.Dataset(input_nc_path_decomp, "r", format="NETCDF4")
velocities = np.load(input_path_velocities)
Expand Down

0 comments on commit 6d39925

Please sign in to comment.