-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_region_files.cc
221 lines (176 loc) · 4.78 KB
/
make_region_files.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
// code takes a binmap and extracts a set of region files
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <sstream>
#include "parammm/parammm.hh"
#include "misc.hh"
#include "fitsio_simple.hh"
using namespace std;
const char* c_prog_version = "0.4";
class extractor
{
public:
extractor(const string& binmapfile, const string& outdir);
~extractor();
void set_binning(double minx, double miny, double binning);
void extract();
private:
// get region file for bin, returns false if error
bool extract_bin(int no, const string& outfname);
private:
double _minx, _miny, _binning;
image_long* _binmap;
string _outdir;
};
extractor::extractor(const string& binmapfile, const string& outdir)
: _minx(0), _miny(0), _binning(1),
_outdir(outdir)
{
FITSFile file(binmapfile);
file.readImage(&_binmap);
}
extractor::~extractor()
{
delete _binmap;
}
void extractor::set_binning(double minx, double miny, double binning)
{
_minx = minx; _miny = miny; _binning = binning;
}
void extractor::extract()
{
int no = 0;
for(;;) {
ostringstream o;
o << _outdir << "/xaf_" << no << ".reg";
const string fname = o.str();
cout << "Bin " << no << " (" << fname << ")\n";
if( ! extract_bin(no, fname) )
break;
no++;
}
}
bool extractor::extract_bin(int no, const string& outfname)
{
const int ixw = _binmap->xw();
const int iyw = _binmap->yw();
int found = false;
dm::memimage<short> inbinimage(ixw, iyw);
for(int y=0; y<iyw; ++y)
for(int x=0; x<ixw; ++x)
if( (*_binmap)(x, y) == no )
{
inbinimage(x, y) = 1;
found = true;
}
if(!found) return false;
ofstream file( outfname.c_str() );
if( ! file ) {
cerr << "Unable writing to file " << outfname << endl;
exit(1);
}
file << "# Region file format: CIAO version 1.0\n";
// single pass rectangular bin identification
for(int y=0; y<iyw; ++y)
for(int x=0; x<ixw; ++x) {
// ignore if bin isn't set
if( not inbinimage(x, y) )
continue;
// try and extend pixel...
int xw=1, yw=1;
for(;;)
{
// check for y extend
bool yextend = true;
for(int xi = 0; xi < xw; xi++)
{
if( (y+yw) >= iyw || not inbinimage(x+xi, y+yw) )
{
yextend = false;
break;
}
}
// check for x extend
bool xextend = true;
for(int yi = 0; yi < yw; yi++)
{
if( (x+xw) >= ixw || not inbinimage(x+xw, y+yi) )
{
xextend = false;
break;
}
}
bool xandyextend = false;
if( (x+xw) < ixw && (y+yw) < iyw )
xandyextend = inbinimage(x+xw, y+yw);
if( ! xextend && ! yextend ) break;
if( xextend && yextend && !xandyextend ) xw++;
else
{
if( xextend ) xw++;
if( yextend ) yw++;
}
} // extend loop
// zero pixels in "bin"
for(int yi = 0; yi < yw; yi++)
for(int xi = 0; xi < xw; xi++)
inbinimage(x+xi, y+yi) = 0;
file << "rotbox("
<< _minx + _binning*(x + xw*0.5) << ','
<< _miny + _binning*(y + yw*0.5) << ','
<< _binning*xw << ','
<< _binning*yw << ','
<< "0)\n" ;
}
if( ! file ) {
cerr << "Error writing to " << outfname << endl;
exit(1);
}
return true;
}
int main(int argc, char *argv[])
{
parammm::param params(argc, argv);
params.set_autohelp("Usage: make_region_files [OPTION] "
"--minx=val --miny=val --bin=val "
"--outdir=dir/ binmap.fits\n"
"Create region files from binmap.\n"
"Written by Jeremy Sanders, 2002.",
"Report bugs to <[email protected]>");
double minx = 0, miny = 0, bin = 1;
string outdir = ".";
params.add_switch( parammm::pswitch("minx", 'x',
parammm::pdouble_opt(&minx),
"Set minimum sky x coord for "
"bin map (req)", "PIX") );
params.add_switch( parammm::pswitch("miny", 'y',
parammm::pdouble_opt(&miny),
"Set minimum sky y coord for "
"bin map (req)", "PIX") );
params.add_switch( parammm::pswitch("bin", 'b',
parammm::pdouble_opt(&bin),
"Set sky binning factor for "
"bin map (def 1)", "PIX") );
params.add_switch( parammm::pswitch("outdir", 'o',
parammm::pstring_opt(&outdir),
"Set output directory (def .)",
"DIR") );
params.enable_autohelp();
params.enable_at_expansion();
params.enable_autoversion(c_prog_version, "Jeremy Sanders",
"Released under the GPL");
params.interpret_and_catch();
if(params.args().size() != 1)
params.show_autohelp();
cout << "Input binmap: " << params.args()[0] << endl
<< "Output directory: " << outdir << endl
<< "Minimum x: " << minx << endl
<< "Minimum y: " << miny << endl
<< "Binning factor: " << bin << endl;
extractor e(params.args()[0], outdir);
e.set_binning(minx, miny, bin);
e.extract();
return 0;
}