view 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
line wrap: on
line source

#!/usr/bin/env python

"""
test that we can grid a solution
"""

import os
import unittest
from common import datafile
from globalneighbors.grid import LatLonGrid
from globalneighbors.locations import locations
from globalneighbors.read import read_city_list


class TestGrid(unittest.TestCase):
    """test gridding functionality"""

    ### test functions

    def test_dimensions(self):

        # make a 2 degree grid
        grid = LatLonGrid(90, 180)
        assert grid.n == (90, 180)
        assert grid.d == (2., 2.)
        assert len(grid.grid) == 90
        for row in grid.grid:
            assert len(row) == 180

    def test_insertion(self):

        coord = (-23., 122.)
        grid = LatLonGrid(3, 4)
        grid.add(1234, *coord)
        i, j = grid.index(*coord)
        assert i == 1
        assert j == 3
        assert grid[(i,j)] == set([1234])

    def test_sample(self):

        samplefile = datafile('sample.tsv')
        assert os.path.exists(samplefile)
        city_locations = locations(read_city_list(samplefile))
        self.grid_locations(city_locations)

    ### generic (utility) functions

    def grid_locations(self, locations):
        """grid locations + test created grid"""

        grid = LatLonGrid(8, 8)
        for geoid, (lat, lon) in locations.items():
            grid.add(geoid, lat, lon)

if __name__ == '__main__':
    unittest.main()