Debian Codegraph @

applic/general/src/filteradap.for

raw
1 C===========================================================================
2 C Copyright (C) 1995-2007 European Southern Observatory (ESO)
3 C
4 C This program is free software; you can redistribute it and/or
5 C modify it under the terms of the GNU General Public License as
6 C published by the Free Software Foundation; either version 2 of
7 C the License, or (at your option) any later version.
8 C
9 C This program is distributed in the hope that it will be useful,
10 C but WITHOUT ANY WARRANTY; without even the implied warranty of
11 C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 C GNU General Public License for more details.
13 C
14 C You should have received a copy of the GNU General Public
15 C License along with this program; if not, write to the Free
16 C Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 C MA 02139, USA.
18 C
19 C Correspondence concerning ESO-MIDAS should be addressed as follows:
20 C Internet e-mail: [email protected]
21 C Postal address: European Southern Observatory
22 C Data Management Division
23 C Karl-Schwarzschild-Strasse 2
24 C D 85748 Garching bei Muenchen
25 C GERMANY
26 C===========================================================================
27 C
28 PROGRAM AFI
29 C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30 C IDENTIFICATION
31 C program afi version 1.00 910320
32 C G.M.Richter ZIAP Sternwarte Babelsberg
33 C
34 C PURPOSE
35 C adaptive filtering of images:
36 C reduction of the noise without damaging the resolution.
37 C cleaning (smoothing, low pass filtering) as well as structur enhancing
38 C and detecting (by two types of high pass filtering: gradient and
39 C Laplace filter) is possible.
40 C
41 C ALGORITHM
42 C The local signal-to-noise ratio as a function of decreasing resolution
43 C is evaluated via the H-transform: mean gradients and curvatures over
44 C different scale lengths (obtained from the H-coefficients of different
45 C order) are compared to the corresponding exspectation values of the
46 C noise. The order for which this signal-to-noise ratio exceedes a given
47 C parameter K indicates the local resolution scale lenght of the signal
48 C (dubbed: the point becomes significant at this order), and determines
49 C the size of the impuls response of the filter at this point.
50 C
51 C KEYWORDS
52 C IN_A input frame
53 C IN_B input mask for noise statistics:
54 C Only unmasked (mask value=0) pixels of the input frame are
55 C used to estimate the noise statistics.
56 C If 'NULL' is put in, no mask is used
57 C OUT_A output frame
58 C INPUTC/C/1/1 type of adaptive filter:
59 C 'S'=smoothing
60 C 'G'=gradient filter
61 C 'L'=Laplace-filter
62 C INPUTC/C/2/1 shape of impulse resonse:
63 C 'B'=box
64 C 'P'=pyramide
65 C INPUTI/I/1/1 maximal size of impulse response
66 C (in regions of high resolution the actual size is smaller)
67 C INPUTR/R/1/1 threshold for significance
68 C INPUTC/C/3/1 noise model:
69 C 'A'=additive noise is assumed
70 C 'P'=Poisson-noise is assumed
71 C
72 C NOTE
73 C When the algorithm is finished some information on the noise
74 C statistics is put out on the terminal: the standard deviation and
75 C the exspectation values of the gradients and the Laplace-terms at
76 C every order involved, and the number of pixels which became significant
77 C on every order by the gradient and by the Laplace term respectively.
78 C The rest pixels are set to the maximal size response.
79 C
80 C
81 C--------------------------------------------------------------------------
82 IMPLICIT NONE
83
84 INTEGER MADRID(1),NRD,K1,K2,STAT,ORD,SIZ,
85 > NAXIS,NPIX(2),NAXISMT,NPIXMT(2),
86 > IMNIN,IMNOUT,IMNH0,IMNH1,IMNHG,IMNHL,IMNM,IMNMT
87
88 INTEGER*8 PNTIN,PNTOUT,PNTH0,PNTH1,PNTHG,PNTHL,PNTM,PNTMT
89 INTEGER NSG,NSL,REST
90 CHARACTER*60 IN,OUT,MT
91 CHARACTER IDENT*72,UNIT*64,TYP*1,FORM*1,NOI*1,YMT*1
92
93 DOUBLE PRECISION START(2),STEP(2)
94
95 REAL K,EG,EL,SG,SL,MOG,MOL,MUL,CUT(4)
96 C
97 C *** include MIDAS definition table
98 INCLUDE 'MID_INCLUDE:ST_DEF.INC'
99 COMMON /VMR/MADRID
100 COMMON /TEST/EG(16),SG(16),EL(16),SL(16),NSG(16),NSL(16),
101 > REST,MOG,MOL,MUL
102 INCLUDE 'MID_INCLUDE:ST_DAT.INC'
103 C
104 C common block for statistics
105
106 C *** set up MIDAS environment
107 CALL STSPRO('afi')
108
109 C get input frame and map it as an image with pixels in floating point format
110 CALL STKRDC('IN_A',1,1,60,NRD,IN,K1,K2,STAT)
111 CALL STIGET(IN,D_R4_FORMAT,F_I_MODE,F_IMA_TYPE,2,NAXIS,NPIX,
112 > START,STEP,IDENT,UNIT,PNTIN,IMNIN,STAT)
113 C
114 IF (NAXIS.LT.2) CALL STETER(1,'*** FATAL: dimension less than 2')
115
116 C get name of output frame and create and map it
117 CALL STKRDC('OUT_A',1,1,60,NRD,OUT,K1,K2,STAT)
118 CALL STIPUT(OUT,D_R4_FORMAT,F_O_MODE,F_IMA_TYPE,NAXIS,NPIX,
119 > START,STEP,IDENT,UNIT,PNTOUT,IMNOUT,STAT)
120
121 C get name of mask image, if name=NULL -> no mask image
122 YMT='Y'
123 CALL STKRDC('IN_B',1,1,60,NRD,MT,K1,K2,STAT)
124 IF ((MT(1:4).EQ.'NULL').OR.(MT(1:4).EQ.'null'))THEN
125 YMT='N'
126 ELSE
127 CALL STIGET(MT,D_R4_FORMAT,F_I_MODE,F_IMA_TYPE,2,NAXISMT,
128 > NPIXMT,START,STEP,IDENT,UNIT,PNTMT,IMNMT,STAT)
129 IF ((NPIXMT(1).NE.NPIX(1)).OR.(NPIXMT(2).NE.NPIX(2))) THEN
130 CALL STETER(1,
131 2 '*** FATAL: Image and mask have different dimensions')
132 ENDIF
133 ENDIF
134 C
135 C *** read char.key 'FILTER TYPE [S/G/L]'
136 CALL STKRDC('INPUTC',1,1,1,NRD,TYP,K1,K2,STAT)
137 C
138 C *** read char.key 'IMPULSE-RESPONSE SHAPE [P/B]'
139 CALL STKRDC('INPUTC',1,2,1,NRD,FORM,K1,K2,STAT)
140 C
141 C *** read integer key 'FILTER-SIZE'
142 CALL STKRDI('INPUTI',1,1,NRD,SIZ,K1,K2,STAT)
143 C
144 C *** read real key 'THRESHOLD K'
145 CALL STKRDR('INPUTR',1,1,NRD,K,K1,K2,STAT)
146 C
147 C read char.key 'NOISE TYPE [A/P]'
148 CALL STKRDC('INPUTC',1,3,1,NRD,NOI,K1,K2,STAT)
149 C
150 C *** check input parameter
151 CALL UPCAS(NOI,NOI)
152 IF (NOI.NE.'A' .AND. NOI.NE.'P') THEN
153 CALL STETER(1,'FATAL: *** INVALID NOISE-TYPE')
154 ENDIF
155 CALL UPCAS(TYP,TYP)
156 IF (TYP.NE.'S' .AND. TYP.NE.'G' .AND. TYP.NE.'L') THEN
157 CALL STETER(1,'*** FATAL: INVALID FILTER-TYPE')
158 ENDIF
159 CALL UPCAS(FORM,FORM)
160 IF (FORM.NE.'B' .AND. FORM.NE.'P') THEN
161 CALL STETER(1,'*** FATAL: INVALID IMPULSE-RESPONSE SHAPE')
162 ENDIF
163 C
164 C *** get transform-order ORD from window-size SIZ
165 IF (FORM.EQ.'P') THEN
166 ORD=2 ! size = 3
167 IF(SIZ.GT.3) ORD=3
168 IF(SIZ.GT.5) ORD=4
169 IF(SIZ.GT.7) ORD=5
170 IF(SIZ.GT.11) ORD=6
171 IF(SIZ.GT.15) ORD=7
172 IF(SIZ.GT.23) ORD=8
173 IF(SIZ.GT.31) ORD=9
174 IF(SIZ.GT.47) ORD=10
175 IF(SIZ.GT.63) ORD=11
176 IF(SIZ.GT.95) ORD=12
177 IF(SIZ.GT.127) ORD=13
178 IF(SIZ.GT.191) ORD=14 !255
179 ELSE
180 ORD=2 ! size = 3
181 IF(SIZ.GT.3) ORD=3
182 IF(SIZ.GT.5) ORD=4
183 IF(SIZ.GT.9) ORD=5
184 IF(SIZ.GT.17) ORD=6
185 IF(SIZ.GT.33) ORD=7
186 IF(SIZ.GT.65) ORD=8
187 IF(SIZ.GT.129) ORD=9
188 IF(SIZ.GT.257) ORD=10 !513
189 ENDIF
190 C
191 C *** create and map images to work
192 CALL STIPUT('mid_h0',D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,NAXIS,NPIX,
193 > START,STEP,IDENT,UNIT,PNTH0,IMNH0,STAT)
194 CALL STIPUT('mid_h1',D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,NAXIS,NPIX,
195 > START,STEP,IDENT,UNIT,PNTH1,IMNH1,STAT)
196 CALL STIPUT('mid_hg',D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,NAXIS,NPIX,
197 > START,STEP,IDENT,UNIT,PNTHG,IMNHG,STAT)
198 CALL STIPUT('mid_hl',D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,NAXIS,NPIX,
199 > START,STEP,IDENT,UNIT,PNTHL,IMNHL,STAT)
200 CALL STIPUT('mid_m',D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,NAXIS,NPIX,
201 > START,STEP,IDENT,UNIT,PNTM,IMNM,STAT)
202 C
203 C *** start algorithm
204 CALL AFIDO(NPIX(1),NPIX(2),MADRID(PNTMT),MADRID(PNTIN),
205 > MADRID(PNTH0),MADRID(PNTH1),MADRID(PNTHG),
206 > MADRID(PNTHL),MADRID(PNTOUT),MADRID(PNTM),
207 > ORD,TYP,FORM,NOI,K,YMT)
208
209 C *** information on image statistics and number of significant points
210 CALL AINFO(ORD)
211 c
212 C *** copy all descriptors from input frame to output frame
213 C
214 C copy descriptors and update LHCUTS + HISTORY
215 CALL DSCUPT(IMNIN,IMNOUT,' ',STAT)
216 C
217 C *** write cuts for gradient and laplace
218 IF (TYP.NE.'S') THEN
219 IF (TYP.EQ.'G') THEN
220 CUT(2)=3.*MOG
221 CUT(1)=0.
222 ELSE
223 CUT(2)=3.*MOL
224 CUT(1)=3.*MUL
225 ENDIF
226 CALL STDWRR(IMNOUT,'LHCUTS',CUT,1,2,K1,STAT)
227 ENDIF
228 C
229 C *** release files, update keywords and exit
230 CALL STSEPI
231 END