-
Notifications
You must be signed in to change notification settings - Fork 3
/
deseas.f90
254 lines (218 loc) · 8.07 KB
/
deseas.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
program deseas
!
! Get rid of isolated seas
!
! Simple diffusion algorithm. Sweep from SW to NE and then back.
!
!
! Usage: deseas file_in file_out
use iso_fortran_env
use netcdf
implicit none
integer(int32) :: i,j,counter,its,its1,its2,sea_num,iblock,jblock,counter2
integer(int32) :: im,ip,jm,jp,land
integer(int32) :: nxt,nyt ! Size of model T grid
integer(int32) :: ncid_out,depth_id_out ! NetCDF ids
integer(int32) :: ncid_topo, depth_id ! NetCDF ids
integer(int32) :: dids_topo_out(2) ! NetCDF ids
integer(int32) :: dids_topo(2) ! NetCDF ids
real(real32),allocatable,dimension(:,:) :: depth
integer(int16),allocatable,dimension(:,:) :: sea
character*128 :: file_in,file_out
real(real32), parameter :: my_miss = -1e30
logical :: choke_west, choke_east, choke_north, choke_south
if(command_argument_count() .ne. 2 ) then
write(*,*) 'ERROR: Wrong number of arguments'
write(*,*) 'Usage: deseas file_in file_out'
stop
endif
call get_command_argument(1,file_in)
call get_command_argument(2,file_out)
! Get info on the grid from input
call handle_error(nf90_open(trim(file_in),nf90_nowrite,ncid_topo))
call handle_error(nf90_inq_dimid(ncid_topo,'nx',dids_topo(1)))
call handle_error(nf90_inq_dimid(ncid_topo,'ny',dids_topo(2)))
call handle_error(nf90_inquire_dimension(ncid_topo,dids_topo(1),len=nxt))
call handle_error(nf90_inquire_dimension(ncid_topo,dids_topo(2),len=nyt))
call handle_error(nf90_inq_varid(ncid_topo,'depth',depth_id))
allocate(depth(nxt,nyt),sea(nxt,nyt))
call handle_error(nf90_get_var(ncid_topo,depth_id,depth))
call handle_error(nf90_close(ncid_topo))
! Do
land=nxt+nyt+1
sea=land
do j=1,nyt
do i=1,nxt
if(depth(i,j) > 0.0 ) sea(i,j) = i+j
enddo
if(all(depth(:,j) > 0.0 )) sea(:,j)=0 ! Southern Ocean all water across
enddo
do its=1,150 ! Only need high number after massive editing session with fjords. Normally 10 or so sweeps works.
counter=0
sea_num=1
! Get number of seas
do j=2,nyt-1
i=1
jm=j-1
jp=j+1
if( sea(i,j) < land .and. sea(i,j) > 0 ) then
if(sea(i,j) >= sea_num) then
sea(i,j) = sea_num
sea_num = max(min(sea_num+1,sea(nxt,j),sea(i,j-1),sea(i+1,j),sea(i,j+1)),sea_num)
endif
endif
do i=2,nxt-1
im=i-1
ip=i+1
if( sea(i,j) < land .and. sea(i,j) > 0 ) then
if(sea(i,j) >= sea_num) then
sea(i,j) = sea_num
sea_num = max(min(sea_num+1,sea(i-1,j),sea(i,j-1),sea(i+1,j),sea(i,j+1)),sea_num)
endif
endif
enddo
i=nxt
if( sea(i,j) < land .and. sea(i,j) > 0 ) then
if(sea(i,j) >= sea_num) then
sea(i,j) = sea_num
sea_num = max(min(sea_num+1,sea(i-1,j),sea(i,j-1),sea(1,j),sea(i,j+1)),sea_num)
endif
endif
enddo
j=nyt
do i=2,nxt-1
if( sea(i,j) < land .and. sea(i,j) > 0 ) then
if(sea(i,j) >= sea_num) then
sea(i,j) = sea_num
sea_num = max(min(sea_num+1,sea(i-1,j),sea(i,j-1),sea(i+1,j)),sea_num)
endif
endif
enddo
! Diffuse lowest values surrounding a point.
! Forward sweep.
do j=2,nyt
jm=j-1
jp=min(j+1,nyt)
i=1
im=nxt
ip=2
if(sea(i,j) < land .and. sea(i,j) > 0 ) then
sea(i,j)=min(sea(im,j),sea(ip,j),sea(i,jm),sea(i,jp))
counter=counter+1
endif
do i=2,nxt-1
im=i-1
ip=i+1
if(sea(i,j) < land .and. sea(i,j) > 0 ) then
if ( all([sea(im,j),sea(ip,j),sea(i,j),sea(i,jm),sea(i,jp)] == land) ) then
sea(i,j)=land
else
!get chokes
choke_east = .not. (any(sea(i:ip,jp)==land) .and. any(sea(i:ip,jm)==land))
choke_west = .not. (any(sea(im:i,jp)==land) .and. any(sea(im:i,jm)==land))
choke_south = .not. (any(sea(im,jm:j)==land) .and. any(sea(ip,jm:j)==land))
choke_north = .not. (any(sea(im,j:jp)==land) .and. any(sea(ip,j:jp)==land))
sea(i,j)=min(minval([sea(im,j),sea(ip,j),sea(i,jm),sea(i,jp)], &
mask=[choke_west,choke_east,choke_south,choke_north]),land)
endif
counter=counter+1
endif
enddo
i=nxt
im=1
ip=i-1
if(sea(i,j) < land .and. sea(i,j) > 0 ) then
sea(i,j)=min(sea(im,j),sea(ip,j),sea(i,jm),sea(i,jp))
counter=counter+1
endif
enddo
! Backward sweep
do j=nyt,2,-1
jm=j-1
jp=min(j+1,nyt)
i=1
im=nxt
ip=2
if(sea(i,j) < land .and. sea(i,j) > 0 ) then
sea(i,j)=min(sea(im,j),sea(ip,j),sea(i,jm),sea(i,jp))
counter=counter+1
endif
do i=nxt-1,2,-1
im=i-1
ip=i+1
if(sea(i,j) < land .and. sea(i,j) > 0 ) then
if ( all([sea(im,j),sea(ip,j),sea(i,j),sea(i,jm),sea(i,jp)] == land) ) then
sea(i,j)=land
else
!get chokes
choke_east = .not. (any(sea(i:ip,jp)==land) .and. any(sea(i:ip,jm)==land))
choke_west = .not. (any(sea(im:i,jp)==land) .and. any(sea(im:i,jm)==land))
choke_south = .not. (any(sea(im,jm:j)==land) .and. any(sea(ip,jm:j)==land))
choke_north = .not. (any(sea(im,j:jp)==land) .and. any(sea(ip,j:jp)==land))
sea(i,j)=min(minval([sea(im,j),sea(ip,j),sea(i,jm),sea(i,jp)], &
mask=[choke_west,choke_east,choke_south,choke_north]),land)
endif
counter=counter+1
endif
enddo
i=nxt
im=1
ip=i-1
if(sea(i,j) < land .and. sea(i,j) > 0 ) then
sea(i,j)=min(sea(im,j),sea(ip,j),sea(i,jm),sea(i,jp))
counter=counter+1
endif
enddo
write(*,*) counter,sea_num
! If we only have one sea or no changes are made we are finished.
if(counter==0 .or.sea_num==1) exit
enddo
! Write out new topography
do j=1,nyt
do i=1,nxt
if( sea(i,j)>0) depth(i,j)=my_miss
enddo
enddo
print *, 'Wring'
call handle_error(nf90_create(trim(file_out),ior(nf90_netcdf4,nf90_clobber),ncid_out))
call handle_error(nf90_def_dim(ncid_out,'xx',nxt,dids_topo_out(1)))
call handle_error(nf90_def_dim(ncid_out,'yy',nyt,dids_topo_out(2)))
call handle_error(nf90_def_var(ncid_out,'depth',nf90_float,dids_topo_out,depth_id_out, &
chunksizes=[nxt/10,nyt/10], &
deflate_level=1,shuffle=.true.))
call handle_error(nf90_put_att(ncid_out,depth_id_out,'missing_value',my_miss))
call handle_error(nf90_put_att(ncid_out,depth_id_out,'long_name','depth'))
call handle_error(nf90_put_att(ncid_out,depth_id_out,'units','m'))
call handle_error(nf90_put_att(ncid_out,nf90_global,'original_file',trim(file_in)))
call handle_error(nf90_put_att(ncid_out,depth_id_out,'lakes_removed','yes'))
call handle_error(nf90_enddef(ncid_out))
print *, 'putting'
call handle_error(nf90_put_var(ncid_out,depth_id_out,depth))
call handle_error(nf90_close(ncid_out))
call handle_error(nf90_create(trim('sea_num'),ior(nf90_netcdf4,nf90_clobber),ncid_out))
call handle_error(nf90_def_dim(ncid_out,'xx',nxt,dids_topo_out(1)))
call handle_error(nf90_def_dim(ncid_out,'yy',nyt,dids_topo_out(2)))
call handle_error(nf90_def_var(ncid_out,'sea_num',nf90_short,dids_topo_out,depth_id_out, &
chunksizes=[nxt/10,nyt/10], &
deflate_level=1,shuffle=.true.))
call handle_error(nf90_enddef(ncid_out))
call handle_error(nf90_put_var(ncid_out,depth_id_out,sea))
call handle_error(nf90_close(ncid_out))
contains
subroutine handle_error(error_flag,isfatal,err_string)
! Simple error handle for NetCDF
integer(int32),intent(in) :: error_flag
logical, intent(in),optional :: isfatal
character(*), intent(in),optional :: err_string
logical :: fatal
fatal = .true.
if(present(isfatal)) fatal=isfatal
if ( error_flag /= nf90_noerr ) then
if ( fatal ) then
write(*,*) 'FATAL ERROR:',nf90_strerror(error_flag)
if (present(err_string)) write(*,*) trim(err_string)
stop
endif
endif
end subroutine handle_error
end program deseas