![](/img/trans.png)
[英]Reading one variable from a text file as a function of two other variables in the same file in fortran
[英]Reading two files and printing same values in the new file in Fortran
我試圖從兩個文件中讀取值,並在新的(第三個)文件中只寫入相同的值。 有我可以使用的代碼或功能嗎? 我嘗試過的是使用DO
語句讀取兩個文件並將它們命名為variable1(i)
, variable2(j)
,但是我認為比較它們對我來說並不容易,這不是以variable1(i) = variable (j)
的方式工作variable1(i) = variable (j)
例子:
文件1:
a1,b1,c1,d1,e1,a2,b2,c2,d2,e2,.........
f(a1),f(b1),f(c1),.......
顯然我不知道函數,我只知道結果。
文件2:
e2,e2,c1,c1,c1,c1,a2,a1,..........
新文件3:
f(e2),f(e2),f(c1),f(c1),f(c1),f(a2),f(a1)......
這是我的代碼,發生錯誤是因為兩個文件中的數據數量不同
real*8 refjd(64285),pha(64285)
real*8 timejd(55436),epha(55436)
real*8 phs
format(47x,f10.2)
open(4,file="neic56.out")
do j=1,55436
read(4,55)timejd(j)
close(4)
format(f10.2,1x,f8.4)
open(3,file="74-17.out")
do i=1,64285
read(3,44)refjd(i),pha(i)
close(3)
end do
if(timejd(j) .EQ. refjd(i)) then
epha(j)=pha(i)
phs=epha(j)/360.
open(5,file="ejplphase.dat")
write(5,66)phs
end if
format(f6.4)
end do
end
可以完成這項工作的朴素算法如下:
program filesOut
implicit none
integer :: i, j
real*8 :: refjd(64285), pha(64285)
real*8 :: timejd(55436)
real*8 :: phs
100 format(47x,f10.2)
open(15, file="neic56.out", status='old')
do j=1,55436
read(15,100) timejd(j)
end do
close(15)
200 format(f10.2,1x,f8.4)
open(25,file="74-17.out", status='old')
do i=1,64285
read(25,200) refjd(i), pha(i)
end do
close(25)
300 format(f6.4)
open(35,file="ejplphase.dat", status='unknown')
outerloop: do i = 1, size(timejd)
do j = 1, size(refjd)
if (timejd(i) == refjd(j)) then
phs = pha(j)/360.
write(35,300) phs
cycle outerloop
end if
end do
end do outerloop
close(35)
end program
根據您的問題的規格,可以進行一些改進以提高效率。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.