Spaces:
Sleeping
Sleeping
Commit
•
5924776
1
Parent(s):
319385e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
# import plotly.graph_objects as go
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
from scipy.stats import multivariate_normal as mvn
|
6 |
+
import random
|
7 |
+
|
8 |
+
|
9 |
+
AIRTABLE_TOKEN = os.environ.get("AIRTABLE_TOKEN")
|
10 |
+
SUMMARY_TABLE_URL = os.environ.get("SUMMARY_TABLE_URL")
|
11 |
+
|
12 |
+
def send_to_airtable(data, url):
|
13 |
+
headers = {
|
14 |
+
"Authorization": f"Bearer {AIRTABLE_TOKEN}",
|
15 |
+
"Content-Type": "application/json"
|
16 |
+
}
|
17 |
+
data = json.dumps(data)
|
18 |
+
response = requests.post(url, headers=headers, data=data)
|
19 |
+
print(response.text)
|
20 |
+
return response.text
|
21 |
+
|
22 |
+
def log_final_decision(group_name, total_yield, injection, production, url):
|
23 |
+
data = {"fields": {"group_name": group_name, "total_yield": total_yield, "injection": injection, "production": production}}
|
24 |
+
return send_to_airtable(data, url)
|
25 |
+
|
26 |
+
|
27 |
+
df = pd.read_csv('volcs.csv')
|
28 |
+
cities = ["Serang", "Bogor", "Jogja", "Bandung", "Tasikmalaya", "Purwokerto", "Semarang", "Malang", "Banyuwangi", "Tarogong", "Temanggung"]
|
29 |
+
|
30 |
+
gammas = [0.9, 0.9, 0.93, 0.89, 0.81, 0.90, 0.98, 0.99, 0.89, 0.85, 0.80]
|
31 |
+
cities_lats = [-6.12028, -6.59444, -7.7956, -6.9175, -7.3268, -7.4214, -6.96667, -7.9839, -8.2186, -7.3178564, -6.3929]
|
32 |
+
cities_lons = [106.15028, 106.78917, 110.3644, 107.6191, 108.2142, 109.2341, 110.4208, 112.6315, 114.3695, 110.1779144, 105.8391]
|
33 |
+
|
34 |
+
|
35 |
+
sigma = 0.05
|
36 |
+
sigma_diag1 = 0.01
|
37 |
+
sigma_diag2 = 0.005
|
38 |
+
offset = 1
|
39 |
+
num_grid = 800
|
40 |
+
Sigma = [[sigma, sigma_diag1], [sigma_diag2, sigma]]
|
41 |
+
|
42 |
+
# min_lat, max_lat = df['Latitude'].min() - offset, df['Latitude'].max() + offset
|
43 |
+
# min_lon, max_lon = df['Longitude'].min() - offset, df['Longitude'].max() + offset
|
44 |
+
|
45 |
+
# lats = np.linspace(min_lat, max_lat, num_grid)
|
46 |
+
# lons = np.linspace(min_lon, max_lon, num_grid)
|
47 |
+
# LON, LAT = np.meshgrid(lons, lats)
|
48 |
+
|
49 |
+
# density = np.zeros(LAT.shape)
|
50 |
+
# for idx, row in df.iterrows():
|
51 |
+
# lat, lon = row['Latitude'], row['Longitude']
|
52 |
+
# gaussian = mvn(mean=[lat, lon], cov=Sigma)
|
53 |
+
# density += gaussian.pdf(np.dstack((LAT, LON)))
|
54 |
+
|
55 |
+
# lon_points = LON.ravel().tolist()
|
56 |
+
# lat_points = LAT.ravel().tolist()
|
57 |
+
# density_points = density.ravel().tolist()
|
58 |
+
|
59 |
+
def get_density(city):
|
60 |
+
idx = cities.index(city)
|
61 |
+
lat, lon = cities_lats[idx], cities_lons[idx]
|
62 |
+
density = 0
|
63 |
+
for idx, row in df.iterrows():
|
64 |
+
lat2, lon2 = row['Latitude'], row['Longitude']
|
65 |
+
gaussian = mvn(mean=[lat2, lon2], cov=Sigma)
|
66 |
+
density += gaussian.pdf([lat, lon])
|
67 |
+
return density
|
68 |
+
|
69 |
+
|
70 |
+
def get_energy_yield(production, injection, group, length=30):
|
71 |
+
density = get_density(production)
|
72 |
+
gamma = gammas[cities.index(production)]
|
73 |
+
dist = random.uniform(0.98, 1.02)
|
74 |
+
|
75 |
+
yield_dict = {"Year": [], "yield": []}
|
76 |
+
for i in range(1, length):
|
77 |
+
yield_dict["Year"].append(i)
|
78 |
+
yield_dict["yield"].append((10*density/dist) * gamma**i)
|
79 |
+
df = pd.DataFrame(yield_dict)
|
80 |
+
|
81 |
+
plt = gr.ScatterPlot(
|
82 |
+
value = df,
|
83 |
+
x = "Year",
|
84 |
+
y = "yield",
|
85 |
+
title = "Energy Yield Over Time",
|
86 |
+
x_title = "Year",
|
87 |
+
y_title = "Energy Yield (MWh)"
|
88 |
+
)
|
89 |
+
total_yield = sum(yield_dict['yield'])
|
90 |
+
total_text = f"Total Energy Yield for 30 years: {total_yield:.2f} MWh"
|
91 |
+
|
92 |
+
#if group is empty, return warning as text label
|
93 |
+
if group == "":
|
94 |
+
total_text = "Please enter your group number"
|
95 |
+
plt = gr.ScatterPlot()
|
96 |
+
return total_text, plt
|
97 |
+
else:
|
98 |
+
log_final_decision(group, total_yield, injection, production, SUMMARY_TABLE_URL)
|
99 |
+
return total_text, plt
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# def draw_map():
|
105 |
+
# fig = go.Figure()
|
106 |
+
|
107 |
+
# fig.add_trace(go.Densitymapbox(
|
108 |
+
# lat=lat_points,
|
109 |
+
# lon=lon_points,
|
110 |
+
# z=density_points,
|
111 |
+
# radius=2,
|
112 |
+
# colorscale='Jet',
|
113 |
+
# opacity=0.5,
|
114 |
+
# hoverinfo="skip",
|
115 |
+
# ))
|
116 |
+
|
117 |
+
# fig.add_trace(go.Scattermapbox(
|
118 |
+
# lat=cities_lats,
|
119 |
+
# lon=cities_lons,
|
120 |
+
# mode='markers',
|
121 |
+
# marker=go.scattermapbox.Marker(color="black"),
|
122 |
+
# text=cities,
|
123 |
+
# hoverinfo="text"
|
124 |
+
# ))
|
125 |
+
|
126 |
+
|
127 |
+
# fig.update_layout(
|
128 |
+
# title='Geothermal Potentials in Java, Indonesia (Old Data)',
|
129 |
+
# mapbox_style="open-street-map",
|
130 |
+
# mapbox=dict(
|
131 |
+
# center=dict(lat=(min_lat + max_lat) / 2, lon=(min_lon + max_lon) / 2),
|
132 |
+
# zoom=6
|
133 |
+
# ),
|
134 |
+
# margin=dict(l=0, r=0, t=0, b=0)
|
135 |
+
|
136 |
+
# )
|
137 |
+
|
138 |
+
|
139 |
+
# return fig
|
140 |
+
|
141 |
+
with gr.Blocks() as demo:
|
142 |
+
|
143 |
+
|
144 |
+
gr.Label("Geothermal Potential in Java, Indonesia")
|
145 |
+
gr.HTML("<center><img src='https://analytics-project-simt-its.github.io/assets/img/geothermal_map.png'></center>")
|
146 |
+
gr.HTML("Interactive map: <a href='https://analytics-project-simt-its.github.io/geothermal_map.html'>here</a>")
|
147 |
+
# map = gr.Plot()
|
148 |
+
|
149 |
+
with gr.Row():
|
150 |
+
injection = gr.Radio(cities, label="Location", info="Choose injection well location"),
|
151 |
+
production = gr.Radio(cities, label="Location", info="Choose production well location")
|
152 |
+
|
153 |
+
|
154 |
+
with gr.Row():
|
155 |
+
group = gr.Textbox(label="Group number", info="Enter your group number")
|
156 |
+
btn = gr.Button("Build Geothermal Wells")
|
157 |
+
|
158 |
+
with gr.Row():
|
159 |
+
energy = gr.Label()
|
160 |
+
energy_30 = gr.ScatterPlot()
|
161 |
+
|
162 |
+
|
163 |
+
# demo.load(draw_map, [], map)
|
164 |
+
demo.load()
|
165 |
+
btn.click(
|
166 |
+
get_energy_yield,
|
167 |
+
inputs = [production, injection, group],
|
168 |
+
outputs = [energy, energy_30]
|
169 |
+
)
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
|
174 |
+
demo.launch()
|