pro dsl2gse,name_thx_xxx_in,name_thx_spinras,name_thx_spindec,name_thx_xxx_out,GSE2DSL=GSE2DSL,ignore_dlimits=ignore_dlimits
cotrans_lib
get_data,name_thx_xxx_in,data=thx_xxx_in, limit=l_in, dl=dl_in
get_data,name_thx_spinras,data=thx_spinras
get_data,name_thx_spindec,data=thx_spindec
if size(thx_spinras, /type) ne 8 || size(thx_spindec, /type) ne 8 then begin
message, 'aborted: must load spin vector data from state file. Try calling thm_load_state,/get_support'
endif
if min(thx_spinras.x,/nan)-min(thx_xxx_in.x,/nan) gt 60*60 || max(thx_xxx_in.x,/nan) - max(thx_spinras.x,/nan) gt 60*60 then begin
dprint,'NON-FATAL-ERROR: ' + name_thx_spinras + ' and ' + name_thx_xxx_in + ' fail to overlap for time greater than 1 hour. Data may have significant interpolation errors.'
endif
if min(thx_spindec.x,/nan)-min(thx_xxx_in.x,/nan) gt 60*60 || max(thx_xxx_in.x,/nan) - max(thx_spindec.x,/nan) gt 60*60 then begin
dprint,'NON-FATAL-ERROR: ' + name_thx_spindec + ' and ' + name_thx_xxx_in + ' fail to overlap for time greater than 1 hour. Data may have significant interpolation errors.'
endif
data_in_coord = cotrans_get_coord(dl_in)
thx_xxx_out=thx_xxx_in
timeS=time_struct(thx_xxx_in.X)
timeSAtt=time_struct(thx_spinras.X)
if keyword_set(GSE2DSL) then begin
DPRINT, 'GSE-->DSL'
if keyword_set(ignore_dlimits) then begin
data_in_coord='gse'
endif
if ~ strmatch(data_in_coord, 'unknown') && ~ strmatch(data_in_coord, $
'gse') then begin
dprint, 'coord of input '+name_thx_xxx_in+': '+data_in_coord+ $
' must be GSE'
return
end
out_coord = 'dsl'
isGSE2DSL=1
endif else begin
DPRINT, 'DSL-->GSE'
if keyword_set(ignore_dlimits) then begin
data_in_coord='dsl'
endif
if ~ strmatch(data_in_coord, 'unknown') && ~ strmatch(data_in_coord, $
'dsl') then begin
dprint, 'coord of input '+name_thx_xxx_in+': '+data_in_coord+ $
' must be DSL'
return
end
out_coord = 'gse'
isGSE2DSL=0
endelse
thx_spinras_highres=thm_interpolate_state(thx_xxx_in=thx_xxx_in,thx_spinras=thx_spinras)
thx_spindec_highres=thm_interpolate_state(thx_xxx_in=thx_xxx_in,thx_spindec=thx_spindec)
timeS=time_struct(thx_xxx_in.X)
count=SIZE(thx_xxx_in.X,/N_ELEMENTS)
DPRINT, 'number of records: ',count
countAtt=SIZE(thx_spinras.X,/N_ELEMENTS)
DPRINT, 'number of records: ',countAtt
spla=(90.d0-(thx_spindec_highres.Y))*!dpi/180.d0
splo=thx_spinras_highres.Y*!dpi/180.d0
zscs=[[(sin(spla)*cos(splo))],[(sin(spla)*sin(splo))],[(cos(spla))]]
if isGSE2DSL eq 0 then begin
subGEI2GSE,timeS,zscs,zscsGSE
sun=[1.d0,0.d0,0.d0]
yscs=[[zscsGSE[*,1]*sun[2]-zscsGSE[*,2]*sun[1]],[zscsGSE[*,2]*sun[0]-zscsGSE[*,0]*sun[2]],[zscsGSE[*,0]*sun[1]-zscsGSE[*,1]*sun[0]]]
yscsNorm=sqrt(yscs[*,0]^2.0+yscs[*,1]^2.0+yscs[*,2]^2.0)
yscs[*,0]=yscs[*,0]/yscsNorm
yscs[*,1]=yscs[*,1]/yscsNorm
yscs[*,2]=yscs[*,2]/yscsNorm
xscs=[[yscs[*,1]*zscsGSE[*,2]-yscs[*,2]*zscsGSE[*,1]],[yscs[*,2]*zscsGSE[*,0]-yscs[*,0]*zscsGSE[*,2]],[yscs[*,0]*zscsGSE[*,1]-yscs[*,1]*zscsGSE[*,0]]]
thx_xxx_out.Y[*,0]=thx_xxx_in.Y[*,0]*xscs[*,0]+thx_xxx_in.Y[*,1]*yscs[*,0]+thx_xxx_in.Y[*,2]*zscsGSE[*,0]
thx_xxx_out.Y[*,1]=thx_xxx_in.Y[*,0]*xscs[*,1]+thx_xxx_in.Y[*,1]*yscs[*,1]+thx_xxx_in.Y[*,2]*zscsGSE[*,1]
thx_xxx_out.Y[*,2]=thx_xxx_in.Y[*,0]*xscs[*,2]+thx_xxx_in.Y[*,1]*yscs[*,2]+thx_xxx_in.Y[*,2]*zscsGSE[*,2]
endif else begin
subGEI2GSE,timeS,zscs,zscsGSE
sun=[1.d0,0.d0,0.d0]
yscs=[[zscsGSE[*,1]*sun[2]-zscsGSE[*,2]*sun[1]],[zscsGSE[*,2]*sun[0]-zscsGSE[*,0]*sun[2]],[zscsGSE[*,0]*sun[1]-zscsGSE[*,1]*sun[0]]]
yscsNorm=sqrt(yscs[*,0]^2.0+yscs[*,1]^2.0+yscs[*,2]^2.0)
yscs[*,0]=yscs[*,0]/yscsNorm
yscs[*,1]=yscs[*,1]/yscsNorm
yscs[*,2]=yscs[*,2]/yscsNorm
xscs=[[yscs[*,1]*zscsGSE[*,2]-yscs[*,2]*zscsGSE[*,1]],[yscs[*,2]*zscsGSE[*,0]-yscs[*,0]*zscsGSE[*,2]],[yscs[*,0]*zscsGSE[*,1]-yscs[*,1]*zscsGSE[*,0]]]
thx_xxx_out.Y[*,0]=thx_xxx_in.Y[*,0]*xscs[*,0]+thx_xxx_in.Y[*,1]*xscs[*,1]+thx_xxx_in.Y[*,2]*xscs[*,2]
thx_xxx_out.Y[*,1]=thx_xxx_in.Y[*,0]*yscs[*,0]+thx_xxx_in.Y[*,1]*yscs[*,1]+thx_xxx_in.Y[*,2]*yscs[*,2]
thx_xxx_out.Y[*,2]=thx_xxx_in.Y[*,0]*zscsGSE[*,0]+thx_xxx_in.Y[*,1]*zscsGSE[*,1]+thx_xxx_in.Y[*,2]*zscsGSE[*,2]
endelse
dl_out=dl_in
cotrans_set_coord, dl_out, out_coord
str_element, dl_out, 'ytitle', /delete
l_out=l_in
str_element, l_out, 'ytitle', /delete
store_data,name_thx_xxx_out,data=thx_xxx_out, limit=l_out, dl=dl_out
DPRINT, 'done'
end