0

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 ?

gansub
  • 1,164
  • 3
  • 20
  • 47
  • 2
    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 (`*_def_var`) with the same name. You can then give the variable whatever values you want. – d_1999 Aug 23 '16 at 09:59
  • @d_1999 great suggestion. Can you add that as an answer ? I will be happy to upvote and accept. – gansub Aug 23 '16 at 10:31

1 Answers1

1

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.

d_1999
  • 854
  • 6
  • 16
  • thanks for your answer. I think the API of nf_def_var does not match the doc- http://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-f77/NF_005fDEF_005fVAR.html – gansub Aug 23 '16 at 11:47
  • @gansub I've editted although I'm not 100% sure it's correct -- I only have experience using the `nf90_*` api, this is one of the reasons I originally posted this as a comment rather than an answer. If anyone else can improve on this I'm obviously happy for them to edit/post another answer. – d_1999 Aug 23 '16 at 12:59