

PRO eis_mapper_year, year, no_processing=no_processing, doy_start=doy_start, $
                     overwrite=overwrite, customize_text=customize_text, $
                     outdir=outdir, aia_wvl=aia_wvl

;+
; NAME:
;     EIS_MAPPER_YEAR
;
; PURPOSE:
;     Creates a set of EIS mapper files for a complete year.
;
; CATEGORY:
;     Hinode/EIS; mapper.
;
; CALLING SEQUENCE:
;     EIS_MAPPER_YEAR, Year
;
; INPUTS:
;     Year:   An integer specifying the year for which files will be
;             written. 
;
; OPTIONAL INPUTS:
;     Doy_Start: By default the routine starts at doy=1 (i.e., 1-Jan)
;                and works forward. This keyword can be used to start
;                a later date.
;     OutDir: Specify the directory where the html files should be written
;     Customize_Text:  Allows some customization of the output html
;                      file. The tags are:
;             .intro  A paragraph to insert above the calendar.
;             .footer Text to insert as a footer.
;     Aia_Wvl: Integer specifying which AIA wavelength to use. Default
;              is 193.
;
;	
; KEYWORD PARAMETERS:
;     NO_PROCESSING: If set, then the routine will not download full
;                    disk images for each day, i.e., the
;                    image_[DOY].png files will not be created. This
;                    is useful if you've previously created the
;                    files and just want to update the html files.
;     OVERWRITE:  If set, then all png and html files from a previous
;                 call will be deleted prior to creating new files.
;
; OUTPUTS:
;     For the specified year, creates a sub-directory
;     $EIS_MAPPER/html/[YEAR]. In this directory is written a html
;     file called mapper_[DOY].html for each day, where [DOY] is an
;     integer for the day-of-year, and an image file called
;     image_[DOY].png. Also created is an index.html file with a
;     calendar containing links to each of the individual
;     mapper_[DOY].html files.
;
; CALLS:
;     EIS_MAPPER_CALENDAR, EIS_MAPPER
; 
; RESTRICTIONS:
;     The $EIS_MAPPER environment variable must be defined.
;
; EXAMPLE:
;     IDL> eis_mapper_year, 2012
;     IDL> eis_mapper_year, 2012, /no_processing
;     IDL> eis_mapper_year, 2012, doy_start=100
;     IDL> eis_mapper_year, 2012, outdir='~/eis_mapper_atlas60'
;
; MODIFICATION HISTORY:
;     Ver.1, 10-Mar-2020, Peter Young
;     Ver.2, 16-Mar-2020, Peter Young
;        Added OUTDIR= optional input; no longer calls
;        eis_mapper_obs_struc.
;     Ver.3, 24-Mar-2020, Peter Young
;        Changed eis_aia_mapper to eis_mapper.
;     Ver.4, 09-Apr-2020, Peter Young
;        Added AIA_WVL= optional input.
;     Ver.5, 21-Jan-2021, Peter Young
;        Changed order of operations in order that /overwrite always
;        cleans the output html directory.
;     Ver.6, 13-Jun-2022, Peter Young
;        Now catches case when the obs structures do not match due to
;        an update to eis_obs_structure.
;-


eis_mapper_dir=getenv('EIS_MAPPER')

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


t=systime(/julian,/utc)
caldat,t,mm,dd,curr_year
IF n_elements(year) EQ 0 THEN BEGIN
  print,'% EIS_MAPPER_YEAR: The year was not specified - using current year...'
  year=curr_year
ENDIF 


;
; The html and png files for each day are written to
; $EIS_MAPPER/html/YEAR/.
;
; An index.html file is also written to this directory, giving the
; calendar that links through to the individual day files.
;
htmldir=concat_dir(outdir,'html')
htmlyeardir=concat_dir(htmldir,trim(year))
chck=file_info(htmlyeardir)
IF chck.directory EQ 0 AND NOT keyword_set(no_processing) THEN file_mkdir,htmlyeardir

IF keyword_set(overwrite) THEN BEGIN 
  chck=file_search(htmlyeardir,'*.png',count=count)
  IF count NE 0 THEN file_delete,chck
 ;
  chck=file_search(htmlyeardir,'*.html',count=count)
  IF count NE 0 THEN file_delete,chck
ENDIF 


;
; Retrieve the EIS observation structure files.
;
datadir=concat_dir(outdir,'data')
list=file_search(datadir,'obs_str_'+trim(year)+'*.save',count=n)
IF n EQ 0 THEN BEGIN
  print,'% EIS_MAPPER_YEAR: no observation structure save files were found. Please run eis_mapper_obs_str before'
  print,'                   calling this routine. Returning...'
  return
ENDIF

output=0
FOR i=0,n-1 DO BEGIN
  restore,list[i]
  IF n_tags(output) EQ 0 THEN BEGIN
    output=temporary(obs)
  ENDIF ELSE BEGIN
    IF n_tags(obs) NE n_tags(output) THEN BEGIN
      print,'*** ERROR: there has been a change in the number of tags in the obs structure. ***'
      print,'    Perhaps eis_obs_structure has been modified?'
      print,'    If yes, then you will need to regenerate the list of obs_str files for this year.'
      print,' Problem file:  ',list[i]
      return
    ENDIF 
    output=[output,temporary(obs)]
  ENDELSE 
ENDFOR 

d=anytim2utc(output.date_obs,/ccsds,/date)
days=d[uniq(d,sort(d))]




n=n_elements(days)
FOR i=0,n-1 DO BEGIN
  ii=where(d EQ days[i],nii)
  str=output[ii]
  day_str=anytim2utc(/vms,/date,str[0].date_obs)
  htmlfile='mapper_'+strpad(trim(doy(days[i])),3,fill='0')+'.html'
 ;
  IF i EQ 0 THEN BEGIN
    previous_link=''
  ENDIF ELSE BEGIN
    doy_previous=strpad(trim(doy(days[i-1])),3,fill='0')
    previous_link='mapper_'+doy_previous+'.html'
  ENDELSE
 ;
  IF i EQ n-1 THEN BEGIN
    next_link=''
  ENDIF ELSE BEGIN
    doy_next=strpad(trim(doy(days[i+1])),3,fill='0')
    next_link='mapper_'+doy_next+'.html'
  ENDELSE
 ;
  chck=file_info(concat_dir(htmlyeardir,htmlfile))
  IF chck.exists EQ 0 OR keyword_set(overwrite) THEN BEGIN
    eis_mapper,obs_struct=str,outdir=htmldir,htmlfile=htmlfile, $
               next_link=next_link,previous_link=previous_link, $
               yr_link='index.html',no_processing=no_processing, $
               aia_wvl=aia_wvl
  ENDIF 
ENDFOR 

;
; This generates the index.html file that contains the calendar. 
;
eis_mapper_calendar,year,outdir=outdir


END 
