[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 971110: Netcdf-3.3.1 gives 4 byte ncShort?



>To: address@hidden
>cc: address@hidden
>From: "Carolyn L. Harris" <address@hidden>
>Subject: Re: 971110: Netcdf-3.3.1 gives 4 byte ncShort?
>Organization: .
>Keywords: 199711111826.LAA20240
>
>
> Glenn,
>
> Thanks for responding to my question yesterday about the size of a
> NetCDF short.  I understand the misconceptions you pointed out to me,
> but I'm still not convinced that an ncshort is represented with only 2
> bytes.  If I have a file that contains only ncshorts, I get exactly
> what you suggest: the file grows as (n + 1) * 2.
>
> But, if I combine ncshorts with other types, I get that the file grows
> as n*4.  My example program is again, below.  It stores one vector of
> ncshort and one of ncfloat.
>
> If I run it with the puts commented out, I get a file of size 120
> bytes, which should be the header you pointed out to me.
>
> If I run it with M=1000, I expect to get a file of size:
> 120 + 4*1000 + 2*1000 = 6120.  Instead, I get a file of size 8120 bytes.
>
> If I run it with M=5000, I expect to get a file of size:
> 120 + 4*5000 + 2*5000 = 30120.  I get a file of size 40120.
>
> Thus it appears to me as though a ncshort takes up 4 bytes
> when stored in a file with other data types.
>
> So, I apologize for still being confused on this point, but I
> can't figure out what's going on.
>
> Thanks,
> Lyn
>
> ************************************************************************
> Lyn Harris                                      address@hidden
> SIO/UCSD Mail Code 0230                         address@hidden
> 9500 Gilman Drive                           fax (619) 534-0704
> La Jolla, CA 92093-0230                   voice (619) 534-5996
>                                          http://chowder.ucsd.edu/~lyn
> ************************************************************************
>
> // CC a.cc -I/usr/local/include -L/usr/local/lib -lnetcdf_c++ -lnetcdf
> // cxx a.cc -I/usr/local/include -L/usr/local/lib -lnetcdf_c++ -lnetcdf
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #include "netcdf.hh"
>
> NcFile *ncfile;
>
> #define M 1000
>
> int main()
> {
>   short t0[M];
>   float f[M];
>
>   for (int k = 0; k < M; k++){
>     t0[k] = k;
>     f[k] = k;
>   }
>
>     NcFile ofile = NcFile("jnk.nc", NcFile::Replace);
>     ncfile = &ofile;
>     if (!ncfile->is_valid()){
>       cerr << "invalid ofile\n";
>       return(1);
>     }
>
>     NcDim* tdim = ncfile->add_dim("time");
>     NcVar* tvar = ncfile->add_var("time", ncShort, tdim);
>     NcVar* fvar = ncfile->add_var("floatvar", ncFloat, tdim);
>     NcVar* t2var = ncfile->add_var("time2", ncShort, tdim);
>
>     if (!tvar->put(t0, M))
>       cerr << "put short failed\n";
>     if (!fvar->put(f, M))
>       cerr << "put float failed\n";
>
>
>     ncfile->~NcFile();
>
> }
>
> //.................................................................
> //.................................................................

The XDR format on which netcdf if based requires that 'seek' addresses
be divisible by 4. So, the beginning of each variable, and the beginning
of the storage for each variable within a "record", must be rounded up
to address which is a multiple of 4. This is documented in the File Format
Specification.

In the above example, all the variables vary with the unlimited
dimension 'time'. This is what we mean by the terminology "record variable".

If you must have a packed array of shorts, you'll have to declare the size of
the 'time' dimension when you create the file.

-glenn