Convert CSV Files to LAS Files with Python
Quick and simple conversion process to create a LAS file from scratch using LASIO and CSV Data
Well log data can be delivered in a variety of formats (DLIS, LAS, CSV, ASC etc.). If you have a CSV file containing well-log data, and you want to convert it to LAS format, you can use the LASIO library
LAS files are a simple format. They are simple text files that contain metadata about the well, parameter information, and log data measurements. These files are easy to open up in any text editor and you can quickly and easily read the content.
However, there may be occasions where you end up with a CSV file containing well-log measurements and you want to convert it to a LAS file. In this article, we are going to cover just that.
We are going to see how we can take a simple CSV file and convert it to a LAS file like this using the excellent LASIO library.
If you prefer, you can watch the version of this on my YouTube channel below.
Notebooks & Data
You can download the notebook and data from my Github Repository here: https://github.com/andymcdgeo/Petrophysics-Python-Series
Load Required Libraries
First off, we will load in the required python libraries. For this tutorial, we will be using lasio and pandas.
import lasio import pandas as pd
Load in CSV File Using Pandas
Next, we need to load our CSV file.
This is done using the
read_csv() function from pandas and passing in the file location and file name.
data = pd.read_csv('Data/Notebook 22/Notebook 22 - VOLVE - 15_9-19.csv')
Once the file has been read, we can then check what the contents of the file are by calling upon the pandas
Which returns a table containing the statistics of the data.
We can now see that we have 18 columns within our dataframe, and a mixture of well log measurements.
To ensure that the data is all numeric and to understand how many nulls are present within the data we can call upon the pandas function
.info(). This is not a necessary step, but it does allow us to check that the columns are numeric (either float64 or int64).
Create an Empty LAS Object With LASIO
Before we can transfer our data from CSV to LAS, we first need to create a blank LAS file. This is achieved by calling upon
lasio.LASFile() and assigning it to a variable. In this example, the variable is called
las_file = lasio.LASFile()
When we try to view the contents of the newly created LAS file we can see that the header information is empty.
We can also confirm that we have no data within the file by calling upon
las_file.curves, which will return an empty list.
Setting up the LAS File Metadata
Now that we have a blank las object to work with, we now need to add information to our LAS header.
The first step is to create a number of variables that we want to fill in. Doing it this way, rather than passing them directly into the HeaderItem functions makes it easier to change them in the future and also makes it more readable.
For instance, if we created a function where we wanted to update specific parameters within the header based on different files, we can easily pass these variables into the function and we won’t have to update the code within the function.
well_name = 'Random Well' field_name = 'Random Field' uwi = '123456789' country = 'Random Country'
To begin assigning the values to the header, we need to call upon
las_file.well and select the header attribute we want to add to. On the right-hand side, we will update the HeaderItem and supply a value to it.
las_file.well['WELL'] = lasio.HeaderItem('WELL', value=well_name) las_file.well['FLD'] = lasio.HeaderItem('FLD', value=field_name) las_file.well['UWI'] = lasio.HeaderItem('UWI', value=uwi) las_file.well['CTRY'] = lasio.HeaderItem('CTRY', value=country)
Once we have done this we can call upon our header again and we can now see that the values for the well name, UWI, country and field name have all been updated.
Adding a Depth Curve
To add curves to the file we can use the
add_curve function and pass in the data and units.
This example here shows how we can add a single curve to the file called DEPT. Note that if adding the main depth data, it does need to go in as DEPT rather than DEPTH.
las_file.add_curve('DEPT', data['DEPTH'], unit='m')
Writing the Remaining Curves
To make things easier, I have created a list containing the measurement units for each well log curve. Note that this does include the units for the depth measurement.
units = ['m', 'inches', 'unitless', 'us/ft', 'us/ft', 'us/ft', 'us/ft', 'API', 'v/v_decimal', 'v/v_decimal', 'v/v_decimal', 'v/v_decimal', 'v/v_decimal', 'g/cm3', 'g/cm3', 'ohm.m', 'ohm.m', 'degC']
We can then begin to loop through each of the logging measurements/columns within the data frame along with the units list. This is achieved using the Python
As we already have depth within our las file, we can skip this column by checking the column name. There are other ways in which this can be handled, for example by writing the depth and curves to the las file in one go.
for col, unit in zip(data.columns, units): if col != 'DEPTH': las_file.add_curve(col, data[col], unit=unit)
When we check the
curves function, we can see that we have all of our curves and they all have the appropriate units. We can also see from the data.shape part of the listing we have 4101 values per curve which confirms we have data.
We can confirm that we have values by calling upon of the curves. In the example below, I have called upon GR, and we get an array returned containing the Gamma Ray values, which match the values in the dataframe presented earlier.
array([ 36.621, 36.374, 30.748, ..., -999. , -999. , -999. ])
Exporting the LAS File
Once we are happy with the las file, we can now export it to a file and use it in any other software package.
In this article, we have covered how to convert a simple CSV file containing well log / petrophysical measurements into an industry-standard LAS (Log ASCII Standard) file. Once you have created a blank LASFile object in lasio, you will be able to manually update the header items with the correct metadata and also update the curves with the correct values.