function bkl2netcdf(filename, m, varargin)
%
%	Creates bkl_tkl_tnd.nc file. 
%	'm' struct is called from convertBKL_TKL_TND2nc.m
%	Uses matlab nc functions
%
%	---------------------------------------
% 
%       Giorgio Santinelli
%
%       Giorgio.Santinelli@deltares.nl
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%	---------------------------------------
%
% $Id: bkl2netcdf.m 8823 2015-11-25 19:27:31Z santinel $
% $Date: 2015-11-25 20:27:31 +0100 (wo, 25 nov 2015) $
% $Author: santinel $
% $Revision: 8823 $
% $HeadURL: https://svn.oss.deltares.nl/repos/openearthrawdata/trunk/rijkswaterstaat/kustlijnkaartenboek/scripts/bkl2netcdf.m $
 

OPT = struct(...
    'publisher_name', getenv('USERNAME'),...
    'publisher_email', 'info@deltares.nl',...
    'historyatt', '$HeadURL $ $Id $',...
    'processing_level', 'preliminary');

OPT = setproperty(OPT, varargin);

datefmt = 'yyyy-mm-ddTHH:MMZ'; % date format
STRINGSIZE = 100;

    Attributes = [...
    struct( 'Name', 'naming_authority', 'Value', 'deltares.nl' ),... 
    struct( 'Name', 'id', 'Value', sprintf('BKL_TKL_TND_release%s', datestr(nowutc, 'yyyymmdd'))),...
    struct( 'Name', 'Metadata_Conventions', 'Value', 'Unidata Dataset Discovery v1.0'),...
    struct( 'Name', 'title', 'Value', 'basal coastline, testing coastline, trend'),...
    struct( 'Name', 'summary', 'Value', 'Coastline indicators BKL, TKL, TND along the Dutch coast since 1965, as described in the '' Kustlijnkaarten rapport'' http://www.rijkswaterstaat.nl/water/veiligheid/bescherming_tegen_het_water/veiligheidsmaatregelen/kustlijnzorg/kustlijnkaarten/'),...
    struct( 'Name', 'keywords', 'Value', 'Coastline, BKL, TKL, TND, Dutch coast'),...
    struct( 'Name', 'keywords_vocabulary', 'Value', 'http://www.eionet.europa.eu/gemet'),...
    struct( 'Name', 'standard_name_vocabulary', 'Value', 'http://cf-convention.github.io/'),...
    struct( 'Name', 'history', 'Value', 'Data processed from xls/csv data - Winkust/Morphan output'),...
    struct( 'Name', 'HEADurl', 'Value', 'https://svn.oss.deltares.nl/repos/openearthrawdata/trunk/rijkswaterstaat/kustlijnkaartenboek/'),...
    struct( 'Name', 'comment', 'Value', 'Data for the TKL-position in all years, except the year 1990, is the formally assigned position (source: Yearly Kustlijnkaartenboek - Rijkswaterstaat). The data for the TKL-position in 1990 deviates: for 1990 the data from the nota GWWS-91.006 (RKB 91-19) DE BASISKUSTLIJN een technische/morfologische uitwerking is used. It concerns the original extrapolation of the (linear) MKL trend over the period 1980-1989 to the year 1990. This TKL-position was used as a basis for the BKL-position, but for several location the position was shifted (verlegd) or released (losgelaten). Shifting or releasing was mainly based on morphological arguments (the applied arguments can be found in the previous reference and DGW-93.035 De Basiskustlijn, Norm voor Dynamisch Handhaven). The direction and magnitude of shifting can be obtained by comparing the TKL1990 position to the BKL-position'),...
    struct( 'Name', 'Years covered', 'Value', ['1990-' num2str(m.jarkusyear(end))  ' (1991 missing) ']),...
    struct( 'Name', 'institution', 'Value', 'Rijkswaterstaat'),...
    struct( 'Name', 'source'     , 'Value', 'on shore and off shore measurements'),...
    struct( 'Name', 'references' , 'Value', 'Original source: Rijkswaterstaat (Morphan), Deltares storage: http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/ converted with script convertBKL_TKL_TND2nc.m'),...
    struct( 'Name', 'Conventions', 'Value', 'CF-1.6'),...
    ... % Creator Search attributes
    struct( 'Name', 'creator_name', 'Value', 'Rijkswaterstaat'),...
    struct( 'Name', 'creator_url', 'Value', 'http://www.rijkswaterstaat.nl'),...
    struct( 'Name', 'creator_email', 'Value', 'info@rijkswaterstaat.nl'),...
    struct( 'Name', 'date_created', 'Value', datestr(nowutc, datefmt)),...
    struct( 'Name', 'date_modified', 'Value', datestr(nowutc, datefmt)),...
    struct( 'Name', 'date_issued', 'Value', datestr(nowutc, datefmt)),...
    ... % Publisher Search attributes
    struct( 'Name', 'publisher_name', 'Value', OPT.publisher_name),...
    struct( 'Name', 'publisher_url', 'Value', 'http://www.deltares.nl'),...
    struct( 'Name', 'publisher_email', 'Value', OPT.publisher_email),...
    ... % Extent Search attributes
    ... % Other Extent Information attributes
    struct( 'Name', 'geospatial_vertical_units', 'Value', 'm'),...
    struct( 'Name', 'geospatial_vertical_positive', 'Value', 'up'),...
    ...% Other attributes
	struct( 'Name', 'processing_level', 'Value', OPT.processing_level),...
	struct( 'Name', 'license', 'Value', [sprintf('These data can be used freely for research purposes provided that the following source is acknowledged: %s. ', 'RIJKSWATERSTAAT')...
                '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.']),...
    struct( 'Name', 'time_coverage_start', 'Value', '1990'),...
    struct( 'Name', 'time_coverage_end', 'Value', num2str(m.jarkusyear(end))),...
    struct( 'Name', 'time_coverage_resolution', 'Value', 'year')...
    ];

