pro seq_sum_p2, lun, v, db

obs_id = mask(v,0,12)
obs_id = [obs_id]		;turn into an array so sort will work
uobs_id = obs_id(uniq(obs_id, sort(obs_id)))
;
cam_id = mask(v, 12, 10)
cam_id = [cam_id]		;turn into an array so sort will work
ucam_id = cam_id(uniq(cam_id, sort(cam_id)))
;
skip_id = [0, 'ffc'x, 'ffd'x, '033'x, '034'x, '00b'x, '00c'x, '020'x, 'fff'x]
for i=0,n_elements(uobs_id)-1 do begin
    ss0 = where(skip_id eq uobs_id(i), nss0)
    if (nss0 eq 0) then begin
        ss2 = where((db.type eq 'OBS') and (db.id eq uobs_id(i)), nss2)
        ss2 = ss2(0)
        str = '     OBS ID Not regognized  (ID = ' + string(uobs_id(i), format='(z3.3)') + ')'
        ;if (nss2 ne 0) then str = string(db(ss2).label+'             ', db(ss2).descr, format='(5x, a20, 3x, a)')
        if (nss2 ne 0) then str = string(db(ss2).label+'             ', format='(5x, a20)')
        ss3 = where(obs_id eq uobs_id(i), nss3)
        dur = string(nss3*7.5/60, format='(" (~", f6.1, " min)")')
        str = string(str + '                                                    ', dur, format='(a50,a)')
        printf, lun, str
    end
end

end
;-----------------------------------------------------------------
pro seq_run_sum, sttim0, entim0, outfil=outfil, month=month, info=info, qstop=qstop
;+
;NAME:
;	seq_run_sum
;PURPOSE:
;	To look through the Sequence ID and log all times when the
;	sequence was running
;SAMPLE CALLING SEQUENCE:
;	seq_run_sum, '18-dec-95', !stime
;	seq_run_sum, month='Jan-96', outfil='$MDI_CAL_INFO/9601.seq_run_sum
;	seq_run_sum, sttim, entim, outfil=outfil, month=month, info=info
;INPUTS:
;	sttim	- starting time
;	entim	- ending time
;OPTIONAL KEYWORD INPUT:
;	outfil	- an output file name to write to
;	month	- Optionally specify the month rather than the sttim/entim
;	info	- The get_hk_info returned structure
;HISTORY:
;	Written Jan-96 by M.Morrison
;	15-Jan-96 (MDM) - Added documentation and cleaned it up
;	31-Jan-96 (MDM) - Added call to HK_TIME_SORT to clean up the slotted
;			  file error problem
;	22-Feb-96 (MDM) - Corrected for case where the sequence was never running
;			- Modified to not corrupt/change the input variable
;-
;
restgen, db, file='$MDI_OPS_IP_DEP/opsdb_prep.genx'
;restore,'~morrison/opsdb_prep.idl
;
lun = -1
if (keyword_set(outfil)) then begin
    openw, lun, outfil, /get_lun
    printf, lun, 'SEQ_RUN_SUM  Ver 1.0  Run ' + !stime
    printf, lun, ' '
end
;
if (keyword_set(month)) then begin
    sttim = '1-' + month
    tarr = anytim2ex(sttim)
    tarr(5) = tarr(5) + 1       ;increment to next month
    if (tarr(5) ge 13) then begin
        tarr(5) = 1
        tarr(6) = tarr(6) + 1
    end
    entim = gt_day(anytim2ints(tarr), /str)
end
if (keyword_set(sttim0)) then sttim = sttim0
if (keyword_set(entim0)) then entim = entim0
if (n_elements(sttim) eq 0) then sttim = '18-Dec-95'
if (n_elements(entim) eq 0) then entim = ut_time()
;
print, 'Processing from ' + sttim + ' to ' + entim
if (n_elements(info) eq 0) then info=get_hk_info([sttim,entim],['mkdpsqst','mksqid'])
if (data_type(info) ne 8) then begin
    print, 'HK Read did not find valid data'
    if (lun ne -1) then free_lun, lun
    return
end
;
hk_time_sort, info		;MDM added 31-Jan-96
;
stat = info.mdi.value(*,0)
qrunning = stat ne 0
ss = where(qrunning)
;
if (ss(0) eq -1) then begin		;MDM added 22-Feb-96
    printf, lun, 'Sequence never running'
    return
end
;
obs_id = mask(info.mdi.value(ss,1),0,12)
uobs_id = obs_id(uniq(obs_id, sort(obs_id)))
print, uobs_id, format='(8z5)'
;
cam_id = mask(info.mdi.value(ss,1), 12, 10)
ucam_id = cam_id(uniq(cam_id, sort(cam_id)))
print, ucam_id, format='(8z5)'
;
for i=0,n_elements(ucam_id)-1 do begin
    ss2 = where((db.type eq 'CAM') and (db.id eq ucam_id(i)), nss2)
    ss2 = ss2(0)
    if (nss2 eq 0) then print, ucam_id(i), 'Not recognized', format='(z5, " --- ", a)' $
                else print, ucam_id(i), db(ss2).label, db(ss2).descr, format='(z5, " --- ", a20, 3x, a)'
end
;
trans = uniq(qrunning)
q1st_on = long(qrunning(0))           ;if already on, move to the first off/on transition
for i=q1st_on,n_elements(trans)-2,2 do begin            ;not complete logging
    ist = trans(i)+1
    ien = trans(i+1)
    sttim = info.mdi.daytime(ist)
    entim = info.mdi.daytime(ien)
    test1 = [info.mdi.value(ist:ien,1)]
    qprint = (min(test1) ne max(test1)) and (min(test1) ne 'ffffffff'x)
    if (qprint) then $
    printf, lun, fmt_tim(sttim), ' -- ', fmt_tim(entim), '   (' + string(int2secarr(entim,sttim)/60., format='(f6.1)') + ' mins)'
    ;
    seq_sum_p2, lun, info.mdi.value(ist:ien,1), db
end

if (keyword_set(qstop)) then stop
if (lun ne -1) then free_lun, lun
end
