SUBROUTINE PIPE_RAMS1
    use SimulationVariables
    ! USE CAccumulatorVariables
    ! USE CBopStackVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! ! use CSimulationVariables
    implicit none
    
!=====================================================================
!                 PIPE RAMS 1- BOP CAMERON Type U 5000
!                   START CONDITIONS FOR PIPE RAMS 1
!=====================================================================  
      data%State%RAM(2)%SuccessionCounter = data%State%RAM(2)%SuccessionCounter + 1
       if (data%EquipmentControl%BopControlPanel%UpperRamsValve == 1.0 .and. data%State%PipeRam1%UpperRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
             if (data%State%BopStackInput%UpperRamsCloseLEDMine == LedOn) then 
                 RETURN             
             end if          
            if ( data%State%RAM(2)%SuccessionCounter /= data%State%RAM(2)%SuccessionCounterOld+1 ) then
                data%State%RAM(2)%SuccessionCounter = 0   ! also in starup
                data%State%RAM(2)%SuccessionCounterOld = 0   ! also in starup 
                !return   
            else
                data%State%RAM(2)%SuccessionCounterOld= data%State%RAM(2)%SuccessionCounter 
            endif
        

        
        
            if ( data%State%RAM(2)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                !return
            
                data%State%RAM(2)%First_CloseTimecheck= 1 
            
                data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOff
                data%State%BopStackInput%UpperRamsOpenLEDMine = LedOff
                data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOn    !LedBlinking
            
                data%State%RAM(2)%FourwayValve = 1
            endif
        
       endif
       
       
       if (data%State%RAM(2)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then    ! 1: Open , 0: Close
    !write(*,*) 'close 2'
           
           data%State%RAM(2)%FourwayValve = 0


         
            data%State%PipeRam1%closed=0
            !PipeRam1_closed_withPossibility= PipeRam1_closed * TD_BOPConnectionPossibility(2)
            data%State%RAM(2)%vdis_tot=0
            data%State%RAM(2)%vdis_bottles=0.
            data%State%RAM(2)%fvr_air=0.
            data%State%RAM(2)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(2)%Qzero=70
            data%State%RAM(2)%Q=data%State%RAM(2)%Qzero
            data%State%RAM(2)%flow=70
            data%State%RAM(2)%tol=0.0037
            if (data%State%PipeRam1%finished==1) then
                data%State%PipeRam1%PipeRams1DotLeverOld=-1.0
           else
               data%State%PipeRam1%PipeRams1DotLeverOld=data%EquipmentControl%BopControlPanel%UpperRamsValve
           endif
           data%State%PipeRam1%finished=0
            data%State%PipeRam1%IsClosing = .true.
            data%State%PipeRam1%IsOpening = .false.

            data%State%RAM(2)%bop_type = 3
            !AbopPipeRam=196.67
            data%State%PipeRam1%A=(data%Configuration%BopStack%UpperRamClose*231)/((data%State%PipeRam1%IDBase-data%State%PipeRam1%ODDrillpipe_inBase)/2.)
            data%State%PipeRam1%NeededVolume=data%State%PipeRam1%A*(data%State%PipeRam1%IDBase-max(data%State%PipeRam1%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase))/(2.*231)  !3.67 galon for each BOP
        endif
       
        if (data%EquipmentControl%BopControlPanel%UpperRamsValve == -1.0 .and. data%State%PipeRam1%UpperRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
            
             if (data%State%BopStackInput%UpperRamsOpenLEDMine == LedOn) then 
                 RETURN             
             end if             

            if ( data%State%RAM(2)%SuccessionCounter /= data%State%RAM(2)%SuccessionCounterOld+1 ) then
                data%State%RAM(2)%SuccessionCounter = 0   ! also in starup
                data%State%RAM(2)%SuccessionCounterOld = 0   ! also in starup
                !return   
            else
                data%State%RAM(2)%SuccessionCounterOld= data%State%RAM(2)%SuccessionCounter
            endif
        
        
            if ( data%State%RAM(2)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                !return
                
                data%State%RAM(2)%First_OpenTimecheck= 1  
                
                data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOff    !new
                data%State%BopStackInput%UpperRamsCloseLEDMine = LedOff    !new
                data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOn    !LedBlinking

                data%State%RAM(2)%FourwayValve = 1
                
            endif
            
        endif
        
        if (data%State%RAM(2)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then    ! 1: Open , 0: Close
    !write(*,*) 'open 2'
            
            data%State%RAM(2)%FourwayValve = 0               
 

            data%State%PipeRam1%closed=0
            !PipeRam1_closed_withPossibility= PipeRam1_closed * TD_BOPConnectionPossibility(2)
            data%State%RAM(2)%vdis_tot=0
            data%State%RAM(2)%vdis_bottles=0.
            data%State%RAM(2)%fvr_air=0.
            data%State%RAM(2)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(2)%Qzero=70
            data%State%RAM(2)%Q=data%State%RAM(2)%Qzero
            data%State%RAM(2)%flow=70
            data%State%RAM(2)%tol=0.0037
            
            

                if (data%State%PipeRam1%finished==1) then
                    data%State%PipeRam1%PipeRams1DotLeverOld=1.0
                else
                    data%State%PipeRam1%PipeRams1DotLeverOld=data%EquipmentControl%BopControlPanel%UpperRamsValve
                endif
            data%State%PipeRam1%finished=0
            data%State%PipeRam1%IsOpening = .true.
            data%State%PipeRam1%IsClosing = .false.

        
         !if (UpperRamsOpenLED == LedOn) then 
         !    RETURN             
         !end if
            
            

            data%State%RAM(2)%bop_type = 3
            !AbopPipeRam=186.5
            data%State%PipeRam1%A=(data%Configuration%BopStack%UpperRamOpen*231)/((data%State%PipeRam1%IDBase-data%State%PipeRam1%ODDrillpipe_inBase)/2.)
            data%State%PipeRam1%NeededVolume=data%State%PipeRam1%A*(data%State%PipeRam1%IDBase-max(data%State%PipeRam1%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase))/(2.*231)  !3.48 galon for each BOP
        endif
      
!=====================================================================   
    
    data%State%Annular%FirstSet= 0
    data%State%AnnularComputational%RamsFirstSet= 0
if (data%State%PipeRam1%IsOpening .or. data%State%PipeRam1%IsClosing .or. data%State%RAM(2)%Bottles_Charged_MalfActive) then
        data%State%Annular%FirstSet= 0
        data%State%AnnularComputational%RamsFirstSet= 0
        CALL PIPE_RAMS1_SUB
end if    
    
    
    END SUBROUTINE PIPE_RAMS1  
    
    

    
    
    
    
    
    
    
    
SUBROUTINE PIPE_RAMS1_SUB    
    
    use SimulationVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables
    implicit none
    
    
    ! FirstSet= 0
    ! RamsFirstSet= 0
    
    ! loop3: do while (finished_pipe1==0)
        
        
             data%State%RAM(2)%SuccessionCounter = data%State%RAM(2)%SuccessionCounter + 1
       
        
           if (data%EquipmentControl%BopControlPanel%UpperRamsValve == 1.0 .and. data%State%PipeRam1%PipeRams1DotLeverOld == -1.0 .and. data%State%PipeRam1%UpperRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
            

              
                if ( data%State%RAM(2)%First_CloseTimecheck == 0 ) then 
                    
                    
                    if ( data%State%RAM(2)%SuccessionCounter /= data%State%RAM(2)%SuccessionCounterOld+1 ) then
                        data%State%RAM(2)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(2)%SuccessionCounterOld = 0   ! also in starup
                        !return   
                    else
                        data%State%RAM(2)%SuccessionCounterOld= data%State%RAM(2)%SuccessionCounter        
                    endif
        

        
                    if ( data%State%RAM(2)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        !return
                        
                        data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOff
                        data%State%BopStackInput%UpperRamsOpenLEDMine = LedOff
                        data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOn    !LedBlinking
                        
                        data%State%RAM(2)%FourwayValve = 1
                        
                    endif
           
                endif
                
           endif
           
        if (data%State%RAM(2)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
    !write(*,*) 'close 4'
            
            data%State%RAM(2)%FourwayValve = 0                                
  
               
            data%State%PipeRam1%closed=0
            !PipeRam1_closed_withPossibility= PipeRam1_closed * TD_BOPConnectionPossibility(2)    ! for TD code
            CALL OpenUpperRams       ! for C code
            data%State%PipeRam1%Situation_forTD= 0       ! open - for TD code
            data%State%RAM(2)%p_bop=data%State%ShearRAM%PA
            data%State%PipeRam1%PipeRams1DotLeverOld = data%EquipmentControl%BopControlPanel%UpperRamsValve

            
            data%State%RAM(2)%bop_type = 3
            !AbopPipeRam=196.67
            data%State%PipeRam1%A=(data%Configuration%BopStack%UpperRamClose*231)/((data%State%PipeRam1%IDBase-data%State%PipeRam1%ODDrillpipe_inBase)/2.)
            !write(*,*) 'NeededVolumeShearRams1=',NeededVolumeShearRams
            data%State%PipeRam1%NeededVolume=data%State%PipeRam1%A*(data%State%PipeRam1%ID-max(data%State%PipeRam1%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase))/(2.*231)
           !  write(*,*) 'NeededVolumeShearRams2=',NeededVolumeShearRams
            
            data%State%RAM(2)%vdis_bottles=0.
            data%State%RAM(2)%fvr_air=0.
            data%State%RAM(2)%vdis_elecp=0.
            data%State%PipeRam1%IsClosing = .true.
            data%State%PipeRam1%IsOpening = .false.
        endif
        
        if (data%EquipmentControl%BopControlPanel%UpperRamsValve == -1.0 .and. data%State%PipeRam1%PipeRams1DotLeverOld == 1.0 .and. data%State%PipeRam1%UpperRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then 
            
            
                if ( data%State%RAM(2)%First_OpenTimecheck == 0 ) then

                    if ( data%State%RAM(2)%SuccessionCounter /= data%State%RAM(2)%SuccessionCounterOld+1 ) then
                        data%State%RAM(2)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(2)%SuccessionCounterOld = 0   ! also in starup
                        !return   
                    else
                        data%State%RAM(2)%SuccessionCounterOld= data%State%RAM(2)%SuccessionCounter        
                    endif
        
                    if ( data%State%RAM(2)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        !return
                        
                        data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOff
                        data%State%BopStackInput%UpperRamsCloseLEDMine = LedOff
                        data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOn    !LedBlinking
                        
                        data%State%RAM(2)%FourwayValve = 1
                        
                        
                    endif
           
                endif
                
        endif
        
        if (data%State%RAM(2)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
    !write(*,*) 'open 4'
            
            data%State%RAM(2)%FourwayValve = 0                
            
    
            
            data%State%PipeRam1%closed=0
            !PipeRam1_closed_withPossibility= PipeRam1_closed * TD_BOPConnectionPossibility(2)
            CALL OpenUpperRams
            data%State%PipeRam1%Situation_forTD= 0       ! open - for TD code
            data%State%RAM(2)%p_bop=data%State%ShearRAM%PA
            data%State%PipeRam1%PipeRams1DotLeverOld = data%EquipmentControl%BopControlPanel%UpperRamsValve

            data%State%RAM(2)%bop_type = 3
            !AbopPipeRam=186.5
            data%State%PipeRam1%A=(data%Configuration%BopStack%UpperRamOpen*231)/((data%State%PipeRam1%IDBase-data%State%PipeRam1%ODDrillpipe_inBase)/2.)
            data%State%PipeRam1%NeededVolume=data%State%PipeRam1%A*(data%State%PipeRam1%IDBase-data%State%PipeRam1%ID)/(2.*231)
            data%State%RAM(2)%vdis_bottles=0.
            data%State%RAM(2)%fvr_air=0.
            data%State%RAM(2)%vdis_elecp=0.

            data%State%PipeRam1%IsOpening = .true.
            data%State%PipeRam1%IsClosing = .false.
            endif

        
            data%State%RAM(2)%First_CloseTimecheck = 0
            data%State%RAM(2)%First_OpenTimecheck = 0
 
 
     data%State%RAM(2)%time=data%State%RAM(2)%time+data%State%RamLine%DeltaT_BOP  !overal time (s)

 

!===================================================
!                        BOP 
!===================================================
if (data%State%PipeRam1%closed==0) then    !bop closing
     call bop_code(2,data%State%PipeRam1%H,2)   !ramtype=2      2=RNUMBER
endif    !bop is closing
!================================================================
if (data%State%PipeRam1%closed==1) then
    data%State%RAM(2)%Q=0
    !p_bop=pram_reg
    data%State%RAM(2)%p_bop=data%State%ShearRAM%PA
    data%State%RAMS%minloss(2,17)=0.       !RNUMBER=2
endif

data%State%RAM(2)%timecounter_ram=data%State%RAM(2)%timecounter_ram+1





!        MiddleRamsStatus = IDshearBop
!        UpperRamsStatus = IDPipeRam1
!        LowerRamsStatus = IDPipeRam2
!        AnnularStatus = IDAnnular
!        AccumulatorPressureGauge = p_acc
!        ManifoldPressureGauge= pram_reg
!        AnnularPressureGauge=Pannular_reg
!
!        
!
!         WRITE(60,60) data%State%RAM(2)%time,data%State%RAM(2)%Q,data%State%RAM(2)%vdis_tot,p_acc, &
!         pram_reg,Pannular_reg,data%State%RAM(2)%p_bop,IDshearBop, &
!         IDPipeRam1,IDPipeRam2,IDAnnular
!60       FORMAT(11(f18.5))
         
         
        !   call sleepqq(100)
          
if (data%State%PipeRam1%closed==1) then
 !   if ((MiddleRamsValve==1. .and. MiddleRamsFailureMalf==0) .or. (MiddleRamsValve==-1.0 .and. MiddleRamsFailureMalf==0) .or. (LowerRamsValve==1. .and. LowerRamsFailureMalf==0) .or. (LowerRamsValve==-1.0 .and. LowerRamsFailureMalf==0) .or. (AnnularValve==1. .and. AnnularFailureMalf==0) .or. (AnnularValve==-1.0 .and. AnnularFailureMalf==0) .or. ChokeLineValve==1. .or. ChokeLineValve==-1.0 .or. KillLineValve==1. .or. KillLineValve==-1.0) then
        data%State%PipeRam1%finished=1
  !  endif
endif          
          
        !   if (IsStopped == .true.) return
          
    ! end do  loop3   !while finished_pipe1==0
    
    
    
    
    
    
    if ( data%State%PipeRam1%finished==1 .and. data%State%RAM(2)%Bottles_Charged_MalfActive==.true.) then
        call bop_code(2,data%State%PipeRam1%H,2)   !ramtype=2      2=RNUMBER
        ! call sleepqq(100)
    endif
    
    
    
    
    
    
END SUBROUTINE PIPE_RAMS1_SUB 