-
Notifications
You must be signed in to change notification settings - Fork 1
/
latstime.c
1098 lines (1011 loc) · 28.1 KB
/
latstime.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
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
630
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
691
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*-Mode: C;-*-
* Module: LATS time functions
*
* Copyright: 1996, Regents of the University of California
* This software may not be distributed to others without
* permission of the author.
*
* Author: Bob Drach, Lawrence Livermore National Laboratory
*
* Version: $Id: latstime.c,v 1.5 1996/10/22 19:05:13 fiorino Exp $
*
* Revision History:
*
* $Log: latstime.c,v $
* Revision 1.5 1996/10/22 19:05:13 fiorino
* latsgrib bug in .ctl creator
*
* Revision 1.4 1996/08/27 19:44:26 drach
* - Fixed up minor compiler warnings
*
* Revision 1.3 1996/06/27 01:11:12 drach
* - Check for POSIX compliance
*
* Revision 1.2 1996/05/03 18:48:44 drach
* - Added CDMS time routines
*
* Revision 1.1 1996/04/25 23:35:07 drach
* - Initial repository version
*
*
*/
#define _POSIX_SOURCE 1
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "latsint.h"
#include "latstime.h"
#define CD_DEFAULT_BASEYEAR "1979" /* Default base year for relative time (no 'since' clause) */
#define ISLEAP(year,timeType) (((timeType) & CdHasLeap) && (!((year) % 4) && (((timeType) & CdJulianType) || (((year) % 100) || !((year) % 400)))))
static int mon_day_cnt[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
static int days_sum[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
/* Return -1, 0, or 1 iff t1 is less than, equal to, */
/* or greater than t2. */
int latsTimeCmp(latsCompTime t1, latsCompTime t2){
if(t1.year < t2.year) {
return -1;
} else if( t1.year > t2.year) {
return 1;
} else if (t1.year == t2.year) {
if(t1.month < t2.month) {
return -1;
} else if(t1.month > t2.month) {
return 1;
} else if (t1.month == t2.month) {
if(t1.day < t2.day) {
return -1;
} else if(t1.day > t2.day) {
return 1;
} else if (t1.day == t2.day) {
if(t1.hour < t2.hour) {
return -1;
} else if(t1.hour > t2.hour) {
return 1;
} else {
return 0;
}
}
}
} else if(t1.year == t2.year &&
t1.month == t2.month &&
t1.day == t2.day &&
t1.hour == t2.hour) {
return 0;
} else {
return 1;
}
}
/* Compute month and day from year and day-of-year.
*
* Input:
* doy (int) (day-of-year)
* date->year (long) (year since 0 BC)
* date->timeType (CdTimetype) (time type)
* date->baseYear base year for relative times
* Output:
* date->month (short) (month in year)
* date->day (short) (day in month)
*
*
* Derived from NRL NEONS V3.6.
*/
void
latsMonthDay(int *doy, CdTime *date)
{
int i; /* month counter */
int idoy; /* day of year counter */
long year;
if ((idoy = *doy) < 1) {
date->month = 0;
date->day = 0;
return;
}
if(!(date->timeType & CdChronCal)) /* Ignore year for Clim calendar */
year = 0;
else if(!(date->timeType & CdBase1970)) /* year is offset from base for relative time */
year = date->baseYear + date->year;
else
year = date->year;
if (ISLEAP(year,date->timeType)) {
mon_day_cnt[1] = 29;
} else {
mon_day_cnt[1] = 28;
}
date->month = 0;
for (i = 0; i < 12; i++) {
(date->month)++;
date->day = idoy;
if ((idoy -= ((date->timeType & Cd365) ? (mon_day_cnt[date->month-1]) : 30)) <= 0) {
return;
}
}
return;
}
/* Compute number of days in a month
*
* Input:
* date->year (long) (year since 0 BC)
* date->month (short) (month in year)
* date->timeType (CdTimetype) (time type)
* date->baseYear base year for relative times
*
* Output:
* days (short) (number of days in month)
*
*/
void
latsDaysInMonth(CdTime *date, int *days)
{
long year;
if(!(date->timeType & CdChronCal)) /* Ignore year for Clim calendar */
year = 0;
else if(!(date->timeType & CdBase1970)) /* year is offset from base for relative time */
year = date->baseYear + date->year;
else
year = date->year;
if (ISLEAP(year,date->timeType)) {
mon_day_cnt[1] = 29;
} else {
mon_day_cnt[1] = 28;
}
*days = (date->timeType & Cd365) ? (mon_day_cnt[date->month-1]) : 30;
return;
}
/* Compute day-of-year from year, month and day
*
* Input:
* date->year (long) (year since 0 BC)
* date->month (short) (month in year)
* date->day (short) (day in month)
* date->baseYear base year for relative times
* Output: doy (int) (day-of-year)
*
* Derived from NRL NEONS V3.6
*/
void
latsDayOfYear(CdTime *date, int *doy)
{
int leap_add = 0; /* add 1 day if leap year */
int month; /* month */
long year;
month = date->month;
if (month < 1 || month > 12) {
latsError( "Day-of-year error; month: %d\n", month);
month = 1;
}
if(!(date->timeType & CdChronCal)) /* Ignore year for Clim calendar */
year = 0;
else if(!(date->timeType & CdBase1970)) /* year is offset from base for relative time */
year = date->baseYear + date->year;
else
year = date->year;
if (ISLEAP(year,date->timeType) && month > 2) leap_add = 1;
*doy = ((date->timeType & Cd365) ? (days_sum[month-1]) : 30*(month-1)) + date->day + leap_add;
return;
}
/* Convert human time to epochal time (hours since 00 jan 1, 1970)
*
* Input: htime = human time representation
*
* Output: etime = epochal time representation
*
* Derived from NRL Neons V3.6
*/
void
latsh2e(CdTime *htime, double *etime)
{
long ytemp, year; /* temporary year holder */
int day_cnt; /* count of days */
int doy; /* day of year */
long baseYear; /* base year for epochal time */
int daysInLeapYear; /* number of days in a leap year */
int daysInYear; /* days in non-leap year */
extern void latsDayOfYear(CdTime *date, int *doy);
latsDayOfYear(htime,&doy);
day_cnt = 0;
baseYear = ((htime->timeType) & CdBase1970) ? 1970 : htime->baseYear;
year = ((htime->timeType) & CdBase1970) ? htime->year : (htime->year + htime->baseYear);
if(!((htime->timeType) & CdChronCal)) baseYear = year = 0; /* set year and baseYear to 0 for Clim */
daysInLeapYear = ((htime->timeType) & Cd365) ? 366 : 360;
daysInYear = ((htime->timeType) & Cd365) ? 365 : 360;
if (year > baseYear) {
for (ytemp = year - 1; ytemp >= baseYear; ytemp--) {
day_cnt += ISLEAP(ytemp,htime->timeType) ? daysInLeapYear : daysInYear;
}
} else if (year < baseYear) {
for (ytemp = year; ytemp < baseYear; ytemp++) {
day_cnt -= ISLEAP(ytemp,htime->timeType) ? daysInLeapYear : daysInYear;
}
}
*etime = (double) (day_cnt + doy - 1) * 24. + htime->hour;
return;
}
/* Convert epochal time (hours since 00 jan 1, 1970)
* to human time (structured)
*
* Input:
* etime = epochal time representation
* timeType = time type (e.g., CdChron, CdClim, etc.) as defined in cdms.h
* baseYear = base real, used for relative time types only
*
* Output: htime = human (structured) time representation
*
* Derived from NRL Neons V3.6
*/
void
latse2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime)
{
long ytemp; /* temporary year holder */
int yr_day_cnt; /* count of days in year */
int doy; /* day of year */
int daysInLeapYear; /* number of days in a leap year */
int daysInYear; /* days in non-leap year */
extern void latsMonthDay(int *doy, CdTime *date);
doy = (long) floor(etime / 24.) + 1;
htime->hour = etime - (double) (doy - 1) * 24.;
htime->baseYear = (timeType & CdBase1970) ? 1970 : baseYear;
if(!(timeType & CdChronCal)) htime->baseYear = 0; /* Set base year to 0 for Clim */
daysInLeapYear = (timeType & Cd365) ? 366 : 360;
daysInYear = (timeType & Cd365) ? 365 : 360;
if (doy > 0) {
for (ytemp = htime->baseYear; ; ytemp++) {
yr_day_cnt = ISLEAP(ytemp,timeType) ? daysInLeapYear : daysInYear;
if (doy <= yr_day_cnt) break;
doy -= yr_day_cnt;
}
} else {
for (ytemp = htime->baseYear-1; ; ytemp--) {
yr_day_cnt = ISLEAP(ytemp,timeType) ? daysInLeapYear : daysInYear;
doy += yr_day_cnt;
if (doy > 0) break;
}
}
htime->year = (timeType & CdBase1970) ? ytemp : (ytemp - htime->baseYear);
if(!(timeType & CdChronCal)) htime->year = 0; /* Set year to 0 for Clim */
htime->timeType = timeType;
latsMonthDay(&doy,htime);
return;
}
/* Convert character time to human time
*
* Input:
* ctime = character time
* timeType = time type (e.g. CdChron) as defined in cdms.h
*
* Output:
* htime = human (structured) time
*/
void
latsc2h(char *ctime, CdTimeType timeType, CdTime *htime)
{
int iyear, imon, iday, ihour, imin;
double dsec;
long baseYear;
switch(timeType){
case CdChron: case CdChronNoLeap: case CdChron360:
sscanf(ctime,"%ld/%hd/%hd %d:%d:%lf",&htime->year,&htime->month,
&htime->day,&ihour,&imin,&dsec);
htime->hour = (double)ihour + (double)imin/60. + dsec/3600;
htime->baseYear = 1970;
htime->timeType = timeType;
break;
case CdRel: case CdRelNoLeap:
sscanf(ctime,"%ld+%ld/%hd/%hd %d:%d:%lf",&htime->baseYear,
&htime->year,&htime->month,&htime->day,&ihour,&imin,&dsec);
htime->hour = (double)ihour + (double)imin/60. + dsec/3600;
htime->timeType = timeType;
break;
case CdClim:
sscanf(ctime,"%hd/%hd %d:%d:%lf",&htime->month,&htime->day,
&ihour,&imin,&dsec);
htime->hour = (double)ihour + (double)imin/60. + dsec/3600;
htime->year = 0;
htime->baseYear = 0;
htime->timeType = timeType;
break;
default:
latsError("Invalid time type: %d\n",timeType);
}
return;
}
/* Convert human (structured) time to character time.
*
* Input:
* htime = human time
*
* Output:
* ctime = character time
*/
void
latsh2c(CdTime *htime, char *ctime)
{
int ihour, imin;
double dmin, dsec;
ihour = (int) htime->hour;
dmin = (htime->hour - (double)ihour) * 60.0;
imin = (int) dmin;
dsec = (dmin - (double)imin) * 60.0;
switch(htime->timeType){
case CdChron: case CdChronNoLeap: case CdChron360:
sprintf(ctime,"%ld/%hd/%hd %d:%d:%.1f",htime->year,htime->month,
htime->day,ihour,imin,dsec);
break;
case CdRel: case CdRelNoLeap:
sprintf(ctime,"%ld+%ld/%hd/%hd %d:%d:%.1f",htime->baseYear,
htime->year,htime->month,htime->day,ihour,imin,dsec);
break;
case CdClim:
sprintf(ctime,"%hd/%hd %d:%d:%.1f",htime->month,htime->day,
ihour,imin,dsec);
break;
default:
latsError("Invalid time type: %d\n",htime->timeType);
}
return;
}
/* Convert character time to epochal time (hours since 00 jan 1, 1970)
*
* Input:
* ctime = character time
* timeType = time type (e.g. CdChron) as defined in cdms.h
*
* Output:
* etime = epochal time
*/
void
latsc2e(char *ctime, CdTimeType timeType, double *etime, long *baseYear)
{
CdTime htime;
extern void latsc2h(char *ctime, CdTimeType timeType, CdTime *htime);
extern void latsh2e(CdTime *htime, double *etime);
latsc2h(ctime,timeType,&htime);
latsh2e(&htime,etime);
*baseYear = htime.baseYear;
return;
}
/* Convert epochal time (hours since 00 jan 1, 1970) to character time
*
* Input:
* etime = epochal time
* timeType = time type, (e.g., CdChron) as defined in cdms.h
* baseYear = base year, used for relative time only
*
* Output:
* ctime = character time
*/
void
latse2c(double etime, CdTimeType timeType, long baseYear, char *ctime)
{
CdTime htime;
extern void latse2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime);
extern void latsh2c(CdTime *htime, char *ctime);
latse2h(etime,timeType,baseYear,&htime);
latsh2c(&htime,ctime);
return;
}
/* Add 'nDel' times 'delTime' to epochal time 'begEtm',
* return the result in epochal time 'endEtm'.
*/
void
latsAddDelTime(double begEtm, long nDel, CdDeltaTime delTime, CdTimeType timeType,
long baseYear, double *endEtm)
{
double delHours;
long delMonths, delYears;
CdTime bhtime, ehtime;
extern void latse2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime);
extern void latsh2e(CdTime *htime, double *etime);
switch(delTime.units){
case CdYear:
delMonths = 12;
break;
case CdSeason:
delMonths = 3;
break;
case CdMonth:
delMonths = 1;
break;
case CdWeek:
delHours = 168.0;
break;
case CdDay:
delHours = 24.0;
break;
case CdHour:
delHours = 1.0;
break;
case CdMinute:
delHours = 1./60.;
break;
case CdSecond:
delHours = 1./3600.;
break;
default:
latsError("Invalid delta time units: %d\n",delTime.units);
return;
}
switch(delTime.units){
case CdYear: case CdSeason: case CdMonth:
latse2h(begEtm,timeType,baseYear,&bhtime);
delMonths = delMonths * nDel * delTime.count + bhtime.month - 1;
delYears = (delMonths >= 0 ? (delMonths/12) : (delMonths+1)/12 - 1);
ehtime.year = bhtime.year + delYears;
ehtime.month = delMonths - (12 * delYears) + 1;
ehtime.day = 1;
ehtime.hour = 0.0;
ehtime.timeType = timeType;
ehtime.baseYear = !(timeType & CdChronCal) ? 0 :
(timeType & CdBase1970) ? 1970 : baseYear; /* base year is 0 for Clim, */
/* 1970 for Chron, */
/* or input base year for Rel */
latsh2e(&ehtime,endEtm);
break;
case CdWeek: case CdDay: case CdHour: case CdMinute: case CdSecond:
delHours *= (nDel * delTime.count);
*endEtm = begEtm + delHours;
break;
}
return;
}
/* Divide ('endEtm' - 'begEtm') by 'delTime',
* return the integer portion of the result in 'nDel'.
*/
void
latsDivDelTime(double begEtm, double endEtm, CdDeltaTime delTime, CdTimeType timeType,
long baseYear, long *nDel)
{
double delHours, frange;
long delMonths, range;
CdTime bhtime, ehtime;
int hoursInYear;
extern void latse2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime);
switch(delTime.units){
case CdYear:
delMonths = 12;
break;
case CdSeason:
delMonths = 3;
break;
case CdMonth:
delMonths = 1;
break;
case CdWeek:
delHours = 168.0;
break;
case CdDay:
delHours = 24.0;
break;
case CdHour:
delHours = 1.0;
break;
case CdMinute:
delHours = 1./60.;
break;
case CdSecond:
delHours = 1./3600.;
break;
default:
latsError("Invalid delta time units: %d\n",delTime.units);
return;
}
switch(delTime.units){
case CdYear: case CdSeason: case CdMonth:
delMonths *= delTime.count;
latse2h(begEtm,timeType,baseYear,&bhtime);
latse2h(endEtm,timeType,baseYear,&ehtime);
if(timeType & CdChronCal){ /* Chron and Rel time */
range = 12*(ehtime.year - bhtime.year)
+ (ehtime.month - bhtime.month);
}
else{ /* Clim time, ignore year */
range = (ehtime.month - bhtime.month);
if(range < 0) range += 12;
}
*nDel = abs(range)/delMonths;
break;
case CdWeek: case CdDay: case CdHour: case CdMinute: case CdSecond:
delHours *= (double)delTime.count;
if(timeType & CdChronCal){ /* Chron and Rel time */
frange = fabs(endEtm - begEtm);
}
else{ /* Clim time, ignore year, but */
/* wraparound relative to hours-in-year*/
frange = endEtm - begEtm;
hoursInYear = (timeType & Cd365) ? 8760. : 8640.;
/* Normalize frange to interval [0,hoursInYear) */
if(frange < 0.0 || frange >= hoursInYear)
frange -= hoursInYear * floor(frange/hoursInYear);
}
*nDel = (frange + 1.e-10*delHours)/delHours;
break;
}
return;
}
/* Validate the component time, return 0 if valid, 1 if not */
int
latsValidateTime(cdCalenType timetype, cdCompTime comptime)
{
if(comptime.month<1 || comptime.month>12){
latsError("Error on time conversion: invalid month = %hd\n",comptime.month);
return 1;
}
if(comptime.day<1 || comptime.day>31){
latsError("Error on time conversion: invalid day = %hd\n",comptime.day);
return 1;
}
if(comptime.hour<0.0 || comptime.hour>24.0){
latsError("Error on time conversion: invalid hour = %lf\n",comptime.hour);
return 1;
}
return 0;
}
/* Trim trailing whitespace, up to n characters. */
/* If no whitespace up to the last character, set */
/* the last character to null, else set the first */
/* whitespace character to null. */
void
latsTrim(char* s, int n)
{
char* c;
if(s==NULL)
return;
for(c=s; *c && c<s+n-1 && !isspace(*c); c++);
*c='\0';
return;
}
/* Map to old timetypes */
int
latsToOldTimetype(cdCalenType newtype, CdTimeType* oldtype)
{
switch(newtype){
case cdStandard:
*oldtype = CdChron;
break;
case cdJulian:
*oldtype = CdJulianCal;
break;
case cdNoLeap:
*oldtype = CdChronNoLeap;
break;
case cd360:
*oldtype = CdChron360;
break;
case cdClim:
*oldtype = CdClim;
break;
case cdClimLeap:
*oldtype = CdClimLeap;
break;
case cdClim360:
*oldtype = CdClim360;
break;
default:
latsError("Error on relative units conversion, invalid timetype = %d",newtype);
return 1;
}
return 0;
}
/* Parse relative units, returning the unit and base component time. */
/* Function returns 1 if error, 0 on success */
int
latsParseRelunits(cdCalenType timetype, char* relunits, cdUnitTime* unit, cdCompTime* base_comptime)
{
char charunits[CD_MAX_RELUNITS];
char basetime_1[CD_MAX_CHARTIME];
char basetime_2[CD_MAX_CHARTIME];
char basetime[CD_MAX_CHARTIME];
double factor;
CdTime humantime;
int nconv;
CdTimeType old_timetype;
/* Parse the relunits */
nconv = sscanf(relunits,"%s since %s %s",charunits,basetime_1,basetime_2);
if(nconv==EOF || nconv==0){
latsError("Error on relative units conversion, string = %s\n",relunits);
return 1;
}
/* Get the units */
latsTrim(charunits,CD_MAX_RELUNITS);
if(!strncmp(charunits,"sec",3) || !strcmp(charunits,"s")){
*unit = cdSecond;
}
else if(!strncmp(charunits,"min",3) || !strcmp(charunits,"mn")){
*unit = cdMinute;
}
else if(!strncmp(charunits,"hour",4) || !strcmp(charunits,"hr")){
*unit = cdHour;
}
else if(!strncmp(charunits,"day",3) || !strcmp(charunits,"dy")){
*unit = cdDay;
}
else if(!strncmp(charunits,"week",4) || !strcmp(charunits,"wk")){
*unit = cdWeek;
}
else if(!strncmp(charunits,"month",5) || !strcmp(charunits,"mo")){
*unit = cdMonth;
}
else if(!strncmp(charunits,"season",6)){
*unit = cdSeason;
}
else if(!strncmp(charunits,"year",4) || !strcmp(charunits,"yr")){
if(!(timetype & cdStandardCal)){
latsError("Error on relative units conversion: climatological units cannot be 'years'.\n");
return 1;
}
*unit = cdYear;
}
else {
latsError("Error on relative units conversion: invalid units = %s\n",charunits);
return 1;
}
/* Build the basetime, if any (default is 1979), */
/* or month 1 for climatological time. */
if(nconv == 1){
if(timetype & cdStandardCal)
strcpy(basetime,CD_DEFAULT_BASEYEAR);
else
strcpy(basetime,"1");
}
/* Convert the basetime to component, then epochal (hours since 1970) */
else{
if(nconv == 2){
latsTrim(basetime_1,CD_MAX_CHARTIME);
strcpy(basetime,basetime_1);
}
else{
latsTrim(basetime_1,CD_MAX_CHARTIME);
latsTrim(basetime_2,CD_MAX_CHARTIME);
sprintf(basetime,"%s %s",basetime_1,basetime_2);
}
}
latsChar2Comp(timetype, basetime, base_comptime);
return 0;
}
/* Parse delta time. Return 0 if success, 1 on error. */
int
latsParseDeltaTime(cdCalenType timetype, char* deltaTime, double* value, cdUnitTime* unit){
char charunits[CD_MAX_TIME_DELTA];
int nconv;
nconv = sscanf(deltaTime,"%lf %s",value,charunits);
if(nconv==EOF || nconv==0){
latsError("Error on delta time conversion, string = %s",deltaTime);
return 1;
}
latsTrim(charunits,CD_MAX_TIME_DELTA);
if(!strncmp(charunits,"sec",3) || !strcmp(charunits,"s")){
*unit = cdSecond;
}
else if(!strncmp(charunits,"min",3) || !strcmp(charunits,"mn")){
*unit = cdMinute;
}
else if(!strncmp(charunits,"hour",4) || !strcmp(charunits,"hr")){
*unit = cdHour;
}
else if(!strncmp(charunits,"day",3) || !strcmp(charunits,"dy")){
*unit = cdDay;
}
else if(!strncmp(charunits,"week",4) || !strcmp(charunits,"wk")){
*unit = cdWeek;
}
else if(!strncmp(charunits,"month",5) || !strcmp(charunits,"mo")){
*unit = cdMonth;
}
else if(!strncmp(charunits,"season",6)){
*unit = cdSeason;
}
else if(!strncmp(charunits,"year",4) || !strcmp(charunits,"yr")){
if(!(timetype & cdStandardCal)){
latsError("Error on delta time conversion: climatological units cannot be 'years'.");
return 1;
}
*unit = cdYear;
}
else {
latsError("Error on delta time conversion: invalid units = %s",charunits);
return 1;
}
return 0;
}
void
latsChar2Comp(cdCalenType timetype, char* chartime, cdCompTime* comptime)
{
double hour, sec;
int ihr, imin, nconv;
long year;
short day;
short month;
comptime->year = CD_NULL_YEAR;
comptime->month = CD_NULL_MONTH;
comptime->day = CD_NULL_DAY;
comptime->hour = CD_NULL_HOUR;
if(timetype & cdStandardCal){
nconv = sscanf(chartime,"%ld-%hd-%hd %d:%d:%lf",&year,&month,&day,&ihr,&imin,&sec);
if(nconv==EOF || nconv==0){
latsError("Error on character time conversion, string = %s\n",chartime);
return;
}
if(nconv >= 1){
comptime->year = year;
}
if(nconv >= 2){
comptime->month = month;
}
if(nconv >= 3){
comptime->day = day;
}
if(nconv >= 4){
if(ihr<0 || ihr>23){
latsError("Error on character time conversion: invalid hour = %d\n",ihr);
return;
}
comptime->hour = (double)ihr;
}
if(nconv >= 5){
if(imin<0 || imin>59){
latsError("Error on character time conversion: invalid minute = %d\n",imin);
return;
}
comptime->hour += (double)imin/60.;
}
if(nconv >= 6){
if(sec<0.0 || sec>60.0){
latsError("Error on character time conversion: invalid second = %lf\n",sec);
return;
}
comptime->hour += sec/3600.;
}
}
else{ /* Climatological */
nconv = sscanf(chartime,"%hd-%hd %d:%d:%lf",&month,&day,&ihr,&imin,&sec);
if(nconv==EOF || nconv==0){
latsError("Error on character time conversion, string = %s",chartime);
return;
}
if(nconv >= 1){
comptime->month = month;
}
if(nconv >= 2){
comptime->day = day;
}
if(nconv >= 3){
if(ihr<0 || ihr>23){
latsError("Error on character time conversion: invalid hour = %d\n",ihr);
return;
}
comptime->hour = (double)ihr;
}
if(nconv >= 4){
if(imin<0 || imin>59){
latsError("Error on character time conversion: invalid minute = %d\n",imin);
return;
}
comptime->hour += (double)imin/60.;
}
if(nconv >= 5){
if(sec<0.0 || sec>60.0){
latsError("Error on character time conversion: invalid second = %lf\n",sec);
return;
}
comptime->hour += sec/3600.;
}
}
(void)latsValidateTime(timetype,*comptime);
return;
}
void
latsChar2Rel(cdCalenType timetype, char* chartime, char* relunits, double* reltime)
{
cdCompTime comptime;
latsChar2Comp(timetype, chartime, &comptime);
latsComp2Rel(timetype, comptime, relunits, reltime);
return;
}
void
latsComp2Char(cdCalenType timetype, cdCompTime comptime, char* time)
{
double dtmp, sec;
int ihr, imin;
int nskip;
if(latsValidateTime(timetype,comptime))
return;
ihr = (int)comptime.hour;
dtmp = 60.0 * (comptime.hour - (double)ihr);
imin = (int)dtmp;
sec = 60.0 * (dtmp - (double)imin);
nskip = 0;
if(sec == 0.0){
if(imin == 0)
nskip = 2;
else
nskip = 1;
}
if(timetype & cdStandardCal){
if(nskip == 0)
sprintf(time,"%ld-%hd-%hd %d:%d:%lf",comptime.year,comptime.month,comptime.day,ihr,imin,sec);
else if(nskip == 1)
sprintf(time,"%ld-%hd-%hd %d:%d",comptime.year,comptime.month,comptime.day,ihr,imin);
else
sprintf(time,"%ld-%hd-%hd %d:0",comptime.year,comptime.month,comptime.day,ihr);
}
else { /* Climatological */
if(nskip == 0)
sprintf(time,"%hd-%hd %d:%d:%lf",comptime.month,comptime.day,ihr,imin,sec);
else if(nskip == 1)
sprintf(time,"%hd-%hd %d:%d",comptime.month,comptime.day,ihr,imin);
else
sprintf(time,"%hd-%hd %d:0",comptime.month,comptime.day,ihr);
}
return;
}
void
latsComp2Rel(cdCalenType timetype, cdCompTime comptime, char* relunits, double* reltime)
{
cdCompTime base_comptime;
CdDeltaTime deltime;
CdTime humantime;
CdTimeType old_timetype;
cdUnitTime unit;
double base_etm, etm, delta;
long ndel, hoursInYear;
/* Parse the relunits */
if(latsParseRelunits(timetype, relunits, &unit, &base_comptime))
return;
/* Convert basetime to epochal */
humantime.year = base_comptime.year;
humantime.month = base_comptime.month;
humantime.day = base_comptime.day;
humantime.hour = base_comptime.hour;
humantime.baseYear = 1970;
/* Map to old-style timetype */
if(latsToOldTimetype(timetype,&old_timetype))
return;
humantime.timeType = old_timetype;
latsh2e(&humantime,&base_etm);
/* Map end time to epochal */
humantime.year = comptime.year;
humantime.month = comptime.month;
humantime.day = comptime.day;
humantime.hour = comptime.hour;
latsh2e(&humantime,&etm);
/* Calculate relative time value for months or hours */
deltime.count = 1;
deltime.units = (CdTimeUnit)unit;
switch(unit){
case cdWeek: case cdDay: case cdHour: case cdMinute: case cdSecond:
delta = etm - base_etm;
if(!(timetype & cdStandardCal)){ /* Climatological time */
hoursInYear = (timetype & cd365Days) ? 8760. : (timetype & cdHasLeap) ? 8784. : 8640.;
/* Normalize delta to interval [0,hoursInYear) */
if(delta < 0.0 || delta >= hoursInYear)
delta -= hoursInYear * floor(delta/hoursInYear);
}
break;
case cdYear: case cdSeason: case cdMonth:
latsDivDelTime(base_etm, etm, deltime, old_timetype, 1970, &ndel);
break;
}
/* Convert to output units */
switch(unit){
case cdSecond:
*reltime = 3600.0 * delta;
break;
case cdMinute:
*reltime = 60.0 * delta;
break;
case cdHour:
*reltime = delta;
break;
case cdDay:
*reltime = delta/24.0;
break;
case cdWeek:
*reltime = delta/168.0;
break;
case cdMonth: case cdSeason: case cdYear: /* Already in correct units */
if(timetype & cdStandardCal)
*reltime = (base_etm <= etm) ? (double)ndel : (double)(-ndel);
else /* Climatological time is already normalized*/
*reltime = (double)ndel;
break;
}
return;
}
void
latsRel2Char(cdCalenType timetype, char* relunits, double reltime, char* chartime)
{
cdCompTime comptime;
latsRel2Comp(timetype, relunits, reltime, &comptime);
latsComp2Char(timetype, comptime, chartime);
return;
}
void
latsRel2Comp(cdCalenType timetype, char* relunits, double reltime, cdCompTime* comptime)