1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctest/add.c,v 1.18 2008/06/10 19:38:03 russ Exp $
*********************************************************************/
/*
* utility functions to update in-memory netcdf by adding new
* dimensions, variables, and attributes.
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for free() */
#include "netcdf.h"
#include "testcdf.h"
#include "add.h"
#include "emalloc.h"
struct netcdf test; /*
* in-memory netcdf structure, kept in sync
* with disk netcdf
*/
void
add_dim (struct netcdf *test, struct cdfdim *idim) /* add the dimension idim to the netcdf test */
{
static char pname[] = "add_dim";
if (test->ndims >= MAX_NC_DIMS) {
(void)fprintf(stderr,
"%s: too many dimensions (%d)", pname, test->ndims);
return;
}
test->dims[test->ndims].size = idim->size;
test->dims[test->ndims].name = (char *) emalloc(strlen(idim->name) + 1);
(void) strcpy(test->dims[test->ndims].name, idim->name);
if (idim->size == NC_UNLIMITED)
test->xdimid = test->ndims;
test->ndims++;
}
void
add_var (struct netcdf *test, struct cdfvar *ivar) /* add the variable ivar to the netcdf test */
{
static char pname[] = "add_var";
int i;
if (test->nvars >= MAX_NC_VARS) {
(void)fprintf(stderr,
"%s: too many variables (%d)", pname, test->nvars);
return;
}
test->vars[test->nvars].name = (char *) emalloc(strlen(ivar->name) + 1);
(void) strcpy(test->vars[test->nvars].name, ivar->name);
test->vars[test->nvars].type = ivar->type;
test->vars[test->nvars].ndims = ivar->ndims;
test->vars[test->nvars].dims = (int *) emalloc(sizeof(int)*(size_t)ivar->ndims);
for (i = 0; i < ivar->ndims; i++)
test->vars[test->nvars].dims[i] = ivar->dims[i];
test->vars[test->nvars].natts = 0;
test->nvars++;
}
void
add_att (struct netcdf *test, int varid, struct cdfatt *iatt) /* add attribute iatt to the netcdf test */
{
static char pname[] = "add_att";
int ia; /* attribute number */
if (test->natts >= MAX_TEST_ATTS) {
(void)fprintf(stderr,
"%s: too many attributes (%d)", pname, test->natts);
return;
}
/* if already defined, change existing attribute and return */
for (ia = 0; ia < test->natts ; ia++) {
if (test->atts[ia].var == varid &&
strcmp(test->atts[ia].name, iatt->name) == 0) {
test->atts[ia].type = iatt->type;
test->atts[ia].len = iatt->len;
test->atts[ia].val = iatt->val;
return;
}
}
/* otherwise, add new attribute to list */
test->atts[test->natts].var = varid;
test->atts[test->natts].name = (char *) emalloc(strlen(iatt->name) + 1);
(void) strcpy(test->atts[test->natts].name, iatt->name);
test->atts[test->natts].type = iatt->type;
test->atts[test->natts].len = iatt->len;
test->atts[test->natts].val = iatt->val;
test->natts++;
if (varid == NC_GLOBAL)
test->ngatts++;
else
test->vars[varid].natts++;
}
void
add_reset(struct netcdf *test) /* reset in-memory netcdf test to empty */
{
test->ndims = 0;
test->nvars = 0;
test->natts = 0;
test->ngatts = 0;
test->xdimid = -1; /* no unlimited dimension */
}
void
del_att (struct netcdf *test, int varid, struct cdfatt *iatt) /* delete attribute iatt in the netcdf test */
{
static char pname[] = "del_att";
int ia, ib; /* attribute number */
for (ia = 0; ia < test->natts ; ia++) { /* find attribute to delete */
if (test->atts[ia].var == varid &&
strcmp(test->atts[ia].name, iatt->name) == 0) {
free(test->atts[ia].name);
for (ib = ia+1; ib < test->natts; ib++) { /* move down */
test->atts[ib-1].var = test->atts[ib].var;
test->atts[ib-1].name = test->atts[ib].name;
test->atts[ib-1].type = test->atts[ib].type;
test->atts[ib-1].len = test->atts[ib].len;
test->atts[ib-1].val = test->atts[ib].val;
}
test->natts--;
if (varid == NC_GLOBAL)
test->ngatts--;
else
test->vars[varid].natts--;
return;
}
}
/* not found */
(void) fprintf(stderr, "%s: no such attribute as (%s, %s)", pname,
test->vars[varid].name, iatt->name);
}
void
add_data(struct netcdf *test, int varid, long start[], long edges[]) /* keep max record written updated */
{
if (varid != test->xdimid) /* not a record variable */
return;
if (start[0] + edges[0] > test->dims[test->xdimid].size)
test->dims[test->xdimid].size = start[0] + edges[0];
}
void
errvar(struct netcdf *cdfp, struct cdfvar *varp)
{
const char *types;
int id;
switch (varp->type) {
case NC_BYTE:
types = "NC_BYTE";
break;
case NC_CHAR:
types = "NC_CHAR";
break;
case NC_SHORT:
types = "NC_SHORT";
break;
case NC_LONG:
types = "NC_LONG";
break;
case NC_FLOAT:
types = "NC_FLOAT";
break;
case NC_DOUBLE:
types = "NC_DOUBLE ";
break;
default:
types = "UNKNOWN";
break;
}
(void) fprintf(stderr," name=%s type=%s dims=(",
varp->name, types);
for (id = 0; id < varp->ndims; id++)
(void) fprintf(stderr, "%ld%s",
(long)cdfp->dims[varp->dims[id]].size,
id < varp->ndims - 1 ? ", " : "");
(void) fprintf(stderr, ")\n");
}
|