cellsize = 20;
data_title = 'Outer Deltas';
data_summary = 'Bathymetry measurements Outer Deltas Zeeland';
data_keywords = 'bathymetry';
path_main = 'E:\products';
path_sub_netcdf = 'rijkswaterstaat\DienstZeeland\';
path_sub_kml = 'rijkswaterstaat\dienstzeeland\';

publisher_email = 'Kees.denHeijer@deltares.nl';
institution_longname = 'Rijkswaterstaat Dienst Zeeland';
institution_shortname = 'Dienst Zeeland';

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 = '$Id$';
path_data_title = lower(strrep(data_title, ' ', '_'));
path_netcdf = fullfile(path_main, 'nc', path_sub_netcdf, path_data_title);
path_kml = fullfile(path_main, 'kml', path_data_title, path_sub_kml, path_data_title);

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/';
        path_sub_kml = 'rijkswaterstaat\dienstzeeland\outer_deltas\';
        kml_server = 'http://kml.deltares.nl/kml';
    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/';
        path_sub_kml = 'rijkswaterstaat\dienstzeeland\outer_deltas\';
        kml_server = 'http://dtvirt5.deltares.nl/kml';
    otherwise
        error
end

url_netcdf = path2os(fullfile(url_main_netcdf, path_sub_netcdf, path_data_title), 'h');

OPT = ncgen_surface_adf;
% --------------------------------------------------------------------------------------------------

OPT.main.file_incl          = '\w001001.adf$';
OPT.main.dir_excl          = '.svn$|wrong_offset';
OPT.main.dir_incl          = '.*';
OPT.main.dir_depth          = 3;
OPT.main.path_source        = '..\outerdeltas\raw';
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}(?=_|n)', 'match'), 'yyyy');
OPT.main.datefield          = 'pathname';

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', 'none',...
    '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    = cellsize;
OPT.schema.grid_offset      = 0;%cellsize/2;
OPT.schema.grid_tilesize    = [500 625]; %[nx ny]
OPT.schema.time_units       = 'days since 0000-01-00 00:00:00 +01:00';
OPT.schema.EPSGcode         = 28992;

OPT.schema.z_deflatelevel   = 9;

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

ncgen_surface_adf(OPT);

%% post process data
% 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', {})

%% 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_%s.txt', path_data_title, datestr(now,30))), 'w'))

%% make kml
path_kml = [path_kml '0'];
nc2kml_surface_imageOverlay(...
...ncgentools_surface_kml(...
    '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</br>provided by <a href="http://www.openearth.nl">OpenEarthTools</a></br>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>', ['<href>' path2os(fullfile(kml_server, path_sub_kml), 'http')])

%%
path_kml = [path_kml(1:end-1) '1'];
ncgentools_surface_kml(...
    '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</br>provided by <a href="http://www.openearth.nl">OpenEarthTools</a></br>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>', ['<href>' path2os(fullfile(kml_server, path_sub_kml), 'http')])


%% 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, kml_server),...
    '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</br>provided by <a href="http://www.openearth.nl">OpenEarthTools</a></br>generated: ', creator_url, institution_shortname, data_title, datestr(now())),...
    'kmlName', [data_title ' overview']);