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

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

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

Line 
1"""
2grid_drawer.py
3============
4
5A GridDrawere knows how to draw a lat-lon grid onto a axes so that it will match
6with a basemap drawing on the same axis.
7
8"""
9#python modules
10import logging
11import operator
12from math import *
13
14#third party modules
15import matplotlib.colors
16import matplotlib.cm
17import matplotlib.collections
18import numpy as N
19
20#internal modules
21from geoplot.array_util import *
22import geoplot.config as geoplot_config
23
24config = geoplot_config.getConfig()
25
26#setup the logging
27log = logging.getLogger(__name__)
28
29class ContourDrawer(object):
30    """
31    Responsible for knowing how to draw contours onto an axis, the data
32    for the contours is taken form a grid object
33    """
34
35    def __init__(self, numContourLines=10, contourLabelInterval=1):
36        """
37        Constructs a ContourDrawer object             
38        """
39
40        self.numContourLines = numContourLines
41        self.contourLabelInterval = contourLabelInterval
42        self._contourFont = config['Fonts']['ContourLines']
43       
44    def draw(self, axes, grid, basemap=None, normalize=None, cmap=None, fontSize='medium'):
45        """
46        Draws contour lines from the grid's data on the axis.
47       
48        @param axes: the axes the grid will be drawn on
49        @type axes: matplotlib.axes
50        @param grid: the grid to be drawn
51        @type grid: geoplot.grid
52        @keyword basemap: the basemap instance to scale the grid values to be
53            drawn on the axis.
54        """
55
56        if cmap == None:
57            cmap = matplotlib.cm.get_cmap()
58            cmap.set_bad("w")
59           
60        #transform the grid data to the correct projection
61        grid_mapUnits = grid.transform(basemap)
62               
63        self._drawContours(axes, cmap, grid_mapUnits, normalize, fontSize)
64       
65    def _drawContours(self, axes, cmap, grid_mapUnits, normalize, fontSize):
66        """
67        Draws the contour lines onto the axes.
68        """
69
70        #check the values aren't all masked
71        valuesFound = not reduce(operator.and_, grid_mapUnits.values.mask.flat)
72       
73        if valuesFound == True:
74
75            if normalize == None :
76                normalize = matplotlib.colors.Normalize(0, 1)
77                       
78            levels = N.linspace(normalize.vmin, normalize.vmax, self.numContourLines)
79           
80            extent=(grid_mapUnits.boundsX.min(), grid_mapUnits.boundsX.max(),
81                    grid_mapUnits.boundsY.min(), grid_mapUnits.boundsY.max())
82           
83            CS = axes.contour(grid_mapUnits.midpointsX, 
84                              grid_mapUnits.midpointsY, 
85                              grid_mapUnits.values,
86                              levels,
87                 cmap=cmap,
88                 origin='lower',
89                 linewidths=1,
90                 norm=normalize,
91                 extend='neither',
92                 )
93           
94            labelDictionary = self._contourFont.getDict(fontSize)
95           
96            axes.clabel(CS,levels[0::self.contourLabelInterval], fontsize=labelDictionary['size'])
97           
98            axes.set_aspect('auto')
99           
Note: See TracBrowser for help on using the repository browser.