function [cookie, m, vals] = readPETScVecBinFile(infilename)
% function [VEC_FILE_COOKIE, m, vals] = readPETScVecBinFile(infilename)
% read Petsc Binary format vector (no support for imaginary numbers)

% VECTOR: 
%   int    VEC_FILE_COOKIE
%   int    number of rows
%   PetscScalar *values of all nonzeros

VEC_FILE_COOKIE = 1211214;

fid = fopen(infilename,'r', 'ieee-be');

cookie = fread(fid, 1, 'int32');
if (cookie ~= VEC_FILE_COOKIE)
    error('Not a petsc vector file.');
end

m = fread(fid, 1, 'int32');
vals = fread(fid, m, 'double');

fclose(fid);

return