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

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

Merging WCS branch back into cows trunk

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