Hi Wei-keng, <br><br>The tailing junk may be there in the netcdf output but it is preceeded by a char(0) null string terminator so the variable written using netcdf is read correctly.  The pnetcdf variable cannot be read correctly as written.   I don't think that you need to copy to another buffer, you just need to add a terminator to the string.  <br>
<br>Jim<br><br><div class="gmail_quote">On Fri, Aug 3, 2012 at 3:55 PM, Wei-keng Liao <span dir="ltr"><<a href="mailto:wkliao@ece.northwestern.edu" target="_blank">wkliao@ece.northwestern.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi, Jim<br>
<br>
I can reproduce the results you got when using ncmpidump and ncdump.<br>
<br>
When you used trim(buf) in nf90_put_var(), you are telling netcdf4<br>
to write 17 characters (implicitly count(1)==17 will be used internally<br>
due to function overloading in F90). So, in theory, those tailing garbage<br>
characters are expected.<br>
<br>
After checking the netcdf 4.1.3 source codes, I found that the data to<br>
be written will be first copied to an internal buffer (of chunk size<br>
8192 by default) and later flushed to the file. The internal buffer<br>
appears to be initialized to all 0s. NetCDF developers can confirm this.<br>
<br>
So, in your case, the call to nf90_put_var() is actually copying 17<br>
characters to that buffer. Since ncdump/ncmpidump skips the tailing<br>
'\0' characters, the output gives no tailing garbage.<br>
<br>
In Pnetcdf, we do not copy write data to a temporary buffer, so the<br>
unwritten part of the variable contains undefined contents.<br>
<span class="HOEnZb"><font color="#888888"><br>
Wei-keng<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Aug 3, 2012, at 9:09 AM, Jim Edwards wrote:<br>
<br>
> Hi Wei-keng,<br>
><br>
> Here is a program that shows the problem.   Running with pnetcdf I get<br>
>  var = "./none/foo.009.ncuth\000\000\000\000 |\215\000\000\000\000\000p\257\377\377\377\177\000\000\250\260\377\377\377\177\000\000\230\260\377\377\377\177\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000v\377a" ;<br>

