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

Changeset 268

Show
Ignore:
Timestamp:
12/02/09 15:27:23
Author:
cbc
Message:

Made MainData? objects callable in order to access profiles by timestamp.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sodar/branches/scintec-branch/sodar/scintec/maindata.py

    r267 r268  
    166166                     for block 
    167167                     in blocks[2:]]) 
    168          
     168     
     169    def __call__(self,timestamp,start=False): 
     170        """ 
     171        Get a profile by timestamp 
     172         
     173        maindata_instance(timestamp[,start]) -> <Profile object> 
     174         
     175        Where: 
     176         
     177            timestamp is a datetime.datetime object representing either 
     178            the start or stop timestamp of the Profile object returned. 
     179             
     180            start is a bool object. If start is False (default), then 
     181            timestamp is matched to the stop timestamp of the returned 
     182            Profile object. Otherwise, if start is True, then timestamp is 
     183            matched to the start timestamp of the returned Profile object. 
     184         
     185        Access profiles by timestamp: 
     186        >>> main_data = MainData(good_mnd) 
     187        >>> main_data(datetime(2009, 11, 17, 0, 30)).stop 
     188        datetime.datetime(2009, 11, 17, 0, 30) 
     189        >>> main_data(datetime(2009, 11, 17, 0, 0),True).start 
     190        datetime.datetime(2009, 11, 17, 0, 0) 
     191        >>> main_data(datetime(2009, 11, 18, 0, 0)).stop 
     192        datetime.datetime(2009, 11, 18, 0, 0) 
     193        >>> main_data(datetime(2009, 11, 17, 23, 0),True).start 
     194        datetime.datetime(2009, 11, 17, 23, 0) 
     195        >>> main_data(datetime(2009, 11, 16, 0, 30)) 
     196        Traceback (most recent call last): 
     197            ... 
     198        IndexError: 'MainData' object index out of range 
     199        >>> main_data('OK') 
     200        Traceback (most recent call last): 
     201            ... 
     202        TypeError: 'MainData' object indices must be datetime.datetime 
     203        instances 
     204        """ 
     205         
     206        for profile in self: 
     207            if start: 
     208                boundary = profile.start 
     209            else: 
     210                boundary = profile.stop 
     211            if boundary == timestamp: 
     212                return profile 
     213        if isinstance(timestamp,datetime): 
     214            raise IndexError, ' '.join([repr(self.__class__.__name__), 
     215                                        'object index out of range',]) 
     216        else: 
     217            raise TypeError, ' '.join([repr(self.__class__.__name__), 
     218                                       'object indices must be', 
     219                                       'datetime.datetime instances',]) 
    169220 
    170221class Profile(list): 
     
    188239    ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] 
    189240     
    190     Access profiles by sequence number and variables by attribute name: 
     241    Access observations by sequence number and variables by attribute name: 
    191242    >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', 
    192243    ...                'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', 
     
    202253    '99.99' 
    203254     
    204     Access profiles by elevation and variables by attribute name: 
     255    Access observations by elevation and variables by attribute name: 
    205256    >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', 
    206257    ...                   'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', 
     
    241292        ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] 
    242293         
    243         Access profiles by sequence number and variables by attribute name: 
     294        Access observations by sequence number and variables by attribute name: 
    244295        >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', 
    245296        ...                'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', 
     
    292343    def __getitem__(self,key): 
    293344        """ 
    294         Get a profile by elevation. 
     345        Get a observation by elevation. 
    295346         
    296347        profile_instance[elevation] -> <Observation object> 
     
    301352            (value keyed by 'z') of the Observation object. 
    302353         
    303         Access profiles by elevation and variables by attribute name: 
     354        Access observations by elevation and variables by attribute name: 
    304355        >>> profile = Profile(good_profile) 
    305356        >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', 
     
    315366        >>> profile['200'].W 
    316367        '-0.07' 
     368        >>> profile[100000] 
     369        Traceback (most recent call last): 
     370            ... 
     371        IndexError: list index out of range 
     372        >>> profile['100000'] 
     373        Traceback (most recent call last): 
     374            ... 
     375        TypeError: list indices must be integers 
    317376        """ 
    318377         
     
    625684            ... 
    626685        TypeError: 'Profile' instance's items are read-only 
    627          
    628686         
    629687        Without affecting the observations: