%-%function opnemers2nc(varargin)
%OPNEMERS2NC  transforms csv file of Markermeer data to netCDF
%
%     opnemers2nc(<keyword,value>) 
%
%  where the following <keyword,value> pairs have been implemented:
%
%   * filename      (default '..\raw\FL42\Analyses\FL42 opnemers.csv';)
%
%  Timeseries data definition:
%   * https://cf-pcmdi.llnl.gov/trac/wiki/PointObservationConventions (full definition)
%   * http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2984788 (simple)
%
% In this example time is both a dimension and a variables.
% The actual datenum values do not show up as a parameter in ncBrowse.
%
%See also: SNCTOOLS

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2009 Deltares
%       Gerben de Boer
%
%       gerben.deboer@deltares.nl	
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library 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
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tools is part of <a href="http://OpenEarth.Deltares.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 12 Oct 2009
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $


%% Initialize

   OPT.dump              = 0;
   OPT.disp              = 0;
   OPT.pause             = 0;
   
   OPT.refdatenum        = datenum(0000,0,0); % matlab datenumber convention: A serial date number of 1 corresponds to Jan-1-0000. Gives wring date sin ncbrowse due to different calenders. Must use doubles here.
   OPT.refdatenum        = datenum(1970,1,1); % lunix  datenumber convention
   OPT.fillvalue         = NaN; % NaNs do work in netcdf API. This is the value in the matlab data, not in the file!!
   
   OPT.stationTimeSeries = 0; % last items to adhere to for upcoming convenction, but not yet supported by QuickPlot
   
   D.filename            = '..\raw\FL42\Analyses\FL42 opnemers.csv';
   
%% Keyword,value

%-%OPT = setProperty(OPT,varargin{:});
   
%% File loop

%% 0 Read raw data

   M                      = csvread(D.filename);
   
   M(M==-99)= NaN;
   
   offset  = 3;
   legend1 = '(-4.08m NAP)';
   legend2 = '(-1.39m NAP)';

   D.excel_days           = M(:,   1);
   D.fraction_of_day      = M(:,   2);
   %% first date should be 20-Aug-2007 11:00:00 according to Excel file of Bas Blok
   D.datenum              = datenum('30-Dec-1899') + D.excel_days + round(D.fraction_of_day*24*3600)./24./3600; % round to whole seconds
   D.line                 = M(:,   3);

   D.Hm0                  = M(:, 1+offset);
   D.std_p                = M(:, 2+offset);
   D.wl_p                 = M(:, 3+offset);
   D.wl_a                 = M(:, 4+offset);
   D.temp_1               = M(:, 5+offset);
   D.temp_2               = M(:, 6+offset);
   D.pH_1                 = M(:, 7+offset);
   D.pH_2                 = M(:, 8+offset);
   D.cond_1               = M(:, 9+offset);
   D.cond_2               = M(:,10+offset);
   D.DO_1                 = M(:,11+offset);
   D.DO_2                 = M(:,12+offset);
   D.turb_1               = M(:,13+offset);
   D.turb_2               = M(:,14+offset);
   D.PAR_1                = M(:,15+offset);
   D.PAR_2                = M(:,16+offset);
   D.lgr_temp             = M(:,17+offset);
   D.battV                = M(:,18+offset);
   D.chlf_a               = M(:,19+offset);
   D.TSM_1                = M(:,20+offset);
   D.TSM_2                = M(:,21+offset);
   D.timezone             = 'MET';

   D.lon                  =  5 + 13.956/60; %  5°13.956 (wgs84)
   D.lat                  = 52 + 31.192/60; % 52°31.192 (wgs84)
   D.version              = 'october 2008 b';
   D.file.date            = 'october 2008';
   D.station_id           = 'FL42';

   fldnames = {'Hm0'
               'temp_1'
               'temp_2'
               'TSM_1'
               'TSM_2'};
    
   for ifld = 1:length(fldnames)
   
      fldname = fldnames{ifld};
      
      D.(fldname)(D.(fldname)==-99) = NaN;
      
   end

