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

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

Added a module to fix the import Image / from PIL import Image problem.

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