Newer
Older
/*
psf.c
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* Part of: SExtractor
*
* Authors: E.BERTIN (IAP)
* P.DELORME (LAOG)
*
* Contents: Fit the PSF to a detection.
*
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "define.h"
#include "globals.h"
#include "prefs.h"
#include "fits/fitscat.h"
#include "check.h"
#include "filter.h"
#include "image.h"
#include "psf.h"
/*------------------------------- variables ---------------------------------*/
extern keystruct objkey[];
extern objstruct outobj;
/********************************* psf_init **********************************/
/*
Allocate memory and stuff for the PSF-fitting.
*/
void psf_init(psfstruct *psf)
{
QMALLOC(thepsfit, psfitstruct, 1);
QMALLOC(thepsfit->x, double, prefs.psf_npsfmax);
QMALLOC(thepsfit->y, double, prefs.psf_npsfmax);
QMALLOC(thepsfit->flux, float, prefs.psf_npsfmax);
QMALLOC(thepsfit->fluxerr, float, prefs.psf_npsfmax);
QMALLOC(ppsfit->x, double, prefs.psf_npsfmax);
QMALLOC(ppsfit->y, double, prefs.psf_npsfmax);
QMALLOC(ppsfit->flux, float, prefs.psf_npsfmax);
QMALLOC(ppsfit->fluxerr, float, prefs.psf_npsfmax);
return;
}
/********************************* psf_end ***********************************/
/*
Free memory occupied by the PSF-fitting stuff.
*/
void psf_end(psfstruct *psf, psfitstruct *psfit)
{
int d, ndim;
if (psf->pc)
pc_end(psf->pc);
ndim = psf->poly->ndim;
for (d=0; d<ndim; d++)
free(psf->contextname[d]);
free(psf->context);
free(psf->contextname);
free(psf->contextoffset);
free(psf->contextscale);
free(psf->contexttyp);
poly_end(psf->poly);
free(psf->maskcomp);
free(psf->maskloc);
free(psf->masksize);
free(psf);
if (psfit)
{
free(psfit->x);
free(psfit->y);
free(psfit->flux);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
return;
}
/********************************* psf_load *********************************/
/*
Read the PSF data from a FITS file.
*/
psfstruct *psf_load(char *filename)
{
static objstruct saveobj;
static obj2struct saveobj2;
psfstruct *psf;
catstruct *cat;
tabstruct *tab;
keystruct *key;
char *head, *ci,*co;
int deg[POLY_MAXDIM], group[POLY_MAXDIM], ndim, ngroup,
i,k;
/* Open the cat (well it is not a "cat", but simply a FITS file */
if (!(cat = read_cat(filename)))
error(EXIT_FAILURE, "*Error*: PSF file not found: ", filename);
/* OK, we now allocate memory for the PSF structure itself */
QCALLOC(psf, psfstruct, 1);
/* Store a short copy of the PSF filename */
if ((ci=strrchr(filename, '/')))
strcpy(psf->name, ci+1);
else
strcpy(psf->name, filename);
if (!(tab = name_to_tab(cat, "PSF_DATA", 0)))
error(EXIT_FAILURE, "*Error*: PSF_DATA table not found in catalog ",
filename);
head = tab->headbuf;
/*-- Dimension of the polynomial */
if (fitsread(head, "POLNAXIS", &ndim, H_INT,T_LONG) == RETURN_OK
&& ndim)
{
/*-- So we have a polynomial description of the PSF variations */
if (ndim > POLY_MAXDIM)
{
sprintf(gstr, "*Error*: The POLNAXIS parameter in %s exceeds %d",
psf->name, POLY_MAXDIM);
error(EXIT_FAILURE, gstr, "");
}
QMALLOC(psf->contextname, char *, ndim);
QMALLOC(psf->context, double *, ndim);
QMALLOC(psf->contexttyp, t_type, ndim);
QMALLOC(psf->contextoffset, double, ndim);
QMALLOC(psf->contextscale, double, ndim);
/*-- We will have to use the outobj structs, so we first save their content */
saveobj = outobj;
saveobj2 = outobj2;
/*-- outobj's are used as FLAG arrays, so we initialize them to 0 */
memset(&outobj, 0, sizeof(outobj));
memset(&outobj2, 0, sizeof(outobj2));
for (i=0; i<ndim; i++)
{
/*---- Polynomial groups */
sprintf(gstr, "POLGRP%1d", i+1);
if (fitsread(head, gstr, &group[i], H_INT,T_LONG) != RETURN_OK)
goto headerror;
/*---- Contexts */
QMALLOC(psf->contextname[i], char, 80);
sprintf(gstr, "POLNAME%1d", i+1);
if (fitsread(head,gstr,psf->contextname[i],H_STRING,T_STRING)!=RETURN_OK)
goto headerror;
if (*psf->contextname[i]==(char)':')
/*------ It seems we're facing a FITS header parameter */
psf->context[i] = NULL; /* This is to tell we'll have to load */
/* a FITS header context later on */
else
/*------ The context element is a dynamic object parameter */
{
if ((k = findkey(psf->contextname[i], (char *)objkey,
sizeof(keystruct)))==RETURN_ERROR)
{
sprintf(gstr, "*Error*: %s CONTEXT parameter in %s unknown",
psf->contextname[i], psf->name);
error(EXIT_FAILURE, gstr, "");
}
key = objkey+k;
psf->context[i] = key->ptr;
psf->contexttyp[i] = key->ttype;
/*------ Declare the parameter "active" to trigger computation by SExtractor */
*((char *)key->ptr) = (char)'\1';
}
/*---- Scaling of the context parameter */
sprintf(gstr, "POLZERO%1d", i+1);
if (fitsread(head, gstr, &psf->contextoffset[i], H_EXPO, T_DOUBLE)
!=RETURN_OK)
goto headerror;
sprintf(gstr, "POLSCAL%1d", i+1);
if (fitsread(head, gstr, &psf->contextscale[i], H_EXPO, T_DOUBLE)
!=RETURN_OK)
Loading
Loading full blame…