1 module read_util_module
5 subroutine arguments(v2file, lmore)
7 character(len=*) :: v2file
8 character(len=120) :: harg
11 integer :: ierr, i, numarg
13 numarg = command_argument_count()
18 do while ( i < numarg)
19 call get_command_argument(number=i, value=harg)
20 print*, 'harg = ', trim(harg)
22 if (harg == "-v") then
25 elseif (harg == "-h") then
31 call get_command_argument(number=i, value=harg)
33 end subroutine arguments
37 character(len=120) :: cmd
38 call get_command_argument(number=0, value=cmd)
40 write(*,'(/,"Usage: ", A, " [-v] v2file ")') trim(cmd)
41 write(*,'(8x, "-v : Print extra info")')
42 write(*,'(8x, "v3file : MM5v3 file name to read.")')
43 write(*,'(8x, "-h : print this help message and exit.",/)')
46 end module read_util_module
54 #include "wrf_status_codes.h"
56 character(len=255) :: flnm
57 character(len=255) :: flnm2
58 character(len=120) :: arg3
59 character(len=19) :: DateStr
60 character(len=19) :: DateStr2
61 character(len=31) :: VarName
62 character(len=31) :: VarName2
65 integer :: flag, flag2
66 integer :: iunit, iunit2
71 integer :: ndim, ndim2
72 integer :: WrfType, WrfType2
75 real*8 :: sumE, sum1, sum2, diff1, diff2, serr, perr, rmse, rms1, rms2, tmp1, tmp2
77 integer, dimension(4) :: start_index, end_index, start_index2, end_index2
78 integer , Dimension(3) :: MemS,MemE,PatS,PatE
79 character (len= 4) :: staggering, staggering2
80 character (len= 3) :: ordering, ordering2, ord
81 character (len=24) :: start_date, start_date2
82 character (len=24) :: current_date, current_date2
83 character (len=31) :: name, name2, tmpname
84 character (len=25) :: units, units2
85 character (len=46) :: description, description2
87 character (len=80), dimension(3) :: dimnames
88 character (len=80) :: SysDepInfo
90 logical :: first, searchcoords
91 integer :: l, n, ntimes
92 integer :: ikdiffs, ifdiffs
93 integer :: icenter, prev_icenter, jcenter, prev_jcenter,ntries
94 real :: searchlat, searchlong
96 real, allocatable, dimension(:,:,:,:) :: data,data2
97 real, allocatable, dimension(:,:) :: xlat,xlong
99 integer :: ierr, ierr2, ier, ier2, Status, Status_next_time, Status_next_time2, Status_next_var, Status_next_var_2
102 logical :: newtime = .TRUE.
103 logical :: justplot, efound
105 logical, external :: iveceq
109 call ext_ncdpar_ioinit(SysDepInfo,Status)
110 call set_wrf_debug_level ( 1 )
112 nargs = command_argument_count()
115 searchcoords = .false.
117 if ( nargs .ge. 2 ) then
118 call get_command_argument(number=1, value=flnm)
119 call get_command_argument(number=2, value=flnm2)
120 IF ( flnm2(1:4) .EQ. '-lat' ) THEN
121 print*,'reading ',TRIM(flnm2(5:))
122 read(flnm2(5:),*)searchlat
123 call get_command_argument(number=3, value=flnm2)
124 IF ( flnm2(1:5) .EQ. '-long' ) THEN
125 print*,'reading ',TRIM(flnm2(6:))
126 read(flnm2(6:),*)searchlong
128 write(*,*)'missing -long argument (no spaces after -lat or -long, either)'
133 searchcoords = .true.
134 call ext_ncdpar_open_for_read( trim(flnm), 0, 0, "", dh1, Status)
138 call ext_ncdpar_open_for_read( trim(flnm), 0, 0, "", dh1, Status)
139 if ( Status /= 0 ) then
140 print*,'error opening ',flnm, ' Status = ', Status ; stop
142 call ext_ncdpar_open_for_read( trim(flnm2), 0, 0, "", dh2, Status)
143 if ( Status /= 0 ) go to 923
147 ! bounce here if second name is not openable -- this would mean that
148 ! it is a field name instead.
150 print*,'could not open ',flnm2
154 if ( nargs .eq. 3 ) then
155 call get_command_argument(number=3, value=arg3)
157 print*,'LEVLIM = ',LEVLIM
160 print*,'Usage: command file1 file2'
164 print*,'Just plot ',Justplot
167 print*, 'flnm = ', trim(flnm)
170 call ext_ncdpar_get_next_time(dh1, DateStr, Status_next_time)
173 DO WHILE ( Status_next_time .eq. 0 )
174 write(*,*)'Next Time ',TRIM(Datestr)
176 call ext_ncdpar_get_next_var (dh1, VarName, Status_next_var)
177 DO WHILE ( Status_next_var .eq. 0 )
178 ! write(*,*)'Next Var |',TRIM(VarName),'|'
182 call ext_ncdpar_get_var_info (dh1,VarName,ndim,ordering,staggering,start_index,end_index, WrfType, ierr )
183 if(WrfType /= WRF_REAL .AND. WrfType /= WRF_DOUBLE) then
184 call ext_ncdpar_get_next_var (dh1, VarName, Status_next_var)
187 IF ( .NOT. searchcoords ) THEN
188 write(*,'(A9,1x,I1,3(1x,I5),1x,A,1x,A)')&
189 VarName, ndim, end_index(1), end_index(2), end_index(3), &
190 trim(ordering), trim(DateStr)
193 if ( VarName .eq. name .OR. TRIM(VarName) .EQ. 'XLAT' .OR. TRIM(VarName) .EQ. 'XLONG' ) then
194 write(*,*)'Writing fort.88 file for ', trim(name)
196 allocate(data(end_index(1), end_index(2), end_index(3), 1))
198 if ( ndim .eq. 3 ) then
200 else if ( ndim .eq. 2 ) then
202 else if ( ndim .eq. 1 ) then
204 else if ( ndim .eq. 0 ) then
208 call ext_ncdpar_read_field(dh1,DateStr,TRIM(VarName),data,WRF_REAL,0,0,0,ord, &
209 staggering, dimnames , &
210 start_index,end_index, & !dom
211 start_index,end_index, & !mem
212 start_index,end_index, & !pat
216 write(*,*)'error reading data record'
217 write(*,*)' ndim = ', ndim
218 write(*,*)' end_index(1) ',end_index(1)
219 write(*,*)' end_index(2) ',end_index(2)
220 write(*,*)' end_index(3) ',end_index(3)
223 write(*,*)'name: ',TRIM(VarName)
224 IF ( TRIM(VarName) .EQ. 'XLAT' .AND. .NOT. ALLOCATED(xlat)) THEN
225 write(*,*)'allocating xlat'
226 ALLOCATE(xlat(end_index(1), end_index(2)))
229 IF ( TRIM(VarName) .EQ. 'XLONG' .AND. .NOT. ALLOCATED(xlong)) THEN
230 write(*,*)'allocating xlong'
231 ALLOCATE(xlong(end_index(1), end_index(2)))
232 xlong = data(:,:,1,1)
236 if ( VarName .eq. name ) then
238 ! uncomment this to have the code give i-slices
239 do i = 1, end_index(1)
240 if ( levlim .eq. -1 .or. i .eq. levlim ) then
241 write(88,*)end_index(2),end_index(3),' ',trim(name),' ',k,' time ',TRIM(Datestr)
242 do k = start_index(3), end_index(3)
243 do j = 1, end_index(2)
244 write(88,*) data(i,j,k,1)
251 do k = start_index(3), end_index(3)
252 if ( levlim .eq. -1 .or. k .eq. levlim ) then
253 write(88,*)end_index(1),end_index(2),' ',trim(name),' ',k,' time ',TRIM(Datestr)
254 do j = 1, end_index(2)
255 do i = 1, end_index(1)
256 write(88,*) data(i,j,k,1)
266 call ext_ncdpar_get_next_var (dh1, VarName, Status_next_var)
267 IF ( ntimes .EQ. 1 .AND. ALLOCATED(xlong) .AND. ALLOCATED(xlat) .AND. first ) THEN
274 DO WHILE ( ntries .LT. 10 .AND. (icenter .NE. prev_icenter .OR. jcenter .NE. prev_jcenter ))
275 prev_icenter = icenter
276 prev_jcenter = jcenter
277 DO j = start_index(2), end_index(2)-1
278 IF ( xlat(icenter,j) .LE. searchlat .AND. searchlat .LT. xlat(icenter,j+1) ) THEN
280 !write(*,*)'xlat ',ntries,icenter,jcenter,xlat(icenter,j),searchlat
284 DO i = start_index(1), end_index(1)-1
285 IF ( xlong(i,jcenter) .LE. searchlong .AND. searchlong .LT. xlong(i+1,jcenter)) THEN
287 !write(*,*)'xlon ',ntries,icenter,jcenter,xlong(i,jcenter),searchlong
293 write(*,*)'Lon ',searchlong,' Lat ',searchlat,' : ',icenter,jcenter
294 write(*,*)'Coordinates at that point ',xlong(icenter,jcenter),xlat(icenter,jcenter)
295 write(*,*)'Coordinates at next point ',xlong(icenter+1,jcenter+1),xlat(icenter+1,jcenter+1)
296 write(*,*)'Ntries : ',ntries
297 if ( ntries .GE. 10 ) write(*,*)'max tries exceeded. Probably did not find'
300 call ext_ncdpar_get_next_time(dh1, DateStr, Status_next_time)
303 write (6,FMT='(4A)') 'Diffing ',trim(flnm),' ',trim(flnm2)
305 call ext_ncdpar_get_next_time(dh1, DateStr, Status_next_time)
306 call ext_ncdpar_get_next_time(dh2, DateStr2, Status_next_time2)
308 IF ( DateStr .NE. DateStr2 ) THEN
309 print*,'They differ big time. Dates do not match'
310 print*,' ',flnm,' ',DateStr
311 print*,' ',flnm2,' ',DateStr2
315 DO WHILE ( Status_next_time .eq. 0 .AND. Status_next_time2 .eq. 0 )
316 write(*,*)'Next Time ',TRIM(Datestr)
318 call ext_ncdpar_get_next_var (dh1, VarName, Status_next_var)
319 DO WHILE ( Status_next_var .eq. 0 )
320 ! write(*,*)'Next Var |',TRIM(VarName),'|'
327 call ext_ncdpar_get_var_info (dh1,VarName,ndim,ordering,staggering,start_index,end_index, WrfType, ierr )
328 call ext_ncdpar_get_var_info (dh2,VarName,ndim2,ordering2,staggering2,start_index2,end_index2, WrfType2, ierr )
329 IF ( ierr /= 0 ) THEN
330 write(*,*)'Big difference: ',VarName,' not found in ',flnm2
333 IF ( ndim /= ndim2 ) THEN
334 write(*,*)'Big difference: Number of dimensions for ',Varname,' differs in ',flnm2,'(',ndim,') /= (',ndim2
337 IF ( WrfType /= WrfType2 ) THEN
338 write(*,*)'Big difference: The types do not match'
341 if( WrfType == WRF_REAL) then
343 IF ( end_index(i) /= end_index2(i) ) THEN
344 write(*,*)'Big difference: dim ',i,' lengths differ for ',Varname,' differ in ',flnm2
355 ! write(*,'(A9,1x,I1,3(1x,I3),1x,A,1x,A)')&
356 ! VarName, ndim, end_index(1), end_index(2), end_index(3), &
357 ! trim(ordering), trim(DateStr)
359 allocate(data (end_index(1), end_index(2), end_index(3), 1))
360 allocate(data2(end_index(1), end_index(2), end_index(3), 1))
362 if ( ndim .eq. 3 ) then
364 else if ( ndim .eq. 2 ) then
366 else if ( ndim .eq. 1 ) then
368 else if ( ndim .eq. 0 ) then
372 call ext_ncdpar_read_field(dh1,DateStr,TRIM(VarName),data,WRF_REAL,0,0,0,ord,&
373 staggering, dimnames , &
374 start_index,end_index, & !dom
375 start_index,end_index, & !mem
376 start_index,end_index, & !pat
379 IF ( ierr /= 0 ) THEN
380 write(*,*)'Error reading ',Varname,' from ',flnm
381 write(*,*)' ndim = ', ndim
382 write(*,*)' end_index(1) ',end_index(1)
383 write(*,*)' end_index(2) ',end_index(2)
384 write(*,*)' end_index(3) ',end_index(3)
386 call ext_ncdpar_read_field(dh2,DateStr,TRIM(VarName),data2,WRF_REAL,0,0,0,ord,&
387 staggering, dimnames , &
388 start_index,end_index, & !dom
389 start_index,end_index, & !mem
390 start_index,end_index, & !pat
392 IF ( ierr /= 0 ) THEN
393 write(*,*)'Error reading ',Varname,' from ',flnm2
394 write(*,*)' ndim = ', ndim
395 write(*,*)' end_index(1) ',end_index(1)
396 write(*,*)' end_index(2) ',end_index(2)
397 write(*,*)' end_index(3) ',end_index(3)
407 DO K = 1,end_index(3)-start_index(3)+1
408 IF (LEVLIM.EQ.-1.OR.K.EQ.LEVLIM.OR.NDIM.eq.2) THEN
411 do i = 1, end_index(1)-cross
412 do j = 1, end_index(2)-cross
415 ! borrowed from Thomas Oppe's comp program
416 sumE = sumE + ( a - b ) * ( a - b )
419 diff1 = max ( diff1 , abs ( a - b ) )
420 diff2 = max ( diff2 , abs ( b ) )
423 IKDIFFS = IKDIFFS + 1
424 IFDIFFS = IFDIFFS + 1
430 rmsE = sqrt ( sumE / dble( n ) )
431 rms1 = sqrt ( sum1 / dble( n ) )
432 rms2 = sqrt ( sum2 / dble( n ) )
434 IF ( sum2 .GT. 0.0d0 ) THEN
435 serr = sqrt ( sumE / sum2 )
437 IF ( sumE .GT. 0.0d0 ) serr = 1.0
440 IF ( diff2 .GT. 0.0d0 ) THEN
443 IF ( diff1 .GT. 0.0d0 ) perr = 1.0
446 IF ( rms1 - rms2 .EQ. 0.0d0 ) THEN
449 IF ( rms2 .NE. 0 ) THEN
450 tmp1 = 1.0d0/( ( abs( rms1 - rms2 ) ) / rms2 )
451 IF ( tmp1 .NE. 0 ) THEN
457 IF (IFDIFFS .NE. 0 ) THEN
458 ! create the fort.88 and fort.98 files because regression scripts will
459 ! look for these to see if there were differences.
460 write(88,*)trim(varname)
461 write(98,*)trim(varname)
462 PRINT 77,trim(varname), IFDIFFS, ndim, rms1, rms2, digits, rmsE, perr
463 76 FORMAT (5x,'Field ',2x,'Ndifs',4x,'Dims ',6x,'RMS (1)',12x,'RMS (2)',5x,'DIGITS',4x,'RMSE',5x,'pntwise max')
464 77 FORMAT ( A10,1x,I9,2x,I3,1x,e18.10,1x,e18.10,1x,i3,1x,e12.4,1x,e12.4 )
471 call ext_ncdpar_get_next_var (dh1, VarName, Status_next_var)
473 call ext_ncdpar_get_next_time(dh1, DateStr, Status_next_time)
474 call ext_ncdpar_get_next_time(dh2, DateStr2, Status_next_time2)
475 IF ( DateStr .NE. DateStr2 ) THEN
476 print*,'They differ big time. Dates do not match'
477 print*,'They differ big time. Dates do not match'
478 print*,' ',flnm,' ',DateStr
479 print*,' ',flnm2,' ',DateStr2
488 logical function wrf_dm_on_monitor()
489 wrf_dm_on_monitor=.true.
490 end function wrf_dm_on_monitor
492 logical function iveceq( a, b, n )
499 if ( a(i) .ne. b(i) ) iveceq = .false.
504 ! stubs for routines called by module_wrf_error (used by netcdf implementation of IO api)
507 END SUBROUTINE wrf_abort
509 SUBROUTINE get_current_time_string( time_str )
510 CHARACTER(LEN=*), INTENT(OUT) :: time_str
512 END SUBROUTINE get_current_time_string
514 SUBROUTINE get_current_grid_name( grid_str )
515 CHARACTER(LEN=*), INTENT(OUT) :: grid_str
517 END SUBROUTINE get_current_grid_name