-
Notifications
You must be signed in to change notification settings - Fork 1
/
aemet-get-geo.py
executable file
·48 lines (43 loc) · 1.84 KB
/
aemet-get-geo.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
#!/usr/bin/python
# encoding: utf-8
import StringIO
import sys
import csv
import urllib2
import string
manylines = 1
if len(sys.argv) == 2:
manylines = int(sys.argv[1])
if len(sys.argv) > 2:
print "Usage: ./aemet-get.py [number of last values to retrieve]"
sys.exit(254)
if manylines > 24:
print "The maximum of last values that can be retrieved from AEMET is 24"
sys.exit(254)
with open('geo.id') as stnfile:
stations = csv.reader(stnfile, delimiter=';')
for station in stations:
stationProv = station[0]
stationId = station[1]
stationLoc = station[2]
stationGeo = station[3]
# we construct the curl URI
curlUri = 'http://www.aemet.es/es/eltiempo/observacion/ultimosdatos_' + stationId + '_datos-horarios.csv?k=and&l=' + stationId + '&datos=det&w=0&f=temperatura&x=h24'
try:
urlResponse = urllib2.urlopen(curlUri)
aemetData = urlResponse.read()
lastline = manylines + 4
for thisline in range(4, lastline, 1):
aemetDataLast = aemetData.splitlines()[thisline]
lineOut = '"' + stationProv + '",' + '"' + stationId + '",' + '"' + stationLoc + '",' + '"' + stationGeo + '",' + aemetDataLast
# now let's separate into csv, we need this to build the unique ID for ElasticSearch
dataLine = StringIO.StringIO(lineOut)
csvData = csv.reader(dataLine, delimiter=',')
for csvDataLine in csvData:
rawId = csvDataLine[4] + csvDataLine[1]
id = filter(str.isalnum, rawId)
# and now for constructing our CSV output and printing it, first field is ID
outLine = '"' + id + '",' + lineOut
print outLine
except:
message = "Sorry, no data for Station ID: " + stationId