-
Notifications
You must be signed in to change notification settings - Fork 184
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
Single and quadruple precisions for load/savetxt #37
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,65 @@ | ||
module stdlib_experimental_io | ||
use iso_fortran_env, only: sp=>real32, dp=>real64 | ||
use iso_fortran_env, only: sp=>real32, dp=>real64 ,qp=>real128 | ||
implicit none | ||
private | ||
public :: loadtxt, savetxt | ||
|
||
interface loadtxt | ||
module procedure sloadtxt | ||
module procedure dloadtxt | ||
module procedure qloadtxt | ||
end interface | ||
|
||
interface savetxt | ||
module procedure ssavetxt | ||
module procedure dsavetxt | ||
module procedure qsavetxt | ||
end interface | ||
|
||
contains | ||
|
||
!PUBLIC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am used to sort the different procedures in groups of public and private procedures. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Once we fix #4, we'll consolidate all such things. |
||
subroutine sloadtxt(filename, d) | ||
! Loads a 2D array from a text file. | ||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
! | ||
! Arguments | ||
! --------- | ||
! | ||
! Filename to load the array from | ||
character(len=*), intent(in) :: filename | ||
! The array 'd' will be automatically allocated with the correct dimensions | ||
real(sp), allocatable, intent(out) :: d(:,:) | ||
real(dp), allocatable :: tmp(:,:) | ||
call dloadtxt(filename, tmp) | ||
allocate(d(size(tmp,1),size(tmp,2))) | ||
d = real(tmp,sp) | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! real(sp), allocatable :: data(:, :) | ||
! call loadtxt("log.txt", data) ! 'data' will be automatically allocated | ||
! | ||
! Where 'log.txt' contains for example:: | ||
! | ||
! 1 2 3 | ||
! 2 4 6 | ||
! 8 9 10 | ||
! 11 12 13 | ||
! ... | ||
! | ||
integer :: s | ||
integer ::nrow,ncol,i | ||
|
||
open(newunit=s, file=filename, status="old") | ||
|
||
! determine number of columns | ||
ncol=number_of_columns(s) | ||
|
||
! determine number or rows | ||
nrow = number_of_rows_numeric(s) | ||
|
||
allocate(d(nrow, ncol)) | ||
do i = 1, nrow | ||
read(s, *) d(i, :) | ||
end do | ||
close(s) | ||
end subroutine | ||
|
||
subroutine dloadtxt(filename, d) | ||
|
@@ -50,34 +87,59 @@ subroutine dloadtxt(filename, d) | |
! 11 12 13 | ||
! ... | ||
! | ||
character :: c | ||
integer :: s, ncol, nrow, ios, i | ||
logical :: lastwhite | ||
real(dp) :: r | ||
integer :: s | ||
integer ::nrow,ncol,i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Corrected |
||
|
||
open(newunit=s, file=filename, status="old") | ||
|
||
! determine number of columns | ||
ncol = 0 | ||
lastwhite = .true. | ||
do | ||
read(s, '(a)', advance='no', iostat=ios) c | ||
if (ios /= 0) exit | ||
if (lastwhite .and. .not. whitechar(c)) ncol = ncol + 1 | ||
lastwhite = whitechar(c) | ||
end do | ||
|
||
rewind(s) | ||
ncol=number_of_columns(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Corrected |
||
|
||
! determine number or rows | ||
nrow = 0 | ||
do | ||
read(s, *, iostat=ios) r | ||
if (ios /= 0) exit | ||
nrow = nrow + 1 | ||
nrow = number_of_rows_numeric(s) | ||
|
||
allocate(d(nrow, ncol)) | ||
do i = 1, nrow | ||
read(s, *) d(i, :) | ||
end do | ||
close(s) | ||
end subroutine | ||
|
||
subroutine qloadtxt(filename, d) | ||
! Loads a 2D array from a text file. | ||
! | ||
! Arguments | ||
! --------- | ||
! | ||
! Filename to load the array from | ||
character(len=*), intent(in) :: filename | ||
! The array 'd' will be automatically allocated with the correct dimensions | ||
real(qp), allocatable, intent(out) :: d(:,:) | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! real(qp), allocatable :: data(:, :) | ||
! call loadtxt("log.txt", data) ! 'data' will be automatically allocated | ||
! | ||
! Where 'log.txt' contains for example:: | ||
! | ||
! 1 2 3 | ||
! 2 4 6 | ||
! 8 9 10 | ||
! 11 12 13 | ||
! ... | ||
! | ||
integer :: s | ||
integer ::nrow,ncol,i | ||
|
||
open(newunit=s, file=filename, status="old") | ||
|
||
rewind(s) | ||
! determine number of columns | ||
ncol=number_of_columns(s) | ||
|
||
! determine number or rows | ||
nrow = number_of_rows_numeric(s) | ||
|
||
allocate(d(nrow, ncol)) | ||
do i = 1, nrow | ||
|
@@ -86,10 +148,28 @@ subroutine dloadtxt(filename, d) | |
close(s) | ||
end subroutine | ||
|
||
|
||
subroutine ssavetxt(filename, d) | ||
character(len=*), intent(in) :: filename | ||
real(sp), intent(in) :: d(:,:) | ||
call dsavetxt(filename, real(d,dp)) | ||
! Saves a 2D array into a textfile. | ||
! | ||
! Arguments | ||
! --------- | ||
! | ||
character(len=*), intent(in) :: filename ! File to save the array to | ||
real(sp), intent(in) :: d(:,:) ! The 2D array to save | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! real(sp) :: data(3, 2) | ||
! call savetxt("log.txt", data) | ||
|
||
integer :: s, i | ||
open(newunit=s, file=filename, status="replace") | ||
do i = 1, size(d, 1) | ||
write(s, *) d(i, :) | ||
end do | ||
close(s) | ||
end subroutine | ||
|
||
subroutine dsavetxt(filename, d) | ||
|
@@ -115,6 +195,70 @@ subroutine dsavetxt(filename, d) | |
close(s) | ||
end subroutine | ||
|
||
subroutine qsavetxt(filename, d) | ||
! Saves a 2D array into a textfile. | ||
! | ||
! Arguments | ||
! --------- | ||
! | ||
character(len=*), intent(in) :: filename ! File to save the array to | ||
real(qp), intent(in) :: d(:,:) ! The 2D array to save | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! real(dp) :: data(3, 2) | ||
! call savetxt("log.txt", data) | ||
|
||
integer :: s, i | ||
open(newunit=s, file=filename, status="replace") | ||
do i = 1, size(d, 1) | ||
write(s, *) d(i, :) | ||
end do | ||
close(s) | ||
end subroutine | ||
|
||
|
||
!PRIVATE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this does a similar thing as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
integer function number_of_columns(s) | ||
! determine number of columns | ||
integer,intent(in)::s | ||
|
||
integer :: ios | ||
character :: c | ||
logical :: lastwhite | ||
|
||
rewind(s) | ||
number_of_columns = 0 | ||
lastwhite = .true. | ||
do | ||
read(s, '(a)', advance='no', iostat=ios) c | ||
if (ios /= 0) exit | ||
if (lastwhite .and. .not. whitechar(c)) number_of_columns = number_of_columns + 1 | ||
lastwhite = whitechar(c) | ||
end do | ||
rewind(s) | ||
|
||
end function | ||
|
||
integer function number_of_rows_numeric(s) | ||
! determine number or rows | ||
integer,intent(in)::s | ||
integer :: ios | ||
|
||
real::r | ||
|
||
rewind(s) | ||
number_of_rows_numeric = 0 | ||
do | ||
read(s, *, iostat=ios) r | ||
if (ios /= 0) exit | ||
number_of_rows_numeric = number_of_rows_numeric + 1 | ||
end do | ||
|
||
rewind(s) | ||
|
||
end function | ||
|
||
logical function whitechar(char) ! white character | ||
! returns .true. if char is space (32) or tab (9), .false. otherwise | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space after
,
and no space before?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected