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

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

WMS now works sufficiently to be crudely viewable with openlayers but
bugs remain.

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