1 | !> @file src/inifor_util.f90 |
---|
2 | !------------------------------------------------------------------------------! |
---|
3 | ! This file is part of the PALM model system. |
---|
4 | ! |
---|
5 | ! PALM is free software: you can redistribute it and/or modify it under the |
---|
6 | ! 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 is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
12 | ! 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-2020 Leibniz Universitaet Hannover |
---|
18 | ! Copyright 2017-2020 Deutscher Wetterdienst Offenbach |
---|
19 | !------------------------------------------------------------------------------! |
---|
20 | ! |
---|
21 | ! Current revisions: |
---|
22 | ! ----------------- |
---|
23 | ! |
---|
24 | ! |
---|
25 | ! Former revisions: |
---|
26 | ! ----------------- |
---|
27 | ! $Id: inifor_util.f90 4523 2020-05-07 15:58:16Z eckhard $ |
---|
28 | ! respect integer working precision (iwp) specified in inifor_defs.f90 |
---|
29 | ! |
---|
30 | ! |
---|
31 | ! 4499 2020-04-16 15:51:56Z eckhard |
---|
32 | ! Bugfix: avoid using already overwritten elements in 'reverse' subroutine |
---|
33 | ! |
---|
34 | ! |
---|
35 | ! 4481 2020-03-31 18:55:54Z maronga |
---|
36 | ! Bugfix: use explicit loop in 'reverse' subroutine instead of implicit loop |
---|
37 | ! |
---|
38 | ! 3866 2019-04-05 14:25:01Z eckhard |
---|
39 | ! Use PALM's working precision |
---|
40 | ! Improved coding style |
---|
41 | ! |
---|
42 | ! |
---|
43 | ! 3785 2019-03-06 10:41:14Z eckhard |
---|
44 | ! Prefixed all INIFOR modules with inifor_ |
---|
45 | ! |
---|
46 | ! |
---|
47 | ! 3557 2018-11-22 16:01:22Z eckhard |
---|
48 | ! Updated documentation |
---|
49 | ! |
---|
50 | ! |
---|
51 | ! 3447 2018-10-29 15:52:54Z eckhard |
---|
52 | ! Renamed source files for compatibilty with PALM build system |
---|
53 | ! |
---|
54 | ! |
---|
55 | ! 3395 2018-10-22 17:32:49Z eckhard |
---|
56 | ! New routines for computing potential temperature and moist air density |
---|
57 | ! Increased number of digits in real-to-str conversion |
---|
58 | ! |
---|
59 | ! 3183 2018-07-27 14:25:55Z suehring |
---|
60 | ! Improved real-to-string conversion |
---|
61 | ! |
---|
62 | ! |
---|
63 | ! 3182 2018-07-27 13:36:03Z suehring |
---|
64 | ! Initial revision |
---|
65 | ! |
---|
66 | ! |
---|
67 | ! |
---|
68 | ! Authors: |
---|
69 | ! -------- |
---|
70 | !> @author Eckhard Kadasch (Deutscher Wetterdienst, Offenbach) |
---|
71 | ! |
---|
72 | ! Description: |
---|
73 | ! ------------ |
---|
74 | !> The util module provides miscellaneous utility routines for INIFOR. |
---|
75 | !------------------------------------------------------------------------------! |
---|
76 | MODULE inifor_util |
---|
77 | |
---|
78 | USE inifor_defs, & |
---|
79 | ONLY : PI, DATE, SNAME, iwp, wp |
---|
80 | USE inifor_types, & |
---|
81 | ONLY : grid_definition |
---|
82 | USE, INTRINSIC :: ISO_C_BINDING, & |
---|
83 | ONLY : C_CHAR, C_INT, C_PTR, C_SIZE_T |
---|
84 | |
---|
85 | IMPLICIT NONE |
---|
86 | |
---|
87 | !------------------------------------------------------------------------------! |
---|
88 | ! Description: |
---|
89 | ! ------------ |
---|
90 | !> Fortran implementation of C's struct tm for representing points in time |
---|
91 | !------------------------------------------------------------------------------! |
---|
92 | TYPE, BIND(c) :: tm_struct |
---|
93 | INTEGER(C_INT) :: tm_sec !< seconds after the minute [0, 61] |
---|
94 | INTEGER(C_INT) :: tm_min !< minutes after the hour [0, 59] |
---|
95 | INTEGER(C_INT) :: tm_hour !< hours since midnight [0, 23] |
---|
96 | INTEGER(C_INT) :: tm_mday !< day of the month [1, 31] |
---|
97 | INTEGER(C_INT) :: tm_mon !< month since January [0, 11] |
---|
98 | INTEGER(C_INT) :: tm_year !< years since 1900 |
---|
99 | INTEGER(C_INT) :: tm_wday !< days since Sunday [0, 6] |
---|
100 | INTEGER(C_INT) :: tm_yday !< days since January 1st [0, 356] |
---|
101 | INTEGER(C_INT) :: tm_isdst !< Daylight Saving Time flag |
---|
102 | END TYPE |
---|
103 | |
---|
104 | INTERFACE |
---|
105 | |
---|
106 | !------------------------------------------------------------------------------! |
---|
107 | ! Description: |
---|
108 | ! ------------ |
---|
109 | !> Interface to C's strptime function, which converts a string to a tm |
---|
110 | !> structure. |
---|
111 | !------------------------------------------------------------------------------! |
---|
112 | FUNCTION strptime(string, format, timeinfo) BIND(c, NAME='strptime') |
---|
113 | IMPORT :: C_CHAR, C_SIZE_T, tm_struct |
---|
114 | |
---|
115 | IMPLICIT NONE |
---|
116 | |
---|
117 | CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(IN) :: string, format |
---|
118 | TYPE(tm_struct), INTENT(OUT) :: timeinfo |
---|
119 | |
---|
120 | INTEGER(C_SIZE_T) :: strptime |
---|
121 | END FUNCTION |
---|
122 | |
---|
123 | |
---|
124 | !------------------------------------------------------------------------------! |
---|
125 | ! Description: |
---|
126 | ! ------------ |
---|
127 | !> Interface to C's strftime function, which converts the given 'timeinfo' |
---|
128 | !> structure to a string in the given 'format'. |
---|
129 | !------------------------------------------------------------------------------! |
---|
130 | FUNCTION strftime(string, string_len, format, timeinfo) BIND(c, NAME='strftime') |
---|
131 | IMPORT :: C_CHAR, C_SIZE_T, tm_struct |
---|
132 | |
---|
133 | IMPLICIT NONE |
---|
134 | |
---|
135 | CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(OUT) :: string |
---|
136 | CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(IN) :: format |
---|
137 | INTEGER(C_SIZE_T), INTENT(IN) :: string_len |
---|
138 | TYPE(tm_struct), INTENT(IN) :: timeinfo |
---|
139 | |
---|
140 | INTEGER(C_SIZE_T) :: strftime |
---|
141 | END FUNCTION |
---|
142 | |
---|
143 | |
---|
144 | !------------------------------------------------------------------------------! |
---|
145 | ! Description: |
---|
146 | ! ------------ |
---|
147 | !> Interface to C's mktime function, which converts the given tm structure to |
---|
148 | !> Unix time (number of seconds since 0 UTC, 1st January 1970). INIFOR uses the |
---|
149 | !> side effect that a call to mktime noramlizes the given 'timeinfo' structure, |
---|
150 | !> e.g. increments the date if hours overfow 24. |
---|
151 | !------------------------------------------------------------------------------! |
---|
152 | FUNCTION mktime(timeinfo) BIND(c, NAME='mktime') |
---|
153 | IMPORT :: C_PTR, tm_struct |
---|
154 | |
---|
155 | IMPLICIT NONE |
---|
156 | |
---|
157 | TYPE(tm_struct), INTENT(IN) :: timeinfo |
---|
158 | |
---|
159 | TYPE(C_PTR) :: mktime |
---|
160 | END FUNCTION |
---|
161 | |
---|
162 | END INTERFACE |
---|
163 | |
---|
164 | CONTAINS |
---|
165 | |
---|
166 | !------------------------------------------------------------------------------! |
---|
167 | ! Description: |
---|
168 | ! ------------ |
---|
169 | !> Takes a string of the form YYYYMMDDHH, adds the given number of hours to its |
---|
170 | !> tm structure representation, and returns the corresponding string in the same |
---|
171 | !> format |
---|
172 | !------------------------------------------------------------------------------! |
---|
173 | CHARACTER(LEN=DATE) FUNCTION add_hours_to(date_string, hours) |
---|
174 | CHARACTER(LEN=DATE), INTENT(IN) :: date_string |
---|
175 | INTEGER(iwp), INTENT(IN) :: hours |
---|
176 | |
---|
177 | CHARACTER(KIND=C_CHAR, LEN=*), PARAMETER :: format_string = "%Y%m%d%H" |
---|
178 | CHARACTER(KIND=C_CHAR, LEN=DATE) :: c_date_string |
---|
179 | TYPE(C_PTR) :: c_pointer |
---|
180 | TYPE(tm_struct) :: time_info |
---|
181 | INTEGER(iwp) :: err |
---|
182 | |
---|
183 | c_date_string = date_string |
---|
184 | |
---|
185 | ! Convert C string to C tm struct |
---|
186 | CALL init_tm(time_info) |
---|
187 | err = strptime(c_date_string, format_string, time_info) |
---|
188 | |
---|
189 | ! Manipulate and normalize C tm struct |
---|
190 | time_info%tm_hour = time_info%tm_hour + hours |
---|
191 | c_pointer = mktime(time_info) |
---|
192 | |
---|
193 | ! Convert back to C string |
---|
194 | err = strftime(c_date_string, INT(DATE, KIND=C_SIZE_T), & |
---|
195 | format_string, time_info) |
---|
196 | |
---|
197 | add_hours_to = c_date_string |
---|
198 | END FUNCTION |
---|
199 | |
---|
200 | |
---|
201 | !------------------------------------------------------------------------------! |
---|
202 | ! Description: |
---|
203 | ! ------------ |
---|
204 | !> Print all members of the given tm structure |
---|
205 | !------------------------------------------------------------------------------! |
---|
206 | SUBROUTINE print_tm(timeinfo) |
---|
207 | TYPE(tm_struct), INTENT(IN) :: timeinfo |
---|
208 | |
---|
209 | PRINT *, "sec: ", timeinfo%tm_sec, & !< seconds after the minute [0, 61] |
---|
210 | "min: ", timeinfo%tm_min, & !< minutes after the hour [0, 59] |
---|
211 | "hr: ", timeinfo%tm_hour, & !< hours since midnight [0, 23] |
---|
212 | "day: ", timeinfo%tm_mday, & !< day of the month [1, 31] |
---|
213 | "mon: ", timeinfo%tm_mon, & !< month since January [0, 11] |
---|
214 | "yr: ", timeinfo%tm_year, & !< years since 1900 |
---|
215 | "wday:", timeinfo%tm_wday, & !< days since Sunday [0, 6] |
---|
216 | "yday:", timeinfo%tm_yday, & !< days since January 1st [0, 356] |
---|
217 | "dst: ", timeinfo%tm_isdst !< Daylight Saving time flag |
---|
218 | END SUBROUTINE print_tm |
---|
219 | |
---|
220 | |
---|
221 | !------------------------------------------------------------------------------! |
---|
222 | ! Description: |
---|
223 | ! ------------ |
---|
224 | !> Initialize the given tm structure with zero values |
---|
225 | !------------------------------------------------------------------------------! |
---|
226 | SUBROUTINE init_tm(timeinfo) |
---|
227 | TYPE(tm_struct), INTENT(INOUT) :: timeinfo |
---|
228 | |
---|
229 | timeinfo%tm_sec = 0 |
---|
230 | timeinfo%tm_min = 0 |
---|
231 | timeinfo%tm_hour = 0 |
---|
232 | timeinfo%tm_mday = 0 |
---|
233 | timeinfo%tm_mon = 0 |
---|
234 | timeinfo%tm_year = 0 |
---|
235 | timeinfo%tm_wday = 0 |
---|
236 | timeinfo%tm_yday = 0 |
---|
237 | |
---|
238 | ! We use UTC times, so marking Daylight Saving Time (DST) 'not available' |
---|
239 | ! (< 0). If this is set to 0, mktime will convert the timeinfo to DST and |
---|
240 | ! add one hour. |
---|
241 | timeinfo%tm_isdst = -1 |
---|
242 | END SUBROUTINE init_tm |
---|
243 | |
---|
244 | |
---|
245 | !------------------------------------------------------------------------------! |
---|
246 | ! Description: |
---|
247 | ! ------------ |
---|
248 | !> Fill the given array with values equally spaced between and including start |
---|
249 | !> and stop |
---|
250 | !------------------------------------------------------------------------------! |
---|
251 | SUBROUTINE linspace(start, stop, array) |
---|
252 | |
---|
253 | REAL(wp), INTENT(IN) :: start, stop |
---|
254 | REAL(wp), INTENT(INOUT) :: array(0:) |
---|
255 | INTEGER(iwp) :: i, n |
---|
256 | |
---|
257 | n = UBOUND(array, 1) |
---|
258 | |
---|
259 | IF (n .EQ. 0) THEN |
---|
260 | |
---|
261 | array(0) = start |
---|
262 | |
---|
263 | ELSE |
---|
264 | |
---|
265 | DO i = 0, n |
---|
266 | array(i) = start + REAL(i, wp) / n * (stop - start) |
---|
267 | ENDDO |
---|
268 | |
---|
269 | ENDIF |
---|
270 | |
---|
271 | END SUBROUTINE linspace |
---|
272 | |
---|
273 | |
---|
274 | !------------------------------------------------------------------------------! |
---|
275 | ! Description: |
---|
276 | ! ------------ |
---|
277 | !> Reverse the order of the third (vertical) array dimension from top-down |
---|
278 | !> (COSMO) to bottom-up (PALM) |
---|
279 | !------------------------------------------------------------------------------! |
---|
280 | SUBROUTINE reverse(input_arr) |
---|
281 | |
---|
282 | INTEGER(iwp) :: idx, opposite_idx, half_idx |
---|
283 | INTEGER(iwp) :: size_1st_dimension |
---|
284 | INTEGER(iwp) :: size_2nd_dimension |
---|
285 | INTEGER(iwp) :: size_3rd_dimension |
---|
286 | INTEGER(iwp) :: lbound_3rd_dimension |
---|
287 | INTEGER(iwp) :: ubound_3rd_dimension |
---|
288 | |
---|
289 | REAL(wp), INTENT(INOUT) :: input_arr(:,:,:) |
---|
290 | REAL(wp), ALLOCATABLE :: buffer_arr(:,:) |
---|
291 | |
---|
292 | lbound_3rd_dimension = LBOUND(input_arr, 3) |
---|
293 | ubound_3rd_dimension = UBOUND(input_arr, 3) |
---|
294 | size_1st_dimension = SIZE(input_arr, 1) |
---|
295 | size_2nd_dimension = SIZE(input_arr, 2) |
---|
296 | size_3rd_dimension = SIZE(input_arr, 3) |
---|
297 | half_idx = lbound_3rd_dimension + size_3rd_dimension / 2 - 1 |
---|
298 | |
---|
299 | ALLOCATE( buffer_arr(size_1st_dimension, size_2nd_dimension) ) |
---|
300 | |
---|
301 | DO idx = lbound_3rd_dimension, half_idx |
---|
302 | opposite_idx = ubound_3rd_dimension - ( idx - lbound_3rd_dimension ) |
---|
303 | buffer_arr(:,:) = input_arr(:,:,idx) |
---|
304 | input_arr(:,:,idx) = input_arr(:,:,opposite_idx) |
---|
305 | input_arr(:,:,opposite_idx) = buffer_arr(:,:) |
---|
306 | ENDDO |
---|
307 | |
---|
308 | DEALLOCATE( buffer_arr ) |
---|
309 | |
---|
310 | END SUBROUTINE reverse |
---|
311 | |
---|
312 | |
---|
313 | !------------------------------------------------------------------------------! |
---|
314 | ! Description: |
---|
315 | ! ------------ |
---|
316 | !> |
---|
317 | !------------------------------------------------------------------------------! |
---|
318 | SUBROUTINE deaverage(avg_1, t1, avg_2, t2, avg_3, t3) |
---|
319 | |
---|
320 | REAL(wp), DIMENSION(:,:,:), INTENT(IN) :: avg_1, avg_2 |
---|
321 | REAL(wp), INTENT(IN) :: t1, t2, t3 |
---|
322 | REAL(wp), DIMENSION(:,:,:), INTENT(OUT) :: avg_3 |
---|
323 | |
---|
324 | REAL(wp) :: ti |
---|
325 | |
---|
326 | ti = 1.0_wp / t3 |
---|
327 | |
---|
328 | avg_3(:,:,:) = ti * ( t2 * avg_2(:,:,:) - t1 * avg_1(:,:,:) ) |
---|
329 | |
---|
330 | END SUBROUTINE deaverage |
---|
331 | |
---|
332 | |
---|
333 | !------------------------------------------------------------------------------! |
---|
334 | ! Description: |
---|
335 | ! ------------ |
---|
336 | !> Compute the COSMO-DE/-D2 basic state pressure profile |
---|
337 | !------------------------------------------------------------------------------! |
---|
338 | SUBROUTINE get_basic_state(z, beta, p_sl, t_sl, rd, g, p0) |
---|
339 | |
---|
340 | REAL(wp), INTENT(IN) :: z(1:) !< height [m] |
---|
341 | REAL(wp), INTENT(IN) :: beta !< logarithmic lapse rate, dT / d ln(p) [K] |
---|
342 | REAL(wp), INTENT(IN) :: p_sl !< reference pressure [Pa] |
---|
343 | REAL(wp), INTENT(IN) :: t_sl !< reference tempereature [K] |
---|
344 | REAL(wp), INTENT(IN) :: rd !< ideal gas constant of dry air [J/kg/K] |
---|
345 | REAL(wp), INTENT(IN) :: g !< acceleration of Earth's gravity [m/s^2] |
---|
346 | REAL(wp), INTENT(OUT) :: p0(1:) !< COSMO basic state pressure [Pa] |
---|
347 | REAL(wp) :: root_frac, factor !< precomputed factors |
---|
348 | |
---|
349 | factor = - t_sl / beta |
---|
350 | root_frac = (2.0_wp * beta * g) / (rd * t_sl*t_sl) |
---|
351 | |
---|
352 | p0(:) = p_sl * EXP( & |
---|
353 | factor * ( 1.0_wp - SQRT( 1.0_wp - root_frac * z(:) ) ) & |
---|
354 | ) |
---|
355 | |
---|
356 | END SUBROUTINE get_basic_state |
---|
357 | |
---|
358 | |
---|
359 | !------------------------------------------------------------------------------! |
---|
360 | ! Description: |
---|
361 | ! ------------ |
---|
362 | !> Converts the absolute temperature to the potential temperature in place using |
---|
363 | !> the identity a^b = e^(b ln(a)). |
---|
364 | !> |
---|
365 | !> theta = T * (p_ref/p)^(R/c_p) = T * e^( R/c_p * ln(p_ref/p) ) |
---|
366 | !------------------------------------------------------------------------------! |
---|
367 | SUBROUTINE potential_temperature(t, p, p_ref, r, cp) |
---|
368 | REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) :: t |
---|
369 | REAL(wp), DIMENSION(:,:,:), INTENT(IN) :: p |
---|
370 | REAL(wp), INTENT(IN) :: p_ref, r, cp |
---|
371 | REAL(wp) :: rcp |
---|
372 | |
---|
373 | rcp = r/cp |
---|
374 | t(:,:,:) = t(:,:,:) * EXP( rcp * LOG(p_ref / p(:,:,:)) ) |
---|
375 | |
---|
376 | END SUBROUTINE potential_temperature |
---|
377 | |
---|
378 | |
---|
379 | !------------------------------------------------------------------------------! |
---|
380 | ! Description: |
---|
381 | ! ------------ |
---|
382 | !> Compute the density in place of the given temperature (t_rho). |
---|
383 | !------------------------------------------------------------------------------! |
---|
384 | SUBROUTINE moist_density(t_rho, p, qv, rd, rv) |
---|
385 | REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) :: t_rho |
---|
386 | REAL(wp), DIMENSION(:,:,:), INTENT(IN) :: p, qv |
---|
387 | REAL(wp), INTENT(IN) :: rd, rv |
---|
388 | |
---|
389 | t_rho(:,:,:) = p(:,:,:) / ( & |
---|
390 | (rv * qv(:,:,:) + rd * (1.0_wp - qv(:,:,:))) * t_rho(:,:,:) & |
---|
391 | ) |
---|
392 | |
---|
393 | END SUBROUTINE moist_density |
---|
394 | |
---|
395 | !------------------------------------------------------------------------------! |
---|
396 | ! Description: |
---|
397 | ! ------------ |
---|
398 | ! Convert a real number to a string in scientific notation showing four |
---|
399 | ! significant digits. |
---|
400 | !------------------------------------------------------------------------------! |
---|
401 | CHARACTER(LEN=SNAME) FUNCTION real_to_str(val, format) |
---|
402 | |
---|
403 | REAL(wp), INTENT(IN) :: val |
---|
404 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: format |
---|
405 | |
---|
406 | IF (PRESENT( format ) ) THEN |
---|
407 | WRITE( real_to_str, format ) val |
---|
408 | ELSE |
---|
409 | WRITE( real_to_str, '(E11.4)' ) val |
---|
410 | ENDIF |
---|
411 | real_to_str = ADJUSTL( real_to_str ) |
---|
412 | |
---|
413 | END FUNCTION real_to_str |
---|
414 | |
---|
415 | |
---|
416 | !------------------------------------------------------------------------------! |
---|
417 | ! Description: |
---|
418 | ! ------------ |
---|
419 | !> Converts the given real value to a string |
---|
420 | !------------------------------------------------------------------------------! |
---|
421 | CHARACTER(LEN=16) FUNCTION real_to_str_f(val) |
---|
422 | |
---|
423 | REAL(wp), INTENT(IN) :: val |
---|
424 | |
---|
425 | WRITE(real_to_str_f, '(F16.8)') val |
---|
426 | real_to_str_f = ADJUSTL(real_to_str_f) |
---|
427 | |
---|
428 | END FUNCTION real_to_str_f |
---|
429 | |
---|
430 | |
---|
431 | !------------------------------------------------------------------------------! |
---|
432 | ! Description: |
---|
433 | ! ------------ |
---|
434 | !> Converts the given integer value to a string |
---|
435 | !------------------------------------------------------------------------------! |
---|
436 | CHARACTER(LEN=10) FUNCTION str(val) |
---|
437 | |
---|
438 | INTEGER(iwp), INTENT(IN) :: val |
---|
439 | |
---|
440 | WRITE(str, '(i10)') val |
---|
441 | str = ADJUSTL(str) |
---|
442 | |
---|
443 | END FUNCTION str |
---|
444 | |
---|
445 | |
---|
446 | !------------------------------------------------------------------------------! |
---|
447 | ! Description: |
---|
448 | ! ------------ |
---|
449 | !> If the given path is not conlcuded by a slash, add one. |
---|
450 | !------------------------------------------------------------------------------! |
---|
451 | SUBROUTINE normalize_path(path) |
---|
452 | |
---|
453 | CHARACTER(LEN=*), INTENT(INOUT) :: path |
---|
454 | INTEGER(iwp) :: n |
---|
455 | |
---|
456 | n = LEN_TRIM(path) |
---|
457 | |
---|
458 | IF (path(n:n) .NE. '/') THEN |
---|
459 | path = TRIM(path) // '/' |
---|
460 | ENDIF |
---|
461 | |
---|
462 | END SUBROUTINE |
---|
463 | |
---|
464 | END MODULE inifor_util |
---|