source: trunk/anuga_core/source/anuga/coordinate_transforms/test_geo_reference.py @ 7967

Last change on this file since 7967 was 7317, checked in by rwilson, 15 years ago

Replaced 'print' statements with log.critical() calls.

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