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

[netCDF #HEV-388573]: NetCDF C library question



I attempted to build an equivalent (almost) program in C
(program attached). It seems to work ok. The big difference is
in the setting of the _FillValue attribute. I realized you
seem to be giving the wrong value to it. Note that since the variable
type is NC_CHAR, the fill value must be of type NC_CHAR and of length
one.
=Dennis Heimbigner
  Unidata


Ticket Details
===================
Ticket ID: HEV-388573
Department: Support netCDF
Priority: Critical
Status: Open
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netcdf.h>

#define TESTFILLVALUE

#define D1 10
#define D2 5
#define NCHARS (D1 * D2)

int
main()
{
    int stat = NC_NOERR;
    int ncid;
    int varid;
    int d_ndims;
    nc_type d_array_type;
    nc_type xtype;
    char val[NCHARS+1];
    int i;
    int d_dim_ids[NC_MAX_VAR_DIMS];
#ifdef TESTFILLVALUE
    size_t start[NC_MAX_VAR_DIMS];
    size_t count[NC_MAX_VAR_DIMS];
#endif

    /* Initialize */
    d_array_type = NC_CHAR;
    d_ndims = 2;
    memset(d_dim_ids,0,sizeof(d_dim_ids));
#ifdef TESTFILLVALUE
    memset(start,0,sizeof(start));
    memset(count,0,sizeof(count));
#endif
    stat = nc_create("james.nc",NC_CLOBBER,&ncid);
    if(stat) goto fail;

    stat = nc_def_dim(ncid, "d1", D1, &d_dim_ids[0]);
    if(stat) goto fail;
    stat = nc_def_dim(ncid, "d2", D2, &d_dim_ids[1]);
    if(stat) goto fail;

    stat = nc_def_var(ncid, "var", d_array_type, d_ndims, d_dim_ids, &varid);
    if(stat) goto fail;

    stat = nc_inq_vartype(ncid, varid, &xtype);
    if(stat) goto fail;

    stat = nc_put_att_text(ncid, varid, "_FillValue", 1, "X");
    if(stat) goto fail;

    stat = nc_enddef(ncid);
    if(stat) goto fail;

    for(i=0;i<NCHARS;i++) {val[i] = '0' + (i % 10);}
    val[NCHARS]='\0';

#ifdef TESTFILLVALUE
    count[0] = D1;
    count[1] = D2 - 2;
    stat = nc_put_vara_text(ncid, varid, start, count, val);
#else
    stat = nc_put_var_text(ncid, varid, val);
#endif
    if(stat) goto fail;

    stat = nc_close(ncid);
    if(stat) goto fail;

    return 0;

fail:
    fprintf(stderr,"fail: (%d) %s\n",stat,nc_strerror(stat));
    return 1;
}