File: model.h

package info (click to toggle)
wsclean 3.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,296 kB
  • sloc: cpp: 129,246; python: 22,066; sh: 360; ansic: 230; makefile: 185
file content (193 lines) | stat: -rw-r--r-- 5,976 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef MODEL_H
#define MODEL_H

#include <algorithm>
#include <vector>

#include "modelsource.h"

namespace wsclean {

class Model {
 public:
  typedef std::vector<ModelSource>::iterator iterator;
  typedef std::vector<ModelSource>::const_iterator const_iterator;

  Model() {}

  Model(const Model&) = default;
  Model(Model&&) = default;

  explicit Model(const char* filename) { read(filename); }
  explicit Model(const std::string& filename) { read(filename.c_str()); }

  Model& operator=(const Model&) = default;
  Model& operator=(Model&&) = default;

  void operator+=(const Model& rhs);

  size_t SourceCount() const { return _sources.size(); }

  size_t ClusterCount() const { return _clusters.size(); }

  bool Empty() const { return _sources.size() == 0; }

  ModelSource& Source(size_t index) { return _sources[index]; }
  const ModelSource& Source(size_t index) const { return _sources[index]; }

  const_iterator begin() const { return _sources.begin(); }
  const_iterator end() const { return _sources.end(); }

  iterator begin() { return _sources.begin(); }
  iterator end() { return _sources.end(); }

  static size_t npos;

  void AddSource(const ModelSource& source) { _sources.push_back(source); }

  /**
   * Returns the index of the source with the given name, or npos if not found.
   */
  size_t FindSourceIndex(const std::string& sourceName) const {
    for (const_iterator i = begin(); i != end(); ++i) {
      if (i->Name() == sourceName) return i - begin();
    }
    return npos;
  }

  void AddCluster(const ModelCluster& cluster) {
    bool wasAdded =
        _clusters.insert(std::make_pair(cluster.Name(), cluster)).second;
    if (!wasAdded)
      throw std::runtime_error(
          "AddCluster(): Adding cluster with duplicate name");
  }

  ModelCluster& FindOrAddCluster(const std::string& clusterName) {
    std::map<std::string, ModelCluster>::iterator c =
        _clusters.find(clusterName);
    if (c == _clusters.end()) {
      ModelCluster newCluster;
      newCluster.SetName(clusterName);
      c = _clusters.insert(std::make_pair(clusterName, newCluster)).first;
    }
    return c->second;
  }

  ModelCluster& FindCluster(const std::string& clusterName) {
    std::map<std::string, ModelCluster>::iterator c =
        _clusters.find(clusterName);
    if (c == _clusters.end())
      throw std::runtime_error("FindCluster(): Non-existing cluster '" +
                               clusterName + "' requested.");
    return c->second;
  }

  const ModelCluster& FindCluster(const std::string& clusterName) const {
    std::map<std::string, ModelCluster>::const_iterator c =
        _clusters.find(clusterName);
    if (c == _clusters.end())
      throw std::runtime_error("FindCluster(): Non-existing cluster '" +
                               clusterName + "' requested.");
    return c->second;
  }

  void GetSourcesInCluster(const std::string& clusterName,
                           SourceGroup& destGroup) const {
    for (const_iterator s = begin(); s != end(); ++s) {
      if (s->ClusterName() == clusterName) destGroup.AddSource(*s);
    }
  }

  void GetClusterNames(std::vector<std::string>& clusterNames) {
    clusterNames.clear();
    clusterNames.reserve(_clusters.size());
    std::set<std::string> clusterWasAdded;
    for (const_iterator s = begin(); s != end(); ++s) {
      if (clusterWasAdded.count(s->ClusterName()) == 0) {
        clusterNames.push_back(s->ClusterName());
        clusterWasAdded.insert(s->ClusterName());
      }
    }
  }

  void RemoveSource(size_t index) { _sources.erase(_sources.begin() + index); }

  void Save(const std::string& filename) const { Save(filename.c_str()); }
  void Save(const char* filename) const;
  void Save(std::ostream& stream) const;

  double TotalFlux(double frequencyStartHz, double frequencyEndHz,
                   aocommon::PolarizationEnum polarization) const {
    double flux = 0.0;
    for (const_iterator i = begin(); i != end(); ++i)
      flux += i->TotalFlux(frequencyStartHz, frequencyEndHz, polarization);

    return flux;
  }

  double TotalFlux(double frequency,
                   aocommon::PolarizationEnum polarization) const {
    double flux = 0.0;
    for (const_iterator i = begin(); i != end(); ++i)
      flux += i->TotalFlux(frequency, polarization);

    return flux;
  }

  void SetUnpolarized() {
    for (iterator sourcePtr = begin(); sourcePtr != end(); ++sourcePtr) {
      for (ModelSource::iterator compPtr = sourcePtr->begin();
           compPtr != sourcePtr->end(); ++compPtr) {
        if (compPtr->HasMeasuredSED()) {
          MeasuredSED& sed = compPtr->MSED();
          for (MeasuredSED::iterator m = sed.begin(); m != sed.end(); ++m) {
            long double totalFlux =
                m->second.FluxDensity(aocommon::Polarization::StokesI);
            m->second.SetZeroExceptSinglePol(aocommon::Polarization::StokesI,
                                             totalFlux);
          }
        }
      }
    }
  }

  void CombineMeasurements(const Model& model) {
    if (Empty())
      (*this) = model;
    else {
      for (const_iterator i = model.begin(); i != model.end(); ++i)
        combineMeasurements(*i);
    }
  }

  size_t ComponentCount() const {
    size_t count = 0;
    for (const_iterator i = begin(); i != end(); ++i)
      count += i->ComponentCount();
    return count;
  }

  void Sort() { std::sort(_sources.rbegin(), _sources.rend()); }

  template <class Compare>
  void Sort(Compare comp) {
    std::sort(_sources.rbegin(), _sources.rend(), comp);
  }

 private:
  void read(const char* filename);
  std::vector<ModelSource> _sources;
  std::map<std::string, ModelCluster> _clusters;

  static bool isCommentSymbol(char c) { return c == '#'; }
  static bool isDelimiter(char c) {
    return c == ' ' || c == '\t' || c == '\r' || c == '\n';
  }
  void add(const ModelSource& source);
  void combineMeasurements(const ModelSource& source);
};

}  // namespace wsclean

#endif