You are not logged in.

#1 2012-08-16 05:00:30

xcabal
Member
Registered: 2012-07-26
Posts: 33

conky and noaa weather from xml

i am looking into trying to retrieve weather data from noaa xml file for locations then displaying it in conky. i am having trouble just extracting the data i want without downloading the xml file to the computer let alone getting it to appear in conky.
i have tried wget | grep but all that happens is the file gets downloaded and grep part does nothing.
can someone point me in the right direction  that may help me

thanks.

Offline

#2 2012-08-16 06:46:37

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 216

Re: conky and noaa weather from xml

You could try using xmllint which is part of the libxml2 package.

Replace the ????.xml with your preferred observation station.

wget -q -O - http://w1.weather.gov/xml/current_obs/????.xml | xmllint -xpath '//temp_f' - | grep -o [0-9.]*

Which would give you, for instance, the current temperature in degrees fahrenheit.

Edit: the above will only work for temperatures that are above zero.  You should probably do something like:

grep -E -o -- '-?[0-9.]*'

or perhaps better to use sed

sed 's/<[^>]*>//g'

Last edited by rockin turtle (2012-08-16 07:35:10)

Offline

#3 2012-08-16 14:25:27

xcabal
Member
Registered: 2012-07-26
Posts: 33

Re: conky and noaa weather from xml

thanks, ill look at that
i was wondering if you know how to do the samething with http://forecast.weather.gov/MapClick.ph … lon=-73.91 xml  that appears above the map for example, all i get when running it in the terminal is the raw html and grep doesnt work.


if you dont mind also referring me to where you learn all this.

Offline

#4 2012-08-17 00:49:33

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 216

Re: conky and noaa weather from xml

I really don't know where I learned all this, mostly just google over time.

I believe that the weather station you are using is from La Guardia airport which is KLGA.

You can perhaps find a better station here.

Below is a script you may modify to suit your needs.

A little information about the script.  It uses wget to download the file into in /tmp/weather. On Arch Linux, /tmp is not actually on disk, but is in shared memory.  Thus the .xml file will be downloaded into shared memory.

Since it appears that the weather data is updated once per hour (as near as I can tell), the script will not try to retrieve new data until at least one hour and 15 minutes after the last observed time.  (I chose 1:15 since at La Guardia at least, there is about a 11 minute delay between when they make a weather observation and put the data online.) If the new data is still old (which could happen if the weather station is having problems or maintenence, etc.) then the script will keep trying to get new data roughly once every five minutes until the station comes back on line.

The script takes the weather station (KLGA for instance) as a command line parameter so you could easily use a different station.

#!/bin/bash

station="$1.xml"
wdir='/tmp/weather'

update_xml() {
	if [ ! -e "$station" ]; then
		wget -q http://w1.weather.gov/xml/current_obs/${station}
		[ -e "$station" ] && touch "${station}"
	else
		# dtime: time the .xml file was downloaded
		# otime: time the weather data was observed
		# ctime: current time (time this script is being run)
		dtime=$(stat -c %Y $station)
		otime=$(date -d "$utime" +%s)
		ctime=$(date +%s)

		if (( "$otime" + 4507 < "$ctime" )); then
			if (( "$dtime" + 307 < "$ctime" )); then
				wget -q -O "$station" http://w1.weather.gov/xml/current_obs/${station}
				[ -e "$station" ] && touch "${station}"
			fi
		fi
	fi
}

from_xml() { xmllint -xpath "//$1" - <<< "$xml" | sed 's/<[^>]*>//g'; }
	
[ -d "$wdir" ] || mkdir -p "$wdir"
cd "$wdir" || exit 1

xml=''
[ -r $station ] && xml="$(< $station)"
( update_xml >/dev/null 2>&1 ) &

if [ -n "$xml" ]; then
	location=$(from_xml "location")
	utime=$(from_xml "observation_time_rfc822")
	weather=$(from_xml "weather")
	temperature=$(from_xml "temp_f")
	humid=$(from_xml "relative_humidity")
	wind_dir=$(from_xml "wind_dir")
	case "$wind_dir" in
		"North") wind_dir="N" ;;
		"South") wind_dir="S" ;;
		"East") wind_dir="E" ;;
		"West") wind_dir="W" ;;
		"Northwest") wind_dir="NW" ;;
		"Northeast") wind_dir="NE" ;;
		"Southwest") wind_dir="SW" ;;
		"Southeast") wind_dir="SE" ;;
	esac
	wind_speed=$(from_xml "wind_kt")
	baro_pressure=$(from_xml "pressure_in")

	echo "$location"
	echo "Updated: $(date -d "$utime" 2>/dev/null )"
	printf 'Weather: %s %s°F\n' "$weather" "$temperature"
	printf 'Barometric Pressure: %s inches\n' "$baro_pressure"
	printf 'Wind: %s at %s knots\n' "$wind_dir" "$wind_speed"
	printf 'Humidity: %s%%\n' "$humid"
else
	echo "No weather data available."
fi

Then in your .conkyrc do (don't forget the KLGA):

${exec /path/to/script KLGA}

Finally, I see that there are also a set of thumbnail images that NOAA provides that coorespond to the current weather conditions. As conky is capable of displaying these thumbnails, you could set up conky to do that as well.  It is a little more involved, so let me know if you are interested.

Offline

#5 2012-08-18 01:48:41

xcabal
Member
Registered: 2012-07-26
Posts: 33

Re: conky and noaa weather from xml

thank you for the script
I do have a few questions so i understand whats going on
what type of script language is this?  so i can learn what it is
is the script is get the station from the command execution of conky or do i need to add it to the script?
i didn't know conky could display images it would be nice to have the image in conky

thank you for spending the time doing this and helping me

Offline

#6 2012-08-18 02:29:46

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 216

Re: conky and noaa weather from xml

This is what is known as a bash shell script. It is very common in Unix/Linux.

http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29
http://www.gnu.org/software/bash/manual/bashref.html
http://tldp.org/LDP/Bash-Beginners-Guide/html/

Save the script in a file, then in your .conkyrc file put:

${exec /path/to/script KLGA}

Offline

#7 2012-08-26 14:59:55

Paramvir
Member
Registered: 2009-08-06
Posts: 126
Website

Re: conky and noaa weather from xml

Hi

I have written a program which extracts data from weather underground web site - see the wiki at https://bitbucket.org/plikhari/conkywx_pub/wiki/Home

Is there anything specific you are looking for - perhaps I can help.

I chose this site as the data is virtually real time while noaa and others update data much slower.

The thread is mentioned in my signature wink

Offline

#8 2012-08-26 15:47:44

thisoldman
Member
From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: conky and noaa weather from xml

NOAA has text formats for the current conditions and the forecast.  Why parse XML when text is easier?  Here's a simple script to display both in the console (for Pittsburgh).  The original source was https://bbs.archlinux.org/viewtopic.php … 30#p963030, thanks to woddfellow2.

#! /bin/bash
echo "********** CURRENT CONDITIONS **********"
wget -q -O- http://weather.noaa.gov/pub/data/observations/metar/decoded/KPIT.TXT
echo
echo "********** NWS ZONE FORECAST **********"
wget -q -O- http://weather.noaa.gov/pub/data/forecasts/zone/pa/paz021.txt

If you want to save the file to parse for conky, change the wget command to:

wget -q -O output_file.txt http://weather.noaa.gov/pub/data/observations/metar/decoded/KPIT.TXT

The accompanying script to display the weather map using feh is also nice.

#!/bin/bash
feh http://radar.weather.gov/lite/N0R/PBZ_0.png

Offline

Board footer

Powered by FluxBB