File: optionsfunction.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (236 lines) | stat: -rw-r--r-- 9,901 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
#include "optionsfunction.h"

#include <version.h>

extern "C" {
#include <lualib.h>
#include <lauxlib.h>
}

std::map<std::string, Options> OptionsFunction::GetOptions(
    lua_State* state, const Options& cmdLineOptions) {
  lua_getglobal(state, "options");
  const int error = lua_pcall(state, 0, 1, 0);
  std::map<std::string, Options> optionMap;
  if (error) {
    lua_pop(state, 1);  // pop error
  } else {
    // options function should have returned a table:
    if (!lua_istable(state, -1))
      throw std::runtime_error("Function options() did not return a table");

    // Iterate over the table. Keys are the name of the run, values
    // are itself a table with the options for that run.
    lua_pushnil(state);
    while (lua_next(state, -2) != 0) {
      // 'key' is at index -2 and 'value' is at index -1

      // It is not allowed to change 'key' on the stack during traversal, and
      // lua_tostring() might change it. Therefore, we make a temp copy of the
      // key:
      lua_pushvalue(state, -2);
      const char* key = lua_tostring(state, -1);
      lua_pop(state, 1);

      if (key == nullptr)
        throw std::runtime_error(
            "Function options() returned a table with keys that were not "
            "convertable to a string");
      if (!lua_istable(state, -1))
        throw std::runtime_error(
            std::string("Invalid type of element '") + key +
            "' return by function options(): should be an option table");
      const Options option =
          fillOptions(state, cmdLineOptions, std::string(key));
      optionMap.emplace(key, option);

      // remove 'value'; keeps 'key' for next iteration
      lua_pop(state, 1);
    }
  }
  return optionMap;
}

std::string OptionsFunction::strOption(lua_State* state,
                                       const std::string& keyName,
                                       const std::string& runName) {
  const char* val = lua_tostring(state, -1);
  if (val == nullptr)
    throw std::runtime_error(
        "Option " + keyName + " for run name '" + runName +
        "' returned by options() was not convertable to a string");
  return std::string(val);
}

bool OptionsFunction::boolOption(lua_State* state, const std::string& keyName,
                                 const std::string& runName) {
  if (!lua_isboolean(state, -1))
    throw std::runtime_error(
        "Option " + keyName + " for run name '" + runName +
        "' returned by options() should be of type boolean");
  return lua_toboolean(state, -1);
}

size_t OptionsFunction::uintOption(lua_State* state, const std::string& keyName,
                                   const std::string& runName) {
  if (!lua_isinteger(state, -1))
    throw std::runtime_error(
        "Option " + keyName + " for run name '" + runName +
        "' returned by options() should be of type integer");
  return lua_tointeger(state, -1);
}

std::vector<size_t> OptionsFunction::uintListOption(
    lua_State* state, const std::string& keyName, const std::string& runName) {
  if (!lua_istable(state, -1))
    throw std::runtime_error(
        "Option " + keyName + " for run name '" + runName +
        "' returned by options() should be a table of integers");
  std::vector<size_t> vals;
  lua_pushnil(state);
  while (lua_next(state, -2) != 0) {
    if (!lua_isinteger(state, -1))
      throw std::runtime_error(
          "Option " + keyName + " for run name '" + runName +
          "' returned by options() should be a table of integers");
    vals.emplace_back(lua_tointeger(state, -1));
    lua_pop(state, 1);
  }
  return vals;
}

std::vector<std::string> OptionsFunction::stringListOption(
    lua_State* state, const std::string& keyName, const std::string& runName) {
  if (!lua_istable(state, -1))
    throw std::runtime_error(
        "Option " + keyName + " for run name '" + runName +
        "' returned by options() should be a table of strings");
  std::vector<std::string> vals;
  lua_pushnil(state);
  while (lua_next(state, -2) != 0) {
    if (!lua_isstring(state, -1))
      throw std::runtime_error(
          "Option " + keyName + " for run name '" + runName +
          "' returned by options() should be a table of strings");
    vals.emplace_back(lua_tostring(state, -1));
    lua_pop(state, 1);
  }
  return vals;
}

