
PRO eis_ingest, filelist, force=force, gzip=gzip

;+
; NAME:
;     EIS_INGEST
;
; PURPOSE:
;     This 'ingests' a file or set of files into the EIS file
;     directory structure. That is, the files will be moved from one
;     directory to another. The move will not happen if the file(s)
;     already exist in the output directory.
;
;     Please see EIS Software Note 18 for information on how to
;     organize EIS data on your computer.
;
; CATEGORY:
;     Hinode/EIS; file handling.
;
; CALLING SEQUENCE:
;     EIS_INGEST, FileList
;
; INPUTS:
;     FileList: A string array giving the list of EIS files to be
;               ingested. If not specified, then routine will look for
;               all files of the form eis_*.fits in the current
;               working directory.
;
; KEYWORD PARAMETERS:
;     FORCE: If the file already exists in the EIS directory then by
;            default it will not be moved. Setting /FORCE forces the
;            existing file to be over-written by the new file.
;     GZIP:  If set, then the EIS file is compressed before it is
;            moved. If the file is already compressed, then gzip will
;            not be applied again.
;
; OUTPUTS:
;     The file/files specified by FILELIST will be moved into the EIS
;     data directory tree. 
;
; CALLS:
;     ANYTIM2DIR, ANYTIM2UTC
;
; EXAMPLE:
;     IDL> filelist=file_search('data','eis_*.fits')
;     IDL> eis_ingest,filelist,/gzip
;
; MODIFICATION HISTORY:
;     Ver.1, 8-Aug-2012, Peter Young
;     Ver.2, 20-Aug-2012, Peter Young
;        now checks if the compressed or uncompressed file exists in the
;        output directory before moving.
;     Ver.3, 28-Aug-2012, Peter Young
;        added compatibility with $EIS_FITS_DATA for users who have
;        set this variable; introduced /GZIP keyword.
;     Ver.4, 30-Jun-2015, Peter Young
;        fixed bug for /force option.
;     Ver.5, 24-Oct-2015, Peter Young
;        if the top-level output directory (e.g., the level-0 or
;        level-1 directory) does not exist, then the routine now
;        exists without an error.
;     Ver.6, 17-May-2020, Peter Young
;        modified behavior so that if FILELIST not specified then
;        routine will try to ingest files in the current working
;        directory; updated header.
;-



IF n_elements(filelist) EQ 0 THEN BEGIN
  print,'% EIS_INGEST: FILELIST was not specified. Searching for files in current working directory.'
  filelist=file_search('eis_*.fits',count=count)
  IF count GT 0 THEN BEGIN
    print,'% EIS_INGEST: '+trim(count)+' files found. Attempting to ingest...'
  ENDIF ELSE BEGIN
    print,'% EIS_INGEST: no files found. Returning...'
    return
  ENDELSE 
ENDIF 

eisdata=getenv('EIS_FITS_DATA')

hindata=getenv('HINODE_DATA')
IF eisdata EQ '' AND hindata EQ '' THEN BEGIN
  print,'% EIS_INGEST:  please define the environment variable $HINODE_DATA to point to the location'
  print,'              of your Hinode data. Returning...'
  return
ENDIF 

IF hindata NE '' THEN eisdata=concat_dir(hindata,'eis')

l0dir=getenv('EIS_L0_DATA')
l1dir=getenv('EIS_L1_DATA')

IF l0dir EQ '' THEN l0dir=concat_dir(hindata,'eis/level0')
IF l1dir EQ '' THEN l1dir=concat_dir(hindata,'eis/level1')

;
; 'loc' is the location where the corresponding filetype will go.
;
chckstr=['_l0_','_er_','_l1_']
loc=[l0dir,l1dir,l1dir]
nn=n_elements(chckstr)

FOR ii=0,nn-1 DO BEGIN 
  chck=strpos(filelist,chckstr[ii])
  k=where(chck GE 0,nk)

  IF nk GT 0 THEN BEGIN 
   ;
    dirinfo=file_info(loc[ii])
    IF dirinfo.directory EQ 0 THEN BEGIN
      print,'% EIS_INGEST: the output directory '+loc[ii]+' was not found. The files were not ingested.'
    ENDIF ELSE BEGIN 
      FOR i=0,nk-1 DO BEGIN 
        swtch=0b
        file=filelist[k[i]]
        filebase_any=file_basename(file)
        filebase=file_basename(file,'.gz')
        filebase_gz=filebase+'.gz'
        filedir=file_dirname(file)
     ;
        bits=str_sep(filebase,'_')
        date=bits[2]
        IF strlen(date) NE 8 THEN BEGIN
          print,'% EIS_INGEST: the filename does not have the correct EIS format. Please check'
          print,'       -- ',file
          return
        ENDIF 
        yr=strmid(date,0,4)
        mo=strmid(date,4,2)
        dd=strmid(date,6,2)
        outdir=concat_dir(loc[ii],yr)
        outdir=concat_dir(outdir,mo)
        outdir=concat_dir(outdir,dd)
     ;
     ; If /gzip is set and the file is not already compressed, then proceed
     ; to create compressed file.
     ;
        IF keyword_set(gzip) AND filebase_any NE filebase_gz THEN BEGIN
          print,'% EIS_INGEST: compressing file '+filebase
          spawn,'gzip -f '+file
          chckfile=concat_dir(filedir,filebase_gz)
          chck=file_search(chckfile,count=count)
          IF count EQ 0 THEN BEGIN
            print,'% EIS_INGEST: a problem occurred during compression. Gzipped file not created.'
            swtch=1b
          ENDIF ELSE BEGIN
            file=chck[0]    ; re-define file
          ENDELSE 
        ENDIF 
     ;
        chck=file_info(outdir)
        IF chck.directory EQ 1 THEN BEGIN
          chck1=file_search(outdir,filebase)
          chck2=file_search(outdir,filebase_gz)
          IF (chck1  NE '' OR chck2 NE '') AND NOT keyword_set(force) THEN BEGIN
            print,file
            print,'  - file already exists in EIS tree; not moved'
          ENDIF ELSE BEGIN 
            IF swtch EQ 0 THEN file_move,file,outdir,/overwrite
          ENDELSE
        ENDIF ELSE BEGIN
          file_mkdir,outdir
          print,'% EIS_INGEST:  created new directory '+outdir
          IF swtch EQ 0 THEN file_move,file,outdir
        ENDELSE 
      ENDFOR
    ENDELSE 
  ENDIF
ENDFOR 

END
