* PACKAGE GTSEL   !" 選択, フィールド変更
**************************************************************************
      PROGRAM GTSEL
*
#ifdef SYS_IBMS
      INCLUDE    (GTSINC)
      INCLUDE    (GZSIZE)
#else
#include         "gtsinc.F"
#include         "gzsize.F"
#endif
      PARAMETER  ( RUNDEF=-999., IUNDEF=-999 )
      COMMON     /GMWORK/ MWORK
      REAL       MWORK  ( IJKDIM )
*
      CHARACTER  HHEAD( NDC )*(NCC)
      REAL       GDATA( IJKDIM )
*
      CHARACTER  HFILE( 1 ) *(NFILN)
      DATA       HFILE / '$GTTMPDIR/gtool.out' /
      DATA       IFILE / 50 /
      DATA       IFILP / 51 /
      DATA       JFILE / 60 /
      CHARACTER  ITEMZ *(NCC)
*
      INTEGER    X, Y, Z
      DATA       X, Y, Z / 3*-1 /
      INTEGER    STR, END, STEP
      DATA       STR, END, STEP / 1, 9999999, 1 /
      CHARACTER  SEL *(NCC)
      DATA       SEL    / ' ' /
      CHARACTER  OUT    *(NFILN)
      DATA       OUT    / '$GTTMPDIR/gtool.out' /
      LOGICAL    APND
      DATA       APND   / .FALSE. /
      CHARACTER  ITEM   *(NCC)
      CHARACTER  UNIT   *(NCC)
      CHARACTER  TITLE  *(NCC*2)
      CHARACTER  DSET   *(NCC)
      CHARACTER  EDIT   *(NCC)
      CHARACTER  ETTL   *(NCC)
      REAL       DIVS , DIVL
      REAL       DMIN , DMAX
      REAL       DMISS, ROPTN
      DATA       DIVS , DIVL, DMIN, DMAX, DMISS, ROPTN / 6*RUNDEF /
      INTEGER    STYP
      DATA       STYP / IUNDEF /
      INTEGER    IOPTN 
      DATA       IOPTN / IUNDEF /
      CHARACTER  COPTN  *(NCC)
      DATA       COPTN / ' ' /
      DATA       ITEM, UNIT, TITLE, DSET, EDIT, ETTL /6*' '/
      LOGICAL    GRESET
      DATA       GRESET / .FALSE. /
      LOGICAL    HELP
      DATA       HELP   / .FALSE. /
*
      NAMELIST  /OPTION/ X, Y, Z, STR, END, STEP, SEL, OUT, APND,
     &                   ITEM, UNIT, TITLE, DSET, EDIT, ETTL, 
     &                   DIVS, DIVL, DMIN, DMAX, DMISS, STYP,
     &                   ROPTN, IOPTN, COPTN, GRESET, 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 GTOPEN
      CALL GTSIZE ( HHEAD, IJKDIM )
      CALL GMSIZE ( IJKDIM  )
*
      CALL GURNTF ( HFILE( 1 ), OUT  , '$GTTMPDIR/gtool.in' )
*
      CALL GFROPN ( IFILE, HFILE( 1 ) )
      CALL GFOOPN ( JFILE,  OUT , APND )
*
      CALL GUNENV( OUT,'.',.FALSE. )
      IL=LENC(OUT)
      WRITE (6,*) 'output='//OUT(1:IL)
*
      IF ( STEP .LE. 1 ) STEP = 1
*
      II = 0
 1100 CONTINUE
         CALL   GFREAD
     O        ( HHEAD , GDATA , IEOD  ,
     I          IFILE , 1               )
*
         IF ( IEOD.EQ.0 ) THEN
*
            CALL GHCGET( HHEAD, 'ITEM', ITEMZ )
            IF (  ( SEL .EQ. ' '   )  .OR.
     &            ( SEL .EQ. ITEMZ )        ) THEN
*
               II = II + 1
               IF ( ( II.GE.STR ).AND.( II.LE.END ).AND.
     &              ( MOD( II-STR,STEP ).EQ.0 )          ) THEN         
*
                  IF ( Z .EQ. 0 ) THEN
                     CALL GMZAVG
     M               ( HHEAD , GDATA ,
     I                 'ZM'  , 'vert mean'  )
                  ELSE IF ( Z .GT. 0 ) THEN 
                     CALL GMZSEL
     M               ( HHEAD , GDATA , Z ,
     I                 '  '  , '  '        )
                  ENDIF
                  IF ( Y .EQ. 0 ) THEN
                     CALL GMYAVG
     M               ( HHEAD , GDATA ,
     I                 'YM'  , 'merid mean' )
                  ELSE IF ( Y .GT. 0 ) THEN 
                     CALL GMYSEL
     M               ( HHEAD , GDATA , Y ,
     I                 '  '  , '  '         )
                  ENDIF
                  IF ( X .EQ. 0 ) THEN
                     CALL GMXAVG
     I               ( HHEAD , GDATA ,
     I                 'XM'  , 'zonal mean' )
                  ELSE IF ( X .GT. 0 ) THEN 
                     CALL GMXSEL
     I               ( HHEAD , GDATA , X ,
     I                 '  '  , '  '        )
                  ENDIF
*
                  IF ( ITEM .NE. ' ' ) THEN
                     CALL GHCSET( HHEAD , 'ITEM', ITEM )
                  ENDIF
                  IF ( UNIT .NE. ' ' ) THEN
                     CALL GHCSET( HHEAD , 'UNIT', UNIT )
                  ENDIF
                  IF ( TITLE .NE. ' ' ) THEN
                     CALL GHCSTS( HHEAD , 'TITL', TITLE )
                  ENDIF
                  IF ( DSET .NE. ' ' ) THEN
                     CALL GHCSET( HHEAD , 'DSET', DSET )
                  ENDIF
                  IF ( GRESET ) THEN
                     CALL GHRSGP( HHEAD  )
                  ENDIF
                  IF ( DIVS .NE. RUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'DIVS', DIVS )
                  ENDIF
                  IF ( DIVL .NE. RUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'DIVL', DIVL )
                  ENDIF
                  IF ( DMIN .NE. RUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'DMIN', DMIN )
                  ENDIF
                  IF ( DMAX .NE. RUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'DMAX', DMAX )
                  ENDIF
                  IF ( DMISS .NE. RUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'MISS', DMISS )
                  ENDIF
                  IF ( ROPTN .NE. RUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'ROPTN', ROPTN )
                  ENDIF
                  IF ( STYP .NE. IUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'STYP', STYP )
                  ENDIF
                  IF ( IOPTN .NE. IUNDEF ) THEN
                     CALL GHPSET( HHEAD, 'IOPTN', IOPTN )
                  ENDIF
                  IF ( COPTN .NE. ' ' ) THEN
                     CALL GHCSET( HHEAD, 'COPTN', COPTN )
                  ENDIF

*
                  CALL  GFWRIT
     I                 ( HHEAD , GDATA ,
     I                   JFILE , 1     , 0       )
*
               ENDIF
            ENDIF
*
            IF ( II .LT. END ) GOTO 1100
         ENDIF
*
      STOP
      END
