cdf_int.c 22.8 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
#ifdef HAVE_CONFIG_H
2
#include "config.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3
4
5
6
7
#endif

#include <sys/stat.h>

#include "cdi.h"
8
#include "cdi_int.h"
9
#include "cdf.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
10
#include "cdf_int.h"
11
#include "namespace.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12

Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
#ifdef HAVE_LIBNETCDF
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
15
16

void cdf_create(const char *path, int cmode, int *ncidp)
{
17
  size_t initialsz = 0, chunksizehint = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18

19
20
21
#if defined(__SX__) || defined(ES)
  chunksizehint = 16777216; /* 16 MB */
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22

23
24
  if ( cdiNcChunksizehint != CDI_UNDEFID )
    chunksizehint = (size_t)cdiNcChunksizehint;
25

26
  cdi_nc__create_funcp my_nc__create =
27
    (cdi_nc__create_funcp)namespaceSwitchGet(NSSWITCH_NC__CREATE).func;
28
  int status = my_nc__create(path, cmode, initialsz, &chunksizehint, ncidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30

  if ( CDF_Debug || status != NC_NOERR )
31
    Message("ncid=%d  mode=%d  chunksizehint=%zu  file=%s", *ncidp, cmode, chunksizehint, path);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32

33
  if ( status != NC_NOERR ) Error("%s: %s", path, nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34

35
  int oldfill;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
  status = nc_set_fill(*ncidp, NC_NOFILL, &oldfill);

38
  if ( status != NC_NOERR ) Error("%s: %s", path, nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
40
41
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
int cdf_open(const char *path, int omode, int *ncidp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
  int status = 0;
45
  bool dapfile = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46

47
#ifdef HAVE_LIBNC_DAP
48
  if ( strncmp(path, "http:", 5) == 0 || strncmp(path, "https:", 6) == 0 ) dapfile = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51
52
53
54
55
56
#endif

  if ( dapfile )
    {
      status = nc_open(path, omode, ncidp);
    }
  else
    {
57
      struct stat filestat;
58
      if ( stat(path, &filestat) != 0 ) SysError(path);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59

60
      size_t chunksizehint = 0;
61
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
      chunksizehint = (size_t) filestat.st_blksize * 4;
63
      if ( chunksizehint > (size_t) filestat.st_size ) chunksizehint = (size_t) filestat.st_size;
64
#endif
65
      /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
      if ( chunksizehint < ChunkSizeMin ) chunksizehint = ChunkSizeMin;
67
      */
68
69
      if ( cdiNcChunksizehint != CDI_UNDEFID )
        chunksizehint = (size_t)cdiNcChunksizehint;
70

71
      /* FIXME: parallel part missing */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73
      status = nc__open(path, omode, &chunksizehint, ncidp);

74
      if ( CDF_Debug ) Message("chunksizehint %zu", chunksizehint);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
    }

77
  if ( CDF_Debug )
78
    Message("ncid=%d  mode=%d  file=%s", *ncidp, omode, path);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79

80
  if ( CDF_Debug && status != NC_NOERR ) Message("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81

82
  return status;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84
85
86
87
}


void cdf_close(int ncid)
{
88
  const int status = nc_close(ncid);
89
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
92
93
94
}


void cdf_redef(int ncid)
{
95
  const int status = nc_redef(ncid);
96
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
98
99
100
101
}


void cdf_enddef(int ncid)
{
102
  const int status = nc_enddef(ncid);
103
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
void cdf__enddef(const int ncid, const size_t hdr_pad)
{
109
110
111
  const size_t v_align   = 4UL; // [B] Alignment of beginning of data section for fixed variables
  const size_t v_minfree = 0UL; // [B] Pad at end of data section for fixed size variables
  const size_t r_align   = 4UL; // [B] Alignment of beginning of data section for record variables
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112

113
  // nc_enddef(ncid) is equivalent to nc__enddef(ncid, 0, 4, 0, 4)
114
  const int status = nc__enddef(ncid, hdr_pad, v_align, v_minfree, r_align);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
117
118
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
120
void cdf_sync(int ncid)
{
121
  const int status = nc_sync(ncid);
122
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
void cdf_inq(int ncid, int *ndimsp, int *nvarsp, int *ngattsp, int *unlimdimidp)
{
128
  const int status = nc_inq(ncid, ndimsp, nvarsp, ngattsp, unlimdimidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130

  if ( CDF_Debug || status != NC_NOERR )
131
    Message("ncid=%d  ndims=%d  nvars=%d  ngatts=%d  unlimid=%d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
	    ncid, *ndimsp, *nvarsp, *ngattsp, *unlimdimidp);

134
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
139
}


void cdf_def_dim(int ncid, const char *name, size_t len, int *dimidp)
{
140
  const int status = nc_def_dim(ncid, name, len, dimidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142

  if ( CDF_Debug || status != NC_NOERR )
143
    Message("ncid=%d  name=%s  len=%d", ncid, name, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144

145
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
}

148
149

void cdf_inq_dimid(int ncid, const char *name, int *dimidp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
{
151
  const int status = nc_inq_dimid(ncid, name, dimidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

153
  if ( CDF_Debug || status != NC_NOERR )
154
    Message("ncid=%d  name=%s  dimid=%d", ncid, name, *dimidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155

156
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
}

159
160

void cdf_inq_dim(int ncid, int dimid, char *name, size_t * lengthp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
{
162
  const int status = nc_inq_dim(ncid, dimid, name, lengthp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164

  if ( CDF_Debug || status != NC_NOERR )
165
    Message("ncid=%d  dimid=%d  length=%d  name=%s", ncid, dimid, *lengthp, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166

167
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
}

170
171

void cdf_inq_dimname(int ncid, int dimid, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
{
173
  const int status = nc_inq_dimname(ncid, dimid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175

  if ( CDF_Debug || status != NC_NOERR )
176
    Message("ncid=%d  dimid=%d  name=%s", ncid, dimid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177

178
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
}

181
182

void cdf_inq_dimlen(int ncid, int dimid, size_t * lengthp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
{
184
  const int status = nc_inq_dimlen(ncid, dimid, lengthp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
186

  if ( CDF_Debug || status != NC_NOERR )
187
    Message("ncid=%d  dimid=%d  length=%d", ncid, dimid, *lengthp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188

189
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
191
}

192
193

void cdf_def_var(int ncid, const char *name, nc_type xtype, int ndims,
194
                 const int dimids[], int *varidp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
{
196
  cdi_cdf_def_var_funcp my_cdf_def_var
197
    = (cdi_cdf_def_var_funcp)namespaceSwitchGet(NSSWITCH_CDF_DEF_VAR).func;
198
199
  my_cdf_def_var(ncid, name, xtype, ndims, dimids, varidp);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
200

201
202
203
204
void
cdf_def_var_serial(int ncid, const char *name, nc_type xtype, int ndims,
                   const int dimids[], int *varidp)
{
205
  const int status = nc_def_var(ncid, name, xtype, ndims, dimids, varidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207

  if ( CDF_Debug || status != NC_NOERR )
208
    Message("ncid=%d  name=%s  xtype=%d  ndims=%d  varid=%d", ncid, name, xtype, ndims, *varidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209

Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212
}

213

214

Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
void cdf_inq_varid(int ncid, const char *name, int *varidp)
{
217
  const int status = nc_inq_varid(ncid, name, varidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219

  if ( CDF_Debug || status != NC_NOERR )
220
    Message("ncid=%d  name=%s  varid=%d", ncid, name, *varidp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221

222
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
}
224
225
226


void cdf_inq_nvars(int ncid, int *nvarsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
{
228
  const int status = nc_inq_nvars(ncid, nvarsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230

  if ( CDF_Debug || status != NC_NOERR )
231
    Message("ncid=%d  nvars=%d", ncid, *nvarsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232

233
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
235
}

236
237
238

void cdf_inq_var(int ncid, int varid, char *name, nc_type *xtypep, int *ndimsp,
		 int dimids[], int *nattsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
{
240
  const int status = nc_inq_var(ncid, varid, name, xtypep, ndimsp, dimids, nattsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
242

  if ( CDF_Debug || status != NC_NOERR )
243
    Message("ncid=%d  varid=%d  ndims=%d  xtype=%d  natts=%d  name=%s",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
	    ncid, varid, *ndimsp, *xtypep, *nattsp, name);

246
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
}

249
250

void cdf_inq_varname(int ncid, int varid, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
{
252
  const int status = nc_inq_varname(ncid, varid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254

  if ( CDF_Debug || status != NC_NOERR )
255
    Message("ncid=%d  varid=%d  name=%s", ncid, varid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
256

257
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
}

260
261

void cdf_inq_vartype(int ncid, int varid, nc_type *xtypep)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
{
263
  const int status = nc_inq_vartype(ncid, varid, xtypep);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265

  if ( CDF_Debug || status != NC_NOERR )
266
    Message("ncid=%d  varid=%d  xtype=%s", ncid, varid, *xtypep);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267

268
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
}

271
272

void cdf_inq_varndims(int ncid, int varid, int *ndimsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
{
274
  const int status = nc_inq_varndims(ncid, varid, ndimsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
276

  if ( CDF_Debug || status != NC_NOERR )
277
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278

279
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
281
}

282
283

void cdf_inq_vardimid(int ncid, int varid, int dimids[])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
{
285
  const int status = nc_inq_vardimid(ncid, varid, dimids);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287

  if ( CDF_Debug || status != NC_NOERR )
288
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289

290
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
292
}

293
294

void cdf_inq_varnatts(int ncid, int varid, int *nattsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
295
{
296
  const int status = nc_inq_varnatts(ncid, varid, nattsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297

298
  if ( CDF_Debug || status != NC_NOERR )
299
    Message("ncid=%d  varid=%d  nattsp=%d", ncid, varid, *nattsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300

301
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
303
}

304
305

void cdf_put_var_text(int ncid, int varid, const char *tp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
{
307
  const int status = nc_put_var_text(ncid, varid, tp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308

309
310
  if ( CDF_Debug || status != NC_NOERR )
    Message("%d %d %s", ncid, varid, tp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311

312
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
315
}


316
void cdf_put_var_short(int ncid, int varid, const short *sp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
{
318
  const int status = nc_put_var_short(ncid, varid, sp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319

320
321
  if ( CDF_Debug || status != NC_NOERR )
    Message("%d %d %hd", ncid, varid, *sp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322

323
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
}

326
327

void cdf_put_var_int(int ncid, int varid, const int *ip)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
{
329
  const int status = nc_put_var_int(ncid, varid, ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330

331
332
  if ( CDF_Debug || status != NC_NOERR )
    Message("%d %d %d", ncid, varid, *ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333

334
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
}

337
338

void cdf_put_var_long(int ncid, int varid, const long *lp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339
{
340
  const int status = nc_put_var_long(ncid, varid, lp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341

342
343
  if ( CDF_Debug || status != NC_NOERR )
    Message("%d %d %ld", ncid, varid, *lp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344

345
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
}

348
349

void cdf_put_var_float(int ncid, int varid, const float *fp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
{
351
  const int status = nc_put_var_float(ncid, varid, fp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
352

353
354
  if ( CDF_Debug || status != NC_NOERR )
    Message("%d %d %f", ncid, varid, *fp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355

356
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
}

359
360
361
362
363
364
365
366
367
368
369
static
const char *cdf_var_type(nc_type xtype)
{
  const char *ctype = "unknown";

  if      ( xtype == NC_BYTE   )  ctype = "NC_BYTE";
  else if ( xtype == NC_CHAR   )  ctype = "NC_CHAR";
  else if ( xtype == NC_SHORT  )  ctype = "NC_SHORT";
  else if ( xtype == NC_INT    )  ctype = "NC_INT";
  else if ( xtype == NC_FLOAT  )  ctype = "NC_FLOAT";
  else if ( xtype == NC_DOUBLE )  ctype = "NC_DOUBLE";
370
#ifdef  HAVE_NETCDF4
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
  else if ( xtype == NC_UBYTE  )  ctype = "NC_UBYTE";
  else if ( xtype == NC_LONG   )  ctype = "NC_LONG";
  else if ( xtype == NC_USHORT )  ctype = "NC_USHORT";
  else if ( xtype == NC_UINT   )  ctype = "NC_UINT";
  else if ( xtype == NC_INT64  )  ctype = "NC_INT64";
  else if ( xtype == NC_UINT64 )  ctype = "NC_UINT64";
#endif

  return ctype;
}

static
void minmaxval(size_t nvals, const double *array, double *minval, double *maxval)
{
  *minval = array[0];
  *maxval = array[0];
  for ( size_t i = 1; i < nvals; ++i )
    {
      if      ( array[i] > *maxval ) *maxval = array[i];
      else if ( array[i] < *minval ) *minval = array[i];
    }
}

static
void minmaxvalf(size_t nvals, const float *array, double *minval, double *maxval)
{
  *minval = array[0];
  *maxval = array[0];
  for ( size_t i = 1; i < nvals; ++i )
    {
      if      ( array[i] > *maxval ) *maxval = array[i];
      else if ( array[i] < *minval ) *minval = array[i];
    }
}
405

406
407
void cdf_put_vara_double(int ncid, int varid, const size_t start[],
                         const size_t count[], const double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
{
409
  const int status = nc_put_vara_double(ncid, varid, start, count, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
411

  if ( CDF_Debug || status != NC_NOERR )
412
413
414
    {
      char name[256];
      nc_inq_varname(ncid, varid, name);
415
416
417
418
      nc_type xtype;
      nc_inq_vartype(ncid, varid, &xtype);
      int ndims;
      nc_inq_varndims(ncid, varid, &ndims);
419
      double minval = 0.0, maxval = 0.0;
420
421
422
      size_t nvals = 1;
      for ( int i = 0; i < ndims; ++i ) nvals *= count[i];
      minmaxval(nvals, dp, &minval, &maxval);
423
      // Message("ncid=%d  varid=%d  val0=%f", ncid, varid, *dp);
424
      Message("name=%s  type=%s  minval=%f  maxval=%f", name, cdf_var_type(xtype), minval, maxval);
425
426
427
    }

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
429
}

430

431
432
void cdf_put_vara_float(int ncid, int varid, const size_t start[],
                        const size_t count[], const float *fp)
433
{
434
  const int status = nc_put_vara_float(ncid, varid, start, count, fp);
435
436

  if ( CDF_Debug || status != NC_NOERR )
437
438
439
440
441
442
443
    {
      char name[256];
      nc_inq_varname(ncid, varid, name);
      nc_type xtype;
      nc_inq_vartype(ncid, varid, &xtype);
      int ndims;
      nc_inq_varndims(ncid, varid, &ndims);
444
      double minval = 0.0, maxval = 0.0;
445
446
447
      size_t nvals = 1;
      for ( int i = 0; i < ndims; ++i ) nvals *= count[i];
      minmaxvalf(nvals, fp, &minval, &maxval);
448
      // Message("ncid=%d  varid=%d  val0=%f", ncid, varid, *dp);
449
450
      Message("name=%s  type=%s  minval=%f  maxval=%f", name, cdf_var_type(xtype), minval, maxval);
    }
451

452
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
453
454
455
}


456
457
void cdf_put_vara(int ncid, int varid, const size_t start[], const size_t count[], const void *cp)
{
458
  const int status = nc_put_vara(ncid, varid, start, count, cp);
459
460

  if ( CDF_Debug || status != NC_NOERR )
461
    Message("ncid=%d  varid=%d", ncid, varid);
462
463
464
465
466

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
}


467
468
void cdf_get_vara(int ncid, int varid, const size_t start[], const size_t count[], void *cp)
{
469
  const int status = nc_get_vara(ncid, varid, start, count, cp);
470
471

  if ( CDF_Debug || status != NC_NOERR )
472
    Message("ncid=%d  varid=%d", ncid, varid);
473
474
475
476
477

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
}


478
void cdf_get_vara_int(int ncid, int varid, const size_t start[], const size_t count[], int *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
479
{
480
  const int status = nc_get_vara_int(ncid, varid, start, count, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
482

  if ( CDF_Debug || status != NC_NOERR )
483
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484

485
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
487
}

488

489
void cdf_get_vara_double(int ncid, int varid, const size_t start[], const size_t count[], double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
{
491
  const int status = nc_get_vara_double(ncid, varid, start, count, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
493

  if ( CDF_Debug || status != NC_NOERR )
494
    Message("ncid=%d  varid=%d  start[0]=%zu  count[0]=%zu", ncid, varid, start[0], count[0]);
495

496
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
497
498
499
}


500
void cdf_get_vara_float(int ncid, int varid, const size_t start[], const size_t count[], float *fp)
501
{
502
  const int status = nc_get_vara_float(ncid, varid, start, count, fp);
503
504

  if ( CDF_Debug || status != NC_NOERR )
505
    Message("ncid=%d  varid=%d  start[0]=%zu  count[0]=%zu", ncid, varid, start[0], count[0]);
506

507
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
508
509
510
}


511
void cdf_get_vara_text(int ncid, int varid, const size_t start[], const size_t count[], char *tp)
512
{
513
  const int status = nc_get_vara_text(ncid, varid, start, count, tp);
514
515

  if ( CDF_Debug || status != NC_NOERR )
516
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517

518
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
520
}

521

522
void cdf_get_vara_uchar(int ncid, int varid, const size_t start[], const size_t count[], unsigned char *tp)
523
{
524
  const int status = nc_get_vara_uchar(ncid, varid, start, count, tp);
525
526

  if ( CDF_Debug || status != NC_NOERR )
527
    Message("ncid=%d  varid=%d", ncid, varid);
528
529
530
531
532

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
}


533
void cdf_put_var_double(int ncid, int varid, const double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
{
535
  const int status = nc_put_var_double(ncid, varid, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536
537

  if ( CDF_Debug || status != NC_NOERR )
538
    Message("ncid=%d  varid=%d  val0=%f", ncid, varid, *dp);
539

540
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
541
542
543
544
545
}


void cdf_get_var1_text(int ncid, int varid, const size_t index[], char *tp)
{
546
  const int status = nc_get_var1_text(ncid, varid, index, tp);
547
548

  if ( CDF_Debug || status != NC_NOERR )
549
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
550

551
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
553
}

554
555

void cdf_get_var1_double(int ncid, int varid, const size_t index[], double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
{
557
  const int status = nc_get_var1_double(ncid, varid, index, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558
559

  if ( CDF_Debug || status != NC_NOERR )
560
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
561

562
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
564
}

565
566

void cdf_put_var1_double(int ncid, int varid, const size_t index[], const double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
{
568
  const int status = nc_put_var1_double(ncid, varid, index, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
569
570

  if ( CDF_Debug || status != NC_NOERR )
571
    Message("ncid=%d  varid=%d  val=%f", ncid, varid, *dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572

573
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
575
}

576
577

void cdf_get_var_text(int ncid, int varid, char *tp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
{
579
  const int status = nc_get_var_text(ncid, varid, tp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
581

  if ( CDF_Debug || status != NC_NOERR )
582
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
583

584
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
585
586
}

587
588

void cdf_get_var_short(int ncid, int varid, short *sp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
{
590
  const int status = nc_get_var_short(ncid, varid, sp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592

  if ( CDF_Debug || status != NC_NOERR )
593
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594

595
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596
597
}

598
599

void cdf_get_var_int(int ncid, int varid, int *ip)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
{
601
  const int status = nc_get_var_int(ncid, varid, ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
603

  if ( CDF_Debug || status != NC_NOERR )
604
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605

606
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
608
}

609
610

void cdf_get_var_long(int ncid, int varid, long *lp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
611
{
612
  const int status = nc_get_var_long(ncid, varid, lp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
614

  if ( CDF_Debug || status != NC_NOERR )
615
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616

617
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
619
}

620
621

void cdf_get_var_float(int ncid, int varid, float *fp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
622
{
623
  const int status = nc_get_var_float(ncid, varid, fp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
624
625

  if ( CDF_Debug || status != NC_NOERR )
626
    Message("ncid=%d  varid=%d", ncid, varid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627

628
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
630
}

631
632

void cdf_get_var_double(int ncid, int varid, double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
{
634
  const int status = nc_get_var_double(ncid, varid, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
635

636
  if ( CDF_Debug || status != NC_NOERR )
637
    Message("ncid=%d  varid=%d  val[0]=%f", ncid, varid, *dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638

639
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641
}

642

643
void cdf_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
644
{
645
  const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646

647
648
  if ( CDF_Debug || status != NC_NOERR )
    Message("%d %d %s %d %d", ncid_in, varid_out, name, ncid_out, varid_out);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649

650
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
652
}

653

654
void cdf_put_att_text(int ncid, int varid, const char *name, size_t len, const char *tp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
{
656
  const int status = nc_put_att_text(ncid, varid, name, len, tp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
657

658
  if ( CDF_Debug || status != NC_NOERR )
659
    Message("ncid=%d  varid=%d  att=%s  text=%.*s", ncid, varid, name, (int)len, tp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
660

661
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
662
663
}

664

665
void cdf_put_att_int(int ncid, int varid, const char *name, nc_type xtype, size_t len, const int *ip)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
{
667
  const int status = nc_put_att_int(ncid, varid, name, xtype, len, ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668

669
  if ( CDF_Debug || status != NC_NOERR )
670
    Message("ncid=%d  varid=%d  att=%s  val=%d", ncid, varid, name, *ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
671

672
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
674
}

675

676
void cdf_put_att_float(int ncid, int varid, const char *name, nc_type xtype, size_t len, const float *dp)
677
{
678
  const int status = nc_put_att_float(ncid, varid, name, xtype, len, dp);
679
680

  if ( CDF_Debug || status != NC_NOERR )
681
    Message("ncid=%d  varid=%d  att=%s  val=%g", ncid, varid, name, *dp);
682
683
684
685
686

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
}


687
void cdf_put_att_double(int ncid, int varid, const char *name, nc_type xtype, size_t len, const double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
688
{
689
  const int status = nc_put_att_double(ncid, varid, name, xtype, len, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
690

691
  if ( CDF_Debug || status != NC_NOERR )
692
    Message("ncid=%d  varid=%d  att=%s  val=%g", ncid, varid, name, *dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
693

694
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
695
696
}

697

698
void cdf_get_att_text(int ncid, int varid, const char *name, char *tp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
699
{
700
  const int status = nc_get_att_text(ncid, varid, name, tp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
701

702
  if ( CDF_Debug || status != NC_NOERR )
703
    Message("ncid=%d  varid=%d  name=%s", ncid, varid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
704

705
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
707
}

708

709
710
void cdf_get_att_string(int ncid, int varid, const char *name, char **tp)
{
711
#ifdef  HAVE_NETCDF4
712
  const int status = nc_get_att_string(ncid, varid, name, tp);
713
714

  if ( CDF_Debug || status != NC_NOERR )
715
    Message("ncid=%d  varid=%d  name=%s", ncid, varid, name);
716
717
718
719
720

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
#endif
}

721

722
void cdf_get_att_int(int ncid, int varid, const char *name, int *ip)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
723
{
724
  const int status = nc_get_att_int(ncid, varid, name, ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
725

726
  if ( CDF_Debug || status != NC_NOERR )
727
    Message("ncid=%d  varid=%d  att=%s  val=%d", ncid, varid, name, *ip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728

729
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730
731
}

732

733
734
void cdf_get_att_long(int ncid, int varid, const char *name, long *ip)
{
735
#ifdef  HAVE_NETCDF4
736
  const int status = nc_get_att_long(ncid, varid, name, ip);
737
738

  if ( CDF_Debug || status != NC_NOERR )
739
    Message("ncid=%d  varid=%d  att=%s  val=%ld", ncid, varid, name, *ip);
740
741
742
743
744
745

  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
#endif
}


746
void cdf_get_att_double(int ncid, int varid, const char *name, double *dp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
747
{
748
  const int status = nc_get_att_double(ncid, varid, name, dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
749
750

  if ( CDF_Debug || status != NC_NOERR )
751
    Message("ncid=%d  varid=%d  att=%s  val=%.9g", ncid, varid, name, *dp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
752

753
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755
}

756

757
void cdf_inq_att(int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
{
759
  const int status = nc_inq_att(ncid, varid, name, xtypep, lenp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
761

  if ( CDF_Debug || status != NC_NOERR )
762
    Message("ncid=%d  varid=%d  att=%s", ncid, varid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763

764
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
766
}

767
768

void cdf_inq_atttype(int ncid, int varid, const char *name, nc_type * xtypep)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
{
770
  const int status = nc_inq_atttype(ncid, varid, name, xtypep);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
771
772

  if ( CDF_Debug || status != NC_NOERR )
773
    Message("ncid=%d  varid=%d  att=%s", ncid, varid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
774

775
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
776
777
}

778
779

void cdf_inq_attlen(int ncid, int varid, const char *name, size_t * lenp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
{
781
  const int status = nc_inq_attlen(ncid, varid, name, lenp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
782

783
  if ( CDF_Debug || status != NC_NOERR )
784
    Message("ncid=%d  varid=%d  att=%s  len=%d", ncid, varid, name, *lenp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785

786
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
787
788
}

789
790

void cdf_inq_attname(int ncid, int varid, int attnum, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
791
{
792
  const int status = nc_inq_attname(ncid, varid, attnum, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793

794
  if ( CDF_Debug || status != NC_NOERR )
795
    Message("ncid=%d  varid=%d  attnum=%d  att=%s", ncid, varid, attnum, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796

797
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
798
799
}

800
801

void cdf_inq_attid(int ncid, int varid, const char *name, int *attnump)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
{
803
  const int status = nc_inq_attid(ncid, varid, name, attnump);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
804
805

  if ( CDF_Debug || status != NC_NOERR )
806
    Message("ncid=%d  varid=%d  att=%s", ncid, varid, name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
807

808
  if ( status != NC_NOERR ) Error("%s", nc_strerror(status));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
809
810
}