This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESMF_TimeMod.F90
1574 lines (1392 loc) · 47.8 KB
/
ESMF_TimeMod.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
! $Id$
!
! Earth System Modeling Framework
! Copyright 2002-2003, University Corporation for Atmospheric Research,
! Massachusetts Institute of Technology, Geophysical Fluid Dynamics
! Laboratory, University of Michigan, National Centers for Environmental
! Prediction, Los Alamos National Laboratory, Argonne National Laboratory,
! NASA Goddard Space Flight Center.
! Licensed under the GPL.
!
!==============================================================================
!
! ESMF Time Module
module ESMF_TimeMod
!
!==============================================================================
!
! This file contains the Time class definition and all Time class methods.
!
!------------------------------------------------------------------------------
! INCLUDES
#include "ESMF_TimeMgr.inc"
!==============================================================================
!BOPI
! !MODULE: ESMF_TimeMod
!
! !DESCRIPTION:
! Part of Time Manager F90 API wrapper of C++ implemenation
!
! Defines F90 wrapper entry points for corresponding
! C++ class {\tt ESMC\_Time} implementation
!
! See {\tt ../include/ESMC\_Time.h} for complete description
!
!------------------------------------------------------------------------------
! !USES:
! inherit from ESMF base class
use ESMF_BaseMod
! inherit from base time class
use ESMF_BaseTimeMod
! associated derived types
use ESMF_TimeIntervalMod
use ESMF_CalendarMod
use ESMF_ShrTimeMod, only : ESMF_Time
! added by Jhe
use ESMF_Stubs
implicit none
!
!------------------------------------------------------------------------------
! !PRIVATE TYPES:
private
!------------------------------------------------------------------------------
! ! ESMF_Time
!
! ! F90 class type to match C++ Time class in size only;
! ! all dereferencing within class is performed by C++ implementation
! move to ESMF_ShrTimeMod
! type ESMF_Time
! type(ESMF_BaseTime) :: basetime ! inherit base class
! ! time instant is expressed as year + basetime
! integer :: YR
! type(ESMF_Calendar), pointer :: calendar ! associated calendar
! end type
!------------------------------------------------------------------------------
! !PUBLIC DATA:
!------------------------------------------------------------------------------
! !PUBLIC TYPES:
public ESMF_Time
!------------------------------------------------------------------------------
!
! !PUBLIC MEMBER FUNCTIONS:
public ESMF_TimeGet
public ESMF_TimeSet
public ESMF_TimePrint
! Required inherited and overridden ESMF_Base class methods
public ESMF_TimeCopy
public ESMF_SetYearWidth
! !PRIVATE MEMBER FUNCTIONS:
private ESMF_TimeGetDayOfYear
private ESMF_TimeGetDayOfYearInteger
! Inherited and overloaded from ESMF_BaseTime
public operator(+)
public ESMF_TimeInc
public operator(-)
private ESMF_TimeDec
private ESMF_TimeDiff
public operator(.EQ.)
public ESMF_TimeEQ
public operator(.NE.)
public ESMF_TimeNE
public operator(.LT.)
public ESMF_TimeLT
public operator(.GT.)
public ESMF_TimeGT
public operator(.LE.)
public ESMF_TimeLE
public operator(.GE.)
public ESMF_TimeGE
!EOPI
!------------------------------------------------------------------------------
! The following line turns the CVS identifier string into a printable variable.
character(*), parameter, private :: version = &
'$Id$'
integer :: yearWidth = 4
!==============================================================================
!
! INTERFACE BLOCKS
!
!==============================================================================
!BOP
! !INTERFACE:
interface ESMF_TimeGetDayOfYear
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeGetDayOfYearInteger
! !DESCRIPTION:
! This interface overloads the {\tt ESMF\_GetDayOfYear} method
! for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(+)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeInc, ESMF_TimeInc2
! !DESCRIPTION:
! This interface overloads the + operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface assignment (=)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeCopy
! !DESCRIPTION:
! This interface overloads the = operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(-)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeDec, ESMF_TimeDec2
! !DESCRIPTION:
! This interface overloads the - operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(-)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeDiff
! !DESCRIPTION:
! This interface overloads the - operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(.EQ.)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeEQ
! !DESCRIPTION:
! This interface overloads the .EQ. operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(.NE.)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeNE
! !DESCRIPTION:
! This interface overloads the .NE. operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(.LT.)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeLT
! !DESCRIPTION:
! This interface overloads the .LT. operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(.GT.)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeGT
! !DESCRIPTION:
! This interface overloads the .GT. operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(.LE.)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeLE
! !DESCRIPTION:
! This interface overloads the .LE. operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!BOP
! !INTERFACE:
interface operator(.GE.)
! !PRIVATE MEMBER FUNCTIONS:
module procedure ESMF_TimeGE
! !DESCRIPTION:
! This interface overloads the .GE. operator for the {\tt ESMF\_Time} class
!
!EOP
end interface
!
!------------------------------------------------------------------------------
!==============================================================================
contains
!==============================================================================
!------------------------------------------------------------------------------
!BOP
! !IROUTINE: ESMF_TimeGet - Get value in user-specified units
! !INTERFACE:
! subroutine ESMF_TimeGet(time, YY, YRl, MM, DD, D, Dl, H, M, S, Sl, MS, &
! US, NS, d_, h_, m_, s_, ms_, us_, ns_, Sn, Sd, &
! dayOfYear, dayOfYear_r8, dayOfYear_intvl, &
! timeString, rc)
recursive subroutine ESMF_TimeGet(time, YY, MM, DD, D, Dl, H, M, S, MS, &
Sn, Sd, &
dayOfYear, dayOfYear_r8, dayOfYear_intvl, &
timeString, rc)
! !ARGUMENTS:
type(ESMF_Time), intent(in) :: time
integer, intent(out), optional :: YY
! integer(ESMF_KIND_I8), intent(out), optional :: YRl
integer, intent(out), optional :: MM
integer, intent(out), optional :: DD
integer, intent(out), optional :: D
integer(ESMF_KIND_I8), intent(out), optional :: Dl
integer, intent(out), optional :: H
integer, intent(out), optional :: M
integer, intent(out), optional :: S
! integer(ESMF_KIND_I8), intent(out), optional :: Sl
integer, intent(out), optional :: MS
! integer, intent(out), optional :: US
! integer, intent(out), optional :: NS
! double precision, intent(out), optional :: d_
! double precision, intent(out), optional :: h_
! double precision, intent(out), optional :: m_
! double precision, intent(out), optional :: s_
! double precision, intent(out), optional :: ms_
! double precision, intent(out), optional :: us_
! double precision, intent(out), optional :: ns_
integer, intent(out), optional :: Sn
integer, intent(out), optional :: Sd
integer, intent(out), optional :: dayOfYear
real(ESMF_KIND_R8), intent(out), optional :: dayOfYear_r8
character (len=*), intent(out), optional :: timeString
type(ESMF_TimeInterval), intent(out), optional :: dayOfYear_intvl
integer, intent(out), optional :: rc
! !DESCRIPTION:
! Get the value of the {\tt ESMF\_Time} in units specified by the user
! via F90 optional arguments.
!
! Time manager represents and manipulates time internally with integers
! to maintain precision. Hence, user-specified floating point values are
! converted internally from integers.
!
! See {\tt ../include/ESMC\_BaseTime.h and ../include/ESMC\_Time.h} for
! complete description.
!
! The arguments are:
! \begin{description}
! \item[time]
! The object instance to query
! \item[{[YY]}]
! Integer year CCYR (>= 32-bit)
! \item[{[YRl]}]
! Integer year CCYR (large, >= 64-bit)
! \item[{[MM]}]
! Integer month 1-12
! \item[{[DD]}]
! Integer day of the month 1-31
! \item[{[D]}]
! Integer Julian days (>= 32-bit)
! \item[{[Dl]}]
! Integer Julian days (large, >= 64-bit)
! \item[{[H]}]
! Integer hours
! \item[{[M]}]
! Integer minutes
! \item[{[S]}]
! Integer seconds (>= 32-bit)
! \item[{[Sl]}]
! Integer seconds (large, >= 64-bit)
! \item[{[MS]}]
! Integer milliseconds
! \item[{[US]}]
! Integer microseconds
! \item[{[NS]}]
! Integer nanoseconds
! \item[{[d\_]}]
! Double precision days
! \item[{[h\_]}]
! Double precision hours
! \item[{[m\_]}]
! Double precision minutes
! \item[{[s\_]}]
! Double precision seconds
! \item[{[ms\_]}]
! Double precision milliseconds
! \item[{[us\_]}]
! Double precision microseconds
! \item[{[ns\_]}]
! Double precision nanoseconds
! \item[{[Sn]}]
! Integer fractional seconds - numerator
! \item[{[Sd]}]
! Integer fractional seconds - denominator
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
! !REQUIREMENTS:
! TMG2.1, TMG2.5.1, TMG2.5.6
!EOP
type(ESMF_TimeInterval) :: day_step
integer :: ierr
TYPE(ESMF_Time) :: begofyear
TYPE(ESMF_TimeInterval) :: difftobegofyear
INTEGER :: year, month, dayofmonth, hour, minute, second
INTEGER :: i
INTEGER(ESMF_KIND_I8) :: cnt
ierr = ESMF_SUCCESS
IF ( PRESENT( YY ) ) THEN
YY = time%YR
ENDIF
IF ( PRESENT( MM ) ) THEN
CALL timegetmonth( time, MM )
ENDIF
IF ( PRESENT( DD ) ) THEN
CALL timegetdayofmonth( time, DD )
ENDIF
if (present(d) .or. present(dl)) then
cnt = 0
do i = 0,time%yr-1
cnt = cnt + ndaysinyear(i,time%calendar%type)
enddo
do i = time%yr,-1
cnt = cnt - ndaysinyear(i,time%calendar%type)
enddo
call timegetmonth(time,month)
do i = 1,month-1
cnt = cnt + ndaysinmonth(time%yr,i,time%calendar%type)
enddo
call timegetdayofmonth( time, dayofmonth)
cnt = cnt + dayofmonth
if (present(d)) then
d = cnt
endif
if (present(dl)) then
dl = cnt
endif
endif
!
!$$$ push HMS down into ESMF_BaseTime
IF ( PRESENT( H ) ) THEN
H = mod( time%basetime%S, SECONDS_PER_DAY ) / SECONDS_PER_HOUR
ENDIF
IF ( PRESENT( M ) ) THEN
M = mod( time%basetime%S, SECONDS_PER_HOUR) / SECONDS_PER_MINUTE
ENDIF
IF ( PRESENT( S ) ) THEN
S = mod( time%basetime%S, SECONDS_PER_MINUTE )
ENDIF
IF ( PRESENT( S ) .AND. PRESENT( DD ) ) THEN
IF ( ( .NOT. PRESENT( H ) ) .AND. ( .NOT. PRESENT( M ) ) ) THEN
S = mod( time%basetime%S, SECONDS_PER_DAY )
ENDIF
ENDIF
IF ( PRESENT( MS ) ) THEN
IF ( time%basetime%Sd /= 0 ) THEN
MS = NINT( ( time%basetime%Sn*1.0D0 / time%basetime%Sd*1.0D0 ) * 1000.0D0 )
ELSE
MS = 0
ENDIF
ENDIF
IF ( PRESENT( Sd ) .AND. PRESENT( Sn ) ) THEN
Sd = time%basetime%Sd
Sn = time%basetime%Sn
ENDIF
IF ( PRESENT( dayOfYear ) ) THEN
CALL ESMF_TimeGetDayOfYear( time, dayOfYear, rc=ierr )
ENDIF
IF ( PRESENT( timeString ) ) THEN
! This duplication for YMD is an optimization that avoids calling
! timegetmonth() and timegetdayofmonth() when it is not needed.
year = time%YR
CALL timegetmonth( time, month )
CALL timegetdayofmonth( time, dayofmonth )
!$$$ push HMS down into ESMF_BaseTime
hour = mod( time%basetime%S, SECONDS_PER_DAY ) / SECONDS_PER_HOUR
minute = mod( time%basetime%S, SECONDS_PER_HOUR) / SECONDS_PER_MINUTE
second = mod( time%basetime%S, SECONDS_PER_MINUTE )
CALL ESMFold_TimeGetString( year, month, dayofmonth, &
hour, minute, second, timeString )
ENDIF
IF ( PRESENT( dayOfYear_intvl ) ) THEN
year = time%YR
CALL ESMF_TimeSet( begofyear, yy=year, mm=1, dd=1, s=0, &
calendar=time%calendar, rc=ierr )
IF ( ierr == ESMF_FAILURE)THEN
rc = ierr
RETURN
END IF
dayOfYear_intvl = time - begofyear
ENDIF
IF ( PRESENT( dayOfYear_r8) ) THEN
year = time%YR
CALL ESMF_TimeSet( begofyear, yy=year, mm=1, dd=1, s=0, &
calendar=time%calendar, rc=ierr )
IF ( ierr == ESMF_FAILURE)THEN
rc = ierr
RETURN
END IF
CALL ESMF_TimeIntervalSet( day_step, d=1, s=0, rc=ierr )
IF ( ierr == ESMF_FAILURE)THEN
rc = ierr
RETURN
END IF
difftobegofyear = time - begofyear + day_step
CALL ESMF_TimeIntervalGet( difftobegofyear, d_r8=dayOfYear_r8, rc=ierr )
IF ( ierr == ESMF_FAILURE)THEN
rc = ierr
RETURN
END IF
ENDIF
IF ( PRESENT( rc ) ) THEN
rc = ierr
ENDIF
end subroutine ESMF_TimeGet
!------------------------------------------------------------------------------
!BOP
! !IROUTINE: ESMF_TimeSet - Initialize via user-specified unit set
! !INTERFACE:
! subroutine ESMF_TimeSet(time, YY, YRl, MM, DD, D, Dl, H, M, S, Sl, &
! MS, US, NS, d_, h_, m_, s_, ms_, us_, ns_, &
! Sn, Sd, calendar, calkindflag, rc)
subroutine ESMF_TimeSet(time, YY, MM, DD, D, Dl, H, M, S, &
MS, &
Sn, Sd, calendar, calkindflag, rc)
! !ARGUMENTS:
type(ESMF_Time), intent(inout) :: time
integer, intent(in), optional :: YY
! integer(ESMF_KIND_I8), intent(in), optional :: YRl
integer, intent(in), optional :: MM
integer, intent(in), optional :: DD
integer, intent(in), optional :: D
integer(ESMF_KIND_I8), intent(in), optional :: Dl
integer, intent(in), optional :: H
integer, intent(in), optional :: M
integer, intent(in), optional :: S
! integer(ESMF_KIND_I8), intent(in), optional :: Sl
integer, intent(in), optional :: MS
! integer, intent(in), optional :: US
! integer, intent(in), optional :: NS
! double precision, intent(in), optional :: d_
! double precision, intent(in), optional :: h_
! double precision, intent(in), optional :: m_
! double precision, intent(in), optional :: s_
! double precision, intent(in), optional :: ms_
! double precision, intent(in), optional :: us_
! double precision, intent(in), optional :: ns_
integer, intent(in), optional :: Sn
integer, intent(in), optional :: Sd
type(ESMF_Calendar), intent(in), target, optional :: calendar
type(ESMF_CalKind_Flag), intent(in), optional :: calkindflag
integer, intent(out), optional :: rc
! locals
INTEGER :: ierr
logical :: dset
! !DESCRIPTION:
! Initializes a {\tt ESMF\_Time} with a set of user-specified units
! via F90 optional arguments.
!
! Time manager represents and manipulates time internally with integers
! to maintain precision. Hence, user-specified floating point values are
! converted internally to integers.
!
! See {\tt ../include/ESMC\_BaseTime.h and ../include/ESMC\_Time.h} for
! complete description.
!
! The arguments are:
! \begin{description}
! \item[time]
! The object instance to initialize
! \item[{[YY]}]
! Integer year CCYR (>= 32-bit)
! \item[{[YRl]}]
! Integer year CCYR (large, >= 64-bit)
! \item[{[MM]}]
! Integer month 1-12
! \item[{[DD]}]
! Integer day of the month 1-31
! \item[{[D]}]
! Integer Julian days (>= 32-bit)
! \item[{[Dl]}]
! Integer Julian days (large, >= 64-bit)
! \item[{[H]}]
! Integer hours
! \item[{[M]}]
! Integer minutes
! \item[{[S]}]
! Integer seconds (>= 32-bit)
! \item[{[Sl]}]
! Integer seconds (large, >= 64-bit)
! \item[{[MS]}]
! Integer milliseconds
! \item[{[US]}]
! Integer microseconds
! \item[{[NS]}]
! Integer nanoseconds
! \item[{[d\_]}]
! Double precision days
! \item[{[h\_]}]
! Double precision hours
! \item[{[m\_]}]
! Double precision minutes
! \item[{[s\_]}]
! Double precision seconds
! \item[{[ms\_]}]
! Double precision milliseconds
! \item[{[us\_]}]
! Double precision microseconds
! \item[{[ns\_]}]
! Double precision nanoseconds
! \item[{[Sn]}]
! Integer fractional seconds - numerator
! \item[{[Sd]}]
! Integer fractional seconds - denominator
! \item[{[cal]}]
! Associated {\tt Calendar}
! \item[{[tz]}]
! Associated timezone (hours offset from GMT, e.g. EST = -5)
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
! !REQUIREMENTS:
! TMGn.n.n
!EOP
! PRINT *,'DEBUG: BEGIN ESMF_TimeSet()'
!$$$ push this down into ESMF_BaseTime constructor
IF ( PRESENT( rc ) ) then
rc = ESMF_FAILURE
ENDIF
time%YR = 0
time%basetime%S = 0
time%basetime%Sn = 0
time%basetime%Sd = 0
IF ( PRESENT(calendar) )THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): using passed-in calendar'
IF ( .not. ESMF_CalendarInitialized( calendar ) )THEN
call wrf_error_fatal( "Error:: ESMF_CalendarCreate not "// &
"called on input Calendar")
END IF
! call flush(6)
! write(6,*) 'tcx1 ESMF_TimeSet point to calendar'
! call flush(6)
time%Calendar => calendar
ELSE
! PRINT *,'DEBUG: ESMF_TimeSet(): using default calendar'
! for the sake of WRF, check ESMF_IsInitialized, revised by Juanxiong He
IF ( .not. ESMF_IsInitialized() )THEN
call wrf_error_fatal( "Error:: ESMF_Initialize not called")
END IF
! IF ( .not. ESMF_CalendarInitialized( defaultCal ) )THEN
! call wrf_error_fatal( "Error:: ESMF_Initialize not called")
! END IF
if (present(calkindflag)) then
! write(6,*) 'tcx2 ESMF_TimeSet point to calendarkindflag',calkindflag%caltype
! call flush(6)
if (calkindflag%caltype == ESMF_CALKIND_GREGORIAN%caltype) then
time%Calendar => gregorianCal
elseif (calkindflag%caltype == ESMF_CALKIND_NOLEAP%caltype) then
time%Calendar => noleapCal
else
call wrf_error_fatal( "Error:: ESMF_TimeSet invalid calkindflag")
endif
else
! write(6,*) 'tcx3 ESMF_TimeSet point to defaultcal'
! call flush(6)
time%Calendar => defaultCal
endif
END IF
! write(6,*) 'tcxn ESMF_TimeSet ',ESMF_CALKIND_NOLEAP%caltype
! call flush(6)
! write(6,*) 'tcxg ESMF_TimeSet ',ESMF_CALKIND_GREGORIAN%caltype
! call flush(6)
! write(6,*) 'tcxt ESMF_TimeSet ',time%calendar%type%caltype
! call flush(6)
dset = .false.
if (present(D)) then
if (present(Dl)) CALL wrf_error_fatal( 'ESMF_TimeSet: D and Dl not both valid')
time%basetime%s = SECONDS_PER_DAY * INT(D-1,ESMF_KIND_I8)
dset=.true.
elseif (present(Dl)) then
time%basetime%s = SECONDS_PER_DAY * Dl-1_ESMF_KIND_I8
dset=.true.
endif
IF ( PRESENT( YY ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): YY = ',YY
if (dset) CALL wrf_error_fatal( 'ESMF_TimeSet: D or DL and YY,MM,DD not both valid')
time%YR = YY
ENDIF
IF ( PRESENT( MM ) ) THEN
if (dset) CALL wrf_error_fatal( 'ESMF_TimeSet: D or DL and YY,MM,DD not both valid')
! PRINT *,'DEBUG: ESMF_TimeSet(): MM = ',MM
CALL timeaddmonths( time, MM, ierr )
IF ( ierr == ESMF_FAILURE ) THEN
IF ( PRESENT( rc ) ) THEN
rc = ESMF_FAILURE
RETURN
ELSE
CALL wrf_error_fatal( 'ESMF_TimeSet: MM out of range' )
ENDIF
ENDIF
! PRINT *,'DEBUG: ESMF_TimeSet(): back from timeaddmonths'
ENDIF
IF ( PRESENT( DD ) ) THEN
if (dset) CALL wrf_error_fatal( 'ESMF_TimeSet: D or DL and YY,MM,DD not both valid')
!$$$ no check for DD in range of days of month MM yet
!$$$ Must separate D and DD for correct interface!
! PRINT *,'DEBUG: ESMF_TimeSet(): DD = ',DD
time%basetime%S = time%basetime%S + &
( SECONDS_PER_DAY * INT( (DD-1), ESMF_KIND_I8 ) )
ENDIF
!$$$ push H,M,S,Sn,Sd,MS down into ESMF_BaseTime constructor
IF ( PRESENT( H ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): H = ',H
time%basetime%S = time%basetime%S + &
( SECONDS_PER_HOUR * INT( H, ESMF_KIND_I8 ) )
ENDIF
IF ( PRESENT( M ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): M = ',M
time%basetime%S = time%basetime%S + &
( SECONDS_PER_MINUTE * INT( M, ESMF_KIND_I8 ) )
ENDIF
IF ( PRESENT( S ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): S = ',S
time%basetime%S = time%basetime%S + &
INT( S, ESMF_KIND_I8 )
ENDIF
IF ( PRESENT( Sn ) .AND. ( .NOT. PRESENT( Sd ) ) ) THEN
CALL wrf_error_fatal( &
"ESMF_TimeSet: Must specify Sd if Sn is specified")
ENDIF
IF ( PRESENT( Sd ) .AND. PRESENT( MS ) ) THEN
CALL wrf_error_fatal( &
"ESMF_TimeSet: Must not specify both Sd and MS")
ENDIF
time%basetime%Sn = 0
time%basetime%Sd = 0
IF ( PRESENT( MS ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): MS = ',MS
time%basetime%Sn = MS
time%basetime%Sd = 1000_ESMF_KIND_I8
ELSE IF ( PRESENT( Sd ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): Sd = ',Sd
time%basetime%Sd = Sd
IF ( PRESENT( Sn ) ) THEN
! PRINT *,'DEBUG: ESMF_TimeSet(): Sn = ',Sn
time%basetime%Sn = Sn
ENDIF
ENDIF
! PRINT *,'DEBUG: ESMF_TimeSet(): calling normalize_time()'
!$$$DEBUG
!IF ( time%basetime%Sd > 0 ) THEN
! PRINT *,'DEBUG ESMF_TimeSet() before normalize: S,Sn,Sd = ', &
! time%basetime%S, time%basetime%Sn, time%basetime%Sd
!ENDIF
!$$$END DEBUG
CALL normalize_time( time )
!$$$DEBUG
!IF ( time%basetime%Sd > 0 ) THEN
! PRINT *,'DEBUG ESMF_TimeSet() after normalize: S,Sn,Sd = ', &
! time%basetime%S, time%basetime%Sn, time%basetime%Sd
!ENDIF
!$$$END DEBUG
! PRINT *,'DEBUG: ESMF_TimeSet(): back from normalize_time()'
IF ( PRESENT( rc ) ) THEN
rc = ESMF_SUCCESS
ENDIF
end subroutine ESMF_TimeSet
!------------------------------------------------------------------------------
!BOP
! !IROUTINE: ESMFold_TimeGetString - Get time instant value in string format
! !INTERFACE:
subroutine ESMFold_TimeGetString( year, month, dayofmonth, &
hour, minute, second, TimeString )
! !ARGUMENTS:
integer, intent(in) :: year
integer, intent(in) :: month
integer, intent(in) :: dayofmonth
integer, intent(in) :: hour
integer, intent(in) :: minute
integer, intent(in) :: second
character*(*), intent(out) :: TimeString
character*(256) :: TimeFormatString
! !DESCRIPTION:
! Convert {\tt ESMF\_Time}'s value into ISO 8601 format YYYY-MM-DDThh:mm:ss
!
! The arguments are:
! \begin{description}
! \item[time]
! The object instance to convert
! \item[TimeString]
! The string to return
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
! !REQUIREMENTS:
! TMG2.4.7
!EOP
!PRINT *,'DEBUG: ESMF_TimePrint(): YR,S,Sn,Sd = ',time%YR,time%basetime%S,time%basetime%Sn,time%basetime%Sd
!PRINT *,'DEBUG: ESMF_TimePrint(): year = ',year
!PRINT *,'DEBUG: ESMF_TimePrint(): month, dayofmonth = ',month,dayofmonth
!PRINT *,'DEBUG: ESMF_TimePrint(): hour = ',hour
!PRINT *,'DEBUG: ESMF_TimePrint(): minute = ',minute
!PRINT *,'DEBUG: ESMF_TimePrint(): second = ',second
!$$$here... add negative sign for YR<0
!$$$here... add Sn, Sd ??
write(TimeFormatString,FMT="(A,I4.4,A,I4.4,A)") &
"(I", yearWidth, ".", yearWidth, ",'-',I2.2,'-',I2.2,'_',I2.2,':',I2.2,':',I2.2)"
write(TimeString,FMT=TimeFormatString) year,month,dayofmonth,hour,minute,second
end subroutine ESMFold_TimeGetString
!------------------------------------------------------------------------------
!BOP
! !IROUTINE: ESMF_TimeGetDayOfYearInteger - Get time instant's day of the year as an integer value
!
! !INTERFACE:
subroutine ESMF_TimeGetDayOfYearInteger(time, DayOfYear, rc)
!
! !ARGUMENTS:
type(ESMF_Time), intent(in) :: time
integer, intent(out) :: DayOfYear
integer, intent(out), optional :: rc
!
! !DESCRIPTION:
! Get the day of the year the given {\tt ESMF\_Time} instant falls on
! (1-365). Returned as an integer value
!
! The arguments are:
! \begin{description}
! \item[time]
! The object instance to query
! \item[DayOfYear]
! The {\tt ESMF\_Time} instant's day of the year (1-365)
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
! !REQUIREMENTS:
!EOP
! requires that time be normalized
!$$$ bug when Sn>0? test
!$$$ add tests
DayOfYear = ( time%basetime%S / SECONDS_PER_DAY ) + 1
IF ( PRESENT( rc ) ) rc = ESMF_SUCCESS
end subroutine ESMF_TimeGetDayOfYearInteger
!------------------------------------------------------------------------------
!BOP
! !IROUTINE: ESMF_TimeInc - Increment time instant with a time interval
!
! !INTERFACE:
function ESMF_TimeInc(time, timeinterval)
!
! !RETURN VALUE:
type(ESMF_Time) :: ESMF_TimeInc
!
! !ARGUMENTS:
type(ESMF_Time), intent(in) :: time
type(ESMF_TimeInterval), intent(in) :: timeinterval
! !LOCAL:
INTEGER :: year,month,day,sec,nmon,nyr,mpyi4
!
! !DESCRIPTION:
! Increment {\tt ESMF\_Time} instant with a {\tt ESMF\_TimeInterval},
! return resulting {\tt ESMF\_Time} instant
!
! Maps overloaded (+) operator interface function to
! {\tt ESMF\_BaseTime} base class
!
! The arguments are:
! \begin{description}
! \item[time]
! The given {\tt ESMF\_Time} to increment
! \item[timeinterval]
! The {\tt ESMF\_TimeInterval} to add to the given {\tt ESMF\_Time}
! \end{description}
!
! !REQUIREMENTS:
! TMG1.5.4, TMG2.4.4, TMG2.4.5, TMG2.4.6, TMG5.1, TMG5.2, TMG7.2
!EOP
mpyi4 = MONTHS_PER_YEAR
! copy ESMF_Time specific properties (e.g. calendar, timezone)
ESMF_TimeInc = time
! write(6,*) 'tcx timeinc1 ',ESMF_TimeInc%yr,ESMF_TimeInc%basetime%s
CALL normalize_time( ESMF_TimeInc )
! write(6,*) 'tcx timeint ',timeinterval%yr,timeinterval%mm,timeinterval%basetime%s
! add years and months by manually forcing incremental years then adjusting the day of
! the month at the end if it's greater than the number of days in the month
! esmf seems to do exactly this based on testing
nmon = timeinterval%mm
nyr = timeinterval%yr
if (abs(nmon) > 0 .or. abs(nyr) > 0) then
call ESMF_TimeGet(ESMF_TimeInc,yy=year,mm=month,dd=day,s=sec)
! write(6,*) 'tcx timeinc mon1 ',year,month,day,sec,nyr,nmon
year = year + nyr
month = month + nmon
do while (month > MONTHS_PER_YEAR)
month = month - mpyi4
year = year + 1
enddo
do while (month < 1)
month = month + mpyi4
year = year - 1
enddo
! write(6,*) 'tcx timeinc mon2 ',year,month,day,sec
day = min(day,ndaysinmonth(year,month,ESMF_TimeInc%calendar%type))
call ESMF_TimeSet(ESMF_TimeInc,yy=year,mm=month,dd=day,s=sec,calkindflag=time%calendar%type)
call ESMF_TimeGet(ESMF_TimeInc,yy=year,mm=month,dd=day,s=sec)
! write(6,*) 'tcx timeinc mon3 ',nmon,year,month,day,sec
endif
! finally add seconds
! write(6,*) 'tcx timeinc sec ',ESMF_TimeInc%basetime%s,timeinterval%basetime%s
ESMF_TimeInc%basetime = ESMF_TimeInc%basetime + timeinterval%basetime
! and normalize
! write(6,*) 'tcx timeinc2p ',ESMF_TimeInc%yr,ESMF_TimeInc%basetime%s
CALL normalize_time( ESMF_TimeInc )
! write(6,*) 'tcx timeinc2 ',ESMF_TimeInc%yr,ESMF_TimeInc%basetime%s
end function ESMF_TimeInc
! this is added for certain compilers that don't deal with commutativity
function ESMF_TimeInc2(timeinterval, time)
type(ESMF_Time) :: ESMF_TimeInc2
type(ESMF_Time), intent(in) :: time
type(ESMF_TimeInterval), intent(in) :: timeinterval
ESMF_TimeInc2 = ESMF_TimeInc( time, timeinterval )
end function ESMF_TimeInc2
!------------------------------------------------------------------------------
!BOP
! !IROUTINE: ESMF_TimeDec - Decrement time instant with a time interval
!
! !INTERFACE:
function ESMF_TimeDec(time, timeinterval)
!
! !RETURN VALUE:
type(ESMF_Time) :: ESMF_TimeDec
!
! !ARGUMENTS:
type(ESMF_Time), intent(in) :: time
type(ESMF_TimeInterval), intent(in) :: timeinterval
! !LOCAL:
TYPE (ESMF_TimeInterval) :: neginterval
! !DESCRIPTION:
! Decrement {\tt ESMF\_Time} instant with a {\tt ESMF\_TimeInterval},
! return resulting {\tt ESMF\_Time} instant
!
! Maps overloaded (-) operator interface function to
! {\tt ESMF\_BaseTime} base class
!
! The arguments are:
! \begin{description}
! \item[time]
! The given {\tt ESMF\_Time} to decrement