function [D,typname] = rws_wfs_kusthoogte_vlieglijnen_read(filename)
%RWS_WFS_KUSTHOOGTE_VLIEGLIJNEN_READ parse WFS file
%
%See also: xml_read

   R = xml_read   (filename,struct('KeepNS',0));

%% parse xml

   clear D
   n = length(R.featureMember);
   D.datenum  = repmat(nan,[n 1]);
   D.x0       = repmat(nan,[n 1]);
   D.x1       = repmat(nan,[n 1]);
   D.y0       = repmat(nan,[n 1]);
   D.y1       = repmat(nan,[n 1]);
   D.id       = cell(n,1);
   D.idnr     = repmat(nan,[n 1]);
   D.datestr  = cell(n,1);
   D.filenaam = cell(n,1);
   D.OBJECTID      = cell(n,1);
   D.LENGTH        = repmat(nan,[n 1]);
   D.STROKEN_ID    = repmat(nan,[n 1]);
   D.FILENAAM      = cell(n,1);
   D.BR_LINKS      = repmat(nan,[n 1]);
   D.BR_RECHTS     = repmat(nan,[n 1]);
   R.featureMember(1)
   typname = fieldnames(R.featureMember(1));
   if length(typname) > 1
       error('WFS xml with multiple featureMembers')
   else
      typname = char(typname);
   end
   
   %% extract dates: catch 6 flavours
   for i=1:n
       
   % vliegl_kust_2012, vliegl_kust_2011, AAA200, AAA300, AAA500, AAA800:
   %         <rws:VLIEGDATUM>00:00:00 mm/dd/2009</rws:VLIEGDATUM>
   % 
   % AAA100,AAA400, AAA700, AAA600, AAA700, AAA900, AAA1100: mm/dd swapped !!
   %         <rws:VLIEGDATUM>dd-mm-2007</rws:VLIEGDATUM>
   % 
   % AAA700, AAA400: dutch month names !!
   %         <rws:VLIEGDATUM>17 dec 2007</rws:VLIEGDATUM>
   %         <rws:VLIEGDATUM>27 maart  2008</rws:VLIEGDATUM>   
   %         <rws:VLIEGDATUM>27 april
   % AAA500:
   %         <rws:VLIEGDATUM>&lt;null&gt;</rws:VLIEGDATUM>
   % 
   % AAA1000:
   %         <rws:DATE_> </rws:DATE_>
   %         <rws:START_TIME>13:00:57</rws:START_TIME>
   %         <rws:END_TIME>13:02:14</rws:END_TIME>

   if  isfield(R.featureMember(i).(typname),'VLIEGDATUM')
      datetxt = R.featureMember(i).(typname).VLIEGDATUM;
   else
      datetxt = R.featureMember(i).(typname).DATUM;
   end
   
   %disp(datetxt)
   try
      D.datenum(i) = datenum(datetxt,'HH:MM:SS mm/dd/yyyy');
   catch
       try
       D.datenum(i) = datenum(datetxt,'dd-mm-yyyy');
       catch
           try
           tmp = strtokens2cell(datetxt);
           tmp{2} = monthstr_mmmm_dutch2eng(tmp{2});
           D.datenum(i) = datestr([tmp{3},'-',tmp{2},'-',tmp{1}],'yyyy-mmm-dd'); % 'dd mmm yyyy': part of AAA700, AAA400 Dutch !!
           catch
           D.datenum(i) = nan; % part of AAA500
           end
       end
   end
   % rounded to whole days, grr
   end
   disp([filename,':  ',datestr(min(D.datenum),'yyyy-mmm-dd'), '  >  ', datestr(max(D.datenum),'yyyy-mmm-dd')])

%% load (x,y) and sort

   for i=1:length(D.datenum)
      D.x0(i)         = R.featureMember(i).(typname).GEOMETRY.LineString.posList.CONTENT(1);
      D.x1(i)         = R.featureMember(i).(typname).GEOMETRY.LineString.posList.CONTENT(3);
      D.y0(i)         = R.featureMember(i).(typname).GEOMETRY.LineString.posList.CONTENT(2);
      D.y1(i)         = R.featureMember(i).(typname).GEOMETRY.LineString.posList.CONTENT(4);
      D.id{i}         = R.featureMember(i).(typname).ATTRIBUTE.id; % R.featureMember(j).(typname).OBJECTID;
      D.OBJECTID{i}   = R.featureMember(i).(typname).OBJECTID;
      if isfield(R.featureMember(i).(typname),'LENGTH');    D.LENGTH(i)     = R.featureMember(i).(typname).LENGTH;    end
      if isfield(R.featureMember(i).(typname),'STROKEN_ID');D.STROKEN_ID(i) = R.featureMember(i).(typname).STROKEN_ID;end
      if isfield(R.featureMember(i).(typname),'FILENAAM');  D.FILENAAM{i}   = R.featureMember(i).(typname).FILENAAM;  end
      % 2006, 2006_2, 2007, 2007_2, 2008, 2008_2, 2009, 2009_2, 2010 (niet 2004, 2011, 2012)
      
      if isfield(R.featureMember(i).(typname),'BR_LINKS');  D.BR_LINKS(i)   = R.featureMember(i).(typname).BR_LINKS;  end
      if isfield(R.featureMember(i).(typname),'BR_RECHTS'); D.BR_RECHTS(i)  = R.featureMember(i).(typname).BR_RECHTS; end

      j = strfind(D.id{i},'.');
      D.idnr(i)    = str2num(D.id{i}(j+1:end));
      D.datestr{i} = datestrnan(D.datenum(i),['yyyy-mmm-dd'],' '); % take care of exact times HH:MM here later
   end
   
    [~,ind] = sort(D.idnr);
    D = structsubs(D,ind);
   
   [D.lon0,D.lat0] = convertCoordinates(D.x0,D.y0,'CS1.code',28992,'CS2.code',4326);
   [D.lon1,D.lat1] = convertCoordinates(D.x1,D.y1,'CS1.code',28992,'CS2.code',4326);