@@ -215,21 +215,31 @@ |
climate_data = [['cell', 'date', 'tmpmin (F)', 'tmpmax (F)', 'apcp (in)', 'dswrf (langley/day)', 'rh (%)', 'windspeed (mph)'],] |
|
c = getConnBeta().cursor(cursor_factory=psycopg2.extras.DictCursor) |
+ |
+ # Maintain simple cache to hold repeats |
+ cache = {} |
+ |
for i in range(len(tiles)): |
gid = tiles[i] |
cell_name = names[i] |
- |
- c.execute("SELECT * FROM narr.cells_ts WHERE gid=%s AND date>=%s AND date<=%s ORDER BY date", (gid, start_date, end_date)) |
- for row in c.fetchall(): |
- climate_data.append([cell_name, |
- row['date'].strftime("%Y-%m-%d"), |
- round(conv_dict['tmp'](row['tmin (C)']), 2), |
- round(conv_dict['tmp'](row['tmax (C)']), 2), |
- round(conv_dict['apcp'](row['ppt (mm)']), 3), |
- round(conv_dict['dswrf'](row['dswrf (W/m^2)']), 1), |
- round(conv_dict['rh'](row['rh (%)']), 0), |
- round(conv_dict['windgust'](row['windgust (m/s)']), 2) |
- ]) |
+ |
+ if gid in cache: |
+ cell_data = cache[gid] |
+ else: |
+ c.execute("SELECT * FROM narr.cells_ts WHERE gid=%s AND date>=%s AND date<=%s ORDER BY date", (gid, start_date, end_date)) |
+ cell_data = [] |
+ for row in c.fetchall(): |
+ cell_data.append([cell_name, |
+ row['date'].strftime("%Y-%m-%d"), |
+ round(conv_dict['tmp'](row['tmin (C)']), 2), |
+ round(conv_dict['tmp'](row['tmax (C)']), 2), |
+ round(conv_dict['apcp'](row['ppt (mm)']), 3), |
+ round(conv_dict['dswrf'](row['dswrf (W/m^2)']), 1), |
+ round(conv_dict['rh'](row['rh (%)']), 0), |
+ round(conv_dict['windgust'](row['windgust (m/s)']), 2) |
+ ]) |
+ cache[gid] = cell_data |
+ climate_data += cell_data |
return climate_data, cell_defs, cell_extents |
|
def WEgetDataMACA(self, input_zone_file, model, forecast_option, units, start_date, end_date): |
@@ -392,19 +402,28 @@ |
conv_dict = to_english_fn |
climate_data = [['cell', 'date', 'tmpmin (F)', 'tmpmax (F)', 'apcp (in)']] |
|
+ # Maintain simple cache to hold repeats |
+ cache = {} |
+ |
c = getConnBeta().cursor(cursor_factory=psycopg2.extras.DictCursor) |
for i in range(len(tiles)): |
gid = tiles[i] |
cell_name = names[i] |
|
- c.execute("SELECT * FROM prism.cells_ts WHERE gid=%s AND date>=%s AND date<=%s ORDER BY date", (gid, start_date, end_date)) |
- for row in c.fetchall(): |
- climate_data.append([cell_name, |
- row['date'].strftime("%Y-%m-%d"), |
- round(conv_dict['temp'](row['tmin']), 2), |
- round(conv_dict['temp'](row['tmax']), 2), |
- round(conv_dict['ppt'](row['ppt']), 2), |
- ]) |
+ if gid in cache: |
+ cell_data = cache[gid] |
+ else: |
+ c.execute("SELECT * FROM prism.cells_ts WHERE gid=%s AND date>=%s AND date<=%s ORDER BY date", (gid, start_date, end_date)) |
+ cell_data = [] |
+ for row in c.fetchall(): |
+ cell_data.append([cell_name, |
+ row['date'].strftime("%Y-%m-%d"), |
+ round(conv_dict['temp'](row['tmin']), 2), |
+ round(conv_dict['temp'](row['tmax']), 2), |
+ round(conv_dict['ppt'](row['ppt']), 2), |
+ ]) |
+ cache[gid] = cell_data |
+ climate_data += cell_data |
return climate_data, cell_defs, cell_extents |
|
def WEgetDataGHCND(self, input_zone_file_or_station_list, units, start_date, end_date, generate_average=False, IDW_center=False): |