
PRO eis_mapper_clean_recent, n_months=n_months, directory=directory

;+
; NAME:
;     EIS_MAPPER_CLEAN_RECENT
;
; PURPOSE:
;     Deletes the most recent months of EIS mapper data.
;
; CATEGORY:
;     EIS Mapper; file management.
;
; CALLING SEQUENCE:
;     EIS_MAPPER_CLEAN_RECENT
;
; INPUTS:
;     None.
;
; OPTIONAL INPUTS:
;     N_Months: Specifies the number of months to remove (beginning
;               with the current month). Default is 2.
;     Directory: Specifies the name of the directory containing the
;                EIS mapper data. If not specified, then set to
;                $EIS_MAPPER. 
;	
; OUTPUTS:
;     Deletes the obs structure data files in DIRECTORY/data for the
;     most recent two months, and deletes the image and html files in
;     DIRECTORY/html/YEAR for the two months.
;
; NOTES:
;     When running the EIS mapper software as a regular script, then
;     it will use the most recent EIS data file information. Sometimes
;     EIS files may initially fail to be generated (e.g., missing
;     packets) but are recovered a few weeks later. The purpose of the
;     current routine is delete the recent EIS mapper files so that
;     they can be regenerated with a new call to EIS_MAPPER_YEAR, thus
;     picking up the recovered EIS files.
;
; MODIFICATION HISTORY:
;     Ver.1, 08-Apr-2020, Peter Young
;     Ver.2, 28-Jan-2021, Peter Young
;       Fixed bug when cleaning January (the December data were not
;       being removed); added extra info message.
;-

t=systime(/jul)
caldat,t,curr_month,dd,curr_year

eis_mapper_dir=getenv('EIS_MAPPER')
;
IF n_elements(directory) NE 0 THEN BEGIN
  chck=file_search(directory,count=count)
  IF count EQ 0 THEN file_mkdir,directory
ENDIF ELSE BEGIN 
  IF eis_mapper_dir EQ '' THEN BEGIN
    print,'% EIS_MAPPER_CLEAN_RECENT: the $EIS_MAPPER environment variable is not defined.'
    return
  ENDIF ELSE BEGIN
    directory=eis_mapper_dir
  ENDELSE
ENDELSE 

print,'% EIS_MAPPER_CLEAN_RECENT: -- Processing directory '+directory+' --'


datadir=concat_dir(directory,'data')
htmldir=concat_dir(directory,'html')

IF n_elements(n_months) EQ 0 THEN n_months=2

FOR i=0,n_months-1 DO BEGIN
 ;
 ; First remove the obs structure save file for the specified month.
 ;
  dstr=trim(curr_year)+strpad(trim(curr_month),2,fill='0')
  datafile='obs_str_'+dstr+'.save'
  datafile=concat_dir(datadir,datafile)
  chck=file_info(datafile)
  IF chck.exists EQ 1 THEN BEGIN 
    file_delete,datafile
    print,'% EIS_MAPPER_CLEAN_RECENT: removed '+file_basename(datafile)+'.'
  ENDIF 
 ;
 ; Now have to remove the html and png files for each day of the month
 ;
  yeardir=concat_dir(htmldir,trim(curr_year))
  date0='1-'+get_month(curr_month-1,/trunc)+'-'+trim(curr_year)
  doy0=doy(date0)
  IF curr_month EQ 12 THEN BEGIN
     doy1=366
     date1='1-'+get_month(0,/trunc)+'-'+trim(curr_year+1)
  ENDIF ELSE BEGIN 
     date1='1-'+get_month(curr_month,/trunc)+'-'+trim(curr_year)
     doy1=doy(date1)
  ENDELSE
 ;
  FOR j=doy0,doy1-1 DO BEGIN
    pngfile=concat_dir(yeardir,'image_'+strpad(trim(j),3,fill='0')+'.png')
    chck=file_info(pngfile)
    IF chck.exists EQ 1 THEN BEGIN
      file_delete,pngfile
      print,'% EIS_MAPPER_CLEAN_RECENT: removed '+file_basename(pngfile)+'.'
    ENDIF 
   ;
    htmlfile=concat_dir(yeardir,'mapper_'+strpad(trim(j),3,fill='0')+'.html')
    chck=file_info(htmlfile)
    IF chck.exists EQ 1 THEN BEGIN
      file_delete,htmlfile
      print,'% EIS_MAPPER_CLEAN_RECENT: removed '+file_basename(htmlfile)+'.'
    ENDIF 
  ENDFOR
 ;
  IF curr_month EQ 1 THEN BEGIN
    curr_month=12
    curr_year=curr_year-1
  ENDIF ELSE BEGIN
    curr_month=curr_month-1
  ENDELSE 
ENDFOR 

END
