Skip to content

Commit f343c7a

Browse files
committed
add workaround to replace numpy.round() banker's rounding of floats ending in .5
1 parent 5f851e7 commit f343c7a

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

pycurrents_ADCP_processing/plot_westcoast_nc_LX.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import matplotlib
5151
# from pycurrents_ADCP_processing.utils import parse_processing_history
5252
import warnings
53+
from utils import round_to_int
5354

5455

5556
def resolve_to_alongcross(u_true, v_true, along_angle):
@@ -242,7 +243,7 @@ def plot_adcp_pressure(nc: xr.Dataset, dest_dir: str, resampled=None, is_pre_spl
242243

243244
png_name = plot_dir + "{}-{}_{}_{}m_PRESPR01.png".format(
244245
nc.station, nc.deployment_number, nc.instrument_serial_number.data,
245-
int(np.round(nc.instrument_depth.data)))
246+
round_to_int(nc.instrument_depth.data))
246247

247248
plot_title, png_name = review_plot_naming(
248249
plot_title, png_name, nc.instrument_serial_number.data, is_pre_split, resampled
@@ -429,7 +430,7 @@ def plots_diagnostic(nc: xr.Dataset, dest_dir, level0=False, time_range=None, bi
429430
)
430431
fig_name = plot_dir + '{}-{}_{}_{}m_diagnostic.png'.format(
431432
nc.station, str(nc.deployment_number), nc.instrument_serial_number.data,
432-
str(int(np.round(nc.instrument_depth.data, 0)))
433+
round_to_int(nc.instrument_depth.data)
433434
)
434435

435436
suptitle, fig_name = review_plot_naming(
@@ -626,7 +627,7 @@ def make_pcolor_ne(nc: xr.Dataset, dest_dir, time_lim, bin_depths_lim,
626627
# Have to round instrument depth twice due to behaviour of the float
627628
plot_name = plot_dir + '{}-{}_{}_{}m_{}_{}{}.png'.format(
628629
nc.attrs['station'], nc.attrs['deployment_number'], nc.instrument_serial_number.data,
629-
int(np.round(np.round(instrument_depth, 1))), vel_type, filter_type, resampled_4fname
630+
round_to_int(instrument_depth), vel_type, filter_type, resampled_4fname
630631
)
631632
fig.savefig(plot_name)
632633
plt.close()
@@ -691,7 +692,7 @@ def make_pcolor_speed(dest_dir: str, station: str, deployment_number: str, instr
691692
ax2_title = f'ADCP current direction (CCW from East) {station}-{deployment_number} {instrument_depth}m'
692693
ax2.set_title(ax2_title)
693694

694-
plot_name = f'{station}-{deployment_number}_{instrument_depth}m_spd_dir.png'
695+
plot_name = f'{station}-{deployment_number}_{round_to_int(instrument_depth)}m_spd_dir.png'
695696
if resampled:
696697
plot_name = plot_name.replace('.png', f'_{resampled}_resampled.png')
697698

@@ -829,7 +830,7 @@ def make_pcolor_ac(nc: xr.Dataset, dest_dir, time_lim, bin_depths_lim, ns_lim, e
829830
# have to round instrument depth twice due to behaviour of float
830831
plot_name = '{}-{}_{}_{}m_AC_{}{}.png'.format(
831832
nc.attrs['station'], nc.attrs['deployment_number'], nc.instrument_serial_number.data,
832-
int(np.round(np.round(instrument_depth, 1))), filter_type, resampled_4fname
833+
round_to_int(instrument_depth), filter_type, resampled_4fname
833834
)
834835
fig.savefig(plot_dir + plot_name)
835836
plt.close()
@@ -848,7 +849,7 @@ def num_ens_per_hr(nc: xr.Dataset):
848849
min2sec = 60
849850
sec2nsec = 1e9
850851
hr2nsec = hr2min * min2sec * sec2nsec
851-
return int(np.round(hr2nsec / time_incr, decimals=0))
852+
return round_to_int(hr2nsec / time_incr)
852853

853854

854855
def filter_godin(nc: xr.Dataset):
@@ -998,14 +999,14 @@ def binplot_compare_filt(nc: xr.Dataset, dest_dir, time, dat_raw, dat_filt, filt
998999
'ADCP {}-{} {} bin {} at {}m'.format(nc.attrs['station'], nc.attrs['deployment_number'],
9991000
vel_code, bin_index + 1, bin_depth), fontsize=14)
10001001
plot_name = nc.attrs['station'] + '-' + nc.attrs['deployment_number'] + '_{}m'.format(
1001-
str(np.round(nc.instrument_depth.data))) + '-{}_bin{}_compare_{}.png'.format(
1002+
round_to_int(nc.instrument_depth.data)) + '-{}_bin{}_compare_{}.png'.format(
10021003
vel_code, bin_index + 1, filter_type)
10031004
else:
10041005
ax1.set_title('ADCP {}-{} {} bin {} at {}m, {} subsampled'.format(
10051006
nc.attrs['station'], nc.attrs['deployment_number'], vel_code, bin_index + 1, bin_depth,
10061007
resampled), fontsize=14)
10071008
plot_name = nc.attrs['station'] + '-' + nc.attrs['deployment_number'] + '_{}m'.format(
1008-
str(np.round(nc.instrument_depth.data))) + '-{}_bin{}_compare_{}_{}_subsamp.png'.format(
1009+
round_to_int(nc.instrument_depth.data)) + '-{}_bin{}_compare_{}_{}_subsamp.png'.format(
10091010
vel_code, bin_index + 1, filter_type, resampled)
10101011

10111012
fig.savefig(plot_dir + plot_name)
@@ -1177,7 +1178,7 @@ def quiver_plot(dest_dir: str, data_filename,
11771178
:return: Absolute file path of the figure(s) this function creates
11781179
"""
11791180

1180-
instrument_depth = int(np.round(instrument_depth))
1181+
instrument_depth = round_to_int(instrument_depth)
11811182

11821183
# Apply the bin indices
11831184
ew_lim = ew_lim[single_bin_inds, :]
@@ -1632,7 +1633,7 @@ def make_plot_rotary_spectra(dest_dir: str, data_filename, station: str, deploym
16321633
16331634
"""
16341635

1635-
instrument_depth = int(np.round(instrument_depth))
1636+
instrument_depth = round_to_int(instrument_depth)
16361637

16371638
# Cycles per hour to cycles per day
16381639
cph_to_cpd = 24
@@ -1681,7 +1682,7 @@ def make_plot_rotary_spectra(dest_dir: str, data_filename, station: str, deploym
16811682

16821683
# Save the figure
16831684
plot_name = (f'{station}-{deployment_number}_{serial_number}_{instrument_depth}m'
1684-
f'_rotary_spectra_bin_{int(np.round(bin_depth))}m.png')
1685+
f'_rotary_spectra_bin_{round_to_int(bin_depth)}m.png')
16851686

16861687
if do_tidal_annotation:
16871688
plot_name = plot_name.replace('.png', '_ttide.png')
@@ -1770,7 +1771,8 @@ def pcolor_rot_component(dest_dir: str, data_filename, station: str, deployment_
17701771
plt.tight_layout()
17711772

17721773
# Save the figure
1773-
plot_name = f'{station}-{deployment_number}_{serial_number}_{instrument_depth}m_depth_prof_rot_spec.png'
1774+
plot_name = (f'{station}-{deployment_number}_{serial_number}_{round_to_int(instrument_depth)}m'
1775+
f'_depth_prof_rot_spec.png')
17741776
if neg_or_pos == 'neg':
17751777
plot_name = plot_name.replace('.png', '_neg_cw.png')
17761778
else:
@@ -1802,7 +1804,7 @@ def make_depth_prof_rot_spec(dest_dir: str, data_filename, station: str, deploym
18021804
Plot them: https://gitlab.com/krassovski/Tools/-/blob/master/Signal/series/adcp_report.m?ref_type=heads
18031805
-> rot_pcolor_plot()
18041806
"""
1805-
instrument_depth = int(np.round(instrument_depth))
1807+
instrument_depth = round_to_int(instrument_depth)
18061808

18071809
fs = sampling_freq(time_lim)
18081810

@@ -2120,7 +2122,7 @@ def set_lims(a):
21202122
a.set_ylim(min(a_ylim[0], y_lim[0]), max(a_ylim[1], y_lim[1]))
21212123
return
21222124

2123-
instrument_depth = int(np.round(instrument_depth))
2125+
instrument_depth = round_to_int(instrument_depth)
21242126

21252127
# Major tidal constituents
21262128
major_constit = ['M2', 'K1', 'N2', 'S2', 'O1', 'P1', 'Q1', 'K2']
@@ -2402,8 +2404,8 @@ def plot_single_bin_velocity(
24022404
ax[i].set_ylim((-vlim, vlim))
24032405
ax[i].tick_params(axis='both', direction='in', top=True, right=True)
24042406

2405-
plot_name = (f'{station}-{deployment_number}_{serial_number}_{int(np.round(instrument_depth))}m_'
2406-
f'NE_bin_{int(np.round(bin_depth))}m_{filter_type}.png')
2407+
plot_name = (f'{station}-{deployment_number}_{serial_number}_{round_to_int(instrument_depth)}m_'
2408+
f'NE_bin_{round_to_int(bin_depth)}m_{filter_type}.png')
24072409

24082410
_, plot_name = review_plot_naming(
24092411
plot_title=None, png_name=plot_name, serial_number=serial_number, is_pre_split=False, resampled=resampled

pycurrents_ADCP_processing/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,10 @@ def geospatial_vertical_extrema(orientation: str, sensor_depth: float, distance:
126126
num_decimals = len(str(distance[0]).split('.')[1])
127127

128128
return np.round(geospatial_vertical_min, num_decimals), np.round(geospatial_vertical_max, num_decimals)
129+
130+
131+
def round_to_int(x):
132+
"""
133+
Use instead of numpy.round(), which uses "banker's rounding" which rounds 1.5 and 2.5 to 2 !!
134+
"""
135+
return int(np.ceil(x)) if x % 1 >= .5 else int(np.floor(x))

0 commit comments

Comments
 (0)