;+
; Project     :	SOHO - CDS
;
; Name        :	EMERGENCY_STUDY()
;
; Purpose     :	Gets emergency study information.
;
; Explanation :	The emergency study is a study which will run after the
;               last study in the daily plan if no cancellation command is received from
;               the ground. It is intended to carry on providing science if commanding to
;               the CDS is lost for some reason.
;               The emergency study ID and variation is kept in the CDHS state database.
;               The study will start running one hour into the next day or one hour after
;               the last study whichever is the later.
;               The study must have fixed pointing.
;
; Use         : < study = emergency_study ( last_study ) >
;
; Inputs      : last_study      = structure containing details of the last study of the daily plan;
;               
; Opt. Inputs : None.
;
; Outputs     : study : structure containing details of the emergency study.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	cp_get_entry, tai2utc, utc2tai, anytim2tai.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	Version 0.0, Martin Carter, RAL, 11/8/95
;
; Modified    :	Version 0.1, MKC
;                            Used cp_get_entry.
;               Version 0.2, MKC
;                            Corrected bug in calculating the time of the following study.
;               Version 0.3, MKC, 28/5/97
;                            Added database read to get studt details.
;
; Version     :	Version 0.3, 28/5/97
;-
;**********************************************************

FUNCTION emergency_study, sciplan_details

  ; get emergency study ID and variation

  ss = cp_get_entry ( 'XSTUDY', [0,1] )

  ; get time to run emergency study

  time_last_study = sciplan_details.date_end

  utc = tai2utc ( time_last_study )

  utc.mjd  = utc.mjd + 1       ; following day
  utc.time = 3600*1000L        ; one hour in ms

  tai = utc2tai ( utc )        ; get TAI time of one hour into next day

  ; select later time

  IF tai GT time_last_study+3600 THEN time_start = tai $
                                 ELSE time_start = time_last_study+3600

  ; make details

  details = sciplan_details
  
  ; set up tag values

  details.prog_id     = 0
  details.study_id    = ss(0).active
  details.studyvar    = ss(1).active
  details.sci_obj     = 'Emergency study'
  details.sci_spec    = 'Emergency study'
  details.cmp_no      = 0
  details.object      = ''
  details.obj_id      = 0
  details.date_obs    = time_start 
  details.date_end    = anytim2tai('2000-1-1')
  details.orig_dur    = details.date_end-details.date_obs
  details.n_rasters1  = 1
  details.time_tagged = 1
  details.tracking    = 0
  details.n_pointings = 0  ; pointing is fixed within study
  details.n_repeat_s  = 1
  details.flag_master = 0
  details.gset_id     = -1
  details.get_raw     = 0

  RETURN, details  

END
