无法在Fortran中读取未知长度字符串的实数

时间:2018-03-02 04:32:27

标签: fortran

这是我尝试阅读的数据的一小部分:

 01/06/2009,Tom Sanders,,264,220,73,260
 01/08/2009,Adam Apple,158,,260,,208
 01/13/2009,Lori Freeman,230,288,218,282,234
 01/15/2009,Diane Greenberg,170,,250,321,197
 01/20/2009,Adam Apple,257,,263,256,190
 01/21/2009,Diane Greenberg,201,,160,195,142
 01/27/2009,Tom Sanders,267,,143,140,206
 01/29/2009,Tina Workman,153,,124,155,140
 02/03/2009,Tina Workman,233,,115,,163
 02/03/2009,Adam Apple,266,130,310,,310

每个逗号之间的数字来自不同的位置 其中两个逗号表示缺少数据,尾随逗号表示缺少第五个数据点

我的目标是在计算每个站点和人的平均值后将数据组织到一个表中,因此我的两个暗淡的数组

我希望我的输出看起来像下面这样: (显然更整洁的格式,但仍然是一张桌子)

              Average Observed TDS (mg/l)
     Name Site 1 Site 2 Site 3 Site 4 Site 5
  ------------------------------------------------------
  Tom Sanders 251.0 172.5 251.7 160.0 229.0  
  Adam Apple 227.0 130.0 277.7 256.0 236.0
  Lori Freeman 194.0 288.0 216.7 279.0 202.7
  Diane Greenberg 185.5 190.0 205.0 258.0 169.5
  Tina Workman 193.0 140.0 119.5 155.0 163.0

到目前为止,这是我的计划:

program name_finder
implicit none


integer, parameter :: wp = selected_real_kind(15)
real(wp) :: m, tds
real(wp), dimension(20,5) :: avg_site, site_sum
integer, dimension(20) :: nobs
integer, dimension(5) :: x
integer :: ierror, i, nemp, cp, non, ni, n
character(len=40), dimension(20) :: names
character(len=200) :: line, aname
character(len=20) :: output, filename
character(len=3), parameter :: a = "(A)"

do 
    write(*,*) "Enter file to open."
    read(*,*) filename
    open(unit=10,file = filename, status = "old", iostat = ierror) 
    if (ierror==0) exit
end do
 write(*,*) "File, ",trim(filename)," has been opened."


 non = 0
outer: do
     read(10,a, iostat = ierror) line
     if (ierror/=0) exit
     cp = index(line(12:),",") + 11
     aname = line(12:cp-1)

n=0
middle: do
    read(line,'(Tcp,f4.2)') tds
    write(*,*) "tds=", tds
    n=n+1
    if (n>10) exit
    i = 1
    inner: do
        if (i > non) then
            non = non +1
            names(non) = trim(aname)
            !ni = non
            exit
        end if

        if (aname == names(i)) then
            !ni = i
            !cycle outer
            exit inner
        end if
         i = i + 1
       end do inner
     end do middle
 end do outer

 write(*,*)
 write(*,*) "Names:"
 do i = 1,non
      write(*,*) i, names(i)
  end do


  close(10)
  close(20)
  STOP
  end program name_finder

TLDR;我无法从名称后面的每个站点顶部显示的文件中读取数据。

连连呢?谢谢!

1 个答案:

答案 0 :(得分:0)

我希望以下内容对您有所帮助。我已经省略了任何容易假设的声明或任何进一步的数据操作或写入另一个文件。该代码仅用于逐行读取数据。

character(150)  :: word         
read(fileunit, '(A)') word         ! read the entire line
comma_ind = index(word,',')        ! find the position of first comma

! Find the position of next comma
data_begin = index(word(comma_ind+1:),',')

! Save the name
thename = word(comma_ind+1:comma_ind+data_begin-1)

! Define next starting point
data_begin = comma_ind+data_begin

! Read the rest of the data
outer: do 
   if (word(data_begin+1:data_begin+1) == ',') then
      ! decide what to do when missing an entry
      data_begin = data_begin + 1
      cycle outer
   else if (word(data_begin+1:data_begin+1) == ' ') then
      ! Missing last entry
      exit outer
   else             
      ! Use it to find the length of current entry
      st_ind = index(word(data_begin+1:),',')

      if (st_ind == 0) then 
          ! You reached the last entry, read it and exit
          read(word(data_begin+1:), *) realData
          exit outer
      else
         ! Read current entry
         read(word(data_begin+1: data_begin+st_ind-1),*) realData
      end if
      ! Update starting point
      data_begin = data_begin + st_ind

   end if
end do outer

可能有一种更优雅的方式可以做到,但我现在想不到任何方法。