
pro ct_throughput_data_ops, t0, t1, index_ct=index_ct, level0=level0, $
  max_filesperdir=max_filesperdir, nfiles_max=nfiles_max, $
  do_prep=do_prep, no_update=no_update, do_save=do_save, quiet=quiet, $
  no_zbuff=no_zbuff, _extra=_extra

if not exist(interval1) then interval1 = 1	; Days
if not exist(interval2) then interval2 = 60
if not exist(level) then level0 = 1
;if not exist(max_filesperdir) then max_filesperdir = 100
if not exist(max_filesperdir) then max_filesperdir = 10
if not exist(nfiles_max) then nfiles_max = 10

 set_logenv, 'HINODE_OPS', '/archive1/hinode'
 set_logenv, 'SOT_OPS',    '/archive1/hinode/sot'

;if get_logenv('DIR_HINODE_INSTR_DB') eq '' then $
;set_logenv, 'DIR_HINODE_INSTR_DB',        '/sanhome/slater/public_html/missions/hinode/sot/instr_db'
 set_logenv, 'DIR_HINODE_INSTR_DB',        '$HOME/db/missions/hinode/sot/instrument_databases'

;if get_logenv('DIR_HINODE_INSTR_DB_BACKUP') eq '' then $
;set_logenv, 'DIR_HINODE_INSTR_DB_BACKUP', '/sanhome/slater/public_html/missions/hinode/sot/instr_db_backup'
 set_logenv, 'DIR_HINODE_INSTR_DB_BACKUP', '$HOME/db/missions/hinode/sot/instrument_databases_backups'

dir_hinode_instr_db = get_logenv('DIR_HINODE_INSTR_DB')
;if not file_exist(dir_hinode_instr_db) then begin
;  print, 'Aborting due to missing Hinode database directory.  Returning with error condition.'
;  return
;endif
dir_hinode_instr_db_backup = get_logenv('DIR_HINODE_INSTR_DB_BACKUP')
;if not file_exist(dir_hinode_instr_db_backup) then begin
;  print, 'Aborting due to missing Hinode database backup directory.  Returning with error condition.'
;  return
;endif

if not exist(filnam_ct_response) then filnam_ct_response = 'ct_response_mission.sav'
dbnam_ct_response = '/sanhome/slater/public_html/missions/hinode/sot/instr_db/ct_throughput.sav'
; concat_dir(dir_hinode_instr_db, filnam_ct_response)
if not file_exist(dbnam_ct_response) then begin
  print, 'Aborting due to missing CT response stats database.  Returning with error condition.'
  return
endif

delvarx, index_ct
; concat_dir(dir_hinode_instr_db, 'index_ct_template.sav')
restore, '/archive1/hinode/sot/ct_throughput/index_ct_template.sav'
index_ct_template = add_tag(index_ct_template, fltarr(4), 'ct_mom')
index_ct_template = add_tag(index_ct_template, 0., 'ct_median')

if level0 eq 1 then set_logenv,'SOT_DATA','/archive1/hinode/sot/level0' else $
  set_logenv,'SOT_DATA','/archive1/hinode/sot/QuickLook'

restore, dbnam_ct_response
if not exist(index_ct) then index_ct = index_arr



if not keyword_set(no_update) then begin



if not exist(t0) then begin
  if exist(index_ct) then begin
    t_arr = index_ct.date_obs
    t0 = anytim(t_arr(n_elements(t_arr)-1),/yoh)
  endif else begin
    t0 = '16-oct-2006'
  endelse
endif

if not exist(t1) then t1 = anytim(!stime,/yoh)

t0_sec = anytim(t0)
t1_sec = anytim(t1)
if t1_sec gt t0_sec then prstr,[t0,t1]

t_grid = anytim(timegrid(t0,t1,days=interval1),/yoh)
np_grid = n_elements(t_grid)

for i=0, np_grid-2 do begin
  delvarx, files_ct
  nfiles = 0
  proc_start_time = !stime
; NOTE: Could also use "obs='cr'" in the following:
  files_ct = sot_time2files(t_grid(i), t_grid(i+1) ,/cr, $
    max_filesperdir=max_filesperdir, quiet=quiet)
  if files_ct(0) ne '' then begin
    nfiles = n_elements(files_ct) < nfiles_max
    mreadfits, files_ct(0:nfiles-1), index_ct0, data_ct0, strtemplate=index_ct_template
;STOP
;    index_ct0 = rep_tag_value(index_ct0,double(index_ct0.xcen),'xcen')
    helio_ct = ssw_index2carr(index_ct0, /add)

    for j=0,nfiles-1 do begin
      index_ct0(j).ct_mom = moment(data_ct0(*,*,j))
      index_ct0(j).ct_median = median(data_ct0(*,*,j))
    endfor

    if not exist(index_ct) then begin
      index_ct = index_ct0
    endif else begin
      index_ct = [index_ct,index_ct0]
    endelse
  endif else begin
    nfiles = 0
  endelse

  if nfiles ne 0 then begin
    proc_end_time = !stime
    proc_time = addtime(proc_end_time, diff=proc_start_time, /sec)
    print, 'Finished for ' + strmid(t_grid(i),0,9) + '. ' + $
	   strtrim(proc_time,2) + ' sec total. ' + $
	   string(nfiles,format='$(i5.5)') + ' files. ' + $
	   strtrim(float(proc_time)/nfiles,2) + ' sec per file.'
  endif

endfor


endif


if ~keyword_set(no_zbuff) then set_plot,'z'
set_plot, 'z'
wdef,0,1280,768

plot_time_stamp = 'Plot Created: ' + !stime

n_pts = n_elements(index_ct)
t_range = [index_ct[0].date_obs, index_ct[n_pts-1].date_obs]
utplot, index_ct.date_obs, index_ct.ct_mom[0]/1250, psym=3, xstyle=1, timerange=t_range, $
  yrange=[0.60,1.1], ystyle=1, ytit='Normalized Mean Value', $
  tit='SOT CT Normalized Response History', /year, charsize=1.5
plot_time_stamp = 'Plot Created: ' + strmid(anytim(!stime, /ccsds),0,16)
xyouts, 0.70, 0.01, plot_time_stamp, /norm, charsize=1.2; , color=0
;xyouts, 0.70, 0.7, plot_time_stamp, /norm, color=0
buff = 255b-tvrd()
dir_ct_response = concat_dir(get_logenv('SOT_OPS'), 'ct_throughput')
file_gif = '/archive1/hinode/sot/ct_throughput/ct_throughput_mission.gif'
write_gif, file_gif, buff
;write_gif, '/archive1/hinode/sot/ct_throughput/ct_throughput_mission.gif', buff
;concat_dir(dir_ct_response, 'ct_throughput_mission.gif'), buff
chmod, file_gif, '0664'o

set_plot, 'x'


; Optionally save latest arrays to current db:
if keyword_set(do_save) then begin
  save, file=dbnam_ct_response, index_ct

; Save latest arrays to dated backup file:
  dbnam_ct_response_backup = $
    strmid(filnam_ct_response,0,strlen(dbnam_ct_response)-4) + '_' + $
      anytim(ut_time(),/ccsds,/date) + '.sav'
  dbnam_ct_response_backup = concat_dir(dir_hinode_instr_db_backup, dbnam_ct_response_backup)
; save, file='./ct_throughput_mission.sav', index_ct ; dbnam_ct_response_backup, index_ct
  save, file='/sanhome/slater/public_html/sot/instrument/mechanisms_data_ops/' + $
             'ct_throughput_mission.sav', index_ct ; dbnam_ct_response_backup, index_ct
endif

if keyword_set(qstop) then stop, ' Stopping at requested point.'

end

