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

Changeset 68

Show
Ignore:
Timestamp:
08/20/07 16:40:47
Author:
cbc
Message:

Fulfill ticket #9: Compute sample time interval

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sodar/trunk/sodar/data.py

    r67 r68  
    6060import re 
    6161import numpy as n 
     62import datetime 
    6263 
    6364class Data(list): 
     
    112113        self._copy() 
    113114        self._convert() 
    114         # compute time interval 
     115        self._stamp() 
     116        self._timeInterval() 
    115117        # correct for missing times 
    116118        # compute minium altitude 
     
    134136        INVALID = "-9999" 
    135137        for sample in self.samples: 
    136             body = sample[1] 
    137             for altitude in body: 
     138            for altitude in sample['body']: 
    138139                for key,value in altitude.items(): 
    139140                    try: 
     
    143144                    except (ValueError, TypeError, KeyError): 
    144145                        altitude[key] = n.NaN 
     146            for key,value in sample['header'].items(): 
     147                try: 
     148                    if value == INVALID: 
     149                        raise ValueError 
     150                    sample['header'][key] = int(value) 
     151                except (ValueError, TypeError, KeyError): 
     152                    sample['header'][key] = n.NaN 
     153     
     154    def _stamp(self): 
     155        """Add a datetime stamp to each sample.""" 
     156        for sample in self.samples: 
     157            try: 
     158                header = sample['header'] 
     159                sample['stamp'] = datetime.datetime(header['YEAR'], 
     160                                                    header['MONTH'], 
     161                                                    header['DAY'], 
     162                                                    header['HOUR'], 
     163                                                    header['MIN']) 
     164            except (KeyError, TypeError): 
     165                sample['stamp'] = datatime.datetime.min 
     166     
     167    def _timeInterval(self): 
     168        """Add a time interval attribute""" 
     169        intervals = zip([sample['stamp'] for sample in self.samples[:-1]], 
     170                        [sample['stamp'] for sample in self.samples[1:]]) 
     171        intervals = [interval[1] - interval[0] for interval in intervals] 
     172        accumulator = {} 
     173        for interval in intervals: 
     174            if interval in accumulator: 
     175                accumulator[interval] += 1 
     176            else: 
     177                accumulator[interval] = 1 
     178        maxVotes = max(accumulator.values()) 
     179        for key,value in accumulator.items(): 
     180            if value == maxVotes: 
     181                self.timeInterval = key 
     182                break 
     183        self.timeInterval = getattr(self, 
     184                                    'timeInterval', 
     185                                    datetime.timedelta.resolution) 
    145186 
    146187 
     
    176217     
    177218    def _copy(self): 
    178         """Create a deep copy as a tuple of header and body copies.""" 
    179         return (self.header._copy(), self.body._copy()) 
     219        """Create a deep copy as a dictionary of header and body copies.""" 
     220        return {'header':self.header._copy(), 
     221                'body':self.body._copy()} 
    180222 
    181223