* PACKAGE GTCURV    !"  折れ線図                      A.Numaguti
*"                             for dcl-5.0 : 95/05/31 S.Takehiro 
*"                  異なる軸ファイルに対応 : 95/11/07 S.Takehiro 
*"                             miner bugfix: 96/09/06 S.Takehiro
*******************************************************************
      PROGRAM    GTCURV
*
#ifdef SYS_IBMS
      INCLUDE    (GTSINC)
      INCLUDE    (GZSIZE)
#else
#include         "gtsinc.F"
#include         "gzsize.F"
#endif
      PARAMETER  ( NFILE=10, NIDX=24 )
      COMMON     /GMWORK/ MWORK
      REAL       MWORK  ( IJKDIM )
*
      CHARACTER  HHEAD ( NDC )*(NCC)
      REAL       GDATA ( IJKDIM )
*
      INTEGER    IFILE( NFILE )
      DATA       IFILE / 50,51,52,53,54,55,56,57,58,59 /
      PARAMETER  (NFM1=NFILE-1)
      CHARACTER  HFILE( NFILE ) *(NFILN)
      DATA       HFILE / '$GTTMPDIR/gtool.out',NFM1*' ' /
*
      INTEGER    X, Y, Z
      DATA       X, Y, Z / 3*-1 /
      INTEGER    OVERLAY
      DATA       OVERLAY / 1 /
      CHARACTER  LTITEM  *(NCC)
      DATA       LTITEM / 'ITEM' /
      LOGICAL    LINT
      DATA       LINT  / .TRUE. /
      REAL       DIV ( 2 )
      DATA       DIV   / -999.,-999. /
      REAL       RANGE ( 2 )
      DATA       RANGE / -999.,-999. /

      PARAMETER  (NIDM1=NIDX-1,NIDM4=NIDX-4)
      INTEGER    LIDX( NIDX ), LTYPE( NIDX )
      DATA       LIDX  / 2,NIDM1*-1 / 
      DATA       LTYPE / 1,2,3,4,NIDM4*-1 /
      REAL       BITLEN
      DATA       BITLEN / -1. /
      INTEGER    LAY
      DATA       LAY / 1 /
      INTEGER    WSN
      DATA       WSN / 0 /
      LOGICAL    MONO, PRINT
      DATA       MONO, PRINT / 2*.FALSE. /
      CHARACTER  PS   *(NFILN)
      DATA       PS  / '$GTTMPDIR/sgks.ps' /
      LOGICAL    TICK , ATTL
      DATA       TICK , ATTL / 2*.TRUE. /
      INTEGER    STR, END, STEP
      DATA       STR, END, STEP / 1, 999999, 1 /
      LOGICAL    EXCH
      DATA       EXCH  /.FALSE./
      INTEGER    STYPE
      DATA       STYPE / 0 /
      CHARACTER  ITEM   *(NCC)
      CHARACTER  UNIT   *(NCC)
      CHARACTER  TITLE  *(NCC*2)
      CHARACTER  DSET   *(NCC)
      CHARACTER  EDIT   *(NCC)
      CHARACTER  ETTL   *(NCC)
      DATA       ITEM, UNIT, TITLE, DSET, EDIT, ETTL /6*' '/
      LOGICAL    GRESET
      DATA       GRESET / .FALSE. /
      LOGICAL    HELP
      DATA       HELP   / .FALSE. /
*
      NAMELIST /OPTION/  X, Y, Z, DIV, RANGE,
     &                   OVERLAY, LIDX, LTYPE, BITLEN, LTITEM, LINT,
     &                   LAY , WSN,  MONO, PRINT, PS, TICK, ATTL,
     &                   STR, END, STEP, EXCH , STYPE,
     &                   ITEM, UNIT, TITLE, DSET, EDIT, ETTL, GRESET,
     &                   HELP, HFILE
*
      CALL OPTARG ( 91, 'OPTION', 'HFILE', NOPT, NFILES )
      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  )
*
      IF ( WSN .LE. 0 ) THEN
         IF ( PRINT ) THEN
            WSN = 2
            IF ( PS .NE. ' ' ) THEN
               CALL GUNENV( PS, '.', .FALSE. )
               CALL SWCSET( 'PSFILE', PS     )
               IP=LENC(PS)
               WRITE (6,*) 'ps='//PS(1:IP)
            ENDIF
         ELSE
            WSN = 1
         ENDIF
      ENDIF
      CALL GGOPEN ( WSN )
*
      NFL = 1
      IF ( NFILES .EQ. 0 ) NFILES = 1
      DO 300 IFL = 1, NFILES
         IF ( HFILE(IFL) .NE. ' ' ) THEN
            CALL GFROPN ( IFILE(IFL), HFILE(IFL) )
            NFL = IFL
         ENDIF
 300  CONTINUE
*
      IF ( Z .GE. 0 ) THEN
         IF ( X.LT.0  .AND. Y.LT.0 ) X = 0
      ELSE IF ( Y .GE. 0 ) THEN
         IF ( X.LT.0  .AND. Z.LT.0 ) Z = 0
      ELSE IF ( X .GE. 0 ) THEN
         IF ( Y.LT.0  .AND. Z.LT.0 ) Z = 0
      ELSE
         X = 0
         Z = 0
      ENDIF
*
      OVERLAY = MAX( OVERLAY, NFL )
      STEP    = MAX( STEP, 1 )
*
      NLIDX  = 1
      NLTYPE = 1
      DO 500 ID = 1, NIDX
         IF ( LIDX(ID)  .NE. -1 ) NLIDX  = ID
         IF ( LTYPE(ID) .NE. -1 ) NLTYPE = ID
 500  CONTINUE
*
      IF ( BITLEN .GT. 0 ) THEN
         CALL SGPSET( 'BITLEN', BITLEN )
      ENDIF
      CALL CUPPER( LTITEM )
      CALL GGCSET( 'LTITEM', LTITEM )
      CALL GGPSET( 'LLINT', LINT )
*
      II  = 0
      IID = 0
 1100 CONTINUE
         IFILEX = IFILE( MOD(II,NFL)+1 )
         CALL GFREAD
     O            ( HHEAD , GDATA , IEOD  ,
     I              IFILEX, 1               )
*
         IF ( IEOD .EQ. 0 ) THEN
*
            II = II + 1
            IF ( ( II.GE.STR ).AND.( II.LE.END ).AND.
     &           ( MOD( II-STR,STEP ).EQ.0 )          ) THEN         
               IID = IID + 1
*
               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 ( STYPE .NE. 0 ) THEN
                  CALL GHPSET ( HHEAD, 'STYP', STYPE )
               ENDIF
*     
               IF ( DIV( 1 ) .GT. 0. ) THEN
                  CALL GHPSET( HHEAD, 'DIVS', DIV( 1 ) )
               ENDIF
               IF ( DIV( 2 ) .GT. 0. ) THEN
                  CALL GHPSET( HHEAD, 'DIVL', DIV( 2 ) )
               ENDIF
               IF ( RANGE(1) .NE. -999. ) THEN
                  CALL GHPSET( HHEAD, 'DMIN', RANGE(1) )
               ENDIF
               IF ( RANGE(2) .NE. -999. ) THEN
                  CALL GHPSET( HHEAD, 'DMAX', RANGE(2) )
               ENDIF
*
               IF ( MOD(IID-1,OVERLAY) .EQ. 0 ) THEN
                  IF      ( LAY .EQ. 2 ) THEN
                     CALL GGLAY2  ( HHEAD )
                  ELSE IF ( LAY .EQ. 3 ) THEN
                     CALL GGLAY3  ( HHEAD )
                  ELSE
                     CALL GGLAY1  ( HHEAD )
                  ENDIF
               ENDIF
*
               IF ( .NOT. EXCH ) THEN
                  CALL GGAXIS  ( HHEAD , 'X' )
               ELSE
                  CALL GGAXIS  ( HHEAD , 'Y' )
               ENDIF
*
               LI = MOD( IID-1,OVERLAY )+1
               LIDXS  = LIDX ( MOD( LI-1,NLIDX  )+1 )
               LTYPES = LTYPE( MOD( LI-1,NLTYPE )+1 )
               IF ( LIDXS .GT. 0 ) THEN
                  CALL SGSPLI  ( LIDXS )
                  CALL SGSPLT  ( LTYPES )
                  IF ( .NOT. EXCH ) THEN
                     IF ( .NOT. TICK ) CALL GGCSET( 'CYSIDET', ' ' )
                     IF ( .NOT. ATTL ) CALL GGCSET( 'CYSIDEL', ' ' )
                     CALL GGCURV  ( HHEAD , GDATA , 'Y' )
                  ELSE
                     IF ( .NOT. TICK ) CALL GGCSET( 'CXSIDET', ' ' )
                     IF ( .NOT. ATTL ) CALL GGCSET( 'CXSIDEL', ' ' )
                     CALL GGCURV  ( HHEAD , GDATA , 'X' )
                  ENDIF
               ENDIF
*
            ENDIF
            IF ( II .LT. END ) GOTO 1100
         ENDIF
*
      CALL GGCLSE
*
      STOP
      END
