;+
; NAME:
;     SRSP_OPENARC
; PURPOSE:
;     Open the archive file of a given filename, and associate
;     the variable A with it, returning the logical unit number.
; CATEGORY:
;     SRSP APC FILE
; CALLING SEQUENCE:
;     lun = srsp_openarc(filename,a,nrec[,/update])
; INPUTS:
;     filename  The fully qualified name of the file to open
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     update    A switch to indicate that the file is to be opened
;                 for writing/updating.
; ROUTINES CALLED:
;     widget_message
; OUTPUTS:
;     lun       The logical unit number of the open file.  To
;                 close the file, use the procedure FREE_LUN,lun
;     a         The ASSOC variable associated with the file.
;     nrec      The number of records currently in the file (the
;                 file may grow later)
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 06-Jun-2001 by Dale E. Gary
;-

function srsp_openarc,filename,a,nrec,update=update

   ; Open file and associate variable A to it

   CATCH, err_stat
   if (err_stat eq 0) then begin
      if (keyword_set(update)) then openu,lun,/get_lun,filename $
                               else openr,lun,/get_lun,filename
      CATCH,/CANCEL
   endif else begin
      if (keyword_set(update)) then ans = widget_message('SRSP_OPENARC: Could not open file for writing.',/error) $
                               else ans = widget_message('SRSP_OPENARC: Could not open file for reading.',/error)
      CATCH,/CANCEL
      return, 0
   endelse

   a = assoc(lun,intarr(512))

   ; Calculate the number of records in the file from
   ; the file size (should be a multiple of 1024)
   fs = fstat(lun)
   nrec = fs.size/1024L

   ; If not a multiple of 1024, bail out
   if (nrec*1024 ne fs.size) then begin
      ans = widget_message(['OPENARC: File length not a multiple of 1024.',$
                            'File structure not consistent with SRSP .ARC file.'],$
                            /error)
;      free_lun,lun
;      return,0
   endif

return,lun
end