%% 1a Create file

   outputfile    = ['FL42.nc'];
   
   nc_create_empty (outputfile)

   %% Add overall meta info
   %  http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#description-of-file-contents
   %------------------

   nc_attput(outputfile, nc_global, 'title'         , '');
   nc_attput(outputfile, nc_global, 'institution'   , 'Rijkswaterstaat IJsselmeergebied / Meet- en informatiedienst');
   nc_attput(outputfile, nc_global, 'source'        , 'surface observation');
   nc_attput(outputfile, nc_global, 'history'       , ['Original filename: ',filenameext(D.filename),...
                                                       ', version:' ,D.version,...
                                                       ', filedate:',D.file.date,...
                                                       ', Measured, processed and QA bij Bas Blok (Deltares)',...
                                                       ', tranformation to netCDF: $HeadURL$ $Id$']);
   nc_attput(outputfile, nc_global, 'references'    , '<http://rijkswaterstaat.nl>');
   nc_attput(outputfile, nc_global, 'email'         , '<>');
   
   nc_attput(outputfile, nc_global, 'comment'       , '');
   nc_attput(outputfile, nc_global, 'version'       , D.version);
						    
   nc_attput(outputfile, nc_global, 'Conventions'   , 'CF-1.4');
   nc_attput(outputfile, nc_global, 'CF:featureType', 'stationTimeSeries');  % https://cf-pcmdi.llnl.gov/trac/wiki/PointObservationConventions
   						    
   nc_attput(outputfile, nc_global, 'stationname'   , 'FL42');
   
   nc_attput(outputfile, nc_global, 'terms_for_use' , 'These data can be used freely for research purposes provided that the following source is acknowledged: Rijkswaterstaat.');
   nc_attput(outputfile, nc_global, 'disclaimer'    , 'This data is made available 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.');
   
%% 2 Create dimensions

   nc_add_dimension(outputfile, 'time'                                       ,length(D.datenum));
   nc_add_dimension(outputfile, ['char_length',num2str(length(D.station_id))],length(D.station_id));
   

%% 3 Create variables

   clear nc
   ifld = 0;
   
   %% Station number: allows for exactly same variables when multiple timeseries in one netCDF file
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'station_id';
   nc(ifld).Nctype       = 'char'; % no double needed
   nc(ifld).Dimension    = {['char_length',num2str(length(D.station_id))]}; %{'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'station identification number');
   nc(ifld).Attribute(2) = struct('Name', 'standard_name'  ,'Value', 'station_id');

   %% For standard names see:
   %  http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/current/standard-name-table

   %% Longitude
   % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#longitude-coordinate
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'lon';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {}; %{'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'station longitude');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_east');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'longitude');
    
   %% Latitude
   % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#latitude-coordinate
   
      ifld = ifld + 1;
   nc(ifld).Name         = 'lat';
   nc(ifld).Nctype       = 'float'; % no double needed
   nc(ifld).Dimension    = {}; %{'locations'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'station latitude');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degrees_north');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'latitude');

   %% Time
   % http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#time-coordinate
   % time is a dimension, so there are two options:
   % * the variable name needs the same as the dimension
   %   http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2984551
   % * there needs to be an indirect mapping through the coordinates attribute
   %   http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#id2984605
   
   OPT.timezone = timezone_code2iso(D.timezone);

      ifld = ifld + 1;
   nc(ifld).Name         = 'time';
   nc(ifld).Nctype       = 'double'; % float not sufficient as datenums are big: doubble
   nc(ifld).Dimension    = {'time'}; % QuickPlot error: plots dimensions instead of datestr
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'time');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value',['days since ',datestr(OPT.refdatenum,'yyyy-mm-dd'),' 00:00:00 ',OPT.timezone]);
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'time');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   
   %% Parameters with standard names
   % * http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/current/

      ifld = ifld + 1; % 1
   nc(ifld).Name         = 'Hm0';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'significant wave height');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_surface_wave_significant_height');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 2
   nc(ifld).Name         = 'std_p';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'standard deviation of pressure');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_water_pressure');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: standard_deviation (interval: 10 minutes)');


      ifld = ifld + 1; % 3
   nc(ifld).Name         = 'wl_p';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'waterlevel (pressure sensor, NAP)');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_surface_height');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');
   nc(ifld).Attribute(6) = struct('Name', 'positive'        ,'Value', 'up');

      ifld = ifld + 1; % 4
   nc(ifld).Name         = 'wl_a';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'waterlevel (acoustic sensor, NAP)');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_surface_height');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');
   nc(ifld).Attribute(6) = struct('Name', 'positive'       ,'Value', 'up');

      ifld = ifld + 1; % 5
   nc(ifld).Name         = 'temp_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['temperature ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degree_Celsius');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_water_temperature');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');
   nc(ifld).Attribute(6) = struct('Name', 'height'         ,'Value', -4.08);

      ifld = ifld + 1; % 6
   nc(ifld).Name         = 'temp_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['temperature ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degree_Celsius');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_water_temperature');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');
   nc(ifld).Attribute(6) = struct('Name', 'height'         ,'Value', -1.39);

      ifld = ifld + 1; % 7
   nc(ifld).Name         = 'pH_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['pH ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', '1');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 8
   nc(ifld).Name         = 'pH_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['pH ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', '1');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 9
   nc(ifld).Name         = 'cond_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['electrical conductivity ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'mS/cm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_water_electrical_conductivity');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 10
   nc(ifld).Name         = 'cond_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['electrical conductivity ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'mS/cm');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'sea_water_electrical_conductivity');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 11
   nc(ifld).Name         = 'DO_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['dissolved oxygen DO ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', '100*kg/kg');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'mass_concentration_of_oxygen_in_sea_water');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 12
   nc(ifld).Name         = 'DO_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['dissolved oxygen DO ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', '100*kg/kg');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'mass_concentration_of_oxygen_in_sea_water');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 13
   nc(ifld).Name         = 'turb_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['turbidity ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'FTU');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 14
   nc(ifld).Name         = 'turb_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['turbidity ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'FTU');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 15
   nc(ifld).Name         = 'PAR_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['photosynthetic quantum energy PAR ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'micromol/s/m2');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 16
   nc(ifld).Name         = 'PAR_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['photosynthetic quantum energy PAR ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'micromol/s/m2');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 17
   nc(ifld).Name         = 'lgr_temp';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'internal temperature of data logger');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'degree_Celsius');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 18
   nc(ifld).Name         = 'battV';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'accu voltage of data logger');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'Volt');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', '');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 19
   nc(ifld).Name         = 'chlf_a';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value', 'Chlorofyll');
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'microg/l');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'concentration_of_chlorophyll_in_sea_water');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');

      ifld = ifld + 1; % 20
   nc(ifld).Name         = 'TSM_1';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['concentration of total suspended matter ',legend1]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'g/m3');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'concentration_of_suspended_matter_in_sea_water');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');
   nc(ifld).Attribute(6) = struct('Name', 'height'         ,'Value', -4.08);

      ifld = ifld + 1; % 21
   nc(ifld).Name         = 'TSM_2';
   nc(ifld).Nctype       = 'float';
   nc(ifld).Dimension    = {'time'};
   nc(ifld).Attribute(1) = struct('Name', 'long_name'      ,'Value',['concentration of total suspended matter ',legend2]);
   nc(ifld).Attribute(2) = struct('Name', 'units'          ,'Value', 'g/m3');
   nc(ifld).Attribute(3) = struct('Name', 'standard_name'  ,'Value', 'concentration_of_suspended_matter_in_sea_water');
   nc(ifld).Attribute(4) = struct('Name', '_FillValue'     ,'Value', OPT.fillvalue);
   nc(ifld).Attribute(5) = struct('Name', 'cell_methods'   ,'Value', 'time: mean (interval: 10 minutes)');
   nc(ifld).Attribute(6) = struct('Name', 'height'         ,'Value', -1.39);

%% 4 Create variables with attibutes
% When variable definitons are created before actually writing the
% data in the next cell, netCDF can nicely fit all data into the
% file without the need to relocate any info.

   for ifld=1:length(nc)
      if OPT.disp;disp(['adding ',num2str(ifld),' ',nc(ifld).Name]);end
      nc_addvar(outputfile, nc(ifld));
   end

%% 5 Fill variables

   nc_varput(outputfile, 'lon'                        , D.lon );
   nc_varput(outputfile, 'lat'                        , D.lat );
   nc_varput(outputfile, 'station_id'                 , D.station_id);
   nc_varput(outputfile, 'time'                       , D.datenum-OPT.refdatenum);

   nc_varput(outputfile, 'Hm0'                        , D.Hm0     );
   nc_varput(outputfile, 'std_p'                      , D.std_p   );
   nc_varput(outputfile, 'wl_p'                       , D.wl_p    );
   nc_varput(outputfile, 'wl_a'                       , D.wl_a    );
   nc_varput(outputfile, 'temp_1'                     , D.temp_1  );
   nc_varput(outputfile, 'temp_2'                     , D.temp_2  );
   nc_varput(outputfile, 'pH_1'                       , D.pH_1    );
   nc_varput(outputfile, 'pH_2'                       , D.pH_2    );
   nc_varput(outputfile, 'cond_1'                     , D.cond_1  );
   nc_varput(outputfile, 'cond_2'                     , D.cond_2  );
   nc_varput(outputfile, 'DO_1'                       , D.DO_1    );
   nc_varput(outputfile, 'DO_2'                       , D.DO_2    );
   nc_varput(outputfile, 'turb_1'                     , D.turb_1  );
   nc_varput(outputfile, 'turb_2'                     , D.turb_2  );
   nc_varput(outputfile, 'PAR_1'                      , D.PAR_1   );
   nc_varput(outputfile, 'PAR_2'                      , D.PAR_2   );
   nc_varput(outputfile, 'lgr_temp'                   , D.lgr_temp);
   nc_varput(outputfile, 'battV'                      , D.battV   );
   nc_varput(outputfile, 'chlf_a'                     , D.chlf_a  );
   nc_varput(outputfile, 'TSM_1'                      , D.TSM_1   );
   nc_varput(outputfile, 'TSM_2'                      , D.TSM_2   );
   
%% 6 Check

   if OPT.dump
   nc_dump(outputfile);
   end
   
%% Pause

   if OPT.pause
      pausedisp
   end

%% EOF
