-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
318 lines (223 loc) · 9.77 KB
/
main.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# This Python file uses the following encoding: utf-8
"""
Copyright (C) 2020 Michael Wentz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
You can contact me at michaelwiv@gmail.com
"""
import datetime
import json
import os
import sys
import webbrowser
from pathlib import Path
import matplotlib as mpl
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import QFile, QObject, Signal, Slot
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import (
QApplication,
QComboBox,
QFileDialog,
QMainWindow,
QMessageBox,
)
import eia_data.commodity_prices as prices
import petrolpy_equations.petrolpy_equations as petrolpy
from model.cashflow import create_cashflow as model_create_cashflow
from model.create_decline_curve import (
create_decline_curve as model_create_decline_curve,
)
from model.custom_models import ListModel, TableModel
from model.import_data import import_data as model_import_data
from model.mpl_canvas_widget import DynamicMplCanvas
from model.plot_decline_curve import plot_decline_curve as model_plot_decline_curve
from model.plot_production import plot_production as model_plot_production
from model.set_production_widgets import (
set_production_widgets as model_set_production_widgets,
)
from ui_mainwindow import Ui_main_window
mpl.use("Qt5Agg")
VERSION = "0.3.0-beta"
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_main_window()
self.ui.setupUi(self)
# Application variables
self.model = ListModel()
self.well_dataframes_dict = {}
self.decline_curves_dict = {}
self.model_oil_curves = ListModel()
self.model_gas_curves = ListModel()
# Cashflow models
self.model_cashflow_monthly = TableModel()
self.model_cashflow_annual = TableModel()
self.model_cashflow_summary = TableModel()
# Production Plot setup
self.widget_production_plot = DynamicMplCanvas(self.ui.widgetProductionPlot)
toolbar = NavigationToolbar2QT(
self.widget_production_plot, self.ui.widgetProductionPlot
)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(toolbar)
layout.addWidget(self.widget_production_plot)
self.ui.widgetProductionPlot.setLayout(layout)
self.ui.comboBoxPhase.addItems(["Oil", "Gas"])
self.ui.comboBoxUnits.addItems(["BOPM/MCFPM", "BOPD/MCFPD"])
def help_tutorial(self):
"""Open tutorial from Read the Docs"""
webbrowser.open(
"https://petroalchemy.readthedocs.io/en/latest/alpha_tutorial.html"
)
def help_documentation(self):
"""Open Read the Docs home page"""
webbrowser.open("https://petroalchemy.readthedocs.io/en/latest/")
def help_website(self):
"""Open Github Pages website"""
webbrowser.open("https://mwentzww.github.io/PetroAlchemy/")
def help_bug(self):
"""Open github bug report"""
webbrowser.open(
"https://github.com/mwentzWW/PetroAlchemy/issues/new?assignees=mwentzWW&labels=bug&template=bug_report.md&title="
)
def help_feature(self):
"""Open github feature request"""
webbrowser.open(
"https://github.com/mwentzWW/PetroAlchemy/issues/new?assignees=mwentzWW&labels=enhancement&template=feature_request.md&title="
)
def help_open_bugs_features(self):
"""Open current bugs and features on github"""
webbrowser.open("https://github.com/mwentzWW/PetroAlchemy/issues")
def help_contact(self):
"""Open linkedin page"""
webbrowser.open("https://www.linkedin.com/in/mwentzww/")
def about_version(self):
"""Message with current version info"""
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(f"💻 Version installed: {VERSION}")
msg.setWindowTitle("Current Version")
msg.exec_()
def about_take_survey(self):
"""Open survey"""
webbrowser.open("https://www.surveymonkey.com/r/F22RYZ5")
def about_donate(self):
"""Open paypal link"""
webbrowser.open("https://paypal.me/MichaelWentz")
def import_data(self):
model_import_data(self)
self.ui.comboBoxWellSelect.setModel(self.model)
self.ui.wellListView.setModel(self.model)
@Slot(str)
def well_selected(self, well_name):
"""Well selected in comboBoxWellSelect changed, and triggers production plot to redraw"""
self.widget_production_plot.plot_production(self, well_name, init=True)
self.ui.widgetProductionPlot.update()
self.widget_production_plot.update()
@Slot(str)
def units_changed(self, units):
"""Well units selected (BOPM/MCFPM or BOPD/MCFPD) and label needs to refresh"""
self.widget_production_plot.axes.set_ylabel(units)
self.widget_production_plot.draw()
self.ui.widgetProductionPlot.update()
self.widget_production_plot.update()
@Slot(str)
def phase_changed(self, phase):
"""Updates widgets based on phase selected"""
well_name = self.ui.comboBoxWellSelect.currentText()
model_set_production_widgets(self, well_name, phase=phase)
def create_decline_curve(self):
"""Calls create_decline_curve from model/"""
model_create_decline_curve(self)
# Production Plot comboboxes
self.ui.comboBoxOilDeclineCurves.setModel(self.model_oil_curves)
self.ui.comboBoxGasDeclineCurves.setModel(self.model_gas_curves)
# Cashflow comboboxes
self.ui.comboBoxOilDeclineCurve.setModel(self.model_oil_curves)
self.ui.comboBoxGasDeclineCurve.setModel(self.model_gas_curves)
self.model_oil_curves.layoutChanged.emit()
self.model_gas_curves.layoutChanged.emit()
def reset_plot(self):
"""Resets plot with production"""
well_name = self.ui.comboBoxWellSelect.currentText()
self.widget_production_plot.plot_production(self, well_name)
def plot_decline_curves(self):
"""Plots the selected curves on the plot"""
# Checks whether to use oil, gas, or both curves
use_oil = self.ui.checkBoxUseOil.isChecked()
use_gas = self.ui.checkBoxUseGas.isChecked()
oil_curve = self.ui.comboBoxOilDeclineCurves.currentText()
gas_curve = self.ui.comboBoxGasDeclineCurves.currentText()
if use_oil and use_gas:
model_plot_decline_curve(self, oil_curve, reset=False)
model_plot_decline_curve(self, gas_curve, reset=False)
elif use_oil:
model_plot_decline_curve(self, oil_curve, reset=False)
elif use_gas:
model_plot_decline_curve(self, gas_curve, reset=False)
else:
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(f"Use the two checkboxes to use the decline curves 👀")
msg.setWindowTitle("No Decline Curves Used")
msg.exec_()
def delete_decline_curves(self):
"""Deletes the selected curves on the plot"""
# Checks whether to use oil, gas, or both curves
use_oil = self.ui.checkBoxUseOil.isChecked()
use_gas = self.ui.checkBoxUseGas.isChecked()
oil_curve = self.ui.comboBoxOilDeclineCurves.currentText()
gas_curve = self.ui.comboBoxGasDeclineCurves.currentText()
if use_oil and use_gas:
self.model_oil_curves.delete(oil_curve)
del self.decline_curves_dict[oil_curve]
self.model_gas_curves.delete(gas_curve)
del self.decline_curves_dict[oil_curve]
elif use_oil:
self.model_oil_curves.delete(oil_curve)
del self.decline_curves_dict[oil_curve]
elif use_gas:
self.model_gas_curves.delete(gas_curve)
del self.decline_curves_dict[oil_curve]
else:
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(f"Use the two checkboxes to use the decline curves 👀")
msg.setWindowTitle("No Decline Curves Used")
msg.exec_()
def create_cashflow(self):
"""Creates cashflow from setup page inputs"""
model_create_cashflow(self)
self.ui.tableViewCashflow.setModel(self.model_cashflow_monthly)
self.ui.tableViewSummaryValues.setModel(self.model_cashflow_summary)
def save_cashflow(self):
"""Save cashflow and summary output to excel"""
fileName = QFileDialog.getSaveFileName(self, "Save Cashflow to Excel")[0]
with pd.ExcelWriter(fileName) as writer:
self.df_summary_output.to_excel(
writer, sheet_name="PetroAlchemy Summary Output", index=False
)
self.df_cashflow_monthly.to_excel(
writer, sheet_name="PetroAlchemy Cashflow"
)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())