;+
; NAME:
;	GENERAL_SPEED
;
;
; PURPOSE:
;	Calculate apparent HT of TIPD from speed and size
;
;
; CATEGORY:
;	Utils
;
;
; CALLING SEQUENCE:
;	p = general_speed(t, v, th)
;
;
; INPUTS:
;	t	the times at which to get heights (hours)
;	v	The actual speed (km/s)
;	th	The size (half angle of the transient) (degrees)
;
;
; OUTPUTS:
;	p	The apparent height a a function of time.
;
;
; OPTIONAL OUTPUTS:
;	vp	The apparent speeds
;
;
; MODIFICATION HISTORY:
;	Original: 12/6/03
;-

function general_speed, t, v, th, vp = vp

au = 1.5e8                      ;km
thr = th*!dtor
ts = t*3600.

r = (v*ts)/au
locs = where(r le 1.0)
r = r[locs]
thr = thr[locs]

eps = atan(r*sin(thr), 1-r*cos(thr))
locs =  where(thr+eps gt !pi/2, na)
if na ne 0 then eps[locs] = asin(r[locs])

p = sin(complex(eps))

if arg_present(vp) then begin
    rr = r*sin(thr)/(1 - r*cos(thr))

    vp =  cos(eps)*(1./(1+rr^2))* $
      (1./(1-r*cos(thr)) + $
       r*cos(thr)/(1-r*cos(thr))^2)*v*sin(thr)
    if na ne 0 then vp[locs] = v
endif

return, p

end
