subroutine TD_StringConnectionModes

   use CHoistingVariables
    use SimulationVariables
   use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables
   use UnitySignalVariables
    use UnitySignalsModule
!    use OperationScenariosModule
!    use OperationScenariosModule !!CElevator...
!    Use CSlipsEnumVariables
!    use UnitySignalsModuleVariables
   use SoftwareInputsVariables
!    Use CZeroStringSpeed
!    use UnitySignalsModule
!    Use CTdsElevatorModesEnumVariables
   use OperationScenariosModule
   Use TD_DrillStemComponents
   use SimulationVariables !@
   use SimulationVariables !@
   use SimulationVariables !@
   Use TD_StringConnectionData
   use SimulationVariables !@!, only: Drawworks
   
   
   
   implicit none
   integer :: i , kk
   Real(8) :: LengthSum
   
   
   
   
   
   
   
   !!=====> Read Data
   !kk        = 0
   !LengthSum = 0.d0
   !
   !Do i = data%State%TD_String%DrillStemComponentsNumbs , 1 , -1
   !     if (data%State%TD_DrillStems(i)%ComponentType==3) then
   !         kk = i
   !         exit
   !     else if (data%State%TD_DrillStems(i)%ComponentType==5 .or. data%State%TD_DrillStems(i)%ComponentType==7) then
   !         LengthSum = LengthSum+data%State%TD_DrillStems(i)%LengthIni
   !     end if
   !End Do
   
   
   
   
   
   
   
   !####C_Program -----> data%Configuration%Hoisting%DriveType = 
   !                                        = 0    TopDrive_DriveType
   !                                        = 1    Kelly_DriveType
   
   !####C_Program -----> data%State%TD_StConn%KellyDriveTypeMode = 
   !                                           = 0    Drill Mode (Kelly)
   !                                           = 1    Trip Mode (Elevator)
   !                                           = 2    TopDrive Mode
   
   if ( data%Configuration%Hoisting%DriveType==1 .and. Get_OperationCondition()==OPERATION_DRILL ) then
       data%State%TD_StConn%KellyDriveTypeMode = 0
   else if ( data%Configuration%Hoisting%DriveType==1 .and. Get_OperationCondition()==OPERATION_TRIP ) then
       data%State%TD_StConn%KellyDriveTypeMode = 1
   else if ( data%Configuration%Hoisting%DriveType==0 ) then
       data%State%TD_StConn%KellyDriveTypeMode = 2
   end if
   
   data%State%TD_StConn%HookHeight           = data%State%Drawworks%TDHookHeight        ! unit: [ft]
   data%State%TD_StConn%ElevatorConst        = 17.985d0                      ! [ft]   Elevator Length(14.84)   ????????????????? adad ha daghigh shavand
   data%State%TD_StConn%ElevatorECG          = data%State%OperationScenario%ECG         ! [ft]
   data%State%TD_StConn%KellyConst           = 63.280d0                      ! [ft]   Kelly Length(61.74) + Safety Valve Length(1.54)
   data%State%TD_StConn%KellyElementConst    = 41.840d0                      ! [ft]   Kelly Element Length(40.3) + Safety Valve Length(1.54)
   data%State%TD_StConn%TDSLength            = 24.08d0  !??????????????????? ! [ft]   
   data%State%TD_StConn%TDSToolJointLength   = 0.77d0  !???????????????????  ! [ft]   
   data%State%TD_StConn%TDSElevatorLength    = 26.837d0   !??????            ! TDS with Elevator Length [ft]
   data%State%TD_StConn%TDSElevatorToolLength= 0.859d0  !??????????????????? ! [ft]
   data%State%TD_StConn%TDSElevatorECG       = 2.454d0                       ! [ft]
   data%State%TD_Load%NumOfCables            = data%Configuration%Hoisting%NumberOfLine
   data%State%TD_Load%WeightTB               = data%Configuration%Hoisting%TravelingBlockWeight ! [lb]
   data%State%TD_Load%WeightTD               = data%Configuration%Hoisting%TopDriveWeight       ! [lb]
   data%State%TD_Load%KellyWeight            = data%Configuration%Hoisting%KellyWeight          ! [lb]
   
   
   
   
   
   
   
   
   !=====> Velocity Calculation
   data%State%TD_StConn%HookVelocity = (data%State%TD_StConn%HookHeight-data%State%TD_StConn%HookHeightOld)/data%State%TD_General%TimeStep   ! [ft/s]
   
   
   
   
   
   
   
   
   
!====================================================
!       Add or Remove DrillStem Components
!====================================================
    Call TD_AddComponents
    
    Call TD_RemoveComponents
    
    
   !=====> Read Data
   kk        = 0
   LengthSum = 0.d0

   Do i = data%State%TD_String%DrillStemComponentsNumbs , 1 , -1
        if (data%State%TD_DrillStems(i)%ComponentType==3) then
            kk = i
            exit
        else if (data%State%TD_DrillStems(i)%ComponentType==5 .or. data%State%TD_DrillStems(i)%ComponentType==7) then
            LengthSum = LengthSum+data%State%TD_DrillStems(i)%LengthIni
        end if
   End Do
    !print*, 'kk' , kk
    !print*, 'data%State%TD_DrillStems(kk)%LengthIni0=' , data%State%TD_DrillStems(kk)%LengthIni
    
    
  

