Fortran Coder

查看: 7020|回复: 7
打印 上一主题 下一主题

[文件读写] 额滴个神,为神马就是读不进去?

[复制链接]

13

帖子

9

主题

0

精华

入门

F 币
75 元
贡献
52 点
跳转到指定楼层
楼主
发表于 2016-3-8 16:52:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 zhangzhipeng 于 2016-3-8 21:44 编辑

[Fortran] 纯文本查看 复制代码
Module SACParameter
Implicit None
    !Integer, parameter :: FileUnit = 100
    Character( Len = * ), Parameter :: WaveformDIR = "..\WAV\AustrData\Waveform\"
    Character( Len = * ), Parameter :: ResponseDIR = "..\WAV\AustrData\Response\"
    Character ( Len = : ), Allocatable :: Buffer 
    REal( Kind = 8 ), Parameter :: PI = 3.14159265358979
    Integer( kind = 4) :: FileNameLong
    Character( Len = 1 )  :: separator = "."
    Character( Len = 4 ) :: Station 
    Integer( kind = 4 ) :: StationNumber
    Integer( kind = 4 ) :: FileNumber
    Integer( kind = 4 ) :: DeltaTdt = 0.05
!
    Type :: EventPara
        Character( Len = 16 ), allocatable :: StationName(:)    
        Character( Len = 4 ), allocatable :: ComponentName(:)   
        Character( Len = 1 ), allocatable :: stringFileName(:)  
        Real(kind = 4), allocatable :: StationWaveformLong(:)   
        Real(kind = 4), allocatable :: StationMaximumAmplitude(:)
        Real(kind = 4), allocatable :: StationDELTA(:)         
        Integer(kind = 4), allocatable :: Flag(:)               
    End Type EventPara
!
    Type :: SACHeader
        SEQUENCE
        Real( Kind = 4 ), Allocatable :: Header1(:,:)      
        Integer( Kind = 4 ), Allocatable :: Header2(:,:)      
        Character( Len = 8 ), Allocatable :: Header3(:,:)     
        Character( Len = 4 ), Allocatable :: WaveformBinary(:,:)
        Real( kind = 4 ), Allocatable :: Waveform(:,:)          
        Integer( Kind = 4 ), Allocatable :: WaveformLong(:)    
    End Type SACHeader 
!
public WaveformDIR, ResponseDIR, Buffer, FileNameLong, separator, Station, DoWithWildcard, WriteName, EventPara, SACHeader,  Response
!------------------------------------------------------------------------------------------
Contains
Subroutine DoWithWildcard(cWildcard,CallBack,iTotal)
Use DFLib,only:GetFileInfoQQ,GetLastErrorQQ,FILE$INFO,FILE$LAST,FILE$ERROR,FILE$FIRST,ERR$NOMEM,ERR$NOENT,FILE$DIR
type(EventPara) :: EVENT1
type(SACHeader) :: SACFILE1
    Interface 
        Subroutine CallBack( FileName , loop )
            Character( Len = * ),Intent(In) :: FileName
            Integer,Intent(In) :: loop
        End Subroutine CallBack
    End Interface
    Character*(*),Intent(In)::cWildcard
    Integer,Intent(Out)::iTotal
    TYPE (FILE$INFO) info
    INTEGER(4)::Wildhandle,length,retInt
    Wildhandle = FILE$FIRST
    iTotal = 0
    DO WHILE (.TRUE.)
        length = GetFileInfoQQ(cWildCard,info,Wildhandle)
        IF ((Wildhandle .EQ. FILE$LAST) .OR.(Wildhandle .EQ. FILE$ERROR)) THEN
            SELECT CASE (GetLastErrorQQ())
            CASE (ERR$NOMEM)  !//内存不足
                iTotal = - 1
                Return
            CASE (ERR$NOENT)  !//碰到通配符序列尾
                Return
            CASE DEFAULT
                iTotal = 0
                Return
            END SELECT
        END IF
        If ((info%permit.AND.FILE$DIR).Eq.0) then
            Call CallBack( Trim(info.Name) , iTotal + 1 )
            iTotal = iTotal + 1
        End If
    END DO
End Subroutine DoWithWildcard
Subroutine WriteName( FileName , loop )
    Character( Len = * ), Intent(In) :: FileName
    Integer, Intent(In) :: loop
    Write(*,*) loop, FileName
    StationNumber = loop
End Subroutine WriteName
Subroutine OpenFile( FileName , loop )                                                         
    Character( Len = * ), Intent(In) :: FileName            
    Integer,Intent(In) :: loop                     
    integer( kind = 4 ) :: iFileUnit
    iFileUnit = 100 + loop
    Write(*,*) iFileUnit, FileName
    pause 3
    !Open( Unit = iFileUnit, File = trim(FileName), access = 'Sequential', Form = 'Unformatted', Status = 'OLD', Action = 'READ' )
    !Open( Unit = iFileUnit, File = FileName(1:len_trim(FileName)), Form = 'Unformatted', Status = 'OLD', Action = 'READ' )
    Call GetFileN( iFileUnit , loop, FileName )
    close( iFileUnit )
