-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw3partmd.ftn
1383 lines (1362 loc) · 43.3 KB
/
w3partmd.ftn
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "w3macros.h"
!/ ------------------------------------------------------------------- /
MODULE W3PARTMD
!/
!/ +-----------------------------------+
!/ | WAVEWATCH III USACE/NOAA |
!/ | Barbara Tracy |
!/ | H. L. Tolman |
!/ | FORTRAN 90 |
!/ | Last update : 23-Jul-2018 |
!/ +-----------------------------------+
!/
!/ 01-Nov-2006 : Origination. ( version 3.10 )
!/ 02-Nov-2006 : Adding tail to integration. ( version 3.10 )
!/ 24-Mar-2007 : Bug fix IMI, adding overall field ( version 3.11 )
!/ and sorting.
!/ 15-Apr-2008 : Clean up for distribution. ( version 3.14 )
!/ 02-Dec-2010 : Adding a mapping PMAP between ( version 3.14 )
!/ original and combined partitions
!/ ( M. Szyszka )
!/ 23-Jul-2018 : Added alternative partitioning ( version 6.05 )
!/ methods (C. Bunney, UKMO)
! 1. Purpose :
!
! Spectral partitioning according to the watershed method.
!
! 2. Variables and types :
!
! Name Type Scope Description
! ----------------------------------------------------------------
! MK, MTH Int. Private Dimensions of stored neighour array.
! NEIGH I.A. Private Nearest Neighbor array.
! ----------------------------------------------------------------
! Note: IHMAX, HSPMIN, WSMULT, WSCUT and FLCOMB used from W3ODATMD.
!
! 3. Subroutines and functions :
!
! Name Type Scope Description
! ----------------------------------------------------------------
! W3PART Subr. Public Interface to watershed routines.
! PTSORT Subr. Public Sort discretized image.
! PTNGHB Subr. Public Defeine nearest neighbours.
! PT_FLD Subr. Public Incremental flooding algorithm.
! FIFO_ADD, FIFO_EMPTY, FIFO_FIRST
! Subr. PT_FLD Queue management.
! PTMEAN Subr. Public Compute mean parameters.
! ----------------------------------------------------------------
!
! 4. Subroutines and functions used :
!
! Name Type Module Description
! ----------------------------------------------------------------
! STRACE Subr. W3SERVMD Subroutine traceing.
! WAVNU1 Subr. W3DISPMD Wavenumber computation.
! ----------------------------------------------------------------
!
! 5. Remarks :
!
! 6. Switches :
!
! !/S Enable subroutine tracing.
! !/T Enable test output
!
! 7. Source code :
!
!/ ------------------------------------------------------------------- /
!
USE W3ODATMD, ONLY: IHMAX, HSPMIN, WSMULT, DIMP, PTMETH, PTFCUT
!
PUBLIC
!
INTEGER, PRIVATE :: MK = -1, MTH = -1
INTEGER, ALLOCATABLE, PRIVATE :: NEIGH(:,:)
!/
CONTAINS
!/ ------------------------------------------------------------------- /
SUBROUTINE W3PART ( SPEC, UABS, UDIR, DEPTH, WN, NP, XP, DIMXP )
!/
!/ +-----------------------------------+
!/ | WAVEWATCH III USACE/NOAA |
!/ | Barbara Tracy |
!/ | H. L. Tolman |
!/ | FORTRAN 90 |
!/ | Last update : 02-Dec-2010 !
!/ +-----------------------------------+
!/
!/ 28-Oct-2006 : Origination. ( version 3.10 )
!/ 02-Dec-2010 : Adding a mapping PMAP between ( version 3.14 )
!/ original and combined partitions
!/ ( M. Szyszka )
!/
! 1. Purpose :
!
! Interface to watershed partitioning routines.
!
! 2. Method :
!
! Watershed Algorithm of Vincent and Soille, 1991, implemented by
! Barbara Tracy (USACE/ERDC) for NOAA/NCEP.
!
! 3. Parameters :
!
! Parameter list
! ----------------------------------------------------------------
! SPEC R.A. I 2-D spectrum E(f,theta).
! UABS Real I Wind speed.
! UDIR Real I Wind direction.
! DEPTH Real I Water depth.
! WN R.A. I Wavenumebers for each frequency.
! NP Int. O Number of partitions.
! -1 : Spectrum without minumum energy.
! 0 : Spectrum with minumum energy.
! but no partitions.
! XP R.A. O Parameters describing partitions.
! Entry '0' contains entire spectrum.
! DIMXP Int. I Second dimension of XP.
! ----------------------------------------------------------------
!
! 4. Subroutines used :
!
! Name Type Module Description
! ----------------------------------------------------------------
! STRACE Sur. W3SERVMD Subroutine tracing.
! ----------------------------------------------------------------
!
! 5. Called by :
!
! 6. Error messages :
!
! 7. Remarks :
!
! - To achieve minimum storage but guaranteed storage of all
! partitions DIMXP = ((NK+1)/2) * ((NTH-1)/2) unless specified
! otherwise below.
!
! This version of W3PART contains alternate Met Office partitioning
! methods, selected at runtime using the PTMETH namlist variable:
! 1) Standard WW3 partitioning
! 2) Met Office extended partitioning using split-partitions
! (removes the wind sea part of any swell partiton and combines
! with total wind sea partition).
! 3) Met Office "wave systems" - no classification or combining of
! wind sea partitions. All partitions output and ordered simply
! by wave height.
! 4) Classic, simple wave age based partitioning generating
! a single wind sea and swell partition. [DIMXP = 2]
! 5) 2-band partitioning; produces hi and low freqency band partitions
! using a user-defined cutoff frequency (PTFCUT). [DIMXP = 2]
!
! (Chris Bunney, UK Met Office, Jul 2018)
!
! 8. Structure :
!
! 9. Switches :
!
! !/S Enable subroutine tracing.
! !/T Enable test output
!
! 10. Source code :
!
!/ ------------------------------------------------------------------- /
!/
USE CONSTANTS
!/S USE W3SERVMD, ONLY: STRACE
!
USE W3GDATMD, ONLY: NK, NTH, NSPEC, SIG, TH
USE W3ODATMD, ONLY: WSCUT, FLCOMB
!
IMPLICIT NONE
!/
!/ ------------------------------------------------------------------- /
!/ Parameter list
!/
INTEGER, INTENT(OUT) :: NP
INTEGER, INTENT(IN) :: DIMXP
REAL, INTENT(IN) :: SPEC(NK,NTH), WN(NK), UABS, &
UDIR, DEPTH
REAL, INTENT(OUT) :: XP(DIMP,0:DIMXP)
!/
!/ ------------------------------------------------------------------- /
!/ Local parameters
!/
INTEGER :: ITH, IMI(NSPEC), IMD(NSPEC), &
IMO(NSPEC), IND(NSPEC), NP_MAX, &
IP, IT(1), INDEX(DIMXP), NWS, &
IPW, IPT, ISP
INTEGER :: PMAP(DIMXP)
!/S INTEGER, SAVE :: IENT = 0
REAL :: ZP(NSPEC), ZMIN, ZMAX, Z(NSPEC), &
FACT, WSMAX, HSMAX
REAL :: TP(DIMP,DIMXP)
INTEGER :: IK, WIND_PART ! ChrisB; added for new
REAL :: C, UPAR, SIGCUT ! UKMO partioning methods
!/
!/ ------------------------------------------------------------------- /
! 0. Initializations
!
!/S CALL STRACE (IENT, 'W3PART')
!
NP = 0
XP = 0.
!
! -------------------------------------------------------------------- /
! 1. Process input spectrum
! 1.a 2-D to 1-D spectrum
!
DO ITH=1, NTH
ZP(1+(ITH-1)*NK:ITH*NK) = SPEC(:,ITH)
END DO
!
! PTMETH == 4 : Do simple partitioning based solely on the
! wave age criterion (produces one swell and one wind sea only):
!
IF( PTMETH .EQ. 4 ) THEN
DO IK=1, NK
DO ITH=1, NTH
ISP = IK + (ITH-1) * NK ! index into partition array IMO
UPAR = WSMULT * UABS * MAX(0.0, COS(TH(ITH)-DERA*UDIR))
C = SIG(IK) / WN(IK)
IF( UPAR .LE. C ) THEN
! Is swell:
IMO(ISP) = 2
ELSE
! Is wind sea:
IMO(ISP) = 1
ENDIF
ENDDO
ENDDO
! We have a max of up to two partitions:
NP_MAX=2
! Calculate mean parameters:
CALL PTMEAN ( NP_MAX, IMO, ZP, DEPTH, UABS, UDIR, WN, &
NP, XP, DIMXP, PMAP )
! No more processing required, return:
RETURN
ENDIF ! PTMETH == 4
!
! PTMETH == 5 : produce "high" and "low" band partitions
! using a frequency cutoff:
!
IF( PTMETH .EQ. 5 ) THEN
SIGCUT = TPI * PTFCUT
DO IK = 1, NK
! If bin center <= freq cutoff then mark as "low band".
IF(SIG(IK) .LE. SIGCUT) THEN
IP = 2
ELSE
IP = 1
ENDIF
DO ITH=1, NTH
ISP = IK + (ITH-1) * NK ! index into partition array IMO
IMO(ISP) = IP
ENDDO
ENDDO
! We only ever have 2 partitions:
NP_MAX=2
! Calculate mean parameters:
CALL PTMEAN ( NP_MAX, IMO, ZP, DEPTH, UABS, UDIR, WN, &
NP, XP, DIMXP, PMAP )
! No more processing required, return:
RETURN
ENDIF ! PTMETH == 5
!
! 1.b Invert spectrum and 'digitize'
!
ZMIN = MINVAL ( ZP )
ZMAX = MAXVAL ( ZP )
IF ( ZMAX-ZMIN .LT. 1.E-9 ) RETURN
!
Z = ZMAX - ZP
!
FACT = REAL(IHMAX-1) / ( ZMAX - ZMIN )
IMI = MAX ( 1 , MIN ( IHMAX , NINT ( 1. + Z*FACT ) ) )
!
! 1.c Sort digitized image
!
CALL PTSORT ( IMI, IND, IHMAX )
!
! -------------------------------------------------------------------- /
! 2. Perform partitioning
! 2.a Update nearest neighbor info as needed.
!
CALL PTNGHB
!
! 2.b Incremental flooding
!
CALL PT_FLD ( IMI, IND, IMO, ZP, NP_MAX )
!
! 2.c Compute parameters per partition
! NP and NX initialized inside routine.
!
CALL PTMEAN ( NP_MAX, IMO, ZP, DEPTH, UABS, UDIR, WN, &
NP, XP, DIMXP, PMAP )
!
! 2.d PTMETH == 2: move the wind sea part of the partitions into a
! seperate partition and recalculate the mean parameters.
!
IF ( NP .GT. 0 .AND. PTMETH .EQ. 2 ) THEN
WIND_PART = NP_MAX
DO IK=1, NK
DO ITH=1, NTH
ISP = IK + (ITH-1) * NK ! index into partition array IMO
UPAR = WSMULT * UABS * MAX(0.0, COS(TH(ITH)-DERA*UDIR))
C = SIG(IK) / WN(IK)
IF( C .LT. UPAR ) THEN
! Bin is wind forced - mark as new wind partition:
WIND_PART = NP_MAX + 1
! Update status map to show new wind partition
IMO(ISP) = WIND_PART
ENDIF
ENDDO
ENDDO
IF( WIND_PART .NE. NP_MAX ) THEN
! Some bins were marked as wind sea - recalculate
! integrated parameters:
NP_MAX = WIND_PART
CALL PTMEAN ( NP_MAX, IMO, ZP, DEPTH, UABS, UDIR, WN, &
NP, XP, DIMXP, PMAP )
ENDIF
ENDIF
!
! -------------------------------------------------------------------- /
! 3. Sort and recombine wind seas as needed
! 3.a Sort by wind sea fraction
!
IF ( NP .LE. 1 ) RETURN
! -----------------------------------------------------------------
! PTMETH == 3: Don't classify or combine any partitions as wind sea.
! Simply sort by HS and return.
! -----------------------------------------------------------------
IF( PTMETH .EQ. 3 ) THEN
TP(:,1:NP) = XP(:,1:NP)
XP(:,1:NP) = 0.
DO IP=1, NP
IT = MAXLOC(TP(1,1:NP))
XP(:,IP) = TP(:,IT(1))
TP(1,IT(1)) = -1.
END DO
RETURN ! Don't process any further
ENDIF ! PTMETH == 3
! -----------------------------------------------------------------
! PTMETH == 1: Default WW3 partitioning.
! -----------------------------------------------------------------
TP(:,1:NP) = XP(:,1:NP)
XP(:,1:NP) = 0.
INDEX(1:NP) = 0
NWS = 0
!
DO IP=1, NP
IT = MAXLOC(TP(6,1:NP))
INDEX(IP) = IT(1)
XP(:,IP) = TP(:,INDEX(IP))
IF ( TP(6,IT(1)) .GE. WSCUT ) NWS = NWS + 1
TP(6,IT(1)) = -1.
END DO
!
! 3.b Combine wind seas as needed and resort
!
IF ( NWS.GT.1 .AND. FLCOMB ) THEN
IPW = PMAP(INDEX(1))
DO IP=2, NWS
IPT = PMAP(INDEX(IP))
DO ISP=1, NSPEC
IF ( IMO(ISP) .EQ. IPT ) IMO(ISP) = IPW
END DO
END DO
!
CALL PTMEAN ( NP_MAX, IMO, ZP, DEPTH, UABS, UDIR, WN, &
NP, XP, DIMXP, PMAP )
IF ( NP .LE. 1 ) RETURN
!
TP(:,1:NP) = XP(:,1:NP)
XP(:,1:NP) = 0.
INDEX(1:NP) = 0
NWS = 0
!
DO IP=1, NP
IT = MAXLOC(TP(6,1:NP))
INDEX(IP) = IT(1)
XP(:,IP) = TP(:,INDEX(IP))
IF ( TP(6,IT(1)) .GE. WSCUT ) NWS = NWS + 1
TP(6,IT(1)) = -1.
END DO
!
END IF
!
! 3.c Sort remaining fields by wave height
!
NWS = MIN ( 1 , NWS )
!
TP(:,1:NP) = XP(:,1:NP)
XP(:,1:NP) = 0.
!
IF ( NWS .GT. 0 ) THEN
XP(:,1) = TP(:,1)
TP(1,1) = -1.
NWS = 1
END IF
!
DO IP=NWS+1, NP
IT = MAXLOC(TP(1,1:NP))
XP(:,IP) = TP(:,IT(1))
TP(1,IT(1)) = -1.
END DO
!
! -------------------------------------------------------------------- /
! 4. End of routine
!
RETURN
!/
!/ End of W3PART ----------------------------------------------------- /
!/
END SUBROUTINE W3PART
!/ ------------------------------------------------------------------- /
SUBROUTINE PTSORT ( IMI, IND, IHMAX )
!/
!/ +-----------------------------------+
!/ | WAVEWATCH III USACE/NOAA |
!/ | Barbara Tracy |
!/ | H. L. Tolman |
!/ | FORTRAN 90 |
!/ | Last update : 19-Oct-2006 !
!/ +-----------------------------------+
!/
!/ 19-Oct-2006 : Origination. ( version 3.10 )
!/
! 1. Purpose :
!
! This subroutine sorts the image data in ascending order.
! This sort original to F.T.Tracy (2006)
!
! 3. Parameters :
!
! Parameter list
! ----------------------------------------------------------------
! IMI I.A. I Input discretized spectrum.
! IND I.A. O Sorted data.
! IHMAX Int. I Number of integer levels.
! ----------------------------------------------------------------
!
! 4. Subroutines used :
!
! Name Type Module Description
! ----------------------------------------------------------------
! STRACE Sur. W3SERVMD Subroutine tracing.
! ----------------------------------------------------------------
!
! 10. Source code :
!
!/ ------------------------------------------------------------------- /
!
!/S USE W3SERVMD, ONLY: STRACE
!
USE W3GDATMD, ONLY: NSPEC
!
IMPLICIT NONE
!/
!/ ------------------------------------------------------------------- /
!/ Parameter list
!/
INTEGER, INTENT(IN) :: IHMAX, IMI(NSPEC)
INTEGER, INTENT(OUT) :: IND(NSPEC)
!/
!/ ------------------------------------------------------------------- /
!/ Local parameters
!/
INTEGER :: I, IN, IV
!/S INTEGER, SAVE :: IENT = 0
INTEGER :: NUMV(IHMAX), IADDR(IHMAX), &
IORDER(NSPEC)
!/
!/S CALL STRACE (IENT, 'PTSORT')
!
! -------------------------------------------------------------------- /
! 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, IHMAX-1
IADDR(I+1) = IADDR(I) + NUMV(I)
END DO
!
! -------------------------------------------------------------------- /
! 3. Order points
!
DO I=1, NSPEC
IV = IMI(I)
IN = IADDR(IV)
IORDER(I) = IN
IADDR(IV) = IN + 1
END DO
!
! -------------------------------------------------------------------- /
! 4. Sort points
!
DO I=1, NSPEC
IND(IORDER(I)) = I
END DO
!
RETURN
!/
!/ End of PTSORT ----------------------------------------------------- /
!/
END SUBROUTINE PTSORT
!/ ------------------------------------------------------------------- /
SUBROUTINE PTNGHB
!/
!/ +-----------------------------------+
!/ | WAVEWATCH III USACE/NOAA |
!/ | Barbara Tracy |
!/ | H. L. Tolman |
!/ | FORTRAN 90 |
!/ | Last update : 20-Oct-2006 !
!/ +-----------------------------------+
!/
!/ 20-Oct-2006 : Origination. ( version 3.10 )
!/
! 1. Purpose :
!
! This subroutine computes the nearest neighbors for each grid
! point. Wrapping of directional distribution (0 to 360)is taken
! care of using the nearest neighbor system
!
! 3. Parameters :
!
! Parameter list
! ----------------------------------------------------------------
! IMI I.A. I Input discretized spectrum.
! IMD I.A. O Sorted data.
! IHMAX Int. I Number of integer levels.
! ----------------------------------------------------------------
!
! 4. Subroutines used :
!
! Name Type Module Description
! ----------------------------------------------------------------
! STRACE Sur. W3SERVMD Subroutine tracing.
! ----------------------------------------------------------------
!
! 10. Source code :
!
!/ ------------------------------------------------------------------- /
!
!/S USE W3SERVMD, ONLY: STRACE
!
USE W3GDATMD, ONLY: NK, NTH, NSPEC
!
IMPLICIT NONE
!/
!/ ------------------------------------------------------------------- /
!/ Parameter list
!/
! INTEGER, INTENT(IN) :: IHMAX, IMI(NSPEC)
! INTEGER, INTENT(IN) :: IMD(NSPEC)
!/
!/ ------------------------------------------------------------------- /
!/ Local parameters
!/
INTEGER :: N, J, I, K
!/S INTEGER, SAVE :: IENT = 0
!/
!/S CALL STRACE (IENT, 'PTNGHB')
!
! -------------------------------------------------------------------- /
! 1. Check on need of processing
!
IF ( MK.EQ.NK .AND. MTH.EQ.NTH ) RETURN
!
IF ( MK.GT.0 ) DEALLOCATE ( NEIGH )
ALLOCATE ( NEIGH(9,NSPEC) )
MK = NK
MTH = NTH
!
! -------------------------------------------------------------------- /
! 2. Build map
!
NEIGH = 0
!
! ... Base loop
!
DO N = 1, NSPEC
!
J = (N-1) / NK + 1
I = N - (J-1) * NK
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. NK ) 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 - NK
END IF
!
! ... ADD Point at bottom_wrap to top
!
IF ( J .EQ. 1 ) THEN
K = K + 1
NEIGH(K,N) = NSPEC - (NK-I)
END IF
!
! ... Point at the top(4)
!
IF ( J .NE. NTH ) THEN
K = K + 1
NEIGH(K, N) = N + NK
END IF
!
! ... ADD Point to top_wrap to bottom
!
IF ( J .EQ. NTH ) THEN
K = K + 1
NEIGH(K,N) = N - (NTH-1) * NK
END IF
!
! ... Point at the bottom, left(5)
!
IF ( (I.NE.1) .AND. (J.NE.1) ) THEN
K = K + 1
NEIGH(K, N) = N - NK - 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 + NK * (NTH-1)
END IF
!
! ... Point at the bottom, right(6)
!
IF ( (I.NE.NK) .AND. (J.NE.1) ) THEN
K = K + 1
NEIGH(K, N) = N - NK + 1
END IF
!
! ... Point at the bottom, right with wrap
!
IF ( (I.NE.NK) .AND. (J.EQ.1) ) THEN
K = K + 1
NEIGH(K,N) = N + 1 + NK * (NTH - 1)
END IF
!
! ... Point at the top, left(7)
!
IF ( (I.NE.1) .AND. (J.NE.NTH) ) THEN
K = K + 1
NEIGH(K, N) = N + NK - 1
END IF
!
! ... Point at the top, left with wrap
!
IF ( (I.NE.1) .AND. (J.EQ.NTH) ) THEN
K = K + 1
NEIGH(K,N) = N - 1 - (NK) * (NTH-1)
END IF
!
! ... Point at the top, right(8)
!
IF ( (I.NE.NK) .AND. (J.NE.NTH) ) THEN
K = K + 1
NEIGH(K, N) = N + NK + 1
END IF
!
! ... Point at top, right with wrap
!
!
IF ( (I.NE.NK) .AND. (J.EQ.NTH) ) THEN
K = K + 1
NEIGH(K,N) = N + 1 - (NK) * (NTH-1)
END IF
!
NEIGH(9,N) = K
!
END DO
!
RETURN
!/
!/ End of PTNGHB ----------------------------------------------------- /
!/
END SUBROUTINE PTNGHB
!/ ------------------------------------------------------------------- /
SUBROUTINE PT_FLD ( IMI, IND, IMO, ZP, NPART )
!/
!/ +-----------------------------------+
!/ | WAVEWATCH III NOAA/NCEP |
!/ | H. L. Tolman |
!/ | FORTRAN 90 |
!/ | Last update : 01-Nov-2006 !
!/ +-----------------------------------+
!/
!/ 01-Nov-2006 : Origination. ( version 3.10 )
!/
! 1. Purpose :
!
! This subroutine does incremental flooding of the image to
! determine the watershed image.
!
! 3. Parameters :
!
! Parameter list
! ----------------------------------------------------------------
! 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.
! ----------------------------------------------------------------
!
! 4. Subroutines used :
!
! Name Type Module Description
! ----------------------------------------------------------------
! STRACE Sur. W3SERVMD Subroutine tracing.
! ----------------------------------------------------------------
!
! 10. Source code :
!
!/ ------------------------------------------------------------------- /
!
!/S USE W3SERVMD, ONLY: STRACE
!
USE W3GDATMD, ONLY: NSPEC
!
IMPLICIT NONE
!/
!/ ------------------------------------------------------------------- /
!/ Parameter list
!/
INTEGER, INTENT(IN) :: IMI(NSPEC), IND(NSPEC)
INTEGER, INTENT(OUT) :: IMO(NSPEC), NPART
REAL, INTENT(IN) :: ZP(NSPEC)
!/
!/ ------------------------------------------------------------------- /
!/ Local parameters
!/
INTEGER :: MASK, INIT, IWSHED, IMD(NSPEC), &
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
!/S INTEGER, SAVE :: IENT = 0
REAL :: ZPMAX, EP1, DIFF
!/
!/S CALL STRACE (IENT, 'PT_FLD')
!
! -------------------------------------------------------------------- /
! 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