data_title = 'Vaklodingen';
data_summary = 'bathymetry and topography measurements along the Dutch coast';
data_keywords = 'bathymetry, coast';
path_main = 'D:\products';
path_sub_netcdf = 'rijkswaterstaat\vaklodingen';

publisher_email = 'Kees.denHeijer@deltares.nl';
institution_longname = 'Rijkswaterstaat Data-ICT-Dienst (RWS DID)';
institution_shortname = 'Rijkswaterstaat';

creator_name = institution_longname;
creator_url = 'www.rijkswaterstaat.nl';
creator_email = 'info@rijkswaterstaat.nl';

processing_level = 'preliminary';

publisher_name = getenv('USERNAME');
publisher_domain = getenv('USERDOMAIN');
publisher_computer = getenv('COMPUTERNAME');
publish_script_id = '$Header$';
path_data_title = lower(strrep(data_title, ' ', '_'));
path_netcdf = fullfile(path_main, 'nc', path_sub_netcdf);
path_sub_kml = 'rijkswaterstaat\vaklodingen\';

switch processing_level
    case 'final'
        url_main_netcdf = 'http://opendap.deltares.nl/thredds/dodsC/opendap/';
        url_ftp_main_netcdf = 'http://opendap.deltares.nl/thredds/fileServer/opendap/';
        kml_server = 'kml.deltares.nl';
    case 'preliminary'
        url_main_netcdf = 'http://dtvirt5.deltares.nl:8080/thredds/dodsC/opendap/';
        url_ftp_main_netcdf = 'http://dtvirt5.deltares.nl:8080/thredds/fileServer/opendap/';
        kml_server = 'dtvirt5.deltares.nl';
    otherwise
        error
end
path_kml = fullfile(path_main, 'kml', path_data_title, path_sub_kml);

url_netcdf = strrep(fullfile(url_main_netcdf, path_sub_netcdf), filesep, '/');

OPT = ncgen_surface_asc;
% --------------------------------------------------------------------------------------------------

OPT.main.file_incl          = '\.asc$';
OPT.main.dir_depth          = 0;
OPT.main.path_source        = '..\..\raw\grid';
OPT.main.path_netcdf        = path_netcdf;
OPT.main.store_hash         = false;
OPT.main.hash_source        = false;

% use regular expression to match irregular names
% '(?<=_)\d{4}(?=\d{4})' returns four digits '\d{4}' that are followed by
% four more digits '(?=\d{4})' and are preceded by an _ '(?<=_)'
OPT.main.dateFcn            = @(s) datenum(regexp(s, '(?<=_)\d{4}(?=\d{4})', 'match'), 'yyyy');

OPT.schema.meta             = struct(...
    ... % Identification
...    'id', 'none',...
    'naming_authority', 'deltares.nl',... % based on reverse DNS lookup (http://remote.12dt.com/)
    'Metadata_Conventions', 'CF-1.5',...
...    'metadata_link', 'none',...
    ... Text search
    'title', data_title,...
    'summary', data_summary,...
    'keywords', data_keywords,...
	'keywords_vocabulary', 'http://www.eionet.europa.eu/gemet',...
	'standard_name_vocabulary', 'http://cf-pcmdi.llnl.gov/documents/cf-standard-names/',...
    'history', sprintf('Created on %s by %s on computer %s\\%s with script %s',...
    datestr(now,31), publisher_name, publisher_domain, publisher_computer, publish_script_id),...
...    'comment', 'none',...
    'institution', institution_longname,...
	'cdm_data_type', 'grid',...
 	'acknowledgment', 'WSA Emden provided data in the German Ems-Dollard area',...
    'creator_name', creator_name,...
    'creator_url', creator_url,...
    'creator_email', creator_email,...
    'publisher_name', publisher_name,...
    'publisher_url', 'www.deltares.nl',...
    'publisher_email', publisher_email,...
    ... % Other attributes
	'processing_level', processing_level,...
	'license',[sprintf('These data can be used freely for research purposes provided that the following source is acknowledged: %s. ', institution_shortname)...
                '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.'] ...
    );

OPT.schema.format           = 'classic'; % netcdf-3 format

OPT.schema.grid_cellsize    = 20;
OPT.schema.grid_offset      = 10;
OPT.schema.grid_tilesize    = [500 625]; %[nx ny]
OPT.schema.time_units       = 'days since 1970-01-01 00:00:00 +01:00';
OPT.schema.EPSGcode         = 28992;
% OPT.schema.x_add_offset     = min(S.xllcorner(:));
OPT.schema.x_datatype       = 'int32';
% OPT.schema.x_scale_factor   = 10; % 10 m precision
% OPT.schema.y_add_offset     = min(S.yllcorner(:));
OPT.schema.y_datatype       = 'int32';
% OPT.schema.y_scale_factor   = 10; % 10 m precision
% OPT.schema.z_datatype       = 'int32';
% OPT.schema.z_scale_factor   = .001; % mm precision

OPT.read.z_scalefactor      = 0.01; % conversion from source data in cm to m
OPT.write.filenameFcn       = @(x,y) sprintf('vaklodingenKB%03.0f_%04.0f.nc',min(x-10)/10000 + 111,round(11008 + (min(y+10) * -0.01616)));

ncgen_surface_asc(OPT);

%% post process vaklodingen
% ncgentools_fill_and_sort_along_dimension(...
%     'E:\products\nc\rijkswaterstaat\vaklodingen',...
%     'E:\products\nc\rijkswaterstaat\vaklodingen_filled',...
%     'dimension_name','time',...
%     'fill_variable_name','z');

%% create catalog.nc files
% ncgentools_generate_catalog(...
%     'urlPath', path_netcdf,...
%     'urlPathrep', url_netcdf,...
%     'var', {},...
%     'mode', 'clobber')

% ncgentools_generate_catalog(...
%     'urlPath', 'e:\products\nc\rijkswaterstaat\vaklodingen_filled\',...
%     'urlPathrep', 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/vaklodingen_filled/',...
%     'var', {})

%% create overview with nc_cf_gridset_get_list
% nc_cf_gridset_get_list(path_netcdf,...
%     'fid', fopen(fullfile(fileparts(which(mfilename)), sprintf('nc_cf_gridset_get_list_%s.txt', datestr(now,30))), 'w'))

%% make kml 
ncgentools_surface_kml(...
    'path_netcdf','D:\products\nc\rijkswaterstaat\vaklodingen',...
    'path_kml','D:\products\kml\rijkswaterstaat\vaklodingen',...
    'descriptivename','vaklodingen',...
    'CBtemplateVer','KML_colorbar_template_vertical_vanoord.png',...
    'lowestLevel',16,...
    'lighting_effects',true,...
    'colorMap',@(m)colormap_cpt('bathymetry_vaklodingen',m),...
    'cLim',[-50 25]);

% nc2kml_surface_imageOverlay(...
%     'path_netcdf'   , path_netcdf,...
%     'path_kml'      , path_kml,...
%     'CBtemplateVer' ,'KML_colorbar_template_vertical.png',...
%     'descriptivename', data_title,...
%     'description', sprintf('<a href="%s">%s</a> %s provided by <a href="http://www.openearth.nl">OpenEarthTools</a> generated: ', creator_url, institution_shortname, data_title, datestr(now())),...
%     'lowestLevel'   ,15,...
%     'filledInTime'  ,false)
% 
% % copy surface overlay kml one level higher and add hard urls
% kml_surface_overlay = fullfile(path_kml, 'Surface_overlay.kml');
% kml_surface_overlay_copy = fullfile(fileparts(fullfile(path_kml, 'Surface_overlay.kml')), '..', [strrep(lower(data_title), ' ', '_') '.kml']);
% copyfile(kml_surface_overlay,...
%     kml_surface_overlay_copy)
% 
% strrep_in_files(kml_surface_overlay_copy,...
%     '<href>', strrep(fullfile(['<href>http://' kml_server '/kml/'], path_sub_kml), filesep, '/'))
% 
% % nc2kml_surface_imageOverlay(...
% %     'path_netcdf'   ,'E:\products\nc\rijkswaterstaat\vaklodingen_filled',...
% %     'path_kml'      ,'E:\products\kml\rijkswaterstaat\vaklodingen_filled',...
% %     'CBtemplateVer' ,'KML_colorbar_template_vertical.png',...
% %     'lowestLevel'   ,15,...
% %     'filledInTime'  ,true)
% 
% %% create kml overview indicating the tiles
% nc_cf_gridset_overview(...
%     'fileName', fullfile(path_kml, '..', [path_data_title '_overview.kml']),...
%     'load_kml', path_kml,...
%     'kmlPathFcn',@(s) strrep(s, path_kml, ['http://' kml_server '/kml/']),...
%     'datestr','YYYY-mm',...
%     'load_nc', path_netcdf,...
%     'urlPathFcn',@(s) path2os(strrep(s, fullfile(path_main, 'nc'), url_main_netcdf),'http'),...
%     'ftpPathFcn',@(s) path2os(strrep(s, fullfile(path_main, 'nc'), url_ftp_main_netcdf),'http'),...
%     'description', sprintf('<a href="%s">%s</a> %s provided by <a href="http://www.openearth.nl">OpenEarthTools</a> generated: ', creator_url, institution_shortname, data_title, datestr(now())),...
%     'kmlName', [data_title ' overview']);