%VLIEGLIJNEN  download+parse+plot WFS vlieglijnen for kusthoogte
%
% the accuracy is days
%
% http://geoservices.rijkswaterstaat.nl/kusthoogte_vlieglijnen?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetcApabilities
%
%See also: xml_read

clear all
close all
clc

OPT.download = 0;
OPT.png      = 0;
OPT.kml      = 1;
OPT.dirout   = mfilename;

%% download WFS GML xml
% define *_2 to be waddenzee and voordelta

typenames = {'AAA1000' }; % different time notation, no date, only time
filenames = {'2005.xml'};

typenames = {'AAA1100','AAA900','AAA800','AAA700','AAA600','AAA500','AAA400','AAA300','AAA200','AAA100','vliegl_kust_2011','vliegl_kust_2012'};
filenames = {'2004'   ,'2006_2','2006'  ,'2007_2','2007'  ,'2008_2','2008'  ,'2009_2','2009'  ,'2010'  ,'2011'            ,'2012'            };

% WFS typename    | filename=rws meta-data name
% ----------------+----------------------------
% vliegl_kust_2012 2012
% vliegl_kust_2011 2011
% AAA100           2010
% AAA200           2009
% AAA300           2009_2
% AAA400           2008
% AAA500           2008_2
% AAA600           2007
% AAA700           2007_2
% AAA800           2006
% AAA900           2006_2
% AAA1000          2005
% AAA1100          2004

% 2004:   2004-Mar-17  >  2004-Oct-11
% 2006_2: 2006-Dec-01  >  2006-Dec-10
% 2006:   2006-Mar-12  >  2006-May-06
% 2007_2: 2007-Aug-04  >  2007-Dec-14
% 2007:   2007-Mar-09  >  2007-Apr-13
% 2008_2: 2008-May-14  >  2008-Jul-28
% 2008:   2008-Apr-01  >  2008-Jun-24
% 2009_2: 2009-Apr-02  >  2009-Jul-13
% 2009:   2009-Mar-16  >  2009-Apr-05
% 2010:   2010-Mar-06  >  2010-May-25
% 2011:   2011-Jan-20  >  2011-Aug-19
% 2012:   2012-Jan-02  >  2012-Mar-22


%% plot adjuvant
L = nc2struct('D:\opendap.deltares.nl\thredds\dodsC\opendap\deltares\landboundaries\holland_fillable.nc');
 
kmlnames = {};   

for ifile=1:length(typenames)
    
   disp(['processing ',num2str(ifile),'/',num2str(length(typenames))])
   wfs = ['http://geoservices.rijkswaterstaat.nl/kusthoogte_vlieglijnen?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=',typenames{ifile}];
   url = ['http://geoservices.rijkswaterstaat.nl/metadata/HOOGTE.vlieglijnen_kust_',filenames{ifile}];

%% download

   if OPT.download
      urlwrite(wfs,[OPT.dirout,filesep,filenames{ifile},'.xml']);
   end

%% load gml xml

   D = rws_wfs_kusthoogte_vlieglijnen_read([OPT.dirout,filesep,filenames{ifile},'.xml']);

%% plot

   if OPT.kml
      kmlnames{end+1} = [OPT.dirout,filesep,typenames{ifile},'.kml'];
      timeIn  =  D.datenum  ;timeIn (isnan(timeIn )) = min(timeIn);
      timeOut = D.datenum+1;timeOut(isnan(timeOut)) = max(timeOut);
      
      description = ...   
      ['<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">Rijkswaterstaat WFS-GML-XML      </td><td bgcolor="#FFFFFF"><a href="',wfs,'">xml file</a></td>',...
       '</tr><tr><td bgcolor="#FFFFFF">Rijkswaterstaat ISO-XML meta-data</td><td bgcolor="#FFFFFF"><a href="',url,'">html file</a></td></tr></tbody></table><hr>'];
      KMLline([D.lat0 D.lat1 ]',[D.lon0 D.lon1]',...
                     'fileName',kmlnames{end},...
                       'timeIn',timeIn,...
                      'timeOut',timeOut,...
                    'lineColor',[1 1 1],...
                      'kmlName',['RWS:name',pad(filenames{ifile},14,' '),' - WFS:typename ',typenames{ifile}],...
                         'name',cellstr([char(D.id),repmat(' - ',[length(D.id) 1]),char(D.datestr)]),...
                  'description',description);
   end

   if OPT.png
      close
      days = unique(round(D.datenum));
      days = [days(~isnan(days));nan]; % 1 nan for missing dates
      nday = length(days);
      colors  = [jet(nday-1); .5 .5 .5];% gray for missing dates
      daytext = [datestr(days(1:end-1),'yyyy/mm/dd HH:MM');'no date         '];
      for i=1:n
         iday = find(round(D.datenum(i))==days);
         if isempty(iday)
             iday = nday;
         end
         plot([D.x0(i) D.x1(i)]',[D.y0(i) D.y1(i)]','color',colors(iday,:))
         hold on
      end
      caxis([1 nday]+[-.5 .5])
      colormap(colors)
      [ax,h]=colorbarwithvtext('time',[1:nday]);
       set(ax,'YTickLabel',daytext)
      axis(axis);
      axis([0      290000      300000      640000])
      tickmap('xy','texttype','text')
      axislat(0)
      plot(L.x,L.y,'color',[.5 .5 .5])
      grid on
      title({mktex(['RWS:meta-data name: ',filenames{ifile},' - WFS:typename ',typenames{ifile}]),...
             mktex(url)})
      
      text(1,0,{' Presentation:OpenEarth.nl, data:',...
                ' http://geoservices.Rijkswaterstaat.nl/kusthoogte_vlieglijnen?',...
                ' SERVICE=WFS&',...
                ' VERSION=1.1.0&',...
                ' REQUEST=GetFeature&',...
                [' TYPENAME=',typenames{ifile}]},'rotation',90,'units','normalized','vert','bot','fontsize',7)
      
      print2screensizeoverwrite([OPT.dirout,filesep,'kusthoogte_vlieglijnen_',filenames{ifile}])
   
   end % plot   
  
   
end % files

%% merge all seperate kmls

   KMLmerge_files('sourceFiles',kmlnames,'fileName','kusthoogte_vlieglijnen.kml',...
   'description',str2line(loadstr('vlieglijnen_description.kml'),'s',' '));