diff --git a/app.js b/app.js index fca1fd7..e55febd 100644 --- a/app.js +++ b/app.js @@ -20,8 +20,8 @@ app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/data/python', statisticsRouter); -app.use('/data/python/pm10', pm10Router); +app.use('/python', statisticsRouter); +app.use('/python/pm10', pm10Router); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/getStatistics.py b/getStatistics.py index d654901..138aa31 100644 --- a/getStatistics.py +++ b/getStatistics.py @@ -15,6 +15,7 @@ from io import BytesIO import datetime from math import sin, floor,cos, sqrt, atan2, radians +import glob # fig = plt.figure() # plt.plot(values) @@ -112,13 +113,16 @@ def converTime(date): month = date[5:7] day = date[8:10] clock = date[11:13] - result = year + month + day +clock + result = year + month + day +"00" return int(result) ####################### def __senseBox__(): boxId = sys.argv[1] phenomenon = sys.argv[2] - url = "https://api.opensensemap.org/statistics/descriptive?senseboxid="+boxId+"&phenomenon="+phenomenon+"&from-date="+subtractMonth(today)+"Z&to-date="+str(today)+"Z&operation=arithmeticMean&window=3600000&format=json" + start = sys.argv[5] + 'T00:00:00.000Z' + end = sys.argv[6] + 'T00:00:00.000Z' + window = sys.argv[7] + url = "https://api.opensensemap.org/statistics/descriptive?senseboxid="+boxId+"&phenomenon="+phenomenon+"&from-date="+start+"&to-date="+end+"&operation=arithmeticMean&window="+window+"&format=json" response = requests.get(url) res = json.loads(response.content) boxData=res[0] @@ -155,18 +159,22 @@ def __DWD__(): folder = 'pub/CDC/observations_germany/climate/hourly'+title+'/recent/' ftp.cwd(folder) filedata = open(filename, 'wb') + ftp.retrbinary('RETR '+filename, filedata.write) filedata.close() ftp.quit() #### Unzip the just downloaded file to ressources/ zip_ref = zipfile.ZipFile(filename, 'r') zip_ref.extractall(path) + os.remove('/home/eric/Documents/Bachelorarbeit/senseboard_backend/senseBoard/'+filename) zip_ref.close() #### fetch the correct file onlyfiles = [f for f in os.listdir(path) if isfile(join(path, f))] - for filename in onlyfiles: - if filename.startswith('produkt'): - file = filename + for filenames in onlyfiles: + if filenames.startswith('produkt'): + file = filenames + else: + os.remove(path+'/'+filenames) ##### Read csv and extract data for further plotting dates = [] values = [] @@ -176,11 +184,14 @@ def __DWD__(): value = row[3] date = row[1] if not date == 'MESS_DATUM': - if float(date)>converTime(subtractMonth(today)) and float(date)=converTime(sys.argv[5]) and float(date)<=converTime(sys.argv[6]): dates.append(date[6:8]) if not value == 'TT_TU': value_new = float(value.strip()) values.append(value_new) + # delete files that were downloaded + os.remove(path+'/'+file) + os.rmdir(path) return values def __main__(): @@ -199,13 +210,14 @@ def __main__(): plt.grid() plt.legend() plt.xlabel('Datum') + plt.ylabel(sys.argv[2]) plt.xticks(ticks,labels) plt.savefig(bytes,format='jpg') else: plt.plot(senseBoxData) - plt.title(sys.argv[2]) plt.grid() plt.xlabel('Datum') + plt.ylabel(sys.argv[2]) plt.xticks(ticks,labels) plt.savefig(bytes,format='jpg') bytes.seek(0) diff --git a/routes/getPM10.js b/routes/getPM10.js index 801edd3..d9cd46b 100644 --- a/routes/getPM10.js +++ b/routes/getPM10.js @@ -13,13 +13,13 @@ var PythonShell = require('python-shell'); // args: [req.params.title] // }; /* GET users listing. */ -router.get('/:id/:phenomenon/:lat/:lon', function(req, res, next) { +router.get('/:id/:phenomenon/:lat/:lon/:from/:to/:window', function(req, res, next) { // Comment out this line: PythonShell.run(myPythonScriptPath, options={ mode: 'text', pythonPath: 'python3', pythonOptions: ['-u'], // get print results in real-time - args: [req.params.id,req.params.phenomenon,req.params.lat,req.params.lon]}, + args: [req.params.id,req.params.phenomenon,req.params.lat,req.params.lon,req.params.from,req.params.to,req.params.window]}, function (err, results) { if (err) throw err; // results is an array consisting of messages collected during execution diff --git a/routes/getStatistics.js b/routes/getStatistics.js index 6bcd026..dfddbf2 100644 --- a/routes/getStatistics.js +++ b/routes/getStatistics.js @@ -6,13 +6,13 @@ var myPythonScriptPath = 'getStatistics.py'; var PythonShell = require('python-shell'); var pyshell = new PythonShell(myPythonScriptPath); /* GET users listing. */ -router.get('/:id/:phenomenon/:lat/:lon', function(req, res, next) { +router.get('/:id/:phenomenon/:lat/:lon/:from/:to/:window', function(req, res, next) { // Comment out this line: PythonShell.run(myPythonScriptPath, options={ mode: 'text', pythonPath: 'python3', pythonOptions: ['-u'], // get print results in real-time - args: [req.params.id,req.params.phenomenon,req.params.lat,req.params.lon]}, + args: [req.params.id,req.params.phenomenon,req.params.lat,req.params.lon,req.params.from,req.params.to,req.params.window]}, function (err, results) { if (err) throw err; // results is an array consisting of messages collected during execution diff --git a/test.png b/test.png deleted file mode 100644 index ec3922f..0000000 Binary files a/test.png and /dev/null differ