Fortran Comparing line in one file to multiple lines in another/FORTRAN

AI Thread Summary
The discussion revolves around comparing two files line by line, specifically aiming to find matches where the difference between coordinates is less than 0.003. The initial code provided attempts to compare one line from the first file against all lines in the second file, but it lacks the necessary structure for proper comparison. Key points include the need for declaring arrays for the coordinate variables, as the current implementation uses indices without proper array declarations. Additionally, the suggestion is made to read the entire data from one file into an array for efficient comparison, while the other file can be read line by line. The importance of including 'end do' statements and file rewind commands is also highlighted to ensure the program can iterate through the files correctly. Overall, the focus is on optimizing the code for effective data comparison.
maria4376
Messages
2
Reaction score
0
Hi y'all

I am trying to compare two files, so make one line from one file be compared to alll the lines in the other file until it finds a match where the difference is less than 0.003.

This what I had thought of, but I still think it is just comparing line by line, instead of one line to all the other lines. Any suggestions would be great!

This is what I have so far:


program matchCoords
implicit none

real :: xcoord1, ycoord1,x1,y1,mag1,merr1,sharp1,chi1
real :: xcoord2, ycoord2,x2,y2,mag2,merr2,sharp2,chi2
real, parameter :: arcsec = 0.0003
character (len=50) :: inputfile1, inputfile2, outputfile
integer :: i

!Open input file as unit 15
write(*,*) 'Enter inputfile #1:'
read(*,*) inputfile1
write(*,*) 'Enter inputfile #2'
read(*,*) inputfile2
write(*,*) 'Enter output file name:'
read(*,*) outputfile
open(unit=15, file=inputfile1, status='old')
open(unit=16, file=inputfile2, status='old')
open(unit=17, file=outputfile, status='new')

do i=0,37200
read(16,*) xcoord2, ycoord2,x2,y2,mag2,merr2,sharp2,chi2
do j=0,37000
read(15,*) xcoord1(j), ycoord1(j),x1,y1,mag1,merr1,sharp1,chi1

if(ABS(xcoord1(j)-xcoord2).le.arcsec.and.ABS(ycoord1(i)-ycoor&
&d2).le.arcsec) then

write(17,*) xcoord1(j),ycoord1(j),x1,y1,mag1,merr1,&
&sharp1,chi1,xcoord2,ycoord2,x2,y2,&
&mag2,merr2,sharp2,chi2
 
Technology news on Phys.org
You have not declared any arrays, yet, you are using indices with xcoord1 and ycoord1.

I don't see the 'end do' lines nor the file rewind...from what I can see, once you go thru the inner loop once (and you get to the end of that file), you will not be able to do that again without a rewind on that file.

I think it is best to simply read the entire array of data, at least, the one that is going to be in the inner loop; in the outer loop, you can get away by just reading one line at a time until the end of file.

So declare arrays for your xxxx1 variables long enough to read the entire file; then, go ahead and do the nested do loop...there will only be need to read a line in the outer loop...in the inner one, you can skip the read statement and go straight to the comparison (using array indices).
 
Thank you so much, gsal, for taking the timel! I will try that today!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
16
Views
4K
Replies
5
Views
5K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
2
Views
2K
Replies
5
Views
6K
Replies
3
Views
3K
Replies
5
Views
4K
Back
Top