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 | import whitson_connect
CLIENT = "your_domain_here" # This is the company suffix in your whitson urls ex. 'courses' in courses.whitson.com
CLIENT_ID = "your_client_id_here" # Available on request
CLIENT_SECRET = "your_client_secret" # Available on request
PROJECT_DICT = {
('PERMIAN'): 1,
}
PROJECT_ID_LIST = [value for value in PROJECT_DICT.values()]
whitson_connection = whitson_connect.WhitsonConnection(
CLIENT, CLIENT_ID, CLIENT_SECRET
)
whitson_connection.access_token = whitson_connection.get_access_token_smart()
# ------------------------------------------------------------------------------------------------------------------
# SNOWFLAKE CONNECTION
# ------------------------------------------------------------------------------------------------------------------
snowflake_connection = whitson_connection.snowflake_connection(
account='YOUR_ACCOUNT', # example: 'abc12345.east-us-1.azure'
user='YOUR_USERNAME', # example: 'WHITSON_READ'
password='YOUR_PASSWORD', # example: 'password123!'
database='YOUR_DB_NAME' # example: 'ABCDEFG_123456_WHITSON'
)
whitson_wells = whitson_connection.get_wells_from_projects(PROJECT_ID_LIST, 200)
uwi_id_dict = {item['uwi_api']: item['id'] for item in whitson_wells}
# ------------------------------------------------------------------------------------------------------------------
# 2.1 Upload well deviation surveys
# ------------------------------------------------------------------------------------------------------------------
snowflake_query = """SELECT * FROM YOUR_DB_NAME.PUBLIC.DEVIATION_SURVEY_DATA"""
snowflake_dev_df = whitson_connection.snowflake_table_to_dataframe(snowflake_connection, snowflake_query)
payloads_by_well = {}
for index, row in snowflake_dev_df.iterrows():
uwi_api = row['well_id']
md_value = row['md']
tvd_value = row['tvd']
if uwi_api in payloads_by_well:
payloads_by_well[uwi_api].append({"md": md_value, "tvd": tvd_value})
else:
payloads_by_well[uwi_api] = [{"md": md_value, "tvd": tvd_value}]
for uwi_api, payload in payloads_by_well.items():
well_id = uwi_id_dict.get(uwi_api)
if whitson_connection.is_default_deviation_survey(well_id):
try:
whitson_connection.edit_well_deviation_survey(well_id=well_id, payload=payload)
except:
print(f'{uwi_api} (well_id: {well_id}): Something is off with this well id. This is the payload: {payload}')
continue
else:
print(f'{uwi_api} (well_id: {well_id}): Well deviation survey already uploaded for this well.')
# ------------------------------------------------------------------------------------------------------------------
# 2.2 Upload top and bottom perforations
# ------------------------------------------------------------------------------------------------------------------
snowflake_query = """SELECT * FROM YOUR_DB_NAME.PUBLIC.PERFORATIONS"""
snowflake_perf_df = whitson_connection.snowflake_table_to_dataframe(snowflake_connection, snowflake_query)
processed_wells = {}
for index, row in snowflake_perf_df.iterrows():
uwi_api = row['uwi']
top_md = row['depthtop']
bottom_md = row['depthbtm']
if uwi_api in processed_wells:
continue
payload = {
"top_perforation_md": top_md,
"bottom_perforation_md": bottom_md
}
well_id = uwi_id_dict.get(uwi_api)
if whitson_connection.is_default_perforated_interval(well_id):
try:
whitson_connection.edit_perf_interval(well_id, payload=payload)
except:
print(f'{uwi_api} (well_id: {well_id}): Something is off with this well id. This is the payload: {payload}')
else:
print(f'{uwi_api} (well_id: {well_id}): Perforated interval is already uploaded for this well.')
processed_wells[uwi_api] = True
# -------------------------------------------------------------------------------------------------------------------
# 2.3.1 Upload Casings First
# -------------------------------------------------------------------------------------------------------------------
snowflake_query = """SELECT * FROM YOUR_DB_NAME.PUBLIC.CASING"""
snowflake_casing_df = whitson_connection.snowflake_table_to_dataframe(snowflake_connection, snowflake_query)
well_payloads = {}
def _get_well_casing_data(uwi, group):
""" Returns the casing data in relevant format. Returns None if not a production casing is found. """
if 'Production' not in group['casingdes'].values:
return None, None
well_id = uwi_id_dict.get(uwi)
top_md = 0
bottom_md = whitson_connection.get_max_md_well_deviation_data(well_id)
d_casing_inner = whitson_connection.round_to_significant_digits(group['szidnommincalc'].min() * 39.37) # in this example DB is in m, need to convert to inches
use_from_date = group['dttmspud'].iloc[0].strftime('%Y-%m-%d')
well_casing_data = [ {
"d_casing_inner": d_casing_inner,
"pipe_number": 1,
"k_casing": roughness,
"bottom_md": bottom_md,
"top_md": top_md,
}
]
return well_casing_data, use_from_date
# Define some example values
last_valve_open = True
calculate_from_gauge = False
compute_through = "flowing_side"
compute_static_down_to = "end_of_tubing"
roughness = 0.0006 # default in whitson+
well_data_casing = []
for uwi, group in snowflake_casing_df.groupby('uwi'):
well_data_casing, use_from_date = _get_well_casing_data(uwi, group)
if well_data_casing is not None:
well_payload = {
"use_from_date": use_from_date,
"flow_path": "casing", # Placeholder, replace with actual logic if available
"lift_method": "none", # Placeholder, replace with actual logic if available
"gas_lift_config": 'automatic', # Placeholder, replace with actual logic if available
"last_valve_open": last_valve_open,
"gauge_depth": None, # Placeholder, replace with actual logic if available
"calculate_from_gauge": calculate_from_gauge,
"compute_through": compute_through,
"compute_static_down_to": compute_static_down_to,
"well_data_casing": well_data_casing,
"well_data_tubing": [], # Placeholder, add logic to populate if needed
"gas_lift_data": [] # Placeholder, add logic to populate if needed
}
# Append the payload to the well_payloads dictionary
if uwi not in well_payloads:
well_payloads[uwi] = []
well_payloads[uwi].append(well_payload)
# -------------------------------------------------------------------------------------------------------------------
# # 2.3.2 Upload Tubings Second
# -------------------------------------------------------------------------------------------------------------------
snowflake_query = """SELECT * FROM YOUR_DB_NAME.PUBLIC.TUBING"""
snowflake_tubing_df = whitson_connection.snowflake_table_to_dataframe(snowflake_connection, snowflake_query)
# Iterate through each UWI
for uwi, tubing_group in snowflake_tubing_df.groupby('uwi'):
for install_date, tubing in tubing_group.groupby('dttmspud'):
tubing = tubing[tubing['compsubtyp'] == 'tubing']
tubing_bottom_md = tubing['depthbtm'].max() * 3.28084 # Convert from meter to ft
install_date = install_date.strftime('%Y-%m-%d')
# Filter rows where 'compsubtyp' is 'tubing'
df_tubing = tubing[tubing['compsubtyp'] == 'tubing']
# Check if all values in 'szidnom' and 'szodnom' are the same
szidnom_unique = df_tubing['szidnom'].nunique()
szodnom_unique = df_tubing['szodnom'].nunique()
if szidnom_unique != 1 or szodnom_unique != 1:
print("Values of 'szidnom' and/or 'szodnom' are not the same for all tubing sizes.")
# Select the most common value for tubing ID and OD 'szidnom' and 'szodnom'
tubing_id = whitson_connection.round_to_significant_digits(df_tubing['szidnom'].mode().iloc[0] * 39.37) # in this example DB is in m, need to convert to inches
tubing_od = whitson_connection.round_to_significant_digits(df_tubing['szodnom'].mode().iloc[0] * 39.37) # in this example DB is in m, need to convert to inches
if tubing_od <= tubing_id:
print(f'WARNING! Tubing ID ({tubing_od}) > Tubing OD ({tubing_id}). Tubing OD will be set to tubing ID / 0.84 : {tubing_od/0.84}')
tubing_od = whitson_connection.round_to_significant_digits(tubing_id/0.84)
well_data_tubing = [
{
"pipe_number": 1,
"d_tubing_inner": tubing_id,
"d_tubing_outer": tubing_od,
"k_tubing": roughness,
"bottom_md": tubing_bottom_md, # Assembly_Btm_Md of last tubing string
}
]
well_data_casing = well_payloads.get(uwi, [{}])[0].get('well_data_casing')
if well_data_casing == None:
continue
glv_exists = bool(tubing['des'].str.contains('AGL|GL', case=False, na=False).any())
lift_method = 'gas_lift' if glv_exists else 'none'
# Define the well payload for this api_no12
well_payload = {
"use_from_date": install_date,
"flow_path": "unknown",
"lift_method": lift_method,
"gas_lift_config": 'automatic',
"last_valve_open": last_valve_open,
"gauge_depth": None,
"calculate_from_gauge": calculate_from_gauge,
"compute_through": compute_through,
"compute_static_down_to": compute_static_down_to,
"well_data_casing": well_data_casing,
"well_data_tubing": well_data_tubing,
"gas_lift_data": []
}
# Append the payload to the well_payloads dictionary
if uwi not in well_payloads:
well_payloads[uwi] = []
well_payloads[uwi].append(well_payload)
for uwi, new_well_configurations in well_payloads.items():
well_id = uwi_id_dict.get(uwi)
existing_wellbore_data = whitson_connection.get_well_data(well_id)
# Delete the default wellbore if the well has a default
try:
if existing_wellbore_data and whitson_connection.is_default_well_configuration(existing_wellbore_data):
well_data_id = existing_wellbore_data[0]['id']
whitson_connection.delete_wellbore_config_by_well_data_id(well_data_id=well_data_id)
# Check if there is a well config upload for this date already
for new_well_configuration in new_well_configurations:
if whitson_connection.is_wellbore_configuration_already_uploaded(new_well_configuration, existing_wellbore_data):
print('already uploaded for this date')
continue
else:
try:
whitson_connection.upload_well_data_to_well(well_id, new_well_configurations)
except:
print(str(uwi) +': failed for this well. Here is the payload. ' + str(new_well_configuration))
except:
print('Something went wrong. Here is the api number ' + str(well_id) + 'and existing wellbore data ' + str(existing_wellbore_data)
|