1
1
module stdlib_experimental_io
2
- use iso_fortran_env, only: sp= >real32, dp= >real64
2
+ use iso_fortran_env, only: sp= >real32, dp= >real64 ,qp = >real128
3
3
implicit none
4
4
private
5
5
public :: loadtxt, savetxt
6
6
7
7
interface loadtxt
8
8
module procedure sloadtxt
9
9
module procedure dloadtxt
10
+ module procedure qloadtxt
10
11
end interface
11
12
12
13
interface savetxt
13
14
module procedure ssavetxt
14
15
module procedure dsavetxt
16
+ module procedure qsavetxt
15
17
end interface
16
18
17
19
contains
18
20
21
+ ! PUBLIC
19
22
subroutine sloadtxt (filename , d )
23
+ ! Loads a 2D array from a text file.
24
+ !
25
+ ! Arguments
26
+ ! ---------
27
+ !
28
+ ! Filename to load the array from
20
29
character (len=* ), intent (in ) :: filename
30
+ ! The array 'd' will be automatically allocated with the correct dimensions
21
31
real (sp), allocatable , intent (out ) :: d(:,:)
22
- real (dp), allocatable :: tmp(:,:)
23
- call dloadtxt(filename, tmp)
24
- allocate (d(size (tmp,1 ),size (tmp,2 )))
25
- d = real (tmp,sp)
32
+ !
33
+ ! Example
34
+ ! -------
35
+ !
36
+ ! real(sp), allocatable :: data(:, :)
37
+ ! call loadtxt("log.txt", data) ! 'data' will be automatically allocated
38
+ !
39
+ ! Where 'log.txt' contains for example::
40
+ !
41
+ ! 1 2 3
42
+ ! 2 4 6
43
+ ! 8 9 10
44
+ ! 11 12 13
45
+ ! ...
46
+ !
47
+ integer :: s
48
+ integer :: nrow,ncol,i
49
+
50
+ open (newunit= s, file= filename, status= " old" )
51
+
52
+ ! determine number of columns
53
+ ncol= number_of_columns(s)
54
+
55
+ ! determine number or rows
56
+ nrow = number_of_rows_numeric(s)
57
+
58
+ allocate (d(nrow, ncol))
59
+ do i = 1 , nrow
60
+ read (s, * ) d(i, :)
61
+ end do
62
+ close (s)
26
63
end subroutine
27
64
28
65
subroutine dloadtxt (filename , d )
@@ -50,34 +87,59 @@ subroutine dloadtxt(filename, d)
50
87
! 11 12 13
51
88
! ...
52
89
!
53
- character :: c
54
- integer :: s, ncol, nrow, ios, i
55
- logical :: lastwhite
56
- real (dp) :: r
90
+ integer :: s
91
+ integer :: nrow,ncol,i
57
92
58
93
open (newunit= s, file= filename, status= " old" )
59
94
60
95
! determine number of columns
61
- ncol = 0
62
- lastwhite = .true.
63
- do
64
- read (s, ' (a)' , advance= ' no' , iostat= ios) c
65
- if (ios /= 0 ) exit
66
- if (lastwhite .and. .not. whitechar(c)) ncol = ncol + 1
67
- lastwhite = whitechar(c)
68
- end do
69
-
70
- rewind(s)
96
+ ncol= number_of_columns(s)
71
97
72
98
! determine number or rows
73
- nrow = 0
74
- do
75
- read (s, * , iostat = ios) r
76
- if (ios /= 0 ) exit
77
- nrow = nrow + 1
99
+ nrow = number_of_rows_numeric(s)
100
+
101
+ allocate (d(nrow, ncol))
102
+ do i = 1 , nrow
103
+ read (s, * ) d(i, :)
78
104
end do
105
+ close (s)
106
+ end subroutine
107
+
108
+ subroutine qloadtxt (filename , d )
109
+ ! Loads a 2D array from a text file.
110
+ !
111
+ ! Arguments
112
+ ! ---------
113
+ !
114
+ ! Filename to load the array from
115
+ character (len=* ), intent (in ) :: filename
116
+ ! The array 'd' will be automatically allocated with the correct dimensions
117
+ real (qp), allocatable , intent (out ) :: d(:,:)
118
+ !
119
+ ! Example
120
+ ! -------
121
+ !
122
+ ! real(qp), allocatable :: data(:, :)
123
+ ! call loadtxt("log.txt", data) ! 'data' will be automatically allocated
124
+ !
125
+ ! Where 'log.txt' contains for example::
126
+ !
127
+ ! 1 2 3
128
+ ! 2 4 6
129
+ ! 8 9 10
130
+ ! 11 12 13
131
+ ! ...
132
+ !
133
+ integer :: s
134
+ integer :: nrow,ncol,i
135
+
136
+ open (newunit= s, file= filename, status= " old" )
79
137
80
- rewind(s)
138
+ ! determine number of columns
139
+ ncol= number_of_columns(s)
140
+
141
+ ! determine number or rows
142
+ nrow = number_of_rows_numeric(s)
81
143
82
144
allocate (d(nrow, ncol))
83
145
do i = 1 , nrow
@@ -86,10 +148,28 @@ subroutine dloadtxt(filename, d)
86
148
close (s)
87
149
end subroutine
88
150
151
+
89
152
subroutine ssavetxt (filename , d )
90
- character (len=* ), intent (in ) :: filename
91
- real (sp), intent (in ) :: d(:,:)
92
- call dsavetxt(filename, real (d,dp))
153
+ ! Saves a 2D array into a textfile.
154
+ !
155
+ ! Arguments
156
+ ! ---------
157
+ !
158
+ character (len=* ), intent (in ) :: filename ! File to save the array to
159
+ real (sp), intent (in ) :: d(:,:) ! The 2D array to save
160
+ !
161
+ ! Example
162
+ ! -------
163
+ !
164
+ ! real(sp) :: data(3, 2)
165
+ ! call savetxt("log.txt", data)
166
+
167
+ integer :: s, i
168
+ open (newunit= s, file= filename, status= " replace" )
169
+ do i = 1 , size (d, 1 )
170
+ write (s, * ) d(i, :)
171
+ end do
172
+ close (s)
93
173
end subroutine
94
174
95
175
subroutine dsavetxt (filename , d )
@@ -115,6 +195,70 @@ subroutine dsavetxt(filename, d)
115
195
close (s)
116
196
end subroutine
117
197
198
+ subroutine qsavetxt (filename , d )
199
+ ! Saves a 2D array into a textfile.
200
+ !
201
+ ! Arguments
202
+ ! ---------
203
+ !
204
+ character (len=* ), intent (in ) :: filename ! File to save the array to
205
+ real (qp), intent (in ) :: d(:,:) ! The 2D array to save
206
+ !
207
+ ! Example
208
+ ! -------
209
+ !
210
+ ! real(dp) :: data(3, 2)
211
+ ! call savetxt("log.txt", data)
212
+
213
+ integer :: s, i
214
+ open (newunit= s, file= filename, status= " replace" )
215
+ do i = 1 , size (d, 1 )
216
+ write (s, * ) d(i, :)
217
+ end do
218
+ close (s)
219
+ end subroutine
220
+
221
+
222
+ ! PRIVATE
223
+ integer function number_of_columns (s )
224
+ ! determine number of columns
225
+ integer ,intent (in ):: s
226
+
227
+ integer :: ios
228
+ character :: c
229
+ logical :: lastwhite
230
+
231
+ rewind(s)
232
+ number_of_columns = 0
233
+ lastwhite = .true.
234
+ do
235
+ read (s, ' (a)' , advance= ' no' , iostat= ios) c
236
+ if (ios /= 0 ) exit
237
+ if (lastwhite .and. .not. whitechar(c)) number_of_columns = number_of_columns + 1
238
+ lastwhite = whitechar(c)
239
+ end do
240
+ rewind(s)
241
+
242
+ end function
243
+
244
+ integer function number_of_rows_numeric (s )
245
+ ! determine number or rows
246
+ integer ,intent (in ):: s
247
+ integer :: ios
248
+
249
+ real :: r
250
+
251
+ rewind(s)
252
+ number_of_rows_numeric = 0
253
+ do
254
+ read (s, * , iostat= ios) r
255
+ if (ios /= 0 ) exit
256
+ number_of_rows_numeric = number_of_rows_numeric + 1
257
+ end do
258
+
259
+ rewind(s)
260
+
261
+ end function
118
262
119
263
logical function whitechar (char ) ! white character
120
264
! returns .true. if char is space (32) or tab (9), .false. otherwise
0 commit comments