Fortran Coder

标题: 新人求助,想去除一个文件中负数 [打印本页]

作者: 18268732401    时间: 2016-10-11 15:48
标题: 新人求助,想去除一个文件中负数
应该很简单的问题。搞了一整天才搞定怎么读取、写入。文件如果水深为负数则整行去除,不知到怎么改
怎么改if a(:,i)能表示在TXT中得位置呢

作者: vvt    时间: 2016-10-11 16:37
文件什么样子?
作者: 18268732401    时间: 2016-10-11 16:42
我发来。处理一半的程序和文件

tx1.txt

65.93 KB, 下载次数: 5


作者: 18268732401    时间: 2016-10-11 16:43
[Fortran] 纯文本查看 复制代码
Program main
  Use DFile_Mod
  Implicit None
  Real , allocatable :: a(:,:)
  Character(len=512) :: cStr
  Real    :: r  
  integer :: FILE_IN , i , nRow , nCol
  Open( NewUnit = FILE_IN , File = "text.asc" )
  nRow = GetFileN( FILE_IN ) - 2 !//获得文件行数
  Read( FILE_IN , * ) !//跳过第一行
  Read( FILE_IN , '(a512)' ) cStr!//读取第三行
  nCol = GetDataN( cStr ) - 8 !//获得第三行有多少列
  write( *, '("Row:",g0," Col: ",g0)' ) nRow , nCol
  Allocate( a( nCol , nRow ) )
  Backspace( FILE_IN ) !//退回到第三行
   open( 11 , file ="tx1.txt")
    write( 11 , * )"  depth          temp          sal           turb  "
  Do i = 1 , nRow
    Read( FILE_IN , * ) r ,r, r, r, a( : , i )
    write( 11 , * ) a(:,i)
  End Do
  Deallocate( a )
  Close( FILE_IN )
End Program main

作者: 18268732401    时间: 2016-10-11 16:43
上面有模版,不过没粘贴
作者: 18268732401    时间: 2016-10-11 16:44
想把所有水深是负的行都去掉,因为是误差值
作者: fcode    时间: 2016-10-11 17:06
text.asc 是什么样子?
作者: 18268732401    时间: 2016-10-11 17:22
原始文档就是这个。网站不让传ASC,我复制到TXT了。没啥区别

数据.txt

85.87 KB, 下载次数: 5


作者: fcode    时间: 2016-10-11 19:29
只是筛选数据的话,就简单了。

[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
  implicit none
  character(len=512) :: c
  character(len=1)   :: s
  integer :: FILE_IN , FILE_OUT , k , i
  real    :: DepSM
  Open( NewUnit = FILE_IN , File = "text.asc" )
  Open( NewUnit = FILE_OUT, File = "text.txt" )
  Read( FILE_IN , 100 ) c
  Write( FILE_OUT , 99 ) trim(c)
  Do
    Read( FILE_IN , 100 , ioStat = k ) c
    if ( k /= 0 ) exit
    Read( c , * ) i , i , i , s , DepSM
    If ( DepSM > 0.0 ) then
      Write( FILE_OUT , 99 ) trim(c)
    End If
  End Do
  Close( FILE_IN )
  Close( FILE_OUT )
100 format(a512)
99  format(a)
End Program www_fcode_cn

作者: 18268732401    时间: 2016-10-12 08:41
厉害了,我得哥。有效果诶。。 不过程序没看明白,我好好研究一下。昨天九点多走的时候没发现有回复啊,奇怪。Ths!




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2