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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/layer_drawer_grid_fast.py@6118
Revision 6118, 3.4 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"""
2A layer drawer that draws the grid boxes of the data onto an image object.
3"""
4
5import logging
6import time
7
8import geoplot.utils as geoplot_utils
9from geoplot.layer_drawer_grid import LayerDrawerGrid
10from geoplot.grid_builder_lat_lon_subset import GridBuilderLatLonSubset
11
12log = logging.getLogger(__name__)
13
14class LayerDrawerGridFast(LayerDrawerGrid):
15    "Draw a lat/lon grid using subsampling and assuming that the bounds are paralell"
16   
17
18    def makeImage(self, xLimits=None, yLimits=None, width=800, height=600, dpi=200):
19        """
20        Creates a PIL image of the selected area of the layer.
21        """
22        st = time.time()
23
24        fig = self._getFigure(width, height, dpi)
25        figTime = time.time() 
26
27        axes = self._addAxes(fig)
28        axTime = time.time() 
29
30        # modified the _buildGrid call to pass it the xLimits and yLimits to be
31        # used in the subsetting.
32        grid = self._buildGrid(self.cdmsVar, width, height, xLimits, yLimits)
33        gridTime = time.time() 
34       
35        self._drawToAxes(axes, grid, xLimits, yLimits)
36        drawAxTime = time.time() 
37       
38        self._resetAxes(axes, xLimits, yLimits)
39        resetTime = time.time() 
40       
41        im = geoplot_utils.figureToImage(fig)
42        figToImTime = time.time() 
43       
44        log.debug("drawn layer in %.4fs : fig=%.2f ax=%.2f gd=%.2f draw=%.2f res=%.2f im=%.2f" % 
45                  (time.time() -st , figTime - st, axTime - figTime, gridTime - axTime, drawAxTime - gridTime, resetTime - drawAxTime, figToImTime - resetTime))
46       
47        return im
48
49    def _buildGrid(self, cdmsVar, width, height, xLimits, yLimits):
50        """
51        Builds a new grid object using the data found in the cdmsVar.
52        """
53       
54        gridBuilder = GridBuilderLatLonSubset(self.cdmsVar)
55       
56        grid = gridBuilder.buildGrid(width, height, xLimits, yLimits)
57               
58        return grid
59       
60    def _drawToAxes(self, axes, grid, xLimits, yLimits):
61        """
62        Creates a new grid object and draws it onto the axis.
63        """
64       
65        scheme = self._csBuilder.buildScheme(grid=grid)
66       
67        self._applyMask(grid, scheme, self.hideOutside)
68       
69        # draw the grid on the axis, not the basemap is None as it
70        # must be lat/lon
71        self._gridDrawer.draw(axes, grid,
72                              basemap=self._getBasemap(xLimits, yLimits), 
73                              limits=(xLimits, yLimits),
74                              norm=scheme.norm,
75                              cmap=self._getCmap(scheme),
76                              assumeBoundsParallel=True)
77               
78   
79    ### properties ###
80       
81    def __set_gridType(self, value):
82        if value != 'latlon':
83           
84            raise Exception(\
85             "For LayerDrawerGridFast the grid type must be latlon, not %s" 
86                % (value, ))
87           
88
89    def __get_gridType(self):
90        return 'latlon'
91   
92    gridType = property(__get_gridType, __set_gridType, None, None)
93       
94    def __set_projection(self, value):
95        if value != 'latlon':
96           
97            raise Exception(\
98             "For LayerDrawerGridFast the projection must be latlon, not %s" 
99                % (value, ))
100                   
101
102    def __get_projection(self):
103        return 'latlon'
104   
105    projection = property(__get_projection, __set_projection, None, None)
106   
Note: See TracBrowser for help on using the repository browser.