function [z]=get_depths(fname,gname,tindex,type);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Get the depths of the sigma levels
%
%  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) 2002-2006 by Pierrick Penven 
%  e-mail:Pierrick.Penven@ird.fr  
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read grid h using native oct_netcdf
ngid = oct_netcdf.open(gname,'NC_NOWRITE');
try
  h = oct_netcdf.getVar(ngid,oct_netcdf.inqVarID(ngid,'h'));
catch
  oct_netcdf.close(ngid);
  error(['h not found in ',gname])
end
oct_netcdf.close(ngid);

% open history file
ncid = oct_netcdf.open(fname,'NC_NOWRITE');
try
  zeta = squeeze(oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'zeta')));
catch
  zeta = [];
end
try
  hmorph = squeeze(oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'hmorph')));
catch
  hmorph = [];
end
if ~isempty(hmorph), h=hmorph; end;
try
  theta_s = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'theta_s'));
catch
  theta_s = [];
end
if isempty(theta_s)
  try
    theta_s = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'theta_s'));
    theta_b = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'theta_b'));
    Tcline  = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'Tcline'));
  catch
    theta_s = [];
  end
else
  try
    theta_b = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'theta_b'));
    Tcline  = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'Tcline'));
    hc = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'hc'));
  catch
    Tcline = [];
  end
end
if isempty(Tcline)
  try
    hc = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'hc'));
  catch
    hc = [];
  end
else
  hmin = min(min(h));
  hc = min(hmin,double(Tcline));
end
% number of s_rho
try
  s_rho = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'s_rho'));
  N = length(s_rho);
catch
  try
    tmp = oct_netcdf.inqVar(ncid,oct_netcdf.inqVarID(ncid,'temp'));
    N = tmp(2);
  catch
    N = [];
  end
end
s_coord = 1;
VertCoordType = [];
try
  VertCoordType = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'VertCoordType'));
catch
end
if isempty(VertCoordType)
  try
    vtrans = oct_netcdf.getVar(ncid,oct_netcdf.inqVarID(ncid,'Vtransform'));
    if ~isempty(vtrans), s_coord = vtrans; end
  catch
  end
elseif strcmp(char(VertCoordType(:)'),'NEW')
  s_coord=2;
end
if s_coord==2
  hc = Tcline;
end
oct_netcdf.close(ncid)
%
%
%
if isempty(zeta)
  zeta=0.*h;
end

vtype=type;
if (type=='u')|(type=='v')
  vtype='r';
end
z=zlevs(h,zeta,theta_s,theta_b,hc,N,vtype,s_coord);
if type=='u'
  z=rho2u_3d(z);
end
if type=='v'
  z=rho2v_3d(z);
end
return
