NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/sodar/branches/scintec-branch/sodar/scintec/maindata.py

Revision 257 (checked in by cbc, 14 years ago)

File name and path attributes on MainData?.

Line 
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     Class to parse Scintec sodar .mnd files.
15    
16     Parse the 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     41
38     >>> main_data[0][0]
39     '2009-11-17 00:30:00 00:30:00'
40     >>> main_data[0][-1]
41     '200  99.99  999.9  -0.07  99.99  9.99E+37       0'
42    
43     Parse the last profile:
44     >>> len(main_data[-1])
45     41
46     >>> main_data[-1][0]
47     '2009-11-18 00:00:00 00:30:00'
48     >>> main_data[-1][-1]
49     '200  15.05   71.8  -0.19   0.53  9.99E+37       0'
50     """
51    
52     def __init__(self, mnd, *args):
53         """
54         Parse main daily Scintec sodar .mnd file.
55        
56         MainData(mnd[,file_name[,file_path]]) -> <MainData object>
57        
58         Where:
59          
60             mnd is a str object containing the complete contents read from a
61             Scintec .mnd daily sodar file including all line endings,
62            
63             file_name is an optional string object representing a file name for
64             a file which contains the referenced .mnd daily sodar file,
65            
66             file_path is an optional string object representing the path to
67             file_name.
68        
69         Parse a known good .mnd file:
70        
71         >>> main_data = MainData(good_mnd)
72         >>> main_data = MainData(good_mnd,good_name)
73         >>> main_data.file_name == good_name
74         True
75         >>> main_data = MainData(good_mnd,good_name,good_path)
76         >>> main_data.file_name == good_name
77         True
78         >>> main_data.file_path == good_path
79         True
80         """
81        
82         super(MainData, self).__init__()
83        
84         self.file_name = ''
85         self.file_path = ''
86        
87         try:
88             self.file_name = str(args[0])
89             self.file_path = str(args[1])
90         except IndexError:
91             pass
92        
93         print mnd
94        
95         self._blocks = [self._block.strip()
96                       for self._block in mnd.split('\n\n')
97                       if self._block.strip()]
98         self._format_header = [self._line.strip()
99                               for self._line in self._blocks[0].split('\n')
100                               if self._line.strip()]
101         self._file_header_body = [self._line.strip()
102                                   for self._line in self._blocks[1].split('\n')
103                                   if self._line.strip()]
104         self.extend([[self._line.strip()
105                       for self._line in self._block.split('\n')
106                       if self._line.strip()]
107                      for self._block in self._blocks[2:]])
108
109 def _test():
110     """
111     Run module tests in script mode.
112    
113     >>> from sodar.scintec.maindata import _test
114     """
115     import doctest
116     from sodar.tests import suite
117     good_mnd = suite.setUpGoodMndData()
118     doctest.testmod(extraglobs=dict(good_mnd=good_mnd))
119
120 if __name__ == "__main__":
121     _test()
Note: See TracBrowser for help on using the browser.