source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/csml_util.py @ 2677

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/csml_util.py@2677
Revision 2677, 5.4 KB checked in by domlowe, 12 years ago (diff)

get_csml_doc method updated to include exist access. Working with cache except cache does not respect security yet

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6csml access routines.
7
8@author: Stephen Pascoe
9"""
10from pylons import Response, c, g, cache, request, session
11import csml, cdms
12import os, string
13import zipfile
14import tempfile
15from ows_server.models import ndgObject,ndgRetrieve
16
17def get_csml_doc(fileoruri):
18        """
19        Gets a csml document from file or exist when passed a file name or uri         
20               
21        Note, access control is not implemented on file access, only on exist access.
22       
23        """
24        if string.find(fileoruri,'__NDG-A0__') == -1:
25            #it's a local file not an identifier
26            file=fileoruri
27            csml_dir = request.environ['paste.config']['app_conf']['csml_dir']
28            path = os.path.join(csml_dir, file)
29            if os.path.exists(path+'.csml'):
30                f = path+'.csml'
31            elif os.path.exists(path+'.xml'):
32                f = path +'.xml'
33            else:
34                raise ValueError("Cannot find CSML file %s" % file)
35           
36            d = csml.parser.Dataset()
37            d.parse(f)
38       
39        else:
40            #it's an NDG identifier, get the document from exist.
41            uri=fileoruri
42            uri='badc.nerc.ac.uk__NDG-A0__ReMryRVA'       
43            uriN=ndgObject.ndgObject(uri)
44            cf=request.environ['ndgConfig']
45            requestor=request.environ['REMOTE_ADDR']
46            if 'ndgSec' in session:
47                securityTokens=session['ndgSec']
48            else: securityTokens=None
49            status,x=ndgRetrieve.ndgRetrieve(
50                    uriN,cf,requestor=requestor,
51                    securityTokens=securityTokens)
52            d=csml.parser.Dataset()
53            d.parseElemTree(x.tree)                         
54        return d
55
56#this version of get_csml_doc can be deleted now if no problems found with new version:
57#def get_csml_doc(file):
58    #"""
59    #A trivial document retrieval function.
60
61    #This could be replaced with a proper csml server object that supports
62    #multiple stores (filesystem, exist) and cache's the results for
63    #performance.
64
65    #"""
66    #csml_dir = request.environ['paste.config']['app_conf']['csml_dir']
67    #path = os.path.join(csml_dir, file)
68    #if os.path.exists(path+'.csml'):
69        #f = path+'.csml'
70    #elif os.path.exists(path+'.xml'):
71        #f = path +'.xml'
72    #else:
73        #raise ValueError("Cannot find CSML file %s" % file)
74   
75    #d = csml.parser.Dataset()
76    #d.parse(f)
77     
78    #return d
79
80       
81def extractToNetCDF(feature, sel, publish=False):
82    """
83       performs the CSML subset and returns a filename of the netcdf extract
84       publish flag is used to indicate that the netcdf file should be made available to the webserver (for asynchronous delivery)
85    """
86
87    if publish:
88        #if publishing to download directory is required, do so and return publishable file name
89        #used e.g. in WCS when "STORE = true"
90        extract_dir=request.environ['paste.config']['app_conf']['publish_dir']
91    else:
92        extract_dir = request.environ['paste.config']['app_conf']['tmp_dir']
93   
94    # Subset the feature
95    (fd, filename) = tempfile.mkstemp('.nc', 'csml_wxs_', extract_dir); os.close(fd)
96    feature.subsetToGridSeries(ncname=os.path.basename(filename), outputdir=os.path.dirname(filename) ,**sel)
97
98    return filename
99       
100def extractToVariable(feature, sel):
101    """
102      calls extractToNetCDF to get the netcdf file, then returns a cdms variable from this file
103    """
104    #call extractToNetCDF:
105    filename = extractToNetCDF(feature, sel)
106    d = cdms.open(filename)
107    var = d(feature.name.CONTENT, squeeze=1)
108    # Work around for CSML bug
109    #var = d(feature.name.CONTENT, longitude=sel['longitude'], latitude=sel['latitude'],
110    #        squeeze=1)
111
112    d.close()
113    #os.remove(filename)
114    return var
115       
116class CsmlBundle(csml.API.csmlContainer.Container):
117    """
118    A quick stab at bundling CSML.
119
120    """
121    def __init__(self, context, **kwargs):
122        """
123        @param context: A directory path to store the CSML and NetCDF
124        """
125        self.context = context
126        kwargs['csmlpath'] = os.path.join(context, 'container.xml')
127        super(CsmlBundle, self).__init__(**kwargs)
128
129    def add(self, fpd):
130        """
131        Overrides the file storage descriptor so that the CSML points relative to the
132        bundle context
133
134        @warning: THIS IS A HACK!
135
136        """
137        (feature, path, desc) = fpd
138        if os.path.dirname(path) != self.context:
139            raise ValueError, "Extract not in bundle context"
140        desc.fileName.CONTENT = os.path.basename(path)
141
142        return super(CsmlBundle, self).add(fpd)
143
144    def join(self, *p):
145        return os.path.join(self.context, *p)
146
147    def makeBundle(self, file, bundleName=None):
148        """
149        Writes the bundle to a zipfile.
150
151        @param file: path or file-like object (see zipfile docs)
152
153        """
154        if bundleName is None:
155            bundleName = os.path.splitext(os.path.basename(file))[0]
156       
157        zf = zipfile.ZipFile(file, 'w')
158        self.getContents()
159        for f in self.containerContents:
160            zf.write(f, os.path.join(bundleName, os.path.basename(f)))
161        zf.close()
162
163        return file
Note: See TracBrowser for help on using the repository browser.