taxis.c 18.1 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <string.h>

#include "dmemory.h"

#include "cdi.h"
#include "stream_int.h"

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

static int  TimeTableInit = 0;

TAXIS *taxisTable;
int    taxisTableSize = 0;

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);
}


void taxisTableInit(void)
{
  static char func[] = "taxisTableInit";
  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);
    }

  TimeTableInit = 1;
}


void ptaxisInit(TAXIS *taxis)
{
  if ( ! TimeTableInit ) taxisTableInit();

  taxis->used       = FALSE;
  taxis->type       = DefaultTimeType;
  taxis->vdate      = 0;
  taxis->vtime      = 0;
  taxis->rdate      = CDI_UNDEFID;
  taxis->rtime      = 0;
  taxis->calendar   = DefaultCalendar;
  taxis->unit       = DefaultTimeUnit;
  taxis->numavg     = 0;
  taxis->has_bounds = FALSE;
  taxis->vdate_lb   = 0;
  taxis->vtime_lb   = 0;
  taxis->vdate_ub   = 0;
  taxis->vtime_ub   = 0;
}


void ptaxisCopy(TAXIS *dest, TAXIS *source)
{
  memcpy(dest, source, sizeof(TAXIS));
}


int taxisTableNewEntry(int timetype)
{
  static char func[] = "taxisTableNewEntry";
  int taxisID = 0;

  if ( ! TimeTableInit ) taxisTableInit();

  /*
    Look for a free slot in taxisTable.
    (Create the table the first time through).
  */
  if ( ! taxisTableSize )
    {
      int i;

      taxisTableSize = 2;
      taxisTable = (TAXIS *) malloc(taxisTableSize*sizeof(TAXIS));
      if ( taxisTable == NULL )
	{
          Message(func, "taxisTableSize = %d", taxisTableSize);
	  SysError(func, "Allocation of TAXISTABLE failed");
	}

      for ( i = 0; i < taxisTableSize; i++ )
	{
	  taxisTable[i].used = FALSE;
	  ptaxisInit(&taxisTable[i]);
	}
    }
  else
    {
      while ( taxisID < taxisTableSize )
	{
	  if ( ! taxisTable[taxisID].used ) break;
	  taxisID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if ( taxisID == taxisTableSize )
    {
      int i;

      taxisTableSize = 2*taxisTableSize;
      taxisTable = (TAXIS *) realloc(taxisTable, taxisTableSize*sizeof(TAXIS));
      if ( taxisTable == NULL )
	{
          Message(func, "taxisTableSize = %d", taxisTableSize);
	  SysError(func, "Reallocation of TAXISTABLE failed");
	}

      taxisID = taxisTableSize/2;

      for ( i = taxisID; i < taxisTableSize; i++ )
	{
	  taxisTable[i].used = FALSE;
	  ptaxisInit(&taxisTable[i]);
	}
    }

  taxisTable[taxisID].type = timetype;
  taxisTable[taxisID].used = TRUE;

  return (taxisID);
}


static void taxisCheckID(const char *func, int taxisID)
{
  if ( taxisID == CDI_UNDEFID )
    Error(func, "taxisID undefined!");

  if ( taxisID < 0 || taxisID >= taxisTableSize )
    Error(func, "taxisID %d undefined!", taxisID);

  if ( taxisTable[taxisID].type == CDI_UNDEFID )
    Error(func, "taxisID %d type undefined!", taxisID);
}


/*
181
@Function  taxisCreate
182
@Title     Create a Time axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183

184
@Prototype int taxisCreate(int taxistype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187
    @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
188
189

@Description
190
The function @func{taxisCreate} creates a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
@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
210
211
@EndFunction
*/
212
int taxisCreate(int taxistype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
{
214
  static char func[] = "taxisCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  int taxisID;

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

  /* get next free time entry */
  taxisID = taxisTableNewEntry(taxistype);

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

  return (taxisID);
}


230
231
232
233
234
235
/*
@Function  taxisDestroy
@Title     Destroy a Time axis

@Prototype void taxisDestroy(int taxisID)
@Parameter
236
    @Item  taxisID  Time axis ID, from a previous call to @func{taxisCreate}
237
238
239
240
241
242
243
244

@EndFunction
*/
void taxisDestroy(int taxisID)
{
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
int taxisDuplicate(int taxisID1)
{
  static char func[] = "taxisDuplicate";
  int taxisID2;
  /* get next free time entry */
  taxisID2 = taxisTableNewEntry(taxisInqType(taxisID1));

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

  ptaxisCopy(taxisPtr(taxisID2), taxisPtr(taxisID1));

  return (taxisID2);
}


void taxisDefType(int taxisID, int type)
{
  static char func[] = "taxisDefType";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].type = type;
}


/*
@Function  taxisDefVdate
@Title     Define the verification date

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

@Description
281
The function @func{taxisDefVdate} defines the verification date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

@EndFunction
*/
void taxisDefVdate(int taxisID, int vdate)
{
  static char func[] = "taxisDefVdate";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].vdate = vdate;
}


/*
@Function  taxisDefVtime
@Title     Define the verification time

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

@Description
305
The function @func{taxisDefVtime} defines the verification time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

@EndFunction
*/
void taxisDefVtime(int taxisID, int vtime)
{
  static char func[] = "taxisDefVtime";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].vtime = vtime;
}


/*
@Function  taxisDefRdate
@Title     Define the reference date

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

@Description
329
The function @func{taxisDefVdate} defines the reference date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

@EndFunction
*/
void taxisDefRdate(int taxisID, int rdate)
{
  static char func[] = "taxisDefRdate";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].rdate = rdate;
}


