subroutine TD_DrillStemConfiguration

   Use CStringConfigurationVariables
   use SoftwareInputsVariables
!    Use CNearFloorConnection
   use CDataDisplayConsole
    use SimulationVariables
   use SimulationVariables !@
   use UnitySignalVariables
    use UnitySignalsModule
!    use UnitySignalsModuleVariables
   use OperationScenariosModule
   Use TD_DrillStemComponents
   use SimulationVariables !@
   use SimulationVariables !@
   Use TD_StringConnectionData
   Use sROP_Variables
   use DownHoleModule
   
   
   implicit none 

   Integer :: i , j , k , kk , TD_Status
  

   
   
   
   
!====================================================
!      Drill Stem Components Data Modification
!====================================================
   
   
!=====> Drill Stem Total Length&Weight Calculation
   data%State%TD_String%DrillStemTotalLength    = 0.0d0
   data%State%TD_String%DrillStemTotalLengthIni = 0.0d0
   data%State%TD_String%DrillStemTotalWeight    = 0.0d0
   Do i= 1,data%State%TD_String%DrillStemComponentsNumbs
       data%State%TD_String%DrillStemTotalLength    = data%State%TD_String%DrillStemTotalLength+data%State%TD_DrillStems(i)%Length
       data%State%TD_String%DrillStemTotalLengthIni = data%State%TD_String%DrillStemTotalLengthIni+data%State%TD_DrillStems(i)%LengthIni
       data%State%TD_String%DrillStemTotalWeight    = data%State%TD_String%DrillStemTotalWeight+data%State%TD_DrillStems(i)%Weight
   End Do
   
   
   
   
   
   
   
!=====> Top&Down Depth Calculation Of Initial Drill Stem Components (Graphic)
   data%State%TD_DrillStems(1)%DownDepth = data%State%TD_String%DrillStemTotalLength-data%State%TD_StConn%ConnectionHeight
   data%State%TD_String%DrillStemBottom         = data%State%TD_DrillStems(1)%DownDepth
   if ( data%State%TD_DrillStems(1)%DownDepth>=data%State%TD_WellGeneral%WellTotalLength ) then
       !if (  data%State%TD_StConn%HookHeight>=data%State%TD_StConn%HookHeightOld .and. data%State%ROP_Bit%RateOfPenetration==0. ) then
       !    data%State%TD_StConn%GRigidConnectionHeight = data%State%TD_StConn%GRigidConnectionHeight
       !else
           data%State%TD_StConn%GRigidConnectionHeight      = data%State%TD_StConn%GRigidConnectionHeight-(data%State%TD_WellGeneral%WellTotalLength-data%State%TD_DrillStems(1)%DownDepthIniG)
           data%State%TD_DrillStems(1)%DownDepthIniG = data%State%TD_WellGeneral%WellTotalLength           !???????????
       !    !data%State%TD_StConn%GRigidConnectionHeight     = data%State%TD_StConn%GRigidConnectionHeight
       !end if
   else
       data%State%TD_StConn%GRigidConnectionHeight      = data%State%TD_StConn%ConnectionHeight
       data%State%TD_DrillStems(1)%DownDepthIniG = data%State%TD_String%DrillStemTotalLengthIni-data%State%TD_StConn%GRigidConnectionHeight
   end if
   
   !data%State%TD_DrillStems(1)%DownDepthIniG     = data%State%TD_String%DrillStemTotalLengthIni-data%State%TD_StConn%GRigidConnectionHeight
   !if ( data%State%TD_DrillStems(1)%DownDepthIniG>data%State%TD_WellGeneral%WellTotalLength ) then
   !    data%State%TD_StConn%GRigidConnectionHeight      = data%State%TD_StConn%GRigidConnectionHeight+(data%State%TD_DrillStems(1)%DownDepthIniG-data%State%TD_WellGeneral%WellTotalLength)
   !    data%State%TD_DrillStems(1)%DownDepthIniG = data%State%TD_WellGeneral%WellTotalLength       !???????????
   !end if
   data%State%TD_DrillStems(1)%TopDepthIniG      = data%State%TD_DrillStems(1)%DownDepthIniG-data%State%TD_DrillStems(1)%LengthIni
   Do i = 2,data%State%TD_String%DrillStemComponentsNumbs
       data%State%TD_DrillStems(i)%TopDepthIniG  = data%State%TD_DrillStems(i-1)%TopDepthIniG-data%State%TD_DrillStems(i)%LengthIni
       data%State%TD_DrillStems(i)%DownDepthIniG = data%State%TD_DrillStems(i-1)%DownDepthIniG-data%State%TD_DrillStems(i-1)%LengthIni
   End Do
   
   
   
   
   
   
   
   
