Index: sodar/branches/scintec-branch/sodar/scintec/maindata.py =================================================================== --- sodar/branches/scintec-branch/sodar/scintec/maindata.py (revision 267) +++ sodar/branches/scintec-branch/sodar/scintec/maindata.py (revision 268) @@ -166,5 +166,56 @@ for block in blocks[2:]]) - + + def __call__(self,timestamp,start=False): + """ + Get a profile by timestamp + + maindata_instance(timestamp[,start]) -> + + Where: + + timestamp is a datetime.datetime object representing either + the start or stop timestamp of the Profile object returned. + + start is a bool object. If start is False (default), then + timestamp is matched to the stop timestamp of the returned + Profile object. Otherwise, if start is True, then timestamp is + matched to the start timestamp of the returned Profile object. + + Access profiles by timestamp: + >>> main_data = MainData(good_mnd) + >>> main_data(datetime(2009, 11, 17, 0, 30)).stop + datetime.datetime(2009, 11, 17, 0, 30) + >>> main_data(datetime(2009, 11, 17, 0, 0),True).start + datetime.datetime(2009, 11, 17, 0, 0) + >>> main_data(datetime(2009, 11, 18, 0, 0)).stop + datetime.datetime(2009, 11, 18, 0, 0) + >>> main_data(datetime(2009, 11, 17, 23, 0),True).start + datetime.datetime(2009, 11, 17, 23, 0) + >>> main_data(datetime(2009, 11, 16, 0, 30)) + Traceback (most recent call last): + ... + IndexError: 'MainData' object index out of range + >>> main_data('OK') + Traceback (most recent call last): + ... + TypeError: 'MainData' object indices must be datetime.datetime + instances + """ + + for profile in self: + if start: + boundary = profile.start + else: + boundary = profile.stop + if boundary == timestamp: + return profile + if isinstance(timestamp,datetime): + raise IndexError, ' '.join([repr(self.__class__.__name__), + 'object index out of range',]) + else: + raise TypeError, ' '.join([repr(self.__class__.__name__), + 'object indices must be', + 'datetime.datetime instances',]) class Profile(list): @@ -188,5 +239,5 @@ ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] - Access profiles by sequence number and variables by attribute name: + Access observations by sequence number and variables by attribute name: >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', @@ -202,5 +253,5 @@ '99.99' - Access profiles by elevation and variables by attribute name: + Access observations by elevation and variables by attribute name: >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', @@ -241,5 +292,5 @@ ['z', 'speed', 'dir', 'W', 'sigW', 'bck', 'error'] - Access profiles by sequence number and variables by attribute name: + Access observations by sequence number and variables by attribute name: >>> profile[0] == {'z':'10', 'speed':'99.99', 'dir':'999.9', ... 'W':'-0.05', 'sigW':'0.40', 'bck':'5.46E+03', @@ -292,5 +343,5 @@ def __getitem__(self,key): """ - Get a profile by elevation. + Get a observation by elevation. profile_instance[elevation] -> @@ -301,5 +352,5 @@ (value keyed by 'z') of the Observation object. - Access profiles by elevation and variables by attribute name: + Access observations by elevation and variables by attribute name: >>> profile = Profile(good_profile) >>> profile['10'] == {'z':'10', 'speed':'99.99', 'dir':'999.9', @@ -315,4 +366,12 @@ >>> profile['200'].W '-0.07' + >>> profile[100000] + Traceback (most recent call last): + ... + IndexError: list index out of range + >>> profile['100000'] + Traceback (most recent call last): + ... + TypeError: list indices must be integers """ @@ -625,5 +684,4 @@ ... TypeError: 'Profile' instance's items are read-only - Without affecting the observations: