Пример #1
0
def test_constructors():
    newgrid = grid.from_ascii(dir_path, dtype=np.uint8, crs=crs)
    new_fdir = grid.read_ascii(dir_path, dtype=np.uint8, crs=crs)
    assert ((fdir == new_fdir).all())
    newgrid = Grid(viewfinder=fdir.viewfinder)
Пример #2
0
from pysheds.rfsm import RFSM

# TODO: Major todo's
# - self.mask should be a raster
# - grid.clip_to should be able to take a raster (use _input_handler)

current_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.abspath(os.path.join(current_dir, '../data'))
dir_path = os.path.join(data_dir, 'dir.asc')
dem_path = os.path.join(data_dir, 'dem.tif')
roi_path = os.path.join(data_dir, 'roi.tif')
eff_path = os.path.join(data_dir, 'eff.tif')
dinf_eff_path = os.path.join(data_dir, 'dinf_eff.tif')

# Initialize grid
grid = Grid()
crs = pyproj.Proj('epsg:4326', preserve_units=True)
grid.read_ascii(dir_path, 'dir', dtype=np.uint8, crs=crs)
grid.read_raster(dem_path, 'dem')
grid.read_raster(roi_path, 'roi')
grid.read_raster(eff_path, 'eff')
grid.read_raster(dinf_eff_path, 'dinf_eff')
# set nodata to 1
# why is that not working with grid.view() in test_accumulation?
#grid.eff[grid.eff==grid.eff.nodata] = 1
#grid.dinf_eff[grid.dinf_eff==grid.dinf_eff.nodata] = 1

# Initialize parameters
dirmap = (64, 128, 1, 2, 4, 8, 16, 32)
acc_in_frame = 76499
acc_in_frame_eff = 76498  # max value with efficiency
Пример #3
0
def compute_section_mask(fdir_values,
                         fdir_map=None,
                         fdir_nodata=0,
                         geo_reference=None,
                         section_reference=None):

    if fdir_map is None:
        fdir_map = [8, 9, 6, 3, 2, 1, 4, 7]

    geo_values = geo_reference['values']
    geo_longitude = geo_reference['longitude']
    geo_latitude = geo_reference['latitude']
    geo_transform = geo_reference['transform']
    geo_crs = geo_reference['crs']

    mask_values = np.zeros([fdir_values.shape[0], fdir_values.shape[1]],
                           dtype=bool)
    mask_values[:, :] = True

    grid = Grid()
    grid.add_gridded_data(data=fdir_values,
                          data_name='fdir',
                          affine=geo_transform,
                          crs=pyproj.Proj(geo_crs),
                          nodata=fdir_nodata)
    grid.add_gridded_data(data=mask_values,
                          data_name='mask',
                          affine=geo_transform,
                          crs=pyproj.Proj(geo_crs),
                          nodata=False)

    section_obj = {}
    for section_tag, section_fields in section_reference.items():
        section_idx_ji = section_fields['section_idx_ji']
        section_j = section_idx_ji[0] - 1
        section_i = section_idx_ji[1] - 1
        section_mask = 'section_mask'

        grid.catchment(data=grid.fdir,
                       x=section_i,
                       y=section_j,
                       dirmap=fdir_map,
                       out_name=section_mask,
                       recursionlimit=15000,
                       nodata_out=0,
                       ytype='index')
        section_mask = np.array(grid.section_mask).astype(np.float32)

        section_mask[section_mask == 0] = 0
        section_mask[geo_values < 0] = 0
        section_mask[section_mask >= 1] = 1

        geo_latitude = np.flipud(geo_latitude)

        section_da = create_darray_2d(section_mask,
                                      geo_longitude,
                                      geo_latitude,
                                      coord_name_x='Longitude',
                                      coord_name_y='Latitude',
                                      dim_name_x='west_east',
                                      dim_name_y='south_north',
                                      dims_order=['south_north', 'west_east'])

        section_obj[section_tag] = {}
        section_obj[section_tag] = section_da

    return section_obj