function oct_vert_correc(ncfile,tindex,biol,pisces,namebiol,namepisces)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Vertically reinterpolate embedded 3D variables
% when the topography (and so the sigma grid) has
% been changed
%
%  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) 2004-2006 by Pierrick Penven 
%  e-mail:Pierrick.Penven@ird.fr  
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp(' ')
disp(' Vertical corrections... ')
ncid = netcdf.open(ncfile, 'NC_WRITE');
N=netcdf.inqDimLen(ncid, netcdf.inqDimID(ncid, 's_rho'));
theta_s = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'theta_s'));

if isempty(theta_s)
  theta_s=ncid.theta_s(:);
  theta_b=ncid.theta_b(:);
  hc=ncid.hc(:);
else
  theta_b=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'theta_b'));
  hc=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'hc'));
end
vtransform=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'Vtransform'));
if  ~exist('vtransform')
    vtransform=1; %Old Vtransform
    disp([' NO VTRANSFORM parameter found'])
    disp([' USE TRANSFORM default value vtransform = 1'])
end
%
zeta=squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'zeta')));
if isempty(zeta)
  zeta=squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'SSH')));
end
grd_file = ncid.grd_file(:);
ng_id = netcdf.open(grd_file, 'NC_NOWRITE');
hold=squeeze(netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'hraw')));
if isempty(zeta)
  zeta=0*hold;
end
hnew=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'h'));
latr=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'lat_rho'));
latu=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'lat_u'));
latv=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'lat_v'));
lonr=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'lon_rho'));
lonu=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'lon_u'));
lonv=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'lon_v'));
maskr=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_rho'));
masku=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_u'));
maskv=netcdf.getVar(ng_id, netcdf.inqVarID(ng_id, 'mask_v'));
result=close(ng_id);
%
% get the depths
%
disp('get the depths')
zrold=oct_zlevs(hold,zeta,theta_s,theta_b,hc,N,'r',vtransform);
zrnew=oct_zlevs(hnew,zeta,theta_s,theta_b,hc,N,'r',vtransform);
zuold=0.5*(zrold(:,:,1:end-1)+zrold(:,:,2:end));
zunew=0.5*(zrnew(:,:,1:end-1)+zrnew(:,:,2:end));
zvold=0.5*(zrold(:,1:end-1,:)+zrold(:,2:end,:));
zvnew=0.5*(zrnew(:,1:end-1,:)+zrnew(:,2:end,:));
%
% perform the modifications
%
disp('u...')
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'u'), tindex,:,:,:-1, 1, oct_change_sigma(lonu,latu,masku,...);  % [conv] 0-based
				   squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'u'))),...
				   zuold,zunew);
disp('v...')
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'v'), tindex,:,:,:-1, 1, oct_change_sigma(lonv,latv,maskv,...);  % [conv] 0-based
				   squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'v'))),...
				   zvold,zvnew);
disp('temp...')
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'temp'), tindex,:,:,:-1, 1, oct_change_sigma(lonr,latr,maskr,...);  % [conv] 0-based
				      squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'temp'))),...
				      zrold,zrnew);
disp('salt...')
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'salt'), tindex,:,:,:-1, 1, oct_change_sigma(lonr,latr,maskr,...);  % [conv] 0-based
				      squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'salt'))),...
				      zrold,zrnew);
%
if (biol==1)
  disp('oct_vert_correc for biology variables')
  for k=1:length(namebiol)
    disp([char(namebiol(k)),'...'])
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, char(namebiol(k))), tindex,:,:,:-1, 1, oct_change_sigma(lonr,latr,maskr,...);  % [conv] 0-based
						     squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, char(namebiol(k))))),...
						     zrold,zrnew);
  end
end
%
if (pisces==1)
  disp('oct_vert_correc for pisces variables')
  for k=1:length(namepisces)
      disp([char(namepisces(k)),'...'])
      netcdf.putVar(ncid, netcdf.inqVarID(ncid, char(namepisces(k))), tindex,:,:,:-1, 1, oct_change_sigma(lonr,latr,maskr,...);  % [conv] 0-based
          squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, char(namepisces(k))))),...
          zrold,zrnew);
  end
end
%
netcdf.close(ncid);
return

