source: branches/numpy/anuga/coordinate_transforms/test_geo_reference.py @ 7244

Last change on this file since 7244 was 7061, checked in by rwilson, 16 years ago

Made geo_ref attributes be forced to python types, not numpy, added test case.

File size: 26.7 KB
Line 
1#!/usr/bin/env python
2#
3
4import unittest
5import tempfile
6import os
7
8from geo_reference import *
9from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
10
11import numpy as num
12
13
14class geo_referenceTestCase(unittest.TestCase):
15    def setUp(self):
16        pass
17       
18    def tearDown(self):
19        pass
20
21   
22   
23    def test_get_origin(self):
24        g = Geo_reference(56,1.9,1.9)
25        (z,x,y) = g.get_origin()
26
27        self.failUnless(z == g.get_zone(), ' failed')
28        self.failUnless(x == g.get_xllcorner(), ' failed')
29        self.failUnless(y == g.get_yllcorner(), ' failed') 
30       
31    def test_read_write_NetCDF(self):
32        from Scientific.IO.NetCDF import NetCDFFile
33        g = Geo_reference(56,1.9,1.9)
34        file_name = tempfile.mktemp(".geo_referenceTest")
35       
36        out_file = NetCDFFile(file_name, netcdf_mode_w)
37        g.write_NetCDF(out_file)
38        out_file.close()
39       
40        in_file = NetCDFFile(file_name, netcdf_mode_r)
41        new_g = Geo_reference(NetCDFObject=in_file)
42        in_file.close()
43        os.remove(file_name)
44
45        self.failUnless(g == new_g, 'test_read_write_NetCDF failed') 
46       
47    def test_read_NetCDFI(self):
48        # test if read_NetCDF
49        from Scientific.IO.NetCDF import NetCDFFile
50        g = Geo_reference(56,1.9,1.9)
51        file_name = tempfile.mktemp(".geo_referenceTest")
52       
53        outfile = NetCDFFile(file_name, netcdf_mode_w)
54        outfile.xllcorner = g.get_xllcorner() 
55        outfile.yllcorner =  g.get_yllcorner() 
56        outfile.zone = g.get_zone()
57        outfile.close()
58       
59        in_file = NetCDFFile(file_name, netcdf_mode_r)
60        new_g = Geo_reference(NetCDFObject=in_file)
61        in_file.close()
62        os.remove(file_name)
63
64        self.failUnless(g == new_g, ' failed')
65       
66    def test_read_write_ASCII(self):
67        from Scientific.IO.NetCDF import NetCDFFile
68        g = Geo_reference(56,1.9,1.9)
69        file_name = tempfile.mktemp(".geo_referenceTest")
70        fd = open(file_name,'w')
71        g.write_ASCII(fd)
72        fd.close()
73       
74        fd = open(file_name,'r')
75        new_g = Geo_reference(ASCIIFile=fd)
76        fd.close()
77        os.remove(file_name)
78
79        self.failUnless(g == new_g, 'test_read_write_ASCII failed') 
80   
81    def test_read_write_ASCII2(self):
82        from Scientific.IO.NetCDF import NetCDFFile
83        g = Geo_reference(56,1.9,1.9)
84        file_name = tempfile.mktemp(".geo_referenceTest")
85        fd = open(file_name,'w')
86        g.write_ASCII(fd)
87        fd.close()       
88        fd = open(file_name,'r')
89        line = fd.readline()
90        new_g = Geo_reference(ASCIIFile=fd, read_title=line)
91        fd.close()
92        os.remove(file_name)
93
94        self.failUnless(g == new_g, 'test_read_write_ASCII failed')
95       
96    def test_read_write_ASCII3(self):
97        from Scientific.IO.NetCDF import NetCDFFile
98        g = Geo_reference(56,1.9,1.9)
99        file_name = tempfile.mktemp(".geo_referenceTest")
100        fd = open(file_name,'w')
101        g.write_ASCII(fd)
102        fd.close()       
103        fd = open(file_name,'r')
104        line = fd.readline()
105        line = "fail !!"
106        try:
107            new_g = Geo_reference(ASCIIFile=fd, read_title=line)
108            fd.close()
109            os.remove(file_name)
110        except TitleError:
111            fd.close()
112            os.remove(file_name)
113        else:
114            self.failUnless(0 ==1,
115                        'bad text file did not raise error!')
116           
117    def test_change_points_geo_ref(self):
118        x = 433.0
119        y = 3.0
120        g = Geo_reference(56,x,y)
121        lofl = [[3.0,311.0], [677.0,6.0]]
122        new_lofl = g.change_points_geo_ref(lofl)
123        #print "lofl",lofl
124        #print "new_lofl",new_lofl
125
126        self.failUnless(type(new_lofl) == types.ListType, ' failed')
127        self.failUnless(type(new_lofl) == type(lofl), ' failed')
128        for point,new_point in map(None,lofl,new_lofl):
129            self.failUnless(point[0]-x==new_point[0], ' failed')
130            self.failUnless(point[1]-y==new_point[1], ' failed')
131         
132       
133    def test_change_points_geo_ref2(self):
134        x = 3.0
135        y = 543.0
136        g = Geo_reference(56,x,y)
137        lofl = [[3.0,388.0]]
138        new_lofl = g.change_points_geo_ref(lofl)
139        #print "lofl",lofl
140        #print "new_lofl",new_lofl
141
142        self.failUnless(type(new_lofl) == types.ListType, ' failed')
143        self.failUnless(type(new_lofl) == type(lofl), ' failed')
144        for point,new_point in map(None,lofl,new_lofl):
145            self.failUnless(point[0]-x==new_point[0], ' failed')
146            self.failUnless(point[1]-y==new_point[1], ' failed')
147       
148    def test_change_points_geo_ref3(self):
149        x = 3.0
150        y = 443.0
151        g = Geo_reference(56,x,y)
152        lofl = [3.0,345.0]
153        new_lofl = g.change_points_geo_ref(lofl)
154        #print "lofl",lofl
155        #print "new_lofl",new_lofl
156
157        self.failUnless(type(new_lofl) == types.ListType, ' failed')
158        self.failUnless(type(new_lofl) == type(lofl), ' failed')
159        for point,new_point in map(None,[lofl],new_lofl):
160            self.failUnless(point[0]-x==new_point[0], ' failed')
161            self.failUnless(point[1]-y==new_point[1], ' failed')
162       
163   
164    def test_change_points_geo_ref4(self):
165        x = 3.0
166        y = 443.0
167        g = Geo_reference(56,x,y)
168        lofl = num.array([[3.0,323.0], [6.0,645.0]])
169        new_lofl = g.change_points_geo_ref(lofl)
170        #print "4 lofl",lofl
171        #print "4 new_lofl",new_lofl
172
173        self.failUnless(isinstance(new_lofl, num.ndarray), ' failed')
174        self.failUnless(type(new_lofl) == type(lofl), ' failed')
175        lofl[:,0] -= x
176        lofl[:,1] -= y
177        assert num.allclose(lofl,new_lofl)
178       
179    def test_change_points_geo_ref5(self):
180        x = 103.0
181        y = 3.0
182        g = Geo_reference(56,x,y)
183        lofl = num.array([[3.0,323.0]])
184
185       
186        #print "5 lofl before",lofl         
187        new_lofl = g.change_points_geo_ref(lofl.copy())
188        #print "5 lofl",lofl
189        #print "5 new_lofl",new_lofl
190
191        self.failUnless(isinstance(new_lofl, num.ndarray), ' failed')
192        self.failUnless(type(new_lofl) == type(lofl), ' failed')
193
194
195        for point,new_point in map(None,lofl,new_lofl):
196            self.failUnless(point[0]-x==new_point[0], ' failed')
197            self.failUnless(point[1]-y==new_point[1], ' failed')
198       
199    def test_change_points_geo_ref6(self):
200        x = 53.0
201        y = 3.0
202        g = Geo_reference(56,x,y)
203        lofl = num.array([355.0,3.0])
204        new_lofl = g.change_points_geo_ref(lofl.copy())       
205        #print "lofl",lofl
206        #print "new_lofl",new_lofl
207
208        self.failUnless(isinstance(new_lofl, num.ndarray), ' failed')
209        self.failUnless(type(new_lofl) == type(lofl), ' failed')
210        for point,new_point in map(None,[lofl],new_lofl):
211            self.failUnless(point[0]-x==new_point[0], ' failed')
212            self.failUnless(point[1]-y==new_point[1], ' failed')
213     
214    def test_change_points_geo_ref7(self):
215        x = 23.0
216        y = 3.0
217        point_x = 9.0
218        point_y = -60.0
219        g = Geo_reference(56,x,y)
220        points_geo_ref = Geo_reference(56,point_x,point_y)
221        lofl = [[3.0,30.0], [67.0,6.0]]
222        new_lofl = g.change_points_geo_ref(lofl,points_geo_ref=points_geo_ref)
223        #print "lofl",lofl
224        #print "new_lofl",new_lofl
225
226        self.failUnless(type(new_lofl) == types.ListType, ' failed')
227        self.failUnless(type(new_lofl) == type(lofl), ' failed')
228        for point,new_point in map(None,lofl,new_lofl):
229            self.failUnless(point[0]+point_x-x==new_point[0], ' failed')
230            self.failUnless(point[1]+point_y-y==new_point[1], ' failed')
231
232    def test_get_absolute_list(self):
233        # test with supplied offsets
234        x = 7.0
235        y = 3.0
236       
237        g = Geo_reference(56, x, y)
238        points = [[3.0,34.0], [64.0,6.0]]
239        new_points = g.get_absolute(points)
240
241        self.failUnless(isinstance(new_points, list), 'failed')
242        self.failUnless(type(new_points) == type(points), 'failed')
243        for point, new_point in map(None, points, new_points):
244            self.failUnless(point[0]+x == new_point[0], 'failed')
245            self.failUnless(point[1]+y == new_point[1], 'failed')
246
247        # test with no supplied offsets
248        g = Geo_reference()
249        points = [[3.0,34.0], [64.0,6.0]]
250        new_points = g.get_absolute(points)
251
252        self.failUnless(isinstance(new_points, list), 'failed')
253        self.failUnless(type(new_points) == type(points), 'failed')
254        for point, new_point in map(None, points, new_points):
255            self.failUnless(point[0] == new_point[0], 'failed')
256            self.failUnless(point[1] == new_point[1], 'failed')
257           
258        # test that calling get_absolute twice does the right thing
259        # first call
260        dx = 10.0
261        dy = 12.0
262        g = Geo_reference(56, dx, dy)
263        points = [[3.0,34.0], [64.0,6.0]]
264        expected_new_points = [[3.0+dx,34.0+dy], [64.0+dx,6.0+dy]]
265        new_points = g.get_absolute(points)
266
267        self.failUnless(isinstance(new_points, list), 'failed')
268        self.failUnless(type(new_points) == type(points), 'failed')
269        self.failUnless(new_points == expected_new_points, 'failed')
270
271        # and repeat from 'new_points = g.get_absolute(points)' above
272        # to see if second call with same input gives same results.
273        new_points = g.get_absolute(points)
274
275        self.failUnless(isinstance(new_points, list), 'failed')
276        self.failUnless(type(new_points) == type(points), 'failed')
277        self.failUnless(new_points == expected_new_points, 'failed')
278
279    def test_get_absolute_array(self):
280        '''Same test as test_get_absolute_list(), but with numeric arrays.'''
281
282        # test with supplied offsets
283        x = 7.0
284        y = 3.0
285       
286        g = Geo_reference(56, x, y)
287        points = num.array([[3.0,34.0], [64.0,6.0]])
288        new_points = g.get_absolute(points)
289
290        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
291        self.failUnless(type(new_points) == type(points), 'failed')
292        msg = 'points=\n%s\nnew_points=\n%s' % (str(points), str(new_points))
293        for point, new_point in map(None, points, new_points):
294            self.failUnless(point[0]+x == new_point[0], msg)
295            self.failUnless(point[1]+y == new_point[1], msg)
296
297        # test with no supplied offsets
298        g = Geo_reference()
299        points = num.array([[3.0,34.0], [64.0,6.0]])
300        new_points = g.get_absolute(points)
301
302        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
303        self.failUnless(type(new_points) == type(points), 'failed')
304        self.failUnless(num.alltrue(points == new_points), 'failed')
305
306        # test that calling get_absolute twice does the right thing
307        # first call
308        dx = 11.0
309        dy = 13.0
310        g = Geo_reference(56, dx, dy)
311        points = num.array([[3.0,34.0], [64.0,6.0]])
312        expected_new_points = num.array([[3.0+dx,34.0+dy], [64.0+dx,6.0+dy]])
313        new_points = g.get_absolute(points)
314
315        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
316        self.failUnless(type(new_points) == type(points), 'failed')
317        msg = ('First call of .get_absolute() returned %s\nexpected %s'
318               % (str(new_points), str(expected_new_points)))
319        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
320
321        # and repeat from 'new_points = g.get_absolute(points)' above
322        # to see if second call with same input gives same results.
323        new_points = g.get_absolute(points)
324
325        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
326        self.failUnless(type(new_points) == type(points), 'failed')
327        msg = ('Second call of .get_absolute() returned\n%s\nexpected\n%s'
328               % (str(new_points), str(expected_new_points)))
329        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
330
331        # and repeat again to see if *third* call with same input
332        # gives same results.
333        new_points = g.get_absolute(points)
334
335        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
336        self.failUnless(type(new_points) == type(points), 'failed')
337        msg = ('Third call of .get_absolute() returned %s\nexpected %s'
338               % (str(new_points), str(expected_new_points)))
339        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
340
341    def test_get_relative_list(self):
342        # test with supplied offsets
343        x = 7.0
344        y = 3.0
345       
346        g = Geo_reference(56, x, y)
347        points = [[3.0,34.0], [64.0,6.0]]
348        new_points = g.get_relative(points)
349
350        self.failUnless(isinstance(new_points, list), 'failed')
351        self.failUnless(type(new_points) == type(points), 'failed')
352        for point, new_point in map(None, points, new_points):
353            self.failUnless(point[0]-x == new_point[0], 'failed')
354            self.failUnless(point[1]-y == new_point[1], 'failed')
355
356        # test with no supplied offsets
357        g = Geo_reference()
358        points = [[3.0,34.0], [64.0,6.0]]
359        new_points = g.get_relative(points)
360
361        self.failUnless(isinstance(new_points, list), 'failed')
362        self.failUnless(type(new_points) == type(points), 'failed')
363        for point, new_point in map(None, points, new_points):
364            self.failUnless(point[0] == new_point[0], 'failed')
365            self.failUnless(point[1] == new_point[1], 'failed')
366           
367        # test that calling get_absolute twice does the right thing
368        # first call
369        dx = 10.0
370        dy = 12.0
371        g = Geo_reference(56, dx, dy)
372        points = [[3.0,34.0], [64.0,6.0]]
373        expected_new_points = [[3.0-dx,34.0-dy], [64.0-dx,6.0-dy]]
374        new_points = g.get_relative(points)
375
376        self.failUnless(isinstance(new_points, list), 'failed')
377        self.failUnless(type(new_points) == type(points), 'failed')
378        self.failUnless(new_points == expected_new_points, 'failed')
379
380        # and repeat from 'new_points = g.get_absolute(points)' above
381        # to see if second call with same input gives same results.
382        new_points = g.get_relative(points)
383
384        self.failUnless(isinstance(new_points, list), 'failed')
385        self.failUnless(type(new_points) == type(points), 'failed')
386        self.failUnless(new_points == expected_new_points, 'failed')
387
388    def test_get_relative_array(self):
389        '''Same test as test_get_relative_list(), but with numeric arrays.'''
390
391        # test with supplied offsets
392        x = 7.0
393        y = 3.0
394       
395        g = Geo_reference(56, x, y)
396        points = num.array([[3.0,34.0], [64.0,6.0]])
397        new_points = g.get_relative(points)
398
399        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
400        self.failUnless(type(new_points) == type(points), 'failed')
401        msg = 'points=\n%s\nnew_points=\n%s' % (str(points), str(new_points))
402        for point, new_point in map(None, points, new_points):
403            self.failUnless(point[0]-x == new_point[0], msg)
404            self.failUnless(point[1]-y == new_point[1], msg)
405
406        # test with no supplied offsets
407        g = Geo_reference()
408        points = num.array([[3.0,34.0], [64.0,6.0]])
409        new_points = g.get_relative(points)
410
411        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
412        self.failUnless(type(new_points) == type(points), 'failed')
413        self.failUnless(num.alltrue(points == new_points), 'failed')
414
415        # test that calling get_relative twice does the right thing
416        # first call
417        dx = 11.0
418        dy = 13.0
419        g = Geo_reference(56, dx, dy)
420        points = num.array([[3.0,34.0], [64.0,6.0]])
421        expected_new_points = num.array([[3.0-dx,34.0-dy], [64.0-dx,6.0-dy]])
422        new_points = g.get_relative(points)
423
424        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
425        self.failUnless(type(new_points) == type(points), 'failed')
426        msg = ('First call of .get_relative() returned %s\nexpected %s'
427               % (str(new_points), str(expected_new_points)))
428        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
429
430        # and repeat from 'new_points = g.get_relative(points)' above
431        # to see if second call with same input gives same results.
432        new_points = g.get_relative(points)
433
434        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
435        self.failUnless(type(new_points) == type(points), 'failed')
436        msg = ('Second call of .get_relative() returned\n%s\nexpected\n%s'
437               % (str(new_points), str(expected_new_points)))
438        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
439
440        # and repeat again to see if *third* call with same input
441        # gives same results.
442        new_points = g.get_relative(points)
443
444        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
445        self.failUnless(type(new_points) == type(points), 'failed')
446        msg = ('Third call of .get_relative() returned %s\nexpected %s'
447               % (str(new_points), str(expected_new_points)))
448        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
449
450    def test_is_absolute(self):
451       
452        g = Geo_reference(34,0,0)
453        points = [[3.0,34.0], [64.0,6.0]]
454
455        assert g.is_absolute()
456
457        g = Geo_reference(34,7,-6)
458        assert not g.is_absolute()       
459
460                       
461    def test___cmp__(self):
462        g = Geo_reference(56,1.9,1.9,)
463        new_g = Geo_reference(56,1.9,1.9)
464     
465        self.failUnless(g == new_g, 'test___cmp__ failed')   
466
467
468    def test_reconcile(self):
469        g1 = Geo_reference(56,2,5)
470        g2 = Geo_reference(50,4,5)
471        g3 = Geo_reference(50,66,6)       
472        g_default = Geo_reference()               
473
474
475        g2.reconcile_zones(g3)
476        assert g2.get_zone() == g3.get_zone()
477
478        g_default.reconcile_zones(g3)
479        assert g_default.get_zone() == g3.get_zone()
480
481        g_default = Geo_reference()               
482        g3.reconcile_zones(g_default)
483        assert g_default.get_zone() == g3.get_zone()               
484
485        try:
486            g1.reconcile_zones(g2)
487        except:
488            pass
489        else:
490            msg = 'Should have raised an exception'
491            raise msg
492 
493    def test_bad_ASCII_title(self):     
494 # create an text file
495        point_file = tempfile.mktemp(".xxx")
496        fd = open(point_file,'w')
497        fd.write("# hey! \n")
498        fd.close()
499       
500        fd = open(point_file,'r')
501        #
502        #new_g = Geo_reference(ASCIIFile=fd)
503        try:
504            new_g = Geo_reference(ASCIIFile=fd)
505            fd.close()
506            os.remove(point_file)
507        except TitleError:
508            fd.close()
509            os.remove(point_file)
510        else:
511            self.failUnless(0 ==1,
512                        'bad text file did not raise error!')
513            os.remove(point_file)
514
515    def test_read_write_ASCII_test_and_fail(self):
516        from Scientific.IO.NetCDF import NetCDFFile
517
518        # This is to test a fail
519        g = Geo_reference(56,1.9,1.9)
520        file_name = tempfile.mktemp(".geo_referenceTest")
521        fd = open(file_name,'w')
522        g.write_ASCII(fd)
523        fd.close()       
524        fd = open(file_name,'r')
525        line = fd.readline()
526        line = " #Geo"
527        try:
528            new_g = Geo_reference(ASCIIFile=fd, read_title=line)
529            fd.close()
530            os.remove(file_name)
531        except TitleError:
532            fd.close()
533            os.remove(file_name)
534        else:
535            self.failUnless(0 ==1,
536                        'bad text file did not raise error!')
537
538        # this tests a pass
539        g = Geo_reference(56,1.9,1.9)
540        file_name = tempfile.mktemp(".geo_referenceTest")
541        fd = open(file_name,'w')
542        g.write_ASCII(fd)
543        fd.close()
544       
545        fd = open(file_name,'r')
546        line = fd.readline()
547        line = "#geo_yeah"
548        new_g = Geo_reference(ASCIIFile=fd, read_title=line)
549        fd.close()
550        os.remove(file_name)
551
552        self.failUnless(g == new_g, 'test_read_write_ASCII failed')
553       
554        # this tests a pass
555        g = Geo_reference(56,1.9,1.9)
556        file_name = tempfile.mktemp(".geo_referenceTest")
557        fd = open(file_name,'w')
558        g.write_ASCII(fd)
559        fd.close()
560       
561        fd = open(file_name,'r')
562        line = fd.readline()
563        line = "#geo crap"
564        new_g = Geo_reference(ASCIIFile=fd, read_title=line)
565        fd.close()
566        os.remove(file_name)
567
568        self.failUnless(g == new_g, 'test_read_write_ASCII failed')
569       
570    def test_good_title(self):     
571 # create an .xxx file
572        point_file = tempfile.mktemp(".xxx")
573        fd = open(point_file,'w')
574        fd.write("#Geo crap \n 56\n ")
575        fd.close()
576       
577        fd = open(point_file,'r')
578        #
579        #new_g = Geo_reference(ASCIIFile=fd)
580        try:
581            new_g = Geo_reference(ASCIIFile=fd)
582            fd.close()
583            os.remove(point_file)
584        except ValueError:
585            fd.close()
586            os.remove(point_file)
587        else:
588            self.failUnless(0 ==1,
589                        'bad text file did not raise error!')
590            os.remove(point_file)
591
592    def test_error_message_ShapeError(self):
593       
594        new_g = Geo_reference()
595        try:
596            new_g.get_absolute((8.9, 7.8, 9.0)) 
597        except ShapeError:
598            pass
599        else:
600            self.failUnless(0 ==1,
601                        'bad shape did not raise error!')
602            os.remove(point_file)
603           
604        new_g = Geo_reference()
605        try:
606            new_g.get_absolute(((8.9, 7.8, 9.0))) 
607        except ShapeError:
608            pass
609        else:
610            self.failUnless(0 ==1,
611                        'bad shape did not raise error!')
612            os.remove(point_file)
613
614    def test_functionality_get_absolute(self):
615        x0 = 1000.0
616        y0 = 2000.0
617        geo = Geo_reference(56, x0, y0)
618
619        # iterable points (*not* num.array())
620        points = ((2,3), (3,1), (5,2))
621        abs_points = geo.get_absolute(points)
622        # check we haven't changed 'points' itself
623        self.failIf(num.alltrue(abs_points == points))
624        new_points = abs_points.copy()
625        new_points[:,0] -= x0
626        new_points[:,1] -= y0
627        self.failUnless(num.alltrue(new_points == points))
628
629        # points in num.array()
630        points = num.array(((2,3), (3,1), (5,2)), num.float)
631        abs_points = geo.get_absolute(points)
632        # check we haven't changed 'points' itself
633        self.failIf(num.alltrue(abs_points == points))
634        new_points = abs_points.copy()
635        new_points[:,0] -= x0
636        new_points[:,1] -= y0
637        self.failUnless(num.alltrue(new_points == points))
638
639    def test_georef_types(self):
640        '''Ensure that attributes of a georeference are of correct type.
641       
642        zone            int
643        false_easting   int
644        false_northing  int
645        xllcorner       float
646        yllcorner       float
647        '''
648
649        from Scientific.IO.NetCDF import NetCDFFile
650
651        # ensure that basic instance attributes are correct
652        g = Geo_reference(56, 1.8, 1.8)
653        self.failUnless(isinstance(g.zone, int),
654                        "geo_ref .zone should be 'int' type, "
655                        "was '%s' type" % type(g.zone)) 
656        self.failUnless(isinstance(g.false_easting, int),
657                        "geo_ref .false_easting should be int type, "
658                        "was '%s' type" % type(g.false_easting)) 
659        self.failUnless(isinstance(g.false_northing, int),
660                        "geo_ref .false_northing should be int type, "
661                        "was '%s' type" % type(g.false_northing))
662        self.failUnless(isinstance(g.xllcorner, float),
663                        "geo_ref .xllcorner should be float type, "
664                        "was '%s' type" % type(g.xllcorner))
665        self.failUnless(isinstance(g.yllcorner, float),
666                        "geo_ref .yllcorner should be float type, "
667                        "was '%s' type" % type(g.yllcorner))
668
669        # now write fikle, read back and check types again
670        file_name = tempfile.mktemp(".geo_referenceTest")
671       
672        out_file = NetCDFFile(file_name, netcdf_mode_w)
673        g.write_NetCDF(out_file)
674        out_file.close()
675       
676        in_file = NetCDFFile(file_name, netcdf_mode_r)
677        new_g = Geo_reference(NetCDFObject=in_file)
678        in_file.close()
679        os.remove(file_name)
680
681        self.failUnless(isinstance(new_g.zone, int),
682                        "geo_ref .zone should be 'int' type, "
683                        "was '%s' type" % type(new_g.zone)) 
684        self.failUnless(isinstance(new_g.false_easting, int),
685                        "geo_ref .false_easting should be int type, "
686                        "was '%s' type" % type(new_g.false_easting)) 
687        self.failUnless(isinstance(new_g.false_northing, int),
688                        "geo_ref .false_northing should be int type, "
689                        "was '%s' type" % type(new_g.false_northing))
690        self.failUnless(isinstance(new_g.xllcorner, float),
691                        "geo_ref .xllcorner should be float type, "
692                        "was '%s' type" % type(new_g.xllcorner))
693        self.failUnless(isinstance(new_g.yllcorner, float),
694                        "geo_ref .yllcorner should be float type, "
695                        "was '%s' type" % type(new_g.yllcorner))
696       
697    def test_georef_types_coerceable(self):
698        '''Ensure that attributes of a georeference are of correct type.
699       
700        zone            int
701        false_easting   int
702        false_northing  int
703        xllcorner       float
704        yllcorner       float
705        '''
706
707        # now provide wrong types but coerceable
708        g = Geo_reference(56.0, '1.8', '1.8')
709        self.failUnless(isinstance(g.zone, int),
710                        "geo_ref .zone should be 'int' type, "
711                        "was '%s' type" % type(g.zone)) 
712        self.failUnless(isinstance(g.false_easting, int),
713                        "geo_ref .false_easting should be int type, "
714                        "was '%s' type" % type(g.false_easting)) 
715        self.failUnless(isinstance(g.false_northing, int),
716                        "geo_ref .false_northing should be int type, "
717                        "was '%s' type" % type(g.false_northing))
718        self.failUnless(isinstance(g.xllcorner, float),
719                        "geo_ref .xllcorner should be float type, "
720                        "was '%s' type" % type(g.xllcorner))
721        self.failUnless(isinstance(g.yllcorner, float),
722                        "geo_ref .yllcorner should be float type, "
723                        "was '%s' type" % type(g.yllcorner))
724
725       
726#-------------------------------------------------------------
727
728if __name__ == "__main__":
729    suite = unittest.makeSuite(geo_referenceTestCase, 'test')
730    runner = unittest.TextTestRunner() #verbosity=2)
731    runner.run(suite)
732   
Note: See TracBrowser for help on using the repository browser.