
    module PipeRams2Main
    implicit none
    public
    contains
    
    ! subroutine PipeRams2_Setup()
    !     ! use CSimulationVariables
    !     implicit none
    !     call OnSimulationInitialization%Add(PipeRams2_Init)
    !     call OnSimulationStop%Add(PipeRams2_Init)
    !     call OnPipeRams2Step%Add(PipeRams2_Step)
    !     call OnPipeRams2Output%Add(PipeRams2_Output)
    !     call OnPipeRams2Main%Add(PipeRams2MainBody)
    ! end subroutine
    
    ! subroutine PipeRams2_Init
    !     implicit none
    ! end subroutine PipeRams2_Init
    
    subroutine PipeRams2_Step
        use SimulationVariables
        implicit none
        if (data%State%PipeRam2%finished/=0) then
            call PIPE_RAMS2
            if (data%State%PipeRam2%IsOpening .or. data%State%PipeRam2%IsClosing .or. data%State%RAM(3)%Bottles_Charged_MalfActive) then
                if(data%State%PipeRam2%finished==0) then
                    call PIPE_RAMS2_SUB !only body of loop2
                endif
            endif
        else
            call PIPE_RAMS2_SUB
        endif
    end subroutine PipeRams2_Step
    
    ! subroutine PipeRams2_Output
    !     implicit none
    ! end subroutine PipeRams2_Output
        
    ! subroutine PipeRams2MainBody
    !     USE ifport
    !     USE ifmt
    !     ! use CSimulationVariables
    !   !  USE BOP
    !     implicit none 
        
    !     INTEGER :: PipeRam2Duration
    !     integer,dimension(8) :: PipeRam2StartTime , PipeRam2EndTime          
        
    !     !    CALL BOP_StartUp()
    !     loop1: DO
            
    !         CALL DATE_AND_TIME(values=PipeRam2StartTime)

            
    !         CALL PIPE_RAMS2
            
    !         CALL DATE_AND_TIME(values=PipeRam2EndTime)
            
    !         PipeRam2Duration = 3600000 * (PipeRam2EndTime(5) - PipeRam2StartTime(5)) + 60000 * (PipeRam2EndTime(6) - PipeRam2StartTime(6)) + 1000 * (PipeRam2EndTime(7) - PipeRam2StartTime(7)) + (PipeRam2EndTime(8) - PipeRam2StartTime(8))
            
    !         if (PipeRam2Duration < 100) then
    !             call sleepqq(100 - PipeRam2Duration)
    !         ELSE
    !             WRITE (*,*) 'PipeRam2 BOP run duration exceeded 100 ms and =', PipeRam2Duration
    !         end if             
            
    !         IF (IsStopped==.true.) THEN
    !             EXIT loop1
    !         ENDIF
            

    !     ENDDO loop1 


    !     !    CALL DEALLOCATE_ARRAYS()
    ! end subroutine PipeRams2MainBody
    
end module PipeRams2Main