I want to write an 3 x 4 matrix from a 12 line txt numerical file
I have written a Fortran 90 program for the same
program array
implicit none
integer, parameter :: I4B = selected_int_kind(4)
integer (I4B), allocatable, dimension (:,:) :: arr
integer (I4B) :: i
open(unit=99, file='1.txt')
open(unit=100, file='out.txt')
Allocate (arr(3,4))
do i=1, 12
read(99,*)arr(3,4)
write(100,*),arr(3,4)
enddo
close (99)
deAllocate (arr)
stop
endprogram array
but it's giving an error
At line 10 of file array.f90 (unit = 99, file = '1.txt')
Fortran runtime error: End of file
Line number 10 is read(99,*)arr(3,4).
Here's a very simple implementation of your array. It uses the fact that the first index is the fastest changing. So I just keep reading until all 12 elements of the array are filled.
Then, for output, I specify a format that it should write 3 values per line.
program readfile
implicit none
integer, dimension(:, :), allocatable :: arr
open(unit=99, file='1.txt', action='READ', status='OLD')
open(unit=100, file='out.txt', action='WRITE', status='NEW')
allocate(arr(3, 4))
read(99, *) arr
write(100, '(3I4)') arr
close(99)
close(100)
end program readfile
If you want to do it explicitly, you have to calculate the two indices independently for each value read:
program readfile
implicit none
integer, dimension(:, :), allocatable :: arr
integer :: i, row, col
open(unit=99, file='1.txt', action='READ', status='OLD')
open(unit=100, file='out.txt', action='WRITE', status='NEW')
allocate(arr(3, 4))
! Read the elements:
do i = 1, 12
row = mod(i-1, 3)+1
col = (i-1) / 3 + 1
read(99, *) arr(row, col)
end do
! write the elements:
do i = 1, 4
write(100, '(3I4)') arr(:, i)
end do
close(99)
close(100)
end program readfile
By the way, your code:
do i = 1, 12
read(99, *) arr(3, 4)
write(100, *) arr(3, 4)
end do
would just 12 times read a single number from the input file, store it in the last location of the array, then write that same number back to the output file.
Also, your error message suggests that you have tried to read past the end of the file. Either your 1.txt doesn't contain 12 lines, or you might have read something else first, for example to find out how many elements there are. In that case, you would need to add a rewind(99) before you start reading the actual numbers.
Related
I haven't done any Fortran programming for year and it seems I'm rather rusty now. So, I won't provide you with all my failed attempts but will humbly ask you to help me with the following.
I've got the following "input" file
1 5 e 4
A b & 1
c Z ; b
y } " N
t r ' +
It can have more columns and/or rows. I would now like to assign each of these ASCII characters to arrays x(i,j) so that I can process them further after ICHAR conversions. In this example i=1,4, j=1,5, but it can be any No depending on the input file. The simplest example
PROGRAM Example
integer :: i, j
CHARACTER, ALLOCATABLE, DIMENSION(:,:) :: A
READ *, A
ALLOCATE (A(i,j))
PRINT *, A
END PROGRAM Example
compiles (Example.f95) but
cat input | ./Example.f95
does not give any output.
I would greatly appreciate an advice on how to import the afore-mentioned strings into the program as x(i,j) terms of an array.
In Fortran, it's always best to know in advance how big your arrays need to be. I understand that in your case you can't know.
Assuming that your input is at least formatted correctly (i.e. the columns match up and have only a single space in between them), I've created a code that should in theory be able to read them in an arbitrary shape. (Not quite arbitrary, it assumes that there are fewer than 511 columns.)
It uses two ways:
It simply reads the first line in at once (1024 characters, hence the 511 limit on columns) then calculates from the length the number of columns
It then allocates an array with a guessed number of rows, and once it notices that the guess was too small, it creates a new allocation with double the number of rows. It then uses the move_alloc command to swap the allocations.
To find when it should end reading the values, it simply checks whether the read returns the IOSTAT_END error code.
Here's the code:
program read_input
use iso_fortran_env, only: IOSTAT_END
implicit none
character, dimension(:,:), allocatable :: A, A_tmp
character(len=1024) :: line ! Assumes that there are never more than 500 or so columns
integer :: i, ncol, nrow, nrow_guess
integer :: ios
character :: iom
! First, read the first line, to see how many columns there are
read(*, '(A)', iostat=ios, iomsg=iom) line
call iocheck('read first line', ios, iom)
ncol = (len_trim(line) + 1) / 2
! Let's first allocate memory for two rows, we can make it more later.
nrow_guess = 2
allocate(A(ncol, nrow_guess))
! Instead of standard input, we're reading from the line we read before.
read(line, '(*(A1,X))', iostat=ios, iomsg=iom) A(:, 1)
call iocheck('read first line into vals', ios, iom)
! Now loop over all the rows
nrow = 1
read_loop: do
if (nrow >= nrow_guess) then ! We have guessed too small.
! This is a bit convoluted, but the best
! way to increase the array shape.
nrow_guess = nrow_guess * 2
allocate(A_tmp(ncol, nrow_guess))
A_tmp(:, 1:nrow_guess/2) = A(:,:)
call move_alloc(A_tmp, A)
end if
read(*, '(*(A1,X))', iostat = ios, iomsg=iom) A(:, nrow+1)
if (ios == IOSTAT_END) exit read_loop ! We're done reading.
call iocheck('read line into vals', ios, iom)
nrow = nrow + 1
end do read_loop
! The last guess was probably too large,
! let's move it to an array of the correct size.
if (nrow < nrow_guess) then
allocate(A_tmp(ncol, nrow))
A_tmp(:,:) = A(:, 1:nrow)
call move_alloc(A_tmp, A)
end if
! To show we have all values, print them out.
do i = 1, nrow
print '(*(X,A))', A(:, i)
end do
contains
! This is a subroutine to check for IO Errors
subroutine iocheck(op, ios, iom)
character(len=*), intent(in) :: op, iom
integer, intent(in) :: ios
if (ios == 0) return
print *, "IO ERROR"
print *, "Operation: ", op
print *, "Message: ", iom
end subroutine iocheck
end program read_input
Edited to add
I had trouble with the special characters in your example input file, otherwise I'd just have made a read(*, *) A(:, nrow) -- but that messed the special characters up. That's why I chose the explicit (*(A1, X)) format. Of course that messes up when your characters don't start at the first position in the line.
You need to read the first line and determine how characters there in the line. Then read the entire file to determine the number of lines. Allocate the 2D array to hold characters. Then read the file and parse each line into the 2D array. There are more elegant ways of doing this, but here you go
program foo
implicit none
character(len=:), allocatable :: s
character, allocatable :: a(:,:)
integer fd, i, j, n, nr, nc
!
! Open file for reading
!
open(newunit=fd, file='tmp.dat', status='old', err=9)
!
! Determine number of characters in a row. Assumes all rows
! are of the same length.
!
n = 128
1 if (allocated(s)) then
deallocate(s)
n = 2 * n
end if
allocate(character(len=n) :: s)
read(fd,'(A)') s
if (len_trim(s) == 128) goto 1
s = adjustl(s)
n = len_trim(s)
deallocate(s)
!
! Allocate a string of the correct length.
!
allocate(character(len=n) :: s)
!
! Count the number of rows
!
rewind(fd)
nr = 0
do
read(fd,*,end=2)
nr = nr + 1
end do
!
! Read file and store individual characters in a(:,:)
!
2 rewind(fd)
nc = n / 2 + 1
allocate(a(nr,nc))
do i = 1, nr
read(fd,'(A)') s
do j = 1, nc
a(i,j) = s(2*j-1:2*j-1)
end do
end do
close(fd)
write(s,'(I0)') nc
s = '(' // trim(s) // '(A,1X))'
do i = 1, nr
write(*,s) a(i,:)
end do
stop
9 write(*,'(A)') 'Error: cannot open tmp.dat'
end program foo
Apparently, GOTO is verbotem, here. Here's an elegant solution.
program foo
implicit none
character, allocatable :: s(:), a(:,:)
integer fd, i, j, n, nr, nc
! Open file for reading
open(newunit=fd, file='tmp.dat', status='old', access='stream', err=9)
inquire(fd, size = n) ! Determine file size.
allocate(s(n)) ! Allocate space
read(fd) s ! Read the entire file
close(fd)
nr = count(ichar(s) == 10) ! Number of rows
nc = (count(ichar(s) /= 32) - nr) / nr ! Number of columns
a = reshape(pack(s, ichar(s) /= 32 .and. ichar(s) /= 10), [nc,nr])
a = transpose(a)
do i = 1, nr
do j = 1, nc
write(*,'(A,1X)',advance='no') a(i,j)
end do
write(*,*)
end do
stop
9 write(*,'(A)') 'Error: cannot open tmp.dat'
end program foo
If one has a file containing, for example:
1 2 3 4 5
6 7 8 9 10
11 12 13
14 15
16 17
18
19 20
How can one get the correct number of integers (in the given example, 20) from counting them in the file in Fortran?
Here's a little program wot I wrote for this problem. I've subjected it to a tiny battery of tests. It should be fairly clear what the program and subroutine are doing but if you want any explanation of what is going on, ask. If you spot any errors, fix them.
PROGRAM test
USE iso_fortran_env
IMPLICIT NONE
INTEGER :: cnt, filstat
CHARACTER(len=132) :: aline ! change the length if you want to
cnt = 0
OPEN(101,file='data.txt')
DO
READ(101,'(a132)',iostat=filstat) aline
IF (filstat/=0) EXIT
CALL get_int(cnt,aline)
END DO
WRITE(*,*) 'counted ', cnt, 'integers'
CLOSE(101)
CONTAINS
RECURSIVE SUBROUTINE get_int(cnt, str)
INTEGER, INTENT(inout) :: cnt
CHARACTER(*), INTENT(in) :: str
! Local variables
CHARACTER(len= 10), PARAMETER :: digits = '0123456789'
INTEGER :: d1, os, n
! First strip off any leading spaces
d1 = SCAN(str,digits)
IF (d1==0) THEN ! no digit was found
RETURN
END IF
! Read the first integer on the line
READ(str(d1:),*) n
cnt = cnt+1
! Figure out how many character positions to skip over
os = INT(LOG10(REAL(n)))+1
! Recurse
CALL get_int(cnt,str(d1+os+1:))
END SUBROUTINE get_int
END PROGRAM TEST
Interesting question.
Normally you should first tell us what you have tried so far. But anyway.
One solution that I came up with is to create an array that is clearly larger, set all of them to a value that is not valid, then read in from the file.
Make sure that you capture the iostat parameter, otherwise your program will crash.
Then by looking at the first occurrence of that value, you can deduce the size (and you have the values there already):
program read_data
implicit none
integer, dimension(100) :: d
integer :: s, err
d = -9999
open(unit=100, file='data.txt', action='READ', status='OLD')
read(100, *, iostat=err) d
s = 0
do
s = s + 1
if ((s == size(d)) .or. (d(s+1) == -9999)) exit
end do
if (s == size(d)) then
print *, "We might have missed some..."
end if
write(*, '(5I5)') d(1:s)
close(100)
end program read_data
Now this isn't a very good program. You're wasting memory (large array) and to scale up you have to change the code and re-compile.
I'll think about that a bit more later.
Is it possible in a modern Fortran compiler such as Intel Fortran to determine array strides at runtime? For example, I may want to perform a Fast Fourier Transform (FFT) on an array section:
program main
complex(8),allocatable::array(:,:)
allocate(array(17, 17))
array = 1.0d0
call fft(array(1:16,1:16))
contains
subroutine fft(a)
use mkl_dfti
implicit none
complex(8),intent(inout)::a(:,:)
type(dfti_descriptor),pointer::desc
integer::stat
stat = DftiCreateDescriptor(desc, DFTI_DOUBLE, DFTI_COMPLEX, 2, shape(a) )
stat = DftiCommitDescriptor(desc)
stat = DftiComputeForward(desc, a(:,1))
stat = DftiFreeDescriptor(desc)
end subroutine
end program
However, the MKL Dfti* routines need to be explicitly told the array strides.
Looking through reference manuals I have not found any intrinsic functions which return stride information.
A couple of interesting resources are here and here which discuss whether array sections are copied and how Intel Fortran handles arrays internally.
I would rather not restrict myself to the way that Intel currently uses its array descriptors.
How can I figure out the stride information? Note that in general I would want the fft routine (or any similar routine) to not require any additional information about the array to be passed in.
EDIT:
I have verified that an array temporary is not created in this scenario, here is a simpler piece of code which I have checked on Intel(R) Visual Fortran Compiler XE 14.0.2.176 [Intel(R) 64], with optimizations disabled and heap arrays set to 0.
program main
implicit none
real(8),allocatable::a(:,:)
pause
allocate(a(8192,8192))
pause
call random_number(a)
pause
call foo(a(:4096,:4096))
pause
contains
subroutine foo(a)
implicit none
real(8)::a(:,:)
open(unit=16, file='a_sum.txt')
write(16, *) sum(a)
close(16)
end subroutine
end program
Monitoring the memory usage, it is clear that an array temporary is never created.
EDIT 2:
module m_foo
implicit none
contains
subroutine foo(a)
implicit none
real(8),contiguous::a(:,:)
integer::i, j
open(unit=16, file='a_sum.txt')
write(16, *) sum(a)
close(16)
call nointerface(a)
end subroutine
end module
subroutine nointerface(a)
implicit none
real(8)::a(*)
end subroutine
program main
use m_foo
implicit none
integer,parameter::N = 8192
real(8),allocatable::a(:,:)
integer::i, j
real(8)::count
pause
allocate(a(N, N))
pause
call random_number(a)
pause
call foo(a(:N/2,:N/2))
pause
end program
EDIT 3:
The example illustrates what I'm trying to achieve. There is a 16x16 contiguous array, but I only want to transform the upper 4x4 array. The first call simply passes in the array section, but it doesn't return a single one in the upper left corner of the array. The second call sets the appropriate stride and a subsequently contains the correct upper 4x4 array. The stride of the upper 4x4 array with respect to the full 16x16 array is not one.
program main
implicit none
complex(8),allocatable::a(:,:)
allocate(a(16,16))
a = 0.0d0
a(1:4,1:4) = 1.0d0
call fft(a(1:4,1:4))
write(*,*) a(1:4,1:4)
pause
a = 0.0d0
a(1:4,1:4) = 1.0d0
call fft_stride(a(1:4,1:4), 1, 16)
write(*,*) a(1:4,1:4)
pause
contains
subroutine fft(a) !{{{
use mkl_dfti
implicit none
complex(8),intent(inout)::a(:,:)
type(dfti_descriptor),pointer::desc
integer::stat
stat = DftiCreateDescriptor(desc, DFTI_DOUBLE, DFTI_COMPLEX, 2, shape(a) )
stat = DftiCommitDescriptor(desc)
stat = DftiComputeForward(desc, a(:,1))
stat = DftiFreeDescriptor(desc)
end subroutine !}}}
subroutine fft_stride(a, s1, s2) !{{{
use mkl_dfti
implicit none
complex(8),intent(inout)::a(:,:)
integer::s1, s2
type(dfti_descriptor),pointer::desc
integer::stat
integer::strides(3)
strides = [0, s1, s2]
stat = DftiCreateDescriptor(desc, DFTI_DOUBLE, DFTI_COMPLEX, 2, shape(a) )
stat = DftiSetValue(desc, DFTI_INPUT_STRIDES, strides)
stat = DftiCommitDescriptor(desc)
stat = DftiComputeForward(desc, a(:,1))
stat = DftiFreeDescriptor(desc)
end subroutine !}}}
end program
I'm guessing you get confused because you worked around the explicit interface of the MKL function DftiComputeForward by giving it a(:,1). This is contiguous and doesn't need an array temporary. It's wrong, however, the low-level routine will get the whole array and that's why you see that it works if you specify strides. Since the DftiComputeForward exects an array complex(kind), intent inout :: a(*), you can work by passing it through an external subroutine.
program ...
call fft(4,4,a(1:4,1:4))
end program
subroutine fft(m,n,a) !{{{
use mkl_dfti
implicit none
complex(8),intent(inout)::a(*)
integer :: m, n
type(dfti_descriptor),pointer::desc
integer::stat
stat = DftiCreateDescriptor(desc, DFTI_DOUBLE, DFTI_COMPLEX, 2, (/m,n/) )
stat = DftiCommitDescriptor(desc)
stat = DftiComputeForward(desc, a)
stat = DftiFreeDescriptor(desc)
end subroutine !}}}
This will create an array temporary though when going into the subroutine. A more efficient solution is then indeed strides:
program ...
call fft_strided(4,4,a,16)
end program
subroutine fft_strided(m,n,a,lda) !{{{
use mkl_dfti
implicit none
complex(8),intent(inout)::a(*)
integer :: m, n, lda
type(dfti_descriptor),pointer::desc
integer::stat
integer::strides(3)
strides = [0, 1, lda]
stat = DftiCreateDescriptor(desc, DFTI_DOUBLE, DFTI_COMPLEX, 2, (/m,n/) )
stat = DftiSetValue(desc, DFTI_INPUT_STRIDES, strides)
stat = DftiCommitDescriptor(desc)
stat = DftiComputeForward(desc, a)
stat = DftiFreeDescriptor(desc)
end subroutine !}}}
Tho routine DftiComputeForward accepts an assumed size array. If you pass something complicated and non-contiguous, a copy will have to be made at passing. The compiler can check at run-time if the copy is actually necessary or not. In any case for you the stride is always 1, because that will be the stride the MKL routine will see.
In your case you pass A(:,something), this is a contiguous section, provided A is contiguous. If A is not contiguous a copy will have to be made. Stride is always 1.
Some of the answers here do not understand the different between fortran strides and memory strides (though they are related).
To answer your question for future readers beyond the specific case you have here - there does not appear to be away to find an array stride solely in fortran, but it can be done via C using inter-operability features in newer compilers.
You can do this in C:
#include "stdio.h"
size_t c_compute_stride(int * x, int * y)
{
size_t px = (size_t) x;
size_t py = (size_t) y;
size_t d = py-px;
return d;
}
and then call this function from fortran on the first two elements of an array, e.g.:
program main
use iso_c_binding
implicit none
interface
function c_compute_stride(x, y) bind(C, name="c_compute_stride")
use iso_c_binding
integer :: x, y
integer(c_size_t) :: c_compute_stride
end function
end interface
integer, dimension(10) :: a
integer, dimension(10,10) :: b
write(*,*) find_stride(a)
write(*,*) find_stride(b(:,1))
write(*,*) find_stride(b(1,:))
contains
function find_stride(x)
integer, dimension(:) :: x
integer(c_size_t) :: find_stride
find_stride = c_compute_stride(x(1), x(2))
end function
end program
This will print out:
4
4
40
In short: assumed-shape arrays always have stride 1.
A bit longer: When you pass a section of an array to a subroutine which takes an assumed-shape array, as you have here, then the subroutine doesn't know anything about the original size of the array. If you look at the upper- and lower-bounds of the dummy argument in the subroutine, you'll see they will always be the size of the array section and 1.
integer, dimension(10:20) :: array
integer :: i
array = [ (i, i=10,20) ]
call foo(array(10:20:2))
subroutine foo(a)
integer, dimension(:) :: a
integer :: i
print*, lbound(a), ubound(a)
do i=lbound(a,1), ubound(a,2)
print*, a(i)
end do
end subroutine foo
This gives the output:
1 6
10 12 14 16 18 20
So, even when your array indices start at 10, when you pass it (or a section of it), the subroutine thinks the indices start at 1. Similarly, it thinks the stride is 1. You can give a lower bound to the dummy argument:
integer, dimension(10:) :: a
which will make lbound(a) 10 and ubound(a) 15. But it's not possible to give an assumed-shape array a stride.
Basically I am looking to enter X,Y pairs read from a file into arrays of length n where n is the number of lines(and thus x,y pairs) in the file. Unfortunately all my attempts at determining the length of the file then using that to set the size of the array have been unsuccessful. How can I accomplish this in Fortran 77? Hoping I am not missing something obvious, I am more used to Python and Java where this is rather trivial.
PS. Before asking this I looked around and it seemed that the general feeling was that you just set the size larger then you would expect it to be but that seems very memory wasteful and inefficient.
The solution is to use Fortran 90/95/2003/2008, which has the capabilities needed for your problem, while FORTRAN 77 doesn't. Read the file once to determine the number of data items. Rewind the file. Allocate the array of the required length. Read the file again, reading into the arrays.
Using Fortran 2003/2008 (not tested):
use iso_fortran_env
real :: xtmp, ytmp
real, dimension (:), allocatable :: x, y
integer :: i, n
integer :: Read_Code
open (unit=75, file=...)
n = 0
LengthLoop: do
read ( 75, *, iostat=Read_Code) xtmp, ytmp
if ( Read_Code /= 0 ) then
if ( Read_Code == iostat_end ) then
exit LengthLoop
else
write ( *, '( / "read error: ", I0 )' ) Read_Code
stop
end if
end if
n = n + 1
end do LengthLoop
allocate (x(n))
allocate (y(n))
rewind (75)
do i=1, n
read (75, *) x(i), y(i)
end do
close (75)
I try to implement a code that read in a number n, creates a vector to store n double precision numbers, read this number, call a subroutine printminmax() to find min and max. My code work perfect for normal numbers (integer,real etc) but when i have scientific notation (0.3412E+01) stack.Why? I thought with * read all the formats. Thanks
implicit none
integer, dimension(:), allocatable :: x
integer :: n
open (unit=77, file='input2.dat', action='read', status='old')
read(77,*), n
allocate(x(n))
call printminmax(n)
deallocate(x)
end
subroutine printminmax(y)
implicit none
integer, dimension(:), allocatable :: x
integer :: y,max,min,i
allocate(x(y))
read(77,*) x
!print *,'Maximun=', maxval(x)
!print *,'Minimun=', minval(x
!initialize the value max & min
max=x(1)
min=x(1)
do i=2,y
if (x(i)>max) max=x(i)
if (x(i)<min) min=x(i)
end do
write(*,*) 'Maximum=',max
write(*,*) 'Minimum=',min
end subroutine printminmax
one example of the stack input is
1000
5.39524398466520e-01
9.85099770130787e-01
7.38946122872518e-01
6.47771620257608e-01
8.80871051119695e-01
2.99375585725816e-02
the error that i take for scientific notation is
At line 13 of file io.f90 (unit = 77, file = 'input3.dat')
Fortran runtime error: Bad integer for item 1 in list input
ok i found it.I should have double precision on x, no integer.