!**************************************************************
!
!   GRAPHIC 3D : UNIVERSAL ROUTINE
!
!**************************************************************

SUBROUTINE TXGRUR(GX,GTX,GYL,NRMAX,NGT,NGTM)!,STR,KV,INQ)

  implicit none
  integer(4),                  intent(in) :: NRMAX, NGT, NGTM!, INQ
  real(4), dimension(0:NRMAX), intent(in) :: GX
  real(4), dimension(0:NGT),   intent(in) :: GTX
  real(4), dimension(0:NRMAX,0:NGTM), intent(in) :: GYL
!  CHARACTER(LEN=80),INTENT(IN):: STR, KV
  integer(4) :: INQ
  CHARACTER(LEN=80) :: STR, KV
  REAL(4) :: GX1, GX2, GY1, GY2

  STR = '@3D Graphic@'
  KV  = ''
  INQ = 0

  GX1=3.0
  GX2=18.0
  GY1=2.0
  GY2=17.0

  CALL PAGES
  CALL GSGLENABLELIGHTING
  CALL TXGR3D(GX1,GX2,GY1,GY2,GX,GTX,GYL,NRMAX+1,NRMAX+1,NGT+1,STR,KV,2+INQ)
  CALL PAGEE

  RETURN
END SUBROUTINE TXGRUR

!***********************************************************
!
!   SUBPROGRAM FOR 3D PROFILE
!
!***********************************************************

SUBROUTINE TXGR3D(GX1,GX2,GY1,GY2,GX,GY,GZ,NXM,NXMAX,NYMAX,STR,KV,MODE)

  implicit none
  REAL(4),    INTENT(IN) :: GX1, GX2, GY1, GY2
  INTEGER(4), INTENT(IN) :: NXM, NXMAX, NYMAX, MODE
  REAL(4), DIMENSION(NXMAX),     INTENT(IN) :: GX
  REAL(4), DIMENSION(NYMAX),     INTENT(IN) :: GY
  REAL(4), DIMENSION(NXM,NYMAX), INTENT(IN) :: GZ
  INTEGER(4) :: I
  REAL(4) :: GXMIN, GXMAX, GYMIN, GYMAX, GZMIN, GZMAX, GSXMIN, GSXMAX, GSYMIN, GSYMAX, &
       &     GSZMIN, GSZMAX, GSTEPX, GSTEPY, GSTEPZ, GXL, GYL, GZL, GPHI, GTHETA, &
       &     GRADIUS, GOX, GOY, GOZ
  CHARACTER(LEN=80) :: STR, KV
  CHARACTER(LEN=80) :: KT
  CHARACTER(LEN=1)  :: KDL
  EXTERNAL R2G2B_Gat0

  CALL SETFNT(32)
  CALL SETCHS(0.3,0.0)
  CALL SETLIN(0,0,7)
  KDL=STR(1:1)
  I=2
1 IF(STR(I:I).EQ.KDL.OR.I.EQ.80) GOTO 2
  KT(I-1:I-1)=STR(I:I)
  I=I+1
  GOTO 1

