-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux_reader.cpp
221 lines (201 loc) · 7.64 KB
/
aux_reader.cpp
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
#include "data.h"
#include "cmd_slave.h"
#include <fstream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sagecal.h"
#include "utils.h"
#include "utils_dn.h"
using namespace std;
using namespace Data;
int aux_reader(dlg_app_info *app) {
char table_name[256];
int len = app->inputs[0].read(table_name,255);
cout << "read len:" << len << endl;
table_name[len] = '\0';
Data::IOData iodata;
Data::LBeam beam;
iodata.tilesz=Data::TileSize;
iodata.deltat=1.0;
iodata.msname = (char *)malloc(sizeof(char) * (strlen(table_name)-1));
snprintf(iodata.msname, strlen(table_name)-1, "%s", table_name);
iodata.msname[strlen(table_name)-1] = '\0';
if (Data::SkyModel && Data::Clusters) {
cout << "Using SkyModel: " << Data::SkyModel << ", Clusters: " << Clusters << ", table_name: " << iodata.msname << endl;
} else {
cout << "ERROR, SkyModel: " << Data::SkyModel << ", Clusters: " << Clusters << ", table_name: " << iodata.msname << endl;
}
if (!Data::SkyModel || !Data::Clusters) {
exit(1);
}
cout << "Selecting baselines > " << Data::min_uvcut << " and < " << Data::max_uvcut << " wavelengths." << endl;
if (!DoSim) {
cout << "Using ";
if (Data::solver_mode == SM_LM_LBFGS || Data::solver_mode == SM_OSLM_LBFGS || Data::solver_mode == SM_RTR_OSLM_LBFGS ||
Data::solver_mode == SM_NSD_RLBFGS) {
cout << "Gaussian noise model for solver." << endl;
} else {
cout << "Robust noise model for solver with degrees of freedom [" << Data::nulow << "," << Data::nuhigh << "]." << endl;
}
} else {
cout << "Only doing simulation (with possible correction for cluster id " << Data::ccid << ")." << endl;
}
openblas_set_num_threads(1);
if (!doBeam) {
readAuxData(iodata.msname, &iodata);
} else {
readAuxData(iodata.msname, &iodata, &beam);
}
/**********************************************************/
int M, Mt, ci, cj, ck;
double *p, *pinit;
double **pm;
clus_source_t *carr;
baseline_t *barr;
read_sky_cluster(Data::SkyModel, Data::Clusters, &carr, &M, iodata.freq0, iodata.ra0, iodata.dec0, Data::format);
/* exit if there are 0 clusters (incorrect sky model/ cluster file)*/
if (M <= 0) {
fprintf(stderr, "%s: %d: no clusters to solve\n", __FILE__, __LINE__);
exit(1);
} else {
printf("Got %d clusters\n", M);
}
/* array to store baseline->sta1,sta2 map */
if ((barr = (baseline_t *) calloc((size_t) iodata.Nbase * iodata.tilesz, sizeof(baseline_t))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
generate_baselines(iodata.Nbase, iodata.tilesz, iodata.N, barr, Data::Nt);
/* calculate actual no of parameters needed,
this could be > M */
Mt = 0;
for (ci = 0; ci < M; ci++) {
//printf("cluster %d has %d time chunks\n",carr[ci].id,carr[ci].nchunk);
Mt += carr[ci].nchunk;
}
printf("Total effective clusters: %d\n", Mt);
/* parameters 8*N*M ==> 8*N*Mt */
if ((p = (double *) calloc((size_t) iodata.N * 8 * Mt, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* update cluster array with correct pointers to parameters */
cj = 0;
for (ci = 0; ci < M; ci++) {
if ((carr[ci].p = (int *) calloc((size_t) carr[ci].nchunk, sizeof(int))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
for (ck = 0; ck < carr[ci].nchunk; ck++) {
carr[ci].p[ck] = cj * 8 * iodata.N;
cj++;
}
}
/* pointers to parameters */
if ((pm = (double **) calloc((size_t) Mt, sizeof(double *))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* setup the pointers */
for (ci = 0; ci < Mt; ci++) {
pm[ci] = &(p[ci * 8 * iodata.N]);
}
/* initilize parameters to [1,0,0,0,0,0,1,0] */
for (ci = 0; ci < Mt; ci++) {
for (cj = 0; cj < iodata.N; cj++) {
pm[ci][8 * cj] = 1.0;
pm[ci][8 * cj + 6] = 1.0;
}
}
/* backup of default initial values */
if ((pinit = (double *) calloc((size_t) iodata.N * 8 * Mt, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memcpy(pinit, p, (size_t) iodata.N * 8 * Mt * sizeof(double));
/**********************************************************/
/* timeinterval in seconds */
cout << "For " << iodata.tilesz << " samples, solution time interval (s): "
<< iodata.deltat * (double) iodata.tilesz << endl;
cout << "Freq: " << iodata.freq0 / 1e6 << " MHz, Chan: " << iodata.Nchan << " Bandwidth: " << iodata.deltaf / 1e6
<< " MHz" << ", Nms: " << iodata.Nms << endl;
iodata.M = M;
iodata.Mt = Mt;
/*------------------------------------share data file-------------------------------------------------------------*/
dump_share_iodata(Data::shareDir, iodata.msname, &iodata);
if (doBeam) {
dump_share_beam(Data::shareDir, iodata.msname, &iodata, &beam);
}
dump_share_barr(Data::shareDir, iodata.msname, &iodata, barr);
write_share_XYZ(Data::shareDir, iodata.msname, p, iodata.N * 8 * Mt, "p");
write_share_XYZ(Data::shareDir, iodata.msname, pinit, iodata.N * 8 * Mt, "pinit");
/*------------------------------------output----------------------------------------------------------------------*/
dump_iodata_dn(&(app->outputs[0]), &iodata);
if (Data::doBeam) {
dump_beam_dn(&(app->outputs[0]), &iodata, &beam);
}
/*------------------------------------free ----------------------------------------------------------------------*/
exinfo_gaussian *exg;
exinfo_disk *exd;
exinfo_ring *exr;
exinfo_shapelet *exs;
for (ci = 0; ci < M; ci++) {
free(carr[ci].ll);
free(carr[ci].mm);
free(carr[ci].nn);
free(carr[ci].sI);
free(carr[ci].p);
free(carr[ci].ra);
free(carr[ci].dec);
for (cj = 0; cj < carr[ci].N; cj++) {
/* do a proper typecast before freeing */
switch (carr[ci].stype[cj]) {
case STYPE_GAUSSIAN:
exg = (exinfo_gaussian *) carr[ci].ex[cj];
if (exg) free(exg);
break;
case STYPE_DISK:
exd = (exinfo_disk *) carr[ci].ex[cj];
if (exd) free(exd);
break;
case STYPE_RING:
exr = (exinfo_ring *) carr[ci].ex[cj];
if (exr) free(exr);
break;
case STYPE_SHAPELET:
exs = (exinfo_shapelet *) carr[ci].ex[cj];
if (exs) {
if (exs->modes) {
free(exs->modes);
}
free(exs);
}
break;
default:
break;
}
}
free(carr[ci].ex);
free(carr[ci].stype);
free(carr[ci].sI0);
free(carr[ci].f0);
free(carr[ci].spec_idx);
free(carr[ci].spec_idx1);
free(carr[ci].spec_idx2);
}
free(carr);
free(barr);
free(p);
free(pinit);
free(pm);
/* free data memory */
if (!doBeam) {
Data::freeData(iodata);
} else {
Data::freeData(iodata, beam);
}
cout << "[aux_reader]========, Done." << endl;
return 0;
}