subroutine TD_RemoveComponents
    
    Use CStringConfigurationVariables
    use UnitySignalVariables
    use UnitySignalsModule
    ! Use CStringUpdateVariables
    ! Use CSafetyValveEnumVariables
    ! use UnitySignalsModule
    ! use UnitySignalsModuleVariables
    ! use OperationScenariosModule
    ! use OperationScenariosModule !!CElevator...
    use OperationScenariosModule
    ! use UnitySignalsModule
    ! Use CTdsElevatorModesEnumVariables
    use CHoistingVariables
    use SimulationVariables
    Use TD_DrillStemComponents
    use SimulationVariables !@
    use SimulationVariables !@
    Use TD_StringConnectionData
  
   

   implicit none
   
   integer :: i , TD_NumOfRemovedComponents
   
   
   
!====================================================
!    Remove Safety Valve & Kelly (OPERATION_DRILL)
!====================================================
   
   if ( data%Configuration%Hoisting%DriveType==1 .and. Get_OperationCondition()==OPERATION_DRILL .and. Get_KellyConnection() /= KELLY_CONNECTION_STRING ) then
       data%State%TD_Count%KellyNewRemove = 1
   else
       data%State%TD_Count%KellyNewRemove = 0
       data%State%TD_Count%KellyOldRemove = 0
    end if
   if ( data%Configuration%Hoisting%DriveType==1 .and. Get_OperationCondition()==OPERATION_DRILL .and. Get_KellyConnection() /= KELLY_CONNECTION_STRING .and. data%State%TD_Count%KellyNewRemove/=data%State%TD_Count%KellyOldRemove ) then
       !print* , 'Remove Safety Valve & Kelly='
       TD_NumOfRemovedComponents = 1
       Do i= data%State%TD_String%DrillStemComponentsNumbs , (data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents+1) , -1
         data%State%TD_DrillStems(i)%ComponentType   = 0       !????????????
         data%State%TD_DrillStems(i)%Id              = 0.
         data%State%TD_DrillStems(i)%Od              = 0.
         data%State%TD_DrillStems(i)%Area            = 0.
         data%State%TD_DrillStems(i)%RtoolJoint      = 0.
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.
         data%State%TD_DrillStems(i)%Length          = 0.
         data%State%TD_DrillStems(i)%LengthIni       = 0.
         data%State%TD_DrillStems(i)%WeightperLength = 0.
         data%State%TD_DrillStems(i)%Weight          = 0.
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents
       
       
       i = data%State%TD_String%StringConfigurationCount
       if ( data%State%TD_DrillStem(i)%Numbs>1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 6
           data%State%TD_DrillStem(i)%Numbs           = data%State%TD_DrillStem(i)%Numbs-1
           data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStem(i)%Id           ![ft]
           data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStem(i)%Od           ![ft]
           data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStem(i)%Length       ![ft]
           data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStem(i)%WeightperLength  
           data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount
       else if ( data%State%TD_DrillStem(i)%Numbs==1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 6  
           data%State%TD_DrillStem(i)%Numbs           = 0
           data%State%TD_DrillStem(i)%Id              = 0.   
           data%State%TD_DrillStem(i)%Od              = 0.   
           data%State%TD_DrillStem(i)%Length          = 0.
           data%State%TD_DrillStem(i)%WeightperLength = 0.
           data%State%TD_DrillStem(i)%TotalLength     = 0.
           data%State%TD_DrillStem(i)%TotalWeight     = 0.
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount-1
       end if
       
       
       data%State%TD_Count%KellyOldRemove = data%State%TD_Count%KellyNewRemove
       !print* , 'Remove Safety Valve & Kelly (OPERATION_DRILL)'
   end if
   
   
   
   
   
   
   
!====================================================
!                   Remove Single
!====================================================
   
   if ( Get_StringUpdate() == STRING_UPDATE_REMOVE_SINGLE ) then
       !print* , 'STRING_UPDATE_REMOVE_SINGLE='
       TD_NumOfRemovedComponents = 1
       Do i= data%State%TD_String%DrillStemComponentsNumbs , (data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents+1) , -1
         data%State%TD_DrillStems(i)%ComponentType   = 0       !????????????
         data%State%TD_DrillStems(i)%Id              = 0.
         data%State%TD_DrillStems(i)%Od              = 0.
         data%State%TD_DrillStems(i)%Area            = 0.
         data%State%TD_DrillStems(i)%RtoolJoint      = 0.
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.
         data%State%TD_DrillStems(i)%Length          = 0.
         data%State%TD_DrillStems(i)%LengthIni       = 0.
         data%State%TD_DrillStems(i)%WeightperLength = 0.
         data%State%TD_DrillStems(i)%Weight          = 0.
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents
       
       
       i = data%State%TD_String%StringConfigurationCount
       if ( data%State%TD_DrillStem(i)%Numbs>1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 3     
           data%State%TD_DrillStem(i)%Numbs           = data%State%TD_DrillStem(i)%Numbs-1
           data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStem(i)%Id   ![ft]
           data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStem(i)%Od   ![ft]
           data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount
       else if ( data%State%TD_DrillStem(i)%Numbs==1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 3
           data%State%TD_DrillStem(i)%Numbs           = 0
           data%State%TD_DrillStem(i)%Id              = 0.   
           data%State%TD_DrillStem(i)%Od              = 0.   
           data%State%TD_DrillStem(i)%Length          = 0.
           data%State%TD_DrillStem(i)%WeightperLength = 0.
           data%State%TD_DrillStem(i)%TotalLength     = 0.
           data%State%TD_DrillStem(i)%TotalWeight     = 0.
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount-1
       end if
       
       
       Call Set_StringUpdate(STRING_UPDATE_NEUTRAL)
       !print* , 'Remove Single'
   end if
   
   
   
   
   
   
   
   
!====================================================
!                   Remove Stand
!====================================================
   
   if ( Get_StringUpdate() == STRING_UPDATE_REMOVE_STAND ) then
       
       TD_NumOfRemovedComponents = 3
       Do i= data%State%TD_String%DrillStemComponentsNumbs , (data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents+1) , -1
         data%State%TD_DrillStems(i)%ComponentType   = 0       !????????????
         data%State%TD_DrillStems(i)%Id              = 0.
         data%State%TD_DrillStems(i)%Od              = 0.
         data%State%TD_DrillStems(i)%Area            = 0.
         data%State%TD_DrillStems(i)%RtoolJoint      = 0.
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.
         data%State%TD_DrillStems(i)%Length          = 0.
         data%State%TD_DrillStems(i)%LengthIni       = 0.
         data%State%TD_DrillStems(i)%WeightperLength = 0.
         data%State%TD_DrillStems(i)%Weight          = 0.
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents
       
       
       i = data%State%TD_String%StringConfigurationCount
       if ( data%State%TD_DrillStem(i)%Numbs>3 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 3
           data%State%TD_DrillStem(i)%Numbs           = data%State%TD_DrillStem(i)%Numbs-3
           data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStem(i)%Id   ![ft]
           data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStem(i)%Od   ![ft]
           data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount
       else if ( data%State%TD_DrillStem(i)%Numbs==3 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 3
           data%State%TD_DrillStem(i)%Numbs           = 0
           data%State%TD_DrillStem(i)%Id              = 0.  
           data%State%TD_DrillStem(i)%Od              = 0.   
           data%State%TD_DrillStem(i)%Length          = 0.
           data%State%TD_DrillStem(i)%WeightperLength = 0.
           data%State%TD_DrillStem(i)%TotalLength     = 0.
           data%State%TD_DrillStem(i)%TotalWeight     = 0.
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount-1
       end if
       
       
       
       Call Set_StringUpdate(STRING_UPDATE_NEUTRAL)
       !print* , 'Remove Stand'
   end if
   
   
   
   
   

   
   
!====================================================
!                   Remove IBOP
!====================================================
   
   !if ( Get_Ibop() == IBOP_REMOVE ) then
   !    data%State%TD_Count%IBOPNewRemove = 1
   !else
   !    data%State%TD_Count%IBOPNewRemove = 0
   !    data%State%TD_Count%IBOPOldRemove = 0
   !end if
   if ( Get_Ibop() == IBOP_REMOVE .and. data%State%TD_Count%IBOPOldRemove==0 ) then
       
       TD_NumOfRemovedComponents = 1
       Do i= data%State%TD_String%DrillStemComponentsNumbs , (data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents+1) , -1
         data%State%TD_DrillStems(i)%ComponentType   = 0       !????????????
         data%State%TD_DrillStems(i)%Id              = 0.
         data%State%TD_DrillStems(i)%Od              = 0.
         data%State%TD_DrillStems(i)%Area            = 0.
         data%State%TD_DrillStems(i)%RtoolJoint      = 0.
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.
         data%State%TD_DrillStems(i)%Length          = 0.
         data%State%TD_DrillStems(i)%LengthIni       = 0.
         data%State%TD_DrillStems(i)%WeightperLength = 0.
         data%State%TD_DrillStems(i)%Weight          = 0.
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents
       
       
       i = data%State%TD_String%StringConfigurationCount
       if ( data%State%TD_DrillStem(i)%Numbs>1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 5
           data%State%TD_DrillStem(i)%Numbs           = data%State%TD_DrillStem(i)%Numbs-1
           data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStem(i)%Id           ![ft]
           data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStem(i)%Od           ![ft]
           data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStem(i)%Length       ![ft]
           data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStem(i)%WeightperLength  
           data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount
       else if ( data%State%TD_DrillStem(i)%Numbs==1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 5
           data%State%TD_DrillStem(i)%Numbs           = 0
           data%State%TD_DrillStem(i)%Id              = 0.
           data%State%TD_DrillStem(i)%Od              = 0. 
           data%State%TD_DrillStem(i)%Length          = 0.
           data%State%TD_DrillStem(i)%WeightperLength = 0.
           data%State%TD_DrillStem(i)%TotalLength     = 0.
           data%State%TD_DrillStem(i)%TotalWeight     = 0.
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount-1
       end if
       

       !data%State%TD_Count%IBOPOldRemove = data%State%TD_Count%IBOPNewRemove
       
       if ( Get_ElevatorConnection() /= ELEVATOR_CONNECTION_STRING .or. (Get_TdsConnectionModes()/=TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()/=TDS_ELEVATOR_CONNECTION_STRING) ) then
           data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight-data%State%TD_StConn%IBOPLength
       end if
       !print* , 'Remove IBOP'
   end if
   
   if ( Get_Ibop() == IBOP_REMOVE ) then
       data%State%TD_Count%IBOPOldRemove = 1
   else
       data%State%TD_Count%IBOPOldRemove = 0
   end if
   
    
   
   
   
   
   

!====================================================
!       Remove Safety Valve (OPERATION_TRIP)
!====================================================
   
   !if ( data%State%TD_StConn%OldOperationCondition==1 .and. data%State%TD_StConn%KellyDriveTypeMode==1 .and. Get_SafetyValve() == SAFETY_VALVE_REMOVE ) then
   !    data%State%TD_Count%SafetyValveNewRemove = 1
   !else
   !    data%State%TD_Count%SafetyValveNewRemove = 0
   !    data%State%TD_Count%SafetyValveOldRemove = 0
   !end if
   !print* , 'Get_SafetyValve()=' , Get_SafetyValve()
   if ( data%State%TD_StConn%KellyDriveTypeMode/=0 .and. data%State%TD_StConn%OldOperationCondition/=0 .and. Get_SafetyValve() == SAFETY_VALVE_REMOVE .and. data%State%TD_Count%SafetyValveOldRemove==0 ) then
       
       TD_NumOfRemovedComponents = 1
       Do i= data%State%TD_String%DrillStemComponentsNumbs , (data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents+1) , -1
         data%State%TD_DrillStems(i)%ComponentType   = 0       !????????????
         data%State%TD_DrillStems(i)%Id              = 0.
         data%State%TD_DrillStems(i)%Od              = 0.
         data%State%TD_DrillStems(i)%Area            = 0.
         data%State%TD_DrillStems(i)%RtoolJoint      = 0.
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.
         data%State%TD_DrillStems(i)%Length          = 0.
         data%State%TD_DrillStems(i)%LengthIni       = 0.
         data%State%TD_DrillStems(i)%WeightperLength = 0.
         data%State%TD_DrillStems(i)%Weight          = 0.
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs-TD_NumOfRemovedComponents
       
       
       i = data%State%TD_String%StringConfigurationCount
       if ( data%State%TD_DrillStem(i)%Numbs>1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 7
           data%State%TD_DrillStem(i)%Numbs           = data%State%TD_DrillStem(i)%Numbs-1
           data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStem(i)%Id           ![ft]
           data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStem(i)%Od           ![ft]
           data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStem(i)%Length       ![ft]
           data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
           data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount
       else if ( data%State%TD_DrillStem(i)%Numbs==1 ) then
           data%State%TD_DrillStem(i)%ComponentType   = 7
           data%State%TD_DrillStem(i)%Numbs           = 0
           data%State%TD_DrillStem(i)%Id              = 0.
           data%State%TD_DrillStem(i)%Od              = 0.  
           data%State%TD_DrillStem(i)%Length          = 0.
           data%State%TD_DrillStem(i)%WeightperLength = 0.
           data%State%TD_DrillStem(i)%TotalLength     = 0.
           data%State%TD_DrillStem(i)%TotalWeight     = 0.
           data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount-1
       end if
   !print* , 'Get_OperationCondition()=' , Get_OperationCondition()
   !print* , 'data%State%TD_StConn%OldOperationCondition=' , data%State%TD_StConn%OldOperationCondition
   !print* , 'data%State%TD_Count%SafetyValveNewRemove=' , data%State%TD_Count%SafetyValveNewRemove
   !print* , 'data%State%TD_Count%SafetyValveOldRemove=' , data%State%TD_Count%SafetyValveOldRemove
       !data%State%TD_Count%SafetyValveOldRemove = data%State%TD_Count%SafetyValveNewRemove
       
       if ( Get_ElevatorConnection() /= ELEVATOR_CONNECTION_STRING .or. (Get_TdsConnectionModes()/=TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()/=TDS_ELEVATOR_CONNECTION_STRING) ) then
           data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight-data%State%TD_StConn%SafetyValveLength
       end if
       
   end if
   
   if ( Get_SafetyValve() == SAFETY_VALVE_REMOVE ) then
       data%State%TD_Count%SafetyValveOldRemove = 1
   else
       data%State%TD_Count%SafetyValveOldRemove = 0
   end if
   
   if ( data%State%TD_StConn%KellyDriveTypeMode==1 ) then
       data%State%TD_StConn%OldOperationCondition = 1
   else
       data%State%TD_StConn%OldOperationCondition = 0
   end if
   
   !print* , 'data%State%TD_StConn%KellyDriveTypeMode=' , data%State%TD_StConn%KellyDriveTypeMode
   !print* , 'data%State%TD_StConn%OldOperationCondition2=' , data%State%TD_StConn%OldOperationCondition
   !print* , 'data%State%TD_Count%SafetyValveNewRemove2=' , data%State%TD_Count%SafetyValveNewRemove
   !print* , 'data%State%TD_Count%SafetyValveOldRemove2=' , data%State%TD_Count%SafetyValveOldRemove
   
   
end subroutine