72 subroutine addfield(cgrib,lcgrib,ipdsnum,ipdstmpl,ipdstmplen,
73 & coordlist,numcoord,idrsnum,idrstmpl,
74 & idrstmplen,fld,ngrdpts,ibmap,bmap,ierr)
80 character(len=1),
intent(inout) :: cgrib(lcgrib)
81 integer,
intent(in) :: ipdsnum,ipdstmpl(*)
82 integer,
intent(in) :: idrsnum,numcoord,ipdstmplen,idrstmplen
83 integer,
intent(in) :: lcgrib,ngrdpts,ibmap
84 real,
intent(in) :: coordlist(numcoord)
85 real(kind = 4) :: coordlist_4(numcoord)
86 real,
target,
intent(in) :: fld(ngrdpts)
87 integer,
intent(out) :: ierr
88 integer,
intent(inout) :: idrstmpl(*)
89 logical*1,
intent(in) :: bmap(ngrdpts)
91 character(len=4),
parameter :: grib=
'GRIB',c7777=
'7777'
92 character(len=4):: ctemp
93 character(len=1),
allocatable :: cpack(:)
94 real,
pointer,
dimension(:) :: pfld
95 real(4) :: coordieee(numcoord),re00
96 integer(4) :: ire00,allones
97 integer :: mappds(ipdstmplen),intbmap(ngrdpts),mapdrs(idrstmplen)
98 integer,
parameter :: zero=0,one=1,four=4,five=5,six=6,seven=7
99 integer,
parameter :: minsize=50000
100 integer iofst,ibeg,lencurr,len,mappdslen,mapdrslen,lpos3
101 integer width,height,ndpts
102 integer lensec3,lensec4,lensec5,lensec6,lensec7
103 logical issec3,needext,isprevbmap
104 integer :: nbits, newlen, nsize, lcpack, left
105 integer :: ibmprev, ilen, ioctet, iscan, isecnum, itemp
106 integer :: i, jj, kk, mm
107 integer :: iret, istat
109 allones = int(z
'FFFFFFFF')
115 if(cgrib(i) /= grib(i:i))
then
119 if (.not. match)
then
120 print *,
'addfield: GRIB not found in given message.'
121 print *,
'addfield: Call to routine gribcreate required',
122 &
' to initialize GRIB messge.'
131 ctemp=cgrib(lencurr-3)//cgrib(lencurr-2)//cgrib(lencurr-1)
133 if (ctemp.eq.c7777)
then
134 print *,
'addfield: GRIB message already complete. Cannot',
135 &
' add new section.'
154 if (isecnum.eq.3)
then
160 if (isecnum.eq.6)
then
163 if ((ibmprev.ge.0).and.(ibmprev.le.253)) isprevbmap=.true.
167 if (len.eq.lencurr)
exit
170 if (len.gt.lencurr)
then
171 print *,
'addfield: Section byte counts don''t add to total.'
172 print *,
'addfield: Sum of section byte counts = ',len
173 print *,
'addfield: Total byte count in Section 0 = ',lencurr
180 if ((isecnum.ne.3) .and. (isecnum.ne.7))
then
181 print *,
'addfield: Sections 4-7 can only be added after',
183 print *,
'addfield: Section ',isecnum,
' was the last found in',
184 &
' given GRIB message.'
189 elseif (.not.issec3)
then
190 print *,
'addfield: Sections 4-7 can only be added if Section',
191 &
' 3 was previously included.'
192 print *,
'addfield: Section 3 was not found in',
193 &
' given GRIB message.'
194 print *,
'addfield: Call to routine addgrid required',
195 &
' to specify Grid definition.'
229 nbits=iabs(mappds(i))*8
230 if ((mappds(i).ge.0).or.(ipdstmpl(i).ge.0))
then
231 call g2_sbytec(cgrib,ipdstmpl(i),iofst,nbits)
234 call g2_sbytec(cgrib,iabs(ipdstmpl(i)),iofst+1,nbits-1)
241 if (numcoord .ne. 0)
then
243 coordlist_4(i) = real(coordlist(i), 4)
245 call mkieee(coordlist_4, coordieee, numcoord)
246 call g2_sbytesc(cgrib,coordieee,iofst,32,0,numcoord)
247 iofst=iofst+(32*numcoord)
252 lensec4=(iofst-ibeg)/8
266 if (ibmap.eq.0 .OR. ibmap.eq.254)
then
267 allocate(pfld(max(2,ngrdpts)))
277 if(ndpts==0 .and. ngrdpts>0)
then
286 if (nsize .lt. minsize) nsize=minsize
287 allocate(cpack(nsize),stat=istat)
288 if (idrsnum.eq.0)
then
289 call simpack(pfld,ndpts,idrstmpl,cpack,lcpack)
290 elseif (idrsnum.eq.2.or.idrsnum.eq.3)
then
291 call cmplxpack(pfld,ndpts,idrsnum,idrstmpl,cpack,lcpack)
292 elseif (idrsnum.eq.50)
then
293 call simpack(pfld(2),ndpts-1,idrstmpl,cpack,lcpack)
294 call mkieee(real(pfld(1)),re00,1)
296 ire00=transfer(re00,ire00)
298 elseif (idrsnum.eq.51)
then
299 call getpoly(cgrib(lpos3),lensec3,jj,kk,mm)
300 if (jj.ne.0 .AND. kk.ne.0 .AND. mm.ne.0)
then
301 call specpack(pfld,ndpts,jj,kk,mm,idrstmpl,cpack,lcpack)
303 print *,
'addfield: Cannot pack DRT 5.51.'
308 elseif (idrsnum.eq.40 .OR. idrsnum.eq.40000)
then
309 if (ibmap.eq.255)
then
310 call getdim(cgrib(lpos3),lensec3,width,height,iscan)
311 if (width.eq.0 .OR. height.eq.0)
then
314 elseif (width.eq.allones .OR. height.eq.allones)
then
317 elseif (ibits(iscan,5,1) .eq. 1)
then
326 if(width<1 .or. height<1)
then
328 write(0,*)
'Warning: bitmask off everywhere.'
329 write(0,*)
' Pretend one point in jpcpack to avoid crash.'
335 call jpcpack(pfld,width,height,idrstmpl,cpack,lcpack)
337 elseif (idrsnum.eq.41 .OR. idrsnum.eq.40010)
then
338 if (ibmap.eq.255)
then
339 call getdim(cgrib(lpos3),lensec3,width,height,iscan)
340 if (width.eq.0 .OR. height.eq.0)
then
343 elseif (width.eq.allones .OR. height.eq.allones)
then
346 elseif (ibits(iscan,5,1) .eq. 1)
then
356 call pngpack(pfld,width,height,idrstmpl,cpack,lcpack)
359 print *,
'addfield: Data Representation Template 5.',idrsnum,
360 *
' not yet implemented.'
364 if (ibmap.eq.0 .OR. ibmap.eq.254)
then
367 if (lcpack .lt. 0)
then
368 if(
allocated(cpack))
deallocate(cpack)
387 nbits=iabs(mapdrs(i))*8
388 if ((mapdrs(i).ge.0).or.(idrstmpl(i).ge.0))
then
389 call g2_sbytec(cgrib,idrstmpl(i),iofst,nbits)
392 call g2_sbytec(cgrib,iabs(idrstmpl(i)),iofst+1,nbits-1)
399 lensec5=(iofst-ibeg)/8
412 call g2_sbytesc(cgrib,intbmap,iofst,1,0,ngrdpts)
418 if ((ibmap.eq.254).and.(.not.isprevbmap))
then
419 print *,
'addfield: Requested previously defined bitmap, ',
420 &
' but one does not exist in the current GRIB message.'
432 lensec6=(iofst-ibeg)/8
441 if (lcpack.ne.0)
then
443 cgrib(ioctet+1:ioctet+lcpack)=cpack(1:lcpack)
444 iofst=iofst+(8*lcpack)
449 lensec7=(iofst-ibeg)/8
452 if(
allocated(cpack) )
deallocate(cpack)
455 newlen=lencurr+lensec4+lensec5+lensec6+lensec7
subroutine addfield(cgrib, lcgrib, ipdsnum, ipdstmpl, ipdstmplen, coordlist, numcoord, idrsnum, idrstmpl, idrstmplen, fld, ngrdpts, ibmap, bmap, ierr)
Pack up Sections 4 through 7 for a given field and add them to a GRIB2 message.
subroutine cmplxpack(fld, ndpts, idrsnum, idrstmpl, cpack, lcpack)
Pack up a data field using a complex packing algorithm.
subroutine g2_gbytec(in, iout, iskip, nbits)
Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked a...
subroutine g2_sbytec(out, in, iskip, nbits)
Put arbitrary size values into a packed bit string, taking the low order bits from each value in the ...
subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
Put arbitrary size values into a packed bit string, taking the low order bits from each value in the ...
subroutine getdim(csec3, lcsec3, width, height, iscan)
This subroutine returns the dimensions and scanning mode of a grid definition packed in GRIB2 Grid De...
subroutine getpoly(csec3, lcsec3, jj, kk, mm)
Return the J, K, and M pentagonal resolution parameters specified in a GRIB2 Grid Definition Section ...
subroutine jpcpack(fld, width, height, idrstmpl, cpack, lcpack)
Pack a data field into a JPEG2000 code stream.
subroutine mkieee(a, rieee, num)
Copy an array of real to an array of 32-bit IEEE floating points.
Handles Data Representation Templates used in Section 5.
subroutine getdrstemplate(number, nummap, map, needext, iret)
Return DRS template information for a specified Data Representation Template.
Information on all GRIB2 Product Definition Templates used in Section 4 - the Product Definition Sect...
subroutine extpdstemplate(number, list, nummap, map)
This subroutine generates the remaining octet map for a given Product Definition Template,...
subroutine getpdstemplate(number, nummap, map, needext, iret)
This subroutine returns PDS template information for a specified Product Definition Template.
subroutine pngpack(fld, width, height, idrstmpl, cpack, lcpack)
This subroutine packs up a data field into PNG image format.
subroutine simpack(fld, ndpts, idrstmpl, cpack, lcpack)
Pack up a data field using a simple packing algorithm as defined in the GRIB2 documention.
subroutine specpack(fld, ndpts, JJ, KK, MM, idrstmpl, cpack, lcpack)
This subroutine packs a spectral data field using the complex packing algorithm for spherical harmoni...