-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtabgen.c
More file actions
129 lines (108 loc) · 3.09 KB
/
Copy pathtabgen.c
File metadata and controls
129 lines (108 loc) · 3.09 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
125
126
127
128
129
/*
* TABGEN: generate a table with (random) numbers, purely for benchmarking
*/
#include <nemo.h>
string defv[] = {
"out=???\n Output table",
"nr=10\n Number of rows",
"nc=5\n Number of columns",
"mode=1\n Mode (1=uniform, 2=normal 3=constant 4=linear, 5=linear)",
"seed=123\n Random seed",
"fmt=%g\n Format statement for output",
"sep=s\n column separator (s=space t=tab c=comma v=vertical bar)",
"addrow=f\n Add row number (1=first) as first column?",
"VERSION=0.8\n 14-feb-2024 PJT",
"header=None\n Add a dummy header in a given style [ecsv, ipac]",
NULL,
};
string usage="Create a table with (random) numbers";
typedef real (*my_real_proc)(real,real);
local real constant(real c1, real c2)
{
return c2;
}
local real linear(real c1, real c2)
{
static real count = 0;
count = count + 1;
return count;
}
string add_header(int nc, string header)
{
char *result = malloc(100);
result[0] = '\0';
int first = 1;
int arr_len = 0;
strcat(result, "| ");
for (int i = 0; i < nc; i++) {
char col_num[100];
if (!first) {
strcat(result, " | ");
}
sprintf(col_num, "col%d", (i+1));
strcat(result, col_num);
first = 0;
arr_len++;
}
strcat(result, " |");
strcat(result, "\n");
strcat(result, "| double");
for (int i = 1; i < arr_len; i++) {
strcat(result, " | double");
}
strcat(result, " |");
return result;
}
void nemo_main()
{
stream ostr = stropen(getparam("out"),"w");
int i, nr = getiparam("nr");
int j, nc = getiparam("nc");
int mode = getiparam("mode");
int amode = ABS(mode);
int seed = init_xrandom(getparam("seed"));
string fmt = getparam("fmt");
my_real_proc my_random = NULL;
string seps = getparam("sep");
bool Qrow = getbparam("addrow");
char sep[8];
string header = getparam("header");
fprintf(ostr, "%s\n", add_header(header));
if (seps[0] == 'c') strcpy(sep,",");
else if (seps[0] == 's') strcpy(sep," ");
else if (seps[0] == 't') strcpy(sep,"\t");
else if (seps[0] == 'v') strcpy(sep,"|");
else strcpy(sep,seps);
dprintf(1,"seed=%d\n",seed);
if (amode == 1) {
dprintf(1,"Uniform values between 0 and 1\n");
my_random = xrandom;
} else if (amode == 2) {
dprintf(1,"Uniform values between 0 and 1\n");
my_random = grandom;
} else if (amode == 3) {
dprintf(1,"Uniform values between 0 and 1\n");
my_random = constant;
} else if (amode == 4) {
dprintf(1,"Uniform values between 0 and 1\n");
my_random = linear;
} else
error("Illegal mode=%d",mode);
if (mode < 0) {
// only produce random numbers, testing production rate
real *x = (real *) allocate(nc*nr*sizeof(real));
int k=0;
for (i=0; i<nr; i++)
for (j=0; j<nc; j++)
x[k++] = my_random(0.0,1.0);
} else
// output to file as well
for (i=0; i<nr; i++) {
if (Qrow) fprintf(ostr,"%d ", i+1);
for (j=0; j<nc; j++) {
if (j>0) fprintf(ostr, "%s", sep);
fprintf(ostr,fmt, my_random(0.0, 1.0));
}
fprintf(ostr,"\n");
}
}