Home > custom > prevah > write_prevah_grid.m

write_prevah_grid

PURPOSE ^

PURPOSE: export data to the PREVAH hydrological model binary grid format

SYNOPSIS ^

function write_prevah_grid(file, data, header, stats)

DESCRIPTION ^

 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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_prevah_grid(file, data, header, stats)
0002 % PURPOSE: export data to the PREVAH hydrological model binary grid format
0003 % -------------------------------------------------------------------
0004 % USAGE:  write_prevah(file, data, header, stats)
0005 % INPUT:  file   - filename and location as string (file will be created or
0006 %                  overwritten!)
0007 %         data   - n*m matrix formated according to col and rows info in
0008 %                  header
0009 %         header - cell with number of cols, rows, coordinates of llc, cell
0010 %                  size and nodata value as given by read_prevah
0011 %         stats  - statistics: cell with no of valid cells, min, max, sum,
0012 %                  mean, std
0013 %
0014 % Example: write_prevah('C:\test.bin', peaks(20), header, stats)
0015 % where:
0016 % header.cols=20;
0017 % header.rows=20;
0018 % header.xll=1;
0019 % header.yll=1;
0020 % header.cellsize=10;
0021 % header.nodata=-9999;
0022 % stats.count=400;
0023 % stats.min=-6.3605;
0024 % stats.max=7.5561;
0025 % stats.sum=131.1184;
0026 % stats.mean=0.3278;
0027 % stats.std=1.8167;
0028 %
0029 % Note: NaNs are replaced with the header nodata value
0030 %
0031 % See also: read_prevah_grid, read_prevah_result, read_prevat_input
0032 %
0033 % Felix Hebeler, Geography Dept., University Zurich, Feb 2009
0034 
0035 % create new file
0036 [fid,message] = fopen(file, 'w');
0037 if fid==-1
0038     error(message)
0039 end
0040 
0041 % format header
0042 h(1)=header.cols;
0043 h(2)=header.rows;
0044 h(3)=header.xll;
0045 h(4)=header.yll;
0046 h(5)=header.cellsize;
0047 h(6)=header.nodata;
0048 
0049 %format stats
0050 s(1)=stats.count;
0051 s(2)=stats.min;
0052 s(3)=stats.max;
0053 s(4)=stats.sum;
0054 s(5)=stats.mean;
0055 s(6)=stats.std;
0056 clear stats;
0057 
0058 % format data
0059 data(isnan(data))=header.nodata; % set NaN to NoData value
0060 data=flipud(data); % align: flip and rotate
0061 data=rot90(data,-1);
0062 
0063 
0064 % write data
0065 count = fwrite(fid,h,'float32');
0066 if count~=6
0067     error('Error writing header (header rows 1-6)')
0068 end
0069 count = fwrite(fid,s,'float32');
0070 if count~=6
0071     error('Error writing stats (header rows 7-12)')
0072 end
0073 count = fwrite(fid,data,'float32');
0074 if count~=header.rows*header.cols
0075     error(['Error writing data: ',num2str(count),' of ',num2str(header.rows*header.cols), 'records written'])
0076 end
0077 
0078 % close file
0079 status = fclose(fid);
0080 if status==-1
0081     error('Unable to successfully close input binary file')
0082 end

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