检查 NETCDF 文件是否已经存在

Checking if a NETCDF file already exists

检查 Fortran90 中是否存在给定 netCDF 文件的最佳方法是什么?我有一个 Fortran 程序,它模拟多个系统并将数据存储在 NETCDF 文件中,我希望能够对其进行设置,以便我可以 运行 代码与已经存在的 netCDF 文件并仅附加额外的数据。

它似乎适用于以下内容:

status = nf90_open(filePath, NF90_WRITE, netcdfID)
if (status /= nf90_noerr) then
    new_status = nf90_create(filePath, NF90_NETCDF4, netcdfID)
    ! setup
else
    ! check file has correct dimensions etc
end if

但我希望能够具体说明错误,但是 the documentation 不是很具体说明当文件已经存在时错误代码是什么。

我也考虑过检查 nf90_create(...) 调用的状态,但我不知道如何同时指定 cmode=NF90_NETCDF4cmode=NF90_NOCLOBBER

老实说,当我将 NetCDF 与 Fortran 结合使用并需要查看文档时,我会查看 https://www.unidata.ucar.edu/software/netcdf/docs/modules.html 处的 C 文档,'translate' 将其转到 Fortran API(例如用 nf90_ 替换 nc_)因为 C 文档比 Fortran 文档更新得多。

似乎有这些可能的状态结果:

  • NF90_NOERR 没有错误。
  • NF90_EPERM 正在尝试在您无权打开文件的目录中创建 netCDF 文件。
  • NF90_ENFILE打开的文件太多
  • NF90_ENOMEM 内存不足。
  • NF90_EHDFERR HDF5 错误。 (仅限 NetCDF-4 文件。)
  • NF90_EDIMMETA netCDF-4 维度元数据错误。 (仅限 NetCDF-4 文件。)
其中

None 表明您打开了一个不存在的文件。也许试试看?

但是如果只想知道文件是否已经存在,为什么不用Fortran的INQUIRE语句呢?

logical :: fileExists
...
inquire(file=filePath, exist=fileExists)
if (fileExists) then
    call nf90_open(
else
    call nf90_create(
end if