data_title = 'Hydrografie survey grids';
data_summary = 'bathymetry and topography measurements of the Dutch Continental Shelf';
data_keywords = 'bathymetry, coast';
if ispc
    userdir = getenv('USERPROFILE');
else
    userdir = getenv('HOME');
end
path_main = fullfile(userdir, 'products');
path_sub_netcdf = fullfile('hydrografie', 'surveys');

publisher_email       = 'Tommer.Vermaas@deltares.nl';
institution_longname  = 'Koninklijke Marine Dienst der Hydrografie';
institution_shortname = 'Dienst der Hydrografie';

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

processing_level   = 'final';

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_asc;
% --------------------------------------------------------------------------------------------------

OPT.main.file_incl          = '\.asc$';
OPT.main.dir_depth          = 0;
OPT.main.path_source        = fullfile(fileparts(mfilename('fullpath')), '..', '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');
% '(?<=_)\d{8}(?=\.asc)' returns four digits '\d{8}' that are followed by
% .asc and are preceded by an _ '(?<=_)'
% OPT.main.dateFcn            = @(s) datenum(regexp(s, '(?<=_)\d{8}(?=\.asc)', 'match'), 'yyyymmdd');
OPT.main.dateFcn            = @(s) str2double(regexp(s, '(?<=_)[0-9]+(?=\.asc)', 'match'));
% OPT.main.dateFcn            = @(s) regexp(s, '(?<=_(sid)?)[0-9]+', 'match');

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', '',... %todo...url
	... % 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(nowutc, fmt), publisher_name, publisher_domain, publisher_computer, publish_script_id),...
	...'comment', '',...
	'cdm_data_type', 'grid',...
	'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.'] ...
    );

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

OPT.schema.grid_cellsize    = 25;
OPT.schema.grid_offset      = 12.5;
OPT.schema.grid_tilesize    = [200 200]; %[nx ny]
OPT.schema.time_units       = 'days since 1970-01-01';
OPT.schema.EPSGcode         = 32631; %WGS84 UTM zone 31N
% OPT.schema.x_add_offset     = min(S.xllcorner(:)); -> onderhoek!!
OPT.schema.x_datatype       = 'single';
% OPT.schema.x_scale_factor   = 10; % 10 m precision
% OPT.schema.y_add_offset     = min(S.yllcorner(:));
OPT.schema.y_datatype       = 'single';
% 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      = 1; % conversion from source data in cm to m
OPT.write.filenameFcn       = @(x,y) sprintf('x%6.0fy%7.0f.nc', min(x)-12.5,min(y)-12.5);

ncgen_surface_asc(OPT);

%% post process hydro nc with nc_hydro_postprocess.m
nc_hydro_postprocess([OPT.main.path_netcdf,filesep])

%% create catalog.nc files
ncgentools_generate_catalog(...
    'urlPath', path_netcdf,...
    'urlPathrep', url_netcdf)


% ncgentools_generate_catalog(...
%     'urlPath', path_netcdf,...
%     'urlPathrep', url_netcdf,...
%     'var', {},...
% ...    'mode', 'clobber',...
%     'reduce', true)

% 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 

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')))

% 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'],...
    'EPSGcode',32631);