        SUBROUTINE VEPGT (
*
*  outputs
*
     :                  Y, YERR, EPOCH, TMIN, TMAX, EPMIN, EPMAX,
     :                  TEMP0, TIME0, ORDER, WFLAG, TYPE, NPTS, NROWS,
     :                  NCOEFF, NATTRB, ATTRB, BASEKY,
     :                  DETID, APERT, VOLT, GAIN, THRESH, OFILE, STATUS)
*
*  Module number: 15.1.0.2.1
*
*  Module name: polyepoch
*
*  Keyphrase:
*  ----------
*  Input parameters and data needed for VEPOCH
*
*  Description:
*  ------------
*
*  FORTRAN name: VEPGT.FOR
*
*  Keywords of accessed files and tables:
*  --------------------------------------
*  Name                         I/O     Description / Comments
*
*       CL parameters:
*
*  'INTABLE'                     I      SDAS table containing input data
*  'OUTTABLE'                    I      SDAS table containing result
*  'Y_ROOT'                      I      root name of input coefficients
*  'DY_ROOT'                     I      root name of input coefficients's
*                                       standard deviation
*  'ORDER'                       I      maximum order of the fitting polynomial
*  'WEIGHT'                      I      weighting flag
*  'TYPE'                        I      data type
*  'TEMP0'                       I      base temperature
*  'TIME0'                       I      base epoch
*  'TREFNAME'                    I      column name of the base temperature
*  'TMINNAME'                    I      column name of minimum temperature
*  'TMAXNAME'                    I      column name of maximum temperature
*  'NATTRIB'                     I      number of attributes to be copied to the
*                                       output table
*  'ATTRIBi'                     I      attributes to be copied to the output
*                                       table (max i = 5)
*
*       input table column names:
*
*  (y_rooti)                     I      y array as passed from 'Y_ROOT'
*  (dy_rooti)                    I      y array's standard deviation as
*                                       passed from 'DY_ROOT'
*  'EPOCHAVE'                    I      mean epoch
*  'EPOCHMIN'                    I      minimum epoch
*  'EPOCHMAX'                    I      maximum epoch
*  (tminname)                    I      minimum temperature as passed from 'TMINNAME'
*  (attribi)                     I      attributes to be copied to the output
*                                       table as passed from 'ATTRIBi'
*
*  Subroutines Called:
*  -------------------
*  CDBS:
*       CDTIN
*  SDAS:
*       UCLGSR, UCLGST, UTRGTR, UTRGTD, UTRGTT, UTTCLO, UMSPUT
*  Others:
*       None
*
*  History:
*  --------
*  Version      Date        Author          Description
*     1       10-15-85     J.-C. HSU        design and coding
*     2       07-15-85     J.-C. HSU        F77 standard
*
*-------------------------------------------------------------------------------
*
*== output:
*                                       --Y quantity and its standard deviation
        REAL                    Y(1), YERR(1),
*                                       --weighting scheme
     :                          WFLAG,
*                                       --minimum and maximum temperature
     :                          TMIN(1), TMAX(1),
*                                       --reference X value,
     :                          TEMP0,
*                                       --high voltage, gain setting, and
*                                       --discriminator threshold setting
     :                          VOLT, GAIN, THRESH
*                                       --epoch
        DOUBLE PRECISION        EPOCH(1),
*                                       --minimum and maximum epoch
     :                          EPMIN(1), EPMAX(1),
*                                       --base epoch
     :                          TIME0
*                                       --attribute names
        CHARACTER*(*)           ATTRB(1),
*                                       --data type
     :                          TYPE,
*                                       --output file name
     :                          OFILE,
*                                       --aperture name
     :                          APERT,
*                                       --name of base value
     :                          BASEKY
*                                       --total number of valid input data points
        INTEGER                 NPTS,
*                                       --order of fittinf polynomial
     :                          ORDER,
*                                       --number of input coefficients
     :                          NCOEFF,
*                                       --detector ID
     :                          DETID,
*                                       --number of attributes
     :                          NATTRB,
*                                       --error status
     :                          STATUS
*
*== local:
*                                       --number of data points
        INTEGER                 NROWS,
*                                       --order of the input fitting
     :                          TORDER,
*                                       --pointer of table descripter and column
*                                       --identifier
     :                          TP, COLIDN(20),
*                                       --length of root names
     :                          YLEN, DYLEN,
*                                       --loop indices, number of input columns
     :                          I, J, K, NCOLS, NC,
*                                       --index of first valid data
     :                          INDEX1,
*                                       --status
     :                          STAT(30), STATOK
*                                       --data buffer
        REAL                    CBUFF(20)
        DOUBLE PRECISION        CBUFF2(20)
*                                       --root names
        CHARACTER*15            YROOT, DYROOT
*                                       --column names
        CHARACTER*16            COLNAM(20),
*                                       --name of maximum and minimum temperature
     :                          TMINKY, TMAXKY,
*                                       --name of reference temperature
     :                          TREFKY
*                                       --null flag in UTRGTR
        LOGICAL                 NULMSK(30)
        CHARACTER*1             CHAR1
        CHARACTER*5             CHAR5
*                                       --input file name
        CHARACTER*128           IFILE
*                                       --error message context
        CHARACTER*130           CONTXT
*=========================begin hsp.inc=========================================
*                                       --status return code
        INTEGER         OK, ERRNUM(20)
*                                       --message destination and priority
        INTEGER         DEST, PRIO
        DATA OK /0/
        DATA ERRNUM /701, 702, 703, 704, 705, 706, 707, 708, 709, 710,
     :               711, 712, 713, 714, 715, 716, 717, 718, 719, 720/
        DATA DEST, PRIO /1, 0/
*=========================end hsp.inc===========================================
*------------------------------------------------------------------------------
*
*  input parameters:
*  input and output table names, number of attributes, attributes, order of
*  the fitting polynomial, weighting
*  scheme, reference X value, data type, column names of:
*  (1) the y quantity, (2) standard deviation of y, (3) x quantity,
*  (4) reference x value, (5) minimum x value, (6) maximum x value.
*
        CALL UCLGST ('intable', IFILE, STAT(1))
        CALL UCLGST ('outtable', OFILE, STAT(2))
        CALL UCLGSI ('nattrib', NATTRB, STAT(3))
        CALL UCLGSI ('order', ORDER, STAT(4))
        CALL UCLGSI ('ncoeff', NCOEFF, STAT(5))
        CALL UCLGSR ('weight', WFLAG, STAT(6))
        CALL UCLGST ('y_root', YROOT, STAT(7))
        CALL UCLGST ('dy_root', DYROOT, STAT(8))
        CALL UCLGST ('base_value_key', BASEKY, STAT(9))
        CALL UCLGSR ('base_temp', TEMP0, STAT(10))
        CALL UCLGSD ('base_time', TIME0, STAT(11))
        CALL UCLGST ('tminname', TMINKY, STAT(12))
        CALL UCLGST ('tmaxname', TMAXKY, STAT(13))
        CALL UCLGST ('trefname', TREFKY, STAT(14))

	NC = NCOEFF*2 + 7

        DO 10 I = 1, NATTRB
            WRITE (CHAR1, '(I1)') I
            CALL UCLGST ('attrib'//CHAR1, ATTRB(I), STAT(14+I))
            COLNAM(NC+I) = ATTRB(I)
   10   CONTINUE
*
        DO 20 I = 1, 14+NATTRB
            IF (STAT(I) .NE. OK) THEN
                STATUS = ERRNUM(1)
                CONTXT = 'can not get CL parameter(s)'
                GO TO 999
            END IF
   20   CONTINUE
*
*  input the following: Y quantity and its standard deviation,
*  mean epoch, from input SDAS table
*
*  construct column names of the input quantities and their standard deviations
*
        DO 30 I = 1, 15
            IF (YROOT(I:I) .EQ. ' ') THEN
                YLEN = I - 1
                GO TO 40
            END IF
   30   CONTINUE
        YLEN = 15
   40   DO 50 I = 1, 15
            IF (DYROOT(I:I) .EQ. ' ') THEN
                DYLEN = I - 1
                GO TO 60
            END IF
   50   CONTINUE
        DYLEN = 15
   60   IF (YLEN .EQ. 0 .OR. DYLEN .EQ. 0) THEN
            STATUS = ERRNUM(1)
            CONTXT = 'root name of input columns is blank'
            GO TO 999
        END IF
*
        DO 70 I = 1, NCOEFF
            WRITE(CHAR1, '(I1)') I-1
            COLNAM(I) = YROOT(1:YLEN) // CHAR1
            COLNAM(I+NCOEFF) = DYROOT(1:DYLEN) // CHAR1
   70   CONTINUE
*
        COLNAM(NCOEFF*2+1) = TMINKY
        COLNAM(NCOEFF*2+2) = TMAXKY
        COLNAM(NCOEFF*2+3) = 'EPOCHAVE'
        COLNAM(NCOEFF*2+4) = 'EPOCHMIN'
        COLNAM(NCOEFF*2+5) = 'EPOCHMAX'
        COLNAM(NCOEFF*2+6) = 'ORDER'
        COLNAM(NCOEFF*2+7) = 'TYPE'
        NCOLS = NC + NATTRB
*
        CALL CDTIN (IFILE, COLNAM, NCOLS,
     :                  TP, COLIDN, NROWS, STATUS)
        IF (STATUS .NE. OK) THEN
            CONTXT = 'error setting up input table'
            GO TO 999
        END IF
*
        NPTS = 0
        DO 110 I = 1, NROWS
            CALL UTRGTR (TP, COLIDN, NCOEFF*2+2, I,
     :                   CBUFF, NULMSK, STAT(1))
            CALL UTRGTD (TP, COLIDN(NCOEFF*2+3), 3, I,
     :                   CBUFF2, NULMSK(NCOEFF*2+3), STAT(2))
            CALL UTRGTI (TP, COLIDN(NCOEFF*2+6), 1, I,
     :                   TORDER, NULMSK(NCOEFF*2+6), STAT(3))
*
            STATOK = ABS(STAT(1) - OK) + ABS(STAT(2) -OK) +
     :                  ABS(STAT(3) - OK) + OK
            DO 80 J = 1, NC
                IF (NULMSK(J)) THEN
                    STATOK = ERRNUM(2)
                    GO TO 90
                END IF
   80       CONTINUE
*
*  if error in UTRGT_, put an error message and do NOT increment the data array
*  index
*
   90       CONTINUE
            IF (STATOK .NE. OK) THEN
                WRITE(CHAR5, '(I5)') I
                CONTXT = 'can not read input table row #' // CHAR5
                CALL UMSPUT (CONTXT, DEST, PRIO, STAT(30))
            ELSE IF (TORDER+1 .NE. NCOEFF) THEN
                WRITE(CHAR5, '(I5)') I
                CONTXT = 'inconsistent number of input quantities at '
     :                  // 'row #' // CHAR5
                CALL UMSPUT (CONTXT, DEST, PRIO, STAT(30))
            ELSE
                NPTS = NPTS + 1
                IF (NPTS .EQ. 1) THEN
                    INDEX1 = I
                END IF
                DO 100 J = 1, NCOEFF
                    K = (J-1) * NROWS + NPTS
                    Y(K) = CBUFF(J)
                    YERR(K) = CBUFF(NCOEFF+J)
  100           CONTINUE
*
                TMIN(NPTS) = CBUFF(NCOEFF*2+1)
                TMAX(NPTS) = CBUFF(NCOEFF*2+2)
                EPOCH(NPTS) = CBUFF2(1)
                EPMIN(NPTS) = CBUFF2(2)
                EPMAX(NPTS) = CBUFF2(3)
            END IF
  110   CONTINUE
*
*  read attributes
*
        CALL UTRGTT (TP, COLIDN(NCOEFF*2+7), 1, INDEX1,
     :                   TYPE, NULMSK(NCOEFF*2+6), STAT(4))
        IF (NATTRB .GT. 0) THEN
            DO 120 I = 1, NATTRB
                IF (ATTRB(I) .EQ. 'DETECTOB' .OR. ATTRB(I) .EQ.
     :                          'DET_NUM') THEN
                    CALL UTRGTI (TP, COLIDN(NC+I), 1, INDEX1,
     :                          DETID, NULMSK(NC+I), STAT(4+I))
                ELSE IF (ATTRB(I) .EQ. 'APERTOBJ' .OR. ATTRB(I)
     :                          .EQ. 'APER_NAME') THEN
                    CALL UTRGTT (TP, COLIDN(NC+I), 1, INDEX1,
     :                          APERT, NULMSK(NC+I), STAT(4+I))
                ELSE IF (ATTRB(I) .EQ. 'VOLTAGE') THEN
                    CALL UTRGTR (TP, COLIDN(NC+I), 1, INDEX1,
     :                          VOLT, NULMSK(NC+I), STAT(4+I))
                ELSE IF (ATTRB(I) .EQ. 'VGAIND') THEN
                    CALL UTRGTR (TP, COLIDN(NC+I), 1, INDEX1,
     :                          GAIN, NULMSK(NC+I), STAT(4+I))
                ELSE IF (ATTRB(I) .EQ. 'THRESH') THEN
                    CALL UTRGTR (TP, COLIDN(NC+I), 1, INDEX1,
     :                          THRESH, NULMSK(NC+I), STAT(4+I))
                ELSE
                    STATUS = ERRNUM(3)
                    CONTXT = 'invalid attribute ' // ATTRB(I)
                    GO TO 999
                END IF
  120       CONTINUE
        END IF
*
*  close the input table
*
        CALL UTTCLO (TP, STATUS)
        IF (STATUS .NE. OK) THEN
            CONTXT = 'cannot close input table'
            GO TO 999
        END IF
*
        IF (NPTS .LE. 0) THEN
            STATUS = ERRNUM(4)
            CONTXT = 'no input data been successfully read'
            GO TO 999
        END IF
*
        STATUS = OK
        GO TO 1000
*
*  write error message
*
  999   CALL UMSPUT ('VEPGT: ' // CONTXT, DEST, PRIO,        STATOK)
*
 1000   RETURN
        END
