PB3D  [2.45]
Ideal linear high-n MHD stability in 3-D
files_utilities.f90
Go to the documentation of this file.
1 !------------------------------------------------------------------------------!
3 !------------------------------------------------------------------------------!
5 #include <PB3D_macros.h>
6  use str_utilities
7  use messages
8  use num_vars, only: dp, max_str_ln
9  implicit none
10  private
13 
14 contains
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 
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 
85  subroutine get_file_info(file_name,file_size,acc_time,mod_time)
86  ! input / output
87  character(len=*), intent(in) :: file_name
88  integer, intent(inout), optional :: file_size
89  integer, intent(inout), optional :: acc_time
90  integer, intent(inout), optional :: mod_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 
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 
143  integer function delete_file(file_name) result(istat)
144  ! input / output
145  character(len=*), intent(inout) :: file_name
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 
169  integer function count_lines(file_i) result(nr_lines)
170  ! input / output
171  integer, intent(in) :: file_i
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
191 end module files_utilities
files_utilities::delete_file
integer function, public delete_file(file_name)
Removes a file.
Definition: files_utilities.f90:144
num_vars::dp
integer, parameter, public dp
double precision
Definition: num_vars.f90:46
num_vars
Numerical variables used by most other modules.
Definition: num_vars.f90:4
files_utilities::skip_comment
integer function, public skip_comment(file_i, file_name)
Skips comment when reading a file.
Definition: files_utilities.f90:55
files_utilities::get_file_info
subroutine, public get_file_info(file_name, file_size, acc_time, mod_time)
Gets file information.
Definition: files_utilities.f90:86
num_vars::max_str_ln
integer, parameter, public max_str_ln
maximum length of strings
Definition: num_vars.f90:50
str_utilities::i2str
elemental character(len=max_str_ln) function, public i2str(k)
Convert an integer to string.
Definition: str_utilities.f90:18
str_utilities
Operations on strings.
Definition: str_utilities.f90:4
files_utilities::nextunit
integer function, public nextunit(unit)
Search for available new unit.
Definition: files_utilities.f90:27
messages::writo
subroutine, public writo(input_str, persistent, error, warning, alert)
Write output to file identified by output_i.
Definition: messages.f90:275
messages
Numerical utilities related to giving output.
Definition: messages.f90:4
files_utilities::count_lines
integer function, public count_lines(file_i)
Count non-comment lines in a file.
Definition: files_utilities.f90:170
num_vars::pb3d_name
character(len=max_str_ln), public pb3d_name
name of PB3D output file
Definition: num_vars.f90:139
files_utilities::get_full_pb3d_name
character(len=max_str_ln) function, public get_full_pb3d_name(rich_lvl)
Returns the name of the PB3D output file.
Definition: files_utilities.f90:119
files_utilities
Numerical utilities related to files.
Definition: files_utilities.f90:4