/*
@Function  taxisDefRtime
@Title     Define the reference time

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

@Description
353
The function @func{taxisDefVdate} defines the reference time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

@EndFunction
*/
void taxisDefRtime(int taxisID, int rtime)
{
  static char func[] = "taxisDefRtime";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].rtime = rtime;
}


/*
@Function  taxisDefCalendar
@Title     Define the calendar

@Prototype void taxisDefCalendar(int taxisID, int calendar)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
374
375
376
    @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
377
378

@Description
379
The function @func{taxisDefCalendar} defines the calendar of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451

@EndFunction
*/
void taxisDefCalendar(int taxisID, int calendar)
{
  static char func[] = "taxisDefCalendar";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].calendar = calendar;
}


void taxisDefTunit(int taxisID, int unit)
{
  static char func[] = "taxisDefTunit";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].unit = unit;
}

void taxisDefNumavg(int taxisID, int numavg)
{
  static char func[] = "taxisDefNumavg";

  taxisCheckID(func, taxisID);

  taxisTable[taxisID].numavg = numavg;
}


/*
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";

  taxisCheckID(func, taxisID);

  return (taxisTable[taxisID].type);
}


void taxisCopyTimestep(int taxisIDdes, int taxisIDsrc)
{
  static char func[] = "taxisCopyTimestep";

  taxisCheckID(func, taxisIDsrc);
  taxisCheckID(func, taxisIDdes);

  taxisTable[taxisIDdes].vdate = taxisTable[taxisIDsrc].vdate;
  taxisTable[taxisIDdes].vtime = taxisTable[taxisIDsrc].vtime;

  if ( taxisTable[taxisIDdes].has_bounds )
    {
      taxisTable[taxisIDdes].vdate_lb = taxisTable[taxisIDsrc].vdate_lb;
      taxisTable[taxisIDdes].vtime_lb = taxisTable[taxisIDsrc].vtime_lb;
      taxisTable[taxisIDdes].vdate_ub = taxisTable[taxisIDsrc].vdate_ub;
      taxisTable[taxisIDdes].vtime_ub = taxisTable[taxisIDsrc].vtime_ub;
    }
}


/*
@Function  taxisInqVdate
@Title     Get the verification date

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

@Description
455
The function @func{taxisInqVdate} returns the verification date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456
457

@Result
458
@func{taxisInqVdate} returns the verification date.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

@EndFunction
*/
int taxisInqVdate(int taxisID)
{
  static char func[] = "taxisInqVdate";

  taxisCheckID(func, taxisID);

  return (taxisTable[taxisID].vdate);
}


