;+
; Project     : SOHO - CDS
;
; Name        : GET_STUDY_PAR
;
; Purpose     : to get CDS study parameter information
;
; Category    : database, planning
;
; Explanation : 
;
; Syntax      : IDL> get_study_par,study_id,studyvar
;
; Inputs      :	STUDY_ID  = study id
;             : STUDYVAR = study variation
;
; Opt. Inputs :	None.
;
; Outputs     :	See keywords.
;
; Opt. Outputs:	None.
;
; Keywords    :	
;               STUDY = study definition (optional input)
;               PLAN  = plan structure (optional input)
;               RASTERS = raster definition (optional output)
;               COUNT = no of rasters found (zero for engineering)
;               ERR = any error string
;               RDUR  = duration (secs) of individual rasters composing study 
;               REPS  = # of repeats per raster
;               DETS  = detectors per raster (e.g. ['N','G','N'....])
;               SLITS = slit number per raster
;               SDUR = total duration of study from summing rasters
;                      (not taking account of slit and OPS motions)
;               ENG  =1/0 if engineering/science study
;               GIS  =1/0 if study uses GIS
;               TELEM  = raster telemetry modes
;               VERBOSE = verbose output
;
; Common      : None
;
; Restrictions: Input study must be valid. Cannot enter PLAN and STUDY
;               keywords simultaneously
;
; Side effects: None
;
; History     : Version 1,  1-Jan-1996,  D.M. Zarro.  Written
;
; Contact     : DZARRO@SOLAR.STANFORD.EDU
;-

pro get_study_par,study_id,studyvar,count=count,err=err,oh=oh,slits=slits,$
            dets=dets,rdur=rdur,reps=reps,rasters=rasters,study=study,$
            plan=plan,sdur=sdur,eng=eng,gis=gis,zone_id=zone_id,$
            acronym=acronym,telem=telem,verbose=verbose

err=''

;-- defaults
dets='' & count=0 & reps=0 & dur=0. & rdur=0. & sdur=0. & eng=1 & gis=0
telem='M'
zone_id=0
home_pos=get_cds_home()
slits=home_pos.slitn

verbose=keyword_set(verbose)
mess1='Engineering study'
mess2='No rasters in study'

id_entered=exist(study_id) and exist(studyvar)
plan_entered=datatype(plan) eq 'STC'
study_entered=datatype(study) eq 'STC'

;-- study ID's take precedence over STUDY keyword which takes precedence
;   over PLAN keyword

if not id_entered then begin
 if study_entered then begin
  if tag_exist(study,'STUDY_ID') then study_id=study.study_id
  if tag_exist(study,'STUDYVAR') then studyvar=study.studyvar
 endif else begin
  if plan_entered then begin
   if tag_exist(plan,'STUDY_ID') then study_id=plan.study_id
   if tag_exist(plan,'STUDYVAR') then studyvar=plan.studyvar
  endif
 endelse
endif

;-- bail out if inputs don't make sense

if (not exist(study_id)) or (not exist(studyvar)) then begin
 message,'No STUDY_ID/STUDYVAR associated with PLAN',/cont
 return
endif

;-- inputs should be ok by now

err=''
if not study_entered then $
 study=get_cds_study(study_id,studyvar,err=err,verbose=verbose)

if (err ne '') then return

;-- get ZONE_ID

zone_id=study.zone_id

;-- acronym

acronym=trim(study.obs_prog)

;-- check if engineering study

if study.n_raster_def eq 0 then begin      
 if verbose then message,mess1,/contin

;-- get engineering study duration 

 if tag_exist(study,'DURATION0') then begin
  sdur=study.duration0
  if sdur eq 0. then begin
   if tag_exist(study,'DURATION1') then begin
    sdur=study.duration1
   endif
  endif
 endif
 return
endif

;-- check for rasters

if not tag_exist(study,'RASTERS') then begin
 if verbose then message,mess2,/contin
 return
endif

;-- sum raster durations

eng=0 & err=''
rasters=study.rasters
if (study.study_id lt 0) or (study.studyvar lt 0) then return
get_raster_par,rasters,sdur=sdur,verbose=verbose,$
 slits=slits,rdur=rdur,dets=dets,err=err,reps=reps,telem=telem

;-- check for GIS detectors

if exist(dets) then begin
 chk=where(dets eq 'G',gcount)
 gis=(gcount gt 0)
endif
count=n_elements(rasters)

return & end

