
pro mk_mech_data_ops, t0=t0, t1=t1, mech_name=mech_name, mnem_name=mnem_name, delta_days=delta_days, $
  nskip=nskip, t_mid_arr=t_mid_arr, use_existing_cat=use_existing_cat, $
  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, $
  mnem_ops_arr=mnem_ops_arr, mnem_ops_cum=mnem_ops_cum, mnem_ops_total=mnem_ops_total, $
  mnem_steps_arr=mnem_steps_arr, mnem_steps_cum=mnem_steps_cum, mnem_steps_total=mnem_steps_total, $
  _extra=extra, verbose=verbose

if keyword_set(use_existing_cat) then begin
; filnam_sav = '/sanhome/slater/public_html/sot/instrument/mechanisms_data_ops/' + mech_name + '.sav'
  filnam_sav = '/archive1/hinode/sot/instrument/mechanisms_data_ops/' + mech_name + '.sav'
  restore, filnam_sav

  if strlowcase(mech_name) ne 'pmu' then begin
    cmd0 = 't_mid_arr = t_mid_arr_' + mech_name
    result0 = execute(cmd0)
    cmd1 = 'mnem_ops_arr = ' + mech_name + '_ops_arr'
    result1 = execute(cmd1)
    cmd2 = 'mnem_ops_cum = ' + mech_name + '_ops_cum'
    result2 = execute(cmd2)
    cmd3 = 'mnem_steps_arr = ' + mech_name + '_steps_arr'
    result3 = execute(cmd3)
    cmd4 = 'mnem_steps_cum = ' + mech_name + '_steps_cum'
    result4 = execute(cmd4)
    n_tim_prev = n_elements(t_mid_arr)

    if not exist(t0) then t0 = t_mid_arr[n_tim_prev-1]
  endif

endif

if not exist(t0) then t0 = '22-oct-2006'
if not exist(t1) then t1 = anytim(!stime, /yoh)
if not exist(delta_days) then delta_days = 1 ;10 ;5
if not exist(message_cadence) then message_cadence = delta_days
if not exist(prefix_cat) then prefix_cat = 'mech'

; Note: For periodic psoitions (circular), the position differences are calculated by assuming the smallest
;       rotation angle was used
; TODO: Add 'use_existing' logic
; TODO: Add 'shortest distance' logic
; TODO: Normalized plots on one graph

mech_name_arr = ['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']
mnem_name_arr = ['fpp_pmu_status','fpp_focus_pos','fpp_bfi_filt_pos','fpp_bfi_sht_pos','fpp_nfi_blkfilt_pos', $
                 'fpp_nfi_sht_pos','fpp_nfi_mask_pos','fpp_sp_slitscan_pos','fpp_ww_pos', $
                 'fpp_nfi_tf_elm1_pos','fpp_nfi_tf_elm2_pos','fpp_nfi_tf_elm3_pos','fpp_nfi_tf_elm4_pos', $
                 'fpp_nfi_tf_elm5_pos','fpp_nfi_tf_elm6_pos','fpp_nfi_tf_elm7_pos','fpp_nfi_tf_elm8_pos']
pos_limit_lo = long([  0,-10000,  0,-10000,  0,-10000,  0,-1000,  0, $
                       0,  0,  0,  0,  0,  0,  0,  0])
pos_limit_hi = long([239, 10000,239, 10000,239, 10000,179, 1000,179, $
                     167,167,167,167,167,167,167,167])
pos_range = pos_limit_hi - pos_limit_lo

step_factor = [1,20,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

;if keyword_set(use_existing_cat) then begin
  if not exist(dir_top_mech) then dir_top_mech = '/sanhome/slater/public_html/sot/instrument/mechanisms_data_ops'

tg = timegrid(t0, t1, days=delta_days, out='vms')
ng = n_elements(tg)

; Determine which mech was passed:
if exist(mech_name) then ss_match = where(strlowcase(mech_name) eq mech_name_arr, n_match) else $
  if exist(mnem_name) then ss_match = where(strlowcase(mnem_name) eq mnem_name_arr, n_match)
mech_name0 = strlowcase(mech_name)
if n_match gt 0 then mech_num = ss_match+1

if not exist(nskip) then nskip = 1
for i = 0,ng-2,nskip do begin
  delvarx, mnem_struc0, mnem_name0, mnem_data0, t_mnem0

IF ( file_exist(concat_dir('/archive1/hinode/sot/hk/0x0544', time2file(tg[i],   /date) + '.0x0544' )) and $
     file_exist(concat_dir('/archive1/hinode/sot/hk/0x0544', time2file(tg[i],   /date) + '.0x0544x')) and $
     file_exist(concat_dir('/archive1/hinode/sot/hk/0x0544', time2file(tg[i+1], /date) + '.0x0544' )) and $
     file_exist(concat_dir('/archive1/hinode/sot/hk/0x0544', time2file(tg[i+1], /date) + '.0x0544x')) ) THEN BEGIN

  mnem_struc0 = sag_get_mnem(tg(i), tg(i+1), mnem_name_arr[ss_match])

  mnem_name0 = mnem_struc0.(0).mnem
  mnem_data0 = mnem_struc0.(0).value
  n_val0 = n_elements(mnem_data0)

IF N_VAL0 GT 1 THEN BEGIN

  tg0_sec = anytim(tg(i))
  tg1_sec = anytim(tg(i+1))
  
  t_mnem0 = mnem_struc0.(0).daytime
  t_mnem_sec0 = anytim(t_mnem0)

ss_good = where( ((t_mnem_sec0 ge tg0_sec) and (t_mnem_sec0 le tg1_sec)), n_good) 

IF N_GOOD GT 0 THEN BEGIN

  mnem_data0 = mnem_data0[ss_good]
  t_mnem0 = t_mnem0[ss_good]
  t_mnem_sec0 = t_mnem_sec0[ss_good]
  n_val0 = n_elements(mnem_data0)

  t_mnem_sec_avg0 = mean(t_mnem_sec0)
  t_mid0 = anytim(t_mnem_sec_avg0, /yoh)
; HELP, TG[I], TG[i+1], MNEM_STRUC0, MNEM_NAME0[0], MNEM_DATA0
  if mech_num eq 1 then begin

; For the PMU, calculate the total 'on' time and convert into number of rotations:
; For the PMU, we assume that a value of 8 for 'fpp_pmu_status' indicates the PMU is rotating
; at one rev per 1.6 seconds:

    pmu_on0 = lonarr(n_val0)
    ss_status_8 = where(mnem_data0 eq 8, n_status_8)
    if n_status_8 gt 0 then pmu_on0[ss_status_8] = 1l
    status_val_arr0 = all_vals(mnem_data0)

    if not exist(pmu_on_arr) then begin
      t_mid_arr = t_mid0
      pmu_on_arr = total(pmu_on0)
      status_val_arr = status_val_arr0
    endif else begin
      t_mid_arr = [t_mid_arr, t_mid0]
      pmu_on_arr = [pmu_on_arr, total(pmu_on0)]
      status_val_arr = [status_val_arr, status_val_arr0]
      status_val_arr = status_val_arr[uniq(status_val_arr)]
    endelse
    pmu_rot_arr = pmu_on_arr*10./1.6
    pmu_rot_cum = total(pmu_on_arr, /cumulative)*10./1.6
    pmu_rot_total = total(pmu_on_arr)*10./1.6

    if (verbose ge 1) then begin
      print, 'Finished for ' + mech_name_arr[ss_match] + ' for ' + strmid(t_mid0,0,9) + $
             '.  Total rotations = ' + strtrim(pmu_rot_total,2)
    endif

  endif else begin

; For all other mechs, we calculate the total 'operations' and total steps:
     
; Here we calculate the shortest distance, and assume that is the path taken (with noted except):
    mnem_data_long = long(mnem_data0)
    pos_diff_1 = abs(mnem_data_long[1:*] - mnem_data_long)
    pos_diff_2 = (pos_range[ss_match])[0] - pos_diff_1
;ss_neg_test = where((pos_diff_1 lt 0) or (pos_diff_2 lt 0), n_neg_test)
;if n_neg_test gt 0 then stop, 'Negative pos_diff_2 values for ' + strtrim(mech_name_arr[ss_match],2) + '.  Stopping.'

    if ((mech_num ne 2) and (mech_num ne 4) and (mech_num ne 15) and (mech_num ne 16)) then begin
      diff_12 = pos_diff_1 - pos_diff_2
      ss_diff_1 = where(diff_12 le 0, n_diff_1)
      ss_diff_2 = where(diff_12 gt 0, n_diff_2)
      mnem_steps0 = lonarr(n_val0)
      if n_diff_1 gt 0 then mnem_steps0[ss_diff_1] = pos_diff_1[ss_diff_1]
      if n_diff_2 gt 0 then mnem_steps0[ss_diff_2] = pos_diff_2[ss_diff_2]
    endif else begin
      mnem_steps0 = pos_diff_1
    endelse

    if exist(mnem_steps_arr) then mnem_steps_median = median(mnem_steps_arr) else $
       mnem_steps_median = mnem_steps0
    
IF TOTAL(MNEM_STEPS0) LE 4*MNEM_STEPS_MEDIAN THEN BEGIN

    mnem_ops0 = lonarr(n_val0)
    ss_op0 = where(mnem_steps0 ne 0, n_op0)
    if n_op0 gt 0 then mnem_ops0[ss_op0] = 1l

    if not exist(t_mid_arr) then begin
      t_mid_arr = t_mid0
      mnem_ops_arr = total(mnem_ops0)
      mnem_steps_arr = total(mnem_steps0)
    endif else begin
      t_mid_arr = [t_mid_arr, t_mid0]
      mnem_ops_arr = [mnem_ops_arr, total(mnem_ops0)]
      mnem_steps_arr = [mnem_steps_arr, total(mnem_steps0)]
    endelse
    mnem_ops_cum = total(mnem_ops_arr, /cumulative)
    mnem_ops_total = total(mnem_ops_arr)
    mnem_steps_cum = total(mnem_steps_arr, /cumulative)
    mnem_steps_total = total(mnem_steps_arr)

    if (verbose ge 1) then begin
      print, 'Finished for ' + mech_name_arr[ss_match] + ' for ' + strmid(t_mid0,0,9) + $
             '.  Total steps = ' + strtrim(mnem_steps_total,2)
    endif

 ENDIF

  endelse

;  mech_struct = {str_subset(cat, ['date_obs', 'obs_type', 'wave'])     ; Reduce structure to desired tag subset
;  if count gt 0 then $
;     write_genxcat, cat_hk, topdir=outpath, prefix=prefix, /day_round, /nelement, /geny

ENDIF

ENDIF

ENDIF

endfor

end

