forked from ESCOMP/CDEPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshr_cal_mod.F90
1431 lines (1105 loc) · 49.7 KB
/
shr_cal_mod.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
! !MODULE: shr_cal_mod -- calendar module, relates elapsed days to calendar date.
!
! !DESCRIPTION:
! These calendar routines do conversions between...
! \begin{itemize}
! \item the integer number of elapsed days
! \item the integers year, month, day (three inter-related integers)
! \item the integer coded calendar date (yyyymmdd)
! \end{itemize}
! Possible uses include: a calling routine can increment the elapsed days
! integer and use this module to determine what the corresponding calendar
! date is; this module can be used to determine how many days apart two
! arbitrary calendar dates are.
!
! !REVISION HISTORY:
! 2001-dec-28 - B. Kauffman - created initial version, taken from cpl5
! 2012-feb-10 - T. Craig - update to esmf 5.2.0rp1, some noleap functions
! associated with elapsed days must still be done internally
!
! !REMARKS:
! Following are some internal assumptions. These assumptions are somewhat
! arbitrary -- they were chosen because they result in the simplest code given
! the requirements of this module. These assumptions can be relaxed as
! necessary:
! o the valid range of years is [-999,999999]
! o elapsed days = 0 <=> January 1st, year 0000 for noleap
!
! !INTERFACE: ------------------------------------------------------------------
module shr_cal_mod
! !USES:
use shr_kind_mod ! kinds
use shr_const_mod ! constants
use shr_sys_mod ! system
use shr_string_mod, only: shr_string_toLower
use shr_log_mod, only: s_loglev => shr_log_Level
use shr_log_mod, only: s_logunit => shr_log_Unit
use esmf
implicit none
private ! except
! !PUBLIC TYPES:
type, public :: calParamType
! parameters to replace use of numbers for dates in code
! We'd like to have these be parameters, but then they
! can't be part of a derived type
integer :: january = 1
integer :: february = 2
integer :: march = 3
integer :: april = 4
integer :: may = 5
integer :: june = 6
integer :: july = 7
integer :: august = 8
integer :: september = 9
integer :: october = 10
integer :: november = 11
integer :: december = 12
integer :: firstDayOfMonth = 1
end type calParamType
! Instance of calender parameters as a protected type so can't change outside this module
type(calParamType), public, protected :: calParams
! !PUBLIC MEMBER FUNCTIONS:
public :: shr_cal_calendarName ! checks and returns valid cal name
public :: shr_cal_numDaysinMonth ! number of days in a month
public :: shr_cal_numDaysinYear ! number of days in a year
public :: shr_cal_elapsDaysStrtMonth ! elapsed days on start of month
public :: shr_cal_timeset ! set ESMF Time from ymd, s, shr_cal calendar
public :: shr_cal_date2ymd ! converts coded-date to yr,month,day
public :: shr_cal_date2julian! converts coded-date,sec to julian days
public :: shr_cal_ymd2julian ! converts yr,month,day,sec to julian days
public :: shr_cal_ymd2date ! converts yr,month,day to coded-date
public :: shr_cal_advDate ! advance date/secs real seconds
public :: shr_cal_advDateInt ! advance date/secs integer seconds
public :: shr_cal_validDate ! logical function: is coded-date valid?
public :: shr_cal_validYMD ! logical function: are yr,month,day valid?
public :: shr_cal_validHMS ! logical function: are hr, min, sec valid?
public :: shr_cal_getDebug ! get internal debug level
public :: shr_cal_setDebug ! set internal debug level
public :: shr_cal_ymdtod2string ! translate ymdtod to string for filenames
public :: shr_cal_datetod2string ! translate date to string for filenames
public :: shr_cal_ymds2rday_offset ! translate yr,month,day,sec offset to a fractional day offset
public :: shr_cal_leapyear ! logical function: is this a leap year?
! !PUBLIC DATA MEMBERS:
! none
!EOP
! ! subroutine interfaces
interface shr_cal_timeSet
module procedure shr_cal_timeSet_int
module procedure shr_cal_timeSet_long
end interface shr_cal_timeSet
interface shr_cal_date2ymd
module procedure shr_cal_date2ymd_int
module procedure shr_cal_date2ymd_long
end interface shr_cal_date2ymd
interface shr_cal_date2julian
module procedure shr_cal_date2julian_int
module procedure shr_cal_date2julian_long
end interface shr_cal_date2julian
interface shr_cal_ymd2date
module procedure shr_cal_ymd2date_int
module procedure shr_cal_ymd2date_long
end interface shr_cal_ymd2date
interface shr_cal_advDate
module procedure shr_cal_advDate_int
module procedure shr_cal_advDate_long
end interface shr_cal_advDate
interface shr_cal_advDateInt
module procedure shr_cal_advDateInt_int
module procedure shr_cal_advDateInt_long
end interface shr_cal_advDateInt
interface shr_cal_validdate
module procedure shr_cal_validdate_int
module procedure shr_cal_validdate_long
end interface shr_cal_validdate
interface shr_cal_datetod2string
module procedure shr_cal_datetod2string_int
module procedure shr_cal_datetod2string_long
end interface shr_cal_datetod2string
integer(SHR_KIND_IN),parameter,public :: shr_cal_calMaxLen = 64
character(len=*),parameter,public :: &
shr_cal_noleap = 'NO_LEAP', &
shr_cal_gregorian = 'GREGORIAN'
!--- trigger internal debug output ---
integer(SHR_KIND_IN) :: debug = 0
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contains
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_calendarName - check calendar name and translate
!
! !DESCRIPTION:
! Check the validity of the calendar name and translate to standard naming
!
! !REVISION HISTORY:
! 2010-oct-7 - T Craig - initial version.
!
! !INTERFACE: -----------------------------------------------------------------
function shr_cal_calendarName(calendar,trap)
implicit none
! !INPUT/OUTPUT PARAMETERS:
character(*) ,intent(in) :: calendar ! calendar type
logical ,optional,intent(in) :: trap
character(len=shr_cal_calMaxLen) :: shr_cal_calendarName
!EOP
character(len=shr_cal_calMaxLen) :: lcal
logical :: ltrap
character(len=shr_cal_calMaxLen) :: lowercalendar
character(*),parameter :: subName = "(shr_cal_calendarName)"
ltrap = .true.
if (present(trap)) then
ltrap = trap
endif
lcal = ' '
lowercalendar = trim(shr_string_toLower(trim(calendar)))
lcal = trim(calendar)
selectcase(trim(lowercalendar))
case ('noleap')
lcal = trim(shr_cal_noleap)
case ('no_leap')
lcal = trim(shr_cal_noleap)
case ('365_day')
lcal = trim(shr_cal_noleap)
case ('365day')
lcal = trim(shr_cal_noleap)
case ('gregorian')
lcal = trim(shr_cal_gregorian)
case ('standard')
lcal = trim(shr_cal_gregorian)
case ('proleptic_gregorian')
lcal = trim(shr_cal_gregorian)
case default
if (ltrap) then
write(s_logunit,*) trim(subname),' : ERROR calendar not supported : ',trim(calendar)
call shr_sys_abort(trim(subname)//' : calendar not supported : '//trim(calendar))
endif
end select
shr_cal_calendarName = trim(lcal)
end function shr_cal_calendarName
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_timeSet - create and esmf_time type for ymd, sec and calendar
!
! !DESCRIPTION:
! Create ESMF_Time type from ymd, sec, calendar
!
! !REVISION HISTORY:
! 2012-feb-10 - T. Craig - initial version.
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_timeSet_int(etime,ymd,sec,calendar, rc)
implicit none
! !INPUT/OUTPUT PARAMETERS:
type(ESMF_Time),intent(out) :: etime
integer(SHR_KIND_IN),intent(in ) :: ymd,sec ! ymd, sec
character(*) ,intent(in) :: calendar ! calendar type
integer, intent(out), optional :: rc
!EOP
integer(SHR_KIND_IN) :: year,month,day
type(ESMF_CALKIND_FLAG) :: calkind
character(len=shr_cal_calMaxLen) :: lcalendar
integer :: lrc
character(*),parameter :: subName = "(shr_cal_timeSet)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
lcalendar = shr_cal_calendarName(calendar)
calkind = ESMF_CALKIND_NOLEAP
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call shr_cal_date2ymd(ymd,year,month,day)
call ESMF_TimeSet(etime,yy=year,mm=month,dd=day,s=sec,calkindflag=calkind,rc=lrc)
if (present(rc)) then
rc = lrc
else if(lrc /= ESMF_SUCCESS) then
call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
endif
end subroutine shr_cal_timeSet_int
subroutine shr_cal_timeSet_long(etime,ymd,sec,calendar, rc)
implicit none
! !INPUT/OUTPUT PARAMETERS:
type(ESMF_Time),intent(out) :: etime
integer(SHR_KIND_I8),intent(in ) :: ymd ! ymd
integer(SHR_KIND_IN), intent(in) :: sec ! ymd
character(*) ,intent(in) :: calendar ! calendar type
integer, intent(out), optional :: rc
!EOP
integer(SHR_KIND_IN) :: year,month,day
type(ESMF_CALKIND_FLAG) :: calkind
character(len=shr_cal_calMaxLen) :: lcalendar
integer :: lrc
character(*),parameter :: subName = "(shr_cal_timeSet_long)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
lcalendar = shr_cal_calendarName(calendar)
calkind = ESMF_CALKIND_NOLEAP
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call shr_cal_date2ymd(ymd,year,month,day)
call ESMF_TimeSet(etime,yy=year,mm=month,dd=day,s=sec,calkindflag=calkind,rc=lrc)
if(present(rc)) then
rc = lrc
else if(rc /= ESMF_SUCCESS) then
call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
endif
end subroutine shr_cal_timeSet_long
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_numDaysInMonth - return the number of days in a month.
!
! !DESCRIPTION:
! Deturn the number of days in a month.
!
! !REVISION HISTORY:
! 2002-sep-18 - B. Kauffman - initial version.
!
! !INTERFACE: -----------------------------------------------------------------
integer function shr_cal_numDaysInMonth(year,month,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: year,month ! calendar year,month
character(*) ,intent(in) :: calendar ! calendar type
!EOP
type(ESMF_time) :: time1,time2
type(ESMF_timeInterval) :: timeint
type(ESMF_CALKIND_FLAG) :: calkind
integer(SHR_KIND_IN) :: eday
character(len=shr_cal_calMaxLen) :: lcalendar
integer :: rc
character(*),parameter :: subName = "(shr_cal_numDaysInMonth)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
lcalendar = shr_cal_calendarName(calendar)
calkind = ESMF_CALKIND_NOLEAP
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call ESMF_TimeSet(time1,yy=year,mm=month,dd=1,s=0,calkindflag=calkind,rc=rc)
if(rc /= ESMF_SUCCESS) call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
if (month < 12) then
call ESMF_TimeSet(time2,yy=year,mm=month+1,dd=1,s=0,calkindflag=calkind,rc=rc)
if(rc /= ESMF_SUCCESS) call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
else
call ESMF_TimeSet(time2,yy=year+1,mm=1,dd=1,s=0,calkindflag=calkind,rc=rc)
if(rc /= ESMF_SUCCESS) call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
endif
timeint = time2-time1
call ESMF_TimeIntervalGet(timeint,StartTimeIn=time1,d=eday)
shr_cal_numDaysInMonth = eday
end function shr_cal_numDaysInMonth
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_numDaysInYear - return the number of days in a year.
!
! !DESCRIPTION:
! Deturn the number of days in a year.
!
! !REVISION HISTORY:
! 2002-sep-18 - B. Kauffman - initial version.
!
! !INTERFACE: -----------------------------------------------------------------
integer function shr_cal_numDaysInYear(year,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: year ! calendar year
character(*) ,intent(in) :: calendar ! calendar type
!EOP
type(ESMF_time) :: time1,time2
type(ESMF_timeInterval) :: timeint
type(ESMF_CALKIND_FLAG) :: calkind
integer(SHR_KIND_IN) :: eday
character(len=shr_cal_calMaxLen) :: lcalendar
integer :: rc
character(*),parameter :: subName = "(shr_cal_numDaysInYear)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
lcalendar = shr_cal_calendarName(calendar)
calkind = ESMF_CALKIND_NOLEAP
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call ESMF_TimeSet(time1,yy=year,mm=1,dd=1,s=0,calkindflag=calkind,rc=rc)
if(rc /= ESMF_SUCCESS) call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
call ESMF_TimeSet(time2,yy=year+1,mm=1,dd=1,s=0,calkindflag=calkind,rc=rc)
if(rc /= ESMF_SUCCESS) call ESMF_Finalize(rc=rc, endflag=ESMF_END_ABORT)
timeint = time2-time1
call ESMF_TimeIntervalGet(timeint,StartTimeIn=time1,d=eday)
shr_cal_numDaysInYear = eday
end function shr_cal_numDaysInYear
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_elapsDaysStrtMonth - return the number of elapsed days
! at start of month
!
! !DESCRIPTION:
! Return the number of elapsed days at start of a month.
!
! !REVISION HISTORY:
! 2002-Oct-29 - R. Jacob - initial version
!
! !INTERFACE: -----------------------------------------------------------------
integer function shr_cal_elapsDaysStrtMonth(year,month,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: year,month ! calendar year,month
character(*) ,intent(in) :: calendar ! calendar type
!EOP
integer :: k
character(*),parameter :: subName = "(shr_cal_elapsDaysStrtMonth)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
shr_cal_elapsDaysStrtMonth = 0
do k = 1,month-1
shr_cal_elapsDaysStrtMonth = shr_cal_elapsDaysStrtMonth + &
shr_cal_numDaysInMonth(year,k,calendar)
enddo
end function shr_cal_elapsDaysStrtMonth
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_date2ymd - converts coded-date to year/month/day.
!
! !DESCRIPTION:
! Converts coded-date (yyyymmdd) to year/month/day.
!
! !REVISION HISTORY:
! 2001-dec-28 - B. Kauffman - initial version, taken from cpl5
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_date2ymd_int (date,year,month,day)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in) :: date ! coded-date (yyyymmdd)
integer(SHR_KIND_IN),intent(out) :: year,month,day ! calendar year,month,day
!EOP
integer(SHR_KIND_IN) :: tdate ! temporary date
character(*),parameter :: subName = "(shr_cal_date2ymd)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',date
tdate = abs(date)
year =int( tdate /10000)
if (date < 0) year = -year
month=int( mod(tdate,10000)/ 100)
day = mod(tdate, 100)
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',year,month,day
end subroutine shr_cal_date2ymd_int
subroutine shr_cal_date2ymd_long (date,year,month,day)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_I8),intent(in) :: date ! coded-date ([yy]yyyymmdd)
integer(SHR_KIND_IN),intent(out) :: year,month,day ! calendar year,month,day
!EOP
integer(SHR_KIND_I8) :: tdate ! temporary date
character(*),parameter :: subName = "(shr_cal_date2ymd_long)"
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',date
tdate = abs(date)
year =int( tdate /10000)
if (date < 0) year = -year
month=int( mod(tdate,10000_SHR_KIND_I8)/100, SHR_KIND_IN)
day =int( mod(tdate, 100_SHR_KIND_I8), SHR_KIND_IN)
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',year,month,day
end subroutine shr_cal_date2ymd_long
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_date2julian - converts coded-date to julian day of year
!
! !DESCRIPTION:
! Converts coded-date to julian day of year
!
! !REVISION HISTORY:
! 2009-Oct-23 - T. Craig - initial version
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_date2julian_int(date,sec,jday,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: date ! coded (yyyymmdd) calendar date
integer(SHR_KIND_IN),intent(in ) :: sec ! seconds
real (SHR_KIND_R8),intent(out) :: jday ! julian day of year
character(*) ,intent(in) :: calendar ! calendar type
!EOP
!--- local ---
integer(SHR_KIND_IN) :: year,month,day
character(*),parameter :: subName = "(shr_cal_date2julian)"
!-------------------------------------------------------------------------------
! NOTE:
! julian day of year since yy-01-01-00000
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',date,sec
call shr_cal_date2ymd(date,year,month,day)
call shr_cal_ymd2julian(year,month,day,sec,jday,calendar)
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',jday
end subroutine shr_cal_date2julian_int
subroutine shr_cal_date2julian_long(date,sec,jday,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_I8),intent(in ) :: date ! coded ([yy]yyyymmdd) calendar date
integer(SHR_KIND_IN),intent(in ) :: sec ! seconds
real (SHR_KIND_R8),intent(out) :: jday ! julian day of year
character(*) ,intent(in) :: calendar ! calendar type
!EOP
!--- local ---
integer(SHR_KIND_IN) :: year,month,day
character(*),parameter :: subName = "(shr_cal_date2julian)"
!-------------------------------------------------------------------------------
! NOTE:
! julian day of year since yy-01-01-00000
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',date,sec
call shr_cal_date2ymd(date,year,month,day)
call shr_cal_ymd2julian(year,month,day,sec,jday,calendar)
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',jday
end subroutine shr_cal_date2julian_long
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_ymd2julian - converts y,m,d,s to julian day of year
!
! !DESCRIPTION:
! Converts y,m,d,s to julian day of year
!
! !REVISION HISTORY:
! 2009-Oct-23 - T. Craig - initial version
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_ymd2julian(year,month,day,sec,jday,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: year ! year
integer(SHR_KIND_IN),intent(in ) :: month ! month
integer(SHR_KIND_IN),intent(in ) :: day ! day
integer(SHR_KIND_IN),intent(in ) :: sec ! seconds
real (SHR_KIND_R8),intent(out) :: jday ! julian day of year
character(*) ,intent(in) :: calendar ! calendar type
!EOP
!--- local ---
character(*),parameter :: subName = "(shr_cal_ymd2julian)"
!-------------------------------------------------------------------------------
! NOTE:
! julian day of year since yy-01-01-000000
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',year,month,day,sec
if (.not. shr_cal_validYMD(year,month,day,calendar)) then
write(s_logunit,*) trim(subname),' ERROR: invalid ymd',year,month,day
call shr_sys_abort(trim(subname)//' ERROR: invalid ymd')
endif
jday = shr_cal_elapsDaysStrtMonth(year,month,calendar) + day + sec/SHR_CONST_CDAY
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',jday
end subroutine shr_cal_ymd2julian
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_ymd2date - converts year, month, day to coded-date
!
! !DESCRIPTION:
! Converts year, month, day to coded-date
!
! !REVISION HISTORY:
! 2001-dec-28 - B. Kauffman - initial version, taken from cpl5
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_ymd2date_int(year,month,day,date)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: year,month,day ! calendar year,month,day
integer(SHR_KIND_IN),intent(out) :: date ! coded (yyyymmdd) calendar date
!EOP
!--- local ---
character(*),parameter :: subName = "(shr_cal_ymd2date)"
!-------------------------------------------------------------------------------
! NOTE:
! this calendar has a year zero (but no day or month zero)
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',year,month,day
date = abs(year)*10000 + month*100 + day ! coded calendar date
if (year < 0) date = -date
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',date
end subroutine shr_cal_ymd2date_int
subroutine shr_cal_ymd2date_long(year,month,day,date)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN),intent(in ) :: year,month,day ! calendar year,month,day
integer(SHR_KIND_I8),intent(out) :: date ! coded ([yy]yyyymmdd) calendar date
!EOP
!--- local ---
character(*),parameter :: subName = "(shr_cal_ymd2date)"
!-------------------------------------------------------------------------------
! NOTE:
! this calendar has a year zero (but no day or month zero)
!-------------------------------------------------------------------------------
if (debug > 1) write(s_logunit,*) trim(subname),'_a ',year,month,day
date = abs(year)*10000_SHR_KIND_I8 + month*100 + day ! coded calendar date
if (year < 0) date = -date
if (debug > 1) write(s_logunit,*) trim(subname),'_b ',date
end subroutine shr_cal_ymd2date_long
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_advDate - advances a date and seconds with a delta time
!
! !DESCRIPTION:
! Advances a date and seconds with a delta time
! Accuracy only good to nearest second
!
! !REVISION HISTORY:
! 2009-Jun-09 - T. Craig - allows delta < 0
! 2005-Jun-10 - B. Kauffman - bug fix, simplified algorithm
! 2005-May-15 - T. Craig - initial version
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_advDate_int(delta,units,dateIN,secIN,dateOUT,secOUT,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
real (SHR_KIND_R8) ,intent(in) :: delta ! time increment
character(*) ,intent(in) :: units ! units of increment
integer(SHR_KIND_IN) ,intent(in) :: dateIN ! base date, yyyymmdd
real (SHR_KIND_R8) ,intent(in) :: secIN ! base seconds
integer(SHR_KIND_IN) ,intent(out) :: dateOUT ! new date, yyyymmdd
real (SHR_KIND_R8) ,intent(out) :: secOUT ! new seconds
character(*) ,intent(in) :: calendar ! calendar type
!EOP
!--- local ---
type(ESMF_time) :: timeIn, timeOut
type(ESMF_timeInterval) :: dt
real (SHR_KIND_R8) :: dSec ! delta-sec: advance date this many seconds
integer(SHR_KIND_I8) :: i8dsec, i8dday ! delta sec and day in i8
integer(SHR_KIND_I8) :: spd ! seconds per day in i8
integer(SHR_KIND_I4) :: idday, idsec ! delta sec and dat in i4
integer(SHR_KIND_I4) :: year, month, day, sec ! calendar stuff
character(len=shr_cal_calMaxLen) :: lcalendar
type(ESMF_CALKIND_FLAG) :: calkind
!--- formats ---
character(*),parameter :: subName = "(shr_cal_advDate)"
character(*),parameter :: F00 = "('(shr_cal_advDate) ',a,i5)"
character(*),parameter :: F02 = "('(shr_cal_advDate) ',a,i8.8,f10.3)"
!-------------------------------------------------------------------------------
! NOTE:
!-------------------------------------------------------------------------------
dSec = 0.0_SHR_KIND_R8
!--- calculate delta-time in seconds ---
if (trim(units) == 'days' ) then
dSec = delta * SHR_CONST_CDAY
elseif (trim(units) == 'hours' ) then
dSec = delta * 3600.0_SHR_KIND_R8
elseif (trim(units) == 'minutes') then
dSec = delta * 60.0_SHR_KIND_R8
elseif (trim(units) == 'seconds') then
dSec = delta * 1.0_SHR_KIND_R8
else
call shr_sys_abort(trim(subname)//' ERROR: unrecognized time units '//trim(units))
endif
! take secIn into account here since it's real
dSec = dSec + secIn
! i8 math, convert reals to nearest second
i8dSec = nint(dSec,SHR_KIND_I8)
spd = nint(SHR_CONST_CDAY)
i8dday = i8dsec/spd
i8dsec = i8dsec - i8dday*spd
! convert to i4
idday = int(i8dday, SHR_KIND_I4)
idsec = int(i8dsec, SHR_KIND_I4)
calkind = ESMF_CALKIND_NOLEAP
lcalendar = shr_cal_calendarName(calendar)
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call shr_cal_date2ymd(dateIn,year,month,day)
call ESMF_TimeSet(timeIN,yy=year,mm=month,dd=day,calkindflag=calkind)
call ESMF_TimeIntervalSet(dt,d=idday,s=idsec)
timeOut = timeIn + dt
call ESMF_TimeGet(timeOut,yy=year,mm=month,dd=day,s=sec)
call shr_cal_ymd2date(year,month,day,dateOut)
secOut = sec
if (debug>0) then
if (s_loglev > 0) write(s_logunit,*) subName," units,delta,calendar=",trim(units),delta,' ',trim(calendar)
if (s_loglev > 0) write(s_logunit,F02) "dateIN ,secIN =",dateIN ,secIN
if (s_loglev > 0) write(s_logunit,F02) "dateOUT,secOUT=",dateOUT,secOUT
end if
end subroutine shr_cal_advDate_int
subroutine shr_cal_advDate_long(delta,units,dateIN,secIN,dateOUT,secOUT,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
real (SHR_KIND_R8) ,intent(in) :: delta ! time increment
character(*) ,intent(in) :: units ! units of increment
integer(SHR_KIND_I8) ,intent(in) :: dateIN ! base date, [yy]yyyymmdd
real (SHR_KIND_R8) ,intent(in) :: secIN ! base seconds
integer(SHR_KIND_I8) ,intent(out) :: dateOUT ! new date, [yy]yyyymmdd
real (SHR_KIND_R8) ,intent(out) :: secOUT ! new seconds
character(*) ,intent(in) :: calendar ! calendar type
!EOP
!--- local ---
type(ESMF_time) :: timeIn, timeOut
type(ESMF_timeInterval) :: dt
real (SHR_KIND_R8) :: dSec ! delta-sec: advance date this many seconds
integer(SHR_KIND_I8) :: i8dsec, i8dday ! delta sec and day in i8
integer(SHR_KIND_I8) :: spd ! seconds per day in i8
integer(SHR_KIND_I4) :: idday, idsec ! delta sec and dat in i4
integer(SHR_KIND_I4) :: year, month, day, sec ! calendar stuff
character(len=shr_cal_calMaxLen) :: lcalendar
type(ESMF_CALKIND_FLAG) :: calkind
!--- formats ---
character(*),parameter :: subName = "(shr_cal_advDate)"
character(*),parameter :: F00 = "('(shr_cal_advDate) ',a,i5)"
character(*),parameter :: F02 = "('(shr_cal_advDate) ',a,i10.8,f10.3)"
!-------------------------------------------------------------------------------
! NOTE:
!-------------------------------------------------------------------------------
dSec = 0.0_SHR_KIND_R8
!--- calculate delta-time in seconds ---
if (trim(units) == 'days' ) then
dSec = delta * SHR_CONST_CDAY
elseif (trim(units) == 'hours' ) then
dSec = delta * 3600.0_SHR_KIND_R8
elseif (trim(units) == 'minutes') then
dSec = delta * 60.0_SHR_KIND_R8
elseif (trim(units) == 'seconds') then
dSec = delta * 1.0_SHR_KIND_R8
else
call shr_sys_abort(trim(subname)//' ERROR: unrecognized time units '//trim(units))
endif
! take secIn into account here since it's real
dSec = dSec + secIn
! i8 math, convert reals to nearest second
i8dSec = nint(dSec,SHR_KIND_I8)
spd = nint(SHR_CONST_CDAY)
i8dday = i8dsec/spd
i8dsec = i8dsec - i8dday*spd
! convert to i4
idday = int(i8dday, SHR_KIND_I4)
idsec = int(i8dsec, SHR_KIND_I4)
calkind = ESMF_CALKIND_NOLEAP
lcalendar = shr_cal_calendarName(calendar)
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call shr_cal_date2ymd(dateIn,year,month,day)
call ESMF_TimeSet(timeIN,yy=year,mm=month,dd=day,calkindflag=calkind)
call ESMF_TimeIntervalSet(dt,d=idday,s=idsec)
timeOut = timeIn + dt
call ESMF_TimeGet(timeOut,yy=year,mm=month,dd=day,s=sec)
call shr_cal_ymd2date(year,month,day,dateOut)
secOut = sec
if (debug>0) then
if (s_loglev > 0) write(s_logunit,*) subName," units,delta,calendar=",trim(units),delta,' ',trim(calendar)
if (s_loglev > 0) write(s_logunit,F02) "dateIN ,secIN =",dateIN ,secIN
if (s_loglev > 0) write(s_logunit,F02) "dateOUT,secOUT=",dateOUT,secOUT
end if
end subroutine shr_cal_advDate_long
!===============================================================================
!BOP ===========================================================================
!
! !IROUTINE: shr_cal_advDateInt - advances a date and seconds with a delta time
!
! !DESCRIPTION:
! Advances a date and seconds with a delta time
!
! !REVISION HISTORY:
! 2009-???-?? - ?? - replicated from shr_cal_advDate()
!
! !INTERFACE: -----------------------------------------------------------------
subroutine shr_cal_advDateInt_int(delta,units,dateIN,secIN,dateOUT,secOUT,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS:
integer(SHR_KIND_IN) ,intent(in) :: delta ! time increment
character(*) ,intent(in) :: units ! units of increment
integer(SHR_KIND_IN) ,intent(in) :: dateIN ! base date, yyyymmdd
integer(SHR_KIND_IN) ,intent(in) :: secIN ! base seconds
integer(SHR_KIND_IN) ,intent(out) :: dateOUT ! new date, yyyymmdd
integer(SHR_KIND_IN) ,intent(out) :: secOUT ! new seconds
character(*) ,intent(in) :: calendar ! calendar type
!EOP
!--- local ---
type(ESMF_time) :: timeIn, timeOut
type(ESMF_timeInterval) :: dt
integer(SHR_KIND_IN) :: year,month,day
character(len=shr_cal_calMaxLen) :: lcalendar
type(ESMF_CALKIND_FLAG) :: calkind
!--- formats ---
character(*),parameter :: subName = "(shr_cal_advDateInt)"
character(*),parameter :: F00 = "('(shr_cal_advDateInt) ',a,i5)"
character(*),parameter :: F02 = "('(shr_cal_advDateInt) ',a,i8.8,f10.3)"
!-------------------------------------------------------------------------------
! NOTE:
!-------------------------------------------------------------------------------
lcalendar = shr_cal_calendarName(calendar)
calkind = ESMF_CALKIND_NOLEAP
if (trim(lcalendar) == trim(shr_cal_gregorian)) then
calkind = ESMF_CALKIND_GREGORIAN
endif
call shr_cal_date2ymd(dateIn,year,month,day)
call ESMF_TimeSet(timeIN,yy=year,mm=month,dd=day,s=secIn,calkindflag=calkind)
!--- calculate delta-time in seconds ---
if (trim(units) == 'days' ) then
call ESMF_TimeIntervalSet(dt,d=delta)
elseif (trim(units) == 'hours' ) then
call ESMF_TimeIntervalSet(dt,h=delta)
elseif (trim(units) == 'minutes') then
call ESMF_TimeIntervalSet(dt,m=delta)
elseif (trim(units) == 'seconds') then
call ESMF_TimeIntervalSet(dt,s=delta)
else
call shr_sys_abort(trim(subname)//' ERROR: unrecognized time units '//trim(units))
endif
timeOut = timeIn + dt
call ESMF_TimeGet(timeOut,yy=year,mm=month,dd=day,s=secOut)
call shr_cal_ymd2date(year,month,day,dateOut)
if (debug>0) then
if (s_loglev > 0) write(s_logunit,*) subName," units,delta,calendar=",trim(units),delta,' ',trim(calendar)
if (s_loglev > 0) write(s_logunit,F02) "dateIN ,secIN =",dateIN ,secIN
if (s_loglev > 0) write(s_logunit,F02) "dateOUT,secOUT=",dateOUT,secOUT
end if
end subroutine shr_cal_advDateInt_int
subroutine shr_cal_advDateInt_long(delta,units,dateIN,secIN,dateOUT,secOUT,calendar)
implicit none
! !INPUT/OUTPUT PARAMETERS: