NoCode and AI
Python and Data Science

ChatGPT for Oil and Gas - Part 3: Python (for Non-Coders)

Alan Mourgues
February 28, 2024

In Part 1 of the series, we explored all the text-related capabilities of the ChatGPT language model, including text generation, proofreading, translation, and summarization.

Then, in Part 2, we delved into ChatGPT's advanced capabilities, ranging from creating Excel VBA codes to interpreting data from images for insightful analyses. We provided practical examples, such as generating production profiles and adding interactive features to Excel.

Now, it's time to delve into Python.

Brief Python Rundown

Python is a high-level programming language known for its ease of use and readability, making it accessible even to those who are new to coding. In recent years, it has gained significant traction in the Oil & Gas industry due to its versatility in handling various applications and data analysis methods.

Python works by allowing users to write 'snippets' or blocks of code, which are sequences of commands and functions designed to perform specific tasks. These snippets are written in a straightforward syntax that closely resembles natural language, reducing the learning curve.

Once a snippet is created, it is run or executed by a Python interpreter, a tool that reads the code and carries out the instructions it contains. This execution process translates the commands into actions, whether it's data analysis, automation of tasks, or even controlling complex systems.

Let's see it in action, leveraging ChatGPT to create snippets of code for tasks we explain in plain English.

Prompt:

“create a python script that asks the user for the initial rate in MMscfd and the decline rate in percent per annum and generates a production profile plot in MMscf for the first 10 years using the exponential decline formula

Output:

This is from the ChatGPT chat instance (which you can access in full, here):

Ok, so we've got a piece of code. Strictly speaking, we don’t need to know anything about coding, but we do know that we have a block of Python code with instructions to perform the task we want. So, what do we do with this?

Well, we need to run this code, and for that, we need to bring the snippet to a code interpreter, as explained in the initial rundown.

Two popular methods for running Python code snippets are using Google Colab or running the code locally on your computer.

Both methods have their advantages, and the choice often depends on your specific needs and preferences. Google Colab simplifies access to high-end resources, while running locally with VS Code offers more control and is well-suited for offline development.

  1. Google Colab: This is a free, cloud-based service provided by Google. It allows you to write and execute Python in your browser, with no setup required. Google Colab also offers free access to powerful computing resources, making it an excellent choice for more complex or resource-intensive tasks. This method is ideal if you want to leverage Google's infrastructure without installing anything on your computer. There are plenty of YouTube tutorials on how to copy your Python snippet into a new Colab notebook and run it. You can explore these at your own pace if you're interested (it's worthwhile!). For the rest of the tutorial, we’ll go with the second method.
  2. Running Locally: If you prefer to run code on your own computer, you can set up a local Python environment. One of the best tools for this is Visual Studio Code (VS Code), a free, open-source code editor developed by Microsoft. It's versatile and supports Python programming out of the box. After installing Python and VS Code, you can open your Python snippet in VS Code and run it directly. This method gives you full control over your coding environment and works well for both simple scripts and larger projects.

I know what you’re thinking: “Ugh... this is already too complicated. Installing stuff, dealing with many parts and big words... you lost me at Python environment.

Bear with me. Installing Python and VS Code is a one-off process, and thereafter you’re equipped to run Python on your computer any time you want!

Plus, you can ask ChatGPT for step-by-step instructions on how to install Python and VS Code and then how to run a Python snippet inside VS Code. Let’s do just that.

Prompt:

give me step-by-step instructions to install VS Code on my Windows PC

Output:

All you need to do is follow this straightforward, 'child-proof' set of instructions provided by ChatGPT. Once done, you've got VS Code on your desktop, hooray! 🎉

Launch it by double clicking on your new VS Code icon, and you’ll see this:

Now that we’re inside VS Code, what exactly is it that we do with our snippet of code here? Let’s ask ChatGPT, once more:

Prompt:

I have VS Code open and I got a snippet of code as a block of text. Please guide me through the steps to run this code

Output:

Ok, the reply provides us with a step-by-step guide on how to turn the piece of code into a .py file, which we then simply run. The only missing step is the one-off installation of the Python interpreter in VS Code.

Prompt:

please guide me on how to install the interpreter for Python

Output:

Ok, it might look like a lot of steps and technical terms, but it's actually quite simple. Here's what you need to do:

It might feel a bit tricky at first, but it's really worth it. I want to emphasize that you only need to set it up once, and then you can run Python on your computer indefinitely. Plus, you'll have ChatGPT to assist you in creating Python code for whatever you need, which is exactly the whole point.

So, let’s scroll up to the answer we got before about creating a .py file inside VS Code:

Step 1: Create a New File

  1. Open a New File: In VS Code, go to File > New File from the menu bar at the top. This will open a new, blank file.
  2. Paste Your Code: Copy the code snippet you have and paste it into this new file in VS Code.

Step 2: Save the File

  1. Save the File: Go to File > Save As, and save the file with the appropriate file extension for the language of your code (e.g., .py for Python). Choose a convenient location on your computer to save this file.

Let’s do just that; paste the code snippet ChatGPT provided for us, and save the file with the name ‘Decline.py’.

You could have also pasted the code into a Notepad window, saved it with the .py extension, and then simply opened that .py file in VS Code. Same deal.

All that's left to do is run the code… finally!

Hit the PLAY button at the top right to run the code and see how the Terminal window at the bottom prompts for the Initial rate and decline rate, just as we had requested.

After entering the two inputs, watch the graph magically appear on your screen.

Congratulations, you’ve just successfully run your first Python script! 🥳

Now that you're a Python expert, you can get fancier and tweak your code. Suppose you want a more attractive user interface to request the two inputs from the user, and you also want the graph to be presented with a log scale for the rate (vertical) axis.

Prompt:

create a python script that creates a nice user interface in the middle of the screen to ask the user for two inputs: the initial rate in MMscfd and the decline rate in percent per annum. The code then generates a production profile plot in MMscf for the first 10 years using the exponential decline formula and present the result in a graph with the Y axis in log scale

Output:

Again, you don’t need to understand much about the code. Simply copy it and follow the steps that ChatGPT gives you. In this case, it instructs you to install a couple of libraries using a simple pip command, and then repeat the process to create a .py file and run it.

In this GIF, I run the code snippet in the new .py file I created. See how the inputs are prompted inside a nicer-looking little window (the user interface), and now the production profile is graphed with the Y-axis in log-scale, making the exponential decline trend appear as a straight line, just as we expected.

Let’s explore one more thing. Imagine you have a spreadsheet with three columns depicting a pressure profile as a function of distance to the well (Pr vs r), as well as for various times. In other words, a table with columns for r, Pr, and t. Let’s create an animated GIF to see how the pressure profile changes over time. In the next issue, I’ll show you this very tool to generate and animate a pressure profile inside Excel.

Prompt:

I have an Excel table with three columns r, Pr, t. Please generate a Python code snippet to generate an animated chart that shows Pr vs r for all t values

Output:

The reply first tell you you need to install pandas and then it gives you the code snippets with additional comments and instructions on how to run it.

In the next GIF, I show the structure of the Excel file and the code pasted in VS Code, just as we've done before, along with the change I made to include the path to the file. When running the code, the animation is generated, just as we requested.

As an extra touch, we can go back to ChatGPT and ask it to add a piece of code to save the animation as .gif file:

Prompt:

Add a code instruction to save the animation as a gif file

Output:

In the reply (here), ChatGPT first instructs you to install ‘pillow’ (one does not need to know what this is!), and then it provides the updated code. This includes a simple extra line where you only need to edit the path to your output file. Just for fun, we can also alter the speed of the animation by reducing the interval parameter to 20 (meaning 20 milliseconds between frames, as opposed to the original 200).

Here’s the resulting GIF file:

And voilà, you're off to the races with Python. You're now equipped with a powerful toolkit that can significantly multiply your capabilities. Go ahead and start experimenting.

Let's leave it here for now. I hope this issue has sparked your curiosity about using ChatGPT alongside Python for various tasks.

Stay tuned for our final issue, where we'll delve deeper into data analytics and explore other insightful topics.

---

PS: Everything I’m sharing through this series was demonstrated on-screen during a webinar I gave on November 16, 2023. Watch it here:

More Tools

Explore a curated collection of valuable resources in our Store, all designed to help you upskill. Currently, you can take advantage of three exclusive, limited-time offers, each crafted to aid in funding our initial marketing efforts.

Alan is a Consulting Petroleum Reservoir Engineer with 20+ years of international industry experience. Alan is the founder of CrowdField, a marketplace that connects Oil & Gas and Energy businesses with a global network of niche talent for task-based freelance solutions. His mission is to help skilled individuals monetize their knowledge as the Energy transition unfolds, by bringing their expertise to the open market and creating digital products to sell in CrowdField's Digital Store.

Featured...

All blog posts

"O&G AI Wave"
Newsletter

Subscribe to O&G AI Wave – your indispensable guide to the evolving world of AI and NoCode and their practical applications for Oil & Gas professionals.

Our newsletter brings you the latest trends and insights in AI and NoCode technologies directly impacting the O&G sector.

Every issue is packed with expert analyses and practical tips to help you navigate and leverage these transformative technologies.

Don’t miss out on the opportunity to stay ahead of the trend and future-proof your career in this incredibly dynamic field.

How we can help

At CrowdField, our mission is to empower YOU—helping you showcase your skills in the open market and monetize them effectively. Here's how you benefit from being with us:

👉 Make your mark and find freelance opportunities by listing yourself in our freelancer directory to get noticed. Join us for free here.

👉 Turn your skills into digital products that sell. We'll help you polish, launch, and list them in our Store, promote them on our LinkedIn page, to our email subscribers, and feature your success in a case study on our Blog, amplifying both your product and personal brand. Whether you're at the idea stage, midway through implementation, or nearing completion, if you see potential for monetization, we're interested. Take the first step by getting in touch to start a conversation at hello@crowdfield.net.

👉 Discover bargains in our digital store with heavily discounted prices during our market discovery period. Take advantage of these limited-time offers as we expand our network. Dive in now and find your gem! Got to Store