-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdumpdata.cc
234 lines (199 loc) · 5.54 KB
/
dumpdata.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
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
#include <vector>
#include <string>
#include <cassert>
#include <fstream>
#include <string>
#include <cmath>
#include <iostream>
#include "misc.hh"
#include "fitsio_simple.hh"
class point_holder
{
public:
// point_holder();
point_holder(const image_long& binmap,
const image_dbl& vals);
unsigned no_bins() { return _no_bins; }
void get_bin_info(unsigned bin,
double* ret_x, double* ret_y, double* ret_val,
double* ret_rms_dx, double* ret_rms_dy,
double* ret_total,
unsigned* pix_count);
private:
unsigned count_no_bins(const image_long& binmap);
void calc_points(const image_long& binmap,
const image_dbl& vals);
private:
struct bininfo
{
double x, y;
double val;
double total;
double rms_dx, rms_dy;
unsigned pix_count;
bininfo() { x = y = val = total = rms_dx = rms_dy = 0; pix_count = 0; }
};
typedef std::vector<bininfo> vec_bininfo;
private:
const unsigned _no_bins;
vec_bininfo _bininfos;
};
point_holder::point_holder(const image_long& binmap,
const image_dbl& vals)
: _no_bins( count_no_bins( binmap ) ),
_bininfos( _no_bins )
{
calc_points(binmap, vals);
std::cout << "(i) Read " << _no_bins << " bins\n";
}
unsigned point_holder::count_no_bins(const image_long& binmap)
{
int no = -1;
for(unsigned y=0; y<binmap.yw(); ++y)
for(unsigned x=0; x<binmap.xw(); ++x)
{
if( binmap(x, y) > int(no) )
no = binmap(x, y);
}
return no+1;
}
void point_holder::calc_points(const image_long& binmap,
const image_dbl& vals)
{
std::vector<unsigned> counts(_no_bins);
vec_dbl totx(_no_bins), toty(_no_bins);
vec_dbl bval(_no_bins), total(_no_bins);
// calculate centroid
for(unsigned y=0; y<binmap.yw(); ++y)
for(unsigned x=0; x<binmap.xw(); ++x)
{
long bin = binmap(x, y);
if( bin >= 0 )
{
totx[bin] += x;
toty[bin] += y;
bval[bin] = vals(x, y);
total[bin] += vals(x, y);
counts[bin] ++;
}
}
for(unsigned bin=0; bin<_no_bins; ++bin)
{
assert( counts[bin] != 0 );
_bininfos[bin].x = totx[bin]/counts[bin];
_bininfos[bin].y = toty[bin]/counts[bin];
_bininfos[bin].val = bval[bin];
_bininfos[bin].total = total[bin];
_bininfos[bin].pix_count = counts[bin];
}
// using centroid, we can get the widths
vec_dbl totx_2(_no_bins), toty_2(_no_bins);
for(unsigned y=0; y<binmap.yw(); ++y)
for(unsigned x=0; x<binmap.xw(); ++x)
{
long bin = binmap(x, y);
if( bin >= 0 )
{
// subtract mean x (preserves accuracy)
const double dx = x - _bininfos[bin].x;
const double dy = y - _bininfos[bin].y;
totx_2[bin] += dx*dx;
toty_2[bin] += dy*dy;
}
}
for(unsigned bin=0; bin<_no_bins; ++bin)
{
_bininfos[bin].rms_dx = std::sqrt( totx_2[bin]/counts[bin] );
_bininfos[bin].rms_dy = std::sqrt( toty_2[bin]/counts[bin] );
}
}
void point_holder::get_bin_info(unsigned bin,
double* ret_x, double* ret_y,
double* ret_val,
double* ret_rms_dx, double* ret_rms_dy,
double* ret_total,
unsigned* ret_pix_count)
{
assert( bin < _no_bins );
const bininfo& bi = _bininfos[bin];
*ret_x = bi.x;
*ret_y = bi.y;
*ret_val = bi.val;
*ret_rms_dx = bi.rms_dx;
*ret_rms_dy = bi.rms_dy;
*ret_total = bi.total;
*ret_pix_count = bi.pix_count;
}
int main(int argc, char* argv[])
{
if( argc != 6 )
{
std::cerr <<
"Usage:\n"
" dumpdata infile.fits nerr_infile.fits perr_infile.fits "
"binmap.fits out.dat\n";
return 1;
}
const std::string indata = argv[1];
const std::string indata_nerr = argv[2];
const std::string indata_perr = argv[3];
const std::string inbinmap = argv[4];
const std::string outdata = argv[5];
image_dbl* in_image;
image_dbl* in_image_nerr;
image_dbl* in_image_perr;
image_long* in_binmap;
// load in the main dataset
{
std::cout << "(i) Loading image " << indata << '\n';
FITSFile dataset(indata);
dataset.readImage(&in_image);
}
// load in the nerr dataset
{
std::cout << "(i) Loading image " << indata_nerr << '\n';
FITSFile dataset(indata_nerr);
dataset.readImage(&in_image_nerr);
}
// load in the perr dataset
{
std::cout << "(i) Loading image " << indata_perr << '\n';
FITSFile dataset(indata_perr);
dataset.readImage(&in_image_perr);
}
// load binmap
{
std::cout << "(i) Loading image " << inbinmap << '\n';
FITSFile dataset(inbinmap);
dataset.readImage(&in_binmap);
}
point_holder ph(*in_binmap, *in_image);
point_holder ph_nerr(*in_binmap, *in_image_nerr);
point_holder ph_perr(*in_binmap, *in_image_perr);
std::ofstream fileout(outdata.c_str());
assert( fileout );
const unsigned no_bins = ph.no_bins();
for(unsigned bin=0; bin<no_bins; ++bin)
{
double x, y, val, nerr, perr;
double rms_dx, rms_dy, total;
unsigned pixcount;
// get data for bin
ph_nerr.get_bin_info(bin, &x, &y, &nerr, &rms_dx, &rms_dy, &total,
&pixcount);
ph_perr.get_bin_info(bin, &x, &y, &perr, &rms_dx, &rms_dy, &total,
&pixcount);
ph.get_bin_info(bin, &x, &y, &val, &rms_dx, &rms_dy, &total,
&pixcount);
// subtract
const double err_n = nerr - val;
const double err_p = perr - val;
// kludge to make symmetric errors
const double error = std::sqrt(0.5*(err_n*err_n + err_p*err_p));
fileout << x << '\t' << y << '\t'
<< val << '\t' << error << '\t'
<< rms_dx << '\t' << rms_dy << '\t' << total
<< '\t' << pixcount << '\t' << bin << '\t'
<< err_n << '\t' << err_p << '\n';
}
}