source: qesdi/geoplot/trunk/lib/geoplot/grid.py @ 5710

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/grid.py@5710
Revision 5710, 4.0 KB checked in by pnorton, 11 years ago (diff)

Cleaned up a the import statements for the geoplot modules, also fixed a couple of the tests.

Line 
1"""
2grid.py
3============
4
5A simple class that represents a latitude-longitude grid with ascociated values.
6
7"""
8#python modules
9import logging
10import numpy as N
11import sets
12
13#third party modules
14
15#internal modules
16import geoplot.utils as utils
17#setup the logging
18log = logging.getLogger(__name__)
19
20class Grid(object):
21    """
22    Represents a grid with ascociated values and midpoints
23    """
24
25    def __init__(self, boundsX, boundsY, midpointsX, midpointsY, values):
26        """
27        Constructs a new Grid object
28        """
29        self.boundsX = boundsX
30        self.boundsY = boundsY
31        self.midpointsX = midpointsX
32        self.midpointsY = midpointsY
33        self.values = values
34        self._uniqueValues = None
35       
36    def transform(self, transfrom):
37        "Creates a new grid with transformed bounds and midpoints but the same data"
38       
39        if transfrom == None:
40            return Grid(self.boundsX.copy(), self.boundsY.copy(), self.midpointsX.copy(), self.midpointsY.copy(), self.values.copy())
41       
42        else:
43            transformedBoundsX, transfromedBoundsY =  transfrom(self.boundsX, self.boundsY)
44            transformedMidpointsX, transformedMidpointsY = transfrom(self.midpointsX, self.midpointsY)
45           
46            return Grid(transformedBoundsX, transfromedBoundsY,
47                        transformedMidpointsX, transformedMidpointsY,
48                        self.values.copy())
49   
50    def getUniqueValues(self, minVal=None, maxVal=None):
51        """
52        Returns a sorted numpy array of the unique non-masked values found in the
53        .values array. If minVal and maxVal are both specified then only the unique
54        values between these bounds (inclusive) will be returned.
55        """
56        uniqueVals = list(sets.Set(self.values.compressed().tolist()))
57        uniqueVals.sort()
58       
59        if minVal != None and maxVal != None:
60            uniqueVals = filter(lambda x: minVal <= x <= maxVal, uniqueVals)
61           
62        return N.array(uniqueVals)
63   
64    def getUniqueValueBounds(self, minVal=None, maxVal=None):
65        """
66        Returns an array of bounds for the unique values found in the .values array.
67        The first and last bounds are interpolated using the first two and
68        the last two data points but the rest of the bounds are calculated.
69        """
70        return utils.getBounds(self.getUniqueValues(minVal, maxVal))
71           
72   
73if __name__ == '__main__':
74
75    import log_util
76    log_util.setupGeoplotConsoleHandler(log)
77
78    xLimits=(-13.0, 6.2) ; yLimits=(47.0, 61.0)
79
80    from numpy import meshgrid
81    from cdms_utils.cdms_compat import MA
82
83    x = [-6,-4,-2, 0, 2]
84    xlim = 4
85    y = [54,55,56]
86    ylim = 2
87    X,Y = meshgrid(x,y)
88
89    Z=[[0]*xlim for i in range(ylim)]
90    for i in range(0 , xlim*ylim):
91        vX= i%xlim
92        vY= (i - i%xlim) / xlim
93        Z[vY][vX] = i
94    #Z = N.array(Z)
95    Z = MA.masked_values(Z, 2)
96    grid = Grid(X,Y,None, None, Z)
97
98    from geoplot.mpl_imports import basemap
99    from matplotlib.backends.backend_agg import FigureCanvasAgg
100    from matplotlib.figure import Figure
101    from matplotlib import cm
102
103
104    figsize=(600 / 80, 800 / 80)
105    fig = Figure(figsize=figsize, dpi=80)
106    axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
107
108
109    bm = basemap.Basemap(llcrnrlon=xLimits[0],
110                         llcrnrlat=yLimits[0],
111                         urcrnrlon=xLimits[1],
112                         urcrnrlat=yLimits[1],
113                         resolution='h',
114                         suppress_ticks=False)
115    cmap = cm.jet
116    cmap.set_bad("w")
117
118    sm = bm.pcolormesh(grid.boundsX, grid.boundsY, grid.values, cmap=cmap, ax=axes, edgecolors='k')
119
120    bm.drawcoastlines(ax = axes)
121    bm.drawrivers(ax = axes, color='b')
122    bm.drawmeridians([-10,-5,0,5,10], ax=axes, dashes=(None,None))
123    bm.drawparallels([50,55,60], ax=axes, dashes=(None,None))
124
125    canvas = FigureCanvasAgg(fig)
126    filename = "out.png"
127    canvas.print_figure(filename)
128    log.debug("Wrote " + filename)
Note: See TracBrowser for help on using the repository browser.