function wave2netcdf(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]=size(DATE);


% CreatenetCDF
nc_create_empty(output)

%Add attributes 

   nc_attput(output, nc_global, 'title'           , 'Cadiz Coastal Wave Data');
   nc_attput(output, nc_global, 'institution'     , 'UCA');
   nc_attput(output, nc_global, 'source'          , 'Puertos Del Estado');
   nc_attput(output, nc_global, 'email'           , 'haris.plomaritis@uca.es, sperez@puertos.es');
   nc_attput(output, nc_global, 'Conventions'     , 'CF-1.4');
   nc_attput(output, nc_global, 'Location'        , '-6.333, 36.555');
   nc_attput(output, nc_global, 'Depth'           , '20m');

% Add dimensions 


nc_add_dimension(output, 'Time', i)
nc_adddim(output, 'datelength', j)


% Add variables
%%
DateVariable = struct(...
     'Name', 'DATE', ...
     'Nctype', 'double', ...
     'Dimension', {{'Time','datelength'}}, ...
     'Attribute', struct('Name', 'Date_and_Time_GMT', 'Value', '[YEAR MONTH DAY HOURS MINUTES SECONDS]') ...
     );

nc_addvar(output, DateVariable);

Hm0Variable = struct(...
    'Name', 'Hm0', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Spectral_Significan_Wave_height', 'Value', ' metre') ...
    );
nc_addvar(output, Hm0Variable);

H13Variable = struct(...
    'Name', 'H13', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Zero_crossing_Significan_Wave_height', 'Value', 'metre') ...
    );
nc_addvar(output, H13Variable);

TmVariable = struct(...
    'Name', 'Tm', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Zero_crossing_Significan_Wave_height', 'Value', 'seconds') ...
    );
nc_addvar(output, TmVariable);

Tm02Variable = struct(...
    'Name', 'Tm02', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Spectral_Mean_Period', 'Value', 'seconds') ...
    );
nc_addvar(output, Tm02Variable);

TpVariable = struct(...
    'Name', 'Tp', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Spectral_Peak_Period', 'Value', 'seconds') ...
    );
nc_addvar(output, TpVariable);

HmaxVariable = struct(...
    'Name', 'Hmax', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Maximum_Wave_Height', 'Value', 'metre') ...
    );
nc_addvar(output, HmaxVariable);

ThmaxVariable = struct(...
    'Name', 'Thmax', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Period_of_the_Maximum_Wave_Height', 'Value', 'seconds') ...
    );
nc_addvar(output, ThmaxVariable);

Pro_OeVariable = struct(...
    'Name', 'Pro_Oe', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Scalar_Observation_Characteristic', 'Value',...
    ['0= A) Calm conditions, Included in statistics (in that case Qc_e is 3'...
     '   B) Data dont exist'...
     '1= Data Processed by Puertos del Estado'...
     '2= Real Time data']) ...
    );
nc_addvar(output, Pro_OeVariable);

DmdVariable = struct(...
    'Name', 'Dmd', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Mean_Wave_Direction_(Atmospheric_Convention)', 'Value', 'degrees') ...
    );
nc_addvar(output, DmdVariable);

Dmd_PVariable = struct(...
    'Name', 'Dmd_P', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Spectral_Peak_Wave_Direction_(Atmospheric_Convention)', 'Value', 'deg') ...
    );
nc_addvar(output, Dmd_PVariable);

DsVariable = struct(...
    'Name', 'Ds', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Wave_Angular_Dispersion_in_all_freq', 'Value', 'deg') ...
    );
nc_addvar(output, DsVariable);

Ds_PVariable = struct(...
    'Name', 'Ds_P', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Peak_Energy_Angular_Dispersion', 'Value', 'deg') ...
    );
nc_addvar(output, Ds_PVariable);

Pro_OdVariable = struct(...
    'Name', 'Pro_Od', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Directional_Observation_Characteristic', 'Value',...
    ['0= A) Calm conditions, Included in statistics (in that case Qc_e is 3'...
     '   B) Data dont exist'...
     '1= Data Processed by Puertos del Estado'...
     '2= Real Time data']) ...
    );
nc_addvar(output, Pro_OdVariable);

Qc_eVariable = struct(...
    'Name', 'Qc_e', ...
    'Nctype', 'int', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Qualitry_Control_Values', 'Value',...
    ['1= Good Quality Data'...
     '2= Good Quality Data'...
     '3= Doubtful Quality Data']) ...
    );
nc_addvar(output, Qc_eVariable);



TsVariable = struct(...
    'Name', 'Ts', ...
    'Nctype', 'double', ...
    'Dimension', {{'Time'}}, ...
    'Attribute', struct('Name', 'Water_Surface_Temperature', 'Value', 'degrees Celcius ') ...
    );
nc_addvar(output, TsVariable);

% Store Variable

nc_varput(output, 'DATE', DATE)
nc_varput(output, 'Hm0', Hm0)
nc_varput(output, 'H13', H13)
nc_varput(output, 'Tm', Tm)
nc_varput(output, 'Tm02', Tm02)
nc_varput(output, 'Tp', Tp)
nc_varput(output, 'Hmax', Hmax)
nc_varput(output, 'Thmax', Thmax)
nc_varput(output, 'Pro_Oe', Pro_Oe)
nc_varput(output, 'Dmd', Dmd)
nc_varput(output, 'Dmd_P', Dmd_P)
nc_varput(output, 'Ds', Ds)
nc_varput(output, 'Ds_P', Ds_P)
nc_varput(output, 'Pro_Od', Pro_Od)
nc_varput(output, 'Ts', Ts)
