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

Click For Summary
SUMMARY

The discussion focuses on comparing a single line from one file to multiple lines in another file using FORTRAN. The user aims to find matches where the difference between coordinates is less than 0.003 arcseconds. Suggestions include declaring arrays for the coordinate variables to facilitate efficient comparisons and avoiding repeated file reads in the inner loop. The proposed solution emphasizes reading the entire dataset into arrays for optimal performance.

PREREQUISITES
  • FORTRAN programming language knowledge
  • Understanding of file I/O operations in FORTRAN
  • Familiarity with floating-point precision and comparisons
  • Basic knowledge of array data structures
NEXT STEPS
  • Implement array declarations for coordinate variables in FORTRAN
  • Explore FORTRAN file handling techniques for efficient data processing
  • Learn about nested loops and their performance implications in FORTRAN
  • Study floating-point arithmetic and precision issues in scientific computing
USEFUL FOR

FORTRAN developers, data analysts, and researchers working with coordinate data comparisons and file processing in scientific applications.

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!
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K