source: cows/trunk/cows/service/imps/csmlbackend/wcs_csmllayer.py @ 4769

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/service/imps/csmlbackend/wcs_csmllayer.py@4769
Revision 4769, 6.0 KB checked in by domlowe, 12 years ago (diff)

adding gml:timepositions to describe coverage response

Line 
1"""
2implementation of ILayerMapper, IwmsLayer, IwmsDimension, ILayerSlab interfaces, as defined in wms_iface.py & wxs_iface.py
3
4"""
5import os, string
6import csml
7try:
8    import cdms2 as cdms
9except:
10    import cdms
11from pylons import config, request, session
12from copy import copy
13
14import logging
15log = logging.getLogger(__name__)
16
17from cows.model.wcs import WcsDatasetSummary
18from cows.service.imps.csmlbackend.csmlcommon import CSMLLayerMapper, CSMLConnector, extractToNetCDF
19
20
21class CSMLwcsCoverageMapper(CSMLLayerMapper): 
22    """
23    Map keyword arguments to a collection of layers (coverages)
24    Supports the retrieval of coverages according to arbitrary
25    keyword/value pairs. 
26    """
27    def __init__(self):
28        super(CSMLwcsCoverageMapper, self).__init__()
29       
30   
31    def getInfo(self, feature):
32        ''' given a csml feature, return info about the layer/feature
33        @return:   title, abstract, units, crss '''
34
35        title, abstract = super(CSMLwcsCoverageMapper, self).getInfo(feature)
36        units=feature.getDomainUnits()
37        tmpunits=copy(units)
38        tmpunits.reverse()
39        domain = feature.getDomain()
40        tax= feature.getTimeAxis()
41        timepositions=domain[tax]
42        timelimits=[domain[tax][0],domain[tax][len(domain[tax])-1]] 
43        crs=feature.getNativeCRS()
44        crss=[self._crscat.getCRS(crs).twoD]
45        if 'EPSG:4326' in crss:
46            crss.append('CRS:84')
47            crss.append('WGS84')
48
49        return title, abstract, timepositions, timelimits, units, crss
50           
51    def getCoverageDescription(self):
52        pass
53       
54     
55    def map(self, **kwargs):
56        """
57        Given csml.parser.Dataset object list the names of
58        all layers available.
59       
60        @return: A mapping of layer names to ILayer implementations.
61        @raise ValueError: If no layers are available for these keywords.
62        """
63        fileoruri=kwargs['fileoruri']
64        if fileoruri in self.layermapcache.keys():
65            #we've accessed this layer map before, get it from the cache dictionary
66            return self.layermapcache[fileoruri]
67         
68        ds = self.connector.getCsmlDoc(fileoruri)
69        layermap={}
70        self._crscat=csml.csmllibs.csmlcrs.CRSCatalogue()
71        for feature in csml.csmllibs.csmlextra.listify(ds.featureCollection.featureMembers):
72            title, abstract, timepositions, timelimits, units, crss=self.getInfo(feature)
73            layermap[feature.id]=CSMLCoverage([title],[abstract], timepositions, timelimits, units, crss, feature)
74        if len(layermap) > 0:
75            self.layermapcache[fileoruri]=layermap
76            return layermap
77        else:
78            raise ValueError
79
80
81
82
83class CSMLCoverage(): #TODO: define ICoverage
84    """ represents a WCS Coverage. Implements ICoverage """
85   
86    def __init__(self, title, abstract, timepositions, timelimits, units, crss, feature):
87        self.title=title
88        self.abstract=abstract
89        self.description='TO DO - coverage description'
90        self.timePositions=timepositions
91        self.timeLimits=timelimits
92        self.units=units
93        self.crss=crss
94        self._feature=feature
95        self.id=feature.id
96        bb = self._feature.getCSMLBoundingBox().getBox()
97        #convert 0 - 360 to -180, 180 as per common WXS convention
98        if abs(bb[2]-bb[0]) >= 359 and abs(bb[2]-bb[0]) < 361:
99            bb[0], bb[2]=-180, 180
100        self.wgs84BBox = bb
101        self.featureInfoFormats = ['text/html']
102   
103    def getBBox(self, crs):
104        """
105        @return: A 4-typle of the bounding box in the given coordinate
106            reference system.
107        """
108        #TODO: make this generic
109        return self.wgs84BBox
110   
111    def getCvg(self, bbox, time=None, crs=None, response_crs=None):
112        """
113        Creates a subset of the layer in a particular CRS and set of
114        dimensions.
115        #TODO: this is all out of synch
116        @param crs: The coordinate reference system.
117        @param dimValues: A mapping of dimension names to dimension values
118            as specified in the IDimension.extent
119        @param renderOpts: A generic mapping object for passing rendering
120            options
121        @return: An object implementing ILayerSlab
122        #create netcdf for whole lat/lon for given dimValues, use to init slab
123        """
124
125        log.debug('WCS: getSubset(%s, %s, %s)' % (bbox, time, crs))
126        #unpack the Boundingbox.
127       
128       
129        ############################# from old WCS stack #################
130        boundingbox=bbox
131        lon=self._feature.getLongitudeAxis()
132        lat=self._feature.getLatitudeAxis()
133        t=self._feature.getTimeAxis()
134        if None in [lon, lat, t]:
135            #TODO need to return a suitable wcs error.
136            print 'warning, could not get correct axis info'
137            #best guess!
138            if t is None:
139                t='time'
140            if lon is None:
141                lon = 'longitude'
142            if lat is None:
143                lat = 'latitude'
144       
145        #create selection dictionary:
146        sel={}
147        sel[lat]=(boundingbox[1], boundingbox[3])
148        sel[lon]=(boundingbox[0], boundingbox[2])
149        if time is not None:
150            if  type(time) is unicode:
151                sel[t]=str(time)
152            else:
153                sel[t]=time
154        #z is the 4th axis (eg height or pressure).
155        #NOTE, need to decide whether bounding box is of form: x1,y1,z1,x2,y2,z2 or x1,y1,x2,y2,z1,z2
156        #currently the latter is implemented.
157           
158        if len(boundingbox)  == 6:
159            for ax in self._feature.getAxisLabels():
160                if ax not in [lat, lon, t]:
161                    #must be Z 
162                    z=str(ax)
163                    sel[z]=(boundingbox[4], boundingbox[5])
164           
165           
166        axisNames=self._feature.getAxisLabels()
167        ##################################################################
168       
169        filename = extractToNetCDF(self._feature, sel)
170        return filename
171       
Note: See TracBrowser for help on using the repository browser.