1 #include "TimeSeries/deserialize.h"
3 #include "Util/utils.h"
5 // Deserialize a portion of a binary file into an array of PowerCurve, or a file
6 // WARNING: NO start bytes, NO date times (all series have the same length, at same times).
7 PowerCurve
* deserialize(const char* ifileName
, const char* ofileName
,
8 uint32_t* ranks
, uint32_t nbRanks
)
10 // Read tsLength at the beginning of the file
11 uint32_t tsLength
= get_tsLength(ifileName
);
13 uint32_t valuesPerSerie
= (tsLength
- 4) / 3; //remove 4 bytes of ID
15 FILE* ifile
= fopen(ifileName
, "rb");
18 ofile
= fopen(ofileName
, "w");
20 if (!ranks
|| nbRanks
<= 0)
22 nbRanks
= get_nbSeries(ifileName
);
26 PowerCurve
* powerCurves
= NULL
;
28 powerCurves
= (PowerCurve
*) malloc(nbRanks
* sizeof(PowerCurve
));
30 for (uint32_t i
= 0; i
< nbRanks
; i
++)
32 // position to the beginning of current (binarized) time-series
33 // NOTE: shift by 8 bytes, because data size and series length are written first
34 fseek(ifile
, 8 + (ranks
? ranks
[i
] : i
) * tsLength
, SEEK_SET
);
36 PowerCurve
* powerCurve
;
39 powerCurve
= powerCurves
+ i
;
40 powerCurve
->values
= (Real
*) malloc(valuesPerSerie
* sizeof(Real
));
43 // translate 4-bytes binary integer into integer ID
44 void* binaryID
= malloc(4);
45 size_t lengthRead
= fread(binaryID
, 4, 1, ifile
);
47 fprintf(stderr
,"Warning: deserializing truncated binary file.\n");
48 uint32_t ID
= bInt_to_uint((Byte
*) binaryID
, 4);
51 fprintf(ofile
, "%u,", ID
);
55 // translate 3-bytes binary integers into Real
56 Byte
* binarySerie
= (Byte
*) malloc(3 * valuesPerSerie
);
57 lengthRead
= fread(binarySerie
, 1, 3*valuesPerSerie
, ifile
);
58 if (lengthRead
!= 3*valuesPerSerie
)
59 fprintf(stderr
,"Warning: deserializing truncated binary file.\n");
60 for (uint32_t i
= 0; i
< valuesPerSerie
; i
++)
62 uint32_t powerInt
= bInt_to_uint(binarySerie
+ 3 * i
, 3);
65 fprintf(ofile
, "%g", powerInt
/ 10.0 - 0.0);
66 if (i
< valuesPerSerie
-1)
70 powerCurve
->values
[i
] = powerInt
/ 10.0 - 0.0;