cdf_read.c 29.5 KB
Newer Older
1
2
#ifdef HAVE_CONFIG_H
#include "config.h"
3
4
5
6
7
8
9
10
11
12
13
14
15
#endif

#ifdef HAVE_LIBNETCDF

#include <limits.h>
#include <float.h>

#include "dmemory.h"
#include "cdi.h"
#include "cdi_int.h"
#include "stream_cdf.h"
#include "cdf_int.h"
#include "vlist.h"
16
#include "vlist_var.h"
17
18
19
20
21


static
void cdfReadGridTraj(stream_t *streamptr, int gridID)
{
22
23
  const int vlistID = streamptr->vlistID;
  const int fileID  = streamptr->fileID;
24

25
26
27
  const int gridindex = vlistGridIndex(vlistID, gridID);
  const int lonID = streamptr->ncgrid[gridindex].ncIDs[CDF_VARID_X];
  const int latID = streamptr->ncgrid[gridindex].ncIDs[CDF_VARID_Y];
28

29
30
  const int tsID = streamptr->curTsID;
  const size_t index = (size_t)tsID;
31
32
33
34
35
36
37
38
39
40
41
42

  double xlon, xlat;
  cdf_get_var1_double(fileID, lonID, &index, &xlon);
  cdf_get_var1_double(fileID, latID, &index, &xlat);

  gridDefXvals(gridID, &xlon);
  gridDefYvals(gridID, &xlat);
}

static
void cdfGetSlapDescription(stream_t *streamptr, int varID, size_t (*start)[4], size_t (*count)[4])
{
43
44
45
46
47
48
  const int vlistID = streamptr->vlistID;
  const int tsID = streamptr->curTsID;
  const int gridID = vlistInqVarGrid(vlistID, varID);
  const int zaxisID = vlistInqVarZaxis(vlistID, varID);
  const int timetype = vlistInqVarTimetype(vlistID, varID);
  const int gridindex = vlistGridIndex(vlistID, gridID);
49
50
51

  if ( CDI_Debug ) Message("tsID = %d", tsID);

52
  int xid = CDI_UNDEFID, yid = CDI_UNDEFID;
53
54
55
56
57
58
  if ( gridInqType(gridID) == GRID_TRAJECTORY )
    {
      cdfReadGridTraj(streamptr, gridID);
    }
  else
    {
59
      xid = streamptr->ncgrid[gridindex].ncIDs[CDF_DIMID_X];
60
      yid = streamptr->ncgrid[gridindex].ncIDs[CDF_DIMID_Y];
61
    }
62
63
  const int zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
  const int zid = streamptr->zaxisID[zaxisindex];
64
65
66
67
68
69
70
71

  int ndims = 0;
#define addDimension(startCoord, length) do \
    { \
      (*start)[ndims] = startCoord; \
      (*count)[ndims] = length; \
      ndims++; \
    } while(0)
72
  if ( timetype != TIME_CONSTANT ) addDimension((size_t)tsID, 1);
73
  if ( zid != CDI_UNDEFID ) addDimension(0, (size_t)zaxisInqSize(zaxisID));
74
75
  if ( yid != CDI_UNDEFID ) addDimension(0, gridInqYsize(gridID));
  if ( xid != CDI_UNDEFID ) addDimension(0, gridInqXsize(gridID));
76
77
78
79
80
81
82
83
84
85
86
87
88
#undef addDimension

  assert(ndims <= (int)(sizeof(*start)/sizeof(**start)));
  assert(ndims <= (int)(sizeof(*count)/sizeof(**count)));

  if ( CDI_Debug )
    for (int idim = 0; idim < ndims; idim++)
      Message("dim = %d  start = %d  count = %d", idim, start[idim], count[idim]);
}

//Scans the data array for missVals, optionally applying first a scale factor and then an offset.
//Returns the number of missing + out-of-range values encountered.
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
size_t cdfDoInputDataTransformationDP(int vlistID, int varID, size_t valueCount, double *data)
90
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
93
94
95
96
97
98
99
100
101
  double missVal = vlistInqVarMissval(vlistID, varID);
  const bool haveMissVal = vlistInqVarMissvalUsed(vlistID, varID);
  double validRange[2];
  if (!(haveMissVal && vlistInqVarValidrange(vlistID, varID, validRange)))
    validRange[0] = DBL_MIN, validRange[1] = DBL_MAX;
  double validMin = validRange[0];
  double validMax = validRange[1];
  double offset = vlistInqVarAddoffset(vlistID, varID);
  double scaleFactor = vlistInqVarScalefactor(vlistID, varID);

  const bool haveOffset = IS_NOT_EQUAL(offset, 0);
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
192
193
194
195
  const bool haveScaleFactor = IS_NOT_EQUAL(scaleFactor, 1);
  size_t missValCount = 0;

  if ( IS_EQUAL(validMin, VALIDMISS) ) validMin = DBL_MIN;
  if ( IS_EQUAL(validMax, VALIDMISS) ) validMax = DBL_MAX;

  bool haveRangeCheck = (IS_NOT_EQUAL(validMax, DBL_MAX)) | (IS_NOT_EQUAL(validMin,DBL_MIN));
  assert(!haveRangeCheck || haveMissVal);

  switch ((int)haveMissVal | ((int)haveScaleFactor << 1)
          | ((int)haveOffset << 2) | ((int)haveRangeCheck << 3))
    {
    case 15: /* haveRangeCheck & haveMissVal & haveScaleFactor & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? missVal
            : isMissVal ? data[i] : data[i] * scaleFactor + offset;
        }
      break;
    case 13: /* haveRangeCheck & haveMissVal & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? missVal
            : isMissVal ? data[i] : data[i] + offset;
        }
      break;
    case 11: /* haveRangeCheck & haveMissVal & haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? missVal
            : isMissVal ? data[i] : data[i] * scaleFactor;
        }
      break;
    case 9: /* haveRangeCheck & haveMissVal */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? missVal : data[i];
        }
      break;
    case 7: /* haveMissVal & haveScaleFactor & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        if ( DBL_IS_EQUAL(data[i], missVal) )
          missValCount++;
        else
          data[i] = data[i] * scaleFactor + offset;
      break;
    case 6: /* haveOffset & haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        data[i] = data[i] * scaleFactor + offset;
      break;
    case 5: /* haveMissVal & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        if ( DBL_IS_EQUAL(data[i], missVal) )
          missValCount++;
        else
          data[i] += offset;
      break;
    case 4: /* haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        data[i] += offset;
      break;
    case 3: /* haveMissVal & haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        if ( DBL_IS_EQUAL(data[i], missVal) )
          missValCount++;
        else
          data[i] *= scaleFactor;
      break;
    case 2: /* haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        data[i] *= scaleFactor;
      break;
    case 1: /* haveMissVal */
      for ( size_t i = 0; i < valueCount; i++ )
        missValCount += (unsigned)DBL_IS_EQUAL(data[i], missVal);
      break;
    }

  return missValCount;
}

static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
size_t cdfDoInputDataTransformationSP(int vlistID, int varID, size_t valueCount, float *data)
197
 {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
200
201
202
203
204
205
206
207
208
  double missVal = vlistInqVarMissval(vlistID, varID);
  const bool haveMissVal = vlistInqVarMissvalUsed(vlistID, varID);
  double validRange[2];
  if (!(haveMissVal && vlistInqVarValidrange(vlistID, varID, validRange)))
    validRange[0] = DBL_MIN, validRange[1] = DBL_MAX;
  double validMin = validRange[0];
  double validMax = validRange[1];
  double offset = vlistInqVarAddoffset(vlistID, varID);
  double scaleFactor = vlistInqVarScalefactor(vlistID, varID);

  const bool haveOffset = IS_NOT_EQUAL(offset, 0);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  const bool haveScaleFactor = IS_NOT_EQUAL(scaleFactor, 1);
  size_t missValCount = 0;

  if ( IS_EQUAL(validMin, VALIDMISS) ) validMin = DBL_MIN;
  if ( IS_EQUAL(validMax, VALIDMISS) ) validMax = DBL_MAX;

  bool haveRangeCheck = (IS_NOT_EQUAL(validMax, DBL_MAX)) | (IS_NOT_EQUAL(validMin,DBL_MIN));
  assert(!haveRangeCheck || haveMissVal);

  switch ((int)haveMissVal | ((int)haveScaleFactor << 1)
          | ((int)haveOffset << 2) | ((int)haveRangeCheck << 3))
    {
    case 15: /* haveRangeCheck & haveMissVal & haveScaleFactor & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? (float)missVal
            : isMissVal ? data[i] : (float)(data[i] * scaleFactor + offset);
        }
      break;
    case 13: /* haveRangeCheck & haveMissVal & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? (float)missVal
            : isMissVal ? data[i] : (float)(data[i] + offset);
        }
      break;
    case 11: /* haveRangeCheck & haveMissVal & haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? (float)missVal
            : isMissVal ? data[i] : (float)(data[i] * scaleFactor);
        }
      break;
    case 9: /* haveRangeCheck & haveMissVal */
      for ( size_t i = 0; i < valueCount; i++ )
        {
          int outOfRange = data[i] < validMin || data[i] > validMax;
          int isMissVal = DBL_IS_EQUAL(data[i], missVal);
          missValCount += (size_t)(outOfRange | isMissVal);
          data[i] = outOfRange ? (float)missVal : data[i];
        }
      break;
    case 7: /* haveMissVal & haveScaleFactor & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        if ( DBL_IS_EQUAL(data[i], missVal) )
          missValCount++;
        else
          data[i] = (float)(data[i] * scaleFactor + offset);
      break;
    case 6: /* haveOffset & haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        data[i] = (float)(data[i] * scaleFactor + offset);
      break;
    case 5: /* haveMissVal & haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        if ( DBL_IS_EQUAL(data[i], missVal) )
          missValCount++;
        else
          data[i] = (float)(data[i] + offset);
      break;
    case 4: /* haveOffset */
      for ( size_t i = 0; i < valueCount; i++ )
        data[i] = (float)(data[i] + offset);
      break;
    case 3: /* haveMissVal & haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        if ( DBL_IS_EQUAL(data[i], missVal) )
          missValCount++;
        else
          data[i] = (float)(data[i] * scaleFactor);
      break;
    case 2: /* haveScaleFactor */
      for ( size_t i = 0; i < valueCount; i++ )
        data[i] = (float)(data[i] * scaleFactor);
      break;
    case 1: /* haveMissVal */
      for ( size_t i = 0; i < valueCount; i++ )
        missValCount += (unsigned)DBL_IS_EQUAL(data[i], missVal);
      break;
    }

  return missValCount;
}

static
size_t min_size(size_t a, size_t b)
{
  return a < b ? a : b;
}

static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
void transpose2dArrayDP(int gridId, double *data)
310
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
312
313
  size_t inWidth = gridInqYsize(gridId);
  size_t inHeight = gridInqXsize(gridId);
  
314
315
  const size_t cacheBlockSize = 256;   // Purely an optimization parameter. Current value of 32 means we are handling 8kB blocks,
                                       // which should be a decent compromise on many architectures.
316
317
  double **out = (double**) malloc(inWidth*sizeof(double*));
  double **temp = (double**) malloc(inHeight*sizeof(double*));
318
  temp[0] = (double *) malloc(inHeight*inWidth*sizeof(double));
319
  memcpy(temp[0], data, inHeight*inWidth*sizeof(double));
320
321
  for (size_t i = 0; i < inWidth; i++) out[i] = data + (inHeight*i);
  for (size_t i = 1; i < inHeight; i++) temp[i] = temp[0] + (inWidth*i);
322
323
324
325
326
327
328
329

  /*
  for ( size_t y = 0; y < inHeight; ++y )
    for ( size_t x = 0; x < inWidth; ++x )
      out[x][y] = temp[y][x];
  */

  for ( size_t yBlock = 0; yBlock < inHeight; yBlock += cacheBlockSize )
330
331
332
333
334
335
    for ( size_t xBlock = 0; xBlock < inWidth; xBlock += cacheBlockSize )
      for ( size_t y = yBlock, yEnd = min_size(yBlock + cacheBlockSize, inHeight); y < yEnd; y++ )
        for ( size_t x = xBlock, xEnd = min_size(xBlock + cacheBlockSize, inWidth); x < xEnd; x++ )
          {
            out[x][y] = temp[y][x];
          }
336

337
  free(out);
338
339
  free(temp[0]);
  free(temp);
340
341
342
}

static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
void transpose2dArraySP(size_t gridId, float *data)
344
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
346
347
  size_t inWidth = gridInqYsize(gridId);
  size_t inHeight = gridInqXsize(gridId);

348
349
  const size_t cacheBlockSize = 256;   // Purely an optimization parameter. Current value of 32 means we are handling 8kB blocks,
                                       // which should be a decent compromise on many architectures.
350
351
  float **out = (float**) malloc(inWidth*sizeof(float*));
  float **temp = (float**) malloc(inHeight*sizeof(float*));
352
  temp[0] = (float *) malloc(inHeight*inWidth*sizeof(float));
353
  memcpy(temp[0], data, inHeight*inWidth*sizeof(float));
354
355
  for (size_t i = 0; i < inWidth; i++) out[i] = data + (inHeight*i);
  for (size_t i = 1; i < inHeight; i++) temp[i] = temp[0] + (inWidth*i);
356
357
358
359
360
361
362
363

  /*
  for ( size_t y = 0; y < inHeight; ++y )
    for ( size_t x = 0; x < inWidth; ++x )
      out[x][y] = temp[y][x];
  */

  for ( size_t yBlock = 0; yBlock < inHeight; yBlock += cacheBlockSize )
364
365
366
367
368
369
    for ( size_t xBlock = 0; xBlock < inWidth; xBlock += cacheBlockSize )
      for ( size_t y = yBlock, yEnd = min_size(yBlock + cacheBlockSize, inHeight); y < yEnd; y++ )
        for ( size_t x = xBlock, xEnd = min_size(xBlock + cacheBlockSize, inWidth); x < xEnd; x++ )
          {
            out[x][y] = temp[y][x];
          }
370

371
  free(out);
372
373
  free(temp[0]);
  free(temp);
374
375
376
377
378
}

static
void cdfInqDimIds(stream_t *streamptr, int varId, int (*outDimIds)[3])
{
379
380
  const int gridId = vlistInqVarGrid(streamptr->vlistID, varId);
  const int gridindex = vlistGridIndex(streamptr->vlistID, gridId);
381

382
  (*outDimIds)[0] = (*outDimIds)[1] = (*outDimIds)[2] = CDI_UNDEFID;
383
384
385
386
387
388
389
  switch ( gridInqType(gridId) )
    {
      case GRID_TRAJECTORY:
        cdfReadGridTraj(streamptr, gridId);
        break;

      case GRID_UNSTRUCTURED:
390
        (*outDimIds)[0] = streamptr->ncgrid[gridindex].ncIDs[CDF_DIMID_X];
391
392
        break;

393
394
395
396
      case GRID_GAUSSIAN_REDUCED:
        (*outDimIds)[0] = streamptr->ncgrid[gridindex].ncIDs[CDF_DIMID_X];
        break;

397
      default:
398
        (*outDimIds)[0] = streamptr->ncgrid[gridindex].ncIDs[CDF_DIMID_X];
399
        (*outDimIds)[1] = streamptr->ncgrid[gridindex].ncIDs[CDF_DIMID_Y];
400
401
402
        break;
    }

403
404
  const int zaxisID = vlistInqVarZaxis(streamptr->vlistID, varId);
  const int zaxisindex = vlistZaxisIndex(streamptr->vlistID, zaxisID);
405
406
407
408
409
410
  (*outDimIds)[2] = streamptr->zaxisID[zaxisindex];
}

static
int cdfGetSkipDim(int fileId, int ncvarid, int (*dimIds)[3])
{
411
412
  if((*dimIds)[0] != CDI_UNDEFID) return 0;
  if((*dimIds)[1] != CDI_UNDEFID) return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413

414
415
416
417
418
419
  int nvdims;
  cdf_inq_varndims(fileId, ncvarid, &nvdims);
  if(nvdims != 3) return 0;

  int varDimIds[3];
  cdf_inq_vardimid(fileId, ncvarid, varDimIds);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420

421
422
423
424
425
426
427
428
429
430
431
  size_t size = 0;
  if ( (*dimIds)[2] == varDimIds[2] )
    {
      cdf_inq_dimlen(fileId, varDimIds[1], &size);
      if ( size == 1 ) return 1;
    }
  else if ( (*dimIds)[2] == varDimIds[1] )
    {
      cdf_inq_dimlen(fileId, varDimIds[2], &size);
      if ( size == 1 ) return 2;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432

433
434
435
436
437
438
  return 0;
}

static
void cdfGetSliceSlapDescription(stream_t *streamptr, int varId, int levelId, bool *outSwapXY, size_t (*start)[4], size_t (*count)[4])
{
439
  const int tsID = streamptr->curTsID;
440
441
  if ( CDI_Debug ) Message("tsID = %d", tsID);

442
443
444
  const int fileId = streamptr->fileID;
  const int vlistId = streamptr->vlistID;
  const int ncvarid = streamptr->vars[varId].ncvarid;
445

446
447
448
  const int gridId = vlistInqVarGrid(vlistId, varId);
  const int timetype = vlistInqVarTimetype(vlistId, varId);
  const size_t gridsize = gridInqSize(gridId);
449
450
451

  streamptr->numvals += gridsize;

452
  int dimIds[3]; // this array joins the old variables xid, yid, and zid
453
454
  cdfInqDimIds(streamptr, varId, &dimIds);

455
  const int skipdim = cdfGetSkipDim(fileId, ncvarid, &dimIds);
456
457
458
459

  int dimorder[3];
  vlistInqVarDimorder(vlistId, varId, &dimorder);

460
  *outSwapXY = (dimorder[2] == 2 || dimorder[0] == 1) && dimIds[0] != CDI_UNDEFID && dimIds[1] != CDI_UNDEFID ;
461
462
463
464
465
466
467
468
469

  int ndims = 0;

#define addDimension(startIndex, extent) do {   \
      (*start)[ndims] = startIndex; \
      (*count)[ndims] = extent; \
      ndims++; \
  } while(0)

470
  if ( timetype != TIME_CONSTANT ) addDimension((size_t)tsID, 1);
471
472
473
474
475
  if ( skipdim == 1 ) addDimension(0, 1);

  for ( int id = 0; id < 3; ++id )
    {
      size_t size;
476
      const int curDimId = dimIds[dimorder[id]-1];
477
      if ( curDimId == CDI_UNDEFID ) continue;
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
      switch ( dimorder[id] )
        {
          case 1:
          case 2:
            cdf_inq_dimlen(fileId, curDimId, &size);
            addDimension(0, size);
            break;
          case 3:
            addDimension((size_t)levelId, 1);
            break;
          default:
            Error("Internal errror: Malformed dimension order encountered. Please report this bug.\n");
        }
    }

  if ( skipdim == 2 ) addDimension(0, 1);

  assert(ndims <= (int)(sizeof(*start)/sizeof(**start)));
  assert(ndims <= (int)(sizeof(*count)/sizeof(**count)));

#undef addDimension

  if ( CDI_Debug )
    for (int idim = 0; idim < ndims; idim++)
502
      Message("dim = %d  start = %d  count = %d", idim, (*start)[idim], (*count)[idim]);
503
504
505
506
507
508
509
510

  int nvdims;
  cdf_inq_varndims(fileId, ncvarid, &nvdims);

  if ( nvdims != ndims )
    Error("Internal error, variable %s has an unsupported array structure!", vlistInqVarNamePtr(vlistId, varId));
}

