;+
; Project     : SOHO - CDS     
;                   
; Name        : TM_NEW_DB
;               
; Purpose     : Produce new format of telemetry database.
;               
; Explanation : Reads original telemetry database file and reformats for
;               use by humans and procedure EMON.  The output file is best
;               printed using "cds2ps -w -F7 new_database.txt"
;               
; Use         : IDL> tm_new_db
;    
; Inputs      : None
;               
; Opt. Inputs : None
;               
; Outputs     : New file new_database.txt created.
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: Input and output files must be in current directory.
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 22-Dec-94
;               
; Modified    : Make work like documentation says! CDP, 25-Jan-95
;               Cut off last c/r character appearing on lines of input file.
;                                                  CDP, 24-Aug-95
;
; Version     : Version 3, 24-Aug-95
;-            

pro tm_new_db

;
;  open input and output files
;
openr,lin,'tmdb.txt',/get_lun
openw,lout,'new_database.txt',/get_lun,width=160
title='Parameter  B T   O S  Mask  Sub Cal      Op1       Op2       Op3   '+$
            '    Op4       Op5       Op6       Op7       Op8'+$
            '                    Comment'
under = '-'
for i=0,148 do under = under + '-'
text = ' '
k=0

while not eof(lin) do begin
   readf,lin,text
   if strmid(text,0,1) ne '!' then begin
      text = strtrim(text,2)
      text = strmid(text,0,strlen(text)-1)
      x = str_sep(text,'	')
      n = where(x ne '' and x ne ' ')
      if n(0) ge 0 then x = x(n)
      if (n_elements(x) gt 6) and $
          (strmid(x(0),0,1) eq 'A' or $
          strmid(x(0),0,1) eq 'B')  then begin
         x(0) = strpad(x(0),10,/after)
         x(1) = strpad(x(1),40,/after)
         if x(6) eq '0X' then x(6) = '0XFFFF'
         if strlen(x(6)) eq 4 then x(6)  = strep(x(6),'0X','0X00')
         if strlen(x(6)) eq 5 then x(6)  = strep(x(6),'0X','0X0')
         otext = string(x(0),x(2:*),$
           format='(a10,1x,a1,1x,a1,1x,a3,1x,a1,1x,a6,3(1x,a1),2x,8(a9,1x))')
         otext = strpad(otext,115,/after)
         if (k mod 66) eq 0 then begin
            printf,lout,under
            printf,lout,title
            printf,lout,under
            k=0
         endif
         k = k+1
         printf,lout,otext,' ',x(1)
      endif
   endif
endwhile

free_lun,lin
free_lun,lout

end

