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.

write_prevah_grid(file, data, header, stats)
function write_prevah_grid(file, data, header, stats)
% PURPOSE: export data to the PREVAH hydrological model binary grid format
% -------------------------------------------------------------------
% USAGE:  write_prevah(file, data, header, stats)
% INPUT:  file   - filename and location as string (file will be created or
%                  overwritten!)
%         data   - n*m matrix formated according to col and rows info in
%                  header
%         header - cell with number of cols, rows, coordinates of llc, cell
%                  size and nodata value as given by read_prevah
%         stats  - statistics: cell with no of valid cells, min, max, sum,
%                  mean, std
%
% Example: write_prevah('C:\test.bin', peaks(20), header, stats)
% where:
% header.cols=20;
% header.rows=20;
% header.xll=1;
% header.yll=1;
% header.cellsize=10;
% header.nodata=-9999;
% stats.count=400;
% stats.min=-6.3605;
% stats.max=7.5561;
% stats.sum=131.1184;
% stats.mean=0.3278;
% stats.std=1.8167;
%
% Note: NaNs are replaced with the header nodata value
% 
% See also: read_prevah_grid, read_prevah_result, read_prevat_input
%
% Felix Hebeler, Geography Dept., University Zurich, Feb 2009

% create new file
[fid,message] = fopen(file, 'w');
if fid==-1
    error(message)
end

% format header
h(1)=header.cols;
h(2)=header.rows;
h(3)=header.xll;
h(4)=header.yll;
h(5)=header.cellsize;
h(6)=header.nodata;

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

% format data
data(isnan(data))=header.nodata; % set NaN to NoData value
data=flipud(data); % align: flip and rotate
data=rot90(data,-1);


% write data
count = fwrite(fid,h,'float32');
if count~=6
    error('Error writing header (header rows 1-6)')
end
count = fwrite(fid,s,'float32');
if count~=6
    error('Error writing stats (header rows 7-12)')
end
count = fwrite(fid,data,'float32');
if count~=header.rows*header.cols
    error(['Error writing data: ',num2str(count),' of ',num2str(header.rows*header.cols), 'records written'])
end

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

Contact us at files@mathworks.com