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

Changeset 71

Show
Ignore:
Timestamp:
09/05/07 12:17:11
Author:
cbc
Message:

Rename Data class to RawData for ticket #21.

Files:

Legend:

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

    r70 r71  
    11#!/usr/bin/python 
    22""" 
    3 Classes to handle sodar data samples. 
    4  
    5 Sodar data samples are collected into daily files. Each sample consists of a 
     3Classes to handle raw sodar data samples. 
     4 
     5Raw sodar data samples are collected into daily files. Each sample consists of a 
    66header followed by an observation for each height. 
    77 
    8 The daily file is split into a list (modeled by the class Data) of samples 
    9 (modeled by the class Sample) in chronological order. A Data object is 
     8The daily file is split into a list (modeled by the class RawData) of samples 
     9(modeled by the class Sample) in chronological order. A RawData object is 
    1010initialized with a string representing the daily file data: 
    1111 
    12      dataHandle = open('20070601.dat') 
    13      dataString = data.read() 
    14      dataObject = Data(dataString) 
     12     rawDataHandle = open('20070601.dat') 
     13     rawDataString = rawDataHandle.read() 
     14     rawDataObject = RawData(rawDataString) 
    1515 
    1616Each Sample object has attributes for a Header and Body object. The Samples 
    17 within a Data object may also be accessed by time using a string of the format 
    18 YYYY-MM-DD-HH-MM as in index on the Data object to return the first matching 
    19 Sample in the Data object: 
    20  
    21     dataObject[0] # the first Sample object of the day 
    22     dataObject['2007-06-01-09-15'] # the Sample object for 9:15am 
    23     dataObject[15].header # the Header object of the 16th Sample 
    24     dataObject['2007-06-01-09-15'].body # the Body object for 9:15am 
     17within a RawData object may also be accessed by time using a string of the format 
     18YYYY-MM-DD-HH-MM as in index on the RawData object to return the first matching 
     19Sample in the RawData object: 
     20 
     21    rawDataObject[0] # the first Sample object of the day 
     22    rawDataObject['2007-06-01-09-15'] # the Sample object for 9:15am 
     23    rawDataObject[15].header # the Header object of the 16th Sample 
     24    rawDataObject['2007-06-01-09-15'].body # the Body object for 9:15am 
    2525 
    2626Header objects act as dictionaries. Access each sample-wide parameter of 
    2727interest using the header parameter name as a keyword on the Header object: 
    2828 
    29     dataObject[15].header['VAL2'] # the number of validations for beam 2 
    30     dataObject['2007-06-01-09-15'].header['SPU3'] # normalized false signal 
    31                                                   # probability on beam 3 
    32     dataObject[0].header['SNR1'] # signal to noise on beam 1 
     29    rawDataObject[15].header['VAL2'] # the number of validations for beam 2 
     30    rawDataObject['2007-06-01-09-15'].header['SPU3'] # normalized false signal 
     31                                                     # probability on beam 3 
     32    rawDataObject[0].header['SNR1'] # signal to noise on beam 1 
    3333 
    3434Consult your Sodar documentation for a complete list of header parameters. 
     35Different sodar models have different sets of header parameters. This model 
     36seeks to be model agnostic, and parses the header parameter names from the 
     37raw data itself. 
    3538 
    3639Body objects act as lists of dictionaries. The dictionaries access 
     
    3942an altitude string: 
    4043 
    41     dataObject[15].body[0] # the data for the lowest altitude, 16th sample 
    42     dataObject['2007-06-01-09-15'].body['70'] # the data for 70 meters 
    43     dataObject[15].body[0]['SPEED'] # wind speed at lowest altitude 
    44     dataObject['2007-06-01-09-15'].body['70']['DIR'] # wind direction 
    45                                                      # at 70 meters 
     44    rawDataObject[15].body[0] # the data for the lowest altitude, 16th sample 
     45    rawDataObject['2007-06-01-09-15'].body['70'] # the data for 70 meters 
     46    rawDataObject[15].body[0]['SPEED'] # wind speed at lowest altitude 
     47    rawDataObject['2007-06-01-09-15'].body['70']['DIR'] # wind direction 
     48                                                        # at 70 meters 
    4649 
    4750The body attribute of a Sample object may also be indexed directly on a Sample 
    4851object for the most convenient semantics: 
    4952 
    50     dataObject[15][0]['SPEED'] # wind speed at lowest altitude, 16th sample 
    51     dataObject['2007-06-01-09-15']['70']['DIR'] # wind direction, 
    52                                                 # 70 meters, 9:15am 
     53    rawDataObject[15][0]['SPEED'] # wind speed at lowest altitude, 16th sample 
     54    rawDataObject['2007-06-01-09-15']['70']['DIR'] # wind direction, 
     55                                                   # 70 meters, 9:15am 
    5356""" 
    5457 
     
    6265import datetime 
    6366 
    64 class Data(list): 
    65      
    66     """Daily sodar file data. 
     67class RawData(list): 
     68     
     69    """Raw daily sodar file data. 
    6770        
    6871       (A chronologically ordered list of samples.) 
     
    7174    def __init__(self, data): 
    7275        """Divide daily string into list of Samples separated by $.""" 
    73         super(Data, self).__init__() 
     76        super(RawData, self).__init__() 
    7477        self.extend([Sample(sample) 
    7578                     for sample in 
     
    8184        """Allow sample retrieval by Sample time in header.""" 
    8285        try: 
    83             return super(Data,self).__getitem__(index) 
     86            return super(RawData,self).__getitem__(index) 
    8487        except TypeError: 
    8588            return self._find(index) 
    8689 
    8790    def _find(self, index): 
    88         """Find Sample in Data 
     91        """Find Sample in RawData. 
    8992            
    90            where sample time of form YYYY-MM-DD-HH-MM. 
     93           Where sample time of form YYYY-MM-DD-HH-MM. 
    9194        """ 
    9295         
     
    9497            year,month,day,hour,minute = index.split('-') 
    9598        except ValueError: 
    96             raise ValueError('Data index by date must be "YYYY-MM-DD-HH-MM"') 
     99            raise ValueError('RawData index by date must be "YYYY-MM-DD-HH-MM"') 
    97100        except AttributeError: 
    98             raise AttributeError('Data index by date must be "YYYY-MM-DD-HH-MM"') 
     101            raise AttributeError('RawData index by date must be "YYYY-MM-DD-HH-MM"') 
    99102        for sample in self: 
    100103            try: 
     
    107110            except TypeError:   # sample.header may not exist 
    108111                continue 
    109         raise IndexError('Data index out of range') 
     112        raise IndexError('RawData index out of range') 
    110113 
    111114    def _normalize(self): 
     
    163166                                                    header['MIN']) 
    164167            except (KeyError, TypeError): 
    165                 sample['stamp'] = datatime.datetime.min 
     168                sample['stamp'] = datetime.datetime.min 
    166169     
    167170    def _sampleInterval(self): 
     
    228231class Sample(object): 
    229232     
    230     """A single sample from daily sodar file data. 
     233    """A single sample from raw daily sodar file data. 
    231234        
    232235       (A header and a body attribute.) 
     
    264267class Header(dict): 
    265268     
    266     """A sodar data sample header. 
     269    """A raw sodar data sample header. 
    267270 
    268271      (A dictionary of sample-wide parameters.) 
     
    292295class Body(list): 
    293296     
    294     """A sodar data sample body. 
     297    """A raw sodar data sample body. 
    295298 
    296299       (A list of dictionariess at each altitude.) 
     
    317320 
    318321    def __getitem__(self, index): 
    319         """Return altitude data by altitude string.""" 
     322        """Return raw altitude data by altitude string.""" 
    320323        try: 
    321324            return super(Body, self).__getitem__(index) 
     
    324327 
    325328    def _find(self, index): 
    326         """Find altitude data in Body.""" 
     329        """Find raw altitude data in Body.""" 
    327330        for altitude in self: 
    328331            try: 
     
    342345    import urllib2 
    343346    try: 
    344         dataHandle = urllib2.urlopen('http://nemo.isis.unc.edu/data/nccoos/level0/dukeforest/sodar/store/2007-06/20070601.dat') 
    345         dataString = dataHandle.read() 
     347        rawDataHandle = urllib2.urlopen('http://nemo.isis.unc.edu/data/nccoos/level0/dukeforest/sodar/store/2007-06/20070601.dat') 
     348        rawDataString = rawDataHandle.read() 
    346349    except: 
    347         raise IOError("Failure to read test data") 
    348     dataObject = Data(dataString) 
    349     print dataObject['2007-06-01-09-15']['70']['SPEED'] 
     350        raise IOError("Failure to read raw test data") 
     351    rawDataObject = RawData(rawDataString) 
     352    print rawDataObject['2007-06-01-09-15']['70']['SPEED'] 
    350353 
    351354if __name__ == "__main__":