profit.c 76.9 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
2
3
4
5
6
7
8
9
10
11
 /*
 				profit.c

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	Part of:	SExtractor
*
*	Authors:	E.BERTIN (IAP)
*
*	Contents:	Fit an arbitrary profile combination to a detection.
*
12
*	Last modify:	07/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
14
15
16
17
18
19
20
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

#ifdef HAVE_CONFIG_H
#include        "config.h"
#endif

21
#include	<math.h>
Emmanuel Bertin's avatar
Emmanuel Bertin committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

#include	"define.h"
#include	"globals.h"
#include	"prefs.h"
#include	"fits/fitscat.h"
#include	"levmar/lm.h"
#include	"fft.h"
#include	"fitswcs.h"
#include	"check.h"
#include	"pattern.h"
#include	"psf.h"
#include	"profit.h"

38
39
static float	prof_interpolate(profstruct *prof, float *posin);
static float	interpolate_pix(float *posin, float *pix, int *naxisn,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
40
41
		interpenum interptype);

42
static void	make_kernel(float pos, float *kernel, interpenum interptype);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/*------------------------------- variables ---------------------------------*/

char		profname[][32]={"background offset", "Sersic spheroid",
		"De Vaucouleurs spheroid", "exponential disk", "spiral arms",
		"bar", "inner ring", "outer ring", "tabulated model",
		""};

int		interp_kernwidth[5]={1,2,4,6,8};
int theniter, the_gal;
/* "Local" global variables; it seems dirty but it simplifies a lot */
/* interfacing to the LM routines */
static picstruct	*the_field, *the_wfield;
profitstruct		*theprofit;

/****** profit_init ***********************************************************
PROTO	profitstruct profit_init(psfstruct *psf)
PURPOSE	Allocate and initialize a new profile-fitting structure.
INPUT	Pointer to PSF structure.
OUTPUT	A pointer to an allocated profit structure.
NOTES	-.
AUTHOR	E. Bertin (IAP)
65
VERSION	07/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
 ***/
profitstruct	*profit_init(psfstruct *psf)
  {
   profitstruct		*profit;
   int			p, nprof,
			backflag, spheroidflag, diskflag, barflag, armsflag;

  QCALLOC(profit, profitstruct, 1);
  profit->psf = psf;
  profit->psfdft = NULL;

  profit->nparam = 0;
  QMALLOC(profit->prof, profstruct *, PROF_NPROF);
  backflag = spheroidflag = diskflag = barflag = armsflag = 0;
  nprof = 0;
  for (p=0; p<PROF_NPROF; p++)
    if (!backflag && FLAG(obj2.prof_offset_flux))
      {
      profit->prof[p] = prof_init(profit, PROF_BACK);
      backflag = 1;
      nprof++;
      }
    else if (!spheroidflag && FLAG(obj2.prof_spheroid_flux))
      {
      profit->prof[p] = prof_init(profit,
	FLAG(obj2.prof_spheroid_sersicn)? PROF_SERSIC : PROF_DEVAUCOULEURS);
      spheroidflag = 1;
      nprof++;
      }
    else if (!diskflag && FLAG(obj2.prof_disk_flux))
      {
      profit->prof[p] = prof_init(profit, PROF_EXPONENTIAL);
      diskflag = 1;
      nprof++;
      }
    else if (diskflag && !barflag && FLAG(obj2.prof_bar_flux))
      {
      profit->prof[p] = prof_init(profit, PROF_BAR);
      barflag = 1;
      nprof++;
      }
    else if (barflag && !armsflag && FLAG(obj2.prof_arms_flux))
      {
      profit->prof[p] = prof_init(profit, PROF_ARMS);
      armsflag = 1;
      nprof++;
      }

114
  QMALLOC(profit->covar, float, profit->nparam*profit->nparam);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
  profit->nprof = nprof;

  return profit;
  }  


/****** profit_end ************************************************************
PROTO	void prof_end(profstruct *prof)
PURPOSE	End (deallocate) a profile-fitting structure.
INPUT	Prof structure.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
VERSION	26/04/2008
 ***/
void	profit_end(profitstruct *profit)
  {
   int	p;

  for (p=0; p<profit->nprof; p++)
    prof_end(profit->prof[p]);
  free(profit->prof);
  free(profit->covar);
  free(profit->psfdft);
  free(profit);

  return;
  }


/****** profit_fit ************************************************************
PROTO	void profit_fit(profitstruct *profit, picstruct *field,
		picstruct *wfield, objstruct *obj, obj2struct *obj2)
PURPOSE	Fit profile(s) convolved with the PSF to a detected object.
INPUT	Array of profile structures,
	Number of profiles,
	Pointer to the profile-fitting structure,
	Pointer to the field,
	Pointer to the field weight,
	Pointer to the obj.
OUTPUT	Pointer to an allocated fit structure (containing details about the
	fit).
NOTES	It is a modified version of the lm_minimize() of lmfit.
AUTHOR	E. Bertin (IAP)
159
VERSION	07/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
160
161
162
163
164
 ***/
