PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wrfv2_fire/external/io_grib2/io_grib2.F

http://github.com/jbeezley/wrf-fire
FORTRAN Legacy | 4530 lines | 2683 code | 900 blank | 947 comment | 280 complexity | b4b51a64af2749be95dcb7ea22ea3fb5 MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. !*-----------------------------------------------------------------------------
  2. !*
  3. !* Todd Hutchinson
  4. !* WSI
  5. !* 400 Minuteman Road
  6. !* Andover, MA 01810
  7. !* thutchinson@wsi.com
  8. !*
  9. !* August, 2005
  10. !*-----------------------------------------------------------------------------
  11. !*
  12. !* This io_grib2 API is designed to read WRF input and write WRF output data
  13. !* in grib version 2 format.
  14. !*
  15. #include "wrf_projection.h"
  16. module gr2_data_info
  17. !*
  18. !* This module will hold data internal to this I/O implementation.
  19. !* The variables will be accessible by all functions (provided they have a
  20. !* "USE gr2_data_info" line).
  21. !*
  22. USE grib2tbls_types
  23. integer , parameter :: FATAL = 1
  24. integer , parameter :: DEBUG = 100
  25. integer , parameter :: DateStrLen = 19
  26. integer , parameter :: maxMsgSize = 300
  27. integer , parameter :: firstFileHandle = 8
  28. integer , parameter :: maxFileHandles = 200
  29. integer , parameter :: maxLevels = 1000
  30. integer , parameter :: maxSoilLevels = 100
  31. integer , parameter :: maxDomains = 500
  32. character(200) :: mapfilename = 'grib2map.tbl'
  33. integer , parameter :: JIDSSIZE = 13
  34. integer , parameter :: JPDTSIZE = 15
  35. integer , parameter :: JGDTSIZE = 30
  36. logical :: grib2map_table_filled = .FALSE.
  37. logical :: WrfIOnotInitialized = .true.
  38. integer, dimension(maxDomains) :: domains
  39. integer :: max_domain = 0
  40. character*24 :: StartDate = ''
  41. character*24 :: InputProgramName = ''
  42. real :: timestep
  43. integer :: full_xsize, full_ysize
  44. REAL, dimension(maxSoilLevels) :: soil_depth, soil_thickness
  45. REAL, dimension(maxLevels) :: half_eta, full_eta
  46. integer :: wrf_projection
  47. integer :: background_proc_id
  48. integer :: forecast_proc_id
  49. integer :: production_status
  50. integer :: compression
  51. real :: center_lat, center_lon
  52. real :: dx,dy
  53. real :: truelat1, truelat2
  54. real :: proj_central_lon
  55. TYPE :: HandleVar
  56. character, dimension(:), pointer :: fileindex(:)
  57. integer :: CurrentTime
  58. integer :: NumberTimes
  59. integer :: sizeAllocated = 0
  60. logical :: write = .FALSE.
  61. character (DateStrLen), dimension(:),allocatable :: Times(:)
  62. logical :: committed, opened, used
  63. character*128 :: DataFile
  64. integer :: FileFd
  65. integer :: FileStatus
  66. integer :: recnum
  67. real :: last_scalar_time_written
  68. ENDTYPE
  69. TYPE (HandleVar), dimension(maxFileHandles),SAVE :: fileinfo
  70. character(len=30000), dimension(maxFileHandles) :: td_output
  71. character(len=30000), dimension(maxFileHandles) :: ti_output
  72. character(len=30000), dimension(maxFileHandles) :: scalar_output
  73. character(len=30000), dimension(maxFileHandles) :: global_input = ''
  74. character(len=30000), dimension(maxFileHandles) :: scalar_input = ''
  75. real :: last_fcst_secs
  76. real :: fcst_secs
  77. logical :: half_eta_init = .FALSE.
  78. logical :: full_eta_init = .FALSE.
  79. logical :: soil_thickness_init = .FALSE.
  80. logical :: soil_depth_init = .FALSE.
  81. end module gr2_data_info
  82. !*****************************************************************************
  83. subroutine ext_gr2_ioinit(SysDepInfo,Status)
  84. USE gr2_data_info
  85. implicit none
  86. #include "wrf_status_codes.h"
  87. #include "wrf_io_flags.h"
  88. CHARACTER*(*), INTENT(IN) :: SysDepInfo
  89. integer ,intent(out) :: Status
  90. integer :: i
  91. CHARACTER (LEN=300) :: wrf_err_message
  92. call wrf_debug ( DEBUG , 'Entering ext_gr2_ioinit')
  93. do i=firstFileHandle, maxFileHandles
  94. fileinfo(i)%used = .false.
  95. fileinfo(i)%committed = .false.
  96. fileinfo(i)%opened = .false.
  97. td_output(i) = ''
  98. ti_output(i) = ''
  99. scalar_output(i) = ''
  100. enddo
  101. domains(:) = -1
  102. last_fcst_secs = -1.0
  103. fileinfo(1:maxFileHandles)%FileStatus = WRF_FILE_NOT_OPENED
  104. WrfIOnotInitialized = .false.
  105. Status = WRF_NO_ERR
  106. return
  107. end subroutine ext_gr2_ioinit
  108. !*****************************************************************************
  109. subroutine ext_gr2_ioexit(Status)
  110. USE gr2_data_info
  111. implicit none
  112. #include "wrf_status_codes.h"
  113. integer ,intent(out) :: Status
  114. call wrf_debug ( DEBUG , 'Entering ext_gr2_ioexit')
  115. Status = WRF_NO_ERR
  116. if (grib2map_table_filled) then
  117. call free_grib2map()
  118. grib2map_table_filled = .FALSE.
  119. endif
  120. return
  121. end subroutine ext_gr2_ioexit
  122. !*****************************************************************************
  123. SUBROUTINE ext_gr2_open_for_read_begin ( FileName , Comm_compute, Comm_io, &
  124. SysDepInfo, DataHandle , Status )
  125. USE gr2_data_info
  126. USE grib2tbls_types
  127. USE grib_mod
  128. IMPLICIT NONE
  129. #include "wrf_status_codes.h"
  130. #include "wrf_io_flags.h"
  131. CHARACTER*(*) :: FileName
  132. INTEGER , INTENT(IN) :: Comm_compute , Comm_io
  133. CHARACTER*(*) :: SysDepInfo
  134. INTEGER , INTENT(OUT) :: DataHandle
  135. INTEGER , INTENT(OUT) :: Status
  136. CHARACTER (LEN=maxMsgSize) :: msg
  137. integer :: center, subcenter, MasterTblV, &
  138. LocalTblV, Disc, Category, ParmNum, DecScl, BinScl
  139. integer :: fields_to_skip
  140. integer :: JIDS(JIDSSIZE), JPDTN, JPDT(JPDTSIZE), JGDTN, &
  141. JGDT(JGDTSIZE)
  142. logical :: UNPACK
  143. character*(100) :: VarName
  144. type(gribfield) :: gfld
  145. integer :: idx
  146. character(len=DateStrLen) :: theTime,refTime
  147. integer :: time_range_convert(13)
  148. integer :: fcstsecs
  149. integer :: endchar
  150. integer :: ierr
  151. INTERFACE
  152. Subroutine load_grib2map (filename, message, status)
  153. USE grib2tbls_types
  154. character*(*), intent(in) :: filename
  155. character*(*), intent(inout) :: message
  156. integer , intent(out) :: status
  157. END subroutine load_grib2map
  158. END INTERFACE
  159. call wrf_debug ( DEBUG , &
  160. 'Entering ext_gr2_open_for_read_begin, opening '//trim(FileName))
  161. CALL gr2_get_new_handle(DataHandle)
  162. !
  163. ! Open grib file
  164. !
  165. if (DataHandle .GT. 0) then
  166. call baopenr(DataHandle,trim(FileName),status)
  167. if (status .ne. 0) then
  168. Status = WRF_ERR_FATAL_BAD_FILE_STATUS
  169. else
  170. fileinfo(DataHandle)%opened = .true.
  171. fileinfo(DataHandle)%DataFile = TRIM(FileName)
  172. fileinfo(DataHandle)%FileStatus = WRF_FILE_OPENED_NOT_COMMITTED
  173. ! fileinfo(DataHandle)%CurrentTime = 1
  174. endif
  175. else
  176. Status = WRF_WARN_TOO_MANY_FILES
  177. return
  178. endif
  179. fileinfo(DataHandle)%recnum = -1
  180. !
  181. ! Fill up the grib2tbls structure from data in the grib2map file.
  182. !
  183. if (.NOT. grib2map_table_filled) then
  184. grib2map_table_filled = .TRUE.
  185. CALL load_grib2map(mapfilename, msg, status)
  186. if (status .ne. 0) then
  187. call wrf_message(trim(msg))
  188. Status = WRF_ERR_FATAL_BAD_FILE_STATUS
  189. return
  190. endif
  191. endif
  192. !
  193. ! Get the parameter info for metadata
  194. !
  195. VarName = "WRF_GLOBAL"
  196. CALL get_parminfo(VarName, center, subcenter, MasterTblV, &
  197. LocalTblV, Disc, Category, ParmNum, DecScl, BinScl, status)
  198. if (status .ne. 0) then
  199. write(msg,*) 'Could not find parameter for '// &
  200. trim(VarName)//' Skipping output of '//trim(VarName)
  201. call wrf_message(trim(msg))
  202. Status = WRF_GRIB2_ERR_GRIB2MAP
  203. return
  204. endif
  205. !
  206. ! Read the metadata
  207. !
  208. fields_to_skip = 0
  209. !
  210. ! First, set all values to the wildcard, then reset values that we wish
  211. ! to specify.
  212. !
  213. call gr2_g2lib_wildcard(JIDS, JPDT, JGDT)
  214. JIDS(1) = center
  215. JIDS(2) = subcenter
  216. JIDS(3) = MasterTblV
  217. JIDS(4) = LocalTblV
  218. JIDS(5) = 1 ! Indicates that time is "Start of Forecast"
  219. JIDS(13) = 1 ! Type of processed data (1 for forecast products)
  220. JPDTN = 0 ! Product definition template number
  221. JPDT(1) = Category
  222. JPDT(2) = ParmNum
  223. JPDT(3) = 2 ! Generating process id
  224. JPDT(9) = 0 ! Forecast time
  225. JGDTN = -1 ! Indicates that any Grid Display Template is a match
  226. UNPACK = .FALSE. ! Dont unpack bitmap and data values
  227. CALL GETGB2(DataHandle, DataHandle, fields_to_skip, -1, Disc, JIDS, JPDTN, &
  228. JPDT, JGDTN, JGDT, UNPACK, fileinfo(DataHandle)%recnum, gfld, status)
  229. if (status .ne. 0) then
  230. if (status .eq. 99) then
  231. write(msg,*)'Could not find metadata field named '//trim(VarName)
  232. else
  233. write(msg,*)'Retrieving grib field '//trim(VarName)//' failed, ',status
  234. endif
  235. call wrf_message(trim(msg))
  236. status = WRF_GRIB2_ERR_GETGB2
  237. return
  238. endif
  239. global_input(DataHandle) = transfer(gfld%local,global_input(DataHandle))
  240. global_input(DataHandle)(gfld%locallen+1:30000) = ' '
  241. call gf_free(gfld)
  242. !
  243. ! Read and index all scalar data
  244. !
  245. VarName = "WRF_SCALAR"
  246. CALL get_parminfo(VarName, center, subcenter, MasterTblV, &
  247. LocalTblV, Disc, Category, ParmNum, DecScl, BinScl, status)
  248. if (status .ne. 0) then
  249. write(msg,*) 'Could not find parameter for '// &
  250. trim(VarName)//' Skipping reading of '//trim(VarName)
  251. call wrf_message(trim(msg))
  252. Status = WRF_GRIB2_ERR_GRIB2MAP
  253. return
  254. endif
  255. !
  256. ! Read the metadata
  257. !
  258. ! First, set all values to wild, then specify necessary values
  259. !
  260. call gr2_g2lib_wildcard(JIDS, JPDT, JGDT)
  261. JIDS(1) = center
  262. JIDS(2) = subcenter
  263. JIDS(3) = MasterTblV
  264. JIDS(4) = LocalTblV
  265. JIDS(5) = 1 ! Indicates that time is "Start of Forecast"
  266. JIDS(13) = 1 ! Type of processed data (1 for forecast products)
  267. JPDTN = 0 ! Product definition template number
  268. JPDT(1) = Category
  269. JPDT(2) = ParmNum
  270. JPDT(3) = 2 ! Generating process id
  271. JGDTN = -1 ! Indicates that any Grid Display Template is a match
  272. UNPACK = .FALSE. ! Dont unpack bitmap and data values
  273. fields_to_skip = 0
  274. do while (status .eq. 0)
  275. CALL GETGB2(DataHandle, 0, fields_to_skip, -1, -1, JIDS, JPDTN, &
  276. JPDT, JGDTN, JGDT, UNPACK, fileinfo(DataHandle)%recnum, &
  277. gfld, status)
  278. if (status .eq. 99) then
  279. exit
  280. else if (status .ne. 0) then
  281. write(msg,*)'Finding data field '//trim(VarName)//' failed 1.'
  282. call wrf_message(trim(msg))
  283. Status = WRF_GRIB2_ERR_READ
  284. return
  285. endif
  286. ! Build times list here
  287. write(refTime,'(I4,A,I2.2,A,I2.2,A,I2.2,A,I2.2,A,I2.2)') &
  288. gfld%idsect(6),'-',gfld%idsect(7),'-',gfld%idsect(8),'_',&
  289. gfld%idsect(9),':',gfld%idsect(10),':',gfld%idsect(11)
  290. time_range_convert(:) = -1
  291. time_range_convert(1) = 60
  292. time_range_convert(2) = 60*60
  293. time_range_convert(3) = 24*60*60
  294. time_range_convert(10) = 3*60*60
  295. time_range_convert(11) = 6*60*60
  296. time_range_convert(12) = 12*60*60
  297. time_range_convert(13) = 1
  298. if (time_range_convert(gfld%ipdtmpl(8)) .gt. 0) then
  299. fcstsecs = gfld%ipdtmpl(9)*time_range_convert(gfld%ipdtmpl(8))
  300. else
  301. write(msg,*)'Invalid time range in input data: ',gfld%ipdtmpl(8),&
  302. ' Skipping'
  303. call wrf_message(trim(msg))
  304. call gf_free(gfld)
  305. cycle
  306. endif
  307. call advance_wrf_time(refTime,fcstsecs,theTime)
  308. call gr2_add_time(DataHandle,theTime)
  309. fields_to_skip = fields_to_skip + fileinfo(DataHandle)%recnum
  310. scalar_input(DataHandle) = transfer(gfld%local,scalar_input(DataHandle))
  311. scalar_input(DataHandle)(gfld%locallen+1:30000) = ' '
  312. call gf_free(gfld)
  313. enddo
  314. !
  315. ! Fill up the eta levels variables
  316. !
  317. if (.not. full_eta_init) then
  318. CALL gr2_fill_levels(DataHandle, "ZNW", full_eta, ierr)
  319. if (ierr .eq. 0) then
  320. full_eta_init = .TRUE.
  321. endif
  322. endif
  323. if (.not. half_eta_init) then
  324. CALL gr2_fill_levels(DataHandle, "ZNU", half_eta, ierr)
  325. if (ierr .eq. 0) then
  326. half_eta_init = .TRUE.
  327. endif
  328. endif
  329. !
  330. ! Fill up the soil levels
  331. !
  332. if (.not. soil_depth_init) then
  333. call gr2_fill_levels(DataHandle,"ZS",soil_depth, ierr)
  334. if (ierr .eq. 0) then
  335. soil_depth_init = .TRUE.
  336. endif
  337. endif
  338. if (.not. soil_thickness_init) then
  339. call gr2_fill_levels(DataHandle,"DZS",soil_thickness, ierr)
  340. if (ierr .eq. 0) then
  341. soil_thickness_init = .TRUE.
  342. endif
  343. endif
  344. !
  345. ! Fill up any variables from the global metadata
  346. !
  347. CALL gr2_get_metadata_value(global_input(DataHandle), &
  348. 'START_DATE', StartDate, status)
  349. if (status .ne. 0) then
  350. write(msg,*)'Could not find metadata value for START_DATE, continuing'
  351. call wrf_message(trim(msg))
  352. endif
  353. CALL gr2_get_metadata_value(global_input(DataHandle), &
  354. 'PROGRAM_NAME', InputProgramName, status)
  355. if (status .ne. 0) then
  356. write(msg,*)'Could not find metadata value for PROGRAM_NAME, continuing'
  357. call wrf_message(trim(msg))
  358. else
  359. endchar = SCAN(InputProgramName," ")
  360. InputProgramName = InputProgramName(1:endchar)
  361. endif
  362. Status = WRF_NO_ERR
  363. call wrf_debug ( DEBUG , 'Exiting ext_gr2_open_for_read_begin')
  364. RETURN
  365. END SUBROUTINE ext_gr2_open_for_read_begin
  366. !*****************************************************************************
  367. SUBROUTINE ext_gr2_open_for_read_commit( DataHandle , Status )
  368. USE gr2_data_info
  369. IMPLICIT NONE
  370. #include "wrf_status_codes.h"
  371. #include "wrf_io_flags.h"
  372. character(len=maxMsgSize) :: msg
  373. INTEGER , INTENT(IN ) :: DataHandle
  374. INTEGER , INTENT(OUT) :: Status
  375. call wrf_debug ( DEBUG , 'Entering ext_gr2_open_for_read_commit')
  376. Status = WRF_NO_ERR
  377. if(WrfIOnotInitialized) then
  378. Status = WRF_IO_NOT_INITIALIZED
  379. write(msg,*) 'ext_gr2_ioinit was not called ',__FILE__,', line', __LINE__
  380. call wrf_debug ( FATAL , msg)
  381. return
  382. endif
  383. fileinfo(DataHandle)%committed = .true.
  384. fileinfo(DataHandle)%FileStatus = WRF_FILE_OPENED_FOR_READ
  385. Status = WRF_NO_ERR
  386. RETURN
  387. END SUBROUTINE ext_gr2_open_for_read_commit
  388. !*****************************************************************************
  389. SUBROUTINE ext_gr2_open_for_read ( FileName , Comm_compute, Comm_io, &
  390. SysDepInfo, DataHandle , Status )
  391. USE gr2_data_info
  392. IMPLICIT NONE
  393. #include "wrf_status_codes.h"
  394. CHARACTER*(*) :: FileName
  395. INTEGER , INTENT(IN) :: Comm_compute , Comm_io
  396. CHARACTER*(*) :: SysDepInfo
  397. INTEGER , INTENT(OUT) :: DataHandle
  398. INTEGER , INTENT(OUT) :: Status
  399. call wrf_debug ( DEBUG , 'Entering ext_gr2_open_for_read')
  400. DataHandle = 0 ! dummy setting to quiet warning message
  401. CALL ext_gr2_open_for_read_begin( FileName, Comm_compute, Comm_io, &
  402. SysDepInfo, DataHandle, Status )
  403. IF ( Status .EQ. WRF_NO_ERR ) THEN
  404. CALL ext_gr2_open_for_read_commit( DataHandle, Status )
  405. ENDIF
  406. return
  407. RETURN
  408. END SUBROUTINE ext_gr2_open_for_read
  409. !*****************************************************************************
  410. SUBROUTINE ext_gr2_open_for_write_begin(FileName, Comm, IOComm, SysDepInfo, &
  411. DataHandle, Status)
  412. USE gr2_data_info
  413. implicit none
  414. #include "wrf_status_codes.h"
  415. #include "wrf_io_flags.h"
  416. character*(*) ,intent(in) :: FileName
  417. integer ,intent(in) :: Comm
  418. integer ,intent(in) :: IOComm
  419. character*(*) ,intent(in) :: SysDepInfo
  420. integer ,intent(out) :: DataHandle
  421. integer ,intent(out) :: Status
  422. integer :: ierr
  423. CHARACTER (LEN=maxMsgSize) :: msg
  424. INTERFACE
  425. Subroutine load_grib2map (filename, message, status)
  426. USE grib2tbls_types
  427. character*(*), intent(in) :: filename
  428. character*(*), intent(inout) :: message
  429. integer , intent(out) :: status
  430. END subroutine load_grib2map
  431. END INTERFACE
  432. call wrf_debug ( DEBUG , 'Entering ext_gr2_open_for_write_begin')
  433. Status = WRF_NO_ERR
  434. if (.NOT. grib2map_table_filled) then
  435. grib2map_table_filled = .TRUE.
  436. CALL load_grib2map(mapfilename, msg, status)
  437. if (status .ne. 0) then
  438. call wrf_message(trim(msg))
  439. Status = WRF_ERR_FATAL_BAD_FILE_STATUS
  440. return
  441. endif
  442. endif
  443. CALL gr2_get_new_handle(DataHandle)
  444. if (DataHandle .GT. 0) then
  445. call baopenw(DataHandle,trim(FileName),ierr)
  446. if (ierr .ne. 0) then
  447. Status = WRF_ERR_FATAL_BAD_FILE_STATUS
  448. else
  449. fileinfo(DataHandle)%opened = .true.
  450. fileinfo(DataHandle)%DataFile = TRIM(FileName)
  451. fileinfo(DataHandle)%FileStatus = WRF_FILE_OPENED_NOT_COMMITTED
  452. endif
  453. fileinfo(DataHandle)%last_scalar_time_written = -1
  454. fileinfo(DataHandle)%committed = .false.
  455. td_output(DataHandle) = ''
  456. ti_output(DataHandle) = ''
  457. scalar_output(DataHandle) = ''
  458. fileinfo(DataHandle)%write = .true.
  459. else
  460. Status = WRF_WARN_TOO_MANY_FILES
  461. endif
  462. RETURN
  463. END SUBROUTINE ext_gr2_open_for_write_begin
  464. !*****************************************************************************
  465. SUBROUTINE ext_gr2_open_for_write_commit( DataHandle , Status )
  466. USE gr2_data_info
  467. IMPLICIT NONE
  468. #include "wrf_status_codes.h"
  469. #include "wrf_io_flags.h"
  470. INTEGER , INTENT(IN ) :: DataHandle
  471. INTEGER , INTENT(OUT) :: Status
  472. call wrf_debug ( DEBUG , 'Entering ext_gr2_open_for_write_commit')
  473. IF ( fileinfo(DataHandle)%opened ) THEN
  474. IF ( fileinfo(DataHandle)%used ) THEN
  475. fileinfo(DataHandle)%committed = .true.
  476. fileinfo(DataHandle)%FileStatus = WRF_FILE_OPENED_FOR_WRITE
  477. ENDIF
  478. ENDIF
  479. Status = WRF_NO_ERR
  480. RETURN
  481. END SUBROUTINE ext_gr2_open_for_write_commit
  482. !*****************************************************************************
  483. subroutine ext_gr2_inquiry (Inquiry, Result, Status)
  484. use gr2_data_info
  485. implicit none
  486. #include "wrf_status_codes.h"
  487. character *(*), INTENT(IN) :: Inquiry
  488. character *(*), INTENT(OUT) :: Result
  489. integer ,INTENT(INOUT) :: Status
  490. SELECT CASE (Inquiry)
  491. CASE ("RANDOM_WRITE","RANDOM_READ")
  492. Result='ALLOW'
  493. CASE ("SEQUENTIAL_WRITE","SEQUENTIAL_READ")
  494. Result='NO'
  495. CASE ("OPEN_READ", "OPEN_WRITE", "OPEN_COMMIT_WRITE")
  496. Result='REQUIRE'
  497. CASE ("OPEN_COMMIT_READ","PARALLEL_IO")
  498. Result='NO'
  499. CASE ("SELF_DESCRIBING","SUPPORT_METADATA","SUPPORT_3D_FIELDS")
  500. Result='YES'
  501. CASE ("MEDIUM")
  502. Result ='FILE'
  503. CASE DEFAULT
  504. Result = 'No Result for that inquiry!'
  505. END SELECT
  506. Status=WRF_NO_ERR
  507. return
  508. end subroutine ext_gr2_inquiry
  509. !*****************************************************************************
  510. SUBROUTINE ext_gr2_inquire_opened ( DataHandle, FileName , FileStat, Status )
  511. USE gr2_data_info
  512. IMPLICIT NONE
  513. #include "wrf_status_codes.h"
  514. #include "wrf_io_flags.h"
  515. INTEGER , INTENT(IN) :: DataHandle
  516. CHARACTER*(*) :: FileName
  517. INTEGER , INTENT(OUT) :: FileStat
  518. INTEGER , INTENT(OUT) :: Status
  519. call wrf_debug ( DEBUG , 'Entering ext_gr2_inquire_opened')
  520. FileStat = WRF_NO_ERR
  521. if ((DataHandle .ge. firstFileHandle) .and. &
  522. (DataHandle .le. maxFileHandles)) then
  523. FileStat = fileinfo(DataHandle)%FileStatus
  524. else
  525. FileStat = WRF_FILE_NOT_OPENED
  526. endif
  527. Status = FileStat
  528. RETURN
  529. END SUBROUTINE ext_gr2_inquire_opened
  530. !*****************************************************************************
  531. SUBROUTINE ext_gr2_ioclose ( DataHandle, Status )
  532. USE gr2_data_info
  533. IMPLICIT NONE
  534. #include "wrf_status_codes.h"
  535. #include "wrf_io_flags.h"
  536. INTEGER DataHandle, Status
  537. INTEGER istat
  538. character(len=1000) :: outstring
  539. character :: lf
  540. character*(maxMsgSize) :: msg
  541. integer :: idx
  542. lf=char(10)
  543. call wrf_debug ( DEBUG , 'Entering ext_gr2_ioclose')
  544. Status = WRF_NO_ERR
  545. if (fileinfo(DataHandle)%write .eqv. .TRUE.) then
  546. call gr2_fill_local_use(DataHandle,scalar_output(DataHandle),&
  547. "WRF_SCALAR",fcst_secs,msg,status)
  548. if (status .ne. 0) then
  549. call wrf_message(trim(msg))
  550. return
  551. endif
  552. fileinfo(DataHandle)%last_scalar_time_written = fcst_secs
  553. scalar_output(DataHandle) = ''
  554. call gr2_fill_local_use(DataHandle,&
  555. trim(ti_output(DataHandle))//trim(td_output(DataHandle)),&
  556. "WRF_GLOBAL",0,msg,status)
  557. if (status .ne. 0) then
  558. call wrf_message(trim(msg))
  559. return
  560. endif
  561. ti_output(DataHandle) = ''
  562. td_output(DataHandle) = ''
  563. endif
  564. do idx = 1,fileinfo(DataHandle)%NumberTimes
  565. if (allocated(fileinfo(DataHandle)%Times)) then
  566. deallocate(fileinfo(DataHandle)%Times)
  567. endif
  568. enddo
  569. fileinfo(DataHandle)%NumberTimes = 0
  570. fileinfo(DataHandle)%sizeAllocated = 0
  571. fileinfo(DataHandle)%CurrentTime = 0
  572. fileinfo(DataHandle)%write = .FALSE.
  573. call baclose(DataHandle,status)
  574. if (status .ne. 0) then
  575. call wrf_message("Closing file failed, continuing")
  576. else
  577. fileinfo(DataHandle)%opened = .true.
  578. fileinfo(DataHandle)%DataFile = ''
  579. fileinfo(DataHandle)%FileStatus = WRF_FILE_NOT_OPENED
  580. endif
  581. fileinfo(DataHandle)%used = .false.
  582. RETURN
  583. END SUBROUTINE ext_gr2_ioclose
  584. !*****************************************************************************
  585. SUBROUTINE ext_gr2_write_field( DataHandle , DateStrIn , VarName , &
  586. Field , FieldType , Comm , IOComm, &
  587. DomainDesc , MemoryOrder , Stagger , &
  588. DimNames , &
  589. DomainStart , DomainEnd , &
  590. MemoryStart , MemoryEnd , &
  591. PatchStart , PatchEnd , &
  592. Status )
  593. USE gr2_data_info
  594. USE grib2tbls_types
  595. IMPLICIT NONE
  596. #include "wrf_status_codes.h"
  597. #include "wrf_io_flags.h"
  598. integer ,intent(in) :: DataHandle
  599. character*(*) ,intent(in) :: DateStrIn
  600. character*(*) ,intent(in) :: VarName
  601. integer ,intent(in) :: FieldType
  602. integer ,intent(inout) :: Comm
  603. integer ,intent(inout) :: IOComm
  604. integer ,intent(in) :: DomainDesc
  605. character*(*) ,intent(in) :: MemoryOrder
  606. character*(*) ,intent(in) :: Stagger
  607. character*(*) , dimension (*) ,intent(in) :: DimNames
  608. integer ,dimension(*) ,intent(in) :: DomainStart, DomainEnd
  609. integer ,dimension(*) ,intent(in) :: MemoryStart, MemoryEnd
  610. integer ,dimension(*) ,intent(in) :: PatchStart, PatchEnd
  611. integer ,intent(out) :: Status
  612. real , intent(in), &
  613. dimension( 1:1,MemoryStart(1):MemoryEnd(1), &
  614. MemoryStart(2):MemoryEnd(2), &
  615. MemoryStart(3):MemoryEnd(3) ) :: Field
  616. character (120) :: DateStr
  617. character (maxMsgSize) :: msg
  618. integer :: xsize, ysize, zsize
  619. integer :: x, y, z
  620. integer :: &
  621. x_start,x_end,y_start,y_end,z_start,z_end
  622. integer :: idx
  623. integer :: proj_center_flag
  624. logical :: vert_stag = .false.
  625. real, dimension(:,:), pointer :: data
  626. integer :: istat
  627. integer :: accum_period
  628. integer, dimension(maxLevels) :: level1, level2
  629. integer, dimension(maxLevels) :: grib_levels
  630. logical :: soil_layers, fraction
  631. integer :: vert_unit1, vert_unit2
  632. integer :: vert_sclFctr1, vert_sclFctr2
  633. integer :: this_domain
  634. logical :: new_domain
  635. real :: &
  636. region_center_lat, region_center_lon
  637. integer :: dom_xsize, dom_ysize;
  638. integer , parameter :: lcgrib = 2000000
  639. character (lcgrib) :: cgrib
  640. integer :: ierr
  641. integer :: lengrib
  642. integer :: center, subcenter, &
  643. MasterTblV, LocalTblV, Disc, Category, ParmNum, DecScl, BinScl
  644. CHARACTER(len=100) :: tmpstr
  645. integer :: ndims
  646. integer :: dim1size, dim2size, dim3size, dim3
  647. integer :: numlevels
  648. integer :: ngrdpts
  649. integer :: bytes_written
  650. call wrf_debug ( DEBUG , 'Entering ext_gr2_write_field for parameter '//&
  651. VarName)
  652. !
  653. ! If DateStr is all 0s, we reset it to StartDate. For some reason,
  654. ! in idealized simulations, StartDate is 0001-01-01_00:00:00 while
  655. ! the first DateStr is 0000-00-00_00:00:00.
  656. !
  657. if (DateStrIn .eq. '0000-00-00_00:00:00') then
  658. DateStr = TRIM(StartDate)
  659. else
  660. DateStr = DateStrIn
  661. endif
  662. !
  663. ! Check if this is a domain that we haven t seen yet. If so, add it to
  664. ! the list of domains.
  665. !
  666. this_domain = 0
  667. new_domain = .false.
  668. do idx = 1, max_domain
  669. if (DomainDesc .eq. domains(idx)) then
  670. this_domain = idx
  671. endif
  672. enddo
  673. if (this_domain .eq. 0) then
  674. max_domain = max_domain + 1
  675. domains(max_domain) = DomainDesc
  676. this_domain = max_domain
  677. new_domain = .true.
  678. endif
  679. zsize = 1
  680. xsize = 1
  681. ysize = 1
  682. soil_layers = .false.
  683. fraction = .false.
  684. ! First, handle then special cases for the boundary data.
  685. CALL get_dims(MemoryOrder, PatchStart, PatchEnd, ndims, x_start, x_end, &
  686. y_start, y_end,z_start,z_end)
  687. xsize = x_end - x_start + 1
  688. ysize = y_end - y_start + 1
  689. zsize = z_end - z_start + 1
  690. do idx = 1, len(MemoryOrder)
  691. if ((MemoryOrder(idx:idx) .eq. 'Z') .and. &
  692. (DimNames(idx) .eq. 'soil_layers_stag')) then
  693. soil_layers = .true.
  694. else if ((VarName .eq. 'LANDUSEF') .or. (VarName .eq. 'SOILCBOT') .or. &
  695. (VarName .eq. 'SOILCTOP')) then
  696. fraction = .true.
  697. endif
  698. enddo
  699. if (zsize .eq. 0) then
  700. zsize = 1
  701. endif
  702. !
  703. ! Fill up the variables that hold the vertical coordinate data
  704. !
  705. if (VarName .eq. 'ZNU') then
  706. do idx = 1, zsize
  707. half_eta(idx) = Field(1,idx,1,1)
  708. enddo
  709. half_eta_init = .TRUE.
  710. endif
  711. if (VarName .eq. 'ZNW') then
  712. do idx = 1, zsize
  713. full_eta(idx) = Field(1,idx,1,1)
  714. enddo
  715. full_eta_init = .TRUE.
  716. endif
  717. if (VarName .eq. 'ZS') then
  718. do idx = 1, zsize
  719. soil_depth(idx) = Field(1,idx,1,1)
  720. enddo
  721. soil_depth_init = .TRUE.
  722. endif
  723. if (VarName .eq. 'DZS') then
  724. do idx = 1, zsize
  725. soil_thickness(idx) = Field(1,idx,1,1)
  726. enddo
  727. soil_thickness_init = .TRUE.
  728. endif
  729. !
  730. ! Check to assure that dimensions are valid
  731. !
  732. if ((xsize .lt. 1) .or. (ysize .lt. 1) .or. (zsize .lt. 1)) then
  733. write(msg,*) 'Cannot output field with memory order: ', &
  734. MemoryOrder,Varname
  735. call wrf_message(trim(msg))
  736. return
  737. endif
  738. if (fileinfo(DataHandle)%opened .and. fileinfo(DataHandle)%committed) then
  739. if (StartDate == '') then
  740. StartDate = DateStr
  741. endif
  742. CALL geth_idts(DateStr,StartDate,fcst_secs)
  743. !
  744. ! If this is a new forecast time, and we have not written the
  745. ! last_fcst_secs scalar output yet, then write it here.
  746. !
  747. if ((abs(fcst_secs - 0.0) .gt. 0.01) .and. &
  748. (last_fcst_secs .ge. 0) .and. &
  749. (abs(fcst_secs - last_fcst_secs) .gt. 0.01) .and. &
  750. (abs(last_fcst_secs - fileinfo(DataHandle)%last_scalar_time_written) .gt. 0.01) ) then
  751. call gr2_fill_local_use(DataHandle,scalar_output(DataHandle),&
  752. "WRF_SCALAR",last_fcst_secs,msg,status)
  753. if (status .ne. 0) then
  754. call wrf_message(trim(msg))
  755. return
  756. endif
  757. fileinfo(DataHandle)%last_scalar_time_written = last_fcst_secs
  758. scalar_output(DataHandle) = ''
  759. endif
  760. call get_vert_stag(VarName,Stagger,vert_stag)
  761. do idx = 1, zsize
  762. call gr2_get_levels(VarName, idx, zsize, soil_layers, vert_stag, &
  763. fraction, vert_unit1, vert_unit2, vert_sclFctr1, &
  764. vert_sclFctr2, level1(idx), level2(idx))
  765. enddo
  766. !
  767. ! Get the center lat/lon for the area being output. For some cases (such
  768. ! as for boundary areas, the center of the area is different from the
  769. ! center of the model grid.
  770. !
  771. if (index(Stagger,'X') .le. 0) then
  772. dom_xsize = full_xsize - 1
  773. else
  774. dom_xsize = full_xsize
  775. endif
  776. if (index(Stagger,'Y') .le. 0) then
  777. dom_ysize = full_ysize - 1
  778. else
  779. dom_ysize = full_ysize
  780. endif
  781. CALL get_region_center(MemoryOrder, wrf_projection, center_lat, &
  782. center_lon, dom_xsize, dom_ysize, dx, dy, proj_central_lon, &
  783. proj_center_flag, truelat1, truelat2, xsize, ysize, &
  784. region_center_lat, region_center_lon)
  785. if (ndims .eq. 0) then ! Scalar quantity
  786. ALLOCATE(data(1:1,1:1), STAT=istat)
  787. call gr2_retrieve_data(MemoryOrder, MemoryStart, MemoryEnd, &
  788. xsize, ysize, zsize, z, FieldType, Field, data)
  789. write(tmpstr,'(G17.10)')data(1,1)
  790. CALL gr2_build_string (scalar_output(DataHandle), &
  791. trim(adjustl(VarName)), tmpstr, 1, Status)
  792. DEALLOCATE(data)
  793. else if (ndims .ge. 1) then ! Vector (1-D) and 2/3 D quantities
  794. if (ndims .eq. 1) then ! Handle Vector (1-D) parameters
  795. dim1size = zsize
  796. dim2size = 1
  797. dim3size = 1
  798. else ! Handle 2/3 D parameters
  799. dim1size = xsize
  800. dim2size = ysize
  801. dim3size = zsize
  802. endif
  803. ALLOCATE(data(1:dim1size,1:dim2size), STAT=istat)
  804. CALL get_parminfo(VarName, center, subcenter, MasterTblV, &
  805. LocalTblV, Disc, Category, ParmNum, DecScl, BinScl, status)
  806. if (status .ne. 0) then
  807. write(msg,*) 'Could not find parameter for '// &
  808. trim(VarName)//' Skipping output of '//trim(VarName)
  809. call wrf_message(trim(msg))
  810. Status = WRF_GRIB2_ERR_GRIB2MAP
  811. return
  812. endif
  813. VERTDIM : do dim3 = 1, dim3size
  814. call gr2_retrieve_data(MemoryOrder, MemoryStart, MemoryEnd, xsize, &
  815. ysize, zsize, dim3, FieldType, Field, data)
  816. !
  817. ! Here, we do any necessary conversions to the data.
  818. !
  819. ! Potential temperature is sometimes passed in as perturbation
  820. ! potential temperature (i.e., POT-300). Other times (i.e., from
  821. ! WRF SI), it is passed in as full potential temperature.
  822. ! Here, we convert to full potential temperature by adding 300
  823. ! only if POT < 200 K.
  824. !
  825. if (VarName == 'T') then
  826. if ((data(1,1) < 200) .and. (data(1,1) .ne. 0)) then
  827. data = data + 300
  828. endif
  829. endif
  830. !
  831. ! For precip, we setup the accumulation period, and output a precip
  832. ! rate for time-step precip.
  833. !
  834. if ((VarName .eq. 'RAINCV') .or. (VarName .eq. 'RAINNCV')) then
  835. ! Convert time-step precip to precip rate.
  836. data = data/timestep
  837. accum_period = 0
  838. else
  839. accum_period = 0
  840. endif
  841. !
  842. ! Create indicator and identification sections (sections 0 and 1)
  843. !
  844. CALL gr2_create_w(StartDate, cgrib, lcgrib, production_status, &
  845. Disc, center, subcenter, MasterTblV, LocalTblV, ierr, msg)
  846. if (ierr .ne. 0) then
  847. call wrf_message(trim(msg))
  848. Status = WRF_GRIB2_ERR_GRIBCREATE
  849. return
  850. endif
  851. !
  852. ! Add the grid definition section (section 3) using a 1x1 grid
  853. !
  854. call gr2_addgrid_w(cgrib, lcgrib, center_lat, proj_central_lon, &
  855. wrf_projection, truelat1, truelat2, xsize, ysize, dx, dy, &
  856. region_center_lat, region_center_lon, ierr, msg)
  857. if (ierr .ne. 0) then
  858. call wrf_message(trim(msg))
  859. Status = WRF_GRIB2_ERR_ADDGRIB
  860. return
  861. endif
  862. if (ndims .eq. 1) then
  863. numlevels = zsize
  864. grib_levels(:) = level1(:)
  865. ngrdpts = zsize
  866. else
  867. numlevels = 2
  868. grib_levels(1) = level1(dim3)
  869. grib_levels(2) = level2(dim3)
  870. ngrdpts = xsize*ysize
  871. endif
  872. !
  873. ! Add the Product Definition, Data representation, bitmap
  874. ! and data sections (sections 4-7)
  875. !
  876. call gr2_addfield_w(cgrib, lcgrib, VarName, Category, ParmNum, &
  877. DecScl, BinScl, fcst_secs, vert_unit1, vert_unit2, &
  878. vert_sclFctr1, vert_sclFctr2, numlevels, &
  879. grib_levels, ngrdpts, background_proc_id, forecast_proc_id, &
  880. compression, data, ierr, msg)
  881. if (ierr .eq. 11) then
  882. write(msg,'(A,I7,A)') 'WARNING: decimal scale for field '//&
  883. trim(VarName)//' at level ',grib_levels(1),&
  884. ' was reduced to fit field into 24 bits. '//&
  885. ' Some precision may be lost!'//&
  886. ' To prevent this message, reduce decimal scale '//&
  887. 'factor in '//trim(mapfilename)
  888. call wrf_message(trim(msg))
  889. else if (ierr .eq. 12) then
  890. write(msg,'(A,I7,A)') 'WARNING: binary scale for field '//&
  891. trim(VarName)//' at level ',grib_levels(1), &
  892. ' was reduced to fit field into 24 bits. '//&
  893. ' Some precision may be lost!'//&
  894. ' To prevent this message, reduce binary scale '//&
  895. 'factor in '//trim(mapfilename)
  896. call wrf_message(trim(msg))
  897. else if (ierr .ne. 0) then
  898. call wrf_message(trim(msg))
  899. Status = WRF_GRIB2_ERR_ADDFIELD
  900. return
  901. endif
  902. !
  903. ! Close out the message
  904. !
  905. call gribend(cgrib,lcgrib,lengrib,ierr)
  906. if (ierr .ne. 0) then
  907. write(msg,*) 'gribend failed with ierr: ',ierr
  908. call wrf_message(trim(msg))
  909. Status = WRF_GRIB2_ERR_GRIBEND
  910. return
  911. endif
  912. !
  913. ! Write the data to the file
  914. !
  915. ! call write_file_n(fileinfo(DataHandle)%FileFd, cgrib, lengrib, ierr)
  916. call bawrite(DataHandle, -1, lengrib, bytes_written, cgrib)
  917. if (bytes_written .ne. lengrib) then
  918. write(msg,*) '1 Error writing cgrib to file, wrote: ', &
  919. bytes_written, ' bytes. Tried to write ', lengrib, ' bytes'
  920. call wrf_message(trim(msg))
  921. Status = WRF_GRIB2_ERR_WRITE
  922. return
  923. endif
  924. ENDDO VERTDIM
  925. DEALLOCATE(data)
  926. endif
  927. last_fcst_secs = fcst_secs
  928. endif
  929. deallocate(data, STAT = istat)
  930. Status = WRF_NO_ERR
  931. call wrf_debug ( DEBUG , 'Leaving ext_gr2_write_field')
  932. RETURN
  933. END SUBROUTINE ext_gr2_write_field
  934. !*****************************************************************************
  935. SUBROUTINE ext_gr2_read_field ( DataHandle , DateStr , VarName , Field , &
  936. FieldType , Comm , IOComm, DomainDesc , MemoryOrder , Stagger , &
  937. DimNames , DomainStart , DomainEnd , MemoryStart , MemoryEnd , &
  938. PatchStart , PatchEnd , Status )
  939. USE gr2_data_info
  940. USE grib_mod
  941. IMPLICIT NONE
  942. #include "wrf_status_codes.h"
  943. #include "wrf_io_flags.h"
  944. INTEGER ,intent(in) :: DataHandle
  945. CHARACTER*(*) ,intent(in) :: DateStr
  946. CHARACTER*(*) ,intent(in) :: VarName
  947. integer ,intent(inout) :: FieldType
  948. integer ,intent(inout) :: Comm
  949. integer ,intent(inout) :: IOComm
  950. integer ,intent(inout) :: DomainDesc
  951. character*(*) ,intent(inout) :: MemoryOrder
  952. character*(*) ,intent(inout) :: Stagger
  953. character*(*) , dimension (*) ,intent(inout) :: DimNames
  954. integer ,dimension(*) ,intent(inout) :: DomainStart, DomainEnd
  955. integer ,dimension(*) ,intent(inout) :: MemoryStart, MemoryEnd
  956. integer ,dimension(*) ,intent(inout) :: PatchStart, PatchEnd
  957. integer ,intent(out) :: Status
  958. INTEGER ,intent(out) :: Field(*)
  959. integer :: xsize,ysize,zsize
  960. integer :: x_start,x_end,y_start,y_end,z_start,z_end
  961. integer :: ndims
  962. character (len=1000) :: Value
  963. character (maxMsgSize) :: msg
  964. integer :: ierr
  965. real :: Data
  966. integer :: center, subcenter, MasterTblV, &
  967. LocalTblV, Disc, Category, ParmNum, DecScl, BinScl
  968. integer :: dim1size,dim2size,dim3size,dim3
  969. integer :: idx
  970. integer :: fields_to_skip
  971. integer :: JIDS(JIDSSIZE), JPDTN, JPDT(JPDTSIZE), JGDTN, &
  972. JGDT(JGDTSIZE)
  973. logical :: UNPACK
  974. type(gribfield) :: gfld
  975. logical :: soil_layers, fraction
  976. logical :: vert_stag = .false.
  977. integer :: vert_unit1, vert_unit2
  978. integer :: vert_sclFctr1, vert_sclFctr2
  979. integer :: level1, level2
  980. integer :: di
  981. real :: tmpreal
  982. call wrf_debug ( DEBUG , 'Entering ext_gr2_read_field'//fileinfo(DataHandle)%DataFile)
  983. CALL get_dims(MemoryOrder, PatchStart, PatchEnd, ndims, x_start, x_end, &
  984. y_start, y_end,z_start,z_end)
  985. xsize = x_end - x_start + 1
  986. ysize = y_end - y_start + 1
  987. zsize = z_end - z_start + 1
  988. !
  989. ! Check to assure that dimensions are valid
  990. !
  991. if ((xsize .lt. 1) .or. (ysize .lt. 1) .or. (zsize .lt. 1)) then
  992. write(msg,*) 'Cannot retrieve field with memory order: ', &
  993. MemoryOrder,Varname
  994. Status = WRF_GRIB2_ERR_READ
  995. call wrf_message(trim(msg))
  996. return
  997. endif
  998. if (ndims .eq. 0) then ! Scalar quantity
  999. call gr2_get_metadata_value(scalar_input(DataHandle),trim(VarName),&
  1000. Value,ierr)
  1001. if (ierr /= 0) then
  1002. Status = WRF_GRIB2_ERR_READ
  1003. CALL wrf_message ( &
  1004. "gr2_get_metadata_value failed for Scalar variable "//&
  1005. trim(VarName))
  1006. return
  1007. endif
  1008. READ(Value,*,IOSTAT=ierr)Data
  1009. if (ierr .ne. 0) then
  1010. CALL wrf_message("Reading data from "//trim(VarName)//" failed")
  1011. Status = WRF_GRIB2_ERR_READ
  1012. return
  1013. endif
  1014. if (FieldType .eq. WRF_INTEGER) then
  1015. Field(1:1) = data
  1016. else if ((FieldType .eq. WRF_REAL) .or. (FieldType .eq. WRF_DOUBLE)) then
  1017. Field(1:1) = TRANSFER(data,Field(1),1)
  1018. else
  1019. write (msg,*)'Reading of type ',FieldType,'from grib data not supported, not reading ',VarName
  1020. call wrf_message(msg)
  1021. endif
  1022. else if (ndims .ge. 1) then ! Vector (1-D) and 2/3 D quantities
  1023. if (ndims .eq. 1) then ! Handle Vector (1-D) parameters
  1024. dim1size = zsize
  1025. dim2size = 1
  1026. dim3size = 1
  1027. else ! Handle 2/3 D parameters
  1028. dim1size = xsize
  1029. dim2size = ysize
  1030. dim3size = zsize
  1031. endif
  1032. CALL get_parminfo(VarName, center, subcenter, MasterTblV, &
  1033. LocalTblV, Disc, Category, ParmNum, DecScl, BinScl, status)
  1034. if (status .ne. 0) then
  1035. write(msg,*) 'Could not find parameter for '// &
  1036. trim(VarName)//' Skipping output of '//trim(VarName)
  1037. call wrf_message(trim(msg))
  1038. Status = WRF_GRIB2_ERR_GRIB2MAP
  1039. return
  1040. endif
  1041. CALL get_vert_stag(VarName,Stagger,vert_stag)
  1042. CALL get_soil_layers(VarName,soil_layers)
  1043. VERTDIM : do dim3 = 1, dim3size
  1044. fields_to_skip = 0
  1045. !
  1046. ! First, set all values to wild, then specify necessary values
  1047. !
  1048. call gr2_g2lib_wildcard(JIDS, JPDT, JGDT)
  1049. JIDS(1) = center
  1050. JIDS(2) = subcenter
  1051. JIDS(3) = MasterTblV
  1052. JIDS(4) = LocalTblV
  1053. JIDS(5) = 1 ! Indicates that time is "Start of Forecast"
  1054. READ (StartDate,'(I4.4,1X,I2.2,1X,I2.2,1X,I2.2,1X,I2.2,1X,I2.2)') &
  1055. (JIDS(idx),idx=6,11)
  1056. JIDS(13) = 1 ! Type of processed data(1 for forecast products)
  1057. JPDT(1) = Category
  1058. JPDT(2) = ParmNum
  1059. JPDT(3) = 2 ! Generating process id
  1060. CALL geth_idts(DateStr,StartDate,tmpreal) ! Forecast time
  1061. JPDT(9) = NINT(tmpreal)
  1062. if (ndims .eq. 1) then
  1063. jpdtn = 1000 ! Product definition tmplate (1000 for cross-sxn)
  1064. else
  1065. call gr2_get_levels(VarName, dim3, dim3size, soil_layers, &
  1066. vert_stag, .false., vert_unit1, vert_unit2, vert_sclFctr1, &
  1067. vert_sclFctr2, level1, level2)
  1068. jpdtn = 0 ! Product definition template (0 for horiz grid)
  1069. JPDT(10) = vert_unit1 ! Type of first surface
  1070. JPDT(11) = vert_sclFctr1 ! Scale factor first surface
  1071. JPDT(12) = level1 ! First surface
  1072. JPDT(13) = vert_unit2 ! Type of second surface
  1073. JPDT(14) = vert_sclFctr2 ! Scale factor second surface
  1074. JPDT(15) = level2 ! Second fixed surface
  1075. endif
  1076. JGDTN = -1 ! Indicates that any Grid Display Template is a match
  1077. UNPACK = .TRUE.! Unpack bitmap and data values
  1078. fields_to_skip = 0
  1079. CALL GETGB2(DataHandle, 0, fields_to_skip, &
  1080. fileinfo(DataHandle)%recnum+1, &
  1081. Disc, JIDS, JPDTN, JPDT, JGDTN, JGDT, UNPACK, &
  1082. fileinfo(DataHandle)%recnum, gfld, status)
  1083. if (status .eq. 99) then
  1084. write(msg,*)'Could not find data for field '//trim(VarName)//&
  1085. ' in file '//trim(fileinfo(DataHandle)%DataFile)
  1086. call wrf_message(trim(msg))
  1087. Status = WRF_GRIB2_ERR_READ
  1088. return
  1089. else if (status .ne. 0) then
  1090. write(msg,*)'Retrieving data field '//trim(VarName)//' failed 2.',status,dim3,DataHandle
  1091. call wrf_message(trim(msg))
  1092. Status = WRF_GRIB2_ERR_READ
  1093. return
  1094. endif
  1095. if(FieldType == WRF_DOUBLE) then
  1096. di = 2
  1097. else
  1098. di = 1
  1099. endif
  1100. !
  1101. ! Here, we do any necessary conversions to the data.
  1102. !
  1103. ! The WRF executable (wrf.exe) expects perturbation potential
  1104. ! temperature. However, real.exe expects full potential T.
  1105. ! So, if the program is WRF, subtract 300 from Potential Temperature
  1106. ! to get perturbation potential temperature.
  1107. !
  1108. if (VarName == 'T') then
  1109. if ( &
  1110. (InputProgramName .eq. 'REAL_EM') .or. &
  1111. (InputProgramName .eq. 'IDEAL') .or. &
  1112. (InputProgramName .eq. 'NDOWN_EM')) then
  1113. gfld%fld = gfld%fld - 300
  1114. endif
  1115. endif
  1116. if (ndims .eq. 1) then
  1117. CALL Transpose1D_grib(MemoryOrder, di, FieldType, Field, &
  1118. MemoryStart(1), MemoryEnd(1), MemoryStart(2), MemoryEnd(2), &
  1119. MemoryStart(3), MemoryEnd(3), &
  1120. gfld%fld, zsize)
  1121. else
  1122. CALL Transpose_grib(MemoryOrder, di, FieldType, Field, &
  1123. MemoryStart(1), MemoryEnd(1), MemoryStart(2), MemoryEnd(2), &
  1124. MemoryStart(3), MemoryEnd(3), &
  1125. gfld%fld, dim3, ysize,xsize)
  1126. endif
  1127. call gf_free(gfld)
  1128. enddo VERTDIM
  1129. endif
  1130. Status = WRF_NO_ERR
  1131. call wrf_debug ( DEBUG , 'Leaving ext_gr2_read_field')
  1132. RETURN
  1133. END SUBROUTINE ext_gr2_read_field
  1134. !*****************************************************************************
  1135. SUBROUTINE ext_gr2_get_next_var ( DataHandle, VarName, Status )
  1136. USE gr2_data_info
  1137. IMPLICIT NONE
  1138. #include "wrf_status_codes.h"
  1139. INTEGER , INTENT(IN) :: DataHandle
  1140. CHARACTER*(*) :: VarName
  1141. INTEGER , INTENT(OUT) :: Status
  1142. call wrf_debug ( DEBUG , 'Entering ext_gr2_get_next_var')
  1143. Status = WRF_WARN_NOOP
  1144. RETURN
  1145. END SUBROUTINE ext_gr2_get_next_var
  1146. !*****************************************************************************
  1147. subroutine ext_gr2_end_of_frame(DataHandle, Status)
  1148. USE gr2_data_info
  1149. implicit none
  1150. #include "wrf_status_codes.h"
  1151. integer ,intent(in) :: DataHandle
  1152. integer ,intent(out) :: Status
  1153. call wrf_debug ( DEBUG , 'Entering ext_gr2_end_of_frame')
  1154. Status = WRF_WARN_NOOP
  1155. return
  1156. end subroutine ext_gr2_end_of_frame
  1157. !*****************************************************************************
  1158. SUBROUTINE ext_gr2_iosync ( DataHandle, Status )
  1159. USE gr2_data_info
  1160. IMPLICIT NONE
  1161. #include "wrf_status_codes.h"
  1162. INTEGER , INTENT(IN) :: DataHandle
  1163. INTEGER , INTENT(OUT) :: Status
  1164. integer :: ierror
  1165. call wrf_debug ( DEBUG , 'Entering ext_gr2_iosync')
  1166. Status = WRF_NO_ERR
  1167. if (DataHandle .GT. 0) then
  1168. CALL flush_file(fileinfo(DataHandle)%FileFd)
  1169. else
  1170. Status = WRF_WARN_TOO_MANY_FILES
  1171. endif
  1172. RETURN
  1173. END SUBROUTINE ext_gr2_iosync
  1174. !*****************************************************************************
  1175. SUBROUTINE ext_gr2_inquire_filename ( DataHandle, FileName , FileStat, &
  1176. Status )
  1177. USE gr2_data_info
  1178. IMPLICIT NONE
  1179. #include "wrf_status_codes.h"
  1180. #include "wrf_io_flags.h"
  1181. INTEGER , INTENT(IN) :: DataHandle
  1182. CHARACTER*(*) :: FileName
  1183. INTEGER , INTENT(OUT) :: FileStat
  1184. INTEGER , INTENT(OUT) :: Status
  1185. CHARACTER *80 SysDepInfo
  1186. call wrf_debug ( DEBUG , 'Entering ext_gr2_inquire_filename')
  1187. FileName = fileinfo(DataHandle)%DataFile
  1188. if ((DataHandle .ge. firstFileHandle) .and. &
  1189. (DataHandle .le. maxFileHandles)) then
  1190. FileStat = fileinfo(DataHandle)%FileStatus
  1191. else
  1192. FileStat = WRF_FILE_NOT_OPENED
  1193. endif
  1194. Status = WRF_NO_ERR
  1195. RETURN
  1196. END SUBROUTINE ext_gr2_inquire_filename
  1197. !*****************************************************************************
  1198. SUBROUTINE ext_gr2_get_var_info ( DataHandle , VarName , NDim , &
  1199. MemoryOrder , Stagger , DomainStart , DomainEnd , WrfType, Status )
  1200. USE gr2_data_info
  1201. IMPLICIT NONE
  1202. #include "wrf_status_codes.h"
  1203. integer ,intent(in) :: DataHandle
  1204. character*(*) ,intent(in) :: VarName
  1205. integer ,intent(out) :: NDim
  1206. character*(*) ,intent(out) :: MemoryOrder
  1207. character*(*) ,intent(out) :: Stagger
  1208. integer ,dimension(*) ,intent(out) :: DomainStart, DomainEnd
  1209. integer ,intent(out) :: WrfType
  1210. integer ,intent(out) :: Status
  1211. call wrf_debug ( DEBUG , 'Entering ext_gr2_get_var_info')
  1212. MemoryOrder = ""
  1213. Stagger = ""
  1214. DomainStart(1) = 0
  1215. DomainEnd(1) = 0
  1216. WrfType = 0
  1217. NDim = 0
  1218. CALL wrf_message('ext_gr2_get_var_info not supported for grib version2 data')
  1219. Status = WRF_NO_ERR
  1220. RETURN
  1221. END SUBROUTINE ext_gr2_get_var_info
  1222. !*****************************************************************************
  1223. SUBROUTINE ext_gr2_set_time ( DataHandle, DateStr, Status )
  1224. USE gr2_data_info
  1225. IMPLICIT NONE
  1226. #include "wrf_status_codes.h"
  1227. INTEGER , INTENT(IN) :: DataHandle
  1228. CHARACTER*(*) :: DateStr
  1229. INTEGER , INTENT(OUT) :: Status
  1230. integer :: found_time
  1231. integer :: idx
  1232. call wrf_debug ( DEBUG , 'Entering ext_gr2_set_time')
  1233. found_time = 0
  1234. do idx = 1,fileinfo(DataHandle)%NumberTimes
  1235. if (fileinfo(DataHandle)%Times(idx) == DateStr) then
  1236. found_time = 1
  1237. fileinfo(DataHandle)%CurrentTime = idx
  1238. endif
  1239. enddo
  1240. if (found_time == 0) then
  1241. Status = WRF_WARN_TIME_NF
  1242. else
  1243. Status = WRF_NO_ERR
  1244. endif
  1245. RETURN
  1246. END SUBROUTINE ext_gr2_set_time
  1247. !*****************************************************************************
  1248. SUBROUTINE ext_gr2_get_next_time ( DataHandle, DateStr, Status )
  1249. USE gr2_data_info
  1250. IMPLICIT NONE
  1251. #include "wrf_status_codes.h"
  1252. INTEGER , INTENT(IN) :: DataHandle
  1253. CHARACTER*(*) , INTENT(OUT) :: DateStr
  1254. INTEGER , INTE

Large files files are truncated, but you can click here to view the full file