-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
274 lines (252 loc) · 9.68 KB
/
utils.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
import pandas as pd
import numpy as np
import os
import colorsys
def get_production_monthly(update=False,
p=r'./data/NOD/production_monthly.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
# monthly by field saleable
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/field_production_monthly&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
# sum wellbores monthly by field
# pp = r"https://factpages.sodir.no/public?/Factpages/external/tableview/field_production_gross_monthly&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_production_yearly(update=False,
p = r'./data/NOD/production_yearly.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/field_production_yearly&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_field_inplace_volumes(update=False,
p=r'./data/NOD/inplace_volumes.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/field_in_place_volumes&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_field_reserves(update=False,
p=r'./data/NOD/reserves.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/field_reserves&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_all_wells(update=False,
p=r'./data/NOD/wells_all.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/wellbore_all_long&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p, index=False)
return df
def get_development_wells(update=False,
p=r'./data/NOD/wells_dev.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/wellbore_development_all&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_exploration_wells(update=False,
p=r'./data/NOD/wells_exploration.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/wellbore_exploration_all&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_wells_with_dst(update=False,
p=r'./data/NOD/wells_with_dst.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp=r"https://factpages.sodir.no/public?/Factpages/external/tableview/wellbore_dst&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false"
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_field_status(update=False,
p=r'./data/NOD/field_status.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp = r'https://factpages.sodir.no/public?/Factpages/external/tableview/field_activity_status_hst&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false'
df = pd.read_csv(pp)
df.to_csv(p)
return df
def get_field_overview(update=False, p=r'./data/NOD/field_overview.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp = r'https://factpages.sodir.no/public?/Factpages/external/tableview/field&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false'
df = pd.read_csv(pp)
df.to_csv(p, index=False)
return df
def get_field_description(update=False,
p=r'./data/NOD/field_description.csv'):
'''
Parameters
----------
update: bool
if True (or "p" does not exists): fetch csv-data from FactPages and save to "p"
if False (and "p" exists): reload local data from "p"
p: str
path to a csv file
'''
if os.path.exists(p) and not update:
df = pd.read_csv(p)
else:
pp = r'https://factpages.sodir.no/public?/Factpages/external/tableview/field_description&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&IpAddress=not_used&CultureCode=en&rs:Format=CSV&Top100=false'
df = pd.read_csv(pp)
df.to_csv(p)
return df
def generate_rainbow_colors(N, saturation=1.0, brightness=1.0, gap=.2):
'''Generates N rainbow colors with specified saturation and brightness
which can be used in Plotly charts.
Parameters
------------
N : int or list-like
number of colors or list-like item
saturation : float
brightness : float
gap : float
if <1, prevents the far right part of the spectrum from coinciding with
the violet one in the left
Returns
-------
colors: list of strings
e.g. ['rgb(255,0,0)', 'rgb(51,255,0)', 'rgb(0,102,255)']
Example
-------
# Generates and plots 20 colors:
N = 20
rainbow_colors = generate_rainbow_colors(N)
fig = go.Figure(
data=[go.Bar(x=list(range(N)), y=[1]*N, marker_color=rainbow_colors)])
fig.show()
'''
colors = []
max_hue = 1.0-gap
if isinstance(N,int):
NN = np.linspace(0,1,N)
else: # if list-like ...
# checks if list constists only on numerics
if all(isinstance(x, (int, float)) for x in N):
try:
NN = (N - min(N))/(max(N)-min(N))
except Exception as err_msg:
NN = np.linspace(0,1,len(N))
print(err_msg)
else:
NN = np.linspace(0,1,len(N))
NN = max_hue*(1-NN)
for n in NN:
if isinstance(n,float) & (not np.isnan(n)):
rgb = colorsys.hsv_to_rgb(n, saturation, brightness)
colors.append(f'rgb({rgb[0]*255:.0f},{rgb[1]*255:.0f},{rgb[2]*255:.0f})')
else:
# replacing occasional nans with grey
colors.append('grey')
return colors