def write(self, fp, start=None, end=None): errstr = c_char_p() start_date = ( c_longlong.in_dll(dickinson, "LONG_TIME_T_MIN") if start is None else c_longlong(self.__datetime_to_time_t(start)) ) end_date = ( c_longlong.in_dll(dickinson, "LONG_TIME_T_MAX") if end is None else c_longlong(self.__datetime_to_time_t(end)) ) text = dickinson.ts_write( self.ts_handle, c_int(self.precision if self.precision is not None else -9999), start_date, end_date, byref(errstr), ) if not text: if not errstr: fp.write("") return raise IOError("Error when writing time series: %s" % (errstr.value,)) try: fp.write(string_at(text).decode("ascii")) finally: dickinson.freemem(text)
def sum(self, start_date=None, end_date=None): start_date = c_longlong.in_dll(dickinson, "LONG_TIME_T_MIN") \ if start_date is None \ else c_longlong(self.__datetime_to_time_t(start_date)) end_date = c_longlong.in_dll(dickinson, "LONG_TIME_T_MAX") \ if end_date is None \ else c_longlong(self.__datetime_to_time_t(end_date)) return dickinson.ts_sum(self.ts_handle, start_date, end_date)
def identify_events( ts_list, start_threshold, ntimeseries_start_threshold, time_separator, end_threshold=None, ntimeseries_end_threshold=None, start_date=None, end_date=None, reverse=False, ): if end_threshold is None: end_threshold = start_threshold if ntimeseries_end_threshold is None: ntimeseries_end_threshold = ntimeseries_start_threshold range_start_date = ( c_longlong.in_dll(dickinson, "LONG_TIME_T_MIN") if start_date is None else c_longlong(_datetime_to_time_t(start_date)) ) range_end_date = ( c_longlong.in_dll(dickinson, "LONG_TIME_T_MAX") if end_date is None else c_longlong(_datetime_to_time_t(end_date)) ) search_range = T_INTERVAL(range_start_date, range_end_date) try: a_timeseries_list = dickinson.tsl_create() a_interval_list = dickinson.il_create() if (not a_timeseries_list) or (not a_interval_list): raise MemoryError("Insufficient memory") for t in ts_list: if dickinson.tsl_append(a_timeseries_list, t.ts_handle): raise MemoryError("Insufficient memory") errstr = c_char_p() if dickinson.ts_identify_events( a_timeseries_list, search_range, c_int(reverse), c_double(start_threshold), c_double(end_threshold), c_int(ntimeseries_start_threshold), c_int(ntimeseries_end_threshold), c_longlong(time_separator.days * _SECONDS_PER_DAY + time_separator.seconds), a_interval_list, byref(errstr), ): raise Exception(errstr.value) result = [] for i in range(a_interval_list.contents.n): a_interval = a_interval_list.contents.intervals[i] result.append((_time_t_to_datetime(a_interval.start_date), _time_t_to_datetime(a_interval.end_date))) return result finally: dickinson.il_free(a_interval_list) dickinson.tsl_free(a_timeseries_list)
def average(self, start_date=None, end_date=None): start_date = ( c_longlong.in_dll(dickinson, "LONG_TIME_T_MIN") if start_date is None else c_longlong(self.__datetime_to_time_t(start_date)) ) end_date = ( c_longlong.in_dll(dickinson, "LONG_TIME_T_MAX") if end_date is None else c_longlong(self.__datetime_to_time_t(end_date)) ) return dickinson.ts_average(self.ts_handle, start_date, end_date)