Home > mrg > MRG_adcp > mrg_son_to_mat.m

mrg_son_to_mat

PURPOSE ^

TODO: Documentation

SYNOPSIS ^

function [sontek_struc] = mrg_son_to_mat(seabedtosen, blankdist, cellsize, varargin)

DESCRIPTION ^

 TODO: Documentation
 Reads SONTEK ASCII files.  
 Outputs a MATLAB structure and (optionally) a DFS0 file.  
% Check input

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [sontek_struc] = mrg_son_to_mat(seabedtosen, blankdist, cellsize, varargin)
0002 % TODO: Documentation
0003 % Reads SONTEK ASCII files.
0004 % Outputs a MATLAB structure and (optionally) a DFS0 file.
0005 %% Check input
0006 if ~exist('seabedtosen', 'var')
0007     error('Distance from seabed to sensor (seabedtosen) not defined.')
0008 end
0009 
0010 if ~exist('blankdist', 'var')
0011     error('Blanking distance (blankdist) not defined.')
0012 end
0013 
0014 if ~exist('cellsize', 'var')
0015     error('Cell size (cellsize) not defined.')
0016 end
0017 
0018 % Number of optional input arguments must be less than 2 x options...
0019 numvarargs = length(varargin);
0020 if numvarargs > 6
0021     error('mrg:InputFail', 'Requires at most 3 optional inputs');
0022 end
0023 
0024 % Set optional input defualts
0025 dfs0 = 1;
0026 % Read optional inputs
0027 while ~isempty(varargin),
0028     if ischar(varargin{1})
0029         switch lower(varargin{1}(1:4))
0030             case 'dfs0'
0031                 dfs0 = varargin{2};
0032             case 'file'
0033                 filename = varargin{2};
0034             case 'beam'
0035                 beamangle = varargin{2};
0036             otherwise
0037                 error('mrg:InputFail','You provided an optional input not handled by the function.');
0038         end
0039     else
0040         error('mrg:InputFail','Optional inputs must be specified using the (keyword, value) syntax');
0041     end
0042     varargin([1 2])=[]; 
0043 end
0044 
0045 if ~exist('filename', 'var')
0046     [filename, path] = uigetfile({'*.hdr','SonTek header files (*.hdr)'}, 'Please select the hdr file');
0047     old_path = cd();
0048     cd(path);
0049 else 
0050     old_path = cd();
0051 end
0052 
0053 if ~exist('beamangle', 'var')
0054     warning('mrg:DefaultValue', 'Beam angle (beamangle) not defined. Defaulting to 25 degrees')
0055     beamangle = 25;
0056 end
0057 
0058 %% Read and process the header file
0059 son_hdr = dlmread(filename);
0060 hdr_names = {'profile', 'year', 'month', 'day', 'hour', 'min', 'sec',...
0061     'no_samples', 'speed', 'mean_heading', 'mean_pitch', 'mean_roll', ...
0062     'mean_temp', 'mean_pressure', 'stdev_heading', 'stdev_pitch', ...
0063     'stdev_roll', 'stdev_temp', 'stdev_pressure', 'volts'};
0064 sontek_struc = struct();
0065 for n=1:length(hdr_names)
0066     sontek_struc.(hdr_names{n}) = son_hdr(:,n);
0067 end
0068 
0069 %% Create a MATLAB DT Object
0070 sontek_struc.m_dt = datenum(sontek_struc.year, sontek_struc.month,...
0071     sontek_struc.day, sontek_struc.hour, sontek_struc.min, sontek_struc.sec);
0072 
0073 %% Read and process the 've', 'vn' and 'vu' files
0074 filename_ve = [filename(1:end-3),'ve'];
0075 filename_vn = [filename(1:end-3),'vn'];
0076 filename_vu = [filename(1:end-3),'vu'];
0077 
0078 % Now, read the ve (i.e. velocity east) file and add it to the struct
0079 son_ve = dlmread(filename_ve);
0080 sontek_struc.vel_east = son_ve(:,2:end)/100;
0081 
0082 % Now, read the vn (i.e. velocity north) file and add it to the struct
0083 son_vn = dlmread(filename_vn);
0084 sontek_struc.vel_north = son_vn(:,2:end)/100;
0085 
0086 % Now, read the vu (i.e. velocity up) file and add it to the struct
0087 son_vu = dlmread(filename_vu);
0088 sontek_struc.vel_up = son_vu(:,2:end)/100;
0089 
0090 % Warn about unit conversion
0091 warning('mrg:UnitChange', 'The velocity components have been divided by 100 to convert from cm/s to m/s')
0092 
0093 %% Trim bins that are out of the water (sidelobe flitering)
0094 cell_distances = 1:1:length(sontek_struc.vel_east(1,:));
0095 cell_distances = cell_distances*cellsize+blankdist;
0096 
0097 for n = 1:length(sontek_struc.profile)
0098     % Test pressure = cutoff depth for cells tested against the
0099     % cell_distances variable which is the distance from the sensor to the
0100     % top of the cell
0101     test_pressure = sontek_struc.mean_pressure(n)*cos(deg2rad(beamangle))-seabedtosen;
0102     above = cell_distances > test_pressure;
0103     sontek_struc.vel_east(n,above) = NaN;
0104     sontek_struc.vel_north(n,above) = NaN;
0105     sontek_struc.vel_up(n,above) = NaN;
0106 end
0107 
0108 %% Calculate Depth Averaged U and V components
0109 for n = 1:length(sontek_struc.profile)
0110     sontek_struc.da_vel_east(n,:) = nanmean(sontek_struc.vel_east(n,:));
0111     sontek_struc.da_vel_north(n,:) = nanmean(sontek_struc.vel_north(n,:));
0112 end
0113 
0114 %% Calculate some depth averaged speed and directions
0115 [theta, rho] = cart2pol(sontek_struc.da_vel_north, sontek_struc.da_vel_east);
0116 sontek_struc.da_curr_speed = rho;
0117 dir = theta*180/pi;
0118 index = ~(dir > 0);
0119 sontek_struc.da_dir = dir + index * 360;
0120 
0121 %% Create a DFS0 File
0122 if dfs0
0123     variables = {'da_vel_east', 'da_vel_north', 'da_curr_speed', 'da_dir', 'mean_pressure'};
0124     mrg_mat_to_DFS0(sontek_struc, 'm_dt', variables);
0125 end
0126 
0127 %% End
0128 cd(old_path)
0129 
0130 end

Generated on Thu 29-May-2014 21:29:53 by m2html © 2005