C
C Program PLGN to convert polygons described by names of the vertices
C into the same polygons described by indices of the vertices
C
C Version: 5.30
C Date: 1999, June 11
C
C Coded by: Ludek Klimes
C     Department of Geophysics, Charles University Prague,
C     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
C     E-mail: klimes@seis.karlov.mff.cuni.cz
C
C.......................................................................
C
C                                                    
C Description of the data files:
C
C Input data read from the * external unit:
C     The data are read in by the list directed input (free format)
C     using a single READ statetment.
C (1) 'SEP',/
C     'SEP'...String in apostrophes containing the name of the input
C             SEP parameter file with the input data specifying the
C             names of the input and output files.
C             Description of file SEP
C     Default: 'SEP'='grd.h'
C
C                                                     
C Data file 'SEP' has the form of the SEP (Stanford Exploration Project)
C parameter file:
C     All the data are specified in the form of PARAMETER=VALUE, e.g.
C     N1=50, with PARAMETER directly preceding = without intervening
C     spaces and with VALUE directly following = without intervening
C     spaces.  The PARAMETER=VALUE couple must be delimited by a space
C     or comma from both sides.
C     The PARAMETER string is not case-sensitive.
C     PARAMETER= followed by a space resets the default parameter value.
C     All other text in the input files is ignored.  The file thus may
C     contain unused data or comments without leading comment character.
C     Everything between comment character # and the end of the
C     respective line is ignored, too.
C     The PARAMETER=VALUE couples may be specified in any order.
C     The last appearance takes precedence.
C Data specifying input files:
C     VRTX='string'... Name of the file with vertices of the polygons.
C             Description of file VRTX
C             Default: VRTX='vrtx.out'
C     PLGNS='string'... Name of the file describing the polygons in
C             terms of the names of the vertices.
C             Description of file PLGNS
C             Default: PLGNS='plgns.out'
C Data specifying output file:
C     PLGN='string'... Name of the file describing the polygons in
C             terms of the indices of the vertices.
C             Description of file PLGN
C             Default: PLGN='plgn.out'
C
C                                                    
C Input file VRTX with the vertices:
C (1) None to several strings terminated by / (a slash)
C (2) For each vertex data (2.1):
C (2.1) 'NAME',X1,X2,X3,Z1,Z2,Z3,/
C     'NAME'... Name of the vertex.  Different vertices must have
C             different names.
C     X1,X2,X3... Coordinates of the vertex.
C     Z1,Z2,Z3... Normal to the surface at the vertex.
C     /...    None to several values terminated by a slash.
C (3) / or end of file.
C
C                                                   
C Input file PLGNS with the polygons:
C (1) For each polygon data (1.1):
C (1.1) 'VRTX1','VRTX2',...,'VRTXN',/
C     'VRTX1','VRTX2',...,'VRTXN'... Strings containing the names of N
C             vertices of the polygon.  The names must correspond to the
C             names in file VRTX.
C     /...    List of vertices must be terminated by a slash.
C (2) / or end of file.
C
C                                                    
C Input file PLGN with the polygons:
C (1) For each polygon data (1.1):
C (1.1) I1,I2,...,IN,/
C     I1,I2,...,IN... Integer indices of N vertices of the polygon.
C             The vertices in file VRTX are indexed by positive integers
C             according to their order.
C     /...    List of vertices is terminated by a slash.
C
C=======================================================================
C                                                   
C     String array for the vertex names:
      INTEGER MVRTX,MIVRTX
      PARAMETER (MVRTX=2048,MIVRTX=21)
      CHARACTER*12 VRTX(MVRTX)
      INTEGER IVRTX(MIVRTX)
      COMMON /VRTXC/ VRTX
C
C     Filenames and parameters:
      CHARACTER*80 FSEP,FVRTX,FPLGNS,FPLGN
      INTEGER LU,LU2,IUNDEF
      PARAMETER (LU=1,LU2=2)
C     Input data:
      CHARACTER*10 FORMAT
      CHARACTER*1 TEXT
C     Other variables:
      INTEGER NVRTX,NIVRTX,N,I,I1
