function ncidini=oct_create_nestedinitial(inifile,gridfile,parentfile,title,...
				    theta_s,theta_b,Tcline,N,time,clobber,...
				    biol,pisces,namebiol,namepisces,...
				    unitbiol,unitpisces,hc,vtransform,Yorig)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  function ncini=oct_create_inifile(inifile,gridfile,theta_s,... 
%                  theta_b,Tcline,N,time,clobber) 
%
%   This function create the header of a Netcdf initial 
%   file.
%
%   Input: 
%
%   inifile     Netcdf initial file name (character string)
%   gridfile     Netcdf grid file name (character string).
%   parentfile  Netcdf oct_parent initial file name (character string).
%   theta_s      S-coordinate surface control parameter.(Real)
%   theta_b      S-coordinate bottom control parameter.(Real)
%   Tcline       Width (m) of surface or bottom boundary layer
%                where higher vertical resolution is required 
%                during stretching.(Real)
%   N            Number of vertical levels.(Integer)
%   time        
%   clobber      Switch to allow or not writing over an existing
%                file.(character string)
%
%   Output
%
%   ncini       Output oct_netcdf object.
% 
%  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(' ')
disp(['Creating the file : ',inifile])
disp(' ')
if nargin < 17
    disp([' NO VTRANSFORM parameter found'])
    disp([' USE TRANSFORM default value vtransform = 1'])
    vtransform = 1; 
    hc=[];
elseif nargin <18
    disp([' NO VTRANSFORM parameter found'])
    disp([' USE TRANSFORM default value vtransform = 1'])
    vtransform = 1; 
end
%
% options for oct_write_time_attributes
insecond = 1  ;
add_cycle = 0 ; 
%
% Initialize Yorig if not provided
if ~exist('Yorig', 'var') ,  Yorig = []; , end
%
% Get time attributes
[time_unit_att,time_second_unit_att,calendar_att]=...
    oct_get_time_attributes(Yorig);
%
%  Read the grid file
%
ncid = netcdf.open(gridfile, 'NC_NOWRITE');
h=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'h')); 
netcdf.close(ncid);
[Mp,Lp]=size(h);
L=Lp-1;
M=Mp-1;
Np=N+1;
%
%  Create the initial file
%
type = 'INITIAL file' ; 
history = 'CROCO' ;
ncidini = netcdf.create(inifile, 'NC_CLOBBER');
%%result = redef(ncini);
%
%  Create dimensions
%
did_xi_u = netcdf.defDim(ncidini, 'xi_u', L);
did_xi_v = netcdf.defDim(ncidini, 'xi_v', Lp);
did_xi_rho = netcdf.defDim(ncidini, 'xi_rho', Lp);
did_eta_u = netcdf.defDim(ncidini, 'eta_u', Mp);
did_eta_v = netcdf.defDim(ncidini, 'eta_v', M);
did_eta_rho = netcdf.defDim(ncidini, 'eta_rho', Mp);
did_s_rho = netcdf.defDim(ncidini, 's_rho', N);
did_s_w = netcdf.defDim(ncidini, 's_w', Np);
did_tracer = netcdf.defDim(ncidini, 'tracer', 2);
did_time = netcdf.defDim(ncidini, 'time', netcdf.getConstant('NC_UNLIMITED'));
did_one = netcdf.defDim(ncidini, 'one', 1);
%
%  Create variables
%
vid_spherical = netcdf.defVar(ncidini, 'spherical', 'NC_CHAR', did_one);
vid_Vtransform = netcdf.defVar(ncidini, 'Vtransform', 'NC_INT', did_one);
vid_Vstretching = netcdf.defVar(ncidini, 'Vstretching', 'NC_INT', did_one);
vid_tstart = netcdf.defVar(ncidini, 'tstart', 'NC_DOUBLE', did_one);
vid_tend = netcdf.defVar(ncidini, 'tend', 'NC_DOUBLE', did_one);
vid_theta_s = netcdf.defVar(ncidini, 'theta_s', 'NC_DOUBLE', did_one);
vid_theta_b = netcdf.defVar(ncidini, 'theta_b', 'NC_DOUBLE', did_one);
vid_Tcline = netcdf.defVar(ncidini, 'Tcline', 'NC_DOUBLE', did_one);
vid_hc = netcdf.defVar(ncidini, 'hc', 'NC_DOUBLE', did_one);
vid_s_rho = netcdf.defVar(ncidini, 's_rho', 'NC_DOUBLE', did_s_rho);
vid_Cs_rho = netcdf.defVar(ncidini, 'Cs_rho', 'NC_DOUBLE', did_s_rho);
vid_scrum_time = netcdf.defVar(ncidini, 'scrum_time', 'NC_DOUBLE', did_time);
vid_u = netcdf.defVar(ncidini, 'u', 'NC_DOUBLE', [did_xi_u, did_eta_u, did_s_rho, did_time]);
vid_v = netcdf.defVar(ncidini, 'v', 'NC_DOUBLE', [did_xi_v, did_eta_v, did_s_rho, did_time]);
vid_ubar = netcdf.defVar(ncidini, 'ubar', 'NC_DOUBLE', [did_xi_u, did_eta_u, did_time]);
vid_vbar = netcdf.defVar(ncidini, 'vbar', 'NC_DOUBLE', [did_xi_v, did_eta_v, did_time]);
vid_zeta = netcdf.defVar(ncidini, 'zeta', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_time]);
vid_temp = netcdf.defVar(ncidini, 'temp', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_time]);
vid_salt = netcdf.defVar(ncidini, 'salt', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_time]);
if biol==1
    for k=1:length(namebiol)
        vid_char_namebiol_k__ = netcdf.defVar(ncidini, 'char(namebiol(k))', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_time]);
    end
