forked from NOAA-GFDL/SIS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ice_utils.F90
370 lines (321 loc) · 15.1 KB
/
ice_utils.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
!***********************************************************************
!* GNU General Public License *
!* This file is a part of SIS2. *
!* *
!* SIS2 is free software; you can redistribute it and/or modify it and *
!* are expected to follow the terms of the GNU General Public License *
!* as published by the Free Software Foundation; either version 2 of *
!* the License, or (at your option) any later version. *
!* *
!* SIS2 is distributed in the hope that it will be useful, but WITHOUT *
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
!* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public *
!* License for more details. *
!* *
!* For the full text of the GNU General Public License, *
!* write to: Free Software Foundation, Inc., *
!* 675 Mass Ave, Cambridge, MA 02139, USA. *
!* or see: http://www.gnu.org/licenses/gpl.html *
!***********************************************************************
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! This module contains convenient utilities for use by the SIS2 sea ice model. !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
module ice_utils_mod
use MOM_coms, only : g_sum=>reproducing_sum
use MOM_domains, only : SCALAR_PAIR, CGRID_NE, BGRID_NE, To_All
use MOM_error_handler, only : SIS_error=>MOM_error, FATAL, WARNING, SIS_mesg=>MOM_mesg
use MOM_error_handler, only : is_root_pe
use SIS_diag_mediator, only : post_SIS_data, SIS_diag_ctrl
use MOM_checksums, only : hchksum, Bchksum, uchksum, vchksum
use SIS_error_checking, only : check_redundant_B
use SIS_hor_grid, only : SIS_hor_grid_type
implicit none ; private
public :: get_avg, post_avg, ice_line, is_NaN, g_sum, ice_grid_chksum
interface post_avg
module procedure post_avg_3d, post_avg_4d
end interface
contains
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! get_avg - take area weighted average over some or all thickness categories. !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
subroutine get_avg(x, cn, avg, wtd)
real, dimension(:,:,:), intent(in) :: x
real, dimension(:,:,:), intent(in) :: cn
real, dimension(:,:), intent(out) :: avg
logical, optional, intent(in) :: wtd
real, dimension(size(x,1),size(x,2)) :: wts
logical :: do_wt
integer :: i, j, k, ni, nj, nk
do_wt = .false. ; if (present(wtd)) do_wt = wtd
ni = size(x,1) ; nj = size(x,2); nk = size(x,3)
if ((size(cn,1) /= ni) .or. (size(cn,2) /= nj) .or. (size(cn,3) /= nk)) &
call SIS_error(FATAL, "Mismatched i- or j- sizes of x and cn in get_avg.")
if ((size(avg,1) /= ni) .or. (size(avg,2) /= nj)) &
call SIS_error(FATAL, "Mismatched i- or j- sizes of x and avg in get_avg.")
if (size(cn,3) /= nk) &
call SIS_error(FATAL, "Mismatched category sizes of x and cn in get_avg.")
if (do_wt) then
avg(:,:) = 0.0 ; wts(:,:) = 0.0
!$OMP parallel do default(none) shared(ni,nj,nk,avg,cn,x,wts)
do j=1,nj
do k=1,nk ; do i=1,ni
avg(i,j) = avg(i,j) + cn(i,j,k)*x(i,j,k)
wts(i,j) = wts(i,j) + cn(i,j,k)
enddo ; enddo
do i=1,ni
if (wts(i,j) > 0.) then
avg(i,j) = avg(i,j) / wts(i,j)
else
avg(i,j) = 0.0
endif
enddo
enddo
else
avg(:,:) = 0.0
!$OMP parallel do default(none) shared(ni,nj,nk,avg,cn,x)
do j=1,nj
do k=1,nk ; do i=1,ni
avg(i,j) = avg(i,j) + cn(i,j,k)*x(i,j,k)
enddo ; enddo
enddo
endif
end subroutine get_avg
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! ice_line - Write out a line with the northern and southern hemisphere ice !
! ice extents and global mean sea surface temperature. !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
subroutine ice_line(year, day, second, cn_ocn, sst, G)
integer, intent(in) :: year, day, second
type(SIS_hor_grid_type), intent(in) :: G
real, dimension(G%isc:G%iec,G%jsc:G%jec), intent(in) :: cn_ocn
real, dimension(G%isc:G%iec,G%jsc:G%jec), intent(in) :: sst
real, dimension(G%isc:G%iec,G%jsc:G%jec) :: x
real :: gx(3)
integer :: n, i, j, isc, iec, jsc, jec
isc = G%isc ; iec = G%iec ; jsc = G%jsc ; jec = G%jec
if (.not.(second==0 .and. mod(day,5)==0) ) return
do n=-1,1,2
do j=jsc,jec ; do i=isc,iec
x(i,j) = 0.0
if (cn_ocn(i,j)<0.85 .and. n*G%geoLatT(i,j)>0.0) &
x(i,j) = G%mask2dT(i,j)*G%areaT(i,j)
enddo ; enddo
gx((n+3)/2) = g_sum(x(isc:iec,jsc:jec))/1e12
enddo
gx(3) = g_sum(sst(isc:iec,jsc:jec)*G%mask2dT(isc:iec,jsc:jec)*G%areaT(isc:iec,jsc:jec)) / &
(g_sum(G%mask2dT(isc:iec,jsc:jec)*G%areaT(isc:iec,jsc:jec)) + 1e-10)
!
! print info every 5 days
!
if ( is_root_pe() .and. second==0 .and. mod(day,5)==0 ) &
print '(a,2I4,3F10.5)','ICE y/d (SH_ext NH_ext SST):', year, day, gx
end subroutine ice_line
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! post_avg - take area weighted average over some or all thickness categories !
! and offer it for diagnostic output. !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
subroutine post_avg_3d(id, val, part, diag, G, mask, scale, offset, wtd)
integer, intent(in) :: id
real, dimension(:,:,:), intent(in) :: val, part
type(SIS_diag_ctrl), intent(in) :: diag
type(SIS_hor_grid_type), optional, intent(in) :: G
logical, dimension(:,:), optional, intent(in) :: mask
real, optional, intent(in) :: scale, offset
logical, optional, intent(in) :: wtd
! This subroutine determines the average of a quantity across thickness
! categories and does a send data on it.
real :: avg(size(val,1),size(val,2)), wts(size(val,1),size(val,2))
real :: scl, off
logical :: do_wt
integer :: i, j, k, ni, nj, nk, is, ie, js, je
ni = size(val,1) ; nj = size(val,2) ; nk = size(val,3)
if (size(part,1) /= ni) call SIS_error(FATAL, &
"Mismatched i-sizes in post_avg.")
if (size(part,2) /= nj) call SIS_error(FATAL, &
"Mismatched j-sizes in post_avg.")
if (size(part,3) /= nk) call SIS_error(FATAL, &
"Mismatched k-sizes in post_avg.")
if (present(G)) then ! Account for the fact that arrays here start at 1.
if ((ni == G%isc-G%iec + 1) .or. (ni == G%isc-G%iec + 2)) then
is = 1; ie = ni ! These arrays have no halos.
elseif (ni == G%ied-(G%isd-1)) then ! Arrays have halos.
is = G%isc - (G%isd-1) ; ie = G%iec - (G%isd-1)
elseif (ni == G%ied-(G%isd-1)+1) then ! Symmetric arrays with halos.
is = G%isc - (G%isd-1) ; ie = G%iec - (G%isd-1) + 1
else
call SIS_error(FATAL,"post_avg: peculiar size in i-direction")
endif
if ((nj == G%jsc-G%jec + 1) .or. (nj == G%jsc-G%jec + 2)) then
js = 1; je = nj ! These arrays have no halos.
elseif (nj == G%jed-(G%jsd-1)) then ! Arrays have halos
js = G%jsc - (G%jsd-1) ; je = G%jec - (G%jsd-1)
elseif (nj == G%jed-(G%jsd-1)+1) then ! Symmetric arrays with halos.
js = G%jsc - (G%jsd-1) ; je = G%jec - (G%jsd-1) + 1
else
call SIS_error(FATAL,"post_avg: peculiar size in j-direction")
endif
else
is = 1; ie = ni ; js = 1 ; je = nj
endif
scl = 1.0 ; if (present(scale)) scl = scale
off = 0.0 ; if (present(offset)) off = offset
do_wt = .false. ; if (present(wtd)) do_wt = wtd
if (do_wt) then
avg(:,:) = 0.0 ; wts(:,:) = 0.0
do k=1,nk ; do j=js,je ; do i=is,ie
avg(i,j) = avg(i,j) + part(i,j,k)*(scl*val(i,j,k) + off)
wts(i,j) = wts(i,j) + part(i,j,k)
enddo ; enddo ; enddo
do j=js,je ; do i=is,ie
if (wts(i,j) > 0.) then
avg(i,j) = avg(i,j) / wts(i,j)
else
avg(i,j) = 0.0
endif
enddo ; enddo
else
avg(:,:) = 0.0
do k=1,nk ; do j=js,je ; do i=is,ie
avg(i,j) = avg(i,j) + part(i,j,k)*(scl*val(i,j,k) + off)
enddo ; enddo ; enddo
endif
call post_SIS_data(id, avg, diag, mask=mask)
end subroutine post_avg_3d
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! post_avg - take area weighted average over some or all thickness categories !
! and offer it for diagnostic output. !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
subroutine post_avg_4d(id, val, part, diag, G, mask, scale, offset, wtd)
integer, intent(in) :: id
real, dimension(:,:,:,:), intent(in) :: val
real, dimension(:,:,:), intent(in) :: part
type(SIS_diag_ctrl), intent(in) :: diag
type(SIS_hor_grid_type), optional, intent(in) :: G
logical, dimension(:,:), optional, intent(in) :: mask
real, optional, intent(in) :: scale, offset
logical, optional, intent(in) :: wtd
! This subroutine determines the average of a quantity across thickness
! categories and does a send data on it.
real :: avg(size(val,1),size(val,2)), wts(size(val,1),size(val,2))
real :: scl, off, I_nLay
logical :: do_wt
integer :: i, j, k, L, ni, nj, nk, nLay, is, ie, js, je
ni = size(val,1) ; nj = size(val,2) ; nk = size(val,3) ; nLay = size(val,4)
if (size(part,1) /= ni) call SIS_error(FATAL, &
"Mismatched i-sizes in post_avg.")
if (size(part,2) /= nj) call SIS_error(FATAL, &
"Mismatched j-sizes in post_avg.")
if (size(part,3) /= nk) call SIS_error(FATAL, &
"Mismatched k-sizes in post_avg.")
if (present(G)) then ! Account for the fact that arrays here start at 1.
if ((ni == G%isc-G%iec + 1) .or. (ni == G%isc-G%iec + 2)) then
is = 1; ie = ni ! These arrays have no halos.
elseif (ni == G%ied-(G%isd-1)) then ! Arrays have halos.
is = G%isc - (G%isd-1) ; ie = G%iec - (G%isd-1)
elseif (ni == G%ied-(G%isd-1)+1) then ! Symmetric arrays with halos.
is = G%isc - (G%isd-1) ; ie = G%iec - (G%isd-1) + 1
else
call SIS_error(FATAL,"post_avg: peculiar size in i-direction")
endif
if ((nj == G%jsc-G%jec + 1) .or. (nj == G%jsc-G%jec + 2)) then
js = 1; je = nj ! These arrays have no halos.
elseif (nj == G%jed-(G%jsd-1)) then ! Arrays have halos
js = G%jsc - (G%jsd-1) ; je = G%jec - (G%jsd-1)
elseif (nj == G%jed-(G%jsd-1)+1) then ! Symmetric arrays with halos.
js = G%jsc - (G%jsd-1) ; je = G%jec - (G%jsd-1) + 1
else
call SIS_error(FATAL,"post_avg: peculiar size in j-direction")
endif
else
is = 1; ie = ni ; js = 1 ; je = nj
endif
scl = 1.0 ; if (present(scale)) scl = scale
off = 0.0 ; if (present(offset)) off = offset
do_wt = .false. ; if (present(wtd)) do_wt = wtd
if (do_wt) then
avg(:,:) = 0.0 ; wts(:,:) = 0.0
do L=1,nLay ; do k=1,nk ; do j=js,je ; do i=is,ie
avg(i,j) = avg(i,j) + part(i,j,k)*(scl*val(i,j,k,L) + off)
enddo ; enddo ; enddo ; enddo
do k=1,nk ; do j=js,je ; do i=is,ie
wts(i,j) = wts(i,j) + nLay * part(i,j,k)
enddo ; enddo ; enddo
do j=js,je ; do i=is,ie
if (wts(i,j) > 0.) then
avg(i,j) = avg(i,j) / wts(i,j)
else
avg(i,j) = 0.0
endif
enddo ; enddo
else
avg(:,:) = 0.0 ; I_nLay = 1.0/nLay
do L=1,nLay ; do k=1,nk ; do j=js,je ; do i=is,ie
avg(i,j) = avg(i,j) + (part(i,j,k)*I_nLay)*(scl*val(i,j,k,L) + off)
enddo ; enddo ; enddo ; enddo
endif
call post_SIS_data(id, avg, diag, mask=mask)
end subroutine post_avg_4d
subroutine ice_grid_chksum(G, haloshift)
type(SIS_hor_grid_type), optional, intent(inout) :: G
integer, optional, intent(in) :: haloshift
integer :: isc, iec, jsc, jec, hs
isc = G%isc ; iec = G%iec ; jsc = G%jsc ; jec = G%jec
hs = 1 ; if (present(haloshift)) hs = haloshift
call hchksum(G%mask2dT, "G%mask2dT", G%HI, haloshift=hs)
call hchksum(G%geoLatT, "G%geoLatT", G%HI, haloshift=hs)
call hchksum(G%geoLonT, "G%geoLonT", G%HI, haloshift=hs)
call hchksum(G%dxT, "G%dxT", G%HI, haloshift=hs)
call hchksum(G%IdxT, "G%IdxT", G%HI, haloshift=hs)
call hchksum(G%IdyT, "G%IdyT", G%HI, haloshift=hs)
call hchksum(G%dyT, "G%dyT", G%HI, haloshift=hs)
call hchksum(G%areaT, "G%areaT", G%HI, haloshift=hs)
call hchksum(G%IareaT, "G%IareaT", G%HI, haloshift=hs)
call hchksum(G%mask2dT, "G%mask2dT", G%HI, haloshift=hs)
call hchksum(G%cos_rot, "G%cos_rot", G%HI)
call hchksum(G%sin_rot, "G%sin_rot", G%HI)
call Bchksum(G%mask2dBu, "G%mask2dBu", G%HI, haloshift=hs)
call Bchksum(G%geoLatBu, "G%geoLatBu", G%HI, haloshift=hs)
call Bchksum(G%geoLonBu, "G%geoLonBu", G%HI, haloshift=hs)
call Bchksum(G%dxBu, "G%dxBu", G%HI, haloshift=hs)
call Bchksum(G%dyBu, "G%dyBu", G%HI, haloshift=hs)
call Bchksum(G%IdxBu, "G%IdxBu", G%HI, haloshift=hs)
call Bchksum(G%IdyBu, "G%IdyBu", G%HI, haloshift=hs)
call Bchksum(G%areaBu, "G%areaBu", G%HI, haloshift=hs)
call Bchksum(G%IareaBu, "G%IareaBu", G%HI, haloshift=hs)
call check_redundant_B("G%dx/dyBu", G%dxBu, G%dyBu, G, &
isc-1, iec+1, jsc-1, jec+1, To_All+Scalar_Pair)
call check_redundant_B("G%Idx/dyBu", G%IdxBu, G%IdyBu, G, &
isc-1, iec+1, jsc-1, jec+1, To_All+Scalar_Pair)
call check_redundant_B("G%areaBu", G%areaBu, G, isc-1, iec+1, jsc-1, jec+1)
call check_redundant_B("G%IareaBu", G%IareaBu, G, isc-1, iec+1, jsc-1, jec+1)
call uchksum(G%mask2dCu, "G%mask2dCu", G%HI, haloshift=hs)
call uchksum(G%geoLatCu, "G%geoLatCu", G%HI, haloshift=hs)
call uchksum(G%geoLonCu, "G%geolonCu", G%HI, haloshift=hs)
call uchksum(G%dxCu, "G%dxCu", G%HI, haloshift=hs)
call uchksum(G%dyCu, "G%dyCu", G%HI, haloshift=hs)
call uchksum(G%IdxCu, "G%IdxCu", G%HI, haloshift=hs)
call uchksum(G%IdyCu, "G%IdyCu", G%HI, haloshift=hs)
call uchksum(G%areaCu, "G%areaCu", G%HI, haloshift=hs)
call uchksum(G%IareaCu, "G%IareaCu", G%HI, haloshift=hs)
call vchksum(G%mask2dCv, "G%mask2dCv", G%HI, haloshift=hs)
call vchksum(G%geoLatCv, "G%geoLatCv", G%HI, haloshift=hs)
call vchksum(G%geoLonCv, "G%geoLonCv", G%HI, haloshift=hs)
call vchksum(G%dxCv, "G%dxCv", G%HI, haloshift=hs)
call vchksum(G%dyCv, "G%dyCv", G%HI, haloshift=hs)
call vchksum(G%IdxCv, "G%IdxCv", G%HI, haloshift=hs)
call vchksum(G%IdyCv, "G%IdyCv", G%HI, haloshift=hs)
call uchksum(G%areaCu, "G%areaCv", G%HI, haloshift=hs)
call uchksum(G%IareaCu, "G%IareaCv", G%HI, haloshift=hs)
call hchksum(G%bathyT, "G%bathyT", G%HI, haloshift=hs)
call Bchksum(G%CoriolisBu, "G%CoriolisBu", G%HI, haloshift=hs)
call hchksum(G%dF_dx, "G%dF_dx", G%HI, haloshift=hs)
call hchksum(G%dF_dy, "G%dF_dy", G%HI, haloshift=hs)
end subroutine ice_grid_chksum
function is_NaN(x)
real, intent(in) :: x
logical :: is_nan
! This subroutine returns .true. if x is a NaN, and .false. otherwise.
is_nan = (((x < 0.0) .and. (x >= 0.0)) .or. &
(.not.(x < 0.0) .and. .not.(x >= 0.0)))
end function is_nan
end module ice_utils_mod