WIP
This commit is contained in:
		
							parent
							
								
									1c64fca4a2
								
							
						
					
					
						commit
						2ec63cc0e3
					
				
					 4 changed files with 40 additions and 44 deletions
				
			
		|  | @ -83,14 +83,26 @@ class DB: | ||||||
|                 WITH ORDINALITY t1(doc, rn) |                 WITH ORDINALITY t1(doc, rn) | ||||||
|             ORDER BY datatype ASC, modelname ASC, scenario ASC; |             ORDER BY datatype ASC, modelname ASC, scenario ASC; | ||||||
|             """ |             """ | ||||||
|         result = db.engine.execute(text(cmd), id=id).fetchall() |         result = db.engine.execute(text(cmd)).fetchall() | ||||||
|         return result or abort(500) |         return result or abort(500) | ||||||
| 
 | 
 | ||||||
| # WITH x AS ( |     @classmethod | ||||||
| #     SELECT name, jsonb_array_elements(data) AS data |     def getTemps(cls, start, end, community_id, model, scenario): | ||||||
| #     FROM new_communities |         years = [str(x) for x in range(int(start), int(end)+1)] | ||||||
| #     WHERE name='Anchorage') |         cmd = """ | ||||||
| # SELECT d.key::INTEGER AS year, d.value AS temperatures |             WITH x AS ( | ||||||
| # FROM x, jsonb_each(data) d |                 SELECT name, jsonb_array_elements(data) AS data | ||||||
| # WHERE data->>'model'='CRU' |                 FROM new_communities | ||||||
| # AND d.key NOT IN ('model', 'datatype', 'scenario', 'modelname', 'resolution'); |                 WHERE id=:community_id) | ||||||
|  |             SELECT d.key::INTEGER AS year, d.value AS temperatures | ||||||
|  |             FROM x, jsonb_each(data) d | ||||||
|  |             WHERE data->>'model'=:model | ||||||
|  |             AND data->>'scenario'=:scenario | ||||||
|  |             AND d.key IN :years; | ||||||
|  |             """ | ||||||
|  |         result = db.engine.execute(text(cmd), | ||||||
|  |                                    community_id=community_id, | ||||||
|  |                                    model=model, | ||||||
|  |                                    scenario=scenario, | ||||||
|  |                                    years=tuple(years)).fetchall() | ||||||
|  |         return result or abort(500) | ||||||
|  |  | ||||||
|  | @ -98,7 +98,7 @@ | ||||||
|                         <td>{{ session['avg_indices'][1] }}</td> |                         <td>{{ session['avg_indices'][1] }}</td> | ||||||
|                         <td>{{ session['des_indices'][0] }}</td> |                         <td>{{ session['des_indices'][0] }}</td> | ||||||
|                         <td>{{ session['des_indices'][1] }}</td> |                         <td>{{ session['des_indices'][1] }}</td> | ||||||
|                         <td>{{ session['ds_name'][0][0] }} ({{ session['ds_name'][0][1] }})</td> |                         <td>{{ session['ds_name'][0] }} ({{ session['ds_name'][1] }})</td> | ||||||
|                         <td><button type="button" class="btn btn-success btn-sm" |                         <td><button type="button" class="btn btn-success btn-sm" | ||||||
|                                     onclick="window.location.href='{{ url_for('main.save') }}'" |                                     onclick="window.location.href='{{ url_for('main.save') }}'" | ||||||
|                                     title="Click to save this search"> |                                     title="Click to save this search"> | ||||||
|  |  | ||||||
|  | @ -1,43 +1,33 @@ | ||||||
| import numpy | import numpy | ||||||
| 
 | 
 | ||||||
| from .models import Temperature, Dataset, DB | from .models import DB | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def getTemps(datasets, community_id, minyear, maxyear): | def getTemps(datasets, community_id, minyear, maxyear): | ||||||
|     temps = Temperature.query.join(Dataset). \ |     model, scenario = datasets.split(',') | ||||||
|         filter(Dataset.id == Temperature.dataset_id, |     data = DB.getTemps(minyear, maxyear, community_id, model, scenario) | ||||||
|                Dataset.id == datasets, |     return data | ||||||
|                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): | def avg_air_temp(temps): | ||||||
|     return numpy.average(temps) |     year_counter, total = 0, 0 | ||||||
|  |     for temp in temps: | ||||||
|  |         total += sum(temp[1]) | ||||||
|  |         year_counter += 1 | ||||||
|  |     return total / (year_counter * 12) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def ann_air_indices(temps): | def ann_air_indices(temps): | ||||||
|     ATI, AFI = 0.0, 0.0 |     ATI, AFI = 0.0, 0.0 | ||||||
|     indices = numpy.zeros((temps.shape[0], 2), dtype='int') |     # TODO: drop numpy | ||||||
|  |     indices = numpy.zeros((len(temps), 2), dtype='int') | ||||||
|     months = [0.0 for m in range(12)] |     months = [0.0 for m in range(12)] | ||||||
|     days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |     days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | ||||||
|     i = 0 |     i = 0 | ||||||
|     for year in temps: |     for year in temps: | ||||||
|         j = 0 |         j = 0 | ||||||
|         for month in months: |         for month in months: | ||||||
|             months[j] = days[j] * year[j] |             months[j] = days[j] * year[1][j] | ||||||
|             j += 1 |             j += 1 | ||||||
| 
 | 
 | ||||||
|         for ind in months: |         for ind in months: | ||||||
|  | @ -52,12 +42,14 @@ def ann_air_indices(temps): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def avg_air_indices(indices): | def avg_air_indices(indices): | ||||||
|  |     # TODO: drop numpy | ||||||
|     temp = numpy.average(indices, axis=0) |     temp = numpy.average(indices, axis=0) | ||||||
|     return (int(temp[0]), int(temp[1])) |     return (int(temp[0]), int(temp[1])) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def des_air_indices(indices): | def des_air_indices(indices): | ||||||
|     if indices.shape[0] > 2: |     if indices.shape[0] > 2: | ||||||
|  |         # TODO: drop numpy | ||||||
|         ati = numpy.sort(indices[:, 0]) |         ati = numpy.sort(indices[:, 0]) | ||||||
|         afi = numpy.sort(indices[:, 1]) |         afi = numpy.sort(indices[:, 1]) | ||||||
|         dti = (ati[-1] + ati[-2] + ati[-3]) / 3.0 |         dti = (ati[-1] + ati[-2] + ati[-3]) / 3.0 | ||||||
|  | @ -67,16 +59,12 @@ def des_air_indices(indices): | ||||||
|         return (None, None) |         return (None, None) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def c_to_f(temp): |  | ||||||
|     return (temp * 9. / 5.) + 32. |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| def communitiesSelect(): | def communitiesSelect(): | ||||||
|     return [(c.id, c.name) for c in DB.getCommunities()] |     return [(c.id, c.name) for c in DB.getCommunities()] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def datasetsSelect(): | def datasetsSelect(): | ||||||
|     return [("{0.model},{0.scenario}".format(d), |     return [("{0.modelname},{0.scenario}".format(d), | ||||||
|         "{x} ({d.resolution}) - {d.modelname} {d.scenario}".format(d=d, |         "{x} ({d.resolution}) - {d.modelname} {d.scenario}".format(d=d, | ||||||
|                                                                    x=d.datatype.title())) |                                                                    x=d.datatype.title())) | ||||||
|         for d in DB.getDatasets()] |         for d in DB.getDatasets()] | ||||||
|  |  | ||||||
|  | @ -2,13 +2,11 @@ from numpy import arange, hstack | ||||||
| 
 | 
 | ||||||
| from flask import session, render_template, request, redirect, url_for | from flask import session, render_template, request, redirect, url_for | ||||||
| 
 | 
 | ||||||
| from flask import current_app |  | ||||||
| 
 |  | ||||||
| from . import main | from . import main | ||||||
| from .forms import AKIForm | from .forms import AKIForm | ||||||
| from .utils import getTemps, avg_air_temp, ann_air_indices, \ | from .utils import getTemps, avg_air_temp, ann_air_indices, \ | ||||||
|     avg_air_indices, des_air_indices, communitiesSelect, datasetsSelect |     avg_air_indices, des_air_indices, communitiesSelect, datasetsSelect | ||||||
| from .models import Dataset, DB | from .models import DB | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @main.route('/', methods=['GET']) | @main.route('/', methods=['GET']) | ||||||
|  | @ -31,9 +29,8 @@ def index(): | ||||||
|                 'longitude': round(community['longitude'], 5), |                 'longitude': round(community['longitude'], 5), | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             session['ds_name'] = Dataset.query. \ |             session['ds_name'] = session['datasets'].split(',') | ||||||
|                 with_entities(Dataset.modelname, Dataset.scenario). \ | 
 | ||||||
|                 filter_by(id=session['datasets']).all() |  | ||||||
|             temps_arr = getTemps(session['datasets'], |             temps_arr = getTemps(session['datasets'], | ||||||
|                                  community_id, |                                  community_id, | ||||||
|                                  session['minyear'], |                                  session['minyear'], | ||||||
|  | @ -58,7 +55,6 @@ def index_submit(): | ||||||
|             session['maxyear'] = session['minyear'] |             session['maxyear'] = session['minyear'] | ||||||
| 
 | 
 | ||||||
|         session['datasets'] = request.form['dataset'] |         session['datasets'] = request.form['dataset'] | ||||||
|         current_app.logger.info(session) |  | ||||||
|         return redirect(url_for('main.index')) |         return redirect(url_for('main.index')) | ||||||
|     else: |     else: | ||||||
|         # TODO: Fix post-POST handling |         # TODO: Fix post-POST handling | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon