计算Fortran循环中的行数 [英] Count lines within a Fortran loop
问题描述
我有一个时间序列数据,范围从0到15000行和几列.我想以60秒的间隔将数据分开,并计算每个间隔内的数据量.
I have a time series data that goes from 0 to 15000 lines and a few columns. I want to separate the data in intervals of 60 seconds and count the amount of data that are inside each interval.
我创建了一个do循环,该循环正确地创建了从开始到文件结尾的60s间隔,但是,我不知道如何计算每个间隔内的行数.
I created a do loop that correctly creates the intervals of 60s from the beggining to the end of the file, however, I don't know how to count the number of lines that are inside each interval.
我要寻找的最终结果是有两列: 间隔数和行数.
The end result that I'm looking for is to have two columns as a result: interval number and number of lines.
这是我到目前为止所得到的:
Here is what I got so far:
program flash_selection
implicit none
integer i, zero, qtd, cellnumb, flashcell, tipo, segment
real time, Etrig, xtrig, ytrig, ztrig, poscharge, negq, chargeneutr
open(1,file='my_file.txt',status='OLD')
read(1,*)
do
read(1,*,end=100) qtd, time, cellnumb, flashcell, tipo, segment, Etrig, xtrig, ytrig, ztrig, poscharge, negq, chargeneutr
do i = 1, 420
open(2,file='test.txt')
if (time .GE. (i-1)*60 .AND. time .LT. i*60) then
write(2,*) qtd, time, tipo, segment, Etrig, xtrig, ytrig, ztrig, poscharge, negq, chargeneutr
end if
end do
end do
IF语句根据我想要的内容正确地分隔了数据,剩下的唯一一件事就是知道每60个间隔的行数.
The IF statement correctly separates the data according to what I wanted, the only thing left is to know the amount of lines that there are on each interval of 60.
推荐答案
只需添加一个counter
变量.每次循环都增加counter
.如果IF测试成功,请打印出counter
,然后将其设置回零.
Just add a counter
variable. Increment counter
every time through the loop. When the IF test is successful, print out counter
and then set it back to zero.
(顺便说一句-不确定您是否要在循环中使用OPEN
语句.您应该使用大于10的文件编号.)
(BTW -- not sure you want your OPEN
statement inside the loops. And you should use file numbers greater than 10.)
这篇关于计算Fortran循环中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!