PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/WPS/ungrib/src/geth_idts.F

http://github.com/jbeezley/wrf-fire
FORTRAN Legacy | 311 lines | 182 code | 46 blank | 83 comment | 30 complexity | edd4834264fcd4b8492c8fe1f268eb93 MD5 | raw file
Possible License(s): AGPL-1.0
  1. subroutine geth_idts (ndate, odate, idts)
  2. implicit none
  3. !***********************************************************************
  4. !
  5. ! purpose - from 2 input mdates ('YYYY-MM-DD HH:MM:SS'), compute
  6. ! the time difference in seconds.
  7. !
  8. ! on entry - ndate - the new hdate.
  9. ! odate - the old hdate.
  10. !
  11. ! on exit - idts - the change in time in seconds.
  12. !
  13. !***********************************************************************
  14. character*(*) ndate, odate
  15. character*19 tdate
  16. integer idts
  17. integer olen, nlen
  18. !
  19. ! Local Variables
  20. !
  21. ! yrnew - indicates the year associated with "ndate"
  22. ! yrold - indicates the year associated with "odate"
  23. ! monew - indicates the month associated with "ndate"
  24. ! moold - indicates the month associated with "odate"
  25. ! dynew - indicates the day associated with "ndate"
  26. ! dyold - indicates the day associated with "odate"
  27. ! hrnew - indicates the hour associated with "ndate"
  28. ! hrold - indicates the hour associated with "odate"
  29. ! minew - indicates the minute associated with "ndate"
  30. ! miold - indicates the minute associated with "odate"
  31. ! scnew - indicates the second associated with "ndate"
  32. ! scold - indicates the second associated with "odate"
  33. ! i - loop counter
  34. ! mday - a list assigning the number of days in each month
  35. ! newhrs - the number of hours between "ndate" and 1901
  36. ! whole 24 hour days
  37. ! oldhrs - the number of hours between "odate" and 1901
  38. !
  39. integer yrnew, monew, dynew, hrnew, minew, scnew
  40. integer yrold, moold, dyold, hrold, miold, scold
  41. integer mday(12), i, newdys, olddys
  42. logical npass, opass
  43. integer isign
  44. ! External function:
  45. integer, external :: nfeb
  46. !************************* Subroutine Begin **************************
  47. if (odate.gt.ndate) then
  48. isign = -1
  49. tdate=ndate
  50. ndate=odate
  51. odate=tdate
  52. else
  53. isign = 1
  54. endif
  55. !
  56. ! Assign the number of days in a months
  57. !
  58. mday( 1) = 31
  59. mday( 2) = 28
  60. mday( 3) = 31
  61. mday( 4) = 30
  62. mday( 5) = 31
  63. mday( 6) = 30
  64. mday( 7) = 31
  65. mday( 8) = 31
  66. mday( 9) = 30
  67. mday(10) = 31
  68. mday(11) = 30
  69. mday(12) = 31
  70. !
  71. ! Break down old hdate into parts
  72. !
  73. hrold = 0
  74. miold = 0
  75. scold = 0
  76. olen = len(odate)
  77. read(odate(1:4), '(I4)', err=101) yrold
  78. read(odate(6:7), '(I2)', err=101) moold
  79. read(odate(9:10), '(I2)', err=101) dyold
  80. if (olen.ge.13) then
  81. read(odate(12:13),'(I2)', err=101) hrold
  82. if (olen.ge.16) then
  83. read(odate(15:16),'(I2)', err=101) miold
  84. if (olen.ge.19) then
  85. read(odate(18:19),'(I2)', err=101) scold
  86. endif
  87. endif
  88. endif
  89. !
  90. ! Break down new hdate into parts
  91. !
  92. hrnew = 0
  93. minew = 0
  94. scnew = 0
  95. nlen = len(ndate)
  96. read(ndate(1:4), '(I4)', err=102) yrnew
  97. read(ndate(6:7), '(I2)', err=102) monew
  98. read(ndate(9:10), '(I2)', err=102) dynew
  99. if (nlen.ge.13) then
  100. read(ndate(12:13),'(I2)', err=102) hrnew
  101. if (nlen.ge.16) then
  102. read(ndate(15:16),'(I2)', err=102) minew
  103. if (nlen.ge.19) then
  104. read(ndate(18:19),'(I2)', err=102) scnew
  105. endif
  106. endif
  107. endif
  108. !
  109. ! Check that the dates make sense.
  110. !
  111. npass = .true.
  112. opass = .true.
  113. ! Check that the month of NDATE makes sense.
  114. if ((monew.gt.12).or.(monew.lt.1)) then
  115. print*, 'GETH_IDTS: Month of NDATE = ', monew
  116. npass = .false.
  117. endif
  118. ! Check that the month of ODATE makes sense.
  119. if ((moold.gt.12).or.(moold.lt.1)) then
  120. print*, 'GETH_IDTS: Month of ODATE = ', moold
  121. opass = .false.
  122. endif
  123. ! Check that the day of NDATE makes sense.
  124. if (monew.ne.2) then
  125. ! ...... For all months but February
  126. if ((dynew.gt.mday(monew)).or.(dynew.lt.1)) then
  127. print*, 'GETH_IDTS: Day of NDATE = ', dynew
  128. npass = .false.
  129. endif
  130. elseif (monew.eq.2) then
  131. ! ...... For February
  132. if ((dynew .gt. nfeb(yrnew)).or.(dynew.lt.1)) then
  133. print*, 'GETH_IDTS: Day of NDATE = ', dynew
  134. npass = .false.
  135. endif
  136. endif
  137. ! Check that the day of ODATE makes sense.
  138. if (moold.ne.2) then
  139. ! ...... For all months but February
  140. if ((dyold.gt.mday(moold)).or.(dyold.lt.1)) then
  141. print*, 'GETH_IDTS: Day of ODATE = ', dyold
  142. opass = .false.
  143. endif
  144. elseif (moold.eq.2) then
  145. ! ....... For February
  146. if ((dyold .gt. nfeb(yrold)).or.(dyold .lt. 1)) then
  147. print*, 'GETH_IDTS: Day of ODATE = ', dyold
  148. opass = .false.
  149. endif
  150. endif
  151. ! Check that the hour of NDATE makes sense.
  152. if ((hrnew.gt.23).or.(hrnew.lt.0)) then
  153. print*, 'GETH_IDTS: Hour of NDATE = ', hrnew
  154. npass = .false.
  155. endif
  156. ! Check that the hour of ODATE makes sense.
  157. if ((hrold.gt.23).or.(hrold.lt.0)) then
  158. print*, 'GETH_IDTS: Hour of ODATE = ', hrold
  159. opass = .false.
  160. endif
  161. ! Check that the minute of NDATE makes sense.
  162. if ((minew.gt.59).or.(minew.lt.0)) then
  163. print*, 'GETH_IDTS: Minute of NDATE = ', minew
  164. npass = .false.
  165. endif
  166. ! Check that the minute of ODATE makes sense.
  167. if ((miold.gt.59).or.(miold.lt.0)) then
  168. print*, 'GETH_IDTS: Minute of ODATE = ', miold
  169. opass = .false.
  170. endif
  171. !
  172. ! Check that the second of NDATE makes sense.
  173. !
  174. if ((scnew.gt.59).or.(scnew.lt.0)) then
  175. print*, 'GETH_IDTS: SECOND of NDATE = ', scnew
  176. npass = .false.
  177. endif
  178. ! Check that the second of ODATE makes sense.
  179. if ((scold.gt.59).or.(scold.lt.0)) then
  180. print*, 'GETH_IDTS: Second of ODATE = ', scold
  181. opass = .false.
  182. endif
  183. if (.not. npass) then
  184. print*, 'Screwy NDATE: ', ndate(1:nlen)
  185. STOP 'Error_ndate'
  186. endif
  187. if (.not. opass) then
  188. print*, 'Screwy ODATE: ', odate(1:olen)
  189. STOP 'Error_odate'
  190. endif
  191. !
  192. ! Date Checks are completed. Continue.
  193. !
  194. !
  195. ! Compute number of days from 1 January ODATE, 00:00:00 until ndate
  196. ! Compute number of hours from 1 January ODATE, 00:00:00 until ndate
  197. ! Compute number of minutes from 1 January ODATE, 00:00:00 until ndate
  198. !
  199. newdys = 0
  200. do i = yrold, yrnew - 1
  201. newdys = newdys + (365 + (nfeb(i)-28))
  202. enddo
  203. if (monew .gt. 1) then
  204. mday(2) = nfeb(yrnew)
  205. do i = 1, monew - 1
  206. newdys = newdys + mday(i)
  207. enddo
  208. mday(2) = 28
  209. end if
  210. newdys = newdys + dynew-1
  211. !
  212. ! Compute number of hours from 1 January ODATE, 00:00:00 until odate
  213. ! Compute number of minutes from 1 January ODATE, 00:00:00 until odate
  214. !
  215. olddys = 0
  216. if (moold .gt. 1) then
  217. mday(2) = nfeb(yrold)
  218. do i = 1, moold - 1
  219. olddys = olddys + mday(i)
  220. enddo
  221. mday(2) = 28
  222. end if
  223. olddys = olddys + dyold-1
  224. !
  225. ! Determine the time difference in seconds
  226. !
  227. idts = (newdys - olddys) * 86400
  228. idts = idts + (hrnew - hrold) * 3600
  229. idts = idts + (minew - miold) * 60
  230. idts = idts + (scnew - scold)
  231. if (isign .eq. -1) then
  232. tdate=ndate
  233. ndate=odate
  234. odate=tdate
  235. idts = idts * isign
  236. endif
  237. return
  238. 101 write(6,*) 'Error reading odate. odate = ',odate
  239. write(6,*) 'Most likely an error in namelist.wps'
  240. stop 'geth_idts 101'
  241. 102 write(6,*) 'Error reading ndate. ndate = ',ndate
  242. write(6,*) 'Most likely an error in namelist.wps'
  243. stop 'geth_idts 102'
  244. !************************** Subroutine End ***************************
  245. end
  246. integer function nfeb(year)
  247. !
  248. ! Compute the number of days in February for the given year.
  249. !
  250. implicit none
  251. integer, intent(in) :: year ! Four-digit year
  252. nfeb = 28 ! By default, February has 28 days ...
  253. if (mod(year,4).eq.0) then
  254. nfeb = 29 ! But every four years, it has 29 days ...
  255. if (mod(year,100).eq.0) then
  256. nfeb = 28 ! Except every 100 years, when it has 28 days ...
  257. if (mod(year,400).eq.0) then
  258. nfeb = 29 ! Except every 400 years, when it has 29 days ...
  259. if (mod(year,3600).eq.0) then
  260. nfeb = 28 ! Except every 3600 years, when it has 28 days.
  261. endif
  262. endif
  263. endif
  264. endif
  265. end function nfeb