function [ t, values ] = GetMatroosSeries(unit,source,location,tstart,tstop)
%function [ t, values ] = get_series(unit,source,location,start_time,end_time);
% eg.
%[t,wl]=get_series('waterlevel','observed','hoekvanholland','200802190000','200802210000');

serverurl='http://deltares:M@TR00$@matroos.deltares.nl/direct/get_series.php?';

%
% available units, sources and locations 
% !!!! at 20080219 may change in the future, TODO read this data from the
% server
% 
locations = {'aalesund','aberdeen','akkaert','alblasserdam','amelander zeegat boei 1-1', ...
           'amelander zeegat boei 1-2','amelander zeegat boei 2-1','amelander zeegat boei 2-2', ...
           'amelander zeegat boei 3-1','amelander zeegat boei 3-2','amelander zeegat boei 4-1', ...
           'amelander zeegat boei 4-2','amelander zeegat boei 5-1','amelander zeegat boei 5-2', ...
           'amelander zeegat boei 6-1','amelander zeegat boei 6-2','anasuria', ...
           'aukfield platform','barseback','beerenplaat','Berkhout','bernisse','bodoe', ...
           'bol van heist','borkum','brakelsveer','Breesem boei 1','Breezanddijk buiten boei 1', ...
           'bremerhaven','brent alpha 1','brest port (Fr)','brienenoordbrug', ...
           'brouwershavensegat 2','brouwershavensegat 2 boven','brouwershavensegat 2 onder', ...
           'brouwershavensegat 8','brouwershavensegat 8 boven','brouwershavensegat 8 onder', ...
           'buoy_M1 (Ire)','buoy_M2 (Ire)','buoy_M3 (Ire)','buoy_M4 (Ire)','buoy_M5 (Ire)', ...
           'cadzand','cherbourg (Fr)','cuxhaven','darsser sill 19m','darsser sill 7m','De Kooij', ...
           'delfzijl','den helder','den oever','Den Oever binnen','Den Oever buiten','deurlo', ...
           'dnz_554','dnz_BRN','dnz_EFA','dnz_FE','dnz_IJ03','dnz_MW4','dnz_NHRSE','dordrecht', ...
           'dover','Edam','eemshaven','Elbe','esbjerg','eurogeul DWE','eurogeul E13','eurogeul E5', ...
           'europlatform','europlatform 2','europlatform 3','F161','FINO','FINO1_-33m','FINO1_12m', ...
           'FINO1_30m','GermanBight','golf_FL25_2006','golf_FL26_2006','golf_FL2_2006', ...
           'golf_FL5_2006','golf_FL9_2006','goteborg torshamnen','GWEms','hagestijn', ...
           'haringvliet 10','haringvliet 10 boven','haringvliet 10 onder','harlingen','helgeroa', ...
           'helgoland','hellevoetsluis','Hoek van Holland Noorderdam','hoekvanholland', ...
           'hoekvanholland NAP-2.5m','hoekvanholland NAP-4.5m','hoekvanholland NAP-9.0m', ...
           'hoekvanholland stroompaal 1','Hollandse brug','holyhead','Hoorn/Terschelling', ...
           'Houtribsluizen noord','Houtribsluizen zuid','huibertgat','husum','hvide sande havn', ...
           'ij-geul IJ5','ij-geul stroompaal 1','ijmond erosieput','ijmond stroompaal', ...
           'ijmond stroompaal 2','ijmuiden','IJmuiden kop pier','ijmuiden munitiestort 1', ...
           'ijmuiden munitiestort 2','ilfracombe','immingham','Kadoelen','Kampen','Kamperhoek', ...
           'Katerveer','Ketelhaven','kittiwake','klagshamn','kornwerderzand', ...
           'Kornwerderzand binnen','Kornwerderzand buiten','kornwerderzand2','kornwerderzand3', ...
           'Krabbersgatsluizen noord','Krabbersgatsluizen zuid','laso ost','laso ost 10m', ...
           'laso ost 2m','laso ost 70m','lauwersoog','le havre (Fr)','leith','Lemmer', ...
           'les minquiers (Fr)','lichteiland goeree 1','lichteiland goeree 2','lobith', ...
           'lowestoft','maassluis','Malzwin boei 1','Marknesse','megen','Mond der Vecht','Nes', ...
           'nieuw beerta','nieuw statenzijl','nieuwpoort','Nijkerkersluis','noordwijk meetpost', ...
           'north cormorant 1','northshields','NSB3','NsbII_-10m','NsbII_15m','NsbII_30m', ...
           'NsbII_6m','NsbIII_-10m','NsbIII_30m','NsbIII_6m','offharwi','Olst','oostende', ...
           'Oosterschelde 04','oosterschelde-11','oosterschelde-4 boven', ...
           'oosterschelde-4 onder','oscarsborg','Oude Schild','ouessant large (Fr)', ...
           'Pannengat boei 1','PKF Post','platform f3','platform k13a','platform k13a-2', ...
           'platform k13a-3','Ramspolbrug','ringhals','Roggebotsluis noord','Roggebotsluis zuid', ...
           'roompot buiten','roscoff (Fr)','rotterdam','Schaar','Schellingwoude','scheur oost', ...
           'scheur west','scheveningen','schiermonnikoog','schiermonnikoog westgat', ...
           'schouwenbank','sheerness','skanor','smogen','spijkenisse','Spooldersluis','stavanger', ...
           'Stavoren','stenungsund','Sylt','terschelling noordzee','Texelhors','Tholen','tiel', ...
           'torsminde','tredge','TW_Ems_-14m','TW_Ems_0m','TW_Ems_15m','TW_Ems_30m','TW_Ems_6m', ...
           'UFS_Deutsche_Bucht_-14m','UFS_Deutsche_Bucht_0m','UFS_Deutsche_Bucht_15m', ...
           'UFS_Deutsche_Bucht_30m','UFS_Deutsche_Bucht_6m','vaderoarna','Valkenburg','vardoe', ... 
           'Vechterweerd','vidaa','vidaasylt','viken','Vilsteren beneden','Vilsteren boven', ...
           'vlaardingen','vlakte vd raan','vlakte vd raan boven','vlakte vd raan onder', ...
           'Vlieland','Vlieland Haven','vlissingen','wadden eierlandse gat', ...
           'wadden schiermonnikoog','werkendam buiten','westhinder','weymouth','whitby','wick', ...
           'wieldrecht','wierumergronden','Wijdenes','Wilhelminadorp','wittduen','zdv8-2d-ALBL', ...
           'zdv8-2d-AO94','zdv8-2d-BEER','zdv8-2d-BGAT','zdv8-2d-BOTB','zdv8-2d-BOTL', ...
           'zdv8-2d-BREE','zdv8-2d-BRIE','zdv8-2d-CALB','zdv8-2d-EEMH','zdv8-2d-HARK', ...
           'zdv8-2d-HARM','zdv8-2d-HEYS','zdv8-2d-HOEK','zdv8-2d-MMND','zdv8-2d-PERN', ...
           'zdv8-2d-ROTB','zdv8-2d-ROTE','zdv8-2d-ROTK','zdv8-2d-SCHK','zdv8-2d-SEIN', ...
           'zdv8-2d-SGRA','zdv8-2d-SPYB','zdv8-2d-SUUR','zdv8-2d-V085','zdv8-2d-WAAL', ...
           'zdv8-2d-WIEL','zdv8-2d-ZAND','Zwartsluis'};
