subroutine Drawworks_Solver_OffMode

    Use CUnityInputs
    use UnitySignalVariables
    use UnitySignalsModule
    use SimulationVariables !@
    use OperationScenariosModule
    use OperationScenariosModule !!CElevator...
    use UnitySignalsModule
    ! Use CTdsElevatorModesEnumVariables
    use OperationScenariosModule
    
    
	IMPLICIT NONE
    
    
    Integer :: j
    Integer :: CrownCollision_Status , FloorCollision_Status , CrownWarning_Status , FloorWarning_Status
    
    
    
    
    data%State%Drawworks%motion      = 0
    data%State%Drawworks%w_old_drum  = 0.d0
    data%State%Drawworks%w_drum      = 0.d0
    data%State%Drawworks%Hook_Height = data%State%Drawworks%Hook_Height
    
    
    
    
    
    
!====================================================
!               Collision & Warning
!====================================================
    if ( data%State%Drawworks%CrownCollision == .false. ) then
        CrownCollision_Status = 0
    end if
    if ( data%State%Drawworks%FloorCollision == .false. ) then
        FloorCollision_Status = 0
    end if
     
     
     
     
     
     
     
!====================================================
!        Crown Collision (Max_Hook_Height)
!====================================================
    if ( ((3.280839895*data%State%Drawworks%Hook_Height)>=data%State%Drawworks%max_Hook_Height) .and. (any(data%State%Drawworks%DrillModeCond==(/3,4,7,10,11,12,14/))) ) then
        if ( CrownCollision_Status==0 .and. data%State%Drawworks%motion==1 ) then
            CrownCollision_Status         = 1
            data%State%Drawworks%CrownCollision      = .true.
            data%State%Drawworks%SoundCrownCollision = .true.
        else
            data%State%Drawworks%SoundCrownCollision = .false.
        end if
        if ( data%State%Drawworks%motion==-1 .and. data%State%Drawworks%CrownCollision==.false. ) then
            data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
        else
            Call DWFixModeMotion
        end if
        return
    end if
     
     
     
     
     
     
     
!====================================================
!        Floor Collision (Min_Hook_Height)
!====================================================
    if ( ((3.280839895*data%State%Drawworks%Hook_Height)<=data%State%Drawworks%min_Hook_Height) .and. (any(data%State%Drawworks%DrillModeCond==(/3,4,7,10,11,12,14/)))  ) then
        if ( FloorCollision_Status==0 .and. data%State%Drawworks%motion==-1 ) then
            FloorCollision_Status         = 1
            data%State%Drawworks%FloorCollision      = .true.
            data%State%Drawworks%SoundFloorCollision = .true.
        else
            data%State%Drawworks%SoundFloorCollision = .false.
        end if
        if ( data%State%Drawworks%motion==1  .and. data%State%Drawworks%FloorCollision==.false. ) then
            data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
        else
            Call DWFixModeMotion
        end if
        return
    end if
     
     
     
     
     
     
     
     
!====================================================
!            Warning (Max_Hook_Height)
!====================================================
    if ( ((3.280839895*data%State%Drawworks%Hook_Height)>=data%State%Drawworks%max_Hook_Height) .and. (any(data%State%Drawworks%DrillModeCond==(/1,2,5,6,8,9,13/))) ) then
        if ( data%State%Drawworks%motion==-1 ) then
            data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
        else
            Call DWFixModeMotion
        end if
        return
    end if
     
     
     
     
     
     
     
!====================================================
!            Warning (Min_Hook_Height)
!====================================================
    if ( ((3.280839895*data%State%Drawworks%Hook_Height)<=data%State%Drawworks%min_Hook_Height) .and. (any(data%State%Drawworks%DrillModeCond==(/1,2,5,6,8,9,13/)))  ) then
        if ( data%State%Drawworks%motion==1 ) then
            data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
        else
            Call DWFixModeMotion
        end if
        return
    end if
     
     
     
     
     
     
     
     
     
!====================================================
! ELEVATOR CONNECTION STRING (SLIPS SET , No Motion)
!====================================================
    if ( data%State%Drawworks%DriveType==1 .and. Get_Slips() == SLIPS_SET_END .and. Get_ElevatorConnection() == ELEVATOR_CONNECTION_STRING .and. data%State%Drawworks%motion/=-1 ) then
        Call DWFixModeMotion
        return
    end if
     
    if ( data%State%Drawworks%DriveType==0 .and. Get_Slips() == SLIPS_SET_END .and. (Get_TdsConnectionModes()==TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()==TDS_ELEVATOR_CONNECTION_STRING) .and. data%State%Drawworks%motion/=-1 ) then
        Call DWFixModeMotion
        return
    end if
     
     
     
     
     
     
     
     
     
