PURIFY
Next-generation radio interferometric imaging
casacore.cc
Go to the documentation of this file.
1 #include "purify/casacore.h"
2 #include <boost/filesystem.hpp>
3 #include <casacore/ms/MeasurementSets/MeasurementSet.h>
4 #include <casacore/tables/TaQL/TableParse.h>
5 #include <casacore/tables/Tables/ArrColDesc.h>
6 #include <casacore/tables/Tables/ArrayColumn.h>
7 #include <casacore/tables/Tables/ColumnDesc.h>
8 #include <casacore/tables/Tables/ScaColDesc.h>
9 #include <casacore/tables/Tables/ScalarColumn.h>
10 #include <casacore/tables/Tables/SetupNewTab.h>
11 #include <casacore/tables/Tables/TableColumn.h>
12 #include "purify/directories.h"
13 
14 #include "purify/types.h"
15 #include "purify/utilities.h"
16 
17 #include "catch2/catch_all.hpp"
18 namespace casa = casacore;
19 
20 TEST_CASE("Casacore") {
21  // create the table descriptor
22  casa::TableDesc simpleDesc = casa::MS::requiredTableDesc();
23  // set up a new table
24  casa::SetupNewTable newTab("simpleTab", simpleDesc, casa::Table::New);
25  // create the MeasurementSet
26  casa::MeasurementSet simpleMS(newTab);
27  // now we need to define all required subtables
28  // the following call does this for us if we don't need to
29  // specify details of Storage Managers for columns.
30  simpleMS.createDefaultSubtables(casa::Table::New);
31  // fill MeasurementSet via its Table interface
32  // For example, construct one of the columns
33  casa::TableColumn feed(simpleMS, casa::MS::columnName(casa::MS::FEED1));
34  casa::uInt rownr = 0;
35  // add a row
36  simpleMS.addRow();
37  // set the values in that row, e.g. the feed column
38  feed.putScalar(rownr, 1);
39  // Access a subtable
40  casa::ArrayColumn<casa::Double> antpos(simpleMS.antenna(),
41  casa::MSAntenna::columnName(casa::MSAntenna::POSITION));
42  simpleMS.antenna().addRow();
43  casa::Array<casa::Double> position(casa::IPosition(1, 3));
44  position(casa::IPosition(1, 0)) = 1.;
45  position(casa::IPosition(1, 1)) = 2.;
46  position(casa::IPosition(1, 2)) = 3.;
47  antpos.put(0, position);
48 }
49 
50 class TmpPath {
51  public:
53  : path_(boost::filesystem::unique_path(boost::filesystem::temp_directory_path() /
54  "%%%%-%%%%-%%%%-%%%%.ms")) {}
56  if (boost::filesystem::exists(path())) boost::filesystem::remove_all(path());
57  }
58  boost::filesystem::path const &path() const { return path_; }
59 
60  private:
61  boost::filesystem::path path_;
62 };
63 
64 class TmpMS : public TmpPath {
65  public:
66  TmpMS() : TmpPath() {
67  casa::TableDesc simpleDesc = casa::MS::requiredTableDesc();
68  casa::SetupNewTable newTab(path().string(), simpleDesc, casa::Table::New);
69  ms_.reset(new casa::MeasurementSet(newTab));
70  ms_->createDefaultSubtables(casa::Table::New);
71  }
72  casa::MeasurementSet const &operator*() const { return *ms_; }
73  casa::MeasurementSet &operator*() { return *ms_; }
74 
75  casa::MeasurementSet const *operator->() const { return ms_.get(); }
76  casa::MeasurementSet *operator->() { return ms_.get(); }
77 
78  protected:
79  std::unique_ptr<casa::MeasurementSet> ms_;
80 };
81 
82 const std::string test_file = atca_filename("0332-391.ms");
83 
84 TEST_CASE("Size/Number of channels") {
86 }
87 
88 TEST_CASE("Single channel") {
89  namespace pc = purify::casa;
90  auto const ms = pc::MeasurementSet(test_file);
91  SECTION("Check channel validity") {
92  CHECK(pc::MeasurementSet::const_iterator::value_type(0, ms).is_valid());
93  CHECK(pc::MeasurementSet::const_iterator::value_type(4, ms).is_valid());
94  }
95  // Raw data from the measurement set "0332-391.ms" was read out using CASA's casabrowser
96  // executable. Then it was copied into this test.
97  SECTION("Raw UVW") {
98  auto const channel = pc::MeasurementSet::const_iterator::value_type(11, ms);
99  REQUIRE(channel.size() == 20541);
100  auto const u = channel.raw_u();
101  REQUIRE(u.size() == 20541);
102  CHECK(std::abs(u[0] - 3889.46519177759410013095475733280181884765625) < 1e-8);
103  CHECK(std::abs(u[3360] - 683.2842475491) < 1e-8);
104  auto const v = channel.raw_v();
105  REQUIRE(v.size() == 20541);
106  CHECK(std::abs(v[0] - 1346.20383349449502929928712546825408935546875) < 1e-8);
107  CHECK(std::abs(v[3360] + 785.8117311632) < 1e-8);
108  auto const w = channel.raw_w();
109  REQUIRE(w.size() == 20541);
110  CHECK(std::abs(w[0] - 1663.30025165469896819558925926685333251953125) < 1e-8);
111  CHECK(std::abs(w[3360] + 970.3123979733) < 1e-8);
112  }
113 
114  SECTION("Raw frequencies") {
115  auto const f0 = pc::MeasurementSet::const_iterator::value_type(0, ms).raw_frequencies();
116  CHECK(f0.size() == 1);
117  CHECK(std::abs(f0(0) - 1431999959.500274181365966796875) < 1e-1);
118 
119  auto const f11 = pc::MeasurementSet::const_iterator::value_type(11, ms).raw_frequencies();
120  CHECK(f11.size() == 1);
121  CHECK(std::abs(f11(0) - 1343999961.9890842437744140625) < 1e-1);
122  }
123 
124  SECTION("data desc id") {
125  REQUIRE(pc::MeasurementSet::const_iterator::value_type(0, ms).data_desc_id().size() == 20490);
126  REQUIRE(pc::MeasurementSet::const_iterator::value_type(4, ms).data_desc_id().size() == 20606);
127  }
128 
129  SECTION("I") {
130  using namespace purify;
131  auto const I = pc::MeasurementSet::const_iterator::value_type(11, ms).I();
132  REQUIRE(I.size() == 20541);
133  CHECK(std::abs(I(0) - t_complex(0.1666463911533, -0.05232101678848)) < 1e-12);
134  CHECK(std::abs(I(10) - t_complex(0.1421023607254, -0.04858251661062)) < 1e-12);
135 
136  REQUIRE(pc::MeasurementSet::const_iterator::value_type(0, ms).I().size() == 20490);
137  }
138 
139  SECTION("wI") {
140  using namespace purify;
141  auto const wI = pc::MeasurementSet::const_iterator::value_type(11, ms).wI();
142  REQUIRE(wI.size() == 20541);
143  CAPTURE(wI.head(5).transpose());
144  CHECK(wI.isApprox(0.5 * purify::Vector<t_real>::Ones(wI.size())));
145  }
146 
147  SECTION("frequencies") {
148  using namespace purify;
149  auto const f = pc::MeasurementSet::const_iterator::value_type(11, ms).frequencies();
150  REQUIRE(f.size() == 20541);
151  CHECK(std::abs(f(0) - 1343999961.989) < 1e-0);
152  CHECK(std::abs(f(1680) - 1343999961.989) < 1e-0);
153  CHECK(std::abs(f(3360) - 1343999961.989) < 1e-0);
154  CHECK(std::abs(f(5040) - 1343999961.989) < 1e-0);
155  }
156 }
157 
158 TEST_CASE("Measurement channel") {
159  using namespace purify;
160  auto const channel = purify::casa::MeasurementSet(test_file)[0];
161  REQUIRE(channel.is_valid());
162  auto const I = channel.I();
163  REQUIRE(I.size() == 20490);
164  CHECK(std::abs(I(0) - t_complex(-0.01469034608454, -0.00434834882617)) < 1e-12);
165  CHECK(std::abs(I(10) - t_complex(-0.09461227059364, -0.01139064785093)) < 1e-12);
166 }
167 
168 TEST_CASE("Channel iteration") {
169  std::vector<int> const valids{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
170  auto const ms = purify::casa::MeasurementSet(test_file);
171  auto i_channel = ms.begin();
172  auto const i_end = ms.end();
173  for (; i_channel < i_end; i_channel += 5) {
174  CHECK(i_channel->channel() < 13);
175  bool const is_valid =
176  std::find(valids.begin(), valids.end(), i_channel->channel()) != valids.end();
177  CHECK(is_valid == i_channel->is_valid());
178  }
179 }
180 
181 TEST_CASE("Direction") {
182  auto const ms = purify::casa::MeasurementSet(test_file);
183  auto const direction = ms.direction();
184  auto const right_ascension = ms.right_ascension();
185  auto const declination = ms.declination();
186  CHECK(std::abs(right_ascension - 0.934273294000000031900299291010014712810516357421875) < 1e-8);
187  CHECK(std::abs(declination + 0.68069387400000003207622967238421551883220672607421875) < 1e-8);
188  CHECK(std::abs(direction[0] - 0.934273294000000031900299291010014712810516357421875) < 1e-8);
189  CHECK(std::abs(direction[1] + 0.68069387400000003207622967238421551883220672607421875) < 1e-8);
190 }
#define CHECK(CONDITION, ERROR)
Definition: casa.cc:6
TmpMS()
Definition: casacore.cc:66
casa::MeasurementSet * operator->()
Definition: casacore.cc:76
casa::MeasurementSet const & operator*() const
Definition: casacore.cc:72
casa::MeasurementSet & operator*()
Definition: casacore.cc:73
casa::MeasurementSet const * operator->() const
Definition: casacore.cc:75
~TmpPath()
Definition: casacore.cc:55
TmpPath()
Definition: casacore.cc:52
boost::filesystem::path const & path() const
Definition: casacore.cc:58
Interface around measurement sets.
Definition: casacore.h:27
const std::vector< t_real > u
data for u coordinate
Definition: operators.cc:18
const std::vector< t_real > v
data for v coordinate
Definition: operators.cc:20
std::string atca_filename(std::string const &filename)
Specific atca data.
const std::string test_file
Definition: casacore.cc:82
TEST_CASE("Casacore")
Definition: casacore.cc:20