87 static const char* c2numpy_float16 =
"<f2";
90 static const char* c2numpy_complex =
"<c16";
91 static const char* c2numpy_complex64 =
"<c8";
92 static const char* c2numpy_complex128 =
"<c16";
94 static const char* c2numpy_str[155] = {
95 "|S0",
"|S1",
"|S2",
"|S3",
"|S4",
"|S5",
"|S6",
"|S7",
"|S8",
"|S9",
96 "|S10",
"|S11",
"|S12",
"|S13",
"|S14",
"|S15",
"|S16",
"|S17",
"|S18",
"|S19",
97 "|S20",
"|S21",
"|S22",
"|S23",
"|S24",
"|S25",
"|S26",
"|S27",
"|S28",
"|S29",
98 "|S30",
"|S31",
"|S32",
"|S33",
"|S34",
"|S35",
"|S36",
"|S37",
"|S38",
"|S39",
99 "|S40",
"|S41",
"|S42",
"|S43",
"|S44",
"|S45",
"|S46",
"|S47",
"|S48",
"|S49",
100 "|S50",
"|S51",
"|S52",
"|S53",
"|S54",
"|S55",
"|S56",
"|S57",
"|S58",
"|S59",
101 "|S60",
"|S61",
"|S62",
"|S63",
"|S64",
"|S65",
"|S66",
"|S67",
"|S68",
"|S69",
102 "|S70",
"|S71",
"|S72",
"|S73",
"|S74",
"|S75",
"|S76",
"|S77",
"|S78",
"|S79",
103 "|S80",
"|S81",
"|S82",
"|S83",
"|S84",
"|S85",
"|S86",
"|S87",
"|S88",
"|S89",
104 "|S90",
"|S91",
"|S92",
"|S93",
"|S94",
"|S95",
"|S96",
"|S97",
"|S98",
"|S99",
105 "|S100",
"|S101",
"|S102",
"|S103",
"|S104",
"|S105",
"|S106",
"|S107",
"|S108",
"|S109",
106 "|S110",
"|S111",
"|S112",
"|S113",
"|S114",
"|S115",
"|S116",
"|S117",
"|S118",
"|S119",
107 "|S120",
"|S121",
"|S122",
"|S123",
"|S124",
"|S125",
"|S126",
"|S127",
"|S128",
"|S129",
108 "|S130",
"|S131",
"|S132",
"|S133",
"|S134",
"|S135",
"|S136",
"|S137",
"|S138",
"|S139",
109 "|S140",
"|S141",
"|S142",
"|S143",
"|S144",
"|S145",
"|S146",
"|S147",
"|S148",
"|S149",
110 "|S150",
"|S151",
"|S152",
"|S153",
"|S154"};
167 std::stringstream fileNameStream;
170 fileNameStream <<
".npy";
171 std::string fileName = fileNameStream.str();
172 writer->
file = fopen(fileName.c_str(),
"wb");
174 std::stringstream headerStream;
175 headerStream <<
"{'descr': [";
178 for (column = 0; column < writer->
numColumns; ++column) {
179 headerStream <<
"('" << writer->
columnNames[column] <<
"', '" 181 if (column < writer->numColumns - 1) headerStream <<
", ";
184 headerStream <<
"], 'fortran_order': False, 'shape': (";
192 headerStream <<
",), }";
194 int headerSize = headerStream.str().size();
197 if (headerSize > 65535) version = 2;
198 while ((6 + 2 + (version == 1 ? 2 : 4) + headerSize) % 16 != 0) {
201 if (headerSize > 65535) version = 2;
204 fwrite(
"\x93NUMPY", 1, 6, writer->
file);
206 fwrite(
"\x01\x00", 1, 2, writer->
file);
207 fwrite(&headerSize, 1, 2, writer->
file);
211 fwrite(
"\x02\x00", 1, 2, writer->
file);
212 fwrite(&headerSize, 1, 4, writer->
file);
216 std::string header = headerStream.str();
217 fwrite(header.c_str(), 1, header.size(), writer->
file);
222 #define C2NUMPY_CHECK_ITEM \ 224 if (writer->file == NULL) { \ 225 int status = c2numpy_open(writer); \ 226 if (status != 0) return status; \ 230 #define C2NUMPY_INCREMENT_ITEM \ 232 if (writer->currentColumn == 0) { \ 233 writer->currentRowInFile += 1; \ 234 if (writer->currentRowInFile == writer->numRowsPerFile) { \ 235 fclose(writer->file); \ 236 writer->file = NULL; \ 237 writer->currentRowInFile = 0; \ 238 writer->currentFileNumber += 1; \ 248 fwrite(&data,
sizeof(int8_t), 1, writer->
file);
257 fwrite(&data,
sizeof(int64_t), 1, writer->
file);
266 fwrite(&data,
sizeof(
int), 1, writer->
file);
275 fwrite(&data,
sizeof(
size_t), 1, writer->
file);
284 fwrite(&data,
sizeof(int8_t), 1, writer->
file);
293 fwrite(&data,
sizeof(int16_t), 1, writer->
file);
302 fwrite(&data,
sizeof(int32_t), 1, writer->
file);
311 fwrite(&data,
sizeof(int64_t), 1, writer->
file);
320 fwrite(&data,
sizeof(uint8_t), 1, writer->
file);
329 fwrite(&data,
sizeof(uint16_t), 1, writer->
file);
338 fwrite(&data,
sizeof(uint32_t), 1, writer->
file);
347 fwrite(&data,
sizeof(uint64_t), 1, writer->
file);
356 fwrite(&data,
sizeof(
double), 1, writer->
file);
373 fwrite(&data,
sizeof(
float), 1, writer->
file);
382 fwrite(&data,
sizeof(
double), 1, writer->
file);
416 if (0 < stringlength && stringlength < 155)
417 fwrite(data, 1, stringlength, writer->
file);
427 if (writer->
file != NULL) {
435 fputc(
' ', writer->
file);
int c2numpy_init(c2numpy_writer *writer, const std::string outputFilePrefix, int32_t numRowsPerFile)
int c2numpy_int32(c2numpy_writer *writer, int32_t data)
#define C2NUMPY_INCREMENT_ITEM
int32_t currentFileNumber
int c2numpy_int8(c2numpy_writer *writer, int8_t data)
int c2numpy_bool(c2numpy_writer *writer, int8_t data)
int c2numpy_uint8(c2numpy_writer *writer, uint8_t data)
int c2numpy_open(c2numpy_writer *writer)
int c2numpy_close(c2numpy_writer *writer)
#define C2NUMPY_CHECK_ITEM
int c2numpy_addcolumn(c2numpy_writer *writer, const std::string name, c2numpy_type type)
int c2numpy_float(c2numpy_writer *writer, double data)
std::vector< c2numpy_type > columnTypes
int c2numpy_int64(c2numpy_writer *writer, int64_t data)
const char * C2NUMPY_VERSION
int c2numpy_uint32(c2numpy_writer *writer, uint32_t data)
int c2numpy_float32(c2numpy_writer *writer, float data)
std::string outputFilePrefix
int c2numpy_uint16(c2numpy_writer *writer, uint16_t data)
int c2numpy_int16(c2numpy_writer *writer, int16_t data)
int c2numpy_intc(c2numpy_writer *writer, int data)
int c2numpy_float64(c2numpy_writer *writer, double data)
const char * c2numpy_descr(c2numpy_type type)
int c2numpy_int(c2numpy_writer *writer, int64_t data)
int c2numpy_uint64(c2numpy_writer *writer, uint64_t data)
int c2numpy_string(c2numpy_writer *writer, const char *data)
int c2numpy_intp(c2numpy_writer *writer, size_t data)
std::vector< std::string > columnNames