Skip to content

Commit a371621

Browse files
author
Jeff Whitaker
authored
Merge pull request Unidata#1387 from headtr1ck/pathlike
support Pathlike in Dataset.fromcdl
2 parents b04f3ad + d419ea8 commit a371621

5 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ netcdftime/_netcdftime.c
1313
venv/
1414
.eggs/
1515
.idea/
16+
.vscode/

‎Changelog‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
since version 1.7.2 release
22
===========================
3+
* support os.PathLike arguments for `Dataset.fromcdl` and raise a `FileNotFoundError`
4+
if the cdl is missing and a `FileExistsError` if the nc file already exists (PR #1387)
35
* raise more informative error when trying to iterate or
46
perform a membership operation on a Dataset (issue #1383)
57
* fix type hint for createEnumType (issue #1378)

‎src/netCDF4/__init__.pyi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class Dataset:
373373
def get_variables_by_attributes(self, **kwargs: Callable[[Any], bool] | Any) -> list[Variable]: ...
374374
@staticmethod
375375
def fromcdl(
376-
cdlfilename: str, ncfilename: str | None = None, mode: AccessMode = "a", format: Format = "NETCDF4"
376+
cdlfilename: str | os.PathLike, ncfilename: str | os.PathLike | None = None, mode: AccessMode = "a", format: Format = "NETCDF4"
377377
) -> Dataset: ...
378378
@overload
379379
def tocdl(self, coordvars: bool = False, data: bool = False, outfile: None = None) -> str: ...

‎src/netCDF4/_netCDF4.pyx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,19 +3502,27 @@ Dataset instance for `ncfilename` is returned.
35023502
[ncgen]: https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_utilities_guide.html#ncgen_guide
35033503
[cdl]: https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_utilities_guide.html#cdl_guide
35043504
"""
3505+
filepath = pathlib.Path(cdlfilename)
35053506
if ncfilename is None:
3506-
filepath = pathlib.Path(cdlfilename)
35073507
ncfilename = filepath.with_suffix('.nc')
3508+
else:
3509+
ncfilename = pathlib.Path(ncfilename)
35083510
formatcodes = {'NETCDF4': 4,
35093511
'NETCDF4_CLASSIC': 7,
35103512
'NETCDF3_CLASSIC': 3,
35113513
'NETCDF3_64BIT': 6, # legacy
35123514
'NETCDF3_64BIT_OFFSET': 6,
35133515
'NETCDF3_64BIT_DATA': 5}
3516+
35143517
if format not in formatcodes:
35153518
raise ValueError('illegal format requested')
3519+
if not filepath.exists():
3520+
raise FileNotFoundError(filepath)
3521+
if ncfilename.exists():
3522+
raise FileExistsError(ncfilename)
3523+
35163524
ncgenargs="-knc%s" % formatcodes[format]
3517-
subprocess.run(["ncgen", ncgenargs, "-o", ncfilename, cdlfilename], check=True)
3525+
subprocess.run(["ncgen", ncgenargs, "-o", str(ncfilename), str(filepath)], check=True)
35183526
return Dataset(ncfilename, mode=mode)
35193527

35203528
def tocdl(self,coordvars=False,data=False,outfile=None):

‎test/test_cdl.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,18 @@ def test_fromcdl(self):
6868
assert len(f1.dimensions["d"]) == len(f2.dimensions["d"])
6969
assert (f1["ub"][:] == f2["ub"][:]).all()
7070
assert (f1["sb"][:] == f2["sb"][:]).all()
71+
72+
# test if os.PathLike works
73+
with netCDF4.Dataset.fromcdl(pathlib.Path("ubyte.cdl"), ncfilename=pathlib.Path("ubyte3.nc")) as f3:
74+
assert f1.variables.keys() == f3.variables.keys()
7175

76+
# check if correct errors are raised
77+
self.assertRaises(FileNotFoundError, netCDF4.Dataset.fromcdl, "doesnotexist.cdl")
78+
self.assertRaises(FileExistsError, netCDF4.Dataset.fromcdl, "ubyte.cdl", ncfilename="ubyte2.nc")
79+
80+
# cleanup
7281
os.remove("ubyte2.nc")
82+
os.remove("ubyte3.nc")
7383

7484
def tearDown(self):
7585
# Remove the temporary files

0 commit comments

Comments
 (0)