
 SUBROUTINE DEALLOCATE_ARRAYS_CHOKE()
    
USE CHOKEVARIABLES
use SimulationVariables !@

implicit none
write(*,*) 'deallocateeeeeeeeeeeee'
!===========================================================================
!                   	AIR PUMP LOSSES INPUT
!===========================================================================
!if(allocated(PIPINGS_AIRPUMP)) deallocate(PIPINGS_AIRPUMP)  

DEALLOCATE (data%State%AirPumpLine%PIPINGS_AIRPUMP,data%State%AirPumpLine%DIAM_AIR_INCH,  &
            data%State%AirPumpLine%Re_air,data%State%AirPumpLine%AREA_AIR,data%State%AirPumpLine%LENGT_AIR,data%State%AirPumpLine%ROUGHNESS_AIRPLINE,data%State%AirPumpLine%REL_ROUGHAIR,  &
           data%State%AirPumpLine%fric_air,data%State%AirPumpLine%fricloss_air)
   !================================================================ 
DEALLOCATE (data%State%AirPumpLine%MINORS_AIRPUMP,data%State%AirPumpLine%MINORDIAM_AIR_INCH,  &
            data%State%AirPumpLine%MINORAREA_AIR,data%State%AirPumpLine%LF_AIR,data%State%AirPumpLine%CV_AIR,data%State%AirPumpLine%NOTE_AIR &
            ,data%State%AirPumpLine%minlosspa_air,data%State%AirPumpLine%minloss_air)   

    END       
    
    
    
    
    
    
 SUBROUTINE LOSS_INPUTS_CHOKE()
USE CHOKEVARIABLES
use SimulationVariables !@
implicit none
    Integer I


!===========================================================================
!                   	AIR PUMP LOSSES INPUT
!===========================================================================  
data%State%AirPumpLine%NO_PIPINGS_AIRPLINE=1

ALLOCATE (data%State%AirPumpLine%PIPINGS_AIRPUMP(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE,3))
                !			ID(INCH)    L(MM)	  ROUGHNESS(MM)=e   DESCRIPTION	
data%State%AirPumpLine%PIPINGS_AIRPUMP(1,1:3)=		 (/0.5,      60960.,   0.03/)            !Avg.acc.distance
 
!60960= 200 ft

  
  ALLOCATE (data%State%AirPumpLine%DIAM_AIR_INCH(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),data%State%AirPumpLine%Re_air(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),data%State%AirPumpLine%AREA_AIR(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),  &
            data%State%AirPumpLine%LENGT_AIR(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),data%State%AirPumpLine%ROUGHNESS_AIRPLINE(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),data%State%AirPumpLine%REL_ROUGHAIR(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),  &
           data%State%AirPumpLine%fric_air(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE),data%State%AirPumpLine%fricloss_air(data%State%AirPumpLine%NO_PIPINGS_AIRPLINE))



DO I=1,data%State%AirPumpLine%NO_PIPINGS_AIRPLINE
    data%State%AirPumpLine%DIAM_AIR_INCH(I)=data%State%AirPumpLine%PIPINGS_AIRPUMP(I,1)
    data%State%AirPumpLine%LENGT_AIR(I)=data%State%AirPumpLine%PIPINGS_AIRPUMP(I,2)
    data%State%AirPumpLine%ROUGHNESS_AIRPLINE(I)=data%State%AirPumpLine%PIPINGS_AIRPUMP(I,3)
    
    
    data%State%AirPumpLine%AREA_AIR(I)=PI*(data%State%AirPumpLine%DIAM_AIR_INCH(I)*0.0254)**2/4   !D(in), AREA(m)
    data%State%AirPumpLine%REL_ROUGHAIR(I)=data%State%AirPumpLine%ROUGHNESS_AIRPLINE(I)/(data%State%AirPumpLine%DIAM_AIR_INCH(I)*25.4)
    !DIAM_RAMLINE_MM(I)=DIAM_RAMLINE_MM(I)*.001  !  (m)
    data%State%AirPumpLine%LENGT_AIR(I)=data%State%AirPumpLine%LENGT_AIR(I)*.001      !  (m) 
    ENDDO

    
   !================================================================ 
data%State%AirPumpLine%NO_MINORS_AIRPLINE=6

ALLOCATE (data%State%AirPumpLine%MINORS_AIRPUMP(data%State%AirPumpLine%NO_MINORS_AIRPLINE,4))

                    !		ID(INCH)		LF	    CV	NOTE(BAR)      DESCRIPTION	
data%State%AirPumpLine%MINORS_AIRPUMP(1,1:4)=      (/2.,			10.,	0.,	    0./)       !Acc.tee	
data%State%AirPumpLine%MINORS_AIRPUMP(2,1:4)=      (/2.,			11.,	0.,	    0./)       !elbow	
data%State%AirPumpLine%MINORS_AIRPUMP(3,1:4)=      (/1.,			0.,	    0.,	    3.4/)      !filter
data%State%AirPumpLine%MINORS_AIRPUMP(4,1:4)=      (/2.,			0.,	    105.,	0./)       !valve	
data%State%AirPumpLine%MINORS_AIRPUMP(5,1:4)=      (/1.,			0.,	    9.2,	0./)       !valve	
data%State%AirPumpLine%MINORS_AIRPUMP(6,1:4)=      (/2.,			6.4,	0.,	    0./)       !unionA  
  

    
  ALLOCATE (data%State%AirPumpLine%MINORDIAM_AIR_INCH(data%State%AirPumpLine%NO_MINORS_AIRPLINE),data%State%AirPumpLine%MINORAREA_AIR(data%State%AirPumpLine%NO_MINORS_AIRPLINE),  &
            data%State%AirPumpLine%LF_AIR(data%State%AirPumpLine%NO_MINORS_AIRPLINE),data%State%AirPumpLine%CV_AIR(data%State%AirPumpLine%NO_MINORS_AIRPLINE),data%State%AirPumpLine%NOTE_AIR(data%State%AirPumpLine%NO_MINORS_AIRPLINE) &
            ,data%State%AirPumpLine%minlosspa_air(data%State%AirPumpLine%NO_MINORS_AIRPLINE),data%State%AirPumpLine%minloss_air(data%State%AirPumpLine%NO_MINORS_AIRPLINE))   
 

 
DO I=1,data%State%AirPumpLine%NO_MINORS_AIRPLINE
    data%State%AirPumpLine%MINORDIAM_AIR_INCH(I)=data%State%AirPumpLine%MINORS_AIRPUMP(I,1)
    data%State%AirPumpLine%LF_AIR(I)=data%State%AirPumpLine%MINORS_AIRPUMP(I,2)
    data%State%AirPumpLine%CV_AIR(I)=data%State%AirPumpLine%MINORS_AIRPUMP(I,3)
    data%State%AirPumpLine%NOTE_AIR(I)=data%State%AirPumpLine%MINORS_AIRPUMP(I,4)
    
    
    data%State%AirPumpLine%MINORAREA_AIR(I)=PI*(data%State%AirPumpLine%MINORDIAM_AIR_INCH(I)*0.0254)**2/4.   !D(in), AREA(m)
ENDDO    
    
 
    END   
    
    
    


! SUBROUTINE PIPE_RAMS_CHOKE(CHNUMBER)
!     USE CHOKEVARIABLES
! use ConfigurationVariables !@
!     Use CSimulationVariables
!     implicit none
!     INTEGER CHNUMBER
!     Integer I
    
!     loop3: do while (ABS(data%EquipmentControl%ChokeControlPanel%ChokeControlLever)==1.0 .AND. data%EquipmentControl%ChokeControlPanel%ChokePanelRigAirSwitch == 1 .AND. data%State%CHOOKE(CHNUMBER)%FailMalf==0 .AND. data%State%AirDrivenPump%ChokeAirFail==0)
!         if (data%EquipmentControl%ChokeControlPanel%ChokeControlLever == 1.0) then
!             data%State%CHOOKE(CHNUMBER)%ChokeIsClosing = .true.
!             data%State%CHOOKE(CHNUMBER)%ChokeIsOpening = .false.
!         endif
!         if (data%EquipmentControl%ChokeControlPanel%ChokeControlLever == -1.0) then 
!             data%State%CHOOKE(CHNUMBER)%ChokeIsOpening = .true.
!             data%State%CHOOKE(CHNUMBER)%ChokeIsClosing = .false.
!         endif
!         data%State%AirPumpLine%TIME=data%State%AirPumpLine%TIME+data%State%AirPumpLine%DeltaT_Choke  !overal time (s)
!         call airpump_code_CHOKE(CHNUMBER)
!         call sleepqq(100)
!         if (IsStopped == .true.) return  
!     end do  loop3   !while finished_Choke==0
!     end
    
 
    
    
    
    
    SUBROUTINE airpump_code_CHOKE(CHNUMBER)    
        USE CHOKEVARIABLES
use SimulationVariables !@
        use CChokeManifold
        use SimulationVariables
        use CSounds
        USE CChokeProblemsVariables
        implicit none
            Integer I

        INTEGER CHNUMBER

        data%State%AirDrivenPump%QAIR_PUMP=data%State%AirPumpLine%QITER+.1   !(gpm)  maximum flow for the start
        data%State%AirPumpLine%diffp_air=-10
        data%State%AirPumpLine%losses_air=10

        !===================================================================
        !                       AIR OPERATED PUMP
        !                       MODEL 10-6000W030 RATIO 55:1   
        !=================for air consumption at 100 psig=================== 
        do while (data%State%AirPumpLine%diffp_air<0)
            data%State%AirDrivenPump%QAIR_PUMP=data%State%AirDrivenPump%QAIR_PUMP-.1
            ! Qup=data%State%AirDrivenPump%QAIR_PUMP;
            !cc1 = 1354; cc2 = -2066; cc3 = -2109; cc4 = -513.6; cc5 = 5935     FOR OUTPUT IN GPM
            data%State%AirPumpLine%P_AIRP=data%State%AirDrivenPump%cc1*(data%State%AirDrivenPump%QAIR_PUMP**4) + data%State%AirDrivenPump%cc2*(data%State%AirDrivenPump%QAIR_PUMP**3) + data%State%AirDrivenPump%cc3*(data%State%AirDrivenPump%QAIR_PUMP**2) + data%State%AirDrivenPump%cc4*data%State%AirDrivenPump%QAIR_PUMP + data%State%AirDrivenPump%cc5 !(psig)
            ! kinetic_air=sg*wdens*(QAIR_PUMP*6.30902e-5/((1/4.)*pi*(2*0.254e-1)**2))**2/(2*6895) !(psi)
            data%State%AirPumpLine%diffp_air= data%State%AirPumpLine%P_AIRP - data%State%AirPumpLine%Pdownstrem
        end do    !returns Qup

        do while (abs((data%State%AirPumpLine%diffp_air-data%State%AirPumpLine%losses_air)/data%State%AirPumpLine%diffp_air)>data%State%AirPumpLine%TOL_AIR) !finding correct QAIR_pump for 1 timecounter_ram
            if (data%State%AirPumpLine%diffp_air-data%State%AirPumpLine%losses_air>0) then
                data%State%AirDrivenPump%QAIR_PUMP=data%State%AirDrivenPump%QAIR_PUMP+.005
            else
                data%State%AirDrivenPump%QAIR_PUMP=data%State%AirDrivenPump%QAIR_PUMP-.005
            endif
            !===================================================================
            !                       AIR OPERATED PUMP
            !                       MODEL 10-6000W030 RATIO 55:1   
            !=================for air consumption at 100 psig=================== 
            data%State%AirPumpLine%P_AIRP=data%State%AirDrivenPump%cc1*(data%State%AirDrivenPump%QAIR_PUMP**4) + data%State%AirDrivenPump%cc2*(data%State%AirDrivenPump%QAIR_PUMP**3) + data%State%AirDrivenPump%cc3*(data%State%AirDrivenPump%QAIR_PUMP**2) + data%State%AirDrivenPump%cc4*data%State%AirDrivenPump%QAIR_PUMP + data%State%AirDrivenPump%cc5 !(psig)
            !kinetic_air=sg*wdens*(QAIR_PUMP*6.30902e-005/((1/4.)*pi*(2*0.254e-1)**2))**2/(2*6895) !(psi)
            data%State%AirPumpLine%diffp_air= data%State%AirPumpLine%P_AIRP - data%State%AirPumpLine%Pdownstrem
            !===========================LOSSES====================================
            do i=1,data%State%AirPumpLine%NO_PIPINGS_AIRPLINE
                data%State%AirPumpLine%Re_air(i)=data%State%AirDrivenPump%QAIR_PUMP*6.30902e-005*data%State%AirPumpLine%DIAM_AIR_INCH(I)*0.0254/(data%State%AirPumpLine%AREA_AIR(i)*nu)
            enddo
            do i=1,data%State%AirPumpLine%NO_PIPINGS_AIRPLINE
                if (data%State%AirPumpLine%Re_air(i)<Re_cr) then
                    data%State%AirPumpLine%fric_air(i)=64/data%State%AirPumpLine%Re_air(i)
                else
                    data%State%AirPumpLine%fric_air(i)=1/(-1.8*log10((data%State%AirPumpLine%REL_ROUGHAIR(i)/3.7)**1.11+6.9/data%State%AirPumpLine%Re_air(i)))**2
                endif
                data%State%AirPumpLine%fricloss_air(i)=((data%State%AirPumpLine%fric_air(i)*(wdens*sg*data%State%AirPumpLine%LENGT_AIR(i)*(data%State%AirDrivenPump%QAIR_PUMP*6.30902e-005/data%State%AirPumpLine%AREA_AIR(i))**2))/(2*data%State%AirPumpLine%DIAM_AIR_INCH(I)*0.0254))/6895
            enddo

            data%State%AirPumpLine%pipe_loss1air=sum(data%State%AirPumpLine%fricloss_air)  !loss before regulator(psi)

            do i=1,data%State%AirPumpLine%NO_MINORS_AIRPLINE
                if (data%State%AirPumpLine%LF_AIR(i)/=0) then
                    data%State%AirPumpLine%minlosspa_air(i)=data%State%AirPumpLine%LF_AIR(i)*wdens*sg*(data%State%AirDrivenPump%QAIR_PUMP*6.30902e-005/data%State%AirPumpLine%MINORAREA_AIR(i))**2/2  !(Pa)
                    data%State%AirPumpLine%minloss_air(i)=data%State%AirPumpLine%minlosspa_air(i)/6895 !(psi)
                elseif (data%State%AirPumpLine%CV_AIR(i)/=0) then
                    data%State%AirPumpLine%minlosspa_air(i)=1000*sg*((11.7*data%State%AirDrivenPump%QAIR_PUMP*6.30902e-005*3600)/(data%State%AirPumpLine%CV_AIR(i)))**2 !(pa)
                    data%State%AirPumpLine%minloss_air(i)=data%State%AirPumpLine%minlosspa_air(i)/6895 !(psi)
                else
                    data%State%AirPumpLine%minlosspa_air(i)=data%State%AirPumpLine%NOTE_AIR(i)*1e5 !(pa)
                    data%State%AirPumpLine%minloss_air(i)=data%State%AirPumpLine%minlosspa_air(i)/6895 !(psi)
                endif
            enddo
    
            data%State%AirPumpLine%minor_loss1air=sum(data%State%AirPumpLine%minloss_air)  !loss before regulator(psi)
            data%State%AirPumpLine%static_loss1air=0
            data%State%AirPumpLine%losses_air=data%State%AirPumpLine%pipe_loss1air+data%State%AirPumpLine%minor_loss1air+data%State%AirPumpLine%static_loss1air !(psi)
            !=========================================================================
        end do  !returns correct QAIR_PUMP for 1 counter

        data%State%AirPumpLine%QITER=data%State%AirDrivenPump%QAIR_PUMP
        data%State%AirDrivenPump%QAIR_PUMP=data%State%AirDrivenPump%QAIR_PUMP*(data%EquipmentControl%ChokeControlPanel%ChokeRateControlKnob/10.)       ! final Q (gpm)        
        data%State%Choke%Cumulative_AirVolume_Choke= data%State%Choke%Cumulative_AirVolume_Choke + ((data%State%AirDrivenPump%QAIR_PUMP * data%State%AirPumpLine%DeltaT_Choke / 60.0 ) / (1.5*0.004329004) )        ! =strokes
        
        if ( data%State%Choke%Cumulative_AirVolume_Choke > 1.0 ) then
            data%State%Choke%SoundChokePump= 60
            data%State%Choke%Cumulative_AirVolume_Choke= data%State%Choke%Cumulative_AirVolume_Choke - 1.0
        else
            data%State%Choke%SoundChokePump= 0.0
        endif   
        data%State%AirPumpLine%DELTAV_AIR=data%State%AirDrivenPump%QAIR_PUMP*data%State%AirPumpLine%DeltaT_Choke/60  !(galon) delta_t=1sec , Q(gpm)
        if (data%State%CHOOKE(CHNUMBER)%ChokeIsClosing)  data%State%CHOOKE(CHNUMBER)%PassedCourse=data%State%CHOOKE(CHNUMBER)%PassedCourse + (data%State%AirPumpLine%DELTAV_AIR*3785.412/data%State%AirPumpLine%Acylinder)!*(ChokeRateControlKnob/10.)        ! 3785.412 : GALON TO CM^3
        if (data%State%CHOOKE(CHNUMBER)%ChokeIsOpening)  data%State%CHOOKE(CHNUMBER)%PassedCourse=data%State%CHOOKE(CHNUMBER)%PassedCourse - (data%State%AirPumpLine%DELTAV_AIR*3785.412/data%State%AirPumpLine%Acylinder)!*(ChokeRateControlKnob/10.)
        !DeltaT_Choke= 1sec or 2sec
        !((((((((IN OUTER LOOP))))))
        !===============AIR PUMP OUTPUTS=========================
        data%State%AirPumpLine%alpha_timeair=data%State%AirPumpLine%TIME  ! overal time (s)
        data%State%AirPumpLine%alpha_Pdownstrem=data%State%AirPumpLine%Pdownstrem
        data%State%AirPumpLine%alpha_pairp=data%State%AirPumpLine%P_AIRP
        data%State%AirPumpLine%alpha_Qair=data%State%AirDrivenPump%QAIR_PUMP
        !write(*,*) 'diffp_air=',diffp_air
        !pause
        data%State%AirPumpLine%alpha_diffpair=data%State%AirPumpLine%diffp_air
        data%State%AirPumpLine%alpha_lossesair=data%State%AirPumpLine%losses_air
        !========================================================
        !OPEN(150,FILE='CHOKE_AIRPUMP_OUTPUTS.DAT')

        if (data%State%CHOOKE(CHNUMBER)%ChokeIsClosing .AND. data%State%CHOOKE(CHNUMBER)%PassedCourse>data%State%AirPumpLine%CourseBase) then
            data%State%CHOOKE(CHNUMBER)%PassedCourse=data%State%AirPumpLine%CourseBase
            data%State%Choke%SoundChokePump= 0.0
        endif
        if (data%State%CHOOKE(CHNUMBER)%ChokeIsOpening .AND. data%State%CHOOKE(CHNUMBER)%PassedCourse<0.) then
            data%State%CHOOKE(CHNUMBER)%PassedCourse=0.
            data%State%Choke%SoundChokePump= 0.0
        endif
        
        call SetSoundChokePump(data%State%Choke%SoundChokePump)
        data%State%CHOOKE(CHNUMBER)%PercentClose= data%State%CHOOKE(CHNUMBER)%PassedCourse/data%State%AirPumpLine%CourseBase
        IF (data%EquipmentControl%ChokeControlPanel%Choke1LED==1) THEN
            data%EquipmentControl%ChokeControlPanel%ChokePosition= (1 - data%State%Choke%GaugeChokePositionMailf) * data%State%CHOOKE(1)%PercentClose*10
        ELSE       ! Choke2LED==1
            data%EquipmentControl%ChokeControlPanel%ChokePosition= (1 - data%State%Choke%GaugeChokePositionMailf) * data%State%CHOOKE(2)%PercentClose*10
        ENDIF
        !CALL SetHydraulicChock1(nint(MIN(data%State%CHOOKE(1)%PercentClose / 0.91 , 1.0)*100))     
        ! CALL SetHydraulicChock2(nint(MIN(data%State%CHOOKE(2)%PercentClose / 0.91 , 1.0)*100)) 
        ! .91 >> 9 percent clearance

        ! =================== calculating Area             
        ! AreaChoke=0.5
        ! .91 >> 9 percent clearance

        data%State%CHOOKE(1)%AreaChoke=0.01334635-(0.01334635* MIN(data%State%CHOOKE(1)%PercentClose / 0.91 , 1.0)) !ft^2     0.01334635 is ChokeAreaFullyOpen (ft^2)
        data%State%CHOOKE(2)%AreaChoke=0.01334635-(0.01334635* MIN(data%State%CHOOKE(2)%PercentClose/ 0.91 , 1.0)) !ft^2
        data%State%Choke%HydraulicChoke1WashoutCoef= data%State%Choke%HydraulicChoke1WashoutCoef * data%State%CHOOKE(1)%WashoutMalf    
        data%State%Choke%HydraulicChoke1WashoutCoef= MIN( 0.5 , data%State%Choke%HydraulicChoke1WashoutCoef+ data%State%CHOOKE(1)%WashoutMalf*(0.5/(60.0/data%State%AirPumpLine%DeltaT_Choke)) ) ! 0.5=maximum washout coef , 60.0 sec= 1min duration time                
        data%State%Choke%HydraulicChoke2WashoutCoef= data%State%Choke%HydraulicChoke2WashoutCoef * data%State%CHOOKE(2)%WashoutMalf    
        data%State%Choke%HydraulicChoke2WashoutCoef= MIN( 0.5 , data%State%Choke%HydraulicChoke2WashoutCoef+ data%State%CHOOKE(2)%WashoutMalf*(0.5/(60.0/data%State%AirPumpLine%DeltaT_Choke)) ) ! 0.5=maximum washout coef , 60.0 sec= 1min duration time
        !write(*,*) 'HydraulicChoke1WashoutCoef=' , HydraulicChoke1WashoutCoef
        
        IF (data%State%CHOOKE(1)%PlugMalf == 1) THEN
            data%State%Choke%Present_HydraulicChoke1Plug= data%State%Choke%Present_HydraulicChoke1Plug * data%State%CHOOKE(1)%PlugMalf 
            ! integer: HydraulicChoke1PluggedPercent,HydraulicChoke1PluggedPercent_Old,PlugTimeCounter,ChokePlugTimeDelay
            ! real: Present_HydraulicChoke1Plug,DeltaPlug1Percent
            !write(*,*) 'HydraulicChoke1PluggedPercent=' , HydraulicChoke1PluggedPercent
    
            if ( (data%problems%ChokeProblems%HydraulicChoke1PluggedPercent - data%State%Choke%HydraulicChoke1PluggedPercent_Old) /= 0) then
                data%State%Choke%DeltaPlug1Percent = (REAL(data%problems%ChokeProblems%HydraulicChoke1PluggedPercent)/100.) - data%State%Choke%Present_HydraulicChoke1Plug
                data%State%Choke%Plug1TimeCounter = 0
                !write(*,*) 'DeltaPlug1Percent, Present_HydraulicChoke1Plug  =  ' ,DeltaPlug1Percent , Present_HydraulicChoke1Plug 
            endif
            data%State%Choke%Plug1TimeCounter= data%State%Choke%Plug1TimeCounter + 1
            data%State%Choke%HydraulicChoke1PluggedPercent_Old= data%problems%ChokeProblems%HydraulicChoke1PluggedPercent
    
            if (data%State%Choke%Plug1TimeCounter <= data%State%Choke%ChokePlugTimeDelay) then    !ChokePlugTimeDelay=600
                data%State%Choke%Present_HydraulicChoke1Plug = data%State%Choke%Present_HydraulicChoke1Plug + data%State%CHOOKE(1)%PlugMalf* ((data%State%Choke%DeltaPlug1Percent / real(data%State%Choke%ChokePlugTimeDelay)))     ! real(ChokePlugTimeDelay)= 600.0
            endif
            !write(*,*) 'Present_HydraulicChoke1Plug=' , Present_HydraulicChoke1Plug        
        ENDIF
    
        IF (data%State%CHOOKE(2)%PlugMalf == 1) THEN 
            data%State%Choke%Present_HydraulicChoke2Plug= data%State%Choke%Present_HydraulicChoke2Plug * data%State%CHOOKE(2)%PlugMalf 
            ! integer: HydraulicChoke2PluggedPercent,HydraulicChoke2PluggedPercent_Old,PlugTimeCounter,ChokePlugTimeDelay
            ! real: Present_HydraulicChoke2Plug,DeltaPlug2Percent
            if ( (data%problems%ChokeProblems%HydraulicChoke2PluggedPercent - data%State%Choke%HydraulicChoke2PluggedPercent_Old) /= 0 ) then
                data%State%Choke%DeltaPlug2Percent = (REAL(data%problems%ChokeProblems%HydraulicChoke2PluggedPercent)/100.) - data%State%Choke%Present_HydraulicChoke2Plug
                data%State%Choke%Plug2TimeCounter = 0
            endif
            data%State%Choke%Plug2TimeCounter= data%State%Choke%Plug2TimeCounter + 1
            data%State%Choke%HydraulicChoke2PluggedPercent_Old= data%problems%ChokeProblems%HydraulicChoke2PluggedPercent
            if (data%State%Choke%Plug2TimeCounter <= data%State%Choke%ChokePlugTimeDelay) then    !ChokePlugTimeDelay=600
                data%State%Choke%Present_HydraulicChoke2Plug = data%State%Choke%Present_HydraulicChoke2Plug + data%State%CHOOKE(2)%PlugMalf *((data%State%Choke%DeltaPlug2Percent / real(data%State%Choke%ChokePlugTimeDelay)))     ! real(ChokePlugTimeDelay)= 600.0
            endif
        ENDIF
        ! fully open area is 123/64 in^2 = 0.01334635 ft^2
        data%State%CHOOKE(1)%AreaChoke=data%State%CHOOKE(1)%AreaChoke+(data%State%CHOOKE(1)%WashoutMalf*data%State%Choke%HydraulicChoke1WashoutCoef*data%State%Choke%ChokeAreaFullyOpen/144.0)      ! Initialised in Choke Startup
        
        !write(*,*) 'data%State%CHOOKE(1)%WashoutMalf , data%State%CHOOKE(1)%AreaChoke='  ,data%State%CHOOKE(1)%WashoutMalf , data%State%CHOOKE(1)%AreaChoke
        
        data%State%CHOOKE(1)%AreaChoke=data%State%CHOOKE(1)%AreaChoke-(data%State%CHOOKE(1)%PlugMalf* data%State%Choke%Present_HydraulicChoke1Plug *data%State%CHOOKE(1)%AreaChoke)         ! Initialised in Choke Startup
        
        data%State%CHOOKE(2)%AreaChoke=data%State%CHOOKE(2)%AreaChoke+(data%State%CHOOKE(2)%WashoutMalf*data%State%Choke%HydraulicChoke2WashoutCoef*data%State%Choke%ChokeAreaFullyOpen/144.0)      ! Initialised in Choke Startup
        data%State%CHOOKE(2)%AreaChoke=data%State%CHOOKE(2)%AreaChoke-(data%State%CHOOKE(2)%PlugMalf* data%State%Choke%Present_HydraulicChoke2Plug *data%State%CHOOKE(2)%AreaChoke)         ! Initialised in Choke Startup
        !write(*,*) 'data%State%CHOOKE(2)%WashoutMalf , data%State%CHOOKE(2)%AreaChoke='  ,data%State%CHOOKE(2)%WashoutMalf , data%State%CHOOKE(2)%AreaChoke
        
        
        data%State%CHOOKE(1)%AreaChokeFinal= data%State%CHOOKE(1)%AreaChoke
        data%State%CHOOKE(2)%AreaChokeFinal= data%State%CHOOKE(2)%AreaChoke
        
        !    144: ft^2 to in^2
        CALL SetHydraulicChock1(100 - nint((data%State%CHOOKE(1)%AreaChokeFinal/(data%State%Choke%ChokeAreaFullyOpen/144.))*100))     ! for manifold valve    
        CALL SetHydraulicChock2(100 - nint((data%State%CHOOKE(2)%AreaChokeFinal/(data%State%Choke%ChokeAreaFullyOpen/144.))*100))     ! for manifold valve     
    
    
        !write(*,*) 'data%State%CHOOKE(1)%PercentClose=' , data%State%CHOOKE(1)%PercentClose     ! close percent 0 to 100
        !write(*,*) 'ChokePosition=' , ChokePosition     ! close position 0 to 10 for display
        !write(*,*) 'data%State%CHOOKE(1)%AreaChokeFinal=' , data%State%CHOOKE(1)%AreaChokeFinal       ! Open Area (in^2) with clearance
        !write(*,*) '(ChokeAreaFullyOpen/144.)=' , (ChokeAreaFullyOpen/144.)
        !write(*,*) 'valve value=' , 100 - nint((data%State%CHOOKE(1)%AreaChokeFinal/(ChokeAreaFullyOpen/144.))*100)        ! percent close 0 to 100 with clearance
        !write(*,*) 'Valve(33)%Status=' , Valve(33)%Status       ! T : open , F: close    
        ! =================== calculating Area   
        !write(*,*) 'data%State%CHOOKE(1)%AreaChoke= ' , data%State%CHOOKE(1)%AreaChoke
        !WRITE(150,50) alpha_timeair,alpha_Qair,alpha_pairp, &
        !alpha_Pdownstrem,alpha_diffpair,alpha_lossesair,data%State%CHOOKE(CHNUMBER)%PassedCourse
        !50           FORMAT(7(f15.5))
        !========================================================================================            
        !========================================================================================           
           !  write(*,*) PassedCourse
    end    
