NCEPLIBS-g2  3.4.5
addgrid.f
Go to the documentation of this file.
1 
6 
45 
46  subroutine addgrid(cgrib,lcgrib,igds,igdstmpl,igdstmplen,
47  & ideflist,idefnum,ierr)
48 
49  use gridtemplates
50 
51  character(len=1),intent(inout) :: cgrib(lcgrib)
52  integer,intent(in) :: igds(*),igdstmpl(*),ideflist(idefnum)
53  integer,intent(in) :: lcgrib,idefnum,igdstmplen
54  integer,intent(out) :: ierr
55 
56  character(len=4),parameter :: grib='GRIB',c7777='7777'
57  character(len=4):: ctemp
58  integer:: mapgrid(igdstmplen)
59  integer,parameter :: one=1,three=3
60  integer lensec3,iofst,ibeg,lencurr,len,mapgridlen
61  logical needext
62 
63  ierr=0
64 
65 ! Check to see if beginning of GRIB message exists
66 
67  do i=1,4
68  if(cgrib(i)/=grib(i:i)) then
69  print *,'addgrid: GRIB not found in given message.'
70  print *,'addgrid: Call to routine gribcreate required',
71  & ' to initialize GRIB messge.'
72  10 format('"',4a1,'" /= "GRIB"')
73  print 10,cgrib(1:4)
74  ierr=1
75  stop 1
76  return
77  endif
78  enddo
79 
80 ! Get current length of GRIB message
81 
82  call g2_gbytec(cgrib,lencurr,96,32)
83 
84 ! Check to see if GRIB message is already complete
85 
86  ctemp=cgrib(lencurr-3)//cgrib(lencurr-2)//cgrib(lencurr-1)
87  & //cgrib(lencurr)
88  if ( ctemp.eq.c7777 ) then
89  print *,'addgrid: GRIB message already complete. Cannot',
90  & ' add new section.'
91  ierr=2
92  return
93  endif
94 
95 ! Loop through all current sections of the GRIB message to
96 ! find the last section number.
97 
98  len=16 ! length of Section 0
99  do
100 ! Get section number and length of next section
101  iofst=len*8
102  call g2_gbytec(cgrib,ilen,iofst,32)
103  iofst=iofst+32
104  call g2_gbytec(cgrib,isecnum,iofst,8)
105  len=len+ilen
106 ! Exit loop if last section reached
107  if ( len.eq.lencurr ) exit
108 ! If byte count for each section doesn't match current
109 ! total length, then there is a problem.
110  if ( len.gt.lencurr ) then
111  print *,'addgrid: Section byte counts don''t add to total.'
112  print *,'addgrid: Sum of section byte counts = ',len
113  print *,'addgrid: Total byte count in Section 0 = ',lencurr
114  ierr=3
115  return
116  endif
117  enddo
118 
119 ! Section 3 can only be added after sections 1, 2 and 7.
120 
121  if ( (isecnum.ne.1) .and. (isecnum.ne.2) .and.
122  & (isecnum.ne.7) ) then
123  print *,'addgrid: Section 3 can only be added after Section',
124  & ' 1, 2 or 7.'
125  print *,'addgrid: Section ',isecnum,' was the last found in',
126  & ' given GRIB message.'
127  ierr=4
128  return
129  endif
130 
131 ! Add Section 3 - Grid Definition Section
132 
133  ibeg=lencurr*8 ! Calculate offset for beginning of section 3
134  iofst=ibeg+32 ! leave space for length of section
135  call g2_sbytec(cgrib,three,iofst,8) ! Store section number ( 3 )
136  iofst=iofst+8
137  call g2_sbytec(cgrib,igds(1),iofst,8) ! Store source of Grid def.
138  iofst=iofst+8
139  call g2_sbytec(cgrib,igds(2),iofst,32) ! Store number of data pts.
140  iofst=iofst+32
141  call g2_sbytec(cgrib,igds(3),iofst,8) ! Store number of extra octets.
142  iofst=iofst+8
143  call g2_sbytec(cgrib,igds(4),iofst,8) ! Store interp. of extra octets.
144  iofst=iofst+8
145 ! if Octet 6 is not equal to zero, Grid Definition Template may
146 ! not be supplied.
147  if ( igds(1).eq.0 ) then
148  call g2_sbytec(cgrib,igds(5),iofst,16) ! Store Grid Def Template num.
149  else
150  call g2_sbytec(cgrib,65535,iofst,16) ! Store missing value as Grid Def Template num.
151  endif
152  iofst=iofst+16
153 
154 ! Get Grid Definition Template
155 
156  if (igds(1).eq.0) then
157  call getgridtemplate(igds(5),mapgridlen,mapgrid,needext,
158  & iret)
159  if (iret.ne.0) then
160  ierr=5
161  return
162  endif
163 
164 ! Extend the Grid Definition Template, if necessary.
165 ! The number of values in a specific template may vary
166 ! depending on data specified in the "static" part of the
167 ! template.
168 
169  if ( needext ) then
170  call extgridtemplate(igds(5),igdstmpl,mapgridlen,mapgrid)
171  endif
172  else
173  mapgridlen=0
174  endif
175 
176 ! Pack up each input value in array igdstmpl into the
177 ! the appropriate number of octets, which are specified in
178 ! corresponding entries in array mapgrid.
179 
180  do i=1,mapgridlen
181  nbits=iabs(mapgrid(i))*8
182  if ( (mapgrid(i).ge.0).or.(igdstmpl(i).ge.0) ) then
183  call g2_sbytec(cgrib,igdstmpl(i),iofst,nbits)
184  else
185  call g2_sbytec(cgrib,one,iofst,1)
186  call g2_sbytec(cgrib,iabs(igdstmpl(i)),iofst+1,nbits-1)
187  endif
188  iofst=iofst+nbits
189  enddo
190 
191 ! If requested,
192 ! Insert optional list of numbers defining number of points
193 ! in each row or column. This is used for non regular
194 ! grids.
195 
196  if ( igds(3).ne.0 ) then
197  nbits=igds(3)*8
198  call g2_sbytesc(cgrib,ideflist,iofst,nbits,0,idefnum)
199  iofst=iofst+(nbits*idefnum)
200  endif
201 
202 ! Calculate length of section 3 and store it in octets
203 ! 1-4 of section 3.
204 
205  lensec3=(iofst-ibeg)/8
206  call g2_sbytec(cgrib,lensec3,ibeg,32)
207 
208 
209 ! Update current byte total of message in Section 0
210 
211  call g2_sbytec(cgrib,lencurr+lensec3,96,32)
212 
213  return
214  end
215 
g2_sbytesc
subroutine g2_sbytesc(OUT, IN, ISKIP, NBYTE, NSKIP, N)
This subrountine is to put arbitrary size values into a packed bit string, taking the low order bits ...
Definition: g2_gbytesc.f:115
gridtemplates::extgridtemplate
subroutine extgridtemplate(number, list, nummap, map)
This subroutine generates the remaining octet map for a given Grid Definition Template,...
Definition: gridtemplates.f:341
gridtemplates::getgridtemplate
subroutine getgridtemplate(number, nummap, map, needext, iret)
This subroutine grid template information for a specified Grid Definition Template 3....
Definition: gridtemplates.f:296
g2_sbytec
subroutine g2_sbytec(OUT, IN, ISKIP, NBYTE)
This subrountine is to put arbitrary size values into a packed bit string, taking the low order bits ...
Definition: g2_gbytesc.f:39
gridtemplates
This Fortran Module contains info on all the available GRIB2 Grid Definition Templates used in Sectio...
Definition: gridtemplates.f:45
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
addgrid
subroutine addgrid(cgrib, lcgrib, igds, igdstmpl, igdstmplen, ideflist, idefnum, ierr)
This routine is used with routines gribcreate(), addlocal(), addfield(), and gribend() to create a co...
Definition: addgrid.f:48