;---------------------------------------------------------------------------
; Document name: soho_items_html.pro
; Created by:    Liyun Wang, NASA/GSFC, August 20, 1997
;
; Last Modified: Thu Aug 21 17:54:35 1997 (lwang@achilles.nascom.nasa.gov)
;---------------------------------------------------------------------------
;
;+
; PROJECT:
;       SOHO - CDS/SUMER
;
; NAME:
;       SOHO_ITEMS_HTML
;
; PURPOSE:
;       List SOHO related plans in HTML format
;
; CATEGORY:
;       Utility
;
; SYNTAX:
;       soho_items_html, unit, title, entries
;
; INPUTS:
;       UNIT  - I/O unit number (must have opened)
;       TITLE - Title of items (instrument name, etc.)
;       ENTRIES - Structure array for entries of items
;
; OPTIONAL INPUTS:
;       None.
;
; OUTPUTS:
;       Partial HTML document written to UNIT
;
; OPTIONAL OUTPUTS:
;       None.
;
; KEYWORDS:
;       NEW - A name variable which must be set to 1 when called for
;             the 1st time; it's set to 0 afterwards
;
; COMMON:
;       None.
;
; RESTRICTIONS:
;       None.
;
; SIDE EFFECTS:
;       Keyword variable NEW is set to 0 after the first call
;
; HISTORY:
;       Version 1, August 20, 1997, Liyun Wang, NASA/GSFC. Written
;
; CONTACT:
;       Liyun Wang, NASA/GSFC (Liyun.Wang.1@gsfc.nasa.gov)
;-
;
PRO html_table_header, unit, break=break, nonsci=nonsci, detail=detail, $
        new=new, finish=finish
;---------------------------------------------------------------------------
;  BREAK  - Set this keyword to break table
;  NONSCI - Set this keyword to make non-science plan related table headers
;  DETAIL - Set this keyword to make table headers for detailed plans
;  NEW    - Set this keyword to begin <table>
;  FINISH - Set this keyword to end </table>
;---------------------------------------------------------------------------
   IF N_ELEMENTS(break) EQ 0 THEN break = 0
   
   IF KEYWORD_SET(finish) THEN BEGIN
      PRINTF, unit, '</table></center>'
      RETURN
   ENDIF

   IF KEYWORD_SET(new) THEN BEGIN
      get_utc, curr_time, /ecs, /trunc
      PRINTF, unit, '<center><table border=3>'
      PRINTF, unit, '<caption align=top><b>(Updated: '+curr_time+$
         ' UT)</caption></b>'
   ENDIF
   
   IF break EQ 1 THEN BEGIN
      PRINTF, unit, '</table></center><br>'
      PRINTF, unit, '<center><table border=3>'
   ENDIF 
   
   IF NOT KEYWORD_SET(new) AND break EQ 0 THEN RETURN
   
   IF KEYWORD_SET(nonsci) THEN BEGIN
      PRINTF, unit, '<tr>'
      PRINTF, unit, '<th rowspan=2>Item</th>'
      PRINTF, unit, '<th colspan=2>Obs. Time (UT)</th>'
      PRINTF, unit, '<th rowspan=2>Duration</th>'
      PRINTF, unit, '</tr><tr>'
      list = ['Start', 'End']
      PRINTF, unit, '<th>'+arr2str(list, delimit='</th><th>')+'</th></tr>'
   ENDIF ELSE BEGIN
      cmp_str = "<a href='http://sohodb.nascom.nasa.gov/cgi-bin/soho_campaign_search/'>CMP<br>NO</a>"
      PRINTF, unit, '<tr>'
      PRINTF, unit, '<th rowspan=2>'+$
         arr2str(['Instrument', 'Program', cmp_str], $
                 delimit='</th><th rowspan=2>')+'</th>'
      PRINTF, unit, '<th colspan=2>Obs. Time (UT)</th>'
      PRINTF, unit, '<th rowspan=2>'+$
         arr2str(['Duration', 'Object'], $
                 delimit='</th><th rowspan=2>')+'</th>'
      PRINTF, unit, '<th colspan=2>Coordinates (arcsecs)</th>'
      IF KEYWORD_SET(detail) THEN BEGIN
         PRINTF, unit, '<th colspan=2>Field of View (arcsecs)</th>'
         list = ['Start', 'End', 'X', 'Y', 'Width', 'Height']
      ENDIF ELSE list = ['Start', 'End', 'X', 'Y']
      PRINTF, unit, '</tr><tr>'
      PRINTF, unit, '<th>'+arr2str(list, delimit='</th><th>')+'</th></tr>'
   ENDELSE
END

PRO soho_items_html, unit, title, entries, new=new
;---------------------------------------------------------------------------
; INPUTS:
;     UNIT    - Output unit number to which the file is written
;     TITLE   - Title of the entries
;     ENTRIES - Array of plan entry structure;
;     Set NEW keyword to make first table; after that NEW is set to 0
;---------------------------------------------------------------------------
   COMMON plan_html0, first_done, prev_nonsci, prev_detail
   IF N_ELEMENTS(first_done) EQ 0 THEN first_done = 0
   IF N_ELEMENTS(prev_nonsci) EQ 0 THEN prev_nonsci = 0
   IF N_ELEMENTS(prev_detail) EQ 0 THEN prev_detail = 0
   n = N_ELEMENTS(entries)
   a = tag_exist(entries(0), 'SCI_OBJ')
   nonsci = (a EQ 0)
   isdetail = tag_exist(entries(0), 'IXWIDTH')
   break = 0
   IF nonsci NE prev_nonsci OR isdetail NE prev_detail THEN break = 1
   prev_nonsci = nonsci
   prev_detail = isdetail
   IF N_ELEMENTS(new) EQ 0 THEN new = 0 
   IF new EQ 1 THEN BEGIN
      html_table_header, unit, nonsci=nonsci, detail=isdetail, /new
      new = 0
   ENDIF ELSE BEGIN
      IF n GT 10 AND first_done EQ 1 THEN break = 1
      html_table_header, unit, break=break, nonsci=nonsci, detail=isdetail
   ENDELSE

   CASE (title) OF
      'CDS': title = '<b><a href=http://orpheus.nascom.nasa.gov/cds/cds_homepage.html>CDS</a></b>'
      'EIT': title = '<b><a href=http://umbra.nascom.nasa.gov/eit/>EIT</a></b>'
      'JOP': title = '<b><a href=http://sohowww.nascom.nasa.gov/operations/JOPs/>JOP</a></b>'
      'CAMPAIGN': title = '<b><a href=http://sohowww.nascom.nasa.gov/operations/campaigns/>Campaigns</a></b>'
      'UVCS': title = '<b><a href=http://cfa-www.harvard.edu/uvcs/>UVCS</a></b>'
      'SUMER': title = '<b><a href=http://hydra.mpae.gwdg.de/mpae_projects/SUMER/sumer.html>SUMER</a></b>'
      'LASCO': title = '<b><a href=http://lasco-www.nrl.navy.mil/lasco.html>LASCO</a></b>'
      ELSE:
   ENDCASE

;---------------------------------------------------------------------------
;  Figure out how many extra lines are needed if multi-pointing
;  entries exist
;---------------------------------------------------------------------------
   np = 0
   IF NOT nonsci AND tag_exist(entries(0), 'XCEN') THEN BEGIN
      xx = entries.xcen
      ii = WHERE(STRPOS(xx, ',') NE -1)
      IF ii(0) GE 0 THEN BEGIN
         xx_1 = xx(ii)
         nl = N_ELEMENTS(xx_1)
         FOR i=0, nl-1 DO BEGIN
            np = np+N_ELEMENTS(str_index(xx_1(i), ','))+1
         ENDFOR
         np = np-nl
      ENDIF
   ENDIF

   PRINTF, unit, '<tr><td align=center rowspan='+STRTRIM(STRING(n+np+1), 2)+$
      '>'+title+'</td></tr>'
   FOR i=0, n-1 DO BEGIN
      PRINTF, unit, '<tr align=center>'
      entry = entries(i)
      itime = get_plan_itime(entry, err=err)
      dur = sec2dhms(LONG(entry.(itime+1)-entry.(itime)) > 0)
      tstart = STRMID(anytim2utc(entry.(itime), /ecs, /trun), 2, 20)
      tstop = STRMID(anytim2utc(entry.(itime+1), /ecs, /trun), 2, 20)
      npoint = 1
      list = [tstart, tstop, dur]
      IF NOT nonsci THEN BEGIN
         prog = '&nbsp;'
         object = '&nbsp;'
         IF tag_exist(entry, 'CMP_NO') THEN BEGIN
            cmp_no = STRTRIM(STRING(entry.cmp_no), 2)
            IF cmp_no NE '0' THEN $
               cmp_no="<a href='http://sohodb.nascom.nasa.gov/cgi-bin/soho_campaign_display/"+cmp_no+"'>"+cmp_no+"</a>"
         ENDIF ELSE cmp_no = ''
         IF tag_exist(entry, 'SCI_OBJ') THEN BEGIN
            prog = STRTRIM(STRMID(entry.sci_obj, 0, 40), 2)
            err = ''
            get_object, entry.object, obj_stc, err=err
            IF err EQ '' THEN BEGIN
               object = STRTRIM(obj_stc.object_desc, 2)
               IF object EQ '' THEN object = '&nbsp;'
            ENDIF ELSE object = '&nbsp;'
            list = [prog, cmp_no, list, object]
            IF (STRPOS(entry.xcen, ','))(0) NE -1 THEN BEGIN
