File: modelparser.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 (279 lines) | stat: -rw-r--r-- 9,883 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifndef MODEL_PARSER_H
#define MODEL_PARSER_H

#include "model.h"
#include "modelsource.h"

#include "tokenizer.h"
#include "powerlawsed.h"

#include <cstdlib>
#include <fstream>
#include <stdexcept>

namespace wsclean {

class ModelParser : private Tokenizer {
 public:
  ModelParser() : _fileVersion1_0(false) {}

  /**
   * Test if the first line of this stream corresponds with the
   * 'ao' source model format. This function advances the stream
   * to the next line.
   */
  static bool IsInModelFormat(std::istream &stream) {
    std::string line;
    std::getline(stream, line);
    return versionLineIsCorrect(line);
  }

  void Parse(Model &model, std::ifstream &stream) {
    SetStream(stream);

    std::string line;
    std::getline(stream, line);
    parseVersionLine(line);
    if (stream.bad()) throw std::runtime_error("Error parsing model");

    std::string token;
    while (getToken(token)) {
      if (token != "source") throw std::runtime_error("Expecting source");

      ModelSource source;
      parseSource(source);
      model.AddSource(source);
      model.FindOrAddCluster(source.ClusterName());
    }
  }

  void Stream(std::ifstream &stream,
              std::function<void(ModelSource &source)> processSource) {
    SetStream(stream);

    std::string line;
    std::getline(stream, line);
    parseVersionLine(line);
    if (stream.bad()) throw std::runtime_error("Error parsing model");

    std::string token;
    while (getToken(token)) {
      if (token != "source") throw std::runtime_error("Expecting source");

      ModelSource source;
      parseSource(source);
      processSource(source);
    }
  }

 private:
  bool _fileVersion1_0;

  static bool versionLineIsCorrect(const std::string &line) {
    const std::string headerStart = "skymodel fileformat ";
    if (line.substr(0, headerStart.size()) != headerStart) return false;
    std::string version = line.substr(headerStart.size());
    if (version != "1.0" && version != "1.1") return false;
    return true;
  }

  void parseVersionLine(const std::string &line) {
    const std::string headerStart = "skymodel fileformat ";
    if (line.substr(0, headerStart.size()) != headerStart)
      throw std::runtime_error(
          "The model file didn't start with a skymodel header");
    std::string version = line.substr(headerStart.size());
    if (version == "1.0") {
      _fileVersion1_0 = true;
      std::cout
          << "Warning: this model is written in the old sky-model file format. "
             "The old format does \n"
             "         not properly support polarization, so is deprecated.\n"
             "         Use \"editmodel -m new_model.txt old_model.txt\" to "
             "convert the model.\n";
    } else if (version != "1.1") {
      throw std::runtime_error("This model specified file version \"" +
                               version + "\": don't know how to read.\n");
    } else
      _fileVersion1_0 = false;
  }

  void parseSource(ModelSource &source) {
    std::string token;
    getToken(token);
    if (token != "{") throw std::runtime_error("Expecting {");
    while (getToken(token) && token != "}") {
      if (token == "name")
        source.SetName(getString());
      else if (token == "cluster")
        source.SetClusterName(getString());
      else if (token == "component") {
        ModelComponent component;
        parseComponent(component);
        source.AddComponent(component);
      } else
        throw std::runtime_error(std::string("Unknown token ") + token);
    }
  }

