This commit is contained in:
Matthew Dillon 2015-10-10 14:14:31 -07:00
parent f70f9d61b4
commit d692c6c744
5 changed files with 16 additions and 63 deletions

View file

@ -3,20 +3,13 @@ from wtforms import IntegerField, SelectField
from wtforms.validators import NumberRange, Required
from sqlalchemy import func
from .models import Temperature, DB
from flask import current_app
class AKIYearField(IntegerField):
def pre_validate(self, form):
pass
# if form.dataset.data is not None:
# ymin, ymax = Temperature.query \
# .with_entities(func.min(Temperature.year),
# func.max(Temperature.year)) \
# .filter(Temperature.dataset_id == form.dataset.data.id).all()[0]
# self.validators = [NumberRange(min=ymin, max=ymax), Required()]
if form.data['dataset'] == 'CRU,TS31':
self.validators = [NumberRange(min=1901, max=2009), Required()]
else:
self.validators = [NumberRange(min=2001, max=2099), Required()]
class AKIForm(Form):

View file

@ -4,44 +4,6 @@ from sqlalchemy.sql import text
from flask import abort
class Dataset(db.Model):
__tablename__ = 'datasets'
id = db.Column(db.Integer, primary_key=True)
datatype = db.Column(db.String(15), nullable=False)
model = db.Column(db.String(15), nullable=False)
modelname = db.Column(db.String(50), nullable=False)
scenario = db.Column(db.String(15), nullable=False)
resolution = db.Column(db.String(15), nullable=False)
temperatures = db.relationship('Temperature', backref='datasets')
@hybrid_property
def type(self):
return self.datatype.lower().capitalize()
class Temperature(db.Model):
__tablename__ = 'temperatures'
id = db.Column(db.Integer, primary_key=True)
dataset_id = db.Column(db.Integer, db.ForeignKey('datasets.id'))
community_id = db.Column(db.Integer, db.ForeignKey('communities.id'))
year = db.Column(db.Integer, nullable=False)
january = db.Column(db.Float, nullable=False)
february = db.Column(db.Float, nullable=False)
march = db.Column(db.Float, nullable=False)
april = db.Column(db.Float, nullable=False)
may = db.Column(db.Float, nullable=False)
june = db.Column(db.Float, nullable=False)
july = db.Column(db.Float, nullable=False)
august = db.Column(db.Float, nullable=False)
september = db.Column(db.Float, nullable=False)
october = db.Column(db.Float, nullable=False)
november = db.Column(db.Float, nullable=False)
december = db.Column(db.Float, nullable=False)
updated = db.Column(db.DateTime, nullable=True)
class DB:
@classmethod
def getCommunity(cls, id):
@ -67,13 +29,11 @@ class DB:
def getDatasets(cls):
cmd = """
SELECT DISTINCT ON (
doc->'model',
doc->'datatype',
doc->'resolution',
doc->'modelname',
doc->'scenario'
)
doc->'model' AS model,
doc->'datatype' AS datatype,
doc->'resolution' AS resolution,
doc->'modelname' AS modelname,
@ -87,22 +47,22 @@ class DB:
return result or abort(500)
@classmethod
def getTemps(cls, start, end, community_id, model, scenario):
def getTemps(cls, start, end, community_id, modelname, scenario):
years = [str(x) for x in range(int(start), int(end)+1)]
cmd = """
WITH x AS (
SELECT name, jsonb_array_elements(data) AS data
FROM new_communities
WHERE id=:community_id)
SELECT d.key::INTEGER AS year, d.value AS temperatures
SELECT d.key AS year, d.value AS temperatures
FROM x, jsonb_each(data) d
WHERE data->>'model'=:model
WHERE data->>'modelname'=:modelname
AND data->>'scenario'=:scenario
AND d.key IN :years;
"""
result = db.engine.execute(text(cmd),
community_id=community_id,
model=model,
modelname=modelname,
scenario=scenario,
years=tuple(years)).fetchall()
return result or abort(500)

View file

@ -137,7 +137,7 @@
<td>{{ value['avg_indices'][1] }}</td>
<td>{{ value['des_indices'][0] }}</td>
<td>{{ value['des_indices'][1] }}</td>
<td>{{ value['ds_name'][0][0] }} ({{ value['ds_name'][0][1] }})</td>
<td>{{ value['ds_name'][0] }} ({{ value['ds_name'][1] }})</td>
<td><button type="button" class="btn btn-danger btn-sm"
onclick="window.location.href='{{ url_for('main.delete', record=key) }}'"
title="Click to delete this search">

View file

@ -2,11 +2,11 @@ from .models import DB
def getTemps(session):
model, scenario = session['datasets'].split(',')
modelname, scenario = session['datasets'].split(',')
data = DB.getTemps(session['minyear'],
session['maxyear'],
session['community_data']['id'],
model,
modelname,
scenario)
return data

View file

@ -70,11 +70,11 @@ def reset():
@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(session)
temps = getTemps({'datasets': request.args.get('datasets', ''),
'minyear': request.args.get('minyear', ''),
'maxyear': request.args.get('maxyear', ''),
'community_data': {'id': request.args.get('community_id', '')}
})
return render_template('main/details.html',
lat=request.args.get('lat', ''),
lon=request.args.get('lon', ''),