Ejemplo n.º 1
0
def get_average(file, points, radius=40):
    '''Like get_values, but computes the average value within a circle of the 
    specified radius (in km).
    
    Missing values are ignored. Returns None if there were no values within the 
    circle.
    
    >>> lat_lons = [(10,10), (20,20), (0,0)]
    >>> [type(x) for x in get_average('bio1', lat_lons, 0)]
    [<type 'float'>, <type 'float'>, <type 'NoneType'>]
    >>> get_average('bio1', lat_lons, 100) != get_average('bio1', lat_lons, 50) != get_average('bio1', lat_lons, 0)
    True
    '''

    data, no_value, ul, dims, size = extract_attributes(file)

    result = []
    for point in points:
        within = points_within_distance(*(point + ul + dims), radius=radius)
        raster_positions = [
            xy_coords(lat, lon, *(ul + dims)) for (lat, lon) in within
        ]
        values = [
            get_point(data, pos[0], pos[1], no_value)
            for pos in raster_positions
        ]
        values = [v for v in values if not v is None]
        if len(values) == 0: result.append(None)
        else:
            result.append(sum(values) / float(len(values)))

    return result
Ejemplo n.º 2
0
def get_average(file, points, radius=40):
    '''Like get_values, but computes the average value within a circle of the 
    specified radius (in km).
    
    Missing values are ignored. Returns None if there were no values within the 
    circle.
    
    >>> lat_lons = [(10,10), (20,20), (0,0)]
    >>> [type(x) for x in get_average('bio1', lat_lons, 0)]
    [<type 'float'>, <type 'float'>, <type 'NoneType'>]
    >>> get_average('bio1', lat_lons, 100) != get_average('bio1', lat_lons, 50) != get_average('bio1', lat_lons, 0)
    True
    '''

    data, no_value, ul, dims, size = extract_attributes(file)
    
    result = []
    for point in points:
        within = points_within_distance(*(point + ul + dims), radius=radius)
        raster_positions = [xy_coords(lat, lon, *(ul + dims)) for (lat, lon) in within]
        values = [get_point(data, pos[0], pos[1], no_value) 
                  for pos in raster_positions]
        values = [v for v in values if not v is None]
        if len(values) == 0: result.append(None)
        else:
            result.append(sum(values)/float(len(values)))

    return result
Ejemplo n.º 3
0
def get_spatial_variance(file, points, radius=40):
    '''Like get_values, but computes the spatial variance within a circle of the
    specified radius (in km).
    
    Missing values are ignored. Returns None if there were no values within the 
    circle.
    
    >>> lat_lons = [(10,10), (20,20), (0,0)]
    >>> get_spatial_variance('bio1', lat_lons, 0)
    [0.0, 0.0, None]
    >>> (get_spatial_variance('bio1', lat_lons[0:1], 100) >= 
    ... get_spatial_variance('bio1', lat_lons[0:1], 50) >= 
    ... get_spatial_variance('bio1', lat_lons[0:1], 0))
    True
    '''

    data, no_value, ul, dims, size = extract_attributes(file)

    result = []
    for point in points:
        # because the distance between longitude points approaches 0 at the
        # poles, only compute variance between 60 N and 60 S
        if abs(point[0]) > 60:
            result.append(None)
            continue

        within = points_within_distance(*(point + ul + dims), radius=radius)
        raster_positions = [
            xy_coords(lat, lon, *(ul + dims)) for (lat, lon) in within
        ]
        values = [
            get_point(data, pos[0], pos[1], no_value)
            for pos in raster_positions
        ]
        values = [v for v in values if not v is None]
        if len(values) == 0: result.append(None)
        else:
            result.append(float(np.var(values)))

    return result
Ejemplo n.º 4
0
def get_spatial_variance(file, points, radius=40):
    '''Like get_values, but computes the spatial variance within a circle of the
    specified radius (in km).
    
    Missing values are ignored. Returns None if there were no values within the 
    circle.
    
    >>> lat_lons = [(10,10), (20,20), (0,0)]
    >>> get_spatial_variance('bio1', lat_lons, 0)
    [0.0, 0.0, None]
    >>> (get_spatial_variance('bio1', lat_lons[0:1], 100) >= 
    ... get_spatial_variance('bio1', lat_lons[0:1], 50) >= 
    ... get_spatial_variance('bio1', lat_lons[0:1], 0))
    True
    '''

    data, no_value, ul, dims, size = extract_attributes(file)
    
    result = []
    for point in points:
        # because the distance between longitude points approaches 0 at the 
        # poles, only compute variance between 60 N and 60 S
        if abs(point[0]) > 60:
            result.append(None)
            continue

        within = points_within_distance(*(point + ul + dims), radius=radius)
        raster_positions = [xy_coords(lat, lon, *(ul + dims)) for (lat, lon) in within]
        values = [get_point(data, pos[0], pos[1], no_value) 
                  for pos in raster_positions]
        values = [v for v in values if not v is None]
        if len(values) == 0: result.append(None)
        else:
            result.append(float(np.var(values)))

    return result