function ncidclim=oct_create_nestedclim(climfile,gridfile,parentfile,title,...
				  theta_s,theta_b,Tcline,N,...
				  ttime,stime,utime,vtime,sshtime,...
				  tcycle,scycle,ucycle,vcycle,sshcycle,...
                  tbiol, cbiol,tpisces,cpisces,...
                  clobber,...    
				  biol,pisces,timebiol,cyclebiol,timepisces,cyclepisces, ...
                  namebiol,namepisces,unitbiol,unitpisces,hc,vtransform,Yorig)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  function ncclim=oct_create_climfile(climfile,gridfile,theta_s,...
%                  theta_b,Tcline,N,...
%            ttime,stime,utime,vtime,sshtime,...
%            tcycle,scycle,ucycle,vcycle,sshcycle,...
%                  clobber) 
%
%   This function create the header of a Netcdf climatology 
%   file.
%
%   Input: 
%
%   climfile     Netcdf climatology file name (character string)
%   gridfile     Netcdf grid 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)
%   ttime        Temperature climatology time.(vector) 
%   stime        Salinity climatology time.(vector)
%   utime        Velocity climatology time.(vector)
%   cycle        Length (days) for cycling the climatology.(Real)
%   clobber      Switch to allow or not writing over an existing
%                file.(character string)
%
%   Output
%
%   ncclim       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  
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% options for oct_write_time_attributes
insecond = 0;
add_cycle = 1; 
%
% 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);
%
disp(' ')
disp(' ')
disp(['Creating the file : ',climfile])
disp(' ')
%
%  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 climatology file
%
type = 'CLIMATOLOGY file' ;
history = 'CROCO' ;
ncidclim = netcdf.create(climfile, 'NC_CLOBBER');
%result = redef(ncclim);
%
%  Create dimensions
%
did_xi_u = netcdf.defDim(ncidclim, 'xi_u', L);
did_xi_v = netcdf.defDim(ncidclim, 'xi_v', Lp);
did_xi_rho = netcdf.defDim(ncidclim, 'xi_rho', Lp);
did_eta_u = netcdf.defDim(ncidclim, 'eta_u', Mp);
did_eta_v = netcdf.defDim(ncidclim, 'eta_v', M);
did_eta_rho = netcdf.defDim(ncidclim, 'eta_rho', Mp);
did_s_rho = netcdf.defDim(ncidclim, 's_rho', N);
did_s_w = netcdf.defDim(ncidclim, 's_w', Np);
did_tracer = netcdf.defDim(ncidclim, 'tracer', 2);
did_tclm_time = netcdf.defDim(ncidclim, 'tclm_time', length(ttime));
did_sclm_time = netcdf.defDim(ncidclim, 'sclm_time', length(stime));
did_uclm_time = netcdf.defDim(ncidclim, 'uclm_time', length(utime));
did_vclm_time = netcdf.defDim(ncidclim, 'vclm_time', length(vtime));
did_ssh_time = netcdf.defDim(ncidclim, 'ssh_time', length(sshtime));

if biol
    for k=1:length(timebiol)
       % disp(char(timebiol(k)))
        ncidclim(char(timebiol(k))) = length(tbiol(k,:));
    end
end
%
if pisces
    for k=1:length(timepisces)
       % disp(char(timepisces(k)))
        ncidclim(char(timepisces(k))) = length(tpisces(k,:));
    end
end
did_one = netcdf.defDim(ncidclim, 'one', 1);
%
%  Create variables
%
vid_spherical = netcdf.defVar(ncidclim, 'spherical', 'NC_CHAR', did_one);
vid_Vtransform = netcdf.defVar(ncidclim, 'Vtransform', 'NC_INT', did_one);
vid_tstart = netcdf.defVar(ncidclim, 'tstart', 'NC_DOUBLE', did_one);
vid_tend = netcdf.defVar(ncidclim, 'tend', 'NC_DOUBLE', did_one);
vid_theta_s = netcdf.defVar(ncidclim, 'theta_s', 'NC_DOUBLE', did_one);
vid_theta_b = netcdf.defVar(ncidclim, 'theta_b', 'NC_DOUBLE', did_one);
vid_Tcline = netcdf.defVar(ncidclim, 'Tcline', 'NC_DOUBLE', did_one);
vid_hc = netcdf.defVar(ncidclim, 'hc', 'NC_DOUBLE', did_one);
vid_s_rho = netcdf.defVar(ncidclim, 's_rho', 'NC_DOUBLE', did_s_rho);
vid_Cs_rho = netcdf.defVar(ncidclim, 'Cs_rho', 'NC_DOUBLE', did_s_rho);
vid_tclm_time = netcdf.defVar(ncidclim, 'tclm_time', 'NC_DOUBLE', did_tclm_time);
vid_sclm_time = netcdf.defVar(ncidclim, 'sclm_time', 'NC_DOUBLE', did_sclm_time);
vid_uclm_time = netcdf.defVar(ncidclim, 'uclm_time', 'NC_DOUBLE', did_uclm_time);
vid_vclm_time = netcdf.defVar(ncidclim, 'vclm_time', 'NC_DOUBLE', did_vclm_time);
vid_ssh_time = netcdf.defVar(ncidclim, 'ssh_time', 'NC_DOUBLE', did_ssh_time);
vid_temp = netcdf.defVar(ncidclim, 'temp', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_tclm_time]);
vid_salt = netcdf.defVar(ncidclim, 'salt', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_s_rho, did_sclm_time]);
vid_u = netcdf.defVar(ncidclim, 'u', 'NC_DOUBLE', [did_xi_u, did_eta_u, did_s_rho, did_uclm_time]);
vid_v = netcdf.defVar(ncidclim, 'v', 'NC_DOUBLE', [did_xi_v, did_eta_v, did_s_rho, did_vclm_time]);
vid_ubar = netcdf.defVar(ncidclim, 'ubar', 'NC_DOUBLE', [did_xi_u, did_eta_u, did_uclm_time]);
vid_vbar = netcdf.defVar(ncidclim, 'vbar', 'NC_DOUBLE', [did_xi_v, did_eta_v, did_vclm_time]);
vid_SSH = netcdf.defVar(ncidclim, 'SSH', 'NC_DOUBLE', [did_xi_rho, did_eta_rho, did_ssh_time]);
%
%
%  Create attributes
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Vtransform'), 'long_name', 'vertical terrain-following transformation equation');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Vstretching'), 'long_name', 'vertical terrain-following stretching function');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'tstart'), 'long_name', 'oct_start processing day');
oct_write_time_attributes(ncid,'tstart',tcycle,time_unit_att,time_second_unit_att,...
                      calendar_att,0,0);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'tend'), 'long_name', 'end processing day');
oct_write_time_attributes(ncid,'tend',tcycle,time_unit_att,time_second_unit_att,...
                      calendar_att,0,0);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'theta_s'), 'long_name', 'S-coordinate surface control parameter');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'theta_s'), 'units', 'nondimensional');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'theta_b'), 'long_name', 'S-coordinate bottom control parameter');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'theta_b'), 'units', 'nondimensional');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Tcline'), 'long_name', 'S-coordinate surface/bottom layer width');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Tcline'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'hc'), 'long_name', 'S-coordinate parameter, critical depth');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'hc'), 'units', 'meter');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 's_rho'), 'long_name', 'S-coordinate at RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 's_rho'), 'units', 'nondimensional');
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 's_rho'), 'valid_min', -1);
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 's_rho'), 'valid_max', 0);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 's_rho'), 'field', 's_rho, scalar');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Cs_rho'), 'long_name', 'S-coordinate stretching curves at RHO-points');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Cs_rho'), 'units', 'nondimensional');
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Cs_rho'), 'valid_min', -1);
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Cs_rho'), 'valid_max', 0);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'Cs_rho'), 'field', 'Cs_rho, scalar');
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'tclm_time'), 'long_name', 'time for temperature climatology');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'tclm_time'), 'field', 'tclm_time, scalar, series');
oct_write_time_attributes(ncid,'tclm_time',tcycle,time_unit_att,time_second_unit_att,...
                        calendar_att,insecond,add_cycle);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'sclm_time'), 'long_name', 'time for salinity climatology');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'sclm_time'), 'field', 'sclm_time, scalar, serie');
oct_write_time_attributes(ncid,'sclm_time',scycle,time_unit_att,time_second_unit_att,...
                        calendar_att,insecond,add_cycle);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'uclm_time'), 'long_name', 'time climatological u');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'uclm_time'), 'field', 'uclm_time, scalar, serie');
oct_write_time_attributes(ncid,'uclm_time',ucycle,time_unit_att,time_second_unit_att,...
                        calendar_att,insecond,add_cycle);
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'vclm_time'), 'long_name', 'time climatological v');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'vclm_time'), 'field', 'vclm_time, scalar, serie');
oct_write_time_attributes(ncid,'vclm_time',vcycle,time_unit_att,time_second_unit_att,...
                        calendar_att,insecond,add_cycle);

