file_grib.F90 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. !#######################################################################
  2. !
  3. !ProTeX: 1.15
  4. !
  5. !BOI
  6. !
  7. ! !TITLE: File_GRIB - module for GRIB in/output
  8. ! !AUTHORS: Arjo Segers
  9. ! !AFFILIATION: KNMI
  10. ! !DATE: \today
  11. !
  12. ! !INTRODUCTION: Usage
  13. !
  14. ! \bv
  15. ! Provides a module:
  16. !
  17. ! use GribFile, only : ...
  18. !
  19. ! The module provides a structure type containing all what should
  20. ! be known to open, read, and close a grib file:
  21. !
  22. ! type(TGribFile) :: gribfile
  23. !
  24. ! The gribfile is opened either for reading or writing with
  25. ! an 'Init' routine, which is in fact an interface to 'PbOpen'.
  26. ! A type TgribFile contains buffers to store one record
  27. ! of a grib file (a 'grib message').
  28. !
  29. ! 1. To open a grib file for reading, use:
  30. !
  31. ! call Init( gribfile, filename, 'r', status )
  32. ! type(TGribFile), intent(inout) :: gribfile
  33. ! character(len=*), intent(in) :: filename
  34. ! character(len=*), intent(in) :: mode
  35. ! integer, intent(out) :: status
  36. !
  37. ! To read next record in buffer, use:
  38. !
  39. ! call ReadRecord( gribfile, status )
  40. !
  41. ! Contents of the message is extracted with the 'Get' routine,
  42. ! for example:
  43. !
  44. ! call Get( gribfile, status, pid=thepid )
  45. !
  46. ! Complete syntaxis and description of arguments
  47. ! (see also the 'GRIB SECTIONS' part later on):
  48. !
  49. ! call Get( gribfile, status, &
  50. ! model_id, pid, &
  51. ! levtype, level, hyb_a, hyb_b, &
  52. ! reftime, timerange, &
  53. ! gridtype, &
  54. ! ll, &
  55. ! lon_first, lon_last, lon_inc, lon_n, &
  56. ! lat_first, lat_last, lat_inc, lat_n, &
  57. ! T, sh, &
  58. ! N, gg )
  59. !
  60. ! integer, intent(out), optional :: model_id
  61. ! ! Element 3 of section 1.
  62. !
  63. ! integer, intent(out), optional :: pid
  64. ! ! Parameter identification.
  65. ! ! See the integer parameters 'pid_' in the code.
  66. ! ! Element 6 of section 1.
  67. !
  68. ! integer, intent(out), optional :: levtype, level
  69. ! ! Elements 7-9 of section 1.
  70. ! ! --> Predefined values for level type:
  71. ! ! integer, parameter :: levtype_sfc = 1 ! surface
  72. ! ! integer, parameter :: levtype_hyb = 109 ! hybrid level
  73. !
  74. ! real, intent(out), optional :: hyb_a(:), hyb_b(:)
  75. ! ! Vertical parameters in real section 2
  76. !
  77. ! integer, intent(out), optional :: reftime(5)
  78. ! ! Reference time: a 5 element integer array with
  79. ! ! year_inc_century, month, day, hour, minutes
  80. ! ! Elements 10-14,21 of section 1.
  81. !
  82. ! integer, intent(out), optional :: timerange(4)
  83. ! ! Time range: a 4 element integer array with
  84. ! ! time range unit, value 1, value 2, indicator
  85. ! ! Elements 15-18 of section 1.
  86. !
  87. ! integer, intent(out), optional :: gridtype
  88. ! ! Element 1 of section 2.
  89. ! ! --> Predefined values:
  90. ! ! integer, parameter :: gridtype_ll = 0 ! lat/lon
  91. ! ! integer, parameter :: gridtype_gg = 4 ! gaussian grid
  92. ! ! integer, parameter :: gridtype_sh = 50 ! spectral
  93. !
  94. ! integer, intent(out), optional :: lon_first, lon_last, lon_inc, lon_n
  95. ! integer, intent(out), optional :: lat_first, lon_last, lat_inc, lat_n
  96. ! ! Elements of section 2
  97. ! ! NOTE: values for lat/lon in mili degrees !
  98. !
  99. ! real, intent(out), optional :: ll(:,:)
  100. ! ! Value of lat/lon grid (stored from west->east, north->south!)
  101. !
  102. ! complex, intent(out), optional :: sh(:)
  103. ! integer, intent(out), optional :: T
  104. ! ! spectral coefficients;
  105. ! ! size should match with spectral truncation 'T'
  106. !
  107. ! real, intent(out), optional :: gg(:)
  108. ! integer, intent(out), optional :: N
  109. ! ! data values on Gaussian grid;
  110. ! ! number of values should match grid size 'N'
  111. !
  112. ! Contents could be checked with the 'Check' routine,
  113. ! for example:
  114. !
  115. ! call Check( gribfile, status, pid=154 )
  116. !
  117. ! Complete syntaxis:
  118. !
  119. ! call Check( gribfile, status, debug=1, &
  120. ! model_id, pid, &
  121. ! levtype, level, &
  122. ! reftime, timerange, &
  123. ! gridtype, &
  124. ! lon_first, lon_last, lon_inc, lon_n, &
  125. ! lat_first, lat_last, lat_inc, lat_n, &
  126. ! T )
  127. !
  128. ! type(TGribFile), intent(in) :: gribfile
  129. ! integer, intent(out) :: status
  130. !
  131. ! integer, intent(in), optional :: debug
  132. !
  133. ! integer, intent(in), optional :: model_id
  134. ! integer, intent(in), optional :: pid
  135. ! integer, intent(in), optional :: levtype, level
  136. ! integer, intent(in), optional :: reftime(5)
  137. ! integer, intent(in), optional :: timerange(4)
  138. ! integer, intent(in), optional :: gridtype
  139. ! integer, intent(in), optional :: lon_first, lon_last, lon_inc, lon_n
  140. ! integer, intent(in), optional :: lat_first, lat_last, lat_inc, lat_n
  141. ! integer, intent(in), optional :: T
  142. !
  143. ! integer, intent(in), optional :: status
  144. ! integer, intent(out), optional :: status
  145. !
  146. ! See the 'Get' command above for a description.
  147. ! In case of one or more fields not matching the grib field:
  148. ! o return status is equal to number of tests failed;
  149. ! o info about the failed test is printed if debug is present and >0 .
  150. !
  151. ! Note: hybride coefficients can not checked due to a
  152. ! lack of precission ... Similar for grid values (ll, gg, and sh).
  153. !
  154. ! The grib file is closed with a 'Done' routine
  155. ! (interface to 'PbClose'):
  156. !
  157. ! call Done( gribfile, status )
  158. !
  159. !
  160. ! 2. To open a grib file for writing, use:
  161. !
  162. ! call Init( gribfile, 'test.gb', 'w', status )
  163. !
  164. ! For safety, set all sections in the buffer to zero
  165. ! or an other safe value:
  166. !
  167. ! call Clear( gribfile, status )
  168. !
  169. ! Fill some or all of the appropriate fields with
  170. ! the 'Set' command:
  171. !
  172. ! call Set( gribfile, status, &
  173. ! model_id, pid, &
  174. ! levtype, hyb_a, hyb_b, level, &
  175. ! reftime, timerange, &
  176. ! gridtype, &
  177. ! ll, lon_n, lat_n, &
  178. ! lon_first, lon_inc, lon_last, &
  179. ! lat_first, lat_inc, lat_last, &
  180. ! scanning_mode, nbits )
  181. !
  182. ! type(TGribFile), intent(inout) :: gribfile
  183. ! integer, intent(out) :: status
  184. !
  185. ! integer, intent(in), optional :: debug
  186. !
  187. ! integer, intent(in), optional :: model_id
  188. ! integer, intent(in), optional :: pid
  189. ! integer, intent(in), optional :: levtype
  190. ! integer, intent(in), optional :: level
  191. ! real, intent(in), optional :: hyb_a(:), hyb_b(:)
  192. ! integer, intent(in), optional :: reftime(5) ! (/yy,mm,dd,hh,min/)
  193. ! integer, intent(in), optional :: timerange(4)
  194. ! integer, intent(in), optional :: gridtype
  195. ! real, intent(in), optional :: ll(:,:)
  196. ! integer, intent(in), optional :: lon_n, lat_n
  197. ! integer, intent(in), optional :: lon_first, lon_inc, lon_last ! mili degree
  198. ! integer, intent(in), optional :: lat_first, lat_inc, lat_last ! mili degree
  199. !
  200. ! See the 'Get' command for a description of most of the fields.
  201. ! Only used as arguments to 'Set':
  202. !
  203. ! integer, intent(in), optional :: scanning_mode
  204. ! ! Something with storage order; does not work proper yet.
  205. !
  206. ! integer, intent(in), optional :: nbits
  207. ! ! Number of bits used to store a real value; default 24 .
  208. !
  209. ! To write the record to the file, use:
  210. !
  211. ! call WriteRecord( gribfile, status )
  212. !
  213. ! 3. To check wether a file is assigned to a grib structure,
  214. ! use the logical function 'Opened' :
  215. !
  216. ! if ( Opened(gribfile) ) ...
  217. ! logical :: Opened
  218. ! type(TgribFile), intent(in) :: gribfile
  219. !
  220. !
  221. ! 4. To copy the buffer from one to another sturcture, use:
  222. !
  223. ! call CopySections( gribIn, gribOut, status )
  224. ! type(TGribFile), intent(in) :: gribIn
  225. ! type(TGribFile), intent(out) :: gribOut
  226. !
  227. ! to copy all sections except real section 4 (the actual data);
  228. ! to copy the data too, use :
  229. !
  230. ! call CopyAllSections( gribIn, gribOut, status )
  231. ! type(TGribFile), intent(in) :: gribIn
  232. ! type(TGribFile), intent(out) :: gribOut
  233. !
  234. ! \ev
  235. !
  236. ! !INTRODUCTION: Grib sections
  237. !
  238. ! Overview of some useful entries in grib files.
  239. ! For code tables, see:
  240. ! o WMO GRIB code tables
  241. ! wms.ecmwf.int/documents/manuals/libraries/gribex/wmoCodeTables.html
  242. ! o ECMWF local table 2 versions
  243. ! wms.ecmwf.int/documents/manuals/libraries/tables/tables_index.html
  244. !
  245. ! \bv
  246. ! Section 1 - Product Definition Section.
  247. ! ---------------------------------------
  248. !
  249. ! 1. Code Table 2 Version Number. 128
  250. ! 2. Originating centre identifier. 98
  251. ! 3. Model identification. 199
  252. ! 4. Grid definition. 255
  253. ! 5. Flag (Code Table 1) 10000000
  254. !
  255. ! 6. Parameter identifier (Code Table 2).
  256. !
  257. ! 7. Type of level (Code Table 3). 109
  258. ! 8. Value 1 of level (Code Table 3).
  259. ! 9. Value 2 of level (Code Table 3).
  260. !
  261. ! 10. Year of reference time of data. 100
  262. ! 11. Month of reference time of data. 8
  263. ! 12. Day of reference time of data. 10
  264. ! 13. Hour of reference time of data. 12
  265. ! 14. Minute of reference time of data. 0
  266. !
  267. ! 15. Time unit (Code Table 4). 1
  268. ! 16. Time range one. 0
  269. ! 17. Time range two. 0
  270. ! 18. Time range indicator (Code Table 5) 0
  271. !
  272. ! 21. Century of reference time of data.
  273. !
  274. ! Section 2 - Grid Description Section.
  275. ! -------------------------------------
  276. !
  277. ! Southern latitudes and Western longitudes are negative.
  278. ! Unit is mili degrees.
  279. !
  280. ! 1. Data represent type = lat/long (Table 6) 0
  281. ! 2. Number of points along a parallel. 360
  282. ! 3. Number of points along a meridian. 181
  283. ! 4. Latitude of first grid point. 90000
  284. ! 5. Longitude of first grid point. -180000
  285. ! 6. Resolution and components flag. 10000000
  286. ! 7. Latitude of last grid point. -90000
  287. ! 8. Longitude of last grid point. 179000
  288. ! 9. i direction (East-West) increment. 1000
  289. ! 10. j direction (North-South) increment. 1000
  290. ! 11. Scanning mode flags (Code Table 8) 00000000
  291. !
  292. ! 1. Data represent type = spectral (Table 6) 50
  293. ! 2. J - Pentagonal resolution parameter. 106 <--- T
  294. ! 3. K - Pentagonal resolution parameter. 106
  295. ! 4. M - Pentagonal resolution parameter. 106
  296. ! 5. Representation type (Table 9) 1
  297. ! 6. Representation mode (Table 10). 2
  298. !
  299. ! 1. Data represent type = gaussian lat/lon (Table 6) 4
  300. ! 2. Number of points along a parallel <0=reduced, >0=regular
  301. ! 3. Number of points along a meridian. 160
  302. ! 4. Latitude of first grid point. 89141
  303. ! 5. Longitude of first grid point. 0
  304. ! 6. Resolution and components flag. 00000000
  305. ! 7. Latitude of last grid point. -89141
  306. ! 8. Longitude of last grid point. 358878
  307. ! 9. i direction (East-West) increment Not given
  308. ! 10. Number of parallels between pole and equator. 80 <--- N
  309. ! 11. Scanning mode flags (Code Table 8) 00000000
  310. ! 23-22*2N. Number of points along a parallel for reduced grid
  311. !
  312. ! 12. Number of vertical coordinate parameters. 122
  313. ! (parameters stored in real part of section 2, starting form index 11)
  314. !
  315. ! Section 4 - Binary Data Section.
  316. ! -------------------------------------
  317. !
  318. ! 1. Number of data values coded/decoded. 11556
  319. ! 2. Number of bits per data value. 16
  320. !
  321. ! 3. Type of data (0=grid pt, 128=spectral). 128
  322. ! 4. Type of packing (0=simple, 64=complex). 64
  323. ! 5. Type of data (0=float, 32=integer). 0
  324. ! 6. Additional flags (0=none, 16=present). 0
  325. ! 7. Reserved. 0
  326. ! 8. Number of values (0=single, 64=matrix). 0
  327. ! 9. Secondary bit-maps (0=none, 32=present). 0
  328. ! 10. Values width (0=constant, 16=variable). 0
  329. ! 11. Byte offset of start of packed data (N). 2214
  330. ! 12. Power (P * 1000). 500
  331. ! 13. Pentagonal resolution parameter J for subset. 20
  332. ! 14. Pentagonal resolution parameter K for subset. 20
  333. ! 15. Pentagonal resolution parameter M for subset. 20
  334. !
  335. ! 3. Type of data (0=grid pt, 128=spectral). 0
  336. ! 4. Type of packing (0=simple, 64=complex). 0
  337. ! 5. Type of data (0=float, 32=integer). 0
  338. ! 6. Additional flags (0=none, 16=present). 0
  339. ! 7. Reserved. 0
  340. ! 8. Number of values (0=single, 64=matrix). 0
  341. ! 9. Secondary bit-maps (0=none, 32=present). 0
  342. ! 10. Values width (0=constant, 16=variable). 0
  343. !
  344. !
  345. ! \ev
  346. !
  347. !EOI
  348. !
  349. !#######################################################################
  350. !
  351. #include "tmm.inc"
  352. !
  353. !#######################################################################
  354. MODULE FILE_GRIB
  355. #ifdef with_grib_api
  356. USE FILE_GRIB_API
  357. #else
  358. BUG_define_a_with_grib_macro_to_select_the_proper_module
  359. #endif
  360. USE FILE_GRIB_BASE, ONLY : LEVTYPE_SFC, LEVTYPE_HYB, LEVTYPE_LAND
  361. USE FILE_GRIB_BASE, ONLY : GRIDTYPE_LL, GRIDTYPE_GG, GRIDTYPE_RED_GG, GRIDTYPE_SH
  362. END MODULE FILE_GRIB