Choral
io.f90
Go to the documentation of this file.
1 
7 
8 module io
9 
10  use real_type, only: rp
11  use basic_tools
13 
14  implicit none
15  private
16 
17 
18  ! %----------------------------------------%
19  ! | |
20  ! | PUBLIC DATA |
21  ! | |
22  ! %----------------------------------------%
23  public :: file_exists ! TESTED
24  public :: write ! TESTED
25  public :: read ! TESTED
26 
27  ! %----------------------------------------%
28  ! | |
29  ! | GENERIc SUBROUTINES |
30  ! | |
31  ! %----------------------------------------%
33  interface write
34  module procedure writevec_f, writevec_i
35  module procedure writearray_f
36  end interface write
37 
39  interface read
40  module procedure readvec_f, readvec_i
41  module procedure readarray_f
42  end interface read
43 
44 contains
45 
47  subroutine writearray_f(tab, fic, transpose)
48  real(RP) , dimension(:,:), intent(in) :: tab
49  character(len=*), intent(in) :: fic
50  logical , intent(in), optional :: transpose
51 
52  integer :: ii, jj
53 
54  logical :: bool
55 
56  if (choral_verb>2) write(*,*) &
57  & 'io : writeArray_f = '&
58  &, trim(fic)
59  open(unit=60,file=fic)
60 
61  bool = .false.
62  if (present(transpose)) bool = transpose
63 
64  if (bool) then
65 
66  !! write transpose(tab)
67  do ii=1, size(tab,2)
68  do jj=1, size(tab,1)
69  write(60, '(7g27.16)', advance='no') tab(jj, ii)
70  end do
71  write(60, '(7g27.16)', advance='yes')
72  end do
73 
74  else
75 
76  do ii=1, size(tab,1)
77  do jj=1, size(tab,2)
78  write(60, '(7g27.16)', advance='no') tab(ii,jj)
79  end do
80  write(60, '(7g27.16)', advance='yes')
81  end do
82 
83  end if
84 
85  close(60)
86  end subroutine writearray_f
87 
89  subroutine writevec_f(vec,fic)
90  real(RP) , dimension(:), intent(in) :: vec
91  character(len=*), intent(in) :: fic
92 
93  integer :: i,sz
94 
95  if (choral_verb>2) write(*,*) &
96  & 'io : writeVec_f = ',&
97  & trim(fic)
98  sz=size(vec,1)
99  open(unit=60,file=fic)
100  do i=1, sz
101  write(60,*) vec(i)
102  end do
103  close(60)
104 
105  end subroutine writevec_f
106 
108  subroutine writevec_i(vec,fic)
109  integer, dimension(:), intent(in) :: vec
110  character(len=*) , intent(in) :: fic
111 
112  integer :: i,sz
113 
114  if (choral_verb>2) write(*,*) &
115  & 'io : writeVec_i = ',&
116  & trim(fic)
117  sz=size(vec,1)
118  open(unit=60,file=fic)
119  do i=1, sz
120  write(60,*) vec(i)
121  end do
122  close(60)
123 
124  end subroutine writevec_i
125 
126 
128  subroutine readvec_f(vec,fic)
129  real(RP), dimension(:), intent(out) :: vec
130  character(len=*) , intent(in) :: fic
131 
132  integer :: i,sz
133 
134  sz = count_lines(fic)
135  if(sz/=size(vec,1)) call quit( 'io: readVec_f' )
136 
137  open(unit=60,file=fic)
138  do i=1, sz
139  read(60,*) vec(i)
140  end do
141  close(60)
142 
143  end subroutine readvec_f
144 
145 
146 
148  subroutine readarray_f(vec,fic)
149  real(RP), dimension(:,:), intent(out) :: vec
150  character(len=*) , intent(in) :: fic
151 
152  integer :: ii, sz
153 
154  sz = count_lines(fic)
155  if(sz/=size(vec,2)) call quit( 'io: readArray_f' )
156 
157  open(unit=60,file=fic)
158  do ii=1, size(vec,2)
159  read(60,*) vec(1:size(vec,1), ii)
160  end do
161  close(60)
162 
163  end subroutine readarray_f
164 
165 
166 
168  subroutine readvec_i(vec,fic)
169  integer, dimension(:), intent(out) :: vec
170  character(len=*) , intent(in) :: fic
171 
172  integer :: i,sz
173 
174  if (choral_verb>2) write(*,*) &
175  & 'io : readVec_i = ',&
176  & trim(fic)
177 
178  sz = count_lines(fic)
179  if(sz/=size(vec,1)) call quit( 'io: readVec_i' )
180 
181  sz=size(vec,1)
182  open(unit=60,file=fic)
183  do i=1, sz
184  read(60,*) vec(i)
185  end do
186  close(60)
187 
188  end subroutine readvec_i
189 
190 
192  function count_lines(fic) result(res)
193  character(len=*) , intent(in) :: fic
194  integer :: res
195 
196  character(len=120) :: sh_comm
197 
198  if(.NOT.file_exists(trim(fic))) call quit( &
199  & "io: count_lines "//trim(fic) )
200 
201  sh_comm='wc -l '//trim(fic)//' > xxxx'
202  call system(trim(sh_comm))
203  open(1,file='xxxx')
204  read(1,*) res
205  close(1)
206 
207  sh_comm='rm -f xxxx'
208  call system(trim(sh_comm))
209 
210  end function count_lines
211 
214  function file_exists(filename) result(res)
215  character(len=*),intent(in) :: filename
216  logical :: res
217 
218  inquire( file=trim(filename), exist=res )
219  end function file_exists
220 
221 
222 end module io
integer function count_lines(fic)
Count lines in a file.
Definition: io.f90:193
BASIC TOOLS
Definition: basic_tools.F90:8
integer, parameter, public rp
real(kind=RP) = real precision in the code REAL_TOL = epsilon to test real equality ...
Definition: real_type.F90:90
subroutine, public quit(message)
Stop program execution, display an error messahe.
subroutine writevec_i(vec, fic)
write vector to file, integer case
Definition: io.f90:109
write to file
Definition: io.f90:33
REAL NUMBERS PRECISION IN CHORAL: selects simple/double/quad
Definition: real_type.F90:33
IO: module for input/output
Definition: io.f90:8
subroutine readvec_i(vec, fic)
read vector from file, integer case
Definition: io.f90:169
subroutine writevec_f(vec, fic)
write vector to file, float case
Definition: io.f90:90
logical function, public file_exists(filename)
Check if the file exists.
Definition: io.f90:215
integer choral_verb
Verbosity level.
read from file
Definition: io.f90:39
DEFINITION OF GLOBAL VARIABLES FOR THE LIBRARY CHORAL
subroutine readarray_f(vec, fic)
read array from file, float case
Definition: io.f90:149
subroutine writearray_f(tab, fic, transpose)
write array to file, float case
Definition: io.f90:48
subroutine readvec_f(vec, fic)
read vector from file, float case
Definition: io.f90:129