8 C EXAMPLE OF USING BUFR UNPACKING/PACKING SOFTWARE.
43 C M. DRAGOSAVAC *ECMWF* 15/09/87.
52 IMPLICIT LOGICAL(L,O,G), CHARACTER*8(C,H,Y)
54 PARAMETER(JSUP = 9,JSEC0= 3,JSEC1= 40,JSEC2= 64 ,JSEC3= 4,
55 1 JSEC4= 2,JELEM=80000,JSUBS=400,JCVAL=150 ,JBUFL=512000,
57 2 JBPW = 64,JTAB =1000,JCTAB=120,JCTST=1800,JCTEXT=1200,
59 2 JBPW = 32,JTAB =1000,JCTAB=120,JCTST=1800,JCTEXT=1200,
61 3 JWORK=4096000,JKEY=46,JBYTE=2048000)
63 PARAMETER (KELEM=40000)
64 PARAMETER (KVALS=4096000)
66 DIMENSION KBUFF(JBUFL)
67 DIMENSION KBUFR(JBUFL)
68 DIMENSION KSUP(JSUP) ,KSEC0(JSEC0),KSEC1(JSEC1)
69 DIMENSION KSEC2(JSEC2),KSEC3(JSEC3),KSEC4(JSEC4)
70 DIMENSION KEY (JKEY),KREQ(2)
73 REAL*8 VALUES(KVALS),VALUE(KVALS)
78 DIMENSION KTDLST(JELEM),KTDEXP(JELEM),KRQ(KELEM)
79 DIMENSION KDATA(200),KBOXR(JELEM*4)
82 CHARACTER*256 CFIN,COUT,CARG(4)
83 CHARACTER*64 CNAMES(KELEM),CBOXN(JELEM*4)
84 CHARACTER*24 CUNITS(KELEM),CBOXU(JELEM*4)
85 CHARACTER*80 CVALS(kelem)
86 CHARACTER*80 CVAL(kelem)
89 C ------------------------------------------------------------------
90 C* 1. INITIALIZE CONSTANTS AND VARIABLES.
91 C -----------------------------------
94 C MISSING VALUE INDICATOR
107 C GET INPUT AND OUTPUT FILE NAME.
109 NARG=COMMAND_ARGUMENT_COUNT()
113 CALL GET_COMMAND_ARGUMENT(number=J, value=CARG(J))
119 IF(CARG(J).EQ.'-i') THEN
121 ELSEIF(CARG(J).EQ.'-o') THEN
126 PRINT*,'USAGE -- decode_bufr -i infile'
129 IF(IO.EQ.0.and.IN.EQ.0) THEN
130 PRINT*,'USAGE -- decode_bufr -i infile -o outfile'
135 IF(IO.NE.0) COUT=CARG(IO+1)
149 CALL PBOPEN(IUNIT1,COUT(1:JJ),'W',IRET)
150 IF(IRET.EQ.-1) STOP 'OPEN FAILED ON BUFR.DAT'
151 IF(IRET.EQ.-2) STOP 'INVALID FILE NAME'
152 IF(IRET.EQ.-3) STOP 'INVALID OPEN MODE SPECIFIED'
161 C SET REQUEST FOR PARTIAL EXPANSION
172 C* 1.2 OPEN FILE CONTAINING BUFR DATA.
173 C -------------------------------
177 CALL PBOPEN(IUNIT,CFIN(1:ILN),'R',IRET)
178 IF(IRET.EQ.-1) STOP 'OPEN FAILED'
179 IF(IRET.EQ.-2) STOP 'INVALID FILE NAME'
180 IF(IRET.EQ.-3) STOP 'INVALID OPEN MODE SPECIFIED'
185 C -----------------------------------------------------------------
186 C* 2. SET REQUEST FOR EXPANSION.
187 C --------------------------
192 WRITE(*,'(A,$)') ' DO YOU WANT TO PRINT( Y/N ) : '
194 IF(YENC(1:1).EQ.'Y'.OR.YENC(1:1).EQ.'y') THEN
198 WRITE(*,'(A,$)') ' CODE TABLES TO BE PRINTED ( Y/N ) : '
200 IF(YCODC(1:1).EQ.'Y'.OR.YCODC(1:1).EQ.'y') THEN
203 WRITE(*,'(A,$)') ' DO YOU WANT ENCODING( Y/N ) : '
205 IF(YENC(1:1).EQ.'Y'.OR.YENC(1:1).EQ.'y') THEN
207 WRITE(*,'(A,$)') ' NUMBER OF SUBSETS TO PACK : '
208 READ(*,'(BN,I4)') NCOM
210 WRITE(*,'(A,$)') ' DO YOU WANT COMPRESSION( Y/N ) : '
212 IF(YCOMP(1:1).EQ.'Y'.OR.YCOMP(1:1).EQ.'y') OCOMP=.TRUE.
214 WRITE(*,'(A,$)') ' RECORD NUMBER TO START FROM : '
219 WRITE(*,'(A,$)') ' REQUESTED ELEMENT : '
220 READ(*,'(BN,I6)') IEL
221 WRITE(*,'(A,$)') ' REQUESTED VALUE : '
222 READ(*,'(BN,F12.2)') VAL
229 IF(VAL.EQ.0.) RQV(J)=RVIND
233 WRITE(*,'(A,$)') ' REQUESTED FLAG 1 : '
234 READ(*,'(BN,I6)') KREQ(1)
236 WRITE(*,'(A,$)') ' REQUESTED FLAG 2 : '
237 READ(*,'(BN,I6)') KREQ(2)
239 WRITE(*,'(A,$)') ' DO YOU WANT TO PRINT SECTION 0-3( Y/N ) : '
240 READ (*,'(A,$)') YENC
242 IF(YENC(1:1).EQ.'Y'.OR.YENC(1:1).EQ.'y') OSEC3=.TRUE.
244 C* 2.1 SET REQUEST FOR PARTIAL EXPANSION.
245 C ----------------------------------
249 CALL BUSRQ(KREQ,KRQL,KRQ,RQV,KERR)
251 C SET VARIABLE TO PACK BIG VALUES AS MISSING VALUE INDICATOR
256 CALL BUPRQ(KPMISS,KPRUS,KOKEY)
258 C -----------------------------------------------------------------
259 C* 3. READ BUFR MESSAGE.
267 CALL PBBUFR(IUNIT,KBUFF,JBYTE,KBUFL,IRET)
270 IF(NPACK.NE.0) GO TO 600
272 PRINT*,'NUMBER OF SUBSETS ',IOBS
273 PRINT*,'NUMBER OF MESSAGES ',N
274 CALL PBCLOSE(IUNIT,IRET)
277 IF(IRET.EQ.-2) STOP 'FILE HANDLING PROBLEM'
278 IF(IRET.EQ.-3) STOP 'ARRAY TOO SMALL FOR PRODUCT'
281 PRINT*,'----------------------------------',N,' ',KBUFL
283 IF(N.LT.NR) GO TO 300
285 C -----------------------------------------------------------------
286 C* 4. EXPAND BUFR MESSAGE.
287 C --------------------
290 CALL BUS012(KBUFL,KBUFF,KSUP,KSEC0,KSEC1,KSEC2,KERR)
292 PRINT*,'ERROR IN BUS012: ',KERR
293 PRINT*,' BUFR MESSAGE NUMBER ',N,' CORRUPTED.'
299 IF(KEL.GT.JELEM) KEL=JELEM
301 CALL BUFREX(KBUFL,KBUFF,KSUP,KSEC0 ,KSEC1,KSEC2 ,KSEC3 ,KSEC4,
302 1 KEL,CNAMES,CUNITS,KVALS,VALUES,CVALS,IERR)
305 IF(IERR.EQ.39) GO TO 300
314 CALL BUSEL(KTDLEN,KTDLST,KTDEXL,KTDEXP,KERR)
315 IF(KERR.NE.0) CALL EXIT(2)
318 c DO 401 IK=1,KSEC3(3)
320 c CALL BUBOX(IK,KSUP,KEL,KTDEXP,CNAMES,CUNITS,KVALS,VALUES,
321 c 1 KBOX,KAPP,KLEN,KBOXR,VALS,CBOXN,CBOXU,IERR)
326 c PRINT*,'bubox error----------------------------------',N
329 C* 4.1 PRINT CONTENT OF EXPANDED DATA.
330 C -------------------------------
333 IF(.NOT.OPRT) GO TO 500
334 IF(.NOT.OSEC3) GO TO 450
336 C* 4.2 PRINT SECTION ZERO OF BUFR MESSAGE.
337 C -----------------------------------
343 C* 4.3 PRINT SECTION ONE OF BUFR MESSAGE.
344 C -----------------------------------
350 C* 4.4 PRINT SECTION TWO OF BUFR MESSAGE.
351 C -----------------------------------
354 C AT ECMWF SECTION 2 CONTAINS RDB KEY.
357 CALL BUUKEY(KSEC1,KSEC2,KEY,KSUP,KERR)
361 CALL BUPRS2(KSUP ,KEY)
363 C* 4.5 PRINT SECTION 3 OF BUFR MESSAGE.
364 C -----------------------------------
367 C FIRST GET DATA DESCRIPTORS
369 CALL BUSEL(KTDLEN,KTDLST,KTDEXL,KTDEXP,KERR)
370 C IF(KERR.NE.0) CALL EXIT(2)
375 CALL BUPRS3(KSEC3,KTDLEN,KTDLST,KTDEXL,KTDEXP,KEL,CNAMES)
378 C* 4.6 PRINT SECTION 4 (DATA).
379 C -----------------------
382 C IN THE CASE OF MANY SUBSETS DEFINE RANGE OF SUBSETS
385 WRITE(*,'(A,$)') ' STARTING SUBSET TO BE PRINTED : '
386 READ(*,'(BN,I6)') IST
387 WRITE(*,'(A,$)') ' ENDING SUBSET TO BE PRINTED : '
388 READ(*,'(BN,I6)') IEND
396 C IF(KSEC1(6).EQ.11) THEN
398 CALL BUPRT(ICODE,IST,IEND,KEL,CNAMES,CUNITS,CVALS,
399 1 KVALS,VALUES,KSUP,KSEC1,IERR)
404 C IF(IEND.GT.KSEC3(3)) IEND=KSEC3(3)
408 C CALL BUBOX(IK,KSUP,KEL,KTDEXP,CNAMES,CUNITS,KVALS,VALUES,
409 C 1 KBOX,KAPP,KLEN,KBOXR,VALS,CBOXN,CBOXU,IERR)
410 C IF(IERR.NE.0) CALL EXIT(2)
412 C CALL BUPRTBOX(KBOX,KAPP,KLEN,KBOXR,VALS,CBOXN,CBOXU)
417 C -----------------------------------------------------------------
418 C* 5. COLLECT DATA FOR REPACKING.
419 C ---------------------------
423 IF(.NOT.OENC) GO TO 300
425 C FIRST GET DATA DESCRIPTORS
427 CALL BUSEL(KTDLEN,KTDLST,KTDEXL,KTDEXP,KERR)
428 IF(KERR.NE.0) CALL EXIT(2)
431 IN=I+(NPACK-1)*KSUP(5)
433 IF(CUNITS(I).EQ.'CCITTIA5') THEN
434 IPOS =VALUES(I)/1000.
435 ICH=NINT(VALUES(I)-IPOS*1000)
437 VALUE(IN)=KKK*1000+ICH
438 CVAL(KKK)=CVALS(IPOS)
444 IF(NPACK.EQ.NCOM) GO TO 600
447 C -----------------------------------------------------------------
448 C* 6. PACK BUFR MESSAGE BACK INTO BUFR.
449 C ---------------------------------
458 KSEC0(3)=4 ! EDITION 4 OF BUFR MESSAGE
459 KSEC3(4)=128 ! NO COMPRESSION
460 IF(OCOMP) KSEC3(4)=192 ! COMPRESSION
463 C GET REPLICATION FACTORS
467 IF(KTDEXP(K).EQ.31001.OR.KTDEXP(K).EQ.31002) THEN
469 KDATA(KK)=NINT(VALUE(K))
475 IF(KSEC1(5).NE.0) THEN
476 CALL BUUKEY(KSEC1,KSEC2,KEY,KSUP,KERR)
478 PRINT*,'BUUKEY: ERROR ',KERR
483 C GET INFORMATION FOR RDB KEY
485 CALL BUCRKEY(KSUP(5),KTDEXP,KSUP,KSEC1,KSEC3,KEY,
488 PRINT*,'ERROR IN BUCREKEY.'
496 CALL BUPKEY(KEY,KSEC1,KSEC2,KERR)
497 IF(KERR.NE.0) CALL EXIT(2)
501 IF(KSUP(6).EQ.0) THEN
502 PRINT*,'ZERO SUBSETS'
507 C* 6.2 ENCODE DATA INTO BUFR MESSAGE.
508 C ------------------------------
511 CALL BUFREN( KSEC0,KSEC1,KSEC2,KSEC3,KSEC4,
512 1 KTDLEN,KTDLST,KDLEN,KDATA,KSUP(5), ! KEL
513 2 KVALS,VALUE,CVAL,KBUFL,KBUFR,KERR)
516 PRINT*,'ERROR IS ',KERR
517 PRINT*,'ERROR DURING ENCODING.'
521 C 6.3 WRITE PACKED BUFR MESSAGE INTO FILE.
522 C ------------------------------------
529 CALL PBWRITE(IUNIT1,KBUFR,ILEN,IERR)
531 PRINT*,'ERROR WRITING INTO TARGET FILE.'
534 PRINT*,'RECORD WRITTEN INTO FILE- ILEN=',ilen
539 C -----------------------------------------------------------------
544 CALL PBCLOSE(IUNIT1,IRET)