C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSFCINIT    INITIALIZE MADIS SFC DATA
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-14
C
C ABSTRACT: READ FILES TO GET PARAMETERS SPECIFIYING WHAT WE NEED TO
C   KNOW TO READ NETCDF VARIABLES AND PROCESS THEM FOR SFC DATASETS,
C   AND FOR ANY OTHER INFO NEEDED ONE TIME FOR SFC PROCESSING.
C
C PROGRAM HISTORY LOG:
C   99-09-14  M. BARTH    ORIGINAL VERSION
C   99-10-28  M. BARTH    ADDED INITIALIZATION OF VARIABLES IN
C                         MADISCOM (HAD BEEN DONE WITH PROBLEMATIC
C                         BLOCK DATA SECTION)
C   00-03-23  M. BARTH    CHANGED DEFAULT TIME WINDOW SETTINGS.
C                         (NOT THE MEANING, JUST THE CODE NUMBERS).
C   01-02-12  M. BARTH    ADDED A PRECIP ACCUM DURATION FIELD TO
C                         SUBSRCIN INPUT LINES.  ADDED PACCUM_CODE
C                         AND SUBLEN TO MADISCOM.
C   01-06-27  M. BARTH    RESTRUCTURED SUBSRCIN TO COVER THESE
C                         NEW CAPABILITIES:
C                         - SUBPROVIDERS
C                         - SOLAR RADIATION TYPE CODE
C                         - CODE TO INDICATE WHETHER PROVIDER OR
C                           SUBPROVIDER PRECIP ACCUM & SOLAR RAD
C                           CODES SHOULD BE HONORED
C   01-06-29  M. BARTH    REMOVED PSOS AND GSOS DATASETS
C   01-07-24  M. BARTH    SPLIT PROVIDER INFO INTO SFC & HYDRO
C   03-05-27  M. BARTH    - CHANGED FORMAT STATEMENT FOR READING STADIC
C                         - INITIALIZE DATA STRUCTURES THAT TRACK NEW
C                           PROVIDERS
C   04-03-22  M. BARTH    ADDED COOP DATASET
C   06-09-01  L. BENJAMIN ADDED URBANET DATASET
C   08-08-16  L. BENJAMIN REMOVED M_OFMOLD CALL
C   08-06-23  M. BARTH    ADDED HIGH FREQUENCY METAR DATASET
C   09-07-23  L. BENJAMIN ADDED NEPP, HCN, AND CRN AS A SFC DATASETS.
C
C USAGE:    CALL MSFCINIT (SFCTBL,STADIC,SUBSRCIN,TOPMINI,STATUS)
C
C   INPUT ARGUMENT LIST:
C
C     SFCTBL   - C       FILENAME OF THE MADIS SFC TABLE
C     STADIC   - C       FILENAME OF THE ASOS STATION DICTIONARY
C     SUBSRCIN - C       FILENAME OF THE SFC SUBSRC DEFINITIONS
C     TOPMINI  - C       FILENAME OF THE GRIDDED MINIMUM TOPOGRAPHY
C
C   OUTPUT ARGUMENT LIST:
C     STATUS   - I       SUCCESS_P OR FAILURE CODE
C
C   INPUT FILES:
C     SFCTBL   - FILE SPECIFYING THE VARIABLES IN THE MADIS NETCDF FILES
C     STADIC   - LISTS ASOS STATIONS BY REGION
C     SUBSRCIN - LISTS "SUBSRCS", OR SUBSETS, OF THE DATA (E.G., ASOS,
C                LOCAL MESONET PROVIDERS)
C     TOPMINI  - GRIDDED MINIMUM ELEVATION USED FOR CREATING MARITIME
C                STATION ELEVATIONS IN THE GREAT LAKES WHEN THEY AREN'T
C                AVAILABLE IN THE OB RECORDS
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, MSFCTBL, M_GETLUN, M_LSTCHR, M_OFMOLD
C
C REMARKS:
C   - LIMITED ERROR CHECKING IS DONE ON THE TABLE, AS IT'S MEANT TO
C     BE STATIC AND CREATED AT CODE DEVELOPMENT TIME.
C   - THE MADISCOM PROVIDER ARRAYS (NPVD, PVDNAME, ETC.) HAVE SFC
C     PROVIDERS IN ROW 1, HYDRO PROVIDERS IN ROW 2, MAP PROVIDERS
C     IN ROW 3.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MSFCINIT(SFCTBL,STADIC,SUBSRCIN,TOPMINI,STATUS)

C     INCLUDES AND COMMONS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'MADISCON'
      INCLUDE 'MADISCOM'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*(*) SFCTBL,STADIC,SUBSRCIN,TOPMINI
      INTEGER       STATUS

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER       LUN,I,J,IFS0,ITMP,JTMP,M_LSTCHR,CODE(3),IOS
      CHARACTER     REG_VEC*1,PSTYP*1,NAME*11
      LOGICAL       ERRFLG,EXISTS

C     ---------------------------------------------------------------
C     INITIALIZE MADISCOM VARIABLES.
C     ---------------------------------------------------------------
      DO 1200 I = 1, MAX_SFCDSET_P
         SFCDSET_ENBL(I) = .TRUE.
 1200 CONTINUE

      DO 1201 I = 1, MAX_SUBSRC_P
         PROVDR_ENBL(I,1) = .TRUE.
 1201 CONTINUE

      NNEWPVD(1) = 0
      ALL_ENBL(1) = .TRUE.

      SFCDSET_NAME(1) = 'METAR    '
      SFCDSET_NAME(2) = 'MARITIME '
      SFCDSET_NAME(3) = 'SAO      '
      SFCDSET_NAME(4) = 'MESONET  '
      SFCDSET_NAME(5) = 'COOP     '
      SFCDSET_NAME(6) = 'UrbaNet  '
      SFCDSET_NAME(7) = 'HFMETAR  '
      SFCDSET_NAME(8) = 'NEPP     '
      SFCDSET_NAME(9) = 'HCN      '
      SFCDSET_NAME(10) = 'CRN      '

      LAST_SFC_ATIME = '         '

      TOTAL_STNS = 0
      DMTYPE = -1
      QCTYPE = 0

      MINBCK = 0
      MINFWD = 0
      RECWIN = 0

      SFC_INIT = INIT_P

C     ------------------------------------------------------------------
C     READ IN THE MADIS SFC TABLE (DEFINES VARIABLES AND DATASETS).
C     MSFCTBL WILL REPORT ANY ERRORS AND SET STATUS.
C     ------------------------------------------------------------------
      CALL MSFCTBL(SFCTBL,STATUS)
      IF(STATUS.NE.SUCCESS_P)RETURN

C     ------------------------------------------------------------------
C     READ IN THE ASOS STATION DICTIONARY FOR SUBSRC IDENTIFICATION.
C     ------------------------------------------------------------------
      CALL M_GETLUN(LUN, 1)
      ERRFLG = .FALSE.
      INQUIRE(FILE=STADIC, EXIST=EXISTS)
      IF (EXISTS) THEN
        OPEN(UNIT=   LUN,
     +       FILE=   STADIC,
     +       STATUS='OLD',
     +       FORM=  'FORMATTED',
     +       IOSTAT= IOS )

        IF (IOS .GT. 0)  THEN
          ERRFLG = .TRUE.
        ENDIF

      ELSE
        ERRFLG = .TRUE.
      ENDIF


      IF(ERRFLG)THEN
         CALL MERROR('MSFCINIT: BIF - OPEN ERROR - STADIC')
         CLOSE (LUN,IOSTAT=IFS0)
         CALL M_GETLUN(LUN, 2)
         STATUS = BAD_FILE_P
         RETURN
      ELSE
         DO 2000 I = 1, 2000
            READ (LUN, 5153, IOSTAT = IFS0) STN_VEC(I), REG_VEC
            IF (IFS0 .LT. 0) GOTO 2001
 5153       FORMAT (A5, 1X, A1)
            TOTAL_STNS = TOTAL_STNS + 1
 2000    CONTINUE
 2001    CONTINUE
         CLOSE (LUN,IOSTAT=IFS0)
         CALL M_GETLUN(LUN, 2)
      ENDIF

C     ------------------------------------------------------------------
C     READ IN THE SUBSRCIN FILE LISTING THE SUBSRC'S (PROVIDERS),
C     SUBPROVIDERS, AND VARIOUS CODES INDICATING HOW TO PROCESS SOME
C     THINGS THAT ARE HANDLED ON A PROVIDER AND/OR SUBPROVIDER BASIS.
C     ------------------------------------------------------------------
      CALL M_GETLUN(LUN, 1)
      SUBSRCIN_NAME(1) = SUBSRCIN

      ERRFLG = .FALSE.
      INQUIRE(FILE=SUBSRCIN_NAME(1), EXIST=EXISTS)
      IF (EXISTS) THEN
        OPEN(UNIT=   LUN,
     +       FILE=   SUBSRCIN_NAME(1),
     +       STATUS='OLD',
     +       FORM=  'FORMATTED',
     +       IOSTAT= IOS )

        IF (IOS .GT. 0)  THEN
          ERRFLG = .TRUE.
        ENDIF

      ELSE
        ERRFLG = .TRUE.
      ENDIF

      IF (ERRFLG) THEN

         CALL MERROR('MSFCINIT: BIF - OPEN ERROR - SUBSRCIN')
         CALL M_GETLUN(LUN, 2)
         STATUS = BAD_FILE_P
         RETURN

      ENDIF


