;+
;Procedure: process_modified_orbits
;                
;Purpose:
;  Determines if a state file was recently modified and reprocesses orbit plots for all modified state files
;
;  basedir: The directory in which to start searching for modified files.  This should be the parent directory of the probe directories
;  days: The number of days prior to the current date within which a file will be considered recent.
;  lunar=lunar: If this keyword is set, this routine will generate lunar(zoomed out) orbit plots in GSE not standard(zoomed-in) orbit plots
;  sse_lunar=sse_lunar: If this keyword is set, this routine will generate lunar orbit plots in SSE(zoomed in, but centered on the Moon) THB & THC only
;  t96_north=t96_north: If this keyword is set northward t96 magnetopause mapping will be run for the specified dates
;  t96_south=t96_south: If this keyword is set southward t96 magnetopause mapping will be run for the specified dates
; $LastChangedBy: jimm $
; $LastChangedDate: 2013-05-13 11:07:04 -0700 (Mon, 13 May 2013) $
; $LastChangedRevision: 12338 $
; $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/thmsoc/trunk/idl/thmsoc/asi/lunar_orbits/process_modified_orbit_dates.pro $
;-

pro process_modified_orbit_dates,basedir,days,lunar=lunar,sse_lunar=sse_lunar,t96_north=t96_north,t96_south=t96_south

  today = systime(/sec)
  probes = 'th'+['a','b','c','d','e']
  for i = 0,n_elements(probes)-1 do begin
    f = file_search(basedir+'/'+probes[i]+'/l1/state/20??/',probes[i]+'_l1_state_????????.cdf')
    if is_array(f) then fs = array_concat(f,fs)
  endfor

  if ~is_array(fs) then return
  
  mtime = (file_info(fs)).mtime
  idx = where(mtime ge today-days*60.*60.*24.,c)
  if c eq 0 then return
  tm_str = strmid(file_basename(fs[idx]),13,4) + '-' + strmid(file_basename(fs[idx]),17,2) + '-' + strmid(file_basename(fs[idx]),19,2)
 
  ;unique dates only to avoid unnessary reprocessing
  tm_str = tm_str[uniq(tm_str,bsort(tm_str))]
 
  for i = 0,n_elements(tm_str)-1 do begin
    if keyword_set(lunar) then begin
      moon_orbits_wrapper,tm_str[i]
    endif else if keyword_set(sse_lunar) then begin
      moon_orbits_wrapper_sse,tm_str[i]
    endif else if keyword_set(t96_north) then begin
      map_themis_state_t96_wrapper,tm_str[i]
    endif else if keyword_set(t96_south) then begin
      map_themis_state_t96_wrapper,tm_str[i],/south
    endif else begin
      orbits_wrapper,tm_str[i]
    endelse 
  endfor
  
end