切换到宽版
  • 2846阅读
  • 8回复

(原创)TXT格式测井文件转LAS格式测井文件源程序 [复制链接]

上一主题 下一主题
离线tanwenbin76
 
发帖
452
财富
1640
威望
0
交易币
0
只看楼主 倒序阅读 使用道具 0楼 发表于: 2009-12-12 | 石油求职招聘就上: 阿果石油英才网
— 本帖被 dwg123 从 软件应用 移动到本区(2009-12-13) —
Dim fs, f, f1, fc, s, Work_Book, ASheets, qxSheet, data_path
Type Bin_Single
    b1 As Byte
    b2 As Byte
    b3 As Byte
    b4 As Byte
End Type
'对文件夹中测井曲线进行转换主程序
'本程序主要逐一提取文件名,并调用cjqxgy过程进行转换
Sub 测井曲线整理()
    Set Work_Book = Workbooks("测井曲线标准化.xls")
    Set ASheets = Work_Book.Sheets("jh")
   
    data_path = "C:\测井数据(原始)"
    data_outpath = "C:\测井数据(整理)"
  
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(data_path)
    Set fc = f.Files
   
    For Each f1 In fc
        s = f1.Name
        If Left(s, 1) <> " " Then
            ExtName = LCase(GetExtensionName(s))
            BaseName = UCase(GetBaseName(s))
        End If
        Debug.Print data_path & "\" & BaseName & "." & ExtName
       
        i = 2
        done = False
        Do While ASheets.Cells(i, 1).Value2 <> ""
            If UCase(ASheets.Cells(i, 1).Value2) = BaseName Then
                done = True
                Exit Do
            End If
            i = i + 1
        Loop
       
        outpath = data_outpath
        If done Then
            bzjh = ASheets.Cells(i, 2).Value2
            If fs.FileExists(data_outpath & "\" & bzjh & ".las") Then
                i = 1
                Exists = True
                Do While Exists
                    newd$ = "\Las(" & Trim(i) & ")"
                    If Not fs.FolderExists(data_outpath & newd$) Then
                       fs.CreateFolder (data_outpath & newd$)
                       outpath = data_outpath & newd$
                       Exists = False
                    Else
                        If Not fs.FileExists(data_outpath & newd$ & "\" & bzjh & ".las") Then
                            outpath = data_outpath & newd$
                            Exists = False
                        End If
                    End If
                    i = i + 1
                Loop
            End If
            Select Case UCase(ExtName)
                Case "ASC"
                    ASC_to_las data_path, outpath, BaseName, ExtName, bzjh
                Case "TXT"
                    TXT_to_las data_path, outpath, BaseName, ExtName, bzjh
                Case "LAS"
                    las_to_las data_path, outpath, BaseName, ExtName, bzjh
                Case Else
                    Stop
            End Select
        End If
    Next f1
End Sub
Sub las_to_las(data_path, data_outpath, BaseName, ExtName, bzjh)
  
    Open data_outpath & "\" & bzjh & ".las" For Output As #1
    Open data_path & "\" & BaseName & "." & ExtName For Input As #2
   
    Do While Not EOF(2)
        Line Input #2, a$
        Print #1, a$
    Loop
    Close #1, #2


End Sub
'进行转换, ASC文件
Sub TXT_to_las(data_path, data_outpath, BaseName, ExtName, bzjh)
   
    Dim qx(100) As String, QX_Data(100) As Single, qx_unit(100) As String, qx_fl(100) As Boolean
    Dim loc As Long
    Dim d_dep, d_dep0
   
    txtline = 0
    Set qxSheets = Work_Book.Sheets("qx")
    Open data_path & "\" & BaseName & "." & ExtName For Input As #2
   
    loc = 1
        Do While Not EOF(2)
        Line Input #2, a$
        txtline = txtline + 1
        If InStr(UCase(a$), "DEP") <> 0 Then
            find_qxName a$, qx(), qx_max, done
            If done = True Then Exit Do
        End If
    Loop
    For k = 1 To qx_max
        If Left(qx(k), 3) = "DEP" Then
            DEP_LOC = k
            Exit For
        End If
    Next k
    For k = 1 To qx_max
        qx_fl(k) = False
    Next k
    For k = 1 To qx_max
        i = 2
        done = False
        Do While qxSheets.Cells(i, 1).Value2 <> ""
            If UCase(qxSheets.Cells(i, 1).Value2) = UCase(qx(k)) Then
                done = True
                Exit Do
            End If
            i = i + 1
        Loop
        If Not done Then
            qxSheets.Cells(i, 1).Value2 = UCase(qx(k))
            qxSheets.Cells(i, 6).Value2 = BaseName & "." & ExtName
        Else
            qx(k) = qxSheets.Cells(i, 2).Value2
            qx_unit(k) = qxSheets.Cells(i, 3).Value2
            If Trim(UCase(qxSheets.Cells(i, 4).Value2)) = "Y" Then qx_fl(k) = True
        End If
    Next k
   
   
    b$ = ""
    Do While b$ = ""
        loc_data = loc
        Line Input #2, a$
        txtline = txtline + 1
        b$ = Replace(Replace(a$, "=", ""), " ", "")
    Loop
   
    Find_qxdate a$, QX_Data(), qx_max, done
    dep = QX_Data(DEP_LOC)
    d_dep0 = 0
    dep_min = dep
    dep_max = dep
   
  
    Do While Not EOF(2)
        Line Input #2, a$
        Find_qxdate a$, QX_Data(), qx_max, done
        If Not done Then Exit Do
        d_dep = QX_Data(DEP_LOC) - dep
        If d_dep0 <> 0 Then
            If d_dep <> d_dep0 Then Stop
            d_dep0 = d_dep
        End If
        dep = QX_Data(DEP_LOC)
        If dep_min > dep Then dep_min = dep
        If dep_max < dep Then dep_max = dep
    Loop
    Close #2
    Open data_outpath & "\" & bzjh & ".las" For Output As #1
    Open data_path & "\" & BaseName & "." & ExtName For Input As #2
   
    Print #1, "~Version Information Block"
    Print #1, " VERS.                2.00:     CWLS LOG ASCII STANDARD - VERSION 2.000000"
    Print #1, " WRAP.                  NO:     One Line Per Depth Step"
    Print #1, "#"
    Print #1, "~Well Information Block"
    Print #1, "#MNEM.UNIT                     Data                         Information"
    Print #1, "#----------   ------------------------------------------   ----------------"
    Print #1, "STRT .M          "; Format(dep_min, ".0000"); ":  START DEPTH"
    Print #1, "STOP .M           "; Format(dep_max, ".0000"); ":  STOP DEPTH"
    Print #1, "STEP .M         "; Format(d_dep, ".0000"); ":  STEP"
    Print #1, "NULL .             -999999.000:  NULL VALUE"
    Print #1, "WELL .             "; bzjh; ":  WELL"
    Print #1, "~Curve Information Block "
    Print #1, "#MNEM.UNIT         API CODE   Curve Description"
    Print #1, "#---------- ----------------  -----------"
   
    For i = 1 To qx_max
        If qx_fl(i) Then
            Print #1, Format(qx(i), "!@@@@@@@"); "."; Format(qx_unit(i), "!@@@@@@@@@@@@@@@@@@@@@@@@@ "); ":"
        End If
    Next i
   
    Print #1, "~A ";
    For i = 1 To qx_max
        If qx_fl(i) Then
            Print #1, Format(qx(i), "!@@@@@@@@@@");
        End If
    Next i
   
    Print #1,
   
    For i = 1 To txtline - 1
        Line Input #2, a$
    Next i
    Do While Not EOF(2)
        Line Input #2, a$
        Find_qxdate a$, QX_Data(), qx_max, done
        For i = 1 To qx_max
            If qx_fl(i) Then
                Print #1, Format(Format(QX_Data(i), "0.000"), "@@@@@@@@@@");
            End If
        Next i
        Print #1,
    Loop
    Close #1, #2
End Sub
'进行转换, ASC文件
Sub ASC_to_las(data_path, data_outpath, BaseName, ExtName, bzjh)
   
    Dim qx(100) As String, QX_Data(100) As Single, qx_unit(100) As String, qx_fl(100) As Boolean
    Dim loc As Long
    Dim d_dep, d_dep0
    Set qxSheets = Work_Book.Sheets("qx")
    Open data_path & "\" & BaseName & "." & ExtName For Binary As #2
    loc = 1
    Do While Not EOF(2)
        a$ = Inputline(2, loc)
        find_qxName a$, qx(), qx_max, done
        If done = True Then Exit Do
    Loop
    For k = 1 To qx_max
        If Left(qx(k), 3) = "DEP" Then
            DEP_LOC = k
            Exit For
        End If
    Next k
    For k = 1 To qx_max
        qx_fl(k) = False
    Next k
    For k = 1 To qx_max
        i = 2
        done = False
        Do While qxSheets.Cells(i, 1).Value2 <> ""
            If UCase(qxSheets.Cells(i, 1).Value2) = UCase(qx(k)) Then
                done = True
                Exit Do
            End If
            i = i + 1
        Loop
        If Not done Then
            qxSheets.Cells(i, 1).Value2 = UCase(qx(k))
            qxSheets.Cells(i, 6).Value2 = BaseName & "." & ExtName
           
        Else
            qx(k) = qxSheets.Cells(i, 2).Value2
            qx_unit(k) = qxSheets.Cells(i, 3).Value2
            If Trim(UCase(qxSheets.Cells(i, 4).Value2)) = "Y" Then qx_fl(k) = True
        End If
    Next k
   
    loc_data = loc
    a$ = Inputline(2, loc)
    Find_qxdate a$, QX_Data(), qx_max, done
    dep = QX_Data(DEP_LOC)
    d_dep0 = 0
    dep_min = dep
    dep_max = dep
   
  
    Do While Not EOF(2)
        a$ = Inputline(2, loc)
        Find_qxdate a$, QX_Data(), qx_max, done
        If Not done Then Exit Do
        d_dep = QX_Data(DEP_LOC) - dep
        If d_dep0 <> 0 Then
            If d_dep <> d_dep0 Then Stop
            d_dep0 = d_dep
        End If
        dep = QX_Data(DEP_LOC)
        If dep_min > dep Then dep_min = dep
        If dep_max < dep Then dep_max = dep
    Loop
    Close #2
    Open data_outpath & "\" & bzjh & ".las" For Output As #1
    Open data_path & "\" & BaseName & "." & ExtName For Binary As #2
   
    Print #1, "~Version Information Block"
    Print #1, " VERS.                2.00:     CWLS LOG ASCII STANDARD - VERSION 2.000000"
    Print #1, " WRAP.                  NO:     One Line Per Depth Step"
    Print #1, "#"
    Print #1, "~Well Information Block"
    Print #1, "#MNEM.UNIT                     Data                         Information"
    Print #1, "#----------   ------------------------------------------   ----------------"
    Print #1, "STRT .M          "; Format(dep_min, ".0000"); ":  START DEPTH"
    Print #1, "STOP .M           "; Format(dep_max, ".0000"); ":  STOP DEPTH"
    Print #1, "STEP .M         "; Format(d_dep, ".0000"); ":  STEP"
    Print #1, "NULL .             -999999.000:  NULL VALUE"
    Print #1, "WELL .             "; bzjh; ":  WELL"
    Print #1, "~Curve Information Block "
    Print #1, "#MNEM.UNIT         API CODE   Curve Description"
    Print #1, "#---------- ----------------  -----------"
   
    For i = 1 To qx_max
        If qx_fl(i) Then
            Print #1, Format(qx(i), "!@@@@@@@"); "."; Format(qx_unit(i), "!@@@@@@@@@@@@@@@@@@@@@@@@@ "); ":"
        End If
    Next i
   
    Print #1, "~A ";
    For i = 1 To qx_max
        If qx_fl(i) Then
            Print #1, Format(qx(i), "!@@@@@@@@@@");
        End If
    Next i
   
    Print #1,
    loc = loc_data
   
    Do While Not EOF(2)
        a$ = Inputline(2, loc)
        Find_qxdate a$, QX_Data(), qx_max, done
        For i = 1 To qx_max
            If qx_fl(i) Then
                Print #1, Format(Format(QX_Data(i), "0.000"), "@@@@@@@@@@");
            End If
        Next i
        Print #1,
    Loop
    Close #1, #2
End Sub
'从字符串中分析出数据,并存入 QX_Data() 数组中
Sub Find_qxdate(Title$, QX_Data() As Single, qx_max, done)
    done = False
    a$ = Trim(Title$)
    l = Len(a$)
    i = 1
    For j = 1 To l
        b$ = Mid(a$, j, 1)
        If b$ = " " Then
            If fl Then
                QX_Data(i) = Val(c$)
                i = i + 1
                c$ = ""
                fl = False
            End If
        Else
            c$ = c$ + b$
            fl = True
        End If
    Next j

If qx_max = i Then done = True
End Sub
'从字符串中分析出测井曲线名称,并存入 QX() 数组中
Sub find_qxName(Title$, qx() As String, qx_max, done)
    done = False
    a$ = Trim(Title$)
    l = Len(a$)
    i = 1
    For j = 1 To l
        b$ = Mid(a$, j, 1)
        If b$ = " " Then
            If fl Then
                i = i + 1
                qx(i) = ""
                fl = False
            End If
        Else
            qx(i) = qx(i) + b$
            fl = True
        End If
    Next j
qx_max = i
done = True
End Sub

'对UNIX格式文本文件读取行字符串,类似于DOS下的lineinput
Function Inputline(filenum, loc)
    Dim b As String * 1
    a$ = ""
    If EOF(filenum) Then
        Inputline = a$
        Exit Function
    End If
   
    Get #filenum, loc, b
    loc = loc + 1
    Do While b <> Chr(13) And b <> Chr(10)
        a$ = a$ + b
        If EOF(filenum) Then
            Inputline = a$
            Exit Function
        End If
        Get #filenum, loc, b
        loc = loc + 1
    Loop
    If b = Chr(13) Then
        If EOF(filenum) Then
            Inputline = a$
            Exit Function
        End If
        Get #filenum, loc, b
        If b = Chr(10) Then
            loc = loc + 1
        End If
    End If
    Inputline = a$

End Function
'得到文件名的扩展名
Function GetExtensionName(fn)
l0 = Len(fn)
l = l0 + 1
Do
    l = l - 1
   
Loop Until (l = 1 Or Mid(fn, l, 1) = ".")
If l = 1 Then
    GetExtensionName = ""
Else
    GetExtensionName = Right(fn, l0 - l)
End If
End Function
'得到文件名的主文件名
Function GetBaseName(fn)
l0 = Len(fn)
l = l0 + 1
Do
    l = l - 1
   
Loop Until (l = 1 Or Mid(fn, l, 1) = ".")
If l = 1 Then
    GetBaseName = fn
Else
    GetBaseName = Left(fn, l - 1)
End If
End Function

1条评分
dwg123 财富 +30 分享技术 2009-12-13
评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
离线coverme
发帖
6308
财富
3593
威望
11
交易币
0
只看该作者 1楼 发表于: 2009-12-16 | 石油求职招聘就上: 阿果石油英才网
很强大啊,但是要怎么用呢?
离线张从才
发帖
51
财富
29
威望
0
交易币
0
只看该作者 2楼 发表于: 2009-12-17 | 石油求职招聘就上: 阿果石油英才网
好东西,先谢了
离线fightiger
发帖
3
财富
0
威望
0
交易币
0
只看该作者 3楼 发表于: 2010-03-15 | 石油求职招聘就上: 阿果石油英才网
很不错~
离线clg123456
发帖
544
财富
794
威望
5
交易币
0
只看该作者 4楼 发表于: 2010-10-23 | 石油求职招聘就上: 阿果石油英才网
谢谢分享
离线burning
发帖
1681
财富
270
威望
63
交易币
0
只看该作者 5楼 发表于: 2010-10-23 | 石油求职招聘就上: 阿果石油英才网
ding dingding
离线guodengch
发帖
615
财富
330
威望
5
交易币
0
只看该作者 6楼 发表于: 2010-12-16 | 石油求职招聘就上: 阿果石油英才网
可以看到的东西,一般顶的人少
离线luoxiqq
发帖
2733
财富
2971
威望
40
交易币
0
只看该作者 7楼 发表于: 2010-12-21 | 石油求职招聘就上: 阿果石油英才网
不错,收藏
离线1109785573
发帖
472
财富
830
威望
3
交易币
10
只看该作者 8楼 发表于: 2015-09-13 | 石油求职招聘就上: 阿果石油英才网
hen很好  谢谢啊  

网站事务咨询:QQ:1392013 | 26189883
阿果石油网为免费个人网站,为石油人提供免费的在线即时技术交流场所,拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论和行为!
如有言论或会员共享的资料涉及到您的权益,请立即通知网站管理员,本站将在第一时间给予配合处理,谢谢!