Exemple #1
0
def extract_environment(layer_name, x, postproc=lambda x:x, id_=None, lock=None):
    "Expects ALL locations to be in decimal degrees."
    
    fname = hashlib.sha1(x.tostring()+layer_name+str(id_)).hexdigest()+'.npy'
    path, name = os.path.split(layer_name)
    name = os.path.splitext(name)[0]
    if fname in os.listdir('anopheles-caches'):
        return name, numpy.load(os.path.join('anopheles-caches',fname))
    else:    
        
        # if lock is not None:
        #     lock.acquire()
        grid_lon, grid_lat, grid_data, grid_type = map_utils.import_raster(name,path)
        
        # Convert to centroids
        grid_lon += (grid_lon[1]-grid_lon[0])/2.
        grid_lat += (grid_lat[1]-grid_lat[0])/2.
        
        # Interpolate
        extracted = map_utils.interp_geodata(grid_lon, grid_lat, postproc(grid_data).data, x[:,0], x[:,1], grid_data.mask, chunk=None, view='y-x+', order=0)
        del grid_data
        # if lock is not None:
        #     lock.release()

        numpy.save(os.path.join('anopheles-caches',fname), extracted)
        return name, extracted
def trees_to_map(brt_evaluator, species_name, layer_names, glob_name, glob_channels, bbox, config_filename, memlim = 4e9):
    """
    Makes maps and writes them out in flt format.
    """
    all_names = get_names(layer_names, glob_name, glob_channels)
    short_layer_names = all_names[:len(layer_names)]
    short_glob_names = all_names[len(layer_names):]
    result_dirname = get_result_dir(config_filename)
    
    llclat,llclon,urclat,urclon = bbox
    
    rasters = {}

    n_rasters = len(layer_names)+len(glob_channels)
    lon,lat,glob,t = map_utils.import_raster(*os.path.split(glob_name)[::-1])
    
    orig_glob_shape = glob.shape
    
    llclati = np.where(lat>=llclat)[0][0]
    llcloni = np.where(lon>=llclon)[0][0]
    urclati = np.where(lat<=urclat)[0][-1]
    urcloni = np.where(lon<=urclon)[0][-1]    
    
    glob = subset_raster(glob, llclati, llcloni, urclati, urcloni)
    where_notmask = np.where(True-glob.mask)
    raster_size = np.prod(glob.shape)*4
    if raster_size * n_rasters > memlim:
        warnings.warn('Species %s: Generating this map would require too much memory. Make the bounding box smaller.'%species_name)
    
    for n, ch in zip(short_glob_names, glob_channels):
        rasters[n] = (glob==ch)[where_notmask]    

    for n, p in zip(short_layer_names, layer_names):
        lon,lat,data,t = map_utils.import_raster(*os.path.split(p)[::-1])
        if data.shape != orig_glob_shape:
            raise ValueError, 'Shape of raster %s does not match shape of Glob raster for species %s. Check config file.'%(n,species_name)
        rasters[n] = subset_raster(data, llclati, llcloni, urclati, urcloni)[where_notmask]
        
    ravelledmap = brt_evaluator(rasters)
    out_raster = glob.astype('float32')
    out_raster[where_notmask] = pm.flib.invlogit(ravelledmap)

    return lon[llcloni:urcloni],lat[llclati:urclati],out_raster
def get_pseudoabsences(eo, buffer_width, n_pseudoabsences, layer_names, glob_name, eo_expand=0):
    if eo_expand:
        eo = lomem_buffer(eo, eo_expand)
    fname = hashlib.sha1(geojson.dumps(eo)+'_'+str(buffer_width)+'_'+str(n_pseudoabsences)).hexdigest()+'.npy'
    if fname in os.listdir('anopheles-caches'):
        pseudoabsences = np.load(os.path.join('anopheles-caches', fname))
    else:
        if buffer_width >= 0:
            buff = lomem_buffer(eo, buffer_width)
            diff_buffer = buff.difference(eo)
        elif buffer_width == -1:
            diff_buffer = eo
        else:
            raise ValueError, 'Buffer width is negative, but not -1.'
        
        if len(layer_names)>0:
            template = layer_names[0]
        else:
            template = glob_name

        lon, lat, test_raster, rtype = map_utils.import_raster(*os.path.split(template)[::-1])
        # Right the raster
        test_raster = map_utils.grid_convert(test_raster,'y-x+','x+y+')
        # Convert lower-left coords to centroids
        lon += (lon[1]-lon[0])/2.
        lat += (lat[1]-lat[0])/2.
    
        def testfn(lon_test,lat_test,r=test_raster,lon=lon,lat=lat):
            lon_ind = np.argmin(np.abs(lon-lon_test))
            lat_ind = np.argmin(np.abs(lat-lat_test))
            return True-test_raster.mask[lon_ind,lat_ind]
    
        pseudoabsences = np.vstack(map_utils.shapefile_utils.multipoly_sample(n_pseudoabsences, diff_buffer, test=testfn)).T
        if not np.all([testfn(l1,l2) for l1,l2 in pseudoabsences]):
            raise ValueError, 'Test failed for some pseudoabsences.'
        np.save(os.path.join('anopheles-caches',fname), pseudoabsences)
        
    return pseudoabsences, eo
Exemple #4
0
try:
    os.mkdir('5k-covariates')
except OSError:
    pass

# Parameters

modis_missing = 0
glob_missing = 210

modis_res = (21600, 43200)
lon = np.linspace(-180,180,modis_res[1])
lat = np.linspace(-90,90,modis_res[0])

af_lon, af_lat, af_data, af_type = map_utils.import_raster('africa','.')
lon_min, lon_max, lat_min, lat_max = af_lon.min(), af_lon.max(), af_lat.min(), af_lat.max()
pete_lon, pete_lat, pete_mask, junk = map_utils.import_raster('ls','.')

# Subset the rasters
lon_min_i, lon_max_i = ((np.array([lon_min, lon_max])+180.)/360.*modis_res[1]).astype('int')
lat_min_i, lat_max_i = ((np.array([lat_min, lat_max])+90.)/180.*modis_res[0]).astype('int')

pete_lat_min_i = np.argmin(np.abs(pete_lat - af_lat.min()))
pete_lat_max_i = np.argmin(np.abs(pete_lat - af_lat.max()))
clipped_pete_mask = grid_convert(grid_convert(pete_mask,'y-x+','x+y+')[lon_min_i/5.:lon_max_i/5.,pete_lat_min_i:pete_lat_max_i],'x+y+','y-x+')

af_lon = af_lon[:-1]
af_lat = af_lat[:-1]
af_data = af_data[:-1,:-1]
<<<<<<< Updated upstream