* PACKAGE GTSHOW   !" 最大/最小キャラクタ表示
************************************************************************
      PROGRAM GTMXMN
*
#ifdef SYS_IBMS
      INCLUDE    (GTSINC)
      INCLUDE    (GZSIZE)
#else
#include         "gtsinc.F"
#include         "gzsize.F"
#endif
*
      CHARACTER  HHEAD ( NDC )*(NCC)
      REAL       GDATA ( IJKDIM )
      CHARACTER  HHFLD       *(NCC)
      CHARACTER  HH          *51
*
      CHARACTER  HFILE( 1 )        *(NFILN)
      DATA       HFILE / '$GTTMPDIR/gtool.out' /
      DATA       IFILE / 50 /
*
      INTEGER    XSTR, XEND, XSTEP, X
      INTEGER    YSTR, YEND, YSTEP, Y
      INTEGER    ZSTR, ZEND, ZSTEP, Z
      INTEGER    TSTR, TEND, TSTEP, T
      DATA       XSTR, XEND, XSTEP, X / 1, 999999, 1, -999 /
      DATA       YSTR, YEND, YSTEP, Y / 1, 999999, 1, -999 /
      DATA       ZSTR, ZEND, ZSTEP, Z / 1, 999999, 1, -999 /
      DATA       TSTR, TEND, TSTEP, T / 1, 999999, 1, -999 /
      LOGICAL    HEAD
      DATA       HEAD   / .FALSE. /
      LOGICAL    NOHEAD
      DATA       NOHEAD / .FALSE. /
      LOGICAL    POINTS
      DATA       POINTS / .FALSE. /
      LOGICAL    ALL
      DATA       ALL  / .FALSE. /
      LOGICAL    HELP
      DATA       HELP / .FALSE. /
      LOGICAL    LOC
      DATA       LOC  / .FALSE. /
*
      NAMELIST  /OPTION/ XSTR, XEND, XSTEP, X,
     &                   YSTR, YEND, YSTEP, Y,
     &                   ZSTR, ZEND, ZSTEP, Z,
     &                   TSTR, TEND, TSTEP, T,
     &                   HEAD, NOHEAD, POINTS, 
     &                   ALL, LOC, HELP, HFILE
*
      CALL OPTARG ( 91, 'OPTION', 'HFILE', NOPT, NFILE )
      READ (91,OPTION,IOSTAT=IOS)
      CLOSE(91)
      IF ( IOS.NE.0 .OR. HELP ) THEN
         WRITE(6,OPTION)
         STOP
      ENDIF
*
      CALL GTSIZE ( HHEAD , IJKDIM )
      CALL GTOPEN
*
      CALL GFROPN ( IFILE, HFILE( 1 ) )
*
      II = 0
 1100 CONTINUE
         II = II + 1
*
         CALL GFREAD
     O            ( HHEAD , GDATA , IEOD  ,
     I              IFILE , 0               )
*
         IF ( IEOD .EQ. 0 ) THEN
            IF ( II .GT. TEND ) GOTO 1900
*
            IF ( ((T.GT.0).AND.(T.EQ.II))   .OR.
     &           ((T.LT.0).AND.(II.GE.TSTR).AND.
     &            (II.LE.TEND).AND.(MOD(II-TSTR,TSTEP).EQ.0)) ) THEN
*
               HH = '###### Data No.      ##############'
*                    12345678901234567890123456789012345
*
               WRITE ( HH(17:20), '(I4)' ) II
               WRITE ( 6,* ) HH
*
               CALL GUQSIZ
     I              ( HHEAD,
     O                IXSTR, IXEND, IXDIM,
     O                IYSTR, IYEND, IYDIM,
     O                IZSTR, IZEND, IZDIM )
*
               CALL GHPGET(  HHEAD, 'MISS',  RMISS )
               CALL GLRSTX( 'RMISS', RMISS )
               CALL GLLSTX( 'LMISS', .TRUE. )  !" デフォルトは欠損値処理ON
*
               CALL MAXMIN( GDATA, IXDIM, IYDIM, IZDIM, 
     &                      IXDIM, IYDIM, HHEAD(3) )
            ENDIF
      GOTO 1100            
         ENDIF
*
 1900 CONTINUE
*
      STOP
      END
************************************************************************
      SUBROUTINE MAXMIN      !"  配列の最小値最大値を出力 
     I         ( DATA, IDIM, JDIM, KMAX, IMAX, JMAX, HITEM )
