;+
; Project     : SOHO - CDS
;
; Name        : CHECK_CDS_MODES
;
; Purpose     : check CDS raster telemetry against DSN submodes
;
; Category    : planning
;
; Explanation : the checks are as follows:
;
;               if high-rate (submode 3) - permit L, M, & H (and P) rasters
;               if medium-rate (submode 5,6) - permit L and M (and P) only
;               if low+ (submode 2) - permit L (and P) only
;               if low-rate (submode 4 ) - permit L only
;
; Syntax      : IDL> flagged=check_cds_modes(plans,dsn)
;
; Inputs      : PLANS = detailed plans array
;             : DSN   = DSN schedule
;
; Opt. Inputs : None
;
; Outputs     : FLAGGED = boolean array with indicies of plans with
;               raster telemetries that don't match current DSN submodes.
;
; Opt. Outputs: None
;
; Keywords    : ERR = error string
;               VERBOSE = verbose output
;               COUNT = # of flagged plans
;               PROBLEM= description of problem
;
; Common      : None
;
; Restrictions: None
;
; Side effects: None
;
; History     : 28-Nov-1997,  D.M. Zarro.  Written
;
; Contact     : DZARRO@SOLAR.STANFORD.EDU
;-

function check_cds_modes,plans,dsn,err=err,verbose=verbose,count=count,problem=problem

on_error,1
err='' & flagged=-1 & count=0
verbose=keyword_set(verbose)
problem=''

;-- check for valid inputs

if (get_plan_type(plans) ne 0) or (get_plan_type(dsn) ne 5) then begin
 err='Missing PLANS/DSN inputs'
 return,flagged
endif

if strpos(strupcase(dsn(0).res_name),'DSN') eq -1 then begin
 err='Input resource is not DSN -> '+dsn.res_type
 return,flagged
endif

nplans=n_elements(plans)
ndsn=n_elements(dsn)

;-- loop over DSN's and flag plans with inconsistent modes

itime=get_plan_itime(plans)
pstart=str_format(plans.(itime))
pend=str_format(plans.(itime+1))
flagged=bytarr(nplans)
problem=strarr(nplans)
rates=['High','Medium','Low+','Low']

for i=0,ndsn-1 do begin

 submode=dsn(i).submode
 case submode of
  2: rate=rates(2)
  3: rate=rates(0)
  4: rate=rates(3)
  5: rate=rates(1)
  6: rate=rates(1)
  else:rate=rates(1)
 endcase

 if verbose then begin
  message,'checking CDS rasters during '+rate+' rate DSN mode',/cont
 endif

;-- if already in high rate telemetry mode, then don't check  

 if rate ne 'High' then begin 
  dstart=str_format(dsn(i).start_time)
  dend=str_format(dsn(i).end_time)

  overlap=where( ((pstart ge dstart) and (pstart le dend)) or $
                 ((pend ge dstart) and (pend le dend)) or $
                 ((pstart lt dstart) and (pend gt dend)),count)

;-- check each plan (if already checked, then skip to next one)

  if count gt 0 then begin
   for k=0,count-1 do begin
    cplan=plans(overlap(k))
    ctime=str_format(cplan.(itime))
    not_checked=1
    if exist(checked_times) then begin
     checked=where(ctime eq checked_times,cdone)
     not_checked=(cdone eq 0)
    endif
    if not_checked then begin
     perr=''
     get_study_par,plan=cplan,tel=tel,err=perr
     if perr eq '' then begin

;-- cannot run high rate rasters during medium or low rate telemetry

      rchk=where(rate eq ['Med','Low+','Low'],rcheck)
      if rcheck gt 0 then begin
       chk=where(tel eq 'H',hcount)
       if hcount gt 0 then begin
        if verbose then begin
         message,'found HI rate rasters during non-HIGH rate telemetry modes',/cont
         problem(overlap(k))='HIGH rate raster during non-HIGH rate DSN'
        endif
        flagged(overlap(k))=1
       endif
      endif

;-- cannot run high or medium rate rasters during low rate telemetry

      rchk=where(rate eq ['Low+','Low'],rcheck)
      if rcheck gt 0 then begin
       chk=where(tel eq 'M',mcount)
       if mcount gt 0 then begin
        if verbose then begin
         message,'found MED rate rasters during LOW rate telemetry modes',/cont
        endif
        flagged(overlap(k))=1
        problem(overlap(k))='MED rate raster during LOW rate DSN'
       endif
      endif
     endif

;-- cannot run LOW+ rasters in low rate telemetry

     if rate eq 'Low' then begin
      chk=where(tel eq 'P',mcount)
      if mcount gt 0 then begin
       if verbose then begin
        message,'found LOW+ rate rasters during LOW rate telemetry mode',/cont
       endif
       flagged(overlap(k))=1 
       problem(overlap(k))='LOW+ rate raster during LOW rate DSN'
      endif
     endif

     if exist(checked_times) then checked_times=[checked_times,ctime] else $
      checked_times=ctime
    endif 
   endfor
  endif
 endif
endfor

fcheck=where(flagged eq 1,count)

return,flagged

end
