;+
;Procedure: THM_LOAD_ESA_PKT
;
;Purpose:  Loads THEMIS ESA data
;
;keywords:
;  probe = Probe name. The default is 'all', i.e., load all available probes.
;          This can be an array of strings, e.g., ['a', 'b'] or a
;          single string delimited by spaces, e.g., 'a b'
;  datatype = The type of data to be loaded, for this case, there is only
;          one option, the default value of 'mom', so this is a
;          placeholder should there be more that one data type. 'all'
;          can be passed in also, to get all variables.
;  TRANGE= (Optional) Time range of interest  (2 element array), if
;          this is not set, the default is to prompt the user. Note
;          that if the input time range is not a full day, a full
;          day's data is loaded
;  /GET_SUPPORT_DATA: load support_data variables as well as data variables
;                      into tplot variables.
;  /DOWNLOADONLY: download file but don't read it.
;  /valid_names, if set, then this routine will return the valid probe, datatype
;          and/or level options in named variables supplied as
;          arguments to the corresponding keywords.
;  files   named varible for output of pathnames of local files.
;  /VERBOSE  set to output some useful info
;
;Example:
;   thg_load_esa,/get_suppport_data,probe=['a', 'b']
;Notes:
; Written by Jim McFadden, 07-03-22
; jmm, replaced datatype(j) with datatype[j] in case statement, 11-jul-2007
; 
;-
pro thm_load_esa_pkt,probe=probe, datatype=datatype, trange=trange, $
		verbose=verbose, downloadonly=downloadonly, $
		get_support_data=get_support_data, $
		valid_names = valid_names, files=files, level=level

thm_init

if keyword_set(level) then print, 'THIS ROUTINE LOADS IN LEVEL 0 DATA ONLY! PROCEEDING...'

vb = keyword_set(verbose) ? verbose : 0
vb = vb > !themis.verbose
if vb ge 5 then printdat,!themis,/pgmtrace

vprobes = ['a','b','c','d','e']
; vlevels = ['l0']
vdatatypes=['454','455','456','457','458','459']

if keyword_set(valid_names) then begin
    probe = vprobes
    datatype = vdatatypes
    return
endif

if not keyword_set(probe) then probe=vprobes
probes=thm_check_valid_name(strtrim(strlowcase(probe),2),vprobes,/include_all)


if probes(0) eq '' then begin
  print,"Invalid probes selected.  Valid probes: 'a','b','c','d' or 'e'  (ie, probe='a')"
  return
end

if not keyword_set(datatype) then datatype=vdatatypes
datatype=thm_check_valid_name(strtrim(strlowcase(datatype),2),vdatatypes,/include_all)
if datatype(0) eq '' then begin
  print,"Invalid datatype selected.  Valid datatypes: '454','455','456','457','458' or '459' (ie, datatype='456')"
  return
end
ndt=n_elements(datatype)

for s=0,n_elements(probes)-1 do begin
	sc = probes[s]

	tt=timerange()
	t1=time_double(strmid(time_string(tt(0)),0,10))
	t2=time_double(strmid(time_string(tt(1)-1.),0,10))
	ndays=1+fix((t2-t1)/(24.*3600.))
	relpathnames=strarr(ndays*ndt) 
	i=0
	while t1 le t2 do begin
		ts=time_string(t1) 
		yr=strmid(ts,0,4) & mo=strmid(ts,5,2) & da=strmid(ts,8,2)
		dir='th'+sc+'/l0/'+yr+'/'+mo+'/'+da+'/' 
		relpathnames[i*ndt:(i+1)*ndt-1]=dir+'th'+sc+'_l0_'+datatype+'_'+yr+mo+da+'.pkt'
		i=i+1
		t1=t1+24.*3600.
	endwhile

     	files = file_retrieve(relpathnames, _extra=!themis)

	if not keyword_set(dwonloadonly) then begin
  	for j=0,ndt-1 do begin
		case datatype[j] of
				'454': thm_load_peif,sc=sc,themishome=!themis.local_data_dir
				'455': thm_load_peir,sc=sc,themishome=!themis.local_data_dir
				'456': thm_load_peib,sc=sc,themishome=!themis.local_data_dir
				'457': thm_load_peef,sc=sc,themishome=!themis.local_data_dir
				'458': thm_load_peer,sc=sc,themishome=!themis.local_data_dir
				'459': thm_load_peeb,sc=sc,themishome=!themis.local_data_dir
		endcase
	endfor
	endif

endfor

end