从更新 Fortran 中的 dat 文件读取数据
Read data from updating dat file in Fortran
我有一个 dat 格式的代码输出。该文件具有以下格式
Text
Text
Text
Text
3241234234
234234
23423423
34123424
1324234
iteration pressure temperature density
1 1234 312 2.12
2 1235 321 2.15
3 1234 312 2.12
4 1235 321 2.15
5 1234 312 2.12
6 1235 321 2.15
pressure temperature density
7 1234 312 2.12
8 1235 321 2.15
9 1234 312 2.12
10 1235 321 2.15
11 1234 312 2.12
warning pressure update is not linked
12 1235 321 2.15
pressure temperature density
13 1234 312 2.12
14 1235 321 2.15
15 1234 312 2.12
warning pressure update is not linked
16 1235 321 2.15
17 1234 312 2.12
18 1235 321 2.15
end of iterations
simulation time
end loop
end of code
我写了一段代码,可以在其中打开 dat 文件。使用 iostat 将其作为文本阅读。然后我跳过 header 文本行和随机数等直到迭代行。然后读取数字(迭代压力温度密度)。但是我被困在几个需要帮助的地方。
开头的文本行不是固定的。有时这些是 4 行,有时是五六行。每次都要调整行数,重新编译。有没有办法自动化这个。我的意思是代码本身会计算文本行并跳过它们。同样是下一个随机数。
我想做的是跳过从开始到迭代的行。但这些也在发生变化。仅读取数值数据
1 1234 312 2.12
2 1235 321 2.15
3 1234 312 2.12
4 1235 321 2.15
5 1234 312 2.12
6 1235 321 2.15
7 1234 312 2.12
8 1235 321 2.15
9 1234 312 2.12
10 1235 321 2.15
11 1234 312 2.12
12 1235 321 2.15
13 1234 312 2.12
14 1235 321 2.15
15 1234 312 2.12
16 1235 321 2.15
17 1234 312 2.12
18 1235 321 2.15
然后将此数据写入输出文件。
以下代码可以满足您的要求。此代码假定数据存储在 input.dat 中,并将输出打印到 output.dat。逻辑很简单。首先,读取行,直到检测到以关键字 WORD_OF_INTEREST 开头的行(此处为 iteration)。然后我们开始读取剩余的行,假设每行有 4 个字段(即迭代、压力温度密度)。不遵守此模式的行将被跳过。
评论将帮助您理解算法。
program readData
implicit none
integer, parameter :: wp = selected_real_kind(15) ! double-precision floats
integer, parameter :: ip = selected_int_kind(8) ! long integers
character (len = *), parameter :: &
WORD_OF_INTEREST = 'iteration'
integer (kind = ip), parameter :: &
LENGTH_WORD = len(WORD_OF_INTEREST), &! calculate length of the word you are trying to find; that is, "iteration"
NO_READING_ERROR = 0, &
END_OF_FILE = -1
integer (kind = ip) :: &
handleFileInput, &
handleFileOutput, &
iteration, &
idError
real (kind = wp) :: &
pressure, &
temperature, &
density
character (len = LENGTH_WORD) :: &
line
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! Start executable section
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! Open the input and output files
open(newUnit = handleFileInput, file = 'input.dat')
open(newUnit = handleFileOutput, file = 'output.dat')
! Read data file until a line starting with WORD_OF_INTEREST is encountered
do
read(handleFileInput, *) line
if (line == WORD_OF_INTEREST) exit
end do
! Read the rest of the file
do
read(handleFileInput, *, iostat = idError) iteration, pressure, temperature, density
! Handle different types of errors
if (idError == NO_READING_ERROR) then
write(handleFileOutput, *) iteration, pressure, temperature, density
else if (idError == END_OF_FILE) then
exit
else
continue
end if
end do
close(handleFileInput)
close(handleFileOutput)
end program readData
我有一个 dat 格式的代码输出。该文件具有以下格式
Text
Text
Text
Text
3241234234
234234
23423423
34123424
1324234
iteration pressure temperature density
1 1234 312 2.12
2 1235 321 2.15
3 1234 312 2.12
4 1235 321 2.15
5 1234 312 2.12
6 1235 321 2.15
pressure temperature density
7 1234 312 2.12
8 1235 321 2.15
9 1234 312 2.12
10 1235 321 2.15
11 1234 312 2.12
warning pressure update is not linked
12 1235 321 2.15
pressure temperature density
13 1234 312 2.12
14 1235 321 2.15
15 1234 312 2.12
warning pressure update is not linked
16 1235 321 2.15
17 1234 312 2.12
18 1235 321 2.15
end of iterations
simulation time
end loop
end of code
我写了一段代码,可以在其中打开 dat 文件。使用 iostat 将其作为文本阅读。然后我跳过 header 文本行和随机数等直到迭代行。然后读取数字(迭代压力温度密度)。但是我被困在几个需要帮助的地方。
开头的文本行不是固定的。有时这些是 4 行,有时是五六行。每次都要调整行数,重新编译。有没有办法自动化这个。我的意思是代码本身会计算文本行并跳过它们。同样是下一个随机数。
我想做的是跳过从开始到迭代的行。但这些也在发生变化。仅读取数值数据
1 1234 312 2.12 2 1235 321 2.15 3 1234 312 2.12 4 1235 321 2.15 5 1234 312 2.12 6 1235 321 2.15 7 1234 312 2.12 8 1235 321 2.15 9 1234 312 2.12 10 1235 321 2.15 11 1234 312 2.12 12 1235 321 2.15 13 1234 312 2.12 14 1235 321 2.15 15 1234 312 2.12 16 1235 321 2.15 17 1234 312 2.12 18 1235 321 2.15
然后将此数据写入输出文件。
以下代码可以满足您的要求。此代码假定数据存储在 input.dat 中,并将输出打印到 output.dat。逻辑很简单。首先,读取行,直到检测到以关键字 WORD_OF_INTEREST 开头的行(此处为 iteration)。然后我们开始读取剩余的行,假设每行有 4 个字段(即迭代、压力温度密度)。不遵守此模式的行将被跳过。
评论将帮助您理解算法。
program readData
implicit none
integer, parameter :: wp = selected_real_kind(15) ! double-precision floats
integer, parameter :: ip = selected_int_kind(8) ! long integers
character (len = *), parameter :: &
WORD_OF_INTEREST = 'iteration'
integer (kind = ip), parameter :: &
LENGTH_WORD = len(WORD_OF_INTEREST), &! calculate length of the word you are trying to find; that is, "iteration"
NO_READING_ERROR = 0, &
END_OF_FILE = -1
integer (kind = ip) :: &
handleFileInput, &
handleFileOutput, &
iteration, &
idError
real (kind = wp) :: &
pressure, &
temperature, &
density
character (len = LENGTH_WORD) :: &
line
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! Start executable section
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! --------------------------------------------------------------------------------------------------
! Open the input and output files
open(newUnit = handleFileInput, file = 'input.dat')
open(newUnit = handleFileOutput, file = 'output.dat')
! Read data file until a line starting with WORD_OF_INTEREST is encountered
do
read(handleFileInput, *) line
if (line == WORD_OF_INTEREST) exit
end do
! Read the rest of the file
do
read(handleFileInput, *, iostat = idError) iteration, pressure, temperature, density
! Handle different types of errors
if (idError == NO_READING_ERROR) then
write(handleFileOutput, *) iteration, pressure, temperature, density
else if (idError == END_OF_FILE) then
exit
else
continue
end if
end do
close(handleFileInput)
close(handleFileOutput)
end program readData