511
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
size_t getSizeVar3D(int vlistID, int varID)
513
{
514
515
  const int gridID  = vlistInqVarGrid(vlistID, varID);
  const int zaxisID = vlistInqVarZaxis(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
  return gridInqSize(gridID) * (size_t)zaxisInqSize(zaxisID);
}


static
void cdfReadDataSliceSP2DP(int fileID, int ncvarid, size_t length, size_t start[4], size_t count[4], double *data)
{
  float *data_fp = (float *) Malloc(length*sizeof(*data_fp));
  cdf_get_vara_float(fileID, ncvarid, start, count, data_fp);
  for ( size_t i = 0; i < length; i++ ) data[i] = (double) data_fp[i];
  Free(data_fp);
}

static
void cdfReadDataSliceDP2SP(int fileID, int ncvarid, size_t length, size_t start[4], size_t count[4], float *data)
{
  double *data_dp = (double *) Malloc(length*sizeof(*data_dp));
  cdf_get_vara_double(fileID, ncvarid, start, count, data_dp);
  for ( size_t i = 0; i < length; i++ ) data[i] = (float) data_dp[i];
  Free(data_dp);
}

static
void cdfCheckDataDP_UINT8(int fileID, int ncvarid, int vlistID, int varID, size_t length, double *data)
{
  if ( vlistInqVarDatatype(vlistID, varID) == CDI_DATATYPE_UINT8 )
    {
      nc_type xtype;
      cdf_inq_vartype(fileID, ncvarid, &xtype);
      if ( xtype == NC_BYTE )
        {
          for ( size_t i = 0; i < length; i++ )
            if ( data[i] < 0 ) data[i] += 256;
        }
    }
}

static
void cdfCheckDataSP_UINT8(int fileID, int ncvarid, int vlistID, int varID, size_t length, float *data)
{
  if ( vlistInqVarDatatype(vlistID, varID) == CDI_DATATYPE_UINT8 )
    {
      nc_type xtype;
      cdf_inq_vartype(fileID, ncvarid, &xtype);
      if ( xtype == NC_BYTE )
        {
          for ( size_t i = 0; i < length; i++ )
            if ( data[i] < 0 ) data[i] += 256;
        }
    }
}
567

Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
569
570
static
void cdfReadDataDP(stream_t *streamptr, int varID, size_t length, size_t start[4], size_t count[4], double *data)
{
571
572
573
  const int vlistID = streamptr->vlistID;
  const int fileID = streamptr->fileID;
  const int ncvarid = streamptr->vars[varID].ncvarid;
574

Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
  if ( vlistInqVarDatatype(vlistID, varID) == CDI_DATATYPE_FLT32 )
    {
      cdfReadDataSliceSP2DP(fileID, ncvarid, length, start, count, data);
    }
  else
    {
      cdf_get_vara_double(fileID, ncvarid, start, count, data);
      
      cdfCheckDataDP_UINT8(fileID, ncvarid, vlistID, varID, length, data);
    }
}

static
void cdfReadDataSP(stream_t *streamptr, int varID, size_t length, size_t start[4], size_t count[4], float *data)
{
590
591
592
  const int vlistID = streamptr->vlistID;
  const int fileID = streamptr->fileID;
  const int ncvarid = streamptr->vars[varID].ncvarid;
593

Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  if ( vlistInqVarDatatype(vlistID, varID) == CDI_DATATYPE_FLT64 )
    {
      cdfReadDataSliceDP2SP(fileID, ncvarid, length, start, count, data);
    }
  else
    {
      cdf_get_vara_float(fileID, ncvarid, start, count, data);
      
      cdfCheckDataSP_UINT8(fileID, ncvarid, vlistID, varID, length, data);
    }
}

static
void cdfReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
{
  if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamptr->self, varID);

611
  const int vlistID = streamptr->vlistID;
612

Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
  size_t start[4], count[4];
614
615
  cdfGetSlapDescription(streamptr, varID, &start, &count);

616
  const size_t length = getSizeVar3D(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
  cdfReadDataDP(streamptr, varID, length, start, count, data);
618

Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
620
621
}

622
static
623
void cdfReadVarSP(stream_t *streamptr, int varID, float *data, size_t *nmiss)
624
625
626
{
  if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamptr->self, varID);

627
  const int vlistID = streamptr->vlistID;
628

Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
  size_t start[4], count[4];
630
631
  cdfGetSlapDescription(streamptr, varID, &start, &count);

632
  const size_t length = getSizeVar3D(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
  cdfReadDataSP(streamptr, varID, length, start, count, data);
634

Uwe Schulzweida's avatar
Uwe Schulzweida committed
635
  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
636
637
638
}


639
void cdf_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *nmiss)
640
641
642
643
644
645
646
647
{
  if ( memtype == MEMTYPE_DOUBLE )
    cdfReadVarDP(streamptr, varID, (double*) data, nmiss);
  else
    cdfReadVarSP(streamptr, varID, (float*) data, nmiss);
}

static
648
void cdfReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *nmiss)
649
650
651
652
653
{
  if ( CDI_Debug )
    Message("streamID = %d  varID = %d  levelID = %d", streamptr->self, varID, levelID);

  bool swapxy;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
654
  size_t start[4], count[4];
655
656
  cdfGetSliceSlapDescription(streamptr, varID, levelID, &swapxy, &start, &count);

657
658
659
  const int vlistID = streamptr->vlistID;
  const int gridId = vlistInqVarGrid(vlistID, varID);
  const size_t length = gridInqSize(gridId);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
660
  cdfReadDataDP(streamptr, varID, length, start, count, data);
661

Uwe Schulzweida's avatar
Uwe Schulzweida committed
662
  if ( swapxy ) transpose2dArrayDP(gridId, data);
663

Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
665
666
}

667
static
668
void cdfReadVarSliceSP(stream_t *streamptr, int varID, int levelID, float *data, size_t *nmiss)
669
670
671
672
673
{
  if ( CDI_Debug )
    Message("streamID = %d  varID = %d  levelID = %d", streamptr->self, varID, levelID);

  bool swapxy;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
674
  size_t start[4], count[4];
675
676
  cdfGetSliceSlapDescription(streamptr, varID, levelID, &swapxy, &start, &count);

677
678
679
  const int vlistID = streamptr->vlistID;
  const int gridId = vlistInqVarGrid(vlistID, varID);
  const size_t length = gridInqSize(gridId);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
  cdfReadDataSP(streamptr, varID, length, start, count, data);
681

Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
  if ( swapxy ) transpose2dArraySP(gridId, data);
683

Uwe Schulzweida's avatar
Uwe Schulzweida committed
684
  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
685
686
687
}


