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 4a95a5b..a2df095 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 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, mode="a",