pyadps 0.3.3b0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,298 @@
1
+ import streamlit as st
2
+ import tempfile
3
+ import os
4
+ import numpy as np
5
+ import pandas as pd
6
+ import plotly.express as px
7
+ import plotly.graph_objects as go
8
+ from datetime import datetime
9
+ from streamlit.runtime.state import session_state
10
+ import utils.writenc as wr
11
+
12
+ # Session states initialization
13
+ if "flead" not in st.session_state:
14
+ st.write(":red[Please Select Data!]")
15
+ st.stop()
16
+
17
+ if "fname" not in st.session_state:
18
+ st.session_state.fname = "No file selected"
19
+
20
+ if "rawfilename" not in st.session_state:
21
+ st.session_state.rawfilename = "rawfile.nc"
22
+
23
+ if "vleadfilename" not in st.session_state:
24
+ st.session_state.vleadfilename = "vlead.nc"
25
+
26
+ if "attributes" not in st.session_state:
27
+ st.session_state.attributes = {}
28
+
29
+ if "add_attributes_DRW" not in st.session_state:
30
+ st.session_state.add_attributes_DRW = "No" # Default value
31
+
32
+
33
+ ################ Functions #######################
34
+ @st.cache_data()
35
+ def file_access(uploaded_file):
36
+ temp_dir = tempfile.mkdtemp()
37
+ path = os.path.join(temp_dir, uploaded_file.name)
38
+ with open(path, "wb") as f:
39
+ f.write(uploaded_file.getvalue())
40
+ return path
41
+
42
+
43
+ @st.cache_data
44
+ def read_file(filepath):
45
+ ds = rd.ReadFile(st.session_state.fpath)
46
+ if not ds.isEnsembleEqual:
47
+ ds.fixensemble()
48
+ st.session_state.ds = ds
49
+
50
+
51
+ @st.cache_data
52
+ def file_write(path, axis_option, add_attributes=True):
53
+ tempdirname = tempfile.TemporaryDirectory(delete=False)
54
+ st.session_state.rawfilename = tempdirname.name + "/rawfile.nc"
55
+
56
+ if add_attributes:
57
+ wr.rawnc(
58
+ path,
59
+ st.session_state.rawfilename,
60
+ st.session_state.date1,
61
+ axis_option,
62
+ attributes=st.session_state.attributes,
63
+ )
64
+ else:
65
+ wr.rawnc(
66
+ path, st.session_state.rawfilename, st.session_state.date1, axis_option
67
+ )
68
+
69
+
70
+ @st.cache_data
71
+ def file_write_vlead(path, axis_option, add_attributes=True):
72
+ tempvardirname = tempfile.TemporaryDirectory(delete=False)
73
+ st.session_state.vleadfilename = tempvardirname.name + "/vlead.nc"
74
+
75
+ if add_attributes:
76
+ wr.vlead_nc(
77
+ path,
78
+ st.session_state.vleadfilename,
79
+ st.session_state.date2,
80
+ axis_option,
81
+ attributes=st.session_state.attributes,
82
+ )
83
+ else:
84
+ wr.vlead_nc(
85
+ path, st.session_state.vleadfilename, st.session_state.date2, axis_option
86
+ )
87
+
88
+
89
+ if "axis_option" not in st.session_state:
90
+ st.session_state.axis_option = "ensemble" # Default value
91
+
92
+ # UI for attribute selection
93
+ st.header("NetCDF File", divider="blue")
94
+
95
+ # Option to add attributes
96
+ st.session_state.add_attributes_DRW = st.radio(
97
+ "Do you want to add attributes to the NetCDF file?", ["No", "Yes"], horizontal=True
98
+ )
99
+
100
+ if st.session_state.add_attributes_DRW == "Yes":
101
+ st.write("### Please fill in the attributes:")
102
+
103
+ # Two-column layout
104
+ col1, col2 = st.columns(2)
105
+
106
+ with col1:
107
+ st.session_state.attributes["Cruise_No."] = st.text_input("Cruise No.")
108
+ st.session_state.attributes["Ship_Name"] = st.text_input("Ship Name")
109
+ st.session_state.attributes["Project_No."] = st.text_input("Project No.")
110
+ st.session_state.attributes["Water_Depth_m"] = st.text_input("Water Depth (m)")
111
+ st.session_state.attributes["Deployment_Depth_m"] = st.text_input(
112
+ "Deployment Depth (m)"
113
+ )
114
+ st.session_state.attributes["Deployment_Date"] = st.date_input(
115
+ "Deployment Date"
116
+ )
117
+ st.session_state.attributes["Recovery_Date"] = st.date_input("Recovery Date")
118
+
119
+ with col2:
120
+ st.session_state.attributes["Latitude"] = st.text_input("Latitude")
121
+ st.session_state.attributes["Longitude"] = st.text_input("Longitude")
122
+ st.session_state.attributes["Platform_Type"] = st.text_input("Platform Type")
123
+ st.session_state.attributes["Participants"] = st.text_area("Participants")
124
+ st.session_state.attributes["File_created_by"] = st.text_input(
125
+ "File created by"
126
+ )
127
+ st.session_state.attributes["Contact"] = st.text_input("Contact")
128
+ st.session_state.attributes["Comments"] = st.text_area("Comments")
129
+
130
+ st.write("Attributes will be added to the NetCDF file once you submit.")
131
+
132
+ # Dropdown for axis_option
133
+ st.session_state.axis_option_DRW = st.selectbox(
134
+ "Select x-axis option:",
135
+ options=["time", "ensemble"],
136
+ index=0, # Default to "time"
137
+ )
138
+
139
+ # Ensure it is passed correctly
140
+ # st.session_state.axis_option = axis_option
141
+
142
+ # Buttons to generate files
143
+ st.session_state.rawnc_download_DRW = st.button("Generate Raw NetCDF File")
144
+ st.session_state.vleadnc_download_DRW = st.button(
145
+ "Generate Raw Variable Leader NetCDF File"
146
+ )
147
+
148
+ if st.session_state.rawnc_download_DRW:
149
+ file_write(
150
+ st.session_state.fpath,
151
+ st.session_state.axis_option_DRW,
152
+ st.session_state.add_attributes_DRW == "Yes",
153
+ )
154
+ st.write(st.session_state.rawfilename)
155
+ with open(st.session_state.rawfilename, "rb") as file:
156
+ st.download_button(
157
+ label="Download Raw File",
158
+ data=file,
159
+ file_name="rawfile.nc",
160
+ )
161
+
162
+ if st.session_state.vleadnc_download_DRW:
163
+ file_write_vlead(
164
+ st.session_state.fpath,
165
+ st.session_state.axis_option,
166
+ st.session_state.add_attributes_DRW == "Yes",
167
+ )
168
+ st.write(st.session_state.vleadfilename)
169
+ with open(st.session_state.vleadfilename, "rb") as file:
170
+ st.download_button(
171
+ label="Download Variable Leader",
172
+ data=file,
173
+ file_name="vlead.nc",
174
+ )
175
+
176
+
177
+ def download_csv_with_ensemble(data, filename):
178
+ # Create ensemble numbers from 1 to the number of rows in the data
179
+ ensembles = np.arange(1, len(next(iter(data.values()))) + 1)
180
+
181
+ # Convert data to a DataFrame and insert ensembles as the first column
182
+ df = pd.DataFrame(data)
183
+ df.insert(0, "RDI_Ensemble", ensembles) # Add ensemble numbers as the first column
184
+
185
+ # Export the DataFrame as a CSV
186
+ csv = df.to_csv(index=False).encode("utf-8")
187
+ return st.download_button(
188
+ label=f"Download {filename} as CSV",
189
+ data=csv,
190
+ file_name=f"{filename}.csv",
191
+ mime="text/csv",
192
+ )
193
+
194
+
195
+ def download_csv(data, filename):
196
+ # Convert data to DataFrame if it's not already one
197
+ if isinstance(data, dict):
198
+ df = pd.DataFrame.from_dict(data, orient="index").T
199
+ else:
200
+ df = pd.DataFrame(data)
201
+
202
+ # Export the DataFrame as a CSV
203
+ csv = df.to_csv(index=False).encode("utf-8")
204
+ return st.download_button(
205
+ label=f"Download {filename} as CSV",
206
+ data=csv,
207
+ file_name=f"{filename}.csv",
208
+ mime="text/csv",
209
+ )
210
+
211
+
212
+ def download_csv1(data, filename):
213
+ # Convert data to DataFrame if it's not already one
214
+ if isinstance(data, dict):
215
+ df = pd.DataFrame.from_dict(data, orient="index").T
216
+ else:
217
+ df = pd.DataFrame(data)
218
+
219
+ # Create ensemble and depth arrays
220
+ ensembles = np.arange(1, df.shape[0] + 1)
221
+ depths = np.arange(1, df.shape[1] + 1)
222
+
223
+ # Add ensemble numbers as the first column
224
+ df.insert(0, "Ensemble", ensembles)
225
+
226
+ # Transpose the DataFrame to switch rows and columns
227
+ df = df.T
228
+
229
+ # Add depth values as the first row
230
+ df.insert(0, "Depth", [""] + list(depths))
231
+
232
+ # Export the DataFrame as a CSV
233
+ csv = df.to_csv(index=False, header=False).encode("utf-8")
234
+ return st.download_button(
235
+ label=f"Download {filename} as CSV",
236
+ data=csv,
237
+ file_name=f"{filename}.csv",
238
+ mime="text/csv",
239
+ )
240
+
241
+
242
+ # Load data
243
+ fdata = st.session_state.flead.fleader
244
+ vdata = st.session_state.vlead.vleader
245
+ velocity = st.session_state.velocity
246
+ echo = st.session_state.echo
247
+ correlation = st.session_state.correlation
248
+ pgood = st.session_state.pgood
249
+
250
+ x = np.arange(0, st.session_state.head.ensembles, 1)
251
+ y = np.arange(0, fdata["Cells"][0], 1)
252
+
253
+ X, Y = np.meshgrid(x, y)
254
+
255
+ # Unified download section
256
+ st.header("Download Raw Data CSV File", divider="blue")
257
+
258
+ # Selection for the data category
259
+ st.session_state.rawcsv_option_DRW = st.selectbox(
260
+ "Select data type to download:",
261
+ [
262
+ "Velocity",
263
+ "Echo Intensity",
264
+ "Correlation",
265
+ "Percent Good",
266
+ "Variable Leader",
267
+ "Fixed Leader",
268
+ ],
269
+ )
270
+
271
+ # Show corresponding variable options based on selection
272
+ if st.session_state.rawcsv_option_DRW == "Fixed Leader":
273
+ # Combine all variables of Fixed Leader into one DataFrame
274
+ f_combined_data = {var: fdata[var] for var in fdata.keys()}
275
+ download_csv_with_ensemble(f_combined_data, "Fixed_Leader_All_Variables")
276
+
277
+ elif st.session_state.rawcsv_option_DRW == "Variable Leader":
278
+ # Combine all variables of Variable Leader into one DataFrame
279
+ v_combined_data = {var: vdata[var] for var in vdata.keys()}
280
+ download_csv(v_combined_data, "Variable_Leader_All_Variables")
281
+
282
+ else:
283
+ st.session_state.rawcsv_beam_DRW = st.radio(
284
+ "Select beam to download", (1, 2, 3, 4), horizontal=True
285
+ )
286
+
287
+ data_type = st.session_state.rawcsv_option_DRW
288
+ beam_download = st.session_state.rawcsv_beam_DRW
289
+ if data_type == "Velocity":
290
+ download_data = velocity[beam_download - 1, :, :]
291
+ elif data_type == "Echo Intensity":
292
+ download_data = echo[beam_download - 1, :, :]
293
+ elif data_type == "Correlation":
294
+ download_data = correlation[beam_download - 1, :, :]
295
+ elif data_type == "Percent Good":
296
+ download_data = pgood[beam_download - 1, :, :]
297
+
298
+ download_csv1(download_data, f"{data_type}_Beam_{beam_download}")