ncdf4a13/nctest/add.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *   Copyright 1993, UCAR/Unidata
00003  *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
00004  *   $Header: /upc/share/CVS/netcdf-3/nctest/add.c,v 1.17 2004/11/16 21:33:08 russ Exp $
00005  *********************************************************************/
00006 
00007 /* 
00008  * utility functions to update in-memory netcdf by adding new 
00009  * dimensions, variables, and attributes.
00010  */
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <stdlib.h>             /* for free() */
00014 #include "netcdf.h"
00015 #include "testcdf.h"
00016 #include "add.h"
00017 #include "emalloc.h"
00018 
00019 struct netcdf test;             /*
00020                                  * in-memory netcdf structure, kept in sync
00021                                  * with disk netcdf
00022                                  */
00023 
00024 void
00025 add_dim (test, idim)            /* add the dimension idim to the netcdf test */
00026      struct netcdf *test;
00027      struct cdfdim *idim;
00028 {
00029     static char pname[] = "add_dim";
00030 
00031     if (test->ndims >= MAX_NC_DIMS) {
00032         (void)fprintf(stderr,
00033                       "%s: too many dimensions (%d)", pname, test->ndims);
00034         return;
00035     }
00036     test->dims[test->ndims].size = idim->size;
00037     test->dims[test->ndims].name = (char *) emalloc(strlen(idim->name) + 1);
00038     (void) strcpy(test->dims[test->ndims].name, idim->name);
00039     if (idim->size == NC_UNLIMITED)
00040       test->xdimid = test->ndims;
00041     test->ndims++;
00042 }
00043 
00044 void
00045 add_var (test, ivar)            /* add the variable ivar to the netcdf test */
00046      struct netcdf *test;
00047      struct cdfvar *ivar;
00048 {
00049     static char pname[] = "add_var";
00050     int i;
00051 
00052     if (test->nvars >= MAX_NC_VARS) {
00053         (void)fprintf(stderr,
00054                       "%s: too many variables (%d)", pname, test->nvars);
00055         return;
00056     }
00057 
00058     test->vars[test->nvars].name = (char *) emalloc(strlen(ivar->name) + 1);
00059     (void) strcpy(test->vars[test->nvars].name, ivar->name);
00060     test->vars[test->nvars].type = ivar->type;
00061     test->vars[test->nvars].ndims = ivar->ndims;
00062     test->vars[test->nvars].dims = (int *) emalloc(sizeof(int)*ivar->ndims);
00063     for (i = 0; i < ivar->ndims; i++)
00064       test->vars[test->nvars].dims[i] = ivar->dims[i];
00065     test->vars[test->nvars].natts = 0;
00066     test->nvars++;
00067 }
00068 
00069 void
00070 add_att (test, varid, iatt)     /* add attribute iatt to the netcdf test */
00071      struct netcdf *test;
00072      int varid;                 /* variable id */
00073      struct cdfatt *iatt;
00074 {
00075     static char pname[] = "add_att";
00076     int ia;                     /* attribute number */
00077 
00078     if (test->natts >= MAX_TEST_ATTS) {
00079         (void)fprintf(stderr,
00080                       "%s: too many attributes (%d)", pname, test->natts);
00081         return;
00082     }
00083 
00084     /* if already defined, change existing attribute and return */
00085     for (ia = 0; ia < test->natts ; ia++) {
00086         if (test->atts[ia].var == varid &&
00087             strcmp(test->atts[ia].name, iatt->name) == 0) {
00088             test->atts[ia].type = iatt->type;
00089             test->atts[ia].len = iatt->len;
00090             test->atts[ia].val = iatt->val;
00091             return;
00092         }
00093     }
00094     /* otherwise, add new attribute to list */
00095     test->atts[test->natts].var = varid;
00096     test->atts[test->natts].name = (char *) emalloc(strlen(iatt->name) + 1);
00097     (void) strcpy(test->atts[test->natts].name, iatt->name);
00098     test->atts[test->natts].type = iatt->type;
00099     test->atts[test->natts].len = iatt->len;
00100     test->atts[test->natts].val = iatt->val;
00101     test->natts++;
00102     if (varid == NC_GLOBAL)
00103       test->ngatts++;
00104     else
00105       test->vars[varid].natts++;
00106 }
00107 
00108 
00109 void
00110 add_reset(test)                 /* reset in-memory netcdf test to empty */
00111      struct netcdf *test;
00112 {
00113     test->ndims = 0;
00114     test->nvars = 0;
00115     test->natts = 0;
00116     test->ngatts = 0;
00117     test->xdimid = -1;          /* no unlimited dimension */
00118 }
00119 
00120 
00121 void
00122 del_att (test, varid, iatt)     /* delete attribute iatt in the netcdf test */
00123      struct netcdf *test;
00124      int varid;                 /* variable id */
00125      struct cdfatt *iatt;
00126 {
00127     static char pname[] = "del_att";
00128     int ia, ib;                 /* attribute number */
00129 
00130     for (ia = 0; ia < test->natts ; ia++) { /* find attribute to delete */
00131         if (test->atts[ia].var == varid &&
00132             strcmp(test->atts[ia].name, iatt->name) == 0) {
00133             free(test->atts[ia].name);
00134             for (ib = ia+1; ib < test->natts; ib++) { /* move down */
00135                 test->atts[ib-1].var =   test->atts[ib].var;
00136                 test->atts[ib-1].name =  test->atts[ib].name;
00137                 test->atts[ib-1].type =  test->atts[ib].type;
00138                 test->atts[ib-1].len =   test->atts[ib].len;
00139                 test->atts[ib-1].val =   test->atts[ib].val;
00140             }
00141             test->natts--;
00142             if (varid == NC_GLOBAL)
00143               test->ngatts--;
00144             else
00145               test->vars[varid].natts--;
00146             return;
00147         }
00148     }
00149     /* not found */
00150     (void) fprintf(stderr, "%s: no such attribute as (%s, %s)", pname,
00151                    test->vars[varid].name, iatt->name);
00152 }
00153 
00154 void
00155 add_data(test, varid, start, edges) /* keep max record written updated */
00156      struct netcdf *test;
00157      int varid;
00158      long start[];
00159      long edges[];
00160 {
00161     if (varid != test->xdimid)  /* not a record variable */
00162       return;
00163     if (start[0] + edges[0] > test->dims[test->xdimid].size)
00164       test->dims[test->xdimid].size = start[0] + edges[0];
00165 }
00166 
00167 
00168 void
00169 errvar(cdfp, varp)
00170      struct netcdf *cdfp;
00171      struct cdfvar *varp;
00172 {
00173     const char *types;
00174     int id;
00175 
00176     switch (varp->type) {
00177       case NC_BYTE:
00178         types = "NC_BYTE";
00179         break;
00180       case NC_CHAR:
00181         types = "NC_CHAR";
00182         break;
00183       case NC_SHORT:
00184         types = "NC_SHORT";
00185         break;
00186       case NC_LONG:
00187         types = "NC_LONG";
00188         break;
00189       case NC_FLOAT:
00190         types = "NC_FLOAT";
00191         break;
00192       case NC_DOUBLE:
00193         types = "NC_DOUBLE      ";
00194         break;
00195       default:
00196         types = "UNKNOWN";
00197         break;
00198     }
00199 
00200     (void) fprintf(stderr,"  name=%s  type=%s  dims=(",
00201                    varp->name, types);
00202     for (id = 0; id < varp->ndims; id++)
00203       (void) fprintf(stderr, "%ld%s",
00204                      (long)cdfp->dims[varp->dims[id]].size,
00205                      id < varp->ndims - 1 ? ", " : "");
00206     (void) fprintf(stderr, ")\n");
00207 }

Generated on Thu Mar 16 18:10:14 2006 for nco by  doxygen 1.4.4