
pro go_mk_mech_data_ops, t0=t0, t1=t1, use_existing_cat=use_existing_cat, $
  do_mk_mech=do_mk_mech, no_pmu=no_pmu, $
  do_restore=do_restore, do_plots=do_plots, entim_stats=entim_stats, do_html=do_html, $
  no_zbuff=no_zbuff, _extra=extra, verbose=verbose

; TODO: Use EXECUTE statements for all this crap
; TODO: Add plot making
; TODO: Add web page making

if not exist(use_existing_cat) then use_existing_cat = 1
if not exist(verbose) then verbose = 1
;if not exist(dir_top_mech) then dir_top_mech = '/sanhome/slater/public_html/sot/instrument/mechanisms_data_ops'
if not exist(dir_top_mech) then dir_top_mech = '/archive1/hinode/sot/instrument/mechanisms_data_ops'
if not exist(dir_top_out) then dir_top_out = dir_top_mech
if not exist(sttim_stats) then sttim_stats = '22-oct-2006'
if not exist(entim_stats) then entim_stats = anytim(ut_time(), /ccsds, /date)
HELP, entim_stats ; & STOP
if keyword_set(do_mk_mech) then begin

if not keyword_set(no_pmu) then begin
  mk_mech_data_ops, t0=t0, t1=t1, mech_name='pmu', t_mid_arr=t_mid_arr_pmu, $
    pmu_on_arr=pmu_on_arr, status_val_arr=status_val_arr, $
    pmu_rot_arr=pmu_rot_arr, pmu_rot_cum=pmu_rot_cum, pmu_rot_total=pmu_rot_total, $
    use_existing_cat=use_existing_cat, verbose=verbose
  filnam_sav = concat_dir(dir_top_mech, 'pmu.sav')
  save, t_mid_arr_pmu, pmu_on_arr, status_val_arr, pmu_rot_arr, pmu_rot_cum, pmu_rot_total, file=filnam_sav
endif

mk_mech_data_ops, t0=t0, t1=t1, mech_name='focus', t_mid_arr=t_mid_arr_focus, $
  mnem_ops_arr=focus_ops_arr, mnem_ops_cum=focus_ops_cum, mnem_ops_total=focus_ops_total, $
  mnem_steps_arr=focus_steps_arr, mnem_steps_cum=focus_steps_cum, mnem_steps_total=focus_steps_total, $
  use_existing_cat=use_existing_cat, verbose=verbose
filnam_sav = concat_dir(dir_top_mech, 'focus.sav')
save, t_mid_arr_focus, focus_ops_arr, focus_ops_cum, focus_ops_total, focus_steps_arr, $
      focus_steps_cum, focus_steps_total, file=filnam_sav


mk_mech_data_ops, t0=t0, t1=t1, mech_name='sp_scan', t_mid_arr=t_mid_arr_sp_scan, $
  mnem_ops_arr=sp_scan_ops_arr, mnem_ops_cum=sp_scan_ops_cum, mnem_ops_total=sp_scan_ops_total, $
  mnem_steps_arr=sp_scan_steps_arr, mnem_steps_cum=sp_scan_steps_cum, mnem_steps_total=sp_scan_steps_total, $
  use_existing_cat=use_existing_cat, verbose=verbose
filnam_sav = concat_dir(dir_top_mech, 'sp_scan.sav')
save, t_mid_arr_sp_scan, sp_scan_ops_arr, sp_scan_ops_cum, sp_scan_ops_total, sp_scan_steps_arr, $
      sp_scan_steps_cum, sp_scan_steps_total, file=filnam_sav

mk_mech_data_ops, t0=t0, t1=t1, mech_name='ct_wedge', t_mid_arr=t_mid_arr_ct_wedge, $
  mnem_ops_arr=ct_wedge_ops_arr, mnem_ops_cum=ct_wedge_ops_cum, mnem_ops_total=ct_wedge_ops_total, $
  mnem_steps_arr=ct_wedge_steps_arr, mnem_steps_cum=ct_wedge_steps_cum, mnem_steps_total=ct_wedge_steps_total, $
  use_existing_cat=use_existing_cat, verbose=verbose
filnam_sav = concat_dir(dir_top_mech, 'ct_wedge.sav')
save, t_mid_arr_ct_wedge, ct_wedge_ops_arr, ct_wedge_ops_cum, ct_wedge_ops_total, ct_wedge_steps_arr, $
      ct_wedge_steps_cum, ct_wedge_steps_total, file=filnam_sav

endif

if keyword_set(do_restore) then begin
  restore, concat_dir(dir_top_mech, 'pmu.sav')
  restore, concat_dir(dir_top_mech, 'focus.sav')
  restore, concat_dir(dir_top_mech, 'sp_scan.sav')
  restore, concat_dir(dir_top_mech, 'ct_wedge.sav')
endif

; Create cumulative usage plots:

if keyword_set(do_plots) then begin

if ~keyword_set(no_zbuff) then set_plot,'z'
wdef,0,768,512, /zbuffer
!p.charsize  = 1.5
!p.psym = 1
!p.thick = 1.5
mech_name_arr = ['pmu','focus','sp_scan','ct_wedge']
;plot_param_arr = ['Rotations','Steps','Operations','Operations','Operations','Operations','Operations', $
;                  'Steps','Steps','Steps','Steps','Steps','Steps','Steps','Steps','Steps','Steps']
plot_param_arr = ['Rotations','Operations','Operations','Operations']
title_arr = strupcase(mech_name_arr) + ' Usage History'
ytitle_arr = 'Integrated ' + strupcase(mech_name_arr) + ' ' + plot_param_arr
timerange = [sttim_stats, entim_stats]

utplot, t_mid_arr_pmu, pmu_rot_cum, timerange=timerange, title=title_arr[0], ytitle=ytitle_arr[0], xstyle=1
plot_pmu = 255b - bytscl(tvrd())
outfil_pmu = concat_dir(dir_top_mech, 'pmu_usage.gif')
write_gif, outfil_pmu, plot_pmu

focus_steps_arr[39] = focus_steps_arr[38]
focus_steps_cum = total(focus_steps_arr, /cumulative)
utplot, t_mid_arr_focus, focus_ops_cum, timerange=timerange, title=title_arr[1], ytitle=ytitle_arr[1], xstyle=1
plot_focus = 255b - bytscl(tvrd())
outfil_focus = concat_dir(dir_top_mech, 'focus_usage.gif')
write_gif, outfil_focus, plot_focus

sp_scan_steps_arr[39] = sp_scan_steps_arr[38]
sp_scan_steps_cum = total(sp_scan_steps_arr, /cumulative)
utplot, t_mid_arr_sp_scan, sp_scan_ops_cum, timerange=timerange, title=title_arr[2], ytitle=ytitle_arr[2], xstyle=1
plot_sp_scan = 255b - bytscl(tvrd())
outfil_sp_scan = concat_dir(dir_top_mech, 'sp_scan_usage.gif')
write_gif, outfil_sp_scan, plot_sp_scan

ct_wedge_steps_arr[39] = ct_wedge_steps_arr[38]
ct_wedge_ops_cum = total(ct_wedge_steps_arr, /cumulative)
utplot, t_mid_arr_ct_wedge, ct_wedge_ops_cum, timerange=timerange, title=title_arr[3], ytitle=ytitle_arr[3], xstyle=1
plot_ct_wedge = 255b - bytscl(tvrd())
outfil_ct_wedge = concat_dir(dir_top_mech, 'ct_wedge_usage.gif')
write_gif, outfil_ct_wedge, plot_ct_wedge

set_plot, 'x'

endif

if keyword_set(do_html) then begin

; Create HTML:

col_head_arr = ['Mech','Use','LifeTest','% LifeTest']

;mech_name_arr = strupcase(['pmu','focus','bfi_filt','bfi_shut','nfi_filt', $
;                           'nfi_shut','nfi_mask','sp_scan','ct_wedge', $
;                           'tf_1','tf_2','tf_3','tf_4','tf_5','tf_6','tf_7','tf_8'])
mech_name_arr = strupcase(['pmu','focus','sp_scan','ct_wedge'])

ss_close_pmu = tim2dset(anytim(t_mid_arr_pmu, /ints), anytim(entim_stats, /ints))
pmu_rot_stats = pmu_rot_cum[ss_close_pmu]
ss_close_focus = tim2dset(anytim(t_mid_arr_focus, /ints), anytim(entim_stats, /ints))
focus_ops_stats = focus_ops_cum[ss_close_focus]
ss_close_sp_scan = tim2dset(anytim(t_mid_arr_sp_scan, /ints), anytim(entim_stats, /ints))
sp_scan_ops_stats = sp_scan_ops_cum[ss_close_sp_scan]
ss_close_ct_wedge = tim2dset(anytim(t_mid_arr_ct_wedge, /ints), anytim(entim_stats, /ints))
ct_wedge_ops_stats = ct_wedge_ops_cum[ss_close_ct_wedge]

 bfi_shut_ops_stats = 3.57920d6
 nfi_shut_ops_stats = 6.21750d6

usage_arr = [pmu_rot_stats, focus_ops_stats, sp_scan_ops_stats, ct_wedge_ops_stats]
usage_arr_string = string(usage_arr, format='$(e8.2)')

;FPP FOCUS MECH: >16M STEPS
;FPP SLIT SCAN MECH: >7.9M STEPS (FPP LIFE TEST)
;FPP WB AND NB SHUTTER: ~68M STEPS (ON-ORBIT MDI); >32M STEPS (SXI LIFE TEST)
;FPP WB AND NB FW: >32M LIFE TEST STEPS (SXI LIFE TEST)
;FPP MASK AND FW WEDGE WHEELS: >32M LIFE TEST STEPS (SXI LIFE TEST)
;FPP TF MECHS: ~>178M STEPS (FPP LIFE TEST)

;pmu_life_test = 
;focus = 1.6d7 steps
;bfi_filt = 3.2d7 steps
;bfi_shut = 6.8d7 steps
;nfi_filt = 3.2d7 steps
;nfi_shut = 6.8d7 steps
;nfi_mask = 3.2d7 steps
;sp_scan = 7.9d6 steps
;ct_wedge = 3.2d7 steps
;tf_* = 1.78d8 steps

lifetest = [3.7d8, 1.6d7, 6.0d7, 3.2d7]
lifetest_string = string(lifetest, format='$(e7.1)')

percent_lifetest = (usage_arr/lifetest)*100d0
percent_lifetest_string = string(percent_lifetest, format='$(f8.2)')

temp = transpose([[mech_name_arr], [usage_arr_string], [lifetest_string], [percent_lifetest_string]])

tab_arr = [col_head_arr]
tab_arr = [[tab_arr],[reform(temp[*, 0])]]
tab_arr = [[tab_arr],[reform(temp[*, 1])]]
tab_arr = [[tab_arr],[reform(temp[*, 2])]]
tab_arr = [[tab_arr],[reform(temp[*, 3])]]

url_arr = './' + strlowcase(mech_name_arr) + '_usage.gif' 

table_html = strtab2html_data_ops(tab_arr, /inc_img_toggle, url_arr=url_arr, /row0)
outfil = concat_dir(dir_top_out, 'usage.html')
prstr, table_html, file=outfil
;STOP
endif

end