Options OptionsFunction::fillOptions(lua_State* state,
                                     const Options& cmdLineOptions,
                                     const std::string& runName) {
  Options options;
  // Iterate over the table. Keys are the name of the run, values
  // are itself a table with the options for that run.
  lua_pushnil(state);
  while (lua_next(state, -2) != 0) {
    if (!lua_isstring(state, -2))
      throw std::runtime_error("options(): Key in option table for run name '" +
                               runName + "' was not convertable to a string");
    const char* key = lua_tostring(state, -2);
    const std::string keyStr(key);

    if (keyStr == "bands") {
      std::vector<size_t> list = uintListOption(state, keyStr, runName);
      options.bands = std::set<size_t>(list.begin(), list.end());
    } else if (keyStr == "baseline-integration") {
      options.baselineIntegration.enable = true;
      const std::string val = strOption(state, keyStr, runName);
      if (val == "count")
        options.baselineIntegration.mode = BaselineIntegration::Count;
      else if (val == "average")
        options.baselineIntegration.mode = BaselineIntegration::Average;
      else if (val == "average-abs")
        options.baselineIntegration.mode = BaselineIntegration::AverageAbs;
      else if (val == "squared")
        options.baselineIntegration.mode = BaselineIntegration::Squared;
      else if (val == "stddev")
        options.baselineIntegration.mode = BaselineIntegration::Stddev;
      else
        throw std::runtime_error(
            "options(): Invalid setting '" + val +
            "' for option 'baseline-integration' returned");
    } else if (keyStr == "baselines") {
      const std::string val = strOption(state, keyStr, runName);
      if (val == "all")
        options.baselineSelection = BaselineSelection::All;
      else if (val == "cross")
        options.baselineSelection = BaselineSelection::CrossCorrelations;
      else if (val == "auto")
        options.baselineSelection = BaselineSelection::AutoCorrelations;
      else
        throw std::runtime_error("options(): Invalid setting '" + val +
                                 "' for option 'baselines' returned");
    } else if (keyStr == "chunk-size") {
      options.chunkSize = uintOption(state, keyStr, runName);
    } else if (keyStr == "column-name") {
      options.dataColumn = strOption(state, keyStr, runName);
    } else if (keyStr == "combine-spws") {
      options.combineSPWs = boolOption(state, keyStr, runName);
    } else if (keyStr == "execute-file") {
      options.executeFilename = strOption(state, keyStr, runName);
    } else if (keyStr == "execute-function") {
      options.executeFunctionName = strOption(state, keyStr, runName);
    } else if (keyStr == "fields") {
      std::vector<size_t> list = uintListOption(state, keyStr, runName);
      options.fields = std::set<size_t>(list.begin(), list.end());
    } else if (keyStr == "files") {
      options.filenames = stringListOption(state, keyStr, runName);
    } else if (keyStr == "min‑aoflagger-version") {
      const std::string minVersion = strOption(state, keyStr, runName);
      const size_t dot = minVersion.find('.');
      if (dot == minVersion.npos)
        throw std::runtime_error(
            "options(): Invalid version specified in option "
            "min-aoflagger-version: should be of the form major.minor");
      int major = std::atoi(minVersion.substr(0, dot).c_str()),
          minor = std::atoi(minVersion.substr(dot + 1).c_str());
      const bool tooOld =
          (AOFLAGGER_VERSION_MAJOR < major) ||
          (AOFLAGGER_VERSION_MAJOR == major && AOFLAGGER_VERSION_MINOR < minor);
      if (tooOld)
        throw std::runtime_error(
            "AOFlagger is too old for this script: required: " + minVersion +
            ", this version: " + AOFLAGGER_VERSION_STR);
    } else if (keyStr == "quiet") {
      if (boolOption(state, keyStr, runName))
        options.logVerbosity = Logger::QuietVerbosity;
    } else if (keyStr == "read-mode") {
      const std::string readMode = strOption(state, keyStr, runName);
      if (readMode == "direct")
        options.readMode = DirectReadMode;
      else if (readMode == "indirect")
        options.readMode = ReorderingReadMode;
      else if (readMode == "memory")
        options.readMode = MemoryReadMode;
      else if (readMode == "auto")
        options.readMode = AutoReadMode;
      else
        throw std::runtime_error(
            "options(): Invalid setting for option 'read-mode' returned");
    } else if (keyStr == "read-uvws") {
      options.readUVW = boolOption(state, keyStr, runName);
    } else if (keyStr == "script-version") {
      options.scriptVersion = strOption(state, keyStr, runName);
    } else if (keyStr == "start-timestep") {
      options.startTimestep = uintOption(state, keyStr, runName);
    } else if (keyStr == "end-timestep") {
      options.endTimestep = uintOption(state, keyStr, runName);
    } else if (keyStr == "threads") {
      options.threadCount = uintOption(state, keyStr, runName);
    } else if (keyStr == "verbose") {
      // Option 'quiet' conflicts with verbose; quiet override verbose:
      if (boolOption(state, keyStr, runName) && !options.logVerbosity)
        options.logVerbosity = Logger::VerboseVerbosity;
    } else {
      Logger::Warn << "options(): Ignoring unknown key '" + keyStr + "'.\n";
    }

    // remove 'value'; keeps 'key' for next iteration
    lua_pop(state, 1);
  }
  options.Override(cmdLineOptions);
  return options;
}