
PRO eis_vso_download_files, date_obs, overwrite=overwrite

;+
; NAME:
;     EIS_VSO_DOWNLOAD_FILES
;
; PURPOSE:
;     This routine identifies the EIS level-0 files that are
;     identified with the specified observation dates. Typically this
;     routine will be used together with EIS_OBS_STRUCTURE.
;
; CATEGORY:
;     Hinode/EIS; file access.
;
; CALLING SEQUENCE:
;     EIS_VSO_DOWNLOAD_FILES, Date_Obs
;
; INPUTS:
;     Date_Obs: A list of observation times in any standard SSW
;               format. Can also be the structure that is output by
;               EIS_OBS_STRUCTURE. 
;
; KEYWORD PARAMETERS:
;     OVERWRITE: If set, then any files that already exist on the
;                user's computer will be overwritten.
;
; OUTPUTS:
;     Downloads EIS level-0 files corresponding to the input dates,
;     and ingests them into the user's Hinode directory
;     structure. If the level-0 file already exists on the
;     user's computer, then the new file is not downloaded.
;
; EXAMPLE:
;     Download all EIS files from the EIS raster YKK_EqCH_01.
;
;     IDL> s=eis_obs_structure(rast_acr='ykk_eqch_01')
;     IDL> eis_vso_download_files, s
;
; MODIFICATION HISTORY:
;     Ver.1, 06-Dec-2019, Peter Young
;     Ver.2, 08-Feb-2021, Peter Young
;       The routine now checks the temporary directory for EIS files
;       before downloading, just in case there was a problem with a
;       previous call to the routine.
;     Ver.3, 10-Feb-2021, Peter Young
;       Modified text output messages; now ingests each file
;       after it's been downloaded.
;     Ver.4, 13-May-2024, Peter Young
;       Added /overwrite keyword.
;-

;
; If date_obs is a structure then it is assumed to be the output from
; EIS_OBS_STRUCTURE, and the observation times will be in the tag
; DATE_OBS. 
;
IF n_tags(date_obs) NE 0 THEN BEGIN
  d_obs=date_obs.date_obs
ENDIF ELSE BEGIN
  d_obs=date_obs
ENDELSE 

l0dir=getenv('EIS_L0_DATA')
IF l0dir EQ '' THEN BEGIN
  hindir=getenv('HINODE_DATA')
  IF hindir NE '' THEN l0dir=concat_dir(hindir,'eis/level0')
ENDIF
IF l0dir EQ '' THEN BEGIN
  print,'% EIS_VSO_DOWNLOAD_FILES: please define $EIS_L0_DATA or $HINODE_DATA to point to a location where you store EIS data. '
  print,'                          Check EIS Software Note No. 18 for more information.'
  return
ENDIF
;
chck=file_info(l0dir)
IF chck.directory EQ 0 THEN BEGIN
  print,'% EIS_VSO_DOWNLOAD_FILES: The EIS level-0 directory was not found. Perhaps a data drive is not attached?'
  print,'                          Returning...'
  return
ENDIF 

outdir=getenv('IDL_TMPDIR')
chck=file_info(outdir)
IF chck.directory EQ 0 THEN file_mkdir,outdir
list=file_search(outdir,'eis_l0_*.*',count=count)
IF count NE 0 THEN BEGIN
   print,'% EIS_VSO_DOWNLOAD_FILES: found '+trim(count)+' EIS level-0 files in temp directory. Ingesting...'
   eis_ingest,list,force=overwrite
ENDIF 

n=n_elements(d_obs)

FOR i=0,n-1 DO BEGIN
   chck=eis_find_file(d_obs[i],count=count,twindow=60.,/quiet)
   IF count EQ 0 OR keyword_set(overwrite) THEN BEGIN

      t0_tai=anytim2tai(d_obs[i])
      t1_tai=t0_tai+60.

      t0=anytim2utc(/ccsds,t0_tai)
      t1=anytim2utc(/ccsds,t1_tai)

      list=vso_search(t0,t1,inst='eis',count=count,/quiet)

      IF count NE 0 THEN BEGIN 
         a=vso_get(list,out_dir=outdir,force=force,/quiet,filename=filename)
         IF filename[0] NE '' THEN BEGIN
            print,'% EIS_VSO_DOWNLOAD_FILES: File downloaded to: '+filename[0]
            eis_ingest,filename[0],force=overwrite
         ENDIF 
      ENDIF
   ENDIF ELSE BEGIN 
      print,'% EIS_VSO_DOWNLOAD_FILES: File '+file_basename(chck[0])+' already exists. Not downloaded.'
   ENDELSE 
ENDFOR


END
