This repository was archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_anim.cc
206 lines (174 loc) · 6.05 KB
/
make_anim.cc
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
#include <stdlib.h>
#include <complex>
#include <fstream>
#include <sstream>
#include <json/json.h>
#include "sfxc_fft_float.h"
#include "utils.h"
#include "output_header.h"
#include "gnuplot_i.h"
const char *tmp_dir = "/tmp";
const int MAX_PLOT = 25;
Json::Value ctrl; // Correlator control file
int st1, st2;
std::vector< std::string > data_files;
inline bool
operator==(const Output_header_baseline &bh1,
const Output_header_baseline &bh2) {
return ((bh1.station_nr1 == bh2.station_nr1) &&
(bh1.station_nr2 == bh2.station_nr2) &&
(bh1.polarisation1 == bh2.polarisation1) &&
(bh1.polarisation2 == bh2.polarisation2) &&
(bh1.sideband == bh2.sideband) &&
(bh1.frequency_nr == bh2.frequency_nr));
}
void get_filename(char *filename) {
int plot_nr = (int)data_files.size();
snprintf(filename, 80, "%s/fringe_%03d.txt", tmp_dir, plot_nr);
}
void read_data() {
char filename[80];
int read;
std::string in_filename = ctrl["output_file"].asString().c_str()+7;
FILE *input = fopen(in_filename.c_str(), "rb");
assert(input != NULL);
Output_header_global global_header;
Output_header_timeslice timeslice_header;
Output_header_baseline baseline_header;
Output_header_baseline baseline_header_cmp;
baseline_header_cmp.station_nr1 = uint8_t(-1);
bool found_baseline = false;
read = fread(&global_header, sizeof(global_header), 1, input);
if (read != 1) {
std::cout << __LINE__ << " didn't read enough data" << std::endl;
}
int number_channels = global_header.number_channels+1;
std::complex<float> tmp_baseline[number_channels];
SFXC_FFT_FLOAT fft;
fft.resize(number_channels);
for (int i=0; i<MAX_PLOT-1; i++) {
get_filename(filename);
std::ofstream out(filename);
out << 0 << " " << i << " 0" << std::endl;
out << number_channels << " " << i << " 0" << std::endl;
data_files.push_back(filename);
}
do {
read = fread(×lice_header, sizeof(timeslice_header), 1, input);
if (read != 1) {
timeslice_header.number_baselines = 0;
}
for (int i=0; i<timeslice_header.number_baselines; i++) {
read = fread(&baseline_header, sizeof(baseline_header), 1, input);
if (read != 1) {
std::cout << __LINE__ << " didn't read enough data" << std::endl;
i = timeslice_header.number_baselines;
baseline_header.station_nr1 = uint8_t(-1);
}
read = fread(&tmp_baseline[0],
number_channels*sizeof(std::complex<float>), 1,
input);
if (read != 1) {
std::cout << __LINE__ << " didn't read enough data" << std::endl;
}
std::cout << (int)baseline_header.station_nr1 << " " << (int)baseline_header.station_nr2 << std::endl;
if (!found_baseline) {
if (((baseline_header.station_nr1 == st1) &&
(baseline_header.station_nr2 == st2)) ||
((baseline_header.station_nr1 == st2) &&
(baseline_header.station_nr2 == st1))) {
baseline_header_cmp = baseline_header;
found_baseline = true;
}
}
if (baseline_header == baseline_header_cmp) {
// do the fft
fft.ifft(&tmp_baseline[0], &tmp_baseline[0]);
// Store the baseline
get_filename(filename);
std::ofstream out(filename);
for (int j=0; j<number_channels; j++) {
out << j << " " << data_files.size() << " "
<< std::abs(tmp_baseline[(j+number_channels/2)%number_channels])
<< std::endl;
}
data_files.push_back(filename);
}
}
} while (!feof(input));
}
void show_data() {
int n_integrations = data_files.size();
std::cout << "Size: " << n_integrations << std::endl;
char cmd[80], data_file[80], plot_file[80];
snprintf(data_file, 80, "%s/data.cin", tmp_dir);
for (int img_nr=0; img_nr < n_integrations-MAX_PLOT; img_nr++) {
//for (int img_nr=10; img_nr < 11; img_nr++) {
std::cout << img_nr << std::endl;
snprintf(plot_file, 80, "%s/mov%03d.png", tmp_dir, img_nr+MAX_PLOT);
{ // Plot the data
gnuplot_ctrl * g = gnuplot_init();
gnuplot_setstyle(g, (char*)"lines" );
//gnuplot_cmd(g, "set hidden3d");
gnuplot_cmd(g, (char*)"set terminal png");
gnuplot_cmd(g, (char*)"set autoscale");
gnuplot_cmd(g, (char*)"set noxtics");
gnuplot_cmd(g, (char*)"set noytics");
gnuplot_cmd(g, (char*)"set noztics");
gnuplot_cmd(g, (char*)"set noborder");
gnuplot_cmd(g, (char*)"set view 70,150,.75,1.5");
snprintf(cmd, 80, "set output \"%s\"", plot_file);
gnuplot_cmd(g, cmd);
bool first_plot = true;
std::stringstream plot_cmd;
plot_cmd << "splot ";
for (int i=0; i<MAX_PLOT; i++) {
if ((img_nr+i >= 0) && (img_nr+i < (int)data_files.size())) {
if (!first_plot) {
plot_cmd << ", ";
} else {
first_plot = false;
}
plot_cmd << "\"" << data_files[i+img_nr] << "\" w l lt " << ((i+img_nr)%4)+1 << " notitle";
}
}
char plot_cmd2[plot_cmd.str().size()];
strcpy(plot_cmd2, plot_cmd.str().c_str());
gnuplot_cmd(g, plot_cmd2);
gnuplot_close(g);
}
}
}
int main(int argc, char * argv[]) {
if (argc != 4) {
std::cout << "Usage: "
<< argv[0] << " <ctrl-file> <station1_nr> <station2_nr>"
<< std::endl;
exit(-1);
}
char * ctrl_file = argv[1];
st1 = atoi(argv[2]);
st2 = atoi(argv[3]);
{ // parse the control file
Json::Reader reader;
std::ifstream in(ctrl_file);
if (!in.is_open()) {
std::cout << "Could not open control file [" << ctrl_file << "]" << std::endl;
exit(-1);
}
bool ok = reader.parse(in, ctrl);
if ( !ok ) {
// report to the user the failure and their locations in the document.
std::cout << "Failed to parse control file\n"
<< reader.getFormatedErrorMessages()
<< std::endl;
exit(-1);
}
}
read_data();
if (!data_files.empty())
show_data();
char cmd[80];
snprintf(cmd, 80, "convert -delay 50 -loop 0 %s/mov*.png anim.gif", tmp_dir);
return system(cmd);
}