function  oct_create_runoff(runoffname,grdname,title,...
    qbart,qbarc,rivername,rivernumber,...
    runoffname_StrLen,dir,psource_ncfile_ts,...
    biol,pisces,quota, Yorig)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 	Create an empty oct_netcdf runoff file
%       runoffname: name of the runoff file
%       grdname: name of the grid file
%       title: title in the oct_netcdf file
%
%
%  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
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
timeref = 1 ;
if nargin < 14
  disp([' Yorig parameter found'])
  disp([' No time reference'])
  timeref = 0 ;
end
if timeref == 1
  time_unit_att = ['days since ',sprintf('%04d', Yorig),'-01-01 00:00:00'];
  calendar_att = 'proleptic_gregorian' ;
else
  time_unit_att = ['days'];
  calendar_att = 'climatological year' ;
end
%
nw_id = netcdf.create(runoffname, 'NC_CLOBBER');
%%%result = redef(nw);
%
%  Create dimensions
%
did_qbar_time = netcdf.defDim(nw_id, 'qbar_time', length(qbart));
did_n_qbar = netcdf.defDim(nw_id, 'n_qbar', rivernumber);
did_runoffname_StrLen = netcdf.defDim(nw_id, 'runoffname_StrLen', runoffname_StrLen);
did_one = netcdf.defDim(nw_id, 'one', 1);
did_two = netcdf.defDim(nw_id, 'two', 2);
%
%  Create variables and attributes
%
vid_qbar_time = netcdf.defVar(nw_id, 'qbar_time', 'NC_DOUBLE', did_qbar_time);
% [conv] línea ncchar duplicada omitida
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'qbar_time'), 'calendar', calendar_att);
netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'qbar_time'), 'cycle_length', qbarc);

if psource_ncfile_ts
    vid_temp_src_time = netcdf.defVar(nw_id, 'temp_src_time', 'NC_DOUBLE', did_qbar_time);
    % [conv] línea ncchar duplicada omitida
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'temp_src_time'), 'calendar', calendar_att);
    netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'temp_src_time'), 'cycle_length', qbarc);
    
    vid_salt_src_time = netcdf.defVar(nw_id, 'salt_src_time', 'NC_DOUBLE', did_qbar_time);
    % [conv] línea ncchar duplicada omitida
    % [conv] línea ncchar duplicada omitida
    netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'salt_src_time'), 'calendar', calendar_att);
    netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'salt_src_time'), 'cycle_length', qbarc);
end
vid_runoff_name = netcdf.defVar(nw_id, 'runoff_name', 'NC_CHAR', [did_runoffname_StrLen, did_n_qbar]);
% [conv] línea ncchar duplicada omitida

%% => actually not used/ read in croco.in

%%nw{'runoff_position'} = ncdouble('n_qbar','two');
%%nw{'runoff_position'}.long_name = ncchar('position of the runoff (by line) in the CROCO grid');

%%nw{'runoff_direction'} = ncdouble('n_qbar','two');
%%nw{'runoff_direction'}.long_name = ncchar('direction/sense of the runoff (by line) in the CROCO grid');

vid_Qbar = netcdf.defVar(nw_id, 'Qbar', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
% [conv] línea ncchar duplicada omitida
% [conv] línea ncchar duplicada omitida

if psource_ncfile_ts
    vid_temp_src = netcdf.defVar(nw_id, 'temp_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
    % [conv] línea ncchar duplicada omitida
    % [conv] línea ncchar duplicada omitida
    
    vid_salt_src = netcdf.defVar(nw_id, 'salt_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
    % [conv] línea ncchar duplicada omitida
    % [conv] línea ncchar duplicada omitida
    
    if biol
        vid_no3_src_time = netcdf.defVar(nw_id, 'no3_src_time', 'NC_DOUBLE', did_qbar_time);
        % [conv] línea ncchar duplicada omitida
        % [conv] línea ncchar duplicada omitida
        netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'no3_src_time'), 'calendar', calendar_att);
        netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'no3_src_time'), 'cycle_length', 360);
        
        vid_NO3_src = netcdf.defVar(nw_id, 'NO3_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
        % [conv] línea ncchar duplicada omitida
        % [conv] línea ncchar duplicada omitida
        if pisces
           vid_po4_src_time = netcdf.defVar(nw_id, 'po4_src_time', 'NC_DOUBLE', did_qbar_time);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'po4_src_time'), 'calendar', calendar_att);
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'po4_src_time'), 'cycle_length', 360);

           vid_PO4_src = netcdf.defVar(nw_id, 'PO4_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida

           vid_si_src_time = netcdf.defVar(nw_id, 'si_src_time', 'NC_DOUBLE', did_qbar_time);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'si_src_time'), 'calendar', calendar_att);
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'si_src_time'), 'cycle_length', 360);

           vid_Si_src = netcdf.defVar(nw_id, 'Si_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida

           vid_dic_src_time = netcdf.defVar(nw_id, 'dic_src_time', 'NC_DOUBLE', did_qbar_time);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'dic_src_time'), 'calendar', calendar_att);
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'dic_src_time'), 'cycle_length', 360);

           vid_DIC_src = netcdf.defVar(nw_id, 'DIC_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida

           vid_doc_src_time = netcdf.defVar(nw_id, 'doc_src_time', 'NC_DOUBLE', did_qbar_time);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'doc_src_time'), 'calendar', calendar_att);
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'doc_src_time'), 'cycle_length', 360);

           vid_DOC_src = netcdf.defVar(nw_id, 'DOC_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida

           vid_talk_src_time = netcdf.defVar(nw_id, 'talk_src_time', 'NC_DOUBLE', did_qbar_time);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'talk_src_time'), 'calendar', calendar_att);
           netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'talk_src_time'), 'cycle_length', 360);

           vid_TALK_src = netcdf.defVar(nw_id, 'TALK_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
           % [conv] línea ncchar duplicada omitida
           % [conv] línea ncchar duplicada omitida
           if quota
              vid_don_src_time = netcdf.defVar(nw_id, 'don_src_time', 'NC_DOUBLE', did_qbar_time);
              % [conv] línea ncchar duplicada omitida
              % [conv] línea ncchar duplicada omitida
              netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'don_src_time'), 'calendar', calendar_att);
              netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'don_src_time'), 'cycle_length', 360);

              vid_DON_src = netcdf.defVar(nw_id, 'DON_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
              % [conv] línea ncchar duplicada omitida
              % [conv] línea ncchar duplicada omitida
            
              vid_dop_src_time = netcdf.defVar(nw_id, 'dop_src_time', 'NC_DOUBLE', did_qbar_time);
              % [conv] línea ncchar duplicada omitida
              % [conv] línea ncchar duplicada omitida
              netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'dop_src_time'), 'calendar', calendar_att);
              netcdf.putAtt(nw_id, netcdf.inqVarID(nw_id, 'dop_src_time'), 'cycle_length', 360);

              vid_DOP_src = netcdf.defVar(nw_id, 'DOP_src', 'NC_DOUBLE', [did_qbar_time, did_n_qbar]);
              % [conv] línea ncchar duplicada omitida
              % [conv] línea ncchar duplicada omitida
           end
        end
    end
end
%%%result = endef(nw);

%
% Create global attributes
%
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(nw_id, netcdf.getConstant('NC_GLOBAL'), 'title', title);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(nw_id, netcdf.getConstant('NC_GLOBAL'), 'date', date);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(nw_id, netcdf.getConstant('NC_GLOBAL'), 'grd_file', grdname);
% [conv] línea ncchar duplicada omitida
netcdf.putAtt(nw_id, netcdf.getConstant('NC_GLOBAL'), 'type', 'CROCO runoff file');

%
% Write time variables
netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'qbar_time'), qbart);
if psource_ncfile_ts
    netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'temp_src_time'), qbart);
    netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'salt_src_time'), qbart);
    if biol
         netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'no3_src_time'), qbart);
         if pisces
            netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'po4_src_time'), qbart);
            netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'si_src_time'), qbart);
            netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'dic_src_time'), qbart);
            netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'doc_src_time'), qbart);
            netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'talk_src_time'), qbart);
            if quota
               netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'don_src_time'), qbart);
               netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'dop_src_time'), qbart);
            end
         end
    end
end
for k=1:rivernumber
    netcdf.putVar(nw_id, netcdf.inqVarID(nw_id, 'runoff_name'), k,:-1, 1, rivername(k,:));  % [conv] 0-based
end
%
netcdf.close(nw_id);
