PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/components/ComponentModels/test/tSpectralIndex.cc

http://casacore.googlecode.com/
C++ | 172 lines | 139 code | 4 blank | 29 comment | 7 complexity | b46d02c811d5719aeda802f225dace27 MD5 | raw file
Possible License(s): GPL-2.0
  1. //# tSpectralIndex.cc: tests the SpectralIndex class
  2. //# Copyright (C) 1998,1999,2000,2001,2003
  3. //# Associated Universities, Inc. Washington DC, USA.
  4. //#
  5. //# This library is free software; you can redistribute it and/or modify it
  6. //# under the terms of the GNU Library General Public License as published by
  7. //# the Free Software Foundation; either version 2 of the License, or (at your
  8. //# option) any later version.
  9. //#
  10. //# This library is distributed in the hope that it will be useful, but WITHOUT
  11. //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  13. //# License for more details.
  14. //#
  15. //# You should have received a copy of the GNU Library General Public License
  16. //# along with this library; if not, write to the Free Software Foundation,
  17. //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
  18. //#
  19. //# Correspondence concerning AIPS++ should be addressed as follows:
  20. //# Internet email: aips2-request@nrao.edu.
  21. //# Postal address: AIPS++ Project Office
  22. //# National Radio Astronomy Observatory
  23. //# 520 Edgemont Road
  24. //# Charlottesville, VA 22903-2475 USA
  25. //#
  26. //# $Id: tSpectralIndex.cc 18093 2004-11-30 17:51:10Z ddebonis $
  27. #include <casa/aips.h>
  28. #include <components/ComponentModels/ComponentType.h>
  29. #include <components/ComponentModels/SpectralIndex.h>
  30. #include <casa/Arrays/Vector.h>
  31. #include <casa/Containers/Record.h>
  32. #include <casa/Containers/RecordFieldId.h>
  33. #include <casa/Exceptions/Error.h>
  34. #include <casa/BasicMath/Math.h>
  35. #include <measures/Measures/MFrequency.h>
  36. #include <measures/Measures/MeasureHolder.h>
  37. #include <measures/Measures/MeasRef.h>
  38. #include <casa/Quanta/MVFrequency.h>
  39. #include <casa/Quanta/Quantum.h>
  40. #include <casa/Utilities/Assert.h>
  41. #include <casa/BasicSL/String.h>
  42. #include <casa/iostream.h>
  43. #include <casa/namespace.h>
  44. int main() {
  45. try {
  46. SpectralModel* siPtr = 0;;
  47. const MFrequency f1(Quantity(1.0, "GHz"), MFrequency::LSRK);
  48. const MFrequency f2(Quantity(2.0, "GHz"), MFrequency::LSRK);
  49. const MFrequency f4(Quantity(4.0, "GHz"), MFrequency::LSRK);
  50. {
  51. SpectralIndex siModel;
  52. AlwaysAssert(siModel.ok(), AipsError);
  53. AlwaysAssert(siModel.type() == ComponentType::SPECTRAL_INDEX,
  54. AipsError);
  55. AlwaysAssert(near(siModel.index(), 0.0), AipsError);
  56. cout << "Passed the default constructor test" << endl;
  57. AlwaysAssert(near(siModel.sample(f1), 1.0), AipsError);
  58. AlwaysAssert(near(siModel.sample(f2), 1.0), AipsError);
  59. siModel.setIndex(1.0);
  60. AlwaysAssert(near(siModel.sample(f1), 1.0), AipsError);
  61. AlwaysAssert(near(siModel.sample(f2), 2.0), AipsError);
  62. siModel.setRefFrequency(f2);
  63. siPtr = siModel.clone();
  64. cout << "Passed the index test" << endl;
  65. }
  66. {
  67. Vector<Double> p = siPtr->parameters().copy();
  68. AlwaysAssert(near(p(0), 1.0), AipsError);
  69. AlwaysAssert(near(siPtr->refFrequency().get("GHz").getValue(), 2.0),
  70. AipsError);
  71. AlwaysAssert(siPtr->nParameters() == 1, AipsError);
  72. p(0) = -1.0;
  73. siPtr->setParameters(p);
  74. cout << "Passed the parameters test" << endl;
  75. SpectralIndex copy(*((SpectralIndex*)siPtr));
  76. SpectralIndex assigned;
  77. assigned = copy;
  78. p(0) = 1.0;
  79. siPtr->setParameters(p);
  80. siPtr->setRefFrequency(f4);
  81. copy.setIndex(0.0);
  82. copy.setRefFrequency(f1);
  83. p(0) = -10.0;
  84. p = siPtr->parameters();
  85. AlwaysAssert(near(p(0), 1.0), AipsError);
  86. AlwaysAssert(near(siPtr->refFrequency().get("GHz").getValue(), 4.0),
  87. AipsError);
  88. AlwaysAssert(near(copy.index(), 0.0), AipsError);
  89. AlwaysAssert(near(copy.refFrequency().get("GHz").getValue(), 1.0),
  90. AipsError);
  91. AlwaysAssert(near(assigned.index(), -1.0), AipsError);
  92. AlwaysAssert(near(assigned.refFrequency().get("GHz").getValue(), 2.0),
  93. AipsError);
  94. cout << "Passed the copy semantics test" << endl;
  95. }
  96. {
  97. SpectralIndex siModel(f2, 1.0);
  98. AlwaysAssert(near(siModel.index(), 1.0), AipsError);
  99. AlwaysAssert(near(siModel.refFrequency().get("GHz").getValue(), 2.0),
  100. AipsError);
  101. Vector<MVFrequency> freqs(3);
  102. freqs(0) = f1.getValue();
  103. freqs(1) = f2.getValue();
  104. freqs(2) = f4.getValue();
  105. MFrequency::Ref ref(MFrequency::LSRK);
  106. Vector<Double> results(3);
  107. siModel.sample(results, freqs, ref);
  108. AlwaysAssert(near(results(0), 0.5), AipsError);
  109. AlwaysAssert(near(results(1), 1.0), AipsError);
  110. AlwaysAssert(near(results(2), 2.0), AipsError);
  111. cout << "Passed the multi-sample test" << endl;
  112. Record rec;
  113. String errMsg;
  114. AlwaysAssert(siModel.toRecord(errMsg, rec), AipsError);
  115. AlwaysAssert(errMsg == "", AipsError);
  116. AlwaysAssert(rec.isDefined("type"), AipsError);
  117. AlwaysAssert(rec.isDefined("frequency"), AipsError);
  118. AlwaysAssert(rec.isDefined("index"), AipsError);
  119. AlwaysAssert(rec.isDefined("error"), AipsError);
  120. String type;
  121. rec.get(RecordFieldId("type"), type);
  122. AlwaysAssert(type == "Spectral Index", AipsError);
  123. Double index;
  124. rec.get(RecordFieldId("index"), index);
  125. AlwaysAssert(near(index, 1.0), AipsError);
  126. Record freqRec = rec.asRecord(RecordFieldId("frequency"));
  127. MeasureHolder mh;
  128. mh.fromRecord(errMsg, freqRec);
  129. AlwaysAssert(errMsg.length() == 0, AipsError);
  130. AlwaysAssert(mh.isMFrequency(), AipsError);
  131. mh = f1;
  132. Record newRec;
  133. newRec.define(RecordFieldId("type"), "spEctrAl IndEx");
  134. newRec.define(RecordFieldId("index"), 0.0);
  135. Record newFreq;
  136. AlwaysAssert(mh.toRecord(errMsg, newFreq), AipsError);
  137. AlwaysAssert(errMsg.length() == 0, AipsError);
  138. newRec.defineRecord(RecordFieldId("frequency"), newFreq);
  139. AlwaysAssert(siModel.fromRecord(errMsg, newRec), AipsError);
  140. AlwaysAssert(near(siModel.index(), 0.0), AipsError);
  141. AlwaysAssert(near(siModel.refFrequency().get("GHz").getValue(), 1.0),
  142. AipsError);
  143. Record emptyRec;
  144. AlwaysAssert(siModel.convertUnit(errMsg, emptyRec), AipsError);
  145. emptyRec.define(RecordFieldId("index"), "");
  146. AlwaysAssert(siModel.convertUnit(errMsg, emptyRec), AipsError);
  147. emptyRec.define(RecordFieldId("index"), "deg");
  148. AlwaysAssert(siModel.convertUnit(errMsg, emptyRec) == False, AipsError);
  149. cout << "Passed the record handling test" << endl;
  150. }
  151. delete siPtr;
  152. }
  153. catch (AipsError x) {
  154. cerr << x.getMesg() << endl;
  155. cout << "FAIL" << endl;
  156. return 1;
  157. }
  158. catch (...) {
  159. cerr << "Exception not derived from AipsError" << endl;
  160. cout << "FAIL" << endl;
  161. return 2;
  162. }
  163. cout << "OK" << endl;
  164. return 0;
  165. }
  166. // Local Variables:
  167. // compile-command: "gmake OPTLIB=1 tSpectralIndex"
  168. // End: