;+
; Project     :	SOHO - CDS
;
; Name        :	MK_CDS_STUDY
;
; Purpose     : create studies by appending rasters to fundamental part
;
; Use         : STUDY=MK_CDS_STUDY(FSTUDY,RASTERS)
;
; Inputs      : FSTUDY = fundamental study definition.
;               RASTERS = raster definition.
;
; Opt. Inputs : None.
;
; Outputs     : STUDY = created study
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Explanation :
;
;                STUDY.N_RASTERS = N_RASTERS0 + N_RASTERS1
;                STUDY.DURATION  = DURATION0  + N_RASTERS1 * DURATION1
; Calls       : 
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : Planning
;
; Prev. Hist. : None.
;
; Written     :	Version 1, Zarro (ARC/GSFC) 31 October 1994
;-

function mk_cds_study,fstudy,rasters

on_error,1

if datatype(fstudy) ne 'STC' then begin
 message,'invalid study input',/cont
 return,0
endif

;-- remove old rasters and add new ones

nrast=n_elements(rasters)
study=fstudy
if nrast gt 0 then begin
 study=rem_tag(study,'RASTERS')
 study=add_tag(study,rasters,'RASTERS')
endif else begin
 study.n_rasters0=0
; study.usable='N'
endelse

study.n_raster_def=nrast
study.duration0=0
study.duration1=0
study.study_id=-1
study.title_id=-1
study.studyvar=-1
study.obs_prog=strupcase(strmid(strtrim(study.obs_prog,2),0,8))
study.title=strupcase(strmid(strtrim(study.title,2),0,80))
study.sv_desc=strupcase(strmid(strtrim(study.sv_desc,2),0,65))
if strtrim(study.sv_desc) eq '' then study.sv_desc='Variation 1'
study.zone_id=(study.zone_id > 1)

if not tag_exist(study,'RASTERS') then return,study

;-- reconcile study durations

rasters=study.rasters
err=''
get_raster_par,rasters,rdur=rdur,sdur=sdur,err=err,reps=reps

if (sdur eq 0.) then return,study    ;-- not much else to do

nrast=n_elements(rasters)
last_rep=reps(nrast-1)
if nrast gt 1 then begin
 study.n_rasters0=total(rasters(0:nrast-2).n_repeat_r)
 study.duration0=total(rdur(0:nrast-2))
 study.duration1=rdur(nrast-1)/last_rep
endif else begin
 study.n_rasters0=0
 study.duration0=0
 study.duration1=rdur(0)/last_rep
endelse

;-- reconcile pointing

pointing=rasters.pointing
chk=where(pointing eq 1,cnt)
if (cnt ne 0) then study.var_point='Y' else study.var_point='N'

return,study & end