%% Define and create dimensions
% %  unlimited "time" dimension in Matlab seems to give variable "time" a 
% %  size equal to the the maximum dimension among all variables [2268]. 
% %  This leads to storing a useless amount of data. 
%    time_dim = struct('Name', 'time', 'Length', 0, 'Unlimited', true);
    time_dim = struct('Name', 'time', 'Length', length(m.time), 'Unlimited', false);
    bounds2_dim = struct('Name', 'bounds2', 'Length', 2, 'Unlimited', false);
    alongshore_dim = struct('Name', 'alongshore', 'Length', length(m.id), 'Unlimited', false);
    cross_shore_dim = struct('Name', 'cross_shore', 'Length', length(m.cross), 'Unlimited', false);
    stringsize_dim = struct('Name', 'stringsize', 'Length', STRINGSIZE, 'Unlimited', false);
    Dimensions = [time_dim, bounds2_dim, alongshore_dim, cross_shore_dim, stringsize_dim];
    
%% add epsg
    % smaller variables first
    epsg        = 28992;
    [~,~,OPT]=convertCoordinates(0,0,'CS1.code',epsg,'CS2.code',4326);
    %[CoordinateSystems, Operations , CoordSysCart ,CoordSysGeo] = GetCoordinateSystems();
    s.Name      = 'epsg';
    s.Datatype    = 'int32';
    s.Dimensions = [];
    s.Attributes = struct('Name', ...
       {'grid_mapping_name', ...
        'semi_major_axis', ...
        'semi_minor_axis', ...
        'inverse_flattening', ...
        'latitude_of_projection_origin', ...
        'longitude_of_projection_origin', ...
        'false_easting', ...
        'false_northing', ...
        'scale_factor_at_projection_origin',...
        'comment'}, ...
        'Value', ...
        {OPT.proj_conv1.method.name,    ...
        OPT.CS1.ellips.semi_major_axis, ...
        OPT.CS1.ellips.semi_minor_axis, ...
        OPT.CS1.ellips.inv_flattening,  ...
        OPT.proj_conv1.param.value(strcmp(OPT.proj_conv1.param.name,'Latitude of natural origin'    )), ...
        OPT.proj_conv1.param.value(strcmp(OPT.proj_conv1.param.name,'Longitude of natural origin'   )),...
        OPT.proj_conv1.param.value(strcmp(OPT.proj_conv1.param.name,'False easting'                 )),...
        OPT.proj_conv1.param.value(strcmp(OPT.proj_conv1.param.name,'False northing'                )),...
        OPT.proj_conv1.param.value(strcmp(OPT.proj_conv1.param.name,'Scale factor at natural origin')),...
        'value is equal to EPSG code'});
    Variables = s;

