Skip to content

Commit f972bdd

Browse files
Robert Jacksonclaude
andcommitted
FIX: Zero out bg_weights at height levels with NaN interpolated background
When the sounding doesn't cover the full vertical extent of the grid, interp1d (bounds_error=False) returns NaN for out-of-range levels. Previously bg_weights was never zeroed at those levels, so the background constraint was applied using NaN wind values, corrupting the cost function. Fix by masking bg_weights wherever u_back or v_back is NaN after interpolation, and replacing NaN values in u_back/v_back with 0 so they don't propagate through cost function arithmetic. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 3e54a96 commit f972bdd

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

pydda/retrieval/wind_retrieve.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ def _get_dd_wind_field_scipy(
502502
parameters.bg_weights[~np.isfinite(parameters.bg_weights)] = 0
503503
parameters.weights[parameters.weights > 0] = 1
504504
parameters.bg_weights[parameters.bg_weights > 0] = 1
505+
506+
# Zero out bg_weights at height levels where the interpolated background
507+
# is NaN (i.e. outside the sounding's vertical range). Also replace NaN
508+
# in u_back/v_back with 0 so those levels don't corrupt cost function
509+
# arithmetic even though they carry zero weight.
510+
nan_bg_levels = ~np.isfinite(parameters.u_back) | ~np.isfinite(parameters.v_back)
511+
parameters.bg_weights[nan_bg_levels] = 0
512+
parameters.u_back = np.nan_to_num(parameters.u_back)
513+
parameters.v_back = np.nan_to_num(parameters.v_back)
505514
sum_Vr = np.nansum(np.square(parameters.vrs * parameters.weights))
506515
parameters.rmsVr = np.sqrt(np.nansum(sum_Vr) / np.nansum(parameters.weights))
507516

0 commit comments

Comments
 (0)