C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSFART      GET SFC TEMPERATURE VARIABLE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 01-07-23
C
C ABSTRACT: GET SFC TEMPERATURE OBS WHERE THERE IS A REPROCESSED
C   VALUE TO CHOOSE FROM.
C
C PROGRAM HISTORY LOG:
C   09-07-28  L. BENJAMIN ORIGINAL VERSION
C
C USAGE:    CALL MSFCART (VCN,DSN,FNUM,OB,QCDESC,QCAPP,QCRES,STATUS)
C
C   INPUT ARGUMENT LIST:
C     VCN      - I       VARIABLE CODE NUMBER FOR T
C     DSN      - I       DATASET CODE NUMBER
C     FNUM     - I       FILE NUMBER (INDEX IN COMMON) OF NETCDF FILE
C
C   OUTPUT ARGUMENT LIST:
C     OB       - RA      OBSERVATIONS IN M/K/S UNITS
C     QCDESC   - CA      QC DATA DESCRIPTORS
C     QCAPP    - RA      QC APPLIED WORD (SHOWS CHECKS APPLIED)
C     QCRES    - RA      QC RESULTS WORD (GOOD/BAD FOR EACH CHECK)
C     STATUS   - I       SUCCESS_P OR ERROR CODE
C
C   INPUT FILES:
C     NETCDF FILE OPENED USING DATA STRUCTURES FOR FNUM IN STRCCOM
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    
C
C REMARKS:
C   - THE REPROCESSED VALUE IS USED IF IT EXISTS.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MSFCART (VCN,DSN,FNUM,OB,QCDESC,QCAPP,QCRES,STATUS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER VCN,DSN,FNUM,STATUS
      REAL OB(*),QCAPP(*),QCRES(*)
      CHARACTER*1 QCDESC(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I,LEN1REC,DUMMY,SAVE_QCTYPE
      REAL OBTMP(MXSFC_P),QCATMP(MXSFC_P),QCRTMP(MXSFC_P)
      CHARACTER*1 QCDTMP(MXSFC_P)
      INTEGER TEMPSTATUS
      LOGICAL HAVE_DATA
      CHARACTER*18 TVNAME
C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------
      EQUIVALENCE(OBTMP,TMP_L4_N1)
      EQUIVALENCE(QCATMP,TMP_L4_N2)
      EQUIVALENCE(QCRTMP,TMP_L4_N3)
      EQUIVALENCE(QCDTMP,TMP_L4_C1)


C     ------------------------------------------------------------------
C     INIT
C     ------------------------------------------------------------------
      HAVE_DATA = .FALSE.

C     ------------------------------------------------------------------
C     GET THE TEMPERATURE AND ITS QC INFO.
C     ------------------------------------------------------------------

      CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN)),SFC_NCID(FNUM),
     1     SFC_RECDIM(FNUM),1,SFC_NRECS(FNUM),MISS_P,OB,
     2     STATUS)
      IF(STATUS.NE.SUCCESS_P)GO TO 1000
         
      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,QCAPP,STATUS)
         
         IF(STATUS.EQ.VAR_NOTINDB_P)THEN
            HAVE_DATA = .TRUE.
            STATUS = SUCCESS_P
            DO I = 1, SFC_NRECS(FNUM)
               QCAPP(I) = 0
               QCRES(I) = 0
               QCDESC(I) = 'Z'
            ENDDO
            GO TO 1000
         ELSE IF(STATUS.NE.SUCCESS_P)THEN
            GO TO 1000
         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,QCRES,STATUS)
            IF(STATUS.NE.SUCCESS_P)GO TO 1000
            
            CALL MGTCHR(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'DD',
     1           SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2           SFC_NRECS(FNUM),QCDESC,LEN1REC,STATUS)
            IF(STATUS.NE.SUCCESS_P)GO TO 1000
            HAVE_DATA = .TRUE.
         ENDIF
      ELSE
         DO 1 I = 1, SFC_NRECS(FNUM)
            QCAPP(I) = 0
            QCRES(I) = 0
            QCDESC(I) = 'Z'
 1       CONTINUE
            
      ENDIF
 1000 CONTINUE
      TEMPSTATUS = STATUS
C     ------------------------------------------------------------------
C     SPECIAL CASE PROCESSING
C     ------------------------------------------------------------------
      IF(DSN.EQ.HCN_DS_P .OR. DSN.EQ.CRN_DS_P)THEN
         TVNAME = 'archiveTemperature'
         TLEN = 18

         IF(.NOT.HAVE_DATA)THEN
C     ------------------------------------------------------------------
C     GET THE ARCHIVE TEMPERATURE AND ITS QC INFO.
C     ------------------------------------------------------------------
            CALL MGTNUM(TVNAME(1:18),SFC_NCID(FNUM),
     1           SFC_RECDIM(FNUM),1,SFC_NRECS(FNUM),MISS_P,OB,
     2           STATUS)
            IF(STATUS.NE.SUCCESS_P)RETURN
         
            IF(VQC(VCN,DSN).EQ.'Q')THEN
            
               CALL MGTNUM(TVNAME(1:18)//'QCA',
     1              SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2              SFC_NRECS(FNUM),MISS_P,QCAPP,STATUS)
         
               IF(STATUS.EQ.VAR_NOTINDB_P)THEN
                  HAVE_DATA = .TRUE.
                  STATUS = SUCCESS_P
                  DO I = 1, SFC_NRECS(FNUM)
                     QCAPP(I) = 0
                     QCRES(I) = 0
                     QCDESC(I) = 'Z'
                  ENDDO
               ELSE IF(STATUS.NE.SUCCESS_P)THEN
                  RETURN
               ELSE
                  CALL MGTNUM(TVNAME(1:18)//'QCR',
     1                 SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                 SFC_NRECS(FNUM),MISS_P,QCRES,STATUS)
                  IF(STATUS.NE.SUCCESS_P)RETURN
                  
                  CALL MGTCHR(TVNAME(1:18)//'DD',
     1                 SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                 SFC_NRECS(FNUM),QCDESC,LEN1REC,STATUS)
                  IF(STATUS.NE.SUCCESS_P)RETURN
                  HAVE_DATA = .TRUE.
               ENDIF
            ELSE
               DO 2 I = 1, SFC_NRECS(FNUM)
                  QCAPP(I) = 0
                  QCRES(I) = 0
                  QCDESC(I) = 'Z'
 2             CONTINUE
            ENDIF

         ELSE
C     ------------------------------------------------------------------
C     GET THE ARCHIVE TEMPERATURE AND ITS QC INFO.
C     ------------------------------------------------------------------
            CALL MGTNUM(TVNAME(1:18),SFC_NCID(FNUM),
     1           SFC_RECDIM(FNUM),1,SFC_NRECS(FNUM),MISS_P,OBTMP,
     2           STATUS)
            IF(STATUS.NE.SUCCESS_P)GO TO 2000
         
            IF(VQC(VCN,DSN).EQ.'Q')THEN
            
               CALL MGTNUM(TVNAME(1:18)//'QCA',
     1              SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2              SFC_NRECS(FNUM),MISS_P,QCATMP,STATUS)
         
               IF(STATUS.EQ.VAR_NOTINDB_P)THEN
                  HAVE_DATA = .TRUE.
                  STATUS = SUCCESS_P
                  DO I = 1, SFC_NRECS(FNUM)
                     QCATMP(I) = 0
                     QCRTMP(I) = 0
                     QCDTMP(I) = 'Z'
                  ENDDO
               ELSE IF(STATUS.NE.SUCCESS_P)THEN
                  GO TO 2000
               ELSE
                  CALL MGTNUM(TVNAME(1:18)//'QCR',
     1                 SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                 SFC_NRECS(FNUM),MISS_P,QCRTMP,STATUS)
                  IF(STATUS.NE.SUCCESS_P)GO TO 2000
                  
                  CALL MGTCHR(TVNAME(1:18)//'DD',
     1                 SFC_NCID(FNUM),SFC_RECDIM(FNUM),1,
     2                 SFC_NRECS(FNUM),QCDTMP,LEN1REC,STATUS)
                  IF(STATUS.NE.SUCCESS_P)GO TO 2000
                  HAVE_DATA = .TRUE.
               ENDIF
            ELSE
               DO 3 I = 1, SFC_NRECS(FNUM)
                  QCATMP(I) = 0
                  QCRTMP(I) = 0
                  QCDTMP(I) = 'Z'
 3             CONTINUE
            ENDIF
         ENDIF
         DO 4 I = 1, SFC_NRECS(FNUM)
            IF(OBTMP(I).NE.MISS_P)THEN
               OB(I) = OBTMP(I)
               QCAPP(I) = QCATMP(I)
               QCRES(I) = QCRTMP(I)
               QCDESC(I) = QCDTMP(I)
            ENDIF
 4       CONTINUE
      ENDIF


C     ------------------------------------------------------------------
C     RETURN TEMPERATURE RETRIEVAL VALUE
C     ------------------------------------------------------------------
 2000 CONTINUE
      STATUS = TEMPSTATUS
      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**---------------------------------------------------------------------
