
forums.silverfrost.com Welcome to the Silverfrost forums

View previous topic :: View next topic 
Author 
Message 
arctica
Joined: 10 Sep 2006 Posts: 71 Location: United Kingdom

Posted: Thu Jun 04, 2020 10:17 am Post subject: Write array to X,Y formatted file 


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
! zdata 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='xydata.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(npoints1) ! calculate increment
do i = 1,npoints
xx(i) = xmin + (i1)*dxy
yy(i) = ymin + (i1)*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=ny1)
Y(:,1) = yy
Y(:,2:nx) = spread(yy, dim=2, ncopies=nx1)
! 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 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Thu Jun 04, 2020 10:33 am Post subject: 


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 


arctica
Joined: 10 Sep 2006 Posts: 71 Location: United Kingdom

Posted: Thu Jun 04, 2020 11:02 am Post subject: Re: 


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 relearning 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 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Thu Jun 04, 2020 11:20 am Post subject: 


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 


JohnCampbell
Joined: 16 Feb 2006 Posts: 2184 Location: Sydney

Posted: Wed Jun 10, 2020 8:59 am Post subject: 


"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 reorder 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 


JohnSilver
Joined: 30 Jul 2013 Posts: 1381 Location: Aerospace Valley

Posted: Sat Jun 13, 2020 7:07 pm Post subject: 


John C, these are useful tips to show the variety of write format orgnisations that are possible.
I'm surprisd that:
write (25,'(f15.10,f15.10)') ( X(i,j),Y(i,j), i=1,ny, j=1,nx)
as my (ill)logical brain would expect that the standards committee would have decided on :
Quote:  write (25,'(f15.10,f15.10)') ( ( X(i,j),Y(i,j) ) , i=1,ny, j=1,nx) 
but I'm sure they have their reasoning for their sometimes counterintuitivedecisions.
It might be difficult to prove that statement these days as F95 is now 530years old and many involved in the decisions made then might no longer be with us.
I saw the other day that Fortran 2018 is now the 'flavour of the day' (for want of a better expression) although it's apparently a minor update of Fortran 2008.
Maybe they're struggling to justify their existence in terms of their relevance, bearing in minf that Fortran (90/)95 does everything any engineer/scientific bod might conceivably need.
Even Fortran 77 satisfies the vast majority of Fortraneers, especially those of AngloRomanic descent _________________ ''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... " 

Back to top 


JohnCampbell
Joined: 16 Feb 2006 Posts: 2184 Location: Sydney

Posted: Mon Jun 15, 2020 2:52 pm Post subject: Re: 


JohnSilver wrote:  Even Fortran 77 satisfies the vast majority of Fortraneers, especially those of AngloRomanic 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 


mecej4
Joined: 31 Oct 2006 Posts: 1334

Posted: Thu Jun 25, 2020 4:00 pm Post subject: 


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 nono 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 iloop inside the jloop, or the other way?
You also wrote:
Quote:  but I'm sure they have their reasoning for their sometimes counterintuitive decisions 
Relying on intuition or AngloRomanic 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 farsighted enough to see through the ramifications of changing the language just to mollify an itch of the moment. 

Back to top 


JohnSilver
Joined: 30 Jul 2013 Posts: 1381 Location: Aerospace Valley

Posted: Fri Jun 26, 2020 5:23 pm Post subject: 


John C and mecej4, angloromanic was a feinted reference to our one and only LitusSaxonicum !!!!!!! ... .who for good lmeasure has coincidentally provided some of the proof of the pudding of my statement in an earlier post today http://forums.silverfrost.com/viewtopic.php?p=29398#29398
As for the brackets, mecej4, JohnC had already corrected mine as well as his dropoffs, we both had left little polly Nomial at the mercy of the dastardly Curly Pi ( https://wwwth.mpp.mpg.de/members/hahn/humour/PollyNomial.html ) _________________ ''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... " 

Back to top 




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
