pro seq_sum_p3, lun, cam_id0, obs_id, vc, db, sttim, entim, not_running=not_running, noscreen=noscreen


ss = where((vc eq 0), nss)		& dur0 = nss*7.5/60
ss = where((vc eq 2), nss)		& dur2 = nss*7.5/60
ss = where((vc eq 3), nss)		& dur3 = nss*7.5/60
sdur23 = string(dur2, dur3, format='(" (~",f6.1,"/",f6.1," mins)")')
sdur0  = string(dur0,       format='(" (~",f6.1,7x,      " mins)")')
;
extra = ''
if (dur2 ne 0) then extra = extra + 'VC2/'
if (dur3 ne 0) then extra = extra + 'VC3/'
extra = strmid(extra, 0, strlen(extra)-1)
if (strlen(extra) ne 0) then extra = ' ' + extra
;
if (not keyword_set(not_running)) then begin
    ;;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)
    ;;ss2 = where(cam_id eq 0, nss2)
    ;;if (nss2 ne 0) then cam_id(ss2) = '3f0'x	;make it look like structure running
    cam_id = cam_id0
    cam_id = [cam_id]		;turn into an array so sort will work
    ucam_id = cam_id(uniq(cam_id, sort(cam_id)))
    ;
    cam_str = '?????              '
    ss2 = where((db.type eq 'CAM') and (db.id eq cam_id(0)), nss2)
    if (nss2 ne 0) then cam_str = string(db(ss2).label+'                    ', format='(a20)')
    if (cam_id(0) eq '3f0'x) then cam_str = 'STRUCTURE/DYN               '
    if (cam_id(0) eq -100) then begin & cam_id = 0 & cam_str = 'STRUCTURE                  ' & end
    if (cam_id(0) eq -200) then begin & cam_id = 0 & cam_str = 'DYNAMICS                   ' & end
    if (cam_id(0) eq -400) then begin & cam_id = 0 & cam_str = 'JITTER RUN                 ' & end
end else begin
    cam_str = 'NOT RUNNING                 '
    cam_id = 0
end
;
;fmt = '(a15, 2x, z3.3, "h", 3x, a, " -- ", a, "  ( ", f6.1, " mins", a, ")")'
fmt = '(a16, 1x, z3.3, "h", 3x, a, " -- ", a, "  ( ", f6.1, " mins", a, ")")'
str = string(cam_str, cam_id(0), fmt_tim(sttim), gt_time(entim,/str), int2secarr(entim,sttim)/60., extra, format=fmt)
printf, lun, str
if (keyword_set(not_running)) then return
if (cam_id0(0) eq -400) then return		;jitter run
;
skip_id = [0, 'ffc'x, 'ffd'x, '033'x, '034'x, '00b'x, '00c'x, '020'x, 'fff'x, '04f'x, '050'x, '096'x]
if (keyword_set(noscreen)) then skip_id = -10000
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='(2x, a20, 3x, a)')
        if (nss2 ne 0) then str = string(db(ss2).label+'             ', format='(2x, 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='(a56,a)')
        ss3a = where((obs_id eq uobs_id(i)) and (vc eq 2), nss3a)
        ss3b = where((obs_id eq uobs_id(i)) and (vc eq 3), nss3b)
        dura = nss3a*7.5/60
        durb = nss3b*7.5/60
        str = string(str + '                                                    ', dura, durb, format='(a56," (~",f6.1,"/",f6.1," mins)")')
        if (cam_id0(0) eq -100) then begin      ;structure (no VC)
            ss3a = where(obs_id eq uobs_id(i), nss3a)
            dura = nss3a*7.5/60
            str = string(str + '                                                    ', dura, format='(a56," (~",f6.1,7x," mins)")')
        end
        printf, lun, str
    end
end

end
;-----------------------------------------------------------------
pro cam_run_sum, sttim0, entim0, outfil=outfil, month=month, info=info, qstop=qstop, noscreen=noscreen
;+
;NAME:
;	cam_run_sum
;PURPOSE:
;	To look through the Sequence ID and log all times when a campaign
;	was running
;SAMPLE CALLING SEQUENCE:
;	cam_run_sum, '18-dec-95', !stime
;	cam_run_sum, month='Jan-96', outfil='$MDI_CAL_INFO/9601.cam_run_sum
;	cam_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 2-Feb-96 by M.Morrison
;	22-Feb-96 (MDM) - patched sttim/entim argument error
;V2.0	21-Mar-96 (MDM) - Modified to show when structure was running
;			- Screen out the new OBS_PRIME00 obs-id
;			- Display the time when it was run in UT
;V2.1	29-Mar-96 (MDM) - Modified the logic for what to do if the sequence is
;			  already running for the first sample point
;V3.0	16-Apr-96 (MDM) - Modified to show when we have dynamics versus structure (vc data)
;			- Modified to show fraction of VC2/VC3 in the obs line
;			- Modified to show when structure is stopped
;			- Added logging capability of jitter runs
;			- Flag on the summary line whether it was VC2 and/or VC3
;V3.1	11-Nov-96 (MDM) - Added more more character for the campaign name
;V3.2	 9-May-97 (MDM) - Modified to log the last (current) campaign running
;			  (previously recorded N-1 campaigns)
;V3.3	22-May-97 (MDM) - Added protection for single run (9-May added an error)
;-
;
restgen, db, file='$MDI_OPS_IP_DEP/opsdb_prep.genx'
;restore,'~morrison/opsdb_prep.idl
;
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()
;
lun = -1
if (keyword_set(outfil)) then begin
    openw, lun, outfil, /get_lun
    printf, lun, 'CAM_RUN_SUM  Ver 3.3  Run ' + ut_time() + ' UT'
    printf, lun, '    Processing from ' + fmt_tim(sttim) + ' to ' + fmt_tim(entim)
    printf, lun, ' '
end
;
print, 'Processing from ' + sttim + ' to ' + entim
if (n_elements(info) eq 0) then info=get_hk_info([sttim,entim],['mkdpsqst','mksqid','mksqrg3','mkdpmd'])
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
n = n_elements(info.mdi.daytime)
;
stat = info.mdi.value(*,0)
qrunning = stat ne 0
ss = where(qrunning)
;
obs_id = mask(info.mdi.value(*,1),0,12)
;
cam_id = mask(info.mdi.value(*,1), 12, 10)
;
trans0 = uniq(qrunning)
;for i=0,n_elements(trans0)-2 do cam_id( trans0(i): (trans0(i)+10)<(n-1) ) = 0		;at sequence startup, the old cam is set
for i=0,n_elements(trans0)-2 do cam_id( trans0(i): (trans0(i)+10)<(n-1) ) = '3f0'x	;at sequence startup, the old cam is set
;
ss2 = where(cam_id eq 0, nss2)
if (nss2 ne 0) then cam_id(ss2) = '3f0'x	;make it look like structure running
;
vc = info.mdi.value(*,2)
;
ss = where((vc eq 0) and (cam_id eq '3f0'x), nss)
if (nss ne 0) then cam_id(ss) = -100			;set to structure
ss = where((vc ne 0) and (cam_id eq '3f0'x), nss)
if (nss ne 0) then cam_id(ss) = -200			;set to dynamics
ss = where(info.mdi.value(*,3) eq 4, nss)
if (nss ne 0) then begin
    cam_id(ss) = -400			;set to jitter run
    qrunning(ss) = 1
end
;
trans = uniq(long(qrunning) * cam_id)
;;q1st_on = long(qrunning(0))           ;if already on, move to the first off/on transition
;;for i=q1st_on,n_elements(trans)-2 do begin            ;not complete logging
q1st_on = long(qrunning(0))
if (q1st_on) then trans = [-1, trans]
nn = n_elements(trans)
if (max(trans) eq n_elements(qrunning)-1) then nn=nn-1		;MDM 22-May-97
for i=0,nn-1 do begin            ;not complete logging
    ist = trans(i)+1
    if (i eq nn-1) then ien = n_elements(qrunning)-1 else ien = trans(i+1)
    ;
    sttim = info.mdi.daytime(ist)
    entim = info.mdi.daytime(ien)
    if (cam_id(ist) ne 0) and (qrunning(ist)) then begin
        test1 = [info.mdi.value(ist:ien,1)]		;mksqid
        qprint = (min(test1) ne max(test1)) and (min(test1) ne 'ffffffff'x)
	if ((min(test1) and 'ffffff00'x) eq 'c4000000'x) then qprint = 1	;MDM added 18-Jun-96
	if (cam_id(ist) eq -400) then qprint = 1	;override above filter?
        if (qprint) then seq_sum_p3, lun, cam_id(ist:ien), obs_id(ist:ien), vc(ist:ien), db, sttim, entim, noscreen=noscreen
    end else begin
	if (ist le ien) then seq_sum_p3, lun, aa, bb, vc(ist:ien), db, sttim, entim, /not_running	
    end
end

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