%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: Dienst der Hydrografie (www.hydro.nl) gridded at 200m resolution';
OPT.baseUrl     = 'http://dtvirt5.deltares.nl/kml/???/';
OPT.logo        = 'hydro.gif';
OPT.lowestLevel = 12;

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

   OPT2(1).clim           = [-50 25];
   OPT2(1).colormap       =  colormap_cpt('bathymetry_vaklodingen',500);
   OPT2(1).varname        = 'depth';
   OPT2(1).dataset        = 'bathy200mgrid';

   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
   
      KMLfig2pngNew(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,']'],...
                    '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

      KMLfig2pngNew(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,']'],...
                    'fileName',[OPT.dataset '.kml']);
   
     try;close(FIG);end
      
   end
   
%% EOF
