C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MGETSFC     GET SFC OBS & QC INFO FOR ONE VARIABLE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-16
C
C ABSTRACT: READ SFC OBS & QC INFO FOR ALL STATIONS FOR ONE VARIABLE
C   AND TIME.
C
C PROGRAM HISTORY LOG:
C   99-09-16  M. BARTH    ORIGINAL VERSION
C   00-03-22  M. BARTH    CHANGES TO ALLOW FOR UPPER-AIR-POINT DATA
C   01-02-08  M. BARTH    - CHANGES FOR NEW GPSMET VARIABLES
C                         - ADDED SPECIAL CASE ROUTINE FOR SOME
C                           OF THE PRECIP OBS, INCLUDING FIXING AN
C                           ERROR WHERE MESONET PRECIP RATE UNITS WERE
C                           A FACTOR OF 1000 TOO LOW
C   01-04-24  M. BARTH    ADD 24H PCP ACCUM TO VARS HANDLED BY MSFCPCP
C   01-05-03  M. BARTH    - CHANGED MRDNUM/MRDCHR TO MGTNUM/MGTCHR
C                         - ADDED DUMMY ARGS TO MQCFLTR CALL
C   01-06-27  M. BARTH    ADDED MSFCSRAD CALL.
C   01-06-29  M. BARTH    - REMOVED PSOS AND GSOS DATASETS.
C                         - MOVED MQCFLTR CALL INSIDE THE DO 100 LOOP
C                           SO IT CAN HANDLE EACH SFC DATASET
C                           INDIVIDUALLY
C                         - FIXED LOGIC ERROR THAT CAUSED FILES TO
C                           BE PROCESSED MULTIPLE TIMES GIVEN SOME
C                           TIME WINDOW CHOICES
C                         - ADDED Q AND DPD MOISTURE VARIABLES
C                         - ADDED MSFCPRS, MSFCTV, MSFCELEV ROUTINES
C                         - CHANGED DIMENSION OF LOCAL VARIABLES TO
C                           ACCOMMODATE SKY COVER LAYER BASE 
C                           (MXSFC_P*6), AND FIXED LOGIC PROBLEM THAT
C                           DIDN'T COVER THE FACT THAT THIS VARIABLE
C                           HAS MORE THAN 1 LEVEL
C                         - CHANGE FROM CALLER-SPECIFIED VARIABLE CODE
C                           NUMBER TO VARIABLE CODE NAME
C                         - ADDED MSFCRDLFT ROUTINE
C   01-02-01  M. BARTH    FIXED BUG WHERE PRECIP TYPE & INTENSITY
C                         SHOULD HAVE BEEN A SPECIAL CASE WITH AN
C                         TWO ELEMENTS PER RECORD (SIMILAR TO SKY COVER
C                         LAYER BASE), BUT HAD BEEN TREATED AS A NORMAL
C                         ONE-ELEMENT-PER-RECORD VARIABLE.
C   02-05-28  M. BARTH    FIXED A BUG WHERE 3/6/12/18 H MESONET
C                         PRECIP ACCUM WEREN'T BEING PROCESSED
C   02-05-29  M. BARTH    RESTRUCTURED TEMPORARY MEMORY ARRAY USAGE
C   03-07-03  M. BARTH    ADDED ABSOLUTE HUMIDITY VARIABLE
C   03-09-12  M. BARTH    ADDED SEVERAL ADDITIONAL SOLAR RADIATION 
C                         VARIABLES.
C   03-09-25  M. BARTH    SET MADISCOM CDTYPE VARIABLE FOR THIS DATA
C                         TYPE -- NO HARM WAS CAUSED BY NOT DOING THIS,
C                         BUT THIS NEEDS TO BE CONSISTENT ACROSS DATA
C                         TYPES TO ALLOW FULL FLEXIBILITY WHEN ADDING
C                         FEATURES.
C   04-03-22  M. BARTH    - ADDED COOP DATASET, WITH SEVERAL MORE 
C                           SPECIAL-CASE VARIABLES.
C                         - FIXED A BUG WHERE SAO 3/6H PRECIP WOULD
C                           HAVE BEEN CONVERTED FROM MM TO M TWICE,
C                           ONCE IN MSFCPCP THEN AGAIN HERE.  HOWEVER,
C                           I DON'T THINK THERE EVER HAVE BEEN ANY SAO
C                           3/6H PRECIP OBS, SO DON'T BOTHER PUTTING
C                           THIS INTO THE RELEASE NOTES.
C   04-06-30  M. BARTH    ADDED PCPTOTL_VN_P FLAVOR OF PRECIP.
C   04-10-16  L. BENJAMIN ADDED NO SUCH VARIABLE CHECK FOR QCA CHECKS
C   05-04-12  L. BENJAMIN ADDED CHECK FOR VARIABLE NOT IN DATABASE.  
C                         CHANGED THIS TO NON-FATAL CASE IF ANY DATA
C                         IS FOUND.
C   05-06-01  L. BENJAMIN ADDED WVMR.
C   05-06-14  M. BARTH    ADDED AN ADDITIONAL SOLAR RADIATION VARIABLE.
C   06-07-12  L. BENJAMIN ADDED CHECK FOR GUST AND HOURLY GUST FOR COOP 
C                         DATA.  OLD VERSION HAVE HOURLY GUST, STARTING
C                         WITH 2.0 ALL WIND GUST ARE 5 MIN DATA AND USE
C                         THE GENERIC WIND GUST VARIABLE.
C   06-10-12  L. BENJAMIN ADDED URBANET 15M CALULATED MOI VARIABLES
C   07-05-16  L. BENJAMIN ADDED PHOTOSYNTHETICALLY ACTIVE RADIATION
C   07-10-17  M. BARTH    ADDED PCP5M_VN_P AND PCP15M_VN_P PRECIP ACCUMS
C   07-10-17  L. BENJAMIN CHANGED NOBS COUNT FOR STATION WITH LEVELS.  
C                         CHANGED TO ONE COUNT PER STATION PER RECORD, 
C                         NO MATTER HOW MANY LEVELS.  ALSO CHECK ALL 
C                         STATIONS AND LEVELS FOR DATA.
C   08-06-23  M. BARTH    ADDED HIGH FREQUENCY METAR DATASET, WHICH
C                         HAS A DIFFERENT NUMBER OF MAX SKY COVER
C                         LEVELS THAN THE OTHER DATASETS WITH THIS
C                         VARIABLE.
C   08-06-26  L. BENJAMIN ADDED NV VARIABLES FOR MISSING INIT.
C   08-07-25  L. BENJAMIN SEPARATED THE OUTPUT ARRAY NUMBER FROM 
C                         THE DATA SETS ARRAY INDEX.  REMOVED THE 
C                         MISSING VALUE INIT ABOVE.
C   09-08-19  L. BENJAMIN ADDED ARCHIVE TEMPERATURE CHECK
C
C USAGE:    CALL MGETSFC (ATIME,VCODE,NSTA,NOBS,OB,QCDESC,QCAPP,QCRES,
C                         STATUS)
C
C   INPUT ARGUMENT LIST:
C     ATIME    - C       YYJJJHHMM OF THE DATA TO BE READ
C     VCODE    - C       VARIABLE CODE NAME
C
C   OUTPUT ARGUMENT LIST:
C     NSTA     - I       NUMBER OF STATIONS RETURNED
C     NOBS     - I       NUMBER OF NON-MISSING OBS (<= NSTA)
C     OB       - RA      OBSERVATIONS IN M/K/S UNITS
C     QCDESC   - CA      QC DATA DESCRIPTORS
C     QCAPP    - IA      QC APPLIED WORD (SHOWS CHECKS APPLIED)
C     QCRES    - IA      QC RESULTS WORD (GOOD/BAD FOR EACH CHECK)
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C
C   INPUT FILES:
C     MADIS SFC NETCDF FILES FOR ATIME
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, MSFCMOI, MSFCWIND, MSFCPCHG, MSFCPCP, MSFCELEV,
C                MGTNUM, MGTCHR, MQCFLTR, MSFCSRAD, MSFCPRS, MSFCTV,
C                MGETVCN, MSFCRDLFT, MSFCSNOW
C
C REMARKS:
C   - THE CALLER MUST CALL MINIT AND MSFCSTA BEFORE CALLING THIS
C     ROUTINE.  MSFCSTA SHOULD BE CALLED FIRST WHENEVER ACCESS TO
C     A NEW TIME IS DESIRED.  IT STORES INFO IN THE STRCCOM COMMON
C     THAT IS USED BY THIS ROUTINE TO FIGURE OUT WHICH RECORDS ARE
C     NEEDED ON NETCDF FILES OPENED BY MSFCSTA.
C   - DOUBLE & CHARACTER DATA TYPES ARE NOT HANDLED BY THIS ROUTINE.
C   - THE CALLER SHOULD DIMENSION ARRAYS TO MATCH MXSFC_P IN
C     madiscon.f.
C   - SEE madistmp.f FOR AN EXPLANATION OF HOW TEMPORARY STORAGE
C     IS HANDLED (ALL ARRAYS NAMED *TMP*).
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MGETSFC(ATIME,VCODE,NSTA,NOBS,OB,QCDESC,QCAPP,QCRES,
     1                   STATUS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      CHARACTER*9 ATIME
      CHARACTER*(*) VCODE
      INTEGER NSTA,NOBS,QCAPP(*),QCRES(*),STATUS
      REAL OB(*)
      CHARACTER*1 QCDESC(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I,J,K,FNUM,DSN,LEN1REC,DUMMY,NV,VCN,NVOB
      REAL OBTMP(MXSFC_P*6),QCATMP(MXSFC_P*6),QCRTMP(MXSFC_P*6),FACTOR
      REAL OBTMP2(1)
      CHARACTER*1 QCDTMP(MXSFC_P*6),VCT
      LOGICAL PROC_FILE(SFC_MXSTRF)
      INTEGER LAST_BEST_STATUS,TOT,STANTOT,LASTTOT,CNT

C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------
      EQUIVALENCE(OBTMP,TMP_L1_N1)
      EQUIVALENCE(QCATMP,TMP_L1_N2)
      EQUIVALENCE(QCRTMP,TMP_L1_N3)
      EQUIVALENCE(QCDTMP,TMP_L1_C1)

C     ------------------------------------------------------------------
C     INITIALIZE STATIONS WITH DATA TO 0
C     ------------------------------------------------------------------
      STANTOT = 0
C     ------------------------------------------------------------------
C     IF THE SFC SUBSYSTEM HASN'T BEEN INITIALIZED, OR IF THE REQUESTED
C     TIME DOESN'T MATCH THE TIME LAST PROCESSED BY MSFCSTA, OR IF THE
C     VCODE IS CLEARLY ILLEGAL, RETURN WITH ERROR.
C     ------------------------------------------------------------------
      IF(SFC_INIT.NE.INIT_P)THEN
         STATUS = SELSUB_NOTINIT_P
         CALL MERROR('MGETSFC: SELECTED SUBSYSTEM NOT INITIALIZED - '//
     1               'SFC ')
         GO TO 900
      ELSE IF(ATIME.NE.LAST_SFC_ATIME.OR.ATIME.EQ.'         ')THEN
         STATUS = TIME_NOTINIT_P
         CALL MERROR('MGETSFC: TIME NOT INITIALIZED BY MSFCSTA')
         GO TO 900
      ELSE 
         CALL MGETVCN(VCODE,VCN,VCT,STATUS)
         IF(STATUS.NE.SUCCESS_P)GO TO 900
      ENDIF

C     ------------------------------------------------------------------
C     SET THE CURRENT DATA TYPE.
C     ------------------------------------------------------------------
      CDTYPE = SFC_DS_P

C     ------------------------------------------------------------------
C     SET THE NUMBER OF LEVELS FOR THE "SFC" VARIABLE...
C     NOTE THAT WE USED TO SET NV=6 FOR SKYCOVLB_VN_P HERE, BUT NOW
C     THIS IS A DATASET-SPECIFIC VALUE, SO THAT'S BEEN MOVED TO THE
C     "EXTRACT DESIRED RECORDS" LOGIC BELOW.
C     ------------------------------------------------------------------
      IF(VCN.EQ.PCPINT_VN_P.OR.VCN.EQ.PCPTYPE_VN_P)THEN
         NV = 2
      ELSE
         NV = 1
      ENDIF
C     ------------------------------------------------------------------
C     INIT OUTPUT ARRAY DIMENSION.
C     ------------------------------------------------------------------
      IF(VCN.EQ.SKYCOVLB_VN_P)THEN
         NVOB = 6
      ELSE
         NVOB = NV
      ENDIF


C     ------------------------------------------------------------------
C     FILL IN MISSING FLAGS FOR ALL POSSIBLE RECORDS IN CALLER'S ARRAY,
C     INDICATE THAT WE HAVEN'T PROCESSED ANY FILES YET.
C     THEN LOOP THROUGH THE FILES THAT ARE NEEDED AND PUT IN REAL DATA
C     WHERE AVAILABLE.
C     ------------------------------------------------------------------
      NSTA = SFC_LNUM
      IF(NSTA.EQ.0)THEN
         STATUS = NO_DATA_P
         GO TO 900
      ENDIF

      FNUM = 0
C     ------------------------------------------------------------------
C     NVOB IS THE OUTPUT ARRAY INDEX, A LITTLE OVERKILL FOR OBTMP, BUT
C     NO HARM NO FOUL.
C     ------------------------------------------------------------------

      DO 1 I = 1, NSTA * NVOB
         OB(I) = MISS_P
         OBTMP(I) = MISS_P
         QCDESC(I) = 'Z'
         QCAPP(I) = 0
         QCRES(I) = 0
 1    CONTINUE

      DO 2 I = 1, SFC_MXSTRF
         PROC_FILE(I) = .FALSE.
 2    CONTINUE

      LAST_BEST_STATUS = -999
      TOT = 0
      LASTTOT = TOT
C     ------------------------------------------------------------------
C     NOTE THAT WE LOOP THROUGH SFC_FNUM TO SEE WHICH FILES ARE NEEDED
C     AND NOT THROUGH SFC_NFILES.  THIS IS DONE AS JUST BECAUSE A FILE
C     GOT READ DOESN'T MEAN ANY OF ITS RECORDS ACTUALLY WERE EXTRACTED
C     WHEN ALL THE FILTERING WAS DONE IN MSFCSTA.  THEREFORE, WE'LL
C     ONLY PROCESS EACH FILE WITH RECORDS IN USE.
C     ------------------------------------------------------------------
      DO 100 I = 1, NSTA

C     ------------------------------------------------------------------
C     ALREADY HANDLED THIS FILE.
C     ------------------------------------------------------------------
         IF(PROC_FILE(SFC_FNUM(I)))GO TO 100

         FNUM = SFC_FNUM(I)
         PROC_FILE(FNUM) = .TRUE.
         DSN = SFC_SOURCE(FNUM)

C     ------------------------------------------------------------------
C     SET DATASET-SPECIFIC VALUE FOR SKYCOVLB_VN_P
C     THSI IS FOR THE TMP ARRAYS INDEX, NOT THE OUTPUT ARRAY
C     ------------------------------------------------------------------
         IF(VCN.EQ.SKYCOVLB_VN_P)THEN
            IF(DSN.EQ.HFMETAR_DS_P)THEN
               NV = 3
            ELSE IF(DSN.EQ.SAO_DS_P)THEN
               NV = 5
            ELSE
               NV = 6
            ENDIF
         ENDIF

C     ------------------------------------------------------------------
C     THE VARIABLE'S NOT ASSOCIATED WITH THIS SOURCE DATASET.
C     ------------------------------------------------------------------
         IF(VLEN(VCN,DSN).EQ.0)GO TO 100

C     ------------------------------------------------------------------
C     THE VARIABLE'S DOUBLE OR CHARACTER DATA TYPE:  THIS WILL BE
C     TRUE FOR ANY DATASET FOR WHICH THIS VARIABLE APPLIES, AND IS
C     AN ILLEGAL OPTION.
C     ------------------------------------------------------------------
         IF(VDTYPE(VCN,DSN).EQ.'d'.OR.VDTYPE(VCN,DSN).EQ.'c')THEN
            STATUS = INV_DTYPE_P
            CALL MERROR('MGETSFC: INVALID VARIABLE DATA TYPE')
            GO TO 900
         ENDIF

C     ------------------------------------------------------------------
C     IF THE VARIABLE NEEDS TO BE CREATED FROM OTHER VARIABLES, CALL
C     A SUBROUTINE TO FILL IN THE LOCAL ARRAYS.  OTHERWISE, READ THE
C     VARIABLE, AND IF AVAILABLE, QC INFO.  IF THERE'S AN ERROR,
C     THE SUBROUTINES WILL HAVE SET STATUS & CALLED MERROR.
C     ------------------------------------------------------------------

         IF(VCN.EQ.SFCTD_VN_P.OR.VCN.EQ.SFCRH_VN_P.OR.
     1      VCN.EQ.SFCQ_VN_P.OR.VCN.EQ.SFCDPD_VN_P.OR.
     2      VCN.EQ.SFCAH_VN_P.OR.VCN.EQ.TD1H_VN_P.OR.
     3      VCN.EQ.RH1H_VN_P.OR.VCN.EQ.Q1H_VN_P.OR.
     4      VCN.EQ.DPD1H_VN_P.OR.VCN.EQ.AH1H_VN_P.OR.
     5      VCN.EQ.SFCMR_VN_P.OR.
     6      VCN.EQ.TD15M_VN_P.OR.VCN.EQ.Q15M_VN_P.OR.
     1      VCN.EQ.DPD15M_VN_P.OR.VCN.EQ.AH15M_VN_P.OR.
     2      VCN.EQ.WVMR15M_VN_P)THEN

            CALL MSFCMOI(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,
     1                   STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.SFCU_VN_P.OR.VCN.EQ.SFCV_VN_P.OR.
     1           VCN.EQ.SFCFF_VN_P.OR.VCN.EQ.SFCDD_VN_P.OR.
     2           VCN.EQ.U1H_VN_P.OR.VCN.EQ.V1H_VN_P.OR.
     3           VCN.EQ.FF1H_VN_P.OR.VCN.EQ.DD1H_VN_P.OR.
     4           VCN.EQ.U24H_VN_P.OR.VCN.EQ.V24H_VN_P.OR.
     5           VCN.EQ.FF24H_VN_P.OR.VCN.EQ.DD24H_VN_P.OR.
     5           VCN.EQ.FFGUST_VN_P.OR.VCN.EQ.DDGUST_VN_P.OR.
     6           VCN.EQ.FFMAX1H_VN_P.OR.VCN.EQ.DDMAX1H_VN_P)THEN

            
            CALL MSFCWIND(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,
     1                    STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.PCHNG_VN_P)THEN

            CALL MSFCPCHG(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,
     1                    STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.PCP1_VN_P.OR.VCN.EQ.PCP3_VN_P.OR.
     1           VCN.EQ.PCP6_VN_P.OR.VCN.EQ.PCP12_VN_P.OR.
     2           VCN.EQ.PCP18_VN_P.OR.VCN.EQ.PCP24_VN_P.OR.
     3           VCN.EQ.PCPRATE_VN_P.OR.VCN.EQ.PCPLCL_VN_P.OR.
     4           VCN.EQ.PCPUTC_VN_P.OR.VCN.EQ.PCPCDAY_VN_P.OR.
     5           VCN.EQ.PCPUDAY_VN_P.OR.VCN.EQ.PCPTOTL_VN_P.OR.
     6           VCN.EQ.PCP5M_VN_P.OR.VCN.EQ.PCP15M_VN_P.OR.
     7           VCN.EQ.RAWPATB_VN_P.OR.VCN.EQ.ARCHPCP_VN_P
     8           )THEN



            CALL MSFCPCP(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,
     1                   STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.SRAD1_VN_P.OR.VCN.EQ.SRAD2_VN_P.OR.
     1           VCN.EQ.SRAD3_VN_P.OR.VCN.EQ.SRAD4_VN_P.OR.
     2           VCN.EQ.SRAD5_VN_P.OR.VCN.EQ.SRAD6_VN_P.OR.
     3           VCN.EQ.SRAD7_VN_P.OR.VCN.EQ.SRAD8_VN_P.OR.
     4           VCN.EQ.SRAD9_VN_P.OR.VCN.EQ.SRAD10_VN_P.OR.
     5           VCN.EQ.SRAD11_VN_P.OR.VCN.EQ.SRAD12_VN_P.OR.
     6           VCN.EQ.SRAD13_VN_P.OR.VCN.EQ.SRAD14_VN_P.OR.
     7           VCN.EQ.SRAD15_VN_P.OR.VCN.EQ.SRAD16_VN_P.OR.
     8           VCN.EQ.SRAD0_VN_P)THEN

            CALL MSFCSRAD(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,
     1                    STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.ALT_VN_P.OR.VCN.EQ.SFCP_VN_P.OR.
     1           VCN.EQ.P15M_VN_P)THEN

            CALL MSFCPRS(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,
     1                   STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.SFCTV_VN_P.OR.VCN.EQ.TV1H_VN_P.OR.
     1           VCN.EQ.TV15M_VN_P)THEN

            CALL MSFCTV(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.ELEV_VN_P)THEN

            CALL MSFCELEV(DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.RDLFT1_VN_P.OR.VCN.EQ.RDLFT2_VN_P.OR.
     1           VCN.EQ.RDLFT3_VN_P.OR.VCN.EQ.RDLFT4_VN_P)THEN 

            CALL MSFCRDLFT(VCN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.SNOWC_VN_P.AND.DSN.EQ.COOP_DS_P)THEN

            CALL MSFCSNOW(VCN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE IF(VCN.EQ.SFCT_VN_P.AND.
     1           (DSN.EQ.HCN_DS_P .OR. DSN.EQ.CRN_DS_P))THEN


            CALL MSFCART(VCN,DSN,FNUM,OBTMP,QCDTMP,QCATMP,QCRTMP,STATUS)

            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               GO TO 100
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

         ELSE

            CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN)),SFC_NCID(FNUM),
     1                  SFC_RECDIM(FNUM),1,SFC_NRECS(FNUM),MISS_P,
     2                  OBTMP,STATUS)
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
               LAST_BEST_STATUS = STATUS
            ENDIF

