No BSD License  

Highlights from
PREVAH data import/export scripts

from PREVAH data import/export scripts by Felix Hebeler
IO scripts to im- and export data from the PREVAH hydrological model to Matlab.

read_prevah_grid(file)
function [data, header, stats] = read_prevah_grid(file)
% PUPROSE: read binary data in the PREVAH hydrological model grid format
% -------------------------------------------------------------------
% USAGE: [data, header, stats] = read_prevah_grid(file)
% Input:  file - filename and location
% Output: data   - n*m matrix formated according to col and rows info in
%                  header
%         header - cell with number of cols, rows, x & y coordinates of
%                  llc, cell size and nodata value
%         stats  - statistics: no of valid cells, min, max, sum, mean, std
%
% EXAMPLE: [data, header, stats] = read_prevah_grid('C:\PREVAH\DEM300.dhm');
%
% Note: nodata values are replaced with NaN
% 
% See also: write_prevah_grid, read_prevah_result, read_prevat_input
%
% Felix Hebeler, Geography Dept., University Zurich, Feb 2009

% open file
[fid,message] = fopen(file, 'r');
if fid==-1
    error(message)
end

% read raw data
h = fread(fid,[6,1],'float32');
s = fread(fid,[6,1],'float32');
data = fread(fid,inf,'float32');
% format header
header.cols=h(1);
header.rows=h(2);
header.xll=h(3);
header.yll=h(4);
header.cellsize=h(5);
header.nodata=h(6);
clear h;

%format stats
stats.count=s(1);
stats.min=s(2);
stats.max=s(3);
stats.sum=s(4);
stats.mean=s(5);
stats.std=s(6);
clear s;

% format data
data=reshape(data,header.cols,header.rows); % reshape to matrix
data(data==header.nodata)=nan; % set nodata to NaN
data=flipud(data); % align: flip and rotate
data=rot90(data,-1);

% close file
status = fclose(fid);
if status==-1
    error('Unable to successfully close input binary file')
end

Contact us at files@mathworks.com