comparison tests/test_grid.py @ 1:1b94f3bf97e5

* limit distance function * start gridding * improve unicode handling
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 14:02:14 -0700
parents 5dba84370182
children 49aae0c0293b
comparison
equal deleted inserted replaced
0:5dba84370182 1:1b94f3bf97e5
2 2
3 """ 3 """
4 test that we can grid a solution 4 test that we can grid a solution
5 """ 5 """
6 6
7 import os
7 import unittest 8 import unittest
9 from common import datafile
8 from globalneighbors.grid import LatLonGrid 10 from globalneighbors.grid import LatLonGrid
11 from globalneighbors.locations import locations
12 from globalneighbors.read import read_city_list
13
9 14
10 class TestGrid(unittest.TestCase): 15 class TestGrid(unittest.TestCase):
11 """test gridding functionality""" 16 """test gridding functionality"""
17
18 ### test functions
12 19
13 def test_dimensions(self): 20 def test_dimensions(self):
14 21
15 # make a 2 degree grid 22 # make a 2 degree grid
16 grid = LatLonGrid(90, 180) 23 grid = LatLonGrid(90, 180)
17 assert grid.n == (90, 180) 24 assert grid.n == (90, 180)
18 assert grid.d == (2., 2.) 25 assert grid.d == (2., 2.)
26 assert len(grid.grid) == 90
27 for row in grid.grid:
28 assert len(row) == 180
19 29
20 def test_insertion(self): 30 def test_insertion(self):
21 31
22 coord = (-23., 122.) 32 coord = (-23., 122.)
23 grid = LatLonGrid(3, 4) 33 grid = LatLonGrid(3, 4)
24 grid.add(1234, *coord) 34 grid.add(1234, *coord)
25 i, j = grid.index(*coord) 35 i, j = grid.index(*coord)
26 assert i == 1 36 assert i == 1
27 assert j == 3 37 assert j == 3
28 assert grid[(i,j)] == [1234] 38 assert grid[(i,j)] == set([1234])
39
40 def test_sample(self):
41
42 samplefile = datafile('sample.tsv')
43 assert os.path.exists(samplefile)
44 city_locations = locations(read_city_list(samplefile))
45 self.grid_locations(city_locations)
46
47 ### generic (utility) functions
48
49 def grid_locations(self, locations):
50 """grid locations + test created grid"""
51
52 grid = LatLonGrid(8, 8)
53 for geoid, (lat, lon) in locations.items():
54 grid.add(geoid, lat, lon)
29 55
30 if __name__ == '__main__': 56 if __name__ == '__main__':
31 unittest.main() 57 unittest.main()