-
Notifications
You must be signed in to change notification settings - Fork 0
/
metars.m
69 lines (58 loc) · 1.95 KB
/
metars.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function [T P] = metars( station, time )
%METARS Get atmospheric temperature and pressure.
% [T P] = METARS( STATION, TIME ) returns the temperature, T, and
% pressure, P, at the weather station, STATION, at time, TIME.
%
% STATION should be a four letter ICAO station identifier, such as
% 'KOSU'. If no STATION is specified, 'KOSU' is assumed. TIME should be a
% 6 digit date/time string of the form 'ddHHMM', such as '251453',
% meaning the 25th day, 14th hour, and 53rd minute. If no TIME is
% specified, the most recent measurements are returned.
if ~exist( 'station', 'var' )
station = 'KOSU';
end
% alpha = isstrprop( varargin, 'alpha' );
% if any(alpha)
% station = varargin{alpha};
% else
% station = 'KOSU';
% end
%
% num = isstrprop( varargin, 'digit' );
% if any(num), time = varargin{num}; end
%%
% Get the ADDS html page
url = 'http://www.aviationweather.gov/adds/metars/';
params = { 'station_ids', station, ...
'chk_metars', 'on' ...
'hoursStr', 'past 36 hours' };
html = urlread( url, 'get', params );
% Extract the METARS strings
expr = [ '>(' station ' [ A-Z0-9/]+)<' ];
strs = regexpi( html, expr, 'tokens' );
strs = cellfun( @(c) c, strs ); % Remove nested cell arrays
%%
% Select the METARS closest to the requested time
if exist('time','var')
expr = ' (\d+)Z '; % ex: find ' 251453Z ', keep '251453'
tstamps = regexpi( strs, expr, 'tokens', 'once' );
tstamps = cellfun( @(c) c, tstamps ); % Remove nested cell arrays
tstamps = cell2mat( tstamps' );
dt = datenum( time, 'ddHHMM' ) - datenum( tstamps, 'ddHHMM' );
[~,k] = min( abs(dt) );
ref = strs{k};
else
% If no time is requested, take the most recent
ref = strs{1};
end
%%
% Temperature [K]
expr = ' (\d+)/\d+ '; % ex: find ' 20/16 ', keep '20'
T = regexpi( ref, expr, 'tokens', 'once' );
T = str2double( T{1} );
T = T + 273.15;
% Pressure [Pa]
expr = ' A(\d+) '; % ex: find ' A2990 ', keep '2990'
P = regexpi( ref, expr, 'tokens', 'once' );
P = str2double( P{1} )/100;
P = 3386 * P;