C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MGETACARSC  GET ACARS CHAR DATA FOR ONE VARIABLE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-06-22
C
C ABSTRACT: READ ACARS CHARACTER DATA FOR ALL STATIONS FOR
C   ONE VARIABLE AND TIME.
C
C PROGRAM HISTORY LOG:
C   00-06-22  M. BARTH    ORIGINAL VERSION
C   01-05-03  M. BARTH    CHANGED MRDNUM/MRDCHR TO MGTNUM/MGTCHR
C   01-07-02  M. BARTH    - ADDED CDBTYPE
C                         - CHANGE FROM CALLER-SPECIFIED VARIABLE CODE
C                           NUMBER TO VARIABLE CODE NAME
C   02-05-29  M. BARTH    RESTRUCTURED TEMPORARY MEMORY ARRAY USAGE
C   04-08-06  M. BARTH    ADDED ACARSTT_VN_P VARIABLE (TIME OF MAX TURB)
C                         AND RENAMED MACARSRT MACARSTIME
C   09/27/05  L. BENJAMIN REDUCED COMMENT SIZE.  NO CODE CHANGE.
C
C USAGE:    CALL MGETACARSC (ATIME,VCODE,NSTA,CDATA,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     CDATA    - CA      ARRAY OF RETURNED CHARACTER DATA
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C
C   INPUT FILES:
C     MADIS ACARS NETCDF FILES FOR ATIME
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MACARSTIME, MERROR, MGTCHR, MGETVCN
C
C REMARKS:
C   - THE CALLER MUST CALL MINIT AND MACARSSTA BEFORE CALLING THIS
C     ROUTINE.  MACARSSTA 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 MACARSSTA.
C   - THE CALLER SHOULD DIMENSION ARRAYS TO MATCH THE MAXIMUM
C     NUMBER OF STATIONS POSSIBLE FOR THIS DATATYPE (madiscon.f),
C     AND TO MATCH THE DIMENSIONS OF THE DESIRED CHARACTER
C     VARIABLE.  SEE THE MADIS USER'S GUIDE TO SEE WHAT DIMENSIONS
C     SHOULD BE USED FOR THE CHARACTER VARIABLES.
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 MGETACARSC(ATIME,VCODE,NSTA,CDATA,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,STATUS
      CHARACTER*1 CDATA(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I,J,K,FNUM,DSN,LEN1REC,CD,VCN
      LOGICAL HAVE_ANY
      CHARACTER*1 CTMP(MAX_CHAR_VLEN_P*MXUAPT_P),VCT

C     ------------------------------------------------------------------
C     TEMPORARY STORAGE DEFINITIONS
C     ------------------------------------------------------------------
      EQUIVALENCE(CTMP,TMP_L1_C1)

C     ------------------------------------------------------------------
C     SET THE CURRENT DATASET TYPE AND DATABASE TYPE TO ACARS.
C     ------------------------------------------------------------------
      CDTYPE = ACARS_DS_P
      CD = ACARS_DS_P
      CDBTYPE = UAPT_DBTYPE(CD)
      DSN = MAX_SFCDSET_P + CD

C     ------------------------------------------------------------------
C     IF THE ACARS SUBSYSTEM HASN'T BEEN INITIALIZED, OR IF THE
C     REQUESTED TIME DOESN'T MATCH THE TIME LAST PROCESSED BY MACARSSTA,
C     OR IF THE VCODE IS ILLEGAL, OR IF THE VARIABLE'S NOT CHARACTER 
C     DATA TYPE, RETURN WITH ERROR.
C     ------------------------------------------------------------------
      IF(UAPT_INIT(CD).NE.INIT_P)THEN
         STATUS = SELSUB_NOTINIT_P
         CALL MERROR('MGETACARSC: SELECTED SUBSYSTEM NOT INITIALIZED'//
     1               ' - '//UAPTDSET_NAME(CD))
         GO TO 900
      ELSE IF(ATIME.NE.LAST_UAPT_ATIME(CD).OR.ATIME.EQ.'         ')THEN
         STATUS = TIME_NOTINIT_P
         CALL MERROR('MGETACARSC: TIME NOT INITIALIZED BY MACARSSTA'//
     1               ' - '//UAPTDSET_NAME(CD))
         GO TO 900
      ENDIF

      CALL MGETVCN(VCODE,VCN,VCT,STATUS)
      IF(STATUS.NE.SUCCESS_P)GO TO 900

C     ------------------------------------------------------------------
C     IF THEY WANT THE RECEIPT TIMES OR TIMES OF MAXIMUM TURBULENCE
C     THEY GET READ IN AS NUMBERS AND TRANSLATED TO CHARACTER STRINGS -- 
C     DO IT ALL VIA A SUBROUTINE.
C     ------------------------------------------------------------------
      IF(VCN.EQ.ACARSRT_VN_P.OR.
     1   (VCN.EQ.ACARSTT_VN_P.AND.CDBTYPE.EQ.FSL_DB_P))THEN
         CALL MACARSTIME(VCN,ATIME,NSTA,CDATA,STATUS)
         RETURN

      ELSE IF(VCN.EQ.ACARSTT_VN_P.AND.CDBTYPE.NE.FSL_DB_P)THEN
         STATUS = VAR_NOTINDB_P
         CALL MERROR('MGETACARSC: VARIABLE NOT IN SELECTED DATABASE')
         GO TO 900

      ELSE IF(VDTYPE(VCN,DSN).NE.'c')THEN
         STATUS = INV_DTYPE_P
         CALL MERROR('MGETACARSC: INVALID VARIABLE DATA TYPE')
         GO TO 900
      ENDIF

      NSTA = UAPT_LNUM(CD)
      IF(NSTA.EQ.0)THEN
         STATUS = NO_DATA_P
         GO TO 900
      ENDIF

      FNUM = 0

C     ------------------------------------------------------------------
C     NOTE THAT WE LOOP THROUGH UAPT_FNUM TO SEE WHICH FILES ARE NEEDED
C     AND NOT THROUGH UAPT_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 MACARSSTA.  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(FNUM.EQ.UAPT_FNUM(I,CD))GO TO 100

C     ------------------------------------------------------------------
C     IF THIS IS THE FIRST TIME WE'VE BEEN HERE, FILL IN BLANKS IN THE
C     CALLER'S ARRAY.
C     ------------------------------------------------------------------

         IF(FNUM.EQ.0)THEN

            FNUM = UAPT_FNUM(I,CD)

            DO 118 J = 1, NSTA

               DO 119 K = 1, VCLEN(VCN,DSN)

                  CDATA((J-1)*VCLEN(VCN,DSN)+K) = ' '

 119           CONTINUE

 118        CONTINUE

         ELSE

            FNUM = UAPT_FNUM(I,CD)

         ENDIF

C     ------------------------------------------------------------------
C     READ THE VARIABLE.  IF THERE'S AN ERROR, THE SUBROUTINE WILL HAVE
C     SET STATUS & CALLED MERROR.  IF THERE ISN'T AN ERROR, INDICATE
C     THAT WE ACTUALLY READ SOMETHING.
C     ------------------------------------------------------------------
         CALL MGTCHR(VNAME(VCN,DSN)(1:VLEN(VCN,DSN)),UAPT_NCID(FNUM,CD),
     1               UAPT_RECDIM(FNUM,CD),1,UAPT_NRECS(FNUM,CD),CTMP,
     2               LEN1REC,STATUS)
         IF(STATUS.NE.SUCCESS_P)GO TO 900

         HAVE_ANY = .TRUE.

C     ------------------------------------------------------------------
C     EXTRACT DESIRED RECORDS INTO CALLER'S ARRAY.  REPLACE NULLS WITH
C     BLANKS (FOR FORTRAN USERS).
C     ------------------------------------------------------------------
         DO 120 J = I, NSTA

            IF(UAPT_FNUM(J,CD).NE.FNUM)GO TO 100

            DO 121 K = 1, LEN1REC

               IF(ICHAR(CTMP((UAPT_RNUM(J,CD)-1)*LEN1REC+K)).NE.0)THEN
                  CDATA((J-1)*VCLEN(VCN,DSN)+K) =
     1                 CTMP((UAPT_RNUM(J,CD)-1)*LEN1REC+K)
               ELSE
                  CDATA((J-1)*VCLEN(VCN,DSN)+K) = ' '
               ENDIF

 121        CONTINUE

            IF(J.EQ.NSTA)GO TO 900

 120     CONTINUE

 100  CONTINUE

 900  IF(STATUS.NE.SUCCESS_P)THEN

         NSTA = 0

      ELSE IF(.NOT.HAVE_ANY)THEN

         STATUS = NO_DATA_P
         NSTA = 0

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