function [out err] = semangle(in,sens)

%Extension of MEANANGLE as written by J.A. Dunne, 10-20-05
%
% Copyright (C) 2018 by Ralitsa Todorova
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or
% (at your option) any later version.
%

if nargin<3
    sens = 1e-12;
end

if nargin<2
    ind = min(find(size(in)>1));
    if isempty(ind)
        %This is a scalar
        out = in;
        return
    end
    dim = ind;
end

%if angles are provided in degrees, which they are not
% in = in * pi/180; 

in = exp(i*in);
mid = nansem(real(in)) + nansem(imag(in))*1i;
out = atan2(imag(mid),real(mid));
out(abs(mid)<sens) = nan;








