-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathreadData.cpp
More file actions
124 lines (102 loc) · 3.08 KB
/
readData.cpp
File metadata and controls
124 lines (102 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* readData.cpp
*
* Created on: Nov 4, 2008
* Author: Doug Roberts
* Modified by: Andrew Pangborn
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "gaussian.h"
using namespace std;
extern "C"
float* readData(char* f, int* ndims, int* nevents);
float* readBIN(char* f, int* ndims, int* nevents);
float* readCSV(char* f, int* ndims, int* nevents);
float* readData(char* f, int* ndims, int* nevents) {
int length = strlen(f);
printf("File Extension: %s\n",f+length-3);
if(strcmp(f+length-3,"bin") == 0) {
return readBIN(f,ndims,nevents);
} else {
return readCSV(f,ndims,nevents);
}
}
float* readBIN(char* f, int* ndims, int* nevents) {
FILE* fin = fopen(f,"rb");
fread(nevents,4,1,fin);
fread(ndims,4,1,fin);
printf("Number of elements removed for memory alignment: %d\n",*nevents % (16 * 2));
*nevents -= *nevents % (16 * 2) ; // 2 gpus
int num_elements = (*ndims)*(*nevents);
printf("Number of rows: %d\n",*nevents);
printf("Number of cols: %d\n",*ndims);
float* data = (float*) malloc(sizeof(float)*num_elements);
fread(data,sizeof(float),num_elements,fin);
fclose(fin);
return data;
}
float* readCSV(char* f, int* ndims, int* nevents) {
string line1;
ifstream file(f);
vector<string> lines;
int num_dims = 0;
char* temp;
float* data;
if (file.is_open()) {
while(!file.eof()) {
getline(file, line1);
if (!line1.empty()) {
lines.push_back(line1);
}
}
file.close();
}
else {
cout << "Unable to read the file " << f << endl;
return NULL;
}
if(lines.size() > 0) {
line1 = lines[0];
string line2 (line1.begin(), line1.end());
temp = strtok((char*)line1.c_str(), ",");
while(temp != NULL) {
num_dims++;
temp = strtok(NULL, ",");
}
lines.erase(lines.begin()); // Remove first line, assumed to be header
int num_events = (int)lines.size();
#if TRUNCATE == 1
printf("Number of events removed to ensure memory alignment %d\n",num_events % (16 * 2));
num_events -= num_events % (16 * 2);
#endif
// Allocate space for all the FCS data
data = (float*)malloc(sizeof(float) * num_dims * (num_events));
if(!data){
printf("Cannot allocate enough memory for FCS data.\n");
return NULL;
}
for (int i = 0; i < num_events; i++) {
temp = strtok((char*)lines[i].c_str(), ",");
for (int j = 0; j < num_dims; j++) {
if(temp == NULL) {
free(data);
return NULL;
}
data[i * num_dims + j] = atof(temp);
temp = strtok(NULL, ",");
}
}
*ndims = num_dims;
*nevents = num_events;
return data;
} else {
return NULL;
}
}