Report Typos and Errors    
Semi-Lagrangian Library
Modular library for kinetic and gyrokinetic simulations of plasmas in fusion energy devices.
sll_m_io_utilities.F90
Go to the documentation of this file.
1 !------------------------------------------------------------------------------
2 ! SELALIB
3 !------------------------------------------------------------------------------
4 ! MODULE: sll_m_io_utilities
5 !
6 ! DESCRIPTION:
11 !------------------------------------------------------------------------------
13 
14 !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15 #include "sll_working_precision.h"
16 
17  implicit none
18 
19  public :: &
28 
29  private
30 !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31 
32 !==============================================================================
33 contains
34 !==============================================================================
35 
36  !----------------------------------------------------------------------------
38  subroutine sll_s_remove_file(filename)
39  character(len=*), intent(in) :: filename
40 
41  integer :: iunit
42 
43  open (newunit=iunit, file=filename, status='OLD')
44  close (iunit, status='DELETE')
45 
46  end subroutine sll_s_remove_file
47 
48  !----------------------------------------------------------------------------
50  subroutine sll_s_read_file(filename, str)
51  character(len=*), intent(in) :: filename
52  character(len=:), allocatable, intent(out) :: str
53 
54  integer :: iunit, istat, filesize
55  character(len=1) :: c
56 
57  ! Open required file
58  open (newunit=iunit, file=filename, status='OLD', &
59  form='UNFORMATTED', access='STREAM')
60 
61  ! How many characters in file
62  inquire (file=filename, size=filesize)
63  if (filesize < 0) then
64  write (*, *) 'ERROR: negative file size'
65  stop
66  end if
67 
68  ! Read whole file into one string
69  allocate (character(len=filesize) :: str)
70  read (iunit, pos=1) str
71 
72  ! Make sure it was all read by trying to read more
73  read (iunit, pos=filesize + 1, iostat=istat) c
74  if (.not. is_iostat_end(istat)) then
75  write (*, *) 'Error: file was not completely read'
76  stop
77  end if
78 
79  ! Close file
80  close (iunit, iostat=istat)
81 
82  end subroutine sll_s_read_file
83 
84  !----------------------------------------------------------------------------
85  ! Return True if two files are identical, False otherwise
86  function sll_f_check_equal_files(filename1, filename2) result(equal)
87  character(len=*), intent(in) :: filename1
88  character(len=*), intent(in) :: filename2
89  logical :: equal
90 
91  character(len=:), allocatable :: str1
92  character(len=:), allocatable :: str2
93 
94  call sll_s_read_file(filename1, str1)
95  call sll_s_read_file(filename2, str2)
96 
97  equal = (str1 == str2)
98 
99  end function sll_f_check_equal_files
100 
101  !----------------------------------------------------------------------------
103  function sll_f_check_empty_file(filename) result(empty)
104  character(len=*), intent(in) :: filename
105  logical :: empty
106 
107  integer :: filesize
108 
109  inquire (file=filename, size=filesize)
110  empty = (filesize == 0)
111 
112  end function sll_f_check_empty_file
113 
114  !----------------------------------------------------------------------------
118  subroutine sll_s_ints_to_string(ints, str)
119  sll_int32, intent(in) :: ints(:)
120  character(len=:), allocatable, intent(out) :: str
121 
122  sll_int32 :: i, ni, nc, lc, str_len
123  character(len=11), allocatable :: tmp(:)
124  sll_int32, allocatable :: ints_len(:)
125 
126  ! Allocate an homogeneous array of character
127  ni = size(ints)
128  allocate (tmp(ni))
129  allocate (ints_len(ni))
130 
131  ! Write integers to an omogeneous array of character,
132  ! as left-justified strings, and store length of each trimmed string
133  do i = 1, ni
134  write (tmp(i), '(i11)') ints(i)
135  tmp(i) = adjustl(tmp(i))
136  ints_len(i) = len_trim(tmp(i), i32)
137  end do
138 
139  ! Allocate single string with minimum length
140  str_len = sum(ints_len) + ni - 1
141  allocate (character(len=str_len) :: str)
142 
143  ! Write trimmed strings to single string, separated by blank space
144  lc = 0
145  do i = 1, ni
146  nc = ints_len(i)
147  str(lc + 1:lc + nc) = trim(tmp(i))
148  lc = lc + nc
149  if (i /= ni) then
150  str(lc + 1:lc + 1) = ' '
151  lc = lc + 1
152  end if
153  end do
154 
155  end subroutine sll_s_ints_to_string
156 
157  !----------------------------------------------------------------------------
159  subroutine sll_s_split_path(path, head, tail)
160  character(len=*), intent(in) :: path
161  character(len=*), intent(out) :: head
162  character(len=*), intent(out) :: tail
163 
164  sll_int32 :: nc
165  sll_int32 :: i
166 
167  ! Number of non-blank characters in path string
168  nc = len_trim(path, i32)
169 
170  ! If last character is '/', tail is empty
171  if (path(nc:nc) == '/') then
172  head = path(1:nc)
173  tail = ''
174  end if
175 
176  ! Search backwards (from right to left) for '/' character, and split path
177  do i = nc - 1, 1, -1
178  if (path(i:i) == '/') then
179  head = path(1:i)
180  tail = path(i + 1:nc)
181  return
182  end if
183  end do
184 
185  ! If no '/' character was found, head is empty
186  head = ''
187  tail = path(1:nc)
188 
189  end subroutine sll_s_split_path
190 
191  !----------------------------------------------------------------------------
193  subroutine sll_s_read_data_real_array(filename, data)
194  character(len=*), intent(in) :: filename
195  sll_real64, intent(out) :: data(:)
196 
197  sll_int32 :: iunit
198 
199  open (newunit=iunit, file=trim(filename), status="old", action="read")
200  read (unit=iunit, fmt=*) data
201  close (unit=iunit)
202 
203  end subroutine sll_s_read_data_real_array
204 
205  !----------------------------------------------------------------------------
212  subroutine sll_s_concatenate_filename_and_path(filename, otherfile, filepath)
213  character(len=*), intent(in) :: filename
214  character(len=*), intent(in) :: otherfile
215  character(len=*), intent(out) :: filepath
216 
217  sll_int32 :: ipath
218 
219  ipath = scan(otherfile, "/", back=.true.)
220  filepath = trim(otherfile(1:ipath))//filename
221 
223 
224 !==============================================================================
225 end module sll_m_io_utilities
Collection of functions/subroutines operating on files and strings.
subroutine, public sll_s_read_data_real_array(filename, data)
Read data from file to real array.
logical function, public sll_f_check_equal_files(filename1, filename2)
subroutine, public sll_s_ints_to_string(ints, str)
Write an array of integers to a single string: . Numbers are separated by a blank space; ....
subroutine, public sll_s_remove_file(filename)
Remove file (dangerous function!)
subroutine, public sll_s_split_path(path, head, tail)
Split path into head (directory) and tail (file)
logical function, public sll_f_check_empty_file(filename)
Return True if file is empty, False otherwise.
subroutine, public sll_s_read_file(filename, str)
Read whole file content into allocatable string.
subroutine, public sll_s_concatenate_filename_and_path(filename, otherfile, filepath)
Concatenate filename and path where path is extracted from another file name Example use: call sll_s_...
    Report Typos and Errors