-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontbin.cc
More file actions
468 lines (401 loc) · 13 KB
/
contbin.cc
File metadata and controls
468 lines (401 loc) · 13 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Contour binning program
// See Sanders (2006)
// Copyright 2002-2025 Jeremy Sanders
// Released under the GNU Public Licence (GPL)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <memory>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include "parammm/parammm.hh"
#include "binner.hh"
#include "flux_estimator.hh"
#include "fitsio_simple.hh"
#include "misc.hh"
const char* const CONTBIN_VERSION = "1.7";
using std::string;
using std::vector;
using std::find;
using std::ostringstream;
using std::cout;
class program
{
public:
program(int argc, char **argv);
void run();
private:
void auto_mask(const image_float& in_data, image_short* mask);
template<class T> void save_image(const string& filename, const T& image,
FITSFile* indataset);
template<class T> void load_image(const string& filename, double *exposure,
T** image);
private:
string _out_fname, _sn_fname, _binmap_fname;
string _bg_fname;
string _in_fname;
string _mask_fname;
string _smoothed_fname;
string _expmap_fname, _bg_expmap_fname;
string _noisemap_fname;
double _sn_threshold;
double _smooth_sn;
bool _do_automask;
bool _constrain_fill;
double _constrain_val;
bool _noscrub;
bool _binup;
double _scrub_large;
};
program::program(int argc, char **argv)
: _out_fname("contbin_out.fits"),
_sn_fname("contbin_sn.fits"),
_binmap_fname("contbin_binmap.fits"),
_sn_threshold(15.),
_smooth_sn(15.),
_do_automask(false),
_constrain_fill(false),
_constrain_val(3),
_noscrub(false),
_binup(false),
_scrub_large(-1)
{
parammm::param params(argc, argv);
params.add_switch( parammm::pswitch("out", 'o',
parammm::pstring_opt(&_out_fname),
"set out file (def contbin_out.fits)",
"FILE"));
params.add_switch( parammm::pswitch("outsn", 'e',
parammm::pstring_opt(&_sn_fname),
"set signal:noise out file (def contbin_sn.fits)",
"FILE"));
params.add_switch( parammm::pswitch("outbinmap", 'n',
parammm::pstring_opt(&_binmap_fname),
"set binmap out file (def contbin_binmap.fits)",
"FILE"));
params.add_switch( parammm::pswitch("bg", 'b',
parammm::pstring_opt(&_bg_fname),
"Set background image file (def none)",
"FILE"));
params.add_switch( parammm::pswitch("mask", 'm',
parammm::pstring_opt(&_mask_fname),
"Set mask image file (def none)",
"FILE"));
params.add_switch( parammm::pswitch("smoothed", 'm',
parammm::pstring_opt(&_smoothed_fname),
"Set smoothed image file (def none)",
"FILE"));
params.add_switch( parammm::pswitch("expmap", 0,
parammm::pstring_opt(&_expmap_fname),
"Set exposure map (fg) (def none)",
"FILE"));
params.add_switch( parammm::pswitch("bgexpmap", 0,
parammm::pstring_opt(&_bg_expmap_fname),
"Set exposure map (bg) (def none)",
"FILE"));
params.add_switch( parammm::pswitch("noisemap", 0,
parammm::pstring_opt(&_noisemap_fname),
"Set noise map (def none)",
"FILE"));
params.add_switch( parammm::pswitch("sn", 's',
parammm::pdouble_opt(&_sn_threshold),
"set signal:noise threshold (def 15)",
"VAL"));
params.add_switch( parammm::pswitch("automask", 0,
parammm::pbool_noopt(&_do_automask),
"automatically mask image",
""));
params.add_switch( parammm::pswitch("constrainfill", 0,
parammm::pbool_noopt(&_constrain_fill),
"constrain filling-factor",
""));
params.add_switch( parammm::pswitch("constrainval", 0,
parammm::pdouble_opt(&_constrain_val),
"set constrain ratio (def 3)",
"VAL"));
params.add_switch( parammm::pswitch("smoothsn", 0,
parammm::pdouble_opt(&_smooth_sn),
"set smoothing signal:noise (def 15)",
"VAL"));
params.add_switch( parammm::pswitch("noscrub", 0,
parammm::pbool_noopt(&_noscrub),
"don't scrub low S/N bins",
""));
params.add_switch( parammm::pswitch("binup", 0,
parammm::pbool_noopt(&_binup),
"start binning from bottom",
""));
params.add_switch( parammm::pswitch("scrublarge", 0,
parammm::pdouble_opt(&_scrub_large),
"Scrub bins with area frac > this",
"VAL"));
params.set_autohelp("Usage: contbin [OPTIONS] file.fits\n"
"Contour binning program\n"
"Written by Jeremy Sanders 2002-2025",
"Report bugs to <jeremy@jeremysanders.net>");
params.enable_autohelp();
params.enable_autoversion(CONTBIN_VERSION,
"Jeremy Sanders",
"Licenced under the GPL - see the file COPYING");
params.enable_at_expansion();
params.interpret_and_catch();
if(params.args().size() != 1)
{
params.show_autohelp();
}
else
{
_in_fname = params.args()[0];
}
}
void program::auto_mask(const image_float& in_data, image_short* mask)
{
cout << "(i) Automasking... ";
cout.flush();
const unsigned blocksize = 8;
const unsigned xw = in_data.xw();
const unsigned yw = in_data.yw();
assert( mask->xw() == xw && mask->yw() == yw );
const unsigned xw_bl = xw / blocksize + 1;
const unsigned yw_bl = yw / blocksize + 1;
// default, we see all pixels
mask->set_all(1);
for(unsigned yb = 0; yb < yw_bl; ++yb)
for(unsigned xb = 0; xb < xw_bl; ++xb)
{
const unsigned sx = xb*blocksize;
const unsigned sy = yb*blocksize;
double sum = 0.;
// count counts in blocksize x blocksize region
for(unsigned dy=0; dy<blocksize; dy++)
for(unsigned dx=0; dx<blocksize; dx++)
{
const unsigned x = sx+dx, y = sy+dy;
if( x >= xw || y >= yw)
continue;
sum += in_data(x, y);
}
// no counts, therefore throw away
if( fabs(sum) < 1e-5 )
{
for(unsigned dy=0; dy<blocksize; dy++)
for(unsigned dx=0; dx<blocksize; dx++)
{
const unsigned x = sx+dx, y = sy+dy;
if( x >= xw || y >= yw)
continue;
mask->pixel(x, y) = 0;
}
}
}
cout << "Done\n";
}
// handy template to load an image
template<class T> void program::load_image(const string& filename, double *exposure,
T** image)
{
FITSFile dataset(filename);
if(exposure != 0)
{
double defval = 1.;
dataset.readKey("EXPOSURE", exposure, &defval);
}
dataset.readImage(image);
}
// write an image with some sensible headers
// copies keywords from indataset
template<class T> void program::save_image(const string& filename, const T& image,
FITSFile* indataset)
{
FITSFile dataset(filename, FITSFile::Create);
dataset.writeImage(image);
indataset->copyHeaderTo(dataset);
dataset.writeDatestamp("contbin");
ostringstream o;
o << "Generated by contbin (Jeremy Sanders 2014)\n"
<< "This filename: " << filename << '\n'
<< "Input image: " << _in_fname << '\n'
<< "Back image: " << _bg_fname << '\n'
<< "Mask image: " << _mask_fname << '\n'
<< "Smoothed image: " << _smoothed_fname << '\n'
<< "Expmap image: " << _expmap_fname << '\n'
<< "Back expmap image: " << _bg_expmap_fname << '\n'
<< "Noise map image: " << _noisemap_fname << '\n'
<< "SN threshold: " << _sn_threshold << '\n'
<< "Smooth SN: " << _smooth_sn << '\n'
<< "Automask: " << _do_automask << '\n'
<< "Constrain fill: " << _constrain_fill << '\n'
<< "Constrain val: " << _constrain_val << '\n'
<< "No scrub: " << _noscrub << '\n'
<< "Bin up: " << _binup << '\n'
<< "Scrub large: " << _scrub_large << '\n';
// split output text and write as lines of history
vector<string> items = split_string(o.str(), '\n');
for(vector<string>::const_iterator i = items.begin();
i != items.end(); ++i)
{
dataset.writeHistory(*i);
}
}
void program::run()
{
/////////////////////////////////////////////////////////////////
// load input images
delete_ptr<image_float> in_image;
double in_exposure;
// load in the main dataset
// (keep open to copy header)
cout << "(i) Loading image " << _in_fname << '\n';
FITSFile indataset(_in_fname);
double defval = 1.;
indataset.readKey("EXPOSURE", &in_exposure, &defval);
indataset.readImage(in_image.pptr());
// do automasking (if any)
image_short mask(in_image->xw(), in_image->yw(), 1);
if( _do_automask )
auto_mask(*in_image, &mask);
// load mask (if any)
if( ! _mask_fname.empty() )
{
cout << "(i) Loading masking image " << _mask_fname << '\n';
image_short* maskim;
load_image( _mask_fname, 0, &maskim );
mask = *maskim;
delete maskim;
if(mask.xw() != in_image->xw() || mask.yw() != in_image->yw())
{
std::cerr << "(!) Input image does not match mask shape\n";
std::exit(1);
}
}
delete_ptr<image_float> expmap;
if( ! _expmap_fname.empty() )
{
cout << "(i) Loading foreground exposure map "
<< _expmap_fname << '\n';
load_image( _expmap_fname, 0, expmap.pptr() );
if(expmap->xw() != in_image->xw() || expmap->yw() != in_image->yw())
{
std::cerr << "(!) Input image does not match exposure map shape\n";
std::exit(1);
}
// mask out pixels with no exposure
for(unsigned y = 0; y != expmap->yw(); ++y)
for(unsigned x = 0; x != expmap->xw(); ++x)
{
if( (*expmap)(x, y) < 1. )
{
mask(x, y) = 0;
}
}
}
else
{
cout << "(i) Using blank foreground exposure (exp="
<< in_exposure << ")\n";
expmap = new image_float( in_image->xw(), in_image->yw(),
in_exposure );
}
delete_ptr<image_float> bg_image;
double bg_exposure;
// load in background file (if any)
if( ! _bg_fname.empty() )
{
cout << "(i) Loading background image " << _bg_fname << '\n';
load_image( _bg_fname, &bg_exposure, bg_image.pptr() );
if(bg_image->xw() != in_image->xw() || bg_image->yw() != in_image->yw())
{
std::cerr << "(!) Input image does not match background shape\n";
std::exit(1);
}
}
else
{
bg_exposure = 1;
}
// load in background exposure map
delete_ptr<image_float> bg_expmap;
if( ! _bg_expmap_fname.empty() )
{
cout << "(i) Loading background exposure map "
<< _bg_expmap_fname << '\n';
load_image( _bg_expmap_fname, 0, bg_expmap.pptr() );
if(bg_expmap->xw() != in_image->xw() || bg_expmap->yw() != in_image->yw())
{
std::cerr << "(!) Input image does not match background exposure map shape\n";
std::exit(1);
}
}
else
{
cout << "(i) Using blank background exposure (exp="
<< bg_exposure << ")\n";
bg_expmap = new image_float( in_image->xw(), in_image->yw(),
bg_exposure );
}
// we avoid division by zero by doing this hack
bg_expmap->trim_up(1e-7);
expmap->trim_up(1e-7);
// load in noise map if passed
delete_ptr<image_float> noisemap;
if( ! _noisemap_fname.empty() )
{
cout << "(i) Loading noise map " << _noisemap_fname << '\n';
load_image( _noisemap_fname, 0, noisemap.pptr());
if(noisemap->xw() != in_image->xw() || noisemap->yw() != in_image->yw())
{
std::cerr << "(!) Input image does not match noise map shape\n";
std::exit(1);
}
}
// smooth data, or use passed file
delete_ptr<image_float> smoothed_image;
if( _smoothed_fname.empty() )
{
cout << "(i) Smoothing data (S/N = "
<< _smooth_sn << ")\n";
// smooth data
flux_estimator fe( in_image.ptr(), bg_image.ptr(), &mask, expmap.ptr(),
bg_expmap.ptr(), noisemap.ptr(), _smooth_sn );
smoothed_image = new image_float( fe() );
}
else
{
cout << "(i) Loading smoothed image " << _smoothed_fname
<< '\n';
load_image( _smoothed_fname, 0, smoothed_image.pptr() );
if(smoothed_image->xw() != in_image->xw() || smoothed_image->yw() != in_image->yw())
{
std::cerr << "(!) Input image does not match smoothed image shape\n";
std::exit(1);
}
}
{
//////////////////////////////////////////////////////////////////
// actually do the binning
binner the_binner(in_image.ptr(), smoothed_image.ptr(), _sn_threshold);
the_binner.set_back_image( bg_image.ptr(), expmap.ptr(), bg_expmap.ptr() );
the_binner.set_noisemap_image(noisemap.ptr());
the_binner.set_mask_image(&mask);
the_binner.set_constrain_fill(_constrain_fill, _constrain_val);
the_binner.set_scrub_large_bins(_scrub_large);
the_binner.do_binning(!_binup);
if( ! _noscrub )
the_binner.do_scrub();
the_binner.calc_outputs();
///////////////////////////////////////////////////////////////////
// write output images
save_image(_out_fname, the_binner.get_output_image(), &indataset);
save_image(_sn_fname, the_binner.get_sn_image(), &indataset);
save_image(_binmap_fname, the_binner.get_binmap_image(), &indataset);
save_image("contbin_mask.fits", mask, &indataset);
}
}
int main(int argc, char *argv[])
{
program prog(argc, argv);
prog.run();
return 0;
}