2 CALL MOVE(GX1,GY2+0.2)
  CALL TEXT(KT,I-2)

  CALL GMNMX2(GZ,NXM,1,NXMAX,1,1,NYMAX,1,GZMIN,GZMAX)
  IF(ABS(GZMAX-GZMIN).LT.1.D-6) THEN
     GZMIN=GZMIN-0.999E-6
     GZMAX=GZMAX+1.000E-6
  ENDIF

  IF(MOD(MODE,2).EQ.0) THEN
     IF(GZMIN.GE.0.0) THEN
        GZMIN=0.0
     ELSEIF(GZMAX.LE.0.0) THEN
        GZMAX=0.0
     ENDIF
  ENDIF

  CALL GMNMX1(GX,1,NXMAX,1,GXMIN,GXMAX)
  CALL GMNMX1(GY,1,NYMAX,1,GYMIN,GYMAX)
  IF(ABS(GXMAX-GXMIN).LT.1.D-6) THEN
     GXMIN=GXMIN-0.999E-6
     GXMAX=GXMAX+1.000E-6
  ENDIF

  !      GXMIN=GX(1)
  !      GXMAX=GX(NXMAX)

  CALL GQSCAL(GXMIN,GXMAX,GSXMIN,GSXMAX,GSTEPX)
  CALL GQSCAL(GYMIN,GYMAX,GSYMIN,GSYMAX,GSTEPY)
  CALL GQSCAL(GZMIN,GZMAX,GSZMIN,GSZMAX,GSTEPZ)
  ! Correct the misestimation by GQSCAL
  IF(GZMAX > GSZMAX) GSZMAX = GSZMAX + GSTEPZ
  IF(GZMIN < GSZMIN) GSZMIN = GSZMIN - GSTEPZ

  ! Origin of the vertical axis is forced to set zero
  !   if either all the values are positive or negative.
  IF(MOD(MODE,2).EQ.0) THEN
     IF(GZMIN.GE.0.0) THEN
        GSZMIN=0.0
     ELSEIF(GZMAX.LE.0.0) THEN
        GSZMAX=0.0
     ENDIF
  ENDIF
  GZMIN=GSZMIN
  GZMAX=GSZMAX
  IF(MOD(MODE/4,2).EQ.1) THEN
     CALL CHMODE
     WRITE(6,*) '## TXGR : XMIN,XMAX,YMIN,YMAX = ',GXMIN,GXMAX,GYMIN,GYMAX
     READ(5,*) GXMIN,GXMAX,GYMIN,GYMAX
     CALL GRMODE
  ENDIF
  CALL GQSCAL(GXMIN,GXMAX,GSXMIN,GSXMAX,GSTEPX)
  CALL GQSCAL(GYMIN,GYMAX,GSYMIN,GSYMAX,GSTEPY)
  CALL GQSCAL(GZMIN,GZMAX,GSZMIN,GSZMAX,GSTEPZ)

  !      IF(GXMIN*GXMAX.LE.0.0) THEN
  !         GXORG=0.0
  !      ELSE
  !         GXORG=GSXMIN
  !      ENDIF
  !      IF(GYMIN*GYMAX.LE.0.0) THEN
  !         GYORG=0.0
  !      ELSE
  !         GYORG=GSYMIN
  !      ENDIF

  CALL GDEFIN(GX1,GX2,GY1,GY2,GSXMIN,GSXMAX,GSYMIN,GSYMAX)
  GXL=10.0*1.5
  GYL=20.0*1.5
  GZL=10.0*1.5
  CALL GDEFIN3D(GXL,GYL,GZL,GSXMIN,GSXMAX,GYMIN,GYMAX,GSZMIN,GSZMAX)

  ! viewpoint
  GPHI=-65.0
  GTHETA=45.0
  GRADIUS=35.0

  GOX=0.5*(GSXMIN+GSXMAX)
  GOY=0.5*(GYMIN+GYMAX)
  GOZ=0.5*(GSZMIN+GSZMAX)
  CALL GVIEW3D(GPHI,GTHETA,GRADIUS,GOX,GOY,GOZ)
  CALL SETCHS(0.3,0.0)
  CALL SETLIN(0,0,4)

  !      CALL GSCALE3DX(GSXMIN,GSTEPX,0.3,0)
  !      CALL GSCALE3DY(GT(1),GSTEPT,0.3,0)
  !      CALL GSCALE3DZ(GSYMIN,GSTEPY,0.3,10)
  !      CALL GVALUE3DX(GSXMIN,GSTEPX,1,1)
  !      CALL GVALUE3DY(GT(1),GSTEPT,1,1)
  !      CALL GVALUE3DZ(GSYMIN,GSTEPY,11,-2)
  CALL GSCALE3DX(GSXMIN,GSTEPX,0.3,0)
  CALL GSCALE3DY(GSYMIN,GSTEPY,0.3,0)
  CALL GSCALE3DZ(GSZMIN,GSTEPZ,0.3,10)
  CALL GVALUE3DX(GSXMIN,GSTEPX,1,1)
  CALL GVALUE3DY(GSYMIN,GSTEPY,1,3)
  CALL GVALUE3DZ(GSZMIN,GSTEPZ,11,-2)

  CALL Set3DTextBaseLine(0.0, 1.0, 0.0, -1.0, 0.0, 0.0)
  CALL GTEXTX3D(GSXMAX+0.15*(GSXMAX-GSXMIN),0.5*(GY(1)+GY(NYMAX)), &
       &              GSZMIN,'@TIME (sec)@',2)
  CALL Set3DTextBaseLine(0.0, 1.0, 0.0, -1.0, 0.0, 0.0)
  CALL GTEXTX3D(0.5*(GSXMIN+GSXMAX),GY(1)+0.1*(GY(1)-GY(NYMAX)), &
       &              GSZMIN,'@R@',2)
  CALL Set3DTextBaseLine(0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  CALL GTEXTX3D(GSXMIN,GY(1)+0.05*(GY(1)-GY(NYMAX)), &
       &              GSZMAX+0.1*(GSZMAX-GSZMIN),KV,2)

  CALL PERS3D2(GZ,GX,GY,NXM,NXMAX,NYMAX,-27,R2G2B_Gat0)
  CALL GAxis3D(0)
  CALL GDrawBack3D(0.5, 0.5, 0.5)

  CALL SETLIN(0,0,4)
  RETURN
END SUBROUTINE TXGR3D
