【问题标题】:Writing a scalar variable along an unlimited dimension in NetCDF在 NetCDF 中沿无限维度编写标量变量
【发布时间】:2014-08-23 17:10:28
【问题描述】:

我正在尝试将流体动力学模型中的时间变量写入 netcdf 文件(无限维度变量)。我在 Fortran90 中附上了一个简化的代码示例,突出了我的问题。

根据用户指定的输出间隔(本示例为 10 次),在模拟期间多次调用写入 netcdf 文件的子例程。我可以在第一次调用子例程时创建文件并添加属性。

在子例程的后续调用期间,我无法正确地将时间变量写入文件的 start 和 count 变量。这是错误,在编写模型时间变量时,我在尝试编译代码时收到:错误:通用'nf90_put_var'没有特定功能

PROGRAM test_netcdf

  IMPLICIT NONE

  INTEGER :: N
  REAL :: time_step = 2.

  ! Call efdc_netcdf 10 times
  DO N=1,10

     CALL efdc_netcdf(N, time_step)

     time_step=time_step + 1.

  ENDDO

END PROGRAM test_netcdf

************************************
! Create NetCDF file and write variables
SUBROUTINE efdc_netcdf(N, time_step)

USE netcdf
IMPLICIT NONE

LOGICAL,SAVE::FIRST_NETCDF=.FALSE.
CHARACTER (len = *), PARAMETER :: FILE_NAME = "efdc_test.nc"
INTEGER :: ncid, status
INTEGER :: time_dimid
INTEGER :: ts_varid, time_varid

INTEGER :: start(1), count(1)
INTEGER :: deltat

INTEGER :: N
REAL :: time_step

start=(/N/)
count=(/1/)

! Create file and add attributes during first call of efdc_netcdf
IF(.NOT.FIRST_NETCDF)THEN

    status=nf90_create(FILE_NAME, NF90_CLOBBER, ncid)

    ! Define global attributes once
    status=nf90_put_att(ncid, NF90_GLOBAL, 'format', 'netCDF-3 64bit offset file')
    status=nf90_put_att(ncid, NF90_GLOBAL, 'os', 'Linux')
    status=nf90_put_att(ncid, NF90_GLOBAL, 'arch', 'x86_64')

    ! Define deltat variable
    status=nf90_def_var(ncid,'deltat',nf90_int,ts_varid)

    ! Define model time dimension
    status=nf90_def_dim(ncid,'efdc_time',nf90_unlimited,time_dimid)

    ! Define model time variable
    status=nf90_def_var(ncid,'efdc_time',nf90_real,time_dimid,time_varid)

    status=nf90_enddef(ncid)

    ! Put deltat during first call
    deltat=7
    status=nf90_put_var(ncid, ts_varid, deltat)

    FIRST_NETCDF=.TRUE.

ENDIF

! Put model time variable
status=nf90_put_var(ncid, time_varid, time_step, start=start, count=count)

! Close file at end of DO loop
IF(N.EQ.10) THEN
   status=nf90_close(ncid)
ENDIF

RETURN
END SUBROUTINE efdc_netcdf

【问题讨论】:

  • 删除之前的评论。 nf90_put_varcount 参数对于标量参数(如单个实数)没有任何意义。忽略这一点,否则您的程序将按照此处编写的方式运行(gfortran、netcdf 4.1.3 和 4.2.1)。
  • 我也会尝试省略计数,但是当我将DIMENSION(1) 添加到两个REAL 变量声明中时,代码就起作用了。

标签: fortran fortran90 netcdf


【解决方案1】:

问题在于编译器标志的行:

status=nf90_put_var(ncid, time_varid, time_step, start=start, count=count)

您(正确地)尝试将标量变量 time_step 写入变量 time_varid 的特定索引 (start),该变量在一维无限范围维度上定义。但是,在这种情况下,可选参数计数没有意义;您正在编写标量,并且 count 只能为 1。因此,采用单个标量作为输入的 nf90_put_var() 的 fortran 绑定没有为 count 定义可选参数,这就是您得到的原因来自编译器的“通用 nf90_put_var 没有特定函数”错误。这一切都非常合理,但无论是错误消息还是文档都对找出解决问题的方法没有太大帮助。

您可以通过将 time_step 数据放入 real, dimension(1) 变量并将其放入来修复您的代码;但最简单的方法是去掉 count 规范,无论如何这里都不需要:

status=nf90_put_var(ncid, time_varid, time_step, start=start)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多