Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

Re: question of char

Hi Alain,

> I would like to create a netcdf file with a variable
> containing two strings which it is like this example
> 
> 
> netcdf fileTOTO {
> dimensions:
>         nbData = UNLIMITED ; // (X currently)
>         numberOfChar = 255 ;
>         numberOfField = 2 ;
> variables:
>         char COD_CAPTEUR(?????) ;
> data:
> 
>  COD_CAPTEUR
>   "hello world","hello USA"
>   "hello France","hello Russ"
>   "xxxxx" "xxxxx"
>    ....
>   "xxxxx" "xxxxx";
> 
> I don't know how to declare my variable and if it possible ?
> I'am using C++ with netcdf-3.4 on Sun Solaris 2.6.

NetCDF supports multidimensional character arrays rather than arrays
of strings, so if your strings are of different lengths, it will waste
some space.  If the maximum length of strings in the COD_CAPTEUR
variable is 20, for example, add an extra "stringlen" dimension of
length 20:

 netcdf fileTOTO {
 dimensions:
         nbData = UNLIMITED ; // (X currently)
         numberOfChar = 255 ;
         numberOfField = 2 ;
         stringlen = 20;
 variables:
         char COD_CAPTEUR(nbData, stringlen) ;
 data:

  COD_CAPTEUR
   "hello world","hello USA",
   "hello France","hello Russ",
   "xxxxx", "xxxxx";
 }

The ncdump and ncgen utilities honor the C convention of
null-terminated strings in such arrays.  In other words, if you create
a binary netCDF file from the above using "ncgen -b ..." and then
ncdump the resulting file, it will preserve the strings, but what is
stored in the file is still a rectangular array of characters.

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
russ@xxxxxxxxxxxxxxxx                     http://www.unidata.ucar.edu

 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Community Programs   Unidata is a member of the UCAR Community Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690