*
*   [PARAM] 
      INTEGER    IDIM
      INTEGER    JDIM
      INTEGER    KMAX
*
*   [INPUT] 
      REAL       DATA( IDIM, JDIM, KMAX )
      INTEGER    IMAX
      INTEGER    JMAX
      CHARACTER  HITEM*(*)                     !" 表題
*
*   [INTERNAL WORK] 
      INTEGER    IPMIN, JPMIN, KPMIN
      INTEGER    IPMAX, JPMAX, KPMAX
      REAL       DMIN, DMAX
*
      CALL RVMXMN
     O     ( DMAX  , DMIN  , 
     O       IPMAX , JPMAX , KPMAX , 
     O       IPMIN , JPMIN , KPMIN , 
     I       DATA  , IDIM  , JDIM  , KMAX , 
     I          1  , IMAX  ,    1  , JMAX ,  1 , KMAX  )
*
      WRITE (6,*) HITEM, ': min = ', DMIN,' at ', IPMIN,JPMIN,KPMIN
      WRITE (6,*) HITEM, ': max = ', DMAX,' at ', IPMAX,JPMAX,KPMAX
*
      RETURN
      END
*********************************************************************
      SUBROUTINE RVMXMN
     O            ( DMAX  , DMIN  , 
     O              IPMAX , JPMAX , KPMAX , 
     O              IPMIN , JPMIN , KPMIN , 
     I              DATA  , IDIM  , JDIM  , KDIM , 
     I              ISTR  , IEND  , JSTR  , JEND , KSTR , KEND  )
*
*   [INPUT
      REAL       DMAX , DMIN             !" 最大・最小値
      INTEGER    IPMAX, JPMAX, KPMAX       !" 最大値をとる要素
      INTEGER    IPMIN, JPMIN, KPMIN       !" 最小値をとる要素
      INTEGER    IDIM, JDIM, KDIM
      REAL       DATA( IDIM, JDIM, KDIM )  !" 調べる配列
      INTEGER    ISTR, IEND                !" 調べる範囲
      INTEGER    JSTR, JEND
      INTEGER    KSTR, KEND
*
      REAL       RMISS                     !" 欠損値
      LOGICAL    LMISS                     !" 欠損値処理スイッチ
      LOGICAL    OSETMN, OSETMX            !" 最大最小設定スイッチ
*
*   [INTERNAL WORK] 
      INTEGER    I, J, K
*
      CALL GLLGET( 'LMISS', LMISS )
      CALL GLRGET( 'RMISS', RMISS )
*
      OSETMN = .FALSE.
      OSETMX = .FALSE.
*
      DO 1100 K=KSTR,KEND
        DO 1100 J=JSTR,JEND
          DO 1100 I=ISTR,IEND
*
             IF ( LMISS ) THEN
                IF ( DATA( I,J,K ) .NE. RMISS ) THEN
                   IF ( DATA( I,J,K ) .LT. DMIN .OR. .NOT. OSETMN ) THEN
                      DMIN = DATA( I,J,K )
                      IPMIN = I
                      JPMIN = J
                      KPMIN = K
                      OSETMN = .TRUE. 
                   ENDIF
*
                   IF ( DATA( I,J,K ) .GT. DMAX .OR. .NOT.OSETMX ) THEN
                      DMAX = DATA( I,J,K )
                      IPMAX = I
                      JPMAX = J
                      KPMAX = K
                      OSETMX = .TRUE. 
                   ENDIF
                ENDIF
             ELSE
                IF ( DATA( I,J,K ) .LT. DMIN .OR. .NOT. OSETMN ) THEN
                   DMIN = DATA( I,J,K )
                   IPMIN = I
                   JPMIN = J
                   KPMIN = K
                   OSETMN = .TRUE. 
                ENDIF
*
                IF ( DATA( I,J,K ) .GT. DMAX .OR. .NOT. OSETMX ) THEN
                   DMAX = DATA( I,J,K )
                   IPMAX = I
                   JPMAX = J
                   KPMAX = K
                   OSETMX = .TRUE. 
                ENDIF
             ENDIF
*
 1100 CONTINUE
*
      IF ( .NOT. OSETMN ) 
     &     CALL MSGDMP('W','RVMXMN','MIN. VALUE NOT FOUND')
      IF ( .NOT. OSETMX ) 
     &     CALL MSGDMP('W','RVMXMN','MAX. VALUE NOT FOUND')
*
      RETURN
      END
