File size: 5,348 Bytes
7a3bca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d033940
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
import streamlit as st
import pandas as pd
from datetime import datetime
from streamlit_ext import download_button as down_btn


def generate_perforations(df,plug_df):
    content = "UNITS METRIC\n\n"

    # Iterate through unique well names
    for well_name in df['well'].unique():
        content += f'WELLNAME "{well_name}"\n'

        # Filter the dataframe for the current well
        well_df = df[df['well'] == well_name]

        # Iterate through rows of the filtered dataframe
        for index, row in well_df.iterrows():
            if row['type'] == 'p':
                date = row['date'].strftime('%d/%m/%Y')
                top = row['top']
                btm = row['btm']
                # Write the perforation line to the content
                content += f'{date} perforation {top} {btm} 0.1905 0 0 0\n'

            if row['type'] == 's':
                date = row['date'].strftime('%d/%m/%Y')
                top = row['top']
                btm = row['btm']
                # Write the shut-in line to the content
                content += f'{date} squeeze {top} {btm}\n'

        # Filter the plug dataframe for the current well
        plug_well_df = plug_df[plug_df['well'] == well_name]

        # Iterate through rows of the filtered plug dataframe
        for index, plug_row in plug_well_df.iterrows():
            date = plug_row['date'].strftime('%d/%m/%Y')
            depth = plug_row['depth']

            # Filter the well dataframe based on the plug depth
            filtered_well_df = well_df[(well_df['top'] >= depth) | (well_df['btm'] >= depth)]

            # Iterate through rows of the filtered well dataframe
            for _, filtered_row in filtered_well_df.iterrows():
                top = filtered_row['top']
                btm = filtered_row['btm']
                if depth > top and depth < btm:
                    # Write the plug line to the content
                    content += f'{date} squeeze {depth} {btm}\n'
                else :# Write the squeeze line to the content
                    content += f'{date} squeeze {top} {btm}\n'

        content += "\n"  # Add a newline between well sections

    return content

def generate_tubing(df,tubing=False):
    content = "UNITS METRIC\n\n"

    # Iterate through unique well names
    for well_name in df['well'].unique():
        content += f'DATE {df["date"].min().strftime("%Y-%m-%d")}\n'

        # Filter the dataframe for the current well
        well_df = df[df['well'] == well_name]

        # Get the deepest perforation depth for the current well
        deepest_depth = well_df['btm'].max() + 20
        shallowest_depth = well_df['top'].min() - 20

        # Write the casing information to the content
        content += f'CASING "{well_name}" "Casing 1"\n'
        content += '0 "C-API-6.625/J-55/20.00"\n'
        content += f'{deepest_depth}\n\n'

        if tubing:
            # Write the tubing information to the content
            content += f'TUBING "Tubing 1" "{well_name}" "{well_name}"\n'
            content += '0 "T-API-5.000/J-55/11.50"\n'
            content += f'{shallowest_depth}\n'

            # Write the packer information to the content
            content += f'PACKER "Packer 1" "{well_name}" {shallowest_depth - 30} "PK_ADD_ON1"\n\n'

    return content




def main():
    #change the page title
    st.set_page_config(page_title="Petrel Perforation and Tubing File Generator",layout="wide")
    #change the page icon
    st.markdown(""" <style>

    #MainMenu {visibility: hidden;}

    footer {visibility: hidden;}

    </style> """, unsafe_allow_html=True)
    

    st.title("Petrel Perforation and Tubing File Generator")

    # Upload Excel file
    uploaded_file = st.sidebar.file_uploader("Upload Perforation Excel file", type=["xlsx", "xls"])
    #check box for tubing
    tubing = st.sidebar.checkbox("Tubing",value=False)

    if uploaded_file is not None:
        # Read the Excel file
        df = pd.read_excel(uploaded_file,sheet_name='perforation')
        plug_df = pd.read_excel(uploaded_file,sheet_name='plugs')

        # Create button to begin the process
    if st.sidebar.button("Generate"):
        # Prevent page refresh using a callback
        st.session_state.generate_clicked = True  # Flag for callback

    # Use a callback to execute actions only when the button is clicked
    if st.session_state.get("generate_clicked", False):
        # Generate content for perforations.ev and tubing.tub
        perforations_content = generate_perforations(df,plug_df)
        tubing_content = generate_tubing(df, tubing=tubing)

        # Display perforations.ev content
        st.subheader("Click the button below to download perforations.ev and tubing.tub")
        # Create buttons to download files
        down_btn(
            label="Download perforations.ev",
            data=perforations_content,
            file_name="perforations.ev"
        )

        down_btn(
            label="Download tubing.tub",
            data=tubing_content,
            file_name="tubing.tub"
        )

        # Reset the flag after execution
        st.session_state.generate_clicked = False



if __name__ == "__main__":
    main()