Ticket 720: WRWP, adjustement to be able to cope with volumes only having scans with...
[baltrad-wrwp.git] / test / pytest / WrwpTest.py
1 '''
2 Copyright (C) 2013 Swedish Meteorological and Hydrological Institute, SMHI,
3
4 This file is part of baltrad-wrwp.
5
6 baltrad-wrwp is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 baltrad-wrwp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with baltrad-wrwp.  If not, see <http://www.gnu.org/licenses/>.
18 ------------------------------------------------------------------------*/
19
20 Tests the wrwp module.
21
22 @file
23 @author Anders Henja (Swedish Meteorological and Hydrological Institute, SMHI)
24 @date 2013-09-17
25 '''
26
27 import unittest
28 import string
29 import _wrwp
30 import _helpers
31 import _raveio, _rave
32
33 class WrwpTest(unittest.TestCase):
34   FIXTURE = "fixtures/pvol_seang_20090501T120000Z.h5"
35   FIXTURE2 = "fixtures/selul_pvol_20151114T1615Z.h5"
36   FIXTURE3 = "fixtures/seang_zdr_qcvol_one_scan_and_elangle_lower_than_wrwp_threshold.h5"
37   
38   def setUp(self):
39     _helpers.triggerMemoryStatus()
40
41   def tearDown(self):
42     pass
43
44   def test_new(self):
45     obj = _wrwp.new()
46     self.assertNotEqual(-1, string.find(`type(obj)`, "WrwpCore"))
47
48   def test_dz(self):
49     obj = _wrwp.new()
50     self.assertEquals(200, obj.dz)
51     obj.dz = 100
52     self.assertEquals(100, obj.dz)
53     try:
54       obj.dz = 200.0
55       self.fail("Expected TypeError")
56     except TypeError, e:
57       pass
58     self.assertEquals(100, obj.dz)
59
60   def test_hmax(self):
61     obj = _wrwp.new()
62     self.assertEquals(12000, obj.hmax)
63     obj.hmax = 100
64     self.assertEquals(100, obj.hmax)
65     try:
66       obj.hmax = 200.0
67       self.fail("Expected TypeError")
68     except TypeError, e:
69       pass
70     self.assertEquals(100, obj.hmax)
71
72   def test_dmin(self):
73     obj = _wrwp.new()
74     self.assertEquals(4000, obj.dmin)
75     obj.dmin = 100
76     self.assertEquals(100, obj.dmin)
77     try:
78       obj.dmin = 200.0
79       self.fail("Expected TypeError")
80     except TypeError, e:
81       pass
82     self.assertEquals(100, obj.dmin)
83     
84   def test_dmax(self):
85     obj = _wrwp.new()
86     self.assertEquals(40000, obj.dmax)
87     obj.dmax = 100
88     self.assertEquals(100, obj.dmax)
89     try:
90       obj.dmax = 200.0
91       self.fail("Expected TypeError")
92     except TypeError, e:
93       pass
94     self.assertEquals(100, obj.dmax)
95
96   def test_emin(self):
97     obj = _wrwp.new()
98     self.assertAlmostEquals(2.5, obj.emin, 4)
99     obj.emin = 3.5
100     self.assertAlmostEquals(3.5, obj.emin, 4)
101     obj.emin = 4
102     self.assertAlmostEquals(4.0, obj.emin, 4)
103
104   def test_vmin(self):
105     obj = _wrwp.new()
106     self.assertAlmostEquals(2.0, obj.vmin, 4)
107     obj.vmin = 3.5
108     self.assertAlmostEquals(3.5, obj.vmin, 4)
109     obj.vmin = 4
110     self.assertAlmostEquals(4.0, obj.vmin, 4)
111   
112   def test_generate(self):
113     pvol = _raveio.open(self.FIXTURE).object
114     generator = _wrwp.new()
115     generator.hmax = 2000
116     generator.dz = 200
117     
118     vp = generator.generate(pvol, "UWND,VWND,HGHT,NV")
119     
120     uwnd = vp.getUWND()
121     vwnd = vp.getVWND()
122     hght = vp.getHGHT()
123     nv = vp.getNV()
124     
125     self.assertEquals(1, uwnd.xsize)
126     self.assertEquals(10, uwnd.ysize)
127     self.assertEquals("UWND", uwnd.getAttribute("what/quantity"))
128
129     self.assertEquals(1, vwnd.xsize)
130     self.assertEquals(10, vwnd.ysize)
131     self.assertEquals("VWND", vwnd.getAttribute("what/quantity"))
132
133     self.assertEquals(1, hght.xsize)
134     self.assertEquals(10, hght.ysize)
135     self.assertEquals("HGHT", hght.getAttribute("what/quantity"))
136     
137     self.assertEquals(1, nv.xsize)
138     self.assertEquals(10, nv.ysize)
139     self.assertEquals("n", nv.getAttribute("what/quantity"))
140
141     self.assertEquals(10,vp.getLevels())
142     self.assertEquals(200, vp.interval)
143     self.assertEquals(0, vp.minheight)
144     self.assertEquals(2000, vp.maxheight)
145     self.assertEquals(pvol.source, vp.source)
146     self.assertEquals(pvol.date, vp.date)
147     self.assertEquals(pvol.time, vp.time)
148     
149   def X_test_generate_2(self):
150     pvol = _raveio.open(self.FIXTURE).object
151     generator = _wrwp.new()
152     generator.hmax = 2000
153     generator.dz = 200
154     
155     vp = generator.generate(pvol)
156
157     robj = _raveio.new()
158     robj.object = vp
159     robj.save("slask.h5")
160
161   def test_generate_with_several_howattributes(self):
162     pvol = _raveio.open(self.FIXTURE2).object
163     generator = _wrwp.new()
164     generator.hmax = 2000
165     generator.dz = 200
166     
167     vp = generator.generate(pvol, "UWND,VWND,HGHT,NV")
168     
169     uwnd = vp.getUWND()
170     vwnd = vp.getVWND()
171     hght = vp.getHGHT()
172     nv = vp.getNV()
173
174     self.assertEquals(1, uwnd.xsize)
175     self.assertEquals(10, uwnd.ysize)
176     self.assertEquals("UWND", uwnd.getAttribute("what/quantity"))
177
178     self.assertEquals(1, vwnd.xsize)
179     self.assertEquals(10, vwnd.ysize)
180     self.assertEquals("VWND", vwnd.getAttribute("what/quantity"))
181
182     self.assertEquals(1, hght.xsize)
183     self.assertEquals(10, hght.ysize)
184     self.assertEquals("HGHT", hght.getAttribute("what/quantity"))
185
186     self.assertEquals(1, nv.xsize)
187     self.assertEquals(10, nv.ysize)
188     self.assertEquals("n", nv.getAttribute("what/quantity"))
189
190     self.assertEquals(10, vp.getLevels())
191     self.assertEquals(200, vp.interval)
192     self.assertEquals(0, vp.minheight)
193     self.assertEquals(2000, vp.maxheight)
194     self.assertEquals(pvol.source, vp.source)
195     self.assertEquals(pvol.date, vp.date)
196     self.assertEquals(pvol.time, vp.time)
197     
198     self.assertEquals(900, vp.getAttribute("how/lowprf"))
199     self.assertEquals(1200, vp.getAttribute("how/highprf"))
200     self.assertAlmostEqual(0.61, vp.getAttribute("how/pulsewidth"), 4)
201     self.assertAlmostEqual(5.35, vp.getAttribute("how/wavelength"), 4)
202     self.assertAlmostEqual(0.8, vp.getAttribute("how/RXbandwidth"), 4)
203     self.assertAlmostEqual(3.1, vp.getAttribute("how/RXlossH"), 4)
204     self.assertAlmostEqual(1.9, vp.getAttribute("how/TXlossH"), 4)
205     self.assertAlmostEqual(44.9, vp.getAttribute("how/antgainH"), 4)
206     self.assertEquals("AVERAGE", vp.getAttribute("how/azmethod"))
207     self.assertEquals("AVERAGE", vp.getAttribute("how/binmethod"))
208     self.assertEquals("False", vp.getAttribute("how/malfunc"))
209     self.assertAlmostEqual(277.4, vp.getAttribute("how/nomTXpower"), 4)
210     self.assertEquals("b94 3dd 000 000 000:", vp.getAttribute("how/radar_msg"), 4)
211     self.assertAlmostEqual(73.101, vp.getAttribute("how/radconstH"), 4)
212     self.assertAlmostEqual(0.2, vp.getAttribute("how/radomelossH"), 4)
213     self.assertAlmostEqual(2.0, vp.getAttribute("how/rpm"), 4)
214     self.assertEquals("PARTEC2", vp.getAttribute("how/software"))
215     self.assertEquals("ERIC", vp.getAttribute("how/system"))
216     self.assertEquals(4000, vp.getAttribute("how/minrange"))
217     self.assertEquals(40000, vp.getAttribute("how/maxrange"))
218
219     robj = _raveio.new()
220     robj.object = vp
221     robj.save("slask1.h5")
222
223   def test_second_generate_with_several_howattributes(self):
224     pvol = _raveio.open(self.FIXTURE3).object
225     generator = _wrwp.new()
226     generator.hmax = 12000
227     generator.dz = 200
228     generator.emin = 4.0
229
230     exceptionTest = False
231
232     try:
233       vp = generator.generate(pvol, "UWND,VWND,HGHT,NV")
234
235       uwnd = vp.getUWND()
236       vwnd = vp.getVWND()
237       hght = vp.getHGHT()
238       nv = vp.getNV()
239
240       self.assertEquals(1, uwnd.xsize)
241       self.assertEquals(60, uwnd.ysize)
242       self.assertEquals("UWND", uwnd.getAttribute("what/quantity"))
243
244       self.assertEquals(1, vwnd.xsize)
245       self.assertEquals(60, vwnd.ysize)
246       self.assertEquals("VWND", vwnd.getAttribute("what/quantity"))
247
248       self.assertEquals(1, hght.xsize)
249       self.assertEquals(60, hght.ysize)
250       self.assertEquals("HGHT", hght.getAttribute("what/quantity"))
251
252       self.assertEquals(1, nv.xsize)
253       self.assertEquals(60, nv.ysize)
254       self.assertEquals("n", nv.getAttribute("what/quantity"))
255
256       self.assertEquals(60, vp.getLevels())
257       self.assertEquals(200, vp.interval)
258       self.assertEquals(0, vp.minheight)
259       self.assertEquals(12000, vp.maxheight)
260       self.assertEquals(pvol.source, vp.source)
261       self.assertEquals(pvol.date, vp.date)
262       self.assertEquals(pvol.time, vp.time)
263     
264       self.assertEquals(450, vp.getAttribute("how/lowprf"))
265       self.assertEquals(600, vp.getAttribute("how/highprf"))
266       self.assertAlmostEqual(0.5, vp.getAttribute("how/pulsewidth"), 4)
267       self.assertAlmostEqual(5.348660945892334, vp.getAttribute("how/wavelength"), 4)
268       self.assertAlmostEqual(2.5, vp.getAttribute("how/RXbandwidth"), 4)
269       self.assertAlmostEqual(1.600000023841858, vp.getAttribute("how/RXlossH"), 4)
270       self.assertAlmostEqual(2.3000001907348633, vp.getAttribute("how/TXlossH"), 4)
271       self.assertAlmostEqual(44.290000915527344, vp.getAttribute("how/antgainH"), 4)
272       self.assertEquals("AVERAGE", vp.getAttribute("how/azmethod"))
273       self.assertEquals("AVERAGE", vp.getAttribute("how/binmethod"))
274       self.assertEquals("False", vp.getAttribute("how/malfunc"))
275       self.assertAlmostEqual(270.0, vp.getAttribute("how/nomTXpower"), 4)
276       self.assertEquals("", vp.getAttribute("how/radar_msg"), 4)
277       self.assertAlmostEqual(71.4227294921875, vp.getAttribute("how/radconstH"), 4)
278       self.assertAlmostEqual(3.0, vp.getAttribute("how/rpm"), 4)
279       self.assertEquals("EDGE", vp.getAttribute("how/software"))
280       self.assertEquals("EECDWSR-2501C-SDP", vp.getAttribute("how/system"))
281       self.assertEquals(4000, vp.getAttribute("how/minrange"))
282       self.assertEquals(40000, vp.getAttribute("how/maxrange"))
283
284       robj = _raveio.new()
285       robj.object = vp
286       robj.save("slask2.h5")
287     except:
288       exceptionTest = True
289
290     self.assertEquals(True, exceptionTest)
291
292 if __name__ == "__main__":
293   unittest.main()