Index: sodar/trunk/sodar/data.py =================================================================== --- sodar/trunk/sodar/data.py (revision 67) +++ sodar/trunk/sodar/data.py (revision 68) @@ -60,4 +60,5 @@ import re import numpy as n +import datetime class Data(list): @@ -112,5 +113,6 @@ self._copy() self._convert() - # compute time interval + self._stamp() + self._timeInterval() # correct for missing times # compute minium altitude @@ -134,6 +136,5 @@ INVALID = "-9999" for sample in self.samples: - body = sample[1] - for altitude in body: + for altitude in sample['body']: for key,value in altitude.items(): try: @@ -143,4 +144,44 @@ except (ValueError, TypeError, KeyError): altitude[key] = n.NaN + for key,value in sample['header'].items(): + try: + if value == INVALID: + raise ValueError + sample['header'][key] = int(value) + except (ValueError, TypeError, KeyError): + sample['header'][key] = n.NaN + + def _stamp(self): + """Add a datetime stamp to each sample.""" + for sample in self.samples: + try: + header = sample['header'] + sample['stamp'] = datetime.datetime(header['YEAR'], + header['MONTH'], + header['DAY'], + header['HOUR'], + header['MIN']) + except (KeyError, TypeError): + sample['stamp'] = datatime.datetime.min + + def _timeInterval(self): + """Add a time interval attribute""" + intervals = zip([sample['stamp'] for sample in self.samples[:-1]], + [sample['stamp'] for sample in self.samples[1:]]) + intervals = [interval[1] - interval[0] for interval in intervals] + accumulator = {} + for interval in intervals: + if interval in accumulator: + accumulator[interval] += 1 + else: + accumulator[interval] = 1 + maxVotes = max(accumulator.values()) + for key,value in accumulator.items(): + if value == maxVotes: + self.timeInterval = key + break + self.timeInterval = getattr(self, + 'timeInterval', + datetime.timedelta.resolution) @@ -176,6 +217,7 @@ def _copy(self): - """Create a deep copy as a tuple of header and body copies.""" - return (self.header._copy(), self.body._copy()) + """Create a deep copy as a dictionary of header and body copies.""" + return {'header':self.header._copy(), + 'body':self.body._copy()}