CR2RE Pipeline Reference Manual 1.6.8
hdrl_cat_radii.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2017 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "hdrl_cat_radii.h"
21
22#include "hdrl_cat_utils.h"
23
24
25/*---------------------------------------------------------------------------*/
33/*----------------------------------------------------------------------------*/
34
37/* ---------------------------------------------------------------------------*/
53/* ---------------------------------------------------------------------------*/
54double hdrl_halflight(double rcores[], double cflux[], double halflight, double peak, cpl_size naper)
55{
56 /* Work out the half-light value from either isophotal flux or the flux at
57 * Rcore. The find out roughly where the curve of growth exceeds this */
58
59 cpl_size gotone = 0;
60 cpl_size i;
61 for (i = 0; i < naper; i++) {
62 if (cflux[i] > halflight) {
63 gotone = 1;
64 break;
65 }
66 }
67
68 if (gotone == 0) i = naper - 1;
69
70 /* Now work out what the radius of half light is */
71 double halfrad;
72 if (i == 0) {
73
74 double delr = (cflux[i] - halflight) / CPL_MAX(1., cflux[i] - peak);
75
76 halfrad = rcores[0] * (1. - delr) + delr * sqrt(1. / CPL_MATH_PI);
77
78 } else {
79
80 double delr = (cflux[i] - halflight) / CPL_MAX(1., (cflux[i] - cflux[i - 1]));
81
82 halfrad = rcores[i - 1] * delr + rcores[i] * (1. - delr);
83 }
84
85 return halfrad;
86}
87
88/* ---------------------------------------------------------------------------*/
104/* ---------------------------------------------------------------------------*/
105double hdrl_exprad(double thresh, double peak, double areal0, double rcores[], cpl_size naper)
106{
107 double pk = CPL_MAX(1.5 * thresh, peak);
108
109 double r_t = sqrt(areal0 / CPL_MATH_PI);
110
111 double rad = CPL_MAX(r_t, CPL_MIN(5. * r_t, CPL_MIN(5. * r_t / log(pk / thresh), rcores[naper - 1])));
112
113 return rad;
114}
115
116/* ---------------------------------------------------------------------------*/
131/* ---------------------------------------------------------------------------*/
132double hdrl_kronrad(double areal0, double rcores[], double cflux[], cpl_size naper)
133{
134 double r_t = sqrt(areal0 / CPL_MATH_PI);
135 double rad = 0.5 * rcores[0] * cflux[0];
136 double sum = cflux[0];
137
138 cpl_size imax = CPL_MIN(naper, 7);
139 for (cpl_size i = 1; i < imax; i++) {
140
141 double wt = CPL_MAX(0., cflux[i] - cflux[i - 1]);
142
143 rad += 0.5 * (rcores[i] + rcores[i - 1]) * wt;
144 sum += wt;
145 }
146 rad /= sum;
147
148 rad = CPL_MAX(r_t, CPL_MIN(5. * r_t, CPL_MIN(2. * rad, rcores[naper - 1])));
149
150 return rad;
151}
152
153/* ---------------------------------------------------------------------------*/
168/* ---------------------------------------------------------------------------*/
169double hdrl_petrad(double areal0, double rcores[], double cflux[], cpl_size naper)
170{
171 double r_t = sqrt(areal0 / CPL_MATH_PI);
172
173 double eta = 1.;
174 double etaold = eta;
175
176 cpl_size j = 1;
177 while (eta > 0.2 && j < naper) {
178
179 etaold = eta;
180
181 double r1 = rcores[j] * rcores[j] / (rcores[j - 1] * rcores[j - 1]) - 1.;
182 double r2 = cflux[j] / cflux[j - 1] - 1.;
183
184 eta = r2 / r1;
185
186 j++;
187 }
188
189 double r_petr = rcores[naper - 1];
190 if (j != naper) {
191
192 double r1 = rcores[j] * rcores[j];
193 double r2 = rcores[j - 1] * rcores[j - 1];
194 double r3 = rcores[j - 2] * rcores[j - 2];
195 double r4 = (etaold - 0.2) / (etaold - eta);
196 double r5 = (0.2 - eta) / (etaold - eta);
197
198 r_petr = r4 * sqrt(0.5 * (r1 + r2)) + r5 * sqrt(0.5 * (r2 + r3));
199 }
200
201 r_petr = CPL_MAX(r_t, CPL_MIN(5. * r_t, CPL_MIN(2. * r_petr, rcores[naper-1])));
202
203 return r_petr;
204}
205
206/* ---------------------------------------------------------------------------*/
224/* ---------------------------------------------------------------------------*/
225void hdrl_flux(ap_t *ap, double parm[IMNUM][NPAR], cpl_size nbit, double apers[],
226 double fluxes[], cpl_size nr, double rcores[], double rfluxes[])
227{
228 /* Set up some local variables */
229 double *map = ap->indata;
230 unsigned char *mflag = ap->mflag;
231
232 cpl_size nx = ap->lsiz;
233 cpl_size ny = ap->csiz;
234
235 /* Section for nbit == 1 */
236 if (nbit == 1) {
237
238 /* Generate image-blend outer boundaries */
239
240 double xmin = parm[0][1] - apers[0] - 0.5;
241 double xmax = parm[0][1] + apers[0] + 0.5;
242 double ymin = parm[0][2] - apers[0] - 0.5;
243 double ymax = parm[0][2] + apers[0] + 0.5;
244
245 cpl_size ix1 = CPL_MAX( 0, (cpl_size)xmin - 1);
246 cpl_size ix2 = CPL_MIN(nx - 1, (cpl_size)xmax );
247 cpl_size iy1 = CPL_MAX( 0, (cpl_size)ymin - 1);
248 cpl_size iy2 = CPL_MIN(ny - 1, (cpl_size)ymax );
249
250 /* Now go through pixel region and add up the contributions inside the aperture */
251 fluxes[0] = 0.;
252 for (cpl_size j = iy1; j <= iy2; j++) {
253
254 cpl_size kk = j * nx;
255
256 for (cpl_size i = ix1; i <= ix2; i++) {
257
258 unsigned char mf = mflag[kk + i];
259
260 if (mf == MF_CLEANPIX || mf == MF_OBJPIX || mf == MF_SATURATED) {
261
262 double t = map[kk+i];
263 double xj = (double)i - parm[0][1] + 1.;
264 double yj = (double)j - parm[0][2] + 1.;
265 fluxes[0] += fraction(xj, yj, apers[0]) * t;
266 }
267 }
268 }
269
270 if (fluxes[0] <= 0) fluxes[0] = parm[0][0];
271
272 } else { /* Section for blended images */
273
274 /* Interpolate circular aperture fluxes */
275 double sumiso = 0.;
276 double sumcf = 0.;
277
278 for (cpl_size j = 0; j < nbit; j++) {
279
280 sumiso += parm[j][0];
281
282 cpl_size n = 1;
283 while (n < nr - 1 && rcores[n] < apers[j]) n++;
284
285 double delr = (rcores[n] - apers[j]) / (rcores[n] - rcores[n - 1]);
286
287 fluxes[j] = rfluxes[j * nr + n]*(1. - delr) + rfluxes[j * nr + n - 1] * delr;
288
289 sumcf += fluxes[j];
290 }
291
292 /* Constrain the result so that the ratios are the same as for the isophotal fluxes */
293 for (cpl_size j = 0; j < nbit; j++) {
294
295 fluxes[j] = sumcf * parm[j][0] / CPL_MAX(1., sumiso);
296
297 if (fluxes[j] < 0.) fluxes[j] = parm[j][0];
298 }
299 }
300}
301
void hdrl_flux(ap_t *ap, double parm[IMNUM][NPAR], cpl_size nbit, double apers[], double fluxes[], cpl_size nr, double rcores[], double rfluxes[])
Work out the fluxes for special radii.
double hdrl_exprad(double thresh, double peak, double areal0, double rcores[], cpl_size naper)
Work out the exponential radius for an object.
double hdrl_petrad(double areal0, double rcores[], double cflux[], cpl_size naper)
Work out the Petrosian radius for an object.
double hdrl_kronrad(double areal0, double rcores[], double cflux[], cpl_size naper)
Work out the Kron radius for an object.
double hdrl_halflight(double rcores[], double cflux[], double halflight, double peak, cpl_size naper)
Work out the half-light radius for an object.
double fraction(double x, double y, double r_out)
Fraction of pixel bounded.