1 | !> @file tests/test-interpolation.f90 |
---|
2 | !------------------------------------------------------------------------------! |
---|
3 | ! This file is part of PALM/PALM-4U. |
---|
4 | ! |
---|
5 | ! PALM/PALM-4U is free software: you can redistribute it and/or modify it under |
---|
6 | ! the terms of the GNU General Public License as published by the Free Software |
---|
7 | ! Foundation, either version 3 of the License, or (at your option) any later |
---|
8 | ! version. |
---|
9 | ! |
---|
10 | ! PALM/PALM-4U is distributed in the hope that it will be useful, but WITHOUT |
---|
11 | ! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
12 | ! FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
13 | ! |
---|
14 | ! You should have received a copy of the GNU General Public License along with |
---|
15 | ! PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | ! |
---|
17 | ! Copyright 2017-2017 Leibniz Universitaet Hannover, Deutscher Wetterdienst |
---|
18 | ! Offenbach |
---|
19 | !------------------------------------------------------------------------------! |
---|
20 | ! |
---|
21 | ! Current revisions: |
---|
22 | ! ----------------- |
---|
23 | ! |
---|
24 | ! |
---|
25 | ! Former revisions: |
---|
26 | ! ----------------- |
---|
27 | ! $Id: test-interpolation.f90 2696 2017-12-14 17:12:51Z knoop $ |
---|
28 | ! Initial revision |
---|
29 | ! |
---|
30 | ! |
---|
31 | ! |
---|
32 | ! Authors: |
---|
33 | ! -------- |
---|
34 | ! @author Eckhard Kadasch |
---|
35 | ! |
---|
36 | ! Description: |
---|
37 | ! ------------ |
---|
38 | !> This program tests INIFOR's horizontal interpolation. |
---|
39 | !------------------------------------------------------------------------------! |
---|
40 | PROGRAM test_interpolation |
---|
41 | |
---|
42 | USE grid, & |
---|
43 | ONLY: grid_definition, init_grid_definition, TO_RADIANS, TO_DEGREES, & |
---|
44 | linspace, hhl |
---|
45 | USE transform, & |
---|
46 | ONLY: find_horizontal_neighbours, compute_horizontal_interp_weights |
---|
47 | USE test_utils |
---|
48 | |
---|
49 | IMPLICIT NONE |
---|
50 | |
---|
51 | ! |
---|
52 | !------------------------------------------------------------------------------ |
---|
53 | !- Test 1: Find neighbours |
---|
54 | !------------------------------------------------------------------------------ |
---|
55 | CHARACTER(LEN=30) :: title = "find neighbours" |
---|
56 | LOGICAL :: res |
---|
57 | |
---|
58 | TYPE(grid_definition) :: palm_grid, cosmo_grid |
---|
59 | INTEGER :: i, j, ii_ref(0:1, 0:1, 4), jj_ref(0:1, 0:1, 4) |
---|
60 | INTEGER, PARAMETER :: nlon=3, nlat=3, nlev=2 |
---|
61 | REAL :: w_ref(4), lat(0:2), lon(0:2) |
---|
62 | |
---|
63 | title = "find neighbours" |
---|
64 | CALL begin_test(title, res) |
---|
65 | |
---|
66 | ! Arange. |
---|
67 | ! Make a COSMO-DE grid with just two horizotal cells/three h. points |
---|
68 | PRINT *, "INIT GRID" |
---|
69 | |
---|
70 | ! Allocate grid.hhl for use in init_grid_definition. In INIFOR, this is done |
---|
71 | ! in get_netcdf_variable_2d. In this test, grid.hhl is not used and only |
---|
72 | ! defined manually because it is used in init_grid_definition. |
---|
73 | ALLOCATE (hhl (nlon, nlat, nlev) ) |
---|
74 | hhl(:,:,:) = 0.0 |
---|
75 | CALL init_grid_definition('cosmo-de', grid = cosmo_grid, & |
---|
76 | xmin = -5.0 * TO_RADIANS, xmax = 5.5 * TO_RADIANS, & |
---|
77 | ymin = -5.0 * TO_RADIANS, ymax = 6.5 * TO_RADIANS, & |
---|
78 | zmin = 0.0, zmax = 10.0, & |
---|
79 | x0 = 0.0, y0 = 0.0, z0 = 0.0, & |
---|
80 | nx = nlon-1, ny = nlat-1, nz = nlev-1) |
---|
81 | |
---|
82 | PRINT *, "GRID DONE" |
---|
83 | PRINT *, "COSMO lats: ", cosmo_grid % lat * TO_DEGREES |
---|
84 | PRINT *, "COSMO lons: ", cosmo_grid % lon * TO_DEGREES |
---|
85 | |
---|
86 | res = assert_equal( (/cosmo_grid%lat(0), cosmo_grid % lon(0), & |
---|
87 | cosmo_grid%lat(2), cosmo_grid % lon(2), & |
---|
88 | cosmo_grid%dx*TO_DEGREES, cosmo_grid%dy*TO_DEGREES/),& |
---|
89 | (/-5.0 * TO_RADIANS, -5.0 * TO_RADIANS, & |
---|
90 | 6.5 * TO_RADIANS, 5.5 * TO_RADIANS, & |
---|
91 | 5.25, 5.75 /), & |
---|
92 | "COSMO grid coordinates" ) |
---|
93 | ! Define a PALM-4U grid with only one cell, i.e. four points in the |
---|
94 | ! horizontal plane. The points are located at the centres of |
---|
95 | ! the COSMO-DE cells. |
---|
96 | CALL init_grid_definition('palm intermediate', grid = palm_grid, & |
---|
97 | xmin = 0.0, xmax = 1.0, & |
---|
98 | ymin = 0.0, ymax = 1.0, & |
---|
99 | zmin = 0.0, zmax = 1.0, & |
---|
100 | x0 = 0.0, y0 = 0.0, z0 = 0.0, & |
---|
101 | nx = 1, ny = 1, nz = 1) |
---|
102 | |
---|
103 | palm_grid % clon(0,0) = 0.5 * cosmo_grid % lon(0) |
---|
104 | palm_grid % clat(0,0) = 0.5 * cosmo_grid % lat(0) |
---|
105 | |
---|
106 | palm_grid % clon(0,1) = 0.5 * cosmo_grid % lon(0) |
---|
107 | palm_grid % clat(0,1) = 0.5 * cosmo_grid % lat(2) |
---|
108 | |
---|
109 | palm_grid % clon(1,1) = 0.5 * cosmo_grid % lon(2) |
---|
110 | palm_grid % clat(1,1) = 0.5 * cosmo_grid % lat(2) |
---|
111 | |
---|
112 | palm_grid % clon(1,0) = 0.5 * cosmo_grid % lon(2) |
---|
113 | palm_grid % clat(1,0) = 0.5 * cosmo_grid % lat(0) |
---|
114 | |
---|
115 | ii_ref(0,0,:) = (/0, 0, 1, 1/) |
---|
116 | jj_ref(0,0,:) = (/0, 1, 1, 0/) |
---|
117 | |
---|
118 | ii_ref(0,1,:) = (/0, 0, 1, 1/) |
---|
119 | jj_ref(0,1,:) = (/1, 2, 2, 1/) |
---|
120 | |
---|
121 | ii_ref(1,1,:) = (/1, 1, 2, 2/) |
---|
122 | jj_ref(1,1,:) = (/1, 2, 2, 1/) |
---|
123 | |
---|
124 | ii_ref(1,0,:) = (/1, 1, 2, 2/) |
---|
125 | jj_ref(1,0,:) = (/0, 1, 1, 0/) |
---|
126 | |
---|
127 | ! Act |
---|
128 | CALL find_horizontal_neighbours(cosmo_grid % lat, cosmo_grid % lon, & |
---|
129 | cosmo_grid % dxi, cosmo_grid % dyi, palm_grid % clat, palm_grid % clon, & |
---|
130 | palm_grid % ii, palm_grid % jj) |
---|
131 | |
---|
132 | ! Assert |
---|
133 | DO j = 0, 1 |
---|
134 | DO i = 0, 1 |
---|
135 | res = res .AND. ALL(palm_grid%ii(i,j,:) == ii_ref(i,j,:)) |
---|
136 | PRINT *, "ii : ", palm_grid%ii(i,j,:) |
---|
137 | PRINT *, "ii_ref : ", ii_ref(i,j,:), " indices match? ", res |
---|
138 | res = res .AND. ALL(palm_grid%jj(i,j,:) == jj_ref(i,j,:)) |
---|
139 | PRINT *, "jj : ", palm_grid%jj(i,j,:) |
---|
140 | PRINT *, "jj_ref : ", jj_ref(i,j,:), " indices match? ", res |
---|
141 | END DO |
---|
142 | END DO |
---|
143 | |
---|
144 | CALL end_test(title, res) |
---|
145 | |
---|
146 | |
---|
147 | ! |
---|
148 | !------------------------------------------------------------------------------ |
---|
149 | !- Test 2: Compute weights for linear interpolation |
---|
150 | !------------------------------------------------------------------------------ |
---|
151 | title = "interpolation weights" |
---|
152 | CALL begin_test(title, res) |
---|
153 | |
---|
154 | ! Arange |
---|
155 | ! defining some shorthands |
---|
156 | lon(:) = cosmo_grid % lon(:) |
---|
157 | lat(:) = cosmo_grid % lat(:) |
---|
158 | |
---|
159 | ! set up PALM-4U points at 1/4 and 1/3 of the COSMO grid widths |
---|
160 | palm_grid % clon(0,0) = -0.25 * (lon(1) - lon(0)) + lon(1) |
---|
161 | palm_grid % clat(0,0) = -2./3. * (lat(1) - lat(0)) + lat(1) |
---|
162 | |
---|
163 | palm_grid % clon(0,1) = -2./3. * (lon(1) - lon(0)) + lon(1) |
---|
164 | palm_grid % clat(0,1) = +0.25 * (lat(2) - lat(1)) + lat(1) |
---|
165 | |
---|
166 | palm_grid % clon(1,1) = +0.25 * (lon(2) - lon(1)) + lon(1) |
---|
167 | palm_grid % clat(1,1) = +2./3. * (lat(2) - lat(1)) + lat(1) |
---|
168 | |
---|
169 | palm_grid % clon(1,0) = +2./3. * (lon(2) - lon(1)) + lon(1) |
---|
170 | palm_grid % clat(1,0) = -0.25 * (lat(1) - lat(0)) + lat(1) |
---|
171 | |
---|
172 | DO j = 0, 1 |
---|
173 | DO i = 0, 1 |
---|
174 | PRINT *, "PALM lon, lat: ", palm_grid % clon(i,j) * TO_DEGREES, palm_grid % clat(i,j)*TO_DEGREES |
---|
175 | END DO |
---|
176 | END DO |
---|
177 | |
---|
178 | ! Act |
---|
179 | CALL find_horizontal_neighbours(cosmo_grid % lat, cosmo_grid % lon, & |
---|
180 | cosmo_grid % dxi, cosmo_grid % dyi, palm_grid % clat, palm_grid % clon, & |
---|
181 | palm_grid % ii, palm_grid % jj) |
---|
182 | |
---|
183 | CALL compute_horizontal_interp_weights(cosmo_grid % lat, cosmo_grid % lon, & |
---|
184 | cosmo_grid % dxi, cosmo_grid % dyi, palm_grid % clat, & |
---|
185 | palm_grid % clon, palm_grid % ii, palm_grid % jj, palm_grid % w_horiz) |
---|
186 | |
---|
187 | ! Assert |
---|
188 | ! asserting that neighbours are still correct |
---|
189 | DO j = 0, 1 |
---|
190 | DO i = 0, 1 |
---|
191 | res = res .AND. ALL(palm_grid%ii(i,j,:) == ii_ref(i,j,:)) |
---|
192 | PRINT *, "ii : ", palm_grid%ii(i,j,:) |
---|
193 | PRINT *, "ii_ref : ", ii_ref(i,j,:), " indices match? ", res |
---|
194 | res = res .AND. ALL(palm_grid%jj(i,j,:) == jj_ref(i,j,:)) |
---|
195 | PRINT *, "jj : ", palm_grid%jj(i,j,:) |
---|
196 | PRINT *, "jj_ref : ", jj_ref(i,j,:), " indices match? ", res |
---|
197 | END DO |
---|
198 | END DO |
---|
199 | |
---|
200 | ! asserting that all four weights equal, 0.5, 0.25, 1./6., and 1./12., resp. |
---|
201 | w_ref = (/1./6., 1./12., 0.25, 0.5/) |
---|
202 | res = res .AND. assert_equal(palm_grid % w_horiz(0, 0, :), w_ref(:), "weights at (0,0)") |
---|
203 | !res = res .AND. palm_grid % w_horiz(0, 0, 1) == w_ref(1) |
---|
204 | !res = res .AND. palm_grid % w_horiz(0, 0, 2) == w_ref(2) |
---|
205 | !res = res .AND. palm_grid % w_horiz(0, 0, 3) == w_ref(3) |
---|
206 | !res = res .AND. palm_grid % w_horiz(0, 0, 4) == w_ref(4) |
---|
207 | |
---|
208 | w_ref = (/0.5, 1./6., 1./12., 0.25/) |
---|
209 | res = res .AND. assert_equal(palm_grid % w_horiz(0, 1, :), w_ref(:), "weights at (0,1)") |
---|
210 | !res = res .AND. palm_grid % w_horiz(0, 1, 1) == w_ref(4) |
---|
211 | !res = res .AND. palm_grid % w_horiz(0, 1, 2) == w_ref(1) |
---|
212 | !res = res .AND. palm_grid % w_horiz(0, 1, 3) == w_ref(2) |
---|
213 | !res = res .AND. palm_grid % w_horiz(0, 1, 4) == w_ref(3) |
---|
214 | |
---|
215 | w_ref = (/0.25, 0.5, 1./6., 1./12./) |
---|
216 | res = res .AND. assert_equal(palm_grid % w_horiz(1, 1, :), w_ref(:), "weights at (1,1)") |
---|
217 | !res = res .AND. palm_grid % w_horiz(1, 1, 1) == w_ref(3) |
---|
218 | !res = res .AND. palm_grid % w_horiz(1, 1, 2) == w_ref(4) |
---|
219 | !res = res .AND. palm_grid % w_horiz(1, 1, 3) == w_ref(1) |
---|
220 | !res = res .AND. palm_grid % w_horiz(1, 1, 4) == w_ref(2) |
---|
221 | |
---|
222 | w_ref = (/1./12., 0.25, 0.5, 1./6./) |
---|
223 | res = res .AND. assert_equal(palm_grid % w_horiz(1, 0, :), w_ref(:), "weights at (1,0)") |
---|
224 | !res = res .AND. palm_grid % w_horiz(1, 0, 1) == w_ref(2) |
---|
225 | !res = res .AND. palm_grid % w_horiz(1, 0, 2) == w_ref(3) |
---|
226 | !res = res .AND. palm_grid % w_horiz(1, 0, 3) == w_ref(4) |
---|
227 | !res = res .AND. palm_grid % w_horiz(1, 0, 4) == w_ref(1) |
---|
228 | |
---|
229 | CALL end_test(title, res) |
---|
230 | |
---|
231 | END PROGRAM test_interpolation |
---|