;+
; Project     :	SOLAR-B - EIS
;
; Name        :	EIS_CPT_OPEN_FILE
;
; Purpose     :	Opens file.
;
; Explanation :	If open fails then returns 0 else returns logical unit.
;               NB 0 is not a valid logical unit
;
; Use         :	< unit = eis_cpt_open_file(filename) >
;
; Inputs      :	filename : STRING name of file, including directory.
;
; Opt. Inputs : None.
;
; Outputs     : unit   : LONG logical unit of opened file.
;
; Opt. Outputs:	None.
;
; Keywords    : READ   : FLAG indicating open file for reading.
;               WRITE  : FLAG indicating open file for writing.
;
; Calls       : None.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	EIS_CPT.
;
; Prev. Hist. :	None.
;
; Written     :	Martin Carter RAL 15/10/04
;
; Modified    :	Version 0.0, 15/10/04, MKC
;
; Version     :	Version 0.0, 15/10/04
;-
;**********************************************************

FUNCTION eis_cpt_open_file, filename, READ=read, WRITE=write

  ; set io error action

  ON_IOERROR, io_error

  ; open file

  IF KEYWORD_SET(read) THEN BEGIN

    ; check if file exists

    IF NOT eis_cpt_check_file(filename, READ=read, WRITE=write) THEN GOTO, error

    OPENR, unit, filename, /GET_LUN

  ENDIF ELSE BEGIN

    OPENW, unit, filename, /GET_LUN

  ENDELSE

  ; return logical unit

  RETURN, unit

error :

  RETURN, 0

io_error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error

  eis_cpt_message, 'IO ERROR opening  :'+filename

  RETURN, 0

END





