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

Re: 980619: attribute trouble



>To: address@hidden
>From: Nan Rosenbloom <address@hidden>
>Subject: attribute trouble
>Organization: .
>Keywords: 199806191941.NAA03654

Nan,

I wrote:

> Yes, you are making the (reasonable) assumption that netCDF returns
> character array attributes in the form of C strings, properly terminated
> with a null '\0' character.  However netCDF files are language-neutral,
> so Fortran programs can read the files too.  There is only a null
> character terminating a string attribute if you put it there when you
> wrote it, and specified the attribute length (number of characters) to
> include the extra null character.  So you need to have LSCALED defined
> as 4 and LSOURCE defined as 21 to get the effect you expect.

but I forgot to mention that you don't really have to go back and
rewrite all those files to make sure they have the extra null
terminators on all the string attributes.  Instead, just allocate an
extra byte on the space you read the attribute values into, and set it
to the null byte explicitly before or after you read the attribute, or
by using calloc instead of malloc.  For example, instead of

> /* --- retrieve variable scaling factor --- */
>         ncattinq(ncid,varid,"scaled",&vr_type, &vr_len);
>         vr_val = malloc(vr_len*nctypelen(vr_type));
>         ncattget(ncid,varid,"scaled",vr_val);

use something like:

  /* --- retrieve variable scaling factor --- */
          ncattinq(ncid,varid,"scaled",&vr_type, &vr_len);
          vr_val = malloc((vr_len+1)*nctypelen(vr_type));
          ncattget(ncid,varid,"scaled",vr_val);
          vr_val[vr_len] = '\0';

or just

  /* --- retrieve variable scaling factor --- */
          ncattinq(ncid,varid,"scaled",&vr_type, &vr_len);
          vr_val = calloc((vr_len+1)*nctypelen(vr_type));
          ncattget(ncid,varid,"scaled",vr_val);

--Russ