LISFLOOD-FP and MATLAB

From SourceWiki
Revision as of 15:13, 18 March 2008 by Jeff-neal (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page contains various Matlab functions that may be useful for analysing results from the LISFLOOD-FP model.

File import and export

Importing ascii raster files

This function will import an ascii raster file using the string filename. To use it, copy and past text into a textfile called ascii_reader.m

function [dem, ncols, nrows, xllcorner, yllcorner, cellsize] = ascii_reader (filename) 

% [DEM, ncols, nrows, xllcorner, yllcorner, cellsize] = ascii_reader (filename) 

% This function reads arc .asc files
% requires filename string as input

% j neal
% 18/6/07
%% read an ascii file
fin = fopen(filename,'r');
A = fscanf(fin,'%s',1); ncols = fscanf(fin,'%f',1);           %#ok<NASGU>
A = fscanf(fin,'%s',1); nrows = fscanf(fin,'%f',1);           %#ok<NASGU>
A = fscanf(fin,'%s',1); xllcorner = fscanf(fin,'%f',1);       %#ok<NASGU>
A = fscanf(fin,'%s',1); yllcorner = fscanf(fin,'%f',1);       %#ok<NASGU>
A = fscanf(fin,'%s',1); cellsize = fscanf(fin,'%f',1);        %#ok<NASGU>
A = fscanf(fin,'%s',1); nodata = fscanf(fin,'%f',1);          %#ok<NASGU>
dem = fscanf(fin,'%f',[ncols, nrows]);
dem = dem';
fclose('all');

You can view the imported file using the command:

imagesc(dem);

Exporting ascii raster files

This function will create an ascii raster file called filename from the 2D matrix DEM. To use it, copy and past text into a textfile called ascii_write.m

function ascii_write (filename, DEM, xllcorner, yllcorner, cellsize, nodata)

% ascii_write (filename, DEM, xllcorner, yllcorner, cellsize, nodata)
% only filename (string) and DEM (2D matrix) are essential
% this function writes ascii raster files for arc
% j neal
% 6/3/2008

if nargin < 2, 
    error('Requires at least two input arguments'); 
end
if nargin < 4, 
    xllcorner = 0;
    yllcorner = 0;
end
if nargin < 5,
    cellsize = 1;
end
if nargin < 6
    nodata = -9999;
end
A = size(DEM);
fout = fopen (filename,'w');
fprintf(fout, 'ncols         %.0f\n', A(2));  
fprintf(fout, 'nrows         %.0f\n', A(1));
fprintf(fout, 'xllcorner     %f\n', xllcorner);
fprintf(fout, 'yllcorner     %f\n', yllcorner);
fprintf(fout, 'cellsize      %f\n', cellsize);
fprintf(fout, 'NODATA_value  %f\n', nodata); 
for ii = 1:A(1)
    B = DEM(ii,:);
    fprintf(fout, '%1.3f ', B);
    fprintf(fout, '\n');
end
fclose('all');