PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wrfv2_fire/external/io_netcdf/diffwrf.F90

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