><br>
> while netcdf results in:<br>
><br>
>  var = "./none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>" ;<br>
><br>
><br>
> no garbage, no extra space.<br>
><br>
><br>
> #ifdef PNETCDF<br>
> program main<br>
>     use pnetcdf<br>
>     implicit none<br>
>     include 'mpif.h'<br>
><br>
>     integer i, status, ncid, varid, dimid(1)<br>
>     integer(kind=MPI_OFFSET_KIND) :: len, count(1), offset(1)<br>
>     character*(80) buf, rbuf<br>
><br>
>     call MPI_Init(status)<br>
>     status = nfmpi_create(MPI_COMM_WORLD, '<a href="http://ptestfile.nc" target="_blank">ptestfile.nc</a>', &<br>
>                           NF_CLOBBER, MPI_INFO_NULL, ncid)<br>
>     len = 80<br>
>     status = nfmpi_def_dim(ncid, "dim", len, dimid(1))<br>
>     status = nfmpi_def_var(ncid, "var", NF_CHAR, 1, dimid, varid)<br>
>     status = nfmpi_enddef(ncid)<br>
>     status = nfmpi_begin_indep_data(ncid)<br>
><br>
>     buf = "./none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>"<br>
>     status = nfmpi_put_var_text(ncid, varid,trim(buf))<br>
>     status = nfmpi_end_indep_data(ncid)<br>
><br>
>     status = nfmpi_close(ncid)<br>
>     call MPI_Finalize(status)<br>
> end program<br>
> #else<br>
> program main<br>
>     use netcdf<br>
>     implicit none<br>
><br>
>     integer i, status, ncid, varid, dimid(1)<br>
>     integer :: len, count(1), offset(1)<br>
>     character*(80) buf, rbuf<br>
><br>
>     status = nf90_create('<a href="http://ntestfile.nc" target="_blank">ntestfile.nc</a>', &<br>
>                           NF90_CLOBBER, ncid)<br>
>     len = 80<br>
>     status = nf90_set_fill(ncid, NF90_NOFILL, i)<br>
>     status = nf90_def_dim(ncid, "dim", len, dimid(1))<br>
>     status = nf90_def_var(ncid, "var", NF90_CHAR, dimid, varid)<br>
>     status = nf90_enddef(ncid)<br>
><br>
>     buf = "./none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>"<br>
>     status = nf90_put_var(ncid, varid, trim(buf))<br>
><br>
>     status = nf90_close(ncid)<br>
> end program<br>
> #endif<br>
><br>
><br>
> On Thu, Aug 2, 2012 at 9:19 PM, Wei-keng Liao <<a href="mailto:wkliao@ece.northwestern.edu">wkliao@ece.northwestern.edu</a>> wrote:<br>
><br>
> I assume you are calling nfmpi_put_var_text. The "var" APIs are<br>
> intended for writing the entire variable, which in your case it writes<br>
> 80 characters. PnetCDF (and netCDF) will not stop writing at the end<br>
> of the string (string length == 17 in your case). Instead, it writes<br>
> the whole 80 characters from the I/O buffer into the file.<br>
><br>
> Are you seeing those 0s from running ncmpidump, but not from ncdump?<br>
> Strangely, I got the same output from both ncmpidump and ncdump (4.1.3).<br>
> See below. Let me know if this program is same as your program is doing.<br>
><br>
> program main<br>
>     use pnetcdf<br>
>     implicit none<br>
>     include 'mpif.h'<br>
><br>
>     integer i, status, ncid, varid, dimid(1)<br>
>     integer(kind=MPI_OFFSET_KIND) :: len, count(1), offset(1)<br>
>     character*(80) buf, rbuf<br>
><br>
>     call MPI_Init(status)<br>
>     status = nfmpi_create(MPI_COMM_WORLD, '<a href="http://testfile.nc" target="_blank">testfile.nc</a>', &<br>
>                           NF_CLOBBER, MPI_INFO_NULL, ncid)<br>
>     len = 80<br>
>     status = nfmpi_def_dim(ncid, "dim", len, dimid(1))<br>
>     status = nfmpi_def_var(ncid, "var", NF_CHAR, 1, dimid, varid)<br>
>     status = nfmpi_enddef(ncid)<br>
>     status = nfmpi_begin_indep_data(ncid)<br>
><br>
>     buf = "./none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>"<br>
>     status = nfmpi_put_var_text(ncid, varid, buf)<br>
>     status = nfmpi_end_indep_data(ncid)<br>
><br>
>     status = nfmpi_close(ncid)<br>
>     call MPI_Finalize(status)<br>
> end program<br>
><br>
> % ncmpidump <a href="http://testfile.nc" target="_blank">testfile.nc</a><br>
> netcdf testfile {<br>
> // file format: CDF-1<br>
> dimensions:<br>
>         dim = 80 ;<br>
> variables:<br>
>         char var(dim) ;<br>
> data:<br>
><br>
>  var = "./none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>                                                               " ;<br>
> }<br>
><br>
> % 4.1.3/bin/ncdump <a href="http://testfile.nc" target="_blank">testfile.nc</a><br>
> netcdf testfile {<br>
> dimensions:<br>
>         dim = 80 ;<br>
> variables:<br>
>         char var(dim) ;<br>
> data:<br>
><br>
>  var = "./none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>                                                               " ;<br>
> }<br>
><br>
><br>
> Wei-keng<br>
><br>
> On Aug 2, 2012, at 9:26 AM, Jim Edwards wrote:<br>
><br>
> > Hi Wei-keng,<br>
> ><br>
> > In standard netcdf I set NC_NO_FILL when I open the file and I still get this behavior.   I think that you just need to null terminate the string when you pass it from fortran to c.<br>
> ><br>
> > Jim<br>
> ><br>
> > On Wed, Aug 1, 2012 at 7:58 PM, Wei-keng Liao <<a href="mailto:wkliao@ece.northwestern.edu">wkliao@ece.northwestern.edu</a>> wrote:<br>
> > Hi, Jim<br>
> ><br>
> > NetCDF's default for fill mode is NC_FILL and the default fill value for char type<br>
> > is NC_FILL_CHAR == (char)0. See netCDF user guide below.<br>
> > <a href="http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.html#nc_005fset_005ffill" target="_blank">http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.html#nc_005fset_005ffill</a><br>
> > <a href="http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.html#Fill-Values" target="_blank">http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.html#Fill-Values</a><br>
> ><br>
> > In PnetCDF, only NC_NOFILL is implemented. So, for those spaces that were not<br>
> > written by the application, their contents are undefined.<br>
> ><br>
> ><br>
> > Wei-keng<br>
> ><br>
> > On Aug 1, 2012, at 11:27 AM, Jim Edwards wrote:<br>
> ><br>
> > > If I declare a character string variable with a length x and then write a string of length y<x using nfmpi_put_var_char<br>
> > > what is the expected behavior?     I think that what I am getting is incorrect (a bunch of garbage in the string from y:x )<br>
> > ><br>
> > > So for example I want to write string './none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a>' into a variable of length 80.    In the file I am getting:<br>
> > ><br>
> > > './none/<a href="http://foo.009.nc" target="_blank">foo.009.nc</a> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'<br>
> > ><br>
> > ><br>
> > > I think that this is a bug.<br>
> > ><br>
> > ><br>
> > > This is happening with the latest pnetcdf svn trunk code on jaguarpf compiled using:<br>
> > ><br>
> > > Currently Loaded Modulefiles:<br>
> > >   1) modules/<a href="http://3.2.6.6" target="_blank">3.2.6.6</a>                      10) xpmem/0.1-2.0400.31280.3.1.gem       19) DefApps<br>
> > >   2) xtpe-network-gemini                  11) xe-sysroot/4.0.46                    20) altd/1.0<br>
> > >   3) pgi/12.5.0                           12) xt-asyncpe/5.11                      21) subversion/1.6.17<br>
> > >   4) xt-libsci/11.1.00                    13) atp/1.4.1                            22) szip/2.1<br>
> > >   5) udreg/2.3.2-1.0400.5038.0.0.gem      14) PrgEnv-pgi/4.0.46                    23) hdf5/1.8.7<br>
> > >   6) ugni/2.3-1.0400.4374.4.88.gem        15) xt-mpich2/5.5.0                      24) netcdf/4.1.3<br>
> > >   7) pmi/3.0.0-1.0000.8661.28.2807.gem    16) xtpe-interlagos                      25) esmf/5.2.0rp1<br>
> > >   8) dmapp/3.2.1-1.0400.4782.3.1.gem      17) eswrap/1.0.9<br>
> > >   9) gni-headers/2.1-1.0400.4351.3.1.gem  18) lustredu/1.0<br>
> > ><br>
> > > --<br>
> > > Jim Edwards<br>
> > ><br>
> > > CESM Software Engineering Group<br>
> > > National Center for Atmospheric Research<br>
> > > Boulder, CO<br>
> > > 303-497-1842<br>
> > ><br>
> ><br>
> ><br>
> ><br>
> ><br>
> > --<br>
> > Jim Edwards<br>
> ><br>
> ><br>
> ><br>
><br>
><br>
><br>
><br>
> --<br>
> Jim Edwards<br>
><br>
><br>
><br>
<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><pre>Jim Edwards<br><br><br></pre><br>