-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovincias_id.py
83 lines (62 loc) · 2.33 KB
/
provincias_id.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# AEMET Opendata parser
# Spain meteo Agecy AEMET has started an OpenData project...
# Before I was using the CSV files available, but, well, clumsy stuff
# There is no full list of the Station IDs by province, so we build one from data
# available in the dropdowns on https://opendata.aemet.es/centrodedescargas/productosAEMET?
# Provinces are in https://opendata.aemet.es/centrodedescargas/xml/provincias.xml
# Then for each province there is a file https://opendata.aemet.es/centrodedescargas/xml/udat/udat[ID].xml
import httplib, urllib, ssl, json, requests, time
import sys
import xml.etree.ElementTree
reload(sys)
sys.setdefaultencoding('utf8')
# Load the API key from file api.key
# Please note the API key has to be obtained at https://opendata.aemet.es/centrodedescargas/altaUsuario
ssl._create_default_https_context = ssl._create_unverified_context
provinciasUri = '/centrodedescargas/xml/provincias.xml'
conn = httplib.HTTPSConnection("opendata.aemet.es")
conn.request("GET",provinciasUri)
response = conn.getresponse()
data = response.read()
# Creating list for the provinces
provincias = xml.etree.ElementTree.fromstring(data)
ids = []
provs = []
iter = provincias.getiterator('ID')
for elem in iter:
elementName = elem.tag
elementText = elem.text
# print elem.tag, elem.text
ids.append(elementText)
iter = provincias.getiterator('NOMBRE')
for elem in iter:
elementName = elem.tag
elementText = elem.text
# print elem.tag, elem.text
provs.append(elementText)
provdict = dict(zip(ids,provs))
a = 0
for key in provdict:
# we build the URL as above
stationsUri = '/centrodedescargas/xml/udat/udat' + key + '.xml'
conn = httplib.HTTPSConnection("opendata.aemet.es")
conn.request("GET", stationsUri)
response = conn.getresponse()
data = response.read()
# data = data.split("\n",2)[1:]
stations = xml.etree.ElementTree.fromstring(data)
iter = stations.getiterator('ID')
ifemas = []
thisProv = []
currentProv = provdict[key]
# print currentProv
for elem in iter:
elementName = elem.tag
elementText = elem.text
# print elem.tag, elem.text
ifemas.append(elementText)
#print elementText
thisProv.append(currentProv)
stationsdict = dict(zip(ifemas,thisProv))
for esto in stationsdict:
print esto, stationsdict[esto]