function ncidrst=oct_create_nestedrestart(rstfile,gridfile,parentfile,title,clobber,...
				    biol,pisces,namebiol,namepisces,unitbiol,unitpisces,hc,vtransform)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  function ncrst=oct_create_nestedrestart(rstfile,gridfile,parentfile,...
%                                      title,clobber)
%
%   This function create the header of a Netcdf initial 
%   file.
%
%   Input: 
%
%   rstfile     Netcdf restart file name (character string)
%   gridfile    Netcdf grid file name (character string).
%   parentfile  Netcdf oct_parent restart file name (character string).
%   clobber      Switch to allow or not writing over an existing
%                file.(character string)
%
%   Output
%
%   ncrst       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(' ')
%biol
%pisces
%namebiol
%namepisces
%unitbiol
%unitpisces

disp(['Creating the file : ',rstfile])
disp(' ')
%
%  Open the grid file
%
ncidgrd = netcdf.open(gridfile, 'NC_NOWRITE');
%
%  Open the oct_parent file
%
ncidprt = netcdf.open(parentfile, 'NC_NOWRITE');
%
%  Create the restart file
%
type = 'restart file' ; 
history = 'CROCO' ;
ncidrst = netcdf.create(rstfile, 'NC_CLOBBER');
%result = redef(ncrst);

%
%  Create dimensions
%
did_xi_u = netcdf.defDim(ncidrst, 'xi_u', length(ncgrd('xi_u')));
did_xi_rho = netcdf.defDim(ncidrst, 'xi_rho', length(ncgrd('xi_rho')));
did_eta_v = netcdf.defDim(ncidrst, 'eta_v', length(ncgrd('eta_v')));
did_eta_rho = netcdf.defDim(ncidrst, 'eta_rho', length(ncgrd('eta_rho')));
did_s_rho = netcdf.defDim(ncidrst, 's_rho', length(ncprt('s_rho')));
did_s_w = netcdf.defDim(ncidrst, 's_w', length(ncprt('s_w')));
did_one = netcdf.defDim(ncidrst, 'one', 1);
did_auxil = netcdf.defDim(ncidrst, 'auxil', 4);
did_time = netcdf.defDim(ncidrst, 'time', netcdf.getConstant('NC_UNLIMITED'));
did_one = netcdf.defDim(ncidrst, 'one', 1);
%
%  Create variables
%
vid_spherical = netcdf.defVar(ncidrst, 'spherical', 'NC_CHAR', did_one);
vid_el = netcdf.defVar(ncidrst, 'el', 'NC_DOUBLE', did_one);
vid_xl = netcdf.defVar(ncidrst, 'xl', 'NC_DOUBLE', did_one);
% ncrst{'xi_rho'} = ncdouble('xi_rho');
% ncrst{'xi_u'} = ncdouble('xi_u');
% ncrst{'eta_rho'} = ncdouble('eta_rho');
% ncrst{'eta_v'} = ncdouble('eta_v');
vid_Vtransform = netcdf.defVar(ncidrst, 'Vtransform', 'NC_DOUBLE', did_one);
vid_h = netcdf.defVar(ncidrst, 'h', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_f = netcdf.defVar(ncidrst, 'f', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_pm = netcdf.defVar(ncidrst, 'pm', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_pn = netcdf.defVar(ncidrst, 'pn', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_lon_rho = netcdf.defVar(ncidrst, 'lon_rho', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_lat_rho = netcdf.defVar(ncidrst, 'lat_rho', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_angle = netcdf.defVar(ncidrst, 'angle', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_mask_rho = netcdf.defVar(ncidrst, 'mask_rho', 'NC_DOUBLE', [did_xi_rho, did_eta_rho]);
vid_time_step = netcdf.defVar(ncidrst, 'time_step', 'NC_INT', [did_auxil, did_time]);
vid_scrum_time = netcdf.defVar(ncidrst, 'scrum_time', 'NC_DOUBLE', did_time);
vid_u = netcdf.defVar(ncidrst, 'u', 'NC_DOUBLE', [did_xi_u, did_eta_rho, did_s_rho, did_time]);
vid_v = netcdf.defVar(ncidrst, 'v', 'NC_DOUBLE', [did_xi_rho, did_eta_v, did_s_rho, did_time]);
vid_ubar = netcdf.defVar(ncidrst, 'ubar', 'NC_DOUBLE', [did_xi_u, did_eta_rho, did_time]);
vid_vbar = netcdf.defVar(ncidrst, 'vbar', 'NC_DOUBLE', [did_xi_rho, did_eta_v, did_time]);
vid_zeta = netcdf.defVar(ncidrst, 'zeta', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_time]);
vid_temp = netcdf.defVar(ncidrst, 'temp', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_time]);
vid_salt = netcdf.defVar(ncidrst, '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(ncidrst, '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(ncidrst, '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(ncidrst, netcdf.inqVarID(ncidrst, 'spherical'), 'long_name', 'grid type logical switch');
%

% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'xl'), 'long_name', 'domain length in the XI-direction');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'xl'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'el'), 'long_name', 'domain length in the ETA-direction');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'el'), 'units', 'meter');
%
% ncrst{'xi_rho'}.long_name = ncchar('x-dimension of the grid');
% ncrst{'xi_rho'}.long_name = 'x-dimension of the grid';
% ncrst{'xi_rho'}.standard_name = 'x_grid_index_at_u_location' ;
% %
% ncrst{'xi_u'}.long_name = ncchar('x-dimension of the grid at u location');
% ncrst{'xi_u'}.long_name = 'x-dimension of the grid at u location';
% ncrst{'xi_u'}.standard_name = 'x_grid_index_at_u_location' ;
% %
% ncrst{'eta_rho'}.long_name = ncchar('y-dimension of the grid');
% ncrst{'eta_rho'}.long_name = 'y-dimension of the grid';
% ncrst{'eta_rho'}.standard_name = 'x_grid_index_at_v_location'
% %
% ncrst{'eta_v'}.long_name = ncchar('y-dimension of the grid at v location');
% ncrst{'eta_v'}.long_name = 'y-dimension of the grid at v location';
% ncrst{'eta_v'}.standard_name = 'x_grid_index_at_v_location';
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'Vtransform'), 'long_name', 'vertical terrain-following transformation equation');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'h'), 'long_name', 'bathymetry at RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'h'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'f'), 'long_name', 'Coriolis parameter at RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'f'), 'units', 'second-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'pm'), 'long_name', 'curvilinear coordinate metric in XI');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'pm'), 'units', 'meter-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'pn'), 'long_name', 'curvilinear coordinate metric in ETA');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'pn'), 'units', 'meter-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'lon_rho'), 'long_name', 'longitude of RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'lon_rho'), 'units', 'degree_east');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'lat_rho'), 'long_name', 'latitude of RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'lat_rho'), 'units', 'degree_north');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'angle'), 'long_name', 'angle between XI-axis and EAST');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'angle'), 'units', 'radians');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'mask_rho'), 'long_name', 'mask on RHO-points');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'time_step'), 'long_name', 'time step and record numbers from initialization');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'scrum_time'), 'long_name', 'time since intialization');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'scrum_time'), 'units', 'second');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'u'), 'long_name', 'u-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'u'), 'units', 'meter second-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'v'), 'long_name', 'v-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'v'), 'units', 'meter second-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'ubar'), 'long_name', 'vertically integrated u-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'ubar'), 'units', 'meter second-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'vbar'), 'long_name', 'vertically integrated v-momentum component');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'vbar'), 'units', 'meter second-1');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'zeta'), 'long_name', 'free-surface');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'zeta'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'temp'), 'long_name', 'potential temperature');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'temp'), 'units', 'Celsius');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'salt'), 'long_name', 'salinity');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 'salt'), 'units', 'PSU');

%
if  biol == 1
  for k=1:length(namebiol)
    %disp(['K=',num2str(k)])
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, char(namebiol(k))), 'long_name', char(namebiol(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, char(namebiol(k))), 'units', char(unitbiol(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, char(namebiol(k))), 'field', [char(namebiol(k)),', scalar, series']);
  end
end
%
if pisces ==1
  for k=1:length(namepisces)
    %disp(['K=',num2str(k)])
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, char(namepisces(k))), 'long_name', char(namepisces(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, char(namepisces(k))), 'units', char(unitpisces(k)));
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(ncidrst, netcdf.inqVarID(ncidrst, 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(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'type', type);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'title', title);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'date', date);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'rst_file', rstfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'grd_file', gridfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'parent_file', parentfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'history', history);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'creation_method', 'Nestgui @ CROCOTOOLS');
%
% Get the vertical grid
%
disp('Get the vertical grid')
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'theta_s', ncprt.theta_s(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'theta_b', ncprt.theta_b(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'hc', ncprt.hc(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 's_w', ncprt.s_w(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'Cs_w', ncprt.Cs_w(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 's_rho', ncprt.s_rho(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'Cs_rho', ncprt.Cs_rho(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'ntimes', ncprt.ntimes(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'ndtfast', ncprt.ndtfast(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'dt', ncprt.dt(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'dtfast', ncprt.dtfast(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'nwrt', ncprt.nwrt(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'visc2', ncprt.visc2(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'rdrg', ncprt.rdrg(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'rdrg2', ncprt.rdrg2(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'rho0', ncprt.rho0(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'gamma2', ncprt.gamma2(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'SRCS', ncprt.SRCS(:));
netcdf.putAtt(ncidrst, netcdf.getConstant('NC_GLOBAL'), 'CPPS', ncprt.CPPS(:));

%
% Leave define mode
%
%result = endef(ncrst);
%
% Fill variables
%
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'spherical'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'spherical')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'el'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'el')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'xl'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'xl')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'h'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'h')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'f'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'f')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'pm'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'pm')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'pn'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'pn')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'lon_rho'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'lon_rho')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'lat_rho'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'lat_rho')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'angle'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'angle')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'mask_rho'), netcdf.getVar(ncidgrd, netcdf.inqVarID(ncidgrd, 'mask_rho')));
netcdf.putVar(ncidrst, netcdf.inqVarID(ncidrst, 'Vtransform'), vtransform);
%
% Synchronize on disk
%
sync(ncidrst);
netcdf.close(ncidgrd);
netcdf.close(ncidprt);
return


