示例#1
0
def read_wid_csvs():
    data = dict()
    for fname in glob.glob(
            os.path.join(utils.data_root(), 'wid', 'Data',
                         'WID_*_InequalityData.csv')):
        bname = os.path.basename(fname)
        _, iso2, _ = bname.split('_', 3)
        iso2 = iso2.lower()
        df = pd.read_csv(fname, sep=';', encoding='latin1', low_memory=False)
        df.columns = df.iloc[6, :]
        data[iso2] = df
    return data
示例#2
0
def cnames_df():
    cnames = pd.read_csv(
        os.path.join(utils.data_root(), 'ssp-data', 'country-names.csv'))
    return cnames
示例#3
0
                    default=False,
                    action='store_true',
                    help='Project using mainland coefficients '
                    '(default: islands)')
parser.add_argument('--clip',
                    '-c',
                    dest='clip',
                    default=False,
                    action='store_true',
                    help='Clip predictor variables to max value seen '
                    'during model fitting')
args = parser.parse_args()

if args.mainland:
    ISLMAIN = 1
    mask_file = os.path.join(utils.data_root(),
                             '1km/mainland-from-igor-edited-at.tif')
else:
    ISLMAIN = 0
    mask_file = os.path.join(utils.data_root(),
                             '1km/islands-from-igor-edited-at.tif')

# Open the mask raster file (Mainlands)
mask_ds = rasterio.open(mask_file)

# Read Katia's abundance model
mod = modelr.load('/home/vagrant/katia/models/best_model_abund.rds')
predicts.predictify(mod)

# Import standard PREDICTS rasters
rasters = predicts.rasterset('1km', 'medium', year=2005)
示例#4
0
                     win2[0][1])), (win1[1][0] + win2[1][0],
                                    min(win1[1][1] + win2[1][0], win2[1][1])))
    return win1


@click.command()
@click.option('--band', default=1, help='Band of raster to mask')
@click.argument('raster', type=click.Path())
@click.argument('mask', type=click.Path())
def maskit(raster, mask, band=1):
    rds = rasterio.open(raster, 'r+')
    ice = rasterio.open(mask)
    minxs, minys, maxxs, maxys = zip(rds.bounds, ice.bounds)
    bounds = (max(minxs), max(minys), min(maxxs), min(maxys))
    ice_view = ice.window(*bounds)
    nodata = rds.nodatavals[band - 1]

    for ij, win in rds.block_windows():
        if rds.height > 10000 and win[0][1] % 100 == 0:
            print(win)
        rr = rds.read(band, masked=True, window=win)
        ii = ice.read(1, masked=True, window=window_inset(win, ice_view))
        rr.mask = np.logical_or(rr.mask, np.where(ii == 1.0, True, False))
        rds.write(rr.filled(nodata), window=win, indexes=band)


if __name__ == '__main__':
    raster = 'ds/1km/roads.tif'
    mask = 'zip:%s/1km/ICE.zip!ICE_1km_2005.bil' % utils.data_root()
    maskit()
示例#5
0
import click
#import matlibplot.pyplot as plt
import numpy as np
import numpy.ma as ma
import rasterio
from rasterio.plot import show, show_hist

from projections.rasterset import RasterSet, Raster
from projections.simpleexpr import SimpleExpr
import projections.predicts as predicts
import projections.r2py.modelr as modelr
import projections.utils as utils

# Open the mask raster file
mask_file = os.path.join(utils.data_root(),
                         '1km/mainland-from-igor-edited.tif')
mask_ds = rasterio.open(mask_file)

# Richness compositional similarity model with updated PriMin-Urb coefficient
mod = modelr.load('/home/vagrant/katia/models/updated_compsim_rich.rds')
predicts.predictify(mod)

# Import standard PREDICTS rasters
rasters = predicts.rasterset('1km', 'medium', year=2005)

# create an ISL_MAINL raster
# set it to Mainlands this time round (set Mainlands to 1 and Islands to 0)
rasters['ISL_MAINMAINLAND'] = SimpleExpr('ISL_MAINMAINLAND', '2')
rasters['ISL_MAINISLAND'] = SimpleExpr('ISL_MAINISLAND', '0')
示例#6
0
    click.echo('processing %s' % region)
    fips_names = map(wrap, regions[region])
    features = filter(lambda s: s['properties']['FIPS'] in fips_names, src)
    shapes = map(shapely.geometry.shape,
                 [f['geometry'] for f in features])
    union = shapely.ops.unary_union(shapes)
    props = OrderedDict({'name': region})
    dst.write({'geometry': shapely.geometry.mapping(union),
               'type': features[0]['type'],
               'properties': props,
      })

@click.command()
@click.argument('borders', type=click.Path())
@click.argument('regions', type=click.File('r'),
                default=os.path.join(utils.data_root(), 'ssp-data',
                                     'regions.json'))
@click.argument('country-names', type=click.Path(),
                default=os.path.join(utils.data_root(), 'ssp-data',
                                     'country-names.csv'))
@click.argument('output', type=click.Path(dir_okay=False, writable=True))
def main(borders, regions, country_names, output):
  cnames = pd.DataFrame.from_csv(country_names)
  region_data = json.load(regions)
  with fiona.open(borders) as src:
    meta = src.meta
    meta['schema']['properties'] = OrderedDict({'name': 'str:8'})
    with fiona.open(output, 'w', **meta) as dst:
      merge(src, dst, region_data, cnames)

if __name__ == '__main__':
示例#7
0
HPD_MAX = 22490

parser = argparse.ArgumentParser(description="sr.py -- richness projections")
parser.add_argument('--mainland', '-m', dest='mainland', default=False,
                    action='store_true',
                    help='Project using mainland coefficients '
                    '(default: islands)')
parser.add_argument('--clip', '-c', dest='clip', default=False,
                    action='store_true',
                    help='Clip predictor variables to max value seen '
                    'during model fitting')
args = parser.parse_args()

if args.mainland:
  ISLMAIN = 1
  mask_file = os.path.join(utils.data_root(),
                           '1km/mainland-from-igor-edited-at.tif')
else:
  ISLMAIN = 0
  mask_file = os.path.join(utils.data_root(),
                         '1km/islands-from-igor-edited-at.tif')

# Open the mask raster file (Mainlands)
mask_ds = rasterio.open(mask_file)

# Read Katia's richness model
mod = modelr.load('/home/vagrant/katia/models/best_model_rich.rds')
predicts.predictify(mod)

# Import standard PREDICTS rasters
rasters = predicts.rasterset('1km', 'medium', year = 2005)