PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/images/Images/test/tHDF5Image.cc

http://casacore.googlecode.com/
C++ | 346 lines | 266 code | 34 blank | 46 comment | 13 complexity | 7e3ca15ecfb764e9a71c5159fe052f8a MD5 | raw file
Possible License(s): GPL-2.0
  1. //# tHDF5Image.cc: test the HDF5Image class
  2. //# Copyright (C) 2008
  3. //# Associated Universities, Inc. Washington DC, USA.
  4. //#
  5. //# This program is free software; you can redistribute it and/or modify it
  6. //# under the terms of the GNU General Public License as published by the Free
  7. //# Software Foundation; either version 2 of the License, or(at your option)
  8. //# any later version.
  9. //#
  10. //# This program 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 General Public License for
  13. //# more details.
  14. //#
  15. //# You should have received a copy of the GNU General Public License along
  16. //# with this program; if not, write to the Free Software Foundation, Inc.,
  17. //# 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: tHDF5Image.cc 21024 2011-03-01 11:46:18Z gervandiepen $
  27. #include <images/Images/HDF5Image.h>
  28. #include <images/Images/ImageInfo.h>
  29. #include <coordinates/Coordinates/CoordinateSystem.h>
  30. #include <coordinates/Coordinates/CoordinateUtil.h>
  31. #include <lattices/Lattices/ArrayLattice.h>
  32. #include <lattices/Lattices/LatticeIterator.h>
  33. #include <casa/Arrays/Array.h>
  34. #include <casa/Arrays/ArrayIO.h>
  35. #include <casa/Arrays/ArrayMath.h>
  36. #include <casa/Arrays/ArrayLogical.h>
  37. #include <casa/Arrays/Vector.h>
  38. #include <casa/Exceptions/Error.h>
  39. #include <scimath/Functionals/Polynomial.h>
  40. #include <casa/Arrays/IPosition.h>
  41. #include <casa/Arrays/Slicer.h>
  42. #include <casa/Quanta/QLogical.h>
  43. #include <tables/Tables/TableDesc.h>
  44. #include <tables/Tables/SetupNewTab.h>
  45. #include <tables/Tables/Table.h>
  46. #include <tables/Tables/TableRecord.h>
  47. #include <casa/Utilities/Assert.h>
  48. #include <casa/Utilities/DataType.h>
  49. #include <casa/BasicSL/String.h>
  50. #include <casa/Utilities/Regex.h>
  51. #include <casa/stdlib.h>
  52. #include <casa/iostream.h>
  53. using namespace casa;
  54. // Remove the dirname from the file name in an error message.
  55. String removeDir (const String& msg)
  56. {
  57. String s = msg;
  58. s.gsub (Regex("/.*/t"), "t");
  59. return s;
  60. }
  61. Float const_arg_func(const Float& val)
  62. {
  63. return 3.0*val;
  64. }
  65. Float func(Float val)
  66. {
  67. return 2.0*val*val;
  68. }
  69. int main()
  70. {
  71. // Exit with untested if no HDF5 support.
  72. if (! HDF5Object::hasHDF5Support()) {
  73. return 3;
  74. }
  75. try {
  76. // Build things to make a HDF5Image
  77. CoordinateSystem cSys = CoordinateUtil::defaultCoords2D();
  78. IPosition shape(2,32,64);
  79. TiledShape tiledShape(shape);
  80. // Test constructors
  81. {
  82. HDF5Image<Float> pIm(tiledShape, cSys, "tHDF5Image_tmp.img1");
  83. }
  84. AlwaysAssertExit (isHDF5Image("tHDF5Image_tmp.img1"));
  85. AlwaysAssertExit (hdf5imagePixelType("tHDF5Image_tmp.img1") == TpFloat);
  86. {
  87. HDF5Image<Float> pIm2("tHDF5Image_tmp.img1");
  88. }
  89. // Test copy constructor. This is by reference so test that
  90. // this is so
  91. {
  92. HDF5Image<Float> pIm(String("tHDF5Image_tmp.img1"));
  93. HDF5Image<Float> pIm2(pIm);
  94. pIm.putAt(Float(1.0), IPosition(2,0,0));
  95. AlwaysAssert( (pIm(IPosition(2,0,0))==Float(1.0)), AipsError);
  96. pIm2.putAt(Float(10.0), IPosition(2,0,0));
  97. AlwaysAssert((pIm2(IPosition(2,0,0))==Float(10.0)), AipsError);
  98. AlwaysAssert((pIm(IPosition(2,0,0))==Float(10.0)), AipsError);
  99. AlwaysAssert(pIm(IPosition(2,0,0))==pIm.getAt(IPosition(2,0,0)),
  100. AipsError);
  101. }
  102. // Test assignment. This is by reference so test that
  103. // this is so
  104. {
  105. HDF5Image<Float> pIm(String("tHDF5Image_tmp.img1"));
  106. HDF5Image<Float> pIm2(pIm);
  107. pIm2 = pIm;
  108. pIm.putAt(Float(1.0), IPosition(2,0,0));
  109. AlwaysAssert( (pIm(IPosition(2,0,0))==Float(1.0)), AipsError);
  110. pIm2.putAt(Float(10.0), IPosition(2,0,0));
  111. AlwaysAssert((pIm2.getAt(IPosition(2,0,0))==Float(10.0)), AipsError);
  112. AlwaysAssert((pIm.getAt(IPosition(2,0,0))==Float(10.0)), AipsError);
  113. AlwaysAssert(pIm(IPosition(2,0,0))==pIm.getAt(IPosition(2,0,0)),
  114. AipsError);
  115. }
  116. // Test clones. There is not much we can do to make sure they are ok !
  117. // They are by reference too.
  118. {
  119. HDF5Image<Float> pIm(String("tHDF5Image_tmp.img1"));
  120. pIm.putAt(Float(1.0), IPosition(2,0,0));
  121. AlwaysAssert( (pIm(IPosition(2,0,0))==Float(1.0)), AipsError);
  122. Lattice<Float>* lat = pIm.clone();
  123. AlwaysAssert(pIm.shape()==lat->shape(), AipsError);
  124. lat->putAt(Float(10.0), IPosition(2,0,0));
  125. AlwaysAssert(((*lat).getAt(IPosition(2,0,0))==Float(10.0)), AipsError);
  126. AlwaysAssert((pIm.getAt(IPosition(2,0,0))==Float(10.0)), AipsError);
  127. delete lat;
  128. }
  129. {
  130. HDF5Image<Float> pIm(String("tHDF5Image_tmp.img1"));
  131. pIm.putAt(Float(1.0), IPosition(2,0,0));
  132. AlwaysAssert( (pIm.getAt(IPosition(2,0,0))==Float(1.0)), AipsError);
  133. Lattice<Float>* ii = pIm.cloneII();
  134. AlwaysAssert(pIm.shape()==ii->shape(), AipsError);
  135. ii->putAt(Float(10.0), IPosition(2,0,0));
  136. AlwaysAssert(((*ii).getAt(IPosition(2,0,0))==Float(10.0)), AipsError);
  137. AlwaysAssert((pIm.getAt(IPosition(2,0,0))==Float(10.0)), AipsError);
  138. delete ii;
  139. }
  140. // Test some miscellaneous little things
  141. {
  142. HDF5Image<Float> pIm(String("tHDF5Image_tmp.img1"));
  143. AlwaysAssert(hdf5imagePixelType(String("tHDF5Image_tmp.img1"))==TpFloat,
  144. AipsError);
  145. AlwaysAssert(pIm.name(True)==String("tHDF5Image_tmp.img1"),
  146. AipsError);
  147. cout << "Absolute name = " << pIm.name(False) << endl;
  148. AlwaysAssert(pIm.isPaged(), AipsError);
  149. AlwaysAssert(pIm.isWritable(), AipsError);
  150. AlwaysAssert(pIm.ok(), AipsError);
  151. AlwaysAssert(pIm.shape()==shape, AipsError);
  152. IPosition niceCursorShape = pIm.niceCursorShape();
  153. // Only true for small images
  154. AlwaysAssert(niceCursorShape==shape, AipsError);
  155. Unit units("Jy");
  156. pIm.setUnits(units);
  157. AlwaysAssert(pIm.units().getName()=="Jy", AipsError);
  158. Record rec;
  159. rec.define("x", Double(1.0));
  160. rec.define("y", Double(2.0));
  161. pIm.setMiscInfo(rec);
  162. TableRecord rec2 = pIm.miscInfo();
  163. AlwaysAssert(rec2.nfields()==2, AipsError);
  164. AlwaysAssert(rec2.isDefined("x"), AipsError);
  165. AlwaysAssert(rec2.isDefined("y"), AipsError);
  166. AlwaysAssert(rec2.dataType("x")==TpDouble, AipsError);
  167. AlwaysAssert(rec2.dataType("y")==TpDouble, AipsError);
  168. AlwaysAssert(rec2.asDouble("x")==Double(1.0), AipsError);
  169. AlwaysAssert(rec2.asDouble("y")==Double(2.0), AipsError);
  170. CoordinateSystem cSys2 = pIm.coordinates();
  171. Vector<String> axisUnits = cSys2.worldAxisUnits();
  172. axisUnits(0) = "deg"; axisUnits(1) = "deg";
  173. cSys2.setWorldAxisUnits(axisUnits);
  174. pIm.setCoordinateInfo(cSys2);
  175. CoordinateSystem cSys3 = pIm.coordinates();
  176. AlwaysAssert(cSys2.near(cSys3,1e-6), AipsError);
  177. ImageInfo info = pIm.imageInfo();
  178. AlwaysAssert(info.restoringBeam().nelements()==0, AipsError);
  179. Quantum<Double> a1(10.0,Unit("arcsec"));
  180. Quantum<Double> a2(8.0,Unit("arcsec"));
  181. Quantum<Double> a3(-45.0,Unit("deg"));
  182. info.setRestoringBeam(a1, a2, a3);
  183. pIm.setImageInfo(info);
  184. info = pIm.imageInfo();
  185. AlwaysAssert(info.restoringBeam()(0)==a1, AipsError);
  186. AlwaysAssert(info.restoringBeam()(1)==a2, AipsError);
  187. AlwaysAssert(info.restoringBeam()(2)==a3, AipsError);
  188. }
  189. // do{Put,Get}Slice tests
  190. {
  191. IPosition shape2(2,5, 10);
  192. TiledShape tiledShape2(shape2);
  193. CoordinateSystem cSys2 = CoordinateUtil::defaultCoords2D();
  194. HDF5Image<Float> pIm(tiledShape2, cSys2, "tHDF5Image_tmp.img7");
  195. // Fill (in slow way, so use small image)
  196. IPosition pos(2);
  197. for (Int i=0; i<shape2(0); i++) {
  198. for (Int j=0; j<shape2(1); j++) {
  199. pos(0) = i;
  200. pos(1) = j;
  201. pIm.putAt(Float(i*j), pos);
  202. }
  203. }
  204. Slicer slice(IPosition(2,0,0), shape2, IPosition(2,1,1));
  205. Array<Float> data;
  206. pIm.doGetSlice(data, slice);
  207. AlwaysAssert(data.shape()==shape2, AipsError);
  208. for (Int i=0; i<shape2(0); i++) {
  209. for (Int j=0; j<shape2(1); j++) {
  210. pos(0) = i;
  211. pos(1) = j;
  212. AlwaysAssert(data(pos)==pIm.getAt(pos), AipsError);
  213. }
  214. }
  215. data*Float(20.0);
  216. pIm.doPutSlice(data, IPosition(2,0,0), IPosition(2,1,1));
  217. for (Int i=0; i<shape2(0); i++) {
  218. for (Int j=0; j<shape2(1); j++) {
  219. pos(0) = i;
  220. pos(1) = j;
  221. AlwaysAssert(data(pos)==pIm.getAt(pos), AipsError);
  222. }
  223. }
  224. Array<Float> data2(data.copy());
  225. data.resize(IPosition(2,2,2));
  226. data.set(0.0);
  227. pIm.doPutSlice(data, IPosition(2,0,0), IPosition(2,1,1));
  228. pos(0) = 0; pos(1) = 0;
  229. AlwaysAssert(pIm(IPosition(2,0,0))==Float(0.0), AipsError);
  230. AlwaysAssert(pIm(IPosition(2,0,1))==Float(0.0), AipsError);
  231. AlwaysAssert(pIm(IPosition(2,1,0))==Float(0.0), AipsError);
  232. AlwaysAssert(pIm(IPosition(2,1,1))==Float(0.0), AipsError);
  233. for (Int i=2; i<shape2(0); i++) {
  234. for (Int j=2; j<shape2(1); j++) {
  235. pos(0) = i;
  236. pos(1) = j;
  237. AlwaysAssert(data2(pos)==pIm.getAt(pos), AipsError);
  238. }
  239. }
  240. }
  241. // Silly Lattice addition operator
  242. {
  243. IPosition shape2(2,5, 10);
  244. TiledShape tiledShape2(shape2);
  245. CoordinateSystem cSys2 = CoordinateUtil::defaultCoords2D();
  246. HDF5Image<Float> pIm(tiledShape2, cSys2, "tHDF5Image_tmp.img8");
  247. IPosition pos(2);
  248. Array<Float> data(shape2);
  249. for (Int i=0; i<shape2(0); i++) {
  250. for (Int j=0; j<shape2(1); j++) {
  251. pos(0) = i;
  252. pos(1) = j;
  253. data(pos) = Float(i*j);
  254. }
  255. }
  256. pIm.doPutSlice(data, IPosition(2,0,0), IPosition(2,1,1));
  257. ArrayLattice<Float> lat(shape2);
  258. lat.set(Float(10.0));
  259. pIm += lat;
  260. Array<Float> data2(data.copy());
  261. data2 += Float(10.0);
  262. Slicer slice(IPosition(2,0,0), shape2, IPosition(2,1,1));
  263. Array<Float> data3;
  264. pIm.doGetSlice(data3, slice);
  265. AlwaysAssert(allNear(data3, data2, 1e-6), AipsError);
  266. }
  267. // apply functions
  268. {
  269. IPosition shape2(2,5, 10);
  270. TiledShape tiledShape2(shape2);
  271. CoordinateSystem cSys2 = CoordinateUtil::defaultCoords2D();
  272. HDF5Image<Float> pIm(tiledShape2, cSys2, "tHDF5Image_tmp.img9");
  273. pIm.set(3.0);
  274. AlwaysAssert(allEQ(pIm.get(), Float(3.0)), AipsError);
  275. // 2 * x * x
  276. pIm.apply(&func);
  277. AlwaysAssert(allNear(pIm.get(), Float(18.0), Double(1e-6)), AipsError);
  278. // 3 * x (const arg)
  279. pIm.set(3.0);
  280. AlwaysAssert(allEQ(pIm.get(), Float(3.0)), AipsError);
  281. pIm.apply(&const_arg_func);
  282. AlwaysAssert(allNear(pIm.get(), Float(9.0), Double(1e-6)), AipsError);
  283. // Polynomial 1 + 2x + 3x**2
  284. pIm.set(3.0);
  285. AlwaysAssert(allEQ(pIm.get(), Float(3.0)), AipsError);
  286. Polynomial<Float> poly(3);
  287. poly.setCoefficient(1, 1.0);
  288. poly.setCoefficient(2, 2.0);
  289. poly.setCoefficient(3, 3.0);
  290. pIm.apply(poly);
  291. AlwaysAssert(allNear(pIm.get(), poly(3.0), Double(1e-6)), AipsError);
  292. }
  293. // Do some iterating to test the makeIter function (indirectly)
  294. {
  295. IPosition shape2(2, 128, 256);
  296. TiledShape tiledShape2(shape2);
  297. CoordinateSystem cSys2 = CoordinateUtil::defaultCoords2D();
  298. HDF5Image<Float> pIm(tiledShape2, cSys2, "tHDF5Image_tmp.img10");
  299. pIm.set(1.0);
  300. LatticeIterator<Float> it(pIm);
  301. while (!it.atEnd()) {
  302. AlwaysAssert(allEQ(it.cursor(), Float(1.0)), AipsError);
  303. it++;
  304. }
  305. }
  306. cout<< "ok"<< endl;
  307. } catch (AipsError x) {
  308. cerr << "Exception caught: " << x.getMesg() << endl;
  309. return 1;
  310. }
  311. return 0;
  312. }