diff --git a/seabird/netcdf.py b/seabird/netcdf.py index 80c988b..2ef4169 100644 --- a/seabird/netcdf.py +++ b/seabird/netcdf.py @@ -11,7 +11,8 @@ try: import netCDF4 -except: +except ImportError: + netCDF4 = None module_logger.warning("netCDF4 is not available.") @@ -33,6 +34,12 @@ def cnv2nc(data, filename): profile = cnv.fCNV("CTD.cnv") cnv2nc(profile, "CTD.nc") """ + if netCDF4 is None: + raise ImportError( + "netCDF4 is required to export CNV data to NetCDF. " + "Install it with `pip install 'seabird[CDF]'` or `pip install netCDF4`." + ) + logging.info("Saving netcdf output file: %s" % filename) nc = netCDF4.Dataset(filename, "w", format="NETCDF4") diff --git a/tests/test_netcdf.py b/tests/test_netcdf.py new file mode 100644 index 0000000..bf777d3 --- /dev/null +++ b/tests/test_netcdf.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +import pytest + +from seabird import netcdf + + +def test_cnv2nc_requires_netcdf4(monkeypatch): + monkeypatch.setattr(netcdf, "netCDF4", None) + + with pytest.raises(ImportError, match="pip install 'seabird\\[CDF\\]'"): + netcdf.cnv2nc(object(), "output.nc")