function varOut = fort_read(fname,prec)
%varOut = fort_read(fname,prec)
%reads in the "fort" file where first two int32 are the dimensions, and the rest is a column major matrix of single precision
% fname -filename
% prec - output precision 'single' or 'double'
%

if nargin<2 
    prec='double';
end

fid         = fopen(fname,'r');
[mn,c]      = fread(fid,2,'int32');  %size m by n
;
if strcmp(prec,'single')
	varOut      = single(zeros(mn(1), mn(2)));
else
	varOut      = zeros(mn(1), mn(2));
end

for i = 1:mn(2)
	fseek(fid, 8+mn(1)*(i-1)*4, 'bof');
	if strcmp(prec,'single')
		[varOut(:,i),c]  = fread(fid,mn(1),'float32=>float32');
	else
		[varOut(:,i),c]  = fread(fid,mn(1),prec);
	end
end

fid         = fclose(fid);
