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