;+
; NAME:
;       V4MCART
; PURPOSE:
;       Return the distance between two points in a cartesian coordinate system.
;       (normally called by V4METRIC based on the v4type structure)
; CALLING SEQUENCE:
;       out = V4MCART(in1,in2,/verbose,index=ix,units=units,dim=dim)
; INPUTS:
;       in1, in2 - V4 structures containing the co-ordinates to measure
; RETURNS:
;       The distance between the two points
; KEYWORDS: 
;       VERBOSE - Be chatty
;       INDEX   - A 4-array that re-orders the input vectors
;       UNITS   - If present, V4MCART attempts to return a value in the units given.
;                 If not defined, V4MCART sets this to the name of the units in which the
;                 value is returned.
;       DIM     - Number indicating how many dimensions to use (2,3,4)
; METHOD: 
;	We can't ignore the "index" input, because even though the 3-space coordinates
;	are uniform, we have to be positive about which is the time axis.
;
; CAVEATS: 
;	We assume that we've been fed a canonicalized, matched set of v4 vectors.
;
;	We further assume that all the spatial coordinates are in the same units.
;
; AUTHOR:
;	Craig DeForest
; HISTORY:
;	Written 10-Feb-1997 CED
;-
function v4mcart,in1,in2,verbose=verbose,index=index,units=units,dim=dim
if not isvalid(dim) then dim=3
if not isvalid(index) then index = [0,1,2,3]
if not isvalid(units) then units = in1.(index(1)+5)

diffs = [ in2.(index(1))-in1.(index(1)) $
	 ,in2.(index(2))-in1.(index(2)) $
	 ,in2.(index(3))-in1.(index(3)) $
	] * zunits(units,in1.(5+index(1)))

if(dim eq 4) then begin
	if in1.(index(0)+5) eq 'Timestamp' then begin
		a = str2utc(in1.(index(0)))
		b = str2utc(in2.(index(0)))
		deltat = ((b.mjd-a.mjd)*24.0*3600.0)+(b.time-a.time)/1000.0
	end else $
		deltat = (in1.(index(0)))-(in2.(index(0))) * zunits('seconds',in1.(index(0)+5))

	diffs = [diffs, -1 * (2.9979245812D8) * deltat * zunits(units,'meters')]
	return,sqrt(total(diffs*diffs))
end else if(dim eq 3) then return,sqrt(total(diffs*diffs))		$
    else if(dim eq 2) then return,sqrt(total(diffs(0:1)*diffs(0:1)))
end

