ScrunchFactor.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2021 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/General/Pulsar/ScrunchFactor.h
10 
11 #ifndef __Pulsar_ScrunchFactor_h
12 #define __Pulsar_ScrunchFactor_h
13 
14 #include "Pulsar/Pulsar.h"
15 #include "Error.h"
16 
17 namespace Pulsar {
18 
20  class ScrunchFactor
21  {
23  unsigned n_result;
25  unsigned n_scrunch;
26 
27  public:
28 
30  static const ScrunchFactor none;
31 
33  ScrunchFactor (unsigned nresult = 1) { set_nresult (nresult); }
34 
36  unsigned get_nresult () const { return n_result; }
38  void set_nresult (unsigned value) { n_result = value; n_scrunch = 0; }
39 
41  unsigned get_nscrunch () const { return n_scrunch; }
43  void set_nscrunch (unsigned value) { n_scrunch = value; n_result = 0; }
44 
46  bool scrunch_enabled () const { return n_scrunch || n_result; }
47 
49  void disable_scrunch () { n_scrunch = n_result = 0; }
50 
52  unsigned get_nresult (unsigned size) const;
53 
55  unsigned get_nscrunch (unsigned size) const;
56  };
57 
58  std::ostream& operator << (std::ostream& os, const ScrunchFactor&);
59  std::istream& operator >> (std::istream& is, ScrunchFactor&);
60 
62  template<class Container>
63  void fscrunch (Container*, const ScrunchFactor&);
64 
66  template<class Container>
67  void tscrunch (Container*, const ScrunchFactor&);
68 
70  template<class Container>
71  void bscrunch (Container*, const ScrunchFactor&);
72 
73 }
74 
76 template<class Container, class getN, class doScrunch>
77 void scrunch (Container* container, getN get_size, doScrunch scrunch,
78  const Pulsar::ScrunchFactor& factor)
79 {
80  if (factor.get_nresult())
81  {
82  unsigned size = (container->*get_size) ();
83  unsigned new_size = factor.get_nresult();
84 
85  if (size % new_size)
86  Pulsar::warning << "scrunch: requested size=" << new_size << " does not divide current size=" << size << std::endl;
87 
88  unsigned nscrunch = size / new_size;
89  (container->*scrunch) ( nscrunch );
90  }
91  else if (factor.get_nscrunch())
92  {
93  (container->*scrunch) ( factor.get_nscrunch() );
94  }
95  else
96  throw Error (InvalidState, "scrunch (ScrunchFactor) template",
97  "neither nresult nor nscrunch set");
98 }
99 
101 template<class Container>
102 void Pulsar::fscrunch (Container* container, const ScrunchFactor& factor)
103 {
104  scrunch (container, &Container::get_nchan, &Container::fscrunch, factor);
105 }
106 
108 template<class Container>
109 void Pulsar::tscrunch (Container* container, const ScrunchFactor& factor)
110 {
111  scrunch (container, &Container::get_nsubint, &Container::tscrunch, factor);
112 }
113 
115 template<class Container>
116 void Pulsar::bscrunch (Container* container, const ScrunchFactor& factor)
117 {
118  scrunch (container, &Container::get_nbin, &Container::bscrunch, factor);
119 }
120 
121 #endif
122 
void bscrunch(Container *, const ScrunchFactor &)
Integrate phase bins.
Definition: ScrunchFactor.h:116
static const ScrunchFactor none
Useful for passing default = disabled.
Definition: ScrunchFactor.h:40
Warning warning
Warning messages filter.
Definition: Pulsar.C:12
Data storage implementations.
Definition: Container.h:24
void tscrunch(Container *, const ScrunchFactor &)
Integrate sub-integrations.
Definition: ScrunchFactor.h:109
The primary interface to pulsar observational data.
Definition: Archive.h:45
void set_nresult(unsigned value)
Set the number of elements to scrunch to.
Definition: ScrunchFactor.h:48
Manages a combined scrunch factor or target dimension.
Definition: ScrunchFactor.h:25
void fscrunch(Container *, const ScrunchFactor &)
Integrate frequency channels.
Definition: ScrunchFactor.h:102
void disable_scrunch()
Disable scrunching.
Definition: ScrunchFactor.h:59
unsigned get_nresult() const
Get the number of elements to scrunch to.
Definition: ScrunchFactor.h:46
ScrunchFactor(unsigned nresult=1)
Default constructor.
Definition: ScrunchFactor.h:43
void set_nscrunch(unsigned value)
Set the number of elements to be integrated.
Definition: ScrunchFactor.h:53
unsigned get_nscrunch() const
Get the number of elements to be integrated.
Definition: ScrunchFactor.h:51
Defines the PSRCHIVE library.
Definition: CalSource.h:17
bool scrunch_enabled() const
Return true if scrunch should be performed.
Definition: ScrunchFactor.h:56

Generated using doxygen 1.8.17