Philippe 23/02/2016: lfi2cdf: modif for JPHEXT/=1
[MNH-git_open_source-lfs.git] / tools / diachro / src / mesonh / temporal_dist.f90
1 !-----------------------------------------------------------------
2 !--------------- special set of characters for SCCS information
3 !-----------------------------------------------------------------
4 !      @(#) Lib:/mesonh/sources/operators/s.temporal_dist.f90, Version:1.6, Date:98/06/23, Last modified:98/06/04
5 !-----------------------------------------------------------------
6 !     #########################
7       MODULE MODI_TEMPORAL_DIST
8 !     #########################
9 INTERFACE
10       SUBROUTINE TEMPORAL_DIST(KYEARF, KMONTHF, KDAYF, PSECF,     &
11                                KYEARI, KMONTHI, KDAYI, PSECI,     &
12                                PDIST                              )
13 !
14 INTEGER, INTENT(IN) :: KYEARF  ! year of Final date
15 INTEGER, INTENT(IN) :: KMONTHF ! month of Final date
16 INTEGER, INTENT(IN) :: KDAYF   ! day of Final date
17 REAL,    INTENT(IN) :: PSECF   ! number of seconds since date at 00 UTC
18                                ! of Final date
19 INTEGER, INTENT(IN) :: KYEARI  ! year of Initial date
20 INTEGER, INTENT(IN) :: KMONTHI ! month of Initial date
21 INTEGER, INTENT(IN) :: KDAYI   ! day of Initial date
22 REAL,    INTENT(IN) :: PSECI   ! number of seconds since date at 00 UTC
23                                ! of Initial date
24 REAL,    INTENT(OUT):: PDIST   ! temporal distance in secunds between the final 
25                                ! and initial date
26 !
27 END SUBROUTINE TEMPORAL_DIST 
28 !
29 END INTERFACE
30
31 END MODULE MODI_TEMPORAL_DIST
32 !
33 !     #############################################################
34       SUBROUTINE TEMPORAL_DIST(KYEARF, KMONTHF, KDAYF, PSECF,     &
35                                KYEARI, KMONTHI, KDAYI, PSECI,     &
36                                PDIST                              )
37 !     #############################################################
38 !
39 !!****  *TEMPORAL_DIST* - finds the number of secunds between 2 dates
40 !!
41 !!    PURPOSE
42 !!    -------
43 !!
44 !!                                WARNING
45 !!
46 !!      -----> Only correct for dates between 19900301 and 21000228   <-----
47 !!
48 !!  The correct test should be:
49 !! IF( ((MOD(KYEAR,4)==0).AND.(MOD(KYEAR,100)/=0)) .OR. (MOD(KYEAR,400)==0))THEN
50 !!
51 !!**  METHOD
52 !!    ------
53 !!
54 !!      A comparison term by term of the elements of the 2 dates is performed.
55 !!    and the temporal distance between the 2 dates is then deduced.
56 !!
57 !!    EXTERNAL
58 !!    --------
59 !!
60 !!    IMPLICIT ARGUMENTS
61 !!    ------------------
62 !!
63 !!    REFERENCE
64 !!    ---------
65 !!
66 !!    Book 2
67 !!
68 !!    AUTHOR
69 !!    ------
70 !!      
71 !     J.Stein  Meteo-France
72 !!
73 !!    MODIFICATIONS
74 !!    -------------
75 !!      Original    02/01/96
76 !-------------------------------------------------------------------------------
77 !
78 !*       0.    DECLARATIONS
79 !              ------------
80 !
81 IMPLICIT NONE
82 !
83 !*       0.1   Declaration of arguments
84 !              ------------------------
85 INTEGER, INTENT(IN) :: KYEARF  ! year of Final date
86 INTEGER, INTENT(IN) :: KMONTHF ! month of Final date
87 INTEGER, INTENT(IN) :: KDAYF   ! day of Final date
88 REAL,    INTENT(IN) :: PSECF   ! number of seconds since date at 00 UTC
89                                ! of Final date
90 INTEGER, INTENT(IN) :: KYEARI  ! year of Initial date
91 INTEGER, INTENT(IN) :: KMONTHI ! month of Initial date
92 INTEGER, INTENT(IN) :: KDAYI   ! day of Initial date
93 REAL,    INTENT(IN) :: PSECI   ! number of seconds since date at 00 UTC
94                                ! of Initial date
95 REAL,    INTENT(OUT):: PDIST   ! temporal distance in secunds between the final 
96                                ! and initial date
97 !
98 !*       0.2   Declaration of local variables
99 !              ------------------------------
100 !
101 INTEGER :: IDAYS  ! number of days between the two dates
102 INTEGER :: JMONTH,JYEAR ! loop index on months or years 
103 !
104 !-------------------------------------------------------------------------------
105 !
106 !*       1.    SAME YEARS AND SAME MONTHS
107 !              --------------------------
108 !
109 IF ( (KYEARF==KYEARI) .AND. (KMONTHF==KMONTHI) ) THEN
110   PDIST = ( KDAYF-KDAYI) * 86400. + PSECF - PSECI
111   ! check chronological order
112   IF (PDIST < 0.) PDIST=-999.
113 END IF
114 !
115 !-------------------------------------------------------------------------------
116 !
117 !*       2.    SAME YEARS AND DIFFERENT MONTHS
118 !              -------------------------------
119 !
120 IF ( (KYEARF==KYEARI) .AND. (KMONTHF/=KMONTHI) ) THEN
121   ! check chronological order
122   IF ( KMONTHF < KMONTHI ) THEN
123     PDIST=-999.
124     RETURN
125   END IF
126   !
127   ! cumulate the number of days for the months in between KMONTHF-1 and 
128   ! KMONTHI
129   IDAYS = 0
130   DO JMONTH = KMONTHI, KMONTHF-1
131     SELECT CASE (JMONTH)
132       CASE(4,6,9,11)
133         IDAYS=IDAYS+30
134       CASE(1,3,5,7:8,10,12)
135         IDAYS=IDAYS+31
136       CASE(2)
137         IF (MOD(KYEARI,4)==0) THEN 
138           IDAYS=IDAYS+29
139         ELSE
140           IDAYS=IDAYS+28
141         ENDIF
142     END SELECT
143   END DO  
144   !
145   ! compute the temporal distance
146   PDIST = ( IDAYS + KDAYF - KDAYI) * 86400. + PSECF - PSECI
147   !
148 END IF
149 !
150 !-------------------------------------------------------------------------------
151 !
152 !*       3.    DIFFERENT YEARS AND DIFFERENT MONTHS
153 !              ------------------------------------
154 !
155 IF ( (KYEARF/=KYEARI) .AND. (KMONTHF/=KMONTHI) ) THEN
156   ! check chronological order
157   IF ( KYEARF < KYEARI ) THEN
158     PDIST=-999.
159     RETURN
160   END IF
161   !
162   ! cumulate the number of days for the months in between KMONTHI and 
163   ! December
164   IDAYS = 0
165   DO JMONTH = KMONTHI, 12
166     SELECT CASE (JMONTH)
167       CASE(4,6,9,11)
168         IDAYS=IDAYS+30
169       CASE(1,3,5,7:8,10,12)
170         IDAYS=IDAYS+31
171       CASE(2)
172         IF (MOD(KYEARI,4)==0) THEN 
173           IDAYS=IDAYS+29
174         ELSE
175           IDAYS=IDAYS+28
176         ENDIF
177     END SELECT
178   END DO  
179   DO JMONTH = 1,KMONTHF-1
180     SELECT CASE (JMONTH)
181       CASE(4,6,9,11)
182         IDAYS=IDAYS+30
183       CASE(1,3,5,7:8,10,12)
184         IDAYS=IDAYS+31
185       CASE(2)
186         IF (MOD(KYEARF,4)==0) THEN 
187           IDAYS=IDAYS+29
188         ELSE
189           IDAYS=IDAYS+28
190         ENDIF
191     END SELECT
192   END DO  
193   ! add the number of days corresponding to full years between the two dates
194   DO JYEAR=KYEARI+1, KYEARF-1
195     IF (MOD(JYEAR,4)==0) THEN 
196       IDAYS=IDAYS+366
197     ELSE
198       IDAYS=IDAYS+365
199     END IF
200   END DO
201   !
202   ! compute the temporal distance
203   PDIST = ( IDAYS + KDAYF - KDAYI) * 86400. + PSECF - PSECI
204   !
205 END IF
206 !
207 !
208 !-------------------------------------------------------------------------------
209 !
210 END SUBROUTINE TEMPORAL_DIST