PRO seq_find_arch
;TEG on 07/23/02: fixed 'OR" typo error
;Changed week to -8 DCM 12/11/06

cd, '/var/www/htdocs/TRACE/health_mon/seq_weekly'
  ;Opens up the existing archive to be updated

big_file = loc_file('sequence_archive.txt', path=dir, count=count)
openu, lun, big_file, /get_lun

  ; sets the pointer to the last line of the file

pointer = 0
line = ' '
index = 1
while (NOT EOF(lun)) do begin
  readf, lun, line
  index = index+1
endwhile
point_lun, -lun, pointer
point_lun, lun, pointer
index = index - 2

  ;Establishes a time frame of one week, last day of week is three days before running of program
  ;sets up an array where each element in the array is a different day to be searched.
week = systime(0, /julian) - 10
;Temporary fix
;week = systime(0, /julian) - 8
week = round(week)

caldat, week, MM, DD, YY

YY = string(YY)
if (MM lt 10) then begin
  MM = string(MM)
  MM = '0'+MM
  MM = strcompress(MM, /remove_all)
endif
MM = string(MM)
if (DD lt 10) then begin
  DD = string(DD)
  DD = '0'+DD
  DD = strcompress(DD, /remove_all)
endif
DD = string(DD)

Week = YY+MM+DD
Week = strcompress(Week, /remove_all)

n = 0    ;counter to keep track of the total number of days that will be in the array 'date'

while (n le 6) do begin

  day =  systime(0, /julian) - 10 + n 

  caldat, day, mm, dd, yy

  yy = string(yy)
  if (mm lt 10) then begin
    mm = string(mm)
    mm = '0'+mm
    mm = strcompress(mm, /remove_all)
  endif
  mm = string(mm)
  if (dd lt 10) then begin
    dd = string(dd)
    dd = '0'+dd
    dd = strcompress(dd, /remove_all)
  endif
  dd = string(dd)

  date = yy+mm+dd
  date = strcompress(date, /remove_all)
  
  n = n + 1

  m = 0    ;counter to keep track of which day is bing looked at 

  ;Begins to read all the images and look for the sequence id number, the first day is chosen and then split up into an array
  ;of 24 elements, each element being one hourly, then an error statement to catch and missing data, then
  ;reads in the first hourly, and check for the correct seqid. finally prints out the date, start and end time of the flare flag
  ;and which sequence it was.

  files = strarr(25)
  ;print,date
  p = 0  ;counter to increment the hourly file that is put into the array files 

  while (p lt 10) do begin
    p = string(p)   
    files[p] = '/trace-archive/trace00/week'+Week+'//tri'+date+'.0'+p+'00'
    ;files[p] = '/hosts/cherokee/disk7/disk5/raw_fits/tri/week'+Week+'//tri'+date+'.0'+p+'00'
    files[p] = strcompress(files[p], /remove_all)
    p = p + 1
    p = fix(p)
  endwhile
  while (p lt 24) do begin
    p = string(p)  
    files[p] = '/trace-archive/trace00/week'+Week+'//tri'+date+'.'+p+'00'  
    ;files[p] = '/hosts/cherokee/disk7/disk5/raw_fits/tri/week'+Week+'//tri'+date+'.'+p+'00'
    files[p] = strcompress(files[p], /remove_all)
    p = p + 1
    p = fix(p) 
  endwhile

  j = 0    ;counter to keep track of what hourly of what day is beong looked at

  while (j lt 24) do begin

    print, files[j]

    CATCH, Error_status
    
    if (Error_status ne 0) then begin
      printf, lun, 'Data Missing For This Hour:'+files[j]
      j = j + 1
      if (j eq 24) then goto, newday
    endif

  
    read_trace,files[j],-1,index
    Result = SIZE(index, /N_ELEMENTS)

    counter = 0

    i=0    ;counter to keep track of which image is being looked at

    while(i le Result-1) do begin

      ;counter = 0
      
      if((index[i].seqid eq 1401) OR (index[i].seqid eq 5073) OR (index[i].seqid eq 5185) OR (index[i].seqid eq 5184) OR (index[i].seqid eq 4809) OR (index[i].seqid eq 4751) OR (index[i].seqid eq 4750) OR (index[i].seqid eq 4749) OR (index[i].seqid eq 4742) OR (index[i].seqid eq 5120) OR (index[i].seqid eq 4743) OR (index[i].seqid eq 5008) OR (index[i].seqid eq 5009) OR (index[i].seqid eq 4807) OR (index[i].seqid eq 4808) OR (index[i].seqid eq 5072) OR (index[i].seqid eq 4746) OR (index[i].seqid eq 4747) OR (index[i].seqid eq 5121) OR (index[i].seqid eq 5232)) then begin
        lastimage = i
        img_time = gtt_info(index, 'IMG_TIME', /str)
        seq_nam = id2nam(gtt_mnem(index, 'iksqid'), 'SEQ')
        counter = counter + 1
      endif 

      i=i+1

    endwhile  

      line = ' '

      if (counter ne 0) then begin
        firstimage = lastimage - counter
        seqnam2 = index[firstimage].seq_nam
        imgtime2 = index[firstimage].img_time
        seqID2 = index[firstimage].seqid
        date = index[firstimage].date_obs
        line = index[firstimage].date_obs+" | "+index[firstimage].img_time+" - "+index[lastimage].img_time+" | "+index[lastimage].seq_nam
        printf, lun, line
      endif    

    j = j + 1

    newday:

  endwhile

endwhile

free_lun, lun

end