;+
; NAME:
;     GETTASK
; PURPOSE:
;     Function that returns a list of task names appropriate to a given
;     type of entry as used in LAUNCHER, or, if the numtasks switch is set,
;     returns the number of defined tasks.  This gives LAUNCHER its
;     "context-sensitivity."
; CATEGORY:
;     OVRO APC ANALYSIS
; CALLING SEQUENCE:
;     list   = gettask(type)
;     ntasks = gettask(/numtasks)
; INPUTS:
;     type      the type of entry that the task must be able to act
;                   upon.  If numtasks switch is set, this input is ignored.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     numtasks  if set, the number of defined tasks is returned.
; ROUTINES CALLED:
;     ovsa_make_index
; OUTPUTS:
;     list      the list of task names that can act upon entries of the
;                   type specified (only if numtasks switch is not set)
;     numtasks  the number of tasks that have been defined in this file
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
;     The defparms routine must be called before this function is called.
; MODIFICATION HISTORY:
;     Written 25-Sep-1997 by Dale Gary
;     13-Jan-1998  DG
;       Added WTestPlot and Truncate tasks
;     16-Mar-1998  DG
;       Added GCalChek and FCalChek tasks. Had to introduce generic and
;       specific tasks, since my original idea had problems.
;     23-Jun-1998  DG
;       Added SolChek and ACalChek tasks.
;     04-Mar-1999  DG
;       Added DLCalChek task
;     20-Apr-1999  DG
;       Several changes to implement scans properly, instead of using
;       segments.
;     12-May-1999  DG
;       Added DLAscan task
;     15-Nov-1999  DG
;       Added CtrCalChek task
;     29-Dec-1999  DG
;       Added PntChek task
;     12-Mar-2000  DG
;       Added WDialog_History task
;     14-Apr-2000  DG
;       Get rid of C:\TEMP references (use !Defaults.workdir)
;     18-Apr-2000  DG
;       Change to reflect change in call to TPANALYZE
;     07-Nov-2000  DG
;       Added EXTRACT_RAW_DATA task
;     23-Jul-2003  DG
;       Changed name of MAKE_INDEX to OVSA_MAKE_INDEX to avoid conflict with
;       LASCO routine of the same name.
;-

function gettask,intype,numtasks=numtasks

   ; Define the {task} structure
   junk = {task,name:'',routine:'',type:0}
   NTASKS = 24

   if (keyword_set(numtasks)) then return,NTASKS
   task = replicate({task},NTASKS)

   ; Open/Close status should be masked (not relevant to task)
   ocmask = "7747
   type = intype and ocmask

   ; Check that the given type is one of the defined !TYPES
   tindex = where(!TYPES eq type)
   if (tindex(0) eq -1) then begin
      junk = widget_message('Unrecognized TYPE of entry.'+string(type),/error)
      return,junk     ; Return an empty task structure
   endif

   ; Actually define the tasks.  When a new task is written, it should be
   ; entered in this file.  This defines the task structure

   task(0) = {task,'Dump Data','wdumpRec,filename,recn,erec,group=Event.top',  !T_FILE or !T_SCAN or !T_SEGM}
;   task(1) = {task,'Print Date','print_date,filename,recn,erec',  !T_SCAN or !T_SEGM}
   task(1) = {task,'TEST Plot','wfsurvey,filename,recn', !T_SCAN}
   task(2) = {task,'Truncate','truncate,infile=filename,/auto', !T_FILE}

   task(3) = {task,'GCalChek','gcalChek,filename,recn', !T_GSCAN}
   task(4) = {task,'FCalChek','fcalChek,filename,recn', !T_FSCAN}
   task(5) = {task,'WebPage','oview2Web,filename', !T_FILE}
   task(6) = {task,'ACalChek','acalchek,filename,recn', !T_ASCAN}
   task(7) = {task,'PCalChek','pcalchek,filename,recn,erec', !T_PSCAN}
   task(8) = {task,'LockMap','fcalchek,filename,recn,/lockmap', !T_FSCAN}
   task(9) = {task,'TPCalChek','tpcalchek,filename,recn',!T_TPSCN}
   task(10) = {task,'Solaid','solaid,filename,recn,erec',!T_SSCAN or !T_PSCAN}
   task(11) = {task,'QuickLook','quicklook,filename',!T_FILE}
   task(12) = {task,'DLCalChek','dlcalchek,filename,recn', !T_DSCAN}
   task(13) = {task,'DLAscan','dlascan,filename,recn', !T_DSCAN}
   task(14) = {task,'ScreenDump','screenDmp,filename,recn',!T_SEGM}
   task(15) = {task,'Analyze','analyze,filename,recn,120.,[recn,erec],'$
                +'out=!defaults.workdir+string(recn,format="(I5.5)")+".sav"',!T_PSCAN}
   task(16) = {task,'CtrCalChek','ctrcalchek,filename,recn', !T_CRSCN}
   task(17) = {task,'Make_index','ovsa_make_index,filename', !T_FILE}
   task(18) = {task,'PntChek','pntchek,filename,recn',!T_PTSCN}
   task(19) = {task,'TPAnalyze','tpanalyze,filename,recn,0.,[recn,erec]',$
                !T_SSCAN}
   task(20) = {task,'Dialog History','wdialog_history,filename', !T_FILE}
   task(21) = {task,'ExtractRaw','extract_raw_data,filename,recn,erec',  !T_FILE or !T_SCAN or !T_SEGM}
   task(22) = {task,'Peakup','pkupchek,filename,recn,/debug',!T_PUSCN}
   task(23) = {task,'AR Locations','arlocplot,filename,recn',$
                !T_SSCAN}

   ; Now find the tasks that match the type.  Two kinds of tasks are defined.
   ; Generic tasks are those matching any or all of files, scans, or segments,
   ; and have "type" codes less than 8.  Specific tasks are those matching a
   ; specific type of file, scan, or segment, and have "type" codes of 8 or more.

   j = -1

   list = replicate({task},NTASKS)
   for i = 0, NTASKS-1 do begin
      if (task(i).type lt 8) then begin
         ; This is a "generic task" so any matching bit will do.
         if ((task(i).type and type) ne 0) then begin
            j = j + 1
            list(j) = task(i)
         endif
      endif else begin
         ; This is a "specific task" so all bits must match.
         if ((task(i).type and type) eq type) then begin
            j = j + 1
            list(j) = task(i)
         endif
      endelse
   endfor

   if (j eq -1) then begin
      junk = widget_message('No tasks available for TYPE.'+string(type),/error)
      return,junk     ; Return an empty task structure
   endif
   list = list(0:j)
return,list
end
