;+
; NAME:
;	V4XSC2S
; PURPOSE:
;	Convert Earth-Solar Cartesian to Solar coordinates
;	(normally called by V4XFORM based on the v4xforms structure)
; CALLING SEQUENCE:
;	out = V4XCART2SPH(in,/verbose,index=ix,outdex=ox,ocoord=ocoord)
; INPUTS:
;	in - a V4 structure containing the co-ordinates to 
;	     transform
; RETURNS:
;	The transformed vector (or array)
; KEYWORDS:
;	VERBOSE - Be chatty
;	INDEX   - A 4-array that re-orders the input vector  (IGNORED)
;	OUTDEX  - A 4-array that re-orders the output vector (IGNORED)
;	OCOORD  - The coordinate system name for the output coordinates 
; 
; METHOD:
;	Translates the Cartesian coordinates from Earth to the Sun, 
;	then calls V4XCART2SPH.	
;
;	The "standard interpretation" is as follows:
;	I0 = time		O0 = time		
;	I1 = X (west)		O1 = W angle
;	I2 = Y (North)		O2 = N angle
;	I3 = Z (toward Earth)	O3 = R (from Earth)
;
;	(Note that "North" is "Solar North projected into the image plane")
; AUTHOR:
;	Craig DeForest
; HISTORY: 
;	Initial implementation, 7-Oct-1997
; CAUTION:
;	Doesn't check the type of the input V4 structure.
;-
function V4XSC2S,in,verbose=v,index=ix,outdex=ox,ocoords=ocoords

;;;
;;; Check out input keywords for existence...
;;;
if (isvalid(ix) or isvalid(ox)) then $
	if total(where((ix ne [0,1,2,3]) or (ox ne [0,1,2,3]))) then $
		print,"WARNING - V4XSC2S - ignoring indexing!"
v = keyword_set(v)

ix = [0,1,2,3]
ox = ix
iu = ix+5
ou = ox+5

;;;
;;; Convert to uniform distances, in a spare copy of the input...
i2 = in

z2 = zunits(i2.(iu(2)),i2.(iu(1)))
z3 = zunits(i2.(iu(3)),i2.(iu(1)))

if total((z2*z3) eq 0) gt 0 then message,"V4xcart2sph: Non-compatible units in the cartesian frame!"
i2.(ix(2)) = i2.(ix(2))/z2
i2.(ix(3)) = i2.(ix(3))/z3

i2.(iu(2))=i2.(iu(1))
i2.(iu(3))=i2.(iu(1))

;;;
;;; Copy the time co-ordinate and, if desired, the coords keyword
out = in
out.(ox(0))   	= in.(ix(0))
out.(ou(0))	= in.(iu(0))
if isvalid(ocoords) then out.coords=ocoords

;;;
;;; Calculate the Earth-Sun distance...
r = sunearthdist(i2.(ix(0)),i2.(iu(3)))
if(v) then print,"V4XCSC2S: earth-sun distance is ",r,in.(iu(3))

;;;
;;; Translate the origin from the Sun to the Earth...
if (not i2(0).defined(ix(3))) then i2.(ix(3)) = 0
i2.(ix(3)) = r - i2.(ix(3)) 

;;;
;;; Convert to spherical...
;;; Note that the index to v4xcart2sph gives us a LEFT-handed
;;; coordinate system; this is OK because we reversed the sign of
;;; i2.(ix(3)) above, so parity is conserved.
out = v4xcart2sph(i2,v=v,index=[0,3,1,2],outdex=[0,1,2,3],ocoord="Solar")

return,v4canon(out)
end