  void parseComponent(ModelComponent &component) {
    std::string token;
    getToken(token);
    if (token != "{") throw std::runtime_error("Expecting {");
    while (getToken(token) && token != "}") {
      if (token == "type") {
        getToken(token);
        if (token == "point")
          component.SetType(ModelComponent::PointSource);
        else if (token == "gaussian")
          component.SetType(ModelComponent::GaussianSource);
        else
          throw std::runtime_error("Unsupported component type");
      } else if (token == "position") {
        getToken(token);
        try {
          component.SetPosRA(RaDecCoord::ParseRA(token));
        } catch (std::exception &e) {
          throw std::runtime_error("Failed to parse RA: " + token + ",\n" +
                                   e.what());
        }
        getToken(token);
        try {
          component.SetPosDec(RaDecCoord::ParseDec(token));
        } catch (std::exception &e) {
          throw std::runtime_error("Failed to parse dec: " + token + ",\n" +
                                   e.what());
        }
      } else if (token == "measurement") {
        Measurement measurement;
        parseMeasurement(measurement);
        if (component.HasMeasuredSED())
          component.MSED().AddMeasurement(measurement);
        else if (component.HasSED())
          throw std::runtime_error(
              "Invalid 'measurement' combined with other brightness "
              "specification");
        else {
          component.SetSED(MeasuredSED());
          component.MSED().AddMeasurement(measurement);
        }
      } else if (token == "sed") {
        PowerLawSED plSED;
        parsePowerLawSED(plSED);
        if (component.HasSED())
          throw std::runtime_error(
              "Invalid 'sed' combined with other brightness specification");
        else
          component.SetSED(plSED);
      } else if (token == "shape") {
        getToken(token);
        component.SetMajorAxis(atof(token.c_str()) * M_PI / 60.0 / 60.0 /
                               180.0);
        getToken(token);
        component.SetMinorAxis(atof(token.c_str()) * M_PI / 60.0 / 60.0 /
                               180.0);
        getToken(token);
        component.SetPositionAngle(atof(token.c_str()) * M_PI / 180.0);
      } else if (token == "major-axis") {
        getToken(token);
        component.SetMajorAxis(atof(token.c_str()) * M_PI / 60.0 / 60.0 /
                               180.0);
      } else if (token == "minor-axis") {
        getToken(token);
        component.SetMinorAxis(atof(token.c_str()) * M_PI / 60.0 / 60.0 /
                               180.0);
      } else if (token == "position-angle") {
        getToken(token);
        component.SetPositionAngle(atof(token.c_str()) * M_PI / 180.0);
      } else
        throw std::runtime_error("Unknown keyname in component");
    }
  }
  void parseMeasurement(Measurement &measurement) {
    std::string token;
    getToken(token);
    if (token != "{") throw std::runtime_error("Expecting {");
    while (getToken(token) && token != "}") {
      if (token == "frequency") {
        measurement.SetFrequencyHz(getTokenAsDouble() * 1000000.0);
        getToken(token);
      } else if (token == "fluxdensity") {
        getToken(token);  // unit
        if (_fileVersion1_0) {
          std::complex<double> linFluxes[4];
          for (size_t p = 0; p != 4; ++p) linFluxes[p] = getTokenAsDouble();
          double stokesFluxes[4];
          aocommon::Polarization::LinearToStokes(linFluxes, stokesFluxes);
          for (size_t p = 0; p != 4; ++p)
            measurement.SetFluxDensityFromIndex(p, stokesFluxes[p]);
        } else {
          for (size_t p = 0; p != 4; ++p)
            measurement.SetFluxDensityFromIndex(p, getTokenAsDouble());
        }
      } else if (token == "type") {
        getToken(token);
        if (token == "absolute")
          ;  // ignore
        else if (token == "apparent")
          throw std::runtime_error("Model no longer allows apparent values");
        else
          throw std::runtime_error(
              "Measurement type should be absolute or apparent");
      } else if (token == "bandwidth") {
        measurement.SetBandWidthHz(getTokenAsDouble());
        getToken(token);
      } else if (token == "fluxdensity-stddev") {
        getToken(token);
        for (size_t p = 0; p != 4; ++p)
          measurement.SetFluxDensityStddevFromIndex(p, getTokenAsDouble());
      } else if (token == "beam-value") {
        // ignore
      } else
        throw std::runtime_error(std::string("Unknown token ") + token);
    }
  }

  void parsePowerLawSED(PowerLawSED &sed) {
    std::string token;
    getToken(token);
    if (token != "{") throw std::runtime_error("Expecting {");
    double refFrequency = 0.0;
    double brightness[4] = {0.0, 0.0, 0.0, 0.0};
    bool isLogarithmic = true;
    aocommon::UVector<double> terms;
    bool hasFrequency = false, hasBrightness = false;
    while (getToken(token) && token != "}") {
      if (token == "frequency") {
        if (hasFrequency)
          throw std::runtime_error("Double frequency specification");
        refFrequency = getTokenAsDouble() * 1000000.0;
        getToken(token);  // unit
        hasFrequency = true;
      } else if (token == "fluxdensity") {
        if (hasBrightness)
          throw std::runtime_error("Double brightness specification");
        getToken(token);  // unit
        for (size_t p = 0; p != 4; ++p) brightness[p] = getTokenAsDouble();
        hasBrightness = true;
      } else if (token == "spectral-index" || token == "polynomial") {
        isLogarithmic = (token == "spectral-index");
        if (!terms.empty())
          throw std::runtime_error("Double SI/polynomial specification");
        getToken(token);
        if (token != "{") throw std::runtime_error("Expecting {");
        while (getToken(token) && token != "}") {
          terms.push_back(atof(token.c_str()));
        }
      } else
        throw std::runtime_error(std::string("Unknown token ") + token);
    }
    if (!hasFrequency || !hasBrightness || terms.empty())
      throw std::runtime_error("Incomplete SED specification");
    sed.SetData(refFrequency, brightness, terms);
    sed.SetIsLogarithmic(isLogarithmic);
  }
};

}  // namespace wsclean

#endif