C$DEBUG
C     TRANS2D
      IMPLICIT REAL*8 ( A-H,O-Z )
      CHARACTER*20 FNAME8,FNAME20,FNAME7
      REAL*8  , ALLOCATABLE::DB(:)
      INTEGER , ALLOCATABLE::IB(:)
      COMMON /RRR/ X(5000),Y(5000),H(5000),KX(5000,5),
     &          QV(5000),VVR(5000,2),DD(5000)
      CHARACTER*4  HED(20)
C      DIMENSION DB(1000000),IB(100000)
C      MTOTDB=1000000
C      MTOTIB=100000
C
      ISETID = 0
C      WRITE(*,*)'---  FILE NAME SET '
C      WRITE(*,*)'---    READ   WORK.LST/0'
C      WRITE(*,*)'---    INPUT FILE NAME/1'
      WRITE(*,*)'---  結果ファイル（図化用）の読み込みの設定 '
      WRITE(*,*)'---    WORK.LSTから読み込む/0'
      WRITE(*,*)'---    ファイル名を入力　　/1'
      READ(*,*) IFNM
C
      IF(IFNM .EQ. 1) THEN
C      WRITE(*,*)'--- INPUT the File Name. '
      WRITE(*,*)'--- 結果ファイル名の入力 '
      READ(*,'(A20)') FNAME7
      ELSE
        OPEN (98,FILE='WORK.LST', STATUS='OLD',FORM='FORMATTED')
        READ(98,'(A20)')FNAME7
        READ(98,'(A20)')FNAME7
        READ(98,'(A20)')FNAME7
        READ(98,'(A20)')FNAME7
        CLOSE(98)
      END IF
      OPEN(7,FILE=FNAME7,STATUS='OLD',FORM='UNFORMATTED')
C
C      OPEN(5,FILE=FNAME8,STATUS='OLD',FORM='FORMATTED')
C     OPEN(6,FILE='TEC3D.TXT',STATUS='UNKNOWN',FORM='FORMATTED')
        READ(7) HED
        READ(7) NUMNP
        READ(7) (X(I),Y(I),QL,BL,KB,I=1,NUMNP)
        READ(7) NUMEL
        READ(7)(KX(I,1),KX(I,2),KX(I,3),KX(I,4),
     &                              KX(I,5),ALF,I=1,NUMEL)
      WRITE(*,'(20A4)') (HED(I),I=1,20)
      WRITE(*,*) NUMNP,NUMEL
C
      WRITE(*,*)'--- グラフ用データの読み込み '
      WRITE(*,*)'---   　 データファイル名の入力'
      READ(*,'(A20)') FNAME20
      OPEN(9,FILE=FNAME20,STATUS='UNKNOWN',FORM='FORMATTED')
	READ(9,'(6I5)') MAXTIM,NUMHN,NUMQN,NUMVE1,NUMVE2
	IF(NUMHN.EQ.0)  NUMHN = 50
	IF(NUMQN.EQ.0)  NUMQN = 100
c	IF(NUMPN.EQ.0)  NUMPN = 50
	IF(NUMVE1.EQ.0)  NUMVE1 = 50
	IF(NUMVE2.EQ.0)  NUMVE2 = 50
c	IF(NUMAREA.EQ.0)  NUMAREA = 30
	IF(MAXTIM.EQ.0)  MAXTIM =100
	NUMPN = 1
C
C
      N1 =1                                                                                                     
      N2 =N1+NUMPN*MAXTIM             !pp                                          
      N3 =N2+NUMQN*MAXTIM             !qq                                          
      N4 =N3+NUMHN*MAXTIM             !hh
      N5 =N4+MAXTIM             !dtime
      N6 =N5+NUMVE1*2*MAXTIM 
      N7 =N6+NUMVE2*2*MAXTIM 
C
      M1=1                                                              
      M2=M1+NUMHN       !IHNODE
      M3=M2+NUMPN               !IPNODE
      M4=M3+NUMQN                !IQNODE
      M5=M4+NUMVE1            
      M6=M5+NUMVE2   
C
      MTOTDB= N7+100
      MTOTIB= M6+100
      ALLOCATE( DB(MTOTDB))
      ALLOCATE( IB(MTOTIB))
C       WRITE(*,*) ' N25=',N25
C       WRITE(*,*) ' DB=',MTOTDB
C       WRITE(*,*) ' M6=',M6
C       WRITE(*,*) ' IB=',MTOTIB     
C      IF( MTOTDB-N25 .LT.0 .OR. MTOTIB-M6 .LT.1 ) THEN
C        STOP 1234
C      END IF
C
      CALL MAIN( DB(N1),DB(N2),DB(N3),DB(N4),
     &            DB(N5),DB(N6), 
     &           IB(M1),IB(M2),IB(M3),IB(M4),IB(M5),
     &           NUMNP,NUMHN,NUMPN,NUMQN,NUMVE1,NUMVE2,
     &           MAXTIM,NUMEL)
      STOP
      END 
C 
      SUBROUTINE MAIN(PP,QQ,HH,DTIME,VVD,VVA,
     &                IHNODE,IPNODE,IQNODE,IV1ELEM,IV2ELEM,
     &           NUMNP,NUMHN,NUMPN,NUMQN,NUMVE1,NUMVE2,
     &           MAXTIM,NUMEL)
      IMPLICIT REAL*8 ( A-H,O-Z )
      COMMON /RRR/ X(5000),Y(5000),H(5000),KX(5000,5),
     &          QV(5000),VVR(5000,2),DD(5000)
      CHARACTER*4  HED(20)
      DIMENSION IHNODE(NUMHN),IPNODE(NUMPN),IQNODE(NUMQN),
     &  PP(NUMPN,MAXTIM),QQ(NUMQN,MAXTIM),HH(NUMHN,MAXTIM),
     &  DTIME(MAXTIM),VVD(NUMVE1,2,MAXTIM),VVA(NUMVE2,2,MAXTIM),
     &  IV1ELEM(NUMVE1),IV2ELEM(NUMVE2)
