From ec254d117c59e5547ddc24c3523ee07cad868292 Mon Sep 17 00:00:00 2001 From: Mirochill <200482516+Mirochill@users.noreply.github.com> Date: Mon, 25 May 2026 21:25:08 +0200 Subject: [PATCH] Handle missing netCDF4 dependency --- seabird/netcdf.py | 9 ++++++++- tests/test_netcdf.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/test_netcdf.py 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")