NCEPLIBS-g2 4.0.0
Loading...
Searching...
No Matches
degrib2.F90
Go to the documentation of this file.
1
4
14program degrib2
15 use grib_mod
16 use params
17 implicit none
18
19 integer :: msk2, icount, ifl1, itot, j, lengrib, lgrib
20 integer*8 :: iseek8, msk18, lskip8, lgrib8, lengrib8
21 integer :: maxlocal, n, ncgb, numfields, numlocal
22 real :: fldmax, fldmin, sum
23 parameter(msk18 = 32000, msk2 = 4000)
24 character(len = 1), allocatable, dimension(:) :: cgrib
25 integer :: listsec0(3)
26 integer :: listsec1(13)
27 character(len = 250) :: gfile1
28 character(len = 8) :: pabbrev
29 character(len = 40) :: labbrev
30 character(len = 110) :: tabbrev
31 integer(4) narg, iargc, temparg
32 integer :: currlen = 0, numpts = 0
33 logical :: unpack, expand
34 type(gribfield) :: gfld
35 integer :: ierr, ios, is
36
37 call start()
38 unpack = .true.
39 expand = .false.
40
41 ! Get arguments.
42 narg = iargc()
43 if (narg .ne. 1) then
44 call errmsg('degrib2: incorrect usage')
45 call errmsg('usage: degrib2 grib2file')
46 call errexit(2)
47 endif
48
49 ! Open the input file with the bacio library.
50 ifl1 = 10
51 temparg = 1
52 call getarg(temparg, gfile1)
53 ncgb = len_trim(gfile1)
54 call baopenr(ifl1, gfile1(1:ncgb), ios)
55
56 itot = 0
57 icount = 0
58 iseek8 = 0
59 do
60 ! Find a GRIB2 message in the file.
61 call skgb8(ifl1, iseek8, msk18, lskip8, lgrib8)
62 lgrib = lgrib8
63 if (lgrib8 .eq. 0) exit ! end loop at EOF or problem
64
65 ! Read the GRIB2 message from the file.
66 if (lgrib8 .gt. currlen) then
67 if (allocated(cgrib)) deallocate(cgrib)
68 allocate(cgrib(lgrib8), stat = is)
69 currlen = lgrib8
70 endif
71 call bareadl(ifl1, lskip8, lgrib8, lengrib8, cgrib)
72 lengrib = lengrib8
73 if (lgrib8 .ne. lengrib) then
74 write(6, *)' degrib2: IO Error.'
75 call errexit(9)
76 endif
77 iseek8 = lskip8 + lgrib8
78 icount = icount + 1
79 write (6, *)
80 write(6, '(A,I0,A,I0)') ' GRIB MESSAGE ', icount, ' starts at ', lskip8 + 1
81 write (6, *)
82
83 ! Get info about the message.
84 call gb_info(cgrib, lengrib, listsec0, listsec1, &
85 numfields, numlocal, maxlocal, ierr)
86 if (ierr .ne. 0) then
87 write(6, '(A,I0)') ' ERROR extracting field = ', ierr
88 stop 10
89 endif
90 itot = itot + numfields
91 write(6, '(A,3(1x,I0))')' SECTION 0: ', (listsec0(j), j = 1, 3)
92 write(6, '(A,13(1x,I0))')' SECTION 1: ', (listsec1(j), j = 1, 13)
93 write(6, '(A,1x,I0,1x,A,I0,1x,A)') ' Contains ', numlocal, &
94 ' Local Sections and ', numfields, ' data fields.'
95
96 ! Read each field in the message.
97 do n = 1, numfields
98 ! Unpack GRIB2 field.
99 call gf_getfld(cgrib, lengrib, n, unpack, expand, gfld, ierr)
100 if (ierr .ne. 0) then
101 write(6, '(A,I0)') ' ERROR extracting field = ', ierr
102 cycle
103 endif
104
105 write (6, *)
106 write(6, '(A,1x,I0)')' FIELD ', n
107 if (n .eq. 1) then
108 write(6, '(A,2(1x,I0))')' SECTION 0: ', gfld%discipline, gfld%version
109 write(6, '(A,20(1x,I0))')' SECTION 1: ', (gfld%idsect(j), j = 1, gfld%idsectlen)
110 endif
111 if ( associated(gfld%local).AND.gfld%locallen.gt.0 ) then
112 write(6, '(A,I0,A)')' SECTION 2: ', gfld%locallen, ' bytes'
113 endif
114 write(6, '(A,5(1x,I0))')' SECTION 3: ', gfld%griddef, gfld%ngrdpts, gfld%numoct_opt, &
115 gfld%interp_opt, gfld%igdtnum
116 write(6, '(A,1x,I0,A,100(1x,I0))')' GRID TEMPLATE 3.', &
117 gfld%igdtnum, ' : ', (gfld%igdtmpl(j), j = 1, gfld%igdtlen)
118 if (gfld%num_opt .eq. 0) then
119 write(6, *)' NO Optional List Defining Number of Data Points.'
120 else
121 write(6, '(A,1x,150(1x,I0))')' Section 3 Optional List:', &
122 (gfld%list_opt(j), j = 1, gfld%num_opt)
123 endif
124
125 pabbrev = param_get_abbrev(gfld%discipline, gfld%ipdtmpl(1), gfld%ipdtmpl(2))
126 call prlevel(gfld%ipdtnum, gfld%ipdtmpl, labbrev)
127 call prvtime(gfld%ipdtnum, gfld%ipdtmpl, listsec1, tabbrev)
128
129 write(6,'(A,1x,I0,A,A,3(1X,I0),A,80(1x,I0))') ' PRODUCT TEMPLATE 4.', gfld%ipdtnum, &
130 ': ( PARAMETER = ', pabbrev, gfld%discipline, gfld%ipdtmpl(1), gfld%ipdtmpl(2), ' ) ', &
131 (gfld%ipdtmpl(j), j = 1, gfld%ipdtlen)
132
133 write(6, '(A,A,A,A,A)')' FIELD: ', pabbrev, trim(labbrev), " ", trim(tabbrev)
134 if (gfld%num_coord .eq. 0) then
135 write(6, *)' NO Optional Vertical Coordinate List.'
136 else
137 write(6, '(A,1X,150(1x,I0))') ' Section 4 Optional & Coordinates: ', &
138 (gfld%coord_list(j), j = 1, gfld%num_coord)
139 endif
140 if (gfld%ibmap .ne. 255) then
141 write(6, '(A,I0,A,I0)')' Num. of Data Points = ', &
142 gfld%ndpts, ' with BIT-MAP ', gfld%ibmap
143 else
144 write(6, '(A,I0,A)')' Num. of Data Points = ', gfld%ndpts, ' NO BIT-MAP '
145 endif
146 write(6, '(A,I0,A,20(1x,I0))')' DRS TEMPLATE 5. ', gfld%idrtnum, ' : ', &
147 (gfld%idrtmpl(j), j = 1, gfld%idrtlen)
148 if (gfld%ndpts .eq. 0) then
149 fldmax = 0.0
150 fldmin = 0.0
151 numpts = 1
152 sum = 0.0
153 else
154 if (gfld%fld(1) .eq. 9.9990003e+20) then ! checking undefined values
155 fldmax = 0.0
156 fldmin = 99999.99
157 sum = 0.0
158 numpts = 0
159 else
160 fldmax = gfld%fld(1)
161 fldmin = gfld%fld(1)
162 sum = gfld%fld(1)
163 numpts = 1
164 endif
165 do j = 2, gfld%ndpts
166 if (gfld%fld(j) .eq. 9.9990003e+20) then ! checking undefined values
167 cycle
168 end if
169 if (gfld%fld(j) .gt. fldmax) fldmax = gfld%fld(j)
170 if (gfld%fld(j) .lt. fldmin) fldmin = gfld%fld(j)
171 sum = sum + gfld%fld(j)
172 numpts = numpts + 1
173 enddo
174 endif
175
176 write(6, *)' Data Values:'
177 write(6, '(A,I0,A,I0)')' Num. of Data Points = ', &
178 gfld%ndpts, ' Num. of Data Undefined = ', gfld%ndpts-numpts
179 !print *, trim(pabbrev), fldmin, sum / numpts, fldmax
180 write(6, fmt = '( "( PARM= ",A," ) : ", " MIN=",f25.8," AVE=",f25.8, " MAX=",f25.8)') &
181 trim(pabbrev), fldmin, sum / numpts, fldmax
182
183 ! Free memory allocated to hold field.
184 call gf_free(gfld)
185 enddo
186 enddo
187 write(6, *)" "
188 write(6, '(A,I0)')' Total Number of Fields Found = ', itot
189 if (allocated(cgrib)) deallocate(cgrib)
190end program degrib2
program degrib2
This program reads a GRIB2 file and makes an inventory.
Definition degrib2.F90:14
subroutine gb_info(cgrib, lcgrib, listsec0, listsec1, numfields, numlocal, maxlocal, ierr)
Find the number of gridded fields and Local Use Sections in a GRIB2 message.
Definition g2get.F90:54
subroutine gf_free(gfld)
Free memory that was used to store array values in derived type grib_mod::gribfield.
Definition g2gf.F90:585
subroutine gf_getfld(cgrib, lcgrib, ifldnum, unpack, expand, gfld, ierr)
Return the Grid Definition, and Product Definition for a given data field.
Definition g2gf.F90:211
This Fortran module contains the declaration of derived type gribfield.
Definition gribmod.F90:10
This Fortran Module contains info on all the available GRIB Parameters, and their GRIB1 and GRIB2 cod...
Definition params.F90:12
character(len=8) function param_get_abbrev(g2disc, g2cat, g2num)
This function returns the parameter abbreviation for a given GRIB2 Discipline, Category and Parameter...
Definition params.F90:1120
subroutine prlevel(ipdtn, ipdtmpl, labbrev)
Print level information to a character array, given the GRIB2 Product Definition Template information...
Definition prgrib2.F90:24
subroutine prvtime(ipdtn, ipdtmpl, listsec1, tabbrev)
Convert date and time from GRIB2 info to string output.
Definition prgrib2.F90:297
subroutine skgb8(lugb, iseek8, mseek8, lskip8, lgrib8)
Search a file for the next GRIB1 or GRIB2 message.
Definition skgb.F90:53