function cube2netcdf(input, output)

% Transform the data cube (made by add_data.m) into a netCDF 
%   file format. 

comload = ['load ' input];  %(laod the .mat data cube e.g. SITE_allgrids.mat)
eval(comload)
[i,j,k]=size(ZALL);

if input(1:4)=='cort'
    title ='Cortadura-Cadiz';
    [date]=textread('Cortadura_dates.txt' ,'%10c');
elseif input(1:4)=='camp'
    title ='Camposoto-Cadiz';
    [date]=textread('Camposoto_dates.txt' ,'%10c');
else
    title =' ';
end
% CreatenetCDF
nc_create_empty(output)

%Add attributes 

   nc_attput(output, nc_global, 'title'           , title);
   nc_attput(output, nc_global, 'institution'     , 'UCA');
   nc_attput(output, nc_global, 'source'          , 'UCA');
   nc_attput(output, nc_global, 'email'           , 'javier.benavanter@uca.es, haris.plomaritis@uca.es');
   nc_attput(output, nc_global, 'Conventions'     , 'CF-1.4');
   nc_attput(output, nc_global, 'Coordinate_System'     , ...
    ['PROJCS["ED50 / UTM zone 29N"',...
      'GEOGCS["ED50"',...
      '  DATUM["European_Datum_1950"',...
      '      SPHEROID["International 1924",6378388,297',...
      '          AUTHORITY["EPSG","7022"]]',...
      '      AUTHORITY["EPSG","6230"]]',...
      '  PRIMEM["Greenwich",0',...
      '      AUTHORITY["EPSG","8901"]]',...
      '  UNIT["degree",0.01745329251994328',...
      '      AUTHORITY["EPSG","9122"]]',...
      '  AUTHORITY["EPSG","4230"]]',...
      '  UNIT["metre",1',...
      '  AUTHORITY["EPSG","9001"]]',...
      ' PROJECTION["Transverse_Mercator"]',...
      ' PARAMETER["latitude_of_origin",0]',...
      ' PARAMETER["central_meridian",-9]',...
      ' PARAMETER["scale_factor",0.9996]',...
      ' PARAMETER["false_easting",500000]',...
      ' PARAMETER["false_northing",0]',...
      ' AUTHORITY["EPSG","23029"]',...
      ' AXIS["Easting",EAST]',...
      ' AXIS["Northing",NORTH]]']);
 nc_attput(output, nc_global, 'Vertical_Datum'     , 'Cadiz Local Hydrographic Zero (+43.556 above elipsoide)');

% Add dimensions 
nc_add_dimension(output, 'Easting', j)
nc_add_dimension(output, 'Northing', i)
nc_add_dimension(output, 'time', k)
nc_add_dimension(output, 'time_str', 10)

% Add variables

EastingVariable = struct(...
    'Name', 'Easting', ...
    'Nctype', 'float', ...
    'Dimension', {{'Easting'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(output, EastingVariable);

NorthingVariable = struct(...
    'Name', 'Northing', ...
    'Nctype', 'float', ...
    'Dimension', {{'Northing'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'metre') ...
    );
nc_addvar(output, NorthingVariable);

timeVariable = struct(...
    'Name', 'time', ...
    'Nctype', 'char', ...
    'Dimension', {{'time','time_str'}}, ...
    'Attribute', struct('Name', 'unit', 'Value', 'date dd/mm/yyyy') ...
    );
nc_addvar(output, timeVariable);

elevationVariable = struct(...
    'Name', 'elevation', ...
    'Nctype', 'float', ...
    'Dimension', {{'Northing','Easting','time'}}, ...
    'Attribute',  struct('Name', 'unit', 'Value', 'metre above Hydrographic Zero') ...
    );
nc_addvar(output, elevationVariable);

% Store Variable

nc_varput(output, 'Easting', x(1,:))
nc_varput(output, 'Northing', y(:,1))
nc_varput(output, 'time', date)
nc_varput(output, 'elevation', ZALL)

