【发布时间】: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_var的count参数对于标量参数(如单个实数)没有任何意义。忽略这一点,否则您的程序将按照此处编写的方式运行(gfortran、netcdf 4.1.3 和 4.2.1)。 -
我也会尝试省略计数,但是当我将
DIMENSION(1)添加到两个REAL变量声明中时,代码就起作用了。