Re: [netcdfgroup] Why translates nc_get_vara_float all short _FillValues to 0?

Hi Dennis,

the autoconversion from short-_FillValue to float-0.0 is done in the
netcdf-c library when reading with shorts with nc_get_vara_float. It
does not happen with ncdump, since ncdump doesn't use type-conversion
https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf/Type-Conversion.html#Type-Conversion

I attach an example file and C-program:

gcc simple_xy_rd.c -lnetcdf -o simple_xy_rd
./simple_xy_rd out.nc | less


Output will be many -0.0 where I would expect -32767., which I get when
I change float to short in the defines at the beginning of the file:
#define TYPE float
#define NC_GETVARA nc_get_vara_float



Heiko

On 2017-01-16 20:11, dmh@xxxxxxxx wrote:
> Ok, the first thing to check is if the problem is in the netcdf-c
> library or in ncview. Try dumping the variable in question using
> ncdump and the -v option.
> =Dennis Heimbigner
>  Unidata
> 
> p.s. If the netcdf file is not too large, then you can send it to me
> at dmh at ucar dot edu.
> 
> On 1/16/2017 7:12 AM, Heiko Klein wrote:
>> Hi,
>>
>> I just was surprised by strange results from ncview when reading a
>> variable with 'short' values, where all data is undefined. ncview reads
>> all data with nc_geta_vara_float.
>>
>>
>> _FillValues are defined as -32767s. Reading undefined data with
>> nc_get_vara_float results in values = -0 or 0 for all _FillValues. I had
>> expected a value like -32767.f (or more generally, the _FillValue
>> translated into float)
>>
>> Is the 0-result really the expected behaviour of nc_get_vara_float? I
>> have then no idea how to handling _FillValues correctly.
>>
>> Best regards,
>>
>> Heiko
>>
>>
>>
>>
>>
>> _______________________________________________
>> NOTE: All exchanges posted to Unidata maintained email lists are
>> recorded in the Unidata inquiry tracking system and made publicly
>> available through the web.  Users who post to any of the lists we
>> maintain are reminded to remove any personal information that they
>> do not want to be made public.
>>
>>
>> netcdfgroup mailing list
>> netcdfgroup@xxxxxxxxxxxxxxxx
>> For list information or to unsubscribe,  visit:
>> http://www.unidata.ucar.edu/mailing_lists/
>>
> 
> _______________________________________________
> NOTE: All exchanges posted to Unidata maintained email lists are
> recorded in the Unidata inquiry tracking system and made publicly
> available through the web.  Users who post to any of the lists we
> maintain are reminded to remove any personal information that they
> do not want to be made public.
> 
> 
> netcdfgroup mailing list
> netcdfgroup@xxxxxxxxxxxxxxxx
> For list information or to unsubscribe,  visit:
> http://www.unidata.ucar.edu/mailing_lists/

-- 
Dr. Heiko Klein                   Norwegian Meteorological Institute
Tel. + 47 22 96 32 58             P.O. Box 43 Blindern
http://www.met.no                 0313 Oslo NORWAY
/* This is part of the netCDF package.
   Copyright 2006 University Corporation for Atmospheric Research/Unidata.
   See COPYRIGHT file for conditions of use.

   This is a simple example which reads a small dummy array, which was
   written by simple_xy_wr.c. This is intended to illustrate the use
   of the netCDF C API.

   This program is part of the netCDF tutorial:
   http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial

   Full documentation of the netCDF C API can be found at:
   http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c

   $Id: simple_xy_rd.c,v 1.9 2006/08/17 23:00:55 russ Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
#include <math.h>

/* This is the name of the data file we will read. */

/* We are reading 2D data, a 6 x 12 grid. */
#define NX 3600
#define NY 1801
#define NZ 49
#define NT 10 

/* Handle errors by printing an error message and exiting with a
 * non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

#define TYPE float
#define NC_GETVARA nc_get_vara_float

int
main(int argc, char* argv[])
{
   if (argc < 2) {
       printf("usage: %s filename\n", argv[0]);
       exit(1);
   }

   /* This will be the netCDF ID for the file and data variable. */
   int ncid, varid;

   //short data_in[NX][NY][NZ];
   TYPE *data_in;
   data_in = (TYPE*) malloc(NX*NY*NZ*sizeof(TYPE));
   for (int i = 0; i < NX*NY*NZ; i++) {
       data_in[i] = -111;
   }

   /* Loop indexes, and error handling. */
   int x, y, z, t, retval;

   /* Open the file. NC_NOWRITE tells netCDF we want read-only access
    * to the file.*/
   if ((retval = nc_open(argv[1], NC_NOWRITE, &ncid)))
      ERR(retval);

   /* Get the varid of the data variable, based on its name. */
   if ((retval = nc_inq_varid(ncid, "air_temperature_ml", &varid)))
      ERR(retval);

   size_t start[4], count[4];
   start[0]=0;
   start[1]=0;
   start[2]=0;
   start[3]=0;
   count[0]=1;
   count[1]=NZ;
   count[2]=NY;
   count[3]=NX;
   for (t = 0; t < NT; t++) {
     start[0] = t;
     /* Read the data. */
     if ((retval = NC_GETVARA(ncid, varid, start, count, data_in)))
        ERR(retval);

     /* Check the data. */
     for (z = 0; z < NZ; z++)
       for (y = 0; y < NY; y++)
         for (x = 0; x < NX; x++)
               if (data_in[x+NX*y+NX*NY*z] != -32767) {
                  printf("%d %d %d %d: %f\n", x, y ,z, t, 
data_in[x+NX*y+NX*NY*z]);
        //          return ERRCODE;
               }
   }
   /* Close the file, freeing all resources. */
   if ((retval = nc_close(ncid)))
      ERR(retval);

   printf("*** SUCCESS reading example file %s!\n", argv[1]);
   return 0;
}

Attachment: out.nc
Description: Cdf file

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