function oct_rmavgssh(bryname,grdname,obc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%  Remove the averaged SSH in the boundary (bry) files.
% 
% 
%  Further Information:  
%  http://www.croco-ocean.org
%  
%  This file is part of CROCOTOOLS
%
%  CROCOTOOLS 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 2 of the License,
%  or (at your option) any later version.
%
%  CROCOTOOLS is distributed in the hope that it will be useful, but
%  WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software
%  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
%  MA  02111-1307  USA
%
%  Copyright (c) 2001-2006 by Pierrick Penven 
%  e-mail:Pierrick.Penven@ird.fr  
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(' ');
disp('Remove averaged SSH ...');

ng_id = netcdf.open(grdname, 'NC_NOWRITE');
L=netcdf.inqDimLen(ng_id, netcdf.inqDimID(ng_id, 'xi_rho'));
M=netcdf.inqDimLen(ng_id, netcdf.inqDimID(ng_id, 'eta_rho'));

ncid = netcdf.open(bryname, 'NC_WRITE');
time=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'bry_time'));
tlen=length(time);


for l=1:tlen
%for l=1:1
  tssh=0;
  S=0;
  for obcndx=1:4
    if obc(obcndx)==1
      if obcndx==1
        pm=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pm'));
        pn=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pn'));
        rmask=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_rho'));
        suffix='_south';
      elseif obcndx==2
        pm=(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pm')))';
        pn=(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pn')))';
        rmask=(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_rho')))';
        suffix='_east';
      elseif obcndx==3
        pm=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pm'));
        pn=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pn'));
        rmask=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_rho'));
        suffix='_north';
      elseif obcndx==4
        pm=(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pm')))';
        pn=(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'pn')))';
        rmask=(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_rho')))';
        suffix='_west';
      end
      Nx=length(pm);
      ssh=netcdf.getVar(ncid, netcdf.inqVarID(ncid, ['zeta',suffix]));
      tssh=tssh+sum(rmask.*ssh./(pm.*pn));
      S=S+sum(rmask./(pm.*pn)); 
    end
  end
  avgssh=tssh./S;
  for obcndx=1:4
    if obc(obcndx)==1
      if obcndx==1
        suffix='_south';
      elseif obcndx==2
        suffix='_east';
      elseif obcndx==3
        suffix='_north';
      elseif obcndx==4
        suffix='_west';
      end
      ssh=netcdf.getVar(ncid, netcdf.inqVarID(ncid, ['zeta',suffix]));
      netcdf.putVar(ncid, netcdf.inqVarID(ncid, ['zeta',suffix]), l,:-1, 1, ssh-avgssh);  % [conv] 0-based
    end
  end
end
netcdf.close(ng_id);
netcdf.close(ncid);