C     ASSUME QC OK, SET FOR DIFFERNET CONDITIONS
            QCSTATUS = EQCOK_P

            IF(VQC(VCN,DSN).EQ.'Q')THEN

               CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'QCA',
     1                     SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                     SFC_NRECS(FNUM),MISS_P,QCATMP,STATUS)


               IF(STATUS.EQ.VAR_NOTINDB_P)THEN
                  QCSTATUS = EQCFAILED_P
                  STATUS = SUCCESS_P
                  DO J = 1, SFC_NRECS(FNUM) * NV
                  
                     QCDTMP(J) = 'Z'
                     QCATMP(J) = 0
                     QCRTMP(J) = 0
                  ENDDO
               ELSE IF(STATUS.NE.SUCCESS_P)THEN
                  GO TO 900
               ELSE

                  CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'QCR',
     1                     SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                     SFC_NRECS(FNUM),MISS_P,QCRTMP,STATUS)
                  IF(STATUS.NE.SUCCESS_P)THEN
                     GO TO 900
                  ENDIF
                  CALL MGTCHR(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'DD',
     1                     SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                     SFC_NRECS(FNUM),QCDTMP,LEN1REC,STATUS)
                  IF(STATUS.NE.SUCCESS_P)THEN
                     GO TO 900
                  ENDIF
               ENDIF
            ELSE
               QCSTATUS = EQCFAILED_P

               DO 10 J = 1, SFC_NRECS(FNUM) * NV

                  QCDTMP(J) = 'Z'
                  QCATMP(J) = 0
                  QCRTMP(J) = 0

 10            CONTINUE

            ENDIF

