PB3D [2.47]
Ideal linear high-n MHD stability in 3-D
Loading...
Searching...
No Matches
files_utilities.f90
Go to the documentation of this file.
1!------------------------------------------------------------------------------!
2!> Numerical utilities related to files
3!------------------------------------------------------------------------------!
5#include <PB3D_macros.h>
7 use messages
8 use num_vars, only: dp, max_str_ln
9 implicit none
10 private
13
14contains
15 !> Search for available new unit.
16 !!
17 !! \c lun_min and \c lun_max define the range of possible luns to check.
18 !!
19 !! The unit value is returned by the function, and also by the optional
20 !! argument. This allows the function to be used directly in an open
21 !! statement, and optionally save the result in a local variable.
22 !!
23 !! If no units are available, -1 is returned.
24 !!
25 !! \see Adapted from http://fortranwiki.org/fortran/show/newunit
26 integer function nextunit(unit)
27 ! input / output
28 integer, intent(out), optional :: unit
29
30 ! local variables
31 integer, parameter :: lun_min=70, lun_max=1000
32 logical :: file_open
33 integer :: lun
34
35 ! iterate over permitted luns until available one found
36 nextunit=-1
37 do lun=lun_min,lun_max
38 inquire(unit=lun,opened=file_open)
39 if (.not.file_open) then
40 nextunit=lun
41 exit
42 end if
43 end do
44
45 ! return unit number if present
46 if (present(unit)) unit=nextunit
47 end function nextunit
48
49 !> Skips comment when reading a file.
50 !!
51 !! By comment, a line is meant that starts with the character \c \#.
52 !!
53 !! \return ierr
54 integer function skip_comment(file_i,file_name) result(ierr)
55 character(*), parameter :: rout_name = 'skip_comment'
56
57 ! input / output
58 integer, intent(in) :: file_i ! file identifier
59 character(len=*), intent(in), optional :: file_name ! file name
60
61 ! local variables
62 character(len=1) :: loc_data_char ! first data character
63 character(len=max_str_ln) :: err_msg ! error message
64
65 ! initialize ierr
66 ierr = 0
67
68 loc_data_char = '#'
69 do while (loc_data_char.eq.'#')
70 read(file_i,*,iostat=ierr) loc_data_char
71 if (present(file_name)) then
72 err_msg = 'failed to read "'//trim(file_name)//'"'
73 else
74 err_msg = 'failed to read file'
75 end if
76 chckerr(err_msg)
77 end do
78 backspace(file_i)
79 end function skip_comment
80
81 !> Gets file information.
82 !!
83 !! The time informations can be converted to strings using the intrinsic
84 !! function "ctime".
85 subroutine get_file_info(file_name,file_size,acc_time,mod_time)
86 ! input / output
87 character(len=*), intent(in) :: file_name !< name of file
88 integer, intent(inout), optional :: file_size !< file size
89 integer, intent(inout), optional :: acc_time !< file access time
90 integer, intent(inout), optional :: mod_time !< file modification time
91
92 ! local variables
93 integer :: vals(13) ! values
94 integer :: istat ! status
95
96 ! call stat
97 call stat(trim(file_name),vals,istat)
98
99 ! check status
100 if (istat.ne.0) then
101 call writo('call to stat failed',warning=.true.)
102 if (present(file_size)) file_size = 0
103 if (present(acc_time)) acc_time = 0
104 if (present(mod_time)) mod_time = 0
105 else
106 if (present(file_size)) file_size = vals(8)
107 if (present(acc_time)) acc_time = vals(9)
108 if (present(mod_time)) mod_time = vals(10)
109 end if
110 end subroutine get_file_info
111
112 !> Returns the name of the PB3D output file.
113 !!
114 !! Optionally, the Richardson level can be appended as <tt>_R_[lvl]</tt>.
115 !!
116 !! If not positive, it is ignored.
117 character(len=max_str_ln) function get_full_pb3d_name(rich_lvl) &
118 &result(full_pb3d_name)
119 use num_vars, only: pb3d_name
120
121 ! input / output
122 integer, intent(in), optional :: rich_lvl ! optional richardson level to be appended
123
124 ! local variables
125 integer :: rich_lvl_loc ! local richardson level
126
127 ! set local rich_lvl
128 rich_lvl_loc = 0
129 if (present(rich_lvl)) rich_lvl_loc = rich_lvl
130
131 ! set ouput
132 if (rich_lvl_loc.gt.0) then
133 full_pb3d_name = trim(pb3d_name)//'_R_'//&
134 &trim(i2str(rich_lvl_loc))//'.h5'
135 else
136 full_pb3d_name = trim(pb3d_name)//'.h5'
137 end if
138 end function get_full_pb3d_name
139
140 !> Removes a file.
141 !!
142 !! \return istat
143 integer function delete_file(file_name) result(istat)
144 ! input / output
145 character(len=*), intent(inout) :: file_name !< the name that is deleted
146
147 ! local variables
148 integer :: file_i ! file unit
149 logical :: file_open ! whether file is open
150
151 ! initialize istat
152 istat = 0
153
154 ! check if opened and open if not
155 inquire(file=trim(file_name),opened=file_open,number=file_i,&
156 &iostat=istat)
157 chckstt
158 if (.not.file_open) then
159 open(unit=nextunit(file_i),file=trim(file_name),iostat=istat)
160 end if
161 chckstt
162
163 ! remove open file
164 close(unit=file_i,status='delete',iostat=istat)
165 chckstt
166 end function delete_file
167
168 !> Count non-comment lines in a file.
169 integer function count_lines(file_i) result(nr_lines)
170 ! input / output
171 integer, intent(in) :: file_i !< file identifier
172
173 ! local variables
174 integer :: istat ! status
175 character(len=1) :: loc_data_char ! first data character
176
177 ! initialize istat
178 istat = 0
179
180 nr_lines = 0
181 rewind(file_i)
182 do while (istat.eq.0)
183 ! read first character of data
184 read(file_i,*,iostat=istat) loc_data_char
185 if (istat.eq.0 .and. loc_data_char.ne.'#') then ! exclude comment lines
186 nr_lines = nr_lines + 1
187 end if
188 end do
189 rewind(file_i)
190 end function count_lines
191end module files_utilities
Numerical utilities related to files.
subroutine, public get_file_info(file_name, file_size, acc_time, mod_time)
Gets file information.
integer function, public delete_file(file_name)
Removes a file.
integer function, public count_lines(file_i)
Count non-comment lines in a file.
character(len=max_str_ln) function, public get_full_pb3d_name(rich_lvl)
Returns the name of the PB3D output file.
integer function, public skip_comment(file_i, file_name)
Skips comment when reading a file.
integer function, public nextunit(unit)
Search for available new unit.
Numerical utilities related to giving output.
Definition messages.f90:4
subroutine, public writo(input_str, persistent, error, warning, alert)
Write output to file identified by output_i.
Definition messages.f90:275
Numerical variables used by most other modules.
Definition num_vars.f90:4
integer, parameter, public dp
double precision
Definition num_vars.f90:46
integer, parameter, public max_str_ln
maximum length of strings
Definition num_vars.f90:50
character(len=max_str_ln), public pb3d_name
name of PB3D output file
Definition num_vars.f90:139
Operations on strings.
elemental character(len=max_str_ln) function, public i2str(k)
Convert an integer to string.