1  /*
2  				psf.h
3 
4 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 *
6 *	Part of:	SExtractor
7 *
8 *	Authors:	E.BERTIN (IAP)
9 *			P.DELORME (LAOG)
10 *
11 *	Contents:	Include file for psffit.c.
12 *
13 *	Last modify:	12/01/2006
14 *
15 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16 */
17 
18 /*----------------------------- Internal constants --------------------------*/
19 
20 #define	PSF_MAXSHIFT	20.0	/* Max shift from initial guess (pixels)*/
21 #define	PSF_MINSHIFT	1e-3	/* Min shift from previous guess (pixels)*/
22 #define PSF_NITER	20	/* Maximum number of iterations in fit */
23 #define PSF_NA		3	/* Number of fitted parameters per component */
24 #define PSF_NTOT	(PSF_NA*PSF_NPSFMAX)	/* Number of fitted parameters*/
25 #define PSF_DOUBLETOT   ((PSF_NA+1)*PSF_NPSFMAX)/* Nb of fitted parameters */
26 #define	PC_NITER	1	/* Maximum number of iterations in PC fit */
27 
28 /* NOTES:
29 One must have:	PSF_MAXSHIFT > 0.0
30 		PSF_NPSF >= 1
31 		PSF_NITER >= 1
32 */
33 
34 /*--------------------------- structure definitions -------------------------*/
35 
36 typedef struct code
37   {
38   float		*pc;
39   float		**param;
40   int		*parammod;
41   int		ncode;
42   int		nparam;
43   }		codestruct;
44 
45 typedef struct pc
46   {
47   char		name[MAXCHAR];	/* PC filename */
48   int		npc;		/* Number of Principal Components */
49   int		maskdim;	/* Dimensionality of the tabulated data */
50   int		*masksize;	/* PC mask dimensions */
51   int		masknpix;	/* Total number of involved PC pixels */
52   float		*maskcomp; 	/* Convolved pix data (principal components) */
53   int		omaskdim;	/* Dimensionality of the tabulated data */
54   int		*omasksize;	/* PC mask dimensions */
55   int		omasknpix;	/* Total number of involved PC pixels */
56   float		*omaskcomp; 	/* Original pix data (principal components) */
57   double	*maskcurr;	/* Current model */
58   double	*mx2,*my2,*mxy;	/* 2nd order moments for each component */
59   double	*flux;		/* Flux of each component */
60   double	*bt;		/* B/T for each component */
61   codestruct	*code;
62   }	pcstruct;
63 
64 typedef struct
65   {
66   char		name[MAXCHAR];	/* Name of the file containing the PSF data */
67   int		maskdim;	/* Dimensionality of the tabulated data */
68   int		*masksize;	/* PSF mask dimensions */
69   int		masknpix;	/* Total number of involved PSF pixels */
70   float		*maskcomp;      /* Complete pix. data (PSF components) */
71   double	*maskloc;	/* Local PSF */
72   double	**context;	/* Contexts */
73   t_type	*contexttyp;	/* Context types */
74   char		**contextname;	/* Array of context key-names */
75   double	*contextoffset;	/* Offset to apply to context data */
76   double	*contextscale;	/* Scaling to apply to context data */
77   struct poly	*poly;		/* Polynom describing the PSF variations */
78   pcstruct	*pc;		/* PC components */
79   double	fwhm;		/* Typical PSF FWHM */
80   float		pixstep;	/* PSF sampling step */
81   }	psfstruct;
82 
83 typedef struct
84   {
85   int		niter;		/* Number of iterations required */
86   int		npsf;		/* Number of fitted stars for this detection */
87   float		*x,*y;		/* Position derived from the PSF-fitting */
88   float		*flux;		/* Flux derived from the PSF-fitting */
89   }	psfitstruct;
90 
91 /*----------------------------- Global variables ----------------------------*/
92 EXTERN psfstruct	*psf,*ppsf,*thepsf;
93 EXTERN psfitstruct	*thepsfit,*ppsfit,*psfit;
94 EXTERN PIXTYPE		*checkmask;
95 
96 /*-------------------------------- functions --------------------------------*/
97 extern void	compute_pos(int *pnpsf,int *pconvflag,int *pnpsfflag,
98 			double radmin2, double radmax2,double r2, double *sol,
99 			double *flux , double *deltax,double *deltay,
100 			double *pdx,double *pdy),
101 		compute_pos_phot(int *pnpsf,double *sol,double *flux),
102 		compute_poserr(int j,double *var,double *sol,obj2struct *obj2,
103 			double *x2, double *y2,double *xy),
104 		psf_build(psfstruct *psf),
105 		psf_end(psfstruct *psf, psfitstruct *psfit),
106 		psf_init(psfstruct *psf),
107 		svdfit(double *a, double *b, int m, int n, double *sol,
108 			double *vmat, double *wmat),
109 		svdvar(double *vmat, double *wmat, int n, double *covmat);
110 
111 extern double	*compute_gradient (double *weight,int width, int height,
112 			double *masks, double *maskx, double *masky,
113 			double *mat),
114 		*compute_gradient_phot(double *weight,int width, int height,
115 			double *masks, double *pm);
116 
117 extern psfstruct	*psf_load(char *filename);
118 
119 extern void	pc_end(pcstruct *pc),
120 		pc_fit(psfstruct *psf, double *data, double *weight,
121 		int width, int height, int ix, int iy, double dx, double dy,
122 		int npc, float backrms),
123 		double_psf_fit(psfstruct *psf, picstruct *field,
124 			picstruct *wfield, objstruct *obj,
125 			psfstruct *dpsf, picstruct *dfield, picstruct *dwfield),
126 		psf_fit(psfstruct *psf, picstruct *field, picstruct *wfield,
127 		objstruct *obj),
128 		psf_readcontext(psfstruct *psf, picstruct *field);
129 
130 extern pcstruct	*pc_load(catstruct *cat);
131