688
void cdf_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *nmiss)
689
690
691
692
693
694
695
696
{
  if ( memtype == MEMTYPE_DOUBLE )
    cdfReadVarSliceDP(streamptr, varID, levelID, (double*) data, nmiss);
  else
    cdfReadVarSliceSP(streamptr, varID, levelID, (float*) data, nmiss);
}


697
void cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss)
698
699
700
{
  if ( CDI_Debug ) Message("streamID = %d", streamptr->self);

701
702
703
704
705
  const int tsID    = streamptr->curTsID;
  const int vrecID  = streamptr->tsteps[tsID].curRecID;
  const int recID   = streamptr->tsteps[tsID].recIDs[vrecID];
  const int varID   = streamptr->tsteps[tsID].records[recID].varID;
  const int levelID = streamptr->tsteps[tsID].records[recID].levelID;
706

707
  if ( memtype == MEMTYPE_DOUBLE )
708
    cdfReadVarSliceDP(streamptr, varID, levelID, (double*) data, nmiss);
709
  else
710
    cdfReadVarSliceSP(streamptr, varID, levelID, (float*) data, nmiss);
711
712
}

713
714
715
716
717
718
719
720
721
722
723
//----------------------------------------------------------------------------
// Parallel Version
//----------------------------------------------------------------------------

void cdfReadVarSliceDPPart(stream_t *streamptr, int varID, int levelID, int varType, int startpoint, size_t length, double *data, size_t *nmiss)
{
  (void)(varType);

  if ( CDI_Debug )
    Message("streamID = %d  varID = %d  levelID = %d", streamptr->self, varID, levelID);

724
  const int vlistID = streamptr->vlistID;
725
726

  bool swapxy;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
727
  size_t start[4], count[4];
728
729
  cdfGetSliceSlapDescription(streamptr, varID, levelID, &swapxy, &start, &count);

730
731
  const int gridId = vlistInqVarGrid(vlistID, varID);
  const size_t gridsize = gridInqSize(gridId);
732
733
734
735
736
737
738
739
740

  unsigned int position = 0; 
  for (int i=0 ; i<4 ; i++)
    if (count[i] == gridsize)
      position = i;

  start[position] = start[position]+startpoint;
  count[position] = length;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
741
  cdfReadDataDP(streamptr, varID, length, start, count, data);
742

Uwe Schulzweida's avatar
Uwe Schulzweida committed
743
  if ( swapxy ) transpose2dArrayDP(gridId, data);
744

Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
746
747
748
749
750
751
752
753
754
}

void cdfReadVarSliceSPPart(stream_t *streamptr, int varID, int levelID, int varType, int startpoint, size_t length, float *data, size_t *nmiss)
{
  (void)(varType);

  if ( CDI_Debug )
    Message("streamID = %d  varID = %d  levelID = %d", streamptr->self, varID, levelID);

755
  const int vlistID = streamptr->vlistID;
756
757

  bool swapxy;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
  size_t start[4], count[4];
759
760
  cdfGetSliceSlapDescription(streamptr, varID, levelID, &swapxy, &start, &count);

761
  const int gridId = vlistInqVarGrid(vlistID, varID);
762
  size_t gridsize = gridInqSize(gridId);
763
764
765
766
767
768
769
770
771

  unsigned int position = 0; 
  for (int i=0 ; i<4 ; i++)
    if (count[i] == gridsize)
      position = i;

  start[position] = start[position]+startpoint;
  count[position] = length;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
  cdfReadDataSP(streamptr, varID, length, start, count, data);
773

Uwe Schulzweida's avatar
Uwe Schulzweida committed
774
  if ( swapxy ) transpose2dArraySP(gridId, data);
775

Uwe Schulzweida's avatar
Uwe Schulzweida committed
776
  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
777
778
779
780
781
782
783
784
785
786
787
788
789
}

static
int cdiStreamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, size_t size, int memtype, void *data, size_t *nmiss)
{
  int status = 0;

  if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamID, varID);

  check_parg(data);
  check_parg(nmiss);

  stream_t *streamptr = stream_to_pointer(streamID);
790
  const int filetype = streamptr->filetype;