End Subroutine OpenFile
Subroutine GetFileN( iFileUnit , loop, FileName)
    USE, INTRINSIC :: ISO_FORTRAN_ENV
    Implicit None
    type(EventPara) :: EVENT1
    type(SACHeader) :: SACFILE1
    Logical, Parameter :: FileIsNotEnd = .True.
    Integer, Intent( IN ) :: iFileUnit
    Integer, Intent( IN ) :: loop
    Integer( Kind = 4 ) :: errvar
    Character( Len = * ), Intent(In) :: FileName
    Character( Len = 128 ) :: DIRName
    Integer :: i = 1, j = 1, k = 1, l = 1
    k = Index( FileName, separator, .True.)
    l = len_trim( FileName )
    if(FileName(( k + 1 ):l ) == "SAC") Then
        DIRName = WaveformDIR(1:Len_trim(WaveformDIR))//FileName(1:Len_trim(FileName))
        write(*,*) DIRName
        Open( Unit = iFileUnit, File = DIRName(1:len_trim(DIRName)), Form = 'Unformatted', Access = "Stream", Status = 'OLD', Action = 'READ', iostat = errvar, convert = "LITTLE_ENDIAN" )
        pause 4
        Rewind( iFileUnit )
        Do i = 1,70
            Read( Unit = iFileUnit, iostat = k ) SACFILE1%Header1(i,loop)
        End do
        Do i = 1,40
            Read( Unit = iFileUnit, iostat = k ) SACFILE1%Header2(i,loop)
        End do
        Read( Unit = iFileUnit, iostat = k ) SACFILE1%Header3(1,loop)
        Read( Unit = iFileUnit, iostat = k ) SACFILE1%KEVNM(loop)
        Do i = 2,22
            Read( Unit = iFileUnit, iostat = k )SACFILE1%Header3(i,loop)
        End do
        Read(SACFILE1%Header1(1,loop),"(I4)")SACFILE1%WaveformLong(loop)
        Allocate(SACFILE1%WaveformBinary(SACFILE1%WaveformLong(loop), Loop))! 波形(台站,记录)
        SACFILE1%WaveformLong(loop) = 0
        Do while( FileIsNotEnd )
            Read( Unit = iFileUnit, iostat = k) SACFILE1%WaveformBinary( SACFILE1%WaveformLong(Loop) ,loop)       
            SACFILE1%WaveformLong(loop) = SACFILE1%WaveformLong(loop) + 1
            If( k == IOSTAT_END ) Exit
        End do
        Rewind( iFileUnit )
    Else
        DIRName = ResponseDIR(1:Len_trim(ResponseDIR))//FileName(1:Len_trim(ResponseDIR))
        Open( Unit = iFileUnit, File = DIRName(1:len_trim(DIRName)), Form = 'Unformatted', Status = 'OLD', Action = 'READ', IOstat = errvar )
        Rewind( iFileUnit )
        READ( Unit = iFileUnit, iostat = k, FMT = "(A100)" ) Buffer
        If ( Buffer(1:6) == 'B053F06' ) Then
            READ( Unit = iFileUnit, iostat = k, FMT = 160 ) RESPONSE1%FirstColumn, RESPONSE1%SecondColumn, RESPONSE1%NormalizationFactor( Loop )
      READ( Unit = iFileUnit, iostat = k, FMT = 160 ) RESPONSE1%FirstColumn, RESPONSE1%SecondColumn, RESPONSE1%NormalizationFrequency( Loop )
            READ( Unit = iFileUnit, iostat = k, FMT = 170 ) RESPONSE1%FirstColumn, RESPONSE1%SecondColumn, RESPONSE1%ZeroesNumber( Loop )
            READ( Unit = iFileUnit, iostat = k, FMT = 170 ) RESPONSE1%FirstColumn, RESPONSE1%SecondColumn, RESPONSE1%PolesNumber( loop ) 
            Do i = 0, RESPONSE1%ZeroesNumber( Loop ) - 1
                Read( Unit = iFileUnit, iostat = k, FMT = 180 ) RESPONSE1%FirstColumn, j, RESPONSE1%Zeroes(i, Loop), RESPONSE1%ZeroesError(i, Loop)
            End do
            Do i = 0, RESPONSE1%PolesNumber( loop ) - 1
                Read( Unit = iFileUnit, iostat = k, FMT = 180  ) RESPONSE1%FirstColumn, j, RESPONSE1%Poles(i, Loop), RESPONSE1%PolesError(i, Loop)
            End do
        Else
            Continue
        End if
        Rewind( iFileUnit )
    End if