%% Define and create variables
    s.Name       = 'id';
    s.Datatype   = 'int32';
    s.Dimensions = alongshore_dim;
    s.Attributes = struct('Name' ,{'long_name' , 'comment'},...
                         'Value',{'identifier' , 'sum of area code (*1e6) and alongshore coordinate'});
    Variables = [Variables, s];
    
    flag_values = 1:17;%unique(m.areaCode);
    flag_meanings = rws_kustvak(flag_values);
    flag_meanings = str2line(flag_meanings,'s',', ');
    
    s.Name       = 'areacode';
    s.Datatype   = 'int32';
    s.Dimensions = alongshore_dim;
    s.Attributes = struct('Name' ,{'long_name','flag_values','flag_meanings','flag_comment'       ,'comment'},...
                          'Value',{'area code', flag_values , flag_meanings ,'points to: areaname','codes for the 17 coastal areas (kustvakken) as defined by Rijkswaterstaat'});
    Variables = [Variables, s];

    s.Name      = 'areaname';
    s.Datatype  = 'char';
    s.Dimensions = [stringsize_dim, alongshore_dim];
    s.Attributes = struct('Name',{'long_name','flag_comment'        ,'comment'},...
                         'Value',{'area name','indexed in: areacode','names for the 17 coastal areas (kustvakken) as defined by Rijkswaterstaat'});
    Variables = [Variables, s];

    s.Name       = 'alongshore';
    s.Datatype   = 'double';
    s.Dimensions = alongshore_dim;
    s.Attributes = struct('Name' ,{'long_name'             , 'units', 'comment'},...
                         'Value',{'alongshore coordinate', 'm'     , 'alongshore coordinate within the 17 coastal areas (kustvakken) as defined by Rijkswaterstaat'});
    Variables = [Variables, s];

    s.Name       = 'cross_shore';
    s.Datatype   = 'double';
    s.Dimensions = cross_shore_dim;
    s.Attributes = struct('Name' ,{'long_name'             , 'units', 'comment'},...
                         'Value',{'cross-shore coordinate', 'm'    , 'cross-shore coordinate relative to the rsp (rijks strand paal)'});
    Variables = [Variables, s];

    s.Name       = 'time';
    s.Datatype   = 'double';
    s.Dimensions = time_dim;
    s.Attributes = struct('Name' ,{'standard_name'          ,'axis' ,'units'                 ,'cell_methods','bounds'     ,'comment'         },...
                         'Value',{'time'                   ,'T'    ,'days since 1970-01-01' ,'mean'        ,'time_bounds','measurement year (date is artificially set to July, 1st); see time_MKL'});
    Variables = [Variables, s];

    s.Name      = 'time_bounds';
    s.Datatype    = 'double';
    s.Dimensions = [bounds2_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'          ,'units'                },...
                         'Value',{'time'                   ,'days since 1970-01-01'});
    Variables = [Variables, s];

    % ! important: time has to be the last! 
    % http://stackoverflow.com/questions/16938779/matlab-nc-unlimited-in-the-wrong-index-when-writing-unlimited-time-dimension-i
    
% BKL_lat
    s.Name         = 'basal_coastline_lat';
    s.Datatype     = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'         , 'long_name',             'units',        'axis', 'comment'},...
                         'Value',{'latitude'               , 'BKL latitude position', 'degree_north', 'X',    'BKL latitude position as defined by Rijkswaterstaat'});
    Variables = [Variables, s];

% BKL_lon
    s.Name         = 'basal_coastline_lon';
    s.Datatype     = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name', 'long_name',              'units',        'axis', 'comment'},...
                          'Value',{'longitude'    , 'BKL longitude position', 'degrees_east', 'Y',    'BKL longitude position as defined by Rijkswaterstaat'});
        Variables = [Variables, s];

% BKL_x
    s.Name      = 'basal_coastline_x';
    s.Datatype    = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'          , 'long_name',      'units', 'axis', 'comment'},...
                          'Value',{'projection_x_coordinate', 'BKL x position', 'm',     'X',    'BKL x position as defined by Rijkswaterstaat'});
    Variables = [Variables, s];
    

% BKL_y
    s.Name      = 'basal_coastline_y';
    s.Datatype    = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'          , 'long_name',      'units', 'axis', 'comment'},...
                          'Value',{'projection_y_coordinate', 'BKL y position', 'm',     'Y',    'BKL y position as defined by Rijkswaterstaat'});
    Variables = [Variables, s];
    
                          
% BKL_rsp
    s.Name      = 'basal_coastline';
    s.Datatype    = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'long_name'   , 'units', 'comment'},...
                          'Value',{'BKL position', 'm'    , 'BKL position with respect to the beach pole (rijks strand paal) as defined by Rijkswaterstaat'});
    Variables = [Variables, s];
    
% TKL_lat
    s.Name         = 'testing_coastline_lat';
    s.Datatype     = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'         , 'long_name',             'units',        'axis', 'comment'},...
                         'Value',{'latitude'               , 'TKL latitude position', 'degree_north', 'X',    'TKL latitude position as defined by Rijkswaterstaat'});
    Variables = [Variables, s];

% TKL_lon
    s.Name         = 'testing_coastline_lon';
    s.Datatype     = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name', 'long_name',              'units',        'axis', 'comment'},...
                          'Value',{'longitude'    , 'TKL longitude position', 'degrees_east', 'Y',    'TKL longitude position as defined by Rijkswaterstaat'});
        Variables = [Variables, s];

% TKL_x
    s.Name      = 'testing_coastline_x';
    s.Datatype    = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'          , 'long_name',      'units', 'axis', 'comment'},...
                          'Value',{'projection_x_coordinate', 'TKL x position', 'm',     'X',    'TKL x position as defined by Rijkswaterstaat'});
    Variables = [Variables, s];
    

% TKL_y
    s.Name      = 'testing_coastline_y';
    s.Datatype    = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'standard_name'          , 'long_name',      'units', 'axis', 'comment'},...
                          'Value',{'projection_y_coordinate', 'TKL y position', 'm',     'Y',    'TKL y position as defined by Rijkswaterstaat'});
    Variables = [Variables, s];
    
                          
% TKL_rsp
    s.Name      = 'testing_coastline';
    s.Datatype    = 'double';
    s.Dimensions = [alongshore_dim, time_dim];
    s.Attributes = struct('Name' ,{'long_name'   , 'units', 'comment'},...
                          'Value',{'BKL position', 'm'    , 'BKL position with respect to the beach pole (rijks strand paal) as defined by Rijkswaterstaat'});
    Variables = [Variables, s];
    
    

% TND
    s.Name      = 'trend';
    s.Datatype    = 'double';
    s.Dimensions = alongshore_dim;
    s.Attributes = struct('Name' ,{'long_name'   , 'units', 'comment'},...
                          'Value',{'erosion trend', 'm / year'    , 'erosion trend defined by Rijkswaterstaat'});
    Variables = [Variables, s];


%% write schema to file        

ncSchema = struct(... 'Filename', strrep(filename, '.nc', 'test.nc'),...
        'Name', '/',...
        'Dimensions', Dimensions,...
        'Variables', Variables,...
        'Attributes', Attributes,...
        'Groups', [],...
        'Format', 'classic');

    ncwriteschema(filename, ncSchema)

%% Store index variables
    ncwrite(filename, 'time'        , m.time    );
    ncwrite(filename, 'time_bounds' , m.time_bounds);

    ncwrite(filename, 'id'          , m.id);
    ncwrite(filename, 'areacode'    , m.areacode);
    areanames = m.areaname;
    areanames(:, size(areanames,2)+1:STRINGSIZE) = ' ';
    ncwrite(filename, 'areaname', areanames);

    ncwrite(filename, 'alongshore', m.along);
    ncwrite(filename, 'cross_shore', m.cross);
    
    ncwrite(filename, 'basal_coastline_lat'        , m.BKL_lat');
    ncwrite(filename, 'basal_coastline_lon'        , m.BKL_lon');
    ncwrite(filename, 'basal_coastline_x'          , m.BKL_x');
    ncwrite(filename, 'basal_coastline_y'          , m.BKL_y');
    
    ncwrite(filename, 'testing_coastline_lat'        , m.TKL_lat');
    ncwrite(filename, 'testing_coastline_lon'        , m.TKL_lon');
    ncwrite(filename, 'testing_coastline_x'          , m.TKL_x');
    ncwrite(filename, 'testing_coastline_y'          , m.TKL_y');
    
    ncwrite(filename, 'trend'                   , m.tnd);
    
    ncwrite(filename, 'basal_coastline'         , m.Xbkl');
    ncwrite(filename, 'testing_coastline'       , m.Xtkl');
    
%% check
ncinfo(filename);

%%%%%%


end % function mkl2netcdf.m