/*
@Function  taxisInqVtime
@Title     Get the verification time

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

@Description
481
The function @func{taxisInqVtime} returns the verification time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
483

@Result
484
@func{taxisInqVtime} returns the verification time.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503

@EndFunction
*/
int taxisInqVtime(int taxisID)
{
  static char func[] = "taxisInqVtime";

  taxisCheckID(func, taxisID);

  return (taxisTable[taxisID].vtime);
}


/*
@Function  taxisInqRdate
@Title     Get the reference date

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

@Description
507
The function @func{taxisInqRdate} returns the reference date of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508
509

@Result
510
@func{taxisInqVdate} returns the reference date.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

@EndFunction
*/
int taxisInqRdate(int taxisID)
{
  static char func[] = "taxisInqRdate";

  taxisCheckID(func, taxisID);

  if ( taxisTable[taxisID].rdate == -1 )
    {
      taxisTable[taxisID].rdate = taxisTable[taxisID].vdate;
      taxisTable[taxisID].rtime = taxisTable[taxisID].vtime;
    }

  return (taxisTable[taxisID].rdate);
}


/*
@Function  taxisInqRtime
@Title     Get the reference time

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

@Description
539
The function @func{taxisInqRtime} returns the reference time of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
541

@Result
542
@func{taxisInqVtime} returns the reference time.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

@EndFunction
*/
int taxisInqRtime(int taxisID)
{
  static char func[] = "taxisInqRtime";

  taxisCheckID(func, taxisID);

  if ( taxisTable[taxisID].rdate == -1 )
    {
      taxisTable[taxisID].rdate = taxisTable[taxisID].vdate;
      taxisTable[taxisID].rtime = taxisTable[taxisID].vtime;
    }

  return (taxisTable[taxisID].rtime);
}


/*
@Function  taxisInqCalendar
@Title     Get the calendar

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

@Description
571
The function @func{taxisInqCalendar} returns the calendar of a Time axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
573

@Result
574
@func{taxisInqCalendar} returns the type of the calendar,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
one of the set of predefined CDI calendar types.
576
577
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
578
579
580
581
582
583
584
585
586
587
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
622
623
624
625
626
627
628
629

@EndFunction
*/
int taxisInqCalendar(int taxisID)
{
  static char func[] = "taxisInqCalendar";

  taxisCheckID(func, taxisID);

  return (taxisTable[taxisID].calendar);
}


int taxisInqTunit(int taxisID)
{
  static char func[] = "taxisInqTunit";

  taxisCheckID(func, taxisID);

  return (taxisTable[taxisID].unit);
}


int taxisInqNumavg(int taxisID)
{
  static char func[] = "taxisInqNumavg";

  taxisCheckID(func, taxisID);

  return (taxisTable[taxisID].numavg);
}


TAXIS *taxisPtr(int taxisID)
{
  static char func[] = "taxisPtr";

  taxisCheckID(func, taxisID);

  return (&taxisTable[taxisID]);
}


void timeval2vtime(double timevalue, TAXIS *taxis, int *vdate, int *vtime)
{
  static char func[] = "timeval2vtime";
  int year, month, day, hour, minute;
  int rdate, rtime;
  sUnit tunit = {0, 1};
  double value, factor = 1;
  int timeunit;
  int calendar, dpy = 0;
630
  double second = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
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
  static int lwarn = TRUE;

  *vdate = 0;
  *vtime = 0;

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

  if      ( calendar == CALENDAR_360DAYS )   dpy = 360;
  else if ( calendar == CALENDAR_365DAYS )   dpy = 365;
  else if ( calendar == CALENDAR_366DAYS )   dpy = 366;
  else if ( calendar == CALENDAR_PROLEPTIC ) dpy = -1;

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

  if ( timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR )
    {
      if ( timeunit == TUNIT_YEAR ) timevalue *= 12;

      month += NINT(timevalue);

      while ( month > 12 ) { month -= 12; year++; }
      while ( month <  1 ) { month += 12; year--; }
    }
  else
    {
      switch ( timeunit )
	{
	case TUNIT_SECOND: factor =     1.0; break;
	case TUNIT_MINUTE: factor =    60.0; break;
	case TUNIT_HOUR:   factor =  3600.0; break;
	case TUNIT_DAY:    factor = 86400.0; break;
	default:
	  if ( lwarn )
	    {
	      Warning(func, "timeunit %s unsupported!", tunitNamePtr(timeunit));
	      lwarn = FALSE;
	    }
	}

      /* attention: used offset of 1 second to prevent rounding errors */
      cdiInvCalendar(dpy, year, month, day, hour, minute, 1.0, tunit, &value);

      if ( timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR )
	timevalue = value;
      else
	tunit.origin = value;

      tunit.factor = factor;

      cdiCalendar(dpy, timevalue, tunit, &year, &month, &day, &hour, &minute, &second);
    }

  *vdate = year*10000 + month*100 + day;
691
  *vtime = hour*100 + minute /* + (int) (second/60+0.5)*/;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
}


