module PipeRams1Main
    implicit none
    public
    contains
    
    ! subroutine PipeRams1_Setup()
    !     ! use CSimulationVariables
    !     implicit none
    !     call OnSimulationInitialization%Add(PipeRams1_Init)
    !     call OnSimulationStop%Add(PipeRams1_Init)
    !     call OnPipeRams1Step%Add(PipeRams1_Step)
    !     call OnPipeRams1Output%Add(PipeRams1_Output)
    !     call OnPipeRams1Main%Add(PipeRams1MainBody)
    ! end subroutine
    
    subroutine PipeRams1_Init
        call BOP_StartUp()
    end subroutine PipeRams1_Init
    
    subroutine PipeRams1_Step
        use SimulationVariables
        implicit none
        if (data%State%PipeRam1%finished/=0) then
            call PIPE_RAMS1
            if (data%State%PipeRam1%IsOpening .or. data%State%PipeRam1%IsClosing .or. data%State%RAM(2)%Bottles_Charged_MalfActive) then
                if(data%State%PipeRam1%finished==0) then
                    call PIPE_RAMS1_SUB !only body of loop2
                endif
            endif
        else
            call PIPE_RAMS1_SUB
        endif
    end subroutine PipeRams1_Step
    
    ! subroutine PipeRams1_Output
        
    ! end subroutine PipeRams1_Output
        
    ! subroutine PipeRams1MainBody
    
    !     USE ifport
    !     USE ifmt
    !     ! use CSimulationVariables
    !   !  USE BOP

    !     implicit none
        
    !     INTEGER :: PipeRam1Duration
    !     integer,dimension(8) :: PipeRam1StartTime , PipeRam1EndTime        
        
    !       !   CALL BOP_StartUp()
    !     loop1: DO
            
    !         CALL DATE_AND_TIME(values=PipeRam1StartTime)
    !         CALL PIPE_RAMS1
    !         CALL DATE_AND_TIME(values=PipeRam1EndTime)
            
    !         PipeRam1Duration = 3600000 * (PipeRam1EndTime(5) - PipeRam1StartTime(5)) + 60000 * (PipeRam1EndTime(6) - PipeRam1StartTime(6)) + 1000 * (PipeRam1EndTime(7) - PipeRam1StartTime(7)) + (PipeRam1EndTime(8) - PipeRam1StartTime(8))
            
    !         if (PipeRam1Duration < 100) then
    !             call sleepqq(100 - PipeRam1Duration)
    !         ELSE
    !             WRITE (*,*) 'PipeRam1 BOP run duration exceeded 100 ms and =', PipeRam1Duration
    !         end if 
            
            
    !         IF (IsStopped==.true.) THEN
    !             EXIT loop1
    !         ENDIF
            

    !     ENDDO loop1 


    !    !     CALL DEALLOCATE_ARRAYS()
            
    ! end subroutine PipeRams1MainBody
    
end module PipeRams1Main