Jump to content

How to select specific non-specific text from a text file?

The Cool n00B

I'm making a website, and I'd like to get text from this file:

http://weather.noaa.gov/pub/data/observations/metar/stations/KBOS.TXT

PHP should be loading the file via fopen and fread, but how do I select things? What I mean is how do I select "28010G16KT" specifically. It's important to know that this could also be "28013KT" or what it is now, it's weather, so it depends. I fear using indexes of spaces because if I was trying to select "A2995", there could also be an "RA" somewhere in there, if it's raining. I hope this makes sense. Here's what I have:

$stationCode = $_GET["station"];
$stationCode = strtoupper($stationCode);

$filePath = "http://weather.noaa.gov/pub/data/observations/metar/stations/" . $stationCode . ".TXT";
$metarFile = fopen($filePath, "r") or die ("Invalid station: Not found in NOAA database!");
$rawMetar = fread($metarFile,filesize($filePath));

 

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

is the part you need always in the same spot in the file? (as in same line and same amount of "words" before it?

 

if thats the case you can just read the line you need, split at the spaces, and take the part you need.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, brcdncn said:

Do you know all the possible values [28010G16KT, 28013KT] and values of elements (e.g., RA) that you are looking for? Be fairly easy with a standard layout...

No because for example 28010G16KT means winds are coming from 280 degrees (west) at 10 knots gusting to 16kt, so as you can imagine, the possibilities are pretty big.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, manikyath said:

is the part you need always in the same spot in the file? (as in same line and same amount of "words" before it?

 

if thats the case you can just read the line you need, split at the spaces, and take the part you need.

I thought of that, but no as somethings are only there if they're observed (for example, rain)

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, The Cool n00B said:

I thought of that, but no as somethings are only there if they're observed (for example, rain)

would be nice to know how they format the file. then its basicly just a matter of un-formatting the file in the same, but oposite way.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, manikyath said:

would be nice to know how they format the file. then its basicly just a matter of un-formatting the file in the same, but oposite way.

Here's a decoding guide:

http://www.met.tamu.edu/class/metar/quick-metar.html

and here's the directory with the various airports and stations:

http://weather.noaa.gov/pub/data/observations/metar/stations/

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, brcdncn said:

Found this:

 

Direction in tens of degrees from true north (first three digits); next two digits: speed in whole knots; if needed, include character as: Gusts (character) followed by maximum observed speed; always appended with KT to indicate knots; 00000KT for calm; if direction varies by 60o or more and speed greater than 6 knots, a Variable wind direction group is reported, otherwise omitted. If wind direction is variable and speed 6 knots or less, replace wind direction with VRB followed by wind speed in knots. Observing and Coding Wind for additional information.

 

Sure, but that's just the winds. In the case of this raw metar:

KBOS 252354Z 28010G16KT 10SM FEW100 BKN120 29/09 A2995 RMK A02 SLP143 T02890089 10311 20200 53009

it pretty much says this, in order:

  • 25th of the month at 2354 Zulu time
  • Winds 280 at 10, gusts 16
  • Visibility 10 statute miles
  • Clouds few at 10000 feet, broken at 12000 feet,
  • Temp is 29/ dew point 9 degrees (C)
  • Altimeter (barometric pressure) 29.95 inches.
  • the rest is just remarks, they don't matter.

EDIT: As you can imagine, all of that can change.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, brcdncn said:

Yeah, I'm going to give it a whirl with a couple files from different stations - if I get to a good place I'll update here, if I suffer defeat I'll try to bring myself to update here.

okay thanks

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, brcdncn said:

Thanks. tbh, it's been a while since I went anywhere near java, and I'm not sure how to use it in a website, but the XML one is coo.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, The Cool n00B said:

I'm making a website, and I'd like to get text from this file:

http://weather.noaa.gov/pub/data/observations/metar/stations/KBOS.TXT

PHP should be loading the file via fopen and fread, but how do I select things? What I mean is how do I select "28010G16KT" specifically. It's important to know that this could also be "28013KT" or what it is now, it's weather, so it depends. I fear using indexes of spaces because if I was trying to select "A2995", there could also be an "RA" somewhere in there, if it's raining. I hope this makes sense. Here's what I have:


$stationCode = $_GET["station"];
$stationCode = strtoupper($stationCode);

$filePath = "http://weather.noaa.gov/pub/data/observations/metar/stations/" . $stationCode . ".TXT";
$metarFile = fopen($filePath, "r") or die ("Invalid station: Not found in NOAA database!");
$rawMetar = fread($metarFile,filesize($filePath));

 

I dont know if anyone mentioned it since I didnt read all the posts, but have you tried Regex? (google it) . If the string fits a pattern then regex will work fine

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Toxicable said:

I dont know if anyone mentioned it since I didnt read all the posts, but have you tried Regex? (google it) . If the string fits a pattern then regex will work fine

I've found PHP Weather on sourceforge (partial thanks to @brcdncn), and it puts the weather in a 3D array with the unit conversions and all that, so I'm pretty sure I'm good. It actually explodes the METAR into an array (based on spaces) and organizes it into an array based on patterns, so it's pretty much what you're talking about.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Now I just gotta make this $hit look fresh as hell.

 

 

metars thing.png

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Since the order is always going to be the same just use some basic string parsing to get what you want. I'm not sure what this would look like in PHP but in Python you'd do:

text = "KBOS 280454Z 21009KT 10SM CLR 20/16 A3004 RMK AO2 SLP171 T02000156 40228014"
text_split = text.split()
	 
date = text_split[1]
winds = text_split[2]
visibility = text_split[3]
clouds = text_split[4]
temp = text_split[5]
alimeter = text_split[6]

 

"Unix was not designed to stop you from doing stupid things, because that would also stop you from doing clever things." - Doug Gwyn

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, AkaCrabby said:

Since the order is always going to be the same just use some basic string parsing to get what you want.

 

The data has optional fields, what he's trying to get isn't always in the same place so that won't work.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×