From 5ebff202be6178fe788c65d281f4186501f8feb2 Mon Sep 17 00:00:00 2001 From: Rick-Methot-NOAA Date: Fri, 30 Jan 2026 14:15:32 -0800 Subject: [PATCH 1/3] first commit for narrow sizebin problem --- SS_expval.tpl | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/SS_expval.tpl b/SS_expval.tpl index df6fb5eb..f6d499ac 100644 --- a/SS_expval.tpl +++ b/SS_expval.tpl @@ -776,6 +776,7 @@ FUNCTION void Get_expected_values(const int y, const int t); { if (SzFreq_scale(SzFreqMethod) <= 2) // bin demarcations are in weight units (1=kg, 2=lbs), so uses wt_len to compare to bins { + warning<<"process_szfreq"<=topbin, so incr ibin "< 1) { botbin = SzFreq_bins2(SzFreqMethod, ibin); + warning<<" incr botbin to "<= topbin) + { + // this should have the fish in z size range distributed across all bins >= size(z) until get to overlap + warning<<" size > current bin, increment bin index "< Date: Mon, 2 Feb 2026 19:21:51 -0800 Subject: [PATCH 2/3] initial commit of rebin fxn --- SS_miscfxn.tpl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/SS_miscfxn.tpl b/SS_miscfxn.tpl index 76964cb3..8886036c 100644 --- a/SS_miscfxn.tpl +++ b/SS_miscfxn.tpl @@ -120,3 +120,41 @@ FUNCTION dvariable Comp_logL_Dirichlet(const double& Nsamp, const dvariable& dir logL = sum(gammln(Nsamp * obs_comp + dirichlet_Parm * exp_comp)) - sum(gammln(dirichlet_Parm * exp_comp)); return (logL); } + +FUNCTION dvector rebin(const dvector& src_edges, const dvector& src_counts, const dvector& dest_edges) + { + /* + This implementation takes two vectors representing the boundaries (edges) of the source + and destination bins, and one vector for the source counts. + + Rebins frequency data from one set of boundaries to another. + @param src_edges Boundaries of the original bins (size N+1). + @param src_counts Frequency/counts in original bins (size N). + @param dest_edges Boundaries of the new bins (size M+1). + @return Vector of rebinned frequency data (size M). + */ + + dvector dest_counts(dest_edges.size() - 1, 0.0); + for (int i = 0; i < dest_counts.size(); ++i) { + double d_low = dest_edges[i]; + double d_high = dest_edges[i + 1]; + + for (int j = 0; j < src_counts.size(); ++j) { + double s_low = src_edges[j]; + double s_high = src_edges[j + 1]; + + // Calculate the overlap between [d_low, d_high] and [s_low, s_high] + double overlap_low = max(d_low, s_low); + double overlap_high = min(d_high, s_high); + + if (overlap_low < overlap_high) { + double overlap_width = overlap_high - overlap_low; + double src_bin_width = s_high - s_low; + + // Distribute source count proportionally to the overlap area + dest_counts[i] += src_counts[j] * (overlap_width / src_bin_width); + } + } + } + return (dest_counts); + } \ No newline at end of file From 7f10ed4845446ee9d983ba13e986232ca6d47df6 Mon Sep 17 00:00:00 2001 From: Rick-Methot-NOAA Date: Wed, 4 Feb 2026 14:39:00 -0800 Subject: [PATCH 3/3] WIP --- SS_expval.tpl | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/SS_expval.tpl b/SS_expval.tpl index f6d499ac..ef9184e7 100644 --- a/SS_expval.tpl +++ b/SS_expval.tpl @@ -704,7 +704,7 @@ FUNCTION void Get_expected_values(const int y, const int t); { case (1): // units are biomass, so accumulate body weight into the bins; Assume that bin demarcations are also in biomass { - if (SzFreq_Omit_Small(SzFreqMethod) == 1) + if (SzFreq_Omit_Small(SzFreqMethod) == 1) { while (wt_len_low(s, 1, z1 + 1) < SzFreq_bins(SzFreqMethod, 1) && z1 < z2) { @@ -853,6 +853,23 @@ FUNCTION void Get_expected_values(const int y, const int t); else // bin demarcations are in length unit (3=cm, 4=inch) so uses population len_bins to compare to data bins { + /* wt_len_low(s, 1, z1 + 1) < SzFreq_bins(SzFreqMethod, 1) && z1 < z2) + FUNCTION dvector rebin(const dvector& src_edges, const dvector& src_counts, const dvector& dest_edges) + 3darray wt_len_low(1,nseas,1,N_GP,1,nlength2) // wt at lower edge of size bin + */ + dvector freq_in(1, z2-z1); // fill the input + freq_in = 1.; + echoinput << " z1: " << z1 << " z2: " << z2 << endl; + echoinput << " freq_in: " << freq_in << endl; + dvector bins_in(1, z2-z1); + bins_in = len_bins2(z1, z2); // input bins shifted + echoinput << " bins_in: " << bins_in << endl; +// dvector bins_out(1, z2-z1) = SzFreq_bins(SzFreqMethod + dvector freq_out(1, SzFreq_Nbins(SzFreqMethod)); + + freq_out = rebin(bins_in, freq_in, SzFreq_bins(SzFreqMethod)); + echoinput << " freq_out: " << freq_out << endl; + if (SzFreq_Omit_Small(SzFreqMethod) == 1) { while (len_bins2(z1 + 1) < SzFreq_bins(SzFreqMethod, 1))