首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NetCDF中沿无限维度编写标量变量

在NetCDF中沿无限维度编写标量变量
EN

Stack Overflow用户
提问于 2014-07-03 03:05:45
回答 1查看 1.3K关注 0票数 3

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

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

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-07-03 04:24:35

问题出在编译器标记的行中:

代码语言:javascript
复制
status=nf90_put_var(ncid, time_varid, time_step, start=start, count=count)

您正在(正确地)尝试将一个标量变量time_step写入一个特定的索引(start)中,该变量是在一个一维的无限范围维度上定义的变量time_varid。然而,在这种情况下,可选参数count没有意义;您正在编写标量,并且count只能为1。因此,接受单个标量作为输入的nf90_put_var()的fortran绑定没有为count定义可选参数,这就是为什么您从编译器得到"no specific function for the generic‘nf90_put_var“错误的原因。这一切都是完全合理的,但无论是错误消息还是文档都不能很好地帮助您找出解决问题的方法。

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

代码语言:javascript
复制
status=nf90_put_var(ncid, time_varid, time_step, start=start)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24539102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档