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

Re: 20020228: Netcdf-C library use (Trouble adding more dimensions to variables)



>To: address@hidden
>From: Naveenta Anand <address@hidden>
>Subject: Re: 20020228: Netcdf-C library use (Trouble adding more dimensions to 
>variables)
>Keywords: 200202282208.g1SM8j809246

Naveenta,

> Just an FYI here :
> If I add a dimension to existing variable, the error that I get is : Index 
> exceeds dimension bound (which didn't tell until you told me that changing 
> a shape is not possible.

I can't reproduce that behavior.  If I try to add a dimension to an
existing variable by calling nc_def_var(), I get the error message:

  String match to name in use

when I use the name of an existing variable:

#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>

void
check_err(const int stat, const int line, const char *file) {
    if (stat != NC_NOERR) {
           (void) fprintf(stderr, "line %d of %s: %s\n", line, file, 
nc_strerror(stat));
        exit(1);
    }
}

int
main() {                        /* open e.nc and attempt to reshape a variable 
*/

   int  ncid;                   /* netCDF id */

   /* dimension ids */
   int N_LEVELS_dim;
   int N_PROF_dim;

   /* dimension lengths */
   size_t N_LEVELS_len = 7;
   size_t N_PROF_len = 1;

   /* variable ids */
   int PRES_id;

   /* rank (number of dimensions) for each variable */
#  define RANK_PRES 2

   /* variable shapes */
   int PRES_dims[RANK_PRES];

   /* open file */
   int stat = nc_open("e.nc", NC_WRITE, &ncid);
   check_err(stat,__LINE__,__FILE__);

   /* enter define mode to attempt to redefine variable shape */
   stat = nc_redef(ncid);
   check_err(stat,__LINE__,__FILE__);
   
   /* define dimensions */
   stat = nc_def_dim(ncid, "N_LEVELS", N_LEVELS_len, &N_LEVELS_dim);
   check_err(stat,__LINE__,__FILE__);
   stat = nc_def_dim(ncid, "N_PROF", N_PROF_len, &N_PROF_dim);
   check_err(stat,__LINE__,__FILE__);

   /* define variables */

   PRES_dims[0] = N_LEVELS_dim;
   PRES_dims[1] = N_LEVELS_dim;
   stat = nc_def_var(ncid, "PRES", NC_FLOAT, RANK_PRES, PRES_dims, &PRES_id);
   check_err(stat,__LINE__,__FILE__);

   /* leave define mode */
   stat = nc_enddef (ncid);
   check_err(stat,__LINE__,__FILE__);

   {                    /* store PRES */
    static float PRES[] = {8.8999996, 19.4, 29.299999, 39.5, 49.5, 59.200001, 
69.400002};
    stat = nc_put_var_float(ncid, PRES_id, PRES);
    check_err(stat,__LINE__,__FILE__);
   }
   stat = nc_close(ncid);
   check_err(stat,__LINE__,__FILE__);
   return 0;
}

> If I suppress the checkerror statement then I can still run the program but 
> the values do not get displayed. Which tells me that I would be able to do 
> it right if I knew how to?

Maybe I'm still misunderstanding what you are trying to do, but it
appears as if the netCDF library is emitting the correct error message
if you try to redefine a variable to have a new shape.  If you have a
small example that demonstrates otherwise, please send it to us.
Thanks.

--Russ