end
if pisces==1
    for k=1:length(namepisces)
        vid_char_namepisces_k__ = netcdf.defVar(ncidini, 'char(namepisces(k))', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_time]);
    end
end
%
%  Create attributes
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Vtransform'), 'long_name', 'vertical terrain-following transformation equation');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Vstretching'), 'long_name', 'vertical terrain-following stretching function');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'tstart'), 'long_name', 'oct_start processing day');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'tstart'), 'units', 'day');
oct_write_time_attributes(ncid,'tstart','',time_unit_att,time_second_unit_att,...
                      calendar_att,0,0);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'tend'), 'long_name', 'end processing day');
oct_write_time_attributes(ncid,'tend','',time_unit_att,time_second_unit_att,...
                      calendar_att,0,0);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'theta_s'), 'long_name', 'S-coordinate surface control parameter');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'theta_s'), 'units', 'nondimensional');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'theta_b'), 'long_name', 'S-coordinate bottom control parameter');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'theta_b'), 'units', 'nondimensional');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Tcline'), 'long_name', 'S-coordinate surface/bottom layer width');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Tcline'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'hc'), 'long_name', 'S-coordinate parameter, critical depth');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'hc'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 's_rho'), 'long_name', 'S-coordinate at RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 's_rho'), 'units', 'nondimensional');
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 's_rho'), 'valid_min', -1);
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 's_rho'), 'valid_max', 0);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 's_rho'), 'field', 's_rho, scalar');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Cs_rho'), 'long_name', 'S-coordinate stretching curves at RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Cs_rho'), 'units', 'nondimensional');
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Cs_rho'), 'valid_min', -1);
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Cs_rho'), 'valid_max', 0);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'Cs_rho'), 'field', 'Cs_rho, scalar');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'scrum_time'), 'long_name', 'time since initialization');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'scrum_time'), 'field', 'time, scalar, series');
oct_write_time_attributes(ncid,'scrum_time','',time_unit_att,time_second_unit_att,...
                      calendar_att,1,0);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'u'), 'long_name', 'u-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'u'), 'units', 'meter second-1');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'u'), 'field', 'u-velocity, scalar, series');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'v'), 'long_name', 'v-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'v'), 'units', 'meter second-1');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'v'), 'field', 'v-velocity, scalar, series');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'ubar'), 'long_name', 'vertically integrated u-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'ubar'), 'units', 'meter second-1');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'ubar'), 'field', 'ubar-velocity, scalar, series');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'vbar'), 'long_name', 'vertically integrated v-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'vbar'), 'units', 'meter second-1');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'vbar'), 'field', 'vbar-velocity, scalar, series');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'zeta'), 'long_name', 'free-surface');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'zeta'), 'units', 'meter');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'zeta'), 'field', 'free-surface, scalar, series');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'temp'), 'long_name', 'potential temperature');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'temp'), 'units', 'Celsius');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'temp'), 'field', 'temperature, scalar, series');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'salt'), 'long_name', 'salinity');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'salt'), 'units', 'PSU');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, 'salt'), 'field', 'salinity, scalar, series');
%
if biol
  for k=1:length(namebiol)
%    disp(['K=',num2str(k)])
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, char(namebiol(k))), 'long_name', char(namebiol(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, char(namebiol(k))), 'units', char(unitbiol(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, char(namebiol(k))), 'field', [char(namebiol(k)),', scalar, series']);
  end
end;
%
if pisces
  for k=1:length(namepisces)
 %   disp(['K=',num2str(k)])
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, char(namepisces(k))), 'long_name', char(namepisces(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, char(namepisces(k))), 'units', char(unitpisces(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidini, netcdf.inqVarID(ncidini, char(namepisces(k))), 'field', [char(namepisces(k)),', scalar, series']);
  end
end;
%
% Create global attributes
%
disp('Create global attribute')
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'title', title);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'date', date);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'clim_file', inifile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'grd_file', gridfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'parent_file', parentfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'type', type);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'history', history);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidini, netcdf.getConstant('NC_GLOBAL'), 'creation_method', 'Nestgui @ CROCOTOOLS');
%
% Leave define mode
%
%%result = endef(ncini);
[s_rho,Cs_rho,s_w,Cs_w] = oct_scoordinate(theta_s,theta_b,N,hc,vtransform);
%
% Write variables
%
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'spherical'), 'T');
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'Vtransform'), vtransform);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'tstart'), time/(24*3600));
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'tend'), time/(24*3600));
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'theta_s'), theta_s);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'theta_b'), theta_b);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'Tcline'), Tcline);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'hc'), hc);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 's_rho'), s_rho);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'Cs_rho'), Cs_rho);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'scrum_time'), 1-1, 1, time);  % [conv] 0-based
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'u'), 0);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'v'), 0);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'zeta'), 0);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'ubar'), 0);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'vbar'), 0);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'temp'), 0);
netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, 'salt'), 0);
%
if biol==1
   % disp('Write variable biology')
    for k=1:length(namebiol)
        netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, char(namebiol(k))), 0);
    end
end
%
if pisces==1
   % disp('Write variable pisces')
    for k=1:length(namepisces)
        netcdf.putVar(ncidini, netcdf.inqVarID(ncidini, char(namepisces(k))), 0);
    end
end
%
%
% Synchronize on disk
%
sync(ncidini);
return