!=====> Top&Down Depth Calculation Of Initial Drill Stem Components (for fluid module)
   if ( data%State%TD_DrillStems(1)%DownDepthIniG>=(data%State%TD_WellGeneral%WellTotalLength-.1d0) ) then
       data%State%TD_StConn%RigidConnectionHeight = data%State%TD_StConn%GRigidConnectionHeight+(.1d0-(data%State%TD_WellGeneral%WellTotalLength-data%State%TD_DrillStems(1)%DownDepthIniG))
   else
       data%State%TD_StConn%RigidConnectionHeight = data%State%TD_StConn%GRigidConnectionHeight
   end if
   
   data%State%TD_DrillStems(1)%DownDepthIni = data%State%TD_String%DrillStemTotalLengthIni-data%State%TD_StConn%RigidConnectionHeight
   data%State%TD_DrillStems(1)%TopDepthIni  = data%State%TD_DrillStems(1)%DownDepthIni-data%State%TD_DrillStems(1)%LengthIni
   
   Do i = 2,data%State%TD_String%DrillStemComponentsNumbs
       data%State%TD_DrillStems(i)%TopDepthIni  = data%State%TD_DrillStems(i-1)%TopDepthIni-data%State%TD_DrillStems(i)%LengthIni
       data%State%TD_DrillStems(i)%DownDepthIni = data%State%TD_DrillStems(i-1)%DownDepthIni-data%State%TD_DrillStems(i-1)%LengthIni
   End Do
   
   
   
   
   
   
   
   
   
!=====> Top&Down Depth Calculation Of Drill Stem Components
   data%State%TD_DrillStems(1)%TopDepth  = data%State%TD_String%DrillStemTotalLength-data%State%TD_DrillStems(1)%Length-data%State%TD_StConn%GRigidConnectionHeight
   data%State%TD_DrillStems(1)%DownDepth = data%State%TD_String%DrillStemTotalLength-data%State%TD_StConn%GRigidConnectionHeight
   
   Do i = 2,data%State%TD_String%DrillStemComponentsNumbs
       data%State%TD_DrillStems(i)%TopDepth  = data%State%TD_DrillStems(i-1)%TopDepth-data%State%TD_DrillStems(i)%Length
       data%State%TD_DrillStems(i)%DownDepth = data%State%TD_DrillStems(i-1)%DownDepth-data%State%TD_DrillStems(i-1)%Length
   End Do
   
   !data%State%TD_String%DrillStemBottom = data%State%TD_DrillStems(1)%DownDepth
   
   
   
   
   
   
   
   
   
!=====> Hole Type & Inclination Determination Of Drill Stem Components
   Do i = 1,data%State%TD_String%DrillStemComponentsNumbs
       
       
      if (data%State%TD_DrillStems(i)%TopDepth .ge. 0.d0) then
          
          
          Do j = 1,data%State%TD_WellGeneral%WellIntervalsCount
              if (data%State%TD_DrillStems(i)%TopDepth>data%State%TD_WellGeo(j)%TopDepth .and. data%State%TD_DrillStems(i)%TopDepth<data%State%TD_WellGeo(j)%DownDepth) then
                  data%State%TD_DrillStems(i)%HoleType = data%State%TD_WellGeo(j)%HoleType
                  
                  
                  
                  if (data%State%TD_DrillStems(i)%HoleType == 1) then
                      data%State%TD_DrillStems(i)%StartAngle = data%State%TD_WellGeo(j)%StartAngle+(((data%State%TD_DrillStems(i)%TopDepth-data%State%TD_WellGeo(j)%TopDepth)/data%State%TD_WellGeo(j)%RCurvature))  ![rad]
                      data%State%TD_DrillStems(i)%EndAngle   = data%State%TD_WellGeo(j)%StartAngle+(((data%State%TD_DrillStems(i)%DownDepth-data%State%TD_WellGeo(j)%TopDepth)/data%State%TD_WellGeo(j)%RCurvature))
                      data%State%TD_DrillStems(i)%RCurvature = data%State%TD_WellGeo(j)%RCurvature
                  else if (data%State%TD_DrillStems(i)%HoleType == 2) then
                      data%State%TD_DrillStems(i)%StartAngle = data%State%TD_WellGeo(j)%StartAngle-(((data%State%TD_DrillStems(i)%TopDepth-data%State%TD_WellGeo(j)%TopDepth)/data%State%TD_WellGeo(j)%RCurvature))
                      data%State%TD_DrillStems(i)%EndAngle   = data%State%TD_WellGeo(j)%StartAngle-(((data%State%TD_DrillStems(i)%DownDepth-data%State%TD_WellGeo(j)%TopDepth)/data%State%TD_WellGeo(j)%RCurvature))
                      data%State%TD_DrillStems(i)%RCurvature = data%State%TD_WellGeo(j)%RCurvature
                  else
                      data%State%TD_DrillStems(i)%StartAngle = data%State%TD_WellGeo(j)%EndAngle   !Straight
                      data%State%TD_DrillStems(i)%EndAngle   = data%State%TD_DrillStems(i)%StartAngle
                  end if
              
                  
              end if
          End Do
          
          
      else 
          data%State%TD_DrillStems(i)%HoleType   = 0      !Straight
          data%State%TD_DrillStems(i)%StartAngle = 0.0d0   
          data%State%TD_DrillStems(i)%EndAngle   = 0.0d0
      end if
      
      
   End Do

   
   
   
   
   
   
   
   
   