void	profit_fit(profitstruct *profit,
		picstruct *field, picstruct *wfield,
		objstruct *obj, obj2struct *obj2)
  {
165
    profitstruct	pprofit;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
166
167
168
    patternstruct *pattern;
    psfstruct		*psf;
    checkstruct		*check;
169
170
    double		emx2,emy2,emxy, a , cp,sp;
    float		psf_fwhm, dchi2, err;
171
    int			i,j,p, nparam, ncomp, nprof;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
172
173

  nparam = profit->nparam;
174
  nprof = profit->nprof;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
  if (profit->psfdft)
    {
    QFREE(profit->psfdft);
    }

  psf = profit->psf;
  profit->pixstep = psf->pixstep;

/* Create pixmaps at image resolution */
  psf_fwhm = psf->masksize[0]*psf->pixstep;
  profit->objnaxisn[0] = (((int)((obj->xmax-obj->xmin+1) + psf_fwhm + 0.499)
		*1.2)/2)*2 + 1;
  profit->objnaxisn[1] = (((int)((obj->ymax-obj->ymin+1) + psf_fwhm + 0.499)
		*1.2)/2)*2 + 1;
  profit->ix = (int)(obj->mx + 0.49999);/* internal convention: 1st pix = 0 */
  profit->iy = (int)(obj->my + 0.49999);/* internal convention: 1st pix = 0 */

  if (profit->objnaxisn[1]<profit->objnaxisn[0])
    profit->objnaxisn[1] = profit->objnaxisn[0];
  else
    profit->objnaxisn[0] = profit->objnaxisn[1];

/* Use (dirty) global variables to interface with lmfit */
  the_field = field;
  the_wfield = wfield;
  theprofit = profit;
  profit->obj = obj;
  profit->obj2 = obj2;

  QMALLOC(profit->objpix, PIXTYPE, profit->objnaxisn[0]*profit->objnaxisn[1]);
  QMALLOC(profit->objweight, PIXTYPE,profit->objnaxisn[0]*profit->objnaxisn[1]);
  QMALLOC(profit->lmodpix, PIXTYPE, profit->objnaxisn[0]*profit->objnaxisn[1]);
  profit->nresi = profit_copyobjpix(profit, field, wfield);
  if (profit->nresi < nparam)
    {
210
211
212
    if (FLAG(obj2.prof_vector))
      for (p=0; p<nparam; p++)
        obj2->prof_vector[p] = 0.0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
213
214
215
216
    obj2->prof_niter = 0;
    return;
    }

217
  QMALLOC(profit->resi, float, profit->nresi);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
218
219
220
221
222
223
224
225
226
227
228
229

/* Create pixmap at PSF resolution */
  profit->modnaxisn[0] =
	((int)(profit->objnaxisn[0]/profit->pixstep +0.4999)/2+1)*2; 
  profit->modnaxisn[1] =
	((int)(profit->objnaxisn[1]/profit->pixstep +0.4999)/2+1)*2; 
  if (profit->modnaxisn[1] < profit->modnaxisn[0])
    profit->modnaxisn[1] = profit->modnaxisn[0];
  else
    profit->modnaxisn[0] = profit->modnaxisn[1];

/* Allocate memory for the complete model */
230
231
  QCALLOC(profit->modpix, float, profit->modnaxisn[0]*profit->modnaxisn[1]);
  QMALLOC(profit->psfpix, float, profit->modnaxisn[0]*profit->modnaxisn[1]);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
232
233
234
235
236
237
238
239
240
241
242
243
/* Allocate memory for the partial model */
  QMALLOC(profit->pmodpix, float, profit->modnaxisn[0]*profit->modnaxisn[1]);

/* Compute the local PSF */
  profit_psf(profit);

/* Set initial guesses and boundaries */
  obj2->prof_flag = 0;
  profit->sigma = obj->sigbkg;

  profit_resetparams(profit);

244
//the_gal++;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
245
246
247

/* Actual minimisation */
  profit->niter = profit_minimize(profit, PROFIT_MAXITER);
248
  profit_residuals(profit,field,wfield, 10.0, profit->param,profit->resi);
249

250
/*
251
  QMEMCPY(profit->paraminit, oldparaminit, float, nparam);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
252
253
254
255
256
257
258
259
260
  if (profit_setparam(profit, PARAM_ARMS_PITCH, 160.0, 130.0, 175.0)==RETURN_OK)
    {
    oldchi2 = profit->chi2;
    oldniter = profit->niter;
    profit_resetparams(profit);
    profit_setparam(profit, PARAM_ARMS_PITCH, 160.0, 130.0, 175.0);
    profit->niter = profit_minimize(profit, PROFIT_MAXITER);
    if (profit->chi2 > oldchi2)
      {
261
      memcpy(profit->paraminit, oldparaminit, nparam*sizeof(float));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
262
263
264
265
266
267
      profit->chi2 = oldchi2;
      profit->niter = oldniter;
      }
    else
      obj2->prof_flag |= PROFIT_FLIPPED;
    }  
268
*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
269
270
271
272
273
274
275
276
277
278
279
280
281

/* Convert covariance matrix to bound space */
  profit_covarunboundtobound(profit);
  for (p=0; p<nparam; p++)
    profit->paramerr[p]= sqrt(profit->covar[p*(nparam+1)]);

/* Equate param and paraminit vectors to avoid confusion later on */
  for (p=0; p<profit->nparam; p++)
    profit->param[p] = profit->paraminit[p];

/* CHECK-Images */
  if ((check = prefs.check[CHECK_SUBPROFILES]))
    {
282
    profit_residuals(profit,field,wfield, 0.0, profit->param,profit->resi);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
283
284
285
286
287
    addcheck(check, profit->lmodpix, profit->objnaxisn[0],profit->objnaxisn[1],
		profit->ix,profit->iy, -1.0);
    }
  if ((check = prefs.check[CHECK_PROFILES]))
    {
288
    profit_residuals(profit,field,wfield, 0.0, profit->param,profit->resi);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    addcheck(check, profit->lmodpix, profit->objnaxisn[0],profit->objnaxisn[1],
		profit->ix,profit->iy, 1.0);
    }
/* Fill measurement parameters */
  if (FLAG(obj2.prof_vector))
    {
    for (p=0; p<nparam; p++)
      obj2->prof_vector[p]= profit->param[p];
    }
  if (FLAG(obj2.prof_errvector))
    {
    for (p=0; p<nparam; p++)
      obj2->prof_errvector[p]= profit->paramerr[p];
    }

  obj2->prof_niter = profit->niter;
  obj2->flux_prof = profit->flux;
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  if (FLAG(obj2.fluxerr_prof))
    {
    err = 0.0;
    i = j = 0;					/* avoid gcc -Wall warning */
    if (profit->paramlist[PARAM_DISK_FLUX])
      {
      i = profit->paramindex[PARAM_DISK_FLUX];
      err += profit->covar[i*(nparam+1)];
      }
    if (profit->paramlist[PARAM_SPHEROID_FLUX])
      {
      j = profit->paramindex[PARAM_SPHEROID_FLUX];
      err += profit->covar[j*(nparam+1)];
      }
    if (profit->paramlist[PARAM_DISK_FLUX]
	&& profit->paramlist[PARAM_SPHEROID_FLUX])
      err += profit->covar[i+j*nparam]+profit->covar[j+i*nparam];
    obj2->fluxerr_prof = err>0.0? sqrt(err): 0.0;
    }

326
327
328
  obj2->prof_chi2 = (profit->nresi > profit->nparam)?
		profit->chi2 / (profit->nresi - profit->nparam) : 0.0;

Emmanuel Bertin's avatar
Emmanuel Bertin committed
329
330
331
332
333
  if (FLAG(obj2.x_prof))
    {
    i = profit->paramindex[PARAM_X];
    j = profit->paramindex[PARAM_Y];
/*-- Model coordinates follow the FITS convention (first pixel at 1,1) */
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    if (profit->paramlist[PARAM_X])
      {
      obj2->x_prof = profit->ix + *profit->paramlist[PARAM_X] + 1.0;
      obj2->poserrmx2_prof = emx2 = profit->covar[i*(nparam+1)];
      }
    else
      emx2 = 0.0;
    if (profit->paramlist[PARAM_Y])
      {
      obj2->y_prof = profit->iy + *profit->paramlist[PARAM_Y] + 1.0;
      obj2->poserrmy2_prof = emy2 = profit->covar[j*(nparam+1)];
      }
    else
      emy2 = 0.0;
    if (profit->paramlist[PARAM_X] && profit->paramlist[PARAM_Y])
      obj2->poserrmxy_prof = emxy = profit->covar[i+j*nparam];
    else
      emxy = 0.0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

/*-- Error ellipse parameters */
    if (FLAG(obj2.poserra_prof))
      {
       double	pmx2,pmy2,temp,theta;

      if (fabs(temp=emx2-emy2) > 0.0)
        theta = atan2(2.0 * emxy,temp) / 2.0;
      else
        theta = PI/4.0;

      temp = sqrt(0.25*temp*temp+ emxy*emxy);
      pmy2 = pmx2 = 0.5*(emx2+emy2);
      pmx2+=temp;
      pmy2-=temp;

      obj2->poserra_prof = (float)sqrt(pmx2);
      obj2->poserrb_prof = (float)sqrt(pmy2);
370
      obj2->poserrtheta_prof = (float)(theta/DEG);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
      }

    if (FLAG(obj2.poserrcxx_prof))
      {
       double	temp;

      obj2->poserrcxx_prof = (float)(emy2/(temp=emx2*emy2-emxy*emxy));
      obj2->poserrcyy_prof = (float)(emx2/temp);
      obj2->poserrcxy_prof = (float)(-2*emxy/temp);
      }
    }


  if (FLAG(obj2.prof_mx2))
    {
    memset(profit->modpix, 0,
387
	profit->modnaxisn[0]*profit->modnaxisn[1]*sizeof(float));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
    for (p=0; p<profit->nprof; p++)
      prof_add(profit->prof[p], profit);
    profit_moments(profit);
    }

/* Bulge */
  if (FLAG(obj2.prof_spheroid_flux))
    {
    obj2->prof_spheroid_flux = *profit->paramlist[PARAM_SPHEROID_FLUX];
    obj2->prof_spheroid_fluxerr =
		profit->paramerr[profit->paramindex[PARAM_SPHEROID_FLUX]];
    obj2->prof_spheroid_reff = *profit->paramlist[PARAM_SPHEROID_REFF];
    obj2->prof_spheroid_refferr = 
		profit->paramerr[profit->paramindex[PARAM_SPHEROID_REFF]];
    obj2->prof_spheroid_aspect = *profit->paramlist[PARAM_SPHEROID_ASPECT];
    obj2->prof_spheroid_aspecterr = 
		profit->paramerr[profit->paramindex[PARAM_SPHEROID_ASPECT]];
    obj2->prof_spheroid_theta =
			fmod_m90_p90(*profit->paramlist[PARAM_SPHEROID_POSANG]);
    obj2->prof_spheroid_thetaerr = 
		profit->paramerr[profit->paramindex[PARAM_SPHEROID_POSANG]];
    if (FLAG(obj2.prof_spheroid_sersicn))
      {
      obj2->prof_spheroid_sersicn = *profit->paramlist[PARAM_SPHEROID_SERSICN];
      obj2->prof_spheroid_sersicnerr = 
		profit->paramerr[profit->paramindex[PARAM_SPHEROID_SERSICN]];
      }
    }

/* Disk */
  if (FLAG(obj2.prof_disk_flux))
    {
    obj2->prof_disk_flux = *profit->paramlist[PARAM_DISK_FLUX];
    obj2->prof_disk_fluxerr =
		profit->paramerr[profit->paramindex[PARAM_DISK_FLUX]];
    obj2->prof_disk_scale = *profit->paramlist[PARAM_DISK_SCALE];
    obj2->prof_disk_scaleerr =
		profit->paramerr[profit->paramindex[PARAM_DISK_SCALE]];
    obj2->prof_disk_aspect = *profit->paramlist[PARAM_DISK_ASPECT];
    obj2->prof_disk_aspecterr =
		profit->paramerr[profit->paramindex[PARAM_DISK_ASPECT]];
    obj2->prof_disk_theta = fmod_m90_p90(*profit->paramlist[PARAM_DISK_POSANG]);
    obj2->prof_disk_thetaerr =
		profit->paramerr[profit->paramindex[PARAM_DISK_POSANG]];
    if (FLAG(obj2.prof_disk_inclination))
      {
      obj2->prof_disk_inclination = acos(obj2->prof_disk_aspect) / DEG;
      if (FLAG(obj2.prof_disk_inclinationerr))
        {
        a = sqrt(1.0-obj2->prof_disk_aspect*obj2->prof_disk_aspect);
        obj2->prof_disk_inclinationerr = obj2->prof_disk_aspecterr
					/(a>0.1? a : 0.1)/DEG;
        }
      }

/* Disk pattern */
    if (prefs.pattern_flag)
      {
446
447
      profit_residuals(profit,field,wfield, PROFIT_DYNPARAM,
			profit->param,profit->resi);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
      pattern = pattern_init(profit, prefs.pattern_type,
		prefs.prof_disk_patternncomp);
      pattern_fit(pattern, profit);
      if (FLAG(obj2.prof_disk_patternspiral))
        obj2->prof_disk_patternspiral = pattern_spiral(pattern);
      if (FLAG(obj2.prof_disk_patternvector))
        {
        ncomp = pattern->size[2];
        for (p=0; p<ncomp; p++)
          obj2->prof_disk_patternvector[p] = (float)pattern->coeff[p];
        }
      if (FLAG(obj2.prof_disk_patternmodvector))
        {
        ncomp = pattern->ncomp*pattern->nfreq;
        for (p=0; p<ncomp; p++)
          obj2->prof_disk_patternmodvector[p] = (float)pattern->mcoeff[p];
        }
      if (FLAG(obj2.prof_disk_patternargvector))
        {
        ncomp = pattern->ncomp*pattern->nfreq;
        for (p=0; p<ncomp; p++)
          obj2->prof_disk_patternargvector[p] = (float)pattern->acoeff[p];
        }
      pattern_end(pattern);
      }

/* Bar */
    if (FLAG(obj2.prof_bar_flux))
      {
      obj2->prof_bar_flux = *profit->paramlist[PARAM_BAR_FLUX];
      obj2->prof_bar_fluxerr =
		profit->paramerr[profit->paramindex[PARAM_BAR_FLUX]];
      obj2->prof_bar_length = *profit->paramlist[PARAM_ARMS_START]
				**profit->paramlist[PARAM_DISK_SCALE];
      obj2->prof_bar_lengtherr = *profit->paramlist[PARAM_ARMS_START]
		  * profit->paramerr[profit->paramindex[PARAM_DISK_SCALE]]
		+ *profit->paramlist[PARAM_DISK_SCALE]
		  * profit->paramerr[profit->paramindex[PARAM_ARMS_START]];
      obj2->prof_bar_aspect = *profit->paramlist[PARAM_BAR_ASPECT];
      obj2->prof_bar_aspecterr =
		profit->paramerr[profit->paramindex[PARAM_BAR_ASPECT]];
      obj2->prof_bar_posang = 
			fmod_m90_p90(*profit->paramlist[PARAM_ARMS_POSANG]);
      obj2->prof_bar_posangerr =
		profit->paramerr[profit->paramindex[PARAM_ARMS_POSANG]];
      if (FLAG(obj2.prof_bar_theta))
        {
        cp = cos(obj2->prof_bar_posang*DEG);
        sp = sin(obj2->prof_bar_posang*DEG);
        a = obj2->prof_disk_aspect;
        obj2->prof_bar_theta = fmod_m90_p90(atan2(a*sp,cp)/DEG
				+ obj2->prof_disk_theta);
        obj2->prof_bar_thetaerr = obj2->prof_bar_posangerr*a/(cp*cp+a*a*sp*sp);
        }

/* Arms */
      if (FLAG(obj2.prof_arms_flux))
        {
        obj2->prof_arms_flux = *profit->paramlist[PARAM_ARMS_FLUX];
        obj2->prof_arms_fluxerr =
		profit->paramerr[profit->paramindex[PARAM_ARMS_FLUX]];
        obj2->prof_arms_pitch =
		fmod_m90_p90(*profit->paramlist[PARAM_ARMS_PITCH]);
        obj2->prof_arms_pitcherr =
		profit->paramerr[profit->paramindex[PARAM_ARMS_PITCH]];
        obj2->prof_arms_start = *profit->paramlist[PARAM_ARMS_START]
				**profit->paramlist[PARAM_DISK_SCALE];
        obj2->prof_arms_starterr = *profit->paramlist[PARAM_ARMS_START]
		  * profit->paramerr[profit->paramindex[PARAM_DISK_SCALE]]
		+ *profit->paramlist[PARAM_DISK_SCALE]
		  * profit->paramerr[profit->paramindex[PARAM_ARMS_START]];
        obj2->prof_arms_quadfrac = *profit->paramlist[PARAM_ARMS_QUADFRAC];
        obj2->prof_arms_quadfracerr =
		profit->paramerr[profit->paramindex[PARAM_ARMS_QUADFRAC]];
        obj2->prof_arms_posang =
			fmod_m90_p90(*profit->paramlist[PARAM_ARMS_POSANG]);
        obj2->prof_arms_posangerr =
		profit->paramerr[profit->paramindex[PARAM_ARMS_POSANG]];
        }
      }
    }

530
531
532
533
  if (FLAG(obj2.prof_class_star))
    {
    pprofit = *profit;
    memset(pprofit.paramindex, 0, PARAM_NPARAM*sizeof(int));
534
    memset(pprofit.paramlist, 0, PARAM_NPARAM*sizeof(float *));
535
536
537
    pprofit.nparam = 0;
    QMALLOC(pprofit.prof, profstruct *, 1);
    pprofit.prof[0] = prof_init(&pprofit, PROF_DIRAC);
538
    QMALLOC(pprofit.covar, float, pprofit.nparam*pprofit.nparam);
539
540
541
542
543
544
545
546
547
    pprofit.nprof = 1;
    profit_resetparams(&pprofit);
    if (profit->paramlist[PARAM_X] && profit->paramlist[PARAM_Y])
      {
      pprofit.paraminit[pprofit.paramindex[PARAM_X]] = *profit->paramlist[PARAM_X];
      pprofit.paraminit[pprofit.paramindex[PARAM_Y]] = *profit->paramlist[PARAM_Y];
      }
    pprofit.paraminit[pprofit.paramindex[PARAM_DISK_FLUX]] = profit->flux;
    pprofit.niter = profit_minimize(&pprofit, PROFIT_MAXITER);
548
    profit_residuals(&pprofit,field,wfield, 10.0, pprofit.param,pprofit.resi);
549
    dchi2 = 0.5*(pprofit.chi2 - profit->chi2);
550
    obj2->prof_class_star = dchi2 < 50.0?
551
	(dchi2 > -50.0? 2.0/(1.0+expf(dchi2)) : 2.0) : 0.0;
552
553
554
555
556
557
558
559
560
561
562
    if (profit->flux > 0.0 && pprofit.flux > 0.0)
      obj2->prof_concentration = -2.5*log10(pprofit.flux / profit->flux);
    else  if (profit->flux > 0.0)
      obj2->prof_concentration = 99.0;
    else  if (pprofit.flux > 0.0)
      obj2->prof_concentration = -99.0;
    prof_end(pprofit.prof[0]);
    free(pprofit.prof);
    free(pprofit.covar);
    }

Emmanuel Bertin's avatar
Emmanuel Bertin committed
563
564
565
566
567
568
569
570
/* clean up. */
  free(profit->modpix);
  free(profit->psfpix);
  free(profit->pmodpix);
  free(profit->lmodpix);
  free(profit->objpix);
  free(profit->objweight);
  free(profit->resi);
571
/*
Emmanuel Bertin's avatar
Emmanuel Bertin committed
572
  free(oldparaminit);
573
*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
574
575
576
577
578
579
580
581
582
583
584
  return;
  }


/****** profit_psf ************************************************************
PROTO	void	profit_psf(profitstruct *profit)
PURPOSE	Build the local PSF at a given resolution.
INPUT	Profile-fitting structure.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
585
VERSION	07/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
586
587
588
 ***/
void	profit_psf(profitstruct *profit)
  {
589
590
   double	flux;
   float	posin[2], posout[2], dnaxisn[2],
Emmanuel Bertin's avatar
Emmanuel Bertin committed
591
		*pixout,
592
		xcout,ycout, xcin,ycin, invpixstep, norm;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
593
594
595
596
597
   int		d,i;

  psf = profit->psf;
  psf_build(psf);

598
599
  xcout = (float)(profit->modnaxisn[0]/2) + 1.0;	/* FITS convention */
  ycout = (float)(profit->modnaxisn[1]/2) + 1.0;	/* FITS convention */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  xcin = (psf->masksize[0]/2) + 1.0;			/* FITS convention */
  ycin = (psf->masksize[1]/2) + 1.0;			/* FITS convention */
  invpixstep = profit->pixstep / psf->pixstep;

/* Initialize multi-dimensional counters */
  for (d=0; d<2; d++)
    {
    posout[d] = 1.0;					/* FITS convention */
    dnaxisn[d] = profit->modnaxisn[d]+0.5;
    }

/* Remap each pixel */
  pixout = profit->psfpix;
  flux = 0.0;
  for (i=profit->modnaxisn[0]*profit->modnaxisn[1]; i--;)
    {
    posin[0] = (posout[0] - xcout)*invpixstep + xcin;
    posin[1] = (posout[1] - ycout)*invpixstep + ycin;
    flux += ((*(pixout++) = interpolate_pix(posin, psf->maskloc,
		psf->masksize, INTERP_LANCZOS3)));
    for (d=0; d<2; d++)
      if ((posout[d]+=1.0) < dnaxisn[d])
        break;
      else
        posout[d] = 1.0;
    }

/* Normalize PSF flux (just in case...) */
  flux *= psf->pixstep*psf->pixstep;
  if (fabs(flux) > 0.0)
    {
    norm = 1.0/flux;
    pixout = profit->psfpix;
    for (i=profit->modnaxisn[0]*profit->modnaxisn[1]; i--;)
      *(pixout++) *= norm;
    }

  return;
  }


/****** profit_findinit *******************************************************
PROTO	void profit_findinit(profitstruct *profit)
PURPOSE	Find a suitable set of initialisation parameters
INPUT	Pointer to the profit structure involved in the fit.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
VERSION	16/04/2008
 ***/
void	profit_findinit(profitstruct *profit)
  {
   int	p;

  for (p=0; p<profit->nprof; p++)
    switch (profit->prof[p]->code)
      {
      default:
      break;
      }

  return;
  }


/****** profit_minimize *******************************************************
PROTO	void profit_minimize(profitstruct *profit)
PURPOSE	Provide a function returning residuals to lmfit.
INPUT	Pointer to the profit structure involved in the fit,
	maximum number of iterations.
OUTPUT	Number of iterations used.
NOTES	-.
AUTHOR	E. Bertin (IAP)
VERSION	23/05/2008
 ***/
int	profit_minimize(profitstruct *profit, int niter)
  {
677
   float		lm_opts[5], info[LM_INFO_SZ];
Emmanuel Bertin's avatar
Emmanuel Bertin committed
678
679
680
681
682
683
   int			m,n;

/* Allocate work space */
  n = profit->nparam;
  m = profit->nresi;

684
685
  memset(profit->resi, 0, profit->nresi*sizeof(float));
  memset(profit->covar, 0, profit->nparam*profit->nparam*sizeof(float));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
686
687
688
689
  profit_boundtounbound(profit, profit->paraminit);

/* Perform fit */
  lm_opts[0] = 1.0e-3;
690
691
692
  lm_opts[1] = 1.0e-18;
  lm_opts[2] = 1.0e-18;
  lm_opts[3] = 1.0e-18;
693
  lm_opts[4] = 1.0e-5;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
694

695
  niter = slevmar_dif(profit_evaluate, profit->paraminit, profit->resi,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
696
697
698
699
700
701
702
703
704
705
	n, m, niter, lm_opts, info, NULL, profit->covar, profit);

  profit_unboundtobound(profit, profit->paraminit);


  return niter;
  }


/****** profit_printout *******************************************************
706
PROTO	void profit_printout(int n_par, float* par, int m_dat, float* fvec,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
		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
 ***/
722
void	profit_printout(int n_par, float* par, int m_dat, float* fvec,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
		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 ******************************************************
753
PROTO	void profit_evaluate(float *par, float *fvec, int m, int n,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
754
755
756
757
758
759
760
761
762
763
764
765
		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 (unused).
OUTPUT	-.
NOTES	Input arguments are there only for compatibility purposes (unused)
AUTHOR	E. Bertin (IAP)
VERSION	18/09/2008
 ***/
766
void	profit_evaluate(float *par, float *fvec, int m, int n,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
767
768
769
770
771
772
			void *adata)
  {
   profitstruct	*profit;

  profit = (profitstruct *)adata;
  profit_unboundtobound(profit, par);
773
  profit_residuals(profit, the_field, the_wfield, PROFIT_DYNPARAM, par, fvec);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
774
775
776
777
778
779
780
  profit_boundtounbound(profit, par);
  profit_printout(m, par, n, fvec, adata, 0, -1, 0 );
  return;
  }


/****** profit_residuals ******************************************************
781
782
PROTO	float *prof_residuals(profitstruct *profit, picstruct *field,
		picstruct *wfield, float dynparam, float *param, float *resi)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
783
784
785
786
787
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,
788
	dynamic compression parameter (0=no compression),
Emmanuel Bertin's avatar
Emmanuel Bertin committed
789
790
791
792
793
	pointer to the model parameters (output),
	pointer to the computed residuals (output).
OUTPUT	Vector of residuals.
NOTES	-.
AUTHOR	E. Bertin (IAP)
794
VERSION	20/03/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
795
 ***/
796
797
float	*profit_residuals(profitstruct *profit, picstruct *field,
		picstruct *wfield, float dynparam, float *param, float *resi)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
798
799
800
801
  {
   int		p;

  memset(profit->modpix, 0,
802
	profit->modnaxisn[0]*profit->modnaxisn[1]*sizeof(float));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
803
804
  for (p=0; p<profit->nparam; p++)
    profit->param[p] = param[p];
805
806
/* Simple PSF shortcut */
  if (profit->nprof == 1 && profit->prof[0]->code == PROF_DIRAC)
807
808
    profit_resample(profit, profit->psfpix, profit->lmodpix,
		*profit->prof[0]->flux);
809
810
811
812
813
814
815
  else
    {
    for (p=0; p<profit->nprof; p++)
      prof_add(profit->prof[p], profit);
    profit_convolve(profit, profit->modpix);
    profit_resample(profit, profit->modpix, profit->lmodpix, 1.0);
    }
816
  profit_compresi(profit, dynparam, resi);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
817
818
819
820
821
822

  return resi;
  }


/****** profit_compresi ******************************************************
823
824
PROTO	float *prof_compresi(profitstruct *profit, float dynparam,
			float *resi)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
825
826
827
PURPOSE	Compute the vector of residuals between the data and the galaxy
	profile model.
INPUT	Profile-fitting structure,
828
	dynamic-compression parameter (0=no compression),
Emmanuel Bertin's avatar
Emmanuel Bertin committed
829
830
831
832
	vector of residuals (output).
OUTPUT	Vector of residuals.
NOTES	-.
AUTHOR	E. Bertin (IAP)
833
VERSION	07/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
834
 ***/
835
float	*profit_compresi(profitstruct *profit, float dynparam, float *resi)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
836
  {
837
838
   double	error;
   float	*resit;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
839
840
   PIXTYPE	*objpix, *objweight, *lmodpix,
		val,val2,wval, invsig;
841
   int		npix, i;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
842
843
844
845
846
847
848
  
/* Compute vector of residuals */
  resit = resi;
  objpix = profit->objpix;
  objweight = profit->objweight;
  lmodpix = profit->lmodpix;
  error = 0.0;
849
850
  npix = profit->objnaxisn[0]*profit->objnaxisn[1];
  if (dynparam > 0.0)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
851
    {
852
853
    invsig = (PIXTYPE)(1.0/dynparam);
    for (i=npix; i--; lmodpix++)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
854
855
856
857
      {
      val = *(objpix++);
      if ((wval=*(objweight++))>0.0)
        {
858
        val2 = (val - *lmodpix)*wval*invsig;
859
        val2 = val2>0.0? logf(1.0+val2) : -logf(1.0-val2);
860
        *(resit++) = val2*dynparam;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
861
862
863
        error += val2*val2;
        }
      }
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
    profit->chi2 = dynparam*dynparam*error;
    }
  else
    {
    for (i=npix; i--; lmodpix++)
      {
      val = *(objpix++);
      if ((wval=*(objweight++))>0.0)
        {
        val2 = (val - *lmodpix)*wval;
        *(resit++) = val2;
        error += val2*val2;
        }
      }
    profit->chi2 = error;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
879
880
881
882
883
884
885
    }

  return resi;
  }


/****** profit_resample ******************************************************
886
PROTO	void	prof_resample(profitstruct *profit, float *inpix,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
887
888
889
890
891
892
		PIXTYPE *outpix)
PURPOSE	Resample the current full resolution model to image resolution.
INPUT	Profile-fitting structure.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
893
VERSION	07/09/2009
Emmanuel Bertin's avatar
Emmanuel Bertin committed
894
 ***/
895
896
void	profit_resample(profitstruct *profit, float *inpix, PIXTYPE *outpix,
	float factor)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
897
  {
898
899
   double	flux;
   float	posin[2], posout[2], dnaxisn[2],
Emmanuel Bertin's avatar
Emmanuel Bertin committed
900
		*dx,*dy,
901
		xcout,ycout, xcin,ycin, invpixstep;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
902
903
   int		d,i;

904
  xcout = (float)(profit->objnaxisn[0]/2) + 1.0;	/* FITS convention */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
905
906
  if ((dx=(profit->paramlist[PARAM_X])))
    xcout += *dx;
907
  ycout = (float)(profit->objnaxisn[1]/2) + 1.0;	/* FITS convention */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
  if ((dy=(profit->paramlist[PARAM_Y])))
    ycout += *dy;
  xcin = (profit->modnaxisn[0]/2) + 1.0;		/* FITS convention */
  ycin = (profit->modnaxisn[1]/2) + 1.0;		/* FITS convention */
  invpixstep = 1.0/profit->pixstep;

/* Initialize multi-dimensional counters */
  for (d=0; d<2; d++)
    {
    posout[d] = 1.0;					/* FITS convention */
    dnaxisn[d] = profit->objnaxisn[d]+0.5;
    }

/* Remap each pixel */
  flux = 0.0;
  for (i=profit->objnaxisn[0]*profit->objnaxisn[1]; i--;)
    {
    posin[0] = (posout[0] - xcout)*invpixstep + xcin;
    posin[1] = (posout[1] - ycout)*invpixstep + ycin;
927
    flux += ((*(outpix++) = (PIXTYPE)(factor*interpolate_pix(posin, inpix,
Emmanuel Bertin's avatar
Emmanuel Bertin committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
		profit->modnaxisn, INTERP_LANCZOS3))));
    for (d=0; d<2; d++)
      if ((posout[d]+=1.0) < dnaxisn[d])
        break;
      else
        posout[d] = 1.0;
    }

  profit->flux = flux;

  return;
  }


/****** profit_convolve *******************************************************
943
PROTO	void profit_convolve(profitstruct *profit, float *modpix)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
944
945
946
947
948
949
950
951
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
 ***/
952
void	profit_convolve(profitstruct *profit, float *modpix)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  {
  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;
975
976
   float      *mask,*maskt, *ppix;
   float       dx,dy, r,r2,rmin,rmin2,rmax,rmax2,rsig,invrsig2;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
977
978
979
980
981
982
983
984
985
986
987
988
   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;
989
  QCALLOC(mask, float, npix);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
990
991
992
993
994
995
996
997
998
999
1000
  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 faster browsing, not all history is shown. View entire blame