PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/WPS/ungrib/src/parse_table.F

http://github.com/jbeezley/wrf-fire
FORTRAN Legacy | 464 lines | 245 code | 54 blank | 165 comment | 51 complexity | 021d092540e3a20364fb0f86b57a484f MD5 | raw file
Possible License(s): AGPL-1.0
  1. !*****************************************************************************!
  2. ! Subroutine PARSE_TABLE !
  3. ! !
  4. ! Purpose: !
  5. ! Read the Vtable, and fill arrays in the TABLE module with the Vtable !
  6. ! information. Broadly, the Vtable file is how the user tells the !
  7. ! program what fields to extract from the archive files. !
  8. ! !
  9. ! Argument list: !
  10. ! Input: DEBUG_LEVEL: 0 = no prints, bigger numbers = more prints !
  11. !
  12. ! Externals: !
  13. ! Module TABLE !
  14. ! Subroutine ABORT !
  15. ! !
  16. ! Side Effects: !
  17. ! !
  18. ! - File "Vtable" is opened, read, and closed as Fortran unit 10. !
  19. ! !
  20. ! - Various prints, especially if DEBUG_PRINT = .TRUE. !
  21. ! !
  22. ! - Abort for some miscellaneous error conditions. !
  23. ! !
  24. ! - Variables in module TABLE are filled., specifically, variables !
  25. ! MAXVAR !
  26. ! MAXOUT !
  27. ! !
  28. ! - Arrays in module TABLE are filled., specifically, arrays !
  29. ! NAMVAR !
  30. ! NAMEOUT !
  31. ! UNITOUT !
  32. ! DESCOUT !
  33. ! GCODE !
  34. ! LCODE !
  35. ! LEVEL1 !
  36. ! LEVEL2 !
  37. ! IPRTY !
  38. ! DUNITS !
  39. ! DDESC !
  40. ! !
  41. ! Author: Kevin W. Manning !
  42. ! NCAR/MMM !
  43. ! Summer 1998, and continuing !
  44. ! SDG !
  45. ! !
  46. !*****************************************************************************!
  47. subroutine parse_table(debug_level,vtable_columns)
  48. use Table
  49. use module_debug
  50. implicit none
  51. integer :: debug_level
  52. character(LEN=255) :: string = ' '
  53. integer :: ierr
  54. integer :: istart, ibar, i, j, ipcount
  55. integer :: jstart, jbar, jmax, tot_bars
  56. integer :: vtable_columns
  57. integer :: nstart, maxtmp
  58. logical :: lexist
  59. ! added for IBM
  60. blankcode = -99
  61. splatcode = -88
  62. ! end added for IBM
  63. ! Open the file called "Vtable"
  64. open(10, file='Vtable', status='old', form='formatted', iostat=ierr)
  65. ! Check to see that the OPEN worked without error.
  66. if (ierr.ne.0) then
  67. inquire(file='Vtable', exist=LEXIST)
  68. call mprintf(.true.,STDOUT," ***** ERROR in Subroutine PARSE_TABLE:")
  69. call mprintf(.true.,LOGFILE," ***** ERROR in Subroutine PARSE_TABLE:")
  70. if (.not.lexist) then
  71. call mprintf(.true.,STDOUT,"Problem opening file Vtable.")
  72. call mprintf(.true.,STDOUT,"File ''Vtable'' does not exist.")
  73. call mprintf(.true.,LOGFILE,"Problem opening file Vtable.")
  74. call mprintf(.true.,LOGFILE,"File ''Vtable'' does not exist.")
  75. else
  76. call mprintf(.true.,STDOUT,"Problem opening file Vtable.")
  77. call mprintf(.true.,STDOUT,"File Vtable exists, but Fortran OPEN statement")
  78. call mprintf(.true.,STDOUT,"failed with error %i",i1=ierr)
  79. call mprintf(.true.,LOGFILE,"Problem opening file Vtable.")
  80. call mprintf(.true.,LOGFILE,"File Vtable exists, but Fortran OPEN statement")
  81. call mprintf(.true.,LOGFILE,"failed with error %i",i1=ierr)
  82. endif
  83. call mprintf(.true.,ERROR," ***** Stopping in Subroutine PARSE_TABLE")
  84. endif
  85. ! First, read past the headers, i.e., skip lines until we hit the first
  86. ! line beginning with '-'
  87. do while (string(1:1).ne.'-')
  88. read(10,'(A255)', iostat=ierr) string
  89. call mprintf ((ierr /= 0),ERROR,"Read error 1 in PARSE_TABLE.")
  90. enddo
  91. string = ' '
  92. ! Now interpret everything from here to the next '-' line:
  93. !
  94. RDLOOP : do while (string(1:1).ne.'-')
  95. read(10,'(A255)', iostat=ierr) string
  96. call mprintf ((ierr /= 0),ERROR,"Read error 2 in PARSE_TABLE.")
  97. if (string(1:1).eq.'#') cycle RDLOOP
  98. if (len_trim(string) == 0) cycle RDLOOP
  99. if (string(1:1).eq.'-') then
  100. ! Skip over internal header lines
  101. BLOOP : do
  102. read(10,'(A255)', iostat=ierr) string
  103. if (ierr /= 0) exit RDLOOP
  104. if (len_trim(string) == 0) then
  105. cycle BLOOP
  106. else if (string(1:1) == '#') then
  107. cycle BLOOP
  108. else
  109. exit BLOOP
  110. endif
  111. enddo BLOOP
  112. do while (string(1:1).ne.'-')
  113. read(10,'(A255)', iostat=ierr) string
  114. call mprintf ((ierr /= 0),ERROR,"Read error 3 in PARSE_TABLE.")
  115. enddo
  116. string(1:1) = ' '
  117. elseif (string(1:1).ne.'-') then
  118. ! This is a line of values to interpret and parse.
  119. maxvar = maxvar + 1 ! increment the variable count
  120. ! --- Determine Grib1 or Grib2
  121. ! If there are seven fields this is a Grib1 Vtable,
  122. ! if there are eleven fields this is a Grib2 Vtable.
  123. jstart = 1
  124. jmax=jstart
  125. tot_bars=0
  126. do j = 1, vtable_columns
  127. ! The fields are delimited by '|'
  128. jbar = index(string(jstart:255),'|') + jstart - 2
  129. jstart = jbar + 2
  130. if (jstart.gt.jmax) then
  131. tot_bars=tot_bars+1
  132. jmax=jstart
  133. else
  134. cycle
  135. endif
  136. enddo
  137. call mprintf((tot_bars.eq.7.and.vtable_columns.ge.11),ERROR, &
  138. 'Vtable does not contain Grib2 decoding information.'// &
  139. ' 11 or 12 columns of information is expected.'// &
  140. ' *** stopping parse_table ***')
  141. istart = 1
  142. ! There are seven fields (Grib1) or eleven fields (Grib2) to each line.
  143. PLOOP : do i = 1, vtable_columns
  144. ! The fields are delimited by '|'
  145. ibar = index(string(istart:255),'|') + istart - 2
  146. if (i.eq.1) then
  147. ! The first field is the Grib1 param code number:
  148. if (string(istart:ibar) == ' ') then
  149. gcode(maxvar) = blankcode
  150. elseif (scan(string(istart:ibar),'*') /= 0) then
  151. call mprintf(.true.,ERROR,'Parse_table: Please give a '// &
  152. 'Grib1 parm code rather than $ in the first column of Vtable '// &
  153. '*** stopping in parse_table ***')
  154. else
  155. read(string(istart:ibar), * ) gcode(maxvar)
  156. endif
  157. elseif (i.eq.2) then
  158. ! The second field is the Grib1 level type:
  159. if (string(istart:ibar) == ' ') then
  160. if (lcode(maxvar) /= blankcode) then
  161. call mprintf(.true.,ERROR,'Parse_table: '// &
  162. 'Please supply a Grib1 level type in the Vtable: %s '// &
  163. '*** stopping in parse_table ***',s1=string)
  164. else
  165. lcode(maxvar) = blankcode
  166. endif
  167. elseif (scan(string(istart:ibar),'*') /= 0) then
  168. call mprintf(.true.,ERROR,'Parse_table: '// &
  169. "Used a * in Grib1 level type...don't do this! "// &
  170. '*** stopping in parse_table ***')
  171. else
  172. read(string(istart:ibar), *) lcode(maxvar)
  173. endif
  174. elseif (i.eq.3) then
  175. ! The third field is the Level 1 value, which may be '*':
  176. if (string(istart:ibar) == ' ') then
  177. level1(maxvar) = blankcode
  178. elseif (scan(string(istart:ibar),'*') == 0) then
  179. read(string(istart:ibar), *) level1(maxvar)
  180. else
  181. level1(maxvar) = splatcode
  182. endif
  183. elseif (i.eq.4) then
  184. ! The fourth field is the Level 2 value, which may be blank:
  185. if (string(istart:ibar) == ' ') then
  186. if ( (lcode(maxvar) == 112) .or.&
  187. (lcode(maxvar) == 116) ) then
  188. call mprintf(.true.,ERROR,'Parse_table: '// &
  189. 'Level Code expects two Level values. '// &
  190. '*** stopping in parse_table ***')
  191. else
  192. level2(maxvar) = blankcode
  193. endif
  194. elseif (scan(string(istart:ibar),'*') /= 0) then
  195. call mprintf(.true.,ERROR,'Parse_table: '// &
  196. 'Please give a Level 2 value (or blank), rather * in Vtable column 4 '// &
  197. '*** stopping in parse_table ***')
  198. else
  199. read(string(istart:ibar), *) level2(maxvar)
  200. endif
  201. elseif (i.eq.5) then
  202. ! The fifth field is the param name:
  203. if (string(istart:ibar).ne.' ') then
  204. nstart = 0
  205. do while (string(istart+nstart:istart+nstart).eq.' ')
  206. nstart = nstart + 1
  207. enddo
  208. namvar(maxvar) = string(istart+nstart:ibar)
  209. else
  210. call mprintf(.true.,ERROR,'Parse_table: '// &
  211. 'A field name is missing in the Vtable. '// &
  212. '*** stopping in parse_table ***')
  213. endif
  214. elseif (i.eq.6) then
  215. ! The sixth field is the Units string, which may be blank:
  216. if (string(istart:ibar).ne.' ') then
  217. nstart = 0
  218. do while (string(istart+nstart:istart+nstart).eq.' ')
  219. nstart = nstart + 1
  220. enddo
  221. Dunits(maxvar) = string(istart+nstart:ibar)
  222. else
  223. Dunits(maxvar) = ' '
  224. endif
  225. elseif (i.eq.7) then
  226. ! The seventh field is the description string, which may be blank:
  227. if (string(istart:ibar).ne.' ') then
  228. nstart = 0
  229. do while (string(istart+nstart:istart+nstart).eq.' ')
  230. nstart = nstart + 1
  231. enddo
  232. Ddesc(maxvar) = string(istart+nstart:ibar)
  233. ! If the description string is not blank, this is a
  234. ! field we want to output. In that case, copy the
  235. ! param name to the MAXOUT array:
  236. maxout = maxout + 1
  237. nameout(maxout) = namvar(maxvar)
  238. unitout(maxout) = Dunits(maxvar)
  239. descout(maxout) = Ddesc(maxvar)
  240. else
  241. Ddesc(maxvar) = ' '
  242. endif
  243. elseif (i.eq.8) then
  244. ! The eighth field is the Grib2 Product Discipline (see the
  245. ! Product Definition Template, Table 4.2).
  246. !cycle RDLOOP
  247. !read(string(istart:ibar), * ,eor=995) g2code(1,maxvar)
  248. if (string(istart:ibar) == ' ') then
  249. g2code(1,maxvar) = blankcode
  250. elseif (scan(string(istart:ibar),'*') /= 0) then
  251. call mprintf(.true.,STDOUT," ERROR reading Grib2 Discipline")
  252. call mprintf(.true.,STDOUT, &
  253. "This Grib2 Vtable line is incorrectly specified:")
  254. call mprintf(.true.,STDOUT," %s",s1=string)
  255. call mprintf(.true.,LOGFILE," ERROR reading Grib2 Discipline")
  256. call mprintf(.true.,LOGFILE, &
  257. "This Grib2 Vtable line is incorrectly specified:")
  258. call mprintf(.true.,LOGFILE," %s",s1=string)
  259. call mprintf(.true.,ERROR,"Stopping in PARSE_TABLE")
  260. else
  261. read(string(istart:ibar), *) g2code(1,maxvar)
  262. endif
  263. elseif (i.eq.9) then
  264. ! The ninth field is the Grib2 Parameter Category per Discipline.
  265. if (string(istart:ibar) == ' ') then
  266. g2code(2,maxvar) = blankcode
  267. elseif (scan(string(istart:ibar),'*') /= 0) then
  268. call mprintf(.true.,STDOUT," ERROR reading Grib2 Category")
  269. call mprintf(.true.,STDOUT, &
  270. "This Grib2 Vtable line is incorrectly specified:")
  271. call mprintf(.true.,STDOUT," %s",s1=string)
  272. call mprintf(.true.,LOGFILE," ERROR reading Grib2 Category")
  273. call mprintf(.true.,LOGFILE, &
  274. "This Grib2 Vtable line is incorrectly specified:")
  275. call mprintf(.true.,LOGFILE," %s",s1=string)
  276. call mprintf(.true.,ERROR,"Stopping in PARSE_TABLE")
  277. else
  278. read(string(istart:ibar), * ) g2code(2,maxvar)
  279. endif
  280. elseif (i.eq.10) then
  281. ! The tenth field is the Grib2 Parameter Number per Category.
  282. if (string(istart:ibar) == ' ') then
  283. g2code(3,maxvar) = blankcode
  284. elseif (scan(string(istart:ibar),'*') /= 0) then
  285. call mprintf(.true.,STDOUT, &
  286. " ERROR reading Grib2 Parameter Number ")
  287. call mprintf(.true.,STDOUT, &
  288. "This Grib2 Vtable line is incorrectly specified:")
  289. call mprintf(.true.,STDOUT," %s",s1=string)
  290. call mprintf(.true.,LOGFILE, &
  291. " ERROR reading Grib2 Parameter Number ")
  292. call mprintf(.true.,LOGFILE, &
  293. "This Grib2 Vtable line is incorrectly specified:")
  294. call mprintf(.true.,LOGFILE," %s",s1=string)
  295. call mprintf(.true.,ERROR,"Stopping in PARSE_TABLE")
  296. else
  297. read(string(istart:ibar), * ) g2code(3,maxvar)
  298. endif
  299. elseif (i.eq.11) then
  300. ! The eleventh field is the Grib2 Level Type (see the Product
  301. ! Definition Template, Table 4.5).
  302. if (string(istart:ibar) == ' ') then
  303. if (g2code(4,maxvar) /= blankcode) then
  304. call mprintf(.true.,STDOUT," ERROR reading Grib2 Level Type ")
  305. call mprintf(.true.,STDOUT, &
  306. "This Grib2 Vtable line is incorrectly specified:")
  307. call mprintf(.true.,STDOUT," %s",s1=string)
  308. call mprintf(.true.,LOGFILE," ERROR reading Grib2 Level Type ")
  309. call mprintf(.true.,LOGFILE, &
  310. "This Grib2 Vtable line is incorrectly specified:")
  311. call mprintf(.true.,LOGFILE," %s",s1=string)
  312. call mprintf(.true.,ERROR,"Stopping in PARSE_TABLE")
  313. else
  314. g2code(4,maxvar) = blankcode
  315. endif
  316. elseif (scan(string(istart:ibar),'*') /= 0) then
  317. call mprintf(.true.,STDOUT,"ERROR in Subroutine Parse_table: ")
  318. call mprintf(.true.,STDOUT, &
  319. "Used a * in Grib2 level type...don't do this! ")
  320. call mprintf(.true.,STDOUT," %s ",s1=string)
  321. call mprintf(.true.,LOGFILE,"ERROR in Subroutine Parse_table: ")
  322. call mprintf(.true.,LOGFILE, &
  323. "Used a * in Grib2 level type...don't do this! ")
  324. call mprintf(.true.,LOGFILE," %s ",s1=string)
  325. call mprintf(.true.,ERROR," ***** Abort in Subroutine PARSE_TABLE")
  326. else
  327. read(string(istart:ibar), *) g2code(4,maxvar)
  328. endif
  329. elseif (i.eq.12) then
  330. ! The twelfth field is the Grib2 Product Definition Template number
  331. ! Defaults to template 4.0, an instantaneous horizontal field.
  332. ! The only other supported value is 8 - an accumulated or averaged field.
  333. if (istart .lt. ibar) then
  334. if (string(istart:ibar) == ' ') then
  335. g2code(5,maxvar) = 0
  336. elseif (scan(string(istart:ibar),'*') /= 0) then
  337. call mprintf(.true.,STDOUT, &
  338. " ERROR reading Grib2 Parameter Number ")
  339. call mprintf(.true.,STDOUT, &
  340. "This Grib2 Vtable line is incorrectly specified:")
  341. call mprintf(.true.,STDOUT," %s",s1=string)
  342. call mprintf(.true.,LOGFILE, &
  343. " ERROR reading Grib2 Parameter Number ")
  344. call mprintf(.true.,LOGFILE, &
  345. "This Grib2 Vtable line is incorrectly specified:")
  346. call mprintf(.true.,LOGFILE," %s",s1=string)
  347. call mprintf(.true.,ERROR,"Stopping in PARSE_TABLE")
  348. else
  349. read(string(istart:ibar), * ) g2code(5,maxvar)
  350. endif
  351. else ! occurs when 11 columns are in the Vtable rather than 12.
  352. g2code(5,maxvar) = 0
  353. endif
  354. endif
  355. istart = ibar + 2
  356. enddo PLOOP ! 1,vtable_columns
  357. endif
  358. !995 continue
  359. enddo RDLOOP
  360. ! Now we have finished reading the file.
  361. close(10)
  362. ! Now remove duplicates from the NAMEOUT array. Duplicates may arise
  363. ! when we have the same name referred to by different level or parameter
  364. ! codes in some dataset.
  365. maxtmp = maxout
  366. do i = 1, maxtmp-1
  367. do j = i+1, maxtmp
  368. if ((nameout(i).eq.nameout(j)).and.(nameout(j).ne.' ')) then
  369. call mprintf(.true.,DEBUG, &
  370. "Duplicate name. Removing %s from output list.",s1=nameout(j))
  371. nameout(j:maxlines-1) = nameout(j+1:maxlines)
  372. unitout(j:maxlines-1) = unitout(j+1:maxlines)
  373. descout(j:maxlines-1) = descout(j+1:maxlines)
  374. maxout = maxout - 1
  375. endif
  376. enddo
  377. enddo
  378. ! Compute a priority level based on position in the table:
  379. ! This assumes Grib.
  380. ! Priorities are used only for surface fields. If it is not a
  381. ! surface fields, the priority is assigned a value of 100.
  382. ! For surface fields, priorities are assigned values of 100, 101,
  383. ! 102, etc. in the order the field names appear in the Vtable.
  384. ipcount = 99
  385. do i = 1, maxvar
  386. if (lcode(i).eq.105) then
  387. ipcount = ipcount + 1
  388. iprty(i) = ipcount
  389. elseif (lcode(i).eq.116.and.level1(i).le.50.and.level2(i).eq.0) then
  390. ipcount = ipcount + 1
  391. iprty(i) = ipcount
  392. else
  393. iprty(i) = 100
  394. endif
  395. enddo
  396. if (debug_level .gt. 0) then
  397. write(*,'(//"Read from file ''Vtable'' by subroutine PARSE_TABLE:")')
  398. do i = 1, maxvar
  399. if (vtable_columns.ge.11) then
  400. write(*,'(4I6, 3x,A10, 5I6)')&
  401. gcode(i), lcode(i), level1(i), level2(i), namvar(i), &
  402. g2code(1,i), g2code(2,i), g2code(3,i), g2code(4,i), g2code(5,i)
  403. else
  404. write(*,'(4I6, 3x,A10)')&
  405. gcode(i), lcode(i), level1(i), level2(i), namvar(i)
  406. endif
  407. enddo
  408. write(*,'(//)')
  409. endif
  410. end subroutine parse_table