stream_cdf.c 135 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <float.h>

#include "dmemory.h"

#include "cdi.h"
#include "basetime.h"
#include "util.h"  /* gaussaw */
#include "stream_int.h"
#include "stream_cdf.h"
#include "cdf_int.h"
#include "varscan.h"
#include "vlist.h"


#if  defined  (HAVE_LIBNETCDF)
#  include "netcdf.h"
#endif

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


void cdfDefTime(int streamID);
void cdfDefGlobalAtts(int streamID);
void cdfDefLocalAtts(int streamID);

#define  MAXNAMELEN  256

#define  X_AXIS  1
#define  Y_AXIS  2
#define  Z_AXIS  3
#define  T_AXIS  4

typedef struct {
  int     ncvarid;
  int     dimtype;
  size_t  len;
  char    name[MAXNAMELEN];
}
NCDIM;

typedef struct {
  int      vartype;
  int      isvar;
  int      islon;
  int      islat;
  int      islev;
  int      warn;
  int      timeID;
  int      code;
  int      bounds;
  int      gridID;
  int      zaxisID;
  int      gridtype;
  int      zaxistype;
  int      xdim;
  int      ydim;
  int      zdim;
  int      xvarid;
  int      yvarid;
  int      zvarid;
  int      tvarid;
  int      varids[4];
  int      cellarea;
  int      calendar;
  int      tableID;
  int      truncation;
  int      defmiss;
  int      xtype;
  int      ndims;
  int      gmapid;
  int      positive;
  int      dimids[8];
  int      dimtype[8];
  size_t   vlen;
  double  *vdata;
  double   missval;
  double   addoffset;
  double   scalefactor;
  char     name[MAXNAMELEN];
  char     longname[MAXNAMELEN];
  char     stdname[MAXNAMELEN];
  char     units[MAXNAMELEN];
}
NCVAR;


static int splitBasetime(char *timeunits, TAXIS *taxis)
{
  static char func[] = "splitBasetime";
100
  int len, i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
102
103
104
105
106
107
108
  char *ptu;
  int year, month, day;
  int hour = 0, minute = 0, second = 0;
  int timetype = TAXIS_ABSOLUTE;
  int rdate = -1, rtime = -1;
  int timeunit = -1;

  ptu = timeunits;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
  len = (int) strlen(timeunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110

111
112
  for ( i = 0; i < len; i++ ) ptu[i] = tolower((int) ptu[i]);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
114
115
116
117
118
119
120
121
122
123
  (*taxis).type = timetype;

  if      ( strncmp(ptu, "sec",    3) == 0 ) timeunit = TUNIT_SECOND;
  else if ( strncmp(ptu, "minute", 6) == 0 ) timeunit = TUNIT_MINUTE;
  else if ( strncmp(ptu, "hour",   4) == 0 ) timeunit = TUNIT_HOUR;
  else if ( strncmp(ptu, "day",    3) == 0 ) timeunit = TUNIT_DAY;
  else if ( strncmp(ptu, "month",  5) == 0 ) timeunit = TUNIT_MONTH;
  else if ( strncmp(ptu, "calendar_month", 14) == 0 ) timeunit = TUNIT_MONTH;
  else if ( strncmp(ptu, "year",   4) == 0 ) timeunit = TUNIT_YEAR;
  else
    {
124
      Message(func, "Unsupported TIMEUNIT: %s!", ptu);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      return (1);
    }

  while ( ! isspace(*ptu) && *ptu != 0 ) ptu++;
  if ( *ptu )
    {
      while (   isspace(*ptu) ) ptu++;

      if ( strncmp(ptu, "as", 2) == 0 )
	timetype = TAXIS_ABSOLUTE;
      else if ( strncmp(ptu, "since", 5) == 0 )
	timetype = TAXIS_RELATIVE;

      while ( ! isspace(*ptu) && *ptu != 0 ) ptu++;
      if ( *ptu )
	{
	  while (   isspace(*ptu) ) ptu++;

	  if ( timetype == TAXIS_ABSOLUTE )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
	      if ( strncmp(ptu, "%y%m%d.%f", 9) != 0 && timeunit == TUNIT_DAY )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
148
149
		{
		  Message(func, "Unsupported format %s for TIMEUNIT day!", ptu);
		  timeunit = -1;
		}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
	      else if ( strncmp(ptu, "%y%m.%f", 7) != 0 && timeunit == TUNIT_MONTH )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
196
197
198
199
200
201
202
203
204
205
206
207
208
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
		{
		  Message(func, "Unsupported format %s for TIMEUNIT month!", ptu);
		  timeunit = -1;
		}
	    }
	  else if ( timetype == TAXIS_RELATIVE )
	    {
	      year = atoi(ptu);
	      while ( isdigit((int) *ptu) ) ptu++;
	      month = atoi(++ptu);
	      while ( isdigit((int) *ptu) ) ptu++;
	      day = atoi(++ptu);
	      while ( isdigit((int) *ptu) ) ptu++;

	      while ( isspace((int) *ptu) ) ptu++;

	      if ( *ptu )
		{
		  while ( ! isdigit((int) *ptu) ) ptu++;

		  hour = atoi(ptu);
		  while ( isdigit((int) *ptu) ) ptu++;
		  if ( *ptu == ':' )
		    {
		      ptu++;
		      minute = atoi(ptu);
		      while ( isdigit((int) *ptu) ) ptu++;
		      if ( *ptu == ':' )
			{
			  ptu++;
			  second = atoi(ptu);
			  if ( second != 0 )
			    Message(func, "Seconds not supported in time units!");
			}
		    }
		}

	      rdate = year*10000 + month*100 + day;
	      rtime = hour*100 + minute;
	      (*taxis).rdate = rdate;
	      (*taxis).rtime = rtime;
	    }
	}
    }

  (*taxis).type = timetype;
  (*taxis).unit = timeunit;

  if ( CDI_Debug )
    Message(func, "timetype = %d  unit = %d", timetype, timeunit);

  return (0);
}


#if  defined  (HAVE_LIBNETCDF)
static void cdfGetAttInt(int fileID, int ncvarid, char *attname, int *attint)
{
  static char func[] = "cdfGetAttInt";
  size_t attlen;
  int intatt;
  int *pintatt;

  cdf_inq_attlen(fileID, ncvarid, attname, &attlen);

  if ( attlen > 1 )
    pintatt = (int *) malloc(attlen*sizeof(int));
  else
    pintatt = &intatt;

  cdf_get_att_int(fileID, ncvarid, attname, pintatt);

  *attint = *pintatt;

  if ( attlen > 1 )
    free(pintatt);
}
#endif

