1 |
#!/usr/bin/env python |
---|
2 |
|
---|
3 |
""" |
---|
4 |
Create CSV spreadsheets from sponge data NetCDF files. |
---|
5 |
|
---|
6 |
Usage: |
---|
7 |
|
---|
8 |
> python query.py path/to/config/file |
---|
9 |
> python query.py -t |
---|
10 |
> python query.py --test |
---|
11 |
|
---|
12 |
Test silent import: |
---|
13 |
|
---|
14 |
>>> import expand |
---|
15 |
""" |
---|
16 |
|
---|
17 |
__author__ = "Chris Calloway" |
---|
18 |
__email__ = "cbc@chriscalloway.org" |
---|
19 |
__copyright__ = "Copyright 2010 UNC-CH Department of Marine Science" |
---|
20 |
__license__ = "GPL2" |
---|
21 |
|
---|
22 |
import sys |
---|
23 |
import os |
---|
24 |
import glob |
---|
25 |
import zipfile |
---|
26 |
import shutil |
---|
27 |
import fileinput |
---|
28 |
import doctest |
---|
29 |
import unittest |
---|
30 |
from StringIO import StringIO |
---|
31 |
|
---|
32 |
USAGE = "\n".join(__doc__.splitlines()[3:8]) |
---|
33 |
TEST_PATH = os.path.join("tests", "query") |
---|
34 |
|
---|
35 |
|
---|
36 |
def _test(): |
---|
37 |
""" |
---|
38 |
Run doctests as unittest suite. |
---|
39 |
|
---|
40 |
Test silent import: |
---|
41 |
|
---|
42 |
>>> from expand import _test |
---|
43 |
""" |
---|
44 |
|
---|
45 |
suite = [] |
---|
46 |
suite.append(doctest.DocTestSuite()) |
---|
47 |
suite = unittest.TestSuite(suite) |
---|
48 |
unittest.TextTestRunner().run(suite) |
---|
49 |
|
---|
50 |
return |
---|
51 |
|
---|
52 |
|
---|
53 |
def config_path(): |
---|
54 |
""" |
---|
55 |
Return the configuration file path from the command line. |
---|
56 |
|
---|
57 |
Supply too few arguments on command line: |
---|
58 |
|
---|
59 |
>>> save_stdout = sys.stdout |
---|
60 |
>>> temp_stdout = StringIO() |
---|
61 |
>>> sys.stdout = temp_stdout |
---|
62 |
>>> sys.argv = [] |
---|
63 |
>>> _config_path = config_path() |
---|
64 |
>>> sys.stdout = save_stdout |
---|
65 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
66 |
True |
---|
67 |
|
---|
68 |
Supply too many arguments on the command line: |
---|
69 |
|
---|
70 |
>>> save_stdout = sys.stdout |
---|
71 |
>>> temp_stdout = StringIO() |
---|
72 |
>>> sys.stdout = temp_stdout |
---|
73 |
>>> sys.argv = ["", "", "",] |
---|
74 |
>>> _config_path = config_path() |
---|
75 |
>>> sys.stdout = save_stdout |
---|
76 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
77 |
True |
---|
78 |
|
---|
79 |
Supply non-file argument: |
---|
80 |
|
---|
81 |
>>> save_stdout = sys.stdout |
---|
82 |
>>> temp_stdout = StringIO() |
---|
83 |
>>> sys.stdout = temp_stdout |
---|
84 |
>>> _config_path = os.path.join( |
---|
85 |
... os.path.dirname( |
---|
86 |
... os.path.abspath(__file__)), |
---|
87 |
... TEST_PATH) |
---|
88 |
>>> sys.argv = ["", _config_path] |
---|
89 |
>>> _config_path = config_path() |
---|
90 |
>>> sys.stdout = save_stdout |
---|
91 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
92 |
True |
---|
93 |
|
---|
94 |
Supply nonexistent file argument: |
---|
95 |
|
---|
96 |
>>> save_stdout = sys.stdout |
---|
97 |
>>> temp_stdout = StringIO() |
---|
98 |
>>> sys.stdout = temp_stdout |
---|
99 |
>>> _config_path = os.path.join( |
---|
100 |
... os.path.dirname( |
---|
101 |
... os.path.abspath(__file__)), |
---|
102 |
... TEST_PATH, "xxxxx") |
---|
103 |
>>> sys.argv = ["", _config_path] |
---|
104 |
>>> _config_path = config_path() |
---|
105 |
>>> sys.stdout = save_stdout |
---|
106 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
107 |
True |
---|
108 |
|
---|
109 |
Supply valid config path argument: |
---|
110 |
|
---|
111 |
>>> _config_path = os.path.join( |
---|
112 |
... os.path.dirname( |
---|
113 |
... os.path.abspath(__file__)), |
---|
114 |
... TEST_PATH, "config.py") |
---|
115 |
>>> sys.argv = ["", _config_path] |
---|
116 |
>>> _config_path == config_path() |
---|
117 |
True |
---|
118 |
""" |
---|
119 |
|
---|
120 |
path = None |
---|
121 |
try: |
---|
122 |
if len(sys.argv) == 2: |
---|
123 |
if sys.argv[1] == "-t" or sys.argv[1] == "--test": |
---|
124 |
_test() |
---|
125 |
else: |
---|
126 |
path = sys.argv[1] |
---|
127 |
if not os.path.exists(path): |
---|
128 |
raise IOError(path + \ |
---|
129 |
" does not exist.") |
---|
130 |
elif not os.path.isfile(path): |
---|
131 |
raise IOError(path + \ |
---|
132 |
" is not a file.") |
---|
133 |
else: |
---|
134 |
raise IOError("Incorrect number of arguments supplied.") |
---|
135 |
except IOError: |
---|
136 |
print USAGE |
---|
137 |
return path |
---|
138 |
|
---|
139 |
|
---|
140 |
def config(path): |
---|
141 |
""" |
---|
142 |
Return the configuration from a file. |
---|
143 |
|
---|
144 |
Execute empty configuration: |
---|
145 |
|
---|
146 |
>>> _config_path = os.path.join( |
---|
147 |
... os.path.dirname( |
---|
148 |
... os.path.abspath(__file__)), |
---|
149 |
... TEST_PATH, "empty_config.py") |
---|
150 |
>>> ncdir,csvdir, \ |
---|
151 |
ncfile_pattern,csvfile_pattern, \ |
---|
152 |
location,platform,packages = \ |
---|
153 |
config(_config_path) |
---|
154 |
>>> ncdir |
---|
155 |
>>> csvdir |
---|
156 |
>>> ncfile_pattern |
---|
157 |
>>> csvfile_pattern |
---|
158 |
>>> location |
---|
159 |
>>> platform |
---|
160 |
>>> packages |
---|
161 |
|
---|
162 |
Execute nonexistent configuration: |
---|
163 |
|
---|
164 |
>>> save_stdout = sys.stdout |
---|
165 |
>>> temp_stdout = StringIO() |
---|
166 |
>>> sys.stdout = temp_stdout |
---|
167 |
>>> _config_path = os.path.join( |
---|
168 |
... os.path.dirname( |
---|
169 |
... os.path.abspath(__file__)), |
---|
170 |
... TEST_PATH, "xxxxx") |
---|
171 |
>>> ncdir,csvdir, \ |
---|
172 |
ncfile_pattern,csvfile_pattern, \ |
---|
173 |
location,platform,packages = \ |
---|
174 |
config(_config_path) |
---|
175 |
>>> sys.stdout = save_stdout |
---|
176 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
177 |
True |
---|
178 |
>>> ncdir |
---|
179 |
>>> csvdir |
---|
180 |
>>> ncfile_pattern |
---|
181 |
>>> csvfile_pattern |
---|
182 |
>>> location |
---|
183 |
>>> platform |
---|
184 |
>>> packages |
---|
185 |
|
---|
186 |
Execute bad configuration: |
---|
187 |
|
---|
188 |
>>> save_stdout = sys.stdout |
---|
189 |
>>> temp_stdout = StringIO() |
---|
190 |
>>> sys.stdout = temp_stdout |
---|
191 |
>>> _config_path = os.path.join( |
---|
192 |
... os.path.dirname( |
---|
193 |
... os.path.abspath(__file__)), |
---|
194 |
... TEST_PATH, "bad_config.py") |
---|
195 |
>>> ncdir,csvdir, \ |
---|
196 |
ncfile_pattern,csvfile_pattern, \ |
---|
197 |
location,platform,packages = \ |
---|
198 |
config(_config_path) |
---|
199 |
>>> sys.stdout = save_stdout |
---|
200 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
201 |
True |
---|
202 |
>>> ncdir |
---|
203 |
>>> csvdir |
---|
204 |
>>> ncfile_pattern |
---|
205 |
>>> csvfile_pattern |
---|
206 |
>>> location |
---|
207 |
>>> platform |
---|
208 |
>>> packages |
---|
209 |
|
---|
210 |
Execute valid configuration: |
---|
211 |
|
---|
212 |
>>> _config_path = os.path.join( |
---|
213 |
... os.path.dirname( |
---|
214 |
... os.path.abspath(__file__)), |
---|
215 |
... TEST_PATH, "config.py") |
---|
216 |
>>> ncdir,csvdir, \ |
---|
217 |
ncfile_pattern,csvfile_pattern, \ |
---|
218 |
location,platform,packages = \ |
---|
219 |
config(_config_path) |
---|
220 |
>>> ncdir == os.path.join(os.path.dirname(os.path.abspath(__file__)), |
---|
221 |
... TEST_PATH, "nc") |
---|
222 |
True |
---|
223 |
>>> csvdir == os.path.join(os.path.dirname(os.path.abspath(__file__)), |
---|
224 |
... TEST_PATH, "csv") |
---|
225 |
True |
---|
226 |
>>> ncfile_pattern == "%(location)s_%(platform)s_%(package)s_" \ |
---|
227 |
"[0-9][0-9][0-9][0-9]_[0-9][0-9].nc" |
---|
228 |
True |
---|
229 |
>>> csvfile_pattern == "%(location)s_%(platform)s_%(package)s_" \ |
---|
230 |
"[0-9][0-9][0-9][0-9]_[0-9][0-9].csv" |
---|
231 |
True |
---|
232 |
>>> location == "location" |
---|
233 |
True |
---|
234 |
>>> platform == "platform" |
---|
235 |
True |
---|
236 |
>>> packages == ('system', |
---|
237 |
... 'turbidity', |
---|
238 |
... 'optode_127', |
---|
239 |
... 'optode_167', |
---|
240 |
... 'optode_169', |
---|
241 |
... 'optode_170', |
---|
242 |
... 'optode_171', |
---|
243 |
... 'optode_172', |
---|
244 |
... 'optode_173', |
---|
245 |
... 'optode_175', |
---|
246 |
... 'optode_176', |
---|
247 |
... 'optode_178', |
---|
248 |
... 'optode_179', |
---|
249 |
... 'conductivity', |
---|
250 |
... 'pressure', |
---|
251 |
... 'current', |
---|
252 |
... ) |
---|
253 |
True |
---|
254 |
""" |
---|
255 |
|
---|
256 |
namespace = {} |
---|
257 |
namespace["NCDIR"] = None |
---|
258 |
namespace["CSVDIR"] = None |
---|
259 |
namespace["NCFILE_PATTERN"] = None |
---|
260 |
namespace["CSVFILE_PATTERN"] = None |
---|
261 |
namespace["LOCATION"] = None |
---|
262 |
namespace["PLATFORM"] = None |
---|
263 |
namespace["PACKAGES"] = None |
---|
264 |
try: |
---|
265 |
execfile(path, globals(), namespace) |
---|
266 |
except IOError: |
---|
267 |
print USAGE |
---|
268 |
except SyntaxError: |
---|
269 |
print USAGE |
---|
270 |
return (namespace["NCDIR"], |
---|
271 |
namespace["CSVDIR"], |
---|
272 |
namespace["NCFILE_PATTERN"], |
---|
273 |
namespace["CSVFILE_PATTERN"], |
---|
274 |
namespace["LOCATION"], |
---|
275 |
namespace["PLATFORM"], |
---|
276 |
namespace["PACKAGES"], |
---|
277 |
) |
---|
278 |
|
---|
279 |
|
---|
280 |
def query(ncdir, csvdir, |
---|
281 |
ncfile_pattern, csvfile_pattern, |
---|
282 |
location, platform, packages): |
---|
283 |
""" |
---|
284 |
Create CSV spreadsheets from sponge data NetCDF files. |
---|
285 |
""" |
---|
286 |
|
---|
287 |
return |
---|
288 |
|
---|
289 |
|
---|
290 |
def _main(): |
---|
291 |
""" |
---|
292 |
Run module as script. |
---|
293 |
|
---|
294 |
Supply incomplete config file: |
---|
295 |
|
---|
296 |
>>> save_stdout = sys.stdout |
---|
297 |
>>> temp_stdout = StringIO() |
---|
298 |
>>> sys.stdout = temp_stdout |
---|
299 |
>>> _config_path = os.path.join( |
---|
300 |
... os.path.dirname( |
---|
301 |
... os.path.abspath(__file__)), |
---|
302 |
... TEST_PATH, "incomplete_config.py") |
---|
303 |
>>> sys.argv = ["", _config_path] |
---|
304 |
>>> _main() |
---|
305 |
>>> sys.stdout = save_stdout |
---|
306 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
307 |
True |
---|
308 |
""" |
---|
309 |
|
---|
310 |
_config_path = config_path() |
---|
311 |
if _config_path: |
---|
312 |
_config = config(_config_path) |
---|
313 |
if all(_config): |
---|
314 |
query(*_config) |
---|
315 |
else: |
---|
316 |
print USAGE |
---|
317 |
return |
---|
318 |
|
---|
319 |
if __name__ == "__main__": |
---|
320 |
_main() |
---|