-
Notifications
You must be signed in to change notification settings - Fork 95
/
time_manager.F90
3995 lines (3300 loc) · 140 KB
/
time_manager.F90
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
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the GFDL Flexible Modeling System (FMS).
!*
!* FMS is free software: you can redistribute it and/or modify it under
!* the terms of the GNU Lesser General Public License as published by
!* the Free Software Foundation, either version 3 of the License, or (at
!* your option) any later version.
!*
!* FMS is distributed in the hope that it will be useful, but WITHOUT
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
!* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
!* for more details.
!*
!* You should have received a copy of the GNU Lesser General Public
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************
module time_manager_mod
! <CONTACT EMAIL="fms@gfdl.noaa.gov">
! fms
! </CONTACT>
! <HISTORY SRC="http://www.gfdl.noaa.gov/fms-cgi-bin/cvsweb.cgi/FMS/"/>
! <OVERVIEW>
! A software package that provides a set of simple interfaces for
! modelers to perform computations related to time and dates.
! </OVERVIEW>
! <DESCRIPTION>
! The changes between the lima revision and this revision are more
! extensive that all those between antwerp and lima.
! A brief description of these changes follows.
!
! 1) Added option to set the smallest time increment to something less than one second.
! This is controlled by calling the pubic subroutine set_ticks_per_second.
!
! 2) Gregorian calendar fixed.
!
! 3) Optional error flag added to calling arguments of public routines.
! This allows the using routine to terminate the program. It is likely that more
! diagnostic information is available from the user than from time_manager alone.
! If the error flag is present then it is the responsibility of the using
! routine to test it and add additional information to the error message.
!
! 4) Removed the restriction that time increments be positive in routines that increment or decrement
! time and date. The option to prohibit negative increments can be turned on via optional argument.
!
! 5) subroutine set_date_c modified to handle strings that include only hours or only hours and minutes.
! This complies with CF convensions.
!
! 6) Made calendar specific routines private.
! They are not used, and should not be used, by any using code.
!
! 7) Error messages made more informative.
!
! The module defines a type that can be used to represent discrete
! times (accurate to one second) and to map these times into dates
! using a variety of calendars. A time is mapped to a date by
! representing the time with respect to an arbitrary base date (refer
! to <B>NOTES</B> section for the <LINK SRC="#base date">base date</LINK> setting).
!
! The time_manager provides a single defined type, time_type, which is
! used to store time and date quantities. A time_type is a positive
! definite quantity that represents an interval of time. It can be
! most easily thought of as representing the number of seconds in some
! time interval. A time interval can be mapped to a date under a given
! calendar definition by using it to represent the time that has passed
! since some base date. A number of interfaces are provided to operate
! on time_type variables and their associated calendars. Time intervals
! can be as large as n days where n is the largest number represented by
! the default integer type on a compiler. This is typically considerably
! greater than 10 million years (assuming 32 bit integer representation)
! which is likely to be adequate for most applications. The description
! of the interfaces is separated into two sections. The first deals with
! operations on time intervals while the second deals with operations
! that convert time intervals to dates for a given calendar.
! The smallest increment of time is referred to as a tick.
! A tick cannot be larger than 1 second, which also is the default.
! The number of ticks per second is set via pubic subroutine set_ticks_per_second.
! For example, ticks_per_second = 1000 will set the tick to one millisecond.
! </DESCRIPTION>
! <DATA NAME="time_type" TYPE="derived type">
! Derived-type data variable used to store time and date quantities. It
! contains three PRIVATE variables: days, seconds and ticks.
! </DATA>
#include <fms_platform.h>
use constants_mod, only: rseconds_per_day=>seconds_per_day
use fms_mod, only: error_mesg, FATAL, WARNING, write_version_number, stdout
implicit none
private
! Module defines a single type
public time_type
! Operators defined on time_type
public operator(+), operator(-), operator(*), operator(/), &
operator(>), operator(>=), operator(==), operator(/=), &
operator(<), operator(<=), operator(//), assignment(=)
! Subroutines and functions operating on time_type
public set_time, increment_time, decrement_time, get_time, interval_alarm
public repeat_alarm, time_type_to_real, real_to_time_type
public time_list_error
! List of available calendar types
public THIRTY_DAY_MONTHS, JULIAN, GREGORIAN, NOLEAP, NO_CALENDAR, INVALID_CALENDAR
! Subroutines and functions involving relations between time and calendar
public set_calendar_type
public get_calendar_type
public set_ticks_per_second
public get_ticks_per_second
public set_date
public get_date
public increment_date
public decrement_date
public days_in_month
public leap_year
public length_of_year
public days_in_year
public month_name
public valid_calendar_types
! Subroutines for printing version number and time type
public :: time_manager_init, print_time, print_date
! The following exist only for interpolator.F90
! interpolator.F90 uses them to do a calendar conversion,
! which is also done by get_cal_time. interpolator.F90
! should be modified to use get_cal_time instead.
! After interpolator.F90 is fixed, these can be removed
! and the corresponding private routines can be renamed.
! (e.g., rename set_date_julian_private to be just set_date_julian)
public :: set_date_julian, set_date_no_leap, get_date_julian, get_date_no_leap
public :: date_to_string
!====================================================================
! Global data to define calendar type
integer, parameter :: THIRTY_DAY_MONTHS = 1, JULIAN = 2, &
GREGORIAN = 3, NOLEAP = 4, &
NO_CALENDAR = 0, INVALID_CALENDAR =-1
integer, private :: calendar_type = NO_CALENDAR
integer, parameter :: max_type = 4
! Define number of days per month
integer, private :: days_per_month(12) = (/31,28,31,30,31,30,31,31,30,31,30,31/)
integer, parameter :: seconds_per_day = rseconds_per_day ! This should automatically cast real to integer
integer, parameter :: days_in_400_year_period = 146097 ! Used only for gregorian
integer, dimension(days_in_400_year_period) :: coded_date ! Used only for gregorian
integer, dimension(400,12,31) :: date_to_day ! Used only for gregorian
integer, parameter :: invalid_date=-1 ! Used only for gregorian
! time_type is implemented as seconds and days to allow for larger intervals
type time_type
private
integer:: seconds
integer:: days
integer:: ticks
integer:: dummy ! added as a workaround bug on IRIX64 (AP)
end type time_type
!======================================================================
interface operator (+); module procedure time_plus; end interface
interface operator (-); module procedure time_minus; end interface
interface operator (*); module procedure time_scalar_mult
module procedure scalar_time_mult; end interface
interface operator (/); module procedure time_scalar_divide
module procedure time_divide; end interface
interface operator (>); module procedure time_gt; end interface
interface operator (>=); module procedure time_ge; end interface
interface operator (<); module procedure time_lt; end interface
interface operator (<=); module procedure time_le; end interface
interface operator (==); module procedure time_eq; end interface
interface operator (/=); module procedure time_ne; end interface
interface operator (//); module procedure time_real_divide; end interface
interface assignment(=); module procedure time_assignment; end interface
!======================================================================
interface set_time
module procedure set_time_i, set_time_c
end interface
interface set_date
module procedure set_date_i, set_date_c
end interface
!======================================================================
! Include variable "version" to be written to log file.
#include<file_version.h>
logical :: module_is_initialized = .false.
!======================================================================
! A tick is the smallest increment of time.
! That is, smallest increment of time = (1/ticks_per_second) seconds
integer :: ticks_per_second = 1
!======================================================================
contains
! First define all operations on time intervals independent of calendar
!=========================================================================
! <FUNCTION NAME="set_time">
! <OVERVIEW>
! Given some number of seconds and days, returns the
! corresponding time_type.
! </OVERVIEW>
! <DESCRIPTION>
! Given some number of seconds and days, returns the
! corresponding time_type. set_time has two forms;
! one accepts integer input, the other a character string.
! For the first form, there are no restrictions on the range of the inputs,
! except that the result must be positive time.
! e.g. days=-1, seconds=86401 is acceptable.
! For the second form, days and seconds must both be positive.
! </DESCRIPTION>
! <TEMPLATE>
! 1. set_time(seconds, days, ticks, err_msg)
! </TEMPLATE>
! <TEMPLATE>
! 2. set_time(time_string, err_msg, allow_rounding)
! </TEMPLATE>
! <IN NAME="seconds" UNITS="" TYPE="integer" DIM="(scalar)">
! A number of seconds.
! </IN>
! <IN NAME="days" UNITS="" TYPE="integer" DIM="(scalar)">
! A number of days.
! </IN>
! <IN NAME="ticks" UNITS="" TYPE="integer, optional" DIM="(scalar)">
! A number of ticks.
! </IN>
! <OUT NAME="err_msg" TYPE="character, optional" DIM="(scalar)">
! When present, and when non-blank, a fatal error condition as been detected.
! The string itself is an error message.
! It is recommended that, when err_msg is present in the call
! to this routine, the next line of code should be something
! similar to this:
! if(err_msg /= '') call error_mesg('my_routine','additional info: '//trim(err_msg),FATAL)
! </OUT>
! <IN NAME="time_string" TYPE="character">
! Contains days and seconds separated by a single blank.
! days must be integer, seconds may be integer or real.
! Examples: '100 43200' '100 43200.50'
! </IN>
! <IN NAME="allow_rounding" TYPE="logical, optional" DEFAULT=".true.">
! When .true., any fractions of a second will be rounded off to the nearest tick.
! When .false., it is a fatal error if the second fraction cannot be exactly
! represented by a number of ticks.
! </IN>
! <OUT NAME="set_time" UNITS="" TYPE="" DIM="" DEFAULT="">
! A time interval corresponding to this number of days and seconds.
! </OUT>
function set_time_private(seconds, days, ticks, Time_out, err_msg)
! Returns a time interval corresponding to this number of days, seconds, and ticks.
! days, seconds and ticks may be negative, but resulting time must be positive.
! -- pjp --
! To understand why inputs may be negative,
! one needs to understand the intrinsic function "modulo".
! The expanation below is copied from a web page on fortran 90
! In addition, CEILING, FLOOR and MODULO have been added to Fortran 90.
! Only the last one is difficult to explain, which is most easily done with the examples from ISO (1991)
! MOD (8,5) gives 3 MODULO (8,5) gives 3
! MOD (-8,5) gives -3 MODULO (-8,5) gives 2
! MOD (8,-5) gives 3 MODULO (8,-5) gives -2
! MOD (-8,-5) gives -3 MODULO (-8,-5) gives -3
! I don't think it is difficult to explain.
! I think that is it sufficient to say this:
! "The result of modulo(n,m) has the sign of m"
! -- pjp --
logical :: set_time_private
integer, intent(in) :: seconds, days, ticks
type(time_type), intent(out) :: Time_out
character(len=*), intent(out) :: err_msg
integer :: seconds_new, days_new, ticks_new
seconds_new = seconds + floor(ticks/real(ticks_per_second))
ticks_new = modulo(ticks,ticks_per_second)
days_new = days + floor(seconds_new/real(seconds_per_day))
seconds_new = modulo(seconds_new,seconds_per_day)
if ( seconds_new < 0 .or. ticks_new < 0) then
call error_mesg('function set_time_i','Bad result for time. Contact those responsible for maintaining time_manager',FATAL)
endif
if(days_new < 0) then
write(err_msg,'(a,i6,a,i6,a,i6)') 'time is negative. days=',days_new,' seconds=',seconds_new,' ticks=',ticks_new
set_time_private = .false.
else
Time_out%days = days_new
Time_out%seconds = seconds_new
Time_out%ticks = ticks_new
err_msg = ''
set_time_private = .true.
endif
end function set_time_private
!---------------------------------------------------------------------------
function set_time_i(seconds, days, ticks, err_msg)
type(time_type) :: set_time_i
integer, intent(in) :: seconds
integer, intent(in), optional :: days, ticks
character(len=*), intent(out), optional :: err_msg
character(len=128) :: err_msg_local
integer :: odays, oticks
if(.not.module_is_initialized) call time_manager_init
odays = 0; if(present(days)) odays = days
oticks = 0; if(present(ticks)) oticks = ticks
if(present(err_msg)) err_msg = ''
if(.not.set_time_private(seconds, odays, oticks, set_time_i, err_msg_local)) then
if(error_handler('function set_time_i', trim(err_msg_local), err_msg)) return
endif
end function set_time_i
!---------------------------------------------------------------------------
function set_time_c(string, err_msg, allow_rounding)
type(time_type) :: set_time_c
character(len=*), intent(in) :: string
character(len=*), intent(out), optional :: err_msg
logical, intent(in), optional :: allow_rounding
character(len=4) :: formt='(i )'
integer :: i1, i2, i3, day, second, tick, nsps
character(len=32) :: string_sifted_left
character(len=128) :: err_msg_local
logical :: allow_rounding_local
if(.not.module_is_initialized) call time_manager_init
if(present(err_msg)) err_msg = ''
allow_rounding_local=.true.; if(present(allow_rounding)) allow_rounding_local=allow_rounding
err_msg_local = 'Form of character time stamp is incorrect. The character time stamp is: '//trim(string)
string_sifted_left = adjustl(string)
i1 = index(trim(string_sifted_left),' ')
if(i1 == 0) then
if(error_handler('function set_time_c', err_msg_local, err_msg)) return
endif
if(index(string,'-') /= 0 .or. index(string,':') /= 0) then
if(error_handler('function set_time_c', err_msg_local, err_msg)) return
endif
i2 = index(trim(string_sifted_left),'.')
i3 = len_trim(cut0(string_sifted_left))
if(i2 /= 0) then ! There is no decimal point
! Check that decimal is on seconds (not days)
if(i2 < i1) then
if(error_handler('function set_time_c', err_msg_local, err_msg)) return
endif
endif
write(formt(3:3),'(i1)') i1-1
read(string_sifted_left(1:i1-1),formt) day
if(i2 == 0) then ! There is no decimal point
write(formt(3:3),'(i1)') i3-i1
read(string_sifted_left(i1+1:i3),formt) second
tick = 0
else ! There is a decimal point
! nsps = spaces occupied by whole number of seconds
nsps = i2-i1-1
if(nsps == 0) then
second = 0
else
write(formt(3:3),'(i1)') nsps
read(string_sifted_left(i1+1:i2-1),formt) second
endif
if(.not.get_tick_from_string(string_sifted_left(i2:i3), err_msg_local, allow_rounding_local, tick)) then
if(error_handler('function set_time_c', err_msg_local, err_msg)) return
endif
! If tick has been rounded up to ticks_per_second, then bump up second.
if(tick == ticks_per_second) then
second = second + 1
tick = 0
endif
endif
if(.not.set_time_private(second, day, tick, set_time_c, err_msg_local)) then
if(error_handler('function set_time_c', err_msg_local, err_msg)) return
endif
end function set_time_c
!---------------------------------------------------------------------------
! </FUNCTION>
function get_tick_from_string(string, err_msg, allow_rounding, tick)
logical :: get_tick_from_string
character(len=*), intent(in) :: string
character(len=*), intent(out) :: err_msg
logical, intent(in) :: allow_rounding
integer, intent(out) :: tick
character(len=4) :: formt='(i )'
integer :: i3, nspf, fraction, magnitude, tpsfrac
err_msg = ''
get_tick_from_string = .true.
i3 = len_trim(string)
nspf = i3 - 1 ! nspf = spaces occupied by fractional seconds, excluding decimal point
if(nspf == 0) then
tick = 0 ! Nothing to the right of the decimal point
else
write(formt(3:3),'(i1)') nspf
read(string(2:i3),formt) fraction
if(fraction == 0) then
tick = 0 ! All zeros to the right of the decimal point
else
magnitude = 10**nspf
tpsfrac = ticks_per_second*fraction
if(allow_rounding) then
tick = nint((real(tpsfrac)/magnitude))
else
if(modulo(tpsfrac,magnitude) == 0) then
tick = tpsfrac/magnitude
else
write(err_msg,'(a,i6)') 'Second fraction cannot be exactly represented with ticks. '// &
'fraction='//trim(string)//' ticks_per_second=',ticks_per_second
get_tick_from_string = .false.
endif
endif
endif
endif
end function get_tick_from_string
!---------------------------------------------------------------------------
! <SUBROUTINE NAME="get_time">
! <OVERVIEW>
! Given a time interval, returns the corresponding seconds and days.
! </OVERVIEW>
! <DESCRIPTION>
! Given a time interval, returns the corresponding seconds and days.
! </DESCRIPTION>
! <TEMPLATE>
! get_time(time, seconds, days, ticks, err_msg)
! </TEMPLATE>
! <IN NAME="time" TYPE="time_type">
! A time interval.
! </IN>
! <OUT NAME="seconds" UNITS="" TYPE="integer" DIM="(scalar)">
! A number of seconds.
! </OUT>
! <OUT NAME="days" UNITS="" TYPE="integer" DIM="(scalar)">
! A number of days.
! </OUT>
! <OUT NAME="ticks" UNITS="" TYPE="integer, optional" DIM="(scalar)">
! A number of ticks.
! </OUT>
! <OUT NAME="err_msg" TYPE="character, optional" DIM="(scalar)">
! When present, and when non-blank, a fatal error condition as been detected.
! The string itself is an error message.
! It is recommended that, when err_msg is present in the call
! to this routine, the next line of code should be something
! similar to this:
! if(err_msg /= '') call error_mesg('my_routine','additional info: '//trim(err_msg),FATAL)
! </OUT>
subroutine get_time(Time, seconds, days, ticks, err_msg)
! Returns days and seconds ( < 86400 ) corresponding to a time.
type(time_type), intent(in) :: Time
integer, intent(out) :: seconds
integer, intent(out), optional :: days, ticks
character(len=*), intent(out), optional :: err_msg
character(len=128) :: err_msg_local
if(.not.module_is_initialized) call time_manager_init
if(present(err_msg)) err_msg = ''
seconds = Time%seconds
if(present(ticks)) then
ticks = Time%ticks
else
if(Time%ticks /= 0) then
err_msg_local = 'subroutine get_time: ticks must be present when time has a second fraction'
if(error_handler('subroutine get_time', err_msg_local, err_msg)) return
endif
endif
if (present(days)) then
days = Time%days
else
if (Time%days > (huge(seconds) - seconds)/seconds_per_day) then
err_msg_local = 'Integer overflow in seconds. Optional argument days must be present.'
if(error_handler('subroutine get_time', err_msg_local, err_msg)) return
endif
seconds = seconds + Time%days * seconds_per_day
endif
end subroutine get_time
! </SUBROUTINE>
!-------------------------------------------------------------------------
! <FUNCTION NAME="increment_time">
! <OVERVIEW>
! Given a time and an increment of days and seconds, returns
! a time that adds this increment to an input time.
! </OVERVIEW>
! <DESCRIPTION>
! Given a time and an increment of days and seconds, returns
! a time that adds this increment to an input time.
! Increments a time by seconds and days.
! </DESCRIPTION>
! <TEMPLATE>
! increment_time(time, seconds, days, ticks, err_msg, allow_neg_inc)
! </TEMPLATE>
! <IN NAME="time" TYPE="time_type" DIM="(scalar)">
! A time interval.
! </IN>
! <IN NAME="seconds" TYPE="integer" DIM="(scalar)">
! Increment of seconds.
! </IN>
! <IN NAME="days" UNITS="" TYPE="integer, optional" DIM="(scalar)">
! Increment of days.
! </IN>
! <IN NAME="ticks" TYPE="integer, optional" DIM="(scalar)">
! Increment of ticks.
! </IN>
! <OUT NAME="increment_time" TYPE="time_type" DIM="(scalar)">
! A time that adds this increment to the input time.
! A negative result is a fatal error.
! </OUT>
! <OUT NAME="err_msg" TYPE="character, optional" DIM="(scalar)">
! When present, and when non-blank, a fatal error condition as been detected.
! The string itself is an error message.
! It is recommended that, when err_msg is present in the call
! to this routine, the next line of code should be something
! similar to this:
! if(err_msg /= '') call error_mesg('my_routine','additional info: '//trim(err_msg),FATAL)
! </OUT>
! <IN NAME="allow_neg_inc" TYPE="logical, optional" DIM="(scalar)" DEFAULT=".true.">
! When .false., it is a fatal error if any of the input time increments are negative.
! This mimics the behavior of lima and earlier revisions.
! </IN>
function increment_time(Time, seconds, days, ticks, err_msg, allow_neg_inc)
! Increments a time by seconds, days and ticks.
type(time_type) :: increment_time
type(time_type), intent(in) :: Time
integer, intent(in) :: seconds
integer, intent(in), optional :: days, ticks
character(len=*), intent(out), optional :: err_msg
logical, intent(in), optional :: allow_neg_inc
integer :: odays, oticks
character(len=128) :: err_msg_local
logical :: allow_neg_inc_local
odays = 0; if(present(days)) odays = days
oticks = 0; if(present(ticks)) oticks = ticks
allow_neg_inc_local=.true.; if(present(allow_neg_inc)) allow_neg_inc_local=allow_neg_inc
if(.not.allow_neg_inc_local) then
if(seconds < 0 .or. odays < 0 .or. oticks < 0) then
write(err_msg_local,10) seconds, odays, oticks
10 format('One or more time increments are negative: seconds=',i6,' days=',i6,' ticks=',i6)
if(error_handler('function increment_time', err_msg_local, err_msg)) return
endif
endif
if(.not.increment_time_private(Time, seconds, odays, oticks, increment_time, err_msg_local)) then
if(error_handler('function increment_time', err_msg_local, err_msg)) return
endif
end function increment_time
! </FUNCTION>
!--------------------------------------------------------------------------
function increment_time_private(Time_in, seconds, days, ticks, Time_out, err_msg)
! Increments a time by seconds, days and ticks.
logical :: increment_time_private
type(time_type), intent(in) :: Time_in
integer, intent(in) :: seconds, days, ticks
type(time_type), intent(out) :: Time_out
character(len=*), intent(out) :: err_msg
! Watch for immediate overflow on days or seconds
if(days >= huge(days) - Time_in%days) then
err_msg = 'Integer overflow in days in increment_time'
increment_time_private = .false.
return
endif
if(seconds >= huge(seconds) - Time_in%seconds) then
err_msg = 'Integer overflow in seconds in increment_time'
increment_time_private = .false.
return
endif
increment_time_private = set_time_private(Time_in%seconds+seconds, Time_in%days+days, Time_in%ticks+ticks, Time_out, err_msg)
end function increment_time_private
!--------------------------------------------------------------------------
! <FUNCTION NAME="decrement_time">
! <OVERVIEW>
! Given a time and a decrement of days and seconds, returns
! a time that subtracts this decrement from an input time.
! </OVERVIEW>
! <DESCRIPTION>
! Decrements a time by seconds and days.
! </DESCRIPTION>
! <TEMPLATE>
! Decrement_time(time, seconds, days, ticks, err_msg, allow_neg_inc)
! </TEMPLATE>
! <IN NAME="time" TYPE="time_type" DIM="(scalar)">
! A time interval.
! </IN>
! <IN NAME="seconds" TYPE="integer" DIM="(scalar)">
! Decrement of seconds.
! </IN>
! <IN NAME="days" TYPE="integer, optional" DIM="(scalar)">
! Decrement of days.
! </IN>
! <IN NAME="ticks" TYPE="integer, optional" DIM="(scalar)">
! Decrement of ticks.
! </IN>
! <OUT NAME="decrement_time" TYPE="time_type">
! A time that subtracts this decrement from an input time.
! A negative result is a fatal error.
! </OUT>
! <OUT NAME="err_msg" TYPE="character, optional" DIM="(scalar)">
! When present, and when non-blank, a fatal error condition as been detected.
! The string itself is an error message.
! It is recommended that, when err_msg is present in the call
! to this routine, the next line of code should be something
! similar to this:
! if(err_msg /= '') call error_mesg('my_routine','additional info: '//trim(err_msg),FATAL)
! </OUT>
! <IN NAME="allow_neg_inc" TYPE="logical, optional" DIM="(scalar)" DEFAULT=".true.">
! When .false., it is a fatal error if any of the input time increments are negative.
! This mimics the behavior of lima and earlier revisions.
! </IN>
function decrement_time(Time, seconds, days, ticks, err_msg, allow_neg_inc)
! Decrements a time by seconds, days and ticks.
type(time_type) :: decrement_time
type(time_type), intent(in) :: Time
integer, intent(in) :: seconds
integer, intent(in), optional :: days, ticks
character(len=*), intent(out), optional :: err_msg
logical, intent(in), optional :: allow_neg_inc
integer :: odays, oticks
character(len=128) :: err_msg_local
logical :: allow_neg_inc_local
odays = 0; if (present(days)) odays = days
oticks = 0; if (present(ticks)) oticks = ticks
allow_neg_inc_local=.true.; if(present(allow_neg_inc)) allow_neg_inc_local=allow_neg_inc
if(.not.allow_neg_inc_local) then
if(seconds < 0 .or. odays < 0 .or. oticks < 0) then
write(err_msg_local,10) seconds,odays,oticks
10 format('One or more time increments are negative: seconds=',i6,' days=',i6,' ticks=',i6)
if(error_handler('function decrement_time', err_msg_local, err_msg)) return
endif
endif
if(.not.increment_time_private(Time, -seconds, -odays, -oticks, decrement_time, err_msg_local)) then
if(error_handler('function decrement_time', err_msg_local, err_msg)) return
endif
end function decrement_time
! </FUNCTION>
!--------------------------------------------------------------------------
! <FUNCTION NAME="time_gt operator(>)">
! <OVERVIEW>
! Returns true if time1 > time2.
! </OVERVIEW>
! <DESCRIPTION>
! Returns true if time1 > time2.
! </DESCRIPTION>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="logical" DIM="" DEFAULT="">
! Returns true if time1 > time2
! </OUT>
! <TEMPLATE>
! time_gt(time1, time2)
! </TEMPLATE>
function time_gt(time1, time2)
! Returns true if time1 > time2
logical :: time_gt
type(time_type), intent(in) :: time1, time2
time_gt = (time1%days > time2%days)
if(time1%days == time2%days) then
if(time1%seconds == time2%seconds) then
time_gt = (time1%ticks > time2%ticks)
else
time_gt = (time1%seconds > time2%seconds)
endif
endif
end function time_gt
! </FUNCTION>
!--------------------------------------------------------------------------
! <FUNCTION NAME="time_ge; operator(>=)">
! <OVERVIEW>
! Returns true if time1 >= time2.
! </OVERVIEW>
! <DESCRIPTION>
! Returns true if time1 >= time2.
! </DESCRIPTION>
! <TEMPLATE>
! time_ge(time1, time2)
! </TEMPLATE>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="logical" DIM="" DEFAULT="">
! Returns true if time1 >= time2
! </OUT>
function time_ge(time1, time2)
! Returns true if time1 >= time2
logical :: time_ge
type(time_type), intent(in) :: time1, time2
time_ge = (time_gt(time1, time2) .or. time_eq(time1, time2))
end function time_ge
! </FUNCTION>
!--------------------------------------------------------------------------
! <FUNCTION NAME="time_lt; operator(<)">
! <OVERVIEW>
! Returns true if time1 < time2.
! </OVERVIEW>
! <DESCRIPTION>
! Returns true if time1 < time2.
! </DESCRIPTION>
! <TEMPLATE>
! time_lt(time1, time2)
! </TEMPLATE>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="logical" DIM="" DEFAULT="">
! Returns true if time1 < time2
! </OUT>
function time_lt(time1, time2)
! Returns true if time1 < time2
logical :: time_lt
type(time_type), intent(in) :: time1, time2
time_lt = (time1%days < time2%days)
if(time1%days == time2%days)then
if(time1%seconds == time2%seconds) then
time_lt = (time1%ticks < time2%ticks)
else
time_lt = (time1%seconds < time2%seconds)
endif
endif
end function time_lt
! </FUNCTION>
!--------------------------------------------------------------------------
! <FUNCTION NAME="time_le; operator(<=)">
! <OVERVIEW>
! Returns true if time1 <= time2.
! </OVERVIEW>
! <DESCRIPTION>
! Returns true if time1 <= time2.
! </DESCRIPTION>
! <TEMPLATE>
! time_le(time1, time2)
! </TEMPLATE>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="logical" DIM="" DEFAULT="">
! Returns true if time1 <= time2
! </OUT>
function time_le(time1, time2)
! Returns true if time1 <= time2
logical :: time_le
type(time_type), intent(in) :: time1, time2
time_le = (time_lt(time1, time2) .or. time_eq(time1, time2))
end function time_le
! </FUNCTION>
!--------------------------------------------------------------------------
! <FUNCTION NAME="time_eq; operator(==)">
! <OVERVIEW>
! Returns true if time1 == time2.
! </OVERVIEW>
! <DESCRIPTION>
! Returns true if time1 == time2.
! </DESCRIPTION>
! <TEMPLATE>
! time_eq(time1, time2)
! </TEMPLATE>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="logical" DIM="" DEFAULT="">
! Returns true if time1 == time2
! </OUT>
function time_eq(time1, time2)
! Returns true if time1 == time2
logical :: time_eq
type(time_type), intent(in) :: time1, time2
if(.not.module_is_initialized) call time_manager_init
time_eq = (time1%seconds == time2%seconds .and. time1%days == time2%days &
.and. time1%ticks == time2%ticks)
end function time_eq
! </FUNCTION>
!--------------------------------------------------------------------------
! <FUNCTION NAME="time_ne; operator(/=)">
! <OVERVIEW>
! Returns true if time1 /= time2.
! </OVERVIEW>
! <DESCRIPTION>
! Returns true if time1 /= time2.
! </DESCRIPTION>
! <TEMPLATE>
! time_ne(time1, time2)
! </TEMPLATE>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="logical" DIM="" DEFAULT="">
! Returns true if time1 /= time2
! </OUT>
function time_ne(time1, time2)
! Returns true if time1 /= time2
logical :: time_ne
type(time_type), intent(in) :: time1, time2
time_ne = (.not. time_eq(time1, time2))
end function time_ne
! </FUNCTION>
!-------------------------------------------------------------------------
! <FUNCTION NAME="time_plus; operator(+)">
! <OVERVIEW>
! Returns sum of two time_types.
! </OVERVIEW>
! <TEMPLATE>
! time1 + time2
! </TEMPLATE>
! <DESCRIPTION>
! Returns sum of two time_types.
! </DESCRIPTION>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="time_type" DIM="" DEFAULT="">
! Returns sum of two time_types.
! </OUT>
function time_plus(time1, time2)
! Returns sum of two time_types
type(time_type) :: time_plus
type(time_type), intent(in) :: time1, time2
if(.not.module_is_initialized) call time_manager_init
time_plus = increment_time(time1, time2%seconds, time2%days, time2%ticks)
end function time_plus
! </FUNCTION>
!-------------------------------------------------------------------------
! <FUNCTION NAME="time_minus; operator(-)">
! <OVERVIEW>
! Returns difference of two time_types.
! </OVERVIEW>
! <DESCRIPTION>
! Returns difference of two time_types. WARNING: a time type is positive
! so by definition time1 - time2 is the same as time2 - time1.
! </DESCRIPTION>
! <TEMPLATE>
! time_minus(time1, time2)
! </TEMPLATE>
! <TEMPLATE>
! time1 - time2
! </TEMPLATE>
! <IN NAME="time1" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <IN NAME="time2" UNITS="" TYPE="time_type" DIM="">
! A time interval.
! </IN>
! <OUT NAME="" UNITS="" TYPE="time_type" DIM="" DEFAULT="">
! Returns difference of two time_types.
! </OUT>
function time_minus(time1, time2)