C     ------------------------------------------------------------------
C     FIX A PROBLEM WITH MISSING AND FILL VALUE ATTRIBUTES IN THE
C     GPSMET VARIABLES.
C     ------------------------------------------------------------------
            IF(DSN.EQ.MESONET_DS_P.AND.(VCN.EQ.GPWV_VN_P.OR.
     1         VCN.EQ.GTSD_VN_P.OR.VCN.EQ.GDSD_VN_P.OR.
     2         VCN.EQ.GWSD_VN_P.OR.VCN.EQ.GMWT_VN_P.OR.
     3         VCN.EQ.GFE_VN_P.OR.VCN.EQ.GCPI_VN_P))THEN

               DO 11 J = 1, SFC_NRECS(FNUM) * NV
                  IF(OBTMP(J).GT.1.E30.OR.OBTMP(J).EQ.-9999.0)
     1               OBTMP(J) = MISS_P
 11            CONTINUE

            ENDIF

C     ------------------------------------------------------------------
C     DO ANY NECESSARY UNITS CONVERSIONS TO RETURN M/K/S UNITS.
C     - PRESSURES FROM MB TO PA
C     - VISIBILITY FROM KM TO M
C     - GPSMET PWV AND FORMAL ERROR FROM CM TO M
C     - COOP SNOWFALL FROM MM TO M
C     ------------------------------------------------------------------
            FACTOR = 0.0
            IF(DSN.EQ.SAO_DS_P)THEN
               IF(VCN.EQ.SLP_VN_P)THEN
                  FACTOR = 100.0
               ELSE IF(VCN.EQ.VIS_VN_P)THEN
                  FACTOR = 1000.0
               ENDIF
            ELSE IF(DSN.EQ.MESONET_DS_P.AND.(VCN.EQ.GPWV_VN_P.OR.
     1              VCN.EQ.GFE_VN_P))THEN
               FACTOR = 0.01
            ELSE IF(DSN.EQ.COOP_DS_P.AND.(VCN.EQ.SNOW6H_VN_P.OR.
     1              VCN.EQ.SNOW24H_VN_P))THEN
               FACTOR = 0.001
            ENDIF

            IF(FACTOR.NE.0.0)THEN

               DO 110 J = 1, SFC_NRECS(FNUM) * NV
                  IF(OBTMP(J).NE.MISS_P)OBTMP(J) = OBTMP(J) * FACTOR
 110           CONTINUE

            ENDIF


            IF(I.EQ.1.AND.LAST_BEST_STATUS.EQ.-999)THEN
               LAST_BEST_STATUS = STATUS
            ENDIF


         ENDIF

