;+
;procedure: cotrans, name_in, name_out [, time]
;
;Purpose: geophysical coordinate transformations
;
;         GEI<-->GSE;
;         GSE<-->GSM;
;
;         interpolates the spinphase, right ascension, declination
;         updates coord_sys atribute of output tplot variable.
;
;inputs
;
;   name_in 	... data in the input coordinate system (t-plot variable name, 
;                                                        or array)
;   name_out    ... variable name for output            (t-plot variable name,
;                                                        or array)
;   time        ... optional input: array of times for input values, if provided
;                   then the first parameter is an array, and the second
;                   parameter is a named variable to contain the output array.
;
;
;keywords:
;   TRANSFORMATIONS
;
;
;	/GEI2GSE
;	/GSE2GEI
;
;	/GSE2GSM
;	/GSM2GSE
;
;Examples:
;
;
;      cotrans('tha_fgl_gse','tha_fgl_gsm',/GSE2GSM)
;      cotrans('tha_fgl_gsm','tha_fgl_gse',/GSM2GSE)
;
;      cotrans('tha_fgl_gse','tha_fgl_gei',/GSE2GEI)
;      cotrans('tha_fgl_gei','tha_fgl_gse',/GEI2GSE)
;
;Notes: under construction!!
;
;Written by: Hannes Schwarzl
;
; $LastChangedBy: kenb-mac $
; $LastChangedDate: 2007-07-16 19:16:48 -0700 (Mon, 16 Jul 2007) $
; $LastChangedRevision: 1125 $
; $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/ssl_general/branches/QA/cotrans/cotrans.pro $
;-
pro cotrans, name_in,name_out,time,GSM2GSE=GSM2GSE,GSE2GEI=GSE2GEI,GSE2GSM=GSE2GSM,GEI2GSE=GEI2GSE


cotrans_lib

;PRINT,'will run faster soon ...'

if n_params() eq 2 then begin
; get the data using t-plot name
   get_data,name_in,data=data_in, limit=l_in, dl=dl_in ; krb

   data_in_coord = cotrans_get_coord(dl_in) ; krb

endif else begin
   data_in={x:time, y:name_in}
   data_in_coord = 'unknown'
endelse

is_valid_keyws=0





;GSE GSM
if keyword_set(GSE2GSM) then begin
	is_valid_keyws=1
        if ~ strmatch(data_in_coord, 'unknown') && ~ strmatch(data_in_coord, $
                                                              'gse') then begin
           print, 'coord of input '+name_in+': '+data_in_coord+ $
                  'must be GSE'
           return
        end
        sub_GSE2GSM,data_in,data_conv
        out_coord = 'gsm'
endif

if keyword_set(GSM2GSE) then begin
	is_valid_keyws=1
        if ~ strmatch(data_in_coord, 'unknown') && ~ strmatch(data_in_coord, $
                                                              'gsm') then begin
           print, 'coord of input '+name_in+': '+data_in_coord+ $
                  'must be GSM'
           return
        end
	sub_GSE2GSM,data_in,data_conv,/GSM2GSE
        out_coord = 'gse'
endif



;GEI GSE
if keyword_set(GEI2GSE) then begin
	is_valid_keyws=1
        if ~ strmatch(data_in_coord, 'unknown') && ~ strmatch(data_in_coord, $
                                                              'gei') then begin
           print, 'coord of input '+name_in+': '+data_in_coord+ $
                  'must be GEI'
           return
        end
	sub_GEI2GSE,data_in,data_conv
        out_coord = 'gse'
endif

if keyword_set(GSE2GEI) then begin
	is_valid_keyws=1
        if ~ strmatch(data_in_coord, 'unknown') && ~ strmatch(data_in_coord, $
                                                              'gse') then begin
           print, 'coord of input '+name_in+': '+data_in_coord+ $
                  'must be GSE'
           return
        end
	sub_GEI2GSE,data_in,data_conv,/GSE2GEI
        out_coord = 'gei'
endif



if (is_valid_keyws eq 0) then begin
	PRINT,'Not a valid combination of input arguments'
endif

if n_params() eq 2 then begin
   dl_conv = dl_in
   cotrans_set_coord,  dl_conv, out_coord ;krb
   ;; clear ytitle, so that it won't contain wrong info.
   str_element, dl_conv, 'ytitle', /delete
   l_conv=l_in
   str_element, l_conv, 'ytitle', /delete

   store_data,name_out,data=data_conv, limit=l_conv, dl=dl_conv ;krb
endif else name_out = data_conv.y

;RETURN, data_conv
end