taxis.c 41 KB
Newer Older
1
#ifdef HAVE_CONFIG_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2
3
4
#  include "config.h"
#endif

5
#include <stddef.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7

#include "cdi.h"
8
9
#include "calendar.h"
#include "dmemory.h"
10
#include "error.h"
11
#include "taxis.h"
12
#include "cdi_cksum.h"
13
#include "cdi_int.h"
Deike Kleberg's avatar
Deike Kleberg committed
14
#include "namespace.h"
15
#include "serialize.h"
16
#include "resource_handle.h"
17
#include "resource_unpack.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18

19

20
21
static int DefaultTimeType = TAXIS_ABSOLUTE;
static int DefaultTimeUnit = TUNIT_HOUR;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
23


Thomas Jahns's avatar
Thomas Jahns committed
24
static const char *Timeunits[] = {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
  "undefined",
  "seconds",
  "minutes",
Deike Kleberg's avatar
Deike Kleberg committed
28
  "quarters",
29
30
  "30minutes",
  "hours",
31
32
33
  "3hours",
  "6hours",
  "12hours",
34
35
36
  "days",
  "months",
  "years",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
};


40
41
static int    taxisCompareP    ( void * taxisptr1, void * taxisptr2 );
static void   taxisDestroyP    ( void * taxisptr );
Thomas Jahns's avatar
Thomas Jahns committed
42
static void   taxisPrintKernel(taxis_t *taxisptr, FILE * fp);
43
static int    taxisGetPackSize ( void * taxisptr, void *context );
44
static void   taxisPack        ( void * taxisptr, void *buf, int size,
45
				 int *position, void *context );
46
static int    taxisTxCode      ( void );
47

48
49
50
const resOps taxisOps = {
  taxisCompareP,
  taxisDestroyP,
Thomas Jahns's avatar
Thomas Jahns committed
51
  (void (*)(void *, FILE *))taxisPrintKernel,
52
53
54
  taxisGetPackSize,
  taxisPack,
  taxisTxCode
55
};
56

57
#define container_of(ptr, type, member) \
Thomas Jahns's avatar
Thomas Jahns committed
58
  ((type *)(void*)((unsigned char *)ptr - offsetof(type,member)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59

60
61
62
63
64
65
66
67
68
69
struct refcount_string
{
  int ref_count;
  char string[];
};

static char *
new_refcount_string(size_t len)
{
  struct refcount_string *container
70
    = (struct refcount_string *) Malloc(sizeof (*container) + len + 1);
71
72
73
74
75
76
77
78
79
80
81
82
  container->ref_count = 1;
  return container->string;
}

static void
delete_refcount_string(void *p)
{
  if (p)
    {
      struct refcount_string *container
        = container_of(p, struct refcount_string, string);
      if (!--(container->ref_count))
83
        Free(container);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    }
}

static char *
dup_refcount_string(char *p)
{
  if (p)
    {
      struct refcount_string *container
        = container_of(p, struct refcount_string, string);
      ++(container->ref_count);
    }
  return p;
}


#undef container_of
101
102
103
104

static int  TAXIS_Debug = 0;   /* If set to 1, debugging */


105
const char *tunitNamePtr(int unitID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
{
107
  int size = sizeof(Timeunits)/sizeof(*Timeunits);
108
  return (unitID > 0 && unitID < size) ? Timeunits[unitID] : Timeunits[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
}

111
#if 0
112
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
void taxis_defaults(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
{
115
  char *timeunit = getenv("TIMEUNIT");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
117
  if ( timeunit )
    {
Deike Kleberg's avatar
Deike Kleberg committed
118
      if ( strcmp(timeunit, "minutes") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
	DefaultTimeUnit = TUNIT_MINUTE;
Deike Kleberg's avatar
Deike Kleberg committed
120
      else if ( strcmp(timeunit, "hours") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
	DefaultTimeUnit = TUNIT_HOUR;
122
123
124
125
126
127
      else if ( strcmp(timeunit, "3hours") == 0 )
	DefaultTimeUnit = TUNIT_3HOURS;
      else if ( strcmp(timeunit, "6hours") == 0 )
	DefaultTimeUnit = TUNIT_6HOURS;
      else if ( strcmp(timeunit, "12hours") == 0 )
	DefaultTimeUnit = TUNIT_12HOURS;
Deike Kleberg's avatar
Deike Kleberg committed
128
      else if ( strcmp(timeunit, "days") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
	DefaultTimeUnit = TUNIT_DAY;
Deike Kleberg's avatar
Deike Kleberg committed
130
      else if ( strcmp(timeunit, "months") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
	DefaultTimeUnit = TUNIT_MONTH;
Deike Kleberg's avatar
Deike Kleberg committed
132
      else if ( strcmp(timeunit, "years") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
	DefaultTimeUnit = TUNIT_YEAR;
      else
135
	Warning("Unsupported TIMEUNIT %s!", timeunit);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
}
138
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139

140
static
141
void taxisDefaultValue(taxis_t* taxisptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
{
143
  taxisptr->self        = CDI_UNDEFID;
144
  taxisptr->used        = false;
145
  taxisptr->datatype    = CDI_DATATYPE_FLT64;
146
147
148
149
150
  taxisptr->type        = DefaultTimeType;
  taxisptr->vdate       = 0;
  taxisptr->vtime       = 0;
  taxisptr->rdate       = CDI_UNDEFID;
  taxisptr->rtime       = 0;
151
152
  taxisptr->fdate       = CDI_UNDEFID;
  taxisptr->ftime       = 0;
153
154
155
  taxisptr->calendar    = cdiDefaultCalendar;
  taxisptr->unit        = DefaultTimeUnit;
  taxisptr->numavg      = 0;
156
157
  taxisptr->climatology = false;
  taxisptr->has_bounds  = false;
158
159
160
161
  taxisptr->vdate_lb    = 0;
  taxisptr->vtime_lb    = 0;
  taxisptr->vdate_ub    = 0;
  taxisptr->vtime_ub    = 0;
162
163
  taxisptr->fc_unit     = DefaultTimeUnit;
  taxisptr->fc_period   = 0;
164
165
  taxisptr->name        = NULL;
  taxisptr->longname    = NULL;
166
  taxisptr->units       = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168

169
170
static taxis_t *
taxisNewEntry(cdiResH resH)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
{
172
  taxis_t *taxisptr = (taxis_t*) Malloc(sizeof(taxis_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173

174
175
176
177
178
179
180
181
  taxisDefaultValue(taxisptr);
  if (resH == CDI_UNDEFID)
    taxisptr->self = reshPut(taxisptr, &taxisOps);
  else
    {
      taxisptr->self = resH;
      reshReplace(resH, taxisptr, &taxisOps);
    }
182

183
  return taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
}

186
static
187
void taxisInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
{
189
  static bool taxisInitialized = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190

191
  if ( taxisInitialized ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192

193
  taxisInitialized = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194

195
  char *env = getenv("TAXIS_DEBUG");
196
  if ( env ) TAXIS_Debug = atoi(env);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
198
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
/*
200
@Function  taxisCreate
201
@Title     Create a Time axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202

203
@Prototype int taxisCreate(int taxistype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
    @Item  taxistype  The type of the Time axis, one of the set of predefined CDI time axis types.
                      The valid CDI time axis types are @func{TAXIS_ABSOLUTE} and @func{TAXIS_RELATIVE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208

@Description
209
The function @func{taxisCreate} creates a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
211

@Result
212
@func{taxisCreate} returns an identifier to the Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
215
216
217
218
219
220
221
222
223
224
@Example
Here is an example using @func{taxisCreate} to create a relative T-axis
with a standard calendar.

@Source
#include "cdi.h"
   ...
int taxisID;
   ...
taxisID = taxisCreate(TAXIS_RELATIVE);
taxisDefCalendar(taxisID, CALENDAR_STANDARD);
225
taxisDefRdate(taxisID, 19850101);
226
taxisDefRtime(taxisID, 120000);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
@EndFunction
*/
231
int taxisCreate(int taxistype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
{
233
  if ( CDI_Debug ) Message("taxistype: %d", taxistype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234

235
236
  taxisInit ();

237
  taxis_t *taxisptr = taxisNewEntry(CDI_UNDEFID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
  taxisptr->type = taxistype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239

240
241
  int taxisID = taxisptr->self;

242
  if ( CDI_Debug ) Message("taxisID: %d", taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243

244
  return taxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
246
}

247
void taxisDestroyKernel(taxis_t *taxisptr)
248
{
249
250
  delete_refcount_string(taxisptr->name);
  delete_refcount_string(taxisptr->longname);
251
  delete_refcount_string(taxisptr->units);
252
253
}

254
255
256
257
258
259
/*
@Function  taxisDestroy
@Title     Destroy a Time axis

@Prototype void taxisDestroy(int taxisID)
@Parameter
260
    @Item  taxisID  Time axis ID, from a previous call to @func{taxisCreate}
261
262
263
264
265

@EndFunction
*/
void taxisDestroy(int taxisID)
{
266
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Thomas Jahns's avatar
Thomas Jahns committed
267
  reshRemove(taxisID, &taxisOps);
268
  taxisDestroyKernel(taxisptr);
269
  Free(taxisptr);
270
}
271

Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
273

void taxisDestroyP( void * taxisptr )
274
{
275
  taxisDestroyKernel((taxis_t *)taxisptr);
276
  Free(taxisptr);
277
278
279
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
281
int taxisDuplicate(int taxisID1)
{
282
  taxis_t *taxisptr1 = (taxis_t *)reshGetVal(taxisID1, &taxisOps);
283
  taxis_t *taxisptr2 = taxisNewEntry(CDI_UNDEFID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284

285
  int taxisID2 = taxisptr2->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286

287
  if ( CDI_Debug ) Message("taxisID2: %d", taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288

Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
  ptaxisCopy(taxisptr2, taxisptr1);
290
291

  return taxisID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
293
294
295
296
}


void taxisDefType(int taxisID, int type)
{
297
  taxis_t *taxisptr = (taxis_t *) reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298

299
  if ( taxisptr->type != type )
300
301
    {
      taxisptr->type = type;
302
303
304
305
306
      if ( taxisptr->units )
        {
          delete_refcount_string(taxisptr->units);
          taxisptr->units = NULL;
        }
307
308
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
310
311
312
313
314
}

/*
@Function  taxisDefVdate
@Title     Define the verification date

315
@Prototype void taxisDefVdate(int taxisID, int64_t vdate)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320
    @Item  vdate    Verification date (YYYYMMDD)

@Description
321
The function @func{taxisDefVdate} defines the verification date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
324

@EndFunction
*/
325
void taxisDefVdate(int taxisID, int64_t vdate)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
{
327
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328

329
330
331
332
333
  if (taxisptr->vdate != vdate)
    {
      taxisptr->vdate = vdate;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
334
335
336
337
338
339
340
341
}

/*
@Function  taxisDefVtime
@Title     Define the verification time

@Prototype void taxisDefVtime(int taxisID, int vtime)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
343
    @Item  vtime    Verification time (hhmmss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345

@Description
346
The function @func{taxisDefVtime} defines the verification time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
349
350
351

@EndFunction
*/
void taxisDefVtime(int taxisID, int vtime)
{
352
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

354
355
356
357
358
  if (taxisptr->vtime != vtime)
    {
      taxisptr->vtime = vtime;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
360
361
362
363
364
}

/*
@Function  taxisDefRdate
@Title     Define the reference date

365
@Prototype void taxisDefRdate(int taxisID, int64_t rdate)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369
370
    @Item  rdate    Reference date (YYYYMMDD)

@Description
371
The function @func{taxisDefRdate} defines the reference date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374

@EndFunction
*/
375
void taxisDefRdate(int taxisID, int64_t rdate)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
{
377
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378

379
380
381
  if (taxisptr->rdate != rdate)
    {
      taxisptr->rdate = rdate;
382

383
384
385
386
387
      if ( taxisptr->units )
        {
          delete_refcount_string(taxisptr->units);
          taxisptr->units = NULL;
        }
388
389
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
392
393
394
395
396
397
}

/*
@Function  taxisDefRtime
@Title     Define the reference time

@Prototype void taxisDefRtime(int taxisID, int rtime)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
398
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
399
    @Item  rtime    Reference time (hhmmss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401

@Description
402
The function @func{taxisDefRtime} defines the reference time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405
406
407

@EndFunction
*/
void taxisDefRtime(int taxisID, int rtime)
{
408
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409

410
411
412
  if (taxisptr->rtime != rtime)
    {
      taxisptr->rtime = rtime;
413
414
415
416
417
      if ( taxisptr->units )
        {
          delete_refcount_string(taxisptr->units);
          taxisptr->units = NULL;
        }
418
419
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
421
}

422
423
424
425
/*
@Function  taxisDefFdate
@Title     Define the forecast reference date

426
@Prototype void taxisDefFdate(int taxisID, int64_t fdate)
427
428
429
430
431
432
433
434
435
@Parameter
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
    @Item  fdate    Forecast reference date (YYYYMMDD)

@Description
The function @func{taxisDefFdate} defines the forecast reference date of a Time axis.

@EndFunction
*/
436
void taxisDefFdate(int taxisID, int64_t fdate)
437
{
438
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
439

440
441
442
443
444
  if (taxisptr->fdate != fdate)
    {
      taxisptr->fdate = fdate;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
}

/*
@Function  taxisDefFtime
@Title     Define the forecast reference time

@Prototype void taxisDefFtime(int taxisID, int ftime)
@Parameter
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
    @Item  ftime    Forecast reference time (hhmmss)

@Description
The function @func{taxisDefFtime} defines the forecast reference time of a Time axis.

@EndFunction
*/
void taxisDefFtime(int taxisID, int ftime)
{
463
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
464

465
466
467
468
469
  if (taxisptr->ftime != ftime)
    {
      taxisptr->ftime = ftime;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
470
471
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
472
473
474
475
476
477
/*
@Function  taxisDefCalendar
@Title     Define the calendar

@Prototype void taxisDefCalendar(int taxisID, int calendar)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
479
    @Item  calendar The type of the calendar, one of the set of predefined CDI calendar types.
480
                    The valid CDI calendar types are @func{CALENDAR_STANDARD}, @func{CALENDAR_PROLEPTIC},
481
                    @func{CALENDAR_360DAYS}, @func{CALENDAR_365DAYS} and @func{CALENDAR_366DAYS}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
483

@Description
484
The function @func{taxisDefCalendar} defines the calendar of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487
488
489

@EndFunction
*/
void taxisDefCalendar(int taxisID, int calendar)
{
490
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491

492
493
494
495
496
  if (taxisptr->calendar != calendar)
    {
      taxisptr->calendar = calendar;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
498
499
500
501
}


void taxisDefTunit(int taxisID, int unit)
{
502
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
503

504
505
506
  if (taxisptr->unit != unit)
    {
      taxisptr->unit = unit;
507
508
509
510
511
      if ( taxisptr->units )
        {
          delete_refcount_string(taxisptr->units);
          taxisptr->units = NULL;
        }
512
513
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
}

516

517
518
void taxisDefForecastTunit(int taxisID, int unit)
{
519
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
520

521
522
523
524
525
  if (taxisptr->fc_unit != unit)
    {
      taxisptr->fc_unit = unit;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
526
527
528
}


529
530
531
532
void taxisDefForecastPeriod(int taxisID, double fc_period)
{
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );

533
  if ( IS_NOT_EQUAL(taxisptr->fc_period, fc_period) )
534
535
536
537
    {
      taxisptr->fc_period = fc_period;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
538
539
540
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
542
void taxisDefNumavg(int taxisID, int numavg)
{
543
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544

545
546
547
548
549
  if (taxisptr->numavg != numavg)
    {
      taxisptr->numavg = numavg;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
551
552
553
554
555
556
557
}

/*
The type of the time axis, one of the set of predefined CDI time types.
The valid CDI time types are TAXIS_ABSOLUTE and TAXIS_RELATIVE.
*/
int taxisInqType(int taxisID)
{
558
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
559
  return taxisptr->type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
561
562
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
564
int taxisHasBounds(int taxisID)
{
565
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
566
  return taxisptr->has_bounds;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
568
569
}


570
571
572
573
574
575
576
577
578
579
580
581
void taxisWithBounds(int taxisID)
{
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);

  if ( taxisptr->has_bounds == false )
    {
      taxisptr->has_bounds = true;
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
}


582
583
void taxisDeleteBounds(int taxisID)
{
584
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
585

586
  if ( taxisptr->has_bounds )
587
    {
588
      taxisptr->has_bounds = false;
589
590
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
591
592
593
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
void taxisCopyTimestep(int taxisID2, int taxisID1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
{
596
  taxis_t *taxisptr1 = (taxis_t *)reshGetVal(taxisID1, &taxisOps),
597
          *taxisptr2 = (taxis_t *)reshGetVal(taxisID2, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598

599
  reshLock();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600

601
602
603
  taxisptr2->rdate = taxisptr1->rdate;
  taxisptr2->rtime = taxisptr1->rtime;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
604
605
606
607
  taxisptr2->vdate = taxisptr1->vdate;
  taxisptr2->vtime = taxisptr1->vtime;

  if ( taxisptr2->has_bounds )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
609
610
611
612
      taxisptr2->vdate_lb = taxisptr1->vdate_lb;
      taxisptr2->vtime_lb = taxisptr1->vtime_lb;
      taxisptr2->vdate_ub = taxisptr1->vdate_ub;
      taxisptr2->vtime_ub = taxisptr1->vtime_ub;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
614
    }

615
616
617
618
619
620
  taxisptr2->fdate = taxisptr1->fdate;
  taxisptr2->ftime = taxisptr1->ftime;

  taxisptr2->fc_unit   = taxisptr1->fc_unit;
  taxisptr2->fc_period = taxisptr1->fc_period;

621
622
  reshSetStatus(taxisID2, &taxisOps, RESH_DESYNC_IN_USE);
  reshUnlock();
623
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
624
625
626
627
628
629
630

/*
@Function  taxisInqVdate
@Title     Get the verification date

@Prototype int taxisInqVdate(int taxisID)
@Parameter
631
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
632
633

@Description
634
The function @func{taxisInqVdate} returns the verification date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
635
636

@Result
637
@func{taxisInqVdate} returns the verification date.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638
639
640

@EndFunction
*/
641
int64_t taxisInqVdate(int taxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
{
643
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
644
  return taxisptr->vdate;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
645
646
647
}


648
void taxisInqVdateBounds(int taxisID, int64_t *vdate_lb, int64_t *vdate_ub)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649
{
650
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
652
653
654
655
656

  *vdate_lb = taxisptr->vdate_lb;
  *vdate_ub = taxisptr->vdate_ub;
}


657
void taxisDefVdateBounds(int taxisID, int64_t vdate_lb, int64_t vdate_ub)
658
{
659
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
660

661
662
663
  if ( taxisptr->vdate_lb != vdate_lb
       || taxisptr->vdate_ub != vdate_ub
       || taxisptr->has_bounds == false )
664
665
666
    {
      taxisptr->vdate_lb = vdate_lb;
      taxisptr->vdate_ub = vdate_ub;
667
      taxisptr->has_bounds = true;
668
669
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
670
671
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
672
673
674
675
676
677
/*
@Function  taxisInqVtime
@Title     Get the verification time

@Prototype int taxisInqVtime(int taxisID)
@Parameter
678
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
680

@Description
681
The function @func{taxisInqVtime} returns the verification time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
683

@Result
684
@func{taxisInqVtime} returns the verification time.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
685
686
687
688
689

@EndFunction
*/
int taxisInqVtime(int taxisID)
{
690
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
691
  return taxisptr->vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
693
694
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
695
696
void taxisInqVtimeBounds(int taxisID, int *vtime_lb, int *vtime_ub)
{
697
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
698
699
700
701
702
703

  *vtime_lb = taxisptr->vtime_lb;
  *vtime_ub = taxisptr->vtime_ub;
}


704
705
void taxisDefVtimeBounds(int taxisID, int vtime_lb, int vtime_ub)
{
706
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
707

708
709
710
  if ( taxisptr->vtime_lb != vtime_lb
       || taxisptr->vtime_ub != vtime_ub
       || taxisptr->has_bounds == false )
711
712
713
    {
      taxisptr->vtime_lb = vtime_lb;
      taxisptr->vtime_ub = vtime_ub;
714
      taxisptr->has_bounds = true;
715
716
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
    }
717
718
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
720
721
722
/*
@Function  taxisInqRdate
@Title     Get the reference date

723
@Prototype int64_t taxisInqRdate(int taxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
@Parameter
725
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
727

@Description
728
The function @func{taxisInqRdate} returns the reference date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
730

@Result
731
@func{taxisInqRdate} returns the reference date.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
733
734

@EndFunction
*/
735
int64_t taxisInqRdate(int taxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
{
737
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
738

Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
  if ( taxisptr->rdate == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
741
742
      taxisptr->rdate = taxisptr->vdate;
      taxisptr->rtime = taxisptr->vtime;
743
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744
745
    }

746
  return taxisptr->rdate;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
747
748
749
750
751
752
753
754
}

/*
@Function  taxisInqRtime
@Title     Get the reference time

@Prototype int taxisInqRtime(int taxisID)
@Parameter
755
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757

@Description
758
The function @func{taxisInqRtime} returns the reference time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
760

@Result
761
@func{taxisInqRtime} returns the reference time.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
763
764
765
766

@EndFunction
*/
int taxisInqRtime(int taxisID)
{
767
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
768

Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
  if ( taxisptr->rdate == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
771
772
      taxisptr->rdate = taxisptr->vdate;
      taxisptr->rtime = taxisptr->vtime;
773
      reshSetStatus(taxisID, &taxisOps, RESH_DESYNC_IN_USE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
774
775
    }

776
  return taxisptr->rtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
777
778
}

779
780
781
782
/*
@Function  taxisInqFdate
@Title     Get the forecast reference date

783
@Prototype int64_t taxisInqFdate(int taxisID)
784
785
786
787
788
789
790
791
792
793
794
@Parameter
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}

@Description
The function @func{taxisInqFdate} returns the forecast reference date of a Time axis.

@Result
@func{taxisInqFdate} returns the forecast reference date.

@EndFunction
*/
795
int64_t taxisInqFdate(int taxisID)
796
{
797
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
798
799
800
801
802
803
804

  if ( taxisptr->fdate == -1 )
    {
      taxisptr->fdate = taxisptr->vdate;
      taxisptr->ftime = taxisptr->vtime;
    }

805
  return taxisptr->fdate;
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
}

/*
@Function  taxisInqFtime
@Title     Get the forecast reference time

@Prototype int taxisInqFtime(int taxisID)
@Parameter
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}

@Description
The function @func{taxisInqFtime} returns the forecast reference time of a Time axis.

@Result
@func{taxisInqFtime} returns the forecast reference time.

@EndFunction
*/
int taxisInqFtime(int taxisID)
{
826
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
827
828
829
830
831
832
833

  if ( taxisptr->fdate == -1 )
    {
      taxisptr->fdate = taxisptr->vdate;
      taxisptr->ftime = taxisptr->vtime;
    }

834
  return taxisptr->ftime;
835
836
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
837
838
839
840
841
842
/*
@Function  taxisInqCalendar
@Title     Get the calendar

@Prototype int taxisInqCalendar(int taxisID)
@Parameter
843
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate} or @fref{vlistInqTaxis}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
845

@Description
846
The function @func{taxisInqCalendar} returns the calendar of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
847
848

@Result
849
@func{taxisInqCalendar} returns the type of the calendar,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
one of the set of predefined CDI calendar types.
851
The valid CDI calendar types are @func{CALENDAR_STANDARD}, @func{CALENDAR_PROLEPTIC},
852
@func{CALENDAR_360DAYS}, @func{CALENDAR_365DAYS} and @func{CALENDAR_366DAYS}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
853
854
855
856
857

@EndFunction
*/
int taxisInqCalendar(int taxisID)
{
858
  taxis_t *taxisptr = (taxis_t *) reshGetVal ( taxisID, &taxisOps );
859
  return taxisptr->calendar;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
861
862
863
864
}


int taxisInqTunit(int taxisID)
{
865
  taxis_t *taxisptr = (taxis_t *) reshGetVal ( taxisID, &taxisOps );
866
  return taxisptr->unit;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
867
868
869
}


870
871
int taxisInqForecastTunit(int taxisID)
{
872
873
  taxis_t *taxisptr = (taxis_t *) reshGetVal ( taxisID, &taxisOps );
  return taxisptr->fc_unit;
874
875
876
}


877
878
879
double taxisInqForecastPeriod(int taxisID)
{
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
880
  return taxisptr->fc_period;
881
882
883
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
884
885
int taxisInqNumavg(int taxisID)
{
886
  taxis_t *taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
887
  return taxisptr->numavg;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
889
890
}


891
taxis_t *taxisPtr(int taxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
892
{
893
  taxis_t *taxisptr = (taxis_t *)reshGetVal(taxisID, &taxisOps);
894
  return taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
895
896
}

897

898
899
900
901
902
903
void ptaxisDefDatatype(taxis_t *taxisptr, int datatype)
{
  taxisptr->datatype = datatype;
}


904
void ptaxisDefName(taxis_t *taxisptr, const char *name)
905
{
906
  if ( name )
907
908
909
910
911
912
913
914
    {
      size_t len = strlen(name);
      delete_refcount_string(taxisptr->name);
      char *taxisname = taxisptr->name = new_refcount_string(len);
      strcpy(taxisname, name);
    }
}

915
916

void ptaxisDefLongname(taxis_t *taxisptr, const char *longname)
917
{
918
  if ( longname )
919
920
921
922
923
924
925
    {
      size_t len = strlen(longname);
      delete_refcount_string(taxisptr->longname);
      char *taxislongname = taxisptr->longname = new_refcount_string(len);
      strcpy(taxislongname, longname);
    }
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
926

927

928
929
930
931
932
933
934
935
936
937
938
939
void ptaxisDefUnits(taxis_t *taxisptr, const char *units)
{
  if ( units )
    {
      size_t len = strlen(units);
      delete_refcount_string(taxisptr->units);
      char *taxisunits = taxisptr->units = new_refcount_string(len);
      strcpy(taxisunits, units);
    }
}


Thomas Jahns's avatar
Thomas Jahns committed
940
static void
941
cdiDecodeTimevalue(int timeunit, double timevalue, int64_t *days, int *secs)
942
{
943
944
945
  *days = 0;
  *secs = 0;

946
947
948
949
950
951
952
953
954
955
956
957
958
959
  if ( timeunit == TUNIT_MINUTE )
    {
      timevalue *= 60;
      timeunit = TUNIT_SECOND;
    }
  else if ( timeunit == TUNIT_HOUR )
    {
      timevalue /= 24;
      timeunit = TUNIT_DAY;
    }

  if ( timeunit == TUNIT_SECOND )
    {
      *days = (int) (timevalue/86400);
960
      double seconds = timevalue - *days*86400.;
Thomas Jahns's avatar
Thomas Jahns committed
961
      *secs = (int)lround(seconds);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
      if ( *secs < 0 ) { *days -= 1; *secs += 86400; };
963
964
965
966
967
968
969
970
971
972
973
      /*
      {
	double cval = *days*86400. + *secs;
	if ( cval != timevalue )
	  printf("TUNIT_SECOND error: %g %g %d %d\n", timevalue, cval, *days, *secs);
      }
      */
    }
  else if ( timeunit == TUNIT_DAY )
    {
      *days = (int) timevalue;
974
      double seconds = (timevalue - *days)*86400;
Thomas Jahns's avatar
Thomas Jahns committed
975
      *secs = (int)lround(seconds);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
976
      if ( *secs < 0 ) { *days -= 1; *secs += 86400; };
977
978
979
980
981
982
983
984
985
986
      /*
      {
	double cval = *days + *secs/86400.;
	if ( cval != timevalue )
	  printf("TUNIT_DAY error: %g %g %d %d\n", timevalue, cval, *days, *secs);
      }
      */
    }
  else
    {
987
      static bool lwarn = true;
988
989
      if ( lwarn )
	{
990
	  Warning("timeunit %s unsupported!", tunitNamePtr(timeunit));
991
	  lwarn = false;
992
993
994
995
	}
    }
}

996
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
997
void cdiEncodeTimevalue(int days, int secs, int timeunit, double *timevalue)
998
999
1000
1001
1002
{
  if ( timeunit == TUNIT_SECOND )
    {
      *timevalue = days*86400. + secs;
    }
1003
1004
1005
  else if ( timeunit == TUNIT_MINUTE  ||
	    timeunit == TUNIT_QUARTER ||
	    timeunit == TUNIT_30MINUTES )
1006
1007
1008
    {
      *timevalue = days*1440. + secs/60.;
    }
1009
1010
1011
1012
  else if ( timeunit == TUNIT_HOUR   ||
	    timeunit == TUNIT_3HOURS ||
	    timeunit == TUNIT_6HOURS ||
	    timeunit == TUNIT_12HOURS )
1013
1014
1015
1016
1017
1018
1019
1020
1021
    {
      *timevalue = days*24. + secs/3600.;
    }
  else if ( timeunit == TUNIT_DAY )
    {
      *timevalue = days + secs/86400.;
    }
  else
    {
1022
      static bool lwarn = true;
1023
1024
      if ( lwarn )
	{
1025
	  Warning("timeunit %s unsupported!", tunitNamePtr(timeunit));
1026
	  lwarn = false;
1027
1028
1029
1030
	}
    }
}

1031

1032
void timeval2vtime(double timevalue, taxis_t *taxis, int64_t *vdate, int *vtime)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1033
{
1034
  int64_t rdate = taxis->rdate;
1035
  int rtime = taxis->rtime;
1036

1037
1038
1039
1040
1041
1042
  if ( DBL_IS_EQUAL(timevalue, 0.) )
    {
      *vdate = rdate;
      *vtime = rtime;
      return;
    }
1043

1044
  int year, month, day, hour, minute, second;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045
1046
  cdiDecodeDate(rdate, &year, &month, &day);
  cdiDecodeTime(rtime, &hour, &minute, &second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1047

1048
1049
1050
  int timeunit = taxis->unit;
  int calendar = taxis->calendar;

1051
1052
1053
1054
1055
1056
  if ( timeunit == TUNIT_MONTH && calendar == CALENDAR_360DAYS )
    {
      timeunit = TUNIT_DAY;
      timevalue *= 30;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1057
1058
  if ( timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR )
    {
1059
      if ( timeunit == TUNIT_YEAR ) timevalue *= 12;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1060

1061
1062
      int nmon = (int) timevalue;
      double fmon = timevalue - nmon;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063

1064
      month += nmon;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065

1066
1067
      while ( month > 12 ) { month -= 12; year++; }
      while ( month <  1 ) { month += 12; year--; }
1068

1069
      int dpm = days_per_month(calendar, year, month);
1070
1071
      timeunit = TUNIT_DAY;
      timevalue = fmon*dpm;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1072
    }