C     ------------------------------------------------------------------
C     DO ANY REQUESTED QC FILTERING.  NOTE THAT THIS IS DONE INSIDE THE
C     DO 100 LOOP SO THAT MQCFLTR CAN HANDLE EACH SFC DATASET
C     INDIVIDUALLY.  THIS IS NEEDED TO COVER THE CASE OF QC INFO NOT
C     BEING AVAILABLE FOR ALL SFC DATASETS, BUT IS AVAILABLE FOR SOME.
C     ------------------------------------------------------------------
         CALL MQCFLTR(VCN,DSN,NV,SFC_NRECS(FNUM),QCDTMP,DUMMY,DUMMY,
     1                OBTMP)

C     ------------------------------------------------------------------
C     EXTRACT DESIRED RECORDS INTO CALLER'S ARRAY.
C     ------------------------------------------------------------------

         DO 120 J = I, NSTA

            IF(SFC_FNUM(J).NE.FNUM)GO TO 120

            DO 130 K = 1, NV

               OB((J-1)*NVOB+K) = OBTMP((SFC_RNUM(J)-1)*NV+K)
               QCDESC((J-1)*NVOB+K) = QCDTMP((SFC_RNUM(J)-1)*NV+K)
               QCAPP((J-1)*NVOB+K) = QCATMP((SFC_RNUM(J)-1)*NV+K)
               QCRES((J-1)*NVOB+K) = QCRTMP((SFC_RNUM(J)-1)*NV+K)
               IF(OB((J-1)*NVOB+K).NE.MISS_P)TOT = TOT + 1
