;	(25-mar-91)
FUNCTION RDFILE, NAME,N_COLUMN,N_SKIP,N_READ,DOUBLE=DP,HEADER=HDR
					; Read ASCII file N_COLUMN columns
;+
; NAME:
;	RDFILE
; PURPOSE:
;	Open and read an ASCII file. Read N_column columns 
;	and skip first N_SKIP records. 
;
; CALLING SEQUENCE:
;	Data = RDFILE(0)		; Will prompt for file name
;	Data = RDFILE(NAME)		; Will prompt for N_COLUMN,N_SKIP
;	Data = RDFILE(NAME,N_COLUMN)	; No prompts, no records skipped
;	Data = RDFILE(NAME,N_COLUMN,N_SKIP)
;	Data = RDFILE(NAME,N_COLUMN,N_SKIP,N_READ) ;All records are read if
;						   ; N_READ is omitted
; INPUTS:
;	No required inputs
; OPTIONAL INPUT PARAMETERS:
;	NAME	  String variable containing input file name.
;	N_COLUMN  Number of columns to read.
;	N_SKIP	  Number of records at beginning of file to skip.
;	N_READ	  Number of records to read after any skipped records.
;
; KEYWORDS:
;	DOUBLE	  If true, i.e. 1, then data is read in double precision.
;	HEADER	  If present, returns string array with N_SKIP records.
;
; OUTPUTS:
;	Two dimensional data array returned: DATA(i,j)
;		where 	i = 0:N_column - 1
;			j = 0:Number of records - 1
;
; COMMON BLOCKS:
;	None.
;
; SIDE EFFECTS:
;	None.
; RESTRICTIONS:
;	NAME	must be a scalar string variable.
;	N_COLUMN, N_SKIP, and N_READ must be scalar byte, integer or real.
;
; PROCEDURE:
;
;	If no arguments are present, the routine will prompt for
;		NAME (filename), N_COLUMN and N_SKIP.
;	If only the file name is present, routine will prompt for 
;		N_COLUMN and N_SKIP.
;	If NAME and N_COLUMN are present, but not N_SKIP, N_SKIP will
;		be set to 0.
;	If N_READ is present, then N_READ records will be read in, otherwise
;		all records will be read.  (N_READ does not include skipped
;               records.)  
;	If keyword DOUBLE is true, i.e. 1, then data is read in double
;		precision.  Syntax in procedure call is ,/DOUBLE or ,DOUBLE=1 .
;	If keyword HEADER is present, will return N_SKIP header lines.
;
; MODIFICATION HISTORY:
;	Sep, 1985,	Written, J.R. Lemen, MSSL
;	Dec, 1990,	Modified, J.R. Lemen, LPARL, to make compatible with V2.
;	Feb, 1991,	Modified, E.S. Claflin, LPARL, N_READ parameter and
;			  DOUBLE keyword added.
; 	Mar, 1991,	Modified, J.R. Lemen, LPARL, to add HEADER keyword
;-
num_parm = N_params(0)		; Get the number of parameters

;--	If no arguments, get filename from the keyboard:

if N_elements(NAME) eq 0 then begin
  NAME = ''
  read,'* Enter the file name: ',NAME
endif

;--	Open the file

get_lun,lun & openr,lun,NAME

;--	If only 1 argument, get N_COLUMN and N_SKIP

if num_parm le 1 then begin
   print,'$(a)', '$* Enter the number of columns to read: '
   N_COLUMN = 0
   read,N_COLUMN

   print,'$(a)', '$* Enter the number of records to skip: '
   N_SKIP = 0
   read,N_SKIP
endif

if num_parm eq 2 then N_SKIP = 0	; Only NAME and N_COLUMN entered.

;--	Read N_SKIP header records

buf = ''				; String to read headers
Hdr = ''
if N_SKIP gt 0 then begin
   for i =1,N_SKIP do begin
      readf,lun,buf
      Hdr = [Hdr,buf]
   endfor
   Hdr = Hdr(1:*)
endif

  ;--	Set up data array

if keyword_set(DP) then begin

  DATA = dblarr(N_COLUMN,4000)		; 2000 is arbitrary
  colm = dblarr(N_COLUMN)

endif else begin

  DATA = fltarr(N_COLUMN,4000)		; 2000 is arbitrary
  colm = fltarr(N_COLUMN)

endelse

;--	Read the data file

if num_parm eq 4 then begin

  for k=0,n_read-1 do begin
    readf,lun,colm
    for i=0,N_COLUMN-1 do DATA(i,k) = colm(i)
  endfor
  k = n_read - 1

endif else begin

  k = -1					; Loop counter
  while not eof(lun) do begin
    k = k + 1
    readf,lun,colm
    for i=0,N_COLUMN-1 do DATA(i,k) = colm(i)
  endwhile

endelse

;--	Close the input file
close,lun  &  free_lun,lun

print,'$(/i4,a)',k+1,' data rows read'

return,DATA(*,0:k)			; Return the data file
end
