-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmo_file_utils.f90
More file actions
251 lines (187 loc) · 7.27 KB
/
mo_file_utils.f90
File metadata and controls
251 lines (187 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
!> \file mo_file_utils.f90
!> \brief General file utilities
!> \details This module provides general utilities for handling files.
!> \authors Matthias Cuntz
!> \date Dec 2011
module mo_file_utils
! This module provides general utilities for handling files
! Written Matthias Cuntz, Dec 2011
! Modified Matthias Cuntz, Jan 2017 - istart,istop optional in find_next_unit
! Matthias Cuntz, Jan 2017 - lines_in_file
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2017 Matthias Cuntz - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
implicit none
private
public :: find_next_unit ! find file handle that is not used yet
public :: lif ! alias for lines_in_file
public :: lines_in_file ! count number of lines in file
public :: next_unit ! alias for find_next_unit
interface lif
module procedure lines_in_file
end interface lif
interface next_unit
module procedure find_next_unit
end interface next_unit
! ------------------------------------------------------------------
contains
! ------------------------------------------------------------------
! NAME
! find_next_unit
! PURPOSE
!> \brief Find unused file unit.
!> \details Starting from a given number this routine checks in a range
!> if the numbers are already assigned to a file handle.
!> It reports the first number that is not associated yet.
! CALLING SEQUENCE
! iout = find_next_unit(istart,istop)
! INTENT(IN)
! None
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "integer :: istart" Starting unit (default: 20)
!> \param[in] "integer :: istop" Last checked unit (default: 1000)
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return integer :: find_next_unit ! Free unit in interval [istart,istop], returns -1 if no free unit.
! RESTRICTIONS
! None
! EXAMPLE
! iout = find_next_unit(100)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Written, Matthias Cuntz - modified from Echam5 mo_filename.f90/find_next_free_unit
!> Echam5, (C) MPI-MET, Hamburg, Germany
!> \date Dec 2011
! Modified, Matthias Cuntz, Jan 2017 - istart, istop optinal
function find_next_unit(istart,istop)
implicit none
integer, intent(in), optional :: istart, istop
integer :: find_next_unit
logical :: opened
integer :: i, ifirst, ilast
ifirst = 20
if (present(istart)) ifirst = istart
ilast = 1000
if (present(istop)) ilast = istop
opened = .true.
find_next_unit = -1
do i=ifirst, ilast
inquire(unit=i, opened=opened)
if (.not. opened) then
find_next_unit = i
exit
end if
end do
end function find_next_unit
! ------------------------------------------------------------------
! NAME
! lines_in_file
! PURPOSE
!> \brief Count number of lines in a file.
!> \details Count number of lines in file,
!> optionally excluding blank lines and lines starting with comment character.
! CALLING SEQUENCE
! num = lines_in_file(filename, comment, noblank)
! INTENT(IN)
!> \param[in] "character(*) :: filename" Name of file to count lines
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "character(*) :: comment" Exclude lines starting with comment.
!> Leading blanks are ignored.
!> \param[in] "logical :: noblank" .false.: count blank lines,
!> i.e. lines, where trim(adjustl(line)) is epmty.
!> .true.: do not count blank lines,
!> i.e. where trim(adjustl(line)) is epmty.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return integer :: lines_in_file ! number of lines, without commented or blank lines (optional)
! RESTRICTIONS
! None
! EXAMPLE
! iout = lines_in_file(filename, comment='!', noblank=.true.)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Written, Matthias Cuntz
!> \date Jan 2017
function lines_in_file(filename, comment, noblank)
implicit none
character(len=*), intent(in) :: filename
character(len=*), optional, intent(in) :: comment
logical, optional, intent(in) :: noblank
integer :: lines_in_file
! ToDo
integer, parameter :: maxlen = 8192
logical :: iexist, inoblank
integer :: nun, ierr
character(len=maxlen) :: iread, icomment
! options
inoblank = .false.
if (present(noblank)) inoblank = noblank
! file exists
inquire(file=filename, exist=iexist)
if (.not. iexist) then
lines_in_file = -1
return
endif
! open
nun = find_next_unit()
open(nun, file=filename, status='old', action='read', form="formatted", recl=maxlen)
! count
lines_in_file = 0
ierr = 0
do while (ierr==0)
read(nun, '(a)', iostat=ierr) iread
if (ierr==0) then
! exclude commented lines
if (present(comment)) then
icomment = adjustl(iread)
if (icomment(1:len(comment)) == comment) cycle
endif
! exclude blank lines
if (inoblank) then
if (trim(adjustl(iread)) == '') cycle
endif
lines_in_file = lines_in_file + 1
endif
end do
close(nun)
end function lines_in_file
! ------------------------------------------------------------------
END MODULE mo_file_utils