function oct_create_OGCM(fname,lonT,latT,lonU,latU,lonV,latV,depth,time,...
                      temp,salt,u,v,ssh,Yorig)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Create the OGCM file
%
%  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) 2005-2006 by Pierrick Penven
%  e-mail:Pierrick.Penven@ird.fr
%
%  Updated    6-Sep-2006 by Pierrick Penven
%  Updated    7-Oct-2013 by Gildas Cambon
%  Updated   14-Fev-2013 by Gildas Cambon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

missval=NaN;
disp('    Create the OGCM file')
ncid = netcdf.create(fname, 'NC_CLOBBER');
netcdf.reDef(ncid);
did_lonT = netcdf.defDim(ncid, 'lonT', length(lonT));
did_latT = netcdf.defDim(ncid, 'latT', length(latT));
did_lonU = netcdf.defDim(ncid, 'lonU', length(lonU));
did_latU = netcdf.defDim(ncid, 'latU', length(latU));
did_lonV = netcdf.defDim(ncid, 'lonV', length(lonV));
did_latV = netcdf.defDim(ncid, 'latV', length(latV));
did_depth = netcdf.defDim(ncid, 'depth', length(depth));
did_time = netcdf.defDim(ncid, 'time', length(time));
vid_temp = netcdf.defVar(ncid, 'temp', 'NC_FLOAT', [did_lonT, did_latT, did_depth, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'temp'), 'long_name', 'TEMPERATURE');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'temp'), 'units', 'deg. C');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'temp'), 'missing_value', missval);
vid_salt = netcdf.defVar(ncid, 'salt', 'NC_FLOAT', [did_lonT, did_latT, did_depth, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'salt'), 'long_name', 'SALINITY');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'salt'), 'units', 'ppt');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'salt'), 'missing_value', missval);
vid_u = netcdf.defVar(ncid, 'u', 'NC_FLOAT', [did_lonU, did_latU, did_depth, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'u'), 'long_name', 'ZONAL VELOCITY');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'u'), 'units', 'm/sec');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'u'), 'missing_value', missval);
vid_v = netcdf.defVar(ncid, 'v', 'NC_FLOAT', [did_lonV, did_latV, did_depth, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'v'), 'long_name', 'MERIDIONAL VELOCITY');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'v'), 'units', 'm/sec');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'v'), 'missing_value', missval);
vid_ubar = netcdf.defVar(ncid, 'ubar', 'NC_FLOAT', [did_lonU, did_latU, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'ubar'), 'long_name', 'ZONAL BAROTROPIC VELOCITY');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'ubar'), 'units', 'm/sec');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'ubar'), 'missing_value', missval);
vid_vbar = netcdf.defVar(ncid, 'vbar', 'NC_FLOAT', [did_lonV, did_latV, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'vbar'), 'long_name', 'MERIDIONAL BAROTROPIC VELOCITY');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'vbar'), 'units', 'm/sec');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'vbar'), 'missing_value', missval);
%nc{'taux'}=ncfloat('time','latU','lonU') ;
%nc{'taux'}.long_name=ncchar('TAU_X');
%nc{'taux'}.long_name='TAU_X';
%nc{'taux'}.units=ncchar('N.m-2');
%nc{'taux'}.units='N.m-2';
%nc{'taux'}.missing_value=missval;
%nc{'tauy'}=ncfloat('time','latV','lonV') ;
%nc{'tauy'}.long_name=ncchar('TAU_Y');
%nc{'tauy'}.long_name='TAU_Y';
%nc{'tauy'}.units=ncchar('N.m-2');
%nc{'tauy'}.units='N.m-2';
%nc{'tauy'}.missing_value=missval;
vid_ssh = netcdf.defVar(ncid, 'ssh', 'NC_FLOAT', [did_lonT, did_latT, did_time]);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'ssh'), 'long_name', 'SEA LEVEL HEIGHT');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'ssh'), 'units', 'm');
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'ssh'), 'missing_value', missval);
vid_lonT = netcdf.defVar(ncid, 'lonT', 'NC_DOUBLE', did_lonT);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'lonT'), 'units', 'degrees_east');
vid_latT = netcdf.defVar(ncid, 'latT', 'NC_DOUBLE', did_latT);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'latT'), 'units', 'degrees_north');
vid_lonU = netcdf.defVar(ncid, 'lonU', 'NC_DOUBLE', did_lonU);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'lonU'), 'units', 'degrees_east');
vid_latU = netcdf.defVar(ncid, 'latU', 'NC_DOUBLE', did_latU);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'latU'), 'units', 'degrees_north');
vid_lonV = netcdf.defVar(ncid, 'lonV', 'NC_DOUBLE', did_lonV);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'lonV'), 'units', 'degrees_east');
vid_latV = netcdf.defVar(ncid, 'latV', 'NC_DOUBLE', did_latV);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'latV'), 'units', 'degrees_north');
vid_depth = netcdf.defVar(ncid, 'depth', 'NC_DOUBLE', did_depth);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'depth'), 'units', 'meters');
vid_time = netcdf.defVar(ncid, 'time', 'NC_DOUBLE', did_time);
eval(['netcdf.getAtt(ncid, netcdf.inqVarID(ncid, ''time''), 'units')= ncchar(''days since ',sprintf('%04d', Yorig),'-01-01 00:00:00'');'])
eval(['netcdf.getAtt(ncid, netcdf.inqVarID(ncid, ''time''), 'units')= ''days since ',sprintf('%04d', Yorig),'-01-01 00:00:00'';'])
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncid, netcdf.inqVarID(ncid, 'time'), 'calendar', 'proleptic_gregorian');
netcdf.endDef(ncid);
%
% File the file
%
disp('    Fill the OGCM file')
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'depth'), depth);
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'latT'), latT);
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'lonT'), lonT);
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'latU'), latU);
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'lonU'), lonU);
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'latV'), latV);
netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'lonV'), lonV);
%
for tndx=1:length(time)
  %
  netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'time'), tndx-1, 1, time(tndx));  % [conv] 0-based
  %
  if length(time)==1
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'ssh'), tndx,:,:-1, 1, ssh);  % [conv] 0-based
    %    nc{'taux'}(tndx,:,:)=taux;
    %    nc{'tauy'}(tndx,:,:)=tauy;
    u1=u;
    v1=v;
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'u'), tndx,:,:,:-1, 1, u1);  % [conv] 0-based
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'v'), tndx,:,:,:-1, 1, v1);  % [conv] 0-based
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'temp'), tndx,:,:,:-1, 1, temp);  % [conv] 0-based
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'salt'), tndx,:,:,:-1, 1, salt);  % [conv] 0-based
  else
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'ssh'), tndx,:,:-1, 1, squeeze(ssh(tndx,:,:)));  % [conv] 0-based
    %    nc{'taux'}(tndx,:,:)=squeeze(taux(tndx,:,:));
    %    nc{'tauy'}(tndx,:,:)=squeeze(tauy(tndx,:,:));
    u1=squeeze(u(tndx,:,:,:));
    v1=squeeze(v(tndx,:,:,:));
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'u'), tndx,:,:,:-1, 1, u1);  % [conv] 0-based
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'v'), tndx,:,:,:-1, 1, v1);  % [conv] 0-based
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'temp'), tndx,:,:,:-1, 1, squeeze(temp(tndx,:,:,:)));  % [conv] 0-based
    netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'salt'), tndx,:,:,:-1, 1, squeeze(salt(tndx,:,:,:)));  % [conv] 0-based
  end
  %
  % Compute the barotropic velocities
  %
  masku=isfinite(u1);
  maskv=isfinite(v1);
  u1(isnan(u1))=0;
  v1(isnan(v1))=0;
  dz=gradient(depth);
  NZ=length(depth);
  du=0*squeeze(u1(1,:,:));
  zu=du;
  dv=0*squeeze(v1(1,:,:));
  zv=dv;
  for k=1:NZ
    du=du+dz(k)*squeeze(u1(k,:,:));
    zu=zu+dz(k)*squeeze(masku(k,:,:));
    dv=dv+dz(k)*squeeze(v1(k,:,:));
    zv=zv+dz(k)*squeeze(maskv(k,:,:));
  end
  du(zu==0)=NaN;
  dv(zv==0)=NaN;
  zu(zu==0)=NaN;
  zv(zv==0)=NaN;
  ubar=du./zu;
  vbar=dv./zv;
  %
  netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'ubar'), tndx,:,:-1, 1, ubar);  % [conv] 0-based
  netcdf.putVar(ncid, netcdf.inqVarID(ncid, 'vbar'), tndx,:,:-1, 1, vbar);  % [conv] 0-based
  %
end
%
netcdf.close(ncid);
%
return

end

