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

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

Fixed lots of small bugs and tidied up the layer drawer code a bit.

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
23from geoplot.colour_scheme import COLOUR_SCHEME_SCALE
24
25
26config = geoplot_config.getConfig()
27
28#setup the logging
29log = logging.getLogger(__name__)
30
31class ContourDrawer(object):
32    """
33    Responsible for knowing how to draw contours onto an axis, the data
34    for the contours is taken form a grid object
35    """
36
37    def __init__(self, numContourLines=10, contourLabelInterval=1, 
38                 contourLineWidth=0.5,
39                 contourLabelFormat="%.2f"):
40        """
41        Constructs a ContourDrawer object             
42        """
43
44        self.numContourLines = numContourLines
45        self.contourLabelInterval = contourLabelInterval
46        self.contourLineWidth = contourLineWidth
47        self.contourLabelFormat = contourLabelFormat
48        self._contourFont = config['Fonts']['ContourLines']
49       
50    def draw(self, axes, grid, scheme, basemap=None, fontSize='medium'):
51        """
52        Draws contour lines from the grid's data on the axis.
53       
54        @param axes: the axes the grid will be drawn on
55        @type axes: matplotlib.axes
56        @param grid: the grid to be drawn
57        @type grid: geoplot.grid
58        @keyword basemap: the basemap instance to scale the grid values to be
59            drawn on the axis.
60        """
61           
62        #transform the grid data to the correct projection
63        grid_mapUnits = grid.transform(basemap)
64               
65        self._drawContours(axes, scheme, grid_mapUnits, fontSize)
66       
67    def _drawContours(self, axes, scheme, grid_mapUnits, fontSize):
68        """
69        Draws the contour lines onto the axes.
70        """
71
72        #check the values aren't all masked
73        valuesFound = not reduce(operator.and_, grid_mapUnits.values.mask.flat)
74       
75        if valuesFound == True:
76           
77#            if scheme.scale == COLOUR_SCHEME_SCALE.LOG:
78#
79#                #lev_exp = np.arange(np.floor(np.log10(z.min())-1),
80#                #                       np.ceil(np.log10(z.max())+1))
81#                #levs = np.power(10, lev_exp)
82#                lmin = N.log10(scheme.norm.vmin)
83#                lmax = N.log10(scheme.norm.vmax)
84#                levels = N.logspace(lmin, lmax,  self.numContourLines)
85#                labelFormat = "%.1E"
86#            else:
87#                levels = N.linspace(scheme.norm.vmin, scheme.norm.vmax, self.numContourLines)
88#                labelFormat = "%1.3f"
89           
90            levels = scheme.intervals.bounds
91            # make sure the upper contour line is drawn if it is the highest
92            # value in Z
93            if levels[-1] == grid_mapUnits.values.max():
94                levels[-1] = levels[-1] * 0.999999
95           
96            CS = axes.contour(grid_mapUnits.midpointsX, 
97                              grid_mapUnits.midpointsY, 
98                              grid_mapUnits.values,
99                              levels,
100                 cmap=scheme.cmap,
101                 origin='lower',
102                 linewidths=self.contourLineWidth,
103                 norm=scheme.norm,
104                 extend='neither',
105                 )
106           
107            labelDictionary = self._contourFont.getDict(fontSize)
108           
109            axes.clabel(CS,
110                        levels[0::self.contourLabelInterval], 
111                        fontsize=labelDictionary['size'],
112                        fmt=self.contourLabelFormat)
113           
114            axes.set_aspect('auto')
115           
Note: See TracBrowser for help on using the repository browser.