chanschan commited on
Commit
b477385
·
1 Parent(s): c995b22

add file conversion KVL to XYZ

Browse files
Files changed (2) hide show
  1. app.py +5 -2
  2. output_xyz.py +55 -0
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- st.set_page_config(layout="wide", page_title='Rock physics template v.0.1')
3
 
4
  from load_css import local_css
5
  # import pandas as pd
@@ -9,6 +9,7 @@ import home
9
  import plotting
10
  import header
11
  import read_kvl_file as kvl
 
12
  # import plotly.express as px
13
 
14
  # from io import StringIO
@@ -48,7 +49,7 @@ if kvl_file:
48
  # Sidebar Navigation
49
  st.sidebar.title('Navigation')
50
  options = st.sidebar.radio('Select a page:',
51
- ['Home', 'Keys Information', 'Data Visualisation',
52
  ])
53
 
54
  if options == 'Home':
@@ -59,3 +60,5 @@ elif options == 'Keys Information':
59
  # raw_data.raw_data(kvl_file)
60
  elif options == 'Data Visualisation':
61
  plotting.plot(kvl_file)
 
 
 
1
  import streamlit as st
2
+ st.set_page_config(layout="wide", page_title='Kvl Visualisation')
3
 
4
  from load_css import local_css
5
  # import pandas as pd
 
9
  import plotting
10
  import header
11
  import read_kvl_file as kvl
12
+ import output_xyz
13
  # import plotly.express as px
14
 
15
  # from io import StringIO
 
49
  # Sidebar Navigation
50
  st.sidebar.title('Navigation')
51
  options = st.sidebar.radio('Select a page:',
52
+ ['Home', 'Keys Information', 'Data Visualisation', 'Convert KVL to XYZ',
53
  ])
54
 
55
  if options == 'Home':
 
60
  # raw_data.raw_data(kvl_file)
61
  elif options == 'Data Visualisation':
62
  plotting.plot(kvl_file)
63
+ elif options == 'Convert KVL to XYZ':
64
+ output_xyz.output_xyz(kvl_file)
output_xyz.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import base64
4
+ import atexit
5
+
6
+
7
+ def write_xyz_file(kvl_file, filename, keys):
8
+ with open(filename, 'w') as f:
9
+ nt = len(kvl_file['TIME'])
10
+ dt = int(kvl_file['TIME'][1]) - int(kvl_file['TIME'][0])
11
+ sampleno = len(kvl_file['TOP']) / nt
12
+
13
+ for time in kvl_file['TIME']:
14
+ start_index = int(time * sampleno)
15
+ end_index = start_index + int(sampleno)
16
+ for key in keys:
17
+ if key == 'SEALEVEL':
18
+ index=int(time/dt)
19
+ sealevel = kvl_file[key][index]
20
+ f.write(f"0 0 {sealevel}\n")
21
+ else:
22
+ matrix = kvl_file[key][start_index:end_index]
23
+ for i, row in enumerate(matrix):
24
+ for j, value in enumerate(row):
25
+ f.write(f"{i} {j} {value}\n")
26
+
27
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
28
+ with open(bin_file, 'rb') as f:
29
+ data = f.read()
30
+ bin_str = base64.b64encode(data).decode()
31
+ href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">{file_label}</a>'
32
+ return href
33
+
34
+ def cleanup(filename):
35
+ os.remove('./' + filename)
36
+
37
+ # Write the XYZ file
38
+ def output_xyz(kvl_file):
39
+ st.title('Convert KVL to XYZ')
40
+
41
+ if not kvl_file:
42
+ st.warning('No file has been uploaded')
43
+ else:
44
+ outkeys = st.multiselect('Select Keys to output, add SEALEVEL first if you want that key in as (0 0 z)', kvl_file.keys())
45
+ filename = st.text_input('Enter output filename', 'output.xyz')
46
+ write_xyz_file(kvl_file, filename, outkeys)
47
+
48
+ # Function to create a download link
49
+
50
+ if filename:
51
+ # Use the function to create a download link for the XYZ file
52
+ st.markdown(get_binary_file_downloader_html(filename, 'Download XYZ file'), unsafe_allow_html=True)
53
+ atexit.register(cleanup, filename)
54
+
55
+