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) | ||||
|             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) | ||||
| 
 | ||||
| # WITH x AS ( | ||||
| #     SELECT name, jsonb_array_elements(data) AS data | ||||
| #     FROM new_communities | ||||
| #     WHERE name='Anchorage') | ||||
| # SELECT d.key::INTEGER AS year, d.value AS temperatures | ||||
| # FROM x, jsonb_each(data) d | ||||
| # WHERE data->>'model'='CRU' | ||||
| # AND d.key NOT IN ('model', 'datatype', 'scenario', 'modelname', 'resolution'); | ||||
|     @classmethod | ||||
|     def getTemps(cls, start, end, community_id, model, 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 | ||||
|             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['des_indices'][0] }}</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" | ||||
|                                     onclick="window.location.href='{{ url_for('main.save') }}'" | ||||
|                                     title="Click to save this search"> | ||||
|  |  | |||
|  | @ -1,43 +1,33 @@ | |||
| import numpy | ||||
| 
 | ||||
| from .models import Temperature, Dataset, DB | ||||
| from .models import DB | ||||
| 
 | ||||
| 
 | ||||
| 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 | ||||
|     model, scenario = datasets.split(',') | ||||
|     data = DB.getTemps(minyear, maxyear, community_id, model, scenario) | ||||
|     return data | ||||
| 
 | ||||
| 
 | ||||
| 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): | ||||
|     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)] | ||||
|     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] | ||||
|             months[j] = days[j] * year[1][j] | ||||
|             j += 1 | ||||
| 
 | ||||
|         for ind in months: | ||||
|  | @ -52,12 +42,14 @@ def ann_air_indices(temps): | |||
| 
 | ||||
| 
 | ||||
| def avg_air_indices(indices): | ||||
|     # TODO: drop numpy | ||||
|     temp = numpy.average(indices, axis=0) | ||||
|     return (int(temp[0]), int(temp[1])) | ||||
| 
 | ||||
| 
 | ||||
| def des_air_indices(indices): | ||||
|     if indices.shape[0] > 2: | ||||
|         # TODO: drop numpy | ||||
|         ati = numpy.sort(indices[:, 0]) | ||||
|         afi = numpy.sort(indices[:, 1]) | ||||
|         dti = (ati[-1] + ati[-2] + ati[-3]) / 3.0 | ||||
|  | @ -67,16 +59,12 @@ def des_air_indices(indices): | |||
|         return (None, None) | ||||
| 
 | ||||
| 
 | ||||
| def c_to_f(temp): | ||||
|     return (temp * 9. / 5.) + 32. | ||||
| 
 | ||||
| 
 | ||||
| def communitiesSelect(): | ||||
|     return [(c.id, c.name) for c in DB.getCommunities()] | ||||
| 
 | ||||
| 
 | ||||
| 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.datatype.title())) | ||||
|         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 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, communitiesSelect, datasetsSelect | ||||
| from .models import Dataset, DB | ||||
| from .models import DB | ||||
| 
 | ||||
| 
 | ||||
| @main.route('/', methods=['GET']) | ||||
|  | @ -31,9 +29,8 @@ def index(): | |||
|                 'longitude': round(community['longitude'], 5), | ||||
|             } | ||||
| 
 | ||||
|             session['ds_name'] = Dataset.query. \ | ||||
|                 with_entities(Dataset.modelname, Dataset.scenario). \ | ||||
|                 filter_by(id=session['datasets']).all() | ||||
|             session['ds_name'] = session['datasets'].split(',') | ||||
| 
 | ||||
|             temps_arr = getTemps(session['datasets'], | ||||
|                                  community_id, | ||||
|                                  session['minyear'], | ||||
|  | @ -58,7 +55,6 @@ def index_submit(): | |||
|             session['maxyear'] = session['minyear'] | ||||
| 
 | ||||
|         session['datasets'] = request.form['dataset'] | ||||
|         current_app.logger.info(session) | ||||
|         return redirect(url_for('main.index')) | ||||
|     else: | ||||
|         # TODO: Fix post-POST handling | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon