C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MGETSATRAD  GET SATRAD OBS & QC INFO FOR ONE VARIABLE
C   PRGMMR: L. BENJAMIN      ORG: FSL        DATE: 04-09-27
C
C ABSTRACT: READ SATRAD OBS & QC INFO FOR ALL STATIONS FOR ONE
C   VARIABLE AND TIME.
C
C PROGRAM HISTORY LOG:
C   05-10-25  L. BENJAMIN ORIGINAL VERSION
C   07-11-21  L. BENJAMIN REMOVED COMMENTED OUT CODE.
C
C USAGE:    CALL MGETSATRAD (ATIME,VCODE,NSTA,NOBS,NCHANS,OB,
C                            QCDESC,QCAPP,QCRES,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 STATION RECORDS RETURNED
C     NOBS     - I       NUMBER OF NON-MISSING OBS (<= NSTA)
C     NCHANS   - IA      NUMBER OF CHANS
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 SATRAD NETCDF FILES FOR ATIME
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, MGTNUM, MGTCHR, MQCFLTR, MGETVCN,
C                MGENVCN, M_PTOZSA
C
C REMARKS:
C   - THE CALLER MUST CALL MINIT AND MSATRADSTA BEFORE CALLING THIS
C     ROUTINE.  MSATRADSTA 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 MSATRADSTA.
C   - DOUBLE & CHARACTER DATA TYPES ARE NOT HANDLED BY THIS ROUTINE.
C   - THE CALLER SHOULD DIMENSION ARRAYS TO MATCH MXSATR_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 MGETSATRAD(ATIME,VCODE,NSTA,NOBS,NCHANS,OB,
     1     QCDESC,QCAPP,QCRES,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
      INTEGER NCHANS(*)
      REAL OB(*)
      CHARACTER*1 QCDESC(*)

C     -----------------------------------------------------------------
C     LOCAL
C     -----------------------------------------------------------------
      INTEGER I,J,K,L,FNUM,DSN,LEN1REC,DUMMY,VCN,SPT,ORIG_VCN

      INTEGER TOTCHANS

      REAL OBTMP(MXSATR_P*MXSATRCHAN_P),QCATMP(MXSATR_P*MXSATRCHAN_P)
      REAL QCRTMP(MXSATR_P*MXSATRCHAN_P)
      REAL M_PTOZSA

      CHARACTER*1 QCDTMP(MXSATR_P*MXSATRCHAN_P),VCT,CDUMMY
      LOGICAL PROC_FILE(SATR_MXSTRF)

      INTEGER NUMCHANS
      PARAMETER (NUMCHANS = MXSATRCHAN_P)

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     IF THE SATRAD SUBSYSTEM HASN'T BEEN INITIALIZED, OR IF THE 
C     REQUESTED TIME DOESN'T MATCH THE TIME LAST PROCESSED BY 
C     MSATRADSTA, OR IF THE VCODE IS CLEARLY ILLEGAL, RETURN WITH 
C     ERROR.
C     -----------------------------------------------------------------
      IF(SATR_INIT.NE.INIT_P)THEN
         STATUS = SELSUB_NOTINIT_P
         CALL MERROR('MGETSATRAD: SELECTED SUBSYSTEM NOT INITIALIZED'//
     1               ' - SATRAD')
         GO TO 900
      ELSE IF(ATIME.NE.LAST_SATR_ATIME.OR.ATIME.EQ.'         ')THEN
         STATUS = TIME_NOTINIT_P
         CALL MERROR('MGETSATRAD: TIME NOT INITIALIZED BY MSATRADSTA')
         GO TO 900
      ELSE 
         CALL MGETVCN(VCODE,ORIG_VCN,VCT,STATUS)
         IF(STATUS.NE.SUCCESS_P)GO TO 900
      ENDIF

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

C     -----------------------------------------------------------------
C     TRANSLATE THE CALLER'S VCN INTO THE GENERIC VERSION.  THIS IS 
C     USED TO ALLOW THE CALLER TO SELECT ONLY SPECIFIC WIND PRODUCTS 
C     (E.G.IR ONLY).  FOR MOST OF THIS ROUTINE (AND ITS SUBORDINATES) 
C     WE'LL DEAL WITH THE GENERIC VERSION TO MAKE THINGS SIMPLER, THEN 
C     DEAL WITH THE CALLER'S DESIRED SUBSET WHEN IT COMES TIME TO 
C     RETURN THE ACTUAL DATA.
C     -----------------------------------------------------------------
      CALL MGENVCN(ORIG_VCN,SATR_DS_P,CDBTYPE,VCN,SPT)
      TOTCHANS = 1
      IF(SPT.EQ.-20)THEN
         TOTCHANS = NUMCHANS
      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 = SATR_LNUM
      IF(NSTA.EQ.0)THEN
         STATUS = NO_DATA_P
         GO TO 900
      ENDIF

      FNUM = 0

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

      DO  I = 1, NSTA
         NCHANS(I) = 0
      ENDDO

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

C     -----------------------------------------------------------------
C     NOTE THAT WE LOOP THROUGH SATR_FNUM TO SEE WHICH FILES ARE NEEDED
C     AND NOT THROUGH SATR_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 MSATRADSTA.  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(SATR_FNUM(I)))GO TO 100
         FNUM = SATR_FNUM(I)
         PROC_FILE(FNUM) = .TRUE.
         DSN = MAX_SFCDSET_P + MAX_UAPTDSET_P + MAX_HYDRODSET_P +
     1        MAX_SATWDSET_P + MAX_SATSDSET_P + SATR_SOURCE(FNUM)

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('MGETSATRAD: INVALID VARIABLE DATA TYPE')
            GO TO 900
         ENDIF

         CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN)),
     1        SATR_NCID(FNUM),SATR_RECDIM(FNUM),1,SATR_NRECS(FNUM),
     2        MISS_P,OBTMP,STATUS)
         IF(STATUS.NE.SUCCESS_P)GO TO 900

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           SATR_NCID(FNUM),SATR_RECDIM(FNUM),1,
     2           SATR_NRECS(FNUM),MISS_P,QCATMP,STATUS)
               
            IF(STATUS.EQ.VAR_NOTINDB_P)THEN
               QCSTATUS = EQCFAILED_P
               STATUS = SUCCESS_P
               DO J = 1, SATR_NRECS(FNUM)
                  DO K = 1, TOTCHANS                 
                     QCDTMP((J-1)*TOTCHANS+K) = 'Z'
                     QCATMP((J-1)*TOTCHANS+K) = 0
                     QCRTMP((J-1)*TOTCHANS+K) = 0
                  ENDDO
               ENDDO
            ELSE IF(STATUS.NE.SUCCESS_P)THEN
               GO TO 900
            ELSE
                  
               CALL MGTNUM(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'QCR',
     1              SATR_NCID(FNUM),SATR_RECDIM(FNUM),1,
     2              SATR_NRECS(FNUM),MISS_P,QCRTMP,STATUS)
               IF(STATUS.NE.SUCCESS_P)GO TO 900
               
               CALL MGTCHR(VNAME(VCN,DSN)(1:VLEN(VCN,DSN))//'DD',
     1              SATR_NCID(FNUM),SATR_RECDIM(FNUM),1,
     2              SATR_NRECS(FNUM),QCDTMP,LEN1REC,STATUS)
               IF(STATUS.NE.SUCCESS_P)GO TO 900
            ENDIF
         ELSE
            QCSTATUS = EQCFAILED_P

            DO 10 J = 1, SATR_NRECS(FNUM)
               DO K = 1, TOTCHANS
                  QCDTMP((J-1)*TOTCHANS+K) = 'Z'
                  QCATMP((J-1)*TOTCHANS+K) = 0
                  QCRTMP((J-1)*TOTCHANS+K) = 0
               ENDDO
 10         CONTINUE

         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 SATRAD DATASET