%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'ssh_time'), 'long_name', 'time for sea surface height');
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, 'ssh_time'), 'field', 'ssh_time, scalar, serie');
oct_write_time_attributes(ncid,'ssh_time',sshcycle,time_unit_att,time_second_unit_att,...
                        calendar_att,insecond,add_cycle);
%
if biol
    for k=1:length(timebiol)
       % disp(char(timebiol(k)))
        ncidclim{char(timebiol(k))} = ncdouble(char(timebiol(k,:)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(timebiol(k))), 'long_name', char(timebiol(k)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(timebiol(k))), 'field', [char(timebiol(k)),', scalar, series']);
        oct_write_time_attributes(ncid,char(timebiol(k)),cbiol,time_unit_att,time_second_unit_att,...
                        calendar_att,insecond,add_cycle);

        %     
       % disp(char(namebiol(k)))
        ncidclim{char(namebiol(k))} = ncdouble(char(timebiol(k,:)),'s_rho','eta_rho','xi_rho');
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(namebiol(k))), 'long_name', char(namebiol(k)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(namebiol(k))), 'units', char(unitbiol(k)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(namebiol(k))), 'field', [char(namebiol(k)),', scalar, series']);
        %
        %disp('Done')
    end
end
%
if pisces
    for k=1:length(timepisces)
        %disp(char(timepisces(k)))
        ncidclim{char(timepisces(k))} = ncdouble(char(timepisces(k,:)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(timepisces(k))), 'long_name', char(timepisces(k)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(timepisces(k))), 'field', [char(timepisces(k)), ',scalar, series']);
        oct_write_time_attributes(ncid,char(timepisces(k)),cpisces,time_unit_att,time_second_unit_att,...
                                calendar_att,insecond,add_cycle);
        %
        %disp(char(namepisces(k)))
        ncidclim{char(namepisces(k))} = ncdouble(char(timepisces(k,:)),'s_rho','eta_rho','xi_rho');
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(namepisces(k))), 'long_name', char(namepisces(k)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(namepisces(k))), 'units', char(unitpisces(k)));
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(ncidclim, netcdf.inqVarID(ncidclim, char(namepisces(k))), 'field', [char(namepisces(k)),', scalar, series']);
    end
end

%
% Create global attributes
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'title', title);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'date', date);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'clim_file', climfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'grd_file', gridfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'parent_file', parentfile);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'type', type);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(ncidclim, netcdf.getConstant('NC_GLOBAL'), 'history', history);
%
% Leave define mode
%
%result = endef(ncclim);
%
[s_rho,Cs_rho,s_w,Cs_w] = oct_scoordinate(theta_s,theta_b,N,hc,vtransform);
%
% Write variables
%
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'spherical'), 'T');
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'Vtransform'), vtransform);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'tstart'), min([min(ttime) min(stime) min(utime)]));
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'tend'), max([max(ttime) max(stime) max(utime)]));
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'theta_s'), theta_s);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'theta_b'), theta_b);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'Tcline'), Tcline);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'hc'), hc);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 's_rho'), s_rho);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'Cs_rho'), Cs_rho);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'tclm_time'), ttime);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'sclm_time'), stime);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'uclm_time'), utime);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'vclm_time'), vtime);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'ssh_time'), sshtime);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'u'), 0);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'v'), 0);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'ubar'), 0);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'vbar'), 0);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'SSH'), 0);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'temp'), 0);
netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, 'salt'), 0);
%
if biol
   % disp('Write variable biology')
    for k=1:length(namebiol)
        eval(['netcdf.getVar(ncidclim, netcdf.inqVarID(ncidclim, ''',char(timebiol(k)),'''))=squeeze(tbiol(k,:));']);
    end
    for k=1:length(namebiol)
        netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, char(namebiol(k))), 0);
    end
end

%
if pisces
   % disp('Write variable pisces')
    for k=1:length(namepisces)
        eval(['netcdf.getVar(ncidclim, netcdf.inqVarID(ncidclim, ''',char(timepisces(k)),'''))=squeeze(tpisces(k,:));']);
    end
    for k=1:length(namepisces)
        netcdf.putVar(ncidclim, netcdf.inqVarID(ncidclim, char(namepisces(k))), 0);
    end
end
%
%
% Synchronize on disk
%
sync(ncidclim);
return


