-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwicsv.c
245 lines (214 loc) · 7.13 KB
/
uwicsv.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "uwi.h"
#define MAX_COL_COUNT 10000
#define MAX_ROW_COUNT 100000
#define MAX_CHR_COUNT 1000
// Data Type for storing data of a Column
typedef struct csvColumn
{
int currentRow; // Current row of this column
double real[MAX_ROW_COUNT]; // Real valued Column data
double imag[MAX_ROW_COUNT]; // Imaginary Column data
} column;
// Data Type for storing Columns
struct csvData
{
FILE *csvFile; // Defines output file
char *ids[MAX_COL_COUNT]; // Column IDs
column columns[MAX_COL_COUNT]; // All columns in the file
int numCols; // Number of Columns in the file
int numRows; // Number of Rows in the file
short isDigital:1; // Digital wave form Flag
short isComplex:1; // Complex wave form Flag
char sweepVar[5]; // Name of Sweep Variable
double sweep[MAX_ROW_COUNT]; // Sweep variable Column
};
static struct csvData data; // Static instance for storing waveform
// Convert logic integer waveform value to string representation: {0, 1, x, z}
char* l2s(int val)
{
if (val == 0)
return "0";
else if (val == 1)
return "1";
else if (val == 2)
return "x";
else if (val == 3)
return "z";
else
return "";
}
// Open CSV File
uwi_StreamHandle csvOpen(const struct uwi_Setup* setup)
{
char name[MAX_CHR_COUNT];
int nameLen = MAX_CHR_COUNT - 10;
data.csvFile = NULL;
data.numCols = 0;
snprintf(name, nameLen, "%s/%s", setup->rawDir, setup->fileName);
strcat(name, ".csv");
if(setup->analysis == UWI_TRAN)
strcpy(data.sweepVar, "time");
else if(setup->analysis == UWI_AC)
strcpy(data.sweepVar, "freq");
else
strcpy(data.sweepVar, "sweep");
data.csvFile = fopen(name, "w+");
return data.csvFile;
}
// Close CSV File
int csvClose(uwi_StreamHandle stream)
{
fclose((FILE*) stream);
for(int i = 0; i < data.numCols; ++i)
free(data.ids[i]);
return 0;
}
// Define Waveform in CSV Format
uwi_WfHandle csvDefine( uwi_StreamHandle stream
, const struct uwi_WfDefinition* wfDef )
{
int it;
int index, lenID = 0;
if (wfDef->wfName)
lenID = strlen(wfDef->wfName);
if (wfDef->scopeName && wfDef->scopeName[0] != 0) {
for (it = 0; wfDef->scopeName[it] != 0; ++it)
lenID += 1 + strlen(wfDef->scopeName[it]);
}
if (lenID && data.numCols < MAX_COL_COUNT)
data.ids[data.numCols] = (char *)malloc(lenID + 1);
else
return NULL;
if (wfDef->scopeName && wfDef->scopeName[0] != 0) {
data.ids[data.numCols][0] = '\0';
for (it = 0; wfDef->scopeName[it] != 0; ++it ) {
strcat(data.ids[data.numCols], wfDef->scopeName[it]);
strcat(data.ids[data.numCols], "_");
}
strcat(data.ids[data.numCols], wfDef->wfName);
}
else
strcpy(data.ids[data.numCols], wfDef->wfName);
index = ++data.numCols;
data.columns[index].currentRow = 0;
memset(data.columns[index].real, 0.0, sizeof data.columns[index].real);
memset(data.columns[index].imag, 0.0, sizeof data.columns[index].imag);
return (uwi_WfHandle)(size_t)index;
}
// Flush waveform to csv file
int csvFlush(uwi_StreamHandle stream)
{
FILE *fp = (FILE*) stream;
column col;
fprintf(fp, "%s,", data.sweepVar);
for (int i = 0; i < data.numCols; ++i) {
fprintf(fp, "%s", data.ids[i]);
if (i < data.numCols - 1)
fprintf(fp, ",");
else
fprintf(fp, "\n");
}
for (int r = 0; r < data.numRows; ++r) {
fprintf(fp, "%lf", data.sweep[r]);
if (data.isComplex)
fprintf(fp, ":+0.0");
fprintf(fp, ",");
for (int c = 1; c < data.numCols + 1; ++c) {
col = data.columns[c];
if (data.isDigital) {
char* val = l2s((int) col.real[r]);
fprintf(fp, "%s", val);
}
else {
fprintf(fp, "%lf", col.real[r]);
if (data.isComplex)
fprintf(fp, ":+%lf", col.imag[r]);
}
if (c < data.numCols)
fprintf(fp, ",");
}
fprintf(fp, "\n");
}
data.numRows = 0;
for (int c = 0; c < data.numCols; ++c)
data.columns[c].currentRow = 0;
return 0;
}
// Add Real valued Analog data point
int csvAddAWfPoint( uwi_StreamHandle stream
, uwi_WfHandle wfHandle
, double real
, double time )
{
int colIdx = (size_t)(wfHandle);
int idx = data.columns[colIdx].currentRow++;
if (idx < MAX_ROW_COUNT) {
data.columns[colIdx].real[idx] = real;
data.sweep[idx] = time;
data.isDigital = 0;
data.isComplex = 0;
data.numRows = data.numRows < (idx + 1) ? (idx + 1) : data.numRows;
}
else
printf("Exceeded the maximum data point that can be stored \n");
return 0;
}
// Add Complex valued Analog data point
int csvAddAWfComplexPoint( uwi_StreamHandle stream
, uwi_WfHandle wfHandle
, double real
, double imag
, double freq )
{
int colIdx = (size_t)(wfHandle);
int idx = data.columns[colIdx].currentRow++;
if (idx < MAX_ROW_COUNT) {
data.columns[colIdx].real[idx] = real;
data.columns[colIdx].imag[idx] = imag;
data.sweep[idx] = freq;
data.isDigital = 0;
data.isComplex = 1;
data.numRows = data.numRows < (idx + 1) ? (idx + 1) : data.numRows;
}
else
printf("Exceeded the maximum data point that can be stored \n");
return 0;
}
// Add Digital data point
int csvAddDWfPoint( uwi_StreamHandle stream
, uwi_WfHandle wfHandle
, enum uwi_Logic value
, double time )
{
int colIdx = (size_t)(wfHandle);
int idx = data.columns[colIdx].currentRow++;
if (idx < MAX_ROW_COUNT) {
data.columns[colIdx].real[idx] = (double) value;
data.sweep[idx] = time;
data.isDigital = 1;
data.isComplex = 0;
data.numRows = data.numRows < (idx + 1) ? (idx + 1) : data.numRows;
}
else
printf("Exceeded the maximum data point that can be stored \n");
return 0;
}
// Register CSV
struct uwi_WfIntDef* uwi_register()
{
static struct uwi_WfIntDef wfIntDef;
wfIntDef.open = csvOpen;
wfIntDef.defineWf = csvDefine;
wfIntDef.addAWfPoint = csvAddAWfPoint;
wfIntDef.addAWfComplexPoint = csvAddAWfComplexPoint;
wfIntDef.addDWfPoint = csvAddDWfPoint;
wfIntDef.flush = csvFlush;
wfIntDef.close = csvClose;
wfIntDef.getErrMsg = NULL;
wfIntDef.resetXCoord = NULL;
wfIntDef.format = "CSV";
return &wfIntDef;
}