PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wrfv2_fire/external/io_int/diffwrf.F

http://github.com/jbeezley/wrf-fire
FORTRAN Legacy | 461 lines | 322 code | 71 blank | 68 comment | 31 complexity | a29838cf04ddf2558a7fd32361fc747b MD5 | raw file
Possible License(s): AGPL-1.0
  1. module read_util_module
  2. #ifdef crayx1
  3. #define iargc ipxfargc
  4. #endif
  5. contains
  6. #ifdef crayx1
  7. subroutine getarg(i, harg)
  8. implicit none
  9. character(len=*) :: harg
  10. integer :: ierr, ilen, i
  11. call pxfgetarg(i, harg, ilen, ierr)
  12. return
  13. end subroutine getarg
  14. #endif
  15. subroutine arguments(v2file, lmore)
  16. implicit none
  17. character(len=*) :: v2file
  18. character(len=120) :: harg
  19. logical :: lmore
  20. integer :: ierr, i, numarg
  21. integer, external :: iargc
  22. numarg = iargc()
  23. i = 1
  24. lmore = .false.
  25. do while ( i < numarg)
  26. call getarg(i, harg)
  27. print*, 'harg = ', trim(harg)
  28. if (harg == "-v") then
  29. i = i + 1
  30. lmore = .true.
  31. elseif (harg == "-h") then
  32. call help
  33. endif
  34. enddo
  35. call getarg(i,harg)
  36. v2file = harg
  37. end subroutine arguments
  38. subroutine help
  39. implicit none
  40. character(len=120) :: cmd
  41. call getarg(0, cmd)
  42. write(*,'(/,"Usage: ", A, " [-v] v2file ")') trim(cmd)
  43. write(*,'(8x, "-v : Print extra info")')
  44. write(*,'(8x, "v3file : MM5v3 file name to read.")')
  45. write(*,'(8x, "-h : print this help message and exit.",/)')
  46. stop
  47. end subroutine help
  48. end module read_util_module
  49. program readv3
  50. use read_util_module
  51. use module_ext_internal
  52. implicit none
  53. #include "wrf_status_codes.h"
  54. #include "wrf_io_flags.h"
  55. character(len=255) :: flnm
  56. character(len=255) :: flnm2
  57. character(len=120) :: arg3
  58. character(len=19) :: DateStr
  59. character(len=19) :: DateStr2
  60. character(len=31) :: VarName
  61. character(len=31) :: VarName2
  62. integer dh1, dh2
  63. integer :: flag, flag2
  64. integer :: iunit, iunit2
  65. integer :: WrfType, WrfType2
  66. integer :: i,j,k
  67. integer :: levlim
  68. integer :: cross
  69. integer :: ndim, ndim2
  70. real :: time, time2
  71. real*8 :: a, b
  72. real*8 :: sumE, sum1, sum2, diff1, diff2, serr, perr, rmse, rms1, rms2, tmp1, tmp2
  73. integer digits,d1, d2
  74. integer, dimension(4) :: start_index, end_index, start_index2, end_index2
  75. integer , Dimension(3) :: MemS,MemE,PatS,PatE
  76. character (len= 4) :: staggering, staggering2
  77. character (len= 3) :: ordering, ordering2, ord
  78. character (len=24) :: start_date, start_date2
  79. character (len=24) :: current_date, current_date2
  80. character (len=31) :: name, name2, tmpname
  81. character (len=25) :: units, units2
  82. character (len=46) :: description, description2
  83. character (len=80), dimension(3) :: dimnames
  84. integer :: l, n
  85. integer :: ikdiffs, ifdiffs
  86. real, allocatable, dimension(:,:,:,:) :: data,data2
  87. integer :: ierr, ierr2, ier, ier2, Status, Status_next_time, Status_next_time2, Status_next_var, Status_next_var_2
  88. logical :: newtime = .TRUE.
  89. logical :: justplot, efound
  90. integer, external :: iargc
  91. logical, external :: iveceq
  92. levlim = -1
  93. call ext_int_ioinit(' ', Status)
  94. Justplot = .false.
  95. ! get arguments
  96. if ( iargc() .ge. 2 ) then
  97. call getarg(1,flnm)
  98. call getarg(2,flnm2)
  99. ierr = 0
  100. call ext_int_open_for_read( trim(flnm), 0, 0, "", dh1, Status)
  101. if ( Status /= 0 ) then
  102. print*,'error opening ',flnm, ' Status = ', Status ; stop
  103. endif
  104. call ext_int_open_for_read( trim(flnm2), 0, 0, "", dh2, Status)
  105. if ( Status /= 0 ) go to 923
  106. goto 924
  107. 923 continue
  108. ! bounce here if second name is not openable -- this would mean that
  109. ! it is a field name instead.
  110. print*,'could not open ',flnm2
  111. name = flnm2
  112. Justplot = .true.
  113. 924 continue
  114. if ( iargc() .eq. 3 ) then
  115. call getarg(3,arg3)
  116. read(arg3,*)levlim
  117. print*,'LEVLIM = ',LEVLIM
  118. endif
  119. else
  120. print*,'Usage: command file1 file2'
  121. stop
  122. endif
  123. print*,'Just plot ',Justplot
  124. if ( Justplot ) then
  125. print*, 'flnm = ', trim(flnm)
  126. call ext_int_get_next_time(dh1, DateStr, Status_next_time)
  127. DO WHILE ( Status_next_time .eq. 0 )
  128. write(*,*)'Next Time ',TRIM(Datestr)
  129. call ext_int_get_next_var (dh1, VarName, Status_next_var)
  130. DO WHILE ( Status_next_var .eq. 0 )
  131. ! write(*,*)'Next Var |',TRIM(VarName),'|'
  132. start_index = 1
  133. end_index = 1
  134. call ext_int_get_var_info (dh1,VarName,ndim,ordering,staggering,start_index,end_index, WrfType, ierr )
  135. if(WrfType /= WRF_REAL .AND. WrfType /= WRF_DOUBLE) then
  136. call ext_int_get_next_var (dh1, VarName, Status_next_var)
  137. cycle
  138. endif
  139. write(*,'(A9,1x,I1,3(1x,I5),1x,A,1x,A)')&
  140. VarName, ndim, end_index(1), end_index(2), end_index(3), &
  141. trim(ordering), trim(DateStr)
  142. if ( VarName .eq. name ) then
  143. write(*,*)'Writing fort.88 file for ', trim(name)
  144. allocate(data(end_index(1), end_index(2), end_index(3), 1))
  145. if ( ndim .eq. 3 ) then
  146. ord = 'XYZ'
  147. else if ( ndim .eq. 2 ) then
  148. ord = 'XY'
  149. else if ( ndim .eq. 1 ) then
  150. ord = 'Z'
  151. else if ( ndim .eq. 0 ) then
  152. ord = '0'
  153. endif
  154. call ext_int_read_field(dh1,DateStr,TRIM(name),data,WRF_REAL,0,0,0,ord, &
  155. staggering, &
  156. dimnames, &
  157. start_index,end_index, & !dom
  158. start_index,end_index, & !mem
  159. start_index,end_index, & !pat
  160. ierr)
  161. if ( ierr/=0 ) then
  162. write(*,*)'error reading data record'
  163. write(*,*)' ndim = ', ndim
  164. write(*,*)' end_index(1) ',end_index(1)
  165. write(*,*)' end_index(2) ',end_index(2)
  166. write(*,*)' end_index(3) ',end_index(3)
  167. endif
  168. IF ( ndim .eq. 3 ) THEN
  169. do k = start_index(2), end_index(2)
  170. if ( levlim .eq. -1 .or. k .eq. levlim ) then
  171. write(88,*)end_index(1),end_index(3),' ',trim(name),' ',k,' time ',n
  172. do j = 1, end_index(3)
  173. do i = 1, end_index(1)
  174. write(88,*) data(i,k,j,1)
  175. enddo
  176. enddo
  177. endif
  178. enddo
  179. ELSE IF ( ndim .eq. 2 ) THEN
  180. k = 1
  181. write(88,*)end_index(1),end_index(2),' ',trim(name),' ',k,' time ',n
  182. do j = 1, end_index(2)
  183. do i = 1, end_index(1)
  184. write(88,*) data(i,j,1,1)
  185. enddo
  186. enddo
  187. ENDIF
  188. deallocate(data)
  189. endif
  190. call ext_int_get_next_var (dh1, VarName, Status_next_var)
  191. enddo
  192. call ext_int_get_next_time(dh1, DateStr, Status_next_time)
  193. enddo
  194. else
  195. write (6,FMT='(4A)') 'Diffing ',trim(flnm),' ',trim(flnm2)
  196. call ext_int_get_next_time(dh1, DateStr, Status_next_time)
  197. call ext_int_get_next_time(dh2, DateStr2, Status_next_time2)
  198. IF ( DateStr .NE. DateStr2 ) THEN
  199. print*,'They differ big time. Dates do not match'
  200. print*,' ',flnm,' ',DateStr
  201. print*,' ',flnm2,' ',DateStr2
  202. Status_next_time = 1
  203. ENDIF
  204. DO WHILE ( Status_next_time .eq. 0 .AND. Status_next_time2 .eq. 0 )
  205. !write(*,*)'Next Time ',TRIM(Datestr)
  206. print 76
  207. call ext_int_get_next_var (dh1, VarName, Status_next_var)
  208. call ext_int_get_next_var (dh2, VarName, Status_next_var)
  209. DO WHILE ( Status_next_var .eq. 0 )
  210. !write(*,*)'Next Var |',TRIM(VarName),'|'
  211. start_index = 1
  212. end_index = 1
  213. call ext_int_get_var_info (dh1,VarName,ndim,ordering,staggering,start_index,end_index, WrfType, ierr )
  214. call ext_int_get_var_info (dh2,VarName,ndim2,ordering2,staggering2,start_index2,end_index2, WrfType2, ierr2 )
  215. !write(*,*)'ierr , err2 ',TRIM(VarName), ierr , ierr2, ' ',ordering, ' ', ordering2
  216. IF ( ierr /= 0 ) THEN
  217. write(*,*)'Big difference: ',VarName,' not found in ',flnm2
  218. GOTO 1234
  219. ENDIF
  220. IF ( ndim /= ndim2 ) THEN
  221. write(*,*)'Big difference: Number of dimensions for ',Varname,' differs in ',flnm2,'(',ndim,') /= (',ndim2
  222. GOTO 1234
  223. ENDIF
  224. IF ( WrfType /= WrfType2 ) THEN
  225. write(*,*)'Big difference: The types do not match', WrfType, WrfType2
  226. GOTO 1234
  227. ENDIF
  228. if( WrfType == WRF_REAL) then
  229. DO i = 1, ndim
  230. IF ( end_index(i) /= end_index2(i) ) THEN
  231. write(*,*)'Big difference: dim ',i,' lengths differ for ',Varname,' differ in ',flnm2
  232. GOTO 1234
  233. ENDIF
  234. ENDDO
  235. DO i = ndim+1,3
  236. start_index(i) = 1
  237. end_index(i) = 1
  238. start_index2(i) = 1
  239. end_index2(i) = 1
  240. ENDDO
  241. ! write(*,'(A9,1x,I1,3(1x,I3),1x,A,1x,A)')&
  242. ! VarName, ndim, end_index(1), end_index(2), end_index(3), &
  243. ! trim(ordering), trim(DateStr)
  244. allocate(data (end_index(1), end_index(2), end_index(3), 1))
  245. allocate(data2(end_index(1), end_index(2), end_index(3), 1))
  246. if ( ndim .eq. 3 ) then
  247. ord = 'XYZ'
  248. else if ( ndim .eq. 2 ) then
  249. ord = 'XY'
  250. else if ( ndim .eq. 1 ) then
  251. ord = 'Z'
  252. else if ( ndim .eq. 0 ) then
  253. ord = '0'
  254. endif
  255. call ext_int_read_field(dh1,DateStr,TRIM(VarName),data,WRF_REAL,0,0,0,ord, &
  256. staggering, &
  257. dimnames, &
  258. start_index,end_index, & !dom
  259. start_index,end_index, & !mem
  260. start_index,end_index, & !pat
  261. ierr)
  262. IF ( ierr /= 0 ) THEN
  263. write(*,*)'Error reading ',Varname,' from ',flnm
  264. write(*,*)' ndim = ', ndim
  265. write(*,*)' end_index(1) ',end_index(1)
  266. write(*,*)' end_index(2) ',end_index(2)
  267. write(*,*)' end_index(3) ',end_index(3)
  268. ENDIF
  269. call ext_int_read_field(dh2,DateStr,TRIM(VarName),data2,WRF_REAL,0,0,0,ord, &
  270. staggering, &
  271. dimnames, &
  272. start_index,end_index, & !dom
  273. start_index,end_index, & !mem
  274. start_index,end_index, & !pat
  275. ierr)
  276. IF ( ierr /= 0 ) THEN
  277. write(*,*)'Error reading ',Varname,' from ',flnm2
  278. write(*,*)' ndim = ', ndim
  279. write(*,*)' end_index(1) ',end_index(1)
  280. write(*,*)' end_index(2) ',end_index(2)
  281. write(*,*)' end_index(3) ',end_index(3)
  282. ENDIF
  283. IFDIFFS=0
  284. sumE = 0.0
  285. sum1 = 0.0
  286. sum2 = 0.0
  287. diff1 = 0.0
  288. diff2 = 0.0
  289. n = 0
  290. DO K = 1,end_index(3)-start_index(3)+1
  291. IF (LEVLIM.EQ.-1.OR.K.EQ.LEVLIM.OR.NDIM.eq.2) THEN
  292. cross = 0
  293. IKDIFFS = 0
  294. do i = 1, end_index(1)-cross
  295. do j = 1, end_index(2)-cross
  296. a = data(I,J,K,1)
  297. b = data2(I,J,K,1)
  298. ! borrowed from Thomas Oppe's comp program
  299. sumE = sumE + ( a - b ) * ( a - b )
  300. sum1 = sum1 + a * a
  301. sum2 = sum2 + b * b
  302. diff1 = max ( diff1 , abs ( a - b ) )
  303. diff2 = max ( diff2 , abs ( b ) )
  304. n = n + 1
  305. IF (a .ne. b) then
  306. IKDIFFS = IKDIFFS + 1
  307. IFDIFFS = IFDIFFS + 1
  308. ENDIF
  309. ENDDO
  310. ENDDO
  311. ENDIF
  312. enddo
  313. if(n.eq.0) n=1
  314. rmsE = sqrt ( sumE / dble( n ) )
  315. rms1 = sqrt ( sum1 / dble( n ) )
  316. rms2 = sqrt ( sum2 / dble( n ) )
  317. serr = 0.0
  318. IF ( sum2 .GT. 0.0d0 ) THEN
  319. serr = sqrt ( sumE / sum2 )
  320. ELSE
  321. IF ( sumE .GT. 0.0d0 ) serr = 1.0
  322. ENDIF
  323. perr = 0.0
  324. IF ( diff2 .GT. 0.0d0 ) THEN
  325. perr = diff1/diff2
  326. ELSE
  327. IF ( diff1 .GT. 0.0d0 ) perr = 1.0
  328. ENDIF
  329. digits = 0
  330. IF ( rms1 - rms2 .EQ. 0.0d0 ) THEN
  331. digits = 15
  332. ELSE
  333. IF ( rms2 .NE. 0 ) THEN
  334. tmp1 = 1.0d0/( ( abs( rms1 - rms2 ) ) / rms2 )
  335. IF ( tmp1 .NE. 0 ) THEN
  336. digits = log10(tmp1)
  337. ENDIF
  338. ENDIF
  339. ENDIF
  340. IF (IFDIFFS .NE. 0 ) THEN
  341. ! create the fort.88 and fort.98 files because regression scripts will
  342. ! look for these to see if there were differences.
  343. write(88,*)trim(varname)
  344. write(98,*)trim(varname)
  345. PRINT 77,trim(varname), IFDIFFS, ndim, rms1, rms2, digits, rmsE, perr
  346. 76 FORMAT (5x,'Field ',2x,'Ndifs',4x,'Dims ',6x,'RMS (1)',12x,'RMS (2)',5x,'DIGITS',4x,'RMSE',5x,'pntwise max')
  347. 77 FORMAT ( A10,1x,I9,2x,I3,1x,e18.10,1x,e18.10,1x,i3,1x,e12.4,1x,e12.4 )
  348. ENDIF
  349. deallocate(data)
  350. deallocate(data2)
  351. endif
  352. 1234 CONTINUE
  353. call ext_int_get_next_var (dh1, VarName, Status_next_var)
  354. call ext_int_get_next_var (dh2, VarName, Status_next_var)
  355. enddo
  356. call ext_int_get_next_time(dh1, DateStr, Status_next_time)
  357. call ext_int_get_next_time(dh2, DateStr2, Status_next_time2)
  358. IF ( DateStr .NE. DateStr2 ) THEN
  359. print*,'They differ big time. Dates do not match'
  360. print*,'They differ big time. Dates do not match'
  361. print*,' ',flnm,' ',DateStr
  362. print*,' ',flnm2,' ',DateStr2
  363. Status_next_time = 1
  364. ENDIF
  365. enddo
  366. endif
  367. end program readv3
  368. logical function iveceq( a, b, n )
  369. implicit none
  370. integer n
  371. integer a(n), b(n)
  372. integer i
  373. iveceq = .true.
  374. do i = 1,n
  375. if ( a(i) .ne. b(i) ) iveceq = .false.
  376. enddo
  377. return
  378. end function iveceq
  379. ! stubs for routines called by module_wrf_error (used by this implementation of IO api)
  380. SUBROUTINE wrf_abort
  381. STOP
  382. END SUBROUTINE wrf_abort
  383. SUBROUTINE get_current_time_string( time_str )
  384. CHARACTER(LEN=*), INTENT(OUT) :: time_str
  385. time_str = ''
  386. END SUBROUTINE get_current_time_string
  387. SUBROUTINE get_current_grid_name( grid_str )
  388. CHARACTER(LEN=*), INTENT(OUT) :: grid_str
  389. grid_str = ''
  390. END SUBROUTINE get_current_grid_name