NCEPLIBS-g2  3.4.5
gribinfo.f
Go to the documentation of this file.
1 
6 
68 
69  subroutine gribinfo(cgrib,lcgrib,listsec0,listsec1,
70  & numlocal,numfields,maxvals,ierr)
71 
72  character(len=1),intent(in) :: cgrib(lcgrib)
73  integer,intent(in) :: lcgrib
74  integer,intent(out) :: listsec0(3),listsec1(13),maxvals(7)
75  integer,intent(out) :: numlocal,numfields,ierr
76 
77  character(len=4),parameter :: grib='GRIB',c7777='7777'
78  character(len=4) :: ctemp
79  integer,parameter :: zero=0,one=1
80  integer,parameter :: mapsec1len=13
81  integer,parameter ::
82  & mapsec1(mapsec1len)=(/ 2,2,1,1,1,2,1,1,1,1,1,1,1 /)
83  integer iofst,ibeg,istart
84 
85  ierr=0
86  numlocal=0
87  numfields=0
88  maxsec2len=1
89  maxgdstmpl=1
90  maxdeflist=1
91  maxpdstmpl=1
92  maxcoordlist=1
93  maxdrstmpl=1
94  maxgridpts=0
95 !
96 ! Check for beginning of GRIB message in the first 100 bytes
97 !
98  istart=0
99  do j=1,100
100  ctemp=cgrib(j)//cgrib(j+1)//cgrib(j+2)//cgrib(j+3)
101  if (ctemp.eq.grib ) then
102  istart=j
103  exit
104  endif
105  enddo
106  if (istart.eq.0) then
107  print *,'gribinfo: Beginning characters GRIB not found.'
108  ierr=1
109  return
110  endif
111 !
112 ! Unpack Section 0 - Indicator Section
113 !
114  iofst=8*(istart+5)
115  call g2_gbytec(cgrib,listsec0(1),iofst,8) ! Discipline
116  iofst=iofst+8
117  call g2_gbytec(cgrib,listsec0(2),iofst,8) ! GRIB edition number
118  iofst=iofst+8
119  iofst=iofst+32
120  call g2_gbytec(cgrib,lengrib,iofst,32) ! Length of GRIB message
121  iofst=iofst+32
122  listsec0(3)=lengrib
123  lensec0=16
124  ipos=istart+lensec0
125 !
126 ! Currently handles only GRIB Edition 2.
127 !
128  if (listsec0(2).ne.2) then
129  print *,'gribinfo: can only decode GRIB edition 2.'
130  ierr=2
131  return
132  endif
133 !
134 ! Unpack Section 1 - Identification Section
135 !
136  call g2_gbytec(cgrib,lensec1,iofst,32) ! Length of Section 1
137  iofst=iofst+32
138  call g2_gbytec(cgrib,isecnum,iofst,8) ! Section number ( 1 )
139  iofst=iofst+8
140  if (isecnum.ne.1) then
141  print *,'gribinfo: Could not find section 1.'
142  ierr=3
143  return
144  endif
145  !
146  ! Unpack each input value in array listsec1 into the
147  ! the appropriate number of octets, which are specified in
148  ! corresponding entries in array mapsec1.
149  !
150  do i=1,mapsec1len
151  nbits=mapsec1(i)*8
152  call g2_gbytec(cgrib,listsec1(i),iofst,nbits)
153  iofst=iofst+nbits
154  enddo
155  ipos=ipos+lensec1
156 !
157 ! Loop through the remaining sections keeping track of the
158 ! length of each. Also count the number of times Section 2
159 ! and Section 4 appear.
160 !
161  do
162  ctemp=cgrib(ipos)//cgrib(ipos+1)//cgrib(ipos+2)//cgrib(ipos+3)
163  if (ctemp.eq.c7777 ) then
164  ipos=ipos+4
165  if (ipos.ne.(istart+lengrib)) then
166  print *,'gribinfo: "7777" found, but not where expected.'
167  ierr=4
168  return
169  endif
170  exit
171  endif
172  iofst=(ipos-1)*8
173  call g2_gbytec(cgrib,lensec,iofst,32) ! Get Length of Section
174  iofst=iofst+32
175  call g2_gbytec(cgrib,isecnum,iofst,8) ! Get Section number
176  iofst=iofst+8
177  ipos=ipos+lensec ! Update beginning of section pointer
178  if (ipos.gt.(istart+lengrib)) then
179  print *,'gribinfo: "7777" not found at end of GRIB message.'
180  ierr=5
181  return
182  endif
183  if (isecnum.eq.2) then ! Local Section 2
184  ! increment counter for total number of local sections found
185  ! and determine largest Section 2 in message
186  numlocal=numlocal+1
187  lenposs=lensec-5
188  if ( lenposs.gt.maxsec2len ) maxsec2len=lenposs
189  elseif (isecnum.eq.3) then
190  iofst=iofst+8 ! skip source of grid def.
191  call g2_gbytec(cgrib,ngdpts,iofst,32) ! Get Num of Grid Points
192  iofst=iofst+32
193  call g2_gbytec(cgrib,nbyte,iofst,8) ! Get Num octets for opt. list
194  iofst=iofst+8
195  if (ngdpts.gt.maxgridpts) maxgridpts=ngdpts
196  lenposs=lensec-14
197  if ( lenposs.gt.maxgdstmpl ) maxgdstmpl=lenposs
198  if (nbyte.ne.0) then
199  lenposs=lenposs/nbyte
200  if ( lenposs.gt.maxdeflist ) maxdeflist=lenposs
201  endif
202  elseif (isecnum.eq.4) then
203  numfields=numfields+1
204  call g2_gbytec(cgrib,numcoord,iofst,16) ! Get Num of Coord Values
205  iofst=iofst+16
206  if (numcoord.ne.0) then
207  if (numcoord.gt.maxcoordlist) maxcoordlist=numcoord
208  endif
209  lenposs=lensec-9
210  if ( lenposs.gt.maxpdstmpl ) maxpdstmpl=lenposs
211  elseif (isecnum.eq.5) then
212  lenposs=lensec-11
213  if ( lenposs.gt.maxdrstmpl ) maxdrstmpl=lenposs
214  endif
215 
216  enddo
217 
218  maxvals(1)=maxsec2len
219  maxvals(2)=maxgdstmpl
220  maxvals(3)=maxdeflist
221  maxvals(4)=maxpdstmpl
222  maxvals(5)=maxcoordlist
223  maxvals(6)=maxdrstmpl
224  maxvals(7)=maxgridpts
225 
226  return
227  end
228 
gribinfo
subroutine gribinfo(cgrib, lcgrib, listsec0, listsec1, numlocal, numfields, maxvals, ierr)
This subroutine searches through a GRIB2 message and returns the number of Local Use Sections and num...
Definition: gribinfo.f:71
g2_gbytec
subroutine g2_gbytec(IN, IOUT, ISKIP, NBYTE)
This subrountine is to extract arbitrary size values from a packed bit string, right justifying each ...
Definition: g2_gbytesc.f:20