00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <math.h>
00030
00031 #include "imcore.h"
00032 #include "imcore_radii.h"
00033 #include "floatmath.h"
00034 #include "util.h"
00035 #include "ap.h"
00036
00037 static float fraction (float x, float y, float r_out);
00038
00041
00074
00075
00076 extern float imcore_exprad(float thresh, float peak, float areal0,
00077 float rcores[], int naper) {
00078 float pk,r_t,rad;
00079
00080
00081
00082 pk = MAX(1.5*thresh,peak);
00083 r_t = sqrtf(areal0/CPL_MATH_PI);
00084 rad = 5.0*r_t/logf(pk/thresh);
00085 rad = MAX(r_t,MIN(5.0*r_t,MIN(rad,rcores[naper-1])));
00086 return(rad);
00087 }
00088
00089
00120
00121
00122 extern float imcore_kronrad(float areal0, float rcores[], float cflux[],
00123 int naper) {
00124 int i,imax;
00125 float r_t,rad,wt,sum;
00126
00127
00128
00129 r_t = sqrtf(areal0/CPL_MATH_PI);
00130 rad = 0.5*rcores[0]*cflux[0];
00131 sum = cflux[0];
00132 imax = MIN(naper,7);
00133 for (i = 1; i < imax; i++) {
00134 wt = MAX(0.0,cflux[i]-cflux[i-1]);
00135 rad += 0.5*(rcores[i] + rcores[i-1])*wt;
00136 sum += wt;
00137 }
00138 rad /= sum;
00139 rad = MAX(r_t,MIN(5.0*r_t,MIN(2.0*rad,rcores[naper-1])));
00140 return(rad);
00141 }
00142
00143
00174
00175
00176 extern float imcore_petrad (float areal0, float rcores[], float cflux[],
00177 int naper) {
00178 int j;
00179 float eta,r_t,etaold,r1,r2,r3,r4,r5,r_petr;
00180
00181
00182
00183 r_t = sqrtf(areal0/CPL_MATH_PI);
00184 eta = 1.0;
00185 etaold = eta;
00186 j = 1;
00187 while (eta > 0.2 && j < naper) {
00188 etaold = eta;
00189 r1 = rcores[j]*rcores[j]/(rcores[j-1]*rcores[j-1]) - 1.0;
00190 r2 = cflux[j]/cflux[j-1] - 1.0;
00191 eta = r2/r1;
00192 j++;
00193 }
00194 if (j == naper) {
00195 r_petr = rcores[naper-1];
00196 } else {
00197 r1 = rcores[j]*rcores[j];
00198 r2 = rcores[j-1]*rcores[j-1];
00199 r3 = rcores[j-2]*rcores[j-2];
00200 r4 = (etaold - 0.2)/(etaold - eta);
00201 r5 = (0.2 - eta)/(etaold - eta);
00202 r_petr = r4*sqrt(0.5*(r1 + r2)) + r5*sqrt(0.5*(r2 + r3));
00203 }
00204 r_petr = MAX(r_t,MIN(5.0*r_t,MIN(2.0*r_petr,rcores[naper-1])));
00205 return(r_petr);
00206 }
00207
00208
00248
00249
00250 void imcore_flux(ap_t *ap, float parm[IMNUM][NPAR], int nbit, float apers[],
00251 float fluxes[], int nr, float rcores[], float rfluxes[]) {
00252 float *map,t,xj,yj,sumiso,sumcf,delr;
00253 unsigned char *mflag,mf;
00254 long nx,ny;
00255 int xmin,xmax,ymin,ymax,ix1,ix2,iy1,iy2,i,j,kk,n;
00256
00257
00258
00259 map = ap->indata;
00260 mflag = ap->mflag;
00261 nx = ap->lsiz;
00262 ny = ap->csiz;
00263
00264
00265
00266 if (nbit == 1) {
00267
00268
00269
00270 xmin = parm[0][1] - apers[0] - 0.5;
00271 xmax = parm[0][1] + apers[0] + 0.5;
00272 ymin = parm[0][2] - apers[0] - 0.5;
00273 ymax = parm[0][2] + apers[0] + 0.5;
00274 ix1 = MAX(0,(int)xmin-1);
00275 ix2 = MIN(nx-1,(int)xmax);
00276 iy1 = MAX(0,(int)ymin-1);
00277 iy2 = MIN(ny-1,(int)ymax);
00278
00279
00280
00281
00282 fluxes[0] = 0.0;
00283 for(j = iy1; j <= iy2; j++) {
00284 kk = j*nx;
00285 for(i = ix1; i <= ix2; i++) {
00286 mf = mflag[kk+i];
00287 if (mf == MF_CLEANPIX || mf == MF_OBJPIX ||
00288 mf == MF_SATURATED) {
00289 t = map[kk+i];
00290 xj = (float)i - parm[0][1] + 1.0;
00291 yj = (float)j - parm[0][2] + 1.0;
00292 fluxes[0] += fraction(xj,yj,apers[0])*t;
00293 }
00294 }
00295 }
00296 if (fluxes[0] <= 0)
00297 fluxes[0] = parm[0][0];
00298
00299
00300
00301 } else {
00302
00303
00304
00305 sumiso = 0.0;
00306 sumcf = 0.0;
00307 for (j = 0; j < nbit; j++) {
00308 sumiso += parm[j][0];
00309 n = 1;
00310 while (rcores[n] < apers[j] && n < nr-1)
00311 n++;
00312 delr = (rcores[n] - apers[j])/(rcores[n] - rcores[n-1]);
00313 fluxes[j] = rfluxes[j*nr+n]*(1.0 - delr) + rfluxes[j*nr+n-1]*delr;
00314 sumcf += fluxes[j];
00315 }
00316
00317
00318
00319
00320 for (j = 0; j < nbit; j++) {
00321 fluxes[j] = sumcf*parm[j][0]/MAX(1.0,sumiso);
00322 if (fluxes[j] < 0.0)
00323 fluxes[j] = parm[j][0];
00324 }
00325 }
00326 }
00327
00330
00331
00332
00333
00334 static float fraction (float x, float y, float r_out) {
00335 float r,t,x_a,x_b,frac,tanao2,cosa,tanp2a,sqrt2o2;
00336
00337 r = sqrtf(x*x + y*y);
00338 sqrt2o2 = 0.5*CPL_MATH_SQRT2;
00339
00340
00341
00342 if(r > r_out+sqrt2o2)
00343 return(0.0);
00344
00345
00346
00347 if(r < r_out-sqrt2o2)
00348 return(1.0);
00349
00350
00351
00352
00353 x = fabsf(x);
00354 y = fabsf(y);
00355 if(y > x) {
00356 t = x;
00357 x = y;
00358 y = t;
00359 }
00360
00361
00362
00363 if (x > 0.0 && y > 0.0) {
00364 tanao2 = 0.5*y/x;
00365 tanp2a = x/y;
00366 cosa = x/sqrt(x*x + y*y);
00367 } else {
00368 tanao2 = 0.00005;
00369 tanp2a = 10000.0;
00370 cosa = 1.0;
00371 }
00372
00373
00374
00375 x_a = x - tanao2 + (r_out - r)/cosa;
00376 if(x_a < x+0.5) {
00377
00378
00379
00380 x_b = x + tanao2 + (r_out - r)/cosa;
00381
00382
00383
00384 if(x_a < x-0.5)
00385 frac = 0.5*MAX(0.0,x_b-(x-0.5))*MAX(0.0,x_b-(x-0.5))*tanp2a;
00386 else {
00387 if(x_b > x+0.5)
00388 frac = 1.0 - 0.5*(x+0.5-x_a)*(x+0.5-x_a)*tanp2a;
00389 else
00390 frac = 0.5-(x-x_a)+0.5*(x_b-x_a);
00391 }
00392 } else
00393 frac = 1.0;
00394
00395 return(frac);
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423