Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A year in colors, -25 to 24 degrees over a range of colors from blue to red.
import requests
from subprocess import Popen
import sys
'''
A year in colors, -25 to 24 degrees over a range of colors from blue to red.
Color palette from:
- https://www.color-hex.com/color-palette/33335
- https://meyerweb.com/eric/tools/color-blend/#194BFF:0022C9:10:hex
Data from: https://frost.met.no/
It works on linux with Imagemagick installed
- Fedora: sudo dnf install imagemagick
- Ubuntu: sudo apt install imagemagick
Run the script like this:
$ python a_year_in_colors.py STATION YEAR FILENAME
'''
color_table = {
24: ("#F00A0A"),
23: ("#F00A0A"),
22: ("#F00A0A"),
21: ("#F00A0A"),
20: ("#F00A0A"),
19: ("#EA0909"),
18: ("#E50808"),
17: ("#DF0707"),
16: ("#D90606"),
15: ("#D40505"),
14: ("#CE0505"),
13: ("#C90404"),
12: ("#C30303"),
11: ("#BD0202"),
10: ("#B80101"),
9: ("#B20000"),
8: ("#AA080B"),
7: ("#A31017"),
6: ("#9B1722"),
5: ("#931F2D"),
4: ("#8B2738"),
3: ("#842F44"),
2: ("#7C374F"),
1: ("#743F5A"),
0: ("#6C4665"),
-1: ("#654E71"),
-2: ("#5D567C"),
-3: ("#575588"),
-4: ("#515494"),
-5: ("#4A53A0"),
-6: ("#4452AC"),
-7: ("#3E51B8"),
-8: ("#3850C3"),
-9: ("#324FCF"),
-10: ("#2C4EDB"),
-11: ("#254DE7"),
-12: ("#1F4CF3"),
-13: ("#194BFF"),
-14: ("#1747FA"),
-15: ("#1444F5"),
-16: ("#1240F0"),
-17: ("#103CEB"),
-18: ("#0E38E6"),
-19: ("#0B35E2"),
-20: ("#0931DD"),
-21: ("#072DD8"),
-22: ("#0529D3"),
-23: ("#0226CE"),
-24: ("#0022C9"),
-25: ("#0022C9")
}
# Needs a better parameter control
station = sys.argv[1] # 'SN90495' = Tromsø og 'SN87110' = #Andenes
year = sys.argv[2]
output_filename = sys.argv[3]
# Define url and parameters
# You need a client id from frost.met.no
client_id = 'ID GOES HERE'
url = 'https://frost.met.no/observations/v0.jsonld'
parameters = {
'sources': station,
'elements': 'mean(air_temperature P1D)',
'referencetime': f'{year}-01-01/{year}-12-31',
}
# Issue an HTTP GET request and Extract JSON data
r = requests.get(url, parameters, auth=(client_id,''))
json = r.json()
# Check if the request worked, print out any errors
if r.status_code == 200:
data = json['data']
print('Data retrieved from frost.met.no!')
else:
print('Error! Returned status code %s' % r.status_code)
print('Message: %s' % json['error']['message'])
print('Reason: %s' % json['error']['reason'])
exit(1)
#print(len(data))
#print(min(temp_list), max(temp_list), len(temp_list))
temp_list = []
# Some stations doesn't have two daily readings. This can be improved upon
for i in range(len(data)):
try:
temp_list.append(data[i]['observations'][0]['value']) # reading 1
except:
pass
try:
temp_list.append(data[i]['observations'][1]['value']) # reading 2
except:
pass
# Create all the color-stripes
temp_pos = 1
for i in temp_list:
temp_color = color_table.get(round(i))
p = Popen([f'convert -size 2x700 xc:{temp_color} img/stripe-{temp_pos}.png'], shell=True)
p.wait()
temp_pos += 1
# make a wide panoramic image
p = Popen([f'convert +append img/stripe-*.png img/{output_filename}'], shell=True)
p.wait()
# Clean up
p = Popen([f'rm -rf img/stripe-*.png'], shell=True)
p.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment