;+
; Project     :	SOHO - CDS
;
; Name        :	GET_OPS_DELAY
;
; Purpose     :	Calculates the delay in millisecond units for an OPS movement
;
; Explanation :	Uses delay = constant + rate*no. of steps
;
; Use         : <delay = get_ops_delay( ops_start, ops_end, direction, ERRMSG=errmsg)>
;
; Inputs      : ops_start = OPS start position;
;               ops_end   = OPS end position.
;               direction = left or right OPS
;                             0   = left
;                             else  right
;
; Opt. Inputs : None.
;
; Outputs     : delay = delay in ms units;
;
; Opt. Outputs:	None.
;
; Keywords    : 
;       ERRMSG    = If defined and passed, then any error messages will be
;                   returned to the user in this parameter rather than
;                   depending on the MESSAGE routine in IDL.  If no errors are
;                   encountered, then a null string is returned.  In order to
;                   use this feature, ERRMSG must be defined first, e.g.
;
;                       ERRMSG = ''
;                       GET_OPS_DELAY, ERRMSG=ERRMSG, ... 
;                       IF ERRMSG NE '' THEN ...
;
; Calls       :	cp_get_entry.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	Version 0.0, Martin Carter, RAL, 6/3/95
;
; Modified    :	Version 0.1, MKC, 21/4/95
;                            Removed maximum flag.
;               Version 0.2, MKC, 24/5/95
;                            Changed definition of valid ops.
;                            Changed output to LONG.
;               Version 0.3, MKC, 11/8/95
;                            Added CDHS state database values for constants used.
;               Version 0.4, MKC, 14/8/95
;                            Changed to left and right specific.
;		Version 0.5, MKC, 28/9/95
;			Added keyword ERRMSG
;               Version 0.6, MKC, 7/11/95
;                 Modified to use cp_get_entry.
;               Version 0.7  CDP, Modified to use common block. 2-May-96
;
;
; Version     :	Version 0.7, 2/5/96
;-
;**********************************************************

FUNCTION get_ops_delay, ops_start, ops_end, direction, ERRMSG=ERRMSG

;
;  common block for speed
;
common ops_delay,ss,ss1

  ; get ops delays

  if n_elements(ss) eq 0 then begin
     ss = cp_get_entry ( 'CB2LAT', [15,16], ERRMSG=ERRMSG )

  ; check values ok

    IF N_ELEMENTS(ERRMSG) NE 0 THEN IF ERRMSG NE '' THEN GOTO, HANDLE_ERROR

  endif

  ; extract values

  const = ss(0).active  ; ops const delay
  rate  = ss(1).active  ; ops rate  delay

  ; get structure elements corresponding to ops home position

  IF N_ELEMENTS(direction) EQ 0 THEN direction = 0
  IF direction EQ 0 THEN index = [2] ELSE index = [3]

  IF N_ELEMENTS(ss1) EQ 0 THEN BEGIN
     ss1 = cp_get_entry ( 'CB2POSN', index, ERRMSG=ERRMSG  )

  ; check values ok

     IF N_ELEMENTS(ERRMSG) NE 0 THEN IF ERRMSG NE '' THEN GOTO, HANDLE_ERROR
  ENDIF


  ; constants

  ops_home  = ss1(0).active ; ops home position

  ; work out delay
  ; assume in home position if not defined

  IF ops_start GE 0 AND ops_start LE 4095 THEN $
    no_of_steps = ABS(ops_end - ops_start) ELSE $
    no_of_steps = ABS(ops_end - ops_home )

  delay = const + rate*no_of_steps

  ; return delay in ms units

  RETURN, delay

  ;
  ;  Error handling point.
  ;
  HANDLE_ERROR:

  ERRMSG = 'GET_OPS_DELAY: ' + ERRMSG 

  RETURN, -1

END
