Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loadtxt real format update to list directed #805

Merged
merged 8 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/specs/stdlib_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Loads a rank-2 `array` from a text file.

### Syntax

`call ` [[stdlib_io(module):loadtxt(interface)]] `(filename, array [, skiprows] [, max_rows])`
`call ` [[stdlib_io(module):loadtxt(interface)]] `(filename, array [, skiprows] [, max_rows] [, fmt])`

### Arguments

Expand All @@ -29,6 +29,10 @@ Loads a rank-2 `array` from a text file.

`max_rows` (optional): Read `max_rows` lines of content after `skiprows` lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.

`fmt` (optional): Fortran format specifier for the text read. Defaults to the write format for the data type. Setting fmt='*' will specify list directed read.



### Return value

Returns an allocated rank-2 `array` with the content of `filename`.
Expand Down
3 changes: 3 additions & 0 deletions example/io/example_loadtxt.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ program example_loadtxt
implicit none
real, allocatable :: x(:, :)
call loadtxt('example.dat', x)

! Can also use list directed format if the default read fails.
call loadtxt('example.dat', x, fmt='*')
end program example_loadtxt
59 changes: 49 additions & 10 deletions src/stdlib_io.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module stdlib_io
contains

#:for k1, t1 in KINDS_TYPES
subroutine loadtxt_${t1[0]}$${k1}$(filename, d, skiprows, max_rows)
subroutine loadtxt_${t1[0]}$${k1}$(filename, d, skiprows, max_rows, fmt)
!! version: experimental
!!
!! Loads a 2D array from a text file.
Expand All @@ -100,6 +100,8 @@ contains
!! A value of zero results in no lines to be read.
!! The default value is -1.
integer, intent(in), optional :: max_rows
character(len=*), intent(in), optional :: fmt
character(len=:), allocatable :: fmt_
!!
!! Example
!! -------
Expand Down Expand Up @@ -143,15 +145,52 @@ contains
read(s, *)
end do

do i = 1, max_rows_
#:if 'real' in t1
read(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") d(i, :)
#:elif 'complex' in t1
read(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") d(i, :)
#:else
read(s, *) d(i, :)
#:endif
end do
#:if 'real' in t1
! Default to format used for savetxt if fmt not specified.
fmt_ = optval(fmt, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))")

if ( fmt_ == '*' ) then
! Use list directed read if user has specified fmt='*'
do i = 1, max_rows_
read (s,*) d(i, :)
enddo
else
! Otherwise pass default or user specified fmt string.
do i = 1, max_rows_
read (s,fmt_) d(i, :)
enddo
endif
#:elif 'complex' in t1
! Default to format used for savetxt if fmt not specified.
fmt_ = optval(fmt, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))")
if ( fmt_ == '*' ) then
! Use list directed read if user has specified fmt='*'
do i = 1, max_rows_
read (s,*) d(i, :)
enddo
else
! Otherwise pass default or user specified fmt string.
do i = 1, max_rows_
read (s,fmt_) d(i, :)
enddo
endif
#:else
! Default to list directed for integer
fmt_ = optval(fmt, "*")
! Use list directed read if user has specified fmt='*'
if ( fmt_ == '*' ) then
do i = 1, max_rows_
read (s,*) d(i, :)
enddo
else
! Otherwise pass default user specified fmt string.
do i = 1, max_rows_
read (s,fmt_) d(i, :)
enddo
endif

#:endif

close(s)

end subroutine loadtxt_${t1[0]}$${k1}$
Expand Down
79 changes: 65 additions & 14 deletions test/io/test_loadtxt.f90
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ subroutine test_loadtxt_int32(error)
integer(int32), allocatable :: input(:,:), expected(:,:)
real(sp), allocatable :: harvest(:,:)
integer :: n

allocate(harvest(10,10))
allocate(input(10,10))
allocate(expected(10,10))

do n = 1, 10
call random_number(harvest)
input = int(harvest * 100)
call savetxt('test_int32.txt', input)
call loadtxt('test_int32.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default list directed read failed')
if (allocated(error)) return
call loadtxt('test_int32.txt', expected, fmt='*')
call check(error, all(input == expected),'User specified list directed read faile')
if (allocated(error)) return
end do

