ColdPlasma.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2006-2010 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/General/Pulsar/ColdPlasma.h
10 
11 #ifndef __Pulsar_ColdPlasma_h
12 #define __Pulsar_ColdPlasma_h
13 
14 #include "Pulsar/Transformation.h"
15 #include "Pulsar/Integration.h"
16 #include "Pulsar/Archive.h"
17 #include "Pulsar/Profile.h"
18 #include "Physical.h"
19 
20 namespace Pulsar {
21 
23 
37  template<class Corrector, class History>
38  class ColdPlasma : public Transformation<Integration> {
39 
40  public:
41 
42  typedef typename Corrector::Return Type;
43 
45  ColdPlasma () { name = "ColdPlasma"; }
46 
48  virtual double get_correction_measure (const Integration*) = 0;
49 
51  virtual double get_absolute_measure (const Integration*) = 0;
52 
54  virtual bool get_corrected (const Integration*) = 0;
55 
57  virtual double get_effective_measure (const Integration*) = 0;
58 
60  virtual Type get_identity () = 0;
61 
63  virtual void combine (Type& result, const Type& add) = 0;
64 
66  virtual void apply (Integration*, unsigned channel) = 0;
67 
69  virtual void execute (Archive*);
70 
72  void just_do_it (Archive*);
73 
75  virtual void revert (Archive*);
76 
78  void transform (Integration*);
79 
81 
82  void setup (const Integration*);
83 
85 
86  void update (const Integration*);
87 
89  virtual void set (const Integration* data)
90  { setup (data); update (data); }
91 
93  virtual void set_Profile (const Profile* data)
94  { corrector.set_frequency( data->get_centre_frequency () ); }
95 
97  /* \post All data will be corrected to the reference frequency */
98  void execute1 (Integration*);
99 
101  /* \post All data will be uncorrected to the reference frequency */
102  void revert1 (Integration*);
103 
105  void correct (Integration*, unsigned ichan, unsigned jchan, double freq);
106 
108  void match (const Integration* reference, Integration* to_be_corrected);
109 
111  void set_reference_wavelength (double metres);
113  double get_reference_wavelength () const;
114 
116  void set_reference_frequency (double MHz);
118  double get_reference_frequency () const;
119 
121  void set_measure (double measure);
123  double get_measure () const;
124 
126  void set_delta (const Type& d) { delta = d; }
128  Type get_delta () const { return delta; }
129 
130  protected:
131 
132  friend class Integration;
133 
135  void range (Integration*, unsigned ichan, unsigned jchan);
136 
138  Corrector corrector;
139 
141  Type delta;
142 
144  std::string name;
145 
147  std::string val;
148 
149  private:
150  double backup_measure;
151  };
152 
153 }
154 
155 
156 template<class C, class H>
158 {
159  set_reference_frequency( data->get_centre_frequency() );
160  set_measure( get_correction_measure(data) );
161 
162  if (Integration::verbose)
163  std::cerr << "Pulsar::" + name + "::setup lambda="
164  << get_reference_wavelength() << " measure=" << get_measure()
165  << std::endl;
166 }
167 
168 template<class C, class H>
170 {
171  setup (data);
172  execute1 (data);
173 }
174 catch (Error& error)
175 {
176  throw error += "Pulsar::" + name + "::transform";
177 }
178 
179 template<class C, class H>
181 {
182  for (unsigned i=0; i<arch->get_nsubint(); i++)
183  execute1( arch->get_Integration(i) );
184 }
185 
186 template<class C, class H>
188 {
189  for (unsigned i=0; i<arch->get_nsubint(); i++)
190  revert1( arch->get_Integration(i) );
191 }
192 
193 template<class C, class H>
195 {
196  delta = get_identity ();
197 
198  for (unsigned i=0; i<arch->get_nsubint(); i++)
199  range (arch->get_Integration(i), 0, arch->get_nchan());
200 }
201 
202 template<class C, class H>
204 {
205  corrector.set_reference_frequency (MHz);
206 }
207 
208 template<class C, class H>
210 {
211  return corrector.get_reference_frequency ();
212 }
213 
214 template<class C, class H>
216 {
217  corrector.set_reference_wavelength (metres);
218 }
219 
220 template<class C, class H>
222 {
223  return corrector.get_reference_wavelength ();
224 }
225 
226 template<class C, class H>
228 {
229  corrector.set_measure (measure);
230 }
231 
232 template<class C, class H>
234 {
235  return corrector.get_measure ();
236 }
237 
238 template<class Corrector, class History>
240 try
241 {
242  backup_measure = get_measure();
243 
244  double effective_measure = backup_measure;
245 
246  if (Integration::verbose)
247  std::cerr << "Pulsar::" + name + "::update"
248  " backup measure=" << backup_measure << std::endl;
249 
250  delta = get_identity();
251 
252  double absolute_measure = get_absolute_measure (data);
253  if (absolute_measure)
254  {
255  if (Integration::verbose)
256  std::cerr << "Pulsar::" + name + "::update absolute"
257  " measure=" << absolute_measure << std::endl;
258 
259  // the following corrects centre frequency to infinite frequency
260  Corrector absolute;
261  absolute.set_measure( absolute_measure );
262  absolute.set_reference_wavelength( 0 );
263  absolute.set_frequency( data->get_centre_frequency() );
264  combine (delta, absolute.evaluate());
265 
266  // the following corrects channel frequency to centre frequency
267  effective_measure += absolute_measure;
268  }
269 
270  const History* corrected = 0;
271 
272  if ( get_corrected(data) )
273  corrected = data->template get<History>();
274 
275  if (corrected)
276  {
277  double corrected_measure = corrected->get_measure();
278  double lambda = corrected->get_reference_wavelength();
279 
280  if (Integration::verbose)
281  std::cerr << "Pulsar::" + name + "::update corrected"
282  " measure=" << corrected_measure << " lambda=" << lambda
283  << std::endl;
284 
285  // calculate the correction due to the new centre frequency, if any
286  corrector.set_wavelength( lambda );
287  combine (delta, corrector.evaluate());
288 
289  effective_measure -= corrected_measure;
290  }
291 
292  if (Integration::verbose)
293  std::cerr << "Pulsar::" + name + "::update effective_measure="
294  << effective_measure << std::endl;
295 
296  // set the effective correction measure
297  set_measure( effective_measure );
298 }
299 catch (Error& error)
300 {
301  throw error += "Pulsar::"+name+"::update";
302 }
303 
304 template<class C, class History>
306 {
307  update (data);
308 
309  if (Integration::verbose)
310  std::cerr << "Pulsar::"+name+"::execute1"
311  " effective "+val+"=" << get_measure() <<
312  " reference wavelength=" << get_reference_wavelength() << std::endl;
313 
314  range (data, 0, data->get_nchan());
315 
316  // restore the original correction measure
317  set_measure( backup_measure );
318 
319  History* corrected = new History;
320  corrected->set_measure( backup_measure );
321  corrected->set_reference_wavelength( get_reference_wavelength() );
322  data->add_extension( corrected );
323 
324 }
325 catch (Error& error)
326 {
327  throw error += "Pulsar::"+name+"::execute1";
328 }
329 
330 template<class C, class History>
332 {
333  if ( !get_corrected(data) )
334  return;
335 
336  History* corrected = data->template get<History>();
337 
338  if (!corrected)
339  throw Error (InvalidState, "Pulsar::" + name + "::revert1",
340  "no correction history");
341 
342  set_measure( -corrected->get_measure() );
343  set_reference_wavelength( corrected->get_reference_wavelength() );
344  set_delta( get_identity() );
345 
346  if (Integration::verbose)
347  std::cerr << "Pulsar::"+name+"::revert1"
348  " effective "+val+"=" << get_measure() <<
349  " reference wavelength=" << get_reference_wavelength() << std::endl;
350 
351  range (data, 0, data->get_nchan());
352 
353  // this should remove the history
354  delete corrected;
355 }
356 catch (Error& error)
357 {
358  throw error += "Pulsar::"+name+"::revert1";
359 }
360 
362 template<class C, class History>
364  Integration* to_correct)
365 {
366  const History* corrected = reference->template get<History>();
367  if (!corrected)
368  throw Error (InvalidState, "Pulsar::" + name + "::match",
369  "reference has no correction history");
370 
371  set_measure( corrected->get_measure() );
372  set_reference_wavelength( corrected->get_reference_wavelength() );
373 
374  execute1( to_correct );
375 }
376 
388 template<class C, class H>
390  unsigned ichan, unsigned kchan) try
391 {
392 
393  if (Integration::verbose)
394  std::cerr << "Pulsar::"+name+"::range "+val+"=" << get_measure()
395  << " lambda_0=" << get_reference_wavelength() << " m"
396  << " delta=" << delta << std::endl;
397 
398  if (get_measure() == 0 && delta == get_identity())
399  return;
400 
401  if (ichan >= data->get_nchan())
402  throw Error (InvalidRange, "Pulsar::"+name+"::range",
403  "start chan=%d >= nchan=%d", ichan, data->get_nchan());
404 
405  if (kchan > data->get_nchan())
406  throw Error (InvalidRange, "Pulsar::"+name+"::range",
407  "end chan=%d > nchan=%d", kchan, data->get_nchan());
408 
409  for (unsigned jchan=ichan; jchan < kchan; jchan++)
410  {
411  corrector.set_frequency( data->get_centre_frequency (jchan) );
412  apply (data, jchan);
413  }
414 
415 }
416 catch (Error& error) {
417  throw error += "Pulsar::"+name+"::range";
418 }
419 
420 
426 template<class C, class H>
428  unsigned ichan, unsigned kchan,
429  double reference_frequency)
430 try
431 {
432  double effective_measure = get_effective_measure (subint);
433 
434  if (Integration::verbose)
435  std::cerr << "Pulsar::" + name + "::correct"
436  " reference freq=" << reference_frequency <<
437  " effective measure=" << effective_measure << std::endl;
438 
439  set_measure( effective_measure );
440  set_reference_frequency( reference_frequency );
441  set_delta( get_identity() );
442 
443  range (subint, ichan, kchan);
444 }
445 catch (Error& error)
446 {
447  throw error += "Pulsar::"+name+"::correct";
448 }
449 
450 
451 #endif
ColdPlasma()
Default constructor.
Definition: ColdPlasma.h:55
virtual double get_effective_measure(const Integration *)=0
Derived classes must return the effective measure to be corrected.
Corrector corrector
The dielectric effect corrector.
Definition: ColdPlasma.h:148
virtual void set(const Integration *data)
Calls setup then update.
Definition: ColdPlasma.h:99
double get_centre_frequency() const
get the centre frequency (in MHz)
Definition: Profile.h:202
Type delta
The correction due to a change in reference wavelength.
Definition: ColdPlasma.h:151
void correct(Integration *, unsigned ichan, unsigned jchan, double freq)
Correct the selected range according to effective_measure.
Definition: ColdPlasma.h:427
double get_centre_frequency(unsigned ichan) const
Get the Profile centre frequency attribute of the given channel.
Definition: Integration.C:359
void revert1(Integration *)
Undo the correction.
Definition: ColdPlasma.h:331
virtual unsigned get_nsubint() const =0
Get the number of sub-integrations stored in the file.
Any quantity recorded as a function of pulse phase.
Definition: Profile.h:45
The primary interface to pulsar observational data.
Definition: Archive.h:45
void set_reference_frequency(double MHz)
Set the reference frequency in MHz.
Definition: ColdPlasma.h:203
virtual void execute(Archive *)
Execute the correction for an entire Archive.
Definition: ColdPlasma.h:180
void just_do_it(Archive *)
Just do the correction (off the books)
Definition: ColdPlasma.h:194
std::string name
The name to be used in verbose messages.
Definition: ColdPlasma.h:154
double get_reference_frequency() const
Get the reference frequency in MHz.
Definition: ColdPlasma.h:209
virtual void add_extension(Extension *extension)
Add an Extension to the Integration instance.
Definition: Integration.C:90
virtual void combine(Type &result, const Type &add)=0
Derived classes must define the operator.
void set_reference_wavelength(double metres)
Set the reference wavelength in metres.
Definition: ColdPlasma.h:215
void range(Integration *, unsigned ichan, unsigned jchan)
Execute the correction on the selected range.
Definition: ColdPlasma.h:389
virtual unsigned get_nchan() const =0
Get the number of frequency channels used.
virtual bool get_corrected(const Integration *)=0
Return true if the data have been corrected.
Record of phase-coherent dispersion removal algorithm.
Definition: CoherentDedispersion.h:27
virtual double get_absolute_measure(const Integration *)=0
Return the measure to be corrected with respect to infinite frequency.
Integration * get_Integration(unsigned subint)
Return pointer to the specified Integration.
Definition: IntegrationManager.C:41
void update(const Integration *)
update internal variables before execution
Definition: ColdPlasma.h:239
std::string val
The name of the correction measure.
Definition: ColdPlasma.h:157
void set_delta(const Type &d)
Set the correction due to a change in reference wavelength.
Definition: ColdPlasma.h:136
Array of Profiles integrated over the same time interval.
Definition: Integration.h:37
void execute1(Integration *)
Execute the correction.
Definition: ColdPlasma.h:305
virtual Type get_identity()=0
Derived classes must define the identity.
double get_measure() const
Get the correction measure.
Definition: ColdPlasma.h:233
void setup(const Integration *)
Set up internal variables before execution.
Definition: ColdPlasma.h:157
void set_measure(double measure)
Set the correction measure.
Definition: ColdPlasma.h:227
virtual void apply(Integration *, unsigned channel)=0
Derived classes must define how to apply the correction.
Type get_delta() const
Get the correction due to a change in reference wavelength.
Definition: ColdPlasma.h:138
virtual unsigned get_nchan() const =0
Get the number of chans.
virtual void revert(Archive *)
Revert the correction for an entire Archive.
Definition: ColdPlasma.h:187
void match(const Integration *reference, Integration *to_be_corrected)
Correct the second argument as the first argument was corrected.
Definition: ColdPlasma.h:363
Defines the PSRCHIVE library.
Definition: CalSource.h:17
void transform(Integration *)
The default correction.
Definition: ColdPlasma.h:169
double get_reference_wavelength() const
Get the reference wavelength.
Definition: ColdPlasma.h:221
virtual void set_Profile(const Profile *data)
Set the frequency for which the correction will be computed.
Definition: ColdPlasma.h:103
virtual double get_correction_measure(const Integration *)=0
Return the measure to be corrected with respect to centre frequency.

Generated using doxygen 1.8.17