-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_julian.f90
3411 lines (2538 loc) · 113 KB
/
mo_julian.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
!> \file mo_julian.f90
!> \brief Julian date conversion routines
!> \details Julian date to and from day, month, year, and also from day, month, year, hour, minute, and second.\n
!> Different calendars provided:\n
!> gregorian/standard, lilian, 360day, 365day, 366day, 360_day, 365_day/noleap, 366_day/all_leap.\n
!> There are differences between 365day and 365_day calendars, fo example; see below.
!> There is no julian calendar.\n
!> Convenience routines for Julian dates of IMSL are provided (start at 01.01.1990).
!> Also relative dates can be given in dec2date with a unit indicating the reference date.
!> \note Julian day definition starts at noon of the 1st January 4713 BC.\n
!> Here, the astronomical definition is used,
!> i.e. the year 1 BC (historic) is counted as 0 (astronomic), 2 BC is -1, etc.\n
!> This means that Julian day definition starts as 01.01.-4712 in astronomical units.\n
!> \n
!> julday and caldat start at midnight of the 1st January 4713 BC.
!> So date2dec and julday as well as dec2date and caldat are shifted by half a day.\n
!> Use date2dec with dec2date together for fractional Julian dates
!> and use julday with caldat together for integer Julian days.
!> \note Lilian dates start at midnight of 15.10.1582, when Pope Gregory XIII introduced the Gregorian calendar.
!> \note 360day, 365day and 366day calendars start at 01.01.0000 00:00:00,\n
!> while 360_day, 365_day and 366_day calendars are Gregorian calendars starting at 01.01.-4712 noon,
!> as given by the CF-convention. 365_day and 366_day calendars are also called noleap and all_leap.
!> \note Units in dec2date can only be "days/minutes/hours/seconds since YYYY-MM-DD hh:mm:ss".
!> Any precision after reference time giving the time zone (Z, +hh:mm) will be ignored.
!> Numbers in reference date do not have to have leading zeros.
!> \author Matthias Cuntz
!> \date Dec 2011
MODULE mo_julian
! This module provides Julian day conversion routines
! Written Matthias Cuntz, Dec 2011
! Modified Matthias Cuntz, Jan 2013 - added date2dec and dec2date
! Modified Matthias Cuntz, May 2014 - changed to new algorithm with astronomical units
! removed numerical recipes
! Modified David Schaefer, Oct 2015 - addded 360 day calendar procedures
! Modified David Schaefer, Jan 2016 - addded 365 day calendar procedures
! Modified David Schaefer, Feb 2016 - implemented wrapper function and the module calendar state
! Modified Matthias Cuntz, Dec 2016 - remove save variable calendar and associated routines setCalendar
! pass calendar to conversion routines
! Modified Matthias Cuntz, Dec 2016 - Lilian date, fracday
! Modified Matthias Cuntz, Mar 2018 - units in dec2date
! Modified Matthias Cuntz, Oct 2018 - calendars 360_day, 365_day/noleap, 366_day/all_leap
! - complete all missing routines of 36*day
! Modified Matthias Cuntz, Apr 2019 - allow non-leading zero notation in units
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2019 Matthias Cuntz, David Schaefer - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, i8, dp
IMPLICIT NONE
PRIVATE
PUBLIC :: caldat
PUBLIC :: date2dec ! Fractional Julian day from day, month, year, hour, minute, and second
PUBLIC :: dec2date ! Day, month, year, hour, minute, and second from fractional Julian day
PUBLIC :: julday ! Julian day from day, month and year
PUBLIC :: ndays ! IMSL Julian day from day, month and year
PUBLIC :: ndyin ! Day, month and year from IMSL Julian day
public :: caldat360day, caldat365day, caldat366day, caldat360_day, &
caldat365_day, caldat366_day, caldatJulian, caldatLilian
public :: date2dec360day, date2dec365day, date2dec366day, date2dec360_day, &
date2dec365_day, date2dec366_day, date2decJulian, date2decLilian
public :: dec2date360day, dec2date365day, dec2date366day, dec2date360_day, &
dec2date365_day, dec2date366_day, dec2dateJulian, dec2dateLilian
public :: julday360day, julday365day, julday366day, julday360_day, &
julday365_day, julday366_day, juldayJulian, juldayLilian
CONTAINS
! ------------------------------------------------------------------
! NAME
! caldat
! PURPOSE
!> \brief Day, month and year from Julian day in the current or given calendar
!> \details Wrapper around the calendar specific caldat procedures.
!> Inverse of the function julday. Here julian is input as a Julian Day Number,
!> and the routine outputs d0d, mm, and yy as the day, month, and year on which the specified
!> Julian Day started at noon.
!> The zeroth Julian Day depends on the called procedure. See their documentation for details.
! CALLING SEQUENCE
! call caldat(julday, dd, mm, yy, calendar)
! INTENT(IN)
!> \param[in] "integer(i4) :: julday" Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of Julian day
!> \param[out] "integer(i4) :: mm" Month in year of Julian day
!> \param[out] "integer(i4) :: yy" Year of Julian day
! INTENT(IN), OPTIONAL
!> \param[in] "integer(i4), optional :: calendar" The calendar to use. Available calendars:\n
!> gregorian/standard, lilian, 360day, 365day, 366day, 360_day, 365_day/noleap, 366_day/all_leap\n
!> All other strings are taken as 'gregorian'.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, David Schaefer
!> \date Jan 2015
!> Modified, Matthias Cuntz, Oct 2018 - added more calendars
elemental subroutine caldat(julian, dd, mm, yy, calendar)
implicit none
integer(i4), intent(in) :: julian
integer(i4), intent(out) :: dd, mm, yy
character(*), intent(in), optional :: calendar
if (present(calendar)) then
select case(calendar)
case("360day")
call caldat360day(julian,dd,mm,yy)
case("365day")
call caldat365day(julian,dd,mm,yy)
case("366day")
call caldat366day(julian,dd,mm,yy)
case("360_day")
call caldat360_day(julian,dd,mm,yy)
case("365_day", "noleap")
call caldat365_day(julian,dd,mm,yy)
case("366_day", "all_leap")
call caldat366_day(julian,dd,mm,yy)
case("lilian")
call caldatLilian(julian,dd,mm,yy)
case default
call caldatJulian(julian,dd,mm,yy)
end select
else
call caldatJulian(julian,dd,mm,yy)
endif
end subroutine caldat
! ------------------------------------------------------------------
! NAME
! date2dec
! PURPOSE
!> \brief Fractional Julian day from day, month, year, hour, minute, second in the current calendar
!> \details Wrapper around the calendar specific date2dec procedures.
!> In this routine date2dec returns the fractional Julian Day that begins at noon
!> of the calendar date specified by month mm, day dd, and year yy, all integer variables.
!> The zeroth Julian Day depends on the called procedure. See their documentation for details.
! CALLING SEQUENCE
! date2dec = date2dec(dd, mm, yy, hh, nn, ss, fracday, calendar)
! INTENT(IN)
! None
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "integer(i4), optional :: dd" Day in month of Julian day (default: 1)
!> \param[in] "integer(i4), optional :: mm" Month in year of Julian day (default: 1)
!> \param[in] "integer(i4), optional :: yy" Year of Julian day (default: 1)
!> \param[in] "integer(i4), optional :: hh" Hours of Julian day (default: 0)
!> \param[in] "integer(i4), optional :: nn" Minutes of hour of Julian day (default: 0)
!> \param[in] "integer(i4), optional :: ss" Secondes of minute of hour of Julian day (default: 0)
!> \param[in] "real(dp), optional :: fracday" Fractional day, only used if hh,nn,ss not given (default: 0)
!> \param[in] "integer(i4), optional :: calendar" The calendar to use. Available calendars:\n
!> gregorian/standard, lilian, 360day, 365day, 366day, 360_day, 365_day/noleap, 366_day/all_leap\n
!> All other strings are taken as 'julian'.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return real(dp) :: date2dec ! Fractional Julian day
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, David Schaefer
!> \date Jan 2015
!> Modified, Matthias Cuntz, Oct 2018 - added more calendars
elemental function date2dec(dd, mm, yy, hh, nn, ss, fracday, calendar)
implicit none
integer(i4), intent(in), optional :: dd, mm, yy
integer(i4), intent(in), optional :: hh, nn, ss
real(dp), intent(in), optional :: fracday
character(*), intent(in), optional :: calendar
real(dp) :: date2dec
if (present(calendar)) then
select case(calendar)
case("360day")
date2dec = date2dec360day(dd, mm, yy, hh, nn, ss, fracday)
case("365day")
date2dec = date2dec365day(dd, mm, yy, hh, nn, ss, fracday)
case("366day")
date2dec = date2dec366day(dd, mm, yy, hh, nn, ss, fracday)
case("360_day")
date2dec = date2dec360_day(dd, mm, yy, hh, nn, ss, fracday)
case("365_day", "noleap")
date2dec = date2dec365_day(dd, mm, yy, hh, nn, ss, fracday)
case("366_day", "all_leap")
date2dec = date2dec366_day(dd, mm, yy, hh, nn, ss, fracday)
case("lilian")
date2dec = date2decLilian(dd, mm, yy, hh, nn, ss, fracday)
case default
date2dec = date2decJulian(dd, mm, yy, hh, nn, ss, fracday)
end select
else
date2dec = date2decJulian(dd, mm, yy, hh, nn, ss, fracday)
endif
end function date2dec
! ------------------------------------------------------------------
! NAME
! dec2date
! PURPOSE
!> \brief Day, month, year, hour, minute, and second from fractional Julian day in a given calendar.
!> \details Wrapper around the calendar specific dec2date procedures.
!> Inverse of the function date2dec. Here dec2date is input as a fractional Julian Day.
!> The routine outputs dd, mm, yy, hh, nn, ss as the day, month, year, hour, minute, and second
!> on which the specified Julian Day started at noon.\n
!> Fractional day can be output optionally.
!> The zeroth Julian Day depends on the called procedure. See their documentation for details.
! CALLING SEQUENCE
! call dec2date(fJulian, dd, mm, yy, hh, nn, ss, fracday, calendar, units)
! INTENT(IN)
!> \param[in] "real(dp) :: fJulian" fractional Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "character(len=*), optional :: calendar" The calendar to use. Available calendars:\n
!> gregorian/standard, lilian, 360day, 365day, 366day, 360_day, 365_day/noleap, 366_day/all_leap\n
!> All other strings are taken as 'julian'.
!> \param[in] "character(len=*), optional :: units" Units of decimal date in ISO 8601 format.\n
!> Can only be "days/minutes/hours/seconds since YYYY-MM-DD hh:mm:ss".\n
!> Any precision after reference time giving the time zone
!> (http://www.cl.cam.ac.uk/%7Emgk25/iso-time.html -> Z, +hh:mm) will be ignored, e.g.
!> YYYY-MM-DD hh:mm:ssZ for UTC or YYYY-MM-DD hh:mm:ss+hh:mm for a specific time zone.
!>
!> Allow exception to ISO 8601 that numbers must not have leading zeros, i.e.
!> Y-M-D h:m, which is for example produced by the climate data operators (cdo).
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
!> \param[out] "integer(i4), optional :: dd" Day in month of Julian day
!> \param[out] "integer(i4), optional :: mm" Month in year of Julian day
!> \param[out] "integer(i4), optional :: yy" Year of Julian day
!> \param[out] "integer(i4), optional :: hh" Hour of Julian day
!> \param[out] "integer(i4), optional :: nn" Minute in hour of Julian day
!> \param[out] "integer(i4), optional :: ss" Second in minute of hour of Julian day
!> \param[out] "real(dp), optional :: fracday" Fractional day
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, David Schaefer
!> \date Jan 2015
!> Modified, Matthias Cuntz, Mar 2018 - units
!> Modified, Matthias Cuntz, Oct 2018 - added more calendars
!> Modified, Matthias Cuntz, Apr 2019 - allow non-leading zero notation in units
elemental subroutine dec2date(julian, dd, mm, yy, hh, nn, ss, fracday, calendar, units)
implicit none
real(dp), intent(in) :: julian
integer(i4), intent(out), optional :: dd, mm, yy, hh, nn, ss
real(dp), intent(out), optional :: fracday
character(len=*), intent(in), optional :: calendar
character(len=*), intent(in), optional :: units
character(64) :: icalendar, iunit, idate0
integer(i4) :: year0, month0, day0, hour0, minute0, second0
real(dp) :: jdate0, jdate, eps
icalendar = 'julian'
if (present(calendar)) icalendar = calendar
jdate = julian
! No write in pure routines so no error handling (commented)
if (present(units)) then
! get reference date
hour0 = 0
minute0 = 0
second0 = 0
iunit = units(1:index(trim(units),'since')-1) ! days/minutes/hours/seconds
idate0 = units(index(trim(units),'since')+6:) ! YYYY-MM-DD hh:mm:ss
read(idate0(1:index(trim(idate0),'-')-1),*) year0
idate0 = idate0(index(trim(idate0),'-')+1:) ! MM-DD hh:mm:ss
read(idate0(1:index(trim(idate0),'-')-1),*) month0
idate0 = idate0(index(trim(idate0),'-')+1:) ! DD hh:mm:ss
if (index(trim(idate0),' ') > 0) then
read(idate0(1:index(trim(idate0),' ')-1),*) day0
idate0 = idate0(index(trim(idate0),' ')+1:) ! hh:mm:ss
if (index(trim(idate0),':') > 0) then
read(idate0(1:index(trim(idate0),':')-1),*) hour0
idate0 = idate0(index(trim(idate0),':')+1:) ! mm:ss
if (index(trim(idate0),':') > 0) then
read(idate0(1:index(trim(idate0),':')-1),*) minute0
idate0 = idate0(index(trim(idate0),':')+1:) ! ss
if (len_trim(idate0) > 0) read(idate0(1:len_trim(idate0)),*) second0
else
read(idate0(1:len_trim(idate0)),*) minute0
endif
else
read(idate0(1:len_trim(idate0)),*) hour0
endif
else
read(idate0(1:len_trim(idate0)),*) day0
endif
jdate0 = date2dec(day0, month0, year0, hour0, minute0, second0, calendar=icalendar)
! Julian date should have a small offset proportional to julian date for correct re-conversion.
! Add it to final Julian date so substract it from jdate0
eps = epsilon(1.0_dp)
if (eps * abs(jdate0) > eps) then
if (jdate0 > 0._dp) then
jdate0 = jdate0 / (1._dp+eps)
else
jdate0 = jdate0 / (1._dp-eps)
endif
else
jdate0 = jdate0 - eps
endif
! Add time to reference date
select case(trim(iunit))
case("days")
jdate = jdate0 + julian
case("hours")
jdate = jdate0 + julian/24._dp
case("minutes")
jdate = jdate0 + julian/1440._dp
case("seconds")
jdate = jdate0 + julian/86400._dp
! case default
! write(*,*) 'Error dec2date: time unit not days, hours, minutes, or seconds.'
! write(*,*) ' Units must be "days/minutes/hours/seconds since YYYY-MM-DD hh:mm:ss".'
! write(*,*) ' hh:mm:ss can be omitted. Units given: ', trim(units)
! stop
end select
! Now add the offset for the numerics
eps = max(eps * abs(jdate), eps)
jdate = jdate + eps
endif
! dec2date
select case(icalendar)
case("360day")
call dec2date360day(jdate, dd, mm, yy, hh, nn, ss, fracday)
case("365day")
call dec2date365day(jdate, dd, mm, yy, hh, nn, ss, fracday)
case("366day")
call dec2date366day(jdate, dd, mm, yy, hh, nn, ss, fracday)
case("360_day")
call dec2date360_day(jdate, dd, mm, yy, hh, nn, ss, fracday)
case("365_day", "noleap")
call dec2date365_day(jdate, dd, mm, yy, hh, nn, ss, fracday)
case("366_day", "all_leap")
call dec2date366_day(jdate, dd, mm, yy, hh, nn, ss, fracday)
case("lilian")
call dec2dateLilian(jdate, dd, mm, yy, hh, nn, ss, fracday)
case default
call dec2dateJulian(jdate, dd, mm, yy, hh, nn, ss, fracday)
end select
end subroutine dec2date
! ------------------------------------------------------------------
! NAME
! julday
! PURPOSE
!> \brief Julian day from day, month and year in the current or given calendar
!> \details Wrapper around the calendar specific julday procedures.
!> In this routine julday returns the Julian Day Number that begins at noon of the calendar
!> date specified by month mm, day dd, and year yy, all integer variables.
!> The zeroth Julian Day depends on the called procedure. See their documentation for details.
! CALLING SEQUENCE
! julian = julday(dd, mm, yy, calendar)
! INTENT(IN)
!> \param[in] "integer(i4) :: dd" Day in month of Julian day
!> \param[in] "integer(i4) :: mm" Month in year of Julian day
!> \param[in] "integer(i4) :: yy" Year of Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "integer(i4), optional :: calendar" The calendar to use. Available calendars:\n
!> gregorian/standard, lilian, 360day, 365day, 366day, 360_day, 365_day/noleap, 366_day/all_leap\n
!> All other strings are taken as 'julian'.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return integer(i4) :: julian ! Julian day
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, David Schaefer
!> \date Jan 2015
!> Modified, Matthias Cuntz, Oct 2018 - added more calendars
elemental function julday(dd, mm, yy, calendar)
implicit none
integer(i4), intent(in) :: dd, mm, yy
character(*), intent(in), optional :: calendar
integer(i4) :: julday
if (present(calendar)) then
select case(calendar)
case("360day")
julday = julday360day(dd, mm, yy)
case("365day")
julday = julday365day(dd, mm, yy)
case("366day")
julday = julday366day(dd, mm, yy)
case("360_day")
julday = julday360_day(dd, mm, yy)
case("365_day", "noleap")
julday = julday365_day(dd, mm, yy)
case("366_day", "all_leap")
julday = julday366_day(dd, mm, yy)
case("lilian")
julday = juldayLilian(dd, mm, yy)
case default
julday = juldayJulian(dd, mm, yy)
end select
else
julday = juldayJulian(dd, mm, yy)
endif
end function julday
! ------------------------------------------------------------------
! NAME
! ndays
! PURPOSE
!> \brief IMSL Julian day from day, month and year
!> \details In this routine ndays returns the IMSL Julian Day Number. Julian days begin at noon of the calendar
!> date specified by month mm, day dd, and year yy, all integer variables. IMSL treats 01.01.1900
!> as a reference and assigns a Julian day 0 to it.
!> ndays = julday(dd,mm,yy) - julday(01,01,1900)
! CALLING SEQUENCE
! julian = ndays(dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: dd" Day in month of IMSL Julian day
!> \param[in] "integer(i4) :: mm" Month in year of IMSL Julian day
!> \param[in] "integer(i4) :: yy" Year of IMSL Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return integer(i4) :: julian — IMSL Julian day, i.e. days before or after 01.01.1900
! RESTRICTIONS
! None
! EXAMPLE
! ! 0 is 01.01.1900
! julian = ndays(01,01,1990)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Written, Matthias Cuntz
!> \date Dec 2011
ELEMENTAL FUNCTION ndays(dd,mm,yy)
IMPLICIT NONE
INTEGER(i4), INTENT(IN) :: dd, mm, yy
INTEGER(i4) :: ndays
INTEGER(i4), PARAMETER :: IMSLday = 2415021_i4
ndays = julday(dd, mm, yy) - IMSLday
END FUNCTION ndays
! ------------------------------------------------------------------
! NAME
! ndyin
! PURPOSE
!> \brief Day, month and year from IMSL Julian day
!> \details Inverse of the function ndys. Here ISML Julian is input as a Julian Day Number
!> minus the Julian Day Number of 01.01.1900, and the routine outputs id, mm, and yy
!> as the day, month, and year on which the specified Julian Day started at noon.
!> ndyin is caldat(IMSLJulian + 2415021, dd, mm, yy)
! CALLING SEQUENCE
! call ndyin(julian, dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: julian" IMSL Julian day, i.e. days before or after 01.01.1900
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of IMSL Julian day
!> \param[out] "integer(i4) :: mm" Month in year of IMSL Julian day
!> \param[out] "integer(i4) :: yy" Year of IMSL Julian day
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! ! 0 is 01.01.1900
! call ndyin(0,dd,mm,yy)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Written, Matthias Cuntz
!> \date Dec 2011
ELEMENTAL SUBROUTINE ndyin(julian,dd,mm,yy)
IMPLICIT NONE
INTEGER(i4), INTENT(IN) :: julian
INTEGER(i4), INTENT(OUT) :: dd, mm, yy
INTEGER(i4), PARAMETER :: IMSLday = 2415021_i4
call caldat(julian+IMSLday, dd, mm, yy)
END SUBROUTINE ndyin
! ------------------------------------------------------------------
! PRIVATE
! ------------------------------------------------------------------
! ------------------------------------------------------------------
! CALDAT
! ------------------------------------------------------------------
! ------------------------------------------------------------------
! NAME
! caldat360day
! PURPOSE
!> \brief Day, month and year from Julian day in a 360 day calendar.
!> \details Inverse of the function julday360day. Here julian is input as a Julian Day Number,
!> and the routine outputs dd, mm, and yy as the day, month, and year on which the specified
!> Julian Day started at noon.
!> The zeroth Julian Day here is 01.01.0000
! CALLING SEQUENCE
! call caldat360day(julday, dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: julday" Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of Julian day
!> \param[out] "integer(i4) :: mm" Month in year of Julian day
!> \param[out] "integer(i4) :: yy" Year of Julian day
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, David Schaefer
!> \date Oct 2015
elemental subroutine caldat360day(julian,dd,mm,yy)
implicit none
integer(i4), intent(in) :: julian
integer(i4), intent(out) :: dd, mm, yy
integer(i4), parameter :: year=360, month=30
integer(i4) :: remainder
yy = julian/year
remainder = mod(abs(julian),year)
mm = remainder/month + 1
dd = mod(abs(julian), month) + 1
end subroutine caldat360day
! ------------------------------------------------------------------
! NAME
! caldat365day
! PURPOSE
!> \brief Day, month and year from Julian day in a 365 day calendar.
!> \details Inverse of the function julday365day. Here julian is input as a Julian Day Number,
!> and the routine outputs dd, mm, and yy as the day, month, and year on which the specified
!> Julian Day started at noon.
!> The zeroth Julian Day here is 01.01.0000
! CALLING SEQUENCE
! call caldat365day(julday, dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: julday" Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of Julian day
!> \param[out] "integer(i4) :: mm" Month in year of Julian day
!> \param[out] "integer(i4) :: yy" Year of Julian day
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, David Schaefer
!> \date Dec 2015
elemental subroutine caldat365day(julian,dd,mm,yy)
implicit none
integer(i4), intent(in) :: julian
integer(i4), intent(out) :: dd, mm, yy
integer(i4), parameter :: year=365
integer(i4), dimension(12), parameter :: months=(/31,28,31,30,31,30,31,31,30,31,30,31/)
integer(i4) :: remainder
yy = julian/year
remainder = mod(abs(julian),year) + 1
do mm = 1,size(months)
if (remainder .le. months(mm)) then
exit
end if
remainder = remainder - months(mm)
end do
dd = remainder
end subroutine caldat365day
! ------------------------------------------------------------------
! NAME
! caldat366day
! PURPOSE
!> \brief Day, month and year from Julian day in a 366 day calendar.
!> \details Inverse of the function julday366day. Here julian is input as a Julian Day Number,
!> and the routine outputs dd, mm, and yy as the day, month, and year on which the specified
!> Julian Day started at noon.
!> The zeroth Julian Day here is 01.01.0000
! CALLING SEQUENCE
! call caldat366day(julday, dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: julday" Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of Julian day
!> \param[out] "integer(i4) :: mm" Month in year of Julian day
!> \param[out] "integer(i4) :: yy" Year of Julian day
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, Matthias Cuntz
!> \date Oct 2018
elemental subroutine caldat366day(julian,dd,mm,yy)
implicit none
integer(i4), intent(in) :: julian
integer(i4), intent(out) :: dd, mm, yy
integer(i4), parameter :: year=366
integer(i4), dimension(12), parameter :: months=(/31,29,31,30,31,30,31,31,30,31,30,31/)
integer(i4) :: remainder
yy = julian/year
remainder = mod(abs(julian),year) + 1
do mm = 1,size(months)
if (remainder .le. months(mm)) then
exit
end if
remainder = remainder - months(mm)
end do
dd = remainder
end subroutine caldat366day
! ------------------------------------------------------------------
! NAME
! caldat360_day
! PURPOSE
!> \brief Day, month and year from Julian day in a Gregorian calendar with only 30-day months.
!> \details Inverse of the function julday360_day. Here julian is input as a Julian Day Number,
!> and the routine outputs dd, mm, and yy as the day, month, and year on which the specified
!> Julian Day started at noon.
!> The zeroth Julian Day here is 01.01.0000
! CALLING SEQUENCE
! call caldat360_day(julday, dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: julday" Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of Julian day
!> \param[out] "integer(i4) :: mm" Month in year of Julian day
!> \param[out] "integer(i4) :: yy" Year of Julian day
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, Matthias Cuntz
!> \date Oct 2018
elemental subroutine caldat360_day(julian,dd,mm,yy)
implicit none
integer(i4), intent(in) :: julian
integer(i4), intent(out) :: dd, mm, yy
integer(i4), parameter :: year=360, month=30
integer(i4) :: remainder
yy = julian/year - 4712
remainder = mod(abs(julian),year)
mm = remainder/month + 1
dd = mod(abs(julian), month) + 1
end subroutine caldat360_day
! ------------------------------------------------------------------
! NAME
! caldat365_day
! PURPOSE
!> \brief Day, month and year from Julian day in a Gregorian calendar without leap years.
!> \details Inverse of the function julday365_day. Here julian is input as a Julian Day Number,
!> and the routine outputs dd, mm, and yy as the day, month, and year on which the specified
!> Julian Day started at noon.
!> The zeroth Julian Day here is 01.01.-4712.
! CALLING SEQUENCE
! call caldat365_day(julday, dd, mm, yy)
! INTENT(IN)
!> \param[in] "integer(i4) :: julday" Julian day
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "integer(i4) :: dd" Day in month of Julian day
!> \param[out] "integer(i4) :: mm" Month in year of Julian day
!> \param[out] "integer(i4) :: yy" Year of Julian day
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! EXAMPLE
! -> see example in test directory
! HISTORY
!> \author Written, Matthias Cuntz
!> \date Oct 2018
elemental subroutine caldat365_day(julian,dd,mm,yy)
implicit none
integer(i4), intent(in) :: julian
integer(i4), intent(out) :: dd, mm, yy
integer(i4), parameter :: year=365
integer(i4), dimension(12), parameter :: months=(/31,28,31,30,31,30,31,31,30,31,30,31/)
integer(i4) :: remainder
yy = julian/year - 4712
remainder = mod(abs(julian),year) + 1
do mm = 1,size(months)
if (remainder .le. months(mm)) then
exit
end if
remainder = remainder - months(mm)
end do
dd = remainder
end subroutine caldat365_day
! ------------------------------------------------------------------
! NAME
! caldat366_day
! PURPOSE
!> \brief Day, month and year from Julian day in a Gregorian calendar without leap years.
!> \details Inverse of the function julday366_day. Here julian is input as a Julian Day Number,