;+
;Procedure: THM_LOAD_EFI
;
;Purpose:  Loads THEMIS EFI 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, can be an array of strings
;          or single string separate by spaces.  The default is 'all'
;  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
;  level = the level of the data, the default is 'l1', or level-1
;          data. A string (e.g., 'l2') or an integer can be used. 'all'
;          can be passed in also, to get all levels.
;  CDF_DATA: named variable in which to return cdf data structure: only works
;          for a single spacecraft and datafile name.
;  VARNAMES: names of variables to load from cdf: default is all.
;  /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
;  /no_download: use only files which are online locally.
;  relpathnames_all: named variable in which to return all files that are
;          required for specified timespan, probe, datatype, and level.
;          If present, no files will be downloaded, and no data will be loaded.
;  type: 'calibrated' or 'raw'
;  coord: what coordinate system you would like your data in.
;Example:
;   thg_load_efi,/get_suppport_data,probe=['a', 'b']
;Notes:
;
; $LastChangedBy: kenb-mac $
; $LastChangedDate: 2007-07-20 13:28:09 -0700 (Fri, 20 Jul 2007) $
; $LastChangedRevision: 1195 $
; $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/thmsoc/tags/tdas_2_02/idl/themis/spacecraft/fields/thm_load_efi.pro $
;-

pro thm_load_efi,probe=probe, datatype=datatype, trange=trange, $
                 level=level, verbose=verbose, downloadonly=downloadonly, $
                 no_download=no_download,type=type, coord=coord,varformat=varformat,$
                 cdf_data=cdf_data,get_support_data=get_support_data, $
                 varnames=varnames, valid_names = valid_names, files=files, $
                 relpathnames_all = relpathnames_all,$
                 suffix=suffix, progobj=progobj

  thm_init
; If verbose keyword is defined, override !themis.verbose
  vb = size(verbose, /type) ne 0 ? verbose : !themis.verbose
  if not keyword_set(suffix) then suffix=''
  if arg_present(relpathnames_all) then begin
     downloadonly=1
     no_download=1
  end

  thm_load_proc_arg, sname=probe, datatype=datatype, $
                     level=level, verbose=verbose, no_download=no_download, $
                     valid_names = valid_names, $
                     vsnames = 'a b c d e', $
                     type_sname = 'probe', $
                     vdatatypes =    'vaf vap vaw vbf vbp vbw eff efp efw eff_0 efp_0 efw_0', $
                     file_vdatatypes='vaf vap vaw vbf vbp vbw eff efp efw eff   efp   efw', $
                     vL2datatypes= 'vaf vap vaw vbf vbp vbw eff efp efw eff_0 efp_0 efw_0 efs efs_0 efs_dot0', $
                     file_vL2datatypes= 'efi', $
                     vlevels = 'l1 l2', $
                     deflevel = 'l2', $
                     osname=probes, odt=dts, olvl=lvls, $
                     oft=fts, ofdt=fdts, $
                     my_themis=my_themis

  nlvls = n_elements(lvls)
  ndts = n_elements(dts)
  nfts = n_elements(fts)
  nprobes = n_elements(probes)

  if keyword_set(valid_names) then return

  if not keyword_set(type) and lvls[0] eq 'l1' then type='calibrated'


  if keyword_set(type) && lvls[0] eq 'l2' then begin
     print, 'type keyword only applies to l1 data'
     return
  endif

  if lvls[0] eq 'l1' then begin
     ;; default action for loading level 1 is to calibrate
     if ~keyword_set(type) || strmatch(type, 'calibrated') then begin
        ;; we're calibrating, so make sure we get support data
        if not keyword_set(get_support_data) then begin
           get_support_data = 1
           delete_support_data = 1
        endif
     endif
  endif

  if arg_present(cdf_data) && nlvls*ndts*nsnames gt 1 then begin
     print, 'can only get cdf_data for a single datatype'
     return
  endif