!=====> Hole(Well) Diameter Determination Of Drill Stem Components
   Do i = 1,data%State%TD_String%DrillStemComponentsNumbs
       If (data%State%TD_DrillStems(i)%TopDepth>data%State%TD_Casing(1)%TopDepth .and. data%State%TD_DrillStems(i)%TopDepth<data%State%TD_Casing(1)%DownDepth) then
           data%State%TD_DrillStems(i)%HoleDiameter = data%State%TD_Casing(1)%Id
       Else if (data%State%TD_Liner(1)%Length/=0.d0 .and. data%State%TD_DrillStems(i)%TopDepth>data%State%TD_Liner(1)%TopDepth .and. data%State%TD_DrillStems(i)%TopDepth<data%State%TD_Liner(1)%DownDepth) then
           data%State%TD_DrillStems(i)%HoleDiameter = data%State%TD_Liner(1)%Id
       Else if (data%State%TD_OpenHole(1)%Length/=0.d0 .and. data%State%TD_DrillStems(i)%TopDepth>data%State%TD_OpenHole(1)%TopDepth .and. data%State%TD_DrillStems(i)%TopDepth<data%State%TD_OpenHole(1)%DownDepth) then
           data%State%TD_DrillStems(i)%HoleDiameter = data%State%TD_OpenHole(1)%Id
       Else if (data%State%TD_ROPHole(1)%Length/=0.d0 .and. data%State%TD_DrillStems(i)%TopDepth>data%State%TD_ROPHole(1)%TopDepth .and. data%State%TD_DrillStems(i)%TopDepth<data%State%TD_ROPHole(1)%DownDepth) then
           data%State%TD_DrillStems(i)%HoleDiameter = data%State%TD_ROPHole(1)%Id      !????????? check
       End If
       
       !=====> Viscosity Correction Coefficient Calculation
       if (data%State%TD_DrillStems(i)%HoleType == 1 .or. data%State%TD_DrillStems(i)%HoleType == 2) then
           data%State%TD_DrillStems(i)%MudVisCorrectCoef = (2.0d0/pi)*atan(sqrt(data%State%TD_DrillStems(i)%HoleDiameter**2-(data%State%TD_DrillStems(i)%Od**2)) &
                                                / (data%State%TD_DrillStems(i)%Od))*(4.0d0/pi-1.0d0) + 1.0d0
       else if (data%State%TD_DrillStems(i)%HoleType == 0) then
           data%State%TD_DrillStems(i)%MudVisCorrectCoef = 1.d0
       end if
   End Do
    
   
   
   
   
   
   
   
   
!=====> Out_of_Well Length Of DrillStem
   if (data%State%TD_DrillStems(data%State%TD_String%DrillStemComponentsNumbs)%TopDepth<0.) then
       data%State%TD_String%OutOfWellLength = abs(data%State%TD_DrillStems(data%State%TD_String%DrillStemComponentsNumbs)%TopDepth)
   else
       data%State%TD_String%OutOfWellLength = 0.d0
   end if
   
   
   
   
   
   
   
!=====> Near Floor Connection
   Do i = data%State%TD_String%DrillStemComponentsNumbs, 1, -1
       if ( data%State%TD_DrillStems(i)%TopDepth<=0.d0 ) then
           data%State%TD_String%NearFloorConnectionNo = i
           data%State%TD_String%NearFloorConnectionHeight = abs(data%State%TD_DrillStems(i)%TopDepth)
       end if
       if (data%State%TD_DrillStems(i)%TopDepth>0.d0) then
           exit
       end if
   End Do
   Call Set_NearFloorConnection(real(data%State%TD_String%NearFloorConnectionHeight))
   
   
   
   
   
   
   
   