791
792
793
794
795
796
797

  *nmiss = 0;

  // currently we only care for netcdf data
  switch (filetype)
    {
#if defined (HAVE_LIBNETCDF)
798
799
800
801
802
    case CDI_FILETYPE_NC:
    case CDI_FILETYPE_NC2:
    case CDI_FILETYPE_NC4:
    case CDI_FILETYPE_NC4C:
    case CDI_FILETYPE_NC5:
803
804
      {
        if ( memtype == MEMTYPE_FLOAT )
805
          cdfReadVarSliceSPPart(streamptr, varID, levelID, varType, start, size, (float *)data, nmiss);
806
        else
807
          cdfReadVarSliceDPPart(streamptr, varID, levelID, varType, start, size, (double *)data, nmiss);
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
        break;
      }
#endif
    default:
      {
        Error("%s support not compiled in!", strfiletype(filetype));
        status = 2;
        break;
      }
    }

  return status;
}


void cdfReadVarDPPart(stream_t *streamptr, int varID, int varType, int startpoint, size_t length, double *data, size_t *nmiss)
{
  (void)(varType);
  if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamptr->self, varID);

828
829
  const int vlistID = streamptr->vlistID;
  const int ncvarid = streamptr->vars[varID].ncvarid;
830

Uwe Schulzweida's avatar
Uwe Schulzweida committed
831
  size_t start[4], count[4];
832
833
  cdfGetSlapDescription(streamptr, varID, &start, &count);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
835
836
  const int ltime = TIME_CONSTANT != vlistInqVarTimetype(vlistID, varID);
  start[1+ltime] = start[1+ltime]+startpoint;
  count[1+ltime] = length;
837

Uwe Schulzweida's avatar
Uwe Schulzweida committed
838
  cdf_get_vara_double(streamptr->fileID, ncvarid, start, count, data);
839

Uwe Schulzweida's avatar
Uwe Schulzweida committed
840
  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
841
842
843
844
845
846
847
}

void cdfReadVarSPPart(stream_t *streamptr, int varID, int varType, int startpoint, size_t length, float *data, size_t *nmiss)
{
  (void)(varType);
  if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamptr->self, varID);

848
849
  const int vlistID = streamptr->vlistID;
  const int ncvarid = streamptr->vars[varID].ncvarid;
850

Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
  size_t start[4], count[4];
852
853
  cdfGetSlapDescription(streamptr, varID, &start, &count);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
854
855
856
  const int ltime = TIME_CONSTANT != vlistInqVarTimetype(vlistID, varID);
  start[1+ltime] = start[1+ltime]+startpoint;
  count[1+ltime] = length;
857

Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
  cdf_get_vara_float(streamptr->fileID, ncvarid, start, count, data);
859

Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
861
862
863
864
865
866
867
868
869
870
871
872
}

static
void cdiStreamReadVarPart(int streamID, int varID, int varType, int start, size_t size, int memtype, void *data, size_t *nmiss)
{
  (void)(varType);
  if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamID, varID);

  check_parg(data);
  check_parg(nmiss);

  stream_t *streamptr = stream_to_pointer(streamID);
873
  const int filetype = streamptr->filetype;
874
875
876
877
878
879
880

  *nmiss = 0;

  // currently we only care for netcdf data
  switch (filetype)
    {
#if defined (HAVE_LIBNETCDF)
881
882
883
884
885
    case CDI_FILETYPE_NC:
    case CDI_FILETYPE_NC2:
    case CDI_FILETYPE_NC4:
    case CDI_FILETYPE_NC4C:
    case CDI_FILETYPE_NC5:
886
887
      {
        if ( memtype == MEMTYPE_FLOAT )
888
          cdfReadVarSPPart(streamptr, varID, varType, start, size, (float *)data, nmiss);
889
        else
890
          cdfReadVarDPPart(streamptr, varID, varType, start, size, (double *)data, nmiss);
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

        break;
      }
#endif
    default:
      {
        Error("%s support not compiled in!", strfiletype(filetype));
        break;
      }
    }
}

void streamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, size_t size, void *data, size_t *nmiss, int memtype)
{
  if ( cdiStreamReadVarSlicePart(streamID, varID, levelID, varType, start, size, memtype, data, nmiss) )
    {
      Error("Unexpected error returned from cdiStreamReadVarSlicePart()!");
    }
}

void streamReadVarPart(int streamID, int varID, int varType, int start, size_t size, void *data, size_t *nmiss, int memtype)
{
  cdiStreamReadVarPart(streamID, varID, varType, start, size, memtype, data, nmiss);
}

916
#endif
917