Skip to content

Commit 7ac667a

Browse files
Merge pull request #429 from ARamsay17/dev
Various bugfixes
2 parents 22c59cc + 25e51e7 commit 7ac667a

File tree

5 files changed

+37
-63
lines changed

5 files changed

+37
-63
lines changed

Source/PIU/Breakdown_CB.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def plot_sample(
316316
y_mean / cal
317317
) # multiply uncertainties by cal to convert into irradiance/radiance
318318

319-
u_rel = (y / np.abs(y_mean)) * 100
319+
u_rel = self.getpct(y, y_mean)
320320
try:
321321
plt.figure(f"{s}_{self.station}")
322322
except AttributeError:
@@ -488,7 +488,7 @@ def classBasedL2(
488488
except ValueError as err:
489489
from Source.utils.loggingHCP import writeLogFileAndPrint
490490

491-
writeLogFileAndPrint(f"Error in Class Based Breakdown - {keys_rrs[i]}: {err}")
491+
writeLogFileAndPrint(f"Error in Class Based Breakdown - {keys_rrs[indx]}: {err}")
492492
UNCS["Rrs"][keys_rrs[indx]] = prop.Propagate_RRS_HYPER(
493493
rrs_vals, uRrs, corr_between=False
494494
)

Source/PIU/Breakdown_FRM.py

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def plotL2(self, waveSubset, BD_UNCS, signal):
7878
self.plot_spectral_FRM(meas, wvls, UNC['BRDF'], "brdf correction", rel_to=signal[meas], ylim=ylim)
7979

8080
self.plot_spectral_FRM(meas, wvls, UNC['pol'], "polarisation", rel_to=signal[meas], ylim=ylim)
81-
82-
self.save_figure(meas, level='L2') # save the figure once all of the contributions have been added to the plot (will close the figure)
83-
81+
82+
self.save_figure(s=meas, measurement='Spectral') # save the figure once all of the contributions have been added to the plot (will close the figure)
83+
8484
self.plot_pie_FRM(meas, wvls, BD_UNCS[meas], signal[meas], 'L2')
8585

8686

@@ -103,7 +103,7 @@ def plot_spectral_FRM(self, s, x: np.array, y: np.array, name: str, rel_to: Opti
103103
else: # if we have a signal to put uncs in relative units
104104
y_mean = rel_to # otherwise we just use rel_to directly
105105

106-
u_rel = (y/y_mean)*100 # calculate relative uncertainty
106+
u_rel = self.getpct(y, y_mean) # calculate relative uncertainty
107107
plt.plot(x, u_rel, label=f"{name}")
108108
plt.ylabel("relative uncertainty (%)") # unit is always % if relative uncertainties used
109109
if ylim is None:
@@ -213,48 +213,10 @@ def plot_pie_FRM(self, s: str, wavelengths: np.array, BD_UNCS: dict[str: np.arra
213213

214214
# plt.annotate(label, xy=(x,y), rotation=angle, ha=ha, va="center", rotation_mode="anchor", size=8)
215215

216-
# plt.tight_layout() # for improved clarity and less overlapping of labels
217-
# # fp = path.join(self.plot_folder, f"Sensor_pie_{s}_{self.station}_{wvl_at_indx}.png")
218-
labels_list = labels[s]
219-
colors = [LABEL_COLORS[lab] for lab in labels_list]
220-
221-
# Safety: handle empty or all-zero data
222-
if not vals or sum(vals) == 0:
223-
ax.text(0.5, 0.5, "No data to display", ha='center', va='center', transform=ax.transAxes)
224-
plt.title(f"{s} FRM Sensor-Specific Uncertainty: {wvl_at_indx} nm, Total: 0%", pad=20)
225-
plt.axis('off')
226-
plt.tight_layout()
227-
return
228-
229-
# Combined uncertainty
230-
combined = (sum(v**2 for v in vals)) ** 0.5
231-
232-
# --- Sort by value descending for readability ---
233-
sorted_data = sorted(zip(vals, labels_list), key=lambda t: t[0], reverse=True)
234-
vals_sorted, labels_sorted = zip(*sorted_data)
235-
colors_sorted = [LABEL_COLORS[lab] for lab in labels_sorted]
236-
237-
# --- Plot horizontal bars ---
238-
ax.barh(labels_sorted, vals_sorted, color=colors_sorted)
239-
240-
# --- Add percentage labels to the right of each bar ---
241-
total = sum(vals_sorted)
242-
x_offset = max(vals_sorted) * 0.01 # small offset so text doesn’t touch the bar
243-
for i, v in enumerate(vals_sorted):
244-
pct = (v / total) * 100
245-
ax.text(v + x_offset, i, f'{pct:.1f}%', va='center', fontsize=11)
246-
247-
# --- Styling ---
248-
ax.invert_yaxis() # largest at top
249-
ax.set_xlabel("Uncertainty Contribution")
250-
ax.set_ylabel("Contributors")
251-
plt.title(f"{s} FRM Sensor-Specific Uncertainty: {wvl_at_indx} nm, Total: {round(combined, 2)}%", pad=20)
252-
253-
plt.tight_layout()
254-
# fp = path.join(self.plot_folder, f"{s}_SB_pie_{self.station}_{wvl_at_indx}.png")
255-
fp = path.join(self.plot_folder, f"{s}_SB_bar_{self.station}_{wvl_at_indx}.png")
256-
self.save_figure(s=s, fp=fp, legend=False, grid=False, level=level)
257-
# plt.close(fig)
216+
plt.tight_layout() # for improved clarity and less overlapping of labels
217+
fp = path.join(self.plot_folder, f"{s}_pie_chart_SB_{self.station}_{wvl_at_indx}.png")
218+
self.save_figure(s=s, fp=fp, legend=False, grid=False, measurement=level)
219+
# plt.close(fig)
258220

259221
def get_figure(self, s: str) -> plt.figure:
260222
"""
@@ -271,8 +233,8 @@ def get_figure(self, s: str) -> plt.figure:
271233
fig = plt.figure(s)
272234

273235
return fig
274-
275-
def save_figure(self, s: Optional[str]=None, fp: Optional[str]=None, legend: bool=True, grid: bool=True, level='L1B') -> None:
236+
237+
def save_figure(self, s: Optional[str]=None, fp: Optional[str]=None, legend: bool=True, grid: bool=True, measurement='L1B') -> None:
276238
"""
277239
Helper function to save spectral and pie figures based on cast/station information
278240
@@ -294,8 +256,9 @@ def save_figure(self, s: Optional[str]=None, fp: Optional[str]=None, legend: boo
294256
if fp is None:
295257
# Case for spectral plots
296258
try:
297-
# fp = path.join(self.plot_folder, f"BD_plot_{level}_{s}_{self.station}.png")
298-
fp = path.join(self.plot_folder, f"{s}_SB_spectral_{level}_{self.station}.png")
259+
260+
# Sensor_plot-type_regime_datetime.png
261+
fp = path.join(self.plot_folder, f"{s}_{measurement}_SB_{self.station}.png")
299262
except (AttributeError, ValueError):
300263
fp = path.join(self.plot_folder, f"plot_sample_{s}.png")
301264

Source/ProcessL1bTriOS.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def processDarkCorrection_FRM(node, sensortype, stats: dict):
9494
back_mesure = np.zeros((nmes, nband))
9595

9696
ancGroup = node.getGroup('ANCILLARY_METADATA')
97-
sza = ancGroup.datasets['SZA'].columns['NONE']
97+
sza = ancGroup.datasets['SZA'].columns['SZA']
98+
9899
for n in range(nmes):
99100
# Background correction : B0 and B1 read from full charaterisation
100101
back_mesure[n,:] = B0 + B1*(int_time[n]/int_time_t0)

Source/ProcessL2BRDF.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ def procBRDF(root,BRDF_option='M02'):
142142
else:
143143
Rrs_BRDF[k] = np.array(OC_BRDF.nrrs.sel(bands=float(k))).tolist()
144144

145-
if 'unc' in ds and 'HYPER' in ds:
146-
bd_grp = root.getGroup("BREAKDOWN")
147-
bd_ds = bd_grp.addDataset(f"{ds.replace('_unc', '')}_BRDF")
148-
bd_ds.columns = Rrs_BRDF
149-
bd_grp.attributes['BRDF_method'] = BRDF_option
145+
if 'unc' in ds and 'HYPER' in ds:
146+
try:
147+
bd_grp = root.getGroup("BREAKDOWN")
148+
bd_grp.attributes['BRDF_method'] = BRDF_option
149+
bd_ds = bd_grp.addDataset(f"{ds.replace('_unc', '')}_BRDF")
150+
bd_ds.columns = Rrs_BRDF
151+
except AttributeError: # faster to ask forgiveness than permission
152+
print("BREAKDOWN group not found")
153+
150154
else:
151155
Rrs_BRDF_ds = gp.addDataset(f"{ds}_" + BRDF_option)
152156
Rrs_BRDF_ds.columns = Rrs_BRDF
@@ -167,9 +171,12 @@ def procBRDF(root,BRDF_option='M02'):
167171

168172
# Store BRDF corrected nLw
169173
if 'unc' in ds and 'HYPER' in ds:
170-
bd_grp = root.getGroup("BREAKDOWN")
171-
bd_ds = bd_grp.addDataset(f"{ds.replace('Rrs','nLw').replace('_unc', '')}_BRDF")
172-
bd_ds.columns = nLw_BRDF
174+
try:
175+
bd_grp = root.getGroup("BREAKDOWN")
176+
bd_ds = bd_grp.addDataset(f"{ds.replace('Rrs','nLw').replace('_unc', '')}_BRDF")
177+
bd_ds.columns = nLw_BRDF
178+
except AttributeError:
179+
pass
173180
else:
174181
nLw_BRDF_ds = gp.addDataset(f"{ds.replace('Rrs','nLw')}_" + BRDF_option)
175182
nLw_BRDF_ds.columns = nLw_BRDF

Source/utils/plotting.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ def specFilter(inFilePath, Dataset, timeStamp, station=None, filterRange=[400, 7
625625
def plotUncertainties(root, filename):
626626
# read in required values and uncs from root
627627
# import relevant methods
628-
628+
from datetime import datetime as dt
629629
irrGrp = root.getGroup("IRRADIANCE")
630630
radGrp = root.getGroup("RADIANCE")
631631
refGrp = root.getGroup("REFLECTANCE")
@@ -645,7 +645,10 @@ def plotUncertainties(root, filename):
645645
dates = cols.pop("Datetag")
646646
times = cols.pop("Timetag2") # convert from timetag to time
647647
for date, tt2 in zip(dates, times):
648-
casts.append(timeTag2ToDateTime(dateTagToDateTime(date), tt2))
648+
casts.append(
649+
timeTag2ToDateTime(dateTagToDateTime(date), tt2)
650+
)
651+
649652
else:
650653
cols.pop("Datetag")
651654
cols.pop("Timetag2")

0 commit comments

Comments
 (0)