!---------------------------------------------------------------------!
!
      PROGRAM STOMPTOLEAPFROG
!
!--- This program reads a two dimensional plot file in STOMP
!    coordinates and translates the results into a three dimensional
!    x,y,z,temperature, file in Homestake Mine coordinates.
!
!    Written by Mark D. White, PNNL, 7 January 2019.
!
!    To compile with the gnu compiler issue the following command: 
!    gfortran 2dto3d.F -o 2dto3d.x
!
!    Usage: ./2dto3d.x [input grid file] [output grid file] 
!
!----------------------Type Declarations-------------------------------!
!
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER (I-N)
!
      REAL*8, DIMENSION(:), ALLOCATABLE :: XP,YP,ZP,T
      REAL*8, DIMENSION(:), ALLOCATABLE :: XPHS,YPHS,ZPHS,THS
      REAL*8, DIMENSION(8) :: XE,YE,ZE
!
      CHARACTER(512)                                  :: CHDUM
      CHARACTER(132)                                  :: CDUM
      CHARACTER(132)                                  :: INFILE
      CHARACTER(132)                                  :: OUTFILE
!
!----------------------Executable Lines--------------------------------!
!
!     Get inputs from command line or by prompting the user
!
      NCLA = IARGC()
      IF( NCLA.EQ.2 ) THEN
        CALL GETARG( 1,CDUM )
        CALL LCASE( CDUM )
        INFILE = CDUM
        CALL GETARG( 2,CDUM )
        CALL LCASE( CDUM )
        OUTFILE = CDUM
      ELSE
        WRITE(6,'(/,A,/)')'Enter input STOMP plot filename: '
        READ(5,*) INFILE
        WRITE(6,'(/,A,/)')'Enter output LeapFrog xyz filename: '
        READ(5,*) OUTFILE
      ENDIF
!  
!     Open input STOMP plot file
!
      INFILE = TRIM(ADJUSTL(INFILE))
      WRITE(6,'(2A)') 'Opening input ',TRIM(INFILE)
      OPEN(UNIT=55, FILE=INFILE, STATUS='OLD', FORM='FORMATTED')
!  
!     Open output LeapFrog xyz file
!
      OUTFILE = TRIM(ADJUSTL(OUTFILE))
      WRITE(6,'(2A)') 'Creating output ',TRIM(OUTFILE)
      OPEN(UNIT=66, FILE=OUTFILE, FORM='FORMATTED')
      CLOSE(UNIT=66,STATUS='DELETE')
      OPEN(UNIT=66, FILE=OUTFILE, STATUS='NEW', FORM='FORMATTED')