End Subroutine GetFileN
End module SACParameter    
!*************************************************************************    
Program Main
USE SACParameter
Implicit None
    type( EventPara ) :: EVENT1
    type( SACHeader ) :: SACFILE1
    type( Response ) :: RESPONSE1
    Integer( Kind = 4 ) :: LoopNumber
    Integer( Kind = 4 ) :: DELTAdt
    Integer( Kind = 4 ), Allocatable :: Flag(:)
    Integer( Kind = 4 ) :: i, j, k, m, n = 1
    Interface
        Subroutine BubbleSort(Object, Flag, LoopNumber)
            Real(kind = 4), allocatable, Intent( INOUT ) :: Object(:)
            Integer(kind = 4), allocatable, Intent( INOUT ) :: Flag(:)
            Integer(kind = 4), Intent( IN ) :: LoopNumber
        End Subroutine
    End Interface
    Call DoWithWildcard( '..\WAV\AustrData\Waveform\*.BHZ.*.SAC' , WriteName , N)
    If ( N >= 0 ) then
        Write(*,*) '共' , N , '个文件,',StationNumber,'个台站'
    End If
    pause 0
    LoopNumber = StationNumber
    Write(*,*) LoopNumber
    pause 1
    Allocate(EVENT1%StationName( LoopNumber ))                 
    Allocate(EVENT1%ComponentName( LoopNumber ))                
    Allocate(EVENT1%stringFileName( LoopNumber ))               
    Allocate(EVENT1%StationWaveformLong( LoopNumber ))         
    Allocate(EVENT1%StationMaximumAmplitude( LoopNumber ))     
    Allocate(EVENT1%StationDELTA( LoopNumber ))                 
    Allocate(SACFILE1%Header1(70, LoopNumber ))                  
    Allocate(SACFILE1%Header2(40, LoopNumber ))                  
    Allocate(SACFILE1%Header3(22, LoopNumber ))                  
    Allocate(SACFILE1%WaveformLong( LoopNumber ))                
    Allocate(FLAG( LoopNumber ))
    pause 2
    n = 1
    Call DoWithWildcard( '..\WAV\AustrData\Waveform\*.BHZ.*.SAC' , OpenFile , n )
    pause 5
    DELTAdt = DeltaTdt
    Call RecordSACHeader( LoopNumber, SACFILE1 ) 
    Do i = 1, StationNumber
        Write(*,*) SACFILE1%STLA(i), SACFILE1%STLO(i)
    End do
End Program Main


Waveform.zip

897.07 KB, 下载次数: 1

Waveform

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
沙发
发表于 2016-3-8 21:27:34 | 只看该作者
你先把编译错误都解决了

13

帖子

9

主题

0

精华

入门

F 币
75 元
贡献
52 点
板凳
 楼主| 发表于 2016-3-8 21:29:30 | 只看该作者
vvt 发表于 2016-3-8 21:27
你先把编译错误都解决了

编译时没错的,这里贴的不全!!!

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
地板
发表于 2016-3-8 21:37:10 | 只看该作者
你用的什么编译器?
Real( Len = 4 ), Allocatable :: Header1(:,:)   
这种能通过编译?

13

帖子

9

主题

0

精华

入门

F 币
75 元
贡献
52 点
5#
 楼主| 发表于 2016-3-8 21:43:27 | 只看该作者
vvt 发表于 2016-3-8 21:37
你用的什么编译器?
Real( Len = 4 ), Allocatable :: Header1(:,:)   
这种能通过编译? ...

vs2013+ivf2015,DEBUG调试!!!那里原来是real,后来改成了character,贴的时候又改回去了,结果漏了,抱歉!

13

帖子

9

主题

0

精华

入门

F 币
75 元
贡献
52 点
6#
 楼主| 发表于 2016-3-8 21:43:50 | 只看该作者
vvt 发表于 2016-3-8 21:37
你用的什么编译器?
Real( Len = 4 ), Allocatable :: Header1(:,:)   
这种能通过编译? ...

vs2013+ivf2015,DEBUG调试!!!那里原来是real,后来改成了character,贴的时候又改回去了,结果漏了,抱歉!

13

帖子

9

主题

0

精华

入门

F 币
75 元
贡献
52 点
7#
 楼主| 发表于 2016-3-8 21:52:20 | 只看该作者
估计加上仪器响应结构体就行了
Type Response
        Real( Kind = 4 ), Allocatable :: RealResponse1(:,:), RealResponse2(:,:), ImagiResponse1(:,:), ImagiResponse2(:,:)
        Complex( Kind = 4 ), Allocatable :: Zeroes(:,:), Poles(:,:), ZeroesError(:,:), PolesError(:,:)
        Character( Len = 10 ) :: FirstColumn
        Character( Len = 80 ) :: SecondColumn
        Real( kind = 4 ), Allocatable :: NormalizationFactor(:)
                Real( kind = 4 ), Allocatable :: NormalizationFrequency(:)
        Integer( kind = 4 ), Allocatable :: ZeroesNumber(:)
        Integer( kind = 4 ), Allocatable :: PolesNumber(:)
    End Type Response

2022

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1598 元
贡献
689 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

8#
发表于 2016-3-9 22:06:27 | 只看该作者
看了你在QQ群里的代码。
应该是没有读文件。就开始读
Read(SACFILE1%Header1( 1, i ), "(G15.7)")SACFILE1%DELTA( i )  
了。此时 SACFILE1%Header1 还没有值
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-11-23 23:26

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表