在fortran中解析输入文件 [英] parsing input file in fortran

查看:215
本文介绍了在fortran中解析输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的旧线程的延续。

我有一个来自不同代码的文件,我应该使用 parse 作为我的输入。
从它的片段看起来像:

I have a file from different code, that I should parse to use as my input. A snippet from it looks like:

GLOBAL SYSTEM PARAMETER 
NQ                 2 
NT                 2 
NM                 2 
IREL               3 
************************************* 
BEXT        0.00000000000000E+00 
SEMICORE  F 
LLOYD     F 
NE                32         0 
IBZINT             2 
NKTAB            936 
XC-POT    VWN       
SCF-ALG   BROYDEN2   
SCF-ITER          29 
SCF-MIX     2.00000000000000E-01 
SCF-TOL     1.00000000000000E-05 
RMSAVV      2.11362995016878E-06 
RMSAVB      1.25411205586140E-06 
EF          7.27534671479201E-01 
VMTZ       -7.72451391270293E-01 
************************************* 

等等。

目前我正在逐行阅读,如下所示:

Currently I am reading it line by line, as:

Program  readpot 
use iso_fortran_env 
Implicit None 
integer ::i,filestat,nq 
character(len=120):: rdline 
character(10)::key!,dimension(:),allocatable ::key 
real,dimension(:),allocatable ::val 
i=0 

open(12,file="FeRh.pot_new",status="old") 
readline:do 
  i=i+1 
  read(12,'(A)',iostat=filestat) rdline!(i) 

  if (filestat /= 0) then 
    if (filestat == iostat_end ) then 
      exit readline 
    else 
      write ( *, '( / "Error reading file: ", I0 )' )  filestat 
      stop 
    endif 
  end if 

  if (rdline(1:2)=="NQ") then 
    read(rdline(19:20),'(i)'),nq 
    write(*,*)nq 
  end if 
end do readline 

End Program  readpot 

所以,我必须读取每一行,手动找到相应的值列并写下(为了简洁起见,我只显示了一个值)。
我的问题是,这是 正确 的方式吗?还是有其他更简单的方法?请让我知道。

So, I have to read every line, manually find the value column corresponding to the key, and write that(For brevity, I have shown for one value only). My question is, is this the proper way of doing this? or there is other simpler way? Kindly let me know.

推荐答案

如果文件没有变化,你几乎不需要解析它。假设您已经为文件中所有感兴趣的数据项声明了变量,并且这些变量的名称显示在文件的行中。例如

If the file has no variability you scarcely need to parse it at all. Let's suppose that you have declared variables for all the interesting data items in the file and that those variables have the names shown on the lines of the file. For example

  INTEGER :: nq , nt, nm, irel
  REAL:: scf_mix, scf_tol  ! '-' not allowed in Fortran names
  CHARACTER(len=48) :: label, text
  LOGICAL :: semicore, lloyd
  ! Complete this as you wish

然后编写一个像这样的代码块

Then write a block of code like this

  OPEN(12,file="FeRh.pot_new",status="old") 
  READ(12,*) ! Not interested in the 1st line
  READ(12,*) label, nq
  READ(12,*) label, nt
  READ(12,*) label, nm
  READ(12,*) label, irel
  READ(12,*) ! Not interested in this line
  READ(12,*) label, bext
  READ(12,*) label, semicore
  ! Other lines to write
  CLOSE(12)

Fortran的 em>输入理解行中的空白以分隔值。它不会将这些空白作为字符变量的一部分来读取。这种行为可以改变,但在你不需要的情况下。请注意,当读入逻辑变量时,它还将字符 F 理解为 .false。。

Fortran's list-directed input understands blanks in lines to separate values. It will not read those blanks as part of a character variable. That behaviour can be changed but in your case you don't need to. Note that it will also understand the character F to mean .false. when read into a logical variable.

我的代码片段忽略了标签和解释行。如果你是一个紧张的处置,你可以处理它们,也许

My code snippet just ignores the labels and lines of explanation. If you are of a nervous disposition you could process them, perhaps

IF (label/='NE') STOP

或任何你想要的。

这篇关于在fortran中解析输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