* PACKAGE GTEXT    !" サイズの変更, 次元の追加
*********************************************************************
      PROGRAM GTEXT
*
#ifdef SYS_IBMS
      INCLUDE    (GTSINC)
      INCLUDE    (GZSIZE)
#else
#include         "gtsinc.F"
#include         "gzsize.F"
#endif
      COMMON     /GMWORK/ MWORK
      REAL       MWORK  ( IJKDIM )
*
      CHARACTER  HHEAD ( NDC )*(NCC)
      REAL       GDATA ( IJKDIM )
      CHARACTER  HHEADR( NDC )*(NCC)
*
      CHARACTER  HFILE( 1 ) *(NFILN)
      DATA       HFILE / '$GTTMPDIR/gtool.out' /
      DATA       IFILE / 50 /
      DATA       JFILE / 60 /
*
      CHARACTER  HAXX  *(NCC), HAXXR  *(NCC)
      CHARACTER  HAXY  *(NCC), HAXYR  *(NCC)
      CHARACTER  HAXZ  *(NCC), HAXZR  *(NCC)
*
      CHARACTER  X *(NCC), Y *(NCC), Z *(NCC) 
      DATA       X, Y, Z  / 3*' ' /
      INTEGER    XSTR, XEND
      DATA       XSTR, XEND / 0,0 /
      INTEGER    YSTR, YEND
      DATA       YSTR, YEND / 0,0 /
      INTEGER    ZSTR, ZEND
      DATA       ZSTR, ZEND / 0,0 /
      CHARACTER  REF    *(NFILN)
      DATA       REF      / ' ' /
      CHARACTER  OUT    *(NFILN)
      DATA       OUT    / '$GTTMPDIR/gtool.out' /
      LOGICAL    APND
      DATA       APND   / .FALSE. /
      LOGICAL    HELP
      DATA       HELP   / .FALSE. /
*
      NAMELIST  /OPTION/ X, Y, Z,
     &                   XSTR, XEND, YSTR, YEND, ZSTR, ZEND,
     &                   REF , OUT, APND, 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 GTSIZE ( HHEADR, IJKDIM )
      CALL GMSIZE ( IJKDIM  )
*
      CALL GURNTF ( HFILE( 1 ), OUT  , '$GTTMPDIR/gtool.in' )
*
      IF ( REF .NE. ' ' ) THEN
         CALL GFROPN ( IFILE, REF )
         CALL GFRDHD 
     O         ( HHEADR, IEODR,
     I           IFILE , 1     )
         WRITE ( 6,* ) '** AS REFERENCE'
         IF ( IEODR .EQ. 0 ) THEN
            CALL GHCGET( HHEADR, 'AITM1', HAXXR )
            CALL GHCGET( HHEADR, 'AITM2', HAXYR )
            CALL GHCGET( HHEADR, 'AITM3', HAXZR )
            CALL GUQSIZ
     I         ( HHEADR,
     O           IXSTRR, IXENDR, IXDIMR,
     O           IYSTRR, IYENDR, IYDIMR,
     O           IZSTRR, IZENDR, IZDIMR )
         ELSE
            WRITE ( 6,* ) ' no reference file'
            STOP
         ENDIF
         CALL GFCLSE( IFILE )
      ENDIF
      CALL GFROPN ( IFILE, HFILE( 1 ) )
      CALL GFOOPN ( JFILE,  OUT , APND )
*
      CALL GUNENV( OUT,'.',.FALSE. )
      IL=LENC(OUT)
      WRITE (6,*) 'output='//OUT(1:IL)
*
      II = 0
 1100 CONTINUE
         CALL   GFREAD
     O        ( HHEAD , GDATA , IEOD  ,
     I          IFILE , 1               )
*
         IF ( IEOD.EQ.0 ) THEN
*
            IF ( REF .NE. ' ' ) THEN
               CALL GHCGET( HHEAD , 'AITM1', HAXX  )
               CALL GHCGET( HHEAD , 'AITM2', HAXY  )
               CALL GHCGET( HHEAD , 'AITM3', HAXZ  )
               CALL GUQSIZ
     I         ( HHEAD ,
     O           IXSTR , IXEND , IXDIM ,
     O           IYSTR , IYEND , IYDIM ,
     O           IZSTR , IZEND , IZDIM  )
