;+
; Project     : SOHO - CDS
;
; Name        : GET_CDS_FOV
;
; Purpose     : get CDS FOV size (width, height) for CDS rasters in a plan
;
; Category    : planning
;
; Explanation : 
;
; Syntax      : IDL> get_cds_fov,plan,width,height
;
; Inputs      : PLAN = plan structure
;
; Opt. Inputs : None
;
; Outputs     : X, Y = vector of width and heights in arcsecs
;
; Opt. Outputs: None
;
; Keywords    : FIRST = set to just return fov of first raster in plan
;               LAST = set to just return fov of last raster in plan
;               STUDY = study structure associated with PLAN (optional)
;               STRING_FORM = output FOV as strings
;               ONE_DIM = force output vectors to be 1-d
;               ERR = error string
;
; Common      : None
;
; Restrictions: 
;
; Side effects: None
;
; History     : Version 1,  29-Aug-1997,  D.M. Zarro.  Written
;
; Contact     : DZARRO@SOLAR.STANFORD.EDU
;-

pro get_cds_fov,plan,width,height,first=first,last=last,study=study,$
                string_form=string_form,err=err,one_dim=one_dim

on_error,1

width=0. & height=0. & err=''

type=get_plan_type(plan)
if (type lt 0) then begin
 pr_syntax,'GET_CDS_FOV,PLAN,WIDTH,HEIGHT'
 return
endif

;-- check for valid study tags

if (not tag_exist(plan,'STUDY_ID')) or $
   (not tag_exist(plan,'STUDYVAR')) then begin
 message,'No STUDY_ID/STUDYVAR associated with PLAN',/cont
 return
endif

;-- check for rasters

read_study=1
if datatype(study) eq 'STC' then $
 read_study=(study.study_id ne plan.study_id) or (study.studyvar ne plan.studyvar)
if read_study then study=get_cds_study(plan.study_id,plan.studyvar,err=err)
if err ne '' then return

;-- if no rasters, then assume an engineering study and bail out

if (not tag_exist(study,'RASTERS')) or (study.n_raster_def eq 0) then goto,done

rasters=study.rasters
npoints=plan.n_pointings
npoints= npoints > 1
nrast=n_elements(rasters)

width=fltarr(npoints,nrast) & height=width

for i=0,nrast-1 do begin
 err=''
 r=get_cds_raster(rasters(i).ras_id,rasters(i).ras_var,err=err)
 if err eq '' then begin
  raster_size,r,width=w,height=h
  height(*,i)=h
  width(*,i)=w
 endif
 quit=(keyword_set(first) and (i eq 0)) or $
    (keyword_set(last) and (i eq (nrast-1)))
 if quit then begin
  width=width(0,i)
  height=height(0,i)
  goto,done
 endif
endfor

done:

if keyword_set(string_form) or keyword_set(one_dim) then begin
 sx=size(width)
 if sx(0) eq 2 then width=reform(width,sx(1)*sx(2))
 sy=size(height)
 if sy(0) eq 2 then height=reform(height,sy(1)*sy(2))
endif

if keyword_set(string_form) then begin
 width=arr2str(trim(str_format(width,'(i6)')),delim=',')
 height=arr2str(trim(str_format(height,'(i6)')),delim=',')
endif else begin
 if n_elements(width) eq 1 then width=width(0)
 if n_elements(height) eq 1 then height=height(0)
endelse

return & end

