-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metar.php
1837 lines (1560 loc) · 44.4 KB
/
Metar.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
===========================
HSDN METAR/TAF Parser Class
===========================
Version: 1.0
This library is based on GetWx script by Mark Woodward.
(c) 2024, Spin Opel (https://spinopel.top/)
(c) 2013-2020, Information Networks, Ltd. (http://www.hsdn.org/)
(c) 2001-2006, Mark Woodward (http://woody.cowpi.com/phpscripts/)
This script is a PHP library which allows to parse the METAR and TAF code,
and convert it to an array of data parameters. These METAR or TAF can be given
in the form of the ICAO code string (in this case, the script will receive data
from the NOAA website) or in raw format (just METAR/TAF code string). METAR or
TAF code parsed using the syntactic analysis and regular expressions. It solves
the problem of parsing the data in the presence of any error in the code METAR
or TAF. In addition to the return METAR parameters, the script also displays the
interpreted (easy to understand) information of these parameters.
*/
class Metar
{
/*
* Array of decoded result, by default all parameters is null.
*/
private $result = array
(
'raw' => NULL,
'taf' => NULL,
'taf_flag' => NULL,
'station' => NULL,
'observed_date' => NULL,
'observed_day' => NULL,
'observed_time' => NULL,
'observed_age' => NULL,
'wind_speed' => NULL,
'wind_gust_speed' => NULL,
'wind_direction' => NULL,
'wind_direction_label' => NULL,
'wind_direction_varies' => NULL,
'varies_wind_min' => NULL,
'varies_wind_min_label' => NULL,
'varies_wind_max' => NULL,
'varies_wind_max_label' => NULL,
'visibility' => NULL,
'visibility_report' => NULL,
'visibility_min' => NULL,
'visibility_min_direction' => NULL,
'runways_visual_range' => NULL,
'present_weather' => NULL,
'present_weather_report' => NULL,
'clouds' => NULL,
'clouds_report' => NULL,
'cloud_height' => NULL,
'cavok' => NULL,
'temperature' => NULL,
'temperature_f' => NULL,
'dew_point' => NULL,
'dew_point_f' => NULL,
'humidity' => NULL,
'heat_index' => NULL,
'heat_index_f' => NULL,
'wind_chill' => NULL,
'wind_chill_f' => NULL,
'barometer' => NULL,
'barometer_in' => NULL,
'recent_weather' => NULL,
'recent_weather_report' => NULL,
'runways_report' => NULL,
'runways_snoclo' => NULL,
'wind_shear_all_runways' => NULL,
'wind_shear_runways' => NULL,
'forecast_temperature_min' => NULL,
'forecast_temperature_max' => NULL,
'trends' => NULL,
'remarks' => NULL
);
/*
* Methods used for parsing in the order of data
*/
private $method_names = array
(
'taf',
'station',
'time',
'station_type',
'wind',
'varies_wind',
'visibility',
'visibility_min',
'runway_vr',
'present_weather',
'clouds',
'temperature',
'pressure',
'recent_weather',
'runways_report',
'wind_shear',
'forecast_temperature',
'trends',
'remarks'
);
/*
* Interpretation of weather conditions intensity codes.
*/
private $weather_intensity_codes = array
(
'-' => 'light',
'+' => 'strong',
'VC' => 'in the vicinity'
);
/*
* Interpretation of weather conditions characteristics codes.
*/
private $weather_char_codes = array
(
'MI' => 'shallow',
'PR' => 'partial',
'BC' => 'patches of',
'DR' => 'low drifting',
'BL' => 'blowing',
'SH' => 'showers of',
'TS' => 'thunderstorms',
'FZ' => 'freezing'
);
/*
* Interpretation of weather conditions type codes.
*/
private $weather_type_codes = array
(
'DZ' => 'drizzle',
'RA' => 'rain',
'SN' => 'snow',
'SG' => 'snow grains',
'IC' => 'ice crystals',
'PL' => 'ice pellets',
'GR' => 'hail',
'GS' => 'small hail', // and/or snow pellets
'UP' => 'unknown',
'BR' => 'mist',
'FG' => 'fog',
'FU' => 'smoke',
'VA' => 'volcanic ash',
'DU' => 'widespread dust',
'SA' => 'sand',
'HZ' => 'haze',
'PY' => 'spray',
'PO' => 'well-developed dust/sand whirls',
'SQ' => 'squalls',
'FC' => 'funnel cloud, tornado, or waterspout',
'SS' => 'sandstorm/duststorm'
);
/*
* Interpretation of cloud cover codes.
*/
private $cloud_codes = array
(
'NSW' => 'no significant weather are observed',
'NSC' => 'no significant clouds are observed',
'NCD' => 'nil cloud detected',
'SKC' => 'no significant changes expected',
'CLR' => 'clear skies',
'NOBS' => 'no observation',
//
'FEW' => 'a few',
'SCT' => 'scattered',
'BKN' => 'broken sky',
'OVC' => 'overcast sky',
//
'VV' => 'vertical visibility'
);
/*
* Interpretation of cloud cover type codes.
*/
private $cloud_type_codes = array
(
'CB' => 'cumulonimbus',
'TCU' => 'towering cumulus'
);
/*
* Interpretation of runway visual range tendency codes.
*/
private $rvr_tendency_codes = array
(
'D' => 'decreasing',
'U' => 'increasing',
'N' => 'no tendency'
);
/*
* Interpretation of runway visual range prefix codes.
*/
private $rvr_prefix_codes = array
(
'P' => 'more',
'M' => 'less'
);
/*
* Interpretation of runway runway deposits codes.
*/
private $runway_deposits_codes = array
(
'0' => 'clear and dry',
'1' => 'damp',
'2' => 'wet or water patches',
'3' => 'rime or frost covered',
'4' => 'dry snow',
'5' => 'wet snow',
'6' => 'slush',
'7' => 'ice',
'8' => 'compacted or rolled snow',
'9' => 'frozen ruts or ridges',
'/' => 'not reported'
);
/*
* Interpretation of runway runway deposits extent codes.
*/
private $runway_deposits_extent_codes = array
(
'1' => 'from 10% or less',
'2' => 'from 11% to 25%',
'5' => 'from 26% to 50%',
'9' => 'from 51% to 100%',
'/' => NULL
);
/*
* Interpretation of runway runway deposits depth codes.
*/
private $runway_deposits_depth_codes = array
(
'00' => 'less than 1 mm',
'92' => '10 cm',
'93' => '15 cm',
'94' => '20 cm',
'95' => '25 cm',
'96' => '30 cm',
'97' => '35 cm',
'98' => '40 cm or more',
'99' => 'closed',
'//' => NULL
);
/*
* Interpretation of runway runway friction codes.
*/
private $runway_friction_codes = array
(
'91' => 'poor',
'92' => 'medium/poor',
'93' => 'medium',
'94' => 'medium/good',
'95' => 'good',
'99' => 'figures unreliable',
'//' => NULL
);
/*
* Trends time codes.
*/
private $trends_flag_codes = array
(
'BECMG' => 'expected to arise soon',
'TEMPO' => 'expected to arise temporarily',
'INTER' => 'expected to arise intermittent',
'PROV' => 'provisional forecast',
'CNL' => 'cancelled forecast',
'NIL' => 'nil forecast'
);
/*
* Trends time codes.
*/
private $trends_time_codes = array
(
'AT' => 'at',
'FM' => 'from',
'TL' => 'until'
);
/*
* Interpretation of compass degrees codes.
*/
private $direction_codes = array
(
'N', 'NNE', 'NE', 'ENE',
'E', 'ESE', 'SE', 'SSE',
'S', 'SSW', 'SW', 'WSW',
'W', 'WNW', 'NW', 'NNW'
);
/*
* Debug and parse errors information.
*/
private $errors = NULL;
private $debug = NULL;
private $debug_enabled;
/*
* Other variables.
*/
private $raw;
private $raw_parts = array();
private $method = 0;
private $part = 0;
/**
* This method provides METAR and TAF information, you want to parse.
*
* Examples of raw METAR for test:
* UMMS 231530Z 21002MPS 2100 BR OVC002 07/07 Q1008 R13/290062 NOSIG RMK QBB070
* UWSS 231500Z 14007MPS 9999 -SHRA BR BKN033CB OVC066 03/M02 Q1019 R12/220395 NOSIG RMK QFE752
* UWSS 241200Z 12003MPS 0300 R12/1000 DZ FG VV003CB 05/05 Q1015 R12/220395 NOSIG RMK QFE749
* UATT 231530Z 18004MPS 130V200 CAVOK M03/M08 Q1033 R13/0///60 NOSIG RMK QFE755/1006
* KEYW 231553Z 04008G16KT 10SM FEW060 28/22 A3002 RMK AO2 SLP166 T02780222
* EFVR 231620Z AUTO 19002KT 5000 BR FEW003 BKN005 OVC007 09/08 Q0998
* KTTN 051853Z 04011KT M1/2SM VCTS SN FZFG BKN003 OVC010 M02/M02 A3006 RMK AO2 TSB40 SLP176 P0002 T10171017=
* UEEE 072000Z 00000MPS 0150 R23L/0500 R10/1000VP1800D FG VV003 M50/M53 Q1028 RETSRA R12/290395 R31/CLRD// R/SNOCLO WS RWY10L WS RWY11L TEMPO 4000 RADZ BKN010 RMK QBB080 OFE745
* UKDR 251830Z 00000MPS CAVOK 08/07 Q1019 3619//60 NOSIG
* UBBB 251900Z 34015KT 9999 FEW013 BKN030 16/14 Q1016 88CLRD70 NOSIG
* UMMS 251936Z 19002MPS 9999 SCT006 OVC026 06/05 Q1015 R31/D NOSIG RMK QBB080 OFE745
*/
public function __construct($raw, $taf = FALSE, $debug = FALSE, $icao = TRUE)
{
$this->debug_enabled = $debug;
// Raw is a ICAO code
if ($icao AND preg_match('@^([A-Z]{1}[A-Z0-9]{3})$@', $raw))
{
$raw = $this->download_raw($raw, $taf);
}
if (empty($raw))
{
throw new Exception('The METAR or TAF information is not presented.');
}
$raw_lines = explode("\n", $raw, 2);
if (isset($raw_lines[1]))
{
$raw = trim($raw_lines[1]);
// Get observed time from a file data
$observed_time = strtotime(trim($raw_lines[0]));
if ($observed_time != 0)
{
$this->set_observed_date($observed_time);
$this->set_debug('Observation date is set from the METAR/TAF in first line of the file content: '.trim($raw_lines[0]));
}
}
else
{
$raw = trim($raw_lines[0]);
}
$this->raw = rtrim(trim(preg_replace('/[\s\t]+/s', ' ', $raw)), '=');
if ($taf)
{
$this->set_debug('Infromation presented as TAF or trend.');
}
else
{
$this->set_debug('Infromation presented as METAR.');
}
$this->set_result_value('taf', $taf);
$this->set_result_value('raw', $this->raw);
}
/**
* Gets the value from result array as class property.
*/
public function __get($parameter)
{
if (isset($this->result[$parameter]))
{
return $this->result[$parameter];
}
return NULL;
}
/**
* Parses the METAR or TAF information and returns result array.
*/
public function parse()
{
$this->raw_parts = explode(' ', $this->raw);
$current_method = 0;
// See parts
while ($this->part < sizeof($this->raw_parts))
{
$this->method = $current_method;
// See methods
while ($this->method < sizeof($this->method_names))
{
$method = 'get_'.$this->method_names[$this->method];
$token = $this->raw_parts[$this->part];
if ($this->$method($token) === TRUE)
{
$this->set_debug('Token "'.$token.'" is parsed by method: '.$method.', '.
($this->method - $current_method).' previous methods skipped.');
$current_method = $this->method;
$this->method++;
break;
}
$this->method++;
}
if ($current_method != $this->method - 1)
{
$this->set_error('Unknown token: '.$this->raw_parts[$this->part]);
$this->set_debug('Token "'.$this->raw_parts[$this->part].'" is NOT PARSED, '.
($this->method - $current_method).' methods attempted.');
}
$this->part++;
}
// Delete null values from the TAF report
if ($this->result['taf'] === TRUE)
{
foreach ($this->result as $parameter => $value)
{
if (is_null($value))
{
unset($this->result[$parameter]);
}
}
}
return $this->result;
}
/**
* Returns array with debug information.
*/
public function debug()
{
return $this->debug;
}
/**
* Returns array with parse errors.
*/
public function errors()
{
return $this->errors;
}
/**
* This method downloads METAR or TAF information for a given station from the
* National Weather Service. It assumes that the station exists.
*/
private function download_raw($icao, $taf = FALSE)
{
if ($taf)
{
$url = 'http://tgftp.nws.noaa.gov/data/forecasts/taf/stations/'.$icao.'.TXT';
}
else
{
$url = 'http://tgftp.nws.noaa.gov/data/observations/metar/stations/'.$icao.'.TXT';
}
if (!$raw = @file_get_contents($url))
{
throw new Exception('Error while downloading METAR or TAF information');
}
$this->set_debug('METAR/TAF infromation downloaded from: '.$url);
return $raw;
}
/**
* This method formats observation date and time in the local time zone of server,
* the current local time on server, and time difference since observation. $time_utc is a
* UNIX timestamp for Universal Coordinated Time (Greenwich Mean Time or Zulu Time).
*/
private function set_observed_date($time_utc)
{
$local = $time_utc + date('Z');
$now = time();
$this->set_result_value('observed_date', date('r', $local)); // or "D M j, H:i T"
$time_diff = floor(($now - $local) / 60);
if ($time_diff < 91)
{
$this->set_result_value('observed_age', $time_diff.' min. ago');
}
else
{
$this->set_result_value('observed_age', floor($time_diff / 60).':'.sprintf("%02d", $time_diff % 60).' hr. ago');
}
}
/**
* Sets the new value to parameter in result array.
*/
private function set_result_value($parameter, $value, $only_is_null = FALSE)
{
if ($only_is_null)
{
if (is_null($this->result[$parameter]))
{
$this->result[$parameter] = $value;
$this->set_debug('Set value "'.$value.'" ('.gettype($value).') for null parameter: '.$parameter);
}
}
else
{
$this->result[$parameter] = $value;
$this->set_debug('Set value "'.$value.'" ('.gettype($value).') for parameter: '.$parameter);
}
}
/**
* Sets the data group to parameter in result array.
*/
private function set_result_group($parameter, $group)
{
if (is_null($this->result[$parameter]))
{
$this->result[$parameter] = array();
}
array_push($this->result[$parameter], $group);
$this->set_debug('Add new group value ('.gettype($group).') for parameter: '.$parameter);
}
/**
* Sets the report text to parameter in result array.
*/
private function set_result_report($parameter, $report, $separator = ';')
{
$this->result[$parameter] .= $separator.' '.$report;
if (!is_null($this->result[$parameter]))
{
$this->result[$parameter] = ucfirst(ltrim($this->result[$parameter], ' '.$separator));
}
$this->set_debug('Add group report value "'.$report.'" for parameter: '.$parameter);
}
/**
* Adds the debug text to debug information array.
*/
private function set_debug($text)
{
if ($this->debug_enabled)
{
if (is_null($this->debug))
{
$this->debug = array();
}
array_push($this->debug, $text);
}
}
/**
* Adds the error text to parse errors array.
*/
private function set_error($text)
{
if (is_null($this->errors))
{
$this->errors = array();
}
array_push($this->errors, $text);
}
// --------------------------------------------------------------------
// Methods for parsing raw parts
// --------------------------------------------------------------------
/**
* Decodes TAF code if present.
*/
private function get_taf($part)
{
if ($part != 'TAF')
{
return FALSE;
}
if ($this->raw_parts[$this->part + 1] == 'COR' OR $this->raw_parts[$this->part + 1] == 'AMD')
{
$this->set_result_value('taf_flag', $this->raw_parts[$this->part + 1], TRUE);
$this->part++;
}
$this->set_debug('TAF infromation detected.');
$this->set_result_value('taf', TRUE);
return TRUE;
}
/**
* Decodes station code.
*/
private function get_station($part)
{
if (!preg_match('@^([A-Z]{1}[A-Z0-9]{3})$@', $part, $found))
{
return FALSE;
}
$this->set_result_value('station', $found[1]);
$this->method++;
return TRUE;
}
/**
* Decodes observation time.
* Format is ddhhmmZ where dd = day, hh = hours, mm = minutes in UTC time.
*/
private function get_time($part)
{
if (!preg_match('@^([0-9]{2})([0-9]{2})([0-9]{2})Z$@', $part, $found))
{
return FALSE;
}
$day = intval($found[1]);
$hour = intval($found[2]);
$minute = intval($found[3]);
if (is_null($this->result['observed_date']))
{
// Get observed time from a METAR/TAF part
$observed_time = mktime($hour, $minute, 0, date('n'), $day, date('Y'));
// Take one month, if the observed day is greater than the current day
if ($day > date('j'))
{
$observed_time = strtotime('-1 month');
}
$this->set_observed_date($observed_time);
$this->set_debug('Observation date is set from the METAR/TAF information (presented in format: ddhhmmZ)');
}
$this->set_result_value('observed_day', $day);
$this->set_result_value('observed_time', $found[2].':'.$found[3].' UTC');
$this->method++;
return TRUE;
}
/**
* Ignore station type if present.
*/
private function get_station_type($part)
{
if ($part != 'AUTO' AND $part != 'COR')
{
return FALSE;
}
$this->method++;
return TRUE;
}
/**
* Decodes wind direction and speed information.
* Format is dddssKT where ddd = degrees from North, ss = speed, KT for knots,
* or dddssGggKT where G stands for gust and gg = gust speed. (ss or gg can be a 3-digit number.)
* KT can be replaced with MPH for meters per second or KMH for kilometers per hour.
*/
private function get_wind($part)
{
if (!preg_match('@^([0-9]{3}|VRB|///)P?([/0-9]{2,3}|//)(GP?([0-9]{2,3}))?(KT|MPS|KPH)@', $part, $found))
{
return FALSE;
}
$this->set_result_value('wind_direction_varies', FALSE, TRUE);
if ($found[1] == '///' AND $found[2] == '//') { } // handle the case where nothing is observed
else
{
$unit = $found[5];
// Speed
$this->set_result_value('wind_speed', $this->convert_speed($found[2], $unit));
// Direction
if ($found[1] == 'VRB')
{
$this->set_result_value('wind_direction_varies', TRUE);
}
else
{
$direction = intval($found[1]);
if ($direction >= 0 AND $direction <= 360)
{
$this->set_result_value('wind_direction', $direction);
$this->set_result_value('wind_direction_label', $this->convert_direction_label($direction));
}
}
// Speed variations (gust speed)
if (isset($found[4]) AND !empty($found[4]))
{
$this->set_result_value('wind_gust_speed', $this->convert_speed($found[4], $unit));
}
}
$this->method++;
return TRUE;
}
/*
* Decodes varies wind direction information if present.
* Format is fffVttt where V stands for varies from fff degrees to ttt degrees.
*/
private function get_varies_wind($part)
{
if (!preg_match('@^([0-9]{3})V([0-9]{3})$@', $part, $found))
{
return FALSE;
}
$min_direction = intval($found[1]);
$max_direction = intval($found[2]);
if ($min_direction >= 0 AND $min_direction <= 360)
{
$this->set_result_value('varies_wind_min', $min_direction);
$this->set_result_value('varies_wind_min_label', $this->convert_direction_label($min_direction));
}
if ($max_direction >= 0 AND $max_direction <= 360)
{
$this->set_result_value('varies_wind_max', $max_direction);
$this->set_result_value('varies_wind_max_label', $this->convert_direction_label($max_direction));
}
$this->method++;
return TRUE;
}
/**
* Decodes visibility information. This function will be called a second time
* if visibility is limited to an integer mile plus a fraction part.
* Format is mmSM for mm = statute miles, or m n/dSM for m = mile and n/d = fraction of a mile,
* or just a 4-digit number nnnn (with leading zeros) for nnnn = meters.
*/
private function get_visibility($part)
{
if (!preg_match('@^(CAVOK|([0-9]{4})|(M)?([0-9]{0,2})?(([1357])/(2|4|8|16))?SM|////)$@', $part, $found))
{
return FALSE;
}
$this->set_result_value('cavok', FALSE, TRUE);
// Cloud and visibilty OK or ICAO visibilty greater than 10 km
if ($found[1] == 'CAVOK' OR $found[1] == '9999')
{
$this->set_result_value('visibility', 10000);
$this->set_result_value('visibility_report', 'Greater than 10 km');
if ($found[1] == 'CAVOK')
{
$this->set_result_value('cavok', TRUE);
$this->method += 4; // can skip the next 4 methods: visibility_min, runway_vr, present_weather, clouds
}
}
elseif ($found[1] == '////') { } // information not available
else
{
$prefix = '';
// ICAO visibility (in meters)
if (isset($found[2]) AND !empty($found[2]))
{
$visibility = intval($found[2]);
}
// US visibility (in miles)
else
{
if (isset($found[3]) AND !empty($found[3]))
{
$prefix = 'Less than ';
}
if (isset($found[7]) AND !empty($found[7]))
{
$visibility = intval($found[4]) + intval($found[6]) / intval($found[7]);
}
else
{
$visibility = intval($found[4]);
}
$visibility = $this->convert_distance($visibility, 'SM'); // convert to meters
}
$unit = ' meters';
if ($visibility <= 1)
{
$unit = ' meter';
}
$this->set_result_value('visibility', $visibility);
$this->set_result_value('visibility_report', $prefix.$visibility.$unit);
}
return TRUE;
}
/**
* Decodes visibility minimum value and direction if present.
* Format is vvvvDD for vvvv = the minimum horizontal visibility in meters
* (if the visibility is better than 10 km, 9999 is used. 9999 means a minimum
* visibility of 50 m or less), and for DD = the approximate direction of minimum and
* maximum visibility is given as one of eight compass points (N, SW, ...).
*/
private function get_visibility_min($part)
{
if (!preg_match('@^([0-9]{4})(NE|NW|SE|SW|N|E|S|W|)?$@', $part, $found))
{
return FALSE;
}
$this->set_result_value('visibility_min', $found[1]);
if (isset($found[2]) AND !empty($found[2]))
{
$this->set_result_value('visibility_min_direction', $found[2]);
}
$this->method++;
return TRUE;
}
/**
* Decodes runway visual range information if present.
* Format is Rrrr/vvvvFT where rrr = runway number, vvvv = visibility,
* and FT = the visibility in feet.
*/
private function get_runway_vr($part)
{
if (!preg_match('@^R([0-9]{2}[LCR]?)/(([PM])?([0-9]{4})V)?([PM])?([0-9]{4})(FT)?/?([UDN]?)$@', $part, $found))
{
return FALSE;
}
if (intval($found[1]) > 36 OR intval($found[1]) < 1)
{
return FALSE;
}
$unit = 'M';
if (isset($found[6]) AND $found[6] == 'FT')
{
$unit = 'FT';
}
$observed = array
(
'runway' => $found[1],
'variable' => NULL,
'variable_prefix' => NULL,
'interval_min' => NULL,
'interval_max' => NULL,
'tendency' => NULL,
'report' => NULL,
);
// Runway past tendency
if (isset($found[8]) AND isset($this->rvr_tendency_codes[$found[8]]))
{
$observed['tendency'] = $found[8];
}
// Runway visual range
if (isset($found[6]))
{
if (!empty($found[4]))
{
$observed['interval_min'] = $this->convert_distance($found[4], $unit);
$observed['interval_max'] = $this->convert_distance($found[6], $unit);
if (!empty($found[5]))
{
$observed['variable_prefix'] = $found[5];
}
}
else
{
$observed['variable'] = $this->convert_distance($found[6], $unit);
}
}
// Runway visual range report
if (!empty($observed['runway']))
{
$report = array();
if ($observed['variable'] !== NULL)
{
$unit = ' meters';
if ($observed['variable'] <= 1)
{
$unit = ' meter';
}
$report[] = $observed['variable'].$unit;
}
elseif (!is_null($observed['interval_min']) AND !is_null($observed['interval_max']))
{
if (isset($this->rvr_prefix_codes[$observed['variable_prefix']]))
{
$report[] = 'varying from a min. of '.$observed['interval_min'].' meters until a max. of '.
$this->rvr_prefix_codes[$observed['variable_prefix']].' that '.
$observed['interval_max'].' meters';
}
else
{
$report[] = 'varying from a min. of '.$observed['interval_min'].' meters until a max. of '.
$observed['interval_max'].' meters';
}
}
if (!is_null($observed['tendency']))
{
if (isset($this->rvr_tendency_codes[$observed['tendency']]))
{
$report[] = 'and '.$this->rvr_tendency_codes[$observed['tendency']];
}
}
$observed['report'] = ucfirst(implode(' ', $report));
}
$this->set_result_group('runways_visual_range', $observed);
return TRUE;
}
/**
* Decodes present weather conditions if present. This function maybe called several times
* to decode all conditions. To learn more about weather condition codes, visit section
* 12.6.8 - Present Weather Group of the Federal Meteorological Handbook No. 1 at
* www.nws.noaa.gov/oso/oso1/oso12/fmh1/fmh1ch12.htm
*/
private function get_present_weather($part)
{
return $this->decode_weather($part, 'present');
}