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

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

unicode problem in ndgObject wrapped with str

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            print type(uri)
43            #uri='badc.nerc.ac.uk__NDG-A0__ReMryRVA'
44            uri=str(uri)
45            uriN=ndgObject.ndgObject(uri)
46            cf=request.environ['ndgConfig']
47            requestor=request.environ['REMOTE_ADDR']
48            if 'ndgSec' in session:
49                securityTokens=session['ndgSec']
50            else: securityTokens=None
51            status,x=ndgRetrieve.ndgRetrieve(
52                    uriN,cf,requestor=requestor,
53                    securityTokens=securityTokens)
54            d=csml.parser.Dataset()
55            #print x
56            d.parseElemTree(x.tree)                         
57        return d
58
59#this version of get_csml_doc can be deleted now if no problems found with new version:
60#def get_csml_doc(file):
61    #"""
62    #A trivial document retrieval function.
63
64    #This could be replaced with a proper csml server object that supports
65    #multiple stores (filesystem, exist) and cache's the results for
66    #performance.
67
68    #"""
69    #csml_dir = request.environ['paste.config']['app_conf']['csml_dir']
70    #path = os.path.join(csml_dir, file)
71    #if os.path.exists(path+'.csml'):
72        #f = path+'.csml'
73    #elif os.path.exists(path+'.xml'):
74        #f = path +'.xml'
75    #else:
76        #raise ValueError("Cannot find CSML file %s" % file)
77   
78    #d = csml.parser.Dataset()
79    #d.parse(f)
80     
81    #return d
82
83       
84def extractToNetCDF(feature, sel, publish=False):
85    """
86       performs the CSML subset and returns a filename of the netcdf extract
87       publish flag is used to indicate that the netcdf file should be made available to the webserver (for asynchronous delivery)
88    """
89
90    if publish:
91        #if publishing to download directory is required, do so and return publishable file name
92        #used e.g. in WCS when "STORE = true"
93        extract_dir=request.environ['paste.config']['app_conf']['publish_dir']
94    else:
95        extract_dir = request.environ['paste.config']['app_conf']['tmp_dir']
96   
97    # Subset the feature
98    (fd, filename) = tempfile.mkstemp('.nc', 'csml_wxs_', extract_dir); os.close(fd)
99    feature.subsetToGridSeries(ncname=os.path.basename(filename), outputdir=os.path.dirname(filename) ,**sel)
100
101    return filename
102       
103def extractToVariable(feature, sel):
104    """
105      calls extractToNetCDF to get the netcdf file, then returns a cdms variable from this file
106    """
107    #call extractToNetCDF:
108    filename = extractToNetCDF(feature, sel)
109    d = cdms.open(filename)
110    var = d(feature.name.CONTENT, squeeze=1)
111    # Work around for CSML bug
112    #var = d(feature.name.CONTENT, longitude=sel['longitude'], latitude=sel['latitude'],
113    #        squeeze=1)
114
115    d.close()
116    #os.remove(filename)
117    return var
118       
119class CsmlBundle(csml.API.csmlContainer.Container):
120    """
121    A quick stab at bundling CSML.
122
123    """
124    def __init__(self, context, **kwargs):
125        """
126        @param context: A directory path to store the CSML and NetCDF
127        """
128        self.context = context
129        kwargs['csmlpath'] = os.path.join(context, 'container.xml')
130        super(CsmlBundle, self).__init__(**kwargs)
131
132    def add(self, fpd):
133        """
134        Overrides the file storage descriptor so that the CSML points relative to the
135        bundle context
136
137        @warning: THIS IS A HACK!
138
139        """
140        (feature, path, desc) = fpd
141        if os.path.dirname(path) != self.context:
142            raise ValueError, "Extract not in bundle context"
143        desc.fileName.CONTENT = os.path.basename(path)
144
145        return super(CsmlBundle, self).add(fpd)
146
147    def join(self, *p):
148        return os.path.join(self.context, *p)
149
150    def makeBundle(self, file, bundleName=None):
151        """
152        Writes the bundle to a zipfile.
153
154        @param file: path or file-like object (see zipfile docs)
155
156        """
157        if bundleName is None:
158            bundleName = os.path.splitext(os.path.basename(file))[0]
159       
160        zf = zipfile.ZipFile(file, 'w')
161        self.getContents()
162        for f in self.containerContents:
163            zf.write(f, os.path.join(bundleName, os.path.basename(f)))
164        zf.close()
165
166        return file
Note: See TracBrowser for help on using the repository browser.