!====================================================
!       String Connection Mode Determination
!====================================================
   
   !####data%State%TD_StConn%StringConnectionMode = 
   !                            = 0   Slips Set         ???????
   !                            = 1   Kelly Connected to String
   !                            = 2   Kelly Nothing Connected
   !                            = 3   Kelly Connected to Single
   !                            = 4   Elevator Connected to String
   !                            = 5   Elevator Nothing Connected
   !                            = 6   Elevator Connected to Stand
   !                            = 7   Elevator Connected to Single
   !                            = 8   Elevator Latch String
   !                            = 18  Elevator Latch Single
   !                            = 19  Elevator Latch Stand
   !                            = 9   TDS CONNECTION SPINE
   !                            = 10  TDS CONNECTION STRING
   !                            = 11  TDS CONNECTION NOTHING .and. TDS ELEVATOR LATCH STRING
   !                            = 12  TDS CONNECTION NOTHING .and. TDS ELEVATOR CONNECTION STRING
   !                            = 13  TDS CONNECTION NOTHING .and. TDS ELEVATOR CONNECTION NOTHING
   !                            = 14  TDS CONNECTION NOTHING .and. TDS ELEVATOR CONNECTION STAND
   !                            = 15  TDS CONNECTION NOTHING .and. TDS ELEVATOR CONNECTION SINGLE
   !                            = 16  TDS CONNECTION NOTHING .and. TDS ELEVATOR LATCH SINGLE
   !                            = 17  TDS CONNECTION NOTHING .and. TDS ELEVATOR LATCH STAND 
   
    
    
   
  
   !if (data%State%TD_String%DlTouch<=0.) then   ![ft]
   !    if(data%State%TD_StConn%HookVelocity<=0.) then
   !        data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
   !        data%State%TD_StConn%StringVelocity   = 0.0d0
   !        return
   !    end if
   !end if
   
   
   
   !=====> Connection Height Determination
   
   if ( data%State%TD_StConn%KellyDriveTypeMode==0 ) then
       
       if(Get_KellyConnection() == KELLY_CONNECTION_STRING) then
           data%State%TD_Count%KellyNewStatus(1)  = 1
       else
           data%State%TD_Count%KellyNewStatus(1)  = 0
           !data%State%TD_Count%KellyOldStatus(1) = 0
       end if
       
       if(Get_KellyConnection()==KELLY_CONNECTION_NOTHING) then
           data%State%TD_Count%KellyNewStatus(2)  = 1
       else
           data%State%TD_Count%KellyNewStatus(2)  = 0
           !data%State%TD_Count%KellyOldStatus(2) = 0
       end if
       
       if(Get_KellyConnection()==KELLY_CONNECTION_SINGLE) then
           data%State%TD_Count%KellyNewStatus(3)  = 1
       else
           data%State%TD_Count%KellyNewStatus(3)  = 0
           !data%State%TD_Count%KellyOldStatus(3) = 0
       end if
       
       
       if ( Get_KellyConnection() == KELLY_CONNECTION_STRING ) then
           data%State%TD_StConn%StringConnectionMode = 1
           !print*, 'data%State%TD_StConn%ConnectionHeight10=' , data%State%TD_StConn%ConnectionHeight
           !print*, 'data%State%TD_StConn%KellyElementConst1=' , data%State%TD_StConn%KellyElementConst
           !print*, 'data%State%TD_DrillStems(kk)%LengthIni1=' , data%State%TD_DrillStems(kk)%LengthIni
           if (data%State%TD_Count%KellyOldStatus(2)==1 .and. data%State%TD_Count%KellyNewStatus(1)/=data%State%TD_Count%KellyOldStatus(1)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight+data%State%TD_StConn%KellyElementConst
               data%State%TD_Count%KellyOldStatus(1)  = data%State%TD_Count%KellyNewStatus(1)
               data%State%TD_Count%KellyOldStatus(2)  = 0
           else if (data%State%TD_Count%KellyOldStatus(3)==1 .and. data%State%TD_Count%KellyNewStatus(1)/=data%State%TD_Count%KellyOldStatus(1)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight+data%State%TD_StConn%KellyElementConst+30.d0  !data%State%TD_DrillStems(kk)%LengthIni
               data%State%TD_Count%KellyOldStatus(1)  = data%State%TD_Count%KellyNewStatus(1)
               data%State%TD_Count%KellyOldStatus(3)  = 0
           end if
           !print*, 'data%State%TD_StConn%ConnectionHeight1=' , data%State%TD_StConn%ConnectionHeight
           if ( Get_Slips() == SLIPS_SET_END ) then
               !!if ( data%State%TD_StConn%HookVelocity>0. ) then
               !!    Print*, '*** UnSet Slips ***'   !?????????
               !!end if
               data%State%TD_StConn%KellyConnectionHeight = data%State%TD_StConn%HookHeight-(data%State%TD_StConn%KellyConst-data%State%TD_StConn%KellyElementConst)   !?????????
               data%State%TD_StConn%ConnectionHeight      = data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%StringVelocity        = 0.0d0
           else
               !if (data%State%TD_String%DlTouch<=0. .and. data%State%TD_StConn%HookVelocity<=0.) then
               !    data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               !    data%State%TD_StConn%StringVelocity   = 0.0d0
               !else
                data%State%TD_StConn%KellyConnectionHeight = data%State%TD_StConn%HookHeight-(data%State%TD_StConn%KellyConst-data%State%TD_StConn%KellyElementConst)   !??????????
                data%State%TD_StConn%ConnectionHeight      = data%State%TD_StConn%KellyConnectionHeight
                data%State%TD_StConn%StringVelocity        = data%State%TD_StConn%HookVelocity
               !end if
           end if
       
       else if ( Get_KellyConnection() == KELLY_CONNECTION_NOTHING ) then
           data%State%TD_StConn%StringConnectionMode = 2
           !print*, 'data%State%TD_StConn%ConnectionHeight20=' , data%State%TD_StConn%ConnectionHeight
           !print*, 'data%State%TD_StConn%KellyElementConst2=' , data%State%TD_StConn%KellyElementConst
           if (data%State%TD_Count%KellyOldStatus(1)==1 .and. data%State%TD_Count%KellyNewStatus(2)/=data%State%TD_Count%KellyOldStatus(2)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight-data%State%TD_StConn%KellyElementConst
               data%State%TD_Count%KellyOldStatus(2)  = data%State%TD_Count%KellyNewStatus(2)
               data%State%TD_Count%KellyOldStatus(1)  = 0
           else if (data%State%TD_Count%KellyOldStatus(3)==1 .and. data%State%TD_Count%KellyNewStatus(2)/=data%State%TD_Count%KellyOldStatus(2)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(2)  = data%State%TD_Count%KellyNewStatus(2)
               data%State%TD_Count%KellyOldStatus(3)  = 0
           else if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(2)/=data%State%TD_Count%KellyOldStatus(2)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(2)  = data%State%TD_Count%KellyNewStatus(2)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           end if
           !print*, 'data%State%TD_StConn%ConnectionHeight2=' , data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%KellyConnectionHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%KellyConst
           data%State%TD_StConn%ConnectionHeight      = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity        = 0.0d0
       
       else if ( Get_KellyConnection() == KELLY_CONNECTION_SINGLE ) then
           !print*, 'KELLY_CONNECTION_SINGLE' , data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringConnectionMode = 3
           !print*, 'data%State%TD_StConn%ConnectionHeight30=' , data%State%TD_StConn%ConnectionHeight
           !print*, 'data%State%TD_StConn%KellyElementConst3=' , data%State%TD_StConn%KellyElementConst
           !print*, 'data%State%TD_DrillStems(kk)%LengthIni3=' , data%State%TD_DrillStems(kk)%LengthIni
           if (data%State%TD_Count%KellyOldStatus(1)==1 .and. data%State%TD_Count%KellyNewStatus(3)/=data%State%TD_Count%KellyOldStatus(3)) then
               !print*, 'KELLY_CONNECTION_SINGLE1' , data%State%TD_StConn%ConnectionHeight
               !print*, 'data%State%TD_StConn%KellyElementConst=' , data%State%TD_StConn%KellyElementConst
               !print*, 'data%State%TD_DrillStems(kk)%LengthIni=' , data%State%TD_DrillStems(kk)%LengthIni
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight-data%State%TD_StConn%KellyElementConst-30.d0  !data%State%TD_DrillStems(kk)%LengthIni
               data%State%TD_Count%KellyOldStatus(3)  = data%State%TD_Count%KellyNewStatus(3)
               data%State%TD_Count%KellyOldStatus(1)  = 0
           else if (data%State%TD_Count%KellyOldStatus(2)==1 .and. data%State%TD_Count%KellyNewStatus(3)/=data%State%TD_Count%KellyOldStatus(3)) then
               !print*, 'KELLY_CONNECTION_SINGLE2' , data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(3)  = data%State%TD_Count%KellyNewStatus(3)
               data%State%TD_Count%KellyOldStatus(2)  = 0
           end if
           !print*, 'data%State%TD_StConn%ConnectionHeight3=' , data%State%TD_StConn%ConnectionHeight
           !print*, 'KELLY_CONNECTION_SINGLE3' , data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%KellyConnectionHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%KellyConst-data%State%TD_DrillStems(kk)%LengthIni
           data%State%TD_StConn%ConnectionHeight      = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity        = 0.0d0
           !print*, 'KELLY_CONNECTION_SINGLE4' , data%State%TD_StConn%ConnectionHeight
       end if
       !print*, 'KELLY_CONNECTION_SINGLE5' , data%State%TD_StConn%ConnectionHeight
    
   else if ( data%State%TD_StConn%KellyDriveTypeMode==1 ) then
       
       if(Get_ElevatorConnection() == ELEVATOR_CONNECTION_STRING) then
           data%State%TD_Count%KellyNewStatus(4)  = 1
       else
           data%State%TD_Count%KellyNewStatus(4)  = 0
           !data%State%TD_Count%KellyOldStatus(4) = 0
       end if
       
       if(Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING) then
           data%State%TD_Count%KellyNewStatus(5)  = 1
       else
           data%State%TD_Count%KellyNewStatus(5)  = 0
           !data%State%TD_Count%KellyOldStatus(5) = 0
       end if
       
       if(Get_ElevatorConnection() == ELEVATOR_CONNECTION_STAND) then
           data%State%TD_Count%KellyNewStatus(6)  = 1
       else
           data%State%TD_Count%KellyNewStatus(6)  = 0
           !data%State%TD_Count%KellyOldStatus(6) = 0
       end if
       
       if(Get_ElevatorConnection() == ELEVATOR_CONNECTION_SINGLE) then
           data%State%TD_Count%KellyNewStatus(7)  = 1
       else
           data%State%TD_Count%KellyNewStatus(7)  = 0
           !data%State%TD_Count%KellyOldStatus(7) = 0
       end if
       
       if(Get_ElevatorConnection() == ELEVATOR_LATCH_STRING) then
           data%State%TD_Count%KellyNewStatus(8)  = 1
       else
           data%State%TD_Count%KellyNewStatus(8)  = 0
           !data%State%TD_Count%KellyOldStatus(8) = 0
       end if
       
       if(Get_ElevatorConnection() == ELEVATOR_LATCH_SINGLE) then
           data%State%TD_Count%KellyNewStatus(18)  = 1
       else
           data%State%TD_Count%KellyNewStatus(18)  = 0
           !data%State%TD_Count%KellyOldStatus(18) = 0
       end if
       
       if(Get_ElevatorConnection() == ELEVATOR_LATCH_STAND) then
           data%State%TD_Count%KellyNewStatus(19)  = 1
       else
           data%State%TD_Count%KellyNewStatus(19)  = 0
           !data%State%TD_Count%KellyOldStatus(19) = 0
       end if
       
       
       if ( Get_ElevatorConnection() == ELEVATOR_CONNECTION_STRING ) then
           data%State%TD_StConn%StringConnectionMode = 4
           if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(4)/=data%State%TD_Count%KellyOldStatus(4)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(4)  = data%State%TD_Count%KellyNewStatus(4)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           else if (data%State%TD_Count%KellyOldStatus(6)==1 .and. data%State%TD_Count%KellyNewStatus(4)/=data%State%TD_Count%KellyOldStatus(4)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight+(3.d0*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(4)  = data%State%TD_Count%KellyNewStatus(4)
               data%State%TD_Count%KellyOldStatus(6)  = 0
           else if (data%State%TD_Count%KellyOldStatus(7)==1 .and. data%State%TD_Count%KellyNewStatus(4)/=data%State%TD_Count%KellyOldStatus(4)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight+data%State%TD_DrillStems(kk)%LengthIni
               data%State%TD_Count%KellyOldStatus(4)  = data%State%TD_Count%KellyNewStatus(4)
               data%State%TD_Count%KellyOldStatus(7)  = 0
           else if (data%State%TD_Count%KellyOldStatus(8)==1 .and. data%State%TD_Count%KellyNewStatus(4)/=data%State%TD_Count%KellyOldStatus(4)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(4)  = data%State%TD_Count%KellyNewStatus(4)
               data%State%TD_Count%KellyOldStatus(8)  = 0
           end if
           if ( Get_Slips() == SLIPS_SET_END ) then
               !if ( data%State%TD_StConn%HookVelocity>0. ) then
               !    Print*, '*** UnSet Slips ***'   !???????????????????
               !end if
               data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst   !?????????????
               data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%StringVelocity       = 0.0d0
           else
               data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst
               data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst+(LengthSum+data%State%TD_StConn%ElevatorECG)      !(LengthSum+data%State%TD_DrillStems(kk)%ToolJointRange)
               data%State%TD_StConn%StringVelocity       = data%State%TD_StConn%HookVelocity
           end if
       
       else if ( Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING ) then
           data%State%TD_StConn%StringConnectionMode = 5
           if (data%State%TD_Count%KellyOldStatus(4)==1 .and. data%State%TD_Count%KellyNewStatus(5)/=data%State%TD_Count%KellyOldStatus(5)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(5)  = data%State%TD_Count%KellyNewStatus(5)
               data%State%TD_Count%KellyOldStatus(4)  = 0
           else if (data%State%TD_Count%KellyOldStatus(6)==1 .and. data%State%TD_Count%KellyNewStatus(5)/=data%State%TD_Count%KellyOldStatus(5)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(5)  = data%State%TD_Count%KellyNewStatus(5)
               data%State%TD_Count%KellyOldStatus(6)  = 0
           else if (data%State%TD_Count%KellyOldStatus(7)==1 .and. data%State%TD_Count%KellyNewStatus(5)/=data%State%TD_Count%KellyOldStatus(5)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(5)  = data%State%TD_Count%KellyNewStatus(5)
               data%State%TD_Count%KellyOldStatus(7)  = 0
           else if (data%State%TD_Count%KellyOldStatus(8)==1 .and. data%State%TD_Count%KellyNewStatus(5)/=data%State%TD_Count%KellyOldStatus(5)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(5)  = data%State%TD_Count%KellyNewStatus(5)
               data%State%TD_Count%KellyOldStatus(8)  = 0
            else if (data%State%TD_Count%KellyOldStatus(2)==1 .and. data%State%TD_Count%KellyNewStatus(5)/=data%State%TD_Count%KellyOldStatus(5)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(5)  = data%State%TD_Count%KellyNewStatus(5)
               data%State%TD_Count%KellyOldStatus(2)  = 0
           end if
           data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst
           data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity       = 0.0d0
       
       else if ( Get_ElevatorConnection() == ELEVATOR_CONNECTION_STAND ) then
           data%State%TD_StConn%StringConnectionMode = 6
           if (data%State%TD_Count%KellyOldStatus(4)==1 .and. data%State%TD_Count%KellyNewStatus(6)/=data%State%TD_Count%KellyOldStatus(6)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight-(3.*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(6)  = data%State%TD_Count%KellyNewStatus(6)
               data%State%TD_Count%KellyOldStatus(4)  = 0
           else if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(6)/=data%State%TD_Count%KellyOldStatus(6)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(6)  = data%State%TD_Count%KellyNewStatus(6)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           else if (data%State%TD_Count%KellyOldStatus(7)==1 .and. data%State%TD_Count%KellyNewStatus(6)/=data%State%TD_Count%KellyOldStatus(6)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(6)  = data%State%TD_Count%KellyNewStatus(6)
               data%State%TD_Count%KellyOldStatus(7)  = 0
           else if (data%State%TD_Count%KellyOldStatus(8)==1 .and. data%State%TD_Count%KellyNewStatus(6)/=data%State%TD_Count%KellyOldStatus(6)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(6)  = data%State%TD_Count%KellyNewStatus(6)
               data%State%TD_Count%KellyOldStatus(8)  = 0
           end if
           data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst-(3.*data%State%TD_DrillStems(kk)%LengthIni)
           data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity       = 0.0d0
       
       else if ( Get_ElevatorConnection() == ELEVATOR_CONNECTION_SINGLE ) then
           data%State%TD_StConn%StringConnectionMode = 7
           if (data%State%TD_Count%KellyOldStatus(4)==1 .and. data%State%TD_Count%KellyNewStatus(7)/=data%State%TD_Count%KellyOldStatus(7)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight-data%State%TD_DrillStems(kk)%LengthIni
               data%State%TD_Count%KellyOldStatus(7)  = data%State%TD_Count%KellyNewStatus(7)
               data%State%TD_Count%KellyOldStatus(4)  = 0
           else if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(7)/=data%State%TD_Count%KellyOldStatus(7)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(7)  = data%State%TD_Count%KellyNewStatus(7)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           else if (data%State%TD_Count%KellyOldStatus(6)==1 .and. data%State%TD_Count%KellyNewStatus(7)/=data%State%TD_Count%KellyOldStatus(7)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(7)  = data%State%TD_Count%KellyNewStatus(7)
               data%State%TD_Count%KellyOldStatus(6)  = 0
           else if (data%State%TD_Count%KellyOldStatus(8)==1 .and. data%State%TD_Count%KellyNewStatus(7)/=data%State%TD_Count%KellyOldStatus(7)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(7)  = data%State%TD_Count%KellyNewStatus(7)
               data%State%TD_Count%KellyOldStatus(8)  = 0
           end if
           data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst-data%State%TD_DrillStems(kk)%LengthIni
           data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity       = 0.0d0
           
        else if ( Get_ElevatorConnection() == ELEVATOR_LATCH_STRING ) then
           data%State%TD_StConn%StringConnectionMode = 8
           if (data%State%TD_Count%KellyOldStatus(4)==1 .and. data%State%TD_Count%KellyNewStatus(8)/=data%State%TD_Count%KellyOldStatus(8)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(8)  = data%State%TD_Count%KellyNewStatus(8)
               data%State%TD_Count%KellyOldStatus(4)  = 0
           else if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(8)/=data%State%TD_Count%KellyOldStatus(8)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(8)  = data%State%TD_Count%KellyNewStatus(8)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           else if (data%State%TD_Count%KellyOldStatus(6)==1 .and. data%State%TD_Count%KellyNewStatus(8)/=data%State%TD_Count%KellyOldStatus(8)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(8)  = data%State%TD_Count%KellyNewStatus(8)
               data%State%TD_Count%KellyOldStatus(6)  = 0
           else if (data%State%TD_Count%KellyOldStatus(7)==1 .and. data%State%TD_Count%KellyNewStatus(8)/=data%State%TD_Count%KellyOldStatus(8)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(8)  = data%State%TD_Count%KellyNewStatus(8)
               data%State%TD_Count%KellyOldStatus(7)  = 0
           end if
           data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst
           data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity       = 0.0d0
           
        else if ( Get_ElevatorConnection() == ELEVATOR_LATCH_SINGLE ) then
           data%State%TD_StConn%StringConnectionMode = 18
           if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(18)/=data%State%TD_Count%KellyOldStatus(18)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(18)  = data%State%TD_Count%KellyNewStatus(18)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           else if (data%State%TD_Count%KellyOldStatus(7)==1 .and. data%State%TD_Count%KellyNewStatus(18)/=data%State%TD_Count%KellyOldStatus(18)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(18)  = data%State%TD_Count%KellyNewStatus(18)
               data%State%TD_Count%KellyOldStatus(7)  = 0
           else if (data%State%TD_Count%KellyOldStatus(8)==1 .and. data%State%TD_Count%KellyNewStatus(18)/=data%State%TD_Count%KellyOldStatus(18)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(18)  = data%State%TD_Count%KellyNewStatus(18)
               data%State%TD_Count%KellyOldStatus(8)  = 0
           end if
           data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst
           data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity       = 0.0d0
           
        else if ( Get_ElevatorConnection() == ELEVATOR_LATCH_STAND ) then
           data%State%TD_StConn%StringConnectionMode = 19
           if (data%State%TD_Count%KellyOldStatus(5)==1 .and. data%State%TD_Count%KellyNewStatus(19)/=data%State%TD_Count%KellyOldStatus(19)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(19)  = data%State%TD_Count%KellyNewStatus(19)
               data%State%TD_Count%KellyOldStatus(5)  = 0
           else if (data%State%TD_Count%KellyOldStatus(6)==1 .and. data%State%TD_Count%KellyNewStatus(19)/=data%State%TD_Count%KellyOldStatus(19)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(19)  = data%State%TD_Count%KellyNewStatus(19)
               data%State%TD_Count%KellyOldStatus(6)  = 0
           else if (data%State%TD_Count%KellyOldStatus(8)==1 .and. data%State%TD_Count%KellyNewStatus(19)/=data%State%TD_Count%KellyOldStatus(19)) then
               data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(19)  = data%State%TD_Count%KellyNewStatus(19)
               data%State%TD_Count%KellyOldStatus(8)  = 0
           end if
           data%State%TD_StConn%ElevatorHeight       = data%State%TD_StConn%HookHeight-data%State%TD_StConn%ElevatorConst
           data%State%TD_StConn%ConnectionHeight     = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity       = 0.0d0
        end if
        
   else if ( data%State%TD_StConn%KellyDriveTypeMode==2 ) then
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_SPINE ) then
           data%State%TD_Count%KellyNewStatus(9)  = 1
       else
           data%State%TD_Count%KellyNewStatus(9)  = 0
           !data%State%TD_Count%KellyOldStatus(9) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_STRING ) then
           data%State%TD_Count%KellyNewStatus(10)  = 1
       else
           data%State%TD_Count%KellyNewStatus(10)  = 0
           !data%State%TD_Count%KellyOldStatus(10) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_LATCH_STRING ) then
           data%State%TD_Count%KellyNewStatus(11)  = 1
       else
           data%State%TD_Count%KellyNewStatus(11)  = 0
           !data%State%TD_Count%KellyOldStatus(11) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_STRING ) then
           data%State%TD_Count%KellyNewStatus(12)  = 1
       else
           data%State%TD_Count%KellyNewStatus(12)  = 0
           !data%State%TD_Count%KellyOldStatus(12) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_NOTHING ) then
           data%State%TD_Count%KellyNewStatus(13)  = 1
       else
           data%State%TD_Count%KellyNewStatus(13)  = 0
           !data%State%TD_Count%KellyOldStatus(13) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_STAND ) then
           data%State%TD_Count%KellyNewStatus(14)  = 1
       else
           data%State%TD_Count%KellyNewStatus(14)  = 0
           !data%State%TD_Count%KellyOldStatus(14) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_SINGLE ) then
           data%State%TD_Count%KellyNewStatus(15)  = 1
       else
           data%State%TD_Count%KellyNewStatus(15)  = 0
           !data%State%TD_Count%KellyOldStatus(15) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_LATCH_SINGLE ) then
           data%State%TD_Count%KellyNewStatus(16)  = 1
       else
           data%State%TD_Count%KellyNewStatus(16)  = 0
           !data%State%TD_Count%KellyOldStatus(16) = 0
       end if
       
       if( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_LATCH_STAND ) then
           data%State%TD_Count%KellyNewStatus(17)  = 1
       else
           data%State%TD_Count%KellyNewStatus(17)  = 0
           !data%State%TD_Count%KellyOldStatus(17) = 0
       end if
       
       if ( Get_TdsConnectionModes()==TDS_CONNECTION_SPINE ) then
           data%State%TD_StConn%StringConnectionMode  = 9
           if (data%State%TD_Count%KellyOldStatus(10)==1 .and. data%State%TD_Count%KellyNewStatus(9)/=data%State%TD_Count%KellyOldStatus(9)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(9)   = data%State%TD_Count%KellyNewStatus(9)
               data%State%TD_Count%KellyOldStatus(10)  = 0
           else if (data%State%TD_Count%KellyOldStatus(11)==1 .and. data%State%TD_Count%KellyNewStatus(9)/=data%State%TD_Count%KellyOldStatus(9)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(9)   = data%State%TD_Count%KellyNewStatus(9)
               data%State%TD_Count%KellyOldStatus(11)  = 0
           else if (data%State%TD_Count%KellyOldStatus(13)==1 .and. data%State%TD_Count%KellyNewStatus(9)/=data%State%TD_Count%KellyOldStatus(9)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(9)   = data%State%TD_Count%KellyNewStatus(9)
               data%State%TD_Count%KellyOldStatus(13)  = 0
           end if
           if ( Get_Slips() == SLIPS_SET_END ) then
               !if ( data%State%TD_StConn%HookVelocity>0. ) then
               !    Print*, '*** UnSet Slips ***'   !???????????????????
               !end if
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength-data%State%TD_StConn%TDSToolJointLength       !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%StringVelocity    = 0.0d0
           else
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength-data%State%TD_StConn%TDSToolJointLength       !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%TDSHeight
               data%State%TD_StConn%StringVelocity    = data%State%TD_StConn%HookVelocity
           end if
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_STRING ) then
           data%State%TD_StConn%StringConnectionMode  = 10
           if (data%State%TD_Count%KellyOldStatus(9)==1 .and. data%State%TD_Count%KellyNewStatus(10)/=data%State%TD_Count%KellyOldStatus(10)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(10)  = data%State%TD_Count%KellyNewStatus(10)
               data%State%TD_Count%KellyOldStatus(9)   = 0
           end if
           if ( Get_Slips() == SLIPS_SET_END ) then
               !if ( data%State%TD_StConn%HookVelocity>0. ) then
               !    Print*, '*** UnSet Slips ***'   !???????????????????
               !end if
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength-data%State%TD_StConn%TDSToolJointLength       !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%StringVelocity    = 0.0d0
           else
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength-data%State%TD_StConn%TDSToolJointLength       !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%TDSHeight
               data%State%TD_StConn%StringVelocity    = data%State%TD_StConn%HookVelocity
           end if
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_LATCH_STRING ) then
           data%State%TD_StConn%StringConnectionMode  = 11
           if (data%State%TD_Count%KellyOldStatus(9)==1 .and. data%State%TD_Count%KellyNewStatus(11)/=data%State%TD_Count%KellyOldStatus(11)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(11)  = data%State%TD_Count%KellyNewStatus(11)
               data%State%TD_Count%KellyOldStatus(9)   = 0
           else if (data%State%TD_Count%KellyOldStatus(12)==1 .and. data%State%TD_Count%KellyNewStatus(11)/=data%State%TD_Count%KellyOldStatus(11)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(11)  = data%State%TD_Count%KellyNewStatus(11)
               data%State%TD_Count%KellyOldStatus(12)  = 0
           else if (data%State%TD_Count%KellyOldStatus(13)==1 .and. data%State%TD_Count%KellyNewStatus(11)/=data%State%TD_Count%KellyOldStatus(11)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(11)  = data%State%TD_Count%KellyNewStatus(11)
               data%State%TD_Count%KellyOldStatus(13)  = 0
           else if (data%State%TD_Count%KellyOldStatus(16)==1 .and. data%State%TD_Count%KellyNewStatus(11)/=data%State%TD_Count%KellyOldStatus(11)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight+(data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(11)  = data%State%TD_Count%KellyNewStatus(11)
               data%State%TD_Count%KellyOldStatus(16)  = 0
           else if (data%State%TD_Count%KellyOldStatus(17)==1 .and. data%State%TD_Count%KellyNewStatus(11)/=data%State%TD_Count%KellyOldStatus(11)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight+(3.d0*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(11)  = data%State%TD_Count%KellyNewStatus(11)
               data%State%TD_Count%KellyOldStatus(17)  = 0
           end if
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength       !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%StringVelocity    = 0.0d0
       
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_STRING ) then
           data%State%TD_StConn%StringConnectionMode  = 12
           if (data%State%TD_Count%KellyOldStatus(11)==1 .and. data%State%TD_Count%KellyNewStatus(12)/=data%State%TD_Count%KellyOldStatus(12)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(12)  = data%State%TD_Count%KellyNewStatus(12)
               data%State%TD_Count%KellyOldStatus(11)  = 0
           else if (data%State%TD_Count%KellyOldStatus(14)==1 .and. data%State%TD_Count%KellyNewStatus(12)/=data%State%TD_Count%KellyOldStatus(12)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight+(3.d0*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(12)  = data%State%TD_Count%KellyNewStatus(12)
               data%State%TD_Count%KellyOldStatus(14)  = 0
           else if (data%State%TD_Count%KellyOldStatus(15)==1 .and. data%State%TD_Count%KellyNewStatus(12)/=data%State%TD_Count%KellyOldStatus(12)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight+(data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(12)  = data%State%TD_Count%KellyNewStatus(12)
               data%State%TD_Count%KellyOldStatus(15)  = 0
           end if
           if ( Get_Slips() == SLIPS_SET_END ) then
               !if ( data%State%TD_StConn%HookVelocity>0. ) then
               !    Print*, '*** UnSet Slips ***'   !???????????????????
               !end if
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_StConn%StringVelocity    = 0.0d0
           else
               data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength   !?????????????
               data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength-data%State%TD_StConn%TDSElevatorLength   !?????????????
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength+(LengthSum+data%State%TD_StConn%TDSElevatorECG)   !?????????????
               data%State%TD_StConn%StringVelocity    = data%State%TD_StConn%HookVelocity
           end if
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_NOTHING ) then
           data%State%TD_StConn%StringConnectionMode  = 13
           if (data%State%TD_Count%KellyOldStatus(9)==1 .and. data%State%TD_Count%KellyNewStatus(13)/=data%State%TD_Count%KellyOldStatus(13)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(13)  = data%State%TD_Count%KellyNewStatus(13)
               data%State%TD_Count%KellyOldStatus(9)   = 0
           else if (data%State%TD_Count%KellyOldStatus(11)==1 .and. data%State%TD_Count%KellyNewStatus(13)/=data%State%TD_Count%KellyOldStatus(13)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(13)  = data%State%TD_Count%KellyNewStatus(13)
               data%State%TD_Count%KellyOldStatus(11)  = 0
           else if (data%State%TD_Count%KellyOldStatus(12)==1 .and. data%State%TD_Count%KellyNewStatus(13)/=data%State%TD_Count%KellyOldStatus(13)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(13)  = data%State%TD_Count%KellyNewStatus(13)
               data%State%TD_Count%KellyOldStatus(12)  = 0
           else if (data%State%TD_Count%KellyOldStatus(14)==1 .and. data%State%TD_Count%KellyNewStatus(13)/=data%State%TD_Count%KellyOldStatus(13)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(13)  = data%State%TD_Count%KellyNewStatus(13)
               data%State%TD_Count%KellyOldStatus(14)  = 0
           else if (data%State%TD_Count%KellyOldStatus(15)==1 .and. data%State%TD_Count%KellyNewStatus(13)/=data%State%TD_Count%KellyOldStatus(13)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(13)  = data%State%TD_Count%KellyNewStatus(13)
               data%State%TD_Count%KellyOldStatus(15)  = 0
           else if (data%State%TD_Count%KellyOldStatus(16)==1 .and. data%State%TD_Count%KellyNewStatus(13)/=data%State%TD_Count%KellyOldStatus(13)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(13)  = data%State%TD_Count%KellyNewStatus(13)
               data%State%TD_Count%KellyOldStatus(16)  = 0
           end if
           data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength       !?????????????
           data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
           data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity    = 0.0d0
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_STAND ) then
           data%State%TD_StConn%StringConnectionMode  = 14
           if (data%State%TD_Count%KellyOldStatus(12)==1 .and. data%State%TD_Count%KellyNewStatus(14)/=data%State%TD_Count%KellyOldStatus(14)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight-(3.d0*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(14)  = data%State%TD_Count%KellyNewStatus(14)
               data%State%TD_Count%KellyOldStatus(12)  = 0
           else if (data%State%TD_Count%KellyOldStatus(13)==1 .and. data%State%TD_Count%KellyNewStatus(14)/=data%State%TD_Count%KellyOldStatus(14)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(14)  = data%State%TD_Count%KellyNewStatus(14)
               data%State%TD_Count%KellyOldStatus(13)  = 0
           else if (data%State%TD_Count%KellyOldStatus(17)==1 .and. data%State%TD_Count%KellyNewStatus(14)/=data%State%TD_Count%KellyOldStatus(14)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(14)  = data%State%TD_Count%KellyNewStatus(14)
               data%State%TD_Count%KellyOldStatus(17)  = 0
           end if
           data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength       !?????????????
           data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
           data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity    = 0.0d0
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_SINGLE ) then
           data%State%TD_StConn%StringConnectionMode  = 15
           if (data%State%TD_Count%KellyOldStatus(12)==1 .and. data%State%TD_Count%KellyNewStatus(15)/=data%State%TD_Count%KellyOldStatus(15)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight-(data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(15)  = data%State%TD_Count%KellyNewStatus(15)
               data%State%TD_Count%KellyOldStatus(12)  = 0
           else if (data%State%TD_Count%KellyOldStatus(13)==1 .and. data%State%TD_Count%KellyNewStatus(15)/=data%State%TD_Count%KellyOldStatus(15)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(15)  = data%State%TD_Count%KellyNewStatus(15)
               data%State%TD_Count%KellyOldStatus(13)  = 0
           else if (data%State%TD_Count%KellyOldStatus(16)==1 .and. data%State%TD_Count%KellyNewStatus(15)/=data%State%TD_Count%KellyOldStatus(15)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(15)  = data%State%TD_Count%KellyNewStatus(15)
               data%State%TD_Count%KellyOldStatus(16)  = 0
           end if
           data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength       !?????????????
           data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
           data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity    = 0.0d0
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_LATCH_SINGLE ) then
           data%State%TD_StConn%StringConnectionMode  = 16
           if (data%State%TD_Count%KellyOldStatus(11)==1 .and. data%State%TD_Count%KellyNewStatus(16)/=data%State%TD_Count%KellyOldStatus(16)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight-(data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(16)  = data%State%TD_Count%KellyNewStatus(16)
               data%State%TD_Count%KellyOldStatus(11)  = 0
           else if (data%State%TD_Count%KellyOldStatus(13)==1 .and. data%State%TD_Count%KellyNewStatus(16)/=data%State%TD_Count%KellyOldStatus(16)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(16)  = data%State%TD_Count%KellyNewStatus(16)
               data%State%TD_Count%KellyOldStatus(13)  = 0
           else if (data%State%TD_Count%KellyOldStatus(15)==1 .and. data%State%TD_Count%KellyNewStatus(16)/=data%State%TD_Count%KellyOldStatus(16)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
               data%State%TD_Count%KellyOldStatus(16)  = data%State%TD_Count%KellyNewStatus(16)
               data%State%TD_Count%KellyOldStatus(15)  = 0
           end if
           data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength       !?????????????
           data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
           data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity    = 0.0d0
           
       else if ( Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_LATCH_STAND ) then
           data%State%TD_StConn%StringConnectionMode  = 17
           if (data%State%TD_Count%KellyOldStatus(11)==1 .and. data%State%TD_Count%KellyNewStatus(17)/=data%State%TD_Count%KellyOldStatus(17)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight-(3.d0*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(17)  = data%State%TD_Count%KellyNewStatus(17)
               data%State%TD_Count%KellyOldStatus(11)  = 0
           else if (data%State%TD_Count%KellyOldStatus(14)==1 .and. data%State%TD_Count%KellyNewStatus(17)/=data%State%TD_Count%KellyOldStatus(17)) then
               data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight+(3.d0*data%State%TD_DrillStems(kk)%LengthIni)
               data%State%TD_Count%KellyOldStatus(17)  = data%State%TD_Count%KellyNewStatus(17)
               data%State%TD_Count%KellyOldStatus(14)  = 0
           end if
           data%State%TD_StConn%TDSHeight         = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSLength       !?????????????
           data%State%TD_StConn%TDSElevatorHeight = data%State%TD_StConn%HookHeight-data%State%TD_StConn%TDSElevatorLength   !?????????????
           data%State%TD_StConn%ConnectionHeight  = data%State%TD_StConn%ConnectionHeight
           data%State%TD_StConn%StringVelocity    = 0.0d0
       end if

   
   end if
   
   !Print*, 'data%State%TD_StConn%StringConnectionMode=' , data%State%TD_StConn%StringConnectionMode
   !print* , 'data%State%TD_StConn%ConnectionHeight=' , data%State%TD_StConn%ConnectionHeight
   !Print*, 'data%State%TD_StConn%StringConnectionMode=' , data%State%TD_StConn%StringConnectionMode
   !print*, 'data%State%TD_String%DrillStemComponentsNumbs=' , data%State%TD_String%DrillStemComponentsNumbs
   !!Print*, 'data%State%TD_Count%KellyNewStatus(1)=' , data%State%TD_Count%KellyNewStatus(1)
   !Print*, 'data%State%TD_Count%KellyOldStatus(1)=' , data%State%TD_Count%KellyOldStatus(1)
   !Print*, 'data%State%TD_Count%KellyNewStatus(2)=' , data%State%TD_Count%KellyNewStatus(2)
   !Print*, 'data%State%TD_Count%KellyOldStatus(2)=' , data%State%TD_Count%KellyOldStatus(2)
   !Print*, 'data%State%TD_Count%KellyNewStatus(3)=' , data%State%TD_Count%KellyNewStatus(3)
   !Print*, 'data%State%TD_Count%KellyOldStatus(3)=' , data%State%TD_Count%KellyOldStatus(3)
  
   
   
   
   
   
   if ( (any(data%State%TD_StConn%StringConnectionMode==(/1,9,10/))) .or. data%EquipmentControl%DrillingConsole%CloseSafetyValve==.true. ) then   !for fluid module
       data%State%TD_StConn%FluidStringConnectionMode = 1       !string is connected
   !else if ( (any(data%State%TD_StConn%StringConnectionMode==(/1,9,10/))) .or. data%EquipmentControl%DrillingConsole%CloseSafetyValve==.true. ) then
   !    
   else
       data%State%TD_StConn%FluidStringConnectionMode = 0       !string is not connected
   end if
   
   
   
   
   
   
!====================================================
!                 Zero String Speed
!====================================================
   if ( abs(data%State%TD_StConn%StringVelocity)<=0.05d0 ) then   ! [ft/s]
       data%State%TD_Load%ZeroStringSpeed = 1
       Call Set_ZeroStringSpeed(.true.)
   else
       data%State%TD_Load%ZeroStringSpeed = 0
       Call Set_ZeroStringSpeed(.false.)
   end if
  

   
   
   
   
   
end subroutine