Skip to content

Demo Example

This demo demonstrate a typical example that uses API to automate workflow.

The steps of the workflow include:

  • Create a project
  • Create a well inside of the project
  • Upload production data to well
  • Change initial GOR based on production data
  • Add PVT data to the well and run composition calculation
  • Upload wellbore configuration data
  • Run BHP calculation on the well

Use endpoints as functions

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import http.client
import json
import os

import requests

CLIENT_NAME = **your client name**


def get_fields(access_token: str):
    base_url = f"https://{CLIENT_NAME}.whitson.com/api-external/v1/"
    response = requests.get(
        base_url + "fields",
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
    )
    res = response.json()
    if not res:
        raise Exception("no existing fields")
    return res


def get_wells(
    access_token: str,
    project_id: int | None = None,
    name: str | None = None,
    uwi_api: str | None = None,
):
    """
    Get a list of wells.
    """
    base_url = f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells"
    response = requests.get(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        params={"project_id": project_id, "name": name, "uwi_api": uwi_api},
    )
    res = response.json()
    if not res:
        raise Exception("no existing wells")
    return res


def get_projects(
    access_token: str,
    field_id: int | None = None,
):
    """
    Get a list of fields.
    """
    base_url = (
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/fields/{field_id}/projects"
    )
    response = requests.get(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
    )
    res = response.json()
    if not res:
        raise Exception("no existing wells")
    return res


def create_well(access_token: str, payload: dict) -> requests.Response:
    """
    upload well information to create a well in the database
    """
    base_url = f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells"
    response = requests.post(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"successfully created well {payload['name']}")
    else:
        print(response.text)
    return response


def create_project(access_token: str, field_id, payload: dict) -> requests.Response:
    """
    upload well information to create a well in the database
    """
    base_url = (
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/fields/{field_id}/projects"
    )
    response = requests.post(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"successfully created project {payload['name']}")
    else:
        print(response.text)
    return response


def upload_production_to_well(
    access_token: str, well_id: int, payload: list[dict]
) -> requests.Response:
    """
    Upload production to well.
    """
    response = requests.post(
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/production_data",
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"successfully updated production data on well {well_id}")
    else:
        print(response.text)
    return response


def bulk_upload_production_to_well(
    access_token: str, payload: list[dict]
) -> requests.Response:
    """
    Upload production to well.
    """
    response = requests.post(
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/production_data",
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print("success")
    else:
        print(response.text)
    return response


def get_production(
    access_token: str,
    well_id: int,
) -> requests.Response:
    """
    Get a list of wells.
    """
    response = requests.get(
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/production_data",
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
    )
    res = response.json()
    if not res:
        raise Exception("no existing wells")
    return res


def edit_input_quick(
    access_token: str, well_id: int, payload: dict
) -> requests.Response:
    """
    Edit the input quick (PVT) property of a well.
    """
    response = requests.put(
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/input_quick",
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"successfully edited input quick for well {well_id}")
    else:
        print(response.text)
    return response


def upload_well_data_to_well(
    access_token: str, well_id: int, payload: list[dict]
) -> requests.Response:
    """
    Upload a well data to a well.
    """
    base_url = f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/bhp_input/well_data"
    response = requests.post(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"successfully updated well_data to well {well_id}")
    else:
        print(response.text)
    return response


def edit_well_deviation_data(
    access_token: str, well_id: int, payload: list[dict]
) -> requests.Response:
    """
    Edit well deviation data of a well in the database.
    """
    base_url = f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/bhp_input/well_deviation_survey"
    response = requests.put(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
        json=payload,
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"changed well deivation survey on well_id {well_id}")
    else:
        print(response.text)
    return response


def run_composition_calc(access_token: str, well_id: int) -> requests.Response:
    """
    Run composition calculation on well.
    """
    response = requests.get(
        f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/run_composition_calc",
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
    )
    if response.status_code >= 200 and response.status_code < 300:
        print(f"success on running composition calc on well {well_id}")
    else:
        print(response.text)
    return response


def run_bhp_calc(access_token: str, well_id: dict) -> requests.Response:
    """
    Run bhp calculation on the well specified by the provided well_id
    """
    base_url = f"http://{CLIENT_NAME}.whitson.com/api-external/v1/wells/{well_id}/run_bhp_calculation"
    response = requests.get(
        base_url,
        headers={
            "content-type": "application/json",
            "Authorization": f"Bearer {access_token}",
        },
    )
    if response.status_code == 202:
        print(f"successfully ran bhp calc on {well_id}")
    else:
        print(response.text)
    return response

Demo Workflow

1. Know which field you are working with and create project in the field

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
field_id = 1

# create project
response = create_project(
    ACCESS_TOKEN,
    field_id=field_id,
    payload={
        "name": "test project",
        "owner": "jason@whitson.com",
        "company_wide": True,
        "note": "external api integration",
    },
)
assert response.status_code >= 200 and response.status_code < 300

2. Get project id of newly created project

1
2
3
4
5
6
projects = get_projects(ACCESS_TOKEN, field_id=field_id)
project_id = None
for project in projects:
    if project["name"] == "test project":
        project_id = project["id"]
assert project_id is not None

3. Create a well in the project

Well Identifiers:

The rules to follow regarding well identifiers in whitson+:

  • well_id, or id is the primary identifier for a well, you will see this throughout the whitson+ API documentation.
  • well_name, or name is the preferred identifier for a well outside of well_id. well_name must be unique within a project, but a well with the same well_name can exist in a different project.
  • uwi_api is a secondary identifier in whitson+, there are no restrictions on the value of uwi_api other than it needs to be a string and not NULL.
  • external_id is an identifier purely designed for backend process and can not be found on the web GUI. external_id is globally unique just like well_id, and is most suitable for the database ids you have for the wells in your own database.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
well_name = "SPE-DATA-REPOSITORY-DATASET-1-WELL-1-OSPREY"
well_payload = {
    "name": well_name,
    "external_id": "leroy_jenkins",
    "uwi_api": "SPE-DATA-REPOSITORY-DATASET-1-WELL-1-OSPREY",
    "project_id": project_id,
    "t_res": 225,  # reservoir temperature
    "p_res_i": 5400,  # initial reservoir pressure
    "gor": 3300,  # initial gor
    "h": 78,  # reservoir height
    "h_f": 78,  # fracture height
    "phi": 0.063,  # porosity
    "l_w": 5883,  # lateral length
    "n_f": 252,  # number of fractures
    "Sw_i": 26,  # initial water saturation
    "fluid_pumped": 275579,
    "prop_pumped": 13514540,
    "stages": 28,
    "clusters": 252,
}
response = create_well(access_token=ACCESS_TOKEN, payload=well_payload)
assert response.status_code >= 200 and response.status_code < 300

4. Get well id of newly created well

1
2
3
4
# get well id of newly created well
wells = get_wells(ACCESS_TOKEN, project_id=project_id, name=well_name)
assert len(wells) == 1
well_id = wells[0]["id"]

5. Upload production data to a well

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
with open("data/production_payload.json", "r") as file:
    data = file.read()
# Parse the cleaned JSON data
data = json.loads(data)
production_payload = []
for production_record in data:
    if well_name == production_record.pop("name"):
        production_record["well_id"] = well_id
        production_payload.append(production_record)
response = upload_production_to_well(
    ACCESS_TOKEN, well_id, {"production_data": production_payload}
)
assert response.status_code >= 200 and response.status_code < 300

6. Change initial GOR with the median GOR of the first 30 days of production

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from datetime import datetime
import statistics

# Fetch the production data from whitson+ API
production_data = get_production(ACCESS_TOKEN, well_id)

# Sort production data by date
for production_i in production_data:
    production_i["date"] = datetime.strptime(production_i["date"], "%Y-%m-%dT%H:%M:%S")
sorted_production = sorted(production_data, key=lambda x: x["date"])

# Median of the first 30 days of GOR
gor_init = statistics.median([production_i["gor_sc"] for production_i in sorted_production[:30]])

7. Change PVT data

1
2
3
4
5
6
7
8
# add PVT data
payload = {
    "gor": gor_init,
    "gor_only": True,
}
response = edit_input_quick(ACCESS_TOKEN, well_id, payload)
assert response.status_code >= 200 and response.status_code < 300
# Composition calcs are automatically triggered after editing the input

8. Upload deviation survey data to a well

1
2
3
4
5
6
with open("data/demo_deviation_surveys.json", "r") as file:
    data = file.read()
# Parse the cleaned JSON data
deviation_surveys = json.loads(data)
response = edit_well_deviation_data(ACCESS_TOKEN, well_id, deviation_surveys)
assert response.status_code >= 200 and response.status_code < 300

9. Upload wellbore data to a well

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use_from_date = "2015-03-05"
well_data_payload = [
    {
        "use_from_date": use_from_date,
        "flow_path": "tubing",
        "lift_method": "rod_pump",
        "well_data_casing": [
            {
                "bottom_md": 7029,
                "d_casing_inner": 6.276,
                "k_casing": 0.0006,
                "pipe_number": 1,
                "top_md": 0,
            },
            {
                "bottom_md": 13915,
                "d_casing_inner": 4.0,
                "k_casing": 0.0006,
                "pipe_number": 2,
                "top_md": 6936,
            },
        ],
        "well_data_tubing": [
            {
                "bottom_md": 7022,
                "d_tubing_inner": 2.441,
                "d_tubing_outer": 2.875,
                "k_tubing": 0.0006,
                "pipe_number": 1,
            }
        ],
    }
]
response = upload_well_data_to_well(ACCESS_TOKEN, well_id, well_data_payload)
assert response.status_code >= 200 and response.status_code < 300

10. Run BHP calculation on a well

1
2
response = run_bhp_calc(ACCESS_TOKEN, well_id)
assert response.status_code >= 200 and response.status_code < 300