pmwf (Poor Man's Weather Forecast) - current conditions + 3 days forecast

hakerdefo



(07 May 2017) Update  Begins:

Removed the dependencies on Perl and cURL.
Cleaned-up the script a bit.

Don't forget to read the set-up instructions posted down below if you are a new user  ;)

Here is updated pmwf script


#!/usr/bin/env bash
###################
local_latitude=
local_longitude=
local_unit=
api_key=
local_rm=$(which rm)
printf "\033c"
wget --user-agent="Mozilla/5.0 Gecko/20100101" --quiet --timeout=30 "http://api.openweathermap.org/data/2.5/weather?lat=$local_latitude&lon=$local_longitude&units=$local_unit&mode=json&APPID=$api_key" --output-document "/tmp/current.json"
wget --user-agent="Mozilla/5.0 Gecko/20100101" --quiet --timeout=30 "http://api.openweathermap.org/data/2.5/forecast/daily?lat=$local_latitude&lon=$local_longitude&cnt=4&units=$local_unit&mode=json&APPID=$api_key" --output-document "/tmp/forecast.json"
printf "\033c"
if [[ ! -f "/tmp/current.json" ]]; then
    echo ""
    echo -e "Can not connect to openweathermap.org!"
    echo -e "Please check your internet connection!"
    echo ""
    exit 1
else
    place=$(jq -r '.city.name' "/tmp/forecast.json")
    nation=$(jq -r '.city.country' "/tmp/forecast.json")
    time2=$(jq -r '.list[1].dt' "/tmp/forecast.json")
    time3=$(jq -r '.list[2].dt' "/tmp/forecast.json")
    time4=$(jq -r '.list[3].dt' "/tmp/forecast.json")
    time22=$(date -d @"$time2" +'%A, %d %B %Y')
    time33=$(date -d @"$time3" +'%A, %d %B %Y')
    time44=$(date -d @"$time4" +'%A, %d %B %Y')
    tempcurrent=$(jq -r '.main.temp' "/tmp/current.json")
    min2=$(jq -r '.list[1].temp.min' "/tmp/forecast.json")
    min3=$(jq -r '.list[2].temp.min' "/tmp/forecast.json")
    min4=$(jq -r '.list[3].temp.min' "/tmp/forecast.json")
    max2=$(jq -r '.list[1].temp.max' "/tmp/forecast.json")
    max3=$(jq -r '.list[2].temp.max' "/tmp/forecast.json")
    max4=$(jq -r '.list[3].temp.max' "/tmp/forecast.json")
    pressurecurrent=$(jq -r '.main.pressure' "/tmp/current.json")
    pressure2=$(jq -r '.list[1].pressure' "/tmp/forecast.json")
    pressure3=$(jq -r '.list[2].pressure' "/tmp/forecast.json")
    pressure4=$(jq -r '.list[3].pressure' "/tmp/forecast.json")
    humidcurrent=$(jq -r '.main.humidity' "/tmp/current.json")
    humid2=$(jq -r '.list[1].humidity' "/tmp/forecast.json")
    humid3=$(jq -r '.list[2].humidity' "/tmp/forecast.json")
    humid4=$(jq -r '.list[3].humidity' "/tmp/forecast.json")
    windcurrent=$(jq -r '.wind.speed' "/tmp/current.json")
    wind2=$(jq -r '.list[1].speed' "/tmp/forecast.json")
    wind3=$(jq -r '.list[2].speed' "/tmp/forecast.json")
    wind4=$(jq -r '.list[3].speed' "/tmp/forecast.json")
    direcurrent=$(jq -r '.wind.deg' "/tmp/current.json")
    dire2=$(jq -r '.list[1].deg' "/tmp/forecast.json")
    dire3=$(jq -r '.list[2].deg' "/tmp/forecast.json")
    dire4=$(jq -r '.list[3].deg' "/tmp/forecast.json")
    condicurrent=$(jq -r '.weather[].description' "/tmp/current.json")
    condi2=$(jq -r '.list[1].weather[].description' "/tmp/forecast.json")
    condi3=$(jq -r '.list[2].weather[].description' "/tmp/forecast.json")
    condi4=$(jq -r '.list[3].weather[].description' "/tmp/forecast.json")
    if [[ "$local_unit" == imperial ]]; then
windcurrent1="$windcurrent"
wind22="$wind2"
wind33="$wind3"
wind44="$wind4"
symb=$(echo -e "\u2109")
symb2="mph"
    else
windcurrent1=$(awk "BEGIN {print $windcurrent * 3.6}")
wind22=$(awk "BEGIN {print $wind2 * 3.6}")
wind33=$(awk "BEGIN {print $wind3 * 3.6}")
wind44=$(awk "BEGIN {print $wind4 * 3.6}")
symb=$(echo -e "\u2103")
symb2="kph"
    fi
    printf "\033c"
    echo -e '\e[01m'""
    echo -e "Location - $place, $nation:"
    echo ""
    echo -e "Current weather conditions:"
    echo -e '\e[0m'""
    echo -e "Temperature - $tempcurrent $symb"
    echo -e "Atmospheric Pressure - $pressurecurrent hPa"
    echo -e "Humidity - $humidcurrent %"
    echo -e "Wind speed - $windcurrent1 $symb2"
    echo -e "Wind direction - $direcurrent°"
    echo -e "Weather condition - $condicurrent"
    echo -e '\e[01m'""
    echo -e "Forecast for $time22:"
    echo -e '\e[0m'""
    echo -e "Minimum Temperature - $min2 $symb"
    echo -e "Maximum Temperature - $max2 $symb"
    echo -e "Atmospheric Pressure - $pressure2 hPa"
    echo -e "Humidity - $humid2 %"
    echo -e "Wind speed - $wind22 $symb2"
    echo -e "Wind direction - $dire2°"
    echo -e "Weather condition - $condi2"
    echo -e '\e[01m'""
    echo -e "Forecast for $time33:"
    echo -e '\e[0m'""
    echo -e "Minimum Temperature - $min3 $symb"
    echo -e "Maximum Temperature - $max3 $symb"
    echo -e "Atmospheric Pressure - $pressure3 hPa"
    echo -e "Humidity - $humid3 %"
    echo -e "Wind speed - $wind33 $symb2"
    echo -e "Wind direction - $dire3°"
    echo -e "Weather condition - $condi3"
    echo -e '\e[01m'""
    echo -e "Forecast for $time44:"
    echo -e '\e[0m'""
    echo -e "Minimum Temperature - $min4 $symb"
    echo -e "Maximum Temperature - $max4 $symb"
    echo -e "Atmospheric Pressure - $pressure4 hPa"
    echo -e "Humidity - $humid4 %"
    echo -e "Wind speed - $wind44 $symb2"
    echo -e "Wind direction - $dire4°"
    echo -e "Weather condition - $condi4"
    echo -e ""
    "$local_rm" -f "/tmp/forecast.json"
    "$local_rm" -f "/tmp/current.json"
fi
exit 0
###################



Cheers!!!


(07 May 2017) Update Ends:






(19 November 2015) Update  Begins:

Starting from 9 October 2015 OpenweatherMap API requires a valid APPID for access. So you'll need to register at openWeatherMap and get your API key. It's simple and free plan is good enough for most cases. I've updated the script and instructions to reflect these changes. you can get the latest version of pmwf and read updated install instructions on GitHub page here,

pmwf on GitHub

I want to thank PackRat and ozitraveller for bringing this to attention. You guys rock  8)

(19 November 2015) Update Ends:



As per suggestion by VastOne I've modified the original script to display current weather conditions and a 3 days weather forecast.


#!/bin/bash

clear

latit=
longi=
uneet=

curl -s --connect-timeout 30 -o /tmp/current.json "http://api.openweathermap.org/data/2.5/weather?lat=$latit&lon=$longi&units=$uneet&mode=json"
curl -s --connect-timeout 30 -o /tmp/forecast.json "http://api.openweathermap.org/data/2.5/forecast/daily?lat=$latit&lon=$longi&cnt=4&units=$uneet&mode=json"

if [ ! -f /tmp/forecast.json ]; then
echo ""
echo -e "Unable to connect to openweathermap.org!"
echo -e "Check your internet connection!"
echo ""

exit 1

else
place=$(cat /tmp/forecast.json | jq -r '.city.name')
nation=$(cat /tmp/forecast.json | jq -r '.city.country')
time2=$(cat /tmp/forecast.json | jq -r '.list[1].dt')
time3=$(cat /tmp/forecast.json | jq -r '.list[2].dt')
time4=$(cat /tmp/forecast.json | jq -r '.list[3].dt')
time22=$(date -d @"$time2" +'%A, %d %B %Y')
time33=$(date -d @"$time3" +'%A, %d %B %Y')
time44=$(date -d @"$time4" +'%A, %d %B %Y')
tempcurrent=$(cat /tmp/current.json | jq -r '.main.temp')
min2=$(cat /tmp/forecast.json | jq -r '.list[1].temp.min')
min3=$(cat /tmp/forecast.json | jq -r '.list[2].temp.min')
min4=$(cat /tmp/forecast.json | jq -r '.list[3].temp.min')
max2=$(cat /tmp/forecast.json | jq -r '.list[1].temp.max')
max3=$(cat /tmp/forecast.json | jq -r '.list[2].temp.max')
max4=$(cat /tmp/forecast.json | jq -r '.list[3].temp.max')
pressurecurrent=$(cat /tmp/current.json | jq -r '.main.pressure')
pressure2=$(cat /tmp/forecast.json | jq -r '.list[1].pressure')
pressure3=$(cat /tmp/forecast.json | jq -r '.list[2].pressure')
pressure4=$(cat /tmp/forecast.json | jq -r '.list[3].pressure')
humidcurrent=$(cat /tmp/current.json | jq -r '.main.humidity')
humid2=$(cat /tmp/forecast.json | jq -r '.list[1].humidity')
humid3=$(cat /tmp/forecast.json | jq -r '.list[2].humidity')
humid4=$(cat /tmp/forecast.json | jq -r '.list[3].humidity')
windcurrent=$(cat /tmp/current.json | jq -r '.wind.speed')
wind2=$(cat /tmp/forecast.json | jq -r '.list[1].speed')
wind3=$(cat /tmp/forecast.json | jq -r '.list[2].speed')
wind4=$(cat /tmp/forecast.json | jq -r '.list[3].speed')
direcurrent=$(cat /tmp/current.json | jq -r '.wind.deg')
dire2=$(cat /tmp/forecast.json | jq -r '.list[1].deg')
dire3=$(cat /tmp/forecast.json | jq -r '.list[2].deg')
dire4=$(cat /tmp/forecast.json | jq -r '.list[3].deg')
condicurrent=$(cat /tmp/current.json | jq -r '.weather[].description')
condi2=$(cat /tmp/forecast.json | jq -r '.list[1].weather[].description')
condi3=$(cat /tmp/forecast.json | jq -r '.list[2].weather[].description')
condi4=$(cat /tmp/forecast.json | jq -r '.list[3].weather[].description')

if [ "$uneet" == imperial ]
then
windcurrent1="$windcurrent"
wind22="$wind2"
wind33="$wind3"
wind44="$wind4"
symb=°F
symb2=mph
else
windcurrent1=$(perl -le 'print $ARGV[0] * 3.6' "$windcurrent")
wind22=$(perl -le 'print $ARGV[0] * 3.6' "$wind2")
wind33=$(perl -le 'print $ARGV[0] * 3.6' "$wind3")
wind44=$(perl -le 'print $ARGV[0] * 3.6' "$wind4")
symb=°C
symb2=kph
fi

echo ""
echo -e "Location - $place, $nation:"
echo ""
echo -e "Current weather conditions:"
echo ""
echo -e "Temperature - $tempcurrent $symb"
echo -e "Atmospheric Pressure - $pressurecurrent hPa"
echo -e "Humidity - $humidcurrent %"
echo -e "Wind speed - $windcurrent1 $symb2"
echo -e "Wind direction - $direcurrent°"
echo -e "Weather condition - $condicurrent"
echo ""
echo ""
echo -e "Forecast for $time22:"
echo ""
echo -e "Minimum Temperature - $min2 $symb"
echo -e "Maximum Temperature - $max2 $symb"
echo -e "Atmospheric Pressure - $pressure2 hPa"
echo -e "Humidity - $humid2 %"
echo -e "Wind speed - $wind22 $symb2"
echo -e "Wind direction - $dire2°"
echo -e "Weather condition - $condi2"
echo ""
echo ""
echo -e "Forecast for $time33:"
echo ""
echo -e "Minimum Temperature - $min3 $symb"
echo -e "Maximum Temperature - $max3 $symb"
echo -e "Atmospheric Pressure - $pressure3 hPa"
echo -e "Humidity - $humid3 %"
echo -e "Wind speed - $wind33 $symb2"
echo -e "Wind direction - $dire3°"
echo -e "Weather condition - $condi3"
echo ""
echo ""
echo -e "Forecast for $time44:"
echo ""
echo -e "Minimum Temperature - $min4 $symb"
echo -e "Maximum Temperature - $max4 $symb"
echo -e "Atmospheric Pressure - $pressure4 hPa"
echo -e "Humidity - $humid4 %"
echo -e "Wind speed - $wind44 $symb2"
echo -e "Wind direction - $dire4°"
echo -e "Weather condition - $condi4"
echo ""

rm /tmp/forecast.json
rm /tmp/current.json

fi

exit 0



For this script to work you'll need to install one little tool first. Don't worry, it's easy. Just do this in your terminal,


sudo apt-get install jq


Next go to following website and search for the latitude-longitude of your place,

Tageo WorldWide Index

Open the script in your favorite text editor. Right at the beginning of the file you will find three empty variables 'latit', 'longi', and 'uneet'. You need to assign latitude of your location to the variable 'latit', longitude of your location to the variable 'longi' and your preferred units system to the variable 'uneet'. There are two main units systems in use. metric and imperial. Which one should you use? If you prefer to measure temperature in Celsius (Centigrade) & distance in Kilometres (km), use metric. And if you prefer to measure temperature in Fahrenheit & distance in Miles (mi), use imperial.

For example for a VSIDO user in London, UK it would look like this,


latit=51.500
longi=-0.117
uneet=metric


And for someone in New York, US it would look like,


latit=40.714
longi=-74.006
uneet=imperial


Save the script after making the necessary changes with the name 'pmwf' somewhere in your $PATH and 'chmod 755' it.
Whenever you wish to see some weather info for your place simply open terminal, type pmwf, hit Enter and you'll have current weather conditions with a 3 day weather forecast right in terminal.

Cheers!!!
You Can't Always Git What You Want

VastOne

This is a fantastic script hakerdefo... It will allow me to eliminate a nasty but effective plugin from Iceweasel

I appreciate your dedication and work on this, your skills are remarkable

I will also add this to VSIDO and reference this post for setup help

Thank you!
VSIDO      VSIDO Change Blog    

    I dev VSIDO

PackRat

I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo


PackRat

Script is broken on my system -

parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
date: invalid date '@'
date: invalid date '@'
date: invalid date '@'
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8
parse error: Invalid numeric literal at line 1, column 8

Location - , :

Current weather conditions:

Temperature -  °C
Atmospheric Pressure -  hPa
Humidity -  %
Wind speed - 0 kph
Wind direction - °
Weather condition -


Forecast for :

Minimum Temperature -  °C
Maximum Temperature -  °C
Atmospheric Pressure -  hPa
Humidity -  %
Wind speed - 0 kph
Wind direction - °
Weather condition -

<snip>


anyone else having this issue?
I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo

VastOne

