import math


def getCellNum(lon, lat, gridLimit, gridSpace):
    """
    Return the cell number given longitude and latititude
    """
    lon = int(math.floor(lon))
    lat = int(math.ceil(lat))

    if (lon < gridLimit['xMin'] or lon >= gridLimit['xMax'] or
            lat <= gridLimit['yMin'] or lat > gridLimit['yMax']):
        raise ValueError('Invalid input on cellNum: cell number is out of range')

    j = abs((abs(lon) - abs(gridLimit['xMin']))) // abs(gridSpace['x'])
    i = abs((abs(lat) - abs(gridLimit['yMax']))) // abs(gridSpace['y'])

    return int(i * abs((gridLimit['xMax'] - gridLimit['xMin']) // gridSpace['x']) + j)