sources =  {'bsh_oper','csm8_ecmwf_wind','csm8_ukmo_wind','dcsm_kalman_oper','dcsm_oper','dmi_bak', ...
           'dmi_oper','dnmi_oper','hmr_linux_csm8','hmr_linux_ijmond','hmr_linux_kustfijn', ...
           'hmr_linux_kustgrof','hmr_linux_zeedelta','hmr_linux_zuno','ijsselmeer', ...
           'knmi_ecmwf_wind','knmi_hirlam_wind','knmi_noos','knmi_noos_kalman','knmi_preoper', ...
           'knmi_ukmo_wind','markermeer','mumm_omnecs_oper','mumm_oper','observed', ...
           'observed_knmi','rws_oper_hmcz','rws_oper_hmr','rws_oper_svsd','ukmo_oper', ...
           'wind_dsc_wdij'};
units=     {'air_pressure','discharge','salinity','swellwave_height_hm0','water_direction', ...
           'water_speed','water_temperature','waterlevel','waterlevel_astro','waterlevel_surge', ...
           'wave_dir_th0','wave_dirspread_s0bh','wave_height_h1d10','wave_height_h1d3', ...
           'wave_height_h1d50','wave_height_hm0','wave_height_hmax','wave_period_t1d3', ...
           'wave_period_th1d3','wave_period_tm02','wave_period_tm10','wave_period_tp', ...
           'wave_period_tz','wind_blast','wind_direction','wind_speed','wind_speed_max'};

%
%check input
%
iunit = strmatch(unit,units);
if(isempty(iunit)), error('could not find unit');end;
unit=strrep(unit,' ','%20'); % %20=<space>
isource = strmatch(source,sources);
if(isempty(isource)), error('could not find source');end;
source=strrep(source,' ','%20'); % %20=<space>
iloc = strmatch(location,locations);
if(isempty(iloc)), error('could not find location');end;
location=strrep(location,' ','%20'); % %20=<space>

fprintf('unit=%s\n',unit);
fprintf('source=%s\n',source);
fprintf('location=%s\n',location);
fprintf('first time=%s\n',tstart);
fprintf('last time=%s\n',tstop);

%
% get data from matroos
%
urlChar = sprintf('%sunit=%s&source=%s&loc=%s&tstart=%s&tstop=%s', ...
    serverurl,unit,source,location,tstart,tstop);
%disp(urlChar);
%eg temp=geturl('http://svsd:n0v2006@matroos.deltares.nl/direct/get_series.php?loc=hoekvanholland&source=observed&unit=waterlevel&tstart=200802180000&tstop=200802190000')
allLines = geturl_matroos(urlChar);

%
% parse 
%

%skip header
i=0;done=0;
while((done==0)&&(i<length(allLines))),
    i=i+1;
    done=(isempty(findstr(allLines{i},'#')));
end;

%read allLines
done=0;
pointIndex=1;
t=[];
values=[];
while(i<length(allLines)),
    line = allLines{i};
    data = sscanf(line,'%f %f');
    values(pointIndex) = data(end);
    year = sscanf(line(1:4),'%d');
    month= sscanf(line(5:6),'%d');
    day  = sscanf(line(7:8),'%d');
    hour = sscanf(line(9:10),'%d');
    min  = sscanf(line(11:12),'%d');
    sec  = 0;
    t(pointIndex) = datenum(year,month,day,hour,min,sec);
    i=i+1;
    pointIndex=pointIndex+1;
end;

