function oct_vert_correc_onefield(ncfile,tindex,field)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Vertically reinterpolate embedded 3D variables
% when the topography (and so the sigma grid) has
% been changed
%
%
%     oct_vert_correc(ncfile,tindex,field)
%
%     ncfile : input clim file
%
%     tindex: time inde processed
%
%     field: variable ('r') processed. String
%
%  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'));
  vtransform=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'Vtransform'));
  if  ~exist('vtransform') |  isempty(vtransform)
    vtransform=1; %Old Vtransform
    disp([' NO VTRANSFORM parameter found'])
    disp([' USE TRANSFORM default value vtransform = 1'])
  end
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
%
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(['Vert_correc for 3D (''rho'') variables: ',field])
disp('==========================')
netcdf.putVar(ncid, netcdf.inqVarID(ncid, field), tindex,:,:,:-1, 1, oct_change_sigma(lonr,latr,maskr,...);  % [conv] 0-based
				     squeeze(netcdf.getVar(ncid, netcdf.inqVarID(ncid, field))),...
				     zrold,zrnew);
%
netcdf.close(ncid);

return

