X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fdata.c;h=38eba8339d3437ef6197b66d8ad83fae0eada425;hb=3521cee9644bd955fd053c5b4ea173efd1e78fe8;hp=ffd0b67d32d463898bf1e37f259bc4d9ad87b273;hpb=46c219c69676ea4e6f467692b7db6e48a708ab80;p=matches%2FMCTX3420.git diff --git a/server/data.c b/server/data.c index ffd0b67..38eba83 100644 --- a/server/data.c +++ b/server/data.c @@ -1,6 +1,6 @@ /** * @file data.c - * @purpose Implementation of data handling functions; saving, loading, displaying, selecting. + * @brief Implementation of data handling functions; saving, loading, displaying, selecting. */ #include "data.h" @@ -13,9 +13,8 @@ void Data_Init(DataFile * df) { // Everything is NULL - df->filename = NULL; + memset(df, 0, sizeof(DataFile)); pthread_mutex_init(&(df->mutex), NULL); - df->file = NULL; } /** @@ -162,13 +161,13 @@ void Data_PrintByIndexes(DataFile * df, int start_index, int end_index, DataForm switch (format) { case JSON: - fmt_string = "[%f,%f]"; + fmt_string = "[%.9f,%f]"; separator = ','; // For JSON we need an opening bracket FCGI_PrintRaw("["); break; case TSV: - fmt_string = "%f\t%f"; + fmt_string = "%.9f\t%f"; separator = '\n'; break; } @@ -293,7 +292,7 @@ int Data_FindByTime(DataFile * df, double time_stamp, DataPoint * closest) * @param df - DataFile to access * @param start - Info about start_time param * @param end - Info about end_time param - * @param fmt - Info about format param + * @param format - Info about format param * @param current_time - Current time */ void Data_Handler(DataFile * df, FCGIValue * start, FCGIValue * end, DataFormat format, double current_time) @@ -357,3 +356,51 @@ DataFormat Data_GetFormat(FCGIValue * fmt) } return JSON; } + +/** + * Binary search for index of a double in an array + * @param value - The value + * @param x - The array + * @param size - Sizeof the array + */ +int FindClosest(double value, double x[], int size) +{ + int upper = size-1; + int lower = 0; + int index = 0; + while (upper - lower > 1) + { + index = lower + ((upper - lower)/2); + double look = x[index]; + if (look > value) + upper = index; + else if (look < value) + lower = index; + else + return index; + } + + if (x[index] > value && index > 0) + --index; + return index; + +} + +/** + * Get calibrated value by interpolation in array y + * @param value - Raw measured value + * @param x - x values (raw values) of the data + * @param y - calibrated values + * @param size - Number of values in the arrays + * @returns interpolated calibrated value + */ +double Data_Calibrate(double value, double x[], double y[], int size) +{ + int i = FindClosest(value, x, size); + if (i >= size-1) + { + i = size-2; + } + double dist = (value - x[i])/(x[i+1] - x[i]); + return y[i] + dist*(y[i+1]-y[i]); +}