data_title = 'Bathymetry measurements Rijkswaterstaat Dienst Zeeland';
data_summary = 'Bathymetry measurements Zeeland';
data_keywords = 'bathymetry, coast, estuary';
if ispc
    userdir = getenv('USERPROFILE');
else
    userdir = getenv('HOME');
end
path_main = fullfile(userdir, 'products');
path_sub_netcdf = fullfile('rijkswaterstaat', 'dienstzeeland');

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       = path_sub_netcdf;
path_kml = fullfile(path_main, 'kml', path_sub_kml);

fmt = 'yyyy-mm-ddTHH:MMZ';

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

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

%%
OPT = ncgen_surface_adf;

% main settings
OPT.main.file_incl          = 'w001001\.adf$';
OPT.main.dir_excl          = '.svn$';
OPT.main.dir_incl          = '.*';
OPT.main.dir_depth          = inf;
OPT.main.path_source        = fullfile(fileparts(mfilename('fullpath')), '..');
OPT.main.path_netcdf        = path_netcdf;
OPT.main.dateFcn            = @(s) datenum( regexp(s,'(?<=(bez|ga|d|_))\d{4}','match'), 'yyyy');
OPT.main.datefield          = 'pathname';

% schema settings
OPT.schema.grid_cellsize    = 20;
OPT.schema.grid_offset      = 0;
OPT.schema.grid_tilesize    = [500 625]; %[nx ny]
OPT.schema.time_units       = 'days since 1970-01-01';
OPT.schema.EPSGcode         = 28992;
OPT.schema.x_datatype       = 'int32';
OPT.schema.y_datatype       = 'int32';

OPT.schema.meta             = struct(...
	... % Identification
	'id', sprintf('%s_release%s', path_data_title, datestr(nowutc, 'yyyymmdd')),...
	'naming_authority', 'deltares.nl',... % based on reverse DNS lookup (http://remote.12dt.com/)
	'Metadata_Conventions', 'CF-1.6',...
	...'metadata_link', '',...
	... % 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('%s: Created by %s on computer %s\\%s with script %s',...
	datestr(nowutc, fmt), publisher_name, publisher_domain, publisher_computer, publish_script_id),...
	'comment', '',...
	'cdm_data_type', 'grid',...
	...'acknowledgment', '',...
	'creator_name', creator_name,...
	'creator_url', creator_url,...
	'creator_email', creator_email,...
	'institution', institution_longname,...
    'date_issued', datestr(nowutc, fmt),...
	'publisher_name', publisher_name,...
	'publisher_url', 'http://www.deltares.nl',...
	'publisher_email', publisher_email,...
	... % Other attributes
	'processing_level', processing_level,...
	'WARNING','THIS DATA IS NOT TO BE USED FOR NAVIGATIONAL PURPOSES. FOR NAVIGATION CHARTS PLEASE REFER TO <http://www.defensie.nl/marine/hydrografie/nautische_producten/navigatiekaarten> & <http://www.vaarweginformatie.nl>',...
	'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.'] ...
    );

% write settings
OPT.write.filenameFcn       = @(x,y) sprintf('dienstzeeland%s.nc', vaklodingen_coord2kb(min(x),min(y)));

% read settings
OPT.read.z_scalefactor      = 0.01; % conversion from source data in cm to m
OPT.read.checkFcn_args      = {'adjust_offset', @(s) isempty(regexpi(s, 'd1983_z36-37')),...
    'zmin', -2e9};


ncgen_surface_adf(OPT);

% %% post process data
% ncgentools_fill_and_sort_along_dimension(...
%     path_netcdf,...
%     fullfile(path_netcdf, 'sorted'),...
%     'dimension_name','time');

%% create catalog.nc files
ncgentools_generate_catalog(...
    'urlPath', path_netcdf,...
    'urlPathrep', url_netcdf,...
    'reduce', true,...
    '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 

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(nowutc, fmt)),...
    '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 = [fileparts(kml_surface_overlay) '.kml'];
copyfile(kml_surface_overlay,...
    kml_surface_overlay_copy)

strrep_in_files(kml_surface_overlay_copy,...
    '<href>', sprintf('<href>http://%s', path2os(fullfile([kml_server '/kml/'], [path_sub_kml, filesep]), 'h')))
%{

strrep_in_files(sprintf('d:\heijer\sources\heijer\python\sandbox\%s_overview.kml', path_data_title),...
    '<td>netCDF</td><td><a href="', sprintf('<td>netCDF</td><td><a href="http://dtvirt5.deltares.nl:8080/thredds/dodsC/opendap/rijkswaterstaat/%s/', path_data_title))
strrep_in_files(sprintf('d:\heijer\sources\heijer\python\sandbox\%s_overview.kml', path_data_title),...
    '<td>download</td><td><a href="', sprintf('<td>download</td><td><a href="http://dtvirt5.deltares.nl:8080/thredds/fileServer/opendap/rijkswaterstaat/%s/', path_data_title))
%}

% 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', [fileparts(kml_surface_overlay) '_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(nowutc, fmt)),...
    'kmlName', [data_title ' overview']);