replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Write array to X,Y formatted file
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Write array to X,Y formatted file

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
arctica



Joined: 10 Sep 2006
Posts: 146
Location: United Kingdom

PostPosted: Thu Jun 04, 2020 10:17 am    Post subject: Write array to X,Y formatted file Reply with quote

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.

Code:

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
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Thu Jun 04, 2020 10:33 am    Post subject: Reply with quote

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

Code:
write (25,'(f15.10,f15.10)') (X(i), Y(i), i=1, npoints)
Back to top
View user's profile Send private message
arctica



Joined: 10 Sep 2006
Posts: 146
Location: United Kingdom

PostPosted: Thu Jun 04, 2020 11:02 am    Post subject: Re: Reply with quote

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

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


Currently re-learning Fortran 95; been a while!

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

Code:
write (25,'(f15.10,f15.10)') (X(i), Y(i), i=1, npoints)
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Thu Jun 04, 2020 11:20 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Wed Jun 10, 2020 8:59 am    Post subject: Reply with quote

"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:
Code:
do i = 1,nx
  X(:,i) = xx(i)
  Y(:,i) = yy(:)
end do
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Mon Jun 15, 2020 2:52 pm    Post subject: Re: Reply with quote

John-Silver wrote:
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)
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Thu Jun 25, 2020 4:00 pm    Post subject: Reply with quote

John S., you wrote
Quote:
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:
Code:
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:

Quote:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group