【问题标题】:netcdf fortran how to retrieve the total number of global attributesnetcdf fortran如何检索全局属性的总数
【发布时间】:2021-08-03 21:06:42
【问题描述】:

我试图弄清楚如何发现 netcdf 文件有多少全局属性。通过这样做,我正在使用调用:

status = nf90_Inquire_Variable(ncid, NF90_GLOBAL, nAtts=natts)

为了测试它,我创建了一个简单的示例。首先,它创建一个具有全局属性的文件,然后尝试读取它:

...

! create the file
call check( nf90_create("test.nc", NF90_NETCDF4, ncid) )
call check( nf90_put_att(ncid, NF90_GLOBAL, "date", "01/01/2021") )
call check( nf90_put_att(ncid, NF90_GLOBAL, "time", "00:00:00") )
call check( nf90_put_att(ncid, NF90_GLOBAL, "seconds", 1000) )
call check( nf90_close(ncid) )

 
! Read the file.
status = nf90_open("test.nc", NF90_NOWRITE, ncid)
call check(status)

! how many global attributes?
status = nf90_Inquire_Variable(ncid, NF90_GLOBAL, nAtts=natts)
call check(status)

! bye
status = nf90_close(ncid)
call check(status)

netcdf 文件已正确创建

netcdf simpletest {

// global attributes:
        :date = "01/01/2021" ;
        :time = "00:00:00" ;
        :seconds = 1000 ;
}

但出现以下错误:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7f34c7156d01 in ???
#1  0x7f34c7155ed5 in ???
#2  0x7f34c6e2020f in ???
#3  0x7f34c749304e in nf_inq_var_
    at /to/some/path/netcdf-fortran-4.5.1/fortran/nf_genvar.f90:181
#4  0x7f34c74ebedf in __netcdf_MOD_nf90_inquire_variable
    at /to/some/path/netcdf-fortran-4.5.1/fortran/netcdf4_variables.f90:293
Violació de segment (s'ha bolcat la memòria)

GDB 停在:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f5e04e in nf_inq_var (ncid=1, varid=0, name=..., xtype=1077936128, ndims=1073741824, dimids=..., natts=3, _name=256)
    at nf_genvar.f90:181
warning: Source file is more recent than executable.
181      dimids(1:ndims) = cdimids(ndims:1:-1)+1

所以在我看来我做错了什么。我是否以应有的方式使用原始调用?

如果没有,怎么办?通过阅读 netcdf fortran 文档,我找不到它。

我正在使用以下版本:

  • gfortran 9.3
  • zlib 1.2.11
  • hdf5 1.10.5
  • netcdf-c 4.7.1
  • netcdf-fortran 4.5.1

编辑:

正如下面其中一个 cmets 中所建议的,我做了几个具有不同选择的最小可重现示例,因为我对这些选项有点困惑。

我还为 nc_inq_natts 调用创建了一个新的 fortran 绑定,但重命名为 nf90_inq_natts。

  1. a fortran file which creates and read a netcdf file
  2. a c file which creates a netcdf file with 1 global attribute
  3. a fortran file which only read the file from point 2

所有文件在运行时都以调试模式编译。

出现以下输出:

创建一个新文件(C):

*** SUCCESS writing example file simple_xy.nc!

只读取 netcdf 文件(fortran):

 nf90_inquire_variable ... 
 NetCDF: Variable not found
 nf90_inq_natts ... 
 NetCDF: Not a valid ID
 Global attributes (inquire_variable(GLOBAL)):   1788528357  <-- not working
 Global attributes (inq_natts):    260315136                 <-- not working

创建和读取文件(fortran):

 nf90_inquire_variable ... 
 nf90_inq_natts ... 
 NetCDF: Not a valid ID
 Global attributes (inquire_variable(GLOBAL)):            3   <-- it works
 Global attributes (inq_natts):   1256308480                  <-- not working

结果:

  • 我无法重现我第一次提到的错误。 :-/
  • nc_inq_natts 在与 Fortran 混合时似乎不起作用(NetCDF:无效 ID)
  • nf90_inquire_variables 在同一文件中创建 netcdf 文件时似乎可以工作 (?)。关于文档,它不应该发生。
  • 取自 github 的 C 示例按预期工作

【问题讨论】:

  • 请展示一个完整的示例程序:见minimal reproducible example
  • gfortran 有几个选项可以调试程序。尝试 -fcheck=all 开始。
  • 请注意您的 gdb 会话包含“警告:源文件比可执行文件更新”,这意味着我们不能依赖您报告的内容。

标签: fortran netcdf


【解决方案1】:
From a thread on the netcdfgroup mail list found by googling:
A variable ID of NF90_GLOBAL is explicitly allowed with
nf90_inquire_attribute, but not with nf90_inquire_variable.  The intended
functions for discovering global attributes are nf90_inquire and
nf90_inquire_attribute.  With a bit of study, the F90 documentation on this
topic seems rather clear to me.

Attempting to use nf90_inquire_variable for global attributes is a
mis-application of that function.  Furthermore, that function is probably
returning the appropriate error code in this case, as documented:  "The
variable ID is invalid for the specified netCDF dataset."

即,将您调用的函数更改为nf90_inquire_attribute

【讨论】:

  • 从这个意义上说,这就解释了为什么会出错。但它令人困惑,因为它没有正确引发受控错误。
  • 然而它没有回答如何发现一个 netcdf 文件有多少全局参数:-/。据我了解,nf90_inquire_attribute 不允许这样做。
  • @rgrun 它在unidata.ucar.edu/software/netcdf/docs-fortran/… 说“每个变量的属性从 1(第一个属性)到 NATTS 编号,其中 NATTS 是从调用返回的变量的属性数到 NF90_INQ_VARNATTS" 不幸的是,除了alioth-lists.debian.net/pipermail/pkg-grass-devel/2015-January/… 之外,我找不到任何适合 NF90_INQ_VARNATTS 的文档
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
相关资源
最近更新 更多