NCEPLIBS-g2  3.4.5
gettemplates.f
Go to the documentation of this file.
1 
6 
68 
69  subroutine gettemplates(cgrib,lcgrib,ifldnum,igds,igdstmpl,
70  & igdslen,ideflist,idefnum,ipdsnum,ipdstmpl,
71  & ipdslen,coordlist,numcoord,ierr)
72 
73  character(len=1),intent(in) :: cgrib(lcgrib)
74  integer,intent(in) :: lcgrib,ifldnum
75  integer,intent(out) :: igds(*),igdstmpl(*),ideflist(*)
76  integer,intent(out) :: ipdsnum,ipdstmpl(*)
77  integer,intent(out) :: idefnum,numcoord
78  integer,intent(out) :: ierr
79  real,intent(out) :: coordlist(*)
80 
81  character(len=4),parameter :: grib='GRIB',c7777='7777'
82  character(len=4) :: ctemp
83  integer:: listsec0(2)
84  integer iofst,ibeg,istart
85  logical have3,have4
86 
87  have3=.false.
88  have4=.false.
89  ierr=0
90  numfld=0
91 !
92 ! Check for valid request number
93 !
94  if (ifldnum.le.0) then
95  print *,'gettemplates: Request for field number must be ',
96  & 'positive.'
97  ierr=3
98  return
99  endif
100 !
101 ! Check for beginning of GRIB message in the first 100 bytes
102 !
103  istart=0
104  do j=1,100
105  ctemp=cgrib(j)//cgrib(j+1)//cgrib(j+2)//cgrib(j+3)
106  if (ctemp.eq.grib ) then
107  istart=j
108  exit
109  endif
110  enddo
111  if (istart.eq.0) then
112  print *,'gettemplates: Beginning characters GRIB not found.'
113  ierr=1
114  return
115  endif
116 !
117 ! Unpack Section 0 - Indicator Section
118 !
119  iofst=8*(istart+5)
120  call g2_gbytec(cgrib,listsec0(1),iofst,8) ! Discipline
121  iofst=iofst+8
122  call g2_gbytec(cgrib,listsec0(2),iofst,8) ! GRIB edition number
123  iofst=iofst+8
124  iofst=iofst+32
125  call g2_gbytec(cgrib,lengrib,iofst,32) ! Length of GRIB message
126  iofst=iofst+32
127  lensec0=16
128  ipos=istart+lensec0
129 !
130 ! Currently handles only GRIB Edition 2.
131 !
132  if (listsec0(2).ne.2) then
133  print *,'gettemplates: can only decode GRIB edition 2.'
134  ierr=2
135  return
136  endif
137 !
138 ! Loop through the remaining sections keeping track of the
139 ! length of each. Also keep the latest Grid Definition Section info.
140 ! Unpack the requested field number.
141 !
142  do
143  ! Check to see if we are at end of GRIB message
144  ctemp=cgrib(ipos)//cgrib(ipos+1)//cgrib(ipos+2)//cgrib(ipos+3)
145  if (ctemp.eq.c7777 ) then
146  ipos=ipos+4
147  ! If end of GRIB message not where expected, issue error
148  if (ipos.ne.(istart+lengrib)) then
149  print *,'gettemplates: "7777" found, but not where ',
150  & 'expected.'
151  ierr=4
152  return
153  endif
154  exit
155  endif
156  ! Get length of Section and Section number
157  iofst=(ipos-1)*8
158  call g2_gbytec(cgrib,lensec,iofst,32) ! Get Length of Section
159  iofst=iofst+32
160  call g2_gbytec(cgrib,isecnum,iofst,8) ! Get Section number
161  iofst=iofst+8
162  !print *,' lensec= ',lensec,' secnum= ',isecnum
163  !
164  ! If found Section 3, unpack the GDS info using the
165  ! appropriate template. Save in case this is the latest
166  ! grid before the requested field.
167  !
168  if (isecnum.eq.3) then
169  iofst=iofst-40 ! reset offset to beginning of section
170  call unpack3(cgrib,lcgrib,iofst,igds,igdstmpl,igdslen,
171  & ideflist,idefnum,jerr)
172  if (jerr.eq.0) then
173  have3=.true.
174  else
175  ierr=10
176  return
177  endif
178  endif
179  !
180  ! If found Section 4, check to see if this field is the
181  ! one requested.
182  !
183  if (isecnum.eq.4) then
184  numfld=numfld+1
185  if (numfld.eq.ifldnum) then
186  iofst=iofst-40 ! reset offset to beginning of section
187  call unpack4(cgrib,lcgrib,iofst,ipdsnum,ipdstmpl,ipdslen,
188  & coordlist,numcoord,jerr)
189  if (jerr.eq.0) then
190  have4=.true.
191  else
192  ierr=11
193  return
194  endif
195  endif
196  endif
197  !
198  ! Check to see if we read pass the end of the GRIB
199  ! message and missed the terminator string '7777'.
200  !
201  ipos=ipos+lensec ! Update beginning of section pointer
202  if (ipos.gt.(istart+lengrib)) then
203  print *,'gettemplates: "7777" not found at end of GRIB ',
204  & 'message.'
205  ierr=7
206  return
207  endif
208 
209  if (have3.and.have4) return
210 
211  enddo
212 
213 !
214 ! If exited from above loop, the end of the GRIB message was reached
215 ! before the requested field was found.
216 !
217  print *,'gettemplates: GRIB message contained ',numlocal,
218  & ' different fields.'
219  print *,'gettemplates: The request was for the ',ifldnum,
220  & ' field.'
221  ierr=6
222 
223  return
224  end
unpack3
subroutine unpack3(cgrib, lcgrib, iofst, igds, igdstmpl, mapgridlen, ideflist, idefnum, ierr)
This subroutine unpacks Section 3 (Grid Definition Section) starting at octet 6 of that Section.
Definition: getfield.f:353
gettemplates
subroutine gettemplates(cgrib, lcgrib, ifldnum, igds, igdstmpl, igdslen, ideflist, idefnum, ipdsnum, ipdstmpl, ipdslen, coordlist, numcoord, ierr)
This subroutine returns the Grid Definition, and Product Definition for a given data field.
Definition: gettemplates.f:72
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
unpack4
subroutine unpack4(cgrib, lcgrib, iofst, ipdsnum, ipdstmpl, mappdslen, coordlist, numcoord, ierr)
This subroutine unpacks Section 4 (Product Definition Section) starting at octet 6 of that Section.
Definition: getfield.f:482