diff --git a/ArchiveExtractor.py b/ArchiveExtractor.py index f5bb8479bc14c5a3c84affabd4a42840e43ddfa5..b703354405bfcdaa4b7bb443fb54275a5614714f 100755 --- a/ArchiveExtractor.py +++ b/ArchiveExtractor.py @@ -188,6 +188,99 @@ def query_ADB_BetweenDates(attr, logger.debug("Extraction done for %s."%attr) return [date, value] +##---------------------------------------------------------------------------## +def query_ADB_BetweenDates_MinMaxMean( + attr, + dateStart, + dateStop=datetime.datetime.now(), + timeinterval=datetime.timedelta(seconds=60), + extractor="archiving/TDBExtractor/4"): + """ + Query attribute data from archiver database. + Divide the time range in time intervals. + Get min, max and mean value on each time interval. + The date stamp is in the middle of the interval. + + Parameters + ---------- + attr : String + Name of the attribute. Full Tango name i.e. "test/dg/panda/current". + + dateStart : datetime.datetime + Start date for extraction. + + dateStop : datetime.datetime + Stop date for extraction. + Default is now (datetime.datetime.now()) + + timeinterval : datetime.timedelta + Interval time to divide the time range in chunks. + Default is 1 minute. + + extractor : String + Name of the DB Extractor device. + Default is "archiving/TDBExtractor/4" + + Exceptions + ---------- + ValueError + The attribute is not found in the database. + + Returns + ------- + [date, value] : array + date : numpy.ndarray of datetime.datime objects + Dates of the values + value : numpy.ndarray + Archived values + + """ + + # TEMP Dev not finished + logger.error("Feature not implemented yet.") + return + + # Device Proxy to DB + logger.debug("Instantiate proxy to %s"%extractor) + ADB = tango.DeviceProxy(extractor) + + # Give the DB extractor 3 seconds timeout + ADB.set_timeout_millis(3000) + + # Check that the attribute is in the database + logger.debug("Check that %s is archived."%attr) + if not ADB.IsArchived(attr): + logger.error("Attribute '%s' is not archived in DB %s"%(attr, extractor)) + raise ValueError("Attribute '%s' is not archived in DB %s"%(attr, extractor)) + + # Cut data range in time chunks + cdates = [dateStart] + while cdates[-1] < dateStop: + cdates.append(cdates[-1]+timeinterval) + cdates[-1] = dateStop + logger.debug("Cutting time range to %d chunks of time, %s each."%(len(cdates)-1, dt)) + + # Prepare arrays + value_min = np.empty(len(cdates-1)) + value_max = np.empty(len(cdates-1)) + value_mean = np.empty(len(cdates-1)) + + # For each time chunk + for i_d in range(len(cdates)-1): + # Make requests + logger.debug("Perform GetAttDataMaxBetweenDates (%s, %s, %s)"%( + attr, + cdates[i_d].strftime(DBDFMT), + cdates[i_d+1].strftime(DBDFMT)) + ) + + ADB.GetAttDataMaxBetweenDates([ + attr, + cdates[i_d].strftime(DBDFMT), + cdates[i_d+1].strftime(DBDFMT) + ]) + + ##---------------------------------------------------------------------------## def query_ADB_NearestValue(attr, dates,