From 88e4e3d223f6ba4af9c39b2c58b85abf0046e6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Falco?= Date: Tue, 13 Jan 2026 11:59:24 +0100 Subject: [PATCH 1/2] create dir before writing to netcdf --- xdas/core/datacollection.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xdas/core/datacollection.py b/xdas/core/datacollection.py index 4a95a5b..e9feca3 100644 --- a/xdas/core/datacollection.py +++ b/xdas/core/datacollection.py @@ -239,6 +239,8 @@ def to_netcdf(self, fname, mode="w", group=None, virtual=None, encoding=None): location = "/".join([name, str(key)]) if group is not None: location = "/".join([group, location]) + if not os.path.exists(os.path.dirname(fname)): + os.makedirs(os.path.dirname(fname), exist_ok=True) self[key].to_netcdf( fname, mode="a", From dde1be0cee202c3164f9e49f5f056b9379d1f1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Falco?= Date: Tue, 13 Jan 2026 15:01:50 +0100 Subject: [PATCH 2/2] Create dirname for dataarray and datacollection, even if dirname is current dir (i.e., empty) --- xdas/core/dataarray.py | 4 ++++ xdas/core/datacollection.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/xdas/core/dataarray.py b/xdas/core/dataarray.py index 2dd762a..7a9ae94 100644 --- a/xdas/core/dataarray.py +++ b/xdas/core/dataarray.py @@ -3,6 +3,7 @@ import re import warnings from functools import partial +import os import h5netcdf import h5py @@ -904,6 +905,9 @@ def to_netcdf(self, fname, mode="w", group=None, virtual=None, encoding=None): attrs = {} if self.attrs is None else self.attrs attrs |= {"coordinate_interpolation": mapping} if mapping else attrs name = "__values__" if self.name is None else self.name + if os.path.dirname(fname) is not "" and not os.path.exists(os.path.dirname(fname)): + os.makedirs(os.path.dirname(fname), exist_ok=True) + with h5netcdf.File(fname, mode=mode) as file: if group is not None and group not in file: file.create_group(group) diff --git a/xdas/core/datacollection.py b/xdas/core/datacollection.py index e9feca3..a2df095 100644 --- a/xdas/core/datacollection.py +++ b/xdas/core/datacollection.py @@ -239,7 +239,7 @@ def to_netcdf(self, fname, mode="w", group=None, virtual=None, encoding=None): location = "/".join([name, str(key)]) if group is not None: location = "/".join([group, location]) - if not os.path.exists(os.path.dirname(fname)): + if os.path.dirname(fname) is not "" and not os.path.exists(os.path.dirname(fname)): os.makedirs(os.path.dirname(fname), exist_ok=True) self[key].to_netcdf( fname,