C     INDIVIDUALLY.  THIS IS NEEDED TO COVER THE CASE OF QC INFO NOT
C     BEING AVAILABLE FOR ALL SATRAD DATASETS, BUT IS AVAILABLE FOR 
C     SOME.
C     -----------------------------------------------------------------
         CALL MQCFLTR(VCN,DSN,1,SATR_NRECS(FNUM)*TOTCHANS,QCDTMP,
     1        DUMMY,DUMMY,OBTMP)

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


         L = 1
         DO 120 J = I, NSTA
            IF(SATR_FNUM(J).NE.FNUM .OR. SATR_RNUM(J).LT.1)GO TO 120
            DO 140 K = 1, TOTCHANS
               L = SATR_RNUM(J)*TOTCHANS-TOTCHANS+K
               IF((J-1)*TOTCHANS+K.GT.NSTA*MXSATRCHAN_P)
     1              GO TO 120
               IF(OBTMP(L).NE.MISS_P)THEN

                  OB((J-1)*TOTCHANS+K) = OBTMP(L)
                  QCDESC((J-1)*TOTCHANS+K) = QCDTMP(L)
                  QCAPP((J-1)*TOTCHANS+K) = QCATMP(L)
                  QCRES((J-1)*TOTCHANS+K) = QCRTMP(L)
               ENDIF
 140        CONTINUE
            NCHANS(J) = TOTCHANS
 120     CONTINUE


 100  CONTINUE
 900  IF(STATUS.NE.SUCCESS_P)THEN

         NSTA = 0
         NOBS = 0

      ELSE

C     -----------------------------------------------------------------
C     COUNT UP THE NUMBER OF NON-MISSING OBS ACTUALLY RETURNED.
C     -----------------------------------------------------------------
         NOBS = 0


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

         IF(NOBS.EQ.0)THEN
            NSTA = 0
            STATUS = NO_DATA_P
         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**--------------------------------------------------------------------
