Re: [netcdfgroup] nf90_put_var treatment of scalar strings

  • To: Brian Eaton <eaton@xxxxxxxx>
  • Subject: Re: [netcdfgroup] nf90_put_var treatment of scalar strings
  • From: Russ Rew <russ@xxxxxxxxxxxxxxxx>
  • Date: Fri, 17 Jul 2009 16:47:40 -0600
Hi Brian,

On Jul 10, 2009, Brian Eaton wrote:
> I have discovered (the hard way, of course) that the Fortran 90 interface
> nf90_put_var treats scalar strings in an unexpected way.  The specific
> function for writing a scalar string is nf90_put_var_text (in
> netcdf_text_variables.f90) and this function uses the len_trim intrinsic to
> compute count, and uses the trim intrinsic in the call to nf_put_vars_text.
> This strips the trailing blanks that fortran codes depend on.  If the
> default prefill is on then the application that reads the string gets
> trailing nulls rather than blanks which is not too hard to deal with.
> However, if prefill is turned off, which is the normal mode of operation
> for models writing large datasets, then the reading application gets
> trailing garbage which is a disaster.
> 
> The specific interface for scalar strings is the only one that does this;
> arrays of strings are treated properly.
> 
> I consider it a bug that the F90 interface alters the data passed by the
> user to be written to the file.  I'm interested to hear whether there are
> other points of view about this behavior.

Thanks for reporting this problem.

We agree that this was a bug, and have fixed it in the snapshot
release.  This behavior is inconsistent with the behavior from the C,
Java, and C++ APIs, which preserve trailing blanks when writing
character data.

Because this bug has been in the Fortran 90 code since the original
release in 1999, it is possible that some users or programs may depend
on the undocumented behavior as a way to change trailing blanks into
trailing null characters when calling

  nf90_put_var(ncid, varid, charvar) 

with a simple string argument.  If so, that behavior is still
available by applying the Fortran "trim" intrinsic to the string
argument, as in

  nf90_put_var(ncid, varid, trim(charvar)) 

which will use null padding when the trimmed string is shorter than the
length of the declared netCDF variable if using the default fill mode.
Without the trim function, trailing blanks will be preserved in the
data.  If you are relying on the current behavior, it will not hurt to
add the above trim call, and will ensure that this call continues to
work as expected after the netCDF 4.1 release.

We have also added an explanation of this idiom to the Fortran-90
documentation in the snapshot release that discusses writing and reading
character data.

--Russ



  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: