*DECK TLMREG
      SUBROUTINE TLMREG(IPMAT ,IFTRK ,IPRINT,NSKTRK,NBTR  ,NDIM  ,
     >                  NREG  ,MXSEG ,NANGL ,NBDR  ,
     >                  NPLOTS,IPLOT ,IPLP  ,DANGLT,DVNOR)
*
*-----------------------------------------------------------------------
*
*Purpose:
* To generate the Matlab instruction for drawing the
* lines for the region selected.
*
*Copyright:
* Copyright (C) 2006 Ecole Polytechnique de Montreal.
*
*Author(s):
* C. Plamondon, G. Marleau
*
*Update(s):
* None.
*
*Reference:
* None.
*
*Parameters: input
* IPMAT   pointer to Matlab -m file.
* IFTRK   pointer to the TRACKING file.
* IPRINT  print level.
* NSKTRK  number of records to skip on track file before tracking
*         lines can be extracted.
* NBTR    numbre of tracks.
* NDIM    number of dimensions for problem.
* NREG    number of regions for problem.
* MAXSEG  maximum number of segments in a line.
* NANGL   number of direction for tracking.
* NBDR    number of direction for volume normalization.
* NPLOTS  number of plots.
* IPLOT   plot number being processed.
* IPLP    integer plot parameters.
* DANGLT  track directions.
* DVNOR   track normalization factor for regional volumes.
*
*----------
*
      IMPLICIT         NONE
*----
*  Subroutine arguments
*----
      INTEGER          IPMAT,IFTRK
      INTEGER          IPRINT,NSKTRK,NBTR,NDIM,NREG,MXSEG,
     >                 NANGL,NBDR,NPLOTS,IPLOT
      INTEGER          IPLP(6,NPLOTS)
      DOUBLE PRECISION DANGLT(NDIM,NANGL),
     >                 DVNOR(NREG,NBDR)
*----
*  Local parameters
*----
      INTEGER          IOUT
      CHARACTER        NAMSBR*6
      PARAMETER       (IOUT=6,NAMSBR='TLMREG')
*----
*  Local variables for tracking file
*----
      INTEGER          ILINE,IDUM,IANGL,NBSEG,NTLINE,ISEG,
     >                 IPLANE,IPTA2,IPTA3
      DOUBLE PRECISION WEIGHT,TORIG(3),TSTRT
*----
*  Other local variables
*----
      INTEGER          IREG,ILREG,IDIR,ISV,ITRACE,IPM
      DOUBLE PRECISION DXYZ(3,2),FLEN
      CHARACTER        TITLE*36
*----
*  Allocatable arrays
*----
      INTEGER, ALLOCATABLE, DIMENSION(:) :: NUMERO
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:) :: LENGTH
*----
*  Scratch storage allocation
*   NUMERO  region/surface identification number for segment.
*   LENGTH  segment length.
*----
      ALLOCATE(NUMERO(MXSEG),LENGTH(MXSEG))
*----
*  Processing starts:
*  print routine opening header if required
*  and initialize various parameters.
*----
      IF(IPRINT .GE. 1) THEN
        WRITE(IOUT,6000) NAMSBR
      ENDIF
*----
* Print IPMAT header
*----
      IREG=IPLP(2,IPLOT)
      WRITE(TITLE,'(A4,I4)')
     >'Reg=',IREG
      WRITE(IPMAT,7000) NAMSBR,TITLE,NREG
*----
*  Print matlab instructions for line segment
*----
      IF(IPRINT .GE. 10) THEN
        WRITE(IOUT,6002) IREG
      ENDIF
      DO ILINE=1,NSKTRK
        READ(IFTRK) IDUM
      ENDDO
      ILREG=0