C
      ISTP = 0
      NUMPN1=0
C
      READ(9,'(I5)') NUMHN1  !head
	IF(NUMHN1 .GT. 0) READ(9,'(10I7)') (IHNODE(I),I=1,NUMHN1)
	NUMPN1 =0
c      READ(9,'(I5)') NUMPN1  !pres
c	IF(NUMPN1 .GT. 0) READ(9,'(10I7)') (IPNODE(I),I=1,NUMPN1)
       READ(9,'(I5)') NUMQN1 !Q
	IF(NUMQN1 .GT. 0) THEN
       READ(9,'(10I7)') (IQNODE(I),I=1,NUMQN1)
	END IF
       READ(9,'(I5)') NUMVE11  
	IF(NUMVE11 .GT. 0) THEN
       READ(9,'(10I7)') (IV1ELEM(I),I=1,NUMVE11)
	END IF
       READ(9,'(I5)') NUMVE21  
	IF(NUMVE21 .GT. 0) THEN
       READ(9,'(10I7)') (IV2ELEM(I),I=1,NUMVE21)
	END IF
	CLOSE(9)
C
  200 READ(7,END=100)LA,TIME
      READ(7)(KDD,H(I),QV(I),I=1,NUMNP)
      READ(7)(VVR(I,1),VVR(I,2),DD(I),I=1,NUMEL)
C======================================
       ISTP = ISTP + 1
	IF(ISTP .GT. MAXTIM) GO TO 100
C
       DTIME(ISTP) = TIME
C
      IF(NUMPN1 .GT. 0) THEN
       DO I=1,NUMPN1
	 IND = IPNODE(I)
	 IF(IND .GT. 0) PP(I,ISTP) = H(IND)
       END DO
	END IF
C
      IF(NUMHN1 .GT. 0) THEN
       DO I=1,NUMHN1
	 IND = IHNODE(I)
	 IF(IND .GT. 0) HH(I,ISTP) = H(IND) !+Y(IND)
       END DO
	END IF
C
      IF(NUMQN1 .GT. 0) THEN
       DO I=1,NUMQN1
	 IND = IQNODE(I)
	 IF(IND .GT. 0) QQ(I,ISTP) = QV(IND) 
       END DO       
       END IF
C
      IF(NUMVE11 .GT. 0) THEN
       DO I=1,NUMVE11
	 IND = IV1ELEM(I)
	 IF(IND .GT. 0) THEN
	   VVD(I,1,ISTP)= VVR(IND,1) 
	   VVD(I,2,ISTP)= VVR(IND,2) 
	 END IF
       END DO       
       END IF
C
      IF(NUMVE21 .GT. 0) THEN
       DO I=1,NUMVE21
	 IND = IV2ELEM(I)
	 IF(IND .GT. 0) THEN
	   VVA(I,1,ISTP)= VVR(IND,1) / DD(IND)
	   VVA(I,2,ISTP)= VVR(IND,2) / DD(IND)
	 END IF
       END DO       
       END IF
C======================================
      GO TO 200
  100 CONTINUE
C
      IF( NUMHN1 .GE. 1) THEN
      OPEN(10,FILE="GHTIME.CSV",STATUS='UNKNOWN',FORM='FORMATTED')
      WRITE(10,1010) (IHNODE(I),I=1,NUMHN1)
 1010 FORMAT(200(",",I8))
      DO I=1,ISTP
      WRITE(10,1000) DTIME(I),(HH(J,I),J=1,NUMHN1)
	END DO
 1000 FORMAT(E16.7,200(",",1PE14.5))
      CLOSE(10)
	END IF
C
      IF( NUMPN1 .GE. 1) THEN
      OPEN(11,FILE="GPTIME.CSV",STATUS='UNKNOWN',FORM='FORMATTED')
      WRITE(11,1010) (IPNODE(I),I=1,NUMPN1)
      DO I=1,ISTP
      WRITE(11,1000) DTIME(I),(PP(J,I),J=1,NUMPN1)
	END DO
      CLOSE(11)
	END IF
C
      IF( NUMQN1 .GE. 1) THEN
      OPEN(12,FILE="GQTIME.csv",STATUS='UNKNOWN',FORM='FORMATTED')
      WRITE(12,1010) (IQNODE(I),I=1,NUMQN1)
      DO I=1,ISTP
      WRITE(12,1000) DTIME(I),(QQ(J,I),J=1,NUMQN1)
	END DO
      CLOSE(12)
	END IF
C
C
      IF( NUMVE11 .GE. 1) THEN
      OPEN(12,FILE="GVDTIME.csv",STATUS='UNKNOWN',FORM='FORMATTED')
      WRITE(12,1010) (IV1ELEM(I),IV1ELEM(I),I=1,NUMVE11)  !VX,VY
      DO I=1,ISTP
      WRITE(12,1000) DTIME(I),((VVD(J,K,I),K=1,2),J=1,NUMVE11)
	END DO
      CLOSE(12)
	END IF
C
C
      IF( NUMVE21 .GE. 1) THEN
      OPEN(12,FILE="GVATIME.csv",STATUS='UNKNOWN',FORM='FORMATTED')
      WRITE(12,1010) (IV2ELEM(I),IV2ELEM(I),I=1,NUMVE21)  !VX,VY
      DO I=1,ISTP
      WRITE(12,1000) DTIME(I),((VVA(J,K,I),K=1,2),J=1,NUMVE21)
	END DO
      CLOSE(12)
	END IF
C
      RETURN
      END
