;+
; Project     : SOHO - CDS     
;                   
; Name        : UPDATE_STUDY_DUR
;               
; Purpose     : Updates all the study durations in the database.
;               
; Explanation : The calculation of raster durations may change as properties
;               of the CDS are further understood.  This routine allows a 
;               global update of the study duration parameters
;               
; Use         : IDL> update_study_dur [, /quiet]
;    
; Inputs      : None
;               
; Opt. Inputs : STUDY_ID = study ID of specific study to update
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : QUIET   - if set no details of the changes are output to the 
;                         screen
;               ERR     - error strings
;
; Calls       : Database access routines
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Database
;               
; Prev. Hist. : None
;
; Written     : D. M. Zarro, ARC/GSFC, 3-Mar-96
;               
; Modified    : D. M. Zarro, ARC/GSFC, 15-May-96 
;                -- added single study option
;                -- added TITLE_ID search
;
; Version     : Version 2
;-            

  pro update_study_dur,study_id,quiet=quiet,err=err

  on_error,1

  single_study=exist(study_id)

;
;  get a list of all fundamental studies
;

  if not single_study then list_f_study, fs, n else begin
   get_f_study,study_id,fs,err=err
   if fs.study_id eq -1 then return
   n=1
  endelse

;
;  for each one pick up the variations, calculate the new duration and apply it
;

  for i=0,n-1 do begin
   study_id=fs(i).study_id
   title_id=fs(i).title_id
   list_v_study,title_id,sv,nv,/use_title 

   for k=0,nv-1 do begin

    studyvar=sv(k).studyvar

;-- if SV.N_RASTER_DEF EQ 0 then ignore it, since it is an engineering study

    slabel='Study '+fmt_vect([study_id,studyvar],delim='/')

    ok_to_mod=0
    if sv(k).n_raster_def gt 0 then begin
     err=''
     get_study,study_id,studyvar,study,err=err
     if err eq '' then begin
      if tag_exist(study,'RASTERS') then begin
       rasters=study.rasters
       err=''
       get_raster_par,rasters,rdur=rdur,sdur=sdur,err=err,reps=reps
       if err eq '' then begin
        ok_to_mod=1
        nrast=n_elements(rasters)
        last_rep=reps(nrast-1)
        old_dur0 = study.duration0
        old_dur1 = study.duration1
        if nrast gt 1 then begin
         study.duration0=total(rdur(0:nrast-2))
         study.duration1=rdur(nrast-1)/last_rep
        endif else begin
         study.duration0=0
         study.duration1=rdur(0)/last_rep
        endelse
       endif else message,err,/cont
      endif else message,'no rasters for '+slabel,/cont
     endif else message,err,/cont
    endif else message,'skipping engineering '+slabel,/cont

;-- modify study here

    if ok_to_mod then begin
     dprint,'i,k',i,k
     do_mod=(old_dur0 ne study.duration0) or (old_dur1 ne study.duration1)
     if do_mod then begin
      ok = mod_v_study(study_id,studyvar,$
            duration0=study.duration0,duration1=study.duration1)
      if not ok then begin
       bell
       print,'Error updating study database'
       return
      endif

      get_study,study.study_id,study.studyvar,mod_study
      text= ''
      if (mod_study.duration0 eq study.duration0) and $
         (mod_study.duration1 eq study.duration1) then $
       text = '    -- Confirmed' else text = '    -- Error'

      if not keyword_set(quiet) then begin
       print,''
       print,'Study '+fmt_vect([study.study_id,study.studyvar],delim='/')
       print,'DURATION0 changed from ',old_dur0,' to ',mod_study.duration0,text,$
        form='(a,f7.1,a4,f7.1,a)'
       print,'DURATION1 changed from ',old_dur1,' to ',mod_study.duration1,text,$
        form='(a,f7.1,a4,f7.1,a)'
      endif
     endif
    endif
   endfor
  endfor

  return & end
