function NCP2KML(varargin)
%NCP2KML   save TNO netCDF file of NCP as Google Earth file
%
%See also: NCP2NC

%   --------------------------------------------------------------------
%   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/ncp/catalog.html">http://opendap.deltares.nl/thredds/catalog/opendap/tno/ncp/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/ncp/';
   OPT.logo        = 'tno.gif'; % updated
   OPT.lowestLevel = 12;
   OPT.kmlName     = 'Dutch Continental Shelf (Nederlands Continentaal Plat) grain size data';

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

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

   OPT2(1).clim           = [100 450];
   OPT2(1).colormap       =  colormapgrainsize(64);
   OPT2(1).varname        = 'D10';
   OPT2(1).dataset        = 'dz10_juli2007';

   OPT2(2).clim           = [100 450];
   OPT2(2).colormap       = colormapgrainsize(64);
   OPT2(2).varname        = 'D50';
   OPT2(2).dataset        = 'dz50_juli2007';

   OPT2(3).clim           = [100 450];
   OPT2(3).colormap       =  colormapgrainsize(64);
   OPT2(3).varname        = 'D90';
   OPT2(3).dataset        = 'dz90_juli2007';

   OPT2(4).clim           = [0 50];
   OPT2(4).colormap       = colormap(colormapgray(.2,1));
   OPT2(4).varname        = 'pebbles';
   OPT2(4).dataset        = 'grind_fbr2007';

   OPT2(5).clim           = [0 50];
   OPT2(5).colormap       = flipud(copper);
   OPT2(5).varname        = 'mud';
   OPT2(5).dataset        = 'slib_juli2007';

   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;

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

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