Database.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2004 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/Polarimetry/Pulsar/Database.h
10 
11 #ifndef __Pulsar_Database_h
12 #define __Pulsar_Database_h
13 
14 #include "Pulsar/Calibrator.h"
15 #include "Pulsar/Config.h"
16 
17 #include "MEAL/Complex2.h"
18 
19 #include "sky_coord.h"
20 #include "MJD.h"
21 #include "Types.h"
22 
23 #include <iostream>
24 
25 namespace Pulsar {
26 
27  // forward declarations
28  class FluxCalibrator;
29  class PolnCalibrator;
30  class HybridCalibrator;
31  class ReferenceCalibrator;
32  class Archive;
33 
35  class Database : public Reference::Able {
36 
37  public:
38 
40  static bool cache_last_cal;
41 
43  static bool match_verbose;
44 
47 
50 
53 
56 
59 
61  static const Pulsar::Archive* any;
62 
64 
66  enum Sequence {
68  Any,
73  };
74 
76  Database ();
77 
79  Database (const std::string& path, const std::string& metafile);
80 
82  Database (const std::string& path, const std::vector<std::string>& exts);
83 
85  Database (const std::string& filename);
86 
88  ~Database ();
89 
91  void merge (const Database*);
92 
94  void construct (const std::vector<std::string>& filenames);
95 
97  void unload (const std::string& dbase_filename);
98 
100  void load (const std::string& dbase_filename);
101 
103  void add (const Pulsar::Archive* archive);
104 
107  const Calibrator::Type* m);
108 
110  FluxCalibrator* generateFluxCalibrator (Archive*, bool allow_raw=false);
111 
114 
116  void set_feed (MEAL::Complex2* xform) { feed = xform; }
117 
119  std::string get_path () const;
120 
122  unsigned size () const { return entries.size(); }
123 
125  class Entry : public Reference::Able
126  {
127 
128  public:
129 
131 
132  Signal::Source obsType; // FluxCal, PolnCal, Pulsar, etc.
133  sky_coord position; // Where the telescope was pointing
134  double bandwidth; // Bandwidth of observation
135  double frequency; // Centre frequency of observation
136 
137  std::string instrument; // name of backend
138  std::string receiver; // name of receiver
139  std::string filename; // full path of file
140 
142  Entry (const Archive* arch);
143 
145  ~Entry();
146 
147  // factory constructs new Entry from ascii string
148  static Entry* load (const std::string& str);
149 
150  // factory constructs new Entry from Archive
151  static Entry* create (const Archive*);
152 
153  // unload ascii string
154  virtual void unload (std::string& str);
155 
156  // return full path to filename
157  std::string get_filename () const;
158 
159  virtual bool less_than (const Entry*) const;
160  virtual bool equals (const Entry*) const;
161 
162  virtual MJD get_effective_time () const = 0;
163  virtual std::string get_time_str () const = 0;
164  virtual std::string get_nchan_str () const = 0;
165 
166  protected:
167 
169  void init ();
170  };
171 
172  class StaticEntry : public Entry
173  {
174  public:
175  MJD time; // Mid time of observation
176  unsigned nchan; // Number of channels across bandwidth
177 
179  StaticEntry (const Archive* arch = 0);
180 
181  bool equals (const Entry*) const;
182 
183  MJD get_effective_time () const { return time; }
184  std::string get_time_str () const;
185  std::string get_nchan_str () const;
186 
187  protected:
188 
190  void init ();
191  };
192 
193  class InterpolatorEntry : public Entry
194  {
195  public:
196  MJD start_time; // earliest time spanned by interpolator
197  MJD end_time; // latest time spanned by interpolator
198 
200  InterpolatorEntry (const Archive* arch = 0);
201 
202  bool equals (const Entry*) const;
203 
204  MJD get_effective_time () const { return start_time; }
205  std::string get_time_str () const;
206  std::string get_nchan_str () const;
207  };
208 
210  class Criteria : public Reference::Able {
211 
212  public:
213 
216 
218  void set_sequence (Sequence s) { sequence = s; }
219  Sequence get_sequence () const { return sequence; }
220 
221  double minutes_apart;
222  double deg_apart;
223 
224  bool check_receiver;
225  bool check_instrument;
226  bool check_frequency;
227  bool check_bandwidth;
228  bool check_obs_type;
229  bool check_time;
230  bool check_coordinates;
231  bool check_frequency_array;
232 
234  Criteria ();
235 
237  void no_data ();
238 
240  bool match (const Entry* entry) const;
241 
247 
249  mutable std::string match_report;
250 
252  mutable unsigned match_count;
253 
255  mutable double diff_minutes;
256 
258  mutable double diff_degrees;
259 
261 
263  const Entry* best (const Entry* a, const Entry* b) const;
264 
266  static Criteria closest (const Criteria& a, const Criteria& b);
267 
268  protected:
269 
272 
273  template<typename T, typename U, typename Predicate>
274  void compare (const std::string& name,
275  const T& want, const U& have,
276  Predicate equal ) const
277  {
278  match_report += "\t" + name
279  + " want=" + tostring(want) + " have=" + tostring(have) + " ... ";
280 
281  if ( equal (want, have) )
282  {
283  match_report += "match \n";
284  match_count ++;
285  }
286  else
287  {
288  match_report += "no match \n";
289  throw false;
290  }
291  }
292 
293  template<typename T>
294  void compare (const std::string& name,
295  const T& want, const T& have) const
296  {
297  compare (name, want, have, std::equal_to<T>());
298  }
299 
300  bool compare_times (const MJD& want,
301  const MJD& have) const;
302 
303  bool bracketed_time (const MJD& want,
304  const std::pair<MJD,MJD>& have) const;
305 
306  bool compare_coordinates (const sky_coord& want,
307  const sky_coord& have) const;
308  };
309 
310 
312  static Criteria get_default_criteria ();
313 
315  static void set_default_criteria (const Criteria&);
316 
318  const Entry* best_match (const Criteria&) const;
319 
321  void all_matching (const Criteria&,
322  std::vector<const Entry*>& matches) const;
323 
325  Criteria criteria (const Pulsar::Archive* arch,
326  Signal::Source obsType) const;
327 
329  Criteria criteria (const Pulsar::Archive* archive,
330  const Calibrator::Type* calType) const;
331 
333  std::string get_filename (const Entry*) const;
334 
336  void add (Entry* entry);
337 
339  void shorten_filename (Entry* entry);
340 
342  void expand_filename (Entry* entry);
343 
345  std::string get_closest_match_report () const;
346 
347  protected:
348 
349  // list of entries in the database
350  std::vector< Reference::To<Entry> > entries;
351  std::string path;
352 
353  template<class Cal> class Cache
354  {
355  public:
357  Reference::To<Cal> calibrator;
358 
359  void cache (const Entry* _entry, Cal* cal)
360  { entry = _entry; calibrator = cal; }
361  };
362 
363  Cache<PolnCalibrator> lastPolnCal;
364  Cache<HybridCalibrator> lastHybridCal;
365  Cache<FluxCalibrator> lastFluxCal;
366 
368  FluxCalibrator* rawFluxCalibrator (Archive* a);
369 
372 
374  mutable Criteria closest_match;
375  };
376 
377  std::ostream& operator << (std::ostream& os, Database::Sequence);
378 
379  std::istream& operator >> (std::istream& is, Database::Sequence&);
380 }
381 
382 #endif
Calibrator
Definition: Integration.h:429
Flux calibrator.
Definition: CalibratorTypes.h:34
~Database()
Destructor.
Definition: Database.C:868
static bool match_verbose
Print verbose matching information.
Definition: Database.h:48
virtual void set_centre_frequency(double cf)=0
Set the centre frequency of the observation.
Supplements a SystemCalibrator with a SingleAxisCalibrator.
Definition: HybridCalibrator.h:26
std::string get_closest_match_report() const
Get the closest match report.
Definition: Database.C:1097
const Calibrator::Type * get_type() const
Get the type of the calibrator.
Definition: CalibrationInterpolatorExtension.C:74
static bool no_amps
When true, no memory is allocated for amps.
Definition: ProfileAmps.h:38
Data storage implementations.
Definition: Container.h:24
virtual Signal::Source get_type() const =0
Get the observation type (psr, cal)
Sequence
Calibrator matching sequence.
Definition: Database.h:71
static ReferenceCalibrator * factory(const Calibrator::Type *, const Archive *)
Factory creates instances of derived types.
Definition: ReferenceCalibrator.C:496
double diff_degrees
distance between calibrator and requested position (in degrees)
Definition: Database.h:263
static Criteria closest(const Criteria &a, const Criteria &b)
Return the criteria that came closest to matching.
Definition: Database.C:1134
Stores a known feed transformation.
Definition: FeedExtension.h:29
std::string get_name() const
Get the name of the receiver.
Definition: Receiver.h:90
virtual MJD get_epoch() const =0
Get the epoch of the rising edge of bin zero.
static Option< double > max_bandwidth_difference
Maximum difference between calibrator and pulsar bandwidths.
Definition: Database.h:63
Stores information about the instrument backend.
Definition: Backend.h:26
Database()
Null constructor.
Definition: Database.C:748
virtual bool is_a(const Type *that) const
Return true if that is a this.
Definition: CalibratorType.C:29
virtual void set_bandwidth(double bw)=0
Set the overall bandwidth of the observation.
static Option< double > short_time_scale
Time scale over which differential gain and phase remain stable.
Definition: Database.h:54
virtual unsigned get_nsubint() const =0
Get the number of sub-integrations stored in the file.
Full 7 degrees of freedom parameterization of Jones matrix.
Definition: CalibratorTypes.h:80
static Option< double > max_angular_separation
Maximum angular separation between calibrator and pulsar.
Definition: Database.h:57
virtual const Calibrator::Type * get_type() const
Get the type of the calibrator.
Definition: CalibratorExtension.C:53
~Entry()
Destructor.
Definition: Database.C:247
The primary interface to pulsar observational data.
Definition: Archive.h:45
PolnCalibrator * generatePolnCalibrator(Archive *, const Calibrator::Type *m)
Return a pointer to a new PolnCalibrator for the given archive.
Definition: Database.C:1422
static Archive * load(const std::string &name)
Factory returns a new instance loaded from filename.
Definition: Archive_load.C:28
Contains information about the receiver and receiver platform.
Definition: Receiver.h:28
Mixes a SingleAxis and Phenomenological parameterization.
Definition: CalibratorTypes.h:163
const ScalarMath pow(const ScalarMath &x, const ScalarMath &y)
void construct(const std::vector< std::string > &filenames)
Construct from the list of filenames.
Definition: Database.C:835
unsigned size() const
Returns the number of entries in the database.
Definition: Database.h:127
void all_matching(const Criteria &, std::vector< const Entry * > &matches) const
Fills a vector with Entry instances that match the given Criteria.
Definition: Database.C:1035
Stores the Stokes parameters of the reference source.
Definition: CalibratorStokes.h:28
Type * release()
Type * get() const
const ExtensionType * get() const
Template method searches for an Extension of the specified type.
static unsigned verbose
Verbosity level.
Definition: Calibrator.h:40
bool match(const Entry *entry) const
Return true if entry matches within the criteria.
Definition: Database.C:651
virtual unsigned get_nchan() const =0
Get the number of frequency channels used.
static Option< double > max_centre_frequency_difference
Maximum difference between calibrator and pulsar centre frequencies.
Definition: Database.h:60
void unload(const std::string &dbase_filename)
Write a text file representing the database to disk for storage.
Definition: Database.C:943
void init()
Clean slate.
Definition: Database.C:129
virtual double get_centre_frequency() const =0
Get the centre frequency of the observation.
Sequence sequence
The sequence of matching calibrator and pulsar observations.
Definition: Database.h:276
Integration * get_Integration(unsigned subint)
Return pointer to the specified Integration.
Definition: IntegrationManager.C:41
FluxCalibrator * generateFluxCalibrator(Archive *, bool allow_raw=false)
Return a pointer to a new FluxCalibrator for the given archive.
Definition: Database.C:1340
std::string get_filename() const
Returns the full pathname of the Entry filename.
Definition: Database.C:409
std::string get_name() const
Return the name of the Backend.
Definition: Backend.C:83
HybridCalibrator * generateHybridCalibrator(ReferenceCalibrator *, Archive *)
Return a pointer to a new HybridCalibrator.
Definition: Database.C:1557
static Option< double > long_time_scale
Time scale over which calibrator flux and cross-coupling remain stable.
Definition: Database.h:51
static Type * factory(const std::string &name)
Construct a new instance of Calibrator::Type, based on name.
Definition: CalibratorType.C:12
void no_data()
Called when no observation parameters are to be matched.
Definition: Database.C:571
Reference::To< StaticEntry > entry
The parameters to match.
Definition: Database.h:220
Calibrators derived from reference source observations.
Definition: ReferenceCalibrator.h:31
Reference::To< MEAL::Complex2 > feed
If set, this model of the feed is incorporated into all solutions.
Definition: Database.h:376
virtual MJD get_epoch() const
Get the reference epoch of the calibration experiment.
Definition: CalibratorExtension.C:58
Determine if one set of channels is a subset of another.
Definition: ChannelSubsetMatch.h:25
Definition: CalibratorType.h:30
virtual double get_bandwidth() const =0
Get the overall bandwidth of the observation.
void compare(const std::string &name, const T &want, const U &have, Predicate equal) const
log of the attributes compared and the result of the comparison
Definition: Database.h:279
double diff_minutes
time between calibrator and requested epoch (in minutes)
Definition: Database.h:260
std::string match_report
log of the attributes compared and the result of the comparison
Definition: Database.h:254
std::string get_reason() const
Get reason for match failure.
Definition: ChannelSubsetMatch.h:51
bool compare_times(const MJD &want, const MJD &have) const
log of the attributes compared and the result of the comparison
Definition: Database.C:596
void merge(const Database *)
Merge with another database.
Definition: Database.C:936
Stores Calibrator parameters in an Archive.
Definition: CalibratorExtension.h:27
@ Any
Use the nearest calibrator (default)
Definition: Database.h:73
Criteria()
Default constructor.
Definition: Database.C:551
Polarimetric calibrators.
Definition: PolnCalibrator.h:41
bool bracketed_time(const MJD &want, const std::pair< MJD, MJD > &have) const
log of the attributes compared and the result of the comparison
Definition: Database.C:622
std::string get_filename() const
Get the name of the file to which the archive will be unloaded.
Definition: Archive.h:108
unsigned match_count
count of the number of match conditions that tested positive
Definition: Database.h:257
void expand_filename(Entry *entry)
Add path to Entry filename, if applicable.
Definition: Database.C:985
void set_feed(MEAL::Complex2 *xform)
Set the feed transformation to be incorporated into PolnCalibrator.
Definition: Database.h:121
Criteria closest_match
The criteria that last came closest to matching.
Definition: Database.h:379
const Entry * best_match(const Criteria &) const
Returns the best Entry that matches the given Criteria.
Definition: Database.C:1075
Entry(const Archive *arch)
Construct from a Pulsar::Archive.
Definition: Database.C:148
Calibrates flux using standard candles and artificial sources.
Definition: FluxCalibrator.h:29
Pulsar Observation Database.
Definition: Database.h:40
FluxCalibrator * rawFluxCalibrator(Archive *a)
Return a pointer to a new FluxCalibrator for the given archive.
Definition: Database.C:1379
const std::string get_message() const
Pulsar Database Entry.
Definition: Database.h:130
static bool cache_last_cal
Cache the last calibrator?
Definition: Database.h:45
Defines the PSRCHIVE library.
Definition: CalSource.h:17
Stores Polarization Calibration Model Interpolator information.
Definition: CalibrationInterpolatorExtension.h:29
static const Pulsar::Archive * any
Pass this to the criteria methods to retrieve any or all matches.
Definition: Database.h:66
Criteria criteria(const Pulsar::Archive *arch, Signal::Source obsType) const
Return the Criteria for the specified Pulsar::Archive.
Definition: Database.C:1180
bool match(const Archive *super, const Archive *sub)
Check that Archive sub's channels all exist in super.
Definition: ChannelSubsetMatch.C:43
std::string get_filename(const Entry *) const
Returns the full pathname of the Entry filename.
Definition: Database.C:1633
Describes Database matching criteria.
Definition: Database.h:215
void shorten_filename(Entry *entry)
Remove the preceding path from the Entry filename, if applicable.
Definition: Database.C:993
@ CalibratorAfter
Use only calibrators recorded after the observation.
Definition: Database.h:77
std::string get_path() const
Returns the full path to the database summary file.
Definition: Database.C:1645
virtual sky_coord get_coordinates() const =0
Get the coordinates of the source.
static void set_default_criteria(const Criteria &)
Set the default matching criteria for all observations.
Definition: Database.C:1168
void set_sequence(Sequence s)
The sequence of matching calibrator and pulsar observations.
Definition: Database.h:223
void add(const Pulsar::Archive *archive)
Add the given Archive to the database.
Definition: Database.C:968
static Criteria get_default_criteria()
Get the default matching criteria for all observations.
Definition: Database.C:1160
const Entry * best(const Entry *a, const Entry *b) const
Return the best of two entries.
Definition: Database.C:1105
bool compare_coordinates(const sky_coord &want, const sky_coord &have) const
log of the attributes compared and the result of the comparison
Definition: Database.C:632
@ CalibratorBefore
Use only calibrators recorded before the observation.
Definition: Database.h:75
void load(const std::string &dbase_filename)
Read a text file summary and construct a database.
Definition: Database.C:873

Generated using doxygen 1.8.17