C     ---------------------------------------------------------------
C     NOW READ IN THE SUBSOURCES
C     ---------------------------------------------------------------
      NPVD(1) = 0
      NSUB(1) = 0

      DO 1195 J = 1, MAX_SUBSRC_P + MAX_SUBPVDR_P
         READ (LUN, 5225, END=1196) PSTYP,NAME,CODE
 5225    FORMAT (1X,A,5X,A,1X,I2,3X,I2,3X,I2)
         IF(PSTYP.EQ.'P')THEN
            NPVD(1) = NPVD(1) + 1
            PVDNAME(NPVD(1),1) = NAME(1:10)
            PVDLEN(NPVD(1),1) = M_LSTCHR(PVDNAME(NPVD(1),1),10)
            PACCUM_P_CODE(NPVD(1),1) = CODE(1)
            SRAD_P_CODE(NPVD(1),1) = CODE(2)
            SUB_CODE(NPVD(1),1) = CODE(3)
         ELSE IF(PSTYP.EQ.'S')THEN
            NSUB(1) = NSUB(1) + 1
            SUBNAME(NSUB(1),1) = NAME(1:11)
            SUBLEN(NSUB(1),1) = M_LSTCHR(SUBNAME(NSUB(1),1),11)
            PACCUM_S_CODE(NSUB(1),1) = CODE(1)
            SRAD_S_CODE(NSUB(1),1) = CODE(2)
         ENDIF
 1195 CONTINUE
 1196 OLD_NPVD(1) = NPVD(1)
      OLD_NSUB(1) = NSUB(1)
      CLOSE (LUN)
      CALL M_GETLUN(LUN, 2)


C     ---------------------------------------------------------------
C     READ IN THE GRIDDED MINIMIUM TOPOGRAPHY.
C     ---------------------------------------------------------------
      CALL M_GETLUN(LUN, 1)
      OPEN(UNIT=LUN,FILE=TOPMINI,STATUS='OLD',IOSTAT=IFS0)
      IF(IFS0.NE.0)THEN
         CALL MERROR('MSFCINIT: BIF - OPEN ERROR - TOPMINI')
         STATUS = BAD_FILE_P
         CLOSE(LUN,IOSTAT=IFS0)
         CALL M_GETLUN(LUN, 2)
         RETURN
      ENDIF
      DO 1197 J = 1, NY_TOPO_P
         DO 1198 I = 1, NX_TOPO_P
            READ(LUN,*,IOSTAT=IFS0)ITMP,JTMP,GE(I,J)
            IF(IFS0.NE.0)THEN
               CALL MERROR('MSFCINIT: BIF - READ ERROR - TOPMINI')
               STATUS = BAD_FILE_P
               CLOSE(LUN,IOSTAT=IFS0)
               CALL M_GETLUN(LUN, 2)
               RETURN
            ENDIF
 1198    CONTINUE
 1197 CONTINUE
      CLOSE(UNIT=LUN)
      CALL M_GETLUN(LUN, 2)

      STATUS = SUCCESS_P
      RETURN
      END
C**---------------------------------------------------------------------
C**REQUIRED STANDARD FSL DISCLAIMER (NOVEMBER 2000)
C**---------------------------------------------------------------------
C**  OPEN SOURCE LICENSE/DISCLAIMER,  FORECAST SYSTEMS LABORATORY
C**  NOAA/OAR/FSL, 325 BROADWAY BOULDER, CO 80305
C**
C**  THIS SOFTWARE IS DISTRIBUTED UNDER THE OPEN SOURCE DEFINITION, 
C**  WHICH MAY BE FOUND AT http://www.opensource.org/osd.html.
C**
C**  IN PARTICULAR, REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
C**  WITH OR WITHOUT MODIFICATION, ARE PERMITTED PROVIDED THAT THE 
C**  FOLLOWING CONDITIONS ARE MET:
C**
C**  - REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THIS NOTICE, THIS LIST
C**    OF CONDITIONS AND THE FOLLOWING DISCLAIMER.
C**
C**  - REDISTRIBUTIONS IN BINARY FORM MUST PROVIDE ACCESS TO THIS 
C**    NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER, AND
C**    THE UNDERLYING SOURCE CODE.
C**
C**  - ALL MODIFICATIONS TO THIS SOFTWARE MUST BE CLEARLY DOCUMENTED, 
C**    AND ARE SOLELY THE RESPONSIBILITY OF THE AGENT MAKING THE 
C**    MODIFICATIONS.
C**
C**  - IF SIGNIFICANT MODIFICATIONS OR ENHANCEMENTS ARE MADE TO THIS
C**    SOFTWARE, THE FSL SOFTWARE POLICY MANAGER 
C**    (softwaremgr.fsl@noaa.gov) BE NOTIFIED.
C**
C**  THIS SOFTWARE AND ITS DOCUMENTATION ARE IN THE PUBLIC DOMAIN AND 
C**  ARE FURNISHED "AS IS."  THE AUTHORS, THE UNITED STATES GOVERNMENT,
C**  ITS INSTRUMENTALITIES, OFFICERS, EMPLOYEES, AND AGENTS MAKE NO 
C**  WARRANTY, EXPRESS OR IMPLIED, AS TO THE USEFULNESS OF THE SOFTWARE
C**  AND DOCUMENTATION FOR ANY PURPOSE.  THEY ASSUME NO RESPONSIBILITY 
C**  (1) FOR THE USE OF THE SOFTWARE AND DOCUMENTATION; OR (2) TO 
C**  PROVIDE TECHNICAL SUPPORT TO USERS.
C**---------------------------------------------------------------------