#if  defined  (HAVE_LIBNETCDF)
static void cdfGetAttDouble(int fileID, int ncvarid, char *attname, double *attdouble)
{
  static char func[] = "cdfGetAttInt";
  size_t attlen;
  double doubleatt;
  double *pdoubleatt;

  cdf_inq_attlen(fileID, ncvarid, attname, &attlen);

  if ( attlen > 1 )
    pdoubleatt = (double *) malloc(attlen*sizeof(double));
  else
    pdoubleatt = &doubleatt;

  cdf_get_att_double(fileID, ncvarid, attname, pdoubleatt);

  *attdouble = *pdoubleatt;

  if ( attlen > 1 )
    free(pdoubleatt);
}
#endif

#if  defined  (HAVE_LIBNETCDF)
static void cdfGetAttText(int fileID, int ncvarid, char *attname, char *atttext, int maxlen)
{
  size_t attlen;
  char attbuf[65636];

  cdf_inq_attlen(fileID, ncvarid, attname, &attlen);

262
  if ( attlen < sizeof(attbuf) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
264
265
266
267
268
269
270
    {
      cdf_get_att_text(fileID, ncvarid, attname, attbuf);

      attbuf[attlen++] = 0;

      if ( (int) attlen > maxlen ) attlen = maxlen;
      memcpy(atttext, attbuf, attlen);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
272
273
274
  else
    {
      atttext[0] = 0;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
276
277
278
279
280
281
282
}
#endif

#if  defined  (HAVE_LIBNETCDF)
int cdfInqDatatype(int xtype)
{
  int datatype;

283
284
285
286
287
  if      ( xtype == NC_BYTE  )  datatype = DATATYPE_INT8;
  else if ( xtype == NC_SHORT )  datatype = DATATYPE_INT16;
  else if ( xtype == NC_INT   )  datatype = DATATYPE_INT32;
  else if ( xtype == NC_FLOAT )  datatype = DATATYPE_FLT32;
  else                           datatype = DATATYPE_FLT64;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290
291
292
293
294
295
296
297

  return (datatype);
}
#endif

#if  defined  (HAVE_LIBNETCDF)
int cdfDefDatatype(int datatype)
{
  int xtype;

298
  if      ( datatype == DATATYPE_INT8  )  xtype = NC_BYTE;
299
300
  else if ( datatype == DATATYPE_INT16 )  xtype = NC_SHORT;
  else if ( datatype == DATATYPE_INT32 )  xtype = NC_INT;
301
  else if ( datatype == DATATYPE_FLT64 )  xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
303
304
305
306
307
308
  else                                    xtype = NC_FLOAT;

  return (xtype);
}
#endif


Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
int cdfCopyRecord(int streamID2, int streamID1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
311
312
{
  static char func[] = "cdfCopyRecord";
  double *data;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
  int datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
  int tsID1, tsID2, recID1, recID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
316
317
  int ivarID, gridID;
  int nmiss;
  int ierr = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320
  int vlistID1, vlistID2;
  STREAM *streamptr1;
  STREAM *streamptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321

Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
  streamptr1 = stream_to_pointer(streamID1);
  streamptr2 = stream_to_pointer(streamID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324

Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
326
  stream_check_ptr(func, streamptr1);
  stream_check_ptr(func, streamptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327

Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
329
  vlistID1 = streamptr1->vlistID;
  vlistID2 = streamptr2->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330

Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
332
  tsID1 = streamptr1->curTsID;
  tsID2 = streamptr2->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333

Uwe Schulzweida's avatar
Uwe Schulzweida committed
334
335
  recID1 = streamptr1->tsteps[tsID1].curRecID;
  recID2 = streamptr2->tsteps[tsID2].curRecID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
336

Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338
339
  ivarID = streamptr1->tsteps[tsID1].records[recID1].varID;

  gridID = vlistInqVarGrid(vlistID1, ivarID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
341
342
343
344
345
346

  datasize = gridInqSize(gridID);      
  /* bug fix for constant netCDF fields */
  if ( datasize < 1048576 ) datasize = 1048576;

  data = (double *) malloc(datasize*sizeof(double));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
  streamReadRecord(streamID1, data, &nmiss);
  streamWriteRecord(streamID2, data, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
352
353

  free(data);

  return (ierr);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354

Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
358
359
/* not used
int cdfInqRecord(int streamID, int *varID, int *levelID)
{
  static char func[] = "cdfInqRecord";
  int tsID, recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363

Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365

Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
368
  recID = streamptr->tsteps[0].curRecID++;
  printf("cdfInqRecord recID %d %d\n", recID, streamptr->tsteps[0].curRecID);
  printf("cdfInqRecord tsID %d\n", streamptr->curTsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369

Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
371
  if ( streamptr->tsteps[0].curRecID >= 
       streamptr->tsteps[0].nrecs )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
      streamptr->tsteps[0].curRecID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
  *varID   = streamptr->tsteps[0].records[recID].varID;
  *levelID = streamptr->tsteps[0].records[recID].levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378

Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
  streamptr->record->varID   = *varID;
  streamptr->record->levelID = *levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
383
384
385
386
387
388
389
390
391

  if ( CDI_Debug )
    Message(func, "recID = %d  varID = %d  levelID = %d", recID, *varID, *levelID);
  
  return (recID+1);
}
*/
int cdfDefRecord(int streamID)
{
  static char func[] = "cdfDefRecord";
  int ierr = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
402
403
404
405
406
407
408
409
410
411

  return (ierr);
}

void cdfWriteGridTraj(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  int tsID, fileID;
  int lonID, latID, gridindex;
  size_t index;
  double xlon, xlat;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
413
414
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415
416
417
418
419

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
421
  lonID = streamptr->xdimID[gridindex];
  latID = streamptr->ydimID[gridindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
424

  xlon = gridInqXval(gridID, 0);
  xlat = gridInqYval(gridID, 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
425
  tsID = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  index = tsID;

  cdf_put_var1_double(fileID, lonID, &index, &xlon);
  cdf_put_var1_double(fileID, latID, &index, &xlat);
#endif
}


void cdfReadGridTraj(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  int tsID, fileID;
  int lonID, latID, gridindex;
  size_t index;
  double xlon, xlat;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
443
444
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
445
446
447
448
449

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450
451
  lonID = streamptr->xdimID[gridindex];
  latID = streamptr->ydimID[gridindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452

Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
  tsID = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
  index = tsID;

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

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


void cdfDefMissval(int streamID, int varID, int dtype)
{
#if  defined  (HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469
470
471
472
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);

  if ( streamptr->vars[varID].defmiss == FALSE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
475
476
477
478
479
480
481
    {
      int fileID;
      int ncvarid;
      double missval;
      int vlistID;
      int xtype;

      vlistID = streamInqVlist(streamID);
      fileID  = streamInqFileID(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
      ncvarid = streamptr->vars[varID].ncvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
      missval = vlistInqVarMissval(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487
488

      xtype = cdfDefDatatype(dtype);

      cdf_put_att_double(fileID, ncvarid, "_FillValue", (nc_type) xtype, 1L, &missval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
490
491

      if ( cdiNcMissingValue == 1 )
	cdf_put_att_double(fileID, ncvarid, "missing_value", (nc_type) xtype, 1L, &missval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492

Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
      if ( streamptr->ncmode == 2 ) cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494

Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
      streamptr->vars[varID].defmiss = TRUE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
497
498
499
500
501
502
503
504
505
    }
#endif
}

void cdfWriteRecord(int streamID, double *data, int nmiss)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfWriteRecord";
  int varID;
  int levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509

Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
511
  varID   = streamptr->record->varID;
  levelID = streamptr->record->levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513
514
515
516
517
518
519
520
521
522
523
524

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

  cdfWriteVarSliceDP(streamID, varID, levelID, data, nmiss);
#endif
}

int cdfReadRecord(int streamID, double *data, int *nmiss)
{
  static char func[] = "cdfReadRecord";
  int ierr = 0;
  int levelID, varID, tsID, recID, vrecID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
526
527
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
528
529
530

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
534
535
  tsID    = streamptr->curTsID;
  vrecID  = streamptr->tsteps[tsID].curRecID;
  recID   = streamptr->tsteps[tsID].recIDs[vrecID];
  varID   = streamptr->tsteps[tsID].records[recID].varID;
  levelID = streamptr->tsteps[tsID].records[recID].levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551

  cdfReadVarSliceDP(streamID, varID, levelID, data, nmiss);

  return (ierr);
}


void cdfDefTimeValue(int streamID, int tsID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefTimeValue";
  int fileID;
  double timevalue;
  int ncvarid;
  size_t index;
  TAXIS *taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
553
554
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
557
558
559
560

  fileID = streamInqFileID(streamID);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
561
  taxis = &streamptr->tsteps[tsID].taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562

Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
  if ( streamptr->ncmode == 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565
    {
      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
568
569
570
    }

  index = tsID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
571
  timevalue = encode_timeval(taxis->vdate, taxis->vtime, &streamptr->tsteps[0].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
573
  if ( CDI_Debug ) Message(func, "tsID = %d  timevalue = %f", tsID, timevalue);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
  ncvarid = streamptr->basetime.ncvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
576
577
578
579
580
  cdf_put_var1_double(fileID, ncvarid, &index, &timevalue);

  if ( taxis->has_bounds )
    {
      size_t start[2], count[2];

Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
      ncvarid = streamptr->basetime.ncvarboundsid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
582

Uwe Schulzweida's avatar
Uwe Schulzweida committed
583
      timevalue = encode_timeval(taxis->vdate_lb, taxis->vtime_lb, &streamptr->tsteps[0].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
584
585
586
      start[0] = tsID; count[0] = 1; start[1] = 0; count[1] = 1;
      cdf_put_vara_double(fileID, ncvarid, start, count, &timevalue);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
587
      timevalue = encode_timeval(taxis->vdate_ub, taxis->vtime_ub, &streamptr->tsteps[0].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
      start[0] = tsID; count[0] = 1; start[1] = 1; count[1] = 1;
      cdf_put_vara_double(fileID, ncvarid, start, count, &timevalue);
    }
  /*
printf("fileID = %d %d %d %f\n", fileID, time_varid, index, timevalue);
  */
#endif
}


void cdfDefTimestep(int streamID, int tsID)
{
  int vlistID;

  vlistID = streamInqVlist(streamID);

  if ( vlistHasTime(vlistID) ) cdfDefTime(streamID);

  cdfDefTimeValue(streamID, tsID);
}


void cdfDefTime(int streamID)
{
#if  defined  (HAVE_LIBNETCDF)
  int fileID;
  int time_varid;
  int time_bndsid;
  int dims[2];
  int year, month, day, hour, minute;
  char unitstr[80];
  char calstr[80];
  size_t len;
  TAXIS *taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
622
623
624
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
625

Uwe Schulzweida's avatar
Uwe Schulzweida committed
626
  if ( streamptr->basetime.ncvarid != UNDEFID ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627
628
629

  fileID = streamInqFileID(streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
  if ( streamptr->ncmode == 0 ) streamptr->ncmode = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
631

Uwe Schulzweida's avatar
Uwe Schulzweida committed
632
  if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633

Uwe Schulzweida's avatar
Uwe Schulzweida committed
634
  cdf_def_dim(fileID, "time", NC_UNLIMITED, &streamptr->basetime.ncdimid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
635

Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
  dims[0] = streamptr->basetime.ncdimid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
637
638
  cdf_def_var(fileID, "time", NC_DOUBLE, 1, dims, &time_varid);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
639
  streamptr->basetime.ncvarid = time_varid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640

Uwe Schulzweida's avatar
Uwe Schulzweida committed
641
  taxis = &streamptr->tsteps[0].taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
643
644
645
646
647
648
649

  if ( taxis->has_bounds )
    {
      /* fprintf(stderr, "time has bounds\n"); */

      cdf_def_dim(fileID, "tbnds", 2, &dims[1]);
      cdf_def_var(fileID, "time_bnds", NC_DOUBLE, 2, dims, &time_bndsid);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
650
      streamptr->basetime.ncvarboundsid = time_bndsid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
652
653
654
655

      cdf_put_att_text(fileID, time_varid, "bounds", 9, "time_bnds");
    }

  unitstr[0] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
656
  if ( streamptr->tsteps[0].taxis.type == TAXIS_ABSOLUTE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
657
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
658
      if ( streamptr->tsteps[0].taxis.unit == TUNIT_MONTH )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
	sprintf(unitstr, "month as %s", "%Y%m.%f");
      else
	sprintf(unitstr, "day as %s", "%Y%m%d.%f");
    }
  else
    {
      int rdate, rtime;
      int timeunit;

      timeunit = taxis->unit;
      rdate    = taxis->rdate;
      rtime    = taxis->rtime;
      if ( rdate == -1 )
	{
	  rdate  = taxis->vdate;
	  rtime  = taxis->vtime;
	}
      year   =  rdate / 10000;
      month  = (rdate - year*10000) / 100;
      day    =  rdate - year*10000 - month*100;
      hour   =  rtime / 100;
      minute =  rtime - hour*100;
      sprintf(unitstr, "%s since %d-%02d-%02d %02d:%02d",
	      tunitNamePtr(timeunit), year, month, day, hour, minute);
    }

  len = strlen(unitstr);
  if ( len )
    cdf_put_att_text(fileID, time_varid, "units", len, unitstr);

  if ( taxis->has_bounds )
    if ( len )
      cdf_put_att_text(fileID, time_bndsid, "units", len, unitstr);

  if ( taxis->calendar != -1 )
    {
      calstr[0] = 0;

      if      ( taxis->calendar == CALENDAR_NONE )      strcpy(calstr, "none");
      else if ( taxis->calendar == CALENDAR_360DAYS )   strcpy(calstr, "360_day");
      else if ( taxis->calendar == CALENDAR_365DAYS )   strcpy(calstr, "365_day");
      else if ( taxis->calendar == CALENDAR_366DAYS )   strcpy(calstr, "366_day");
      else if ( taxis->calendar == CALENDAR_PROLEPTIC ) strcpy(calstr, "proleptic_gregorian");

      len = strlen(calstr);
      if ( len )
	cdf_put_att_text(fileID, time_varid, "calendar", len, calstr);

      if ( taxis->has_bounds )
	if ( len )
	  cdf_put_att_text(fileID, time_bndsid, "calendar", len, calstr);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
  if ( streamptr->ncmode == 2 ) cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#endif
}


void cdfDefComplex(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  char axisname[] = "complex";
  int index;
  int dimID = UNDEFID;
  int gridID0, gridtype0, gridindex;
  int ngrids;
  int fileID;
  int dimlen;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
729
730
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
731
732
733
734
735
736
737
738

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
      if ( streamptr->xdimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
741
742
743
744
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_SPECTRAL )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
	      dimID = streamptr->xdimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
746
747
748
749
750
751
752
753
754
	      break;
	    }
	}
    }

  if ( dimID == UNDEFID )
    {
      dimlen = 2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
755
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757
758
759

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
761
762
763
    }

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
764
  streamptr->xdimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782

#endif
}

void cdfDefSpc(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  /*
  char longname[] = "Spherical harmonic coefficient";
  */
  char axisname[5] = "spcX";
  int index, iz = 0;
  int gridID0, gridtype0, gridindex;
  int dimID = UNDEFID;
  int ngrids;
  int fileID;
  int dimlen, dimlen0;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784
785
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787
788
789
790
791
792
793
794
795

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  dimlen = gridInqSize(gridID)/2;

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
      if ( streamptr->ydimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
797
798
799
800
801
802
803
804
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_SPECTRAL )
	    {
	      dimlen0 = gridInqSize(gridID0)/2;
	      if ( dimlen == dimlen0 )
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
805
		  dimID = streamptr->ydimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
806
807
808
809
810
811
812
813
814
815
816
817
818
		  break;
		}
	      else
		iz++;	
	    }
	}
    }

  if ( dimID == UNDEFID )
    {
      if ( iz == 0 ) axisname[3] = '\0';
      else           sprintf(&axisname[3], "%1d", iz+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
819
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
821
822
823

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
824
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
826
827
    }

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
  streamptr->ydimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848

#endif
}

void cdfDefTrajLon(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefTrajLon";
  char units[256];
  char longname[256];
  char stdname[256];
  char axisname[256];
  int gridtype, gridindex;
  int dimID = UNDEFID;
  int fileID;
  int dimlen;
  size_t len;
  int ncvarid;
  int vlistID;
  int xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
849
850
851
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
852

853
  if ( gridInqPrec(gridID) == DATATYPE_FLT32 ) xtype = NC_FLOAT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
854
855
856
857
858
859
860
861
862

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  gridtype = gridInqType(gridID);
  dimlen = gridInqXsize(gridID);
  if ( dimlen != 1 ) Error(func, "Xsize isn't 1 for %s grid!", gridNamePtr(gridtype));

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
  ncvarid = streamptr->xdimID[gridindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
864
865
866
867
868
869
870
871

  gridInqXname(gridID, axisname);
  gridInqXlongname(gridID, longname);
  gridInqXstdname(gridID, stdname);
  gridInqXunits(gridID, units);

  if ( ncvarid == UNDEFID )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
      dimID = streamptr->basetime.ncvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
873

Uwe Schulzweida's avatar
Uwe Schulzweida committed
874
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
875
876
877
878
879
880
881
882
883
884

      cdf_def_var(fileID, axisname, (nc_type) xtype, 1, &dimID, &ncvarid);
      if ( (len = strlen(longname)) )
	cdf_put_att_text(fileID, ncvarid, "long_name", len, longname);
      if ( (len = strlen(units)) )
	cdf_put_att_text(fileID, ncvarid, "units", len, units);
      if ( (len = strlen(stdname)) )
	cdf_put_att_text(fileID, ncvarid, "standard_name", len, stdname);

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
885
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
887
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
  streamptr->xdimID[gridindex] = ncvarid; /* var ID for trajectory !!! */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908

#endif
}

void cdfDefTrajLat(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefTrajLat";
  char units[] = "degrees_north";
  char longname[] = "latitude";
  char stdname[] = "latitude";
  char axisname[] = "tlat";
  int gridtype, gridindex;
  int dimID = UNDEFID;
  int fileID;
  int dimlen;
  size_t len;
  int ncvarid;
  int vlistID;
  int xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
910
911
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
912

913
  if ( gridInqPrec(gridID) == DATATYPE_FLT32 ) xtype = NC_FLOAT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914
915
916
917
918
919
920
921
922

  vlistID = streamInqVlist(streamID);
  fileID = streamInqFileID(streamID);

  gridtype = gridInqType(gridID);
  dimlen = gridInqYsize(gridID);
  if ( dimlen != 1 ) Error(func, "Ysize isn't 1 for %s grid!", gridNamePtr(gridtype));

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
923
  ncvarid = streamptr->ydimID[gridindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
924
925
926
927
928
929
930
931

  gridInqYname(gridID, axisname);
  gridInqYlongname(gridID, longname);
  gridInqYstdname(gridID, stdname);
  gridInqYunits(gridID, units);

  if ( ncvarid == UNDEFID )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
      dimID = streamptr->basetime.ncvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
933

Uwe Schulzweida's avatar
Uwe Schulzweida committed
934
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
935
936
937
938
939
940
941
942
943
944

      cdf_def_var(fileID, axisname, (nc_type) xtype, 1, &dimID, &ncvarid);
      if ( (len = strlen(longname)) )
	cdf_put_att_text(fileID, ncvarid, "long_name", len, longname);
      if ( (len = strlen(units)) )
	cdf_put_att_text(fileID, ncvarid, "units", len, units);
      if ( (len = strlen(stdname)) )
	cdf_put_att_text(fileID, ncvarid, "standard_name", len, stdname);

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
945
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
946
947
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
948
  streamptr->ydimID[gridindex] = ncvarid; /* var ID for trajectory !!! */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973

#endif
}


void cdfDefXaxis(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefXaxis";
  char units[256];
  char longname[256];
  char stdname[256];
  char axisname[256];
  int index, index2, iz = 0;
  int gridID0, gridtype0, gridindex;
  int dimID = UNDEFID;
  int dimIDs[2];
  int ngrids;
  int fileID;
  int dimlen, dimlen0;
  size_t len;
  int ncvarid = UNDEFID, ncbvarid = UNDEFID;
  int nvertex = 2, nvdimID = -1;
  int vlistID;
  int xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
974
975
976
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977

978
  if ( gridInqPrec(gridID) == DATATYPE_FLT32 ) xtype = NC_FLOAT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  dimlen = gridInqXsize(gridID);
  gridindex = vlistGridIndex(vlistID, gridID);

  gridInqXname(gridID, axisname);
  gridInqXlongname(gridID, longname);
  gridInqXstdname(gridID, stdname);
  gridInqXunits(gridID, units);

  if ( axisname[0] == 0 ) Error(func, "axis name undefined!\n");

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
997
      if ( streamptr->xdimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
998
999
1000
1001
1002
1003
1004
1005
1006
1007
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_GAUSSIAN    ||
	       gridtype0 == GRID_LONLAT      ||
	       gridtype0 == GRID_CURVILINEAR ||
	       gridtype0 == GRID_GENERIC )
	    {
	      dimlen0 = gridInqXsize(gridID0);
	      if ( dimlen == dimlen0 )
1008
1009
		if ( DBL_IS_EQUAL(gridInqXval(gridID0, 0), gridInqXval(gridID, 0)) &&
		     DBL_IS_EQUAL(gridInqXval(gridID0, dimlen-1), gridInqXval(gridID, dimlen-1)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1010
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1011
		    dimID = streamptr->xdimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1012
1013
1014
		    break;
		  }
	      for ( index2 = 0; index2 < index; index2++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1015
		if ( streamptr->xdimID[index] == streamptr->xdimID[index2] )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
		  break;
	      if ( index2 == index ) iz++;
	    }
	}
    }

  if ( dimID == UNDEFID )
    {
      if ( iz ) sprintf(&axisname[strlen(axisname)], "_%d", iz+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1026
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      if ( gridInqXboundsPtr(gridID) || gridInqYboundsPtr(gridID) )
	cdf_def_dim(fileID, "nv", nvertex, &nvdimID);

      if ( gridInqXvalsPtr(gridID) )
	{
	  cdf_def_var(fileID, axisname, (nc_type) xtype, 1, &dimID, &ncvarid);
	  if ( (len = strlen(longname)) )
	    cdf_put_att_text(fileID, ncvarid, "long_name", len, longname);
	  if ( (len = strlen(units)) )
	    cdf_put_att_text(fileID, ncvarid, "units", len, units);
	  if ( (len = strlen(stdname)) )
	    cdf_put_att_text(fileID, ncvarid, "standard_name", len, stdname);

	  if ( gridInqXboundsPtr(gridID) )
	    {
	      strcat(axisname, "_bounds");
	      dimIDs[0] = dimID;
	      dimIDs[1] = nvdimID;
	      cdf_def_var(fileID, axisname, (nc_type) xtype, 2, dimIDs, &ncbvarid);
	      cdf_put_att_text(fileID, ncvarid, "bounds", strlen(axisname), axisname);
	    }

	  /*
	  if ( gridIsRotated(gridID) )
	    {
	      double north_pole = gridInqXpole(gridID);
	      cdf_put_att_double(fileID, ncvarid, "north_pole", NC_DOUBLE, 1L, &north_pole);
	    }
	  */
	}

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1062
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063
1064
1065
1066
1067

      if ( ncvarid  != UNDEFID ) cdf_put_var_double(fileID, ncvarid, gridInqXvalsPtr(gridID));
      if ( ncbvarid != UNDEFID ) cdf_put_var_double(fileID, ncbvarid, gridInqXboundsPtr(gridID));
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1068
  streamptr->xdimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092

#endif
}

void cdfDefYaxis(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefYaxis";
  char units[256];
  char longname[256];
  char stdname[256];
  char axisname[256];
  int index, index2, iz = 0;
  int gridID0, gridtype0, gridindex;
  int dimID = UNDEFID;
  int dimIDs[2];
  int ngrids;
  int fileID;
  int dimlen, dimlen0;
  size_t len;
  int ncvarid = UNDEFID, ncbvarid = UNDEFID;
  int nvdimID = -1;
  int vlistID;
  int xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1093
1094
1095
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1096

1097
  if ( gridInqPrec(gridID) == DATATYPE_FLT32 ) xtype = NC_FLOAT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  dimlen = gridInqYsize(gridID);
  gridindex = vlistGridIndex(vlistID, gridID);

  gridInqYname(gridID, axisname);
  gridInqYlongname(gridID, longname);
  gridInqYstdname(gridID, stdname);
  gridInqYunits(gridID, units);

  if ( axisname[0] == 0 ) Error(func, "axis name undefined!\n");

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116
      if ( streamptr->ydimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_GAUSSIAN    ||
	       gridtype0 == GRID_LONLAT      ||
	       gridtype0 == GRID_CURVILINEAR ||
	       gridtype0 == GRID_GENERIC )
	    {
	      dimlen0 = gridInqYsize(gridID0);
	      if ( dimlen == dimlen0 )
1127
1128
		if ( DBL_IS_EQUAL(gridInqYval(gridID0, 0), gridInqYval(gridID, 0)) &&
		     DBL_IS_EQUAL(gridInqYval(gridID0, dimlen-1), gridInqYval(gridID, dimlen-1)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1129
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1130
		    dimID = streamptr->ydimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1131
1132
1133
		    break;
		  }
	      for ( index2 = 0; index2 < index; index2++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1134
		if ( streamptr->ydimID[index] == streamptr->ydimID[index2] )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
		  break;
	      if ( index2 == index ) iz++;
	    }
	}
    }

  if ( dimID == UNDEFID )
    {
      if ( iz ) sprintf(&axisname[strlen(axisname)], "_%d", iz+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1145
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      if ( gridInqXboundsPtr(gridID) || gridInqYboundsPtr(gridID) )
	cdf_inq_dimid(fileID, "nv", &nvdimID);

      if ( gridInqYvalsPtr(gridID) )
	{
	  cdf_def_var(fileID, axisname, (nc_type) xtype, 1, &dimID, &ncvarid);
	  if ( (len = strlen(longname)) )
	    cdf_put_att_text(fileID, ncvarid, "long_name", len, longname);
	  if ( (len = strlen(units)) )
	    cdf_put_att_text(fileID, ncvarid, "units", len, units);
	  if ( (len = strlen(stdname)) )
	    cdf_put_att_text(fileID, ncvarid, "standard_name", len, stdname);

	  if ( gridInqYboundsPtr(gridID) )
	    {
	      strcat(axisname, "_bounds");
	      dimIDs[0] = dimID;
	      dimIDs[1] = nvdimID;
	      cdf_def_var(fileID, axisname, (nc_type) xtype, 2, dimIDs, &ncbvarid);
	      cdf_put_att_text(fileID, ncvarid, "bounds", strlen(axisname), axisname);
	    }
	  /*
	  if ( gridIsRotated(gridID) )
	    {
	      double north_pole = gridInqYpole(gridID);
	      cdf_put_att_double(fileID, ncvarid, "north_pole", NC_DOUBLE, 1L, &north_pole);
	    }
	  */
	}

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1180
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181
1182
1183
1184
1185

      if ( ncvarid  != UNDEFID ) cdf_put_var_double(fileID, ncvarid, gridInqYvalsPtr(gridID));
      if ( ncbvarid != UNDEFID ) cdf_put_var_double(fileID, ncbvarid, gridInqYboundsPtr(gridID));
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1186
  streamptr->ydimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218

#endif
}


void cdfDefLonLat2D(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  char xunits[256];
  char xlongname[256];
  char xstdname[256];
  char yunits[256];
  char ylongname[256];
  char ystdname[256];
  char xaxisname[256];
  char yaxisname[256];
  char xdimname[4] = "x";
  char ydimname[4] = "y";
  int index, index2, iz = 0;
  int gridID0, gridtype0, gridindex;
  int xdimID = UNDEFID;
  int ydimID = UNDEFID;
  int dimIDs[3];
  int ngrids;
  int fileID;
  int xdimlen, ydimlen, dimlen0;
  size_t len;
  int ncxvarid = UNDEFID, ncyvarid = UNDEFID;
  int ncbxvarid = UNDEFID, ncbyvarid = UNDEFID, ncavarid = UNDEFID;
  int nvertex = 4, nvdimID = -1;
  int vlistID;
  int xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1219
1220
1221
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1222

1223
  if ( gridInqPrec(gridID) == DATATYPE_FLT32 ) xtype = NC_FLOAT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  xdimlen = gridInqXsize(gridID);
  ydimlen = gridInqYsize(gridID);
  gridindex = vlistGridIndex(vlistID, gridID);

  gridInqXname(gridID, xaxisname);
  gridInqXlongname(gridID, xlongname);
  gridInqXstdname(gridID, xstdname);
  gridInqXunits(gridID, xunits);
  gridInqYname(gridID, yaxisname);
  gridInqYlongname(gridID, ylongname);
  gridInqYstdname(gridID, ystdname);
  gridInqYunits(gridID, yunits);

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1245
      if ( streamptr->xdimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_GAUSSIAN    ||
	       gridtype0 == GRID_LONLAT      ||
	       gridtype0 == GRID_CURVILINEAR ||
	       gridtype0 == GRID_GENERIC )
	    {
	      dimlen0 = gridInqXsize(gridID0);
	      if ( xdimlen == dimlen0 )
1256
1257
		if ( DBL_IS_EQUAL(gridInqXval(gridID0, 0), gridInqXval(gridID, 0)) &&
		     DBL_IS_EQUAL(gridInqXval(gridID0, xdimlen-1), gridInqXval(gridID, xdimlen-1)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1258
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1259
		    xdimID = streamptr->xdimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1260
1261
1262
		    break;
		  }
	      for ( index2 = 0; index2 < index; index2++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1263
		if ( streamptr->xdimID[index] == streamptr->xdimID[index2] )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
		  break;
	      if ( index2 == index ) iz++;
	    }
	}
    }

  if ( xdimID == UNDEFID )
    {
      if ( iz )
	{
	  sprintf(&xaxisname[strlen(xaxisname)], "_%d", iz+1);
	  sprintf(&yaxisname[strlen(yaxisname)], "_%d", iz+1);
	  sprintf(&xdimname[1], "_%d", iz+1);
	  sprintf(&ydimname[1], "_%d", iz+1);
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1280
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300

      cdf_def_dim(fileID, xdimname, xdimlen, &xdimID);
      cdf_def_dim(fileID, ydimname, ydimlen, &ydimID);

      if ( gridInqXboundsPtr(gridID) || gridInqYboundsPtr(gridID) )
	cdf_def_dim(fileID, "nv", nvertex, &nvdimID);

      dimIDs[0] = ydimID;
      dimIDs[1] = xdimID;

      if ( gridInqXvalsPtr(gridID) )
	{
	  cdf_def_var(fileID, xaxisname, (nc_type) xtype, 2, dimIDs, &ncxvarid);
	  if ( (len = strlen(xlongname)) )
	    cdf_put_att_text(fileID, ncxvarid, "long_name", len, xlongname);
	  if ( (len = strlen(xunits)) )
	    cdf_put_att_text(fileID, ncxvarid, "units", len, xunits);
	  if ( (len = strlen(xstdname)) )
	    cdf_put_att_text(fileID, ncxvarid, "standard_name", len, xstdname);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1301
	  streamptr->ncxvarID[gridindex] = ncxvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323

	  if ( gridInqXboundsPtr(gridID) )
	    {
	      strcat(xaxisname, "_bounds");
	      dimIDs[0] = ydimID;
	      dimIDs[1] = xdimID;
	      dimIDs[2] = nvdimID;
	      cdf_def_var(fileID, xaxisname, (nc_type) xtype, 3, dimIDs, &ncbxvarid);
	      cdf_put_att_text(fileID, ncxvarid, "bounds", strlen(xaxisname), xaxisname);
	    }
	}

      if ( gridInqYvalsPtr(gridID) )
	{
	  cdf_def_var(fileID, yaxisname, (nc_type) xtype, 2, dimIDs, &ncyvarid);
	  if ( (len = strlen(ylongname)) )
	    cdf_put_att_text(fileID, ncyvarid, "long_name", len, ylongname);
	  if ( (len = strlen(yunits)) )
	    cdf_put_att_text(fileID, ncyvarid, "units", len, yunits);
	  if ( (len = strlen(ystdname)) )
	    cdf_put_att_text(fileID, ncyvarid, "standard_name", len, ystdname);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1324
	  streamptr->ncyvarID[gridindex] = ncyvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348

	  if ( gridInqYboundsPtr(gridID) )
	    {
	      strcat(yaxisname, "_bounds");
	      dimIDs[0] = ydimID;
	      dimIDs[1] = xdimID;
	      dimIDs[2] = nvdimID;
	      cdf_def_var(fileID, yaxisname, (nc_type) xtype, 3, dimIDs, &ncbyvarid);
	      cdf_put_att_text(fileID, ncyvarid, "bounds", strlen(yaxisname), yaxisname);
	    }
	}

      if ( gridInqAreaPtr(gridID) )
	{
	  char yaxisname[] = "cell_area";
	  char units[] = "m2";
	  char longname[] = "area of grid cell";
	  char stdname[] = "area";

	  cdf_def_var(fileID, yaxisname, (nc_type) xtype, 2, dimIDs, &ncavarid);
	  cdf_put_att_text(fileID, ncavarid, "long_name", strlen(longname), longname);
	  cdf_put_att_text(fileID, ncavarid, "standard_name", strlen(stdname), stdname);
	  cdf_put_att_text(fileID, ncavarid, "units", strlen(units), units);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1349
	  streamptr->ncavarID[gridindex] = ncavarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1350
1351
1352
	}

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1353
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1354
1355
1356
1357
1358
1359
1360
1361

      if ( ncxvarid  != UNDEFID ) cdf_put_var_double(fileID, ncxvarid,  gridInqXvalsPtr(gridID));
      if ( ncbxvarid != UNDEFID ) cdf_put_var_double(fileID, ncbxvarid, gridInqXboundsPtr(gridID));
      if ( ncyvarid  != UNDEFID ) cdf_put_var_double(fileID, ncyvarid,  gridInqYvalsPtr(gridID));
      if ( ncbyvarid != UNDEFID ) cdf_put_var_double(fileID, ncbyvarid, gridInqYboundsPtr(gridID));
      if ( ncavarid  != UNDEFID ) cdf_put_var_double(fileID, ncavarid,  gridInqAreaPtr(gridID));
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1362
1363
  streamptr->xdimID[gridindex] = xdimID;
  streamptr->ydimID[gridindex] = ydimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381

#endif
}


void cdfDefRgrid(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefRgrid";
  char axisname[7] = "rgridX";
  int index, iz = 0;
  int gridID0, gridtype0, gridindex;
  int dimID = UNDEFID;
  int ngrids;
  int fileID;
  int dimlen, dimlen0;
  int vlistID;
  int lwarn = TRUE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1382
1383
1384
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  dimlen = gridInqSize(gridID);

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1395
      if ( streamptr->xdimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1396
1397
1398
1399
1400
1401
1402
1403
1404
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_GAUSSIAN_REDUCED )
	    {
	      dimlen0 = gridInqSize(gridID0);

	      if ( dimlen == dimlen0 )
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1405
		  dimID = streamptr->xdimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
		  break;
		}
	      else
		iz++;	
	    }
	}
    }

  if ( dimID == UNDEFID )
    {
      if ( lwarn )
	{
	  Warning(func, "Writing a netCDF file with data on a gaussian reduced grid.");
	  Warning(func, "The reading of this grid is unsupported!");
	  lwarn = FALSE;
	}

      if ( iz == 0 ) axisname[5] = '\0';
      else           sprintf(&axisname[5], "%1d", iz+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1426
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1427
1428
1429
1430

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1431
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1432
1433
1434
    }

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1435
  streamptr->xdimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451

#endif
}


void cdfDefGdim(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  char axisname[7] = "gsizeX";
  int index, iz = 0;
  int gridID0, gridtype0, gridindex;
  int dimID = UNDEFID;
  int ngrids;
  int fileID;
  int dimlen, dimlen0;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1452
1453
1454
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  dimlen = gridInqSize(gridID);

  if ( gridInqYsize(gridID) == 0 )
    for ( index = 0; index < ngrids; index++ )
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1466
	if ( streamptr->xdimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1467
1468
1469
1470
1471
1472
1473
1474
	  {
	    gridID0 = vlistGrid(vlistID, index);
	    gridtype0 = gridInqType(gridID0);
	    if ( gridtype0 == GRID_GENERIC )
	      {
		dimlen0 = gridInqSize(gridID0);
		if ( dimlen == dimlen0 )
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1475
		    dimID = streamptr->xdimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
		    break;
		  }
		else
		  iz++;	
	      }
	  }
      }

  if ( gridInqXsize(gridID) == 0 )
    for ( index = 0; index < ngrids; index++ )
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1487
	if ( streamptr->ydimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1488
1489
1490
1491
1492
1493
1494
1495
	  {
	    gridID0 = vlistGrid(vlistID, index);
	    gridtype0 = gridInqType(gridID0);
	    if ( gridtype0 == GRID_GENERIC )
	      {
		dimlen0 = gridInqSize(gridID0);
		if ( dimlen == dimlen0 )
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1496
		    dimID = streamptr->ydimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
		    break;
		  }
		else
		  iz++;	
	      }
	  }
      }

  if ( dimID == UNDEFID )
    {
      if ( iz == 0 ) axisname[5] = '\0';
      else           sprintf(&axisname[5], "%1d", iz+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1510
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1511
1512
1513
1514

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1515
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1516
1517
1518
    }

  gridindex = vlistGridIndex(vlistID, gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1519
  streamptr->xdimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539

#endif
}


void cdfDefCell(int streamID, int gridID)
{
#if  defined  (HAVE_LIBNETCDF)
  char axisname[6] = "cellX";
  int index, iz = 0;
  int gridID0, gridtype0, gridindex;
  int dimID = UNDEFID;
  int ngrids;
  int fileID;
  int dimlen, dimlen0;
  int ncxvarid = UNDEFID, ncyvarid = UNDEFID;
  int ncbxvarid = UNDEFID, ncbyvarid = UNDEFID, ncavarid = UNDEFID;
  int nvertex, nvdimID = -1;
  int vlistID;
  int xtype = NC_DOUBLE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1540
1541
1542
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1543

1544
  if ( gridInqPrec(gridID) == DATATYPE_FLT32 ) xtype = NC_FLOAT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);

  ngrids = vlistNgrids(vlistID);

  dimlen = gridInqSize(gridID);
  gridindex = vlistGridIndex(vlistID, gridID);

  for ( index = 0; index < ngrids; index++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1556
      if ( streamptr->xdimID[index] != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1557
1558
1559
1560
1561
1562
1563
1564
	{
	  gridID0 = vlistGrid(vlistID, index);
	  gridtype0 = gridInqType(gridID0);
	  if ( gridtype0 == GRID_CELL )
	    {
	      dimlen0 = gridInqSize(gridID0);
	      if ( dimlen == dimlen0 )
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1565
		  dimID = streamptr->xdimID[index];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
		  break;
		}
	      else
		iz++;	
	    }
	}
    }

  if ( dimID == UNDEFID )
    {
      if ( iz == 0 ) axisname[4] = '\0';
      else           sprintf(&axisname[4], "%1d", iz+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1579
      if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595

      cdf_def_dim(fileID, axisname, dimlen, &dimID);

      nvertex = gridInqNvertex(gridID);
      if ( nvertex > 0 ) cdf_def_dim(fileID, "nv", nvertex, &nvdimID);
	
      if ( gridInqXvalsPtr(gridID) )
	{
	  char xaxisname[] = "lon";
	  char units[] = "degrees_east";
	  char longname[] = "longitude";

	  cdf_def_var(fileID, xaxisname, (nc_type) xtype, 1, &dimID, &ncxvarid);
	  cdf_put_att_text(fileID, ncxvarid, "long_name", strlen(longname), longname);
	  cdf_put_att_text(fileID, ncxvarid, "units", strlen(units), units);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1596
	  streamptr->ncxvarID[gridindex] = ncxvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618

	  if ( nvdimID != UNDEFID && gridInqXboundsPtr(gridID) )
	    {
	      char bxaxisname[] = "lon_vertices";
	      int dimIDs[2];
	      dimIDs[0] = dimID;
	      dimIDs[1] = nvdimID;
	      cdf_def_var(fileID, bxaxisname, (nc_type) xtype, 2, dimIDs, &ncbxvarid);
	      cdf_put_att_text(fileID, ncxvarid, "bounds", strlen(bxaxisname), bxaxisname);
	    }
	}

      if ( gridInqYvalsPtr(gridID) )
	{
	  char yaxisname[] = "lat";
	  char units[] = "degrees_north";
	  char longname[] = "latitude";

	  cdf_def_var(fileID, yaxisname, (nc_type) xtype, 1, &dimID, &ncyvarid);
	  cdf_put_att_text(fileID, ncyvarid, "long_name", strlen(longname), longname);
	  cdf_put_att_text(fileID, ncyvarid, "units", strlen(units), units);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1619
	  streamptr->ncyvarID[gridindex] = ncyvarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643

	  if ( nvdimID != UNDEFID && gridInqYboundsPtr(gridID) )
	    {
	      char byaxisname[] = "lat_vertices";
	      int dimIDs[2];
	      dimIDs[0] = dimID;
	      dimIDs[1] = nvdimID;
	      cdf_def_var(fileID, byaxisname, (nc_type) xtype, 2, dimIDs, &ncbyvarid);
	      cdf_put_att_text(fileID, ncyvarid, "bounds", strlen(byaxisname), byaxisname);
	    }
	}

      if ( gridInqAreaPtr(gridID) )
	{
	  char yaxisname[] = "cell_area";
	  char units[] = "m2";
	  char longname[] = "area of grid cell";
	  char stdname[] = "area";

	  cdf_def_var(fileID, yaxisname, (nc_type) xtype, 1, &dimID, &ncavarid);
	  cdf_put_att_text(fileID, ncavarid, "long_name", strlen(longname), longname);
	  cdf_put_att_text(fileID, ncavarid, "standard_name", strlen(stdname), stdname);
	  cdf_put_att_text(fileID, ncavarid, "units", strlen(units), units);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1644
	  streamptr->ncavarID[gridindex] = ncavarid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1645
1646
1647
	}

      cdf_enddef(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1648
      streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1649
1650
1651
1652
1653
1654
1655
1656

      if ( ncxvarid  != UNDEFID ) cdf_put_var_double(fileID, ncxvarid,  gridInqXvalsPtr(gridID));
      if ( ncbxvarid != UNDEFID ) cdf_put_var_double(fileID, ncbxvarid, gridInqXboundsPtr(gridID));
      if ( ncyvarid  != UNDEFID ) cdf_put_var_double(fileID, ncyvarid,  gridInqYvalsPtr(gridID));
      if ( ncbyvarid != UNDEFID ) cdf_put_var_double(fileID, ncbyvarid, gridInqYboundsPtr(gridID));
      if ( ncavarid  != UNDEFID ) cdf_put_var_double(fileID, ncavarid,  gridInqAreaPtr(gridID));
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1657
  streamptr->xdimID[gridindex] = dimID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667

#endif
}


void cdfDefVCT(int streamID, int zaxisID)
{
#if  defined  (HAVE_LIBNETCDF)
  static char func[] = "cdfDefVCT";
  int type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1668
1669
1670
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689

  type = zaxisInqType(zaxisID);

  if ( type == ZAXIS_HYBRID )
    {
      int i;
      int fileID;
      int ilev = zaxisInqVctSize(zaxisID)/2;
      int mlev = ilev - 1;
      size_t start;
      size_t count = 1;      
      int ncdimid, ncdimid2;
      int ncvarid, ncvarid2;
      int hyaiid, hybiid, hyamid, hybmid;
      int *ilevels = NULL;
      const double *vctptr = zaxisInqVctPtr(zaxisID);
      double mval;
      char tmpname[256];

Uwe Schulzweida's avatar
Uwe Schulzweida committed