pro tcrossp, v1, v2, newname = newname, error = error, out = out_d, diff_tsize_ok = diff_tsize_ok
if arg_present(error) then error = 0
if n_elements(v1) eq 0 then begin
dprint, 'tcrossp: argument v1 must be set'
return
endif
if is_string(v1) || n_elements(v1) eq 1 then begin
v1_name = tnames(v1)
if v1_name eq '' then begin
dprint, 'tcrossp: argument v1 tplot variable must be exist'
return
endif
get_data, v1_name, data = v1_d, dlimits = v1_dl,limits=v1_l
if ~is_struct(v1_d) || ~in_set(strlowcase(tag_names(v1_d)),'y') then begin
dprint,v1_name + ' has bad data'
return
endif
data1 = v1_d.y
time1 = v1_d.x
endif else begin
data1 = v1
v1_name = 'var1'
endelse
if n_elements(v2) eq 0 then begin
dprint, 'tcrossp: argument v2 must be set'
return
endif
if is_string(v2) || n_elements(v2) eq 1 then begin
v2_name = tnames(v2)
if v2_name eq '' then begin
dprint, 'tcrossp: argument v2 must be set'
return
endif
get_data, v2_name, data = v2_d, dlimits = v2_dl,limits=v2_l
if ~is_struct(v2_d) || ~in_set(strlowcase(tag_names(v2_d)),'y') then begin
dprint,v2_name + ' has bad data'
return
endif
data2 = v2_d.y
time2 = v2_d.x
endif else begin
v2_name = 'var2'
data2 = v2
endelse
if ~keyword_set(newname) then newname = v1_name+'_cross_'+v2_name
v1_s = size(data1,/dimension)
v2_s = size(data2,/dimension)
if(n_elements(v1_s) ne 2 || n_elements(v2_s) ne 2) then begin
dprint, 'tcrossp: V1 and V2 may contain only 2-d data arrays'
return
endif
If(keyword_set(diff_tsize_ok)) Then Begin
If(n_elements(time1) Eq 0 Or n_elements(time2) Eq 0) Then Begin
dprint, 'Use of diff_tsize_ok keyword is only possible for input tplot variables'
return
Endif
d2tmp = {x:time2, y:data2}
data2 = data_cut(temporary(d2tmp), time1)
v2_s = size(data2, /dimension)
v2_d = {x:time1, y:data2}
Endif
if(not array_equal([v1_s[0],3],v2_s)) then begin
dprint, 'tcrossp: Dimensions of v2 incorrect'
return
endif
if(not array_equal([v2_s[0],3],v1_s)) then begin
dprint, 'tcrossp: Dimensions of v1 incorrect'
return
endif
if is_struct(v1_d) && is_struct(v2_d) && ~array_equal(v1_d.x,v2_d.x) then begin
dprint,'WARNING: time arrays do not match'
endif
x = data1[*,1] * data2[*,2] - data1[*,2] * data2[*,1]
y = data1[*,2] * data2[*,0] - data1[*,0] * data2[*,2]
z = data1[*,0] * data2[*,1] - data1[*,1] * data2[*,0]
out_d = [[x],[y],[z]]
if ~arg_present(out_d) then begin
if ~is_struct(v1_d) && ~is_struct(v2_d) then begin
dprint, 'tcrossp: No tplot arguments are present, cannot construct output'
return
endif
if is_struct(v1_d) then begin
str_element,v1_d,'v',success=s
if s then $
out = {x:v1_d.x, y:out_d, v:v1_d.v} $
else $
out = {x:v1_d.x, y:out_d}
dl = v1_dl
l = v1_l
endif else begin
str_element,v2_d,'v',success=s
if s then $
out = {x:v2_d.x, y:out_d, v:v2_d.v} $
else $
out = {x:v2_d.x, y:out_d}
dl = v2_dl
l = v2_l
endelse
store_data, newname, data = out, dlimits = dl,limits=l
endif
error = 1
return
end