


PUPROSE: read input tabular data for PREVAH hydrological model
-------------------------------------------------------------------
USAGE: [data, header] = read_prevah_input(file)
Input: file - filename and location
nodata - nodata value (optional, if given value will be replaced
with NaN)
Output: R: x*n column, where n is the number of timesteps and x are
the number of input columns, according to specification
in the PREVAH manual, part III)
YYYY MM DD HH ... ... ...
header - 4*1 cell: elevation, x and y coordinate, name
EXAMPLE: [data, header] = read_prevah_input('rain2003.tab')
See also: write_prevah_grid, read_prevat_grid, read_prevah_result
Felix Hebeler, Geography Dept., University Zurich, Feb 2009

0001 function [R, header] = read_prevah_input(file,nodata) 0002 % PUPROSE: read input tabular data for PREVAH hydrological model 0003 % ------------------------------------------------------------------- 0004 % USAGE: [data, header] = read_prevah_input(file) 0005 % Input: file - filename and location 0006 % nodata - nodata value (optional, if given value will be replaced 0007 % with NaN) 0008 % Output: R: x*n column, where n is the number of timesteps and x are 0009 % the number of input columns, according to specification 0010 % in the PREVAH manual, part III) 0011 % YYYY MM DD HH ... ... ... 0012 % header - 4*1 cell: elevation, x and y coordinate, name 0013 % 0014 % EXAMPLE: [data, header] = read_prevah_input('rain2003.tab') 0015 % 0016 % See also: write_prevah_grid, read_prevat_grid, read_prevah_result 0017 % 0018 % Felix Hebeler, Geography Dept., University Zurich, Feb 2009 0019 0020 % 0021 data = nan(1,4); 0022 0023 %open file 0024 fid = fopen(file,'r'); 0025 0026 % read header 0027 % read first line and ignore 0028 title = fgetl(fid); 0029 % read second line 0030 remain = fgetl(fid); 0031 col=1; 0032 while true % loop once over elevation to find number of columns 0033 [str, remain] = strtok(remain); 0034 if isempty(str), break; end 0035 header{1,col}=str; 0036 col=col+1; 0037 end 0038 col=col-1; 0039 remain = fgetl(fid); 0040 for i=1:col %loop again for xcoords in 3rd row 0041 [str, remain] = strtok(remain); 0042 if isempty(str), break; end 0043 header{2,i}=str; 0044 end 0045 remain = fgetl(fid); 0046 for i=1:col %loop 3rd time for ycoords in 3rd row 0047 [str, remain] = strtok(remain); 0048 if isempty(str), break; end 0049 header{3,i}=str; 0050 end 0051 remain = fgetl(fid); 0052 for i=1:col %loop 4th time for name in 4th row 0053 [str, remain] = strtok(remain); 0054 if isempty(str), break; end 0055 header{4,i}=str; 0056 end 0057 0058 % construct format string for data 0059 format = '%d %d %d'; % first 3 columns are read in as integers (YY MM DD ) 0060 for i=4:col 0061 format = [format,' %n']; % add a %n to read in double for each data column 0062 end 0063 data = textscan(fid,format); 0064 0065 %intialise result array 0066 l=length(cell2mat(data(1,col))); % get length of last column, this is used because result files can contain gibberish from some point on, 0067 % which will not be read, but the column length (no of timesteps) will not 0068 % be correct anymore for the reformating 0069 R=nan(l,col); 0070 0071 % reformat to column vector 0072 for i= 1:col 0073 R(:,i)=data{1,i}(1:l); % l (normally, l equals end) 0074 end 0075 clear data; 0076 % check if nodata values should be replaced 0077 if exist('nodata','var') 0078 R(R==nodata)=NaN; 0079 end 0080 %close file 0081 fclose(fid);