#include "TimeSeries/deserialize.h" #include #include "Util/utils.h" // Deserialize a portion of a binary file into an array of PowerCurve, or a file // WARNING: NO start bytes, NO date times (all series have the same length, at same times). PowerCurve* deserialize(const char* ifileName, const char* ofileName, uint32_t* ranks, uint32_t nbRanks) { // Read tsLength at the beginning of the file uint32_t tsLength = get_tsLength(ifileName); uint32_t valuesPerSerie = (tsLength - 4) / 4; //remove 4 bytes of ID FILE* ifile = fopen(ifileName, "rb"); FILE* ofile = NULL; if (ofileName) ofile = fopen(ofileName, "w"); if (!ranks || nbRanks <= 0) { nbRanks = get_nbSeries(ifileName); ranks = NULL; } PowerCurve* powerCurves = NULL; if (!ofile) powerCurves = (PowerCurve*) malloc(nbRanks * sizeof(PowerCurve)); for (uint32_t i = 0; i < nbRanks; i++) { // position to the beginning of current (binarized) time-series // NOTE: shift by 8 bytes, because data size and series length are written first fseek(ifile, 8 + (ranks ? ranks[i] : i) * tsLength, SEEK_SET); PowerCurve* powerCurve; if (!ofile) { powerCurve = powerCurves + i; powerCurve->values = (float*) malloc(valuesPerSerie * sizeof(float)); } // translate 4-bytes binary integer into integer ID void* binaryID = malloc(4); size_t lengthRead = fread(binaryID, 4, 1, ifile); if (lengthRead != 1) fprintf(stderr,"Warning: deserializing truncated binary file.\n"); uint32_t ID = bInt_to_uint((Byte*) binaryID); free(binaryID); if (ofile) fprintf(ofile, "%u,", ID); else powerCurve->ID = ID; // translate 4-bytes binary integers into float Byte* binarySerie = (Byte*) malloc(4 * valuesPerSerie); lengthRead = fread(binarySerie, 1, 4*valuesPerSerie, ifile); //TODO: assert that lengthRead == 4*valuesPerSerie (...) for (uint32_t i = 0; i < valuesPerSerie; i++) { float power = bReal_to_float(binarySerie + 4 * i); if (ofile) { fprintf(ofile, "%g", power); if (i < valuesPerSerie-1) fprintf(ofile, ","); } else powerCurve->values[i] = power; } free(binarySerie); if (ofile) fprintf(ofile, "\n"); } fclose(ifile); if (ofile) fclose(ofile); return powerCurves; }