# Annotation of /trunk/doc/examples/cookbook/cblib2.py

Revision 2706 - (hide annotations)
Fri Oct 2 02:15:04 2009 UTC (9 years, 9 months ago) by jfenwick
File MIME type: text/x-python
File size: 3163 byte(s)
```Perhaps this will stop the cookbook from hating me.

```
 1 jfenwick 2697 ######################################################## 2 # 3 # Copyright (c) 2009 by University of Queensland 4 # Earth Systems Science Computational Center (ESSCC) 5 6 # 7 # Primary Business: Queensland, Australia 8 # Licensed under the Open Software License version 3.0 9 10 # 11 ######################################################## 12 13 __copyright__="""Copyright (c) 2009 by University of Queensland 14 Earth Systems Science Computational Center (ESSCC) 15 http://www.uq.edu.au/esscc 16 Primary Business: Queensland, Australia""" 17 __license__="""Licensed under the Open Software License version 3.0 18 19 __url__= 20 21 """ 22 Additional routines using matplotlib for cookbook examples. 23 Author: Antony Hallam antony.hallam@uqconnect.edu.au 24 """ 25 26 #Please ensure that if you need to use the agg back-end that you have chosen it before 27 #calling functions in this file 28 29 jfenwick 2706 import numpy as np 30 jfenwick 2697 # Calculate the location of quivers for a matplotlib plot 31 # quivshape :: [x,y] :: number of quivers in x and y direction 32 # lenxax :: length of model along x 33 # lenyax :: length of model along y 34 # qu :: gradient of escript solution ie grad(T) 35 def toQuivLocs(quivshape,lenxax,lenyax,qu): 36 numquiv = quivshape[0]*quivshape[1] # total number of quivers 37 dx = lenxax/quivshape[0]+1. # quiver x spacing 38 dy = lenyax/quivshape[1]+1. # quiver y spacing 39 qulocs=np.zeros([numquiv,2],float) # memory for quiver locations 40 # fill qulocs 41 for i in range(0,quivshape[0]-1): 42 for j in range(0,quivshape[1]-1): 43 qulocs[i*quivshape[0]+j,:] = [dx+dx*i,dy+dy*j] 44 # retreive values for quivers direction and shape from qu 45 quL = Locator(qu.getFunctionSpace(),qulocs.tolist()) 46 qu = quL(qu) #array of dx,dy for quivers 47 qu = np.array(qu) #turn into a numpy array 48 qulocs = quL.getX() #returns actual locations from data 49 qulocs = np.array(qulocs) #turn into a numpy array 50 return qu,qulocs 51 52 53 54 55 # Extract the X and Y coordinates of an array 56 # coords :: escript coordiantes from .getX function 57 def toXYTuple(coords): 58 coords = np.array(coords.toListOfTuples()) #convert to Tuple 59 coordX = coords[:,0]; coordY = coords[:,1] #X and Y components. 60 return coordX,coordY 61 62 def toRegGrid(grid,domain,newx,newy,width,depth): 63 import pylab as pl 64 oldspacecoords=domain.getX() 65 gridT = grid.toListOfTuples(scalarastuple=False) 66 cxy = Data(oldspacecoords, grid.getFunctionSpace()) 67 cX, cY = toXYTuple(cxy) 68 xi = np.linspace(0.0,width,newx) 69 yi = np.linspace(depth,0.0,newy) 70 # grid the data. 71 zi = pl.matplotlib.mlab.griddata(cX,cY,gridT,xi,yi) 72 return xi,yi,zi 73 74 def gradtoRegGrid(grid,domain,newx,newy,width,depth,dim): 75 import pylab as pl 76 cxy = grid.getFunctionSpace().getX() 77 gridT = grid.toListOfTuples()#(scalarastuple=False) 78 #cxy = Data(oldspacecoords, grid.getFunctionSpace()) 79 cX, cY = toXYTuple(cxy) 80 xi = np.linspace(0.0,width,newx) 81 yi = np.linspace(depth,0.0,newy) 82 83 gridT = np.array(gridT) 84 gridT = gridT[:,dim] 85 86 # grid the data. 87 88 zi = pl.matplotlib.mlab.griddata(cX,cY,gridT,xi,yi) 89 return xi,yi,zi