function result = dewa_analyse(ncfile, varargin)

info = nc_info(ncfile);

tdim = info.Dimension(strcmp({info.Dimension.Name}, 'globaltime')).Length;
tmdim = 1;
if any(strcmp({info.Dimension.Name}, 'meantime'))
    tmdim = info.Dimension(strcmp({info.Dimension.Name}, 'meantime')).Length;
end

OPT = struct(...
    'tdim', tdim,...
    'tmdim', tmdim);

OPT = setproperty(OPT, varargin);

tdim = OPT.tdim;
tmdim = OPT.tmdim;

%%
x = nc_varget(ncfile, 'globalx')';
y = nc_varget(ncfile, 'globaly')';
g = xb_stagger(x, y);
[nx ny] = size(x);

dsz = g.dsz;
dsz(1,:) = 0;
s = cumsum(dsz, 1);

zb0 = nc_varget(ncfile, 'zb', [0 0 0], [1 -1 -1])';
zb1 = nc_varget(ncfile, 'zb', [tdim-1 0 0], [1 -1 -1])';

Ero = sum((max(zb1, 5) - max(zb0, 5)) .* g.dsdnz ./ g.dnz);
Sed = sum((min(zb1, 5) - min(zb0, 5)) .* g.dsdnz ./ g.dnz);

%%
idymid = floor(size(x,2)/2);

x = x(:,idymid);
Svsg = select_transect(Svsg, idymid, ny);
% y = y(idymid,:);

zb0 = nc_varget(ncfile, 'zb', [0 0 0], [1 -1 -1]);
zb0 = select_transect(zb0, idymid, ny);
zb1 = nc_varget(ncfile, 'zb', [tdim-1 0 0], [1 -1 -1]);
zb1 = select_transect(zb1, idymid, ny);

ccg = nc_varget(ncfile, 'ccg_mean');%, [0 0 idymid 0], [tmdim -1 1 -1])';
ccg = select_transect(ccg, idymid, ny);

Svsg = nc_varget(ncfile, 'Svsg_mean');%, [0 0 idymid 0], [tmdim -1 1 -1])';
Svsg = select_transect(Svsg, idymid, ny);

dSvsg = diff(Svsg([1:end end],:),1,1);
Svsg_grad = dSvsg ./ g.dnu' .* g.dsu';

ve = nc_varget(ncfile, 've_mean');%, [0 idymid 0], [tmdim 1 -1])';
ve = select_transect(ve, idymid, ny);

zs = nc_varget(ncfile, 'zs_mean');%, [0 idymid 0], [tmdim 1 -1])';
zs = select_transect(zs, idymid, ny);

function outvar = select_transect(invar, idymid, ny)
idx = repmat({':'}, 1, ndims(invar));
ydim = find(size(invar) == ny);
idx{size(invar) == ny} = round(.3 * ny) : round(.7 * ny);
invar = invar(idx{:});
outvar = mean(invar, ydim);

