; $Id: rmfa.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
; $Id: rmfa.pro,v 1.0 1993/10/15

;+
; NAME:
;       RMFA
; PURPOSE:
;       Perform formatted input of datafiles of unspecified length
;       from the specified file.
; CATEGORY:
;       Linear Algebra
; CALLING SEQUENCE:
;       RMFA, UNIT, A [, Columns]
; INPUTS:
;       Unit - The input file unit.
;       E1, ... E10 - Expressions to be output. These can be scalar or
;               array and of any type.
; OUTPUTS:
;       Input is written to the file specified by unit.
; COMMON BLOCKS:
;       None.
; MODIFICATION HISTORY:
;       15 October 1993, Written by Thomas Petersen University of Toronto
;-

pro RMFA, unit, A, columns,  double=dbl, complex=cmplx, format = format

on_error, 2             ; Return to caller on error

n = n_params()
if (n ne 2) and (n ne 3) then message, 'Wrong number of arguments."

;       If Rows were not given, then make sure a is
;       defined.
s = size(a)
if ((n eq 2) and (s(0) eq 0))  then message, 'Argument must be defined as an array if Rows are not supplied.'
;
;       Only read in 1-D and 2-D arrays.
if ((n eq 2) and(s(0) gt 2)) then message, 'Array has too many dimensions.'

if (n eq 2) then begin
    a = transpose(a)
    s = size(a)
    l_columns = s(1)
    if (s(0) eq 1) then l_rows=1 else l_rows=s(2)
    type = s(s(0)+1)
endif else begin
    l_rows = 10000
    l_columns = columns
    dbl = keyword_set(dbl)
    cmplx = keyword_set(cmplx)
    a = make_array(l_columns, l_rows, double= keyword_set(dbl), $
                   complex=keyword_set(cmplx))
endelse

index = 0
temp = make_array(1,columns)
while not EOF(unit) do begin
        if keyword_set(format) then readf, unit, temp, format = format $
        else readf, unit, temp
        a(*,index) = temp
        if index lt 9999 then index=index+1 else stop, 'Too long file (more than 10000 rows)'
endwhile

a = transpose(a)

a = a(0:index-1,0:l_columns-1)

end
