subroutine TVD_Calculator(MeasuredDepth,VerticalDepth)

   Use TD_DrillStemComponents
   use SimulationVariables !@
   use SimulationVariables !@
   Use TD_StringConnectionData
   

   implicit none

   Integer  :: kk
   REAL(8)  :: MeasuredDepth , VerticalDepth
   REAL(8)  :: mm , nn , dl , StartAngle , EndAngle
   
   
   
   
   
   if ( MeasuredDepth<=0. ) then
       VerticalDepth = MeasuredDepth
       !data%State%TDGeo%Angle(ii) = 0.d0
       return
   end if
   
   
   
   mm       = 0.d0    !last vertical depth
   nn       = 0.d0    !last measured depth
   EndAngle = data%State%TD_WellGeo(1)%StartAngle
   !data%State%TDGeo%Angle(jj) = EndAngle
   
   do kk = 1, data%State%TD_WellGeneral%WellIntervalsCount
           StartAngle = EndAngle
           if ( MeasuredDepth>data%State%TD_WellGeo(kk)%TopDepth ) then
               if ( MeasuredDepth>data%State%TD_WellGeo(kk)%DownDepth ) then
                   dl = data%State%TD_WellGeo(kk)%DownDepth-nn                              ![ft]
                   nn = data%State%TD_WellGeo(kk)%DownDepth
                   if ( data%State%TD_WellGeo(kk)%HoleType==0 ) then
                       EndAngle      = StartAngle
                       VerticalDepth = mm+(dl*cos(data%State%TD_WellGeo(kk)%StartAngle))    ![ft]
                       !data%State%TDGeo%Angle(ii)    = EndAngle
                   else if ( data%State%TD_WellGeo(kk)%HoleType==1 ) then
                       EndAngle      = StartAngle+(dl/data%State%TD_WellGeo(kk)%RCurvature) !?????
                       VerticalDepth = mm+(data%State%TD_WellGeo(kk)%RCurvature*sin(abs(EndAngle)-abs(StartAngle))*cos(abs(StartAngle)))-(data%State%TD_WellGeo(kk)%RCurvature*(1.-cos(abs(EndAngle)-abs(StartAngle)))*sin(abs(StartAngle)))
                       !data%State%TDGeo%Angle(ii)    = EndAngle
                   else if ( data%State%TD_WellGeo(kk)%HoleType==2 ) then
                       EndAngle      = StartAngle-(dl/data%State%TD_WellGeo(kk)%RCurvature) !?????
                       VerticalDepth = mm+(data%State%TD_WellGeo(kk)%RCurvature*sin(abs(abs(EndAngle)-abs(StartAngle)))*cos(abs(StartAngle)))+(data%State%TD_WellGeo(kk)%RCurvature*(1.-cos(abs(abs(EndAngle)-abs(StartAngle))))*sin(abs(StartAngle)))
                       !data%State%TDGeo%Angle(ii)    = EndAngle
                   end if
                   mm = VerticalDepth
               else
                   dl = MeasuredDepth-nn
                   nn = MeasuredDepth
                   if ( data%State%TD_WellGeo(kk)%HoleType==0 ) then
                       EndAngle      = StartAngle
                       VerticalDepth = mm+(dl*cos(data%State%TD_WellGeo(kk)%StartAngle))
                       !data%State%TDGeo%Angle(ii)    = EndAngle
                   else if ( data%State%TD_WellGeo(kk)%HoleType==1 ) then
                       EndAngle      = StartAngle+(dl/data%State%TD_WellGeo(kk)%RCurvature) !?????
                       VerticalDepth = mm+(data%State%TD_WellGeo(kk)%RCurvature*sin(abs(EndAngle)-abs(StartAngle))*cos(abs(StartAngle)))-(data%State%TD_WellGeo(kk)%RCurvature*(1.-cos(abs(EndAngle)-abs(StartAngle)))*sin(abs(StartAngle)))
                       !data%State%TDGeo%Angle(ii)    = EndAngle
                   else if ( data%State%TD_WellGeo(kk)%HoleType==2 ) then
                       EndAngle      = StartAngle-(dl/data%State%TD_WellGeo(kk)%RCurvature) !?????
                       VerticalDepth = mm+(data%State%TD_WellGeo(kk)%RCurvature*sin(abs(abs(EndAngle)-abs(StartAngle)))*cos(abs(StartAngle)))+(data%State%TD_WellGeo(kk)%RCurvature*(1.-cos(abs(abs(EndAngle)-abs(StartAngle))))*sin(abs(StartAngle)))
                       !data%State%TDGeo%Angle(ii)    = EndAngle
                   end if
                   mm = VerticalDepth
                   exit
               end if
           end if
   end do
   
   
   
   
   
end subroutine