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

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

Configured a custom temporary directory for CSML extracts.

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
11
12import csml, cdms
13import os
14import zipfile
15import tempfile
16
17def get_csml_doc(file):
18    """
19    A trivial document retrieval function.
20
21    This could be replaced with a proper csml server object that supports
22    multiple stores (filesystem, exist) and cache's the results for
23    performance.
24
25    """
26    csml_dir = request.environ['paste.config']['app_conf']['csml_dir']
27    path = os.path.join(csml_dir, file)
28    if os.path.exists(path+'.csml'):
29        f = path+'.csml'
30    elif os.path.exists(path+'.xml'):
31        f = path +'.xml'
32    else:
33        raise ValueError("Cannot find CSML file %s" % file)
34   
35    d = csml.parser.Dataset()
36    d.parse(f)
37       
38    return d
39
40
41def extract(feature, sel):
42    # Get temporary extract dir
43    extract_dir = request.environ['paste.config']['app_conf']['tmp_dir']
44   
45    # Subset the feature
46    (fd, filename) = tempfile.mkstemp('.nc', 'csml_wms_', extract_dir); os.close(fd)
47    feature.subsetToGridSeries(ncname=os.path.basename(filename),
48                               outputdir=os.path.dirname(filename),
49                               time=sel['time'] # work around for CSML bug
50                               #**sel
51                               )
52    d = cdms.open(filename)
53    #var = d(feature.name.CONTENT, squeeze=1)
54    # Work around for CSML bug
55    var = d(feature.name.CONTENT, longitude=sel['longitude'], latitude=sel['latitude'],
56            squeeze=1)
57
58    d.close()
59    os.remove(filename)
60
61    return var
62       
63class CsmlBundle(csml.API.csmlContainer.Container):
64    """
65    A quick stab at bundling CSML.
66
67    """
68    def __init__(self, context, **kwargs):
69        """
70        @param context: A directory path to store the CSML and NetCDF
71        """
72        self.context = context
73        kwargs['csmlpath'] = os.path.join(context, 'container.xml')
74        super(CsmlBundle, self).__init__(**kwargs)
75
76    def add(self, fpd):
77        """
78        Overrides the file storage descriptor so that the CSML points relative to the
79        bundle context
80
81        @warning: THIS IS A HACK!
82
83        """
84        (feature, path, desc) = fpd
85        if os.path.dirname(path) != self.context:
86            raise ValueError, "Extract not in bundle context"
87        desc.fileName.CONTENT = os.path.basename(path)
88
89        return super(CsmlBundle, self).add(fpd)
90
91    def join(self, *p):
92        return os.path.join(self.context, *p)
93
94    def makeBundle(self, file, bundleName=None):
95        """
96        Writes the bundle to a zipfile.
97
98        @param file: path or file-like object (see zipfile docs)
99
100        """
101        if bundleName is None:
102            bundleName = os.path.splitext(os.path.basename(file))[0]
103       
104        zf = zipfile.ZipFile(file, 'w')
105        self.getContents()
106        for f in self.containerContents:
107            zf.write(f, os.path.join(bundleName, os.path.basename(f)))
108        zf.close()
109
110        return file
Note: See TracBrowser for help on using the repository browser.