function [lat,lon,mask]=read_latlonmask(fname,type);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% function [lat,lon,mask]=read_latlonmask(fname,type);
%
% read the latitude, the longitude and the mask from a
% netcdf CROCO file 
%
% input:
%
%  fname    CROCO file name
%  type    type of the variable (character):
%             r for 'rho'
%             w for 'w'
%             u for 'u'
%             v for 'v'
%
% output:
%
%  lat      Latitude  (2D matrix) 
%  lon      Longitude (2D matrix) 
%  mask     Mask (2D matrix) (1:sea - nan:land)
%  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  
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Use native netcdf calls for compatibility
ncid = netcdf.open(fname,'NC_NOWRITE');
try
  lat = netcdf.getVar(ncid,netcdf.inqVarID(ncid,'lat_rho'));
catch
  try
    lat = 1e-5*netcdf.getVar(ncid,netcdf.inqVarID(ncid,'y_rho'));
  catch
    netcdf.close(ncid);
    error('READ_LATLONMASK: no latitude variable found')
  end
end
try
  lon = netcdf.getVar(ncid,netcdf.inqVarID(ncid,'lon_rho'));
catch
  try
    lon = 1e-5*netcdf.getVar(ncid,netcdf.inqVarID(ncid,'x_rho'));
  catch
    netcdf.close(ncid);
    error('READ_LATLONMASK: no longitude variable found')
  end
end
try
  mask = netcdf.getVar(ncid,netcdf.inqVarID(ncid,'mask_rho'));
catch
  mask = [];
end
if isempty(mask)
  mask = 1+0*lon;
end
netcdf.close(ncid);
%
[Mp,Lp]=size(mask);
%
if (type=='u')
  lat=rho2u_2d(lat);
  lon=rho2u_2d(lon);
  mask=mask(:,1:Lp-1).*mask(:,2:Lp);
end
if (type=='v')
  lat=rho2v_2d(lat);
  lon=rho2v_2d(lon);
  mask=mask(1:Mp-1,:).*mask(2:Mp,:);
end
%
mask(mask==0)=NaN;
%
return
