Philippe 23/02/2016: lfi2cdf: modif for JPHEXT/=1
[MNH-git_open_source-lfs.git] / tools / diachro / src / EXTRACTDIA / temporal_dist_for_ext.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_FOR_EXT
8 !     #########################
9 INTERFACE
10       SUBROUTINE TEMPORAL_DIST_FOR_EXT(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 INTEGER,    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 INTEGER,    INTENT(IN) :: PSECI   ! number of seconds since date at 00 UTC
23                                ! of Initial date
24 INTEGER, INTENT(OUT):: PDIST   ! temporal distance in secunds 
25                                         !between the final and initial date
26 !
27 END SUBROUTINE TEMPORAL_DIST_FOR_EXT 
28 !
29 END INTERFACE
30
31 END MODULE MODI_TEMPORAL_DIST_FOR_EXT 
32 !
33 !     #############################################################
34       SUBROUTINE TEMPORAL_DIST_FOR_EXT(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 !!      Modification02/09/03 (N.Asencio) PDIST must be in DOUBLE PRECISION 
77 !!                           for several years gap
78 !-------------------------------------------------------------------------------
79 !
80 !*       0.    DECLARATIONS
81 !              ------------
82 !
83 IMPLICIT NONE
84 !
85 !*       0.1   Declaration of arguments
86 !              ------------------------
87 INTEGER, INTENT(IN) :: KYEARF  ! year of Final date
88 INTEGER, INTENT(IN) :: KMONTHF ! month of Final date
89 INTEGER, INTENT(IN) :: KDAYF   ! day of Final date
90 INTEGER,    INTENT(IN) :: PSECF   ! number of seconds since date at 00 UTC
91                                ! of Final date
92 INTEGER, INTENT(IN) :: KYEARI  ! year of Initial date
93 INTEGER, INTENT(IN) :: KMONTHI ! month of Initial date
94 INTEGER, INTENT(IN) :: KDAYI   ! day of Initial date
95 INTEGER,    INTENT(IN) :: PSECI   ! number of seconds since date at 00 UTC
96                                ! of Initial date
97 INTEGER, INTENT(OUT):: PDIST   ! temporal distance in secunds 
98                                         !between the final and initial date
99 !
100 !*       0.2   Declaration of local variables
101 !              ------------------------------
102 !
103 INTEGER :: IDAYS  ! number of days between the two dates
104 INTEGER :: JMONTH,JYEAR ! loop index on months or years 
105 !
106 !-------------------------------------------------------------------------------
107 !
108 !*       1.    SAME YEARS AND SAME MONTHS
109 !              --------------------------
110 !
111 IF ( (KYEARF==KYEARI) .AND. (KMONTHF==KMONTHI) ) THEN
112   PDIST = ( KDAYF-KDAYI) * 86400 + PSECF - PSECI
113   ! check chronological order
114   IF (PDIST < 0.) PDIST=-999
115 END IF
116 !
117 !-------------------------------------------------------------------------------
118 !
119 !*       2.    SAME YEARS AND DIFFERENT MONTHS
120 !              -------------------------------
121 !
122 IF ( (KYEARF==KYEARI) .AND. (KMONTHF/=KMONTHI) ) THEN
123   ! check chronological order
124   IF ( KMONTHF < KMONTHI ) THEN
125     PDIST=-999
126     RETURN
127   END IF
128   !
129   ! cumulate the number of days for the months in between KMONTHF-1 and 
130   ! KMONTHI
131   IDAYS = 0
132   DO JMONTH = KMONTHI, KMONTHF-1
133     SELECT CASE (JMONTH)
134       CASE(4,6,9,11)
135         IDAYS=IDAYS+30
136       CASE(1,3,5,7:8,10,12)
137         IDAYS=IDAYS+31
138       CASE(2)
139         IF (MOD(KYEARI,4)==0) THEN 
140           IDAYS=IDAYS+29
141         ELSE
142           IDAYS=IDAYS+28
143         ENDIF
144     END SELECT
145   END DO  
146   !
147   ! compute the temporal distance
148   PDIST = ( IDAYS + KDAYF - KDAYI) * 86400. + PSECF - PSECI
149   !
150 END IF
151 !
152 !-------------------------------------------------------------------------------
153 !
154 !*       3.    DIFFERENT YEARS
155 !              ---------------
156 !
157 IF (KYEARF/=KYEARI) THEN
158   ! check chronological order
159   IF ( KYEARF < KYEARI ) THEN
160     PDIST=-999
161     RETURN
162   END IF
163   !
164   ! cumulate the number of days for the months in between KMONTHI and 
165   ! December
166   IDAYS = 0
167   DO JMONTH = KMONTHI, 12
168     SELECT CASE (JMONTH)
169       CASE(4,6,9,11)
170         IDAYS=IDAYS+30
171       CASE(1,3,5,7:8,10,12)
172         IDAYS=IDAYS+31
173       CASE(2)
174         IF (MOD(KYEARI,4)==0) THEN 
175           IDAYS=IDAYS+29
176         ELSE
177           IDAYS=IDAYS+28
178         ENDIF
179     END SELECT
180   END DO  
181   DO JMONTH = 1,KMONTHF-1
182     SELECT CASE (JMONTH)
183       CASE(4,6,9,11)
184         IDAYS=IDAYS+30
185       CASE(1,3,5,7:8,10,12)
186         IDAYS=IDAYS+31
187       CASE(2)
188         IF (MOD(KYEARF,4)==0) THEN 
189           IDAYS=IDAYS+29
190         ELSE
191           IDAYS=IDAYS+28
192         ENDIF
193     END SELECT
194   END DO  
195   ! add the number of days corresponding to full years between the two dates
196   DO JYEAR=KYEARI+1, KYEARF-1
197     IF (MOD(JYEAR,4)==0) THEN 
198       IDAYS=IDAYS+366
199     ELSE
200       IDAYS=IDAYS+365
201     END IF
202   END DO
203   !
204   ! compute the temporal distance
205   PDIST = ( IDAYS + KDAYF - KDAYI) * 86400 + PSECF - PSECI
206   !
207 END IF
208 !
209 !
210 !-------------------------------------------------------------------------------
211 !
212 END SUBROUTINE TEMPORAL_DIST_FOR_EXT