如何从文本文件中读取数据并将部件/变量保存/写入不同的文本文件?

时间:2016-08-16 22:42:14

标签: fortran text-files fortran77

我对Fortran很新,所以这可能是一个天真的问题。我想读一个大的.txt文件,其中#行= 540001。数据来自声速风速计,速度和温度的测量值为u,v,w,T五个高度。以下是该文件的前几行:

"2011-07-10 09:30:00",9838,1.132,2.30225,-0.5635,29.18585,0.30275,0.689,-0.01125,29.67004,0.2165,-0.25475,0.12725,29.8923,0.51425,3.0405,-0.58375,29.5242,-0.0085,3.6235,-0.65175,29.61972,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
"2011-07-10 09:30:00.05",9839,-0.21325,3.22775,-0.17,29.10953,0.33925,0.6867501,-0.0015,29.67874,0.1715,-0.196,0.1235,29.8923,0.035,2.6915,-0.3845,29.82806,-0.102,3.5505,-0.15825,29.61795,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
"2011-07-10 09:30:00.1",9840,0.403,3.1195,-0.37175,29.22574,0.06550001,0.6655,0.1275,29.76208,0.196,-0.2,0.1,29.901,0.16225,2.31525,-0.5975,29.69263,0.24175,3.11925,-0.3725,29.57977,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
等等......

我想保存/写入矩阵u(5,540001),v(5,540001),w(5,540001)和T(5,540001),以便我可以进行一些计算和绘图。这是我正在使用的:

PROGRAM READ_MAIZE
   IMPLICIT NONE

   REAL,DIMENSION(:,:),Allocatable :: u_r, v_r, w_r, T_r

   CHARACTER(len=*) :: fname
   fname='FINALDATA.txt'
   open(unit=1,file=fname,status='old',action='read')

   do i=1,540001
  READ(1,*)timestamp(i),count(i),u_r(5,i),v_r(5,i),w_r(5,i), &
    T_r(5,i),u_r(2,i),v_r(2,i),w_r(2,i),T_r(2,i), &
u_r(1,i),v_r(1,i),w_r(1,i),T_r(1,i), &
    u_r(3,i),v_r(3,i),w_r(3,i),T_r(3,i), &
u_r(4,i),v_r(4,i),w_r(4,i),T_r(4,i),flags(1:20)
   end do
   close(1)

   WRITE(U_maize,'(A,I7.7,A,I7.7,A)'), &
         '.txt'
   open(11,file=U_maize,status='unknown',action='write')
   write(11,'(F20.14)')(u_r)
   end

不要介意u_r(5,i)中的顺序,然后是u_r(2,i)...(它们只对应于不正常的不同高度)。这不起作用。

1 个答案:

答案 0 :(得分:1)

您的代码中发生了很多事情,这使得您很难理解您最初尝试做的事情。我在下面注释了你的代码并将其转换为编译和生成输出的东西。也许它会帮助你。

APP_DATE