!
!     Read input STOMP plot file
!
      DO
        READ(55,'(A)') CHDUM
        IF( INDEX(CHDUM(1:),'Number of Field Nodes').NE.0 ) EXIT
      ENDDO
      READ( CHDUM(25:34),'(I9)' ) NFLD
      ALLOCATE( XP(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: XP'
        STOP
      ENDIF
      ALLOCATE( YP(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: YP'
        STOP
      ENDIF
      ALLOCATE( ZP(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: ZP'
        STOP
      ENDIF
      ALLOCATE( T(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: T'
        STOP
      ENDIF
      ALLOCATE( XPHS(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: XPHS'
        STOP
      ENDIF
      ALLOCATE( YPHS(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: YPHS'
        STOP
      ENDIF
      ALLOCATE( ZPHS(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: ZPHS'
        STOP
      ENDIF
      ALLOCATE( THS(1:NFLD),STAT=ISTAT )
      IF( ISTAT.NE.0 ) THEN
        PRINT *,'Allocation Error: THS'
        STOP
      ENDIF
      DO
        READ(55,'(A)') CHDUM
        IF( INDEX(CHDUM(1:),'X-Direction Nodal Vertices').NE.0 ) EXIT
      ENDDO
      DO N = 1,NFLD
        READ (55,'(8(1PE16.9,1X))') (XE(M),M=1,8)
        YP(N) = 0.D+0
        DO M = 1,8
          YP(N) = YP(N) + XE(M)
        ENDDO
        YP(N) = 3.28084D+0*YP(N)/8.D+0
      ENDDO
      DO
        READ(55,'(A)') CHDUM
        IF( INDEX(CHDUM(1:),'Z-Direction Nodal Vertices').NE.0 ) EXIT
      ENDDO
      DO N = 1,NFLD
        READ (55,'(8(1PE16.9,1X))') (ZE(M),M=1,8)
        ZP(N) = 0.D+0
        DO M = 1,8
          ZP(N) = ZP(N) + ZE(M)
        ENDDO
        ZP(N) = 3.28084D+0*ZP(N)/8.D+0
        ZP(N) = ZP(N) + 4850.D+0
      ENDDO
      DO
        READ(55,'(A)') CHDUM
        IF( INDEX(CHDUM(1:),'Temperature').NE.0 ) EXIT
      ENDDO
      NC = NFLD/10 - 1
      DO M = 1,NC
        M1 = (M-1)*10 + 1
        M2 = M1 + 9
        READ(55,*) (T(N),N=M1,M2)
      ENDDO
      M1 = NC*10 + 1
      M2 = NFLD
      READ(55,*) (T(N),N=M1,M2)
!
!     Write output grid
!
      XZHS = 2735.D+0
      YZHS = -4326.D+0
      ZZHS = 376.8D+0
      THETA = 57.2D+0*0.017453D+0
      DO J = 1,40
        XPX = (J-1)*5.D+0 - 100.D+0
        DO N = 1,NFLD
          XHS = XPX*COS(THETA) - YP(N)*SIN(THETA) + XZHS
          YHS = XPX*SIN(THETA) + YP(N)*COS(THETA) + YZHS
          ZHS = ZP(N) + ZZHS
          WRITE(66,'(4(PE12.5,1X))') XHS,YHS,ZHS,T(N)
        ENDDO
      ENDDO
!  
!     End of STOMPTOLEAPFROG
!
      STOP
      END


!----------------------Function----------------------------------------!
!
      FUNCTION ICOUNT( I )
!
!-------------------------Disclaimer-----------------------------------!
!
!     This material was prepared as an account of work sponsored by
!     an agency of the United States Government. Neither the
!     United States Government nor the United States Department of
!     Energy, nor Battelle, nor any of their employees, makes any
!     warranty, express or implied, or assumes any legal liability or
!     responsibility for the accuracy, completeness, or usefulness
!     of any information, apparatus, product, software or process
!     disclosed, or represents that its use would not infringe
!     privately owned rights.
!
!----------------------Acknowledgement---------------------------------!
!
!     This software and its documentation were produced with Government
!     support under Contract Number DE-AC06-76RLO-1830 awarded by the
!     United Department of Energy. The Government retains a paid-up
!     non-exclusive, irrevocable worldwide license to reproduce,
!     prepare derivative works, perform publicly and display publicly
!     by or for the Government, including the right to distribute to
!     other Government contractors.
!
!---------------------Copyright Notices--------------------------------!
!
!            Copyright Battelle Memorial Institute, 1996
!                    All Rights Reserved.
!
!----------------------Description-------------------------------------!
!
!     Count the number of digits in an integer variable.
!
!----------------------Authors-----------------------------------------!
!
!     Written by MD White, Battelle, PNL, March, 1993.
!
!----------------------Implicit Double Precision-----------------------!
!
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER (I-N)
!
!----------------------Executable Lines--------------------------------!
!
      IC = ABS(I)
      ICOUNT = 0
   10 CONTINUE
      ICOUNT = ICOUNT + 1
      IC = IC/10
      IF( IC.GT.0 ) GOTO 10
      IF( I.LT.0 ) ICOUNT = ICOUNT + 1
!
!---  End of ICOUNT group
!
      RETURN
      END

!----------------------Subroutine--------------------------------------!
!
      SUBROUTINE LCASE( CHDUM )
!
!-------------------------Disclaimer-----------------------------------!
!
!     This material was prepared as an account of work sponsored by
!     an agency of the United States Government. Neither the
!     United States Government nor the United States Department of
!     Energy, nor Battelle, nor any of their employees, makes any
!     warranty, express or implied, or assumes any legal liability or
!     responsibility for the accuracy, completeness, or usefulness
!     of any information, apparatus, product, software or process
!     disclosed, or represents that its use would not infringe
!     privately owned rights.
!
!----------------------Acknowledgement---------------------------------!
!
!     This software and its documentation were produced with Government
!     support under Contract Number DE-AC06-76RLO-1830 awarded by the
!     United Department of Energy. The Government retains a paid-up
!     non-exclusive, irrevocable worldwide license to reproduce,
!     prepare derivative works, perform publicly and display publicly
!     by or for the Government, including the right to distribute to
!     other Government contractors.
!
!---------------------Copyright Notices--------------------------------!
!
!            Copyright Battelle Memorial Institute, 1996
!                    All Rights Reserved.
!
!----------------------Description-------------------------------------!
!
!     Convert all upper-case characters in a variable-length string
!     variable to lower case.  This subroutine does not disturb
!     non-alphabetic characters; only captial letters
!     (ASCII 65 through 90) are modified.
!
!----------------------Authors-----------------------------------------!
!
!     Written by WE Nichols, Battelle, March, 1991.
!
!----------------------Implicit Double Precision-----------------------!
!
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER (I-N)
!
!----------------------Type Declarations-------------------------------!
!
      CHARACTER*(*) CHDUM
!
!----------------------Executable Lines--------------------------------!
!
      DO 10 N = 1,LEN(CHDUM)
        M = ICHAR(CHDUM(N:N))
        IF( M .GE. 65 .AND. M .LE. 90 ) THEN
          M = M + 32
          CHDUM(N:N) = CHAR(M)
        ENDIF
   10 CONTINUE
!
!---  End of LCASE group
!
      RETURN
      END