*----
*  Scan over lines
*----
      DO ILINE=1,NBTR
        READ(IFTRK) IANGL,NBSEG,WEIGHT,
     >              (NUMERO(ISEG),ISEG=1,NBSEG),
     >              (LENGTH(ISEG),ISEG=1,NBSEG),
     >              NTLINE,IPLANE,IPTA2,IPTA3,
     >              (TORIG(IDIR),IDIR=1,NDIM),TSTRT
        DO IDIR=1,NDIM
          DXYZ(IDIR,1)=TORIG(IDIR)+DANGLT(IDIR,IANGL)*TSTRT
        ENDDO
        ITRACE=1
        DO ISEG=1,NBSEG
          ISV=NUMERO(ISEG)
          IF(ISV .GT. 0) THEN
            FLEN=LENGTH(ISEG)/DVNOR(ISV,1)
            IF(NBDR .GT. 1) FLEN=LENGTH(ISEG)/DVNOR(ISV,IANGL+1)
            DO IDIR=1,NDIM
              DXYZ(IDIR,2)=DXYZ(IDIR,1)+
     >        DANGLT(IDIR,IANGL)*FLEN
            ENDDO
            IF(IREG .EQ. ISV .AND. ITRACE .EQ. 1) THEN
              ILREG=ILREG+1
              IF(ILREG .EQ. 1) THEN
                WRITE(IPMAT,7002)
              ENDIF
              WRITE(IPMAT,7004)
     >        ((DXYZ(IDIR,IPM),IPM=1,2),IDIR=1,NDIM)
            ENDIF
            DO IDIR=1,NDIM
              DXYZ(IDIR,1)=DXYZ(IDIR,2)
            ENDDO
          ENDIF
        ENDDO
      ENDDO
*----
*  Write Matlab commands to trace lines
*----
      IF(ILREG .GE. 1) THEN
        WRITE(IPMAT,7003)
        IF(NDIM .EQ. 2) THEN
          WRITE(IPMAT,7012) IREG
        ELSE
          WRITE(IPMAT,7013) IREG
        ENDIF
*----
*  Change colour for next region
*----
        WRITE(IPMAT,7090)
        IF(IREG .NE. NREG) THEN
          IF(IPLP(1,IPLOT) .GT. 0) WRITE(IPMAT,7091)
        ENDIF
      ENDIF
      REWIND IFTRK
*----
*  Processing finished, return
*----
      IF(IPRINT .GE. 1) THEN
        WRITE(IOUT,6001) NAMSBR
      ENDIF
*----
*  Scratch storage deallocation
*----
      DEALLOCATE(LENGTH,NUMERO)
      RETURN
*----
*  Output formats
*----
 6000 FORMAT('(* Output from --',A6,'-- follows ')
 6001 FORMAT('   Output from --',A6,'-- completed *)')
 6002 FORMAT(' Processing lines for region = ',I8)
*----
*  Matlab .m file format
*----
 7000 FORMAT('%'/'% Output from ',A6/'%'
     >/7Htitle(',A36,3H');/9Hxcol=jet(,i5,2H);)
 7002 FORMAT('TLMIntegrationLines=[')
 7003 FORMAT(12X,'];')
 7004 FORMAT(6F18.10)
 7012 FORMAT('[m,n]=size(TLMIntegrationLines);'/
     > 'for i=1:m'/
     > '  TLMcolorset=line([TLMIntegrationLines(i,1),',
     >            'TLMIntegrationLines(i,2)],',
     >           '[TLMIntegrationLines(i,3),',
     >           'TLMIntegrationLines(i,4)]);'/
     > '  set(TLMcolorset,',8H'Color',,'xcol(',i5,',:));'/
     > 'end;')
 7013 FORMAT('[m,n]=size(TLMIntegrationLines);'/
     > 'for i=1:m'/
     > '  TLMcolorset=line([TLMIntegrationLines(i,1),',
     >            'TLMIntegrationLines(i,2)],',
     >           '[TLMIntegrationLines(i,3),',
     >            'TLMIntegrationLines(i,4)],',
     >           '[TLMIntegrationLines(i,5),',
     >            'TLMIntegrationLines(i,6)]);'/
     > '  set(TLMcolorset,',8H'Color',,'xcol(',i5,',:));'/
     > 'end;')
 7090 FORMAT('clear TLMIntegrationLines TLMcolorset ;')
 7091 FORMAT('pause ;')
      END