VSIDO      VSIDO Change Blog    

    I dev VSIDO

ozitraveller

http://api.openweathermap.org/data/2.5/weather?lat=$latit&lon=$longi&units=$uneet&mode=json
http://api.openweathermap.org/data/2.5/weather?lat=51.500&lon=-0.117&units=metric&mode=json
returns : Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.

http://api.openweathermap.org/data/2.5/forecast/daily?lat=$latit&lon=$longi&cnt=4&units=$uneet&mode=json
http://api.openweathermap.org/data/2.5/forecast/daily?lat=51.500&lon=-0.117&cnt=4&units=metric&mode=json
returns: Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.

Q: API calls return an error 401
A: Starting from 9 October 2015 our API requires a valid APPID for access. Note that this does not mean that our API is subscription-only now - please take a minute to register a free account to receive a key.

We are sorry for inconvenience but this is a necessary measure that will help us deliver our services to you faster and more reliably.

For FOSS developers: we welcome free and open source software and are willing to help you. If you want to use OWM data in your free software application please register an API key and file a ticket describing your application and API key registered. OWM will review your request lift access limits for your key if used in open source application.

Hope that helps

Ozi


A bit more...

How to use API key in API call

Description:

To get access to weather API you need an API key whatever account you chose from Free to Enterprise.

We keep right to not to process API requests without API key.

API call:

http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID={APIKEY}
Parameters:

APPID {APIKEY} is your unique API key
Example of API call:

api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111

Snap


PackRat

I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo

hakerdefo

Thanks a metric ton PackRat and ozitraveller  8)
I've belatedly updated the things. Sorry for the delay  :(

http://vsido.org/index.php?topic=1035.msg11681#msg11681

Cheers!!!
You Can't Always Git What You Want

PackRat

Script works as advertised.

After signing up, be sure to look at the My Services page of your account to see your limitations (calls per day etc  ....) - look to be pretty generous for just checking the weather; but running a conky on your desktop all day may cause some issues.
I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo

VastOne

Got the API key ...

Not understanding this from the instructions:

Assign this key to 'apkey' variable in the file

Are we to add/create a variable apkey and then assign the value? What is the context?
VSIDO      VSIDO Change Blog    

    I dev VSIDO

ozitraveller

Cool hakerdefo!  :) No worries.

Your change looks as I expected. :)


#!/bin/bash

clear

latit=
longi=
uneet=
apkey=

curl -s --connect-timeout 30 -o /tmp/current.json "http://api.openweathermap.org/data/2.5/weather?lat=$latit&lon=$longi&units=$uneet&mode=json&APPID=$apkey"
curl -s --connect-timeout 30 -o /tmp/forecast.json "http://api.openweathermap.org/data/2.5/forecast/daily?lat=$latit&lon=$longi&cnt=4&units=$uneet&mode=json&APPID=$apkey"

...

hakerdefo

Quote from: VastOne on November 19, 2015, 10:15:17 PM
Got the API key ...

Not understanding this from the instructions:

Assign this key to 'apkey' variable in the file

Are we to add/create a variable apkey and then assign the value? What is the context?
VastOne, I've updated the script and there is this empty 'apkey' variable in this version of the script right at the top. Just add your API key to it.
Cheers!!!
You Can't Always Git What You Want

hakerdefo

Quote from: ozitraveller on November 19, 2015, 11:16:39 PM
Cool hakerdefo!  :) No worries.

Your change looks as I expected. :)


#!/bin/bash

clear

latit=
longi=
uneet=
apkey=

curl -s --connect-timeout 30 -o /tmp/current.json "http://api.openweathermap.org/data/2.5/weather?lat=$latit&lon=$longi&units=$uneet&mode=json&APPID=$apkey"
curl -s --connect-timeout 30 -o /tmp/forecast.json "http://api.openweathermap.org/data/2.5/forecast/daily?lat=$latit&lon=$longi&cnt=4&units=$uneet&mode=json&APPID=$apkey"

...

Cheers mate!!!
You Can't Always Git What You Want