%DINO2KML   save dino sediment cores as KML columns + link to dinoloket enriched meta-data
%
% The raw data for this script can be downloaded in the required odv format from nodc.nl.
%
% data source      www.TNO.nl
% data provider    www2.dinoloket.nl
% data distributor www.nodc.nl (SeaDataNet EDMO code 635)
% demo provider    www.NMDC.nl
% demo creation    www.OpenEarth.nl
%
% This script takes for 13427 cores about 10 minutes
%
%See also: OceanDataView

%   --------------------------------------------------------------------
%   Copyright (C) 2011 Deltares for NMDC.eu
%       Gerben J. de Boer
%
%       gerben.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 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 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.pause        = 0;
OPT.basedir      = '..\..\..\seadatanet\';

%% TNO lithogaphy

SET.directory         = [OPT.basedir,filesep,'userHBM-data_centre635-220711_result\'];
SET.sdn_standard_name = 'SDN:P011::XXXXXXXX';
SET.sdn_standard_name = 'SDN:P011::SEGMLENG';
SET.clim              = [0 2];
SET.z                 = 'SDN:P011::COREDIST';
SET.zScaleFun         = @(z)100*z; % deepest cores go down to O(100m)
SET.urlFcn            = @(x,y,z,w,v)['station = ',y,' <br> LOCAL_CDI_ID = ',x,' <br> date = ',z,' <br> link = <a href="http://www2.dinoloket.nl/dinoLks/minisite/Entry?datatype=bor&id=',x,'&queryProperty=NitgNumber">DINO</a> <br> strata = ',v];
SET.urlFcn            = @(x,y,z,w,v)['<hr><table bgcolor="#333333" cellpadding="3" cellspacing="1"><tbody><tr><td colspan="2" bgcolor="#666666"><div style="color:#FFFFFF;">Meta-data:</div></td>',...
 '</tr><tr><td bgcolor="#FFFFFF">station     </td><td bgcolor="#FFFFFF">',y,'</td>',...
 '</tr><tr><td bgcolor="#FFFFFF">LOCAL_CDI_ID</td><td bgcolor="#FFFFFF">',x,'</td>',...
 '</tr><tr><td bgcolor="#FFFFFF">date        </td><td bgcolor="#FFFFFF">',z,'</td>',... 
 '</tr><tr><td bgcolor="#FFFFFF">link        </td><td bgcolor="#FFFFFF"><a href="http://www2.dinoloket.nl/dinoLks/minisite/Entry?datatype=bor&id=',x,'&queryProperty=NitgNumber">DINO</a></td>',...
 '</tr><tr><td bgcolor="#FFFFFF">strata      </td><td bgcolor="#FFFFFF">',v,'</td></tr></tbody></table><hr>'];

%%
for i=1:length(SET)

   L = odv_metadata(SET(i).directory); % add extraction of # params and # 
   
   clear D
   
% Cycle CDi and load

   for ifile=1:length(L.name);
       
      disp([num2str(ifile),'/',num2str(length(L.name))])
   
      fname    = L.name{ifile};
      D(ifile) = odvread([SET(i).directory,filesep,fname],'resolve',0,'CDI_record_id',L.CDI_record_id{ifile});
       
   end % ifile 
   
   % cache save('userHBM-data_centre635-220711_result','D');

%% merge data
   
     M2D     = odv_merge(D,'sdn_standard_name',SET(i).sdn_standard_name,'z',SET(i).z,'metadataFcn',@(z) z(1),'dataFcn',@(z) sum(z));
     M3D     = odv_merge(D,'sdn_standard_name',SET(i).sdn_standard_name,'z',SET(i).z,'metadataFcn',@(z) z(1),'dataFcn',@(z) z(:));
     M3Dtype = odv_merge(D,'sdn_standard_name','SDN:P011::XXXXXXXX'    ,'z',SET(i).z,'metadataFcn',@(z) z(1),'dataFcn',@(z) z(:));
   
%% make one kml with all data from downloaded set, encompassing multiple odv files

      text = cellfun(SET(i).urlFcn,...
                  cells2cell(M2D.LOCAL_CDI_ID),...
                  cells2cell(M2D.station),...
                  cellstr(datestr(cell2mat(M2D.datenum),'yyyy-mmm-dd'))',...
                  cells2cell(M2D.CDI_record_id),...
                  cellfun(@(x) str2line(x,'s','|'),M3Dtype.data,'uni',0)',...
                  'uniformoutput',0);
                  
%% spatio-temporal meta-data and resolvable links 

      KMLmarker(cell2mat(M2D.latitude),cell2mat(M2D.longitude),cell2mat(M2D.data),...
               'kmlName','data points',...
           'description','',...
               'snippet','',...
                  'name',cells2cell(M2D.LOCAL_CDI_ID),... 
                  'html',text,...
              'fileName',[last_subdir(SET(i).directory),'_part.kml'],...
                'timeIn',datestr(cell2mat(M2D.datenum)-1,'yyyy-mm-dd'),... % datestr is Muuch faster
               'timeOut',datestr(cell2mat(M2D.datenum)+1,'yyyy-mm-dd'),...
       'iconnormalState','http://maps.google.com/mapfiles/kml/pal5/icon5.png',... % make it also work locally
    'iconhighlightState','http://maps.google.com/mapfiles/kml/pal5/icon5.png',...
      'scalenormalState',1,'scalehighlightState',1)
      
      kmlmerge_files('fileName',[last_subdir(SET(i).directory),'.kml'],...
                  'sourceFiles',{[last_subdir(SET(i).directory),'_part.kml'],'dino2kml_logos.kml'},...
                      'kmlName','DEMO DINO NCP cores',...
                      'snippet','DEMO of spatio-temporal overview of DINO NCP cores with deep-links to DINO meta-database',...
                  'description',str2line(loadstr('dino2kml_description.kml'),'s',' '));
       
%% 3D cores

      % TO DO think about what should be up
      % and whether to add a dummy offset (no, because a few cores are realy deep)
      % dataclasses = unique(cellstr(char(cellfun(@(x) char(unique(x)),M2.data,'uni',0)))); % see colormap_dino

      [CB.map,CB.label]=colormap_dino;
      M3Dtype.classes = cellfun(@(x) colormap_dino(x),M3Dtype.data,'uni',0);
      M3D.z          = cellfun(@(z) (1.*[0;cumsum(z)]),M3D.data,'uniformoutput',0);
      
      KMLcolumn  (M2D.latitude   ,...
                  M2D.longitude  ,...
                  M3D.z,...
                  M3Dtype.classes,'nTH',6,'R',250,'dR',1,...
                  'fileName',[last_subdir(SET(i).directory),'_cylinders_part.kml'],...
                  'kmlName','data columns upside-down',...
                  'description','The columns are plotted <b>upside-down</b>. This quarantees that layers that are at the same depth in the soil are at the same level in Google Earth, while at the same time it allows simultaneous inspection of different orders of depth: 30 cm grab samples and 200m deep cores.',...
                  'snippet','',...
                  'colorMap',@(x)colormap_dino,...
                  'colorSteps',size(colormap_dino,1),'cLim',[0.5 length(CB.label)+.5],...
                  'fillAlpha',.9,'zScaleFun',SET(i).zScaleFun,...
                  'colorbar',1,...
                  'CBcolorbarlocation',{'E'},...
                  'CBcolorTitle','TNO DINO sediment classification',...
                  'CBcolorTickLabel',CB.label,'CBcolorTick',1:length(CB.label));
      %%
      
      clear D text M2D M3D M3Dtype L % to save memory
      
      kmlmerge_files('fileName',[last_subdir(SET(i).directory),'_cylinders.kml'],...
                  'sourceFiles',{[last_subdir(SET(i).directory),'_cylinders_part.kml'],'dino2kml_logos.kml'},...
                    'LookAtLon',3.2,...      
                    'LookAtLat',52.3,...       
               'LookAtAltitude',[],...  
                  'LookAtrange',2e5,...  
                   'LookAttile',70,...  
                'LookAtheading',25,...  
                      'kmlName','DEMO DINO NCP cores',...
                         'open',1,...
                      'snippet','DEMO of spatio-temporal overview of DINO NCP cores with deep-links to DINO meta-database',...
                  'description',str2line(loadstr('dino2kml_description.kml'),'s',' '));

end % set

%% make kmz

kml2kmz([last_subdir(SET(i).directory),          '.kml'],{'dinoloket1.jpg','icon5.png','logo-TNO.gif','nmdc.jpg','nmdc4GE.png','OET.png','oet4GE.png','OET60.jpg','tno4GE.png'})
kml2kmz([last_subdir(SET(i).directory),'_cylinders.kml'],{'dinoloket1.jpg','icon5.png','logo-TNO.gif','nmdc.jpg','nmdc4GE.png','OET.png','oet4GE.png','OET60.jpg','tno4GE.png'},[last_subdir(SET(i).directory),'_cylinders_part_ver_rgt.png'])