!====================================================
!       RAM & ToolJoint Collision (Top of RAM)
!====================================================
    Do j = 2,4  !startup problem ???????
        if ( data%State%Drawworks%TDBOPElementNo(j)/=0 ) then
            if ( ((data%State%Drawworks%TDBOPHeight(j)-data%State%Drawworks%TDBOPThickness)<=(data%State%Drawworks%TDDrillStemsTopDepth(data%State%Drawworks%TDBOPElementNo(j))+data%State%Drawworks%TDDrillStemsToolJointRange(data%State%Drawworks%TDBOPElementNo(j)))) .and. ((data%State%Drawworks%TDBOPHeight(j)-data%State%Drawworks%TDBOPThickness)>data%State%Drawworks%TDDrillStemsTopDepth(data%State%Drawworks%TDBOPElementNo(j))) .and. (data%State%Drawworks%TDBOPRamDiam(j)<(2.d0*12.d0*data%State%Drawworks%TDDrillStemsRtoolJoint(data%State%Drawworks%TDBOPElementNo(j)))) ) then
                if ( data%State%Drawworks%motion==1 ) then
                    data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
                else
                    Call DWFixModeMotion
                end if
                return
            end if
        end if
    End Do
     
     
     
     
     
     
      
     
     
!====================================================
!      RAM & ToolJoint Collision (Bottom of RAM)
!====================================================
    Do j = 2,4
        if ( data%State%Drawworks%TDBOPElementNo(j)/=0 ) then
            if ( ((data%State%Drawworks%TDBOPHeight(j)+data%State%Drawworks%TDBOPThickness)>=(data%State%Drawworks%TDDrillStemsDownDepth(data%State%Drawworks%TDBOPElementNo(j))-data%State%Drawworks%TDDrillStemsToolJointRange(data%State%Drawworks%TDBOPElementNo(j)))) .and. ((data%State%Drawworks%TDBOPHeight(j)+data%State%Drawworks%TDBOPThickness)<data%State%Drawworks%TDDrillStemsDownDepth(data%State%Drawworks%TDBOPElementNo(j))) .and. (data%State%Drawworks%TDBOPRamDiam(j)<(2.d0*12.d0*data%State%Drawworks%TDDrillStemsRtoolJoint(data%State%Drawworks%TDBOPElementNo(j)))) ) then
                if ( data%State%Drawworks%motion==-1 ) then
                    data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
                else
                    Call DWFixModeMotion
                end if
                return
            end if
        end if
    End Do
     
     
     
     
      
      
      
!====================================================
!               TopDrive (TdsStemIn)
!====================================================
    if ( (data%State%Drawworks%DriveType==0) .and. (Get_TdsStemIn()) .and. Get_Slips() == SLIPS_SET_END  ) then
        if ( data%State%Drawworks%motion==1 ) then
            data%State%Drawworks%Hook_Height_final = 3.280839895d0*data%State%Drawworks%Hook_Height    ![ft]
        else
            Call DWFixModeMotion
        end if
        return
    end if
      
      
      
      
     
     
     
     
!=====> BottomHole ROP Condition
    if ( (int(data%State%Drawworks%TDDrillStemBottom*10000.d0)>=(int((data%State%Drawworks%TDWellTotalLength+data%State%Drawworks%TDDlMax)*10000.d0))) .and. (data%State%Drawworks%motion==-1 .or. data%State%Drawworks%motion==0) ) then
        if ( data%State%Drawworks%StringIsBottomOfWell==0 ) then
            data%State%Drawworks%Hook_Height_final = data%State%Drawworks%Hook_Height_final+(data%State%Drawworks%TDDrillStemBottom-(data%State%Drawworks%TDWellTotalLength+data%State%Drawworks%TDDlMax))
            data%State%Drawworks%StringIsBottomOfWell = 1
        end if
        Call DWFixModeMotion
        return
    else
        data%State%Drawworks%StringIsBottomOfWell = 0
    end if
     
     
     
     
     
     
     data%State%Drawworks%Hook_Height_final=3.280839895d0*data%State%Drawworks%Hook_Height                                    ![ft]
     data%State%Drawworks%HookHeight_graph_output=0.1189d0*((3.280839895d0*data%State%Drawworks%Hook_Height)-28.0d0)-2.6d0    ![ft]
     
     
     
     
  
end subroutine Drawworks_Solver_OffMode