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