Skip to content

Commit

Permalink
refact(PP): export find_beam_vars as function
Browse files Browse the repository at this point in the history
This commit exports the `find_beam_vars` as
`IMOS.adcp.beam_vars`, with some improvements to handle
cases outside the `binMappingPP` functionality.
  • Loading branch information
ocehugo committed Jun 10, 2021
1 parent 26ca6e5 commit b13b8dc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 59 deletions.
61 changes: 2 additions & 59 deletions Preprocessing/adcpBinMappingPP.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
roll = sample_data{k}.variables{rollIdx}.data * pi / 180;
beamAngle = sample_data{k}.meta.beam_angle * pi / 180;

%TODO: the adjusted distances should be exported
%TODO: the adjusted distances should be exported
% as variables to enable further diagnostic plots and debugging.
%TODO: the adjusted distances should be a Nx4 array or Nx3 array.
if isRDI% RDI 4 beams
Expand Down Expand Up @@ -131,7 +131,7 @@
% interpolation conditions. This would allow a progress bar here.

for n = 1:number_of_beams
beam_vars = get_beam_vars(sample_data{k}, n);
beam_vars = IMOS.adcp.beam_vars(sample_data{k}, n);
[all_beam_vars] = IMOS.concatenate_variables(sample_data{k}.variables, beam_vars);

switch n
Expand Down Expand Up @@ -238,60 +238,3 @@
end

end

function [ibvars] = get_beam_vars(sample_data, n)
%function [ibvars] = get_beam_vars(sample_data,n)
%
% Get the available beam variable names
% based on the beam number.
%
% Inputs:
%
% sample_data [struct] - the toolbox struct.
% n [double] - the beam number [1-4]
%
% Outputs:
%
% ibvars [cell{str}] - the variable names associated
% with the beam number.
%
%
% author: hugo.oliveira@utas.edu.au
%
%

narginchk(2, 2)

if ~isstruct(sample_data)
error('Frist argument not a toolbox struct')
end

switch n
case 1
beam_vars = {'ABSI1', 'ABSIC1', 'CMAG1', 'SNR1', 'VEL1'};
case 2
beam_vars = {'ABSI2', 'ABSIC2', 'CMAG2', 'SNR2', 'VEL2'};
case 3
beam_vars = {'ABSI3', 'ABSIC3', 'CMAG3', 'SNR3', 'VEL3'};
case 4
beam_vars = {'ABSI4', 'ABSIC4', 'CMAG4', 'VEL4'};
otherwise
errormsg('Second argument not a valid beam number')
end

ibvars = cell(1, length(beam_vars));
is_a_beam_var = @(sample_data, varname)(IMOS.var_contains_dim(sample_data, varname, 'DIST_ALONG_BEAMS'));

c = 0;

for k = 1:length(beam_vars)

if is_a_beam_var(sample_data, beam_vars{k})
c = c + 1;
ibvars{c} = beam_vars{k};
end

end

ibvars = ibvars(1:c);
end
106 changes: 106 additions & 0 deletions Util/+IMOS/+adcp/beam_vars.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
function [ibvars] = beam_vars(sample_data, n)
%function [ibvars] = beam_vars(sample_data,n)
%
% Get the available beam variable names
% based on the beam number.
% If beam number is not provided, all variables
% names that are along beams are returned.
%
% Inputs:
%
% sample_data [struct] - the toolbox struct.
% n [double] - the beam number [1-4]
%
% Outputs:
%
% ibvars [cell{str}] - the variable names associated
% with the beam number.
%
% Example:
%
% time = (1:100)';
% dab = (1:10)';
% dummy = zeros(100,10);
% type = @double;
% dims = IMOS.gen_dimensions('adcp',2,{'TIME','DIST_ALONG_BEAMS'},{type,type},{time,dab});
% vars = IMOS.gen_variables(dims,{'VEL1','ABSIC1','VEL3','ABSIC3'},{type,type,type,type},{dummy,dummy,dummy,dummy});
% sample_data.dimensions = dims;
% sample_data.variables = vars;
% beam_vars_1 = IMOS.adcp.beam_vars(sample_data,1);
% assert(numel(whereincell(beam_vars_1,{'ABSIC1','VEL1'}))==2)
% assert(numel(whereincell(beam_vars_1,{'VEL3','ABSIC3'}))==0)
%
% %beam 3
% beam_vars_3 = IMOS.adcp.beam_vars(sample_data,3);
% assert(numel(whereincell(beam_vars_3,{'ABSIC1','VEL1'}))==0)
% assert(numel(whereincell(beam_vars_3,{'VEL3','ABSIC3'}))==2)
%
% %all beams available
% all_beams = IMOS.adcp.beam_vars(sample_data);
% assert(numel(whereincell(all_beams,{'VEL1','ABSIC1','ABSIC3','VEL3'}))==4)
%
% %got a beam_variable name but dimensions are not aligned with DIST_ALONG_BEAMS
% dummy = zeros(100,100);
% vars = IMOS.gen_variables(dims,{'VEL1'},{type},{dummy});
% sample_data.dimensions = dims;
% sample_data.variables = vars;
% beam_vars_1 = IMOS.adcp.beam_vars(sample_data,1);
% assert(isempty(beam_vars_1))
%
%
% author: hugo.oliveira@utas.edu.au
%
%
narginchk(1, 2)

if nargin<2
get_all_beam_vars = true;
n=inf;
else
get_all_beam_vars = false;
end


if ~isstruct(sample_data)
error('Frist argument not a toolbox struct')
end

beam_vars_1 = {'ABSI1', 'ABSIC1', 'CMAG1', 'SNR1', 'VEL1'};
beam_vars_2 = {'ABSI2', 'ABSIC2', 'CMAG2', 'SNR2', 'VEL2'};
beam_vars_3 = {'ABSI3', 'ABSIC3', 'CMAG3', 'SNR3', 'VEL3'};
beam_vars_4 = {'ABSI4', 'ABSIC4', 'CMAG4', 'VEL4'};

switch n
case 1
beam_vars = beam_vars_1;
case 2
beam_vars = beam_vars_2;
case 3
beam_vars = beam_vars_3;
case 4
beam_vars = beam_vars_4;
otherwise
if get_all_beam_vars
beam_vars = [beam_vars_1, beam_vars_2, beam_vars_3, beam_vars_4];
else
errormsg('Second argument not a valid beam number')
end
end

%double check if dimensions are
ibvars = cell(1, length(beam_vars));
is_a_beam_var = @(sample_data, varname)(IMOS.var_contains_dim(sample_data, varname, 'DIST_ALONG_BEAMS'));

c = 0;

for k = 1:length(beam_vars)

if is_a_beam_var(sample_data, beam_vars{k})
c = c + 1;
ibvars{c} = beam_vars{k};
end

end

ibvars = ibvars(1:c);
end

0 comments on commit b13b8dc

Please sign in to comment.