forked from jayantsi/nevadaet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·290 lines (268 loc) · 11.3 KB
/
main.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
############################################
## MAIN HANDLER FILE FOR VIEWS ##
#############################################
#
#Before deploying, make sure debug = False
#
#
#############################################
import datetime
import json
import logging
import os
import cgi
import config
import ee
import httplib2
import jinja2
import numpy
import webapp2
import formchecks
import mappingMethods
import timeseriesMethods
import collectionMethods
import application_defaults
import templatevariables
import collection_dataStore
import urllib
from google.appengine.api import users
from google.appengine.api import memcache
from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(180000)
httplib2.Http(timeout=180000)
import time
from time import sleep
#############################################
## SET DIRECTORY FOR PAGES ##
#############################################
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
JINJA_ENVIRONMENT = jinja2.Environment(
autoescape=True,
loader=jinja2.FileSystemLoader(template_dir),
extensions=['jinja2.ext.with_'])
#############################################
## RUN MAX DATES ##
#############################################
class runMaxDates(webapp2.RequestHandler):
def get(self):
ee.Initialize(config.EE_CREDENTIALS)
ee.data.setDeadline(180000) # 3 minutes
import loggerFunctions
logger = loggerFunctions.set_logger('MAX DATES')
try:
maxDates = collection_dataStore.get_all_maxDates()
except:
time.sleep(30)
maxDates = collection_dataStore.get_all_maxDates()
logger.info(maxDates)
for key, value in maxDates.iteritems():
#Check that date is of format yyyy-mm-dd
err = 0
try:
dt.datetime.strptime(value, "%Y-%m-%d")
except:
err = 1
if err:
# 3600s = 1 hr..in seconds (not milliseconds)
#memcache.add(key=key, value=value, time=3600)
memcache.add(key=key, value=value, time=0) #no expiration
#logger.info('added'+key+value)
#else #wait until next time
out_str = '<h4>Max Dates</h4>'
for key, value in sorted(maxDates.iteritems()):
out_str += '{} = {}'.format(key, value) + '<br><br>'
self.response.out.write(out_str)
#############################################
## RUN TOOL ##
#############################################
def runTool(self, applicationName):
ee.Initialize(config.EE_CREDENTIALS)
ee.data.setDeadline(180000)
import loggerFunctions
logger = loggerFunctions.set_logger('TEST')
# Initialize forms
template_values = templatevariables.set_initial_template_values(
self, applicationName)
'''
try:
template_values = templatevariables.set_initial_template_values(
self, applicationName)
except:
template_values = {}
'''
# Check user input for errors:
fieldID, input_err = formchecks.check_user_input(self, template_values)
if input_err is None:
if self.request.arguments():
# Update template values with mapid or time series data
toolAction = template_values['toolAction']
if toolAction == 'getTimeSeriesOverDateRange':
subDomainTypeTS = template_values['subDomainTypeTS']
if subDomainTypeTS == 'points':
shape_type = 'p'
elif subDomainTypeTS == 'customShapes':
shape_type = 'ft'
template_values = timeseriesMethods.get_time_series(
template_values, shape_type)
elif (toolAction == 'getMap' or
toolAction == 'downloadRectangleSubset' or
toolAction == 'showSingleValueOnMap' or
toolAction == 'downloadFusionTableSubset'):
template_values = mappingMethods.get_images(template_values)
else:
template_values['form_error'] = {fieldID: input_err}
#template_values['form_error'] = {'fieldID':str(fieldID),'error':str(input_err)}
return template_values
#############################################
## GENERATE RESPONSE ##
#############################################
def generateResponse(self, template_values):
if template_values['toolAction'] == 'showSingleValueOnMap':
self.response.out.write(template_values['pointValue'])
elif (template_values['toolAction'] == 'downloadRectangleSubset' or
template_values['toolAction'] == 'downloadFusionTableSubset'):
dataobj = {
'downloadURL': template_values['downloadURL'],
'shareLink': template_values['shareLink']
}
self.response.out.write(json.dumps(dataobj))
elif template_values['toolAction'] == 'getTimeSeriesOverDateRange':
dataobj = {
'timeSeriesTextData': [],
'timeSeriesGraphData': [],
'timeSeriesTextData2': [],
'timeSeriesGraphData2': [],
'climoData': [],
'climoData2': [],
'percentileData': [],
'percentileData2': [],
'shareLink': template_values['shareLink']
}
if 'form_error' in template_values.keys() and template_values['form_error']:
dataobj['form_error'] = template_values['form_error']
self.response.out.write(json.dumps(dataobj))
else:
# Converting to python dictionary
dataobj['timeSeriesTextData'] = template_values['timeSeriesTextData']
dataobj['timeSeriesGraphData'] = template_values['timeSeriesGraphData'],
if 'climoData' in template_values.keys():
dataobj['climoData'] = template_values['climoData']
dataobj['percentileData'] = template_values['percentileData']
if 'timeSeriesTextData2' in template_values.keys():
dataobj['timeSeriesTextData2'] = template_values['timeSeriesTextData2']
dataobj['timeSeriesGraphData2'] = template_values['timeSeriesGraphData2']
if 'climoData2' in template_values.keys():
dataobj['climoData2'] = template_values['climoData2']
dataobj['percentileData2'] = template_values['percentileData2']
# Converting to a javascript object(JSON)
self.response.out.write(json.dumps(dataobj))
else:
dataobj = {}
if 'form_error' in template_values.keys() and template_values['form_error']:
dataobj['form_error'] = template_values['form_error']
self.response.out.write(json.dumps(dataobj))
else:
# All mapping toolActions
dataobj = {
"mapid": template_values['mapid'],
"token": template_values['token'],
'shareLink': template_values['shareLink']
}
self.response.out.write(json.dumps(dataobj))
#############################################
## CLIMATE ENGINE EXPERT PAGE ##
#############################################
class climateEngineExpert(webapp2.RequestHandler):
def get(self):
applicationName = 'climateEngineExpert'
template_values = runTool(self, applicationName)
template = JINJA_ENVIRONMENT.get_template('climateEngineExpert.html')
self.response.out.write(template.render(template_values))
def post(self):
applicationName = 'climateEngineExpert'
template_values = runTool(self, applicationName)
generateResponse(self, template_values)
#############################################
## FEWS NET PAGE ##
#############################################
class fewsNet(webapp2.RequestHandler):
def get(self):
applicationName = 'fewsNet'
template_values = runTool(self, applicationName)
template = JINJA_ENVIRONMENT.get_template('fewsNet.html')
self.response.out.write(template.render(template_values))
def post(self):
applicationName = 'fewsNet'
template_values = runTool(self, applicationName)
generateResponse(self, template_values)
#############################################
## NEVADA-ET ##
#############################################
class nevadaet(webapp2.RequestHandler):
def get(self):
applicationName = 'nevadaet'
template_values = runTool(self, applicationName)
template = JINJA_ENVIRONMENT.get_template('nevadaet.html')
self.response.out.write(template.render(template_values))
def post(self):
applicationName = 'nevadaet'
template_values = runTool(self, applicationName)
generateResponse(self, template_values)
#############################################
## NPS LAKE MEAD ##
#############################################
class lakeMead(webapp2.RequestHandler):
def get(self):
applicationName = 'lakeMead'
template_values = runTool(self, applicationName)
template = JINJA_ENVIRONMENT.get_template('lakeMead.html')
self.response.out.write(template.render(template_values))
def post(self):
applicationName = 'lakeMead'
template_values = runTool(self, applicationName)
generateResponse(self, template_values)
#############################################
## PRECISION GRAZING ##
#############################################
class precisionGrazing(webapp2.RequestHandler):
def get(self):
applicationName = 'precisionGrazing'
template_values = runTool(self, applicationName)
template = JINJA_ENVIRONMENT.get_template('precisionGrazing.html')
self.response.out.write(template.render(template_values))
def post(self):
applicationName = 'precisionGrazing'
template_values = runTool(self, applicationName)
generateResponse(self, template_values)
#############################################
## GDD TOOL ##
#############################################
class gddTool(webapp2.RequestHandler):
def get(self):
applicationName = 'gddTool'
template_values = runTool(self, applicationName)
template = JINJA_ENVIRONMENT.get_template('gddTool.html')
self.response.out.write(template.render(template_values))
def post(self):
applicationName = 'gddTool'
template_values = runTool(self, applicationName)
generateResponse(self, template_values)
#############################################
## URL MAPPING ##
#############################################
app = webapp2.WSGIApplication(
[
('/', climateEngineExpert),
('/climateEngineExpert', climateEngineExpert),
('/(?i)fewsNet', fewsNet),
('/(?i)nevadaet', nevadaet),
('/(?i)lakeMead', lakeMead),
('/precisionGrazing', precisionGrazing),
('/gddTool', gddTool),
('/runTool', runTool),
('/tasks/maxDate',runMaxDates),
],
#debug=True,
debug=False,
#config=config
)