*
               IERR = 0
               IF ( HAXZ .EQ. ' ' ) THEN
                  IF ( HAXY .EQ. ' ' ) THEN
                     IF ( HAXX .EQ. ' ' ) THEN
                        X = HAXXR
                        Y = HAXYR
                        Z = HAXZR
                     ELSE IF ( HAXX .EQ. HAXXR ) THEN
                        Y = HAXYR
                        Z = HAXZR
                     ELSE IF ( HAXX .EQ. HAXYR ) THEN
                        X = HAXXR
                        Z = HAXZR
                     ELSE IF ( HAXX .EQ. HAXZR ) THEN
                        X = HAXXR
                        Y = HAXYR
                     ELSE 
                        IERR = 1
                     ENDIF
                  ELSE IF ( HAXY .EQ. HAXYR ) THEN
                     IF ( HAXX .EQ. HAXXR ) THEN
                        Z = HAXZR
                     ELSE
                        IERR = 1
                     ENDIF
                  ELSE IF ( HAXY .EQ. HAXZR ) THEN                   
                     IF ( HAXX .EQ. HAXXR ) THEN
                        Y = HAXYR
                     ELSE IF ( HAXX .EQ. HAXYR ) THEN
                        X = HAXXR
                     ELSE
                        IERR = 1
                     ENDIF
                  ELSE
                     IERR = 1
                  ENDIF
               ELSE
                  IF ( HAXX .NE. HAXXR .OR.
     &                 HAXY .NE. HAXYR .OR.
     &                 HAXZ .NE. HAXZR       ) THEN
                      IERR = 1
                  ENDIF
               ENDIF
*
               IF ( IERR .NE. 0 ) THEN
                  WRITE (6,*) 'cannot extend as reference'
                  WRITE (6,*) HAXX//'-'//HAXY//'-'//HAXZ
                  WRITE (6,*) HAXXR//'-'//HAXYR//'-'//HAXZR
                  STOP
               ENDIF
*
            ENDIF
*            
            IF ( X .NE. ' ' ) THEN
               CALL GMXEXT
     M               ( HHEAD , GDATA ,
     I                 X     , XSTR  , XEND  ,
     I                 ' '   , ' '             )
            ELSE IF ( XSTR .EQ. 0 .AND. XEND .EQ. 0 ) THEN
               CALL GMXEXP
     M               ( HHEAD , GDATA ,
     I                 ' ' , ' '       )
            ELSE
               CALL GMXRDC
     M               ( HHEAD , GDATA ,
     I                 XSTR  , XEND  ,
     I                 ' '   , ' '     )
            ENDIF
*
            IF ( Y .NE. ' ' ) THEN
               CALL GMYEXT
     M               ( HHEAD , GDATA ,
     I                 Y     , YSTR  , YEND  ,
     I                 ' '   , ' '             )
            ELSE IF ( YSTR .EQ. 0 .AND. YEND .EQ. 0 ) THEN
               CALL GMYEXP
     M               ( HHEAD , GDATA ,
     I                 ' ' , ' '       )
            ELSE
               CALL GMYRDC
     M               ( HHEAD , GDATA ,
     I                 YSTR  , YEND  ,
     I                 ' '   , ' '     )
            ENDIF
*
            IF ( Z .NE. ' ' ) THEN
               CALL GMZEXT
     M               ( HHEAD , GDATA ,
     I                 Z     , ZSTR  , ZEND  ,
     I                 ' '   , ' '             )
            ELSE IF ( ZSTR .EQ. 0 .AND. ZEND .EQ. 0 ) THEN
               CALL GMZEXP
     M               ( HHEAD , GDATA ,
     I                 ' ' , ' '       )
            ELSE
               CALL GMZRDC
     M               ( HHEAD , GDATA ,
     I                 ZSTR  , ZEND  ,
     I                 ' '   , ' '     )
            ENDIF
*
            CALL  GFWRIT
     I               ( HHEAD , GDATA ,
     I                 JFILE , 1     , 0       )
*
      GOTO 1100
         ENDIF
*
      STOP
      END
