source: qesdi/geoplot/trunk/lib/geoplot/layer_drawer_grid.py @ 5699

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

Extended geoplot so it can now create a discrete colour bar with named labels instead of a continuouse one.

I've also modified the layer drawing code to fix a transparency bug and to make more use of the new colourbar features.

Line 
1"""
2An object to draw just the layer (map + grid) from the plot,
3can draw to a file, a string or create an Image object.
4"""
5
6
7import logging
8import Image
9import StringIO
10import thread
11
12import numpy as N
13
14from matplotlib.patches import Rectangle
15from matplotlib.figure import Figure
16from matplotlib.backends.backend_agg import FigureCanvasAgg
17from matplotlib.lines import Line2D
18
19from geoplot.grid_builder_lat_lon import GridBuilderLatLon
20from geoplot.grid_builder_national import GridBuilderNational
21from geoplot.grid_builder_rotated import GridBuilderRotated
22
23from geoplot.map_lat_lon import MapLatLon
24
25from geoplot.grid_drawer import GridDrawer
26import geoplot.utils as geoplot_utils
27
28from geoplot.layer_drawer import LayerDrawerBase
29from geoplot.colour_bar import ColourBar
30
31from matplotlib.colors import Normalize
32
33log = logging.getLogger(__name__)
34
35VALID_GRID_TYPES = ['latlon', 'national', 'rotated']
36
37class LayerDrawerGrid(LayerDrawerBase):
38    "Draw only the layer section of the plot"
39   
40    def __init__(self, cdmsVar, gridType='latlon', showGridLines=False, cmap=None, cmapRange=(None,None), outline=False,
41                 transparent=False, 
42                 intervalColourbar=False, 
43                 intervalNames=None):
44       
45        log.debug("transparent = %s" % (transparent,))
46        self._gridDrawer = GridDrawer()
47       
48        log.debug("cmapRange = %s" % (cmapRange,))
49       
50        if cmapRange[0] > cmapRange[1]:
51            log.warning("cmapRange[0] > cmapRange[1], swapping values")
52            cmapRange = (cmapRange[1], cmapRange[0])       
53       
54        self.cdmsVar = cdmsVar
55        self.showGridLines = showGridLines
56        self.outline = outline
57       
58        LayerDrawerBase.__init__(self, gridType=gridType, transparent=transparent, cmap=cmap, cmapRange=cmapRange)
59       
60    def _drawToAxes(self, axes, xLimits, yLimits):
61        """
62        Creates a new grid object and draws it onto the axis.
63        """
64        #create a new grid, this grid will be in terms of lat/lon
65        grid = self._buildGrid(self.cdmsVar, xLimits, yLimits)
66       
67        # if the projection is latlon then there is no need to transform
68        # the data on the grid.
69        if self.projection == 'latlon':
70            basemap = None
71        else:
72            map = self._getMap(xLimits, yLimits)
73            basemap = map.basemap       
74       
75        norm = self._cb.getNormalize(grid=grid)
76        cmap = self._cb.getColourMap(grid=grid)
77
78        #set the bad colour to transparent
79        cmap.set_bad('w', 0.0)
80       
81        #draw the grid on the axis
82        self._gridDrawer.draw(axes, grid,
83                              basemap=basemap, 
84                              limits=(xLimits, yLimits),
85                              norm=norm,
86                              cmap=cmap)
87                             
88   
89    ### properties ###
90                       
91    def __set_showGridLines(self, value):
92        self._gridDrawer.showGridLines = value
93   
94    def __get_showGridLines(self):
95        return self._gridDrawer.showGridLines
96   
97    showGridLines = property(__get_showGridLines, __set_showGridLines) 
98   
99    def __set_outline(self, value):
100        self._gridDrawer.outline = value
101       
102    def __get_outline(self):
103        return self._gridDrawer.outline
104   
105    outline = property(__get_outline, __set_outline)
106 
Note: See TracBrowser for help on using the repository browser.