c
c     Copyright (c) 1999,2000, Compaq Computer Corporation
c
c     See the file "license.terms" for information on usage and redistribution
c     of this file, and for a DISCLAIMER OF ALL WARRANTIES.
c
      subroutine surf_set_default
      implicit none
c
      include 'const.h'
      include 'surf.cmn'
c
      integer i, j
c
      surf_pi = 4.0*ATAN(1.0)
c
      surf_global_xfactor = 1.0
      surf_global_yfactor = 1.0
      surf_global_xoffset = 0.0
      surf_global_yoffset = 0.0
      
c     Plot axis data
      surf_xaxis%min_flag = .FALSE.
      surf_yaxis%min_flag = .FALSE.
      surf_zaxis%min_flag = .FALSE.
      
      surf_xaxis%max_flag = .FALSE.
      surf_yaxis%max_flag = .FALSE.
      surf_zaxis%max_flag = .FALSE.
      
      surf_xaxis%tic_spacing = 0
      surf_yaxis%tic_spacing = 0
      surf_zaxis%tic_spacing = 0
      
      surf_xaxis%longtic_frequency = 5
      surf_yaxis%longtic_frequency = 5
      surf_zaxis%longtic_frequency = 5
      
      surf_xaxis%log = .FALSE.
      surf_yaxis%log = .FALSE.
      surf_zaxis%log = .FALSE.
      
      surf_xaxis%color = 1
      surf_yaxis%color = 1
      surf_zaxis%color = 1
      
      surf_xaxis%omit = .FALSE.
      surf_yaxis%omit = .FALSE.
      surf_zaxis%omit = .FALSE.
      
      surf_xaxis%nonumbering = .FALSE.
      surf_yaxis%nonumbering = .FALSE.
      surf_zaxis%nonumbering = .FALSE.
      
      surf_xaxis%label = ' '
      surf_yaxis%label = ' '
      surf_zaxis%label = ' '
      
c     Eye position      
      surf_eye%xposition = 5.0
      surf_eye%yposition = 5.0
      surf_eye%zposition = 5.0
      call surf_cartesian_to_spherical (surf_eye%xposition,
     +          surf_eye%yposition, surf_eye%zposition,
     +          surf_eye%radius,surf_eye%psi,surf_eye%theta)

c     View specification data 
      surf_view%xupvec   = 0.0     ! UP VECTOR is the Z axis
      surf_view%yupvec   = 0.0
      surf_view%zupvec   = 1.0
      
      surf_view%xnormal  = -1.0
      surf_view%ynormal  = -1.0
      surf_view%znormal  = -1.0

      surf_view%distance = 1.0

      surf_view%umin = -1.25
      surf_view%umax =  1.25
      surf_view%vmin = -1.25
      surf_view%vmax =  1.25

c
c     Use 100000 and -100000 to define clipping in the w direction....
c     NO actual clipping!
      surf_view%wmin     = -100000.
      surf_view%wmax     = 100000.

c     Other default
      surf_curve%line_type = 1
      surf_curve%line_color= 1
      surf_curve%xaxis = 2
      surf_curve%yaxis = 2
      surf_curve%zaxis = 2

c     Global default
      surf_global_yoffset = 0.0
      surf_global_xoffset = 0.0
      surf_global_xfactor = 1.0
      surf_global_yfactor = 1.0
c      
      surf_hidden_line_method = HIDDEN_LINES_WITH_HORIZON_FCN
c
      do j=1,4
         do i=1,4
            surf_unity_xform_3d(i,j) = 0.0
         end do
         surf_unity_xform_3d(j,j) = 1.0
      end do
c
      return
      end
      
      subroutine surf_plot(t)
      implicit none
c
      integer  t ! t=0 (SURF_NO_TITLE)   do not plot title
                 ! t=1 (SURF_WITH_TITLE) plot title
c
      include 'const.h'
      include 'surf.cmn'
c      
c     Write the title
      if (t .eq. SURF_WITH_TITLE) call surf_title_gks
c     Set the window and the view transformation and the 2d transformation
      call surf_set_view
      call surf_set_plotwindow_gks
      call surf_plot_axis_3d
      if( surf_curve%data_is_trigrid ) then
         call surf_plot_tridata_3d
      else
         call surf_plot_data_3d
      endif
c      call surf_username_gks
c
      return
      end

      subroutine surf_plot_axis_3d
      implicit none
c      
      include 'const.h'
      include 'sysdep.cmn'
      include 'surf.cmn'
      include 'io.cmn'
c
      real  xstart, ystart, zstart
      real  xend, yend, zend
      integer type
      
      if(.NOT. surf_xaxis%omit) then
         if( surf_curve%xaxis .eq. 1 ) then
            xstart = 0.0
            ystart = 0.0
            zstart = 0.0
            xend   = 1.0
            yend   = 0.0
            zend   = 0.0
         else if( surf_curve%xaxis .eq. 2 ) then
            xstart = 0.0
            ystart = 1.0
            zstart = 0.0
            xend   = 1.0
            yend   = 1.0
            zend   = 0.0
         else if( surf_curve%xaxis .eq. 3 ) then
            xstart = 0.0
            ystart = 1.0
            zstart = 1.0
            xend   = 1.0
            yend   = 1.0
            zend   = 1.0
         else if( surf_curve%xaxis .eq. 4 ) then
            xstart = 0.0
            ystart = 0.0
            zstart = 1.0
            xend   = 1.0
            yend   = 0.0
            zend   = 1.0
         else
            write(luttyo,100)
100         format(' ','*** ERROR... WRONG AXIS SELECTION')
         endif
         type = SURF_XAXIS_TYPE
         call surf_plot_axis_gks(surf_xaxis, xstart, ystart, zstart,
     +                           xend, yend, zend, type)
      endif
      
      if( .NOT. surf_yaxis%omit ) then
         if(surf_curve%yaxis .eq. 1 ) then
            xstart = 0.0
            ystart = 0.0
            zstart = 0.0
            xend   = 0.0
            yend   = 1.0
            zend   = 0.0
         else if(surf_curve%yaxis .eq. 2 ) then
            xstart = 1.0
            ystart = 0.0
            zstart = 0.0
            xend   = 1.0
            yend   = 1.0
            zend   = 0.0
         else if(surf_curve%yaxis .eq. 3 ) then
            xstart = 1.0
            ystart = 0.0
            zstart = 1.0
            xend   = 1.0
            yend   = 1.0
            zend   = 1.0
         else if(surf_curve%yaxis .eq. 4 ) then
            xstart = 0.0
            ystart = 0.0
            zstart = 1.0
            xend   = 0.0
            yend   = 1.0
            zend   = 1.0
         else
            write(luttyo,100)
         endif
         type = SURF_YAXIS_TYPE
         call surf_plot_axis_gks(surf_yaxis, xstart, ystart, zstart,
     +                           xend, yend, zend, type)
      endif
      
      if( .NOT. surf_zaxis%omit ) then
         if( surf_curve%zaxis .eq. 1 ) then
            xstart = 0.0
            ystart = 0.0
            zstart = 0.0
            xend   = 0.0
            yend   = 0.0
            zend   = 1.0
         else if( surf_curve%zaxis .eq. 2 ) then
            xstart = 1.0
            ystart = 0.0
            zstart = 0.0
            xend   = 1.0
            yend   = 0.0
            zend   = 1.0
         else if( surf_curve%zaxis .eq. 3 ) then
            xstart = 1.0
            ystart = 1.0
            zstart = 0.0
            xend   = 1.0
            yend   = 1.0
            zend   = 1.0
         else if( surf_curve%zaxis .eq. 4 ) then
            xstart = 0.0
            ystart = 1.0
            zstart = 0.0
            xend   = 0.0
            yend   = 1.0
            zend   = 1.0
         else
            write(luttyo,100)
         endif
         type = SURF_ZAXIS_TYPE
         call surf_plot_axis_gks(surf_zaxis, xstart, ystart, zstart,
     +                           xend, yend, zend, type)
         endif
c
      return
      end
c
      subroutine surf_num_to_string(string, number)
      implicit none
c
c     num_to_string() converts a number to a left justified string
c
      real   number
      character*(*) string
c
      integer i, nlen
      real    temp
c      
      if( number .eq. 0.0 ) then
         string = '0'
         return
      endif
c
      temp = abs(number)
      if(       temp .ge. 10000.0 ) then
           write(string,100) number
100        format(1pe12.2)
      else if ( temp .ge. 1000.0 ) then
            write (string,110) number
110         format(f12.0)
      else if ( temp .ge. 100.0 ) then
            write (string,120) number
120         format(f12.0)
      else if ( temp .ge. 10.0 ) then
              write (string,130) number
130           format(f12.1)
      else if ( temp .ge. 1.0 ) then
              write (string,140) number
140           format(f12.2)
      else if ( temp .ge. 0.1 ) then
              write (string,150) number
150           format(f12.2)
      else if ( temp .ge. 0.01 ) then
               write (string,160) number 
160            format(f12.3)
      else if ( temp .ge. 0.001 ) then
               write (string,170) number
170            format(f12.4)
      else
               write (string,180) number
180            format(1pe12.2)
      endif
c
c     Remove any trailing zeroes
      nlen = LEN(string)
      do i=nlen,1,-1
         if(string(i:i) .eq. '0' ) string(i:i) = ' '
         if(string(i:i) .ne. ' ' ) goto 200
      end do
200   continue
c
      return
      end
c
      subroutine surf_cartesian_to_spherical(x,y,z,radius,psi,theta)
      implicit none
c
c     Cartesian_to_spherical transforms from a cartesian coordinate system
c     to a spherical coordinate system
c
      real x,y,z
      real radius,psi,theta
c
      include 'const.h'
      include 'surf.cmn'
c
      real  d2
      real  surf_distance_3d
c
      radius = surf_distance_3d (x,y,z)
      d2     = surf_distance_3d (x,y,0.0)
      psi    = ACOS(x/radius)*180.0/surf_pi
      theta  = ACOS(x/d2)*180.0/surf_pi
c
      return
      end
c
      subroutine surf_spherical_to_cartesian(x,y,z,radius,psi,theta)
      implicit none
c
c     Spherical_to_cartesian transforms from a spherical  coordinate system
c     to a cartesian coordinate system
      real x,y,z
      real radius,psi,theta
c
      include 'const.h'
      include 'surf.cmn'
c
      real  d2
c
      z  = radius * COS(psi*surf_pi/180)
      d2 = radius * SIN(psi*surf_pi/180)
      x  = d2 * COS(theta*surf_pi/180)
      y  = d2 * SIN(theta*surf_pi/180)
c
      return
      end
