Jump to content

[SOLVED] Retrieve TAF instead of METAR?


ERuiz

Recommended Posts

Hello guys and gals,

 

I have a small problem. The following script:

 

<?php

function get_metar($station)
{
$fileName = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/$station.TXT";
$metar = '';
$fileData = @file($fileName);  // or die('Data not available');
if ($fileData != false)
    {
	list($i, $date) = each($fileData);
	while (list($i, $line) = each($fileData))
        {
		$metar .= ' ' . trim($line);
	}
	$metar = trim(str_replace('  ', ' ', $metar));
}
return $metar;
}

  $dep_station = "KMIA";
  $dep_metar = get_metar($dep_station);
  echo $dep_metar;
?>

 

fetches the file and displays it. The file's contents is this format:

 

2007/11/01 04:56

TJSJ 010456Z 10007KT 10SM FEW080 26/23 A2991 RMK AO2 SLP126 T02610228

 

Now, how can I change the code above, so that it will fetch a different file, which has the data stored in a different format such as this one:

 

2007/11/01 02:25

TAF

      AMD KMIA 010225Z 010224 05009G20KT P6SM VCSH FEW015 SCT025 BKN045

    FM0300 05011KT P6SM VCSH FEW015 SCT025 BKN045

    FM1400 03016G26KT P6SM VCSH FEW015 SCT025 BKN040

    FM1800 01014G24KT P6SM VCSH FEW015 SCT025 BKN040

 

I changed the URL on the code to point to the new data, but it will not display anything. Here is the code that needs to be fixed:

 

<?php

function get_metar($station)
{
$fileName = "ftp://tgftp.nws.noaa.gov/data/observations/taf/stations/$station.TXT";
$metar = '';
$fileData = @file($fileName);  // or die('Data not available');
if ($fileData != false)
    {
	list($i, $date) = each($fileData);
	while (list($i, $line) = each($fileData))
        {
		$metar .= ' ' . trim($line);
	}
	$metar = trim(str_replace('  ', ' ', $metar));
}
return $metar;
}

  $dep_station = "KMIA";
  $dep_metar = get_metar($dep_station);
  echo $dep_metar;
?>

 

I might be wrong, but I think the code is not properly stripping down the data.

Link to comment
Share on other sites

Well, I THOUGHT I had it fixed!  :D

 

I need help again guys...

 

function get_taf($station)
{
$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$station.TXT";
$taf = '';
$fileData = @file($fileName);
if ($fileData != false)
    {
	list($i, $date) = each($fileData);
	while (list($i, $line) = each($fileData))
        {
		$taf .= ' ' . trim($line);
	}
	$taf = trim(str_replace('  ', ' ', $taf));
} 
return $taf;
}

 

How can I have this script return "NO DATA AVAILABLE" to $taf, if the file is not found on the server?

Link to comment
Share on other sites

function get_taf($station)
{
$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$station.TXT";
$taf = '';
$fileData = @file($fileName);
if ($fileData != false)
{
	list($i, $date) = each($fileData);
	while (list($i, $line) = each($fileData))
	{
		$taf .= ' ' . trim($line);
	}
	$taf = trim(str_replace('  ', ' ', $taf));
} else {
	return 'NO DATA AVAILABLE';
} 
return $taf;
}

 

might work...

 

PhREEEk

Link to comment
Share on other sites

function get_taf($station)
{
$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$station.TXT";
$taf = '';
$fileData = @file($fileName);
if ($fileData != false)
{
	list($i, $date) = each($fileData);
	while (list($i, $line) = each($fileData))
	{
		$taf .= ' ' . trim($line);
	}
	$taf = trim(str_replace('  ', ' ', $taf));
} else {
	return 'NO DATA AVAILABLE';
} 
return $taf;
}

 

might work...

 

PhREEEk

 

Worked perfectly, thanks!

Link to comment
Share on other sites

  • 2 years later...

Hello ,

i am interested and i m search a script in php to import metar data from noaa in mysql

 

thanks

 

 

 

 

 

function get_taf($station)
{
$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$station.TXT";
$taf = '';
$fileData = @file($fileName);
if ($fileData != false)
{
	list($i, $date) = each($fileData);
	while (list($i, $line) = each($fileData))
	{
		$taf .= ' ' . trim($line);
	}
	$taf = trim(str_replace('  ', ' ', $taf));
} else {
	return 'NO DATA AVAILABLE';
} 
return $taf;
}

 

might work...

 

PhREEEk

 

Worked perfectly, thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.