;+
; Project     : SOHO-CDS
;
; Name        : UPD_SCI_PLAN
;
; Purpose     : Update SCIENCE plan with DETAILS plan 
;
; Category    : planning
;
; Explanation : Use info from DETAILS plan to update SCI_PLAN
;
; Syntax      : upd_sci_plan,iap
;
; Examples    :
;
; Inputs      : DETAILS = DETAILS plan array
;               SCIENCE = SCIENCE array to update
;
; Opt. Inputs : None
;
; Outputs     : None
;
; Opt. Outputs: None
;
; Keywords    : None
;
; Common      : None
;
; Restrictions: None
;
; Side effects: Science plan database will be modified
;
; History     : Written 9 January 1997, D. Zarro, ARC/GSFC
;
; Contact     : dzarro@solar.stanford.edu
;-

pro upd_sci_plan,details,science,err=err

;-- convert to structure

err=''
if get_plan_type(details) ne 0 then begin
 err='Input plan not of type details'
 message,err,/cont
 return
endif


ndetails=n_elements(details)

;-- pull out engineering studies

tdetails=details
no_eng=where(tdetails.study_id gt 0,ndetails)
if ndetails gt 0 then tdetails=tdetails(no_eng)

progs=get_plan_prog(tdetails)
eng_progs=['eng-at','fullccd','nimcp','gimcp_1b','gimcp_2']
chk=where_vector(eng_progs,progs,rest=rest,rcount=rcount,/nosort,/trim)
if rcount gt 0 then begin
 tdetails=tdetails(rest)
 ndetails=rcount
endif

def_inst_plan,new_sci,type=2,rep=ndetails
type=new_sci.struct_type
det_plan=rep_tag_name(tdetails,'date_obs','start_time')
det_plan=rep_tag_name(det_plan,'date_end','end_time')
copy_struct,det_plan,new_sci
new_sci.struct_type=type
 
;-- pull out pointing

for i=0,ndetails-1 do begin
 det=tdetails(i)
 get_cds_xy,det,ins_x,ins_y,study=study,/one_dim
 get_cds_fov,det,width,height,study=study,/one_dim
 concat_pnt,ins_x,ins_y,width,height,/string
 temp_sci=new_sci(i)
 temp_sci.xcen=ins_x(0)
 temp_sci.ycen=ins_y(0)
 new_sci(i)=temp_sci
endfor

if exist(science) then begin
 tmax=str_format(max(new_sci.end_time))
 modify=where(str_format(science.start_time) le tmax,count)
 if count gt 0 then sci_mod=science(modify)
endif

mk_plan_write,new_sci,sci_mod,/retry,/noloop,/nocheck,err=err
 
if exist(science) then begin
 keep=where(str_format(science.start_time) gt tmax,count)
 if count gt 0 then science=concat_struct(sci_mod,science(keep)) else science=sci_mod
endif else science=sci_mod

return & end


