function varargout = regenerate(varargin)
warning('lots of data overwritten in 4948, 4746, 4544')
warning('some erroneous ignored (shifted 10m)
%REGENERATE This routine puts a set of Rijkswaterstaat ascii grid files into a nc_cf_gridset KB collection and kml-ifies them.
%
% See also: find_shifted_vakloding_files, nc_multibeam_from_asc2vaklodingen, nc_cf_opendap2catalog, vaklodingen_definition

%% This is exactly the same script that processes DienstZeeland,jarkus and vaklodingen grids
% the only difference are the OPT settings below

 OPT.rijkswaterstaat_dataset_name = 'vaklodingen'; %'DienstZeeland'; % jarkus, % 
 OPT.nc. raw_path                 = '..\raw\grid';

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2009 Van Oord
%       Mark van Koningsveld
%
%       mrv@vanoord.com	
%
%       Watermanweg 64
%       POBox 8574
%       3009 AN Rotterdam
%       Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed 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.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tools is part of VOTools which is the internal clone of <a href="http://OpenEarth.Deltares.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%% settings
% overrule default settings by property pairs, given in varargin

% the hierarchy of settings is as follows:
%   1: function call
%   2: subfunction properties
%   3: general properties
%   4: default properties

%% clear waitbars
 multiWaitbar('close all')

%% default properties
% where the netCDF files are written now
 OPT.nc. basepath_local      = ['D:\opendap.deltares.nl\thredds\rijkswaterstaat\',OPT.rijkswaterstaat_dataset_name,'\'];

% where the netCDF files will copied to now
 OPT.nc. basepath_network    = []; % '\\opendap\rijkswaterstaat\DienstZeeland';

% where the netCDF files can be linked from after copying
 OPT.nc. basepath_opendap    = ['http://opendap.deltares.nl/thredds/catalog/opendap/rijkswaterstaat/',OPT.rijkswaterstaat_dataset_name,'/']; % te zien na kopieren

% where the kml files are written to now
 OPT.kml.basepath_local      = ['D:\kml.deltares.nl\rijkswaterstaat\',OPT.rijkswaterstaat_dataset_name];

% where the kml files will copied to now
 OPT.kml.basepath_network    = []; % '\\kml\rijkswaterstaat\',OPT.rijkswaterstaat_dataset_name];

% where the netCDF files can be linked from after copying
 OPT.kml.basepath_www        = ['http://kml.deltares.nl/kml/rijkswaterstaat/',OPT.rijkswaterstaat_dataset_name];

% netCDF meta-data
 OPT.nc. Conventions         = 'CF-1.5';
 OPT.nc. CF_featureType      = 'grid';
 OPT.nc. institution         = 'Rijkswaterstaat';
 OPT.nc. source              = 'Rijkswaterstaat';
 OPT.nc. history             = ['Created with: $Id$ ' char(10)...
                               '$HeadURL$'];
 OPT.nc. references          = 'No reference material available';
 OPT.nc. comment             = 'generated by redefining vaklodingen kaartbladen';
 OPT.nc. email               = ' ';
 OPT.nc. version             = ' ';
 OPT.nc. terms_for_use       = ' ';
 OPT.nc. disclaimer          = 'Rijkswaterstaat data owner: http://www.rijkswaterstaat.nl/disclaimer/. OpenEarth netCDF data distributor: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.nc. EPSGcode            = 28992;
 OPT.nc. title               = [OPT.rijkswaterstaat_dataset_name, ' bathymetry'];

% kml settings                           
 OPT.nc. cache_path          = fullfile(tempdir,'nc_asc');
 OPT.kml.lowestLevel         = 16;
 OPT.kml.tiledim             = 256;
 OPT.kml.make_kmz            = false;
 OPT.kml.CBtemplateVer       = 'KML_colorbar_template_vertical.png';
 OPT.kml.ncfile              = '*.nc';
 OPT.kml.descriptivename     =['Rijkswaterstaat ',OPT.rijkswaterstaat_dataset_name,' bathymetry data'];
 OPT.kml.CBcolorTitle        = 'Depth in meters';
 OPT.kml.description         = OPT.nc. history;
 OPT.kml.clim                = [-50 25];
 OPT.kml.colorMap            = @(m)colormap_cpt('bathymetry_vaklodingen',m);
% OPT.kml.lightAdjust       = true;

%% opdate properties
OPT = setPropertyInDeeperStruct(OPT,varargin{:});

%% create netCDF files
asc_to_nc      (OPT,'nc.make' ,1,'nc.copy2server' ,~isempty(OPT.nc .basepath_network),...
                    'kml.make',0,'kml.copy2server',~isempty(OPT.kml.basepath_network)); 

%% create KML files
asc_to_nc      (OPT,'nc.make' ,0,'nc.copy2server' ,~isempty(OPT.nc .basepath_network),...
                    'kml.make',0,'kml.copy2server',~isempty(OPT.kml.basepath_network)); 

end

%% Dataset subfunctions

function OPT = asc_to_nc(varargin)
%% FUNCTIONS TO USE, ADJUST AS NEEDED ------------------------------------------------------------------------------------
    ncFcn                       = @(varargin) (nc_multibeam_from_asc        (varargin{:})); % makes netcdf3 by default now
    kmlFcn                      = @(varargin) (nc_multibeam_to_kml_tiled_png(varargin{:}));
%% LEAVE AS IS ------------------------------------------------------------------------------------------------------------
    OPT.rijkswaterstaat_dataset_name = varargin{1}.rijkswaterstaat_dataset_name;
    OPT.nc                      = ncFcn();
    OPT.nc                      = setproperty(OPT.nc,varargin{1}.nc);
%% NC SETTINGS, ADJUST AS NEEDED ------------------------------------------------------------------------------------------
    OPT.nc. mapsizex            = 10000; % [m] same as vaklodingen, where ncols = 500
    OPT.nc. gridsizex           = 20;
    OPT.nc. mapsizey            = 12500; % [m] same as vaklodingen, where nrows = 625
    OPT.nc. gridsizey           = OPT.nc. gridsizex;
    OPT.nc. xoffset             = OPT.nc. gridsizex/2; % nc_multibeam_from_asc uses xoffset for pixels centres, so should be cellsize/2
    OPT.nc. yoffset             = OPT.nc. gridsizex/2; % dx and dy sare by definition same in ESRI asc file
    OPT.nc. zfactor             = 0.01;                % cm in asc files to m in netcdf files
    OPT.nc. raw_extension       = '*.asc';
    OPT.nc. netcdf_path         = ''; % we do not use the option to fill holes
    OPT.nc. zip                 = false;
    OPT.nc. zip_extension       = '*.7z';
    OPT.nc. dateFcn             = @(s)datenum(s(12:19),'yyyymmdd');
    
%% NC META DATA, ADJUST AS NEEDED -----------------------------------------------------------------------------------------
    OPT.nc. title               = OPT.rijkswaterstaat_dataset_name;
%% LEAVE AS IS ------------------------------------------------------------------------------------------------------------
    OPT.kml                     = kmlFcn();
    OPT.kml                     = setproperty(OPT.kml,varargin{1}.kml);
    OPT                         = setPropertyInDeeperStruct(OPT, varargin{2:end});
%% KML SETTINGS, ADJUST AS NEEDED -----------------------------------------------------------------------------------------
    OPT.kml.ncpath              = fullfile(OPT.nc.basepath_local,OPT.nc. netcdf_path);
    OPT.kml.relativepath        = OPT.nc.netcdf_path;
    OPT.kml.filledInTime        = false;
%% LEAVE AS IS ------------------------------------------------------------------------------------------------------------
    OPT = setPropertyInDeeperStruct(OPT, varargin{2:end});

    nc_multibeam_from_asc_shift_odds(OPT.nc.raw_path, [OPT.nc.raw_path, filesep, 'shifted'], 0)
    ncFcn (OPT.nc);
    kmlFcn(OPT.kml);
    
%% rename to Rijkswaterstaat KB names and add notification of that to global atts (is slow though, but better after nc_pad_header)

    nc_multibeam_from_asc2vaklodingen(OPT.kml.ncpath,'basename',OPT.rijkswaterstaat_dataset_name); 
    
%% remake catalog after renaming, and make sure it links to remove files
  
    CATALOG = nc_cf_opendap2catalog(OPT.kml.ncpath,... % dir where to READ netcdf
                   'catalog_dir',OPT.kml.ncpath,... % dir where to SAVE catalog
                          'save',1,...
                       'varname',{'x','y','time'},...
                          'disp',[]);
    
    dodsCUrl = strrep(OPT.nc. basepath_opendap,'/catalog/','/dodsC/')

    CATALOG.urlPath = char(strrep(cellstr(CATALOG.urlPath),OPT.kml.ncpath,dodsCUrl));
    struct2nc([OPT.kml.ncpath,'catalog.nc'],CATALOG)
    
    CATALOG.urlPath = char(strrep(cellstr(CATALOG.urlPath),dodsCUrl,''));
    struct2nc([OPT.kml.ncpath,'catalog_local_machine.nc2'],CATALOG)

%% check dataset and make inventory

    nc_cf_gridset_get_list(OPT.kml.ncpath,'fid',[OPT.nc. basepath_local,filesep,'nc_cf_gridset_get_list.txt'])

    if OPT.nc.copy2server
    nc_cf_gridset_get_list(OPT.nc.basepath_opendap)
    end
    
%% make kml overview

    nc_cf_gridset_overview('fileName',[OPT.kml.basepath_local,'overview.kml'],...
                           'load_kml',OPT.kml.basepath_local,...
                         'kmlPathFcn',@(s) strrep(s,OPT.kml.basepath_local,OPT.kml.basepath_www),...
                            'datestr','YYYY-mm',...
                            'load_nc',OPT.nc.basepath_local,...
                         'urlPathFcn',@(s) path2os(strrep(OPT.nc. basepath_opendap,'/catalog/','/dodsC/'     ),'http'),...
                         'ftpPathFcn',@(s) path2os(strrep(OPT.nc. basepath_opendap,'/catalog/','/fileServer/'),'http'),...
                        'description',['Rijkswaterstaat ',OPT.rijkswaterstaat_dataset_name,' provided by OpenEarth.eu']);
end