!=====> Graphic Output
   kk = 0
   k  = 0
   Do i= 1,data%State%TD_String%StringConfigurationCount
       k = k+1
       if ( data%State%TD_DrillStems(k)%DownDepthIniG>0.d0 ) then
           kk = kk+1
           if (data%State%TD_DrillStems(k+data%State%TD_DrillStem(i)%Numbs-1)%TopDepthIniG<=0.d0) then
               exit
           end if
           k  = k+data%State%TD_DrillStem(i)%Numbs-1
       end if
   End Do
   
   if (allocated(data%State%G_StringElement)) deallocate(data%State%G_StringElement)
   allocate(data%State%G_StringElement(kk))
   k = 0
   Do i= 1,kk
       data%State%G_StringElement(kk-i+1)%Id            = data%State%TD_DrillStem(i)%Id
       data%State%G_StringElement(kk-i+1)%Od            = data%State%TD_DrillStem(i)%Od
       if ( data%State%TD_DrillStem(i)%ComponentType>4 ) then
           data%State%G_StringElement(kk-i+1)%ComponentType = 3
       else
           data%State%G_StringElement(kk-i+1)%ComponentType = data%State%TD_DrillStem(i)%ComponentType
       end if
       data%State%G_StringElement(kk-i+1)%TopDepth      = data%State%TD_DrillStems(k+data%State%TD_DrillStem(i)%Numbs)%TopDepthIniG
       data%State%G_StringElement(kk-i+1)%DownDepth     = data%State%TD_DrillStems(k+1)%DownDepthIniG
       k = k+data%State%TD_DrillStem(i)%Numbs
   End Do
   if (data%State%G_StringElement(1)%TopDepth<=0.d0) then
       data%State%G_StringElement(1)%TopDepth = 0.d0
   end if
   
   Call SetString(kk, data%State%G_StringElement)
   
   
   if(data%State%TD_DrillStems(1)%DownDepth<0.d0) then
       data%EquipmentControl%DrillingWatch%BitPosition = data%State%TD_DrillStems(1)%DownDepth
   else
       data%EquipmentControl%DrillingWatch%BitPosition = data%State%G_StringElement(kk)%DownDepth   !???????????
       !if ( data%State%G_StringElement(kk)%DownDepth>data%State%TD_WellGeneral%WellTotalLength ) then
       !    data%EquipmentControl%DrillingWatch%BitPosition = data%State%TD_WellGeneral%WellTotalLength        !???????????
       !end if
   end if
   
   !print*, 'kk=' , kk
   !Do i=1,kk
   !    print*, 'Number/' , 'downDepth/' , 'topDepth/' 
   !    print*, 'ID/' , 'OD/' , 'Type/'
   !    print*, i , data%State%G_StringElement(i)%DownDepth , data%State%G_StringElement(i)%TopDepth 
   !    print*, data%State%G_StringElement(i)%Id , data%State%G_StringElement(i)%Od , data%State%G_StringElement(i)%ComponentType
   !End Do
   
   
   
   
   
   
   !print*, 'Top Joint Height before=' , data%State%TD_String%TopJointHeight
!=====> Top Joint Height
   if ( Get_OperationCondition()==OPERATION_DRILL .and. Get_KellyConnection() == KELLY_CONNECTION_STRING ) then
       data%State%TD_String%TopJointHeight = abs(data%State%TD_DrillStems(data%State%TD_String%DrillStemComponentsNumbs-1)%TopDepth)
       !print*, 'TopDepth1=' , data%State%TD_DrillStems(data%State%TD_String%DrillStemComponentsNumbs-1)%TopDepth
       !print*, 'numb1=' , data%State%TD_String%DrillStemComponentsNumbs-1
       !print*, 'data%State%TD_String%TopJointHeight=' , data%State%TD_String%TopJointHeight
   else
       data%State%TD_String%TopJointHeight = abs(data%State%TD_DrillStems(data%State%TD_String%DrillStemComponentsNumbs)%TopDepth)
       !print*, 'TopDepth2=' , data%State%TD_DrillStems(data%State%TD_String%DrillStemComponentsNumbs)%TopDepth
       !print*, 'numb2=' , data%State%TD_String%DrillStemComponentsNumbs
       !print*, 'data%State%TD_String%TopJointHeight2=' , data%State%TD_String%TopJointHeight
   end if
   
   !print*, 'data%State%TD_String%DrillStemComponentsNumbs=' , data%State%TD_String%DrillStemComponentsNumbs
   !print*, 'data%State%TD_String%TopJointHeight_=' , data%State%TD_String%TopJointHeight
   !Print*, 'data%State%TD_StConn%ConnectionHeight_=' , data%State%TD_StConn%ConnectionHeight
   !Print*, 'data%State%TD_StConn%StringConnectionMode_=' , data%State%TD_StConn%StringConnectionMode
   !
   !print*, 'Top Joint Height after=' , data%State%TD_String%TopJointHeight
   

    
   
   
       
end subroutine 