source: palm/trunk/UTIL/inifor/src/inifor_util.f90 @ 3678

Last change on this file since 3678 was 3618, checked in by eckhard, 5 years ago

inifor: Prefixed all INIFOR modules with inifor_ and removed unused variables

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