1 |
""" |
---|
2 |
Module to handle Scintec sodar .mnd files. |
---|
3 |
|
---|
4 |
>>> from sodar.scintec import maindata |
---|
5 |
""" |
---|
6 |
|
---|
7 |
__author__ = 'Chris Calloway' |
---|
8 |
__email__ = 'cbc@chriscalloway.org' |
---|
9 |
__copyright__ = 'Copyright 2009 UNC-CH Department of Marine Science' |
---|
10 |
__license__ = 'GPL2' |
---|
11 |
|
---|
12 |
class MainData(list): |
---|
13 |
""" |
---|
14 |
Contain data from Scintec sodar .mnd files. |
---|
15 |
|
---|
16 |
Parse a known good .mnd file: |
---|
17 |
>>> main_data = MainData(good_mnd) |
---|
18 |
|
---|
19 |
Parse the format header: |
---|
20 |
>>> len(main_data._format_header) |
---|
21 |
4 |
---|
22 |
>>> main_data._format_header[0] |
---|
23 |
'FORMAT-1' |
---|
24 |
|
---|
25 |
Parse the file header after the format header: |
---|
26 |
>>> len(main_data._file_header_body) |
---|
27 |
26 |
---|
28 |
>>> main_data._file_header_body[1] |
---|
29 |
'# file information' |
---|
30 |
|
---|
31 |
Parse the profile data: |
---|
32 |
>>> len(main_data) |
---|
33 |
48 |
---|
34 |
|
---|
35 |
Parse the first profile: |
---|
36 |
>>> len(main_data[0]) |
---|
37 |
39 |
---|
38 |
>>> main_data[0].timestamp |
---|
39 |
'2009-11-17 00:30:00 00:30:00' |
---|
40 |
>>> main_data[0].variables == ['z', 'speed', 'dir', 'W', |
---|
41 |
... 'sigW', 'bck', 'error'] |
---|
42 |
True |
---|
43 |
>>> main_data[0][0] |
---|
44 |
'10 99.99 999.9 -0.05 0.40 5.46E+03 0' |
---|
45 |
>>> main_data[0][-1] |
---|
46 |
'200 99.99 999.9 -0.07 99.99 9.99E+37 0' |
---|
47 |
|
---|
48 |
Parse the last profile: |
---|
49 |
>>> len(main_data[-1]) |
---|
50 |
39 |
---|
51 |
>>> main_data[-1].timestamp |
---|
52 |
'2009-11-18 00:00:00 00:30:00' |
---|
53 |
>>> main_data[-1].variables == ['z', 'speed', 'dir', 'W', |
---|
54 |
... 'sigW', 'bck', 'error'] |
---|
55 |
True |
---|
56 |
>>> main_data[-1][0] |
---|
57 |
'10 99.99 999.9 -0.32 99.99 9.99E+37 0' |
---|
58 |
>>> main_data[-1][-1] |
---|
59 |
'200 15.05 71.8 -0.19 0.53 9.99E+37 0' |
---|
60 |
""" |
---|
61 |
|
---|
62 |
def __init__(self, mnd, *args): |
---|
63 |
""" |
---|
64 |
Parse main daily Scintec sodar .mnd file. |
---|
65 |
|
---|
66 |
MainData(mnd[,file_name[,file_path]]) -> <MainData object> |
---|
67 |
|
---|
68 |
Where: |
---|
69 |
|
---|
70 |
mnd is a str object containing the complete contents read from a |
---|
71 |
Scintec .mnd daily sodar file including all line endings, |
---|
72 |
|
---|
73 |
file_name is an optional string object representing a file name for |
---|
74 |
a file which contains the referenced .mnd daily sodar file, |
---|
75 |
|
---|
76 |
file_path is an optional string object representing the path to |
---|
77 |
file_name. |
---|
78 |
|
---|
79 |
Parse a known good .mnd file: |
---|
80 |
|
---|
81 |
>>> main_data = MainData(good_mnd) |
---|
82 |
>>> main_data = MainData(good_mnd,good_name) |
---|
83 |
>>> main_data.file_name == good_name |
---|
84 |
True |
---|
85 |
>>> main_data = MainData(good_mnd,good_name,good_path) |
---|
86 |
>>> main_data.file_name == good_name |
---|
87 |
True |
---|
88 |
>>> main_data.file_path == good_path |
---|
89 |
True |
---|
90 |
""" |
---|
91 |
|
---|
92 |
super(MainData, self).__init__() |
---|
93 |
|
---|
94 |
self.file_name = '' |
---|
95 |
self.file_path = '' |
---|
96 |
|
---|
97 |
try: |
---|
98 |
self.file_name = str(args[0]) |
---|
99 |
self.file_path = str(args[1]) |
---|
100 |
except IndexError: |
---|
101 |
pass |
---|
102 |
|
---|
103 |
self._blocks = [self._block.strip() |
---|
104 |
for self._block in mnd.split('\n\n') |
---|
105 |
if self._block.strip()] |
---|
106 |
self._format_header = [self._line.strip() |
---|
107 |
for self._line in self._blocks[0].split('\n') |
---|
108 |
if self._line.strip()] |
---|
109 |
self._file_header_body = [self._line.strip() |
---|
110 |
for self._line in self._blocks[1].split('\n') |
---|
111 |
if self._line.strip()] |
---|
112 |
self.extend([Profile([self._line.strip() |
---|
113 |
for self._line in self._block.split('\n') |
---|
114 |
if self._line.strip()]) |
---|
115 |
for self._block in self._blocks[2:]]) |
---|
116 |
del self._blocks, self._block, self._line |
---|
117 |
|
---|
118 |
class Profile(list): |
---|
119 |
""" |
---|
120 |
Contain data for single profile from Scintec sodar .mnd files. |
---|
121 |
|
---|
122 |
Parse a known good profile block: |
---|
123 |
>>> profile = Profile(good_profile) |
---|
124 |
>>> profile.timestamp |
---|
125 |
'2009-11-17 00:30:00 00:30:00' |
---|
126 |
>>> profile.variables == ['z', 'speed', 'dir', 'W', |
---|
127 |
... 'sigW', 'bck', 'error'] |
---|
128 |
True |
---|
129 |
>>> profile[0] |
---|
130 |
'10 99.99 999.9 -0.05 0.40 5.46E+03 0' |
---|
131 |
>>> profile[-1] |
---|
132 |
'200 99.99 999.9 -0.07 99.99 9.99E+37 0' |
---|
133 |
""" |
---|
134 |
|
---|
135 |
def __init__(self, profile_block): |
---|
136 |
""" |
---|
137 |
Parse a profile block from a main daily Scintec sodar .mnd file. |
---|
138 |
|
---|
139 |
Profile(profile_data) -> <Profile object> |
---|
140 |
|
---|
141 |
Where: |
---|
142 |
|
---|
143 |
profile_block is a list of str objects containing all the lines |
---|
144 |
from a single profile in a Scintec .mnd daily sodar file. |
---|
145 |
|
---|
146 |
Parse a known good profile block: |
---|
147 |
>>> profile = Profile(good_profile) |
---|
148 |
>>> profile.timestamp |
---|
149 |
'2009-11-17 00:30:00 00:30:00' |
---|
150 |
>>> profile.variables == ['z', 'speed', 'dir', 'W', |
---|
151 |
... 'sigW', 'bck', 'error'] |
---|
152 |
True |
---|
153 |
>>> profile[0] |
---|
154 |
'10 99.99 999.9 -0.05 0.40 5.46E+03 0' |
---|
155 |
>>> profile[-1] |
---|
156 |
'200 99.99 999.9 -0.07 99.99 9.99E+37 0' |
---|
157 |
""" |
---|
158 |
|
---|
159 |
super(Profile, self).__init__() |
---|
160 |
|
---|
161 |
self.timestamp = profile_block[0] |
---|
162 |
self.variables = profile_block[1].split()[1:] |
---|
163 |
self.extend(profile_block[2:]) |
---|
164 |
|
---|
165 |
def _test(): |
---|
166 |
""" |
---|
167 |
Run module tests in script mode. |
---|
168 |
|
---|
169 |
>>> from sodar.scintec.maindata import _test |
---|
170 |
""" |
---|
171 |
|
---|
172 |
import doctest |
---|
173 |
from sodar.tests import suite |
---|
174 |
mnd_path,mnd_file,mnd,profile = suite.setUpGoodMndData() |
---|
175 |
doctest.testmod(extraglobs=dict(good_mnd=mnd, |
---|
176 |
good_name=mnd_file, |
---|
177 |
good_path=mnd_path, |
---|
178 |
good_profile=profile), |
---|
179 |
optionflags=doctest.NORMALIZE_WHITESPACE) |
---|
180 |
|
---|
181 |
if __name__ == "__main__": |
---|
182 |
_test() |
---|