pyadps 0.1.0__py3-none-any.whl → 0.1.0b0__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,367 @@
1
+ import tempfile
2
+
3
+ import numpy as np
4
+ import pandas as pd
5
+ import plotly.graph_objects as go
6
+ import streamlit as st
7
+ import utils.writenc as wr
8
+ from plotly_resampler import FigureResampler
9
+ import configparser
10
+
11
+
12
+ if "flead" not in st.session_state:
13
+ st.write(":red[Please Select Data!]")
14
+ st.stop()
15
+
16
+ if "fname" not in st.session_state:
17
+ st.session_state.fname = "No file selected"
18
+
19
+ if "rawfilename" not in st.session_state:
20
+ st.session_state.rawfilename = "rawfile.nc"
21
+
22
+ if "vleadfilename" not in st.session_state:
23
+ st.session_state.vleadfilename = "vlead.nc"
24
+
25
+
26
+ # Check if attributes exist in session state
27
+ if "attributes" not in st.session_state:
28
+ st.session_state.attributes = {}
29
+
30
+ if st.session_state.isVelocityMask:
31
+ st.session_state.final_mask = st.session_state.velocity_mask
32
+ st.session_state.final_velocity = st.session_state.veltest_velocity
33
+ if st.session_state.isGridSave:
34
+ st.session_state.final_echo = st.session_state.echo_regrid
35
+ st.session_state.final_correlation = st.session_state.correlation_regrid
36
+ st.session_state.final_pgood = st.session_state.pgood_regrid
37
+ else:
38
+ st.session_state.final_echo = st.session_state.echo
39
+ st.session_state.final_correlation = st.session_state.correlation
40
+ st.session_state.final_pgood = st.session_state.pgood
41
+ else:
42
+ if st.session_state.isGridSave:
43
+ st.session_state.final_mask = st.session_state.mask_regrid
44
+ st.session_state.final_velocity = st.session_state.velocity_regrid
45
+ st.session_state.final_echo = st.session_state.echo_regrid
46
+ st.session_state.final_correlation = st.session_state.correlation_regrid
47
+ st.session_state.final_pgood = st.session_state.pgood_regrid
48
+ else:
49
+ if st.session_state.isProfileMask:
50
+ st.session_state.final_mask = st.session_state.profile_mask
51
+ elif st.session_state.isQCMask:
52
+ st.session_state.final_mask = st.session_state.qc_mask
53
+ else:
54
+ st.session_state.final_mask = st.session_state.orig_mask
55
+ st.session_state.final_velocity = st.session_state.velocity
56
+ st.session_state.final_echo = st.session_state.echo
57
+ st.session_state.final_correlation = st.session_state.correlation
58
+ st.session_state.final_pgood = st.session_state.pgood
59
+
60
+
61
+ if "depth" not in st.session_state:
62
+ st.session_state.isGrid = False
63
+
64
+
65
+ @st.cache_data
66
+ def file_write(filename="processed_file.nc"):
67
+ tempdirname = tempfile.TemporaryDirectory(delete=False)
68
+ outfilepath = tempdirname.name + "/" + filename
69
+ return outfilepath
70
+
71
+
72
+ # If the data is not regrided based on pressure sensor. Use the mean depth
73
+ if not st.session_state.isGrid:
74
+ st.write(":red[WARNING!]")
75
+ st.write(
76
+ "Data not regrided. Using the mean transducer depth to calculate the depth axis."
77
+ )
78
+ mean_depth = np.mean(st.session_state.vlead.vleader["Depth of Transducer"]) / 10
79
+ mean_depth = np.trunc(mean_depth)
80
+ st.write(f"Mean depth of the transducer is `{mean_depth}`")
81
+ cells = st.session_state.flead.field()["Cells"]
82
+ cell_size = st.session_state.flead.field()["Depth Cell Len"] / 100
83
+ bin1dist = st.session_state.flead.field()["Bin 1 Dist"] / 100
84
+ max_depth = mean_depth - bin1dist
85
+ min_depth = max_depth - cells * cell_size
86
+ z = np.arange(-1 * max_depth, -1 * min_depth, cell_size)
87
+ st.session_state.final_depth = z
88
+ else:
89
+ st.session_state.final_depth = st.session_state.depth
90
+
91
+
92
+ @st.cache_data
93
+ def fillplot_plotly(
94
+ x, y, data, maskdata, colorscale="balance", title="Data", mask=False
95
+ ):
96
+ fig = FigureResampler(go.Figure())
97
+ if mask:
98
+ data1 = np.where(maskdata == 1, np.nan, data)
99
+ else:
100
+ data1 = np.where(data == -32768, np.nan, data)
101
+
102
+ fig.add_trace(
103
+ go.Heatmap(
104
+ z=data1[:, 0:-1],
105
+ x=x,
106
+ y=y,
107
+ colorscale=colorscale,
108
+ hoverongaps=False,
109
+ )
110
+ )
111
+ fig.update_layout(
112
+ xaxis=dict(showline=True, mirror=True),
113
+ yaxis=dict(showline=True, mirror=True),
114
+ title_text=title,
115
+ )
116
+ st.plotly_chart(fig)
117
+
118
+
119
+ def call_plot(varname, beam, mask=False):
120
+ if varname == "Velocity":
121
+ fillplot_plotly(
122
+ st.session_state.date,
123
+ st.session_state.final_depth,
124
+ st.session_state.final_velocity[beam - 1, :, :],
125
+ st.session_state.final_mask,
126
+ title=varname,
127
+ mask=mask,
128
+ )
129
+ elif varname == "Echo":
130
+ fillplot_plotly(
131
+ st.session_state.date,
132
+ st.session_state.final_depth,
133
+ st.session_state.final_echo[beam - 1, :, :],
134
+ st.session_state.final_mask,
135
+ title=varname,
136
+ mask=mask,
137
+ )
138
+ elif varname == "Correlation":
139
+ fillplot_plotly(
140
+ st.session_state.date,
141
+ st.session_state.final_depth,
142
+ st.session_state.final_correlation[beam - 1, :, :],
143
+ st.session_state.final_mask,
144
+ title=varname,
145
+ mask=mask,
146
+ )
147
+ elif varname == "Percent Good":
148
+ fillplot_plotly(
149
+ st.session_state.date,
150
+ st.session_state.final_depth,
151
+ st.session_state.final_pgood[beam - 1, :, :],
152
+ st.session_state.final_mask,
153
+ title=varname,
154
+ mask=mask,
155
+ )
156
+
157
+
158
+ st.header("View Processed Data", divider="blue")
159
+ var_option = st.selectbox(
160
+ "Select a data type", ("Velocity", "Echo", "Correlation", "Percent Good")
161
+ )
162
+ beam = st.radio("Select beam", (1, 2, 3, 4), horizontal=True)
163
+
164
+ mask_radio = st.radio("Apply Mask", ("Yes", "No"), horizontal=True)
165
+ plot_button = st.button("Plot Processed Data")
166
+ if plot_button:
167
+ if mask_radio == "Yes":
168
+ call_plot(var_option, beam, mask=True)
169
+ elif mask_radio == "No":
170
+ call_plot(var_option, beam, mask=False)
171
+
172
+
173
+ st.header("Write Data", divider="blue")
174
+
175
+ mask_data_radio = st.radio("Do you want to mask the final data?", ("Yes", "No"))
176
+
177
+ if mask_data_radio == "Yes":
178
+ mask = st.session_state.final_mask
179
+ st.session_state.write_velocity = np.copy(st.session_state.final_velocity)
180
+ st.session_state.write_velocity[:, mask == 1] = -32768
181
+ else:
182
+ st.session_state.write_velocity = np.copy(st.session_state.final_velocity)
183
+
184
+
185
+ file_type_radio = st.radio("Select output file format:", ("NetCDF", "CSV"))
186
+
187
+ if file_type_radio == "NetCDF":
188
+ add_attr_button = st.checkbox("Add attributes to NetCDF file")
189
+
190
+ if add_attr_button:
191
+ st.write("### Modify Attributes")
192
+
193
+ # Create two-column layout for attributes
194
+ col1, col2 = st.columns(2)
195
+
196
+ with col1:
197
+ # Display attributes in the first column
198
+ for key in ["Cruise_No.", "Ship_Name", "Project_No.", "Water_Depth_m", "Deployment_Depth_m","Deployment_Date","Recovery_Date"]:
199
+ if key in st.session_state.attributes:
200
+ st.session_state.attributes[key] = st.text_input(key, value=st.session_state.attributes[key])
201
+ else:
202
+ st.session_state.attributes[key] = st.text_input(key)
203
+
204
+ with col2:
205
+ # Display attributes in the second column
206
+ for key in ["Latitude", "Longitude","Platform_Type","Participants", "File_created_by", "Contact", "Comments"]:
207
+ if key in st.session_state.attributes:
208
+ st.session_state.attributes[key] = st.text_input(key, value=st.session_state.attributes[key])
209
+ else:
210
+ st.session_state.attributes[key] = st.text_input(key)
211
+
212
+ download_button = st.button("Generate Processed files")
213
+
214
+ if download_button:
215
+ st.session_state.processed_filename = file_write()
216
+ st.write(":grey[Processed file created. Click the download button.]")
217
+ st.write(st.session_state.processed_filename)
218
+ depth = np.trunc(st.session_state.final_depth)
219
+
220
+ if file_type_radio == "NetCDF":
221
+ if add_attr_button and st.session_state.attributes:
222
+ # Generate file with attributes
223
+ wr.finalnc(
224
+ st.session_state.processed_filename,
225
+ depth,
226
+ st.session_state.date,
227
+ st.session_state.write_velocity,
228
+ attributes=st.session_state.attributes # Pass edited attributes
229
+ )
230
+ else:
231
+ # Generate file without attributes
232
+ wr.finalnc(
233
+ st.session_state.processed_filename,
234
+ depth,
235
+ st.session_state.date,
236
+ st.session_state.write_velocity
237
+ )
238
+
239
+ with open(st.session_state.processed_filename, "rb") as file:
240
+ st.download_button(
241
+ label="Download NetCDF File",
242
+ data=file,
243
+ file_name="processed_file.nc",
244
+ )
245
+
246
+ if file_type_radio == "CSV":
247
+ udf = pd.DataFrame(
248
+ st.session_state.write_velocity[0, :, :].T,
249
+ index=st.session_state.date,
250
+ columns=-1 * depth,
251
+ )
252
+ vdf = pd.DataFrame(
253
+ st.session_state.write_velocity[1, :, :].T,
254
+ index=st.session_state.date,
255
+ columns=-1 * depth,
256
+ )
257
+ wdf = pd.DataFrame(
258
+ st.session_state.write_velocity[2, :, :].T,
259
+ index=st.session_state.date,
260
+ columns=-1 * depth,
261
+ )
262
+ ucsv = udf.to_csv().encode("utf-8")
263
+ vcsv = vdf.to_csv().encode("utf-8")
264
+ wcsv = wdf.to_csv().encode("utf-8")
265
+ st.download_button(
266
+ label="Download Zonal Velocity File",
267
+ data=ucsv,
268
+ file_name="zonal_velocity.csv",
269
+ mime="text/csf",
270
+ )
271
+ st.download_button(
272
+ label="Download Meridional Velocity File",
273
+ data=vcsv,
274
+ file_name="meridional_velocity.csv",
275
+ mime="text/csf",
276
+ )
277
+ st.download_button(
278
+ label="Download Vertical Velocity File",
279
+ data=vcsv,
280
+ file_name="vertical_velocity.csv",
281
+ mime="text/csf",
282
+ )
283
+
284
+
285
+ # Header for the Config.ini File Generator
286
+ st.header("Config.ini File Generator", divider="blue")
287
+
288
+ # Radio button to decide whether to generate the config.ini file
289
+ generate_config_radio = st.radio("Do you want to generate a config.ini file?", ("No", "Yes"))
290
+
291
+ if generate_config_radio == "Yes":
292
+ # Create a config parser object
293
+ config = configparser.ConfigParser()
294
+
295
+ # Main section
296
+ config["Main"] = {
297
+ "Input_FileName": st.session_state.fname
298
+ }
299
+
300
+ if st.session_state.isQCMask:
301
+ config["QC Test"] = {}
302
+
303
+ # Add the contents of the current QC Mask thresholds
304
+ if "newthresh" in st.session_state:
305
+ for idx, row in st.session_state.newthresh.iterrows():
306
+ config["QC Test"][row["Threshold"].replace(" ", "_")] = row["Values"]
307
+
308
+
309
+ # Profile Test section
310
+ if st.session_state.isProfileMask:
311
+ config["Profile Test"] = {}
312
+
313
+ if st.session_state.update_mask:
314
+
315
+ config["Profile Test"]["Change_Range"] = str(st.session_state.ens_range)
316
+ config["Profile Test"]["Deployment_ensembles"] = str(st.session_state.start_ens)
317
+ config["Profile Test"]["Recovery_ensembles"] = str(st.session_state.end_ens)
318
+
319
+ if st.session_state.update_mask_cutbin:
320
+ config["Profile Test"]["Beam"] = str(st.session_state.beam + 1) # Adding 1 since beams are 1-based
321
+ config["Profile Test"]["cell_to_delete"] = str(st.session_state.extra_cells)
322
+
323
+ if st.session_state.isGrid:
324
+ config["Profile Test"]["Regrid_Depth_cells"] = st.session_state.last_cell # Bin or Surface
325
+
326
+
327
+ # Velocity Test Section
328
+ if st.session_state.isVelocityMask:
329
+ config["Velocity Test"] = {}
330
+
331
+ if st.session_state.isMagnet:
332
+ config["Velocity Test"]["Latitude"] = str(st.session_state.lat)
333
+ config["Velocity Test"]["Longitude"] = str(st.session_state.lon)
334
+ config["Velocity Test"]["Depth"] = str(st.session_state.magnetic_dec_depth)
335
+ config["Velocity Test"]["Year"] = str(st.session_state.year)
336
+
337
+ if st.session_state.isCutoff:
338
+ config["Velocity Test"]["Max_Zoank"] = str(st.session_state.maxuvel)
339
+ config["Velocity Test"]["Max_Meridional"] = str(st.session_state.maxvvel)
340
+ config["Velocity Test"]["Max_Vertical"] = str(st.session_state.maxwvel)
341
+
342
+ if st.session_state.isDespike:
343
+ config["Velocity Test"]["Despike_Kernal_Size"] = str(st.session_state.despike_kernal)
344
+ config["Velocity Test"]["Despike_Cutoff"] = str(st.session_state.despike_cutoff)
345
+
346
+ if st.session_state.isFlatline:
347
+ config["Velocity Test"]["Flatline_Kernal"] = str(st.session_state.flatline_kernal)
348
+ config["Velocity Test"]["Flatline_Deviation"] = str(st.session_state.flatline_cutoff)
349
+
350
+
351
+ # Optional section (attributes)
352
+ config["Optional"] = {}
353
+ for key, value in st.session_state.attributes.items():
354
+ config["Optional"][key] = str(value) # Ensure all values are strings
355
+
356
+ # Write config.ini to a temporary file
357
+ config_filepath = "config.ini"
358
+ with open(config_filepath, "w") as configfile:
359
+ config.write(configfile)
360
+
361
+ # Allow the user to download the generated config.ini file
362
+ with open(config_filepath, "rb") as file:
363
+ st.download_button(
364
+ label="Download config.ini File",
365
+ data=file,
366
+ file_name="config.ini",
367
+ )
pyadps/utils/__init__.py CHANGED
@@ -1,12 +1,12 @@
1
1
  # pyadps/utils/__init__.py
2
2
 
3
+ from pyadps.utils.cutbin import *
3
4
  from pyadps.utils.plotgen import *
4
5
  from pyadps.utils.profile_test import *
5
6
  from pyadps.utils.pyreadrdi import *
6
7
  from pyadps.utils.readrdi import *
7
- from pyadps.utils.sensor_health import *
8
+ from pyadps.utils.regrid import *
8
9
  from pyadps.utils.signal_quality import *
9
10
  from pyadps.utils.velocity_test import *
10
11
  from pyadps.utils.writenc import *
11
- from pyadps.utils.autoprocess import *
12
- from pyadps.utils.script import *
12
+