;+
; Project     :	SOHO - CDS
;
; Name        :	WR_LTB
;
; Purpose     :	Writes part of LTB file connected with a given table type.
;
; Explanation : Writes out the list of indeces for this table type used in the science plan
;               which need to be loaded onto the CDS followed by those already loaded.
;
; Use         : <wr_ltb, unit, table_list, item, newitem>
;
; Inputs      : unit            = logical unit number for the LTB file;
;               table_list      = structure array giving information on tables;
;               item            = character string indicating the table type for loaded 
;                                 tables;
;               newitem         = character string indicating the table type for tables
;                                 which need to be loaded;
;
; Opt. Inputs : None.
;
; Outputs     : Writes LTB data for particular table type to file.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	rem_dup, rem_com.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	Version 0.00, Martin Carter, RAL, 14/10/94
;
; Modified    :	Version 0.01, Martin Carter, RAL, 28/11/94
;                            Added proforma.
;               Version 0.1, MKC, 8/1/96
;                            Corrected bug.
;               Version 0.2, MKC, 4/10/96
;                            Added some info printout.
;
; Version     :	Version 0.2, 4/10/96
;-
;**********************************************************

PRO wr_ltb, unit, table_list, item, newitem
   
  ; get list of items to be loaded

  ; get indeces of items to be loaded

  indeces_nok = WHERE ( table_list.ok EQ 0, count)   

  ; check if any items

  IF count GT 0 THEN BEGIN

    ; get list of items to be loaded

    list_nok = table_list(indeces_nok)

    ; get indeces of non-duplicate cdhsx's for items to be loaded

    indeces_nok = rem_dup(list_nok.cdhsx)

    ; get list of non-duplicate items to be loaded

    list_nok = list_nok(indeces_nok)

    count_nok = N_ELEMENTS(list_nok)

    PRINT, 'NO. OF NEW ' + item + ' TABLES USED = ' + STRTRIM(count_nok,1)

    ; write out items to be loaded
    ; avoiding line wrap 

    FOR n = 0, count_nok/8-1 DO BEGIN

      PRINTF, unit, '# IDs:   ', list_nok(n*8:n*8+7).cdhsid 
      PRINTF, unit, newitem,     list_nok(n*8:n*8+7).cdhsx 
      PRINTF, unit

    ENDFOR

    rem = count_nok MOD 8

    IF rem GT 0 THEN BEGIN

      st = (count_nok/8)*8
      PRINTF, unit, '# IDs:   ', list_nok(st:st+rem-1).cdhsid 
      PRINTF, unit, newitem,     list_nok(st:st+rem-1).cdhsx 
      PRINTF, unit

    ENDIF

  ENDIF

  ; get list of items used but not loaded

  ; get indeces of items  used

  indeces_ok = WHERE ( table_list.ok EQ 1, count)   

  ; check if any items

  IF count GT 0 THEN BEGIN

    ; get list of items used

    list_ok = table_list(indeces_ok)

    ; get indeces of non-duplicate cdhsx's for items used

    indeces_ok = rem_dup(list_ok.cdhsx)

    ; get indeces of items which are not also in list_nok

    IF indeces_nok(0) GE 0 THEN BEGIN

      ; get list of non-duplicate items used

      list_ok = list_ok(indeces_ok)

      indeces_ok = rem_com (list_nok.cdhsx, list_ok.cdhsx)

    ENDIF

    ; check still some items

    IF indeces_ok(0) GE 0 THEN BEGIN

      ; get list of items corresponding to this

      list_ok = list_ok ( indeces_ok )

      count_ok = N_ELEMENTS(list_ok)

      PRINT, 'NO. OF OLD ' + item + ' TABLES USED = ' + STRTRIM(count_ok,1)

      ; write out items used
      ; avoiding line wrap 

      FOR n = 0, count_ok/8-1 DO BEGIN

        PRINTF, unit, '# IDs:', list_ok(n*8:n*8+7).cdhsid 
        PRINTF, unit, item,     list_ok(n*8:n*8+7).cdhsx 
        PRINTF, unit

      ENDFOR

      rem = count_ok MOD 8

      IF rem GT 0 THEN BEGIN

        st = (count_ok/8)*8
        PRINTF, unit, '# IDs:', list_ok(st:st+rem-1).cdhsid 
        PRINTF, unit, item,     list_ok(st:st+rem-1).cdhsx 
        PRINTF, unit

      ENDIF

    ENDIF

  ENDIF

END

