;+
;PROCEDURE:	load_sta_l1_magb
;PURPOSE:	
;   loads STEREOB/IMPACT MAG high resolution burst data for "tplot".
;
;INPUTS:	none, but will call "timespan" if time
;		range is not already set.
;KEYWORDS:
;  TIME_RANGE:  2 element vector specifying the time range
;  POLAR:       Also computes the B field in polar coordinates.
;  DATA:        Data returned in this named variable.
;  NODATA:	Returns 0 if data exists for time range, otherwise returns 1.
;  PREFIX:	(string) prefix for tplot variables.  Default is 'sta_'
;  NAME:	(string) name for tplot variables. Default is 'sta_Bburst'
;  RESOLUTION:	Resolution to return in seconds.
;  MASTERFILE:	(string) full filename of master file.
;SEE ALSO: 
;  "make_cdf_index","loadcdf","loadcdfstr","loadallcdf"
;
;CREATED BY:	Peter Schroeder
;LAST MODIFIED:	11/27/06
;-


pro load_sta_l1_magb,time_range=trange,polar=polar,data=d,  $
  nodata=nodat, $
  prefix = prefix, $
  resolution = res,  $
  name = bname, $
  masterfile=masterfile
  
if not keyword_set(masterfile) then masterfile = 'STA_L1U'
cdfnames = ['B_HGRTN']
ppx = 'Bburst'
myformat = getenv('STA_L1U')+'/STA_L1_MAGB_*.cdf'

d=0
nodat = 0

loadallcdf,myformat,time_range=trange, $
    cdfnames=cdfnames,data=d,res =res

if keyword_set(d) eq 0 then begin
   message,'No STA MAGB data during this time.',/info
   nodat = 1
  return
endif


if data_type(prefix) eq 7 then px=prefix else px = 'sta_'
if data_type(bname) eq 7 then px = bname else px = px+ppx

labs=['B!dx!n','B!dy!n','B!dz!n']

time  = reform(d.time)
str_element,d,cdfnames(0),bhgrtn
bhgrtn = transpose(bhgrtn)

bmag=sqrt(total(bhgrtn*bhgrtn,2))
w =where(bmag gt 1000.,c)
if c ne 0 then bhgrtn[w,*] = !values.f_nan

store_data,px,data={x:time,y:bhgrtn},min= -1e30, dlim={labels:labs}

if keyword_set(polar) then begin
   xyz_to_polar,px,/ph_0_360

   options,px+'_mag','ytitle','|B|',/def
   options,px+'_th','ytitle','!19Q!X!DB!U',/def
   ylim,px+'_th',-90,90,0,/def
   options,px+'_phi','ytitle','!19F!X!DB!U',/def
   options,px+'_phi','psym',3,/def
   ylim,px+'_phi',0,360.,0,/def

endif


end