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

Changeset 73

Show
Ignore:
Timestamp:
09/14/07 17:46:37
Author:
cbc
Message:

Saving tested refactoring.

Files:

Legend:

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

    r72 r73  
    33Classes to handle processed sodar data samples. 
    44 
     5A list of sample data. 
     6 
     7A list of dictionaries, each with an item for header and body data. 
     8 
     9A list of dictionaries of sample data, each with a two items: 
     10    a header dictionary, and 
     11    a body list of alitude dictionaries. 
    512""" 
    613 
     
    1623class ProcessedData(list): 
    1724     
    18     """Processed daily sodar file data.""" 
     25    """Processed daily sodar file data. 
     26     
     27       A list of rawData.Sample deep copies. 
     28    """ 
    1929     
    2030    def __init__(self, data): 
    2131        """Prepare raw sodar daily data for analysis.""" 
    2232        super(ProcessedData, self).__init__() 
    23         self.data = data 
     33        self.extend([sample.data() for sample in data]) 
    2434        self._normalize() 
    2535 
    2636    def _normalize(self): 
    2737        """Clean up data for analysis.""" 
    28         self._copy() 
    2938        self._convert() 
    3039        self._stamp() 
     
    4352        # compute plotting parameters 
    4453     
    45     def _copy(self): 
    46         """Create a deep copy as a list of Sample copies.""" 
    47         self.extend([sample._copy() for sample in self.data]) 
    48      
    4954    def _convert(self): 
    50         """Convert to numbers and correct for invalid values.""" 
     55        """Convert data to numbers and correct for invalid values.""" 
    5156        INVALID = "-9999" 
    5257        for sample in self: 
     58            # convert header data to integers 
     59            for key,value in sample['header'].items(): 
     60                try: 
     61                    if value == INVALID: 
     62                        raise ValueError 
     63                    sample['header'][key] = int(value) 
     64                except (ValueError, TypeError): 
     65                    sample['header'][key] = n.NaN 
     66            # convert body data to floats 
    5367            for altitude in sample['body']: 
    5468                for key,value in altitude.items(): 
     
    5771                            raise ValueError 
    5872                        altitude[key] = float(value) 
    59                     except (ValueError, TypeError, KeyError): 
     73                    except (ValueError, TypeError): 
    6074                        altitude[key] = n.NaN 
    61             for key,value in sample['header'].items(): 
    62                 try: 
    63                     if value == INVALID: 
    64                         raise ValueError 
    65                     sample['header'][key] = int(value) 
    66                 except (ValueError, TypeError, KeyError): 
    67                     sample['header'][key] = n.NaN 
    6875     
    6976    def _stamp(self): 
    70         """Add a datetime stamp to each sample.""" 
     77        """Add a datetime stamp item to each sample.""" 
    7178        for sample in self: 
    7279            try: 
     
    8592                        [sample['stamp'] for sample in self[1:]]) 
    8693        intervals = [interval[1] - interval[0] for interval in intervals] 
    87         accumulator = {} 
    88         for interval in intervals: 
    89             if interval in accumulator: 
    90                 accumulator[interval] += 1 
    91             else: 
    92                 accumulator[interval] = 1 
    93         maxVotes = max(accumulator.values()) 
    94         for key,value in accumulator.items(): 
    95             if value == maxVotes: 
    96                 self.sampleInterval = key 
    97                 break 
    98         self.sampleInterval = getattr(self, 
    99                                     'sampleInterval', 
    100                                     datetime.timedelta.resolution) 
     94        intervals = [interval for interval in intervals if interval] 
     95        intervals = [interval for interval in intervals 
     96                     if interval != datetime.datetime.min] 
     97        try: 
     98            self.interval = min(intervals) 
     99        except ValueError: 
     100            self.interval = datetime.datetime.min 
    101101     
    102102    def _minimumAltitude(self): 
     
    142142def _main(): 
    143143    """Process as script from command line.""" 
    144     pass 
     144    import urllib2 
     145    try: 
     146        rawDataHandle = urllib2.urlopen('http://nemo.isis.unc.edu/'\ 
     147                                        'data/nccoos/level0/dukeforest/sodar/'\ 
     148                                        'store/2007-06/20070601.dat') 
     149        rawDataString = rawDataHandle.read() 
     150    except: 
     151        raise IOError("Failure to read raw test data") 
     152    rawDataObject = rawData.RawData(rawDataString) 
     153    processedDataObject = ProcessedData(rawDataObject) 
    145154 
    146155if __name__ == "__main__": 
  • sodar/trunk/sodar/rawData.py

    r72 r73  
    1515 
    1616Each Sample object has attributes for a Header and Body object. The Samples 
    17 within a RawData object may also be accessed by time using a string of the format 
    18 YYYY-MM-DD-HH-MM as in index on the RawData object to return the first matching 
    19 Sample in the RawData object: 
     17within a RawData object may also be accessed by time using a string of the 
     18format YYYY-MM-DD-HH-MM as in index on the RawData object to return the first 
     19matching Sample in the RawData object: 
    2020 
    2121    rawDataObject[0] # the first Sample object of the day 
     
    9494            year,month,day,hour,minute = index.split('-') 
    9595        except ValueError: 
    96             raise ValueError('RawData index by date must be "YYYY-MM-DD-HH-MM"') 
     96            raise ValueError('RawData index by date must be '\ 
     97                             '"YYYY-MM-DD-HH-MM"') 
    9798        except AttributeError: 
    98             raise AttributeError('RawData index by date must be "YYYY-MM-DD-HH-MM"') 
     99            raise AttributeError('RawData index by date must be '\ 
     100                                 '"YYYY-MM-DD-HH-MM"') 
    99101        for sample in self: 
    100102            try: 
     
    140142            raise IndexError('Sample index out of range') 
    141143     
    142     def _copy(self): 
    143         """Create a deep copy as a dictionary of header and body copies.""" 
    144         return {'header':self.header._copy(), 
    145                 'body':self.body._copy()} 
     144    def data(self): 
     145        """Create a deep copy as a dictionary of header and body data.""" 
     146        return {'header':self.header.data(), 
     147                'body':self.body.data()} 
    146148 
    147149 
     
    169171                             " ".join(headerLines[1::2]).split()))) 
    170172     
    171     def _copy(self): 
    172         """Create a shallow copy as a dictionary.""" 
     173    def data(self): 
     174        """Create a shallow copy of the data as a dictionary.""" 
    173175        return self.copy() 
    174176 
     
    217219        raise IndexError('Body index, out of range') 
    218220 
    219     def _copy(self): 
    220         """Create a deep copy as a list of dictionaries.""" 
     221    def data(self): 
     222        """Create a deep copy of the data as a list of dictionaries.""" 
    221223        return [altitude.copy() for altitude in self] 
    222224 
     
    226228    import urllib2 
    227229    try: 
    228         rawDataHandle = urllib2.urlopen('http://nemo.isis.unc.edu/data/nccoos/level0/dukeforest/sodar/store/2007-06/20070601.dat') 
     230        rawDataHandle = urllib2.urlopen('http://nemo.isis.unc.edu/'\ 
     231                                        'data/nccoos/level0/dukeforest/sodar/'\ 
     232                                        'store/2007-06/20070601.dat') 
    229233        rawDataString = rawDataHandle.read() 
    230234    except: