1
C @(#)avarea.for 19.1 (ES0-DMD) 02/25/03 14:01:03
2
C===========================================================================
3
C Copyright (C) 1995 European Southern Observatory (ESO)
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.
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.
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,
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
27
C===========================================================================
29
REAL FUNCTION AVAREA(METHOD,A,NPIX,SUBPIX,NDIM,O)
31
C ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
C real function AVAREA version 2.80 880630
35
C K. Banse ESO - Garching
38
C median filter, average
41
C find median value or average
44
C as in [MIDAS.PRIM.SUB1]AVERAGE.FOR
47
C use as VALUE = AV_AREA(METHOD,A,NPIX,SUBPIX,NDIM,O)
50
C METHOD: char. exp. A for average, M for median
51
C A: R*4 array input array
52
C NPIX: I*4 no. of pixels per line
53
C SUBPIX: I*4 array start pixels of area
54
C NDIM: I*4 array no. of pixels in subarea
55
C O: R*4 array working buffer
57
C AVAREA: R*4 median value to be returned
59
C --------------------------------------------------------------------
63
INTEGER NPIX,NDIM(*),SUBPIX(*)
64
INTEGER INDX,NH,NN,NNN,NTOT,NX,NY,OFF,TOP
72
C point to first pixel (in lower left corner)
73
OFF = (SUBPIX(2)-1)*NPIX + SUBPIX(1)
74
NTOT = NDIM(1) * NDIM(2)
76
C branch according to method
77
IF (METHOD(1:1).EQ.'A') GOTO 5000
79
C here for median finding
83
NH = (NTOT + 1)/2 !index of median
85
C move through rest of first line
86
DO 1200 NX=1,NDIM(1)-1
87
INDX = OFF + NX !point to "first" pixel in row
89
C fill array O only from 1 to NH
90
DO 800 NN=1,TOP !loop through already ordered set
91
IF (A(INDX).LT.O(NN)) THEN
93
O(NNN+1) = O(NNN) !shift up ...
95
O(NN) = A(INDX) !merge value into array O
100
O(TOP+1) = A(INDX) !add value on top
101
1000 IF (TOP.LT.NH) TOP = TOP + 1
105
C test, if we are only handling single line
106
IF (NDIM(2).LE.1) THEN
110
OFF = OFF + NPIX - 1 !point to next line (start value - 1)
113
C handle remaining lines
114
DO 2300 NY=1,NDIM(2)-1
116
INDX = OFF + NX !point to "first" pixel in row
118
C fill array O only from 1 to NH
119
DO 1800 NN=1,TOP !loop through already ordered set
120
IF (A(INDX).LT.O(NN)) THEN
121
DO 1600 NNN=TOP,NN,-1
122
O(NNN+1) = O(NNN) !shift up ...
124
O(NN) = A(INDX) !merge value into array O
129
O(TOP+1) = A(INDX) !add value on top
130
2000 IF (TOP.LT.NH) TOP = TOP + 1
135
C return median value
139
C here for simple averaging
145
SUM = SUM + A(OFF+NX)
150
C return average value
151
AVAREA = SNGL(SUM/DBLE(NTOT))