Silverfrost Forums

Welcome to our forums

Write array to X,Y formatted file

4 Jun 2020 9:17 #25559

Hello,

I am trying to write X,Y values to a file from a 2D array, but the data does not output correctly in 2 columns for X and Y. At present in writes X as 2 columns and then Y appended on the end. How to write X to col 1 and Y to col 2. Sorry if this is a basic query - using ftn95.

program linear_test

! Purpose: create X and Y 2D arrays
! write file with 2 columns X,Y of 120x120 (14400) values
! z-data has same geometry
implicit none

real :: dxy, xmin, xmax, xrange, ymin, ymax, yrange
integer :: npoints, i, sv, nx, ny, xydim
real,allocatable :: xx(:), yy(:)
real, allocatable :: X(:,:), Y(:,:)

open (25, file='xy-data.dat', status='new') ! open new file for write
npoints=120

allocate(xx(npoints))
xmin = -5.0
xmax = -3.0
allocate(yy(npoints))
ymin = -5.0
ymax = -3.0

xrange = xmax - xmin
!yrange = ymax - ymin
! Linspace
dxy = xrange / real(npoints-1) ! calculate increment

    do i = 1,npoints
       xx(i) = xmin + (i-1)*dxy
       yy(i) = ymin + (i-1)*dxy !same increment in y
    end do
	
sv=0
nx = size(xx, dim=1)
ny = size(yy, dim=1)
xydim = nx*ny
allocate(X(ny,nx),Y(ny,nx),stat=sv)
allocate(X1(xydim),Y1(xydim),stat=sv)

! Meshgrid
X(1,:) = xx
X(2:ny,:) = spread(xx, dim=1, ncopies=ny-1)
Y(:,1) = yy
Y(:,2:nx) = spread(yy, dim=2, ncopies=nx-1)

! write 2 columns (X,Y)
write (25,'(f15.10,f15.10)') X, Y

deallocate(xx,yy,X,Y)
close(25)
end program linear_test

The code is fine if I just want X or Y and generates the 14400 values expected.

Thanks for any pointers

Lester

4 Jun 2020 9:33 #25560

The IOlist in the WRITE statement that you wrote is not suitable for your desired output. Instead, use

write (25,'(f15.10,f15.10)') (X(i), Y(i), i=1, npoints)
4 Jun 2020 10:02 #25562

Many thanks for the pointers. The X and Y are 2D arrays so just had to tweak the line:

write (25,'(f15.10,f15.10)') (X(i,:), Y(:,i), i=1, npoints)

Currently re-learning Fortran 95; been a while!

Quoted from mecej4 The IOlist in the WRITE statement that you wrote is not suitable for your desired output. Instead, use

write (25,'(f15.10,f15.10)') (X(i), Y(i), i=1, npoints)
4 Jun 2020 10:20 #25563

Right, I overlooked the fact that you had X as well as XX as variables (also Y and YY).

Your modified WRITE statement will still print: nx elements of X(1,:), two per line, ny values of Y(:,1), two per line, and repeats this pattern for i = 2:npoints. This may not be what you wanted.

10 Jun 2020 7:59 #25604

'write (25,'(f15.10,f15.10)') X, Y'

This is equivalent to: write (25,'(f15.10,f15.10)') (X(i,j),i=1,ny,j=1,nx), (Y(i,j),i=1,ny,j=1,nx)

If you don't like it then re-order the implied do in the write. I presume you wanted: write (25,'(f15.10,f15.10)') ( X(i,j),Y(i,j), i=1,ny, j=1,nx)

I find an explicit do is more flexible. Not a very readable list though. Perhaps the following would be more useful. write (25,'(2i5,2f15.10)') ( i,j,X(i,j),Y(i,j), i=1,ny, j=1,nx)

For Meshgrid, you could write: do i = 1,nx X(:,i) = xx(i) Y(:,i) = yy(:) end do

15 Jun 2020 1:52 #25665

Quoted from John-Silver Even Fortran 77 satisfies the vast majority of Fortraneers, especially those of Anglo-Romanic descent 😉

Don't think many would agree with that. No Allocate! or System_Clock and lots of others.

Yes, I was wrong with my brackets !! It should be: write (25,'(f15.10,f15.10)') ( ( X(i,j),Y(i,j), i=1,ny ), j=1,nx )

'write (25,'(f15.10,f15.10)') ( ( X(i,j),Y(i,j) ) , i=1,ny, j=1,nx)' is no go either. It could become write (25,'(f15.10,f15.10)') ( ( X(i,j) ) , i=1,ny, j=1,nx)

25 Jun 2020 3:00 #25799

John S., you wrote

write (25,'(f15.10,f15.10)') ( ( X(i,j),Y(i,j) ) , i=1,ny, j=1,nx)

The extra pair of parentheses is a no-no because within those parentheses are present two expressions with a comma in between. The expression (E1,E2), where E1 and E2 are constants of numeric type, is a complex constant. When E1 or E2 is not a literal constant, CMPLX(E1,E2) would give you a complex valued expression. Without CMPLX, what does (E1, E2) mean? In Fortran the integrity of parentheses in expressions has to be guarded, so the parentheses must have a purpose, but what the dickens is it? What would the following mean:

write (25,'(f15.10,f15.10)') ( ( X(i,j),Y(i,j), Z(i,j) ) , i=1,ny, j=1,nx)

Apart from the issue of extra parentheses, what about the implied DO loops? Is the i-loop inside the j-loop, or the other way?

You also wrote:

but I'm sure they have their reasoning for their sometimes counter-intuitive decisions

Relying on intuition or Anglo-Romanic descent to resolve this question in this narrow context is almost certain to create huge problems in other parts of the language. Most of us are not far-sighted enough to see through the ramifications of changing the language just to mollify an itch of the moment.

Please login to reply.