importar archivos en excel

download importar archivos en excel

of 6

description

importar archivos excel

Transcript of importar archivos en excel

  • Pergamon Computers & Geosciences Vol. 22, No. I, pp. 75-80, 1996

    Elsevier Science Ltd 0098-3004(%)ooo58-5 Printed in Great Britain

    0098-3004/96 $15.00 + 0.00

    AN EXCEL MACRO FOR IMPORTING LOG ASCII STANDARD (LAS) FILES INTO EXCEL WORKSHEETS

    SAIT ISMAIL OZKAYA Geology Department, Kuwait University, P.O. Box 17085, Khaldia 72450, Kuwait

    (e-mail:[email protected])

    (Received 9 November 1994; revised 8 February 1995)

    Abstract-An EXCEL 5.0 macro is presented for converting a LAS text file into an EXCEL worksheet. Although EXCEL has commands for importing text files and parsing text lines, LAS files must be decoded line-by-line because three different delimiters are used to separate fields of differing length. The macro is intended to eliminate manual decoding of LAS version 2.0. LAS is a floppy disk format for storage and transfer of log data as text files. LAS was proposed by the Canadian Well Logging Society. The present EXCEL macro decodes different sections of a LAS file, separates, and places the fields into different columns of an EXCEL worksheet. To import a LAS file into EXCEL without errors, the file must not contain any unrecognized symbols, and the data section must be the last section. The program does not check for the presence of mandatory sections or fields as required by LAS rules. Once a file is incorporated into EXCEL, mandatory sections and fields may be inspected visually.

    Key Words: EXCEL macro, File import, Log ASCII standard, Spreadsheet.

    INTRODUCTION

    Logging ASCII Standard (LAS) is a digital well-log data file format designed by the Canadian Well Logging Society for data transfer between personal computers (PCs) using floppy disks (Canadian Well Logging Society, 1991; Struyk and others, 1992; Struyk and others, 1993; Elphick, 1993).

    The well-known (Log Information Standard (LIS)) is for storage and transfer of field data to mainframe computers in binary form. It fails to meet the needs of PC users who are interested mostly in optically presented curves, and prefer to store and transfer limited log data as ASCII text files. An ASCII text file has a significant advantage over binary files for a PC user, because unlike the binary files which have to be input by a special program, ASCII files can be read by any program and any computer. Since its first introduction the LAS format has become popular among log analysts using PCs. Elphick (1991, 1992a, 1992b) presented a few computer programs in BASIC to read and write data in LAS format.

    Spreadsheet programs such as EXCEL comprise an ideal environment for presentation and processing of logging data. The purpose of this paper is to present an EXCEL macro to import LAS files into an EXCEL spreadsheet. EXCEL reads and writes spreadsheet data in its own binary format. Although ASCII text files can be imported into an EXCEL spreadsheet file, parsing is necessary to decode the text file and convert it to a spreadsheet file. The

    parsing capabilities of EXCEL are limited. A straightforward decoding of LAS text files is not possible. EXCEL has fixed-field symbol-delimited field options for parsing. The fixed-field option can- not be used because LAS-file records have variable length fields. The symbol-delimited option cannot be used either, because EXCEL parse commands allow only one delimiter. Records in LAS files have three delimiters: dot, space character, and colon. More- over, these symbols act as delimiters only between certain fields and not elsewhere. For example, dot acts as a delimiter between the parameter mnemonic and the unit fields but not elsewhere. A dot within the data field is not a delimiter but only part of a fractional number. The space character acts as a delimiter between unit and data fields but not else- where. The variable line formats and fields of differ- ing length make it necessary to parse each line of a LAS text file individually to decode it into an EXCEL worksheet. This may become tedious and cumber- some, if several LAS files must be imported regularly.

    The objective of this paper is to present an EXCEL macro which reads a LAS file, decodes and converts it into an EXCEL worksheet to eliminate cumber- some manual decoding. A description of the LAS format is given by Canadian Well Logging Society (1991), and Stuyck and others (1992). The listing of the EXCEL macro for importing LAS files into EXCEL and an example are given in the Appendix. A brief explanation of the decoding procedure is outlined next.

    75

  • 76 S. I. dzkaya

    DECODING LAS FILES INTO EXCEL

    A LAS file typically has four mandatory sections: the version, well,curve and data sections, and two optical sections: the parameters and others sections. The data section is easy to decode, because only one delimiter (the space character) is used to separate number fields in each record of the data section. The other sections, however, are surprisingly difficult to decode, as noted by Elphick (1991), for several reasons. Some of the reasons are: (1) a LAS record has three different delimiters as mentioned, each of which acts as a delimiter only between specific fields but not elsewhere; (2) the space character can preceed all fields and other delimiters but acts as a delimiter only between the units and data fields of some records, and (3) some fields are optional but different fields are optional in different sections. When an optional field is missing the delimiter can be dropped, making it difficult to decide the identity of the next field. The LAS version 1.2 allows spaces before and after the other two delimiters. This flexi- bility makes the LAS 1.2 ambiguous and impossible to decode. Fortunately, in a later version (LAS 2.0) a further restriction was imposed to eliminate the ambiguity. Now, spaces are not allowed after the dot when it is used as a delimiter between mnemonic and unit fields (Struyk and others, 1992; 1993). In LAS 1.2 version each section has a different number of fields and format, making it necessary to identify and treat each section individually when decoding a LAS file. In LAS 2.0 version the section format is standardized. Records of all header sections, except the data section, have four fields: the mnemonic, units, data, and description fields. The mnemonic and units fields must be separated by a dot. The data section must be separated from the mnemonic and units fields by at least one space. It must be delimited by spaces or a colon to the right. The data and description fields must be separated by a colon. Accordingly, a typical LAS record lines has the following layout:

    MNEM .UNITS DATA :DESCRIPTION

    A space character may be present before and after the colon. A space character is allowed only before the dot. The LAS description does not specify whether the delimiter dot is mandatory or whether it can be dropped when the unit field is not present. Apparently, both of the following layouts are acceptable:

    MNEM

    or

    DATA :DESCRIPTION

    MNEM . DATA :DESCRIPTION Similarly, LAS does not specify whether the colon

    : delimiter can be dropped when the description field is not present. The implication is that both of the following layouts must be possible

    MNEM .UNIT DATA MNEM .UNIT DATA :

    The EXCEL macro for decoding a LAS file has the following routines:

    IMPORT-LAS -Main routine DECPAR -Header section decoder DECDATA -Data section decoder

    An outline of the main section and subroutines are presented next. Several checks are incorporated to handle potential errors and end-of-file conditions.

    Main routine-IMPORT-LAS

    The main routine consists of the following steps: (1) Import a LAS file as a text file into EXCEL. (2) Start from the top. Set EXCEL worksheet row

    index i to 1. (3) Call INREC to read lines until the first non-

    blank character is a tilde symbol - . (4) Call DECPAR if the line starts with -V or

    - W or -C or - P. Call DECDATA if the line starts with -A.

    Subroutine-DECPAR

    The DECPAR routine is used to decode all sec- tions other than the data section. This works only for LAS version 2.0.

    (1) Call INREC to read lines. Perform the follow- ing operations on each line until the next tilde - is encountered.

    (2) Set the pointer to the first nonblank character from the left. Set EXCEL worksheet column point k to 1.

    (3) Scan the text line character by character from left-to-right until the first blank or . is encountered.

    (4) Extract the mnemonic field between the pointer and the last location. Move the pointer to the character after the delimiter-blank or dot.

    (5) Put the mnemonic in the ith row, kth column of the EXCEL worksheet. Increment the column counter k by one.

    (6) If there is a blank after the mnemonic field, then proceed until the next nonblank character is encountered. Move pointer to this location.

    (7) Continue scanning the line until a space char- acter is encountered. Extract the units field between the pointer and the space. Set the pointer after the space.

    (8) Place the units into the ith row, kth column of EXCEL worksheet. Increment k by one.

    (9) Continue scanning the line until a colon is encountered. Extract the data field between the pointer and the colon, and the description field right of the colon.

    (10) Place the data field in the ith row, kth column and description field in the ith row, and column k + 1 of the EXCEL worksheet.

    (11) Increment row counter i by one.

  • EXCEL macro II

    Subroutine-DECDATA

    The DECDATA routine decodes the data section. It applies for both unwrapped or wrapped data presentation formats.

    (1) Call INREC to read lines. Perform the follow- ing operations on each line until .LAS is encoun- tered.

    (2) Set the pointer to the first nonblank character from the left. Set EXCEL column pointer k to one.

    (3) Scan the line until next space is encountered. Extract the number field between the pointer and the space. Place the number in EXCEL worksheet column k. Increment k by one.

    (4) Move the pointer to the first character after the space.

    (5) Repeat steps 3 and 4 until the complete line is scanned from left to right.

    (6) Increment the row counter i by one.

    Other subroutines

    The following subroutines are for peripheral tasks:

    OPLAS -Imports an LAS text file into EXCEL

    INREC ERRMSG DECOTHER

    -Reads data lines -Signals error messages -Decodes the - OTHERS sec-

    tion of LAS files

    OPLAS opens a LAS text file specified by a user as an EXCEL worksheet. Each line of the text file is located on a row of the first column of the worksheet.

    The INREC routine reads the file line-by-line, and scans each line until the first nonblank character is encountered. If the line is all blank or is a comment line starting with #, INREC ignores it and moves on to the next line. INREC also checks for the .LAS end-of-file symbol. It counts the number of blank lines encountered. If a .LAS symbol is encountered or the number of blank lines exceed 100 then the end-of-file flag is raised. This will terminate the program.

    The DECOTHER routine simply reads and writes text lines until the next tilde is encountered. In LAS files, the -OTHERS section is intended for com- ments. It does not require any decoding as it stands,

    The ERRMSG routine signals error messages and writes the message next to the line which causes the error. An error occurs when (i) an end-of-file .LAS before the data section is processed, or (ii) a tilde - occurs when the data section is being processed. This accomplishes the two requirements of the LAS for- mat regarding the data section, either (a) the data section is mandatory, or (b) the data section is the last

    section of a LAS file. File processing is terminated when end-of-file signal is received or when a tilde occurs at the end of the data section. The error flag is raised also when (i) an unexpected tilde occurs or (ii) a tilde symbol is followed by an unrecognized section symbol. The section is unprocessed until the next tilde symbol in such a situation.

    LIMITATIONS

    The EXCEL macro IMPORT-LAS does not check for the following:

    (a) Compulsory sections (b) Compulsory fields of each section. As noted, the program only checks if the data

    section is the last section. The LAS format requires that -V, -C, and -D sections must be present. Version and Wrap lines must be present in the Versions section. There are several compulsory lines in the Wells section including starting and stopping depths. It was decided to leave out the checking of compulsory sections and fields because incorporating such checks makes the program too long. Once a LAS file is incorporated into an EXCEL worksheet, then the checks can be carried out visually. The purpose is not to write a log evaluation program reading data from a LAS file, but only to convert a LAS file into an EXCEL worksheet. Obviously, an EXCEL macro for log evaluation using the imported LAS file must check first for the presence of the mandatory sections and fields before processing the data.

    Acknowledgmenls-This work is a product of Kuwait Uni- versity Research Project SG037.

    REFERENCES

    Canadian Well Logging Society-Floppy Disk Committee, 1991, LASA floppy disk standard for log data: Geobyte, v. 6, no. 6, p. 56-61.

    Elphick, R. Y., 1991, Using the LAS format-Part 1: Geobyte, v. 6, No. 6, p. 4461.

    Elphick, R. Y., 1992a, Using the LAS format-Part 2: Geobyte, v. 7, no. 1, p. 22-33.

    Elphick, R. Y., 1992b, Using the LAS format-Sum- mations: Geobyte, v. 7, no. 2, p. 38-52.

    Elphick, R. Y., 1993, Log analysis within the LAS format: The Log Analyst, v. 34, no. 2, p. 67-90.

    Struyk, C., Gray, G., Peterson, G., Drebit, G., Lamborn, R., and Jonkers, J., 1993, New features of LAS version 2.0; the floppy disk standard for log data: The Log Analyst, v. 34, no. 2, p. 60-66.

    Struyk, C., Lambom, R., Gray, G., Jonkers, J., Peterson, G., Tulle, G., and Drebit, G., 1992, New features of LAS version 2.Qthe floppy disk standard for log data: Geobyte, v. 7, no. 6, p. 67-71.

    c,W.iEO 22/I-F

  • 78

    EXCEL 5.0 macro for importing log text fines in LAS format into an EXCEL worksheet

    Programmer: S.I. Ozkaya Date: 1995 Environment: Intel 466 machine with WINDOWS 3.1 and EXCEL 5.0 ,t.t.**t.*..tt..*.,t*.~~**..*,*,*,*,*..,.**.*********.~..***,.,***.

    Option Explicit Option Base 1 Dim satir As String * 256 Dim harfl, harf2 As String l 1 Dim ip, d As Integer Dim basla As Object Dim message As String Dim sonflag, nblank As Integer ,ttt**.*...**..t....****~*~*~...~..***,.*..*~.~...~*~.**...*....**.

    Sub IMPORT_LAS() It..*....t*~*tt**.*********.**.*.*....*.~****..,.,***.~***.,.*~..*.

    nblank = 0 sonflag = -1 errorf = -1 Open text file.. Call oplas(d) Start from lop of the file.. Set basla = Cells(1, 1) Get line which is not blank or comment line.. Call inrec(d) If the line does not start with 1 repeat until such a line is found.. While ((harfl -) And (sonflag < 0)) message = Unexpected line- violates LAS format.. Call errmsg(d) Call inrec(d)

    Wand

    If the line is not blank than extract the first character, harfl, the second character harf2, and the first four characters 14.. If (blank < 0) Then basla.Value = RTrim(satir) harfl = Mid(satir, ip. 1) harf2 = Mid(satir, ip + 1, 1) harf2 = LCase(harf2) 14 = Mid(satir, 1, 4) 14 = LCase(f4) If (14 = .las) Then sonflag = 1 End If End If If number of blank lines exceed 100 then as a precaution, end of record flag is raised.. If (blank > 0) Then nblank = nblank + 1 End If

    Process all lines.. While (sonflag < 0) If (harf2 = v Or harf2 = w Or harf2 = c Or harf2 = p) Then Decode header sections Call decpar(d) Elself (had2 = 0) Then

    Decode others section Call decother(d) Elself (had2 = a) Then Decode data section Call decdata(d) Else

    If (nblank > 100) Then sonflag = 1 basla.Value = .LAS End If End Sub Iftf+t..ff.**ft~tftf*~.~~~~,**.*~***..,.~~~~~,****~~*.~~...***~*~~~

    Sub decpar(ds) Iff.f..f.~.*(lf~f~*..**~*~....~.*.***~***.*~..***~~*~*.~.,.*******~.

    Decode version, well information, curve and parameters sections.. ,**tttt..**,.~*t.tt,.~.*..~..~~***.*~.~*..***.**~*~*..~.*.**~*.~~~*

    Dim symbol As String 1 Dim c

    message = Letter after - is not in LAS format. Section unprocessed.. Dim kutu As Obiect

    Unrecognized symbol.. Call errmsofd) decother ((ij End If

    Wend End Sub

    Dim psi, psr, i, lensat As Integer message = Missing delimiter..

    Set basla = basla.Offset(l , 0) Call inrec(d) Repeat until the next tilde.. Do While ((harfl -) And (sonflag < 0)) lensat = Len(satir) psi = ip i = ip

    S. I. (5zkaya

    APPENDIX

    Listing of the EXCEL macro for converting an LAS file into an EXCEL worksheet

    Sub comb(blank) ,f..*ff.**f*.tft~*~*~~**..***..**...~~.~~.*~.~.....*.*.****..,*..**

    Eliminate blank lines and signal end of file.. I*~.*ftf*.f**.ff*~*...**.,.**..~.*.*.~***~..*.***.*,*....*.***.*,~.

    Dim lensat As Integer Dim f4 As String l 4 Check if the line is blank or if the end of record is reached.. blank = -1 If (satir = ) Then blank = 1 Else lensat = Len(satir) ip = 1 Do While ((Mid(satir. ip, 1) = ) And (ip lensat) Then blank = 1 End If End If

    Sub inrec(ds)

    Process each line of the text file. Skip comments and blank lines.. I*.*f*f.~)ll~*f*f..*...**..*.*.*,******.**.....*~....,*~~**.,**,*.*.*

    Dim blank As Integer harfl = # satir = basla.Value Call comb(blank) Repeat reading lines until a line is encountered which is not a a blank line or a comment line or a line with a dot. If such a line is found before end of record is reached return the line otherwise raise end of file flag and return.. Do While ((blank > 0) Or (harfl = #) Or (had1 = .)) And (sonflag c 0) If (harfl = .) Then message = Mnemonic missing. Line unprocessed.. Call errmsg(d) End If Set basla = basla.Offset(l, 0) satir = basla.Value Call comb(blank) Loop

    End Sub

    Extract mnemonic field.. Do While ((Mid(satir. i, 1) .) And (Mid(satir, i, 1) ) _ 91; 9 ;= lensat))

    Loop If (i > lensat) Then Call errmsg(d) Exit Sub End If psr=i c = Mid(satir, psi, psr - psl) PSI= psr + 1 Set kutu = basla kutu.Value = c Set kutu - basla.Offset(O, 1) If (Mid(satir, i, 1) = ) Then Do While (Mid(satir, i, 1) = ) And (i

  • EXCEL macro

    If (i > lensat) Then Call errmsg(d) Exit Sub End If Extract unit field.. If a dot is not encountered than the unit field is blank.. If (Mid(satir, i, 1) = .) Then psl = i + 1 symbol = Call scanitfi, symbol) If (i > lensat) Then Call errmsg(d) Exit Sub End If psr=i c = Mid(satir, psi. psr - psl) psi = psr + 1 kutu.Value = c Else kutu.Vaiue = End If Extract the data and description fields.. If no colon is found than the remaining part of the line is regarded as the data field.. symbol = : Call scanit(i, symbol) psr = i c = Mid(satir, psi, psr - psl) psl = psr + 1 Set kutu = basla.Offset(O, 2) kutu.Value = c If (i

  • 80 S. I. &kaya

    -PARAMETER INFORMATION #Mnem.unit data description

    RH~MA.GICC RHOF.G/CC GRMAX.GAPI GRMIN.GAPI RMF.OHMM TRMF.DEGF TS.DEGF BHT.DEGF TD.FT FD.FT RW.OHMM PHINCL.OHMM RHOBCL.G/CC RCL.OHMM A. M.

    2.71 1 .oo 100.00 10.00 0.40 86.00 60.00 132.00 7430.00 7000.00 0.07 0.40 2.50 1.50 0.65 2.15 -.

    -A DATA SECTILIN 6850.0 54.0 0.30 2.33 6860.0 33.0 0.33 2.60 6870.0 50.0 0.34 2.40 6880.0 35.0 0.35 2.33 6890.0 20.0 0.24 2.35 6900.0 21.0 0.30 2.25 6910.0 42.0 0.27 2.37 6920.0 60.0 0.39 2.34 6930.0 32.0 0.29 2.21 6940.0 65.0 0.36 2.36 6950.0 25.0 0.25 2.23 6960.0 54.0 0.30 2.30 6970.0 15.0 0.26 2.15 6980.0 14.0 0.22 2.23

    13.0 0.27 2.16 6990.0 7000.0 7010.0 7020.0 7030.0 7040.0 7050.0 7060.0 7070.0 7080.0 7090.0 7100.0 LAS

    16.0 0.28 2.15 16.0 0.27 2.15 17.0 0.27 2.14 15.0 0.24 2.17 13.0 0.27 2.15 12.0 0.28 2.17 12.0 0.26 2.20 13.0 0.24 2.17 28.0 0.25 2.22 37.0 0.25 2.26 30.0 0.28 2.20

    : NEUTRON MATRIX DENSITY : FORMATION FLUID DENSITY : GAMMA RAY MAXIMUM (SHALE VALUE) : GAMMA RAY MINIMUM (CLEAN FRM) : MUD FILTRATE RESISTIVITY : TEMPERATURE OF MEASUREMENT : SURFACE TEMPERATURE : BOTTOM HOLE TEMPERATURE : TOTAL DEPTH : FORMATION DEPTH : FORMATION WATER RESISTIVITY : CLAY RESISTIVITY : CLAY DENSITY : CLAY RESITIVITY : TURTUOSITY FACTOR : CEMENTATION EXPONENT

    3 3 2 9 150 22 30 5 18 4 6 6 200 500 400 330 210 180 200 60

    1 1

    1

    4 5 4 A 3 i 10 7 120 10 22 5 32 8 5 5 18 8 4 3 5 6 6 6 200 7 500 7 400 5 330 6 250 6 180 0 300 6 85 0 1 4 1 3 1 3 1 3 1 3 1 3