More or less copy and paste to get mvp
This commit is contained in:
parent
871a2f97b3
commit
b86ebd63c2
9 changed files with 606 additions and 31 deletions
45
app/main/forms.py
Normal file
45
app/main/forms.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
from flask_wtf import Form
|
||||
from wtforms import IntegerField
|
||||
from wtforms.validators import NumberRange, Required
|
||||
from wtforms.ext.sqlalchemy.fields import QuerySelectField
|
||||
from app.models import Community, Dataset, Temperature
|
||||
from sqlalchemy import func
|
||||
|
||||
|
||||
class AKIYearField(IntegerField):
|
||||
def pre_validate(self, form):
|
||||
if form.model.data is not None:
|
||||
ymin, ymax = Temperature.query \
|
||||
.with_entities(func.min(Temperature.year),
|
||||
func.max(Temperature.year)) \
|
||||
.filter(Temperature.dataset_id == form.model.data.id).all()[0]
|
||||
self.validators = [NumberRange(min=ymin, max=ymax), Required()]
|
||||
|
||||
|
||||
def communities():
|
||||
return Community.query.order_by('name')
|
||||
|
||||
|
||||
def datasets():
|
||||
return Dataset.query.order_by('datatype', 'model', 'scenario')
|
||||
|
||||
|
||||
def dataset_names(ds):
|
||||
return "{0.type} ({0.resolution}) - {0.modelname} {0.scenario}".format(ds)
|
||||
|
||||
|
||||
class AKIForm(Form):
|
||||
community = QuerySelectField(query_factory=communities,
|
||||
get_label='name',
|
||||
allow_blank=True,
|
||||
blank_text='---Select a community---',
|
||||
validators=[Required(message='Please select a community')])
|
||||
|
||||
minyear = AKIYearField('minyear')
|
||||
maxyear = AKIYearField('maxyear')
|
||||
|
||||
model = QuerySelectField(query_factory=datasets,
|
||||
get_label=dataset_names,
|
||||
allow_blank=True,
|
||||
blank_text='---Select a dataset---',
|
||||
validators=[Required(message='Please select a dataset')])
|
71
app/main/utils.py
Normal file
71
app/main/utils.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
import numpy
|
||||
|
||||
from app.models import Temperature, Dataset
|
||||
|
||||
|
||||
def getTemps(datasets, community_id, minyear, maxyear):
|
||||
temps = Temperature.query.join(Dataset). \
|
||||
filter(Dataset.id == Temperature.dataset_id,
|
||||
Dataset.id == datasets,
|
||||
Temperature.community_id == community_id,
|
||||
Temperature.year >= minyear,
|
||||
Temperature.year <= maxyear)
|
||||
|
||||
length = int(maxyear) - int(minyear)
|
||||
temps_arr = numpy.zeros((length+1, 12))
|
||||
|
||||
i = 0
|
||||
for t in temps.all():
|
||||
temps_arr[i,:] = [t.january, t.february, t.march,
|
||||
t.april, t.may, t.june,
|
||||
t.july, t.august, t.september,
|
||||
t.october, t.november, t.december]
|
||||
i += 1
|
||||
return temps_arr
|
||||
|
||||
|
||||
def avg_air_temp(temps):
|
||||
return numpy.average(temps)
|
||||
|
||||
|
||||
def ann_air_indices(temps):
|
||||
ATI, AFI = 0.0, 0.0
|
||||
indices = numpy.zeros((temps.shape[0], 2), dtype='int')
|
||||
months = [0.0 for m in range(12)]
|
||||
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
i = 0
|
||||
for year in temps:
|
||||
j = 0
|
||||
for month in months:
|
||||
months[j] = days[j] * year[j]
|
||||
j += 1
|
||||
|
||||
for ind in months:
|
||||
if ind >= 0.0:
|
||||
ATI = ATI + ind
|
||||
else:
|
||||
AFI = AFI + ind
|
||||
indices[i, 0], indices[i, 1] = int(ATI), int(AFI)
|
||||
ATI, AFI = 0.0, 0.0
|
||||
i += 1
|
||||
return indices
|
||||
|
||||
|
||||
def avg_air_indices(indices):
|
||||
temp = numpy.average(indices, axis=0)
|
||||
return (int(temp[0]), int(temp[1]))
|
||||
|
||||
|
||||
def des_air_indices(indices):
|
||||
if indices.shape[0] > 2:
|
||||
ati = numpy.sort(indices[:,0])
|
||||
afi = numpy.sort(indices[:,1])
|
||||
dti = (ati[-1] + ati[-2] + ati[-3]) / 3.0
|
||||
dfi = (afi[0] + afi[1] + afi[2]) / 3.0
|
||||
return (int(dti), int(dfi))
|
||||
else:
|
||||
return (None, None)
|
||||
|
||||
|
||||
def c_to_f(temp):
|
||||
return (temp * 9. / 5.) + 32.
|
|
@ -1,6 +1,114 @@
|
|||
from numpy import arange, hstack
|
||||
|
||||
from flask import session, render_template, request, redirect, current_app
|
||||
|
||||
from . import main
|
||||
from .forms import AKIForm
|
||||
from .utils import getTemps, avg_air_temp, ann_air_indices, avg_air_indices, des_air_indices
|
||||
from app.models import Community, Dataset, Temperature
|
||||
|
||||
@main.route('/')
|
||||
|
||||
@main.route('/', methods=['GET'])
|
||||
def index():
|
||||
return '<h1>Hello world</h1>'
|
||||
form = AKIForm()
|
||||
session['community_data'] = None
|
||||
session['avg_temp'] = None
|
||||
session['avg_indices'] = None
|
||||
session['des_indices'] = None
|
||||
|
||||
if 'community' in session:
|
||||
community_id = session['community']
|
||||
if all(key in session for key in ('minyear', 'maxyear', 'datasets')):
|
||||
community = Community.query.get_or_404(community_id)
|
||||
|
||||
# TODO: clean this up
|
||||
session['community_data'] = dict()
|
||||
session['community_data']['id'] = community_id
|
||||
session['community_data']['name'] = community.name
|
||||
session['community_data']['latitude'] = round(community.latitude, 5)
|
||||
session['community_data']['longitude'] = round(community.longitude, 5)
|
||||
|
||||
session['ds_name'] = Dataset.query. \
|
||||
with_entities(Dataset.modelname, Dataset.scenario). \
|
||||
filter_by(id=session['datasets']).all()
|
||||
temps_arr = getTemps(session['datasets'], community_id, session['minyear'], session['maxyear'])
|
||||
|
||||
session['avg_temp'] = avg_air_temp(temps_arr)
|
||||
indices = ann_air_indices(temps_arr)
|
||||
session['avg_indices'] = avg_air_indices(indices)
|
||||
session['des_indices'] = des_air_indices(indices)
|
||||
|
||||
return render_template("index.html", form=form)
|
||||
|
||||
|
||||
@main.route('/', methods=['POST'])
|
||||
def index_submit():
|
||||
form = AKIForm()
|
||||
if form.validate():
|
||||
session['community'] = request.form['community']
|
||||
session['minyear'] = request.form['minyear']
|
||||
session['maxyear'] = request.form['maxyear']
|
||||
if session['minyear'] > session['maxyear']:
|
||||
session['maxyear'] = session['minyear']
|
||||
|
||||
session['datasets'] = request.form['model']
|
||||
return redirect('/')
|
||||
else:
|
||||
return render_template("index.html", form=form)
|
||||
|
||||
|
||||
@main.route('/datatypes')
|
||||
def datatypes():
|
||||
return render_template("datatypes.html")
|
||||
|
||||
|
||||
@main.route('/reset')
|
||||
def reset():
|
||||
session.clear()
|
||||
return redirect('/')
|
||||
|
||||
|
||||
@main.route('/details')
|
||||
def details():
|
||||
datasets = request.args.get('datasets', '')
|
||||
community_id = request.args.get('community_id', '')
|
||||
minyear = request.args.get('minyear', '')
|
||||
maxyear = request.args.get('maxyear', '')
|
||||
temps = getTemps(datasets, community_id, minyear, maxyear)
|
||||
years = arange(int(minyear), int(maxyear)+1).reshape(int(maxyear)-int(minyear) + 1, 1)
|
||||
temps = hstack((years, temps))
|
||||
return render_template("details.html",
|
||||
lat=request.args.get('lat', ''),
|
||||
lon=request.args.get('lon', ''),
|
||||
community_name=request.args.get('name', ''),
|
||||
temps=temps)
|
||||
|
||||
|
||||
@main.route('/save')
|
||||
def save():
|
||||
if 'save' in session:
|
||||
i = len(session['save'])
|
||||
save = session['save']
|
||||
else:
|
||||
save = dict()
|
||||
i = 0
|
||||
|
||||
save[i] = dict()
|
||||
save[i]['datasets'] = session['datasets']
|
||||
save[i]['ds_name'] = session['ds_name']
|
||||
save[i]['community_data'] = session['community_data']
|
||||
save[i]['minyear'] = session['minyear']
|
||||
save[i]['maxyear'] = session['maxyear']
|
||||
save[i]['avg_temp'] = session['avg_temp']
|
||||
save[i]['avg_indices'] = session['avg_indices']
|
||||
save[i]['des_indices'] = session['des_indices']
|
||||
session.clear()
|
||||
session['save'] = save
|
||||
return redirect('/')
|
||||
|
||||
|
||||
@main.route('/delete')
|
||||
def delete():
|
||||
record = request.args.get('record', '')
|
||||
session['save'].pop(record)
|
||||
return redirect('/')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue