def json_response(function=None, *args, **kwargs): try: response = function(*args, **kwargs) except Exception as e: error = { 'error': { 'message': unicode(e) } } response = HttpResponseServerError() response.content = json.dumps(error, separators=(',', ':')) response['Content-Type'] = 'application/json' return response
def getraster(request): assert isinstance(request, HttpRequest) json_request = json.dumps(request.GET) try: rparm = request.GET.get('param', None) rtime = extract_time(request) cmap = request.GET.get('cmap','binary') body = cache.blobstore.get_blob_to_bytes('ice2ocean',json_request) response = HttpResponse(body,'image/png') return response except: try: ### AAA new method of extracting an image based on param if rparm in ('roff','prec'): control = fieldManager.getInfo(rparm) # ds = b.fetch_ds(rparm,rtime) ds = b.fetch_ds2(rparm) print ds #lats = ds.variables['latitude'][:] #lons = ds.variables['longitude'][:] #dataField = ds.variables[control["RowKey"]][:].reshape(len(lats),len(lons)) #ds.close() # AAA leave out the reprojection for now #plons,plats = bingProjection(lons,lats) #plons = plons[0,:] #plats = plats[:,0] #print plons, plats # x = np.arange(xMin, xMin + cellSize * fh.nx, cellSize) # y = np.arange(yMin, yMin + cellSize * fh.ny, cellSize) #for aRow in parfile: # if (aRow[0] != '!'): # t = aRow.split('=')[0].strip() # try: # exec(t + " = " + aRow.split('=')[1].strip()) # except: # a = 1 nx = 1810 ny = 900 d = ds.reshape(-1,ny,nx) subset = d[0,:,:] img = subset*(subset > 0) y, x = np.mgrid[0:ny,0:nx] buf = BytesIO() plt.imshow(img) #plot_to_bytes(lons,lats,dataField,buf,float(control["color_min"]), float(control["color_max"]), cmap_name=cmap) plot_to_bytes(x,y,img,buf,float(control["color_min"]), float(control["color_max"]), cmap_name=cmap) buf.seek(0) data = buf.read() buf.close() cache.blobstore.put_block_blob_from_bytes('ice2ocean',json_request,data) response = HttpResponse(data,'image/png') else: control = fieldManager.getInfo(rparm) ds = b.fetch_ds(rtime) if not ds: oops = HttpResponseServerError() oops.content = "Unable to retrieve data for request {0}".format(json_request) return oops lats = ds.variables['lat_psi'][:] lons = ds.variables['lon_psi'][:] plons,plats = bingProjection(lons,lats) plons = plons[0,:] plats = plats[:,0] slab = depthSliceOnly(rtime,float(rdepth),control["RowKey"])[1:,1:] ds.close() buf = BytesIO() plot_to_bytes(plons,plats,slab,buf,float(control["color_min"]), float(control["color_max"]),cmap_name=cmap) buf.seek(0) data = buf.read() buf.close() cache.blobstore.put_block_blob_from_bytes('liveocean', json_request, data) response = HttpResponse(data,'image/png') return response except Exception as a: try: ds.close() except: print "" a.message return cr.invalid_parameter()