SUBROUTINE MF_DCOD ( cldt, bufrtn, nhours, iret ) C************************************************************************ C* MF_DCOD * C* * C* This routine decodes bulletins containing WMO-migrated marine * C* subsurface BUFR data into NCEP BUFR format. * C* * C* MF_DCOD ( CLDT, BUFRTN, NHOURS, IRET ) * C* * C* Input parameters: * C* CLDT CHAR* Date-time from command line * C* BUFRTN CHAR* NCEP BUFR tables file * C* NHOURS INTEGER Max # of hours before run time * C* for creating NCEP BUFR output * C* * C* Output parameters: * C* IRET INTEGER Return code: * C* 0 = normal return * C* * C** * C* Log: * C* J. Ator/NCEP 01/14 * C************************************************************************ INCLUDE 'GEMPRM.PRM' INCLUDE 'BRIDGE.PRM' C* CHARACTER*(*) cldt, bufrtn C* C* Maximum number of descriptors within Section 3 of a C* WMO marine surbsurface BUFR message. C* PARAMETER ( MXLV = 6000 ) C* PARAMETER ( MXMN = 10 ) C* CHARACTER bull*(DCMXBF), cbull*(DCMXBF), bfstyp*8, + seqnum*8, buhd*8, cborg*8, bulldt*8, bbb*8, + rundt*12, sysdt*12, subtyp*8, logmsg*200, + opmm*20, opms*32 C* INTEGER irundt ( 5 ), irptdt ( 5 ), + ibull ( DCMXBF / 4 ) C* LOGICAL bullok, msgok C* REAL*8 r8in ( MXMN, MXLV ), r8wk ( MXMN, MXLV ), + GETBMISS, r8bfms C* EQUIVALENCE ( cbull (1:4), ibull (1) ) C* INCLUDE 'ERMISS.FNC' C*----------------------------------------------------------------------- iret = 0 CALL UT_PRVS ( 'DECOD_DCMSSF v3.0.0' ) C C* Open the tables file for the NCEP BUFR (i.e. output) stream. C CALL FL_SOPN ( bufrtn, iubftn, ierspn ) IF ( ierspn .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', ierspn, bufrtn, ierwlg ) RETURN END IF C C* Open the BUFR messages file. C CALL FL_GLUN ( iubfma, iergln ) IF ( iergln .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', iergln, ' ', ierwlg ) RETURN END IF OPEN ( UNIT = iubfma, FILE = '.dummy/dcmssf', + FORM = 'UNFORMATTED' ) CALL OPENBF ( iubfma, 'SEC3', iubftn ) C C* Open the messages file for the NCEP BUFR (i.e. output) stream. C CALL FL_GLUN ( iubfmn, iergln ) IF ( iergln .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', iergln, ' ', ierwlg ) RETURN END IF C C* Connect the tables and messages files for the C* NCEP BUFR (i.e. output) stream. C CALL OPENBF ( iubfmn, 'NUL', iubftn ) r8bfms = GETBMISS() C C* Specify that NCEP BUFR (i.e. output) messages are to be C* encoded using edition 4 and up to 100K bytes in size. C CALL PKVS01 ( 'BEN', 4 ) CALL MAXOUT ( 100000 ) CALL PKVS01 ( 'MTV', 20 ) C C* Close the tables file for the NCEP BUFR (i.e. output) stream. C CALL FL_CLOS ( iubftn, iercls ) IF ( iercls .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', iercls, ' ', ierwlg ) END IF C DO WHILE ( .true. ) C C* Get a new bulletin from the input pipe. C CALL DC_GBUL ( bull, lenb, ifdtyp, iergbl ) IF ( iergbl .ne. 0 ) THEN C C* A time-out occurred while waiting for a new bulletin C* on the input pipe. Shut down the decoder and exit. C CALL DC_WLOG ( 0, 'DC', iergbl, ' ', ierwlg ) CALL CLOSBF ( iubfma ) CALL CLOSBF ( iubfmn ) CALL FL_CLAL ( iercal ) RETURN END IF C bullok = .true. C C* Decode the header information from this bulletin. C IF ( ifdtyp .eq. 0 ) THEN C C* Decode WMO products. C CALL DC_GHDR ( bull, lenb, seqnum, buhd, cborg, + bulldt, bbb, ibptr, ierghd ) IF ( ierghd .ne. 0 ) THEN CALL DC_WLOG ( 2, 'DC', ierghd, ' ', ierwlg ) bullok = .false. ELSE C C* Start an entry for this bulletin in the decoder log. C logmsg = '########################################' CALL DC_WLOG ( 2, 'DC', 2, logmsg, ierwlg ) logmsg = seqnum // buhd // cborg // bulldt // bbb CALL DC_WLOG ( 2, 'DC', 2, logmsg, ierwlg ) END IF ELSE C C* Do not decode AFOS products. C bullok = .false. END IF C IF ( bullok ) THEN C C* Get the system time. C itype = 1 CALL CSS_GTIM ( itype, sysdt, iergtm ) IF ( iergtm .ne. 0 ) THEN CALL DC_WLOG ( 2, 'SS', iergtm, ' ', ierwlg ) bullok = .false. END IF END IF C IF ( bullok ) THEN C C* If a date-time was entered on the command line, then use it C* as the run date-time. Otherwise, use the system time as C* the run date-time. C IF ( cldt .eq. 'SYSTEM' ) THEN rundt = sysdt ELSE CALL TI_STAN ( cldt, sysdt, rundt, ierstn ) IF ( ierstn .ne. 0 ) THEN CALL DC_WLOG ( 2, 'TI', ierstn, ' ', ierwlg ) bullok = .false. END IF END IF END IF C IF ( bullok ) THEN C C* Convert the run date-time to integer. C CALL TI_CTOI ( rundt, irundt, iercto ) IF ( iercto .ne. 0 ) THEN CALL DC_WLOG ( 2, 'TI', iercto, ' ', ierwlg ) bullok = .false. ELSE nmsg = 0 nrpt = 0 END IF END IF C DO WHILE ( bullok ) C C* Locate the next BUFR message within the bulletin, and C* store it within an equivalenced integer array. C ipt1 = INDEX ( bull (ibptr:lenb), 'BUFR' ) IF ( ipt1 .eq. 0 ) THEN bullok = .false. IF ( nmsg .eq. 0 ) THEN IF ( INDEX ( bull (ibptr:lenb), 'NIL' ) .ne. 0 ) THEN logmsg = 'NIL bulletin' CALL DC_WLOG ( 2, 'DC', 2, logmsg, ierwlg ) END IF ELSE WRITE ( logmsg, FMT = '(A, I3, A, I4, A)' ) + 'contained', nmsg, ' messages and', nrpt, ' reports' CALL DC_WLOG ( 2, 'DC', 2, logmsg, ierwlg ) C C* Make sure that all BUFR output for this bulletin has C* been written to the BUFR output stream before going back C* to DC_GBUL and waiting for a new bulletin on the pipe. C CALL UT_WBFR ( iubfmn, 'mssf', 1, ierwbf ) END IF ELSE istart = ibptr + ipt1 - 1 ibptr = istart + 4 cbull = bull ( istart : lenb ) C C* Ensure the message contains marine subsurface data. C IF ( IUPBS01( ibull, 'MTYP' ) .eq. 31 ) THEN msgok = .true. ELSE msgok = .false. END IF C IF ( msgok ) THEN C C* Open the BUFR message for reading. C CALL READERME ( ibull, iubfma, bfstyp, ibfdt, ierrme ) IF ( ierrme .ne. 0 ) THEN msgok = .false. ELSE nmsg = nmsg + 1 END IF END IF C DO WHILE ( msgok ) C C* Get the next report from this BUFR message. C IF ( IREADSB ( iubfma ) .ne. 0 ) THEN C C* There are no more reports in this message. C msgok = .false. ELSE nrpt = nrpt + 1 C C* Don't create BUFR output for reports that are more C* than NHOURS before or more than 3 hours after the C* run time. C CALL UFBSEQ ( iubfma, r8in, MXMN, MXLV, nlv, + 'YYMMDD' ) rptyr = UT_BMRI ( r8in (1,1) ) rptmo = UT_BMRI ( r8in (2,1) ) rptdy = UT_BMRI ( r8in (3,1) ) CALL UFBSEQ ( iubfma, r8in, MXMN, MXLV, nlv, + 'HHMM' ) rpthr = UT_BMRI ( r8in (1,1) ) rptmi = UT_BMRI ( r8in (2,1) ) IF ( ( ERMISS ( rptyr ) ) .or. + ( ERMISS ( rptmo ) ) .or. + ( ERMISS ( rptdy ) ) .or. + ( ERMISS ( rpthr ) ) .or. + ( ERMISS ( rptmi ) ) ) THEN iertmk = -1 ELSE irptdt (1) = INT ( rptyr ) irptdt (2) = INT ( rptmo ) irptdt (3) = INT ( rptdy ) irptdt (4) = INT ( rpthr ) irptdt (5) = INT ( rptmi ) CALL DC_TMCK ( 2, irundt, irptdt, nhours, 180, + iertmk ) END IF C IF ( iertmk .eq. 0 ) THEN C C* Open a BUFR message for output. C ibfdt = ( irptdt (1) * 1000000 ) + + ( irptdt (2) * 10000 ) + + ( irptdt (3) * 100 ) + irptdt (4) CALL OPENMB ( iubfmn, 'NC031005', ibfdt ) C C* Get and store the date and time. C CALL UFBSEQ ( iubfma, r8in, MXMN, MXLV, nlv, + 'YYMMDD' ) CALL UFBSEQ ( iubfmn, r8in, MXMN, 1, nlv, + 'YYMMDD' ) CALL UFBSEQ ( iubfma, r8in, MXMN, MXLV, nlv, + 'HHMM' ) CALL UFBSEQ ( iubfmn, r8in, MXMN, 1, nlv, + 'HHMM' ) C C* Get and store the latitude and longitude. C CALL UFBSEQ ( iubfma, r8in, MXMN, MXLV, nlv, + 'LTLONH' ) CALL UFBSEQ ( iubfmn, r8in, MXMN, 1, nlv, + 'LTLONH' ) C C* Get the profile level data. C CALL UFBREP ( iubfma, r8in, MXMN, MXLV, nlv, + 'WPRES SSTH SALNH' ) DO ii = 1, nlv r8wk(1,ii) = r8in(1,ii) r8wk(4,ii) = r8in(2,ii) r8wk(7,ii) = r8in(3,ii) END DO C C* Get the quality data. The first iteration of C* values apply to the platform position, while the C* remaining iterations apply to the above profile C* parameters on each profile level. C CALL UFBREP ( iubfma, r8in, MXMN, MXLV, nlv2, + 'QFQF GGQF' ) rqfpos = r8in(1,1) CALL UT_RIBF ( iubfmn, 'QFQF', rqfpos, ier ) rggpos = r8in(2,1) CALL UT_RIBF ( iubfmn, 'GGQF', rggpos, ier ) DO ii = 1, nlv jj = ii*3 r8wk(2,ii) = r8in(1,jj-1) r8wk(3,ii) = r8in(2,jj-1) r8wk(5,ii) = r8in(1,jj) r8wk(6,ii) = r8in(2,jj) r8wk(8,ii) = r8in(1,jj+1) r8wk(9,ii) = r8in(2,jj+1) END DO C C* Store the profile level data. C CALL DRFINI ( iubfmn, nlv, 1, '(GLPFDATA)' ) CALL UFBSEQ ( iubfmn, r8wk, MXMN, nlv, nlv2, + 'GLPFDATA' ) C C* Get the long string values. C CALL READLC ( iubfma, opmm, 'OPMM' ) CALL READLC ( iubfma, opms, 'OPMS' ) C C* Get and store the additional data values. C CALL UT_BFRI ( iubfma, 'WMOP', rval, ier ) CALL UT_RIBF ( iubfmn, 'WMOP', rval, ier ) CALL UT_BFRI ( iubfma, 'BUYTS', rval, ier ) CALL UT_RIBF ( iubfmn, 'BUYTS', rval, ier ) CALL UT_BFRI ( iubfma, 'DCLS', rval, ier ) CALL UT_RIBF ( iubfmn, 'DCLS', rval, ier ) CALL UT_BFRI ( iubfma, 'BUYT', rval, ier ) CALL UT_RIBF ( iubfmn, 'BUYT', rval, ier ) CALL UT_BFRI ( iubfma, 'FCYN', rval, ier ) CALL UT_RIBF ( iubfmn, 'FCYN', rval, ier ) CALL UT_BFRI ( iubfma, 'DIPR', rval, ier ) CALL UT_RIBF ( iubfmn, 'DIPR', rval, ier ) CALL UT_BFRI ( iubfma, 'IWTEMP', rval, ier ) CALL UT_RIBF ( iubfmn, 'IWTEMP', rval, ier ) C C* Store the bulletin header. C CALL UT_CIBF ( iubfmn, 'SEQNUM', seqnum, 8, ier ) CALL UT_CIBF ( iubfmn, 'BUHD', buhd, 8, ier ) CALL UT_CIBF ( iubfmn, 'BORG', cborg, 8, ier ) CALL UT_CIBF ( iubfmn, 'BULTIM', bulldt, 8, ier ) CALL UT_CIBF ( iubfmn, 'BBB', bbb, 8, ier ) C C* Store the receipt time. C CALL UT_RIBF ( iubfmn, 'RCTS', FLOAT (0), ier ) CALL UT_RIBF ( iubfmn, 'RCYR', + FLOAT ( irundt (1) ), ier ) CALL UT_RIBF ( iubfmn, 'RCMO', + FLOAT ( irundt (2) ), ier ) CALL UT_RIBF ( iubfmn, 'RCDY', + FLOAT ( irundt (3) ), ier ) CALL UT_RIBF ( iubfmn, 'RCHR', + FLOAT ( irundt (4) ), ier ) CALL UT_RIBF ( iubfmn, 'RCMI', + FLOAT ( irundt (5) ), ier ) C C* Store the correction indicator. C IF ( ( IUPBS01 ( ibull, 'USN' ) .gt. 0 ) .or. + ( bbb(1:1) .eq. 'C' ) ) THEN icorn = 1 ELSE icorn = 0 END IF CALL UT_RIBF ( iubfmn, 'CORN', FLOAT (icorn), ier ) C C* Write the BUFR output to the BUFR output stream. C CALL UT_WBFR ( iubfmn, 'mssf', 0, ierwbf ) C C* Write each long string value to the BUFR output C* stream, unless it consists of all blank spaces, C* and in which case just let it default to "missing". C CALL ST_LDSP ( opmm, logmsg, nchr, ierlds ) IF ( nchr .ne. 0 ) + CALL WRITLC ( iubfmn, opmm, 'OPMM' ) CALL ST_LDSP ( opms, logmsg, nchr, ierlds ) IF ( nchr .ne. 0 ) + CALL WRITLC ( iubfmn, opms, 'OPMS' ) END IF END IF END DO END IF END DO END DO C* RETURN END