source: qesdi/wms_ddc_vis/trunk/lib/wms_ddc_vis/model/ddc_wms_layer_slab.py @ 5403

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/wms_ddc_vis/trunk/lib/wms_ddc_vis/model/ddc_wms_layer_slab.py@5403
Revision 5403, 3.6 KB checked in by pnorton, 12 years ago (diff)

Moved QESDI tree from DCIP repository
 http://proj.badc.rl.ac.uk/svn/dcip/qesdi@3900.

Line 
1
2import logging
3
4from matplotlib import cm
5from cows.service.wms_iface import IwmsLayerSlab
6from cows.service.imps.csmlbackend.config import config
7
8from wms_ddc_vis.model.rgba_renderer import RGBARenderer
9from wms_ddc_vis.model.geoplot_slab_renderer import GeoplotSlabRenderer
10from wms_ddc_vis.model.grid import Grid
11import thread
12from geoplot.layer_drawer import LayerDrawer
13from geoplot.layer_drawer_contour import LayerDrawerContour
14
15log = logging.getLogger(__name__)
16
17class DDCWmsLayerSlab(IwmsLayerSlab):
18    """
19    Implements LayerSlab
20    Represents a particular horizontal slice of a WMS layer.
21
22    ILayerSlab objects are designed to be convenient to cache.
23    They should be pickleable to enable memcached support in the future.
24
25    @ivar layer: The source ILayer instance.
26    @ivar crs: The coordinate reference system.
27    @ivar dimValues: A mapping of dimension values of this view.
28    @ivar renderOpts: The renderOpts used to create this view.
29    @ivar bbox: The bounding box as a 4-tuple.
30    """
31
32    def __init__(self, netcdf, layer, crs, dimValues, renderOpts, bbox):
33       
34        log.debug("Creating slab: netcdf = %s, layer = %s, crs = %s, dimValues = %s, renderOpts = %s, bbox = %s" % (netcdf, layer, crs, dimValues, renderOpts, bbox))
35
36        self._netcdf=netcdf
37        self.layer = layer
38        self.crs = crs
39        self.dimValues = dimValues
40        self.renderOpts=renderOpts
41        self.bbox=bbox
42       
43        #set colour map for ALL images from this slab
44        v=self._netcdf(layer.title)
45        tvar=v(squeeze=1)
46        #get the min and max values to use for the colourmapping.
47        #change the fill values to ensure they aren't picked up as false max/mins.
48        tvar.missing_value=999999999
49        value=tvar.getValue()
50        self.minval=min(min(l) for l in value)
51        tvar.missing_value=-0.000000001
52        value=tvar.getValue()
53        self.maxval=max(max(l) for l in value)
54       
55        self.variable =  self._netcdf(self.layer.title, squeeze=1) 
56       
57       
58        cmap = cm.get_cmap(config['colourmap'])
59        cmapRange=(self.variable.min(), self.variable.max())
60       
61        self.ld = LayerDrawer(cmap=cmap, showGridLines=False, cmapRange=cmapRange)
62       
63        self.cd = LayerDrawerContour(cmap=cmap, cmapRange=cmapRange)
64
65       
66    def getImage(self, bbox, width, height):
67        """
68        Create an image of a sub-bbox of a given size.
69
70        @ivar bbox: A bbox 4-tuple.
71        @ivar width: width in pixels.` 
72        @ivar height: height in pixels.
73        @return: A PIL Image object.
74
75        """
76
77        log.debug('getImage(%s, %s, %s) #%s' % (bbox, width, height, thread.get_ident()))
78       
79        import time
80        xLimits = (bbox[0], bbox[2])
81        yLimits = (bbox[1], bbox[3])
82                       
83        st = time.time()
84        im = self.ld.makeImage(self.variable, xLimits, yLimits, width, height)
85       
86#        im = self.cd.makeImage(self.variable, xLimits, yLimits, width, height)
87       
88        log.debug("generated image in %.2fs" % (time.time() - st,))
89       
90
91        im.save('/tmp/out.png')
92       
93        return im
94
95#        import time
96#        st = time.time()
97#
98#        cmap = cm.get_cmap(config['colourmap'])
99#        grid=Grid(self.layer, self._netcdf, bbox, width, height)
100#
101#        minval=self.minval
102#        maxval=self.maxval
103#
104#        renderer=GeoplotSlabRenderer(minval, maxval)         
105#       
106#        im = renderer.renderGrid(grid, bbox, width, height, cmap)
107#        log.debug(" rendered grid in %.2fs" % (time.time() - st,))
108#        im.save('/tmp/raw1.png')
109#        return im
Note: See TracBrowser for help on using the repository browser.