88 subroutine addfield(cgrib,lcgrib,ipdsnum,ipdstmpl,ipdstmplen,
89 & coordlist,numcoord,idrsnum,idrstmpl,
90 & idrstmplen,fld,ngrdpts,ibmap,bmap,ierr)
94 character(len=1),
intent(inout) :: cgrib(lcgrib)
95 integer,
intent(in) :: ipdsnum,ipdstmpl(*)
96 integer,
intent(in) :: idrsnum,numcoord,ipdstmplen,idrstmplen
97 integer,
intent(in) :: lcgrib,ngrdpts,ibmap
98 real,
intent(in) :: coordlist(numcoord)
99 real,
target,
intent(in) :: fld(ngrdpts)
100 integer,
intent(out) :: ierr
101 integer,
intent(inout) :: idrstmpl(*)
102 logical*1,
intent(in) :: bmap(ngrdpts)
104 character(len=4),
parameter :: grib=
'GRIB',c7777=
'7777'
105 character(len=4):: ctemp
106 character(len=1),
allocatable :: cpack(:)
107 real,
pointer,
dimension(:) :: pfld
108 real(4) :: coordieee(numcoord),re00
109 integer(4) :: ire00,allones
110 integer :: mappds(ipdstmplen),intbmap(ngrdpts),mapdrs(idrstmplen)
111 integer,
parameter :: zero=0,one=1,four=4,five=5,six=6,seven=7
112 integer,
parameter :: minsize=50000
113 integer iofst,ibeg,lencurr,len,mappdslen,mapdrslen,lpos3
114 integer width,height,ndpts
115 integer lensec3,lensec4,lensec5,lensec6,lensec7
116 logical issec3,needext,isprevbmap
125 if(cgrib(i) /= grib(i:i))
then
129 if ( .not. match )
then
130 print *,
'addfield: GRIB not found in given message.'
131 print *,
'addfield: Call to routine gribcreate required',
132 &
' to initialize GRIB messge.'
143 ctemp=cgrib(lencurr-3)//cgrib(lencurr-2)//cgrib(lencurr-1)
145 if ( ctemp.eq.c7777 )
then
146 print *,
'addfield: GRIB message already complete. Cannot',
147 &
' add new section.'
167 if (isecnum.eq.3)
then
173 if (isecnum.eq.6)
then
176 if ((ibmprev.ge.0).and.(ibmprev.le.253)) isprevbmap=.true.
180 if ( len.eq.lencurr )
exit
183 if ( len.gt.lencurr )
then
184 print *,
'addfield: Section byte counts don''t add to total.'
185 print *,
'addfield: Sum of section byte counts = ',len
186 print *,
'addfield: Total byte count in Section 0 = ',lencurr
194 if ( (isecnum.ne.3) .and. (isecnum.ne.7) )
then
195 print *,
'addfield: Sections 4-7 can only be added after',
197 print *,
'addfield: Section ',isecnum,
' was the last found in',
198 &
' given GRIB message.'
204 elseif (.not.issec3)
then
205 print *,
'addfield: Sections 4-7 can only be added if Section',
206 &
' 3 was previously included.'
207 print *,
'addfield: Section 3 was not found in',
208 &
' given GRIB message.'
209 print *,
'addfield: Call to routine addgrid required',
210 &
' to specify Grid definition.'
248 nbits=iabs(mappds(i))*8
249 if ( (mappds(i).ge.0).or.(ipdstmpl(i).ge.0) )
then
250 call g2_sbytec(cgrib,ipdstmpl(i),iofst,nbits)
253 call g2_sbytec(cgrib,iabs(ipdstmpl(i)),iofst+1,nbits-1)
261 if ( numcoord .ne. 0 )
then
262 call mkieee(coordlist,coordieee,numcoord)
263 call g2_sbytesc(cgrib,coordieee,iofst,32,0,numcoord)
264 iofst=iofst+(32*numcoord)
270 lensec4=(iofst-ibeg)/8
287 if ( ibmap.eq.0 .OR. ibmap.eq.254 )
then
288 allocate(pfld(max(2,ngrdpts)))
298 if(ndpts==0 .and. ngrdpts>0)
then
307 if (nsize .lt. minsize) nsize=minsize
308 allocate(cpack(nsize),stat=istat)
309 if (idrsnum.eq.0)
then
310 call simpack(pfld,ndpts,idrstmpl,cpack,lcpack)
311 elseif (idrsnum.eq.2.or.idrsnum.eq.3)
then
312 call cmplxpack(pfld,ndpts,idrsnum,idrstmpl,cpack,lcpack)
313 elseif (idrsnum.eq.50)
then
314 call simpack(pfld(2),ndpts-1,idrstmpl,cpack,lcpack)
315 call mkieee(real(pfld(1)),re00,1)
317 ire00=transfer(re00,ire00)
319 elseif (idrsnum.eq.51)
then
320 call getpoly(cgrib(lpos3),lensec3,jj,kk,mm)
321 if (jj.ne.0 .AND. kk.ne.0 .AND. mm.ne.0)
then
322 call specpack(pfld,ndpts,jj,kk,mm,idrstmpl,cpack,lcpack)
324 print *,
'addfield: Cannot pack DRT 5.51.'
329 elseif (idrsnum.eq.40 .OR. idrsnum.eq.40000)
then
330 if (ibmap.eq.255)
then
331 call getdim(cgrib(lpos3),lensec3,width,height,iscan)
332 if ( width.eq.0 .OR. height.eq.0 )
then
335 elseif ( width.eq.allones .OR. height.eq.allones )
then
338 elseif ( ibits(iscan,5,1) .eq. 1)
then
347 if(width<1 .or. height<1)
then
349 write(0,*)
'Warning: bitmask off everywhere.'
350 write(0,*)
' Pretend one point in jpcpack to avoid crash.'
356 call jpcpack(pfld,width,height,idrstmpl,cpack,lcpack)
359 elseif (idrsnum.eq.41 .OR. idrsnum.eq.40010)
then
360 if (ibmap.eq.255)
then
361 call getdim(cgrib(lpos3),lensec3,width,height,iscan)
362 if ( width.eq.0 .OR. height.eq.0 )
then
365 elseif ( width.eq.allones .OR. height.eq.allones )
then
368 elseif ( ibits(iscan,5,1) .eq. 1)
then
378 call pngpack(pfld,width,height,idrstmpl,cpack,lcpack)
381 print *,
'addfield: Data Representation Template 5.',idrsnum,
382 *
' not yet implemented.'
386 if ( ibmap.eq.0 .OR. ibmap.eq.254 )
then
389 if ( lcpack .lt. 0 )
then
390 if(
allocated(cpack) )
deallocate(cpack)
412 nbits=iabs(mapdrs(i))*8
413 if ( (mapdrs(i).ge.0).or.(idrstmpl(i).ge.0) )
then
414 call g2_sbytec(cgrib,idrstmpl(i),iofst,nbits)
417 call g2_sbytec(cgrib,iabs(idrstmpl(i)),iofst+1,nbits-1)
425 lensec5=(iofst-ibeg)/8
441 call g2_sbytesc(cgrib,intbmap,iofst,1,0,ngrdpts)
448 if ((ibmap.eq.254).and.(.not.isprevbmap))
then
449 print *,
'addfield: Requested previously defined bitmap, ',
450 &
' but one does not exist in the current GRIB message.'
463 lensec6=(iofst-ibeg)/8
474 if (lcpack.ne.0)
then
476 cgrib(ioctet+1:ioctet+lcpack)=cpack(1:lcpack)
477 iofst=iofst+(8*lcpack)
483 lensec7=(iofst-ibeg)/8
486 if(
allocated(cpack) )
deallocate(cpack)
490 newlen=lencurr+lensec4+lensec5+lensec6+lensec7