C     ------------------------------------------------------------------
C     RE-INIT TO MISSING
C     ------------------------------------------------------------------
               OBTMP((SFC_RNUM(J)-1)*NV+K) = MISS_P

 130        CONTINUE
C     ------------------------------------------------------------------
C     COUNT UP THE NUMBER OF NON-MISSING STATION PERIODS ACTUALLY 
C     RETURNED.
C     ------------------------------------------------------------------
            IF(LASTTOT.NE.TOT)THEN
               LASTTOT = TOT
               STANTOT = STANTOT + 1
            ENDIF

 120     CONTINUE

 100  CONTINUE


      IF(TOT.EQ.0)THEN
         STATUS = VAR_NOTINDB_P
      ELSE
         STATUS = LAST_BEST_STATUS
      ENDIF

 900  IF(STATUS.NE.SUCCESS_P)THEN

         NSTA = 0
         NOBS = 0

      ELSE
         NOBS = 0
C     ------------------------------------------------------------------
C     VERIFY ONE LAST TIME THAT WE DO HAVE NON-MISSING OBS BEING 
C     RETURNED.
C     ------------------------------------------------------------------
         CNT = 0

         DO 121 J = 1, NSTA*NVOB
            IF(OB(J).NE.MISS_P)CNT = CNT + 1
 121     CONTINUE

         IF(CNT.EQ.0)THEN
            NOBS = 0
            NSTA = 0
            STATUS = NO_DATA_P
         ELSE
            NOBS = STANTOT
         ENDIF

      ENDIF

      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**---------------------------------------------------------------------
