我正在尝试将流体动力学模型的时间变量写入netcdf文件(无限维变量) . 我在Fortran90中附加了一个简化的代码示例,突出了我的问题 .
根据用户指定的输出间隔(本例中为10次),在模拟期间多次调用写入netcdf文件的子例程 . 我可以在第一次调用子例程时创建文件并添加属性 .
在后续的子程序调用期间,我无法将启动和计数变量设置为正确,以便将时间变量写入文件 . 这是错误,在编写模型时间变量时,我在尝试编译代码时收到:错误:通用'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
1 回答
问题在于编译器标志:
您(正确地)尝试将标量变量
time_step
写入沿着变量time_varid
的特定索引(start
),该变量在1维无限范围维度上定义 . 但是,在这种情况下,可选参数计数没有意义;你正在编写标量,并且计数只能是1.因此,为了输入单个标量而使用nf90_put_var()
的fortran绑定为什么你从编译器得到了"no specific function for the generic' nf90_put_var"错误 . 这一切都是完全合理的,但是错误信息和文档在确定如何解决问题方面都没有多大帮助 .您可以通过将time_step数据放入
real, dimension(1)
变量来修复代码,然后将其放入;但最容易的就是摆脱计数规范,无论如何这里都没有必要: