function DGM2KML(varargin)
%DGM2KML   save TNO netCDF file of DGM as Google Earth file
%
%See also: DGM2NC

%   --------------------------------------------------------------------
%   Copyright (C) 2010 Deltares for Building with Nature
%       Gerben de Boer
%
%       g.j.deboer@deltares.nl
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 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 General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.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.

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

   clear all
   close all
   
   OPT.description = 'Source data: Geological Survey of the Netherlands (www.tno.nl) gridded at 200m resolution. This OGC KML visualisation is provided by OpenEarth (www.OpenEarth.eu). The data are available as OPeNDAP webservice at <a href="http://opendap.deltares.nl/thredds/catalog/opendap/tno/dgm/catalog.html">http://opendap.deltares.nl/thredds/catalog/opendap/tno/dgm/catalog.html</a>, the original data and processing scripts are stored under version control at $HeadURL$. (OpenEarth registration required.)';
   OPT.baseUrl     = 'http://kml.deltares.nl/kml/tno/dgm/';
   OPT.logo        = 'tno.gif'; % updated
   OPT.lowestLevel = 13;
   OPT.kmlName     = 'Dutch Geological Data';

   ncdirectory   = [first_subdir(mfilename('fullpath'),-1),filesep,'processed',filesep];
   kmldirectory  = [first_subdir(mfilename('fullpath'),-1),filesep,'kml'      ,filesep];

   ncdirectory   = 'F:\opendap\thredds\tno\dgm\';
   kmldirectory  = 'F:\kml\tno\dgm\';

   OPT2(1).clim           = [-150 25];
   OPT2(1).colormap       =  colormapgrainsize(64);
   OPT2(1).varname        = 'kr_bcc';
   OPT2(1).dataset        = 'kr_bcc';

   OPT2(2).clim           = [-40 0];
   OPT2(2).colormap       = colormapgrainsize(64);
   OPT2(2).varname        = 'hl_bcc';
   OPT2(2).dataset        = 'hl_bcc';

   for jj=1:length(OPT2)   
   
      disp(['processing ',num2str(jj,'%0.4d'),': ',OPT2(jj).dataset]);

      OPT.clim       = OPT2(jj).clim;
      OPT.colormap   = OPT2(jj).colormap;
      OPT.varname    = OPT2(jj).varname;
      OPT.dataset    = OPT2(jj).dataset;
      
      OPT

      ncname    = [ncdirectory ,OPT.dataset '.nc' ];
      kmlname   = [kmldirectory,OPT.dataset '.kml'];

   %% load
   
      [D,M] = nc_cf_grid(ncname,OPT.varname,'plot',0);
      
      %hist(D.(OPT.varname)(:),100);
      %xlim([0 100])
      %print2screensize([OPT.varname,'_hist'])
      %close
      %continue
   
   %% plot
   
      FIG = figure('Visible','on');
      h   = surf(D.lon,D.lat,D.(OPT.varname));
      shading    interp;
      axis       tight;
      view      (0,90);
      clim      (OPT.clim);
      colormap  (OPT.colormap);
      break

      %[ax, h]=colorbarwithtitle('mud',OPT.ctick);
      %set(ax,'YTickLabel',OPT.cticklabel)
      axislat
      %colorbar;break
      
      lightangle(0,90)
      material  ([.9 0.08 .07]);
      lighting   phong
      axis       off;
      
   %% make kml: first only mother kml for local use
   
      KMLfigure_tiler(h,D.lat,D.lon,D.(OPT.varname),...
                  'printTiles',0,... % !!!!!!!!!
                 'description',OPT.description,...
                     'baseUrl','',...
                    'basePath',kmldirectory,...
                        'logo',OPT.logo,... 
                 'lowestLevel',OPT.lowestLevel,...
          'mergeExistingTiles',true,...
                     'bgcolor',[255 0 255],...
             'CBcolorbartitle',[M.(OPT.varname).long_name,' [',M.(OPT.varname).units,']'],...
                     'kmlName',OPT.kmlName,...
                    'fileName',[OPT.dataset '.kml']);
                    
      copyfile([kmldirectory filesep OPT.dataset '.kml'],...
               [kmldirectory filesep OPT.dataset '.local_machine.kml']);

   %% make kml: then full set for server use

      KMLfigure_tiler(h,D.lat,D.lon,D.(OPT.varname),...
                  'printTiles',1,...
                 'description',OPT.description,...
                     'baseUrl',OPT.baseUrl,...
                    'basePath',kmldirectory,...
                        'logo',OPT.logo,... 
                 'lowestLevel',OPT.lowestLevel,...
          'mergeExistingTiles',true,...
                     'bgcolor',[255 0 255],...
             'CBcolorbartitle',[M.(OPT.varname).long_name,' [',M.(OPT.varname).units,']'],...
                     'kmlName',OPT.kmlName,...
                    'fileName',[OPT.dataset '.kml']);

     try;close(FIG);end
      
   end
   
%% EOF