C
C     MVRTX...Maximum number of vertices in the list of points plus
C             the number of vertices of the largest polygon plus 1.
C     MIVRTX..Maximum number of vertices of a single polygon increased
C             by 1.
C     NVRTX...Number of vertices in the list of points.
C
C.......................................................................
C
C     Reading main input data:
      WRITE (*,'(A)') '+PLGN: Enter input filename: '
      FSEP='grd.h'
      READ (*,*) FSEP
C
C     Reading input and output filenames:
      CALL RSEP1(LU,FSEP)
      CALL RSEP3T('VRTX',FVRTX,'vrtx.out')
      CALL RSEP3T('PLGNS',FPLGNS,'plgns.out')
      CALL RSEP3T('PLGN',FPLGN,'plgn.out')
C
C     Reading vertices:
      OPEN(LU,FILE=FVRTX)
      READ(LU,*) (TEXT,I=1,20)
      NVRTX=0
   10 CONTINUE
        NVRTX=NVRTX+1
        IF(NVRTX.GT.MVRTX) THEN
C         PLGN-01
          CALL ERROR('PLGN-01: Too small array VRTX')
C         Dimension MVRTX of array VRTX should be
C         increased.
        END IF
        VRTX(NVRTX)='$'
        READ(LU,*,END=11) VRTX(NVRTX)
        IF(VRTX(NVRTX).EQ.'$') THEN
          GO TO 11
        END IF
      GO TO 10
   11 CONTINUE
      NVRTX=NVRTX-1
      CLOSE(LU)
C
C     Checking vertex names:
      DO 15 I=1,NVRTX
        DO 14 I1=I+1,NVRTX
        IF(VRTX(I).EQ.VRTX(I1)) THEN
C         PLGN-02
          CALL ERROR('PLGN-02: Different vertices have the same name')
C         Strings identifying different vertices in the list of points
C         are equal within the considered length, see array
C         VRTX.
        END IF
   14   CONTINUE
   15 CONTINUE
C
C     Output format
      FORMAT='(99(I0,A))'
      I=INT(ALOG10(FLOAT(NVRTX)))+1
      FORMAT(6:6)=CHAR(ICHAR('0')+I)
C
C     Reading polygons:
      OPEN(LU,FILE=FPLGNS)
      OPEN(LU2,FILE=FPLGN)
      NIVRTX=MIN0(MVRTX,NVRTX+MIVRTX)
      DO 21 I=NVRTX+1,NIVRTX
        VRTX(I)='$'
   21 CONTINUE
   22 CONTINUE
        READ(LU,*,END=29) (VRTX(I),I=NVRTX+1,NIVRTX)
        IF(VRTX(NVRTX+1).EQ.'$') THEN
          GO TO 29
        END IF
        DO 25 I=NVRTX+1,NIVRTX
          IF(VRTX(I).EQ.'$') THEN
            N=I-NVRTX-1
            GO TO 27
          END IF
          DO 23 I1=1,NVRTX
            IF(VRTX(I).EQ.VRTX(I1)) THEN
              IVRTX(I-NVRTX)=I1
              GO TO 24
            END IF
   23     CONTINUE
C           PLGN-03
            CALL ERROR('PLGN-03: Vertex not found')
C           String identifying a vertex of a polygon does not match any
C           vertex in the list of points.
   24     CONTINUE
   25   CONTINUE
C         PLGN-04
          CALL ERROR('PLGN-04: Too small array VRTX or IVRTX')
C         Dimension MVRTX of array VRTX or
C         dimension MIVRTX of array IVRTX should be increased.
   27   CONTINUE
        WRITE(LU2,FORMAT) (IVRTX(I),' ',I=1,N-1),IVRTX(N),' /'
        DO 28 I=NVRTX+1,NVRTX+N
          VRTX(I)='$'
   28   CONTINUE
      GO TO 22
   29 CONTINUE

      CLOSE(LU)
      CLOSE(LU2)
C
      STOP
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     error.for
      INCLUDE 'sep.for'
C     sep.for
      INCLUDE 'length.for'
C     length.for
C
C=======================================================================
C