

PRO eis_mapper_calendar, year, outfile=outfile, customize_text=customize_text, $
                         outdir=outdir

;+
; NAME:
;      EIS_MAPPER_CALENDAR
;
; PURPOSE:
;      Creates a html file containing a calendar for the EIS_MAPPER
;      suite of routines.
;
; CATEGORY:
;      Hinode/EIS; quicklook.
;
; CALLING SEQUENCE:
;      EIS_MAPPER_CALENDAR, Year
;
; INPUTS:
;      Year:   A year for which the calendar is required (integer).
;
; OPTIONAL INPUTS:
;      OutDir: Specifies the directory where the html file is
;              written. If not set, then it is written to
;              $EIS_MAPPER/html/[YEAR] where [YEAR] is the input to
;              this routine.
;      OutFile: Specifies the full name of the html file to be
;               written. If not set, then the file
;               $EIS_MAPPER/html/[YEAR]/index.html is 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.
;
; OUTPUTS:
;      Creates a html file called [year].html in the directory
;      $EIS_MAPPER/html. For each day the routine
;      checks if there's a corresponding html file. If yes,
;      then a link is inserted for that day.
;
; OPTIONAL OUTPUTS:
;      Outfile: Allows the output filename to be directly specified. 
;
; CALLS:
;      EIS_MAPPER_MONTH_CALENDAR
;
; EXAMPLE:
;      IDL> eis_mapper_calendar, 2014
;
; MODIFICATION HISTORY:
;      Ver.1, 15-Aug-2018, Peter Young
;      Ver.2, 10-Mar-2020, Peter Young
;        Updated header; added check on $EIS_MAPPER; added
;        CUSTOMIZE_TEXT optional input.
;      Ver.3, 31-Mar-2020, Peter Young
;        Added check to make sure the year directory actually exists.
;      Ver.4, 06-Apr-2020, Peter Young
;        The month shown in the footer was not right.
;-


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_CALENDAR: the $EIS_MAPPER environment variable is not defined.'
    return
  ENDIF ELSE BEGIN
    outdir=eis_mapper_dir
  ENDELSE
ENDELSE 



IF n_elements(outfile) EQ 0 THEN BEGIN 
  htmldir=concat_dir(outdir,'html')
  htmlyeardir=concat_dir(htmldir,trim(year))
  chck=file_info(htmlyeardir)
  IF chck.exists EQ 0 THEN BEGIN
    print,'% EIS_MAPPER_CALENDAR: The directory '+htmlyeardir+' does not exist.'
    print,'                       The calendar file has not been written.'
    return
  ENDIF 
  outfile='index.html'
  outfile=concat_dir(htmlyeardir,outfile)
ENDIF


openw,lout,outfile,/get_lun

printf,lout,'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> '
printf,lout,'<html>'
printf,lout,'<head>'



printf,lout,'<html><head>'
printf,lout,'<title>EIS MAPPER CALENDAR FOR '+trim(year)+'</title>'
printf,lout,'</head><body style="background-color: rgb(255, 255, 255);" vlink="#cc33cc">'
printf,lout,'<center>'
printf,lout,'<table summary="" border="0" cellpadding="0" cellspacing="0" width="700">'
printf,lout,'  <tbody>'
printf,lout,'    <tr>'
printf,lout,'      <td height="30"><br>'
printf,lout,'      </td>'
printf,lout,'    </tr>'
printf,lout,'    <tr>'
printf,lout,'      <td align="left">'
printf,lout,'      <hr>'
printf,lout,'<h1>EIS Mapper Data for '+trim(year)+'</h1>'
printf,lout,'      <hr>'

IF n_tags(customize_text) NE 0 THEN BEGIN
  IF tag_exist(customize_text,'intro') THEN BEGIN
    printf,lout,'<p>'+customize_text.intro+'</p>'
  ENDIF
ENDIF 

chckdir=concat_dir(outdir,'html')
chckfile=concat_dir(chckdir,'index.html')
chck=file_info(chckfile)
IF chck.exists EQ 1 THEN BEGIN
  printf,lout,'<p><a href="../index.html">Index file for all years</a></p>'
ENDIF 

printf,lout,'<p><table>'
printf,lout,'<tr>'
FOR i=0,5 DO BEGIN
  printf,lout,'<td valign=top>'
  a=eis_mapper_month_calendar(i+1,year,outdir=outdir)
  n=n_elements(a)
  FOR j=0,n-1 DO printf,lout,a[j]
  printf,lout,'</td>'
ENDFOR
printf,lout,'</tr>'

printf,lout,'<tr>'
FOR i=0,5 DO BEGIN
  printf,lout,'<td valign=top>'
  a=eis_mapper_month_calendar(i+7,year,outdir=outdir)
  n=n_elements(a)
  FOR j=0,n-1 DO printf,lout,a[j]
  printf,lout,'</td>'
ENDFOR
printf,lout,'</tr></table>'

printf,lout,'      <hr>'

caldat,systime(/julian),a,b,c
date=trim(b)+' '+get_month(a-1)+' '+trim(c)
footer='Page created on '+date+'.'
IF n_tags(customize_text) NE 0 THEN BEGIN
  IF tag_exist(customize_text,'footer') THEN footer=customize_text.footer
ENDIF 
printf,lout,'<p><i>'+footer+'</i></p>'

printf,lout,'      </td>'
printf,lout,'    </tr>'
printf,lout,'  </tbody>'
printf,lout,'</table>'
printf,lout,'</center>'
printf,lout,'</body></html>'

free_lun,lout

print,'% EIS_MAPPER_CALENDAR: the file '+trim(outfile)+' has been written.'

END
