taxis.c 23.2 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <string.h>

#include "dmemory.h"

#include "cdi.h"
#include "stream_int.h"
11
#include "calendar.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

int DefaultTimeType = TAXIS_ABSOLUTE;
int DefaultTimeUnit = TUNIT_HOUR;
int DefaultCalendar = CALENDAR_STANDARD;


char *Timeunits[] = {
  "undefined",
  "seconds",
  "minutes",
  "hours",
  "days",
  "months",
  "years",
};


char *tunitNamePtr(int unitID)
{
  char *name;
  int size = sizeof(Timeunits)/sizeof(char *);

  if ( unitID > 0 && unitID < size )
    name = Timeunits[unitID];
  else
    name = Timeunits[0];

  return (name);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
void taxis_defaults(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
  static char func[] = "taxis_defaults";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  char *timeunit;

  timeunit = getenv("TIMEUNIT");
  if ( timeunit )
    {
      if ( strcmp(timeunit, "minute") == 0 )
	DefaultTimeUnit = TUNIT_MINUTE;
      else if ( strcmp(timeunit, "hour") == 0 )
	DefaultTimeUnit = TUNIT_HOUR;
      else if ( strcmp(timeunit, "day") == 0 )
	DefaultTimeUnit = TUNIT_DAY;
      else if ( strcmp(timeunit, "month") == 0 )
	DefaultTimeUnit = TUNIT_MONTH;
      else if ( strcmp(timeunit, "year") == 0 )
	DefaultTimeUnit = TUNIT_YEAR;
      else
	Warning(func, "Unsupported TIMEUNIT %s!", timeunit);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
}


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

static int _taxis_max = MAX_TAXIS;

static void taxis_initialize(void);

static int _taxis_init = FALSE;

#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>

static pthread_once_t _taxis_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _taxis_mutex;

#  define TAXIS_LOCK           pthread_mutex_lock(&_taxis_mutex);
#  define TAXIS_UNLOCK         pthread_mutex_unlock(&_taxis_mutex);
#  define TAXIS_INIT                               \
   if ( _taxis_init == FALSE ) pthread_once(&_taxis_init_thread, taxis_initialize);

#else

#  define TAXIS_LOCK
#  define TAXIS_UNLOCK
#  define TAXIS_INIT                               \
   if ( _taxis_init == FALSE ) taxis_initialize();

#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
95

Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
typedef struct _taxisPtrToIdx {
  int idx;
  TAXIS *ptr;
  struct _taxisPtrToIdx *next;
} taxisPtrToIdx;


static taxisPtrToIdx *_taxisList  = NULL;
static taxisPtrToIdx *_taxisAvail = NULL;


static void taxis_list_new(void)
{
  static char func[] = "taxis_list_new";

  assert(_taxisList == NULL);

  _taxisList = (taxisPtrToIdx *) malloc(_taxis_max*sizeof(taxisPtrToIdx));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
static void taxis_list_delete(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
  static char func[] = "taxis_list_delete";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120

Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
  if ( _taxisList ) free(_taxisList);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
124
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
static void taxis_init_pointer(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131
132
133
134
135
136
137
138
  int  i;
  
  for ( i = 0; i < _taxis_max; i++ )
    {
      _taxisList[i].next = _taxisList + i + 1;
      _taxisList[i].idx  = i;
      _taxisList[i].ptr  = 0;
    }

  _taxisList[_taxis_max-1].next = 0;

  _taxisAvail = _taxisList;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
140
141
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
TAXIS *taxis_to_pointer(int idx)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
  static char func[] = "taxis_to_pointer";
  TAXIS *taxisptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146

Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
  TAXIS_INIT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
  if ( idx >= 0 && idx < _taxis_max )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
      TAXIS_LOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
      taxisptr = _taxisList[idx].ptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154

Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
      TAXIS_UNLOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
    }
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    Error(func, "taxis index %d undefined!", idx);

  return (taxisptr);
}


/* Create an index from a pointer */
static int taxis_from_pointer(TAXIS *ptr)
{
  static char func[] = "taxis_from_pointer";
  int      idx = -1;
  taxisPtrToIdx *newptr;

  if ( ptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
174
175
      TAXIS_LOCK

      if ( _taxisAvail )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
	  newptr       = _taxisAvail;
178
	  _taxisAvail  = _taxisAvail->next;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
181
182
183
184
	  newptr->next = 0;
	  idx	       = newptr->idx;
	  newptr->ptr  = ptr;
      
	  if ( TAXIS_Debug )
	    Message(func, "Pointer %p has idx %d from taxis list", ptr, idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187
188
189
      else
	Warning(func, "Too many open taxis (limit is %d)!", _taxis_max);

      TAXIS_UNLOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
  else
    Error(func, "Internal problem (pointer %p undefined)", ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193

Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
195
  return (idx);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
197


Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
void taxis_init_ptr(TAXIS *taxisptr)
{
  taxisptr->self       = -1;

  taxisptr->used       = FALSE;
  taxisptr->type       = DefaultTimeType;
  taxisptr->vdate      = 0;
  taxisptr->vtime      = 0;
  taxisptr->rdate      = CDI_UNDEFID;
  taxisptr->rtime      = 0;
  taxisptr->calendar   = DefaultCalendar;
  taxisptr->unit       = DefaultTimeUnit;
  taxisptr->numavg     = 0;
  taxisptr->has_bounds = FALSE;
  taxisptr->vdate_lb   = 0;
  taxisptr->vtime_lb   = 0;
  taxisptr->vdate_ub   = 0;
  taxisptr->vtime_ub   = 0;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217

Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219
220
void taxis_init_entry(TAXIS *taxisptr)
{
  taxis_init_ptr(taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221

Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
  taxisptr->self       = taxis_from_pointer(taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
224
225
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
static TAXIS *taxis_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
230
231
  static char func[] = "taxis_new_entry";
  TAXIS *taxisptr;

  taxisptr = (TAXIS *) malloc(sizeof(TAXIS));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232

Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
  if ( taxisptr ) taxis_init_entry(taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234

235
236
  /*  fprintf(stderr, "self: %d %p\n", taxisptr->self, taxisptr); */

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  return (taxisptr);
}


static void taxis_delete_entry(TAXIS *taxisptr)
{
  static char func[] = "taxis_delete_entry";
  int idx;

  idx = taxisptr->self;

  TAXIS_LOCK

  free(taxisptr);

  _taxisList[idx].next = _taxisAvail;
  _taxisList[idx].ptr  = 0;
  _taxisAvail          = &_taxisList[idx];

  TAXIS_UNLOCK

  if ( TAXIS_Debug )
    Message(func, "Removed idx %d from taxis list", idx);
}


static void taxis_initialize(void)
{
  char *env;

#if  defined  (HAVE_LIBPTHREAD)
  /* initialize global API mutex lock */
  pthread_mutex_init(&_taxis_mutex, NULL);
#endif

  env = getenv("TAXIS_DEBUG");
  if ( env ) TAXIS_Debug = atoi(env);

  taxis_list_new();
  atexit(taxis_list_delete);

278
279
  TAXIS_LOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
  taxis_init_pointer();
281
282
  
  TAXIS_UNLOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

  _taxis_init = TRUE;

  taxis_defaults();
}


static void taxis_copy(TAXIS *taxisptr2, TAXIS *taxisptr1)
{
  int taxisID2;

  taxisID2 = taxisptr2->self;
  memcpy(taxisptr2, taxisptr1, sizeof(TAXIS));
  taxisptr2->self = taxisID2;
}


static void taxis_check_ptr(const char *func, TAXIS *taxisptr)
{
  if ( taxisptr == NULL )
    Error(func, "taxis undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
305
306
307
}


/*
308
@Function  taxisCreate
309
@Title     Create a Time axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310

311
@Prototype int taxisCreate(int taxistype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
    @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
315
316

@Description
317
The function @func{taxisCreate} creates a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
@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);
taxisDefRdate(taxisID, 19870101);
taxisDefRtime(taxisID, 1200);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338
@EndFunction
*/
339
int taxisCreate(int taxistype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
{
341
  static char func[] = "taxisCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
  int taxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345
346
347

  if ( CDI_Debug )
    Message(func, "taxistype: %d", taxistype);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349
350
351
352
353
354
  TAXIS_INIT

  taxisptr = taxis_new_entry();
  if ( ! taxisptr ) Error(func, "No memory");

  taxisID = taxisptr->self;
  taxisptr->type = taxistype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
358
359
360
361
362

  if ( CDI_Debug )
    Message(func, "taxisID: %d", taxisID);

  return (taxisID);
}


363
364
365
366
367
368
/*
@Function  taxisDestroy
@Title     Destroy a Time axis

@Prototype void taxisDestroy(int taxisID)
@Parameter
369
    @Item  taxisID  Time axis ID, from a previous call to @func{taxisCreate}
370
371
372
373
374

@EndFunction
*/
void taxisDestroy(int taxisID)
{
375
376
377
378
379
380
381
382
  static char func[] = "taxisDestroy";
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);

  taxis_check_ptr(func, taxisptr);

  taxis_delete_entry(taxisptr);
383
384
385
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
386
387
388
389
int taxisDuplicate(int taxisID1)
{
  static char func[] = "taxisDuplicate";
  int taxisID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
392
393
394
395
396
397
398
  TAXIS *taxisptr1;
  TAXIS *taxisptr2;

  taxisptr1 = taxis_to_pointer(taxisID1);

  taxisptr2 = taxis_new_entry();
  if ( ! taxisptr2 ) Error(func, "No memory");

  taxisID2 = taxisptr2->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
400

  if ( CDI_Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
    Message(func, "taxisID2: %d", taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402

Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
  ptaxisCopy(taxisptr2, taxisptr1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
405
406
407
408
409
410
411

  return (taxisID2);
}


void taxisDefType(int taxisID, int type)
{
  static char func[] = "taxisDefType";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
413
414
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415

Uwe Schulzweida's avatar
Uwe Schulzweida committed
416
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417

Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
  taxisptr->type = type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
422
423
424
425
426
427
}


/*
@Function  taxisDefVdate
@Title     Define the verification date

@Prototype void taxisDefVdate(int taxisID, int vdate)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
    @Item  vdate    Verification date (YYYYMMDD)

@Description
432
The function @func{taxisDefVdate} defines the verification date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
434
435
436
437
438

@EndFunction
*/
void taxisDefVdate(int taxisID, int vdate)
{
  static char func[] = "taxisDefVdate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
440

Uwe Schulzweida's avatar
Uwe Schulzweida committed
441
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442

Uwe Schulzweida's avatar
Uwe Schulzweida committed
443
444
445
  taxis_check_ptr(func, taxisptr);

  taxisptr->vdate = vdate;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
446
447
448
449
450
451
452
453
454
}


/*
@Function  taxisDefVtime
@Title     Define the verification time

@Prototype void taxisDefVtime(int taxisID, int vtime)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456
    @Item  vtime    Verification time (hhmm)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458

@Description
459
The function @func{taxisDefVtime} defines the verification time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
460
461
462
463
464
465

@EndFunction
*/
void taxisDefVtime(int taxisID, int vtime)
{
  static char func[] = "taxisDefVtime";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
466
467
468
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
469

Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
471

Uwe Schulzweida's avatar
Uwe Schulzweida committed
472
  taxisptr->vtime = vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
475
476
477
478
479
480
481
}


/*
@Function  taxisDefRdate
@Title     Define the reference date

@Prototype void taxisDefRdate(int taxisID, int rdate)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
484
485
    @Item  rdate    Reference date (YYYYMMDD)

@Description
486
The function @func{taxisDefVdate} defines the reference date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
491
492

@EndFunction
*/
void taxisDefRdate(int taxisID, int rdate)
{
  static char func[] = "taxisDefRdate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494

Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496

Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
498
499
  taxis_check_ptr(func, taxisptr);

  taxisptr->rdate = rdate;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501
502
503
504
505
506
507
508
}


/*
@Function  taxisDefRtime
@Title     Define the reference time

@Prototype void taxisDefRtime(int taxisID, int rtime)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
    @Item  rtime    Reference time (hhmm)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
512

@Description
513
The function @func{taxisDefVdate} defines the reference time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
516
517
518
519

@EndFunction
*/
void taxisDefRtime(int taxisID, int rtime)
{
  static char func[] = "taxisDefRtime";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
521
522
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
523

Uwe Schulzweida's avatar
Uwe Schulzweida committed
524
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525

Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
  taxisptr->rtime = rtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
527
528
529
530
531
532
533
534
535
}


/*
@Function  taxisDefCalendar
@Title     Define the calendar

@Prototype void taxisDefCalendar(int taxisID, int calendar)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
537
538
539
    @Item  calendar The type of the calendar, one of the set of predefined CDI calendar types.
                    The valid CDI calendar types are @func{CALENDAR_STANDARD}, @func{CALENDAR_360DAYS},
                    @func{CALENDAR_365DAYS} and @func{CALENDAR_366DAYS}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
541

@Description
542
The function @func{taxisDefCalendar} defines the calendar of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
545
546
547
548

@EndFunction
*/
void taxisDefCalendar(int taxisID, int calendar)
{
  static char func[] = "taxisDefCalendar";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
550

Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552

Uwe Schulzweida's avatar
Uwe Schulzweida committed
553
554
555
  taxis_check_ptr(func, taxisptr);

  taxisptr->calendar = calendar;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
557
558
559
560
561
}


void taxisDefTunit(int taxisID, int unit)
{
  static char func[] = "taxisDefTunit";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562
563
564
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
565

Uwe Schulzweida's avatar
Uwe Schulzweida committed
566
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567

Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
  taxisptr->unit = unit;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
569
570
571
572
573
}

void taxisDefNumavg(int taxisID, int numavg)
{
  static char func[] = "taxisDefNumavg";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575

Uwe Schulzweida's avatar
Uwe Schulzweida committed
576
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577

Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
579
580
  taxis_check_ptr(func, taxisptr);

  taxisptr->numavg = numavg;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582
583
584
585
586
587
588
589
590
}


/*
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)
{
  static char func[] = "taxisInqType";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592
593
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594

Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596

Uwe Schulzweida's avatar
Uwe Schulzweida committed
597
  return (taxisptr->type);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598
599
600
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
601
void taxisCopyTimestep(int taxisID2, int taxisID1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
603
{
  static char func[] = "taxisCopyTimestep";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
604
605
  TAXIS *taxisptr1;
  TAXIS *taxisptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
606

Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
608
  taxisptr1 = taxis_to_pointer(taxisID1);
  taxisptr2 = taxis_to_pointer(taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
609

Uwe Schulzweida's avatar
Uwe Schulzweida committed
610
611
  taxis_check_ptr(func, taxisptr1);
  taxis_check_ptr(func, taxisptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
612

613
614
615
  taxisptr2->rdate = taxisptr1->rdate;
  taxisptr2->rtime = taxisptr1->rtime;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
617
618
619
  taxisptr2->vdate = taxisptr1->vdate;
  taxisptr2->vtime = taxisptr1->vtime;

  if ( taxisptr2->has_bounds )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
621
622
623
624
      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
625
626
627
628
629
630
631
632
633
634
    }
}


/*
@Function  taxisInqVdate
@Title     Get the verification date

@Prototype int taxisInqVdate(int taxisID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
635
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
637

@Description
638
The function @func{taxisInqVdate} returns the verification date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
639
640

@Result
641
@func{taxisInqVdate} returns the verification date.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
643
644
645
646
647

@EndFunction
*/
int taxisInqVdate(int taxisID)
{
  static char func[] = "taxisInqVdate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
648
649
650
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651

Uwe Schulzweida's avatar
Uwe Schulzweida committed
652
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
653

Uwe Schulzweida's avatar
Uwe Schulzweida committed
654
  return (taxisptr->vdate);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
656
657
658
659
660
661
662
663
}


/*
@Function  taxisInqVtime
@Title     Get the verification time

@Prototype int taxisInqVtime(int taxisID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
665
666

@Description
667
The function @func{taxisInqVtime} returns the verification time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668
669

@Result
670
@func{taxisInqVtime} returns the verification time.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
671
672
673
674
675
676

@EndFunction
*/
int taxisInqVtime(int taxisID)
{
  static char func[] = "taxisInqVtime";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
677
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
678

Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680

Uwe Schulzweida's avatar
Uwe Schulzweida committed
681
682
683
  taxis_check_ptr(func, taxisptr);

  return (taxisptr->vtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
684
685
686
687
688
689
690
691
692
}


/*
@Function  taxisInqRdate
@Title     Get the reference date

@Prototype int taxisInqRdate(int taxisID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
693
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
694
695

@Description
696
The function @func{taxisInqRdate} returns the reference date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
698

@Result
699
@func{taxisInqVdate} returns the reference date.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700
701
702
703
704
705

@EndFunction
*/
int taxisInqRdate(int taxisID)
{
  static char func[] = "taxisInqRdate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
707
708
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709

Uwe Schulzweida's avatar
Uwe Schulzweida committed
710
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711

Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
  if ( taxisptr->rdate == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
713
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
714
715
      taxisptr->rdate = taxisptr->vdate;
      taxisptr->rtime = taxisptr->vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
717
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
  return (taxisptr->rdate);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
720
721
722
723
724
725
726
727
}


/*
@Function  taxisInqRtime
@Title     Get the reference time

@Prototype int taxisInqRtime(int taxisID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
730

@Description
731
The function @func{taxisInqRtime} returns the reference time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
733

@Result
734
@func{taxisInqVtime} returns the reference time.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
736
737
738
739
740

@EndFunction
*/
int taxisInqRtime(int taxisID)
{
  static char func[] = "taxisInqRtime";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
741
742
743
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744

Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
746

Uwe Schulzweida's avatar
Uwe Schulzweida committed
747
  if ( taxisptr->rdate == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
749
750
      taxisptr->rdate = taxisptr->vdate;
      taxisptr->rtime = taxisptr->vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
753
  return (taxisptr->rtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755
756
757
758
759
760
761
762
}


/*
@Function  taxisInqCalendar
@Title     Get the calendar

@Prototype int taxisInqCalendar(int taxisID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
764
765

@Description
766
The function @func{taxisInqCalendar} returns the calendar of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
767
768

@Result
769
@func{taxisInqCalendar} returns the type of the calendar,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
one of the set of predefined CDI calendar types.
771
772
The valid CDI calendar types are @func{CALENDAR_STANDARD}, @func{CALENDAR_360DAYS},
@func{CALENDAR_365DAYS} and @func{CALENDAR_366DAYS}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
773
774
775
776
777
778

@EndFunction
*/
int taxisInqCalendar(int taxisID)
{
  static char func[] = "taxisInqCalendar";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
779
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780

Uwe Schulzweida's avatar
Uwe Schulzweida committed
781
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
782

Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784
785
  taxis_check_ptr(func, taxisptr);

  return (taxisptr->calendar);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787
788
789
790
791
}


int taxisInqTunit(int taxisID)
{
  static char func[] = "taxisInqTunit";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
792
793
794
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
795

Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
797

Uwe Schulzweida's avatar
Uwe Schulzweida committed
798
  return (taxisptr->unit);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
799
800
801
802
803
804
}


int taxisInqNumavg(int taxisID)
{
  static char func[] = "taxisInqNumavg";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
805
  TAXIS *taxisptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
806

Uwe Schulzweida's avatar
Uwe Schulzweida committed
807
  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
808

Uwe Schulzweida's avatar
Uwe Schulzweida committed
809
810
811
  taxis_check_ptr(func, taxisptr);

  return (taxisptr->numavg);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
813
814
815
816
817
}


TAXIS *taxisPtr(int taxisID)
{
  static char func[] = "taxisPtr";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
818
819
820
  TAXIS *taxisptr;

  taxisptr = taxis_to_pointer(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
821

Uwe Schulzweida's avatar
Uwe Schulzweida committed
822
  taxis_check_ptr(func, taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
823

Uwe Schulzweida's avatar
Uwe Schulzweida committed
824
  return (taxisptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
826
827
}


828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
void decode_timevalue(int timeunit, double timevalue, int *days, int *secs)
{
  static char func[] = "decode_timevalue";
  static int lwarn = TRUE;

  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);
      *secs = (int) (timevalue - *days*86400.);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
848
      if ( *secs < 0 ) { *days -= 1; *secs += 86400; };
849
850
851
852
853
854
855
856
857
858
859
      /*
      {
	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;
860
      *secs = (int) ((timevalue - *days)*86400 + 0.5);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
      if ( *secs < 0 ) { *days -= 1; *secs += 86400; };
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
      /*
      {
	double cval = *days + *secs/86400.;
	if ( cval != timevalue )
	  printf("TUNIT_DAY error: %g %g %d %d\n", timevalue, cval, *days, *secs);
      }
      */
    }
  else
    {
      if ( lwarn )
	{
	  Warning(func, "timeunit %s unsupported!", tunitNamePtr(timeunit));
	  lwarn = FALSE;
	}
    }
}


void encode_timevalue(int days, int secs, int timeunit, double *timevalue)
{
  static char func[] = "encode_timevalue";
  static int lwarn = TRUE;

  if ( timeunit == TUNIT_SECOND )
    {
      *timevalue = days*86400. + secs;
    }
  else if ( timeunit == TUNIT_MINUTE )
    {
      *timevalue = days*1440. + secs/60.;
    }
  else if ( timeunit == TUNIT_HOUR )
    {
      *timevalue = days*24. + secs/3600.;
    }
  else if ( timeunit == TUNIT_DAY )
    {
      *timevalue = days + secs/86400.;
    }
  else
    {
      if ( lwarn )
	{
	  Warning(func, "timeunit %s unsupported!", tunitNamePtr(timeunit));
	  lwarn = FALSE;
	}
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
914
void timeval2vtime(double timevalue, TAXIS *taxis, int *vdate, int *vtime)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
915
  static char func[] = "timeval2vtime";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916
917
918
  int year, month, day, hour, minute;
  int rdate, rtime;
  int timeunit;
919
920
  int calendar;
  int julday, secofday, days, secs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921
  static int lwarn = TRUE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
922
923
924
925
926
927
928
929
930

  *vdate = 0;
  *vtime = 0;

  timeunit = (*taxis).unit;
  calendar = (*taxis).calendar;

  rdate  = (*taxis).rdate;
  rtime  = (*taxis).rtime;
931
932
  decode_date(rdate, &year, &month, &day);
  decode_time(rtime, &hour, &minute);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
933

934
935
936
937
938
939
  if ( timeunit == TUNIT_MONTH && calendar == CALENDAR_360DAYS )
    {
      timeunit = TUNIT_DAY;
      timevalue *= 30;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941
942
943
  if ( timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR )
    {
      if ( timeunit == TUNIT_YEAR ) timevalue *= 12;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
945
946
947
948
949
      if ( (NINT(timevalue*10))%10 && lwarn )
	{
	  Warning(func, "Possible wrong calculation of date/time!\nTime offset must be integer for time unit MONTH and YEAR!");
	  lwarn = FALSE;
	}
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
951
952
953
954
955
956
      month += NINT(timevalue);

      while ( month > 12 ) { month -= 12; year++; }
      while ( month <  1 ) { month += 12; year--; }
    }
  else
    {
957
      encode_caldaysec(calendar, year, month, day, hour, minute, &julday, &secofday);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
958

959
      decode_timevalue(timeunit, timevalue, &days, &secs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960

961
      julday_add(days, secs, &julday, &secofday);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
962

963
      decode_caldaysec(calendar, julday, secofday, &year, &month, &day, &hour, &minute);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
965
    }

966
967
  *vdate = encode_date(year, month, day);
  *vtime = encode_time(hour, minute);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
968
969
970
971
972
973
974
975
976
977
}


double vtime2timeval(int vdate, int vtime, TAXIS *taxis)
{
  int ryear, rmonth;
  int year, month, day, hour, minute;
  int rdate, rtime;
  double value;
  int timeunit;
978
  int timeunit0;
979
980
  int calendar;
  int julday1, secofday1, julday2, secofday2, days, secs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
981
982
983
984

  timeunit = (*taxis).unit;
  calendar = (*taxis).calendar;

985
986
  rdate = (*taxis).rdate;
  rtime = (*taxis).rtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
987
988
989
990
991
992
  if ( rdate == -1 )
    {
      rdate  = (*taxis).vdate;
      rtime  = (*taxis).vtime;
    }

993
994
  decode_date(rdate, &ryear, &rmonth, &day);
  decode_time(rtime, &hour, &minute);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
995

996
  encode_caldaysec(calendar, ryear, rmonth, day, hour, minute, &julday1, &secofday1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
997

998
999
  decode_date(vdate, &year, &month, &day);
  decode_time(vtime, &hour, &minute);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1000

1001
1002
1003
1004
1005
1006
1007
  timeunit0 = timeunit;

  if ( timeunit == TUNIT_MONTH && calendar == CALENDAR_360DAYS )
    {
      timeunit = TUNIT_DAY;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1008
1009
1010
1011
1012
1013
1014
  if ( timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR )
    {
      value = (year-ryear)*12 - rmonth + month;
      if ( timeunit == TUNIT_YEAR ) value = NINT(value/12);
    }
  else
    {
1015
1016
1017
      encode_caldaysec(calendar, year, month, day, hour, minute, &julday2, &secofday2);

      julday_sub(julday1, secofday1, julday2, secofday2, &days, &secs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018

1019
      encode_timevalue(days, secs, timeunit, &value);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1020
1021
    }

1022
1023
1024
1025
1026
  if ( timeunit0 == TUNIT_MONTH && calendar == CALENDAR_360DAYS )
    {
      value /= 30;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
  return (value);
}


void splitTimevalue(double timevalue, int timeunit, int *date, int *time)
{
  static char func[] = "splitTimevalue";
  int vdate = 0, vtime = 0;
  int hour, minute;

  if ( timeunit == TUNIT_HOUR )
    {
      timevalue /= 24;
      vdate = (int) timevalue;
1041
1042
1043
1044
1045
      if ( vdate < 0 )
	vtime = (int) (-(timevalue - vdate)*1440 + 0.01);
      else
	vtime = (int) ((timevalue - vdate)*1440 + 0.01);
 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1046
1047
1048
1049
1050
1051
1052
      hour   = vtime / 60;
      minute = vtime - hour*60;
      vtime  = hour*100 + minute;
    }
  else if ( timeunit == TUNIT_DAY )
    {
      vdate = (int) timevalue;
1053
1054
1055
1056
      if ( vdate < 0 )
	vtime = (int) (-(timevalue - vdate)*1440 + 0.01);
      else
	vtime = (int) ((timevalue - vdate)*1440 + 0.01);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068

      hour   = vtime / 60;
      minute = vtime - hour*60;
      vtime  = hour*100 + minute;
    }
  else if ( timeunit == TUNIT_MONTH )
    {
      vdate = (int) timevalue*100;
      vtime = 0;
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
      Message(func, "Unsupported TIMEUNIT: %s!", tunitNamePtr(timeunit));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
    }

  *date = vdate;
  *time = vtime;
}


void decode_timeval(double timevalue, TAXIS *taxis, int *date, int *time)
{
  if ( taxis->type == TAXIS_ABSOLUTE )
    splitTimevalue(timevalue, taxis->unit, date, time);
  else
    timeval2vtime(timevalue, taxis, date, time);
}


double encode_timeval(int date, int time, TAXIS *taxis)
{
  double timevalue;

  if ( taxis->type == TAXIS_ABSOLUTE )
    {
      if ( taxis->unit == TUNIT_MONTH )
	{
1094
1095
1096
1097
1098
1099
	  int year, month, day;
	  decode_date(date, &year, &month, &day);
	  if ( day == 0 )
	    timevalue = date/100;
	  else
	    timevalue = date/100 + 0.5;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1100
1101
1102
1103
1104
	}
      else
	{
	  int hour   = time / 100;
	  int minute = time - hour*100;
1105
1106
1107
1108
	  if ( date < 0 )
	    timevalue = -(-date + (hour*60 + minute)/1440.);
	  else
	    timevalue = date + (hour*60 + minute)/1440.;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1109
1110
1111
1112
1113
1114
1115
	}
    }
  else
    timevalue = vtime2timeval(date, time, taxis);

  return (timevalue);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125


void ptaxisInit(TAXIS *taxisptr)
{
  taxis_init_ptr(taxisptr);
}


void ptaxisCopy(TAXIS *dest, TAXIS *source)
{
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
  /* memcpy(dest, source, sizeof(TAXIS)); */
  dest->used       = source->used;  
  dest->type       = source->type;
  dest->vdate      = source->vdate;
  dest->vtime      = source->vtime;
  dest->rdate      = source->rdate;
  dest->rtime      = source->rtime;
  dest->calendar   = source->calendar;
  dest->unit       = source->unit;
  dest->numavg     = source->numavg;
  dest->has_bounds = source->has_bounds;
  dest->vdate_lb   = source->vdate_lb;
  dest->vtime_lb   = source->vtime_lb;
  dest->vdate_ub   = source->vdate_ub;
  dest->vtime_ub   = source->vtime_ub;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
}