NCEPLIBS-g2  3.4.5
gb_info.f
Go to the documentation of this file.
1 
6 
52 
53  subroutine gb_info(cgrib,lcgrib,listsec0,listsec1,
54  & numfields,numlocal,maxlocal,ierr)
55 
56  character(len=1),intent(in) :: cgrib(lcgrib)
57  integer,intent(in) :: lcgrib
58  integer,intent(out) :: listsec0(3),listsec1(13)
59  integer,intent(out) :: numlocal,numfields,maxlocal,ierr
60 
61  character(len=4),parameter :: grib='GRIB',c7777='7777'
62  character(len=4) :: ctemp
63  integer,parameter :: zero=0,one=1
64  integer,parameter :: mapsec1len=13
65  integer,parameter ::
66  & mapsec1(mapsec1len)=(/ 2,2,1,1,1,2,1,1,1,1,1,1,1 /)
67  integer iofst,ibeg,istart
68 
69  ierr=0
70  numlocal=0
71  numfields=0
72  maxlocal=0
73 !
74 ! Check for beginning of GRIB message in the first 100 bytes
75 !
76  istart=0
77  do j=1,100
78  ctemp=cgrib(j)//cgrib(j+1)//cgrib(j+2)//cgrib(j+3)
79  if (ctemp.eq.grib ) then
80  istart=j
81  exit
82  endif
83  enddo
84  if (istart.eq.0) then
85  print *,'gb_info: Beginning characters GRIB not found.'
86  ierr=1
87  return
88  endif
89 !
90 ! Unpack Section 0 - Indicator Section
91 !
92  iofst=8*(istart+5)
93  call g2_gbytec(cgrib,listsec0(1),iofst,8) ! Discipline
94  iofst=iofst+8
95  call g2_gbytec(cgrib,listsec0(2),iofst,8) ! GRIB edition number
96  iofst=iofst+8
97  iofst=iofst+32
98  call g2_gbytec(cgrib,lengrib,iofst,32) ! Length of GRIB message
99  iofst=iofst+32
100  listsec0(3)=lengrib
101  lensec0=16
102  ipos=istart+lensec0
103 !
104 ! Currently handles only GRIB Edition 2.
105 !
106  if (listsec0(2).ne.2) then
107  print *,'gb_info: can only decode GRIB edition 2.'
108  ierr=2
109  return
110  endif
111 !
112 ! Unpack Section 1 - Identification Section
113 !
114  call g2_gbytec(cgrib,lensec1,iofst,32) ! Length of Section 1
115  iofst=iofst+32
116  call g2_gbytec(cgrib,isecnum,iofst,8) ! Section number ( 1 )
117  iofst=iofst+8
118  if (isecnum.ne.1) then
119  print *,'gb_info: Could not find section 1.'
120  ierr=3
121  return
122  endif
123  !
124  ! Unpack each input value in array listsec1 into the
125  ! the appropriate number of octets, which are specified in
126  ! corresponding entries in array mapsec1.
127  !
128  do i=1,mapsec1len
129  nbits=mapsec1(i)*8
130  call g2_gbytec(cgrib,listsec1(i),iofst,nbits)
131  iofst=iofst+nbits
132  enddo
133  ipos=ipos+lensec1
134 !
135 ! Loop through the remaining sections to see if they are valid.
136 ! Also count the number of times Section 2
137 ! and Section 4 appear.
138 !
139  do
140  ctemp=cgrib(ipos)//cgrib(ipos+1)//cgrib(ipos+2)//cgrib(ipos+3)
141  if (ctemp.eq.c7777 ) then
142  ipos=ipos+4
143  if (ipos.ne.(istart+lengrib)) then
144  print *,'gb_info: "7777" found, but not where expected.'
145  ierr=4
146  return
147  endif
148  exit
149  endif
150  iofst=(ipos-1)*8
151  call g2_gbytec(cgrib,lensec,iofst,32) ! Get Length of Section
152  iofst=iofst+32
153  call g2_gbytec(cgrib,isecnum,iofst,8) ! Get Section number
154  iofst=iofst+8
155  ipos=ipos+lensec ! Update beginning of section pointer
156  if (ipos.gt.(istart+lengrib)) then
157  print *,'gb_info: "7777" not found at end of GRIB message.'
158  ierr=5
159  return
160  endif
161  if ( isecnum.ge.2.AND.isecnum.le.7 ) then
162  if (isecnum.eq.2) then ! Local Section 2
163  ! increment counter for total number of local sections found
164  numlocal=numlocal+1
165  lenposs=lensec-5
166  if ( lenposs.gt.maxlocal ) maxlocal=lenposs
167  elseif (isecnum.eq.4) then
168  ! increment counter for total number of fields found
169  numfields=numfields+1
170  endif
171  else
172  print *,'gb_info: Invalid section number found in GRIB',
173  & ' message: ',isecnum
174  ierr=6
175  return
176  endif
177 
178  enddo
179 
180  return
181  end
182 
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
gb_info
subroutine gb_info(cgrib, lcgrib, listsec0, listsec1, numfields, numlocal, maxlocal, ierr)
This subroutine searches through a GRIB2 message and returns the number of gridded fields found in th...
Definition: gb_info.f:55