From 8706d8c93e05678585864dc338fe44c12656381c Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Mon, 28 Mar 2016 22:50:20 +0200
Subject: [PATCH] new command mpbf

---
 pub/lib/commands.cpp |  3 +++
 pub/lib/manip.cpp    | 36 ++++++++++++++++++++++++++++++++++++
 pub/lib/manip.hpp    |  1 +
 3 files changed, 40 insertions(+)

diff --git a/pub/lib/commands.cpp b/pub/lib/commands.cpp
index 95e3107a..f5ee0686 100644
--- a/pub/lib/commands.cpp
+++ b/pub/lib/commands.cpp
@@ -427,6 +427,7 @@ bool frida_command( string cmd )
             "  mpaf  average, reduce number of points by fixed factor\n"
             "  mpaea average according to error bound (absolute)\n"
             "  mpaer average according to error bound (relative)\n"
+            "  mpbf  break into spectra by fixed factor\n"
             "  mpo   sort\n"
             "  mpq   average when x equal\n"
             "  mpsym symmetrize: map -x -> x, then average\n"
@@ -472,6 +473,8 @@ bool frida_command( string cmd )
         NManip::points_rebin_by_factor();
     } else if (cmd.substr(0,4) == "mpae") {
         NManip::points_rebin_by_err( cmd.substr(4) );
+    } else if (cmd == "mpbf") {
+        NManip::points_break_by_factor();
     } else if (cmd == "mpo") {
         NManip::points_sort();
     } else if (cmd == "mpq") {
diff --git a/pub/lib/manip.cpp b/pub/lib/manip.cpp
index 43040ec5..9d0a9f19 100644
--- a/pub/lib/manip.cpp
+++ b/pub/lib/manip.cpp
@@ -241,6 +241,42 @@ void NManip::points_rebin_by_err( const string& subcmd )
 }
 
 
+//! Break into spectra, by fixed factor.
+
+void NManip::points_break_by_factor()
+{
+    FileIterator fiter(SFSel::instance()->selD());
+    static int ng;
+    ng = iask( "Number of points per spectrum", ng );
+    if ( ng<1 )
+        throw S("invalid choice");
+
+    while ( const COld *fin = fiter.nextD() ) {
+        POld fout( fin->new_POld() );
+        fout->log_action( "mpbf " + S(ng) );
+        fout->ZCo.push_back( CCoord("no-in-break", ""));
+
+        for ( int j=0; j<fin->nJ(); j++ ) {
+            const CSpec* sin = fin->VS(j);
+            int jsub = 0;
+            for( int i=0; i<sin->size(); i+=ng ){
+                PSpec sout( new CSpec() );
+                sout->z = sin->z;
+                sout->z.push_back( RObjInt( new CObjInt(jsub++) ) );
+                for( int ii=i; ii<sin->size() && ii<i+ng; ++ii ){
+                    if( sin->has_dy() )
+                        sout->push_xyd( sin->x[ii], sin->y[ii], sin->dy[ii] );
+                    else
+                        sout->push_xy( sin->x[ii], sin->y[ii] );
+                }
+                fout->V.push_back( move(sout) );
+            }
+        }
+        SMem::instance()->mem_store( move(fout), fiter.k() );
+    }
+}
+
+
 //! Sort spectrum according to given expression.
 
 void NManip::points_sort()
diff --git a/pub/lib/manip.hpp b/pub/lib/manip.hpp
index 98af48f6..ab161348 100644
--- a/pub/lib/manip.hpp
+++ b/pub/lib/manip.hpp
@@ -17,6 +17,7 @@ namespace NManip {
     void points_rebin_by_factor();
     void points_rebin_by_err( const string& subcmd );
     void points_rebin_duplicates();
+    void points_break_by_factor();
     void points_sort();
     void points_symmetrize();
     void points_remove_err();
-- 
GitLab