Skip to content

Instantly share code, notes, and snippets.

@maesoser
Created November 4, 2016 09:36
Show Gist options
  • Save maesoser/8695448500216fdeed1ed18fd15204b8 to your computer and use it in GitHub Desktop.
Save maesoser/8695448500216fdeed1ed18fd15204b8 to your computer and use it in GitHub Desktop.
Tiny script to get the Air quality measurements from the sensors deployed on Madrid.
import json
import requests
class Station:
name = ""
uid = 0
lat = ""
lon = ""
quality = ""
dateStr = ""
timeStr = ""
no2_val = ""
pm10_val = ""
o3_val = ""
co_val = ""
so2_val = ""
no2_conc = ""
pm10_conc = ""
o3_conc = ""
co_conc = ""
so2_conc = ""
def __init__(self,jsonstruct):
#{u'latitud_decimal': u'40.407944', u'color': u'gris', u'longitud_decimal': u'-3.645305', u'valor': u'', u'nombre': u'MORATALAZ', u'id': u'36'},
self.uid = jsonstruct["id"]
self.name = jsonstruct["nombre"]
self.lat = jsonstruct["latitud_decimal"]
self.lon = jsonstruct["longitud_decimal"]
self.quality = jsonstruct["valor"]
def getReadings(self):
r = requests.get(urlEstacion+self.uid)
jsonAnsw = r.json()
self.dateStr = jsonAnsw["fecha"]["valor"]
self.dateStr = self.dateStr.replace("\/","-")
self.timeStr = jsonAnsw["hora"]["valor"]
self.no2_val = jsonAnsw["no2"]["valor"]
self.pm10_val = jsonAnsw["pm10"]["valor"]
self.o3_val = jsonAnsw["o3"]["valor"]
self.co_val = jsonAnsw["co"]["valor"]
self.so2_val = jsonAnsw["so2"]["valor"]
self.no2_conc = jsonAnsw["no2"]["concentracion"].split(" ")[0]
self.pm10_conc = jsonAnsw["pm10"]["concentracion"].split(" ")[0]
self.o3_conc = jsonAnsw["o3"]["concentracion"].split(" ")[0]
self.co_conc = jsonAnsw["co"]["concentracion"].split(" ")[0]
self.so2_conc = jsonAnsw["so2"]["concentracion"].split(" ")[0]
def printBasic(self):
print "["+self.uid+"] "+self.name+"\tLAT: "+str(self.lon)+" LON: "+str(self.lat)
def printDetail(self):
print self.dateStr+"\t"+self.timeStr
print "\tNO2 : "+self.no2_val+"\t"+self.no2_conc+"\tO3 : "+self.o3_val+"\t"+self.o3_conc
print "\tCO : "+self.co_val+"\t"+self.co_conc+"\tSO2 : "+self.so2_val+"\t"+self.so2_conc
print "\tPM10: "+self.pm10_val+"\t"+self.pm10_conc
def getCsvLine(self):
#`ID,LAT,LON,FECHA,HORA,NO2,PM10,O3,CO,SO2,NO2_CONC,PM10_CONC,O3_CONC,CO_CONC,SO2_CONC`
description = self.uid+","+self.lat+","+self.lon+","+self.dateStr+","+self.timeStr
values = self.no2_val+","+self.pm10_val+","+self.o3_val+","+self.co_val+","+self.so2_val
concentrations = self.no2_conc+","+self.pm10_conc+","+self.o3_conc+","+self.co_conc+","+self.so2_conc
csvLine = description+","+values+","+concentrations
return csvLine
urlEstaciones = "http://www.mambiente.munimadrid.es/mobile/conector-test.php?accion=estado_estaciones"
urlEstacion = "http://www.mambiente.munimadrid.es/mobile/conector-test.php?accion=indice_estaciones&estacion="
try:
print "Requesting list..."
r = requests.get(urlEstaciones)
except:
exit()
jsonAnsw = r.json()
ofile = open("readings.csv","a")
for item in jsonAnsw:
station = Station(jsonAnsw[item])
station.printBasic()
print "Downloading Advanced data..."
station.getReadings()
station.printDetail()
print "Saving..."
ofile.write(station.getCsvLine()+"\n")
ofile.close()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment