source: cows/trunk/cows/service/imps/csmlbackend/csmlcommon.py @ 4785

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

Various comments on code structure. Grep for !TODO and !FIXME or see the diff.

Also an incomplete module for testing (iface_test_lib.py).

Line 
1""" Common CSML code shared between WMS, WFS and WCS backends
2@Author: Dominic Lowe
3"""
4
5import os, string
6import logging
7import csml
8import tempfile
9#!FIXME: modules in the service package shouldn't depend on pylons.
10from pylons import config #config must have tmpfilebuffer and csmlstore values
11import ConfigParser
12log = logging.getLogger(__name__)
13from cows.bbox_util import union
14 
15try:
16    from ndgUtils import ndgObject, ndgRetrieve
17except:
18    log.warning("ndgUtils library could not be loaded, files in the eXist database won't be available, although you should still be able to access files from the csmlstore directory referenced in the ini file.")
19
20from cows.service.wxs_iface import ILayerMapper
21
22def extractToNetCDF(feature, sel, publish=False):
23    """
24       performs the CSML subset and returns a filename of the netcdf extract
25       publish flag is used to indicate that the netcdf file should be made available to the webserver (for asynchronous delivery)
26    """
27
28    if publish:
29        #if publishing to download directory is required, do so and return publishable file name
30        #used e.g. in WCS when "STORE = true"
31        extract_dir=config['publish_dir']
32    else:
33        extract_dir = config['tmpfilebuffer']
34         
35    # Subset the feature
36    (fd, filename) = tempfile.mkstemp('.nc', 'csml_wxs_', extract_dir); os.close(fd)
37    if type(feature) is csml.parser.GridSeriesFeature:
38        feature.subsetToGridSeries(ncname=os.path.basename(filename), outputdir=os.path.dirname(filename) ,**sel)
39    elif type(feature) is csml.parser.TrajectoryFeature:
40        feature.subsetToTrajectory(ncname=os.path.basename(filename), outputdir=os.path.dirname(filename) ,**sel)
41    elif type(feature) is csml.parser.PointSeriesFeature:
42        del sel['longitude'] #delete dummy values
43        del sel['latitude'] #delete dummy values
44        feature.subsetToPointSeries(ncname=os.path.basename(filename), outputdir=os.path.dirname(filename) ,**sel)
45    elif type(feature) is csml.parser.RaggedSectionFeature:
46        del sel['longitude'] #delete dummy values
47        del sel['latitude'] #delete dummy values
48        feature.subsetByTime(ncname=os.path.basename(filename), outputdir=os.path.dirname(filename) ,**sel)
49    return filename
50
51
52class CSMLLayerMapper(ILayerMapper):
53    """
54    Map keyword arguments to a collection of layers.
55    Supports the retrieval of sets of layers according to arbitrary
56    keyword/value pairs.
57    Implements  ILayerMapper
58   
59    """
60    def __init__(self):
61        self.layermapcache={}
62        self.connector=CSMLConnector()
63   
64    #!TODO: Should be _getInfo() as proposed in wms_csmllayer.  Should move to wfs_csmllayer as
65    #    this is the only place where it isn't overridden.
66    def getInfo(self, feature):
67        ''' given a csml feature, return basic info about the layer/feature/coverage
68        @return:   title, abstract'''
69
70        try:
71            title=feature.name.CONTENT
72        except:
73            title=''
74        try:
75            abstract=feature.description.CONTENT
76        except:
77            abstract=title
78       
79        return title, abstract
80
81#!TODO: What's the point of this class?  It's just a container for a function.
82class CSMLConnector(object):
83    """ Contains common methods for CSML backend used across all WXS services """   
84   
85    def __init__(self):
86        pass
87   
88    def getCsmlDoc(self, fileoruri):
89        """
90        Gets a csml document from file or exist when passed a file name or uri         
91               
92        Note, access control is not implemented on file access, only on exist access.
93       
94        """
95        if string.find(fileoruri,'__NDG-A0__') == -1:
96            #it's a local file not an identifier, read from local csmlstore
97            file=fileoruri
98            csml_dir = config['csmlstore']
99            path = os.path.join(csml_dir, file)
100            if os.path.exists(path+'.csml'):
101                f = path+'.csml'
102            elif os.path.exists(path+'.xml'):
103                f = path +'.xml'
104            else:
105                raise ValueError("Cannot find CSML file %s" % file)
106           
107            d = csml.parser.Dataset()
108            d.parse(f)
109       
110        else:
111            #it's an NDG identifier, get the document from exist.
112            uri=fileoruri
113            uriN=ndgObject(uri)
114            conflocation=config['ndgconfig']
115            cf=ConfigParser.ConfigParser()
116            cf.read(conflocation)
117            status,x=ndgRetrieve(uriN, cf, discovery=0)
118            d=csml.parser.Dataset()
119            d.parse(x)                         
120        return d
121   
Note: See TracBrowser for help on using the repository browser.