0

I am trying to create a simple PHP script which will look at a METAR report and get the first cloud cover condition whenever the PHP script is launched. What I need is a way to search for the first word out of these:

BKN, CAVOK, CLR, FEW, NSC, OVC, SCT, SKC, VV

Sometimes the METAR report will have several cloud conditions with the first being that closest to the ground. I need just the first match to become a variable.

Say this is the current METAR report:

2013/12/28 21:51 KORD 282151Z 21012G21KT 10SM FEW250 NSC900 09/M01 A2992 RMK AO2 SLP136 T00941006

So both FEW and NSC are in that METAR report. I need the variable to be something like $metar = "FEW"; for the variable and nothing else. The other problem is doing it as an array may not work as the codes I need may be at $metars[7] and later could be at $metars[5] or elsewhere in the array.

This is the code I have so far, but I cannot figure out how to do what I need it to do:

<?php
$station = "KORD";// Enter your airport's code here.
$metarwx = "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT";
$metars = file_get_contents($metarwx);
print_r(explode(" ", $metars));
?>

Thanks in advance for any help!

user3142825
  • 37
  • 1
  • 7
  • Can you be more clear in "`The other problem is doing it as an array may not work as the codes I need may be at $metars[7] and later could be at $metars[5] or elsewhere in the array.`", please? – Francisco Presencia Dec 28 '13 at 22:45
  • Just as it says. If I make it an array, sometimes the word I am looking for may be a different number in the array. FEW may be $metars[7] but later might be moved over to $metars[5] when the METAR report is updated by the airport. – user3142825 Dec 28 '13 at 23:00

3 Answers3

0

A short one:

<?php
$station = "KORD";// Enter your airport's code here.
$metarwx = "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT";
$report = file_get_contents($metarwx);

// Get the array you want
$words_to_find = explode(" ", "BKN CAVOK CLR FEW NSC OVC SCT SKC VV");

// Assign each of the words to $metar
foreach ($words_to_find as $word)
  // If found (note that this is not the same as if (!strpos()))
  if (strpos($report, $word) !== false)
    {
    // Assign the $metar variable to the occurrence
    $metar = $word;
    // Exit the foreach loop (so only the first occurrence is considered)
    break;
    }

After that script, $metar will have the option you want or null if nothing was found.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
0

You need to use a combination of explode and preg_replace. A function like this should solve your problem:-

$station = "KORD";// Enter your airport's code here.
$metarwx = "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT";
$metars = file_get_contents($metarwx);
$metar = getCloud($metars);
echo $metar;

function getCloud($wxReport)
{
    $clouds = array('BKN', 'CAVOK', 'CLR', 'FEW', 'NSC', 'OVC', 'SCT', 'SKC', 'VV');
    $wxReportArray = explode(' ', $wxReport);

    foreach($wxReportArray as $rptPart){
        $rptPart = preg_replace('/[0-9]+/', '', $rptPart);
        if(in_array($rptPart,  $clouds)){
            return $rptPart;
        }
    }
}

I haven't tested it for all possible scenarios, but at the time of posting it output 'FEW' on my system.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • Parse error: syntax error, unexpected '[' in /home/content/A/b/c/Abc/html/metar.php on line 9 – user3142825 Dec 28 '13 at 22:52
  • @user3142825 Sorry, that's because of your PHP version. I've edited to work with older versions now. – vascowhite Dec 28 '13 at 22:59
  • Now this is pretty cool! Thanks! I will test it with other METAR reports to verify it is reporting the first cloud condition in the METAR. If I don't reply back then it is great! – user3142825 Dec 28 '13 at 23:07
  • I hope it works for you, let me know if you need further help. If an answer has worked for you click the grey tick to the left of it to turn it green. That's how we thank each other on SO. – vascowhite Dec 28 '13 at 23:13
  • I am giving Digital Chris's answer a try. No offense, but his seems to be simpler. I wish I could select more than one answer. – user3142825 Dec 28 '13 at 23:40
  • No problem, the important thing is you have a solution. – vascowhite Dec 28 '13 at 23:48
0

The way to do this is with array_intersect().

$exampledata = "2013/12/28 21:51 KORD 282151Z 21012G21KT 10SM FEW250 NSC900 09/M01 A2992 RMK AO2 SLP136 T00941006";
// make an array of clouds
$clouds = explode(", ","BKN, CAVOK, CLR, FEW, NSC, OVC, SCT, SKC, VV");

// get a clean  array from $exampledata
$cleandata = explode(" ", preg_replace('/[0-9]+/', '', $exampledata) );

// get the elements that match in both arrays, ordered by the order found in $clouds
$results = array_intersect($cleandata, $clouds);

// return the first result only
$firstresult = array_shift(array_values($results));

Here it is working: https://eval.in/84010

Digital Chris
  • 6,177
  • 1
  • 20
  • 29
  • 1
    This works as well as vascowhite's example. Thank you! – user3142825 Dec 28 '13 at 23:35
  • 2013/12/24 07:30 NZOH 240730Z 31011G21KT 280V340 5000 -SHRA FEW020 BKN048 19/14 Q1006 BECMG 20KM TEMPO 6000 RA BKN012 SPECI COR NZOH 240730Z 31011G21KT 280V340 5000 -SHRA FEW020 BKN048 19/14 Q1006 BECMG 20KM TEMPO 6000 RA BKN012 I came across this METAR and the result came back as BKN instead of FEW. I am not sure why that happens though. – user3142825 Dec 29 '13 at 04:23
  • Ah ok, in that case the arrays in the array_intersect() were backwards. See updated answer with updated link. – Digital Chris Dec 29 '13 at 19:58