Home > custom > prevah > read_prevah_grid.m

read_prevah_grid

PURPOSE ^

PUPROSE: read binary data in the PREVAH hydrological model grid format

SYNOPSIS ^

function [data, header, stats] = read_prevah_grid(file)

DESCRIPTION ^

 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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data, header, stats] = read_prevah_grid(file)
0002 % PUPROSE: read binary data in the PREVAH hydrological model grid format
0003 % -------------------------------------------------------------------
0004 % USAGE: [data, header, stats] = read_prevah_grid(file)
0005 % Input:  file - filename and location
0006 % Output: data   - n*m matrix formated according to col and rows info in
0007 %                  header
0008 %         header - cell with number of cols, rows, x & y coordinates of
0009 %                  llc, cell size and nodata value
0010 %         stats  - statistics: no of valid cells, min, max, sum, mean, std
0011 %
0012 % EXAMPLE: [data, header, stats] = read_prevah_grid('C:\PREVAH\DEM300.dhm');
0013 %
0014 % Note: nodata values are replaced with NaN
0015 %
0016 % See also: write_prevah_grid, read_prevah_result, read_prevat_input
0017 %
0018 % Felix Hebeler, Geography Dept., University Zurich, Feb 2009
0019 
0020 % open file
0021 [fid,message] = fopen(file, 'r');
0022 if fid==-1
0023     error(message)
0024 end
0025 
0026 % read raw data
0027 h = fread(fid,[6,1],'float32');
0028 s = fread(fid,[6,1],'float32');
0029 data = fread(fid,inf,'float32');
0030 % format header
0031 header.cols=h(1);
0032 header.rows=h(2);
0033 header.xll=h(3);
0034 header.yll=h(4);
0035 header.cellsize=h(5);
0036 header.nodata=h(6);
0037 clear h;
0038 
0039 %format stats
0040 stats.count=s(1);
0041 stats.min=s(2);
0042 stats.max=s(3);
0043 stats.sum=s(4);
0044 stats.mean=s(5);
0045 stats.std=s(6);
0046 clear s;
0047 
0048 % format data
0049 data=reshape(data,header.cols,header.rows); % reshape to matrix
0050 data(data==header.nodata)=nan; % set nodata to NaN
0051 data=flipud(data); % align: flip and rotate
0052 data=rot90(data,-1);
0053 
0054 % close file
0055 status = fclose(fid);
0056 if status==-1
0057     error('Unable to successfully close input binary file')
0058 end

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003