-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeologger.py
344 lines (317 loc) · 13 KB
/
geologger.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from celery import task
import subprocess
import csv
import simplejson as json
import pymongo
import tempfile
import datetime, time
import urlparse, urllib
import pandas
import rpy2.robjects as robjects
import os
from util import *
import geojson
template = """
library(lattice)
library(ggplot2)
library(plyr)
%(version)s
"""
def runR(datain,
script,
outformat,
saveoutput=False,
savedisplay=False,
saverdata=False):
""" Helper function to make running R scripted tasks easier"""
# Set current work directory to a tmp dir for R script, gather up all output from there when done.
r = robjects.r
if saveoutput | savedisplay:
tempdir = ""# create temporary directory
if saveoutput:
r('setwd("%s")' % tempdir )
# Optionally store and persist .RData to disk
# PDF Grabbing - grab PDF output and place in sensible location
if savedisplay:
r('pdf("%s")' % tempdir )
r(script)
# cleanup temp directory
if saverdata & saveoutput:
r('save.image()')
return
def getTagData(tagname, user_id="guest", db="geologger", col="lightlogs"):
""" Get light level data for a tag """
url = "http://test.cybercommons.org/mongo/db_find/%s/%s/{'spec':{'tagname':'%s','user_id':'%s'}}" %(db,col,tagname, user_id)
url_get = urllib.urlopen(url_fix(url)).read()
if url_get == "[]":
return {"error": "Empty result"}
else:
return json.loads(url_get)[0]
@task
def importTagData_manual( uploadloc, tagname, notes, location, dateformat=None , task_id=None, user_id=None):
""" Import a geologger tag to mongodb """
data = {
"tagname":tagname,
"notes": notes,
"release_location": location,
"user_id": user_id,
"timestamp": "%sZ" % datetime.datetime.now().isoformat(),
"task_id": task_id
}
data['data'] = csv2json(uploadloc, dateformat)
try:
c = mongoconnect('geologger','lightlogs')
c.insert( data )
return url_fix('http://test.cybercommons.org/mongo/db_find/geologger/lightlogs/{"spec":{"tagname":"%s","user_id":"%s"}}' % (tagname,user_id))
except:
return "Error saving to mongodb"
@task
def importTagData( data=None, task_id=None, user_id=None ):
""" A task for importing geologger tag data """
if isinstance(data,unicode or str):
datain = json.loads(data)
else:
datain = data
dataout = { "data": datain['data'],
"tagname": datain['tagname'],
"notes": datain['notes'],
"species": datain['species'],
"timestamp": "%sZ" % datetime.datetime.now().isoformat(),
"user_id": user_id,
"task_id": task_id
}
try:
c = mongoconnect('geologger','lightlogs')
c.insert(dataout)
return url_fix('http://test.cybercommons.org/mongo/db_find/geologger/lightlogs/{"spec":{"tagname":"%s","user_id":"%s"}}' % (dataout['tagname'],dataout['user_id']))
except:
return "Error saving to mongo"
@task
def twilightCalc( tagname=None, threshold=None, task_id=None, user_id=None):
""" Python wrapper for GeoLight twilightCalc() """
r = robjects.r
r.library('GeoLight')
r.library('RJSONIO')
tagdata = getTagData(tagname,user_id)
if tagdata != {"error": "Empty result"}:
ligdata = dict2csv(tagdata,subkey="data")
r('lig <- read.csv("%s", header=T)' % ligdata)
r('trans <- twilightCalc(lig$datetime, lig$light, LightThreshold=%s, ask=F)' % threshold)
c = mongoconnect('geologger','twilights')
data = {
"data":json.loads(r('toJSON(trans)')[0]),
"tagname": tagname,
"user_id": user_id,
"threshold": threshold,
"timestamp": datetime.datetime.now().isoformat(),
"format": "RJSONIO",
"task_id": task_id
}
c.insert(data)
cleanup([ligdata])
return 'http://test.cybercommons.org/mongo/db_find/geologger/twilights/{"spec":{"tagname":"%s","user_id":"%s"}}' % (tagname, user_id)
else:
return "Had a problem finding lightlog data"
@task
def twilightInsert(tagname=None, data=None, threshold=None, task_id=None,user_id=None):
""" Take twilight data from web interface """
c = mongoconnect('geologger','twilights')
data = {
"data": json.loads( data ),
"tagname": tagname,
"user_id": user_id,
"threshold": threshold,
"timestamp": datetime.datetime.now().isoformat(),
"format": "JSON-list",
"task_id": task_id
}
c.save(data)
return 'http://test.cybercommons.org/mongo/db_find/geologger/twilights/{"spec":{"tagname":"%s","user_id":"%s"}}' % (tagname, user_id)
@task
def deleteTag(tagname=None, user_id=None):
l = mongoconnect('geologger','lightlogs')
l.remove({"tagname":tagname,"user_id":user_id})
t = mongoconnect('geologger','twilights')
t.remove({"tagname":tagname,"user_id":user_id})
c = mongoconnect('geologger','coord')
c.remove({"tagname":tagname,"user_id":user_id})
@task
def changeLight( tagname=None, riseprob=None, setprob=None, days=None, task_id=None, user_id=None):
""" Python wrapper for GeoLight changeLight() """
r = robjects.r
r.library('GeoLight')
r.library('RJSONIO')
twilight = df2csv(getTagData(tagname=tagname, user_id=user_id, col="twilights"), subkey="data")
if len(twilight) < 5:
return "Twilights have not yet been calculated, please compute twilight events and then try again"
r('twilight <- read.csv("%s", header=T)' % twilight)
r('twilight$tFirst <- as.POSIXlt(twilight$tFirst, origin="1970-01-01")') # Convert to R Datetime
r('twilight$tSecond <- as.POSIXlt(twilight$tFirst, origin="1970-01-01")') # Convert to R Datetime
r('change <- changeLight(twilight$tFirst, twilight$tSecond, twilight$type, rise.prob=%s, set.prob=%s, days=%s,plot=F)' % (riseprob,setprob,days))
# Hack to get "." out of variable names so json can be stored in MongoDB
# see: "http://docs.mongodb.org/manual/reference/limits/#Restrictions on Field Names"
r('names(change)[3] <- "rise_prob"')
r('names(change)[4] <- "set_prob"')
r('names(change$setProb)[2] <- "prob_y"')
r('names(change$riseProb)[2] <- "prob_y"')
r('names(change$migTable)[5] <- "P_start"')
r('names(change$migTable)[6] <- "P_end"')
c = mongoconnect('geologger','changelight')
data = {
"data": json.loads(r('toJSON(change)')[0]),
"params": { "riseprob": riseprob, "setprob":setprob,"days":days },
"user_id": user_id,
"tagname": tagname,
"timestamp": datetime.datetime.now().isoformat(),
"task_id": task_id
}
c.insert(data)
cleanup([twilight])
return 'http://test.cybercommons.org/mongo/db_find/geologger/changelight/{"spec":{"tagname":"%s","user_id":"%s"}}' % (tagname, user_id)
@task
def distanceFilter( transdata, elevation, distance, task_id=None, user_id=None ):
""" Python wrapper for GeoLight distanceFilter() """
pass
@task
def coord( data=None, task_id=None, user_id=None ):
""" Python wrapper for GeoLight coord()
expects data like:
data = {
"tagname": "PABU_test",
"sunelevation": -4.5,
"computed": True,
"threshold": 4.5,
"twilights": [{
"tFirst": "2011-07-30T15:21:24.000Z",
"tSecond": "2011-07-31T15:21:24.000Z",
"type": "sunrise",
"active": True
}, {
"tFirst": "2011-07-30T15:21:24.000Z",
"tSecond": "2011-07-31T15:21:24.000Z",
"type": "sunrise",
"active": True
}],
"calibperiod": ["2011-07-30T15:21:24.000Z", "2011-07-30T15:21:24.000Z"]
}
Data can be provided as JSON string or as a python dictionary.
"""
if isinstance(data,unicode or str):
datain = json.loads(data)
else:
datain = data
datain['user_id'] = user_id
datain['timestamp'] = datetime.datetime.now().isoformat()
tagname = datain['tagname']
sunelevation = datain['sunelevation']
r = robjects.r
r.library('GeoLight')
r.library('RJSONIO')
# Save input twilights from UI
t = mongoconnect('geologger','twilights')
t.save(datain)
# Convert input to csv for reading in R
twilight = df2csv(datain, subkey="twilights")
r('twilights <- read.csv("%s", header=T)' % (twilight))
# Filter actives
r('twilights <- subset(twilights, twilights$active == "True")')
# Convert sunrise/sunset to 1,2
r('twilights$typecat[twilights$type == "sunrise"] <- 1')
r('twilights$typecat[twilights$type == "sunset"] <- 2')
# Convert datetimes
r('twilights$tFirst <- as.POSIXct(strptime(twilights$tFirst, format="%Y-%m-%dT%H:%M:%OSZ", tz="GMT"))')
r('twilights$tSecond <- as.POSIXct(strptime(twilights$tSecond, format="%Y-%m-%dT%H:%M:%OSZ", tz="GMT"))')
r('coord <- coord(twilights$tFirst, twilights$tSecond, twilights$typecat, degElevation = %s)'% sunelevation)
r('coord <- as.data.frame(cbind(as.data.frame(coord), twilights$tFirst, twilights$tSecond))' )
r('names(coord) <- c("x","y","tFirst","tSecond")')
r('coord <- subset(coord, !is.na(y) & !is.na(x))')
r('coord$tFirst <- as.character(strftime(coord$tFirst, "%Y-%m-%dT%H:%M:%SZ"))')
r('coord$tSecond <- as.character(strftime(coord$tSecond, "%Y-%m-%dT%H:%M:%SZ"))')
#r('coord <- subset(coord, !is.na(x))')
d = mongoconnect('geologger', 'debug')
c = mongoconnect('geologger','coord')
# dataout = dict(geojson.FeatureCollection(geojson.Feature(geojson.MultiPoint(json.loads(r('toJSON(coord)')[0])))))
df = pandasdf(json.loads(r('toJSON(coord)')[0]))
track = [ dict([
(colname, row[i])
for i,colname in enumerate(df.columns)
])
for row in df.values
]
d.insert({"dataframe": df.to_string(), "fromR": json.loads(r('toJSON(coord)')[0])})
dataout = json.loads(
geojson.dumps(
geojson.FeatureCollection( [
geojson.Feature(geometry=geojson.Point(
[item['x'],item['y']]), properties={"tFirst": item['tFirst'], "tSecond": item['tSecond']}
)
for item in track
]
)
)
)
dataout['properties'] = {
"sunelevation": sunelevation,
"tagname": tagname,
"user_id": user_id,
"timestamp": datetime.datetime.now().isoformat(),
"task_id": task_id
}
c.insert(dataout)
cleanup([twilight])
return 'http://test.cybercommons.org/mongo/db_find/geologger/coord/{"spec":{"tagname":"%s","user_id":"%s"}}' % (tagname,user_id)
@task
def getElevation( data=None, task_id=None, user_id=None):
"""
Wrapper for GeoLight getElevation
Expects data like:
data = {
"twilights": [
{
"active": true,
"tSecond": "2011-07-30T16:21:30.000Z",
"tFirst": "2011-07-30T06:58:15.000Z",
"type": "sunset"
},
{
"active": true,
"tSecond": "2011-07-31T06:53:08.181Z",
"tFirst": "2011-07-30T16:21:30.000Z",
"type": "sunrise"
},
{
"active": true,
"tSecond": "2011-07-31T16:25:39.230Z",
"tFirst": "2011-07-31T06:53:08.181Z",
"type": "sunset"
}
],
"tagname": "Pabu_test",
"release_location": [
35.1,
-97.0
],
"threshold": 5.5
}
"""
if isinstance(data,unicode or str):
datain = json.loads(data)
else:
datain = data
r = robjects.r
r.library('GeoLight')
r.library('RJSONIO')
lat, lon = datain['release_location']
tagname = datain['tagname']
twjson = dict2csv(datain, subkey="twilights")
r('twilights <- read.csv("%s", header=T)' % twjson)
r('twilights$tFirst <- strptime(twilights$tFirst, format="%Y-%m-%dT%H:%M:%OSZ")')
r('twilights$tSecond <- strptime(twilights$tSecond, format="%Y-%m-%dT%H:%M:%OSZ")')
r('paste(levels(twilights$type))')
r('levels(twilights$type) <- c(1,2)')
r('twilights <- subset(twilights, twilights$active == "True")')
r('elev <- getElevation(twilights$tFirst, twilights$tSecond, twilights$type, known.coord=c(%s,%s), plot=F)' %(lon, lat) )
elev = r('elev')
dataout = { "task_id": task_id, "user_id": user_id, "sunelevation": elev[0], "timestamp": datetime.datetime.now().isoformat() , "tagname": tagname }
cleanup([twjson])
return dataout