C
C Program TSURF to convert GOCAD triangulated surfaces into a file with
C points and a file with triangles
C
C Version: 5.90
C Date: 2004, December 9
C
C Coded by: Ludek Klimes
C     Department of Geophysics, Charles University Prague,
C     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
C     http://sw3d.cz/staff/klimes.htm
C
C.......................................................................
C                                                    
C Description of data files:
C
C Input data read from the standard input device (*):
C     The data are read by the list directed input (free format) and
C     consist of a single string 'SEP':
C     'SEP'...String in apostrophes containing the name of the input
C             SEP parameter or history file with the input data.
C     No default, 'SEP' must be specified and cannot be blank.
C
C                                                     
C Input data file 'SEP':
C     File 'SEP' has the form of the SEP
C     parameter file.  The parameters, which do not differ from their
C     defaults, need not be specified in file 'SEP'.
C Data specifying input files:
C     TSURF='string'... Name of the file with GOCAD triangulated
C             surfaces.
C             Description of file TSURF
C             No default, TSURF must be specified.
C Data specifying output files:
C     VRTX='string'... Name of the file with vertices of the polygons.
C             If the filename is blank, the file is not generated.
C             Description of file VRTX
C             Default: VRTX='vrtx.out'
C     PLGNS='string'... Name of the file describing the triangles.
C             If the filename is blank, the file is not generated.
C             Description of file PLGNS
C             Default: PLGNS='trgl.out'
C Optional data to shift the triangles:
C     SHIFT1=real, SHIFT2=real, SHIFT3=real... All triangle vertices
C             will be shifted by vector (SHIFT1,SHIFT2,SHIFT3).
C             SHIFT1=0., SHIFT2=0., SHIFT3=0.
C Optional parameters specifying the form of the real quantities
C written in the output formatted files:
C     MINDIG,MAXDIG=positive integers ... See the description in file
C             forms.for.
C
C                                                   
C Input file TSURF with the GOCAD triangulated surfaces:
C     The file consists of lines (1) and lines (2):
C     For each vertex:
C (1) VRTX,NAME,X1,X2,X3
C     VRTX... String VRTX without apostrophes.
C     NAME... Index of the vertex (integer).
C     X1,X2,X3... Coordinates of the vertex.
C     For each triangle:
C (2) TRGL,NAME1,NAME2,NAME3
C     TRGL... String TRGL without apostrophes.
C     NAME1,NAME2,NAME3... Indices of the vertices of the triangle
C             (integers).
C
C                                                    
C Output file VRTX with the vertices:
C (1) / (a slash)
C (2) For each vertex data (2.1):
C (2.1) 'NAME',X1,X2,X3,/
C     'NAME'..Name of the vertex.  String in apostrophes containing
C             the index of the vertex.
C     X1,X2,X3... Coordinates of the vertex.
C     /...    A slash.
C (3) / (a slash followed by a comment)
C (4) / (a slash followed by a comment)
C
C                                                   
C Output file PLGNS with the triangles:
C     For each triangle data (1):
C (1) 'NAME1','NAME2','NAME3',/
C     'NAME1','NAME2','NAME3'... Names of the vertices.  Strings in
C             apostrophes containing the indices of the vertices.
C     /...    List of vertices is terminated by a slash.
C
C=======================================================================
C
      CHARACTER*80 FILE,FVRTX,FTRGL
      INTEGER LU1,LU2,LU3,LU4
      PARAMETER (LU1=1,LU2=2,LU3=3,LU4=4)
      CHARACTER*4  KEY
      CHARACTER*76 LINE
      CHARACTER*34 FORMAT
      INTEGER I0,I1,I2,I3
      REAL SHIFT1,SHIFT2,SHIFT3
      REAL X(3),X1,X2,X3,X1MIN,X1MAX,X2MIN,X2MAX,X3MIN,X3MAX
      EQUIVALENCE (X(1),X1),(X(2),X2),(X(3),X3)
C
C.......................................................................
C
C     Reading main input data:
      WRITE(*,'(A)') '+TSURF: Enter input filename: '
      FILE=' '
      READ (*,*) FILE
      IF(FILE.EQ.' ') THEN
C       TSURF-01
        CALL ERROR('TSURF-01: No input file specified')
C       Input file in the form of the SEP (Stanford Exploration Project)
C       parameter or history file must be specified.
C       There is no default filename.
      END IF
      WRITE(*,'(A)') '+TSURF: Working...            '
C
C     Opening input and output files:
      CALL RSEP1(LU1,FILE)
      CALL RSEP3T('TSURF',FILE ,' ')
      IF(FILE.EQ.' ') THEN
C       TSURF-02
        CALL ERROR('TSURF-02: No GOCAD file specified')
C       Input file TSURF with the GOCAD triangulated surfaces must be
C       specified.
C       There is no default filename.
      END IF
      CALL RSEP3T('VRTX' ,FVRTX,'vrtx.out')
      CALL RSEP3T('PLGNS',FTRGL,'trgl.out')
      OPEN(LU1,FILE=FILE,STATUS='OLD')
      IF(FVRTX.NE.' ') THEN
        OPEN(LU2,FILE=FVRTX)
        WRITE(LU2,'(A)') '/'
      END IF
      IF(FTRGL.NE.' ') THEN
        OPEN(LU3,FILE=FTRGL)
      END IF
C
C     Optional shift:
      CALL RSEP3R('SHIFT1',SHIFT1,0.)
      CALL RSEP3R('SHIFT2',SHIFT2,0.)
      CALL RSEP3R('SHIFT3',SHIFT3,0.)
C
C     Preparation for the loop over lines of the input GOCAD file
      OPEN(LU4,STATUS='SCRATCH')
      FORMAT(1:10)='(A,I6.6,A,'
      NPTS=0
C
C     Loop over lines of the input GOCAD file with the TSURF object
   10 CONTINUE
        READ(LU1,'(2A)',END=90) KEY,LINE
        WRITE(LU4,'(A)') LINE
        BACKSPACE(LU4)
        IF(KEY.EQ.'VRTX') THEN
C         Writing the vertex
          IF(FVRTX.NE.' ') THEN
            READ(LU4,*) I0,X1,X2,X3
            X1=X1+SHIFT1
            X2=X2+SHIFT2
            X3=X3+SHIFT3
            IF(NPTS.EQ.0) THEN
              X1MIN=X1
              X2MIN=X2
              X3MIN=X3
              X1MAX=X1
              X2MAX=X2
              X3MAX=X3
            ELSE
              X1MIN=AMIN1(X1,X1MIN)
              X2MIN=AMIN1(X2,X2MIN)
              X3MIN=AMIN1(X3,X3MIN)
              X1MAX=AMAX1(X1,X1MAX)
              X2MAX=AMAX1(X2,X2MAX)
              X3MAX=AMAX1(X3,X3MAX)
            END IF
            NPTS=NPTS+1
            CALL FORM2(3,X,X,FORMAT(11:34))
            WRITE(LU2,FORMAT) '''',I0,''' ',X1,' ',X2,' ',X3,' /'
          END IF
        ELSE IF(KEY.EQ.'TRGL') THEN
C         Writing the triangle
          IF(FTRGL.NE.' ') THEN
            READ(LU4,*) I1,I2,I3
            WRITE(LU3,'(4(A,I6.6))')'''',I1,''' ''',I2,''' ''',I3,''' /'
          END IF
        ELSE
C         TSURF-03
          CALL ERROR('TSURF-03: Unexpected key string')
C         Only key strings VRTX and TRGL may be used in the input GOCAD
C         file.
        END IF
      GO TO 10
C
C     Closing input and output files:
   90 CONTINUE
      CLOSE(LU1)
      IF(FVRTX.NE.' ') THEN
        FORMAT(1:10)='(A,       '
        X1=X1MIN
        X2=X2MIN
        X3=X3MIN
        CALL FORM2(3,X,X,FORMAT(11:34))
        WRITE(LU2,FORMAT) '/ Minimum coordinates: ',X1,' ',X2,' ',X3
        X1=X1MAX
        X2=X2MAX
        X3=X3MAX
        CALL FORM2(3,X,X,FORMAT(11:34))
        WRITE(LU2,FORMAT) '/ Maximum coordinates: ',X1,' ',X2,' ',X3
        CLOSE(LU2)
      END IF
      IF(FTRGL.NE.' ') THEN
        CLOSE(LU3)
      END IF
      CLOSE(LU4)
      WRITE(*,'(A)') '+TSURF: Done.                 '
      STOP
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     error.for
      INCLUDE 'sep.for'
C     sep.for
      INCLUDE 'forms.for'
C     forms.for
      INCLUDE 'length.for'
C     length.for
C
C=======================================================================
C