;get file names, loop over all snames, levels and datatypes
  for k = 0, nlvls-1 do $
     for j = 0, nfts-1 do $
        for i = 0, nprobes-1 do begin
     probei = probes[i]
     ftj = fts[j]
     lvlk = lvls[k]

     relpath = 'th'+probei+'/'+lvlk+'/'+ ftj+'/'
     prefix = 'th'+probei+'_'+lvlk+'_'+ftj+'_'
     dir = 'YYYY/'
     ending = '_v01.cdf'

     relpathnames = file_dailynames(relpath, prefix, ending, dir=dir, $
                                    trange = trange,addmaster=addmaster)

     if vb ge 7 then print, 'relpathnames : ', relpathnames

     if arg_present(relpathnames_all) then begin
        if i+j+k eq 0 then relpathnames_all = relpathnames else $
           relpathnames_all = [relpathnames_all, relpathnames]
     endif

     ;;download files for this probe, level, and datatype

     ;; my_themis is a copy of !themis, which may have no_download set
     files = file_retrieve(relpathnames, _extra=my_themis, progobj=progobj)

     if keyword_set(downloadonly) then begin
        continue
     endif

     if arg_present(cdf_data) then begin
        cdf_data = cdf_load_vars(files,varnames=varnames, $
                                 verbose=vb,/all)
        return
     endif

     if  ~keyword_set(varformat) then varformat='th?_??? th?_???_hed'


     if keyword_set(verbose) then print,transpose(['Loading...',files])
     if keyword_set(get_support_data) then begin
     cdf2tplot,file=files,all=all,verbose=vb, varformat=varformat,$
               tplotnames=tplotnames
     endif else cdf2tplot,file=files,all=all,verbose=vb,varformat=varformat,tplotnames=tplotnames
     ;if keyword_set(get_support_data) then begin

     ;if keyword_set(get_support_data)
     if not keyword_set(suffix) then suffix = ''
     ;; add '_raw' (or whatever) suffix to tplot var name
     ;; and add DLIMIT tags to data quantities
     for l=0, n_elements(tplotnames)-1 do begin
        tplot_var = tplotnames[l]
        get_data, tplot_var, data=d_str, limit=l_str, dlimit=dl_str
        if data_type(dl_str) eq 8 && dl_str.cdf.vatt.var_type eq 'data' $
        then begin
           tplot_var_raw = tplot_var+suffix
           data_att = { data_type:'raw', coord_sys:'efi_sensor',units:'ADC'}
           str_element, dl_str, 'data_att', data_att, /add


           if strmatch( strlowcase( tplot_var), 'th'+probei+'_v??') then begin
                 colors = [ 1, 2, 3, 4, 5, 6]
                 labels = [ 'V1', 'V2', 'V3', 'V4', 'V5', 'V6']
              endif
           if strmatch( strlowcase( tplot_var), 'th'+probei+'_e??') then begin
                 colors = [ 2, 4, 6]
                 labels = [ 'e12', 'e34', 'e56']
              endif

           str_element, dl_str, 'colors', colors, /add
           str_element, dl_str, 'labels', labels, /add
           str_element, dl_str, 'labflag', 1, /add
           str_element, dl_str, 'ytitle', $
                        string( tplot_var_raw, 'ADC', format='(A,"!C!C[",A,"]")'), /add


           store_data, delete=tplot_var
           store_data, tplot_var_raw, data=d_str, limit=l_str, dlimit=dl_str

           etype=strmid(tplot_var,4)

        endif


     endfor

  endfor                    ;end of loop over all probes, levels and datatypes.




  if keyword_set(type) && type eq 'calibrated' then begin
     thm_cal_efi, probe=probes, datatype=dts, coord=coord,in_suf=suffix, out_suf=suffix
  endif



  if vb ge 8 && arg_present(relpathnames_all) then $
     print, 'relpathnames_all: ', relpathnames_all
 if keyword_set(delete_support_data) then begin
if not size(dts, /n_dim) gt 0 then dt = strsplit(dt, ' ', /extract)

for i = 0, n_elements(dt)-1L do begin
if tnames('th'+probe+'_'+dts[i]+'_hed') ne '' then del_data, 'th'+probe+'_'+dts[i]+'_hed'
endfor
endif

end