-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecpart.f90
499 lines (474 loc) · 12 KB
/
specpart.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
! -*- f90 -*-
module specpart
private
integer :: ihmax=200, nspec= 0, mk = -1, mth = -1, npart=0
! ----------------------------------------------------------------
! imi i.a. i input discretized spectrum.
! ind i.a. i sorted addresses.
! imo i.a. o output partitioned spectrum.
! zp r.a. i spectral array.
! npart int. o number of partitions found.
! ----------------------------------------------------------------
integer, allocatable :: neigh(:,:),imi(:),ind(:),imo(:)
real, allocatable :: zp(:)
public :: partinit, partition, ihmax, npart
contains
subroutine partinit(nk,nth)
if ( mk.eq.nk .and. mth.eq.nth ) return
nspec=nk*nth
if ( mk.gt.0 ) deallocate ( neigh, imi, imo, ind, zp )
allocate(imi(nspec), imo(nspec), ind(nspec), zp(nspec))
allocate ( neigh(9,nspec) )
mk = nk
mth = nth
call ptnghb
end subroutine partinit
subroutine partition(spec,ipart,nk,nth)
real, intent(in) :: spec(nk,nth)
integer, intent(out) :: ipart(nk,nth)
integer iang,nk,nth
real zmin,zmax
call partinit(nk,nth)
if (nk.ne.mk.or.mth.ne.nth) then
write(*,*) mk,mth,nk,nth
write(*,*) 'Error: partinit must be called with correct spectral dimensions'
stop
endif
do iang=1, mth
zp(1+(iang-1)*mk:iang*mk) = spec(:,iang)
enddo
zmin=minval(zp)
zmax=maxval(zp)
if (zmax-zmin.lt.1.e-9) then
ipart=0
npart=0
return
endif
zp=zmax-zp
fact = real(ihmax-1) / ( zmax - zmin )
imi = max ( 1 , min ( ihmax , nint ( 1. + zp*fact ) ) )
call ptsort (ihmax,nspec)
call pt_fld (nspec, npart)
do iang=1, mth
ipart(:,iang)=imo(1+(iang-1)*mk:iang*mk)
enddo
end subroutine partition
subroutine ptsort(iihmax,nnspec)
!
implicit none
integer :: i, in, iv, iihmax, nnspec
integer :: numv(iihmax), iaddr(iihmax),iorder(nnspec)
! -------------------------------------------------------------------- /
! 1. occurences per height
!
numv = 0
do i=1, nspec
numv(imi(i)) = numv(imi(i)) + 1
end do
!
! -------------------------------------------------------------------- /
! 2. starting address per height
!
iaddr(1) = 1
do i=1, iihmax-1
iaddr(i+1) = iaddr(i) + numv(i)
end do
!
! -------------------------------------------------------------------- /
! 3. order points
!
do i=1, nnspec
iv = imi(i)
in = iaddr(iv)
iorder(i) = in
iaddr(iv) = in + 1
end do
!
! -------------------------------------------------------------------- /
! 4. sort points
!
do i=1, nnspec
ind(iorder(i)) = i
end do
!
return
!/
!/ end of ptsort ----------------------------------------------------- /
!/
end subroutine ptsort
!/ ------------------------------------------------------------------- /
subroutine ptnghb
implicit none
integer :: n, j, i, k
! -------------------------------------------------------------------- /
! 2. build map
!
neigh = 0
!
! ... base loop
!
do n = 1, nspec
!
j = (n-1) / mk + 1
i = n - (j-1) * mk
k = 0
!
! ... point at the left(1)
!
if ( i .ne. 1 ) then
k = k + 1
neigh(k, n) = n - 1
end if
!
! ... point at the right (2)
!
if ( i .ne. mk ) then
k = k + 1
neigh(k, n) = n + 1
end if
!
! ... point at the bottom(3)
!
if ( j .ne. 1 ) then
k = k + 1
neigh(k, n) = n - mk
end if
!
! ... add point at bottom_wrap to top
!
if ( j .eq. 1 ) then
k = k + 1
neigh(k,n) = nspec - (mk-i)
end if
!
! ... point at the top(4)
!
if ( j .ne. mth ) then
k = k + 1
neigh(k, n) = n + mk
end if
!
! ... add point to top_wrap to bottom
!
if ( j .eq. mth ) then
k = k + 1
neigh(k,n) = n - (mth-1) * mk
end if
!
! ... point at the bottom, left(5)
!
if ( (i.ne.1) .and. (j.ne.1) ) then
k = k + 1
neigh(k, n) = n - mk - 1
end if
!
! ... point at the bottom, left with wrap.
!
if ( (i.ne.1) .and. (j.eq.1) ) then
k = k + 1
neigh(k,n) = n - 1 + mk * (mth-1)
end if
!
! ... point at the bottom, right(6)
!
if ( (i.ne.mk) .and. (j.ne.1) ) then
k = k + 1
neigh(k, n) = n - mk + 1
end if
!
! ... point at the bottom, right with wrap
!
if ( (i.ne.mk) .and. (j.eq.1) ) then
k = k + 1
neigh(k,n) = n + 1 + mk * (mth - 1)
end if
!
! ... point at the top, left(7)
!
if ( (i.ne.1) .and. (j.ne.mth) ) then
k = k + 1
neigh(k, n) = n + mk - 1
end if
!
! ... point at the top, left with wrap
!
if ( (i.ne.1) .and. (j.eq.mth) ) then
k = k + 1
neigh(k,n) = n - 1 - (mk) * (mth-1)
end if
!
! ... point at the top, right(8)
!
if ( (i.ne.mk) .and. (j.ne.mth) ) then
k = k + 1
neigh(k, n) = n + mk + 1
end if
!
! ... point at top, right with wrap
!
!
if ( (i.ne.mk) .and. (j.eq.mth) ) then
k = k + 1
neigh(k,n) = n + 1 - (mk) * (mth-1)
end if
!
neigh(9,n) = k
!
end do
!
return
!/
!/ end of ptnghb ----------------------------------------------------- /
!/
end subroutine ptnghb
!/ ------------------------------------------------------------------- /
subroutine pt_fld (nnspec,npart)
!
implicit none
integer, intent(in) :: nnspec
integer, intent(out) :: npart
!/
!/ ------------------------------------------------------------------- /
!/ local parameters
!/
integer :: mask, init, iwshed, imd(nnspec), &
ic_label, ifict_pixel, m, ih, msave, &
ip, i, ipp, ic_dist, iempty, ippp, &
jl, jn, ipt, j
integer :: iq(nspec), iq_start, iq_end
real :: zpmax, ep1, diff
! -------------------------------------------------------------------- /
! 0. initializations
!
mask = -2
init = -1
iwshed = 0
imo = init
ic_label = 0
imd = 0
ifict_pixel = -100
!
iq_start = 1
iq_end = 1
!
zpmax = maxval ( zp )
!
! -------------------------------------------------------------------- /
! 1. loop over levels
!
m = 1
!
do ih=1, ihmax
msave = m
!
! 1.a pixels at level ih
!
do
ip = ind(m)
if ( imi(ip) .ne. ih ) exit
!
! flag the point, if it stays flagge, it is a separate minimum.
!
imo(ip) = mask
!
! consider neighbors. if there is neighbor, set distance and add
! to queue.
!
do i=1, neigh(9,ip)
ipp = neigh(i,ip)
if ( (imo(ipp).gt.0) .or. (imo(ipp).eq.iwshed) ) then
imd(ip) = 1
call fifo_add (ip)
exit
end if
end do
!
if ( m+1 .gt. nspec ) then
exit
else
m = m + 1
end if
!
end do
!
! 1.b process the queue
!
ic_dist = 1
call fifo_add (ifict_pixel)
!
do
call fifo_first (ip)
!
! check for end of processing
!
if ( ip .eq. ifict_pixel ) then
call fifo_empty (iempty)
if ( iempty .eq. 1 ) then
exit
else
call fifo_add (ifict_pixel)
ic_dist = ic_dist + 1
call fifo_first (ip)
end if
end if
!
! process queue
!
do i=1, neigh(9,ip)
ipp = neigh(i,ip)
!
! check for labeled watersheds or basins
!
if ( (imd(ipp).lt.ic_dist) .and. ( (imo(ipp).gt.0) .or. &
(imo(ipp).eq.iwshed))) then
!
if ( imo(ipp) .gt. 0 ) then
!
if ((imo(ip) .eq. mask) .or. (imo(ip) .eq. &
iwshed)) then
imo(ip) = imo(ipp)
else if (imo(ip) .ne. imo(ipp)) then
imo(ip) = iwshed
end if
!
else if (imo(ip) .eq. mask) then
!
imo(ip) = iwshed
!
end if
!
else if ( (imo(ipp).eq.mask) .and. (imd(ipp).eq.0) ) then
!
imd(ipp) = ic_dist + 1
call fifo_add (ipp)
!
end if
!
end do
!
end do
!
! 1.c check for mask values in imo to identify new basins
!
m = msave
!
do
ip = ind(m)
if ( imi(ip) .ne. ih ) exit
imd(ip) = 0
!
if (imo(ip) .eq. mask) then
!
! ... new label for pixel
!
ic_label = ic_label + 1
call fifo_add (ip)
imo(ip) = ic_label
!
! ... and all connected to it ...
!
do
call fifo_empty (iempty)
if ( iempty .eq. 1 ) exit
call fifo_first (ipp)
!
do i=1, neigh(9,ipp)
ippp = neigh(i,ipp)
if ( imo(ippp) .eq. mask ) then
call fifo_add (ippp)
imo(ippp) = ic_label
end if
end do
!
end do
!
end if
!
if ( m + 1 .gt. nspec ) then
exit
else
m = m + 1
end if
!
end do
!
end do
!
! -------------------------------------------------------------------- /
! 2. find nearest neighbor of 0 watershed points and replace
! use original input to check which group to affiliate with 0
! soring changes first in imd to assure symetry in adjustment.
!
do j=1, 5
imd = imo
do jl=1 , nspec
ipt = -1
if ( imo(jl) .eq. 0 ) then
ep1 = zpmax
do jn=1, neigh (9,jl)
diff = abs ( zp(jl) - zp(neigh(jn,jl)))
if ( (diff.le.ep1) .and. (imo(neigh(jn,jl)).ne.0) ) then
ep1 = diff
ipt = jn
end if
end do
if ( ipt .gt. 0 ) imd(jl) = imo(neigh(ipt,jl))
end if
end do
imo = imd
if ( minval(imo) .gt. 0 ) exit
end do
!
npart = ic_label
!
return
!
contains
!/ ------------------------------------------------------------------- /
subroutine fifo_add ( iv )
!
! add point to fifo queue.
!
integer, intent(in) :: iv
!
iq(iq_end) = iv
!
iq_end = iq_end + 1
if ( iq_end .gt. nspec ) iq_end = 1
!
return
end subroutine
!/ ------------------------------------------------------------------- /
subroutine fifo_empty ( iempty )
!
! check if queue is empty.
!
integer, intent(out) :: iempty
!
if ( iq_start .ne. iq_end ) then
iempty = 0
else
iempty = 1
end if
!
return
end subroutine
!/ ------------------------------------------------------------------- /
subroutine fifo_first ( iv )
!
! get point out of queue.
!
integer, intent(out) :: iv
!
iv = iq(iq_start)
!
iq_start = iq_start + 1
if ( iq_start .gt. nspec ) iq_start = 1
!
return
end subroutine
!/
!/ end of pt_fld ----------------------------------------------------- /
!/
end subroutine pt_fld
end module specpart