;---------------------------------------------------------------------------
;           The current entry has more than one pointing values
;---------------------------------------------------------------------------
               xcen = STRTRIM(str2arr(entry.xcen, ','), 2)
               ycen = STRTRIM(str2arr(entry.ycen, ','), 2)
               npoint = N_ELEMENTS(xcen)
               list_pt = [[xcen], [ycen]]
            ENDIF ELSE BEGIN
               IF (STRPOS(entry.xcen, '.'))(0) NE -1 THEN BEGIN
                  xcen = STRTRIM(STRING(entry.xcen, format='(f15.1)'), 2)
                  ycen = STRTRIM(STRING(entry.ycen, format='(f15.1)'), 2)
               ENDIF ELSE BEGIN
                  xcen = STRTRIM(entry.xcen, 2)
                  ycen = STRTRIM(entry.ycen, 2)
                  IF xcen EQ '' THEN xcen = '&nbsp;'
                  IF ycen EQ '' THEN ycen = '&nbsp;'
               ENDELSE
               list = [list, xcen, ycen]
            ENDELSE
         ENDIF
         IF tag_exist(entry, 'IXWIDTH') THEN BEGIN
            IF (STRPOS(entry.ixwidth, ','))(0) NE -1 THEN BEGIN
               width = STRTRIM(str2arr(entry.ixwidth, ','), 2)
               height = STRTRIM(str2arr(entry.iywidth, ','), 2)
               tmp = N_ELEMENTS(width)
               IF tmp LT npoint THEN npoint = tmp
               width = width(0:npoint-1)
               height = height(0:npoint-1)
               list_pt = [[list_pt(0:npoint-1, *)], [width], [height]]
            ENDIF ELSE BEGIN
               IF (STRPOS(entry.ixwidth, '.'))(0) NE -1 THEN BEGIN
                  width = STRTRIM(STRING(entry.ixwidth, format='(f15.1)'), 2)
                  height = STRTRIM(STRING(entry.iywidth, format='(f15.1)'), 2)
               ENDIF ELSE BEGIN
                  width = STRTRIM(entry.ixwidth, 2)
                  height = STRTRIM(entry.iywidth, 2)
                  IF width EQ '' THEN width = '&nbsp;'
                  IF height EQ '' THEN height = '&nbsp;'
               ENDELSE
               list = [list, width, height]
            ENDELSE
         ENDIF
      ENDIF
      IF npoint GT 1 THEN BEGIN
;---------------------------------------------------------------------------
;        Deal with multi-pointing entries
;---------------------------------------------------------------------------
         sr = STRTRIM(STRING(npoint), 2)
         PRINTF, unit, '<td rowspan='+sr+'>'+$
            arr2str(list, delimit='</td><td rowspan='+sr+'>')+'</td>'
         PRINTF, unit,'<td>'+arr2str(TRANSPOSE(list_pt(0,*)), $
                                     delimit='</td><td>')+'</td></tr>'
         FOR j=1, npoint-1 DO BEGIN
            PRINTF, unit, '<tr align=center>'
            PRINTF, unit,'<td>'+arr2str(TRANSPOSE(list_pt(j,*)), $
                                        delimit='</td><td>')+'</td>'
         ENDFOR
      ENDIF ELSE $
         PRINTF, unit, '<td>'+arr2str(list, delimit='</td><td>')+'</td>'

      PRINTF, unit, '</tr>'
      first_done = 1
   ENDFOR
END

;---------------------------------------------------------------------------
; End of 'soho_items_html.pro'.
;---------------------------------------------------------------------------
