;+
; NAME:
;	V4XS2SRS
; PURPOSE:
;	Convert Solar Range Solar to Solar (Earth-Range) 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:
;	Use the Law of Cosines in the Earth-Sun-Point plane to get
;	the Earth-Point radius based on the Earth-Sun radius, the given
;	radius, and the Sun-Earth-Point angle.
;
;	NOTE: because of the ambiguity about reflection in the Z=0 plane,
;	we assume the point is CLOSER to us than the Sun.
; AUTHOR:
;	Craig DeForest
; HISTORY: 
;	Initial implementation, 7-Oct-1997
;
;-
function V4XSRS2S,in,verbose=v,index=ix,outdex=ox,ocoords=ocoords

;;;
;;; Check out input keywords for existence...
;;;
;if (isvalid(ix) or isvalid(ox)) then print,"WARNING - V4XSRS2S - ignoring indexing!"
v = keyword_set(v)

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

i2 = v4canon(in)

xa = in.(1)/3600.0/!radeg
ya = in.(2)/3600.0/!radeg

;;;
;;; Calculate the Earth-Sun distance...
pbr = zpb0r(i2.(ix(0)))
z = zunits(i2.(3+5),"solar-radii")
if(total(z eq 0) gt 0) then message,"Non-distance co-ordinate found in cartesian system!"
r = 1.0/sin(((pbr(2))/60.0)/!radeg) * z
if(v) then print,"V4CSC2S: PB0R returned ",pbr(2)," arcmin radius; distance is ",r," ",i2.(iu(3)),'.'

;;;
;;;  Calculate angle to the l.o.s. from Sun Center
zeta = atan( sqrt(tan(xa)*tan(xa) + tan(ya)*tan(ya)) )

;;;
;;; Plug in distance formula...
;;; (note: you can also choose to add the sqrt, to get the more distant
;;; solution...)
if ((i2.defined)(3)) then $
    i2.(3) = r * cos(zeta) - sqrt(i2.(3)*i2.(3) - sin(zeta)*sin(zeta)*r*r) $
else $
    i2.(3) = 0
	
i2.coords = "Earth Range Solar"
return,i2
end
