-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNA_1km_pfsol2.f90
More file actions
1767 lines (1540 loc) · 51 KB
/
Copy pathNA_1km_pfsol2.f90
File metadata and controls
1767 lines (1540 loc) · 51 KB
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
program pf_sol_gen
!
! written by Reed Maxwell (maxwell5@llnl.gov)
! 10-30-04
! Modified by Basile HECTOR (1st May 2017)
! This version reads a mask, identify perimeter cells (in order)
! then draws triangles for 3 patches. Last patch is the simplest:
! the perimeter: 2 triangles per 4 nodes
! top & bottom patches are similar only the definition of
! triangles change as they should have a normal direction
! pointing outside the domain.
! The big difference with previous version is that top&bottom
! triangles are defined accross each individual latitude band,
! and not for any single cell within the domain
! make sure you start at the correct longitude to not fall on an
! island at first...
!real, allocatable:: DEM(:,:), bottom_elev(:,:),points(:,:),&
!real, allocatable:: enter_ind(:,:), exit_ind(:,:),perim_reordered(:,:)
integer, allocatable:: points(:,:), triangles(:,:), patches(:,:), num_tri_patches(:),&
mask(:,:), mask2(:,:), num_conti_zones(:),highdim(:), enter_ind(:,:), exit_ind(:,:),&
perim_reordered(:,:)
real datum, xmax, ymax, mask_read, dist, mdist
integer*8 num_solid, nt_y0, nt_ymax, nt_x0, nt_xmax, nt_surf, &
perimeter(60000,2),pfind(9,2), iperim,&
checkedge, punsort(60000,2), npunsort, imin, jmin, istart, jstart, &
istartsave,min_lowdim,max_lowdim,tmp1(17),tmp2(17),tmp3(17),&
single_block, enter_exist, exit_exist,diag_lat1,diag_lat2, &
num_points, num_triangles,num_patches,jj,ii,nx_dem,ny_dem,&
kk,i,j,k,num_tri_topface,num_per_pts,x0,y0,dx,dy,option_number,&
enterind, exitind, next_entereind, next_exitind, &
enterindtmp,exitindtmp,next_enterindtmp, next_exitindtmp
character*40 dem_filename, pfsol_filename, mask_filename, bottom_filename
! code creates a triangulated pf solid file
! it reads in a DEM file for the shape of the top surface, but assumes that
! the rest of the domain is rectangularly shaped (and is the same size as the
! extents of the DEM)
!
!NOTES:
! we assume the spatial discretization is the SAME for bottom and top of domain
! we assume rectangular sides
! we assume one (1) solid
! we assume six (6) patches
!
! input block, should be read in and made more general
!nx_dem = 1116 ! num dem pnts in x
!ny_dem = 660 ! num dem pnts in y
nx_dem = 5120 ! num dem pnts in x
ny_dem = 3840 ! num dem pnts in y
!latitude range for diagnostic printing
diag_lat1=10348
diag_lat2=10350
diag_lat1=1805
diag_lat2=1809
! now we need to read in the mask before we know how many triangles
! we will have.
! mask 1 = inactive
! 2 = active
allocate (mask(0:nx_dem+1,0:ny_dem+1))
allocate (mask2(0:nx_dem+1,0:ny_dem+1))
mask = 1
print*, "mask allocated"
print*, "read mask"
!mask_filename = 'mask.CONUS.txt'
mask_filename = 'mask.NA_1km.txt'
open(10, file = trim(mask_filename))
read(10,*)
do j = 1, ny_dem
do i =1, nx_dem
read(10,*) mask_read
mask(i,j) = nint(mask_read)
!print*, mask(i,j)
!mask(i,j)=2
end do
end do
close(10)
print*, "read mask: done"
!mask2 = mask
print*, "clean up mask for thin land bands:"
do ii = 1,4
do j = 1, ny_dem
do i =1, nx_dem
checkedge = mask(i-1,j) *mask(i+1,j)
if((mask(i,j) == 2).and.(mask(i,j-1)==2).and.&
(mask(i,j+1)==2).and.(checkedge == 1)) mask(i,j) = 1
checkedge = mask(i,j-1) *mask(i,j+1)
if((mask(i,j) == 2).and.(mask(i-1,j)==2).and.&
(mask(i+1,j)==2).and.(checkedge == 1)) mask(i,j) = 1
end do
end do
end do
print*, "clean up mask for thin land bands: done"
! clean up mask for single cells
! BH: not clear: smoothing: seems to remov all cells that have 3
! empty nearby cells. Do it 5 times (why?)
print*, "clean up mask for single cells"
do ii = 1,5
do j = 1, ny_dem
do i =1, nx_dem
checkedge = mask(i-1,j) *mask(i,j-1)*mask(i+1,j)*mask(i,j+1)
if((mask(i,j) == 2).and.(checkedge <= 3)) mask(i,j) = 1
end do
end do
end do
print*, "clean up mask for single cells: done"
mask2 = mask
!BH TMP!! TO be remOVED!
!totarea=0
!do j = 1, ny_dem
!do i =1, nx_dem
!mask2(i,j)=mask2(i,j)-1
!totarea=totarea+mask2(i,j)
!end do
!end do
!print*, "total area : ", totarea
!fill interior cells
! BH: ok same idea: set as domain cells inland empty cells
! but only for the temporary mask2 mask, doesn't alter real mask
print*, "fill interior cells"
do ii = 1, 10
do j = 1, ny_dem
do i =1, nx_dem
checkedge = mask2(i-1,j) *mask2(i,j-1)*mask2(i+1,j)*mask2(i,j+1)
if((mask2(i,j) == 1).and.(checkedge > 7)) mask2(i,j) = 2
end do
end do
end do
print*, "fill interior cells: done"
!x0 = 5000. ! origin coord, y
!y0 = 5000. ! origin coord, x
!dx = 10000.0 ! dx
!dy = 10000.0 ! dy
!x0 = 125. ! origin coord, y
!y0 = 125. ! origin coord, x
!dx = 250.0 ! dx
!dy = 250.0 ! dy
x0 = 500 ! origin coord, y
y0 = 500 ! origin coord, x
dx = 1000 ! dx
dy = 1000 ! dy
xmax = float(nx_dem)*dx + x0
ymax = float(ny_dem)*dy + y0
!! determine all perimeter cells, but not in order
open (999,file='perimeter_NA_1km.curve')
open (199,file='perimeter.ij_NA_1km.txt')
ii = 1
write(999,*) '#NA_1km'
do i = 1, nx_dem
do j = 1, ny_dem
! check if edge node
!BH: this allows no inside corner (i.e.) when checkedge=8 but mask2(i+1,j+1)=1
!for instance.
checkedge = mask2(i-1,j)+mask2(i,j-1)+mask2(i+1,j)+mask2(i,j+1)
!BH: new:
kk = mask2(i-1,j-1)+mask2(i-1,j+1)+mask2(i+1,j-1)+mask2(i+1,j+1)
if ((mask2(i,j) == 2) .and.( checkedge < 8).and.(checkedge > 4)) then
punsort(ii,1) = i
punsort(ii,2) = j
!print*, punsort(ii,1:2)
write(999,*) float(i-1)*dx+x0, float(j-1)*dy+y0
write(199,*) i,j
ii = ii + 1
end if
!BH: new:
if ((mask2(i,j) == 2) .and.( checkedge == 8).and.(kk < 8)) then
! this should help to remove these case (if i remember correctly)
! * * - *
! | / |
! * - * / * . . .
! | /
! * *
! | |
! * - *
!
! But mostly makes corners:
! * *
! \ |
! * becomes: * - *
! | |
! * *
punsort(ii,1) = i
punsort(ii,2) = j
!print*, punsort(ii,1:2)
write(999,*) float(i-1)*dx+x0, float(j-1)*dy+y0
write(199,*) i,j
ii = ii + 1
end if
end do ! j
end do ! i
print*, "check if edge node: done"
close(999)
close(199)
npunsort = ii - 1
print*, "number of edge nodes", npunsort
!BH: Now find min max index of edges in smallest dimension (here y)
! before we alterate punsort. Could be used later, but on perimeter...!
min_lowdim=minval(punsort(1:npunsort,2),1)
max_lowdim=maxval(punsort(1:npunsort,2),1)
print*,'miny',min_lowdim
print*,'maxy',max_lowdim
allocate(num_conti_zones(ny_dem))
allocate(highdim(nx_dem))
num_conti_zones=0
!!!!!!!!!!!!!!!!!!!!! Perimeter section:
! finds perimeter cells in order (CCW)
print*, npunsort
iperim = 2
if (iperim == 2) then
!determine perimeter nodes
! i=1, increase j until until mask = 2, then
! we sweep in a CC direction until mask goes from 1 to 2, then repeat
! ii = perimeter counter
ii = 1
!BH seems like i is the first (line?) (starting from north? South?)
! actually j = 1:ny, so j loops over lines.... i over col? yes
! i is the index for longitudes!
!i = 298 !corr BH
!i = 20000 !corr BH shoul dcheck why it was 298 ! with i=1, first j = end of col
!i = 10240 !(20480/2): to start in the middle of the X direction; not sure why
! BH: first point has to be on the mainland (no unconnected island)
i = 5000
!i = 1250
!i = 13000
do j = 1, ny_dem
!if (mask(i,j) == 2) exit
!BH:
if (mask2(i,j) == 2) exit
end do
perimeter(ii, 1) = i
perimeter(ii, 2) = j
istart = i
jstart = j
! remove the intial point
imin = perimeter(1,1)
jmin = perimeter(1,2)
do jj = 1, npunsort
if ((punsort(jj,1)==imin).and.(punsort(jj,2)==jmin)) then
punsort(jj,1) = -999999
punsort(jj,2) = -999999
istartsave = jj
exit
end if
end do ! npunsort
ii = ii + 1
! remove the second point
perimeter(ii, 1) = i+1
perimeter(ii, 2) = j
imin = perimeter(2,1)
jmin = perimeter(2,2)
do jj = 1, npunsort
if ((punsort(jj,1)==imin).and.(punsort(jj,2)==jmin)) then
punsort(jj,1) = -999999
punsort(jj,2) = -999999
exit
end if
end do ! npunsort
print*, 1, perimeter(1,1),perimeter(1,2)
!do ii = 3, 6000 !npunsort
!do ii = 3, 545651 !modif BH
!do ii = 3, 545398 ! BH: number ofedge node given above? Not sure if this is what! sweep around peremiter
!do ii = 3, 543087 ! BH: number ofedge node given above? Not sure if this is what! sweep around peremiter
!do ii = 3, 655495 ! BH: number ofedge node given above? Not sure if this is what! sweep around peremiter
do ii = 3, 57393 ! BH: number ofedge node given above? Not sure if this is what! sweep around peremiter
!do ii = 3, 40000 ! BH: number ofedge node given above? Not sure if this is what! sweep around peremiter
!go to 9090
mdist = 1000000.0
imin = 9999999
jmin = 9999999
option_number=0
!do jj = 1, 50000
!do jj = 1, 545651 !modif BH
!do jj = 1, 545398 !modif BH
!do jj = 1, 543087 !modif BH
!do jj = 1, 655495 !modif BH
do jj = 1, 57393 !modif BH
!do jj = 1, 40000 !modif BH
! locate the closest cell
!if (jj==100) then
!punsort(istartsave,1) = istart
!punsort(istartsave,2) = jstart
!end if
!if ((punsort(jj,1).ne.perimeter(ii-1,1)).and.(punsort(jj,2).ne.perimeter(ii-1,2))) then
dist = sqrt( (float(punsort(jj,1))-float(perimeter(ii-1,1)))**2 + &
(float(punsort(jj,2))-float(perimeter(ii-1,2)))**2 )
!print*, ii,jj, dist,punsort(jj,1), punsort(jj,2), perimeter(ii-1,1), perimeter(ii-1,2)
if (dist < mdist) then
imin = punsort(jj,1)
jmin = punsort(jj,2)
mdist = dist
end if
!end if
!if (dist <= sqrt(2.) ) then
!option_number=option_number+1
!end if
end do ! npunsort (jj?)
!if (option_number>0) then
!print*,'option number > 0: ',option_number
!end if
!option_number=0
if (mdist < 10) then ! if the closest cell has been located
!BH attempt:
!if (mdist <=9) then ! if the closest cell has been located
perimeter(ii,1) = imin
perimeter(ii,2) = jmin
do jj = 1, npunsort
if ((punsort(jj,1)==imin).and.(punsort(jj,2)==jmin)) then
punsort(jj,1) = -999999
punsort(jj,2) = -999999
end if
end do ! npunsort
!print*, ii, perimeter(ii,1),perimeter(ii,2)
!BH: commented:
9090 continue !similar to end do(?)
if ((perimeter(ii,1) == istart).and.(perimeter(ii,2) == jstart)) exit
else
perimeter(ii,1) = istart
perimeter(ii,2) = jstart
exit
end if
end do ! ii
num_per_pts = ii
else
print*,'have to read perimieteer file'
!BH:
print*, "get into the else..."
!BH:
!num_per_pts = 5802
!num_per_pts = 545651
!num_per_pts = 545398
!num_per_pts = 543087
!num_per_pts = 655495
num_per_pts = 57393
!num_per_pts = 37996
!open (99,file='CONUS-perimeter.txt')
open (99,file='CONUS-perimeter.txt')
read(99,*)
do ii = 1, num_per_pts
!print*, ii, perimeter(ii,1), perimeter(ii,2)
read(99,*) perimeter(ii,1), perimeter(ii,2)
end do
end if
print*,'npunsort: ',npunsort,'num_per_points: ',num_per_pts
do jj = 1, npunsort
if ((punsort(jj,1)/=-999999).and.(punsort(jj,2)/=-999999 )) then
!print*, 'yes, found one'
!print*,punsort(jj,1),punsort(jj,2)
mask2(punsort(jj,1),punsort(jj,2))=1
end if
end do ! npunsort
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! End of perimeter section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! This section starting here is made to detect, for each latitude !
! band, the number of contiguous blocks (mask =2), and store !
! the index for entering or leaving each of these blocks. !
! they will be further used to draw the triangles !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!! EXAMPLE !!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! These are examples for testing the fortran
! function to do this enter/exit locations:
!four different cases regarding the E-W edges
!tmp1=[1,1,2,2,2,1,1,2,2,2,2,1,2,2,2,1,1]
!tmp1=[2,2,2,2,2,1,1,2,2,2,2,1,2,2,2,1,1]
!tmp1=[2,2,2,2,2,1,1,2,2,2,2,1,2,2,2,2,2]
!tmp1=[1,1,2,2,2,1,1,2,2,2,2,1,2,2,2,2,2]
! then 3 cases for 1 single contiguous zone:
!tmp1=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1]
!tmp1=[1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
!tmp1=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
! case where enter ind = exit ind
tmp1=[1,1,2,2,2,1,1,1,1,2,1,1,2,2,2,1,1]
!!!! Identify number of contiguous zones and create vectors for
!!!! index entering & exiting:
print*,'example :',tmp1
tmp2=eoshift(tmp1,1)
print*,'eoshift(example,1):',tmp2
tmp3=tmp1-tmp2
print*,'(1) - (2) :',tmp3
print*,'abs( (1) - (2) ) :',abs(tmp3)
print*,'sum :',sum(abs(tmp3))
!1 for the last element anyway, of eoshift
! +1 = 2 to enter first, +1 = 3 to exit first
! +1 =4 to re-enter, +1 =5 to exit again etc
! in this simple case (sum odd), number of contiguous area is (sum -1)/2 =3
! in the second case (sum even), you start by being in, total is sum/2=3
! in the 3rd case (sum even) you start by being in and exit being in: total is sum/2 = 3
! in the 4th case (sum odd) you start out and ends in: total is (sum-1)/2 =3
!!!! Show how to find 1st & 2nd enter/exit indexes (for case 1):
!in the example i is the number of contiguous areas:
i=floor(float(sum(abs(tmp3))/2))
print*, 'number of contiguous zones: ',i
!work on tmp2 instead
tmp2=tmp3
tmp2(17)=0
print*,'first entering index:'
print*,'minloc((1)-(2)) :',minloc(tmp2)
print*,'first exit index:'
print*,'maxloc((1)-(2)) :',maxloc(tmp2)
print*,'index removed:'
tmp2(minloc(tmp2))=0
tmp2(maxloc(tmp2))=0
print*,'second entering index:'
print*,'minloc((1)-(2)) :',minloc(tmp2)
print*,'second exit index:'
print*,'maxloc((1)-(2)) :',maxloc(tmp2)
!!!!!!now back on tmp3 for the real looping (still example)
!loop over each contiguous block in the other dimension:
tmp3(17)=0
!first last index:
ii=1
!populate entr & exit indices
! seems easier for the diff cases to separate entering & exit
! 'been tired, probably smarter ways to write that!
!!!!!entering first:
! if first col is domain:
if (tmp1(1)==2) then
k=0
print*,'entering 1 time: index ',k
tmp3(k+1)=0
ii=2
end if
! if only one zone and first col is not domain
if ((i==1).and.(ii==1)) then
k=minloc(tmp3,1)
print*,'entering 1 time: index ',k
! all other cases:
else
do j = ii,i
k=minloc(tmp3,1)
print*,'entering ',j,' time: index: ',k
tmp3(k)=0
end do
end if
ii=i
!!!!! exit indices:
! if only one zone and last col is domain
if ((i==1).and.(tmp1(17)==2)) then
k=17
print*,'exiting 1 time: index ',k
!if more than one zone and last col is domain
elseif ((i>=2).and.(tmp1(17)==2)) then
ii=ii-1
do j = 1,ii
k=maxloc(tmp3,1)
print*,'exiting ',j,' time: index: ',k
tmp3(k)=0
end do
k = 17
print*,'exiting ',i,' time: index: ',k
! other cases: one zone last col isnot domain
else
do j = 1,ii
k=maxloc(tmp3,1)
print*,'exiting ',j,' time: index: ',k
tmp3(k)=0
end do
end if
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!! end EXAMPLE !!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!! Now apply the algo to the real data:
do i=min_lowdim,max_lowdim
num_conti_zones(i)=1
highdim=mask2(1:nx_dem,i)
num_conti_zones(i)=floor(float(sum(abs(highdim-eoshift(highdim,1)))/2))
!highdim-eoshift(highdim,1)
end do
!print*,num_conti_zones
print*,'max number of contiguous zone per -lowest dimension i.e. lat or lon&
-band: ',maxval(num_conti_zones,1)
allocate(enter_ind(ny_dem,maxval(num_conti_zones,1)),&
exit_ind(ny_dem,maxval(num_conti_zones,1)))
enter_ind=0
exit_ind=0
!loop over lowest dimension within mask
do i = min_lowdim,max_lowdim
highdim=mask2(1:nx_dem,i) - eoshift(mask2(1:nx_dem,i),1)
! remove the last one, or it will be max value
highdim(nx_dem)=0
!first last index:
ii=1
!populate entr & exit indices
!!!!!entering first:
! if first col is domain:
if (mask2(1,i)==2) then
k=0
!enter_ind(i,1)=k*dx+dx/2.+x0
enter_ind(i,1)=k*dx+dx/2+x0
highdim(k+1)=0
ii=2
end if
! if only one zone and first col is not domain
if ((num_conti_zones(i)==1).and.(ii==1)) then
k=minloc(highdim,1)
!enter_ind(i,1)=k*dx+dx/2.+x0
enter_ind(i,1)=k*dx+dx/2+x0
! all other cases:
else
do j = ii,num_conti_zones(i)
k=minloc(highdim,1)
if (k == maxloc(highdim,1)-1) then
enter_ind(i,j)=0
else
!enter_ind(i,j)=k*dx+dx/2.+x0
enter_ind(i,j)=k*dx+dx/2+x0
end if
highdim(k)=0
end do
end if
!!!!! exit indices:
ii=num_conti_zones(i)
! if only one zone and last col is domain
if ((num_conti_zones(i)==1).and.(mask2(nx_dem,i)==2)) then
k=nx_dem
!exit_ind(i,1)=(k-1)*dx+dx/2.+x0
exit_ind(i,1)=(k-1)*dx+dx/2+x0
!if more than one zone and last col is domain
elseif ((num_conti_zones(i)>=2).and.(mask2(nx_dem,i)==2)) then
ii=ii-1
do j = 1,ii
if (enter_ind(i,j)==0) then
exit_ind(i,j)=0
highdim(k)=0
else
k=maxloc(highdim,1)
!exit_ind(i,j)=(k-1)*dx+dx/2.+x0
exit_ind(i,j)=(k-1)*dx+dx/2+x0
highdim(k)=0
end if
end do
exit_ind(i,num_conti_zones(i))=(nx_dem-1)*dx+dx/2+x0
k = nx_dem
! other cases: more than1 zone last col isnot domain
else
do j = 1,ii
if (enter_ind(i,j)==0) then
exit_ind(i,j)=0
else
k=maxloc(highdim,1)
!exit_ind(i,j)=(k-1)*dx+dx/2.+x0
exit_ind(i,j)=(k-1)*dx+dx/2+x0
highdim(k)=0
end if
end do
end if
end do
! check if enter_ind = exit_ind, if so, remove this case
do i = min_lowdim,max_lowdim
k=0
do j =1, num_conti_zones(i)
if ((enter_ind(i,j)==0).and.(j<=num_conti_zones(i))) then
if ((i>diag_lat1).and.(i<diag_lat2)) then
print*,'enter ind(i,j): ',enter_ind(i,j),'enter ind(i,j+1): ',enter_ind(i,j+1)
print*,'exit ind(i,j) : ',exit_ind(i,j),'exit ind(i,j+1) : ',exit_ind(i,j+1)
end if
do kk=j,num_conti_zones(i)-1
enter_ind(i,kk)=enter_ind(i,kk+1)
exit_ind(i,kk)=exit_ind(i,kk+1)
end do
if ((i>diag_lat1).and.(i<diag_lat2)) then
print*,'enter ind(i,j): ',enter_ind(i,j),'enter ind(i,j+1): ',enter_ind(i,j+1)
print*,'exit ind(i,j) : ',exit_ind(i,j),'exit ind(i,j+1) : ',exit_ind(i,j+1)
end if
num_conti_zones(i)=num_conti_zones(i)-1
print*,'latitude band: ',i,'remove block: ',j
end if
end do
end do
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! End of the first section !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!x0 = 5000. ! origin coord, y
!y0 = 5000. ! origin coord, x
!dx = 10000.0 ! dx
!dy = 10000.0 ! dy
!x0 = 125. ! origin coord, y
!y0 = 125. ! origin coord, x
!dx = 250.0 ! dx
!dy = 250.0 ! dy
x0 = 500 ! origin coord, y
y0 = 500 ! origin coord, x
dx = 1000 ! dx
dy = 1000 ! dy
xmax = float(nx_dem)*dx + x0
ymax = float(ny_dem)*dy + y0
datum = 0.0
!open (999,file='perimeter.loop_NA_250m_Vkeepsinglecells.curve')
!open (999,file='perimeter.loop_NA_250m_Vdelband2.curve')
!open (999,file='perimeter.loop_NA_250m_Vcurr.curve')
!open (999,file='perimeter.loop_NA_250m_puns.curve')
open (999,file='perimeter.loop_NA_1km.curve')
!open (999,file='perimeter.loop_NA_250m_Vtmp.curve')
write(999,*) '#NA_250m'
do ii = 1, num_per_pts
!write(999,*) float(perimeter(ii,1)-1)*dx+x0, float(perimeter(ii,2)-1)*dy+y0
!BH:
!write(999,*) float(perimeter(ii,1)-1)*dx+dx/2.+x0, float(perimeter(ii,2)-1)*dy+dy/2.+y0
write(999,*) (perimeter(ii,1)-1)*dx+dx/2+x0, (perimeter(ii,2)-1)*dy+dy/2+y0
end do
close(999)
!
num_solid = 1
! now we count up surface, L, R, F, B number of triangles and points
! to make the indexing easier, we dump out all points, so the
! num points does not depend on the mask
! calculate total number of points
!num_points = 2*nx_dem*ny_dem
!BH only perimeter:
num_points = 2 * (num_per_pts-1)
!Because perimeter(1,1)=perimeter(end,1)
! currently we are gridding 2 triangles per box of four nodes, top and bottom
!BH: mindim_range is the number of rectangles elongated along the largest dim
!BH: perimeter + topx2 (top + bottom)
!BH: the following is a way to calculate the number of triangles fot
!non-too-fancy geometries. just look at how many contiguous zones per latitude
!bands.
!ii=0
!do i = min_lowdim,max_lowdim - 1
!ii=ii+num_conti_zones(i)
!end do
!num_tri_topface=2*ii
!num_triangles = 2 * (num_per_pts-1) + 2*num_tri_topface
!BH: However, sometimes it's not that simple, and one contiguous zone in
!latitude band i matches more than one continguous zones in latitude band i+1.
! in those specific cases, you should move up to latitude band i+1, and look
! back south toward latitude band i, to define the triangles that link i & i+1
! So further we will quickly loop over the whole domain to allocate space for
! triangles.
! currently hard wired for 6 patches
! we also allocate the max size for patches- num_triangles
!num_patches = 6
num_patches = 3
!allocate(DEM(nx_dem,ny_dem),points(num_points,3),triangles(num_triangles,3), &
!patches(num_patches,num_triangles),num_tri_patches(num_triangles),bottom_elev(nx_dem,ny_dem), &
!perim_reordered(num_per_pts-1,4))
!allocate(DEM(nx_dem,ny_dem),points(num_points,3), &
!bottom_elev(nx_dem,ny_dem), &
!perim_reordered(num_per_pts-1,4))
allocate(points(num_points,3),perim_reordered(num_per_pts-1,4))
!bottom_elev = 0. ! elevation of the bottom of the domain, init to 0.
!dem = 1000. ! elevation of the top of the domain, init to 0.
dem_filename = 'demcol.txt'
!pfsol_filename = 'CONUS_new_edge_test.pfsol'
!pfsol_filename = 'CONUS_new_edge_test_lighter_V2.pfsol'
!pfsol_filename = 'CONUS_new_edge_test_lighter_V3.pfsol'
!pfsol_filename = 'CONUS_new_edge_test_lighter_V4.pfsol'
!pfsol_filename = 'NA_250m_new_edge_test_lighter_Vkeepsinglecells.pfsol'
!pfsol_filename = 'NA_250m_lighter_Vdlband2.pfsol'
!pfsol_filename = 'NA_250m_lighter_Vcurr.pfsol'
!pfsol_filename = 'NA_250m_lighter_puns.pfsol'
pfsol_filename = 'NA_1km.pfsol'
!pfsol_filename = 'NA_250m_lighter_Vtmp.pfsol'
print*, 'flag 1'
!bottom_elev(i,j) = 0.0
!constant bottom, dem can be read from file
!do j = 1, ny_dem
!do i =1, nx_dem
!bottom_elev(i,j) = 0.0
!end do
!end do
! loop over points and assign, jj = point counter
! first the DEM/upper surface
! we assume points are in the center of the digital elevation tile
!jj = 1
!do i = 1, nx_dem
!do j = 1, ny_dem
!points(jj,1) = float(i-1)*dx + dx/2. + x0
!points(jj,2) = float(j-1)*dy + dy/2. + y0
!points(jj,3) = dem(i,j)
!jj = jj + 1
!end do
!end do
! then the lower surface; currently a flat surface - i.e. datum
!do i = 1, nx_dem
!do j = 1, ny_dem
!points(jj,1) = float(i-1)*dx + dx/2. + x0
!points(jj,2) = float(j-1)*dy + dy/2. + y0
!points(jj,3) = bottom_elev(i,j)
!jj = jj + 1
!end do
!end do
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!! Fill up the vertice matrix (points)
points=0.0
!BH:
jj=1
!stop one before because perimeter (1,1) = perimeter(end,1)
do kk = 1, num_per_pts-1
!points(jj,1)=float(perimeter(kk,1)-1)*dx+x0
!points(jj,2)=float(perimeter(kk,2)-1)*dy+y0
!points(jj,1)=float(perimeter(kk,1)-1)*dx+dx/2.+x0
!points(jj,2)=float(perimeter(kk,2)-1)*dy+dy/2.+y0
!points(jj,3)=1000.0
points(jj,1)=(perimeter(kk,1)-1)*dx+dx/2+x0
points(jj,2)=(perimeter(kk,2)-1)*dy+dy/2+y0
points(jj,3)=1000
jj = jj + 1
end do
do kk = 1, num_per_pts-1
!points(jj,1)=float(perimeter(kk,1)-1)*dx+x0
!points(jj,2)=float(perimeter(kk,2)-1)*dy+y0
!points(jj,1)=float(perimeter(kk,1)-1)*dx+dx/2.+x0
!points(jj,2)=float(perimeter(kk,2)-1)*dy+dy/2.+y0
!points(jj,3)=0.0
points(jj,1)=(perimeter(kk,1)-1)*dx+dx/2+x0
points(jj,2)=(perimeter(kk,2)-1)*dy+dy/2+y0
points(jj,3)=0
jj = jj+ 1
end do
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
do kk = 1, num_per_pts-1
!perim_reordered(kk,1)=float(perimeter(kk,2)-1)*dy+y0
!perim_reordered(kk,2)=float(perimeter(kk,1)-1)*dx+x0
!perim_reordered(kk,1)=float(perimeter(kk,2)-1)*dy+dy/2.+y0
!perim_reordered(kk,2)=float(perimeter(kk,1)-1)*dx+dx/2.+x0
!perim_reordered(kk,3)= kk
!perim_reordered(kk,4)=perimeter(kk,2)
perim_reordered(kk,1)=(perimeter(kk,2)-1)*dy+dy/2+y0
perim_reordered(kk,2)=(perimeter(kk,1)-1)*dx+dx/2+x0
perim_reordered(kk,3)= kk
perim_reordered(kk,4)=perimeter(kk,2)
end do
!test: check in the case where exit ind is not found on perimeter and enter ind
!of next block neither: then remove that block;
do i = min_lowdim,max_lowdim
k=0
do j =1, num_conti_zones(i)
!!!! locate perimeter cell for enter & exit index
enter_exist=0
exit_exist=0
do k = 1,num_per_pts-1
if ((perim_reordered(k,4)==i).and.(perim_reordered(k,2)==enter_ind(i,j))) then
enterind=perim_reordered(k,3)
enter_exist=1
end if
if ((perim_reordered(k,4)==i).and.(perim_reordered(k,2)==exit_ind(i,j))) then
exitind=perim_reordered(k,3)
exit_exist=1
end if
end do
if ((enter_exist==1).and.(exit_exist==0).and.(j<num_conti_zones(i))) then
!identify the next matching exit:
do jj=j+1,num_conti_zones(i)
do k = 1,num_per_pts-1
if ((perim_reordered(k,4)==i).and.(perim_reordered(k,2)==exit_ind(i,jj))) then
exitind=perim_reordered(k,3)
exit_exist=1
end if
end do
if (exit_exist==1) exit
end do
!jj becomes the index location of the next matching exit
!say current j=5 , num_conti_zones(i) = 13 and jj=8
!=> from jj=8 to 13 all index (enter and exit) have to be shifted to
! 5 to 10 and new num conti zones(i)=10
exit_ind(i,j)=exit_ind(i,jj)
!exit_ind(i,j)=exit_ind(i,j+1)
if (j<num_conti_zones(i)-1) then
!do kk=j,num_conti_zones(i)-1
do kk=j,j+num_conti_zones(i)-jj
!enter_ind(i,kk+1)=enter_ind(i,kk+2)
!exit_ind(i,kk+1)=exit_ind(i,kk+2)
enter_ind(i,kk+1)=enter_ind(i,kk+(jj-j)+1)
exit_ind(i,kk+1)=exit_ind(i,kk+(jj-j)+1)
end do
end if
num_conti_zones(i)=num_conti_zones(i)-(jj-j)
!num_conti_zones(i)=num_conti_zones(i)-1
print*,'latitude band: ',i,'remove blocks (bcse not matching exit): ',j+1,'to',jj
end if
end do
end do
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!! Here: loop over all the domain to count how many triangles
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! to allocate space
num_tri_topface=0
!!!! Loop1: biggest loop: look at each latitude band:
do i =min_lowdim,max_lowdim-1
!print*,i
exit_exist=1
!!!! Loop2: for each latitude, loop over contiguous zones:
if (i==perimeter(1,2)) then
print*,i
print*,num_conti_zones(i)
print*,num_conti_zones(i+1)
end if
if (i==perimeter(1,2)-1) then
print*,i
print*,num_conti_zones(i)
print*,num_conti_zones(i+1)
end if
!if (i>perimeter(1,2)) then
!print*,i
!end if
do j=1,num_conti_zones(i)
!!!! locate perimeter cell for enter & exit index
enter_exist=0
exit_exist=0
do k = 1,num_per_pts-1
if ((perim_reordered(k,4)==i).and.(perim_reordered(k,2)==enter_ind(i,j))) then
enterind=perim_reordered(k,3)
enter_exist=1
end if
if ((perim_reordered(k,4)==i).and.(perim_reordered(k,2)==exit_ind(i,j))) then
exitind=perim_reordered(k,3)
enter_exist=1
exit_exist=1
end if
end do
!if (i==perimeter(1,2)) then
!print*,'block number: ',j
!print*,'enter exist? :', enter_exist
!print*,' exit exist? :', exit_exist
!end if
!if (i==perimeter(1,2)-1) then
!print*,'block number: ',j
!print*,'enter exist? :', enter_exist
!print*,' exit exist? :', exit_exist
!end if
!!!! IF 1: check that enter & exit index are actually found on the perimeter,
!because perimeter is defined after enter & exit (above) using RM's code section
if ((enter_exist==1).and.(exit_exist==1)) then
! Identify the actual triangle vertice for enter & exit.
! for instance, if enter is like
! *
! * * *
! ^ ^
! then dont take the first arrow (the identified enter index
! but instead take the last one before latitude change along the perimeter:
! this should be corrected close to the end of the perimeter list...
do k = 1,enterind
if (perim_reordered(enterind-k,4)>i) then
enterind=enterind-k+1
EXIT
end if
!on the opposite, if we get lower in latitude, its not good, it means we are in
!
!* * * * * *
! * * * * * * * *
! * * * * * * * * * *
! ^ or ^ *
if (perim_reordered(enterind-k,4)<i) then
! in this case, exit
EXIT
end if
end do
do k = 1,exitind
if (perim_reordered(exitind+k,4)>i) then
exitind=exitind+k-1
EXIT
end if
if (perim_reordered(exitind+k,4)<i) then
EXIT
end if
end do
!if (i==perimeter(1,2)) then
!print*,'exitind: ',exitind
!print*,'enterind:',enterind
!end if