


Parse the POLCOMS rivers data file.
get_POLCOMS_rivers(Mobj, polcoms_file, polcoms_grid, polcoms_ij)
DESCRIPTION:
Takes POLCOMS grid and index files and returns the positions and names
of the rivers within the index file.
INPUT:
Mobj - MATLAB mesh object into which the outputs will be added.
polcoms_grid - NetCDF file of the POLCOMS grid.
polcoms_ij - indices in the POLCOMS grid at which each river is
located.
OUTPUT:
Mobj.river_xy - array of lon/lat positions of the POLCOMS rivers.
Mobj.river_polcoms_names - names for the rivers in the positions array.
EXAMPLE USAGE:
Mobj = get_POLCOMS_rivers(Mobj, 'polcoms.nc', 'polcoms.index')
Author(s):
Pierre Cazenave (Plymouth Marine Laboratory)
Revision history:
2013-04-15 - First version from the original get_POLCOMS_rivers.m.
==========================================================================

0001 function Mobj = get_POLCOMS_river_positions(Mobj, polcoms_grid, polcoms_ij) 0002 % Parse the POLCOMS rivers data file. 0003 % 0004 % get_POLCOMS_rivers(Mobj, polcoms_file, polcoms_grid, polcoms_ij) 0005 % 0006 % DESCRIPTION: 0007 % Takes POLCOMS grid and index files and returns the positions and names 0008 % of the rivers within the index file. 0009 % 0010 % INPUT: 0011 % Mobj - MATLAB mesh object into which the outputs will be added. 0012 % polcoms_grid - NetCDF file of the POLCOMS grid. 0013 % polcoms_ij - indices in the POLCOMS grid at which each river is 0014 % located. 0015 % 0016 % OUTPUT: 0017 % Mobj.river_xy - array of lon/lat positions of the POLCOMS rivers. 0018 % Mobj.river_polcoms_names - names for the rivers in the positions array. 0019 % 0020 % EXAMPLE USAGE: 0021 % Mobj = get_POLCOMS_rivers(Mobj, 'polcoms.nc', 'polcoms.index') 0022 % 0023 % Author(s): 0024 % Pierre Cazenave (Plymouth Marine Laboratory) 0025 % 0026 % Revision history: 0027 % 2013-04-15 - First version from the original get_POLCOMS_rivers.m. 0028 % 0029 %========================================================================== 0030 0031 subname = 'get_POLCOMS_river_positions'; 0032 0033 global ftbverbose; 0034 if ftbverbose 0035 fprintf(['\nbegin : ' subname '\n']) 0036 end 0037 0038 % Check inputs 0039 if exist(polcoms_grid, 'file') ~= 2 0040 error('file: %s does not exist or is not readable.', polcoms_grid) 0041 end 0042 if exist(polcoms_ij, 'file') ~= 2 0043 error('file: %s does not exist or is not readable.', polcoms_ij) 0044 end 0045 0046 % Get the positions for each river from the NetCDF grid and the index file. 0047 % Format is: 0048 % 0049 % n 0050 % id1 i j Name1 0051 % id2 i j Name2 0052 % ... 0053 % idn i j Namen 0054 fidx = fopen(polcoms_ij, 'r'); 0055 if fidx < 0 0056 error('Error opening index file (%s).', polcoms_ij); 0057 end 0058 0059 c = 0; % line counter 0060 while ~feof(fidx) 0061 line = fgetl(fidx); 0062 if isempty(line) || ~ischar(line) 0063 continue 0064 else 0065 c = c + 1; 0066 end 0067 0068 if c == 1 0069 % First (valid) line should be number of rivers 0070 nridx = str2double(strtrim(line)); 0071 % Preallocate the output arrays on the basis of this value. If the 0072 % file happens to have more lines, that shouldn't matter. Too few, 0073 % however, and you'll end up with NaNs at the end of your array. I 0074 % should probably remove them later... 0075 pc_idx = nan(nridx, 3); 0076 pc_name = cell(nridx, 1); 0077 else 0078 % We're in the data. 0079 S = regexpi(strtrim(line), ' +', 'split'); 0080 pc_idx(c - 1, :) = [str2double(S{1}), str2double(S{2}), str2double(S{3})]; 0081 pc_name{c - 1} = S{end}; 0082 end 0083 end 0084 clear S line c 0085 0086 fclose(fidx); 0087 0088 % Now read in the NetCDF file and grab the real coordinates of those 0089 % positions. 0090 nc = netcdf.open(polcoms_grid, 'NOWRITE'); 0091 [~, numvars, ~, ~] = netcdf.inq(nc); 0092 0093 for ii = 1:numvars 0094 0095 [varname, ~, ~, ~] = netcdf.inqVar(nc, ii - 1); 0096 varid = netcdf.inqVarID(nc, varname); 0097 0098 if ftbverbose 0099 fprintf('\tvariable %s... ', varname) 0100 end 0101 0102 switch varname 0103 case 'lon' 0104 pc_lon = netcdf.getVar(nc, varid); 0105 0106 case 'lat' 0107 pc_lat = netcdf.getVar(nc, varid); 0108 0109 end 0110 0111 if ftbverbose 0112 fprintf('done.\n') 0113 end 0114 0115 end 0116 0117 clear numdims numvars dimnames 0118 0119 netcdf.close(nc) 0120 0121 % Use the indices from the polcoms_ij file to get the real positions of the 0122 % river nodes. 0123 pc_riv_lonlat = [pc_lon(pc_idx(:, 2), 1), pc_lat(1, pc_idx(:, 3))']; 0124 0125 % Return the positions and names to the Mobj. 0126 Mobj.rivers.positions = pc_riv_lonlat; 0127 Mobj.rivers.names = pc_name; 0128 0129 if ftbverbose 0130 fprintf(['end : ' subname '\n']) 0131 end