function results = GetLocalWeather(varargin)
% Function to access data Citizen Weather Observer Program http://www.wxqa.com/
% The following links provide codes to stations (use the NOAA MesoMap
% codes)
% http://www.wxqa.com/states/unitedkingdom.html
% http://www.wxqa.com/stations.html
% To find observer stations paste "http://www.kanonbra.com/kml/cwop/APRSWXNETStation.kmz"
% into the maps.google.com search box and then zoom to your area. Double
% clicking on a tag will give you the code of the "site" interest but
% you'll need to convert it using the link above.
%
% A limited amount of control is available
% results = GetLocalWeather('days',5,'site','D0067')
% results = GetLocalWeather('days',5) % defaults to London Gatwick Airport
% results = GetLocalWeather('site','D0067') % Aboyne in Scotland, defaults to 50 days
% results = GetLocalWeather('saveasfile',true) % stores a text file of the HTML response
% Error handling not implemented but it only seems to break if either
% a) station is down(very very rare in my experience)
% b) the code is wrong
%
% Mark Selby 2013.
params.days=50;
params.site='EGKK';
params.saveasfile = true;
txParams=fieldnames(params);
for ctParam = 1:2:length(varargin),
ctInd = strcmp(...
varargin{ctParam},...
strvcat({txParams{:}}));
if isempty(ctInd)
error(['Unrecognised param : ',...
varargin{ctParam}])
else
params.(varargin{ctParam}) =...
varargin{ctParam+1};
end
end
% read data directly from URL
URL = 'http://weather.gladstonefamily.net/cgi-bin/wxobservations.pl';
param{1,1} = 'site';
param{1,2} = params.site;
% Give in parameters for password.
param{1,3} = 'days';
param{1,4} = num2str(params.days);
% Give in parameters for seriesname.
param{1,5} = 'html';
param{1,6} = '1';
txResponse = urlread(URL,'get',param);
if params.saveasfile
fid = fopen('response.txt','w');
fprintf(fid,'%s\n',txResponse);
fclose(fid);
end
txt = regexprep(txResponse,'<script.*?/script>','');
ctStart = regexp(txt,'<table>')+7;
ctEnd = regexp(txt,'</table>')-1;
txTable = txt(ctStart:ctEnd);
txRows = regexp(txTable,'<tr>','split');
% parse data and save as structure object
% I'm sure there is a better way of dealing with HTML data
for i = 1:(length(txRows)-2)
txDataPerRow = regexp(txRows{i+2},'<td>','split');
tiMatlabDays(i,:) = datenum(datevec(txDataPerRow{2}, 'yyyy-mm-dd HH:MM:SS'));
pBaro(i,:) = str2num(txDataPerRow{3});
tAir(i,:) = str2num(txDataPerRow{4});
tDew(i,:) = str2num(txDataPerRow{5});
rHumidity(i,:) = str2num(txDataPerRow{6});
dlWind(i,:) = str2num(txDataPerRow{7});
angWind(i,:) = str2num(txDataPerRow{8});
end
results.tiDay = tiMatlabDays;
tiMatlabDays0 = datenum('Jan-0-0000 00:00:00');
results.tiDay =results.tiDay -tiMatlabDays0;
results.pBaro = pBaro;
results.tAir = (tAir-32).*(5/9);
results.tDew = (tDew-32).*(5/9);
results.rHumidity = rHumidity;
results.dlWind = dlWind;
results.angWind = angWind;