forked from geoschem/FVdycoreCubed_GridComp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubeToLatLonRegridder.F90
435 lines (325 loc) · 13.8 KB
/
CubeToLatLonRegridder.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
#define _SUCCESS 0
#define _FAILURE 1
#define _VERIFY(A) if( A/=0) then; if(present(rc)) rc=A; PRINT *, Iam, __LINE__; return; endif
#define _ASSERT(A) if(.not.A) then; if(present(rc)) rc=_FAILURE; PRINT *, Iam, __LINE__; return; endif
#define _RETURN(A) if(present(rc)) rc=A; return
module CubeToLatLonRegridderMod
use MAPL
use CubeLatLonTransformMod
use, intrinsic :: iso_fortran_env, only: REAL32
use, intrinsic :: iso_fortran_env, only: REAL64
implicit none
type, extends (AbstractRegridder) :: CubeToLatLonRegridder
private
type (T_CubeLatLonTransform) :: transform
contains
procedure :: initialize_subclass
procedure :: regrid_scalar_2d_real32
procedure :: regrid_scalar_3d_real32
procedure :: regrid_vector_3d_real32
procedure :: transpose_regrid_scalar_2d_real32
procedure :: transpose_regrid_scalar_3d_real32
procedure :: transpose_regrid_vector_3d_real32
end type CubeToLatLonRegridder
interface CubeToLatLonRegridder
module procedure newCubeToLatLonRegridder
end interface
character(len=*), parameter :: MOD_NAME = 'MAPL_CubeToLatLonRegridder::'
integer, parameter :: NUM_DIMS = 2
contains
function newCubeToLatLonRegridder(regridder_spec, rc) result(regridder)
type (CubeToLatLonRegridder) :: regridder
type (RegridderSpec), intent(inout) :: regridder_spec
integer, optional, intent(out) :: rc
call regridder%initialize(regridder_spec)
end function newCubeToLatLonRegridder
subroutine initialize_subclass(this, unusable, rc)
use MAPL
class (CubeToLatLonRegridder), intent(inout) :: this
class (KeywordEnforcer), optional, intent(in) :: unusable
integer, optional, intent(out) :: rc
real(kind=REAL64), allocatable :: xll(:), yll(:)
integer :: status
character(len=*), parameter :: Iam = MOD_NAME//'initialize_subclass'
integer, parameter :: NUM_DIMS = 2
type (RegridderSpec) :: spec
integer :: N_in(NUM_DIMS), N_out(NUM_DIMS)
integer :: dims(5)
integer :: i0, i1, j0, j1
integer :: local_ij(2,2)
class (AbstractGridFactory), pointer :: factory
spec = this%get_spec()
associate ( grid_in => spec%grid_in, grid_out => spec%grid_out )
call MAPL_GridGet(grid_in, globalCellCountPerDim=dims, rc=status)
_VERIFY(status)
N_in = dims(1:2)
call MAPL_GridGet(grid_out, globalCellCountPerDim=dims, rc=status)
_VERIFY(status)
N_out = dims(1:2)
! Use factory to get global lat/lon 1d arrays
factory => grid_manager%get_factory(grid_out)
select type (latlon_factory => factory)
type is (LatLonGridFactory)
xll = latlon_factory%get_longitudes()
yll = latlon_factory%get_latitudes()
class default
! factory must be lat lon for regrid to lat-lon
_ASSERT(.false.)
end select
call MAPL_grid_interior(grid_out, i0, i1, j0, j1)
local_ij = reshape([i0,i1,j0,j1],[2,2])
this%transform = CubeLatLonCreate(N_in(1), N_in(2), N_out(1), N_out(2), &
& xll(:), yll(:), local_ij, rc=status)
_VERIFY(status)
call CubeLatLonSubset(this%transform,.true.)
end associate
_RETURN(_SUCCESS)
end subroutine initialize_subclass
subroutine regrid_scalar_2d_real32(this, q_in, q_out, rc)
class (CubeToLatLonRegridder), intent(in) :: this
real (kind=REAL32), intent(in) :: q_in(:,:)
real (kind=REAL32), intent(out) :: q_out(:,:)
integer, optional, intent(out) :: rc
integer :: status
character(len=*), parameter :: Iam = MOD_NAME//'regrid_scalar_2d_real32'
real (kind=REAL32) :: undef
real (kind=REAL32), allocatable :: cs_tmp(:,:)
cs_tmp = q_in ! need to copy because of mixed intent in CubeToLatLon().
if (this%has_undef_value()) then
undef = this%get_undef_value()
call CubeToLatLon(this%transform, cs_tmp, q_out, transpose=.false., misval=undef, rc=status)
_VERIFY(status)
else
call CubeToLatLon(this%transform, cs_tmp, q_out, transpose=.false., rc=status)
_VERIFY(status)
end if
deallocate(cs_tmp,stat=status)
_VERIFY(status)
_RETURN(_SUCCESS)
end subroutine regrid_scalar_2d_real32
subroutine regrid_scalar_3d_real32(this, q_in, q_out, rc)
use MAPL
class (CubeToLatLonRegridder), intent(in) :: this
real (kind=REAL32), intent(in) :: q_in(:,:,:)
real (kind=REAL32), intent(out) :: q_out(:,:,:)
integer, optional, intent(out) :: rc
integer :: status
character(len=*), parameter :: Iam = MOD_NAME//'regrid_scalar_2d_real32'
integer :: k
type (RegridderSpec) :: spec
logical :: redistribute
_ASSERT(size(q_in,3) == size(q_out,3))
spec = this%get_spec()
block
integer :: N_in(NUM_DIMS)
integer :: dims(5)
call MAPL_GridGet(spec%grid_in, globalCellCountPerDim=dims, rc=status)
_VERIFY(status)
N_in = dims(1:2)
if ((N_in(1) /= size(q_in,1)) .or. (N_in(2) /= size(q_in,2))) then
redistribute = .true.
else
redistribute = .false.
end if
end block
if (redistribute) then
block
real (kind=REAL32), pointer :: q_in_global(:,:,:)
real (kind=REAL32), allocatable :: q_out_global(:,:,:)
integer :: N_out(NUM_DIMS)
integer :: dims(5)
q_in_global=> null()
call MAPL_CollectiveGather3D(spec%grid_in, q_in, q_in_global, rc=status)
_VERIFY(status)
call MAPL_GridGet(spec%grid_out, globalCellCountPerDim=dims, rc=status)
_VERIFY(status)
N_out = dims(1:2)
allocate(q_out_global(n_out(1), n_out(2), size(q_in_global,3)))
if (size(q_in_global) > 1) then
do k = 1, size(q_in_global,3)
call this%regrid(q_in_global(:,:,k), q_out_global(:,:,k), rc=status)
_VERIFY(status)
end do
end if
deallocate(q_in_global)
call MAPL_CollectiveScatter3D(spec%grid_out, q_out_global, q_out, rc=status)
_VERIFY(status)
deallocate(q_out_global)
end block
else
if (size(q_in) > 1) then
do k = 1, size(q_in,3)
call this%regrid(q_in(:,:,k), q_out(:,:,k), rc=status)
_VERIFY(status)
end do
end if
end if
_RETURN(_SUCCESS)
end subroutine regrid_scalar_3d_real32
subroutine regrid_vector_3d_real32(this, u_in, v_in, u_out, v_out, rotate, rc)
use MAPL
use, intrinsic :: iso_fortran_env, only: REAL32
class (CubeToLatLonRegridder), intent(in) :: this
real(kind=REAL32), intent(in) :: u_in(:,:,:)
real(kind=REAL32), intent(in) :: v_in(:,:,:)
real(kind=REAL32), intent(out) :: u_out(:,:,:)
real(kind=REAL32), intent(out) :: v_out(:,:,:)
logical, optional, intent(in ) :: rotate
integer, optional, intent(out) :: rc
character(len=*), parameter :: Iam = MOD_NAME//'regrid_vector_3d_real32'
logical :: rotateBefore, rotateAfter
integer :: status
logical, parameter :: inputIsLL = .false.
type (RegridderSpec) :: spec
real (kind=REAL32), allocatable :: uvw_in(:,:,:)
real (kind=REAL32), allocatable :: uvw_out(:,:,:)
_ASSERT(size(u_in,3) == size(u_out,3))
_ASSERT(size(v_in,3) == size(v_out,3))
_ASSERT(size(u_in,3) == size(v_in,3))
RotateBefore = InputIsLL
RotateAfter = .not.InputIsLL
if (present(rotate)) then
if (rotate) then
RotateBefore = .true.
RotateAfter = .true.
end if
end if
allocate(uvw_in(size(u_in,1),size(u_in,2),3*size(u_in,3)))
call SphericalToCartesian(this%transform, u_in , v_in , UVW_in , &
& transpose=.false., SphIsLL=InputIsLL, Rotate=RotateBefore, rc=status)
_VERIFY(status)
allocate(uvw_out(size(u_out,1),size(u_out,2),3*size(u_out,3)))
call this%regrid(uvw_in, uvw_out, rc=status)
_VERIFY(status)
deallocate(uvw_in)
call CartesianToSpherical(this%transform, uvw_out, u_out, v_out, &
transpose=.false., SphIsLL=.not.InputIsLL, Rotate=RotateAfter, rc=status)
_VERIFY(status)
deallocate(uvw_out)
_RETURN(_SUCCESS)
end subroutine regrid_vector_3d_real32
subroutine transpose_regrid_scalar_2d_real32(this, q_in, q_out, rc)
class (CubeToLatLonRegridder), intent(in) :: this
real (kind=REAL32), intent(in) :: q_in(:,:)
real (kind=REAL32), intent(out) :: q_out(:,:)
integer, optional, intent(out) :: rc
integer :: status
character(len=*), parameter :: Iam = MOD_NAME//'regrid_scalar_2d_real32'
real (kind=REAL32) :: undef
real (kind=REAL32), allocatable :: ll_tmp(:,:)
ll_tmp = q_in ! need to copy because of mixed intent in CubeToLatLon().
if (this%has_undef_value()) then
undef = this%get_undef_value()
call CubeToLatLon(this%transform, q_out, ll_tmp, transpose=.true., misval=undef, rc=status)
_VERIFY(status)
else
call CubeToLatLon(this%transform, q_out, ll_tmp, transpose=.true., rc=status)
_VERIFY(status)
end if
deallocate(ll_tmp,stat=status)
_VERIFY(status)
_RETURN(_SUCCESS)
end subroutine transpose_regrid_scalar_2d_real32
subroutine transpose_regrid_scalar_3d_real32(this, q_in, q_out, rc)
use MAPL
class (CubeToLatLonRegridder), intent(in) :: this
real (kind=REAL32), intent(in) :: q_in(:,:,:)
real (kind=REAL32), intent(out) :: q_out(:,:,:)
integer, optional, intent(out) :: rc
integer :: status
character(len=*), parameter :: Iam = MOD_NAME//'transpose_regrid_scalar_2d_real32'
integer :: k
type (RegridderSpec) :: spec
logical :: redistribute
_ASSERT(size(q_in,3) == size(q_out,3))
spec = this%get_spec()
block
integer :: N_in(NUM_DIMS)
integer :: dims(5)
call MAPL_GridGet(spec%grid_out, globalCellCountPerDim=dims, rc=status)
_VERIFY(status)
N_in = dims(1:2)
if ((N_in(1) /= size(q_in,1)) .or. (N_in(2) /= size(q_in,2))) then
redistribute = .true.
else
redistribute = .false.
end if
end block
if (redistribute) then
block
real (kind=REAL32), pointer :: q_in_global(:,:,:)
real (kind=REAL32), allocatable :: q_out_global(:,:,:)
integer :: N_out(NUM_DIMS)
integer :: dims(5)
q_in_global=> null()
call MAPL_CollectiveGather3D(spec%grid_out, q_in, q_in_global, rc=status)
_VERIFY(status)
call MAPL_GridGet(spec%grid_in, globalCellCountPerDim=dims, rc=status)
_VERIFY(status)
N_out = dims(1:2)
allocate(q_out_global(n_out(1), n_out(2), size(q_in_global,3)))
if (size(q_in_global) > 1) then
do k = 1, size(q_in_global,3)
call this%transpose_regrid(q_in_global(:,:,k), q_out_global(:,:,k), rc=status)
_VERIFY(status)
end do
end if
deallocate(q_in_global)
call MAPL_CollectiveScatter3D(spec%grid_in, q_out_global, q_out, rc=status)
_VERIFY(status)
deallocate(q_out_global)
end block
else
if (size(q_in) > 1) then
do k = 1, size(q_in,3)
call this%transpose_regrid(q_in(:,:,k), q_out(:,:,k), rc=status)
_VERIFY(status)
end do
end if
end if
_RETURN(_SUCCESS)
end subroutine transpose_regrid_scalar_3d_real32
subroutine transpose_regrid_vector_3d_real32(this, u_in, v_in, u_out, v_out, rotate, rc)
use MAPL
use, intrinsic :: iso_fortran_env, only: REAL32
class (CubeToLatLonRegridder), intent(in) :: this
real(kind=REAL32), intent(in) :: u_in(:,:,:)
real(kind=REAL32), intent(in) :: v_in(:,:,:)
real(kind=REAL32), intent(out) :: u_out(:,:,:)
real(kind=REAL32), intent(out) :: v_out(:,:,:)
logical, optional, intent(in ) :: rotate
integer, optional, intent(out) :: rc
character(len=*), parameter :: Iam = MOD_NAME//'transpose_regrid_vector_3d_real32'
logical :: rotateBefore, rotateAfter
integer :: status
logical, parameter :: inputIsLL = .true.
type (RegridderSpec) :: spec
real (kind=REAL32), allocatable :: uvw_in(:,:,:)
real (kind=REAL32), allocatable :: uvw_out(:,:,:)
_ASSERT(size(u_in,3) == size(u_out,3))
_ASSERT(size(v_in,3) == size(v_out,3))
_ASSERT(size(u_in,3) == size(v_in,3))
RotateBefore = InputIsLL
RotateAfter = .not.InputIsLL
if (present(rotate)) then
if (rotate) then
RotateBefore = .true.
RotateAfter = .true.
end if
end if
spec = this%get_spec()
allocate(uvw_in(size(u_in,1),size(u_in,2),3*size(u_in,3)))
call SphericalToCartesian(this%transform, u_in , v_in , UVW_in , &
& transpose=.true., SphIsLL=InputIsLL, Rotate=RotateBefore, rc=status)
_VERIFY(status)
allocate(uvw_out(size(u_out,1),size(u_out,2),3*size(u_out,3)))
call this%transpose_regrid(uvw_in, uvw_out, rc=status)
_VERIFY(status)
deallocate(uvw_in)
call CartesianToSpherical(this%transform, uvw_out, u_out, v_out, &
transpose=.true., SphIsLL=.not.InputIsLL, Rotate=RotateAfter, rc=status)
_VERIFY(status)
deallocate(uvw_out)
_RETURN(_SUCCESS)
end subroutine transpose_regrid_vector_3d_real32
end module CubeToLatLonRegridderMod