SUBROUTINE ANNULAR_SUB1
    use SimulationVariables
    use CBopControlPanel
    use SimulationVariables
    use PressureDisplayVARIABLESModule
    USE CEquipmentsConstants
    USE CBopStackVariables
    
    implicit none
    
  !write(*,*) 'checkpoint 1'
!=====================================================================
!                 ANNULAR PREVENTER- BOP CAMERON Type U 5000
!                   START CONDITIONS FOR ANNULAR PREVENTER
!=====================================================================   
    
      data%State%RAM(1)%SuccessionCounter = data%State%RAM(1)%SuccessionCounter + 1
        
       if (data%EquipmentControl%BopControlPanel%AnnularValve == 1.0 .and. data%State%Annular%AnnularFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
           
         if (data%State%BopStackInput%AnnularCloseLedMine == LedOn) then
             RETURN              
         end if           
           
           
            if ( data%State%RAM(1)%SuccessionCounter /= data%State%RAM(1)%SuccessionCounterOld+1 ) then
                data%State%RAM(1)%SuccessionCounter = 0   ! also in starup
                data%State%RAM(1)%SuccessionCounterOld = 0   ! also in starup
                !return   
            else
                data%State%RAM(1)%SuccessionCounterOld= data%State%RAM(1)%SuccessionCounter        
            endif
        

        
            if ( data%State%RAM(1)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                !return
            
                data%State%RAM(1)%First_CloseTimecheck= 1
            
                data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOff
                data%State%BopStackInput%AnnularOpenLedMine = LedOff
                data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOn !LedBlinking
            
                data%State%RAM(1)%FourwayValve = 1
            
            endif
        
        endif
           
           
       if (data%State%RAM(1)%FourwayValve == 1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure .and. data%State%Annular%Pannular_reg>data%State%AnnularComputational%AnnularMovingPressure) then    ! 1: Open , 0: Close
        
           
            data%State%RAM(1)%FourwayValve = 0
            
            
            
            data%State%Annular%Annular_closed=0
            !Annular_closed_withPossibility= Annular_closed * TD_BOPConnectionPossibility(1)
            data%State%RAM(1)%vdis_tot=0
            data%State%RAM(1)%vdis_bottles=0.
            data%State%RAM(1)%fvr_air=0.
            data%State%RAM(1)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(1)%Qzero=70
            data%State%RAM(1)%Q=data%State%RAM(1)%Qzero
            data%State%RAM(1)%flow=70
            data%State%Annular%tolAnnular=0.0018
            if (data%State%Annular%finished_Annular==1) then
               data%State%Annular%AnnularLeverOld=-1.0
           else
               data%State%Annular%AnnularLeverOld=data%EquipmentControl%BopControlPanel%AnnularValve 
           endif
           data%State%Annular%finished_Annular=0
            data%State%Annular%AnnularIsClosing = .true.
            data%State%Annular%AnnularIsOpening = .false.

            data%State%RAM(2)%bop_type = 3
            !AbopAnnular=963.1 !(in^2)
            data%State%Annular%AbopAnnular=(data%Configuration%BopStack%AnnularPreventerClose*231.)/((data%State%Annular%IDAnnularBase-data%State%Annular%ODDrillpipe_inAnnularBase)/2.)     ! 231 in^3 = 1 gal
            data%State%Annular%NeededVolumeAnnular=data%State%Annular%AbopAnnular*(data%State%Annular%IDAnnularBase-max(data%State%Annular%ODDrillpipe_inAnnular,data%State%Annular%ODDrillpipe_inAnnularBase))/(2.*231)   !=17.98 galon for IDAnnularBase=13 5/8 , ODDrillpipe_inAnnularBase=5
            !WRITE(*,*) 'a)NeededVolumeAnnular=' , NeededVolumeAnnular
            !write(*,*) 'close 1'
       endif
       
       
        if (data%EquipmentControl%BopControlPanel%AnnularValve == -1.0 .and. data%State%Annular%AnnularFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 ) then
            
             if (data%State%BopStackInput%AnnularOpenLedMine == LedOn) then
                 RETURN              
             end if            
            
            !CasingPressure : PressureGauges(2) *****temp conditionssssss
            
         !note: (AnnularSealingPressure) is only for opening while well is pressurised
                       

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

        
            if ( data%State%RAM(1)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                !return
                
                data%State%RAM(1)%First_OpenTimecheck= 1
                
                data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOff    !new
                data%State%BopStackInput%AnnularCloseLedMine = LedOff    !new
                data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOn  !LedBlinking
                
                data%State%RAM(1)%FourwayValve = 1
                
            endif
            
        endif
        
               
            
            

        if (data%State%RAM(1)%FourwayValve == 1 .and. data%State%Annular%Pannular_reg>data%State%AnnularComputational%AnnularMovingPressure .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure &
            .and. (data%State%Annular%Annular_closed==0 .or. (data%State%Annular%Annular_closed==1 .and. data%State%PressureDisplay%PressureGauges(2) <=100.0) .or. (data%State%Annular%Annular_closed==1 .and. data%State%PressureDisplay%PressureGauges(2)>100.0 .and. data%State%Annular%Pannular_reg>=data%State%AnnularComputational%AnnularSealingPressure))) then    ! 1: Open , 0: Close
    !write(*,*) 'open 2'
            
            data%State%RAM(1)%FourwayValve = 0
            

             
            data%State%Annular%Annular_closed=0
            !Annular_closed_withPossibility= Annular_closed * TD_BOPConnectionPossibility(1)
            data%State%RAM(1)%vdis_tot=0
            data%State%RAM(1)%vdis_bottles=0.
            data%State%RAM(1)%fvr_air=0.
            data%State%RAM(1)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(1)%Qzero=70
            data%State%RAM(1)%Q=data%State%RAM(1)%Qzero
            data%State%RAM(1)%flow=70
            data%State%Annular%tolAnnular=0.0018
            
            

                if (data%State%Annular%finished_Annular==1) then
                    data%State%Annular%AnnularLeverOld=1.0
                else
                    data%State%Annular%AnnularLeverOld=data%EquipmentControl%BopControlPanel%AnnularValve
                endif
            data%State%Annular%finished_Annular=0
            data%State%Annular%AnnularIsOpening = .true.
            data%State%Annular%AnnularIsClosing = .false.

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


            data%State%RAM(1)%bop_type = 3
            !AbopAnnular=758.48 !(in^2)
            data%State%Annular%AbopAnnular=(data%Configuration%BopStack%AnnularPreventerOpen*231)/((data%State%Annular%IDAnnularBase-max(data%State%Annular%ODDrillpipe_inAnnular,data%State%Annular%ODDrillpipe_inAnnularBase))/2.)
            data%State%Annular%NeededVolumeAnnular=data%State%Annular%AbopAnnular*(data%State%Annular%IDAnnularBase-data%State%Annular%ODDrillpipe_inAnnular)/(2.*231)   !=14.16 galon for IDAnnularBase=13 5/8 , ODDrillpipe_inAnnular=5
            !write(*,*) 'open 1'
        
        endif
       
!=====================================================================   
        
if (data%State%Annular%AnnularIsOpening .or. data%State%Annular%AnnularIsClosing .or. data%State%RAM(1)%Bottles_Charged_MalfActive) then
        CALL ANNULAR_SUB2
end if 
        
        
        
        
    END SUBROUTINE ANNULAR_SUB1
    
    
    
    
  
    
    
    
    
    
    
    
    
    
SUBROUTINE ANNULAR_SUB2
    use SimulationVariables
    use PressureDisplayVARIABLESModule
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    USE CBopStackVariables
    ! use CSimulationVariables
    
    implicit none
        
    
    data%State%Annular%FirstSet= 0
    data%State%AnnularComputational%RamsFirstSet= 0
    
    
    ! loop5: do while (finished_Annular==0)
        
  !write(*,*) 'checkpoint 2'
        
        
            data%State%RAM(1)%SuccessionCounter = data%State%RAM(1)%SuccessionCounter + 1

     !   CALL CPU_TIME(Annular_StartTime)  
 
           if (data%EquipmentControl%BopControlPanel%AnnularValve == 1.0 .and. data%State%Annular%AnnularLeverOld == -1.0 .and. data%State%Annular%AnnularFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then
                if ( data%State%RAM(1)%First_CloseTimecheck == 0 ) then 
                    

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

        
                    if ( data%State%RAM(1)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        !return
                        
                        data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOff
                        data%State%BopStackInput%AnnularOpenLedMine = LedOff
                        data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOn !LedBlinking
                        
                        data%State%RAM(1)%FourwayValve = 1   
                        
                    endif
           
                endif
               !write(*,*) 'chekkk 1'
                
           endif
           
                
        if (data%State%RAM(1)%FourwayValve == 1 .and. data%State%Annular%Pannular_reg>data%State%AnnularComputational%AnnularMovingPressure .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
    !write(*,*) 'close 4'
            
            data%State%RAM(1)%FourwayValve = 0           
           
 
            data%State%Annular%Annular_closed=0
            !Annular_closed_withPossibility= Annular_closed * TD_BOPConnectionPossibility(1)
            data%State%AnnularComputational%p_annular=data%State%AnnularComputational%pa_annular
            data%State%Annular%AnnularLeverOld = data%EquipmentControl%BopControlPanel%AnnularValve

            CALL OpenAnnular
            data%State%Annular%Annular_Situation_forTD= 0       ! open - for TD code
            data%State%RAM(1)%bop_type = 3
            !AbopAnnular=963.1 !(in^2)
            data%State%Annular%AbopAnnular=(data%Configuration%BopStack%AnnularPreventerClose*231)/((data%State%Annular%IDAnnularBase-data%State%Annular%ODDrillpipe_inAnnularBase)/2.)
            !write(*,*) 'NeededVolumeShearRams1=',NeededVolumeShearRams
            data%State%Annular%NeededVolumeAnnular=data%State%Annular%AbopAnnular*(data%State%Annular%IDAnnular-max(data%State%Annular%ODDrillpipe_inAnnular,data%State%Annular%ODDrillpipe_inAnnularBase))/(2*231.)
           !  write(*,*) 'NeededVolumeAnnular=',NeededVolumeAnnular

            data%State%RAM(1)%vdis_bottles=0.
            data%State%RAM(1)%fvr_air=0.
            data%State%RAM(1)%vdis_elecp=0.
            data%State%Annular%AnnularIsClosing = .true.
            data%State%Annular%AnnularIsOpening = .false.
            !write(*,*) 'close 2'
            
        endif
        
        if (data%EquipmentControl%BopControlPanel%AnnularValve == -1.0 .and. data%State%Annular%AnnularLeverOld == 1.0 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure .and. data%State%Annular%AnnularFailureMalf==0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 ) then
            
            !CasingPressure : PressureGauges(2) *****temp conditionssssss
            
            !note: (AnnularSealingPressure) is only for opening while well is pressurised
            
                if ( data%State%RAM(1)%First_OpenTimecheck == 0 ) then

                    if ( data%State%RAM(1)%SuccessionCounter /= data%State%RAM(1)%SuccessionCounterOld+1 ) then
                        data%State%RAM(1)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(1)%SuccessionCounterOld = 0   ! also in starup
                        !return   
                    else
                        data%State%RAM(1)%SuccessionCounterOld= data%State%RAM(1)%SuccessionCounter        
                    endif
        
                    if ( data%State%RAM(1)%SuccessionCounter >= int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        !return
                        
                        data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOff
                        data%State%BopStackInput%AnnularCloseLedMine= LedOff
                        data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOn  !LedBlinking
                        
                        data%State%RAM(1)%FourwayValve = 1
                        
                    endif
           
                endif
               !write(*,*) 'chekkk 2'
                
            endif
            
                
                
        if (data%State%RAM(1)%FourwayValve == 1 .and. data%State%Annular%Pannular_reg>data%State%AnnularComputational%AnnularMovingPressure &
            .and. (data%State%Annular%Annular_closed==0 .or. (data%State%Annular%Annular_closed==1 .and. data%State%PressureDisplay%PressureGauges(2) <=100.0) .or. (data%State%Annular%Annular_closed==1 .and. data%State%PressureDisplay%PressureGauges(2)>100.0 .and. data%State%Annular%Pannular_reg>=data%State%AnnularComputational%AnnularSealingPressure))) then
    !write(*,*) 'open 4'
            
            data%State%RAM(1)%FourwayValve = 0
            
            
            data%State%Annular%Annular_closed=0
            !Annular_closed_withPossibility= Annular_closed * TD_BOPConnectionPossibility(1)
            data%State%AnnularComputational%p_annular=data%State%AnnularComputational%pa_annular
            data%State%Annular%AnnularLeverOld = data%EquipmentControl%BopControlPanel%AnnularValve

            CALL OpenAnnular
            data%State%Annular%Annular_Situation_forTD= 0       ! open - for TD code
            data%State%RAM(1)%bop_type = 3
            !AbopAnnular=758.48 !(in^2)
            data%State%Annular%AbopAnnular=(data%Configuration%BopStack%AnnularPreventerOpen*231)/((data%State%Annular%IDAnnularBase-data%State%Annular%ODDrillpipe_inAnnularBase)/2.)
            data%State%Annular%NeededVolumeAnnular=data%State%Annular%AbopAnnular*(data%State%Annular%IDAnnularBase-data%State%Annular%IDAnnular)/(2*231.)
            data%State%RAM(1)%vdis_bottles=0.
            data%State%RAM(1)%fvr_air=0.
            data%State%RAM(1)%vdis_elecp=0.

            data%State%Annular%AnnularIsOpening = .true.
            data%State%Annular%AnnularIsClosing = .false.
            
            !write(*,*) 'open 2'
            
            endif

        

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



 

!===================================================
!                        BOP 
!===================================================
if (data%State%Annular%Annular_closed==0) then    !bop closing
    !write(*,*) 'AnnularIsClosing,AnnularIsOpening' , AnnularIsClosing,AnnularIsOpening
     call bop_codeAnnular(1)   !ramtype=4       1=RNUMBER
endif    !bop is closing
!================================================================
if (data%State%Annular%Annular_closed==1) then
    data%State%RAM(1)%Q=0
    !p_bop=pram_reg
    data%State%AnnularComputational%p_annular=data%State%AnnularComputational%pa_annular
endif

data%State%RAM(1)%timecounter_ram=data%State%RAM(1)%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(1)%time,data%State%RAM(1)%Q,data%State%RAM(1)%vdis_tot,p_acc, &
!         pram_reg,Pannular_reg,data%State%RAM(1)%p_bop,IDshearBop, &
!         IDPipeRam1,IDPipeRam2,IDAnnular
!60       FORMAT(11(f18.5))
         
         
        !   call sleepqq(100)
         
         
                    !CALL CPU_TIME(Annular_EndTime) 
                    !
                    !
                    !data%State%Pump(1)%INT_CPU_TIME=IDINT((Annular_EndTime-Annular_StartTime)*1000.)
                    !data%State%Pump(1)%Dt_ref=IDINT(DeltaT_BOP*1000.)
                    !
                    !call sleepqq(data%State%Pump(1)%Dt_ref-data%State%Pump(1)%INT_CPU_TIME)
         
         
         
         
          
if (data%State%Annular%Annular_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. (UpperRamsValve==1. .and. UpperRamsFailureMalf==0) .or. (UpperRamsValve==-1.0 .and. UpperRamsFailureMalf==0) .or. ChokeLineValve==1. .or. ChokeLineValve==-1.0 .or. KillLineValve==1. .or. KillLineValve==-1.0) then
        data%State%Annular%finished_Annular=1
 !   endif
endif          
          
        !   if (IsStopped == .true.) return 

          
    ! end do  loop5   !while finished_Annular==0
    
    
    
    
    
    if ( data%State%Annular%finished_Annular==1 .and. data%State%RAM(1)%Bottles_Charged_MalfActive==.true.) then
        call bop_codeAnnular(1)   !ramtype=4       1=RNUMBER
        ! call sleepqq(100)
    endif
    
END SUBROUTINE ANNULAR_SUB2