Newer
Older
profit->iter = 0;
memset(dcovar, 0, profit->nparam*profit->nparam*sizeof(double));
lm_opts[1] = 1.0e-12;
lm_opts[2] = 1.0e-12;
lm_opts[3] = 1.0e-12;
profit_boundtounbound(profit, profit->paraminit, dparam, PARAM_ALLPARAMS);
niter = dlevmar_dif(profit_evaluate, dparam, NULL, profit->nfreeparam,
profit->nresi, niter, lm_opts, info, NULL, dcovar, profit);
profit_unboundtobound(profit, dparam, profit->paraminit, PARAM_ALLPARAMS);
/* Convert covariance matrix to bounded space */
profit_covarunboundtobound(profit, dcovar, profit->covar);
return niter;
}
/****** profit_printout *******************************************************
PROTO void profit_printout(int n_par, float* par, int m_dat, float* fvec,
void *data, int iflag, int iter, int nfev )
PURPOSE Provide a function to print out results to lmfit.
INPUT Number of fitted parameters,
pointer to the vector of parameters,
number of data points,
pointer to the vector of residuals (output),
pointer to the data structure (unused),
0 (init) 1 (outer loop) 2(inner loop) -1(terminated),
outer loop counter,
number of calls to evaluate().
OUTPUT -.
NOTES Input arguments are there only for compatibility purposes (unused)
AUTHOR E. Bertin (IAP)
VERSION 17/09/2008
***/
void profit_printout(int n_par, float* par, int m_dat, float* fvec,
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
void *data, int iflag, int iter, int nfev )
{
checkstruct *check;
profitstruct *profit;
char filename[256];
static int itero;
profit = (profitstruct *)data;
if (0 && (iter!=itero || iter<0))
{
if (iter<0)
itero++;
else
itero = iter;
sprintf(filename, "check_%d_%04d.fits", the_gal, itero);
check=initcheck(filename, CHECK_PROFILES, 0);
reinitcheck(the_field, check);
addcheck(check, profit->lmodpix, profit->objnaxisn[0],profit->objnaxisn[1],
profit->ix,profit->iy, 1.0);
reendcheck(the_field, check);
endcheck(check);
}
return;
}
/****** profit_evaluate ******************************************************
PROTO void profit_evaluate(double *par, double *fvec, int m, int n,
void *adata)
PURPOSE Provide a function returning residuals to levmar.
INPUT Pointer to the vector of parameters,
pointer to the vector of residuals (output),
number of model parameters,
number of data points,
pointer to a data structure (we use it for the profit structure here).
void profit_evaluate(double *dpar, double *fvec, int m, int n, void *adata)
profitstruct *profit;
profstruct **prof;
double *dpar0, *dresi;
float *modpixt, *profpixt, *resi,
tparam, val;
PIXTYPE *lmodpixt,*lmodpix2t, *objpix,*weight,
wval;
int c,f,i,p,q, fd,pd, jflag,sflag, nprof;
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
/* Detect "Jacobian-related" calls */
jflag = pd = fd = 0;
dpar0 = profit->dparam;
if (profit->iter)
{
f = q = 0;
for (p=0; p<profit->nparam; p++)
{
if (dpar[f] - dpar0[f] != 0.0)
{
pd = p;
fd = f;
q++;
}
if (profit->freeparam_flag[p])
f++;
}
if (f>0 && q==1)
jflag = 1;
}
if (jflag && !(profit->nprof==1 && profit->prof[0]->code == PROF_DIRAC))
{
prof = profit->prof;
nprof = profit->nprof;
/*-- "Jacobian call" */
tparam = profit->param[pd];
profit_unboundtobound(profit, &dpar[fd], &profit->param[pd], pd);
sflag = 1;
switch(profit->paramrevindex[pd])
{
case PARAM_BACK:
lmodpixt = profit->lmodpix;
lmodpix2t = profit->lmodpix2;
val = (profit->param[pd] - tparam);
for (i=profit->nobjpix;i--;)
*(lmodpix2t++) = val;
break;
case PARAM_X:
case PARAM_Y:
profit_resample(profit, profit->cmodpix, profit->lmodpix2, 1.0);
lmodpixt = profit->lmodpix;
lmodpix2t = profit->lmodpix2;
for (i=profit->nobjpix;i--;)
*(lmodpix2t++) -= *(lmodpixt++);
break;
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
case PARAM_SPHEROID_FLUX:
case PARAM_DISK_FLUX:
case PARAM_ARMS_FLUX:
case PARAM_BAR_FLUX:
if (nprof==1 && tparam != 0.0)
{
lmodpixt = profit->lmodpix;
lmodpix2t = profit->lmodpix2;
val = (profit->param[pd] - tparam) / tparam;
for (i=profit->nobjpix;i--;)
*(lmodpix2t++) = val**(lmodpixt++);
}
else
{
for (c=0; c<nprof; c++)
if (prof[c]->flux == &profit->param[pd])
break;
memcpy(profit->modpix, prof[c]->pix, profit->nmodpix*sizeof(float));
profit_convolve(profit, profit->modpix);
profit_resample(profit, profit->modpix, profit->lmodpix2,
profit->param[pd] - tparam);
}
break;
case PARAM_SPHEROID_REFF:
case PARAM_SPHEROID_ASPECT:
case PARAM_SPHEROID_POSANG:
case PARAM_SPHEROID_SERSICN:
sflag = 0; /* We are in the same switch */
for (c=0; c<nprof; c++)
if (prof[c]->code == PROF_SERSIC
|| prof[c]->code == PROF_DEVAUCOULEURS)
break;
case PARAM_DISK_SCALE:
case PARAM_DISK_ASPECT:
case PARAM_DISK_POSANG:
if (sflag)
for (c=0; c<nprof; c++)
if (prof[c]->code == PROF_EXPONENTIAL)
break;
sflag = 0;
case PARAM_ARMS_QUADFRAC:
case PARAM_ARMS_SCALE:
case PARAM_ARMS_START:
case PARAM_ARMS_POSANG:
case PARAM_ARMS_PITCH:
case PARAM_ARMS_PITCHVAR:
case PARAM_ARMS_WIDTH:
if (sflag)
for (c=0; c<nprof; c++)
if (prof[c]->code == PROF_ARMS)
break;
sflag = 0;
case PARAM_BAR_ASPECT:
case PARAM_BAR_POSANG:
if (sflag)
for (c=0; c<nprof; c++)
if (prof[c]->code == PROF_ARMS)
break;
modpixt = profit->modpix;
profpixt = prof[c]->pix;
val = -*prof[c]->flux;
for (i=profit->nmodpix;i--;)
*(modpixt++) = val**(profpixt++);
memcpy(profit->modpix2, prof[c]->pix, profit->nmodpix*sizeof(float));
prof_add(profit, prof[c], 0);
memcpy(prof[c]->pix, profit->modpix2, profit->nmodpix*sizeof(float));
profit_convolve(profit, profit->modpix);
profit_resample(profit, profit->modpix, profit->lmodpix2, 1.0);
break;
default:
error(EXIT_FAILURE, "*Internal Error*: ",
"unknown parameter index in profit_jacobian()");
break;
}
objpix = profit->objpix;
weight = profit->objweight;
lmodpixt = profit->lmodpix;
lmodpix2t = profit->lmodpix2;
resi = profit->resi;
dresi = fvec;
if (PROFIT_DYNPARAM > 0.0)
for (i=profit->nobjpix;i--; lmodpixt++, lmodpix2t++)
{
val = *(objpix++);
if ((wval=*(weight++))>0.0)
*(dresi++) = *(resi++) + *lmodpix2t
* wval/(1.0+wval*fabs(*lmodpixt - val)/PROFIT_DYNPARAM);
}
else
for (i=profit->nobjpix;i--; lmodpix2t++)
if ((wval=*(weight++))>0.0)
*(dresi++) = *(resi++) + *lmodpix2t * wval;
}
else
{
/*-- "Regular call" */
for (p=0; p<profit->nparam; p++)
dpar0[p] = dpar[p];
profit_unboundtobound(profit, dpar, profit->param, PARAM_ALLPARAMS);
profit_residuals(profit, the_field, the_wfield, PROFIT_DYNPARAM,
profit->param, profit->resi);
for (p=0; p<profit->nresi; p++)
fvec[p] = profit->resi[p];
}
// profit_printout(m, par, n, fvec, adata, 0, -1, 0 );
profit->iter++;
return;
}
/****** profit_residuals ******************************************************
PROTO float *prof_residuals(profitstruct *profit, picstruct *field,
picstruct *wfield, float dynparam, float *param, float *resi)
PURPOSE Compute the vector of residuals between the data and the galaxy
profile model.
INPUT Profile-fitting structure,
pointer to the field,
pointer to the field weight,
dynamic compression parameter (0=no compression),
pointer to the model parameters (output),
pointer to the computed residuals (output).
OUTPUT Vector of residuals.
NOTES -.
AUTHOR E. Bertin (IAP)
float *profit_residuals(profitstruct *profit, picstruct *field,
picstruct *wfield, float dynparam, float *param, float *resi)
nmodpix = profit->modnaxisn[0]*profit->modnaxisn[1]*sizeof(float);
memset(profit->modpix, 0, nmodpix);
for (p=0; p<profit->nparam; p++)
profit->param[p] = param[p];
/* Simple PSF shortcut */
if (profit->nprof == 1 && profit->prof[0]->code == PROF_DIRAC)
profit_resample(profit, profit->psfpix, profit->lmodpix,
*profit->prof[0]->flux);
profit->flux = *profit->prof[0]->flux;
}
else
{
profit->flux = 0.0;
for (p=0; p<profit->nprof; p++)
profit->flux += prof_add(profit, profit->prof[p], 0);
memcpy(profit->cmodpix, profit->modpix, profit->nmodpix*sizeof(float));
profit_convolve(profit, profit->cmodpix);
profit_resample(profit, profit->cmodpix, profit->lmodpix, 1.0);
if (resi)
profit_compresi(profit, dynparam, resi);
return resi;
}
/****** profit_compresi ******************************************************
PROTO float *prof_compresi(profitstruct *profit, float dynparam,
float *resi)
PURPOSE Compute the vector of residuals between the data and the galaxy
profile model.
INPUT Profile-fitting structure,
dynamic-compression parameter (0=no compression),
vector of residuals (output).
OUTPUT Vector of residuals.
NOTES -.
AUTHOR E. Bertin (IAP)
float *profit_compresi(profitstruct *profit, float dynparam, float *resi)
double error;
float *resit;
PIXTYPE *objpix, *objweight, *lmodpix,
val,val2,wval, invsig;
/* Compute vector of residuals */
resit = resi;
objpix = profit->objpix;
objweight = profit->objweight;
lmodpix = profit->lmodpix;
error = 0.0;
npix = profit->objnaxisn[0]*profit->objnaxisn[1];
if (dynparam > 0.0)
invsig = (PIXTYPE)(1.0/dynparam);
for (i=npix; i--; lmodpix++)
{
val = *(objpix++);
if ((wval=*(objweight++))>0.0)
{
val2 = (*lmodpix - val)*wval*invsig;
val2 = val2>0.0? logf(1.0+val2) : -logf(1.0-val2);
profit->chi2 = dynparam*dynparam*error;
}
else
{
for (i=npix; i--; lmodpix++)
{
val = *(objpix++);
if ((wval=*(objweight++))>0.0)
{
val2 = (*lmodpix - val)*wval;
*(resit++) = val2;
error += val2*val2;
}
}
profit->chi2 = error;
}
return resi;
}
/****** profit_resample ******************************************************
PROTO int prof_resample(profitstruct *profit, float *inpix,
PIXTYPE *outpix, float factor)
PURPOSE Resample the current full resolution model to image resolution.
INPUT Profile-fitting structure,
pointer to input raster,
pointer to output raster,
multiplicating factor.
OUTPUT RETURN_ERROR if the rasters don't overlap, RETURN_OK otherwise.
VERSION 12/09/2010
int profit_resample(profitstruct *profit, float *inpix, PIXTYPE *outpix,
PIXTYPE *pixout,*pixout0;
float *pixin,*pixin0, *mask,*maskt, *pixinout, *dpixin,*dpixin0,
*dpixout,*dpixout0, *dx,*dy,
xcin,xcout,ycin,ycout, xsin,ysin, xin,yin, x,y, dxm,dym, val,
invpixstep, norm;
int *start,*startt, *nmask,*nmaskt,
i,j,k,n,t,
ixsout,iysout, ixout,iyout, dixout,diyout, nxout,nyout,
iysina, nyin, hmw,hmh, ix,iy, ixin,iyin;
invpixstep = profit->subsamp/profit->pixstep;
xcin = (profit->modnaxisn[0]/2);
xcout = (float)(profit->objnaxisn[0]/2);
if ((dx=(profit->paramlist[PARAM_X])))
xcout += *dx;
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
xsin = xcin - xcout*invpixstep; /* Input start x-coord*/
if ((int)xsin >= profit->modnaxisn[0])
return RETURN_ERROR;
ixsout = 0; /* Int. part of output start x-coord */
if (xsin<0.0)
{
dixout = (int)(1.0-xsin/invpixstep);
/*-- Simply leave here if the images do not overlap in x */
if (dixout >= profit->objnaxisn[0])
return RETURN_ERROR;
ixsout += dixout;
xsin += dixout*invpixstep;
}
nxout = (int)((profit->modnaxisn[0]-xsin)/invpixstep);/* nb of interpolated
input pixels along x */
if (nxout>(ixout=profit->objnaxisn[0]-ixsout))
nxout = ixout;
if (!nxout)
return RETURN_ERROR;
ycin = (profit->modnaxisn[1]/2);
ycout = (float)(profit->objnaxisn[1]/2);
if ((dy=(profit->paramlist[PARAM_Y])))
ycout += *dy;
ysin = ycin - ycout*invpixstep; /* Input start y-coord*/
if ((int)ysin >= profit->modnaxisn[1])
return RETURN_ERROR;
iysout = 0; /* Int. part of output start y-coord */
if (ysin<0.0)
diyout = (int)(1.0-ysin/invpixstep);
/*-- Simply leave here if the images do not overlap in y */
if (diyout >= profit->objnaxisn[1])
return RETURN_ERROR;
iysout += diyout;
ysin += diyout*invpixstep;
nyout = (int)((profit->modnaxisn[1]-ysin)/invpixstep);/* nb of interpolated
input pixels along y */
if (nyout>(iyout=profit->objnaxisn[1]-iysout))
nyout = iyout;
if (!nyout)
return RETURN_ERROR;
/* Set the yrange for the x-resampling with some margin for interpolation */
iysina = (int)ysin; /* Int. part of Input start y-coord with margin */
hmh = INTERPW/2 - 1; /* Interpolant start */
if (iysina<0 || ((iysina -= hmh)< 0))
iysina = 0;
nyin = (int)(ysin+nyout*invpixstep)+INTERPW-hmh;/* Interpolated Input y size*/
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
if (nyin>profit->modnaxisn[1]) /* with margin */
nyin = profit->modnaxisn[1];
/* Express everything relative to the effective Input start (with margin) */
nyin -= iysina;
ysin -= (float)iysina;
/* Allocate interpolant stuff for the x direction */
QMALLOC(mask, float, nxout*INTERPW); /* Interpolation masks */
QMALLOC(nmask, int, nxout); /* Interpolation mask sizes */
QMALLOC(start, int, nxout); /* Int. part of Input conv starts */
/* Compute the local interpolant and data starting points in x */
hmw = INTERPW/2 - 1;
xin = xsin;
maskt = mask;
nmaskt = nmask;
startt = start;
for (j=nxout; j--; xin+=invpixstep)
{
ix = (ixin=(int)xin) - hmw;
dxm = ixin - xin - hmw; /* starting point in the interpolation func */
if (ix < 0)
n = INTERPW+ix;
dxm -= (float)ix;
ix = 0;
else
n = INTERPW;
if (n>(t=profit->modnaxisn[0]-ix))
n=t;
*(startt++) = ix;
*(nmaskt++) = n;
for (x=dxm, i=n; i--; x+=1.0)
norm += (*(maskt++) = INTERPF(x));
norm = norm>0.0? 1.0/norm : 1.0;
maskt -= n;
for (i=n; i--;)
*(maskt++) *= norm;
QCALLOC(pixinout, float, nxout*nyin); /* Intermediary frame-buffer */
/* Make the interpolation in x (this includes transposition) */
pixin0 = inpix + iysina*profit->modnaxisn[0];
dpixout0 = pixinout;
for (k=nyin; k--; pixin0+=profit->modnaxisn[0], dpixout0++)
{
maskt = mask;
nmaskt = nmask;
startt = start;
dpixout = dpixout0;
for (j=nxout; j--; dpixout+=nyin)
pixin = pixin0+*(startt++);
val = 0.0;
for (i=*(nmaskt++); i--;)
val += *(maskt++)**(pixin++);
*dpixout = val;
/* Reallocate interpolant stuff for the y direction */
QREALLOC(mask, float, nyout*INTERPW); /* Interpolation masks */
QREALLOC(nmask, int, nyout); /* Interpolation mask sizes */
QREALLOC(start, int, nyout); /* Int. part of Input conv starts */
/* Compute the local interpolant and data starting points in y */
hmh = INTERPW/2 - 1;
yin = ysin;
maskt = mask;
nmaskt = nmask;
startt = start;
for (j=nyout; j--; yin+=invpixstep)
{
iy = (iyin=(int)yin) - hmh;
dym = iyin - yin - hmh; /* starting point in the interpolation func */
if (iy < 0)
{
n = INTERPW+iy;
dym -= (float)iy;
iy = 0;
}
else
n = INTERPW;
if (n>(t=nyin-iy))
n=t;
*(startt++) = iy;
*(nmaskt++) = n;
for (y=dym, i=n; i--; y+=1.0)
norm += (*(maskt++) = INTERPF(y));
norm = norm>0.0? 1.0/norm : 1.0;
maskt -= n;
for (i=n; i--;)
*(maskt++) *= norm;
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
}
/* Initialize destination buffer to zero */
memset(outpix, 0, (size_t)profit->nobjpix*sizeof(PIXTYPE));
/* Make the interpolation in y and transpose once again */
dpixin0 = pixinout;
pixout0 = outpix+ixsout+iysout*profit->objnaxisn[0];
for (k=nxout; k--; dpixin0+=nyin, pixout0++)
{
maskt = mask;
nmaskt = nmask;
startt = start;
pixout = pixout0;
for (j=nyout; j--; pixout+=profit->objnaxisn[0])
{
dpixin = dpixin0+*(startt++);
val = 0.0;
for (i=*(nmaskt++); i--;)
val += *(maskt++)**(dpixin++);
*pixout = (PIXTYPE)(factor*val);
}
}
/* Free memory */
free(pixinout);
free(mask);
free(nmask);
free(start);
return RETURN_OK;
}
/****** profit_convolve *******************************************************
PROTO void profit_convolve(profitstruct *profit, float *modpix)
PURPOSE Convolve a model image with the local PSF.
INPUT Pointer to the profit structure,
Pointer to the image raster.
OUTPUT -.
NOTES -.
AUTHOR E. Bertin (IAP)
VERSION 15/09/2008
***/
void profit_convolve(profitstruct *profit, float *modpix)
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
{
if (!profit->psfdft)
profit_makedft(profit);
fft_conv(modpix, profit->psfdft, profit->modnaxisn);
return;
}
/****** profit_makedft *******************************************************
PROTO void profit_makedft(profitstruct *profit)
PURPOSE Create the Fourier transform of the descrambled PSF component.
INPUT Pointer to the profit structure.
OUTPUT -.
NOTES -.
AUTHOR E. Bertin (IAP)
VERSION 22/04/2008
***/
void profit_makedft(profitstruct *profit)
{
psfstruct *psf;
float *mask,*maskt, *ppix;
float dx,dy, r,r2,rmin,rmin2,rmax,rmax2,rsig,invrsig2;
int width,height,npix,offset, psfwidth,psfheight,psfnpix,
cpwidth, cpheight,hcpwidth,hcpheight, i,j,x,y;
if (!(psf=profit->psf))
return;
psfwidth = profit->modnaxisn[0];
psfheight = profit->modnaxisn[1];
psfnpix = psfwidth*psfheight;
width = profit->modnaxisn[0];
height = profit->modnaxisn[1];
npix = width*height;
QCALLOC(mask, float, npix);
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
cpwidth = (width>psfwidth)?psfwidth:width;
hcpwidth = cpwidth>>1;
cpwidth = hcpwidth<<1;
offset = width - cpwidth;
cpheight = (height>psfheight)?psfheight:height;
hcpheight = cpheight>>1;
cpheight = hcpheight<<1;
/* Frame and descramble the PSF data */
ppix = profit->psfpix + (psfheight/2)*psfwidth + psfwidth/2;
maskt = mask;
for (j=hcpheight; j--; ppix+=psfwidth)
{
for (i=hcpwidth; i--;)
*(maskt++) = *(ppix++);
ppix -= cpwidth;
maskt += offset;
for (i=hcpwidth; i--;)
*(maskt++) = *(ppix++);
}
ppix = profit->psfpix + ((psfheight/2)-hcpheight)*psfwidth + psfwidth/2;
maskt += width*(height-cpheight);
for (j=hcpheight; j--; ppix+=psfwidth)
{
for (i=hcpwidth; i--;)
*(maskt++) = *(ppix++);
ppix -= cpwidth;
maskt += offset;
for (i=hcpwidth; i--;)
*(maskt++) = *(ppix++);
}
/* Truncate to a disk that has diameter = (box width) */
rmax = cpwidth - 1.0 - hcpwidth;
if (rmax > (r=hcpwidth))
rmax = r;
if (rmax > (r=cpheight-1.0-hcpheight))
rmax = r;
if (rmax > (r=hcpheight))
rmax = r;
if (rmax<1.0)
rmax = 1.0;
rmax2 = rmax*rmax;
rsig = psf->fwhm/profit->pixstep;
invrsig2 = 1/(2*rsig*rsig);
rmin = rmax - (3*rsig); /* 3 sigma annulus (almost no aliasing) */
rmin2 = rmin*rmin;
maskt = mask;
dy = 0.0;
for (y=hcpheight; y--; dy+=1.0)
{
dx = 0.0;
for (x=hcpwidth; x--; dx+=1.0, maskt++)
if ((r2=dx*dx+dy*dy)>rmin2)
*maskt *= (r2>rmax2)?0.0:expf((2*rmin*sqrtf(r2)-r2-rmin2)*invrsig2);
dx = -hcpwidth;
maskt += offset;
for (x=hcpwidth; x--; dx+=1.0, maskt++)
if ((r2=dx*dx+dy*dy)>rmin2)
*maskt *= (r2>rmax2)?0.0:expf((2*rmin*sqrtf(r2)-r2-rmin2)*invrsig2);
}
dy = -hcpheight;
maskt += width*(height-cpheight);
for (y=hcpheight; y--; dy+=1.0)
{
dx = 0.0;
for (x=hcpwidth; x--; dx+=1.0, maskt++)
if ((r2=dx*dx+dy*dy)>rmin2)
*maskt *= (r2>rmax2)?0.0:expf((2*rmin*sqrtf(r2)-r2-rmin2)*invrsig2);
dx = -hcpwidth;
maskt += offset;
for (x=hcpwidth; x--; dx+=1.0, maskt++)
if ((r2=dx*dx+dy*dy)>rmin2)
*maskt *= (r2>rmax2)?0.0:expf((2*rmin*sqrtf(r2)-r2-rmin2)*invrsig2);
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
}
/* Finally move to Fourier space */
profit->psfdft = fft_rtf(mask, profit->modnaxisn);
free(mask);
return;
}
/****** profit_copyobjpix *****************************************************
PROTO int profit_copyobjpix(profitstruct *profit, picstruct *field,
picstruct *wfield)
PURPOSE Copy a piece of the input field image to a profit structure.
INPUT Pointer to the profit structure,
Pointer to the field structure,
Pointer to the field weight structure.
OUTPUT The number of valid pixels copied.
NOTES Global preferences are used.
AUTHOR E. Bertin (IAP)
Emmanuel Bertin
committed
VERSION 01/12/2009
***/
int profit_copyobjpix(profitstruct *profit, picstruct *field,
picstruct *wfield)
{
Emmanuel Bertin
committed
float dx, dy2, dr2, rad2;
PIXTYPE *pixin,*spixin, *wpixin,*swpixin, *pixout,*wpixout,
backnoise2, invgain, satlevel, wthresh, pix,spix, wpix,swpix;
int i,x,y, xmin,xmax,ymin,ymax, w,h,dw, npix, off, gainflag,
Emmanuel Bertin
committed
badflag, sflag, sx,sy,sn,sw, ix,iy;
/* First put the image background to -BIG */
pixout = profit->objpix;
wpixout = profit->objweight;
Emmanuel Bertin
committed
for (i=profit->objnaxisn[0]*profit->objnaxisn[1]; i--;)
{
*(pixout++) = -BIG;
*(wpixout++) = 0.0;
}
/* Don't go further if out of frame!! */
ix = profit->ix;
iy = profit->iy;
if (ix<0 || ix>=field->width || iy<field->ymin || iy>=field->ymax)
return 0;
backnoise2 = field->backsig*field->backsig;
sn = (int)profit->subsamp;
Emmanuel Bertin
committed
sflag = (sn>1);
w = profit->objnaxisn[0]*sn;
h = profit->objnaxisn[1]*sn;
if (sflag)
backnoise2 *= (PIXTYPE)sn;
invgain = (field->gain > 0.0) ? 1.0/field->gain : 0.0;
satlevel = field->satur_level - profit->obj->bkg;
rad2 = h/2.0;
if (rad2 > w/2.0)
rad2 = w/2.0;
rad2 *= rad2;
/* Set the image boundaries */
pixout = profit->objpix;
wpixout = profit->objweight;
ymin = iy-h/2;
ymax = ymin + h;
if (ymin<field->ymin)
{
Emmanuel Bertin
committed
off = (field->ymin-ymin-1)/sn + 1;
pixout += off*profit->objnaxisn[0];
wpixout += off*profit->objnaxisn[0];
ymin += off*sn;
Emmanuel Bertin
committed
ymax -= ((ymax-field->ymax-1)/sn + 1)*sn;
Emmanuel Bertin
committed
dw = 0;
Emmanuel Bertin
committed
off = (xmax-field->width-1)/sn + 1;
dw += off;
xmax -= off*sn;
Emmanuel Bertin
committed
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
off = (-xmin-1)/sn + 1;
pixout += off;
wpixout += off;
dw += off;
xmin += off*sn;
}
/* Make sure the input frame size is a multiple of the subsampling step */
if (sflag)
{
/*
if (((rem=ymax-ymin)%sn))
{
ymin += rem/2;
ymax -= (rem-rem/2);
}
if (((rem=xmax-xmin)%sn))
{
xmin += rem/2;
pixout += rem/2;
wpixout += rem/2;
dw += rem;
xmax -= (rem-rem/2);
}
*/
sw = field->width;
}
/* Copy the right pixels to the destination */
npix = 0;
if (wfield)
{
wthresh = wfield->weight_thresh;
gainflag = prefs.weightgain_flag;
Emmanuel Bertin
committed
if (sflag)
Emmanuel Bertin
committed
/*---- Sub-sampling case */
for (y=ymin; y<ymax; y+=sn, pixout+=dw,wpixout+=dw)
Emmanuel Bertin
committed
for (x=xmin; x<xmax; x+=sn)
Emmanuel Bertin
committed
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
pix = wpix = 0.0;
badflag = 0;
for (sy=0; sy<sn; sy++)
{
dy2 = (y+sy-iy);
dy2 *= dy2;
dx = (x-ix);
spixin = &PIX(field, x, y+sy);
swpixin = &PIX(wfield, x, y+sy);
for (sx=sn; sx--;)
{
dr2 = dy2 + dx*dx;
dx++;
spix = *(spixin++);
swpix = *(swpixin++);
if (dr2<rad2 && spix>-BIG && spix<satlevel && swpix<wthresh)
{
pix += spix;
wpix += swpix;
}
else
badflag=1;
}
}
*(pixout++) = pix;
if (!badflag) /* A single bad pixel ruins is all (saturation, etc.)*/
{
*(wpixout++) = 1.0 / sqrt(wpix+(pix>0.0?
(gainflag? pix*wpix/backnoise2:pix)*invgain : 0.0));
Emmanuel Bertin
committed
npix++;
}
else
*(wpixout++) = 0.0;
Emmanuel Bertin
committed
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
else
for (y=ymin; y<ymax; y++, pixout+=dw,wpixout+=dw)
{
dy2 = y-iy;
dy2 *= dy2;
pixin = &PIX(field, xmin, y);
wpixin = &PIX(wfield, xmin, y);
for (x=xmin; x<xmax; x++)
{
dx = x-ix;
dr2 = dy2 + dx*dx;
pix = *(pixin++);
wpix = *(wpixin++);
if (dr2<rad2 && pix>-BIG && pix<satlevel && wpix<wthresh)
{
*(pixout++) = pix;
*(wpixout++) = 1.0 / sqrt(wpix+(pix>0.0?
(gainflag? pix*wpix/backnoise2:pix)*invgain : 0.0));
npix++;
}
else
*(pixout++) = *(wpixout++) = 0.0;
}
}
Emmanuel Bertin
committed
{
if (sflag)
Emmanuel Bertin
committed
/*---- Sub-sampling case */
for (y=ymin; y<ymax; y+=sn, pixout+=dw, wpixout+=dw)
Emmanuel Bertin
committed
for (x=xmin; x<xmax; x+=sn)
Emmanuel Bertin
committed
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
pix = 0.0;
badflag = 0;
for (sy=0; sy<sn; sy++)
{
dy2 = y+sy-iy;
dy2 *= dy2;
dx = x-ix;
spixin = &PIX(field, x, y+sy);
for (sx=sn; sx--;)
{
dr2 = dy2 + dx*dx;
dx++;
spix = *(spixin++);
if (dr2<rad2 && spix>-BIG && spix<satlevel)
pix += spix;
else
badflag=1;
}
}
*(pixout++) = pix;
if (!badflag) /* A single bad pixel ruins is all (saturation, etc.)*/
{
*(wpixout++) = 1.0 / sqrt(backnoise2 + (pix>0.0?pix*invgain:0.0));
npix++;
}
else
*(wpixout++) = 0.0;
Emmanuel Bertin
committed
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
else
for (y=ymin; y<ymax; y++, pixout+=dw,wpixout+=dw)
{
dy2 = y-iy;
dy2 *= dy2;
pixin = &PIX(field, xmin, y);
for (x=xmin; x<xmax; x++)
{
dx = x-ix;
dr2 = dy2 + dx*dx;
pix = *(pixin++);
if (dr2<rad2 && pix>-BIG && pix<satlevel)
{
*(pixout++) = pix;
*(wpixout++) = 1.0 / sqrt(backnoise2 + (pix>0.0?pix*invgain : 0.0));
npix++;
}
else
*(pixout++) = *(wpixout++) = 0.0;
}
}
}
return npix;
}
/****** profit_spiralindex ****************************************************
PROTO float profit_spiralindex(profitstruct *profit)
PURPOSE Compute the spiral index of a galaxy image (positive for arms
extending counter-clockwise and negative for arms extending CW, 0 for
no spiral pattern).
INPUT Profile-fitting structure.
OUTPUT Vector of residuals.
NOTES -.
AUTHOR E. Bertin (IAP)
VERSION 18/09/2008
***/
float profit_spiralindex(profitstruct *profit)
{
objstruct *obj;
obj2struct *obj2;
float *dx,*dy, *fdx,*fdy, *gdx,*gdy, *gdxt,*gdyt, *pix,
fwhm, invtwosigma2, hw,hh, ohw,ohh, x,y,xstart, tx,ty,txstart,
gx,gy, r2, spirindex, invsig, val, sep;
PIXTYPE *fpix;
int i,j, npix;
npix = profit->objnaxisn[0]*profit->objnaxisn[1];
obj = profit->obj;
obj2 = profit->obj2;
/* Compute simple derivative vectors at a fraction of the object scale */
fwhm = obj2->hl_radius * 2.0 / 4.0;
if (fwhm < 2.0)
fwhm = 2.0;
sep = 2.0;
invtwosigma2 = -(2.35*2.35/(2.0*fwhm*fwhm));
hw = (float)(profit->objnaxisn[0]/2);
hh = (float)(profit->objnaxisn[1]/2);