double vtime2timeval(int vdate, int vtime, TAXIS *taxis)
{
  static char func[] = "vtime2timeval";
  int ryear, rmonth;
  int year, month, day, hour, minute;
  int rdate, rtime;
  sUnit tunit = {0, 1};
  double value;
  int timeunit;
  int calendar, dpy = 0;
  static int lwarn = TRUE;

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

  if      ( calendar == CALENDAR_360DAYS ) dpy = 360;
  else if ( calendar == CALENDAR_365DAYS ) dpy = 365;
  else if ( calendar == CALENDAR_366DAYS ) dpy = 366;
  else if ( calendar == CALENDAR_PROLEPTIC ) dpy = -1;

  rdate  = (*taxis).rdate;
  rtime  = (*taxis).rtime;
  if ( rdate == -1 )
    {
      rdate  = (*taxis).vdate;
      rtime  = (*taxis).vtime;
    }

  ryear  =  rdate / 10000;
  rmonth = (rdate - ryear*10000) / 100;
  day    =  rdate - ryear*10000 - rmonth*100;
  hour   =  rtime / 100;
  minute =  rtime - hour*100;

  cdiInvCalendar(dpy, ryear, rmonth, day, hour, minute, 0.0, tunit, &value);
  tunit.origin = value;
  tunit.factor = 60.0;
      
  year   =  vdate / 10000;
  month  = (vdate - year*10000) / 100;
  day    =  vdate - year*10000 - month*100;
  hour   =  vtime / 100;
  minute =  vtime - hour*100;

  if ( timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR )
    {
      value = (year-ryear)*12 - rmonth + month;
      if ( timeunit == TUNIT_YEAR ) value = NINT(value/12);
    }
  else
    {
      switch ( timeunit )
	{
	case TUNIT_SECOND: tunit.factor =      1.0; break;
	case TUNIT_MINUTE: tunit.factor =     60.0; break;
	case TUNIT_HOUR:   tunit.factor =   3600.0; break;
	case TUNIT_DAY:    tunit.factor =  86400.0; break;
	default:
	  if ( lwarn )
	    {
	      Warning(func, "timeunit %s unsupported!", tunitNamePtr(timeunit));
	      lwarn = FALSE;
	    }
	}

      cdiInvCalendar(dpy, year, month, day, hour, minute, 0.0, tunit, &value);
    }

  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;
      vtime = (int) ((timevalue - vdate)*1440 + 0.01);

      hour   = vtime / 60;
      minute = vtime - hour*60;
      vtime  = hour*100 + minute;
    }
  else if ( timeunit == TUNIT_DAY )
    {
      vdate = (int) timevalue;
      vtime = (int) ((timevalue - vdate)*1440 + 0.01);

      hour   = vtime / 60;
      minute = vtime - hour*60;
      vtime  = hour*100 + minute;
    }
  else if ( timeunit == TUNIT_MONTH )
    {
      vdate = (int) timevalue*100;
      vtime = 0;
    }
  else
    {
      Message(func, "Unsupported TIMEUNIT %s!", tunitNamePtr(timeunit));
    }

  *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 )
	{
	  timevalue = date/100 + 0.5;
	}
      else
	{
	  int hour   = time / 100;
	  int minute = time - hour*100;
	  timevalue = date + (hour*60 + minute)/1440.;
	}
    }
  else
    timevalue = vtime2timeval(date, time, taxis);

  return (timevalue);
}