
SUBROUTINE SHEAR_RAMS
    use SimulationVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables
    implicit none
    
     
    
!=====================================================================
!                 SHEAR RAMS- BOP CAMERON Type U 5000
!                   START CONDITIONS FOR SHEAR RAMS
!=====================================================================  
    
    data%State%RAM(4)%SuccessionCounter = data%State%RAM(4)%SuccessionCounter + 1
    
    if (data%EquipmentControl%BopControlPanel%MiddleRamsValve == 1.0 .and. data%State%ShearRAM%MiddleRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
        
            if (data%State%BopStackInput%MiddleRamsCloseLEDMine == LedOn) then 
                RETURN             
            end if           
        
        
        if ( data%State%RAM(4)%SuccessionCounter /= data%State%RAM(4)%SuccessionCounterOld+1 ) then
            data%State%RAM(4)%SuccessionCounter = 0   ! also in starup
            data%State%RAM(4)%SuccessionCounterOld = 0   ! also in starup
            !return   
        else
            data%State%RAM(4)%SuccessionCounterOld= data%State%RAM(4)%SuccessionCounter        
        endif
    
    
        if ( data%State%RAM(4)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
            !return
        
            data%State%RAM(4)%First_CloseTimecheck= 1 
            
            data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOff
            data%State%BopStackInput%MiddleRamsOpenLEDMine = LedOff
            data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOn    !LedBlinking
            
            data%State%RAM(4)%FourwayValve = 1
            
        endif
        
    endif
       
           
    if (data%State%RAM(4)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then    ! 1: Open , 0: Close
        !write(*,*) 'close 2'                       
        data%State%RAM(4)%FourwayValve = 0           
        

        data%State%RamLine%ShearBop_closed=0
        !ShearBop_closed_withPossibility= ShearBop_closed * TD_BOPConnectionPossibility(3)
        data%State%RAM(4)%vdis_tot=0
        data%State%RAM(4)%vdis_bottles=0.
        data%State%RAM(4)%fvr_air=0.
        data%State%RAM(4)%vdis_elecp=0.
        data%State%Pumps%Qiter=7
        data%State%RAM(4)%Qzero=70
        data%State%RAM(4)%Q=data%State%RAM(4)%Qzero
        data%State%RAM(4)%flow=70
        data%State%RAM(4)%tol=0.0037
    
    
        if (data%State%RamLine%FINISHED_shear==1) then
            data%State%RamLine%ShearRamsLeverOld=-1.0
        else
            data%State%RamLine%ShearRamsLeverOld=data%EquipmentControl%BopControlPanel%MiddleRamsValve
        endif
        data%State%RamLine%FINISHED_shear=0
        data%State%RamLine%ShearRamIsClosing = .true.
        data%State%RamLine%ShearRamIsOpening = .false.

        data%State%RAM(4)%bop_type = 2
        !NeededVolumeShearRams=5.8  !galon for each BOP
        !AbopShearRam=196.67 !(in^2)
        data%State%ShearRAM%AbopShearRam=(data%Configuration%BopStack%BlindRamClose*231)/(data%State%ShearRAM%IDshearBopBase/2.)
        !NeededVolumeShearRams=BlindRamClose  !galon for each BOP        **changed
        data%State%ShearRAM%NeededVolumeShearRams=data%State%ShearRAM%AbopShearRam*(data%State%ShearRAM%IDshearBopBase-data%State%ShearRAM%ShearIsNotAllowed*data%State%ShearRAM%ODDrillpipe_inShearRam)/(2.*231)  !3.67 galon for each BOP  !     **changed
        data%State%ShearRAM%VA=data%State%ShearRAM%AbopShearRam*(data%State%ShearRAM%IDshearBopBase-data%State%ShearRAM%ODDrillpipe_inShearRam)/(2.*231)
        data%State%ShearRAM%VB=data%State%ShearRAM%NeededVolumeShearRams
    endif
       
    if (data%EquipmentControl%BopControlPanel%MiddleRamsValve == -1.0 .and. data%State%ShearRAM%MiddleRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
        
            if (data%State%BopStackInput%MiddleRamsOpenLEDMine == LedOn) then 
                RETURN             
            end if 
            
        if ( data%State%RAM(4)%SuccessionCounter /= data%State%RAM(4)%SuccessionCounterOld+1 ) then
            data%State%RAM(4)%SuccessionCounter = 0   ! also in starup
            data%State%RAM(4)%SuccessionCounterOld = 0   ! also in starup
            !return   
        else
            data%State%RAM(4)%SuccessionCounterOld= data%State%RAM(4)%SuccessionCounter
        endif
    
    
        if ( data%State%RAM(4)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
            !return
            
            data%State%RAM(4)%First_OpenTimecheck= 1
            
            data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOff    !new
            data%State%BopStackInput%MiddleRamsCloseLEDMine = LedOff    !new 
            data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOn    !LedBlinking
            
        endif
        
    endif
        
               
    if (data%State%RAM(4)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then    ! 1: Open , 0: Close
        !write(*,*) 'open 2'
            
        data%State%RAM(4)%FourwayValve = 0                           
        
        

        data%State%RamLine%ShearBop_closed=0
        !ShearBop_closed_withPossibility= ShearBop_closed * TD_BOPConnectionPossibility(3)
        data%State%RAM(4)%vdis_tot=0
        data%State%RAM(4)%vdis_bottles=0.
        data%State%RAM(4)%fvr_air=0.
        data%State%RAM(4)%vdis_elecp=0.
        data%State%Pumps%Qiter=7
        data%State%RAM(4)%Qzero=70
        data%State%RAM(4)%Q=data%State%RAM(4)%Qzero
        data%State%RAM(4)%flow=70
        data%State%RAM(4)%tol=0.0037

        if (data%State%RamLine%FINISHED_shear==1) then
            data%State%RamLine%ShearRamsLeverOld=1.0
        else
            data%State%RamLine%ShearRamsLeverOld=data%EquipmentControl%BopControlPanel%MiddleRamsValve
        endif
        data%State%RamLine%FINISHED_shear=0
        data%State%RamLine%ShearRamIsOpening = .true.
        data%State%RamLine%ShearRamIsClosing = .false.

        data%State%RAM(4)%bop_type = 3
        !AbopShearRam=186.5  !(in^2)
        data%State%ShearRAM%AbopShearRam=(data%Configuration%BopStack%BlindRamOpen*231)/(data%State%ShearRAM%IDshearBopBase/2.)
        !NeededVolumeShearRams=5.5  !galon for each BOP
        !NeededVolumeShearRams=BlindRamOpen      ! **changed
        data%State%ShearRAM%NeededVolumeShearRams=data%State%ShearRAM%AbopShearRam*(data%State%ShearRAM%IDshearBopBase-data%State%ShearRAM%ShearIsNotAllowed*data%State%ShearRAM%ODDrillpipe_inShearRam)/(2.*231)  !3.67 galon for each BOP  !     **changed
        !va=AbopShearRam*(IDshearBopBase-ODDrillpipe_inShearRam)/(2.*231)
        !vb=NeededVolumeShearRams
    endif
!=====================================================================      
    ! if (ShearRamIsOpening .or. ShearRamIsClosing .or. data%State%RAM(4)%Bottles_Charged_MalfActive) then
    !         CALL SHEAR_RAMS_SUB
    ! end if    
    
END SUBROUTINE SHEAR_RAMS   
    
    
    
    
    
SUBROUTINE SHEAR_RAMS_SUB    
    use SimulationVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables
    implicit none
    
    ! FirstSet= 0
    ! RamsFirstSet= 0

    ! loop2: do while (finished_shear==0)
    data%State%RAM(4)%SuccessionCounter = data%State%RAM(4)%SuccessionCounter + 1
    if (data%EquipmentControl%BopControlPanel%MiddleRamsValve == 1.0 .and. data%State%RamLine%ShearRamsLeverOld == -1.0 .and. data%State%ShearRAM%MiddleRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 .and. data%State%RAM(4)%First_CloseTimecheck == 0) then
        if ( data%State%RAM(4)%SuccessionCounter /= data%State%RAM(4)%SuccessionCounterOld+1 ) then
            data%State%RAM(4)%SuccessionCounter = 0   ! also in starup
            data%State%RAM(4)%SuccessionCounterOld = 0   ! also in starup
        else
            data%State%RAM(4)%SuccessionCounterOld= data%State%RAM(4)%SuccessionCounter        
        endif
        if ( data%State%RAM(4)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
            data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOff
            data%State%BopStackInput%MiddleRamsOpenLEDMine = LedOff
            data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOn    !LedBlinking                 
            data%State%RAM(4)%FourwayValve = 1
        endif
    endif
                            
    if (data%State%RAM(4)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
        data%State%RAM(4)%FourwayValve = 0           
        data%State%RamLine%ShearBop_closed=0
        data%State%RAM(4)%p_bop=data%State%ShearRAM%PA
        data%State%RamLine%ShearRamsLeverOld = data%EquipmentControl%BopControlPanel%MiddleRamsValve
        CALL OpenMiddleRams       ! for C code
        data%State%RamLine%ShearBop_Situation_forTD= 0       ! open - for TD code
        data%State%RAM(4)%bop_type = 2
        data%State%ShearRAM%AbopShearRam=(data%Configuration%BopStack%BlindRamClose*231)/(data%State%ShearRAM%IDshearBopBase/2.)
        data%State%ShearRAM%NeededVolumeShearRams=data%State%ShearRAM%AbopShearRam*(data%State%ShearRAM%IDshearBop-data%State%ShearRAM%ShearIsNotAllowed*data%State%ShearRAM%ODDrillpipe_inShearRam)/(2.*231)       ! **changed
        data%State%RAM(4)%vdis_bottles=0.
        data%State%RAM(4)%fvr_air=0.
        data%State%RAM(4)%vdis_elecp=0.
        data%State%RamLine%ShearRamIsClosing = .true.
        data%State%RamLine%ShearRamIsOpening = .false.
    endif

    if (data%EquipmentControl%BopControlPanel%MiddleRamsValve == -1.0 .and. data%State%RamLine%ShearRamsLeverOld == 1.0 .and. data%State%ShearRAM%MiddleRamsFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 .and. data%State%RAM(4)%First_OpenTimecheck == 0) then 
        if ( data%State%RAM(4)%SuccessionCounter /= data%State%RAM(4)%SuccessionCounterOld+1 ) then
            data%State%RAM(4)%SuccessionCounter = 0   ! also in starup
            data%State%RAM(4)%SuccessionCounterOld = 0   ! also in starup
            !return   
        else
            data%State%RAM(4)%SuccessionCounterOld= data%State%RAM(4)%SuccessionCounter        
        endif

        if ( data%State%RAM(4)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
            data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOff
            data%State%BopStackInput%MiddleRamsCloseLEDMine = LedOff
            data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOn    !LedBlinking  
            data%State%RAM(4)%FourwayValve = 1
        endif
    endif

    if (data%State%RAM(4)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
        data%State%RAM(4)%FourwayValve = 0            
        data%State%RamLine%ShearBop_closed=0
        data%State%RAM(4)%p_bop=data%State%ShearRAM%PA
        data%State%RamLine%ShearRamsLeverOld = data%EquipmentControl%BopControlPanel%MiddleRamsValve
        CALL OpenMiddleRams
        data%State%RamLine%ShearBop_Situation_forTD= 0       ! open - for TD code
        data%State%RAM(4)%bop_type = 3
        data%State%ShearRAM%NeededVolumeShearRams=data%State%ShearRAM%AbopShearRam*(data%State%ShearRAM%IDshearBopBase-data%State%ShearRAM%IDshearBop)/(2.*231)
        data%State%RAM(4)%vdis_bottles=0.
        data%State%RAM(4)%fvr_air=0.
        data%State%RAM(4)%vdis_elecp=0.
        data%State%RamLine%ShearRamIsOpening = .true.
        data%State%RamLine%ShearRamIsClosing = .false.
    endif

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

    !===================================================
    !                        BOP 
    !===================================================
    if (data%State%RamLine%ShearBop_closed==0) then    !bop closing
        call bop_code(1,data%State%ShearRAM%H_ShearRamBop,4)   !ramtype=1      4=RNUMBER
    endif    !bop is closing
    !================================================================
    if (data%State%RamLine%ShearBop_closed==1) then
        data%State%RAM(4)%Q=0
        data%State%RAM(4)%p_bop=data%State%ShearRAM%PA
        data%State%RAMS%minloss(4,17)=0.       !RNUMBER=4
    endif

    data%State%RAM(4)%timecounter_ram=data%State%RAM(4)%timecounter_ram+1
    call sleepqq(100)

    if (data%State%RamLine%ShearBop_closed==1) then
        data%State%RamLine%FINISHED_shear=1
    endif
    !   if (IsStopped == .true.) return 
    ! end do  loop2   !while finished_shear==0
    
    ! if ( finished_shear==1 .and. data%State%RAM(4)%Bottles_Charged_MalfActive==.true.) then
    !     call bop_code(1,H_ShearRamBop,4)   !ramtype=1      4=RNUMBER
    !     call sleepqq(100)
    ! endif    
    
END SUBROUTINE SHEAR_RAMS_SUB