;+
; Just a quick (1-time) program to reformat the RSTN and Penticton fluxes
; from the format they have on the NGDC on-line archive to the format used
; by the daily announcements of fluxes.  This goes from 01/01/1999 to
; 11/30/2000.  The daily flux files will handle newer data.  This routine
; should be kept around, since we may want to go back further into the past
; at some point.
;
; MODIFICATION HISTORY
;   Written 04-Jan-2001 by Dale E. Gary
;-
pro reformat_data

   monstr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
   jd = 2451180L  ; Julian day for 01/01/1999
   file = strarr(6)

   ; Just hardwire the file names--1999.  The order is the one used in the
   ; daily files, so is important.
   file[0] = 'c:\home\ovsa\lear99.txt'
   file[1] = 'c:\home\ovsa\svto99.txt'
   file[2] = 'c:\home\ovsa\sgmr99.txt'
   file[4] = 'c:\home\ovsa\pent1999.obs
   file[3] = 'c:\home\ovsa\pale99.txt'
   data = strarr(365)
   datall99 = strarr(365,4)

   ; Read all of the RSTN data at once into the array DATALL99
   for i = 0, 3 do begin
      openr,lun,/get_lun,file[i]
      readf,lun,data
      free_lun,lun
      datall99[*,i] = data
   endfor

   ; Just hardwire the file names--2000.  The order is the one used in the
   ; daily files, so is important.
   file[0] = 'c:\home\ovsa\lear2000.txt'
   file[1] = 'c:\home\ovsa\svto2000.txt'
   file[2] = 'c:\home\ovsa\sgmr2000.txt'
   file[5] = 'c:\home\ovsa\pent2000.obs
   file[3] = 'c:\home\ovsa\pale2000.txt'
   data = strarr(335)
   datall00 = strarr(335,4)

   ; Read all of the RSTN data at once into the array DATALL00
   for i = 0, 3 do begin
      openr,lun,/get_lun,file[i]
      readf,lun,data
      free_lun,lun
      datall00[*,i] = data
   endfor

   ; Now read all of the Penticton data
   data99 = strarr(37)
   data00 = strarr(37)
   hed = strarr(5)
   openr,lun,/get_lun,file[4]
   readf,lun,hed
   readf,lun,data99
   free_lun,lun
   ; Eliminate all-blank line separators
   goodlines = where(strmid(data99,0,5) ne '     ')
   data99 = data99[goodlines]
   openr,lun,/get_lun,file[5]
   readf,lun,hed
   readf,lun,data00
   free_lun,lun
   ; Eliminate all-blank line separators
   goodlines = where(strmid(data00,0,5) ne '     ')
   data00 = data00[goodlines]

   ; The list of frequencies
   nf = 9
   f = [245,410,610,1415,2695,2800,4995,8800,15400]
   idx = [0,1,2,3,4,6,7,8]
   outarr = strarr(8,nf)
   outarr[0,*] = string(f,format='(I5)')

   ; Open file for writing output to.
   openw,outlun,/get_lun,'c:\ephem\radioflux.1999'
   ; Loop over Julian day until 11/30/2000
   for j = 0, 699 do begin
      outarr[1:*,*] = ''
      caldat,jd+j,mo,da,yr  ; Determine calendar date that goes with this Julian day
      mon = monstr(mo-1)    ; Convert numerical month to string
      datstr = string(yr,mon,da,format='(I4,1X,A3,1X,I2)')
      title = ':Solar_Radio_Flux: '+datstr
      case yr of
         1999: BEGIN
                 for i = 0, 7 do begin
                    outarr[[1,2,3,6],idx[i]] = strmid(datall99[j,*],12+8*i,3)
                 endfor
                 outarr[5,5] = string(nint(strmid(data99[da-1],7+6*(mo-1),4)/10.),format='(I4)')
               END
         2000: BEGIN
                 for i = 0, 7 do begin
                    outarr[[1,2,3,6],idx[i]] = strmid(datall00[j-365,*],12+8*i,3)
                 endfor
                 outarr[5,5] = string(nint(strmid(data00[da-1],7+6*(mo-1),4)/10.),format='(I4)')
               END
      endcase
      missing = where(outarr eq '',nmissing)
      if (nmissing ne 0) then outarr[missing] = ' -1'
      missing = where(outarr eq '   ',nmissing)
      if (nmissing ne 0) then outarr[missing] = ' -1'
      printf,outlun,title
      printf,outlun,outarr,format='(A5,A7,6A11)'
      printf,outlun
   endfor
   free_lun,outlun
return
end