~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to contrib/daophot/libsrc/peak.for

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
C @(#)peak.for  19.1 (ES0-DMD) 02/25/03 13:23:50
 
2
C===========================================================================
 
3
C Copyright (C) 1995 European Southern Observatory (ESO)
 
4
C
 
5
C This program is free software; you can redistribute it and/or 
 
6
C modify it under the terms of the GNU General Public License as 
 
7
C published by the Free Software Foundation; either version 2 of 
 
8
C the License, or (at your option) any later version.
 
9
C
 
10
C This program is distributed in the hope that it will be useful,
 
11
C but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
C GNU General Public License for more details.
 
14
C
 
15
C You should have received a copy of the GNU General Public 
 
16
C License along with this program; if not, write to the Free 
 
17
C Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, 
 
18
C MA 02139, USA.
 
19
C
 
20
C Corresponding concerning ESO-MIDAS should be addressed as follows:
 
21
C       Internet e-mail: midas@eso.org
 
22
C       Postal address: European Southern Observatory
 
23
C                       Data Management Division 
 
24
C                       Karl-Schwarzschild-Strasse 2
 
25
C                       D 85748 Garching bei Muenchen 
 
26
C                       GERMANY
 
27
C===========================================================================
 
28
C
 
29
      SUBROUTINE  DAOPK (PAR, MAXPAR, PSF, MAXPSF, MAXEXP, F, 
 
30
     .     WATCH, FITRAD, PERERR, PROERR)
 
31
C
 
32
C=======================================================================
 
33
C
 
34
C Single-star profile-fitting routine.
 
35
C
 
36
C              OFFICIAL DAO VERSION:  1991 April 18
 
37
C
 
38
C This subroutine reads in an array around each star in an input
 
39
C photometry file and fits the point-spread function to the observed 
 
40
C stellar profile by least-squares.  The three parameters solved for 
 
41
C are the brightness ratio between the PSF and the program star, and 
 
42
C the coordinates of the centroid of the program star.  
 
43
C
 
44
C Arguments
 
45
C
 
46
C  WATCH (INPUT) governs whether information relating to the progress 
 
47
C        of the reductions is to be typed on the terminal screen
 
48
C        during execution.
 
49
C
 
50
C FITRAD (INPUT) is the fitting radius.  Only pixels within FITRAD of 
 
51
C        the current estimate of a star's centroid will be included in 
 
52
C        the least-squares determination of its centroid and magnitude.
 
53
C
 
54
C All are user-definable optional parameters.
 
55
C
 
56
C=======================================================================
 
57
C
 
58
      IMPLICIT NONE
 
59
      INTEGER MAXPSF, MAXEXP, MAXBOX, MAXPAR
 
60
      PARAMETER  (MAXBOX=69)
 
61
C
 
62
C Parameters
 
63
C
 
64
C MAXPSF is the length of the side of the largest PSF look-up table
 
65
C        allowed.  (Note:  half-pixel grid size)
 
66
C
 
67
C MAXBOX is the length of the side of the largest box within which the
 
68
C        PSF can be evaluated.  (MAXBOX = (MAXPSF-7)/2 )
 
69
C
 
70
      CHARACTER*30 COOFIL, MAGFIL, PSFFIL, PROFIL, GRPFIL, SWITCH
 
71
      CHARACTER LINE*80, EXTEND*30, CASE*3
 
72
      REAL F(MAXBOX,MAXBOX)
 
73
      REAL PSF(MAXPSF,MAXPSF,MAXEXP), PAR(MAXPAR)
 
74
      INTEGER NINT, MIN0
 
75
C
 
76
      REAL LOBAD, AMAG, SHARP, CHI, ERRMAG, DELTAX, DELTAY
 
77
      REAL APMAG, RADIUS, FRAD, READNS, AP1, THRESH, HIBAD
 
78
      REAL PROERR, PERERR, FITRAD, WATCH, PHPADU, RONOIS
 
79
      REAL PERR, PKERR
 
80
      REAL X, Y, PSFMAG, BRIGHT, XPSF, YPSF, SCALE, SKY
 
81
      INTEGER RDPSF, NITER, IST, LX, LY, NX, NY
 
82
      INTEGER NFRAC, IBEG, ISTAR, NEXP, NPAR, NPSF, IPSTYP
 
83
      INTEGER IDUM, NL, ISTAT, NCOL, NROW, NTERM
 
84
C
 
85
      COMMON /SIZE/ NCOL, NROW
 
86
      COMMON /FILNAM/ COOFIL, MAGFIL, PSFFIL, PROFIL, GRPFIL
 
87
      COMMON /ERROR/ PHPADU, RONOIS, PERR, PKERR
 
88
C
 
89
C-----------------------------------------------------------------------
 
90
C
 
91
C SECTION 1
 
92
C
 
93
C Ascertain the name of the file containing coordinates and magnitude
 
94
C estimates for the program stars.  Open it and read the header.
 
95
C
 
96
      CALL TBLANK
 
97
  950 CALL GETNAM ('File with aperture results:', MAGFIL)
 
98
      IF ((MAGFIL .EQ. 'END OF FILE') .OR.
 
99
     .     (MAGFIL .EQ. 'GIVE UP')) THEN
 
100
         MAGFIL = ' '
 
101
         RETURN
 
102
      END IF
 
103
      CALL INFILE (2, MAGFIL, ISTAT)
 
104
      IF (ISTAT .NE. 0) THEN
 
105
         MAGFIL = 'GIVE UP'
 
106
         GO TO 950
 
107
      END IF
 
108
C
 
109
      CALL RDHEAD (2, NL, IDUM, IDUM, LOBAD, HIBAD, THRESH, AP1, 
 
110
     .     PHPADU, READNS, FRAD)
 
111
      RONOIS=READNS**2
 
112
C
 
113
C Ascertain the name of the PSF file, open it, and read the PSF.
 
114
C
 
115
  900 CALL GETNAM ('File with the PSF:', PSFFIL)
 
116
      IF ((PSFFIL .EQ. 'END OF FILE') .OR.
 
117
     .     (PSFFIL .EQ. 'GIVE UP')) THEN
 
118
         PSFFIL = ' '
 
119
         CALL CLFILE (2)
 
120
         RETURN
 
121
      END IF
 
122
C
 
123
      ISTAT = RDPSF (PSFFIL, IPSTYP, PAR, MAXPAR, NPAR,
 
124
     .     PSF, MAXPSF, MAXEXP, NPSF, NEXP, NFRAC,
 
125
     .     PSFMAG, BRIGHT, XPSF, YPSF)
 
126
      IF (ISTAT .NE. 0) THEN
 
127
         PSFFIL = 'GIVE UP'
 
128
         GO TO 900
 
129
      END IF
 
130
C
 
131
      NTERM = NEXP+NFRAC
 
132
      PERR = 0.01*PERERR
 
133
      PKERR = 0.01*PROERR/(PAR(1)*PAR(2)) ! **2
 
134
C
 
135
C Get ready to do the PEAK fitting:  get the name of the output file.
 
136
C Open it and write the header.
 
137
C
 
138
      PROFIL=SWITCH(MAGFIL, CASE('.pk'))
 
139
  960 CALL GETNAM ('File for PEAK results:', PROFIL)
 
140
      IF ((PROFIL .EQ. 'END OF FILE') .OR.
 
141
     .     (PROFIL .EQ. 'GIVE UP')) THEN
 
142
         CALL CLFILE (2)
 
143
         PROFIL = ' '
 
144
         RETURN
 
145
      END IF
 
146
      PROFIL = EXTEND(PROFIL, CASE('pk'))
 
147
      CALL OUTFIL (3, PROFIL, ISTAT)
 
148
      IF (ISTAT .NE. 0) THEN
 
149
         CALL STUPID ('Error opening output file '//PROFIL)
 
150
         PROFIL = 'GIVE UP'
 
151
         GO TO 960
 
152
      END IF
 
153
C
 
154
      RADIUS = AMIN1( FITRAD, (REAL(NPSF-1)/2. - 1.)/2. )
 
155
      CALL WRHEAD (3, 1, NCOL, NROW, 7, LOBAD, HIBAD, THRESH, AP1, 
 
156
     .     PHPADU, READNS, RADIUS)
 
157
      IF (WATCH .GT. 0.5) CALL TBLANK               ! Type a blank line
 
158
      IBEG=1
 
159
C
 
160
C-----------------------------------------------------------------------
 
161
C
 
162
C SECTION 2
 
163
C
 
164
C Reduce the stars, one by one.
 
165
C
 
166
C Read data for the next star and initialize things.
 
167
C
 
168
 2000 CALL RDSTAR (2, NL, ISTAR, X, Y, APMAG, SKY)
 
169
      IF (ISTAR .LT. 0) GO TO 9000            ! End-of-file encountered
 
170
      IF (ISTAR .EQ. 0) GO TO 2000            ! Blank line encountered
 
171
      IF (APMAG .GE. 99.) APMAG=PSFMAG+5.     ! The ol' college try
 
172
      DELTAX=(X-1.)/XPSF-1.
 
173
      DELTAY=(Y-1.)/YPSF-1.
 
174
      LX=MAX0(1, INT(X-RADIUS)+1)
 
175
      LY=MAX0(1, INT(Y-RADIUS)+1)
 
176
      NX=MIN0(NCOL, INT(X+RADIUS)) - LX + 1
 
177
      NY=MIN0(NROW, INT(Y+RADIUS)) - LY + 1
 
178
      CALL RDARAY ('DATA', LX, LY, NX, NY, MAXBOX, F, IST)
 
179
C
 
180
C At this point LX, LY are the coordinates in the big picture of the
 
181
C first pixel in the subarray.  The dimensions of the subarray are
 
182
C NX, NY.  
 
183
C
 
184
      X=X-LX+1
 
185
      Y=Y-LY+1
 
186
C
 
187
C X,Y are now the coordinates of the star's centroid in the subframe 
 
188
C (where (x,y)=(1.0,1.0) are the coordinates of the center of the first
 
189
C pixel and (x,y)=(NX,NY) are the coordinates of the center of the last
 
190
C pixel in the subarray).
 
191
C
 
192
C Display the subarray on the terminal, if desired, and type out headers
 
193
C if appropriate.
 
194
C
 
195
      IF (WATCH .GT. 1.5) CALL SHOW (F, F(NINT(X),NINT(Y)), 0.9*SKY,
 
196
     .     NX, NY, MAXBOX)
 
197
      IF ((WATCH .GT. 1.5) .OR. ((WATCH .GT. 0.5) .AND. (IBEG .EQ. 1)))
 
198
     .     WRITE (6,620)
 
199
  620 FORMAT (9X, 'STAR', 6X, 'X', 8X, 'Y', 8X, 'MAGNITUDE', 7X,
 
200
     .     'CHI  SHARP   IT')
 
201
      IBEG=2
 
202
      SCALE=10.**(-0.4*(APMAG-PSFMAG))                 ! Starting value
 
203
      CALL PKFIT (F, NX, NY, MAXBOX, X, Y, SCALE, SKY, RADIUS, LOBAD, 
 
204
     .     HIBAD, BRIGHT, IPSTYP, PAR, MAXPAR, NPAR, 
 
205
     .     PSF, MAXPSF, MAXEXP, NPSF, NEXP, NFRAC, 
 
206
     .     DELTAX, DELTAY, ERRMAG, CHI, SHARP, NITER)
 
207
      IF (NITER .GT. 0) GO TO 2010
 
208
C
 
209
C A singular matrix occurred during the least-squares solution.
 
210
C
 
211
      WRITE (LINE,621) ISTAR
 
212
  621 FORMAT (8X, I5, ' had a singular matrix.')
 
213
      CALL STUPID (LINE)
 
214
      GO TO 2000
 
215
C
 
216
C Everything went fine.
 
217
C
 
218
 2010 X=X+LX-1.
 
219
      Y=Y+LY-1.
 
220
      AMAG=PSFMAG-2.5*ALOG10(SCALE)
 
221
      ERRMAG=AMIN1(2.0, 1.086*ERRMAG/SCALE)
 
222
      IF (WATCH .GT. 0.5) WRITE (6,622) ISTAR, X, Y, AMAG, ERRMAG,
 
223
     .     CHI, SHARP, NITER
 
224
  622 FORMAT (8X, I5, 2F9.2, F9.3, ' +- ', F5.3, 2F7.2, I5)
 
225
      WRITE (3,320) ISTAR, X, Y, AMAG, ERRMAG, SKY, FLOAT(NITER), CHI,
 
226
     .     SHARP
 
227
  320 FORMAT (I6, 3F9.3, F9.4, F9.3, F9.0, F9.2, F9.3)
 
228
      GO TO 2000
 
229
C
 
230
C-----------------------------------------------------------------------
 
231
C
 
232
C Normal return.
 
233
C
 
234
 9000 CALL CLFILE (3)
 
235
      CALL CLFILE (2)
 
236
      CALL STUPID ('    Done.')
 
237
      RETURN
 
238
C
 
239
      END!
 
240
C
 
241
C#######################################################################
 
242
C
 
243
      SUBROUTINE  PKFIT (F, NX, NY, MAXBOX, X, Y, SCALE, SKY, RADIUS,
 
244
     .     LOBAD, HIBAD, BRIGHT, IPSTYP, PAR, MAXPAR, NPAR,
 
245
     .     PSF, MAXPSF, MAXEXP, NPSF, NEXP, NFRAC, 
 
246
     .     DELTAX, DELTAY, ERRMAG, CHI, SHARP, NITER)
 
247
C
 
248
C=======================================================================
 
249
C
 
250
C This is the subroutine which does the actual one-star least-squares
 
251
C profile fit for PEAK. 
 
252
C
 
253
C           OFFICIAL DAO VERSION:  1991 April 6
 
254
C
 
255
C Arguments
 
256
C      F (INPUT) is an NX by NY array containing actual picture data.
 
257
C
 
258
C MAXBOX (INPUT) is the maximum value allowable for either NX or NY,
 
259
C        needed for the dimension statements below.  PEAK and PSF will
 
260
C        provide different values of MAXBOX.
 
261
C
 
262
C  SCALE (INPUT/OUTPUT) is the initial estimate of the brightness of
 
263
C        the star, expressed as a fraction of the brightness of the
 
264
C        PSF.  Upon return, the final computed value of SCALE will
 
265
C        be passed back to the calling routine.
 
266
C
 
267
C   X, Y (INPUT/OUTPUT) are the initial estimates of the centroid of 
 
268
C        the star relative to the corner (1,1) of the subarray.  Upon
 
269
C        return, the final computed values of X and Y will be passed 
 
270
C        back to the calling routine.
 
271
C
 
272
C    SKY (INPUT) is the local sky brightness value, carried on from
 
273
C        PHOTOMETRY via the data files.
 
274
C
 
275
C RADIUS (INPUT) is the fitting radius-- only pixels within RADIUS of
 
276
C        the instantaneous estimate of the star's centroid will be
 
277
C        included in the fit.
 
278
C
 
279
C LOBAD and HIBAD (INPUT) are bad pixel limits-- any pixel whose 
 
280
C        brightness value falls outside this range will be presumed to 
 
281
C        be bad, and will be ignored.
 
282
C
 
283
C BRIGHT (INPUT) contains the brightness normalization of the model
 
284
C        analytic point spread function
 
285
C
 
286
C    PAR (INPUT) contains the values of the remaining NPARAM parameters 
 
287
C        defining the analytic function which approximates the core of 
 
288
C        the PSF.
 
289
C
 
290
C    PSF (INPUT) is an NPSF by NPSF by NEXP+NFRAC look-up table 
 
291
C        containing corrections from the analytic approximation of the 
 
292
C        PSF to the true PSF.
 
293
C
 
294
C ERRMAG (OUTPUT) is the estimated standard error of the value of SCALE
 
295
C        returned by this routine.
 
296
C
 
297
C    CHI (OUTPUT) is the estimated goodness-of-fit statistic:  the ratio
 
298
C        of the observed pixel-to-pixel mean absolute deviation from
 
299
C        the profile fit, to the value expected on the basis of the
 
300
C        read-out noise and the photons/ADU (which are brought in
 
301
C        through COMMON block /ERROR/).
 
302
C
 
303
C  SHARP (OUTPUT) is a goodness-of-fit statistic describing how much
 
304
C        broader the actual profile of the object appears than the
 
305
C        profile of the PSF.
 
306
C
 
307
C  NITER (OUTPUT) is the number of iterations the solution required to
 
308
C        achieve convergence.  If NITER = 50, the solution did not 
 
309
C        converge.  If for some reason a singular matrix occurs during
 
310
C        the least-squares solution, this will be flagged by setting
 
311
C        NITER = -1.
 
312
C
 
313
C=======================================================================
 
314
C        
 
315
      IMPLICIT NONE
 
316
      INTEGER MAXPSF, MAXEXP, MAXBOX, MAXPAR
 
317
C
 
318
C Parameter
 
319
C
 
320
C MAXPSF is the length of the side of the largest PSF look-up table
 
321
C        allowed.  (Note:  half-pixel grid spacing)
 
322
C
 
323
      REAL C(3,3), V(3), CLAMP(3), DTOLD(3)
 
324
      REAL F(MAXBOX,MAXBOX), T(3), DT(3), NUMER
 
325
      REAL PSF(MAXPSF,MAXPSF,MAXEXP), PAR(MAXPAR)
 
326
      REAL USEPSF, AMAX1, AMIN1, ABS
 
327
      INTEGER MIN0, MAX0
 
328
C
 
329
      REAL LOBAD, DF, WT, DFDSIG, RHOSQ, RELERR, SIG, SIGSQ
 
330
      REAL FPOS, DVDXC, DVDYC, RSQ, DX, DXSQ, DY, DYSQ, DATUM
 
331
      REAL DENOM, SUMWT, CHIOLD, RADSQ, SHARP, CHI, ERRMAG
 
332
      REAL DELTAX, DELTAY, X, Y, PHPADU, RONOIS, PERR, PKERR
 
333
      REAL SCALE, SKY, RADIUS, HIBAD, BRIGHT
 
334
      INTEGER I, J, IX, IY, IXLO, IXHI, IYLO, IYHI, ISTAT
 
335
      INTEGER NPIX, NFRAC, NEXP, NITER, NPAR, NPSF, IPSTYP
 
336
      INTEGER NX, NY, NCOL, NROW
 
337
      LOGICAL CLIP, REDO
 
338
C
 
339
      COMMON /SIZE/ NCOL, NROW
 
340
      COMMON /ERROR/ PHPADU, RONOIS, PERR, PKERR
 
341
C
 
342
C-----------------------------------------------------------------------
 
343
C
 
344
C Initialize a few things for the solution.
 
345
C
 
346
      RADSQ=RADIUS**2
 
347
      DO 1010 I=1,3
 
348
      CLAMP(I)=1.
 
349
 1010 DTOLD(I)=0.0
 
350
      CHIOLD=1.
 
351
      NITER=0
 
352
      SHARP=0.
 
353
      CLIP = .FALSE.
 
354
C
 
355
C-----------------------------------------------------------------------
 
356
C
 
357
C Here begins the big least-squares loop.
 
358
C
 
359
 2000 NITER=NITER+1
 
360
C
 
361
C Initialize things for this iteration.  CHI and SHARP will be 
 
362
C goodness-of-fit indices.  CHI will also be used in determining the 
 
363
C weights of the individual pixels.  As the solution iterates, the new 
 
364
C value of CHI will be built up in the variable CHI, while a smoothed
 
365
C value of CHI computed from the previous iteration will be carried 
 
366
C along in CHIOLD.
 
367
C
 
368
      CHI=0.0
 
369
      SUMWT=0.0
 
370
      NUMER=0.0
 
371
      DENOM=0.0
 
372
      DO 2010 I=1,3
 
373
      V(I)=0.0                          ! Zero the vector of residuals
 
374
      DO 2010 J=1,3
 
375
 2010 C(I,J)=0.0                        ! Zero the normal matrix
 
376
C
 
377
C Choose the little box containing points inside the fitting radius.
 
378
C
 
379
      IXLO=MAX0(1, INT(X-RADIUS))
 
380
      IYLO=MAX0(1, INT(Y-RADIUS))
 
381
      IXHI=MIN0(NX, INT(X+RADIUS)+1)
 
382
      IYHI=MIN0(NY, INT(Y+RADIUS)+1)
 
383
C
 
384
C Now build up the normal matrix and vector of residuals.
 
385
C
 
386
      NPIX=0
 
387
      DO 2090 IY=IYLO,IYHI
 
388
      DY=FLOAT(IY)-Y
 
389
      DYSQ=DY**2
 
390
      DO 2090 IX=IXLO,IXHI
 
391
      DATUM=F(IX,IY)
 
392
      IF ((DATUM .LT. LOBAD) .OR. (DATUM .GT. HIBAD)) GO TO 2090
 
393
      DX=FLOAT(IX)-X
 
394
      DXSQ=DX**2
 
395
C
 
396
C DX and DY are the distance of this pixel from the centroid of the 
 
397
C star.  Is this pixel truly inside the fitting radius?
 
398
C
 
399
      RSQ=(DXSQ+DYSQ)/RADSQ
 
400
      IF (1.-RSQ .LE. 2.E-6) GO TO 2090   ! Prevents floating underflows
 
401
C
 
402
C The fitting equation is of the form
 
403
 
404
C Observed brightness = 
 
405
C     [SCALE + delta(SCALE)] * [PSF + delta(Xcen)*d(PSF)/d(Xcen) +
 
406
C                                           delta(Ycen)*d(PSF)/d(Ycen) ]
 
407
C
 
408
C and is solved for the unknowns delta(SCALE) ( = the correction to 
 
409
C the brightness ratio between the program star and the PSF) and 
 
410
C delta(Xcen) and delta(Ycen) ( = corrections to the program star's 
 
411
C centroid).
 
412
C
 
413
C The point-spread function is equal to the sum of the integral under
 
414
C a two-dimensional analytic profile plus values interpolated from
 
415
C a look-up table.
 
416
C
 
417
      T(1)=USEPSF(IPSTYP, DX, DY, BRIGHT, PAR, PSF, NPSF, NPAR, NEXP, 
 
418
     .     NFRAC, DELTAX, DELTAY, DVDXC, DVDYC)
 
419
      IF ((SCALE*T(1)+SKY .GT. HIBAD) .AND. (NITER.GE.3)) GO TO 2090
 
420
      T(2)=SCALE*DVDXC
 
421
      T(3)=SCALE*DVDYC
 
422
      DF=F(IX,IY)-SKY-SCALE*T(1)
 
423
C
 
424
C DF is the residual of the brightness in this pixel from the PSF fit.
 
425
C
 
426
C The expected random error in the pixel is the quadratic sum of
 
427
C the Poisson statistics, plus the readout noise, plus an estimated
 
428
C error of PERERR% of the total brightness for the difficulty of flat-
 
429
C fielding and bias-correcting the chip, plus an estimated error of 
 
430
C some fraction of the fourth derivative at the peak of the profile,
 
431
C to account for the difficulty of accurately interpolating within the 
 
432
C point-spread function.  The fourth derivative of the PSF is roughly
 
433
C proportional to H/sigma**4 (sigma is the width parameter for
 
434
C the stellar core); using the geometric mean of sigma(x) and sigma(y), 
 
435
C this becomes H/[sigma(x)*sigma(y)]**2.  The ratio of the fitting 
 
436
C error to this quantity is estimated from a good-seeing CTIO frame to 
 
437
C be approximately 0.027.  NOWADAYS, PAR(1) and PAR(2) are the HWHM,
 
438
C not the sigma, so the coefficient is now of order 0.05 = 5.0%.
 
439
C
 
440
      FPOS=AMAX1(0., F(IX,IY)-DF)
 
441
C
 
442
C FPOS = raw data minus residual = model-predicted value of the 
 
443
C intensity at this point (which presumably is non-negative).
 
444
C
 
445
      SIGSQ=FPOS/PHPADU+RONOIS+(PERR*FPOS)**2+(PKERR*(FPOS-SKY))**2
 
446
      SIG=SQRT(SIGSQ)
 
447
      RELERR=ABS(DF/SIG)
 
448
C
 
449
C SIG is the anticipated standard error of the intensity in this pixel,
 
450
C including readout noise, Poisson photon statistics, and an estimate
 
451
C of the standard error of interpolating within the PSF.  
 
452
C
 
453
      WT=5./(5.+RSQ/(1.-RSQ))             ! Weight as function of radius
 
454
C
 
455
C Now add this pixel into the quantities which go to make up the SHARP
 
456
C index.
 
457
C
 
458
      RHOSQ=DXSQ/PAR(1)**2+DYSQ/PAR(2)**2
 
459
C
 
460
C Include in the sharpness index only those pixels within six
 
461
C HWHMs of the centroid of the star.  (This saves time and
 
462
C floating underflows by excluding pixels which contribute less than
 
463
C about one part in a million to the index.)
 
464
C
 
465
      IF (RHOSQ .LE. 36.) THEN
 
466
         RHOSQ=0.6931472*RHOSQ
 
467
         DFDSIG=EXP(-RHOSQ)*(RHOSQ-1.)
 
468
         FPOS=AMAX1(0., F(IX,IY)-SKY)+SKY
 
469
         NUMER=NUMER+DFDSIG*DF/SIGSQ
 
470
         DENOM=DENOM+DFDSIG**2/SIGSQ
 
471
      END IF
 
472
C
 
473
C Derive the weight of this pixel.  First of all, the weight depends
 
474
C upon the distance of the pixel from the centroid of the star-- it
 
475
C is determined from a function which is very nearly unity for radii
 
476
C much smaller than the fitting radius, and which goes to zero for
 
477
C radii very near the fitting radius.  Then reject any pixels with 
 
478
C 10-sigma residuals (after the first iteration).
 
479
C
 
480
      CHI=CHI+WT*RELERR
 
481
      SUMWT=SUMWT+WT
 
482
C
 
483
C Now the weight is scaled to the inverse square of the expected mean 
 
484
C error.
 
485
C
 
486
      WT=WT/SIGSQ
 
487
C
 
488
C Reduce the weight of a bad pixel.  A pixel having a residual of 2.5
 
489
C sigma gets reduced to half weight; a pixel having a residual of 5.
 
490
C sigma gets weight 1/257.
 
491
C
 
492
      IF (CLIP) WT=WT/(1.+(0.4*RELERR/CHIOLD)**8)
 
493
C
 
494
C Now add the pixel into the vector of residuals and the normal matrix.
 
495
C
 
496
      DO 2030 I=1,3
 
497
      V(I)=V(I)+DF*T(I)*WT
 
498
      DO 2030 J=1,3
 
499
 2030 C(I,J)=C(I,J)+T(I)*T(J)*WT
 
500
C
 
501
      NPIX=NPIX+1
 
502
 2090 CONTINUE                                 ! End of loop over pixels
 
503
C
 
504
C Compute the (robust) goodness-of-fit index CHI.
 
505
C
 
506
      IF (SUMWT .GT. 3.) THEN
 
507
         CHI=1.2533141*CHI/SQRT(SUMWT*(SUMWT-3.))
 
508
C
 
509
C CHI is pulled toward its expected value of unity before being stored
 
510
C in CHIOLD to keep the statistics of a small number of pixels from 
 
511
C completely dominating the error analysis.  
 
512
C
 
513
         CHIOLD=((SUMWT-3.)*CHI+3.)/SUMWT
 
514
      ELSE
 
515
         CHI = 1.
 
516
         CHIOLD = 1.
 
517
      END IF
 
518
C
 
519
C Compute the parameter corrections and check for convergence.
 
520
C
 
521
      IF (NPIX .LT. 3) THEN
 
522
         NITER=-1
 
523
         RETURN
 
524
      END IF
 
525
      CALL INVERS (C, 3, 3, ISTAT)
 
526
      IF (ISTAT .EQ. 0) GO TO 2100
 
527
      NITER=-1
 
528
      RETURN
 
529
C
 
530
C Everything OK so far.
 
531
C
 
532
 2100 CALL VMUL (C, 3, 3, V, DT)
 
533
 
534
C In the beginning, the brightness of the star will not be permitted
 
535
C to change by more than two magnitudes per iteration (that is to say, 
 
536
C if the estimate is getting brighter, it may not get brighter by
 
537
C more than 525% per iteration, and if it is getting fainter, it may
 
538
C not get fainter by more than 84% per iteration).  The x and y
 
539
C coordinates of the centroid will be allowed to change by no more
 
540
C than one-half pixel per iteration.  Any time that a parameter
 
541
C correction changes sign, the maximum permissible change in that
 
542
C parameter will be reduced by a factor of 2.
 
543
C
 
544
      DO 2110 I=1,3
 
545
      IF (DTOLD(I)*DT(I) .LT. 0.) THEN
 
546
         CLAMP(I)=0.5*CLAMP(I)
 
547
      ELSE
 
548
         CLAMP(I)=MIN(1., 1.1*CLAMP(I))
 
549
      END IF
 
550
 2110 DTOLD(I)=DT(I)
 
551
C
 
552
      SCALE=SCALE+DT(1)/
 
553
     .  (1.+AMAX1(DT(1)/(5.25*SCALE),-DT(1)/(0.84*SCALE))/CLAMP(1))
 
554
      X=X+DT(2)/(1.+ABS(DT(2))/(0.4*CLAMP(2)))
 
555
      Y=Y+DT(3)/(1.+ABS(DT(3))/(0.4*CLAMP(3)))
 
556
      IF (NITER .LE. 1) GO TO 2000
 
557
      REDO=.FALSE.
 
558
C
 
559
C Convergence criteria:  if the most recent computed correction to the 
 
560
C brightness is larger than 0.01% or than 0.05 * sigma(brightness),
 
561
C whichever is larger, OR if the absolute change in X or Y is
 
562
C greater than 0.001 pixels, convergence has not been achieved.
 
563
C
 
564
      ERRMAG=CHIOLD*SQRT(C(1,1))
 
565
      IF (CLIP) THEN
 
566
         IF (ABS(DT(1)) .GT. 
 
567
     .        AMAX1( 0.05*ERRMAG, 0.0001*SCALE )) THEN
 
568
            REDO=.TRUE.
 
569
         ELSE IF (AMAX1(ABS(DT(2)),ABS(DT(3))) .GT. 0.001) THEN
 
570
            REDO=.TRUE.
 
571
         END IF
 
572
      ELSE
 
573
         IF (ABS(DT(1)) .GT. 
 
574
     .        AMAX1( ERRMAG, 0.002*SCALE )) THEN
 
575
            REDO = .TRUE.
 
576
         ELSE IF (AMAX1(ABS(DT(2)),ABS(DT(3))) .GT. 0.02) THEN
 
577
            REDO = .TRUE.
 
578
         END IF
 
579
      END IF
 
580
C
 
581
      IF (NITER .GE. 50) REDO = .FALSE.
 
582
      IF (REDO) GO TO 2000
 
583
      IF ((NITER .LT. 50) .AND. (.NOT. CLIP)) THEN
 
584
         CLIP = .TRUE.
 
585
         DO I=1,3
 
586
            DTOLD(I) = 0.
 
587
            CLAMP(I) = AMAX1(CLAMP(I), 0.25)
 
588
         END DO
 
589
         GO TO 2000
 
590
      ELSE
 
591
         SHARP=1.4427*PAR(1)*PAR(2)*NUMER/(BRIGHT*SCALE*DENOM)
 
592
         SHARP=AMIN1(99.999,AMAX1(SHARP,-99.999))
 
593
         RETURN
 
594
      END IF
 
595
C
 
596
      END!