Home > custom > prevah > read_prevah_result.m

read_prevah_result

PURPOSE ^

PUPROSE: read result tabular data (txt) from PREVAH hydrological model

SYNOPSIS ^

function [R, header] = read_prevah_result(file,nodata)

DESCRIPTION ^

 PUPROSE: read result tabular data (txt) from PREVAH hydrological model
 -------------------------------------------------------------------
 USAGE: [R, header] = read_prevah_result(file,nodata)
 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 result columns, according to specification
               in the PREVAH manual, part III, p73)
               YYYY MM DD HH ... ... ...

 EXAMPLE: [R, header] = read_prevah_result(sample.q,9999);

 Note: at the moment, only *.q, *.mit, *.std and *pri files are explicitly
       supported (e.g. with header column information)
 
 See also: read_prevah_grid, write_prevah_grid, 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 [R, header] = read_prevah_result(file,nodata)
0002 % PUPROSE: read result tabular data (txt) from PREVAH hydrological model
0003 % -------------------------------------------------------------------
0004 % USAGE: [R, header] = read_prevah_result(file,nodata)
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 result columns, according to specification
0010 %               in the PREVAH manual, part III, p73)
0011 %               YYYY MM DD HH ... ... ...
0012 %
0013 % EXAMPLE: [R, header] = read_prevah_result(sample.q,9999);
0014 %
0015 % Note: at the moment, only *.q, *.mit, *.std and *pri files are explicitly
0016 %       supported (e.g. with header column information)
0017 %
0018 % See also: read_prevah_grid, write_prevah_grid, read_prevat_input
0019 %
0020 % Felix Hebeler, Geography Dept., University Zurich, Feb 2009
0021 
0022 %open file
0023 fid = fopen(file,'r');
0024 [pathstr, name, ext] = fileparts(file);
0025 
0026 % header for information only
0027 h=0; % 0 header lines
0028 switch ext
0029     case '.q'
0030         header={'YYYY','MM','DD','HH','RTOT','RTOT','R0','R1','R2','RG1','RG2','RG3'};
0031         col=14;
0032     case '.std'
0033         header={'YYYY','MM','DD','HH','ZERO','P','ADJ-P','SWA','ETP','ETR','EI','EB','R0','R1','R2','RTOT','SSO','SI','SSM','SUZ','SLZ'};
0034         col=21;
0035     case '.mit'
0036         header={'YYYY','MM','DD','P','ADJ-P','SWA','ETP','ETR','EI','EB','R0','R1','R2','RTOT','SSO','SI','SSM','SUZ','SLZ','BIL','ICEM','RG1','RG2','RG3','DIFGA'};
0037         col=25;
0038         h=27;
0039     case '.pri'
0040         header={'BASINID','YYYY','MM','BASINID','P','ADJ-P','SWA','ETP','ETR','EI','EB','R0','R1','R2','RTOT','SSO','SI','SSM','SUZ','SLZ','ICEM'};
0041         col=21;
0042     otherwise
0043         header={'YYYY','MM','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA'};
0044         col=22;
0045 end
0046 
0047 if h>0;
0048     header(col+1)=textscan(fid,'%q',h); % write whatever is read from the header line(s) to the header cell struct
0049 end
0050 
0051 % scan in text
0052 format = '%d %d %d %d'; % first 4 columns are read in as integers
0053 for c=5:col
0054     format = [format,' %n']; % add a %n to read in double for each remaining column
0055 end
0056 data = textscan(fid,format);
0057 
0058 %intialise result array
0059 l=length(cell2mat(data(1,col))); % get length of last column, this is used because result files can contain gibberish from some point on,
0060 % which will not be read, but the column length (no of timesteps) will not
0061 % be correct anymore for the reformating
0062 R=nan(l,col);
0063 
0064 % reformat to column vector
0065 for i= 1:col
0066     R(:,i)=data{1,i}(1:l); % l (normally, l equals end)
0067 end
0068 clear data;
0069 
0070 % check if nodata values should be replaced
0071 if exist('nodata','var')
0072     R(R==nodata)=NaN;
0073 end
0074 %close file
0075 fclose(fid);

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