;+
;Procedure: THM_LOAD_STATE
;
;Purpose:  Loads THEMIS STATE (orbit and attitude) 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 'pos vel'
;  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
;  version = the version of the state file, one of 'v00', 'v01', 'v02', 'v03'.
;            defaults to 'v01'
;  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.
;  suffix= suffix to add to tplot variable names.  Note: this will get added
;          support_data variables as well as regular data variables.
;  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.
;  /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.
;  /valid_names, if set, then this routine will return the valid probe, datatype
;          level, and version 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:
;   thm_load_state
;Notes:
;
; coordinate systems of returned variables:
; *_pos : gei
; *_vel : gei
; *_ras : gei
; *_dec : gei
; *_alpha : spg
; *_beta : spg
; *_spinper : none(listed in dlimits as unknown)
; *_spinphase : none(listed in dlimits as unknown)
; *_roi : none(listed in dlimits as unknown)
; *_man : none(listed in dlimits as unknown)
;
; If you modify the d_names constant make sure to make the
; corresponding changes to the c_names constant
;
; $LastChangedBy: kenb-mac $
; $LastChangedDate: 2007-08-02 12:51:41 -0700 (Thu, 02 Aug 2007) $
; $LastChangedRevision: 1322 $
; $URL $
;-

pro thm_load_state_post, sname=probe, datatype=dt, level=lvl, $
                       tplotnames=tplotnames, $
                       suffix=suffix, proc_type=proc_type, coord=coord, $
                       delete_support_data = delete_support_data, $
                         c_names=c_names, $
                         d_names=d_names, $
                       _extra=_extra

;set the coords properly

dt_temp = strsplit(dt,' ',/extract)

for i = 0,n_elements(dt_temp)-1L do begin
    var_name = 'th'+probe+'_state_'+dt_temp[i]
    idx = where(dt_temp[i] eq d_names)
    if(idx[0] eq -1L) then begin
        message,/info,'thm_load_state_post error setting coords, found unrecognized datatype: ' + dt_temp[i]
        coord_name = 'unknown'
    endif else coord_name = c_names[idx[0]]
   
    if(tnames(var_name) ne '') then begin
        get_data,var_name,data=d,limit=l,dlimit=dl
        cotrans_set_coord,dl,coord_name
        store_data,var_name,data=d,limit=l,dlimit=dl
    endif

endfor

end

pro thm_load_state,probe=probe, datatype=datatype, trange=trange, $
                   level=level, verbose=verbose, downloadonly=downloadonly,$
                   relpathnames_all=relpathnames_all, no_download=no_download,$
                   cdf_data=cdf_data,get_support_data=get_support_data, $
                   varnames=varnames, valid_names = valid_names, files=files, $
                   version=version, suffix=suffix, progobj=progobj

;defining constants

;probe names
  p_names = 'a b c d e'

;datatype names
  d_names = 'pos vel man roi ' + $
               'spinras spindec spinalpha spinbeta spinper spinphase' 

;coordinate names(should have same number of elements as d_names
;because they are in a 1-1 correspondence)
  c_names = 'gei gei unknown unknown gei gei spg spg unknown unknown'

  if arg_present(relpathnames_all) then begin
     downloadonly=1
     no_download=1
  end

  vversions='v00 v01 v02 v03'
  if not keyword_set(valid_names) then begin
     vers = thm_valid_input(version,'Version',vinputs=vversions, $
                            definput='v01', format="('v', I02)", $
                            verbose=verbose)
     if vers eq '' then return
  endif

;  probe_colors = ['m','b','c','g','r','y']
  
  ;; default datatypes to load are just pos and vel if get_support_data not set
  if not keyword_set(datatype) then begin
     if not keyword_set(get_support_data) then datatype = 'pos vel'
  endif
  
  thm_load_xxx,sname=probe, datatype=datatype, trange=trange, $
               level=level, verbose=verbose, downloadonly=downloadonly, $
               relpathnames_all=relpathnames_all, no_download=no_download, $
               cdf_data=cdf_data,get_cdf_data=arg_present(cdf_data), $
               get_support_data=get_support_data, $
               varnames=varnames, valid_names = valid_names, files=files, $
               vsnames = p_names, $
               type_sname = 'probe', $
               vdatatypes = d_names, $
               file_vdatatypes = 'state', $
               midfix = 'state_', $
               vlevels = 'l1', $
               deflevel = 'l1', $
               version=vers, $
               suffix=suffix, $
               post_process_proc = 'thm_load_state_post', $
               progobj = progobj, $
               c_names = strsplit(c_names,' ',/extract), $ ; tricky inheritance to get names to 
               d_names = strsplit(d_names,' ',/extract), $ ; the post process proc
               _extra = _extra

  if keyword_set(valid_names) then  begin
     version = vversions
     message, /info,  $
                string(strjoin(strsplit(vversions, ' ', /extract), ','), $
                       format = '( "Valid versions:",X,A,".")')
  endif

end