SUBROUTINE CP_DCOD ( cldt, coopfl, bufrtb, nhours, iret ) C************************************************************************ C* CP_DCOD * C* * C* This routine decodes COOP data files from FSL into BUFR format. * C* * C* CP_DCOD ( CLDT, COOPFL, BUFRTB, NHOURS, IRET ) * C* * C* Input parameters: * C* CLDT CHAR* Date-time from command line * C* COOPFL CHAR* COOP data file from FSL * C* BUFRTB CHAR* BUFR tables file * C* NHOURS INTEGER Max # of hours before run time * C* for creating BUFR output * C* * C* Output parameters: * C* IRET INTEGER Return code: * C* 0 = normal return * C* * C** * C* Log: * C* J. Ator/NCEP 07/06 * C* S. Guan/NCEP 11/09 To deal with NEPP and HCN data * C* J. Ator/NCEP 11/12 Add error return for unknown type and * C* improve log message * C* J. Ator/NCEP 01/19 Modify in response to call sequence * C* change for MSFCSTA in MADIS 4.3 * C************************************************************************ INCLUDE 'GEMPRM.PRM' INCLUDE 'BRIDGE.PRM' INCLUDE 'cpcmn.cmn' C* CHARACTER*(*) cldt, coopfl, bufrtb C* CHARACTER rundt*12, sysdt*12, + coopdn*(DCMXLN), coopbn*(DCMXLN), + stid(MXSTNS)*10, + prvid(MXSTNS)*10, + crpttm(MXSTNS)*9, cstrtm*9, + qcd(MXSTNS,NVAR)*1, + vcname(NVAR)*7, + rimnem(NRIMN)*8, cimnem(NCIMN)*8, + ccopbn*18, + coopdn2*(DCMXLN), coopbn2*(DCMXLN) C* REAL slat ( MXSTNS ), slon ( MXSTNS ), + selv ( MXSTNS ), obs ( MXSTNS, NVAR ), + slin ( MXSOL ), snhr ( MXSNW ) C* INTEGER irundt (5), irptdt (5), + istnm ( MXSTNS ), isrcn ( MXSTNS ), + iqca ( MXSTNS, NVAR ), iqcr ( MXSTNS, NVAR ), + isolm ( MXSOL ), isolt ( MXSOL ), + idofs ( MXSNW ) C* INCLUDE 'ERMISS.FNC' C* DATA slin + / 2., 4., 8., 20., 40. / C* DATA snhr + / 6., 24. / C* C* The following variable code names define, in accordance C* with the MADIS table files "static/sfctbl.txt" and C* "static/sfcvcn.txt", the variables that will be read C* from the COOP data file. C* DATA vcname + / 'T ', 'TD ', 'DD ', 'FF ', + 'PLATTYP', 'PCP1H ', + 'SOILM2 ', 'SOILM4 ', 'SOILM8 ', 'SOILM20', + 'SOILM40', + 'SOILT2 ', 'SOILT4 ', 'SOILT8 ', 'SOILT20', + 'SOILT40', + 'SNOWC ', 'SNOW6H ', 'SNOW24H' / C* C* Indices (into vcname) of soil moisture variable code names. C* DATA isolm + / 7, 8, 9, 10, 11 / C* C* Indices (into vcname) of soil temperature variable code names. C* DATA isolt + / 12, 13, 14, 15, 16 / C* C* Indices (into vcname) of snow amount variable code names. C* DATA idofs + / 18, 19 / C*----------------------------------------------------------------------- iret = 0 C C* Extract the basename from the COOP data file. C CALL FL_PATH ( coopfl, coopdn, coopbn, ierpth ) C C* Save the COOP data file basename in a separate variable C* for later use. C ccopbn = coopbn(1:18) C C* Extract the directory just before the basename to figure out whether data C* is NEPP or HCN C CALL FL_PATH ( coopdn, coopdn2, coopbn2, ierpth ) C C* The MADIS (Meteorological Assimilation Data Ingest System) C* software from FSL will be used to read the COOP data file. C* Initialize this software. C CALL MINIT ( 'SFC', 'FSL', .false., ierfin ) IF ( ierfin .ne. 0 ) THEN CALL UT_EMSG ( 0, 'MINIT', ierfin ) RETURN END IF C CALL MSETSFCPVDR ( 'ALL-SFC', .false., ierssp1 ) IF ( ierssp1 .ne. 0 ) THEN CALL UT_EMSG ( 0, 'MSETSFCPVDR', ierssp1 ) RETURN END IF C IF ( coopbn2(1:3) .eq. 'hcn' ) THEN CALL MSETSFCPVDR ( 'HCN', .true., ierssp2 ) ELSE IF ( coopbn2(1:4) .eq. 'nepp' ) THEN CALL MSETSFCPVDR ( 'NEPP', .true., ierssp2 ) ELSE logmsg = 'unknown file type ' // coopbn2 CALL DC_WLOG ( 0, 'DC', 2, logmsg, ierwlg ) RETURN END IF C IF ( ierssp2 .ne. 0 ) THEN CALL UT_EMSG ( 0, 'MSETSFCPVDR', ierssp2 ) RETURN END IF C C* Write the filename and type to the decoder log. C logmsg = 'COOP ' // coopbn2(1:4) // ' filename: ' // coopbn CALL DC_WLOG ( 0, 'DC', 2, logmsg, ierwlg ) C C* Using the COOP data file basename, compute the MADIS date-time. C READ ( UNIT = coopbn, FMT = '( I4.4, 2I2.2, 1X, 2I2.2 )', + IOSTAT = ier ) + ifyear, ifmnth, ifdays, ifhour, ifminu IF ( ier .ne. 0 ) THEN CALL UT_EMSG ( 0, 'READ', ier ) RETURN END IF CALL MCHRTIM ( ifyear, ifmnth, ifdays, ifhour, ifminu, + cstrtm, ierfct ) IF ( ierfct .ne. 0 ) THEN CALL UT_EMSG ( 0, 'MCHRTIM', ierfct ) RETURN END IF C C* Retrieve all of the stations for which there is data in the C* COOP data file. C CALL MSFCSTA ( cstrtm, nstns, stid, istnm, slat, slon, selv, + crpttm, prvid, ierfst, isrcn ) IF ( ierfst .ne. 0 ) THEN IF ( ierfst .eq. 1006 ) THEN logmsg = 'contained no reports' CALL DC_WLOG ( 0, 'DC', 2, logmsg, ierwlg ) ELSE CALL UT_EMSG ( 0, 'MSFCSTA', ierfst ) END IF RETURN END IF C C* Now, retrieve the rest of the data for these stations. C DO ii = 1, NVAR CALL MGETSFC ( cstrtm, vcname ( ii ), itmp, nnmsg, + obs ( 1, ii ), qcd ( 1, ii ), + iqca ( 1, ii ), iqcr ( 1, ii ), ierfgs ) IF ( ( ierfgs .ne. 0 ) .and. + ( ierfgs .ne. 1006 ) .and. + ( ierfgs .ne. 1005 ) ) THEN CALL UT_EMSG ( 0, 'MGETSFC', ierfgs ) RETURN END IF END DO C C* Set the pointers for the interface arrays. C CALL CP_IFSP ( rimnem, cimnem, ierfsp ) IF ( ierfsp .ne. 0 ) THEN RETURN ENDIF C C* Open the BUFR tables file. C CALL FL_SOPN ( bufrtb, iunbft, ierspn ) IF ( ierspn .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', ierspn, bufrtb, ierwlg ) RETURN END IF C C* Open the BUFR output file. C CALL FL_GLUN ( iunbfo, iergln ) IF ( iergln .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', iergln, ' ', ierwlg ) RETURN END IF C C* Connect the BUFR tables and output files to the BUFR interface. C CALL OPENBF ( iunbfo, 'NUL', iunbft ) C C* Close the BUFR tables file. C CALL FL_CLOS ( iunbft, iercls ) IF ( iercls .ne. 0 ) THEN CALL DC_WLOG ( 0, 'FL', iercls, ' ', ierwlg ) END IF 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 ) RETURN END IF C C* If a date-time was entered on the command line, then use it as C* the run date-time. Otherwise, use the system time as the run C* 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 ) RETURN END IF END IF 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 ) RETURN END IF C C* Write the number of reports to the decoder log. C WRITE ( UNIT = logmsg, FMT = '( A, I6, A )' ) + 'contained ', nstns, ' reports' CALL DC_WLOG ( 0, 'DC', 2, logmsg, ierwlg ) C C* Loop on each report. C DO ii = 1, nstns C C* Start an entry for this report in the decoder log. C logmsg = '--------------------' CALL DC_WLOG ( 3, 'DC', 2, logmsg, ierwlg ) C C* Initialize the interface values arrays. C CALL CP_IFIV ( ierfiv ) C C* Compute and store the date-time. C CALL MINTTIM ( crpttm ( ii ), ifyear, ifmnth, ifdays, + ifhour, ifminu, ierftm ) IF ( ierftm .ne. 0 ) THEN CALL UT_EMSG ( 2, 'MINTTIM', ierftm ) ELSE rivals ( iryear ) = FLOAT ( 1900 + ifyear ) rivals ( irmnth ) = FLOAT ( ifmnth ) rivals ( irdays ) = FLOAT ( ifdays ) rivals ( irhour ) = FLOAT ( ifhour ) rivals ( irminu ) = FLOAT ( ifminu ) END IF C C* Station and provider IDs. C civals ( icstid ) = stid ( ii )(1:8) civals ( icprvid ) = prvid ( ii )(1:8) C C* Latitude, longitude, and elevation. C rivals ( irslat ) = UT_MDRI ( slat ( ii ) ) rivals ( irslon ) = UT_MDRI ( slon ( ii ) ) rivals ( irselv ) = UT_MDRI ( selv ( ii ) ) C C* Temperature (and associated QC values). C CALL UT_MDQI ( ii, 1, obs, qcd, iqca, iqcr, MXSTNS, + rivals ( irtmpk ), civals ( ictmpkqd ), + rivals ( irtmpkqa ), rivals ( irtmpkqr ), ierf ) C C* Dewpoint temperature (and associated QC values). C CALL UT_MDQI ( ii, 2, obs, qcd, iqca, iqcr, MXSTNS, + rivals ( irdwpk ), civals ( icdwpkqd ), + rivals ( irdwpkqa ), rivals ( irdwpkqr ), ierf ) C C* Wind direction (and associated QC values). C CALL UT_MDQI ( ii, 3, obs, qcd, iqca, iqcr, MXSTNS, + rivals ( irdrct ), civals ( icdrctqd ), + rivals ( irdrctqa ), rivals ( irdrctqr ), ierf ) C C* Wind speed (and associated QC values). C CALL UT_MDQI ( ii, 4, obs, qcd, iqca, iqcr, MXSTNS, + rivals ( irsped ), civals ( icspedqd ), + rivals ( irspedqa ), rivals ( irspedqr ), ierf ) C C* Platform type. C rivals ( irpltp ) = UT_MDRI ( obs ( ii, 5 ) ) C C* 1-hour precipitation (and associated QC values). C CALL UT_MDQI ( ii, 6, obs, qcd, iqca, iqcr, MXSTNS, + rivals ( irpc1h ), civals ( icpc1hqd ), + rivals ( irpc1hqa ), rivals ( irpc1hqr ), ierf ) C C* Total snow cover (and associated QC values). C CALL UT_MDQI ( ii, 17, obs, qcd, iqca, iqcr, MXSTNS, + rivals ( irtosd ), civals ( ictosdqd ), + rivals ( irtosdqa ), rivals ( irtosdqr ), ierf ) C C* Soil moistures/temperatures (and associated QC values). C kk = 0 DO jj = 1, MXSOL CALL UT_MDQI ( ii, isolm ( jj ), + obs, qcd, iqca, iqcr, MXSTNS, + solm, + civals ( icsolmqd ( kk + 1 ) ), + rivals ( irsolmqa ( kk + 1 ) ), + rivals ( irsolmqr ( kk + 1 ) ), + ierf ) CALL UT_MDQI ( ii, isolt ( jj ), + obs, qcd, iqca, iqcr, MXSTNS, + solt, + civals ( icsoltqd ( kk + 1 ) ), + rivals ( irsoltqa ( kk + 1 ) ), + rivals ( irsoltqr ( kk + 1 ) ), + ierf ) IF ( ( .not. ERMISS ( solm ) ) .or. + ( .not. ERMISS ( solt ) ) ) THEN rivals ( irslin ( kk + 1 ) ) = slin ( jj ) rivals ( irsolm ( kk + 1 ) ) = solm rivals ( irsolt ( kk + 1 ) ) = solt kk = kk + 1 END IF END DO rivals ( irnsol ) = kk C C* Snowfall amounts (and associated QC values). C kk = 0 DO jj = 1, MXSNW CALL UT_MDQI ( ii, idofs ( jj ), + obs, qcd, iqca, iqcr, MXSTNS, + dofs, + civals ( icdofsqd ( kk + 1 ) ), + rivals ( irdofsqa ( kk + 1 ) ), + rivals ( irdofsqr ( kk + 1 ) ), + ierf ) IF ( .not. ERMISS ( dofs ) ) THEN rivals ( irsnhr ( kk + 1 ) ) = snhr ( jj ) rivals ( irdofs ( kk + 1 ) ) = dofs kk = kk + 1 END IF END DO rivals ( irnsnw ) = kk C C* Write the interface output to the decoder log. C CALL CP_IFPT ( 3, rimnem, cimnem, ierfpt ) C C* Do not create BUFR output for reports that are more than C* NHOURS before or more than 3 hours after the run time. C IF ( ( ERMISS ( rivals ( iryear ) ) ) .or. + ( ERMISS ( rivals ( irmnth ) ) ) .or. + ( ERMISS ( rivals ( irdays ) ) ) .or. + ( ERMISS ( rivals ( irhour ) ) ) .or. + ( ERMISS ( rivals ( irminu ) ) ) ) THEN iertmk = -1 ELSE irptdt (1) = INT ( rivals ( iryear ) ) irptdt (2) = INT ( rivals ( irmnth ) ) irptdt (3) = INT ( rivals ( irdays ) ) irptdt (4) = INT ( rivals ( irhour ) ) irptdt (5) = INT ( rivals ( irminu ) ) CALL DC_TMCK ( 2, irundt, irptdt, nhours, 180, + iertmk ) END IF IF ( iertmk .eq. 0 ) THEN C C* Convert interface-format data for this report C* into BUFR output and then write the BUFR output C* to the BUFR output stream. C CALL CP_BUFR ( iunbfo, irundt, ccopbn, ierbfr ) END IF C END DO C C* Make sure that all BUFR output has been written before exiting. C CALL UT_WBFR ( iunbfo, 'coop', 1, ierwbf ) C CALL CLOSBF ( iunbfo ) CALL FL_CLAL ( iercal ) C* RETURN END