Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Last active March 30, 2019 09:45
Show Gist options
  • Select an option

  • Save nilforooshan/2a42ff4c6554ba707076197325e7a5ef to your computer and use it in GitHub Desktop.

Select an option

Save nilforooshan/2a42ff4c6554ba707076197325e7a5ef to your computer and use it in GitHub Desktop.
f90: Sort and merge two numeric files by their first column *.exe and *.out are the Windows and Linux executables.
PROGRAM srt_mrg
IMPLICIT NONE
! Declarations
INTEGER:: e, i, j, k, r1, r2, c1, c2, na, rep1, rep2, error1, error2
INTEGER,DIMENSION(:,:),ALLOCATABLE:: a, b, f
INTEGER,DIMENSION(:),ALLOCATABLE:: c, d
CHARACTER(20):: infile1, infile2, outfile, exiit
! Opening prints
PRINT*,
PRINT*,
PRINT*, 'This program is written by Mohammad A. Nilforooshan.'
PRINT*, 'All rights reserved.'
PRINT*, 'http://sites.google.com/site/mannprofile/'
! Ask for the first input file, open and check it
PRINT*,
PRINT*,
PRINT*, 'Type the name of the first input file.'
READ*, infile1
OPEN(UNIT=10, FILE=infile1, STATUS='OLD', IOSTAT=error1)
IF (error1/=0) THEN
PRINT*,
PRINT*,
PRINT*, 'Error reading the input file'
STOP
END IF
! Ask for the size of the first matrix
PRINT*,
PRINT*,
PRINT*, 'Insert the number of rows.'
READ*, r1
PRINT*, 'Insert the number of columns.'
READ*, c1
! Ask for the second input file, open and check it
PRINT*,
PRINT*,
PRINT*, 'Type the name of the second input file.'
READ*, infile2
OPEN(UNIT=11, FILE=infile2, STATUS='OLD', IOSTAT=error2)
IF (error2/=0) THEN
PRINT*,
PRINT*,
PRINT*, 'Error reading the input file'
STOP
END IF
! Ask for the size of the second matrix
PRINT*,
PRINT*,
PRINT*, 'Insert the number of rows.'
READ*, r2
PRINT*, 'Insert the number of columns.'
READ*, c2
! Read the first matrix
ALLOCATE(a(r1,c1))
READ(10,*) ((a(i,j),j=1,c1),i=1,r1)
! Read the second matrix
ALLOCATE(b(r2,c2))
READ(11,*) ((b(i,j),j=1,c2),i=1,r2)
! Ask for the missing value
PRINT*,
PRINT*,
PRINT*, 'How do you define missing value (an integer value)?'
READ*, na
! Concatenate the first columns from the two files in a vector
ALLOCATE(c(r1+r2))
DO i=1,r1
c(i)=a(i,1)
END DO
DO i=r1+1,r1+r2
c(i)=b(i-r1,1)
END DO
! Sort the vector ascendingly
DO i=2,r1+r2
k=c(i)
DO j=i-1,1,-1
IF (c(j)<=k) GO TO 105
c(j+1)=c(j)
END DO
j=0
105 c(j+1)=k
END DO
! Find the number of repeatitions
rep1=0
DO i=2,r1+r2
IF (c(i)==c(i-1)) THEN
rep1=rep1+1
END IF
END DO
! Exclude the repeated observations
ALLOCATE(d(r1+r2-rep1))
rep2=0
DO i=1,r1+r2-rep1
d(i)=c(i+rep2)
e=0
DO k=i+rep2,r1+r2-1
IF (c(k+1)==c(k)) THEN
e=e+1
ELSE
GO TO 134
END IF
END DO
134 rep2=rep2+e
END DO
! Create the output matrix
ALLOCATE(f(r1+r2-rep1,c1+c2-1))
DO i=1,r1+r2-rep1
f(i,1)=d(i)
DO j=2,c1+c2-1
f(i,j)=na
END DO
END DO
DO i=1,r1+r2-rep1
DO k=1,r1
IF (f(i,1)==a(k,1)) THEN
DO j=2,c1
f(i,j)=a(k,j)
END DO
GO TO 156
END IF
156 END DO
END DO
DO i=1,r1+r2-rep1
DO k=1,r2
IF (f(i,1)==b(k,1)) THEN
DO j=c1+1,c1+c2-1
f(i,j)=b(k,j-c1+1)
END DO
GO TO 167
END IF
167 END DO
END DO
! Ask for the name of the output file
PRINT*,
PRINT*,
PRINT*, 'Type the name of the output file.'
READ*, outfile
! Write in the output file
OPEN(UNIT=12, FILE=outfile, STATUS='UNKNOWN')
DO i=1,r1+r2-rep1
WRITE(12,*) (f(i,j), j=1,c1+c2-1)
END DO
! Finished
PRINT*,
PRINT*,
PRINT*, 'Press any key to exit.'
READ*, exiit
IF (exiit=='a') THEN
GO TO 195
END IF
195 END PROGRAM srt_mrg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment