source: DPPP/ukcip02_server/trunk/ukcip_server/ukcip_server/model/pywms/impl.py @ 3570

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/ukcip02_server/trunk/ukcip_server/ukcip_server/model/pywms/impl.py@3570
Revision 3570, 2.8 KB checked in by spascoe, 14 years ago (diff)

Some quick fixes to make pywms align the grid properly. WARNING: This isn't generic code and needs revising.

Line 
1"""
2Implements the WMS interfaces for simple CDMS variables
3
4This implementation is for backward compatibility between the old interface and
5the new.  Only one CRS per layer is supported.
6
7"""
8from ows_common.service.wms_iface import ILayer, ILayerSlab, IDimension
9from wms_cdms import SimpleCdmsLayer, CdmsTimeDimension, CdmsGrid
10import cdtime
11from render_imp import RGBARenderer
12from matplotlib.cm import get_cmap
13
14import logging
15log = logging.getLogger(__name__)
16
17class CdmsLayer(ILayer):
18    """
19    The ILayer interface has superceded model.Layer and therefore CdmsLayer
20    does not inherit from model.Layer or SimpleCdmsLayer.  Instead encapsulation
21    is used.
22
23    """
24    def __init__(self, cdmsVar, GridClass=CdmsGrid, minValue=None, maxValue=None):
25        try:
26            self.title = cdmsVar.long_name
27        except AttributeError:
28            self.title = cdmsVar.id
29
30        self._layer = SimpleCdmsLayer(cdmsVar, GridClass=GridClass)
31        self.abstract = None
32        self.crss = [GridClass.crs]
33        self.units = self._layer.units
34        self.minValue = minValue
35        self.maxValue = maxValue
36
37        dims = {}
38        for dimName, dim in self._layer.dimensions.items():
39            dims[dimName] = Dimension(dim)
40        self.dimensions = dims
41
42       
43
44
45    def getBBox(self, crs):
46        # Get BBOX from pywms layer
47        var = self._layer.var
48        xAxis = var.getAxis(var.getAxisIndex('eastings'))
49        yAxis = var.getAxis(var.getAxisIndex('northings'))
50        #!NOTE order specific code
51        return (xAxis[0], yAxis[-1], xAxis[-1], yAxis[0])
52
53    def getSlab(self, crs, dimValues=None, renderOpts={}):
54        if crs != self.crss[0]:
55            raise ValueError("CRS %s not supported on this layer" % crs)
56       
57        return CdmsLayerSlab(crs, dimValues, renderOpts, self)
58
59    def getCacheKey(self, crs, dimValues=None, renderOpts={}):
60        # Don't support caching
61        return None
62
63class CdmsLayerSlab(ILayerSlab):
64    def __init__(self, crs, dimValues, renderOpts, layer):
65        self.crs = crs
66        self.dimValues = dimValues
67        self.renderOpts = renderOpts
68        self.layer = layer
69        self.bbox = layer.getBBox(crs)
70
71        self._renderer = RGBARenderer(self.layer.minValue, self.layer.maxValue)
72
73    def getImage(self, bbox, width, height):
74        grid = self.layer._layer.selectGrid(bbox, self.dimValues)
75        cmap = get_cmap(self.renderOpts.get('cmap', 'jet'))
76        img = self._renderer.renderGrid(grid, bbox, width, height, cmap)
77        img.save('dump.png')
78
79        return img
80
81    def getCacheKey(self):
82        raise NotImplementedError
83   
84
85class Dimension(IDimension):
86    """
87    Wrapper to fix some interface differences.
88
89    """
90    def __init__(self, pywmsDim):
91        self.units = pywmsDim.units
92        self.extent = pywmsDim.extent.split(',')
Note: See TracBrowser for help on using the repository browser.