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

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

WCS working with store parameter for asynchronous delivery

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 extractToNetCDF(feature, sel, publish=False):
42    """
43       performs the CSML subset and returns a filename of the netcdf extract
44       publish flag is used to indicate that the netcdf file should be made available to the webserver (for asynchronous delivery)
45    """
46
47    if publish:
48        #if publishing to download directory is required, do so and return publishable file name
49        #used e.g. in WCS when "STORE = true"
50        publish_dir=request.environ['paste.config']['app_conf']['publish_dir']
51    else:
52        extract_dir = request.environ['paste.config']['app_conf']['tmp_dir']
53   
54    # Subset the feature
55    (fd, filename) = tempfile.mkstemp('.nc', 'csml_wxs_', extract_dir); os.close(fd)
56    feature.subsetToGridSeries(ncname=os.path.basename(filename),
57                               outputdir=os.path.dirname(filename) ,**sel)
58
59    return filename
60       
61def extractToVariable(feature, sel):
62    """
63      calls extractToNetCDF to get the netcdf file, then returns a cdms variable from this file
64    """
65    #call extractToNetCDF:
66    filename = extractToNetCDF(feature, sel)
67    d = cdms.open(filename)
68    var = d(feature.name.CONTENT, squeeze=1)
69    # Work around for CSML bug
70    #var = d(feature.name.CONTENT, longitude=sel['longitude'], latitude=sel['latitude'],
71    #        squeeze=1)
72
73    d.close()
74    #os.remove(filename)
75    return var
76       
77class CsmlBundle(csml.API.csmlContainer.Container):
78    """
79    A quick stab at bundling CSML.
80
81    """
82    def __init__(self, context, **kwargs):
83        """
84        @param context: A directory path to store the CSML and NetCDF
85        """
86        self.context = context
87        kwargs['csmlpath'] = os.path.join(context, 'container.xml')
88        super(CsmlBundle, self).__init__(**kwargs)
89
90    def add(self, fpd):
91        """
92        Overrides the file storage descriptor so that the CSML points relative to the
93        bundle context
94
95        @warning: THIS IS A HACK!
96
97        """
98        (feature, path, desc) = fpd
99        if os.path.dirname(path) != self.context:
100            raise ValueError, "Extract not in bundle context"
101        desc.fileName.CONTENT = os.path.basename(path)
102
103        return super(CsmlBundle, self).add(fpd)
104
105    def join(self, *p):
106        return os.path.join(self.context, *p)
107
108    def makeBundle(self, file, bundleName=None):
109        """
110        Writes the bundle to a zipfile.
111
112        @param file: path or file-like object (see zipfile docs)
113
114        """
115        if bundleName is None:
116            bundleName = os.path.splitext(os.path.basename(file))[0]
117       
118        zf = zipfile.ZipFile(file, 'w')
119        self.getContents()
120        for f in self.containerContents:
121            zf.write(f, os.path.join(bundleName, os.path.basename(f)))
122        zf.close()
123
124        return file
Note: See TracBrowser for help on using the repository browser.