function oct_write_mercator(OGCM_dir,OGCM_prefix,raw_mercator_name,...
                         mercator_type,vars_id,time,thedatemonth,Yorig)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Extract a subset from Mercator  in the case of hindcast 
% using python  copernicusmarine client
% Write it in a local file (keeping the classic SODA oct_netcdf format)
% 
%  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) 2006 by Pierrick Penven 
%  e-mail:Pierrick.Penven@ird.fr  
%
%  Updated    9-Sep-2006 by Pierrick Penven
%  Updated    19-May-2011 by Andres Sepulveda & Gildas Cambon
%  Updated    12-Feb-2016 by P. Marchesiello
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
disp(['    Writing MERCATOR file'])
%
% Get grid and time frame
%
ncid = netcdf.open(raw_mercator_name, 'NC_NOWRITE');
lon = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'longitude'));
lat = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'latitude'));
depth = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'depth'));
time = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'time'));
time = time / 24 + datenum(1950,1,1) - datenum(Yorig,1,1);
%
% Get SSH
%
%missval = -32767;
disp('    ...SSH')
vname=sprintf('%s',vars_id{1});
ncc=ncid{vname};
ssh=ncc(:);
missval=ncc.FillValue_(:);
scale_factor=ncc.scale_factor(:);
add_offset=ncc.add_offset(:);
ssh(ssh<=missval)=NaN;
ssh = ssh.*scale_factor + add_offset;
%
%
% Get U
%
disp('    ...U')
vname=sprintf('%s',vars_id{2});
ncc=ncid{vname};
u=ncc(:);
missval=ncc.FillValue_(:);
scale_factor=ncc.scale_factor(:);
add_offset=ncc.add_offset(:);
u(u<=missval)=NaN;
u = u.*scale_factor + add_offset;
%
% Get V
%
disp('    ...V')
vname=sprintf('%s',vars_id{3});
ncc=ncid{vname};
v=ncc(:);
missval=ncc.FillValue_(:);
scale_factor=ncc.scale_factor(:);
add_offset=ncc.add_offset(:);
v(v<=missval)=NaN;
v = v.*scale_factor + add_offset;
%
% Get TEMP
%
disp('    ...TEMP')
vname=sprintf('%s',vars_id{4});
ncc=ncid{vname};
temp=ncc(:);
missval=ncc.FillValue_(:);
scale_factor=ncc.scale_factor(:);
add_offset=ncc.add_offset(:);
temp(temp<=missval)=NaN;
temp = temp.*scale_factor + add_offset;
%
% Get SALT
%
disp('    ...SALT')
vname=sprintf('%s',vars_id{5});
ncc=ncid{vname};
salt=ncc(:);
missval=ncc.FillValue_(:);
scale_factor=ncc.scale_factor(:);
add_offset=ncc.add_offset(:);
salt(salt<=missval)=NaN;
salt = salt.*scale_factor + add_offset;
%
% Create the Mercator file
%
netcdf.close(ncid);  % close raw_mercator_name

oct_create_OGCM([OGCM_dir,OGCM_prefix,thedatemonth,'.cdf'],...
             lon,lat,lon,lat,lon,lat,depth,time,...
             squeeze(temp),squeeze(salt),squeeze(u),...
             squeeze(v),squeeze(ssh),Yorig)
%
return

end

