function nc_produce_auto(filename,dims,vars,atts)

%  NC_PROD_AUTO creates a netCDF file ready for population.
%     [VARARGOUT] = NC_PRODUCE_AUTO(FILENAME,DIMS,VARS,ATTS) creates
%     a netCDF file from the information provided in the STRUCT array
%     DIMS (dimensions) , VARS (variables) and ATTS (attributes).
%
%     Input must take the following form:
%       DIMS: dims.name           VARS: vars.name            (%s)
%             dims.standard_name        vars.standard_name   (%s)
%             dims.long_name            vars.long_name       (%s)
%             dims.units                vars.units           (%s)
%             dims.type                 vars.type            (%s)
%             dims.length                                    (%i)
%                                       vars.fillval         (%f)
%                                       vars.dims            (%s)
%       ATTS: atts.desc                                      (%s)
%             atts.author                                    (%s)
%
%     Author: J Harle (jdha@noc.soton.ac.uk) 30-5-07
%
%     See also netcdf nc_var nc_att nc_produce_manu

% Error handling

if nargout > 1
    error('Only one output allowed')
end

% Check all dimensions in VARS are defined in DIMS

ncquiet % no NetCDF warnings.

nc = netcdf(filename, 'clobber') ;                 % Create NetCDF file

% Defining dimensions of the data set

no_dim = length(dims) ;

if mod(no_dim,1)~=0
    error('Length must be an integer number')
end

for i=1:no_dim
    if mod(dims(i).length,1)==0
        eval(['nc(''' dims(i).name ''')=dims(i).length;'])
        eval(['nc{''' dims(i).name '''}=nc' dims(i).type '(''' dims(i).name ''');'])
        eval(['nc{''' dims(i).name '''}.units=dims(i).units;'])
        eval(['nc{''' dims(i).name '''}.standard_name=dims(i).standard_name;'])
        eval(['nc{''' dims(i).name '''}.long_name=dims(i).long_name;'])
    else
        error('Length must be an integer number')
    end
end

% Defining variables of the data set

no_var = length(vars) ;

if mod(no_var,1)~=0
    error('Length must be an integer number')
end

for i=1:no_var
    no_dim_var = length(vars(i).dims) ;
    d='';
    k=1;
   
    while k<no_dim_var
        d = ([d '''' (vars(i).dims{k}) ''',']) ;
        k = k+1 ;
    end

    d = ([d '''' (vars(i).dims{k}) '''']) ;
    
    eval(['nc{''' vars(i).name '''}=nc' vars(i).type '(' d ');']);
    eval(['nc{''' vars(i).name '''}.units=vars(i).units;'])
    eval(['nc{''' vars(i).name '''}.standard_name=vars(i).standard_name;'])
    eval(['nc{''' vars(i).name '''}.long_name=vars(i).long_name;'])
    eval(['nc{''' vars(i).name '''}.FillValue_=vars(i).fillval;'])
    
end

% Global attributes.

%JMB nc.description = atts.desc;

%JMB nc.author = atts.author;

%JMB nc.date = date;

%JMB added
nc.source = atts.source;
nc.author = atts.author;
nc.institute = atts.inst;
nc.title = atts.title;
nc.Conventions = atts.conv;
nc.date = date;

% ---------------------------- STORE THE DATA --------------------------- %

close(nc);

% ---------------------------- RECALL THE FILE -------------------------- %

disp( ['Opening file: ' filename] )
nc = netcdf(filename, 'nowrite');         % Open NetCDF file.
disp( nc.description(:) )
disp( 'File contains:' )
nc_var( nc )

% Close the file or redirect to out

% if nargout == 0
    close(nc)
% else nargout == 1
%     varargout = nc ;
% end

% --------------------------------- DONE -------------------------------- %
