function varargout = regenerate(varargin)
%NC_REGENERATE_DATABASE  This routine (re)runs various data processing routines.
%
%   See also 

%% 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>
% Created: 15 Nov 2009
% Created with Matlab version: 7.7.0.471 (R2008b)

% $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

%% regenerate standard names

%% clear waitbars
multiWaitbar('close all')

%% discard erroneous files first (shifted 10m)

 find_shifted_vakloding_files
 
 % TO DO : catalog

%% default properties

 OPT.nc. basepath_local      = 'D:\opendap.deltares.nl\thredds\rijkswaterstaat\jarkus' %vaklodingen'; % nu wegscrijven
 OPT.nc. basepath_network    = []; % '\\opendap\rijkswaterstaat\vaklodingen'; % naartoe kopieren
%OPT.nc. basepath_network    = []; % '??'; % naartoe kopieren
%OPT.nc. basepath_opendap    = 'http://dtvirt5.deltares.nl/thredds/catalog/opendap/rijkswaterstaat/vaklodingen_remapped/'; % te zien na kopieren
 OPT.nc. basepath_opendap    = 'http://opendap.deltares.nl/thredds/catalog/opendap/rijkswaterstaat/vaklodingen_remapped/'; % te zien na kopieren

OPT.kml.basepath_local      = 'D:\kml.deltares.nl\rijkswaterstaat\vaklodingen';
OPT.kml.basepath_network    = []; % '\\kml\rijkswaterstaat\vaklodingen';
OPT.kml.basepath_www        = 'http://kml.deltares.nl/kml/vaklodingen';

% metadata
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: regenerate.m 246 2010-07-29 15:35:44Z tda@vanoord.com$' 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          = ['These data are 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;

% 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 Vaklodingen';
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;

OPT = setPropertyInDeeperStruct(OPT,varargin{:});

%% call subfunctions

%select one of the following actions
%<<>> create nc files
asc_to_nc      (OPT,'nc.make',1,'nc.copy2server',0,'kml.make',0,'kml.copy2server',0); 
% % 
%<<>> create KML file
% % asc_to_nc      (OPT,'nc.make',0,'nc.copy2server',0,'kml.make',1,'kml.copy2server',0); 
%fill_nc_files  (OPT,'nc.make',1,'nc.copy2server',0,'kml.make',0,'kml.copy2server',0);  % does not work

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.nc                      = ncFcn();
    OPT.nc                      = setproperty(OPT.nc,varargin{1}.nc);
%% NC SETTINGS, ADJUST AS NEEDED ------------------------------------------------------------------------------------------
    OPT.nc. mapsizex            = 10000; % same as vaklodingen, where ncols = 500
    OPT.nc. gridsizex           = 20;
    OPT.nc. mapsizey            = 12500; % same as vaklodingen, where nrows = 625
    OPT.nc. gridsizey           = OPT.nc. gridsizex;
    OPT.nc. xoffset             = OPT.nc. gridsizex/2; % nc_multibeam_from_asc oses 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;
    OPT.nc. raw_path            = '..\raw\grid';
    OPT.nc. raw_extension       = '*.asc';
    OPT.nc. netcdf_path         = 'unfilled';
    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               = 'Jarkus raaien' %'Vaklodingen';
%% 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});
    ncFcn (OPT.nc);
    kmlFcn(OPT.kml);
    
%% rename to Rijkswaterstaat KB names and add notification of that to global atts (is slow though)

    nc_multibeam_from_asc2vaklodingen(OPT.kml.ncpath); 
    
%% check dataset and make inventory

    nc_cf_gridset_get_list(OPT.kml.ncpath,'fid','nc_cf_gridset_get_list.txt')
    if OPT.nc.copy2server
    nc_cf_gridset_get_list(OPT.ncbasepath_opendap)
    end
    
end

function OPT = fill_nc_files(varargin)
%% FUNCTIONS TO USE, ADJUST AS NEEDED ------------------------------------------------------------------------------------
    ncFcn                       = @(varargin) (nc_multibeam_from_nc(varargin{:}));
    kmlFcn                      = @(varargin) (nc_multibeam_to_kml_tiled_png(varargin{:}));
%% LEAVE AS IS ------------------------------------------------------------------------------------------------------------
    OPT.nc                      = ncFcn();
    OPT.nc                      = setproperty(OPT.nc,varargin{1}.nc);
%% NC SETTINGS, ADJUST AS NEEDED ------------------------------------------------------------------------------------------
    OPT.nc. mapsizex            = 10000;
    OPT.nc. gridsizex           = 20;
    OPT.nc. mapsizey            = 10000;
    OPT.nc. gridsizey           = 20;
    OPT.nc. xoffset             = 10;
    OPT.nc. yoffset             = 10;
    OPT.nc. zfactor             = 1;
    OPT.nc. raw_path            = fullfile(OPT.nc. basepath_local, OPT.nc.netcdf_path);
    OPT.nc. raw_extension       = '*.nc';
    OPT.nc. netcdf_path         = 'filled';
    OPT.nc. zip                 = false;
    OPT.nc. zip_extension       = '*.7z';
    
    OPT.nc. fill_data           = true;            

    OPT.nc .gridFcn             = @(X,Y,Z,XI,YI) flipud(Z);
%% NC META DATA, ADJUST AS NEEDED -----------------------------------------------------------------------------------------
    OPT.nc. title               = 'Jarkus raaien' %'Vaklodingen';
%% 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        = true;
%% LEAVE AS IS ------------------------------------------------------------------------------------------------------------
    OPT = setPropertyInDeeperStruct(OPT, varargin{2:end});
    ncFcn(OPT.nc);
    kmlFcn(OPT.kml);

%% rename to Rijkswaterstaat KB names and add notification of that to global atts (is slow though)

    nc_multibeam_from_asc2vaklodingen(OPT.kml.ncpath,'fid','nc_cf_gridset_get_list.txt')
    
%% check dataset and make inventory

    nc_cf_gridset_get_list(OPT.kml.ncpath)
    if OPT.nc.copy2server
    nc_cf_gridset_get_list(OPT.ncbasepath_opendap)
    end

end
