FV3-JEDI
fv3jedi_io_utils_mod.f90
Go to the documentation of this file.
1 ! (C) Copyright 2020 UCAR
2 !
3 ! This software is licensed under the terms of the Apache Licence Version 2.0
4 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 
7 
8 ! iso
9 use iso_c_binding
10 
11 ! fckit
12 use fckit_configuration_module, only: fckit_configuration
13 
14 ! oops
15 use datetime_mod
16 use string_utils, only: swap_name_member
17 
18 implicit none
19 public
20 
21 integer, parameter :: maxstring = 2048
22 
23 ! --------------------------------------------------------------------------------------------------
24 
25 contains
26 
27 ! --------------------------------------------------------------------------------------------------
28 
29 subroutine str_check(str,maxlen)
30 
31 implicit none
32 character(len=*), intent(in) :: str
33 integer, intent(in) :: maxlen
34 
35 character(len=maxlen) :: maxlenstr
36 
37 write (maxlenstr, *) maxlen
38 
39 if (len(str) > maxstring) then
40  call abor1_ftn('Reading '//trim(str)//'from configuration. Too long, max length = '//trim(maxlenstr))
41 endif
42 
43 end subroutine str_check
44 
45 ! --------------------------------------------------------------------------------------------------
46 
47 subroutine vdate_to_datestring(vdate,datest,date,yyyy,mm,dd,hh,min,ss)
48 
49 implicit none
50 type(datetime), intent(in) :: vdate
51 character(len=*), optional, intent(out) :: datest
52 integer, optional, intent(out) :: date(6)
53 character(len=4), optional, intent(out) :: yyyy
54 character(len=2), optional, intent(out) :: mm
55 character(len=2), optional, intent(out) :: dd
56 character(len=2), optional, intent(out) :: hh
57 character(len=2), optional, intent(out) :: min
58 character(len=2), optional, intent(out) :: ss
59 
60 integer :: dateloc(6)
61 integer(kind=c_int) :: idate, isecs
62 
63 ! Outputs various forms of datetime
64 
65 call datetime_to_ifs(vdate, idate, isecs)
66 dateloc(1) = idate/10000
67 dateloc(2) = idate/100 - dateloc(1)*100
68 dateloc(3) = idate - (dateloc(1)*10000 + dateloc(2)*100)
69 dateloc(4) = isecs/3600
70 dateloc(5) = (isecs - dateloc(4)*3600)/60
71 dateloc(6) = isecs - (dateloc(4)*3600 + dateloc(5)*60)
72 
73 if (present(datest)) &
74 write(datest,'(I4,I0.2,I0.2,A1,I0.2,I0.2,I0.2)') dateloc(1),dateloc(2),dateloc(3),"_",&
75  dateloc(4),dateloc(5),dateloc(6)
76 
77 !Optionally pass date back
78 if (present(date)) date = dateloc
79 
80 ! Optionally pass back individual strings of datetime
81 if (present(yyyy)) write(yyyy,'(I4) ') dateloc(1)
82 if (present(mm )) write(mm ,'(I0.2)') dateloc(2)
83 if (present(dd )) write(dd ,'(I0.2)') dateloc(3)
84 if (present(hh )) write(hh ,'(I0.2)') dateloc(4)
85 if (present(min )) write(min ,'(I0.2)') dateloc(5)
86 if (present(ss )) write(ss ,'(I0.2)') dateloc(6)
87 
88 end subroutine vdate_to_datestring
89 
90 ! --------------------------------------------------------------------------------------------------
91 
92 function replace_text (inputstr,search,replace) result(outputstr)
93 
94 implicit none
95 character(len=*), intent(in) :: inputstr
96 character(len=*), intent(in) :: search
97 character(len=*), intent(in) :: replace
98 character(len(inputstr)+100) :: outputstr
99 
100 ! Locals
101 integer :: i, nt, nr
102 
103 outputstr = inputstr
104 nt = len_trim(search)
105 nr = len_trim(replace)
106 
107 do
108  i = index(outputstr,search(:nt)) ; if (i == 0) exit
109  outputstr = outputstr(:i-1) // replace(:nr) // outputstr(i+nt:)
110 end do
111 
112 end function replace_text
113 
114 ! --------------------------------------------------------------------------------------------------
115 
116 subroutine string_from_conf(f_conf,varstring,var,default,memberswap)
117 
118 implicit none
119 type(fckit_configuration), intent(in) :: f_conf
120 character(len=*), intent(in) :: varstring
121 character(len=*), intent(out) :: var
122 character(len=*), optional, intent(in) :: default
123 logical, optional, intent(in) :: memberswap
124 
125 character(len=:), allocatable :: str
126 
127 if (.not. f_conf%get(trim(varstring),str)) then
128 
129  if (present(default)) then
130  var = trim(default)
131  else
132  call abor1_ftn("fv3jedi_io_utils_mod.string_from_conf: "//trim(varstring)// &
133  " not found in config and no default provided. Aborting")
134  endif
135 
136 else
137 
138  if (present(memberswap) .and. memberswap) call swap_name_member(f_conf, str)
139 
140  var = trim(str)
141 
142 endif
143 
144 if (allocated(str)) deallocate(str)
145 
146 end subroutine string_from_conf
147 
148 ! --------------------------------------------------------------------------------------------------
149 
150 end module fv3jedi_io_utils_mod
151 
fv3jedi_io_utils_mod::replace_text
character(len(inputstr)+100) function replace_text(inputstr, search, replace)
Definition: fv3jedi_io_utils_mod.f90:93
fv3jedi_io_utils_mod::str_check
subroutine str_check(str, maxlen)
Definition: fv3jedi_io_utils_mod.f90:30
fv3jedi_io_utils_mod
Definition: fv3jedi_io_utils_mod.f90:6
fv3jedi_io_utils_mod::maxstring
integer, parameter maxstring
Definition: fv3jedi_io_utils_mod.f90:21
fv3jedi_io_utils_mod::vdate_to_datestring
subroutine vdate_to_datestring(vdate, datest, date, yyyy, mm, dd, hh, min, ss)
Definition: fv3jedi_io_utils_mod.f90:48
fv3jedi_io_utils_mod::string_from_conf
subroutine string_from_conf(f_conf, varstring, var, default, memberswap)
Definition: fv3jedi_io_utils_mod.f90:117