Fortran NetCDF - 增加了新的维度,需要用零填充 [英] Fortran NetCDF - added new dimension need to fill it with zeroes
问题描述
retval = nf_open(cfn, NF_WRITE,ncid)
if(retval .ne。nf_noerr)调用handle_err(retval)
retval = nf_redef(ncid)
if(retval .ne。nf_noerr)调用handle_err(retval)
retval = nf_def_dim(ncid,xyz,len,dimid_xyz)
if(retval .ne。nf_noerr)call handle_err(retval)
retval = nf_enddef(ncid)
现在我希望能够用零值填充此维度。这个集合的基数等于我的情况下位势高度的变量的基数。此外,我还有其他三个维度 - 时间(无限制),纬度,经度和级别。
我在fortran中查找了netCDF API,我不确定什么是API调用。当我使用以下API时:
$ p $ ret $ = retval .ne。nf_noerr)调用handle_err(retval)
它最终覆盖0.0的位势高度值(这是我的netCDF文件中唯一的变量)
我该如何去做这件事?
据我所知,维度与变量不同,维度不能有值但可以是变量 - 我认为一个相当普遍的做法可能是创建维度并创建一个变量同名。你可以给变量赋予任何你想要的值。
你的代码可能看起来像
retval = nf_open(cfn,NF_WRITE,ncid)
if(retval .ne。nf_noerr)call handle_err(retval)
retval = nf_redef(ncid)
if(retval。 ne。nf_noerr)调用handle_err(retval)
retval = nf_def_dim(ncid,xyz,len,dimid_xyz)
if(retval .ne。nf_noerr)call handle_err(retval)
retval = nf_def_var(ncid,xyz,netcdf_real,1,[dimid_xyz],varid_xyz)
if(retval .ne。nf_noerr)call handle_err(retval)
retval = nf_enddef (ncid)
retval = nf_put_vara(ncid,varid_xyz,[1],[len],arrayOfZero)
if(retval .ne。nf_noerr)call handle_err(retval)
注意我建议不要在内部使用名为 len
的变量你的 fortran
代码 - 这将与同名的内在冲突。
I added a new dimension to an existing netCDF file in fortran using the following code -
retval = nf_open(cfn,NF_WRITE,ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_redef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,"xyz",len,dimid_xyz)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_enddef(ncid)
Now I want to be able to fill this dimension with values of zero. The cardinality of this set is equal to the cardinality of the variable in my case geopotential height. In addition I have three other dimensions - time(unlimited), latitude, longitude and level.
I looked up the netCDF API in fortran and I am not sure what is the API to call.When I use the following API
retval = nf_put_var_real(ncid,dimid_xyz,xyzArray)
if (retval .ne. nf_noerr) call handle_err(retval)
it ends up overwriting the geopotential height values with 0.0(which is the only variable in my netCDF file)
How do I go about doing this ?
As I understand it a dimension is distinct from a variable, dimensions can't have values but variables can -- I think a fairly common practice may be to create the dimension and also create a variable with the same name. You can then give the variable whatever values you want.
Your code may look like
retval = nf_open(cfn,NF_WRITE,ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_redef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,"xyz",len,dimid_xyz)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid,"xyz",netcdf_real,1,[dimid_xyz], varid_xyz)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_enddef(ncid)
retval = nf_put_vara(ncid,varid_xyz,[1],[len],arrayOfZero)
if (retval .ne. nf_noerr) call handle_err(retval)
Note I'd recommend against using a variable named len
inside your fortran
code -- this will clash with the intrinsic of the same name.
这篇关于Fortran NetCDF - 增加了新的维度,需要用零填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!