Expand All @@ -55,17 +56,23 @@ subroutine test_loadtxt_sp(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
real(sp), allocatable :: input(:,:), expected(:,:)
character(len=*), parameter :: FMT_REAL_SP = '(es15.8e2)'
integer :: n

allocate(input(10,10))
allocate(expected(10,10))

do n = 1, 10
call random_number(input)
input = input - 0.5
call savetxt('test_sp.txt', input)
call loadtxt('test_sp.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_sp.txt', expected, fmt='*')
call check(error, all(input == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_sp.txt', expected, fmt="(*"//FMT_REAL_sp(1:len(FMT_REAL_sp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
if (allocated(error)) return
end do

Expand All @@ -77,7 +84,8 @@ subroutine test_loadtxt_sp_huge(error)
type(error_type), allocatable, intent(out) :: error
real(sp), allocatable :: input(:,:), expected(:,:)
integer :: n

character(len=*), parameter :: FMT_REAL_SP = '(es15.8e2)'

allocate(input(10,10))
allocate(expected(10,10))

Expand All @@ -86,7 +94,13 @@ subroutine test_loadtxt_sp_huge(error)
input = (input - 0.5) * huge(input)
call savetxt('test_sp_huge.txt', input)
call loadtxt('test_sp_huge.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_sp_huge.txt', expected, fmt='*')
call check(error, all(input == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_sp_huge.txt', expected, fmt="(*"//FMT_REAL_sp(1:len(FMT_REAL_sp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
if (allocated(error)) return
end do

Expand All @@ -98,6 +112,7 @@ subroutine test_loadtxt_sp_tiny(error)
type(error_type), allocatable, intent(out) :: error
real(sp), allocatable :: input(:,:), expected(:,:)
integer :: n
character(len=*), parameter :: FMT_REAL_SP = '(es15.8e2)'

allocate(input(10,10))
allocate(expected(10,10))
Expand All @@ -107,7 +122,13 @@ subroutine test_loadtxt_sp_tiny(error)
input = (input - 0.5) * tiny(input)
call savetxt('test_sp_tiny.txt', input)
call loadtxt('test_sp_tiny.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_sp_tiny.txt', expected, fmt='*')
call check(error, all(input == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_sp_tiny.txt', expected, fmt="(*"//FMT_REAL_sp(1:len(FMT_REAL_sp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
if (allocated(error)) return
end do

Expand All @@ -119,6 +140,7 @@ subroutine test_loadtxt_dp(error)
type(error_type), allocatable, intent(out) :: error
real(dp), allocatable :: input(:,:), expected(:,:)
integer :: n
character(len=*), parameter :: FMT_REAL_DP = '(es24.16e3)'

allocate(input(10,10))
allocate(expected(10,10))
Expand All @@ -128,7 +150,13 @@ subroutine test_loadtxt_dp(error)
input = input - 0.5
call savetxt('test_dp.txt', input)
call loadtxt('test_dp.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_dp.txt', expected, fmt='*')
call check(error, all(input == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_dp.txt', expected, fmt="(*"//FMT_REAL_dp(1:len(FMT_REAL_dp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
if (allocated(error)) return
end do

Expand All @@ -140,6 +168,7 @@ subroutine test_loadtxt_dp_max_skip(error)
type(error_type), allocatable, intent(out) :: error
real(dp), allocatable :: input(:,:), expected(:,:)
integer :: n, m
character(len=*), parameter :: FMT_REAL_DP = '(es24.16e3)'

allocate(input(10,10))

Expand All @@ -149,7 +178,13 @@ subroutine test_loadtxt_dp_max_skip(error)
input = input - 0.5
call savetxt('test_dp_max_skip.txt', input)
call loadtxt('test_dp_max_skip.txt', expected, skiprows=m, max_rows=n)
call check(error, all(input(m+1:min(n+m,10),:) == expected))
call check(error, all(input(m+1:min(n+m,10),:) == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_dp_max_skip.txt', expected, skiprows=m, max_rows=n, fmt='*')
call check(error, all(input(m+1:min(n+m,10),:) == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_dp_max_skip.txt', expected, fmt="(*"//FMT_REAL_dp(1:len(FMT_REAL_dp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
deallocate(expected)
if (allocated(error)) return
end do
Expand All @@ -163,6 +198,7 @@ subroutine test_loadtxt_dp_huge(error)
type(error_type), allocatable, intent(out) :: error
real(dp), allocatable :: input(:,:), expected(:,:)
integer :: n
character(len=*), parameter :: FMT_REAL_DP = '(es24.16e3)'

allocate(input(10,10))
allocate(expected(10,10))
Expand All @@ -172,7 +208,13 @@ subroutine test_loadtxt_dp_huge(error)
input = (input - 0.5) * huge(input)
call savetxt('test_dp_huge.txt', input)
call loadtxt('test_dp_huge.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_dp_huge.txt', expected, fmt='*')
call check(error, all(input == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_dp_huge.txt', expected, fmt="(*"//FMT_REAL_dp(1:len(FMT_REAL_dp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
if (allocated(error)) return
end do

Expand All @@ -184,7 +226,8 @@ subroutine test_loadtxt_dp_tiny(error)
type(error_type), allocatable, intent(out) :: error
real(dp), allocatable :: input(:,:), expected(:,:)
integer :: n

character(len=*), parameter :: FMT_REAL_DP = '(es24.16e3)'

allocate(input(10,10))
allocate(expected(10,10))

Expand All @@ -193,7 +236,13 @@ subroutine test_loadtxt_dp_tiny(error)
input = (input - 0.5) * tiny(input)
call savetxt('test_dp_tiny.txt', input)
call loadtxt('test_dp_tiny.txt', expected)
call check(error, all(input == expected))
call check(error, all(input == expected),'Default format read failed')
if (allocated(error)) return
call loadtxt('test_dp_tiny.txt', expected, fmt='*')
call check(error, all(input == expected),'List directed read failed')
if (allocated(error)) return
call loadtxt('test_dp_tiny.txt', expected, fmt="(*"//FMT_REAL_dp(1:len(FMT_REAL_dp)-1)//",1x))")
call check(error, all(input == expected),'User specified format failed')
if (allocated(error)) return
end do

Expand All @@ -206,6 +255,7 @@ subroutine test_loadtxt_complex(error)
complex(dp), allocatable :: input(:,:), expected(:,:)
real(dp), allocatable :: re(:,:), im(:,:)
integer :: n
character(len=*), parameter :: FMT_COMPLEX_DP = '(es24.16e3,1x,es24.16e3)'

allocate(re(10,10))
allocate(im(10,10))
Expand All @@ -219,6 +269,8 @@ subroutine test_loadtxt_complex(error)
call savetxt('test_complex.txt', input)
call loadtxt('test_complex.txt', expected)
call check(error, all(input == expected))
call loadtxt('test_complex.txt', expected, fmt="(*"//FMT_COMPLEX_dp(1:len(FMT_COMPLEX_dp)-1)//",1x))")
call check(error, all(input == expected))
if (allocated(error)) return
end do

Expand All @@ -237,7 +289,6 @@ program tester
character(len=*), parameter :: fmt = '("#", *(1x, a))'

stat = 0

testsuites = [ &
new_testsuite("loadtxt", collect_loadtxt) &
]
Expand Down
Loading