1
/*===========================================================================
2
Copyright (C) 1995-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
.AUTHOR K. Banse ESO - Garching
33
.PURPOSE calculate the magnitude of a star using method JMETH
37
call as stat = Cjmagn( jmeth, arr, npix, ni, nb, xycen,
38
mag, dmag, sky, dsky, apix, flux )
40
int jmeth method of magnitude calculation
41
1: sum of 9 central pixels
42
2: sum of all pixels in "flux" area (DEFAULT)
44
float *arr data around the star
45
int npix[2] dimension of ARR
46
int ni number of pixels between "flux" area
48
int nb width of background along edge
49
float xycen[2] center of star in pixels in array "arr" (x,y)
51
float *mag magnitude of star
52
float *dmag uncertainty in magnitude determination
53
float *sky mean sky intensity
54
float *dsky uncertainty in sky determination
55
float *nrpix number of pixels used for magn. determination
56
float *flux total flux of the star
58
.RETURNS status 1: put star in center of image
60
-1: array "arr" too small
61
-2: center not in array "arr"
63
#include <midas_def.h> Prototypes for MIDAS interfaces
65
.VERSIONS 1.00 940411 from JMAGN.FOR RvH
68
------------------------------------------------------------*/
70
/* Define _POSIX_SOURCE to indicate that this is a POSIX program */
72
#define _POSIX_SOURCE 1
81
/*++++++++++++++++++++++++++++++
82
.IDENTIFIER static function pixfrac
83
.PURPOSE calculate the fraction of a pixel which is inside a circle
84
with radius r around (xc,yc)
86
call as static function
88
double f pixel value (flux)
89
double df[4] neg. partial derivative of f with respect to x
90
pos. partial derivative of f with respect to x
91
neg. partial derivative of f with respect to y
92
pos. partial derivative of f with respect to y
93
float xdelt xdelta (x - ycenter)
94
float ydelt ydelta (y - ycenter)
95
double r radius of circle
97
double *fi flux inside circle
98
double *pi area inside circle
99
------------------------------*/
102
static void PFRAC(double f, double df[4], float xdelt, float ydelt,
103
double r, double *fi, double *pi)
105
static void PFRAC(f, df, xdelt, ydelt, r, fi, pi)
107
double f, df[4], r, *fi, *pi;
111
double fx, fy, x, y, ai;
112
double v, vy, rx, ry, rp;
113
double dx = 0.1, dy = 0.1, da = 0.01;
117
ai = f - 0.5 * (df[0] - df[1] + df[2] - df[3]);
118
y = -0.5 + (0.5 * dy);
121
/* Subdivide pixel and check */
128
x = -0.5 + (0.5 * dx);
144
/* check if inside */
153
if (x < 0.5) goto sect_200;
156
if (y < 0.5) goto sect_100;
159
*fi = (ai * *pi) + (*fi * da);
166
int Cjmagn( jmeth, arr, npix, ni, nb, pfac, xycen,
167
mag, dmag, sky, dsky, nrpix, flux )
168
int jmeth, *npix, ni, nb;
169
float *arr, *pfac, *xycen, *mag, *dmag, *sky, *dsky, *nrpix, *flux;
172
register int ii, ix, iy;
173
int nn, ninb, nxb[2], nyb[2];
174
int ierr, xmin, ymin, nrx, nry;
176
float rn, rns, dflux;
177
float *pntra, *pntr, a, xrval, yrval, fac;
179
double s, sk, sq, akap;
184
*mag = -9999; /* set return values */
185
*dmag = *sky = *dsky = *flux = 0.0;
192
/* check dimensions and position */
194
if ((npix[0] <nn) || (npix[1] < nn)) return (-3); /* array too small */
196
if ((*xycen < (ninb-1)) || (xycen[0] > (float)(npix[0]-ninb-1)))
198
if (jmeth == 1) return (-2); /* center not in array */
199
ierr = 1; /* put star in center */
200
xycen[0] = (npix[0]-1) * 0.5;
203
if ((xycen[1] < (ninb-1)) || (xycen[1] > (float)(npix[1]-ninb-1)))
205
if (jmeth == 1) return (-2); /* center not in array */
206
ierr = 1; /* put star in center */
207
xycen[1] = (npix[1]-1) * 0.5;
211
/* for all methods we determine the sky intensity by clipping
214
akap = 1.0e+30; /* huge Kappa */
216
nxb[1] = npix[0] - nb;
218
nyb[1] = npix[1] - nb;
221
/* calculate background sky, if so required */
225
xrval = 0.0; /* xrval = 0.0 (initial *sky) */
227
for (ii=0; ii<10; ii++) /* 10 iterations... */
234
for (ix=0; ix<npix[0]; ix++)
236
for (iy=0; iy<npix[1]; iy++)
238
if ((ix <= nxb[0]) || (ix >= nxb[1]) ||
239
(iy <= nyb[0]) || (iy >= nyb[1]))
242
if ( fabs((double) (a - xrval) ) <= akap )
256
xrval = (float) (s / rns);
257
a = sq/rns - (xrval*xrval);
260
*dsky = (float) sqrt((double) a);
261
akap = fac * (*dsky);
274
/* calculate the magnitude for a given method */
279
/* method #1: sum of 9 central pixels */
283
xmin = (int) floor((double) xycen[0] ); /* C indexing, so remove */
284
nrx = xmin + 2; /* subtraction of 1.0 from xycen */
285
ymin = (int) floor((double) xycen[1] );
289
/* method #2: sum of all pixels within background */
293
xmin = ymin = ninb + 1;
294
nrx = npix[0] - ninb;
295
nry = npix[1] - ninb;
298
/* sum flux within area */
303
if ((xmin > nrx) || (ymin > nry)) return (-3);
305
pntra = arr + (xmin-1 + (npix[0]*(ymin-1))); /* point to start */
306
for (iy=0; iy<(nry-ymin+1); iy++)
309
for (ix=0; ix<(nrx-xmin+1); ix++)
321
/* method #3: circular aperture */
327
double fi, r, dr, ro, ri, pi, val, dval[4];
330
ro = npix[0] - xycen[0] - 1.0 - nb;
332
dr = sqrt((double)0.5);
341
for (iy=0; iy<npix[1]; iy++)
344
yrval = (float) (iy - xycen[1]);
346
for (ix=0; ix<npix[0]; ix++)
349
xrval = (float) (ix-xycen[0]);
350
r = sqrt((double) ((xrval*xrval) + ry));
351
if ( (nb > 0) && (r >= ro) && (fabs(val - *sky) <= akap) )
367
dval[0] = *(pntr+1) - val;
369
if (ix == npxm1) dval[0] = a;
371
if (ix == 0) dval[1] = dval[0];
373
dval[2] = *(pntr+npix[0]) - val;
374
a = val - *(pntr-npix[0]);
375
if (iy == npym1) dval[2] = a;
377
if (iy == 0) dval[3] = dval[2];
379
PFRAC(val,dval,xrval, yrval,ri,&fi,&pi);
392
xrval = (float) (sk / rns);
394
a = (float) (sq/rns - (xrval*xrval));
396
*dsky = (float) sqrt((double) a);
403
/* For all methods: compute magnitude and uncertainty */
409
*flux = s - (rn * *sky);
410
dflux = rn * (*dsky) * (float) sqrt((double) (1.0/rn + 1.0/rns) );
418
if (*flux >= (0.1*dflux))
420
*mag = -2.5 * log10( (double) *flux );
421
*dmag = 1.0857362 * dflux / *flux;