Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Fake function.
"""
del args, kwargs
return (np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)))
def fake_gsfni(typ, area, data, *args, **kwargs):
"""Fake function.
"""
del typ, area, args, kwargs
return data - 1
kd_tree.old_get_neighbour_info = kd_tree.get_neighbour_info
kd_tree.get_neighbour_info = fake_get_neighbour_info
kd_tree.old_gsfni = kd_tree.get_sample_from_neighbour_info
kd_tree.get_sample_from_neighbour_info = fake_gsfni
data = numpy.fromfunction(lambda y, x: (y + x)*10**-6, (5000, 100))
lons = numpy.fromfunction(lambda y, x: 3 + (10.0/100)*x, (5000, 100))
lats = numpy.fromfunction(lambda y, x: 75 - (50.0/5000)*y, (5000, 100))
swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
data_multi = numpy.column_stack((data.ravel(), data.ravel(),\
data.ravel()))
if sys.version_info < (2, 6):
valid_input_index, valid_output_index, index_array, distance_array = \
kd_tree.get_neighbour_info(swath_def,
self.area_def,
50000, segments=1)
else:
with warnings.catch_warnings(record=True) as w:
valid_input_index, valid_output_index, index_array, distance_array = \
kd_tree.get_neighbour_info(swath_def,
self.area_def,
50000, segments=1)
self.assertFalse(len(w) != 1, 'Failed to create neighbour radius warning')
self.assertFalse(('Possible more' not in str(w[0].message)), 'Failed to create correct neighbour radius warning')
res = kd_tree.get_sample_from_neighbour_info('custom', (800, 800),
data_multi,
valid_input_index, valid_output_index,
index_array, distance_array,
weight_funcs=[wf1, wf2, wf3])
cross_sum = res.sum()
expected = 1461.842980746
self.assertAlmostEqual(cross_sum, expected,\
msg='Swath multi channel custom resampling from neighbour info failed 1')
data = numpy.ones((50, 10))
data[:, 5:] = 2
mask1 = numpy.ones((50, 10))
mask1[:, :5] = 0
mask2 = numpy.ones((50, 10))
mask2[:, 5:] = 0
mask3 = numpy.ones((50, 10))
mask3[:25, :] = 0
data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel()))
mask_multi = numpy.column_stack((mask1.ravel(), mask2.ravel(), mask3.ravel()))
masked_data = numpy.ma.array(data_multi, mask=mask_multi)
lons = numpy.fromfunction(lambda y, x: 3 + x, (50, 10))
lats = numpy.fromfunction(lambda y, x: 75 - y, (50, 10))
swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
valid_input_index, valid_output_index, index_array, distance_array = \
kd_tree.get_neighbour_info(swath_def,
self.area_def,
50000, neighbours=1, segments=1)
res = kd_tree.get_sample_from_neighbour_info('nn', (800, 800),
masked_data,
valid_input_index,
valid_output_index, index_array,
fill_value=None)
expected_fill_mask = numpy.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_full_fill_multi.dat'),
sep=' ').reshape((800, 800, 3))
fill_mask = res.mask
self.assertTrue(numpy.array_equal(fill_mask, expected_fill_mask),
msg='Failed to create fill mask on masked data')
"""Fake function.
"""
del args, kwargs
return (np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)))
def fake_gsfni(typ, area, data, *args, **kwargs):
"""Fake function.
"""
del typ, area, args, kwargs
return data - 1
kd_tree.old_get_neighbour_info = kd_tree.get_neighbour_info
kd_tree.get_neighbour_info = fake_get_neighbour_info
kd_tree.old_gsfni = kd_tree.get_sample_from_neighbour_info
kd_tree.get_sample_from_neighbour_info = fake_gsfni
"projections_directory")
except ConfigParser.NoSectionError:
pass
self._filename = os.path.join(projections_directory, filename)
try:
self._cache = {}
self._file_cache = np.load(self._filename)
except:
logger.info("Computing projection from %s to %s...",
in_id, out_id)
if self.mode == "nearest":
valid_index, valid_output_index, index_array, distance_array = \
kd_tree.get_neighbour_info(self.in_area,
self.out_area,
self.radius,
neighbours=1,
nprocs=nprocs)
del distance_array
self._cache = {}
self._cache['valid_index'] = valid_index
self._cache['valid_output_index'] = valid_output_index
self._cache['index_array'] = index_array
elif self.mode == "quick":
ridx, cidx = \
utils.generate_quick_linesample_arrays(self.in_area,
self.out_area)
self._cache = {}
self._cache['row_idx'] = ridx
Valid indices in the input data
idx_arr : numpy array
Mapping array from valid source points to target points
"""
# Check source_geo_def
# if isinstance(source_geo_def, tuple):
# from pyresample.geometry import SwathDefinition
# lons, lats = _mask_coordinates(source_geo_def[0], source_geo_def[1])
# source_geo_def = SwathDefinition(lons, lats)
# Calculate neighbour information
with warnings.catch_warnings():
warnings.simplefilter("ignore")
(input_idxs, output_idxs, idx_ref, dists) = \
kd_tree.get_neighbour_info(source_geo_def, target_area_def,
radius, neighbours=neighbours,
nprocs=nprocs, reduce_data=reduce_data,
segments=segments, epsilon=epsilon)
del output_idxs, dists
# Reduce index reference
input_size = input_idxs.sum()
index_mask = (idx_ref == input_size)
idx_ref = np.where(index_mask, 0, idx_ref)
# Get output projection as pyproj object
proj = Proj(target_area_def.proj_str)
# Get output x/y coordinates
out_x, out_y = _get_output_xy(target_area_def, proj)
Source area definition as geometry definition object
target_area_def : object
Target area definition as geometry definition object
radius_of_influence : float
Cut off distance in meters
nprocs : int, optional
Number of processor cores to be used
Returns
-------
(row_indices, col_indices) : tuple of numpy arrays
"""
from pyresample.kd_tree import get_neighbour_info
valid_input_index, valid_output_index, index_array, distance_array = \
get_neighbour_info(source_area_def,
target_area_def,
radius_of_influence,
neighbours=1,
nprocs=nprocs)
# Enumerate rows and cols
rows = np.fromfunction(lambda i, j: i, source_area_def.shape,
dtype=np.int32).ravel()
cols = np.fromfunction(lambda i, j: j, source_area_def.shape,
dtype=np.int32).ravel()
# Reduce to match resampling data set
rows_valid = rows[valid_input_index]
cols_valid = cols[valid_input_index]
# Get result using array indexing
number_of_valid_points = valid_input_index.sum()
lats=orig_lats_1d)
if (new_grid_ny > 0) and (new_grid_nx > 0):
# 1D grid values
new_grid_lon, new_grid_lat = np.meshgrid(i_reg, j_reg)
# define the lat lon points of the two parts.
new_grid = pr.geometry.GridDefinition(lons=new_grid_lon,
lats=new_grid_lat)
# Get the neighbor info once.
# It will be used repeatedly late to resample data
# fast for each of the datasets that is based on
# the same swath, e.g. for a model variable at different times.
valid_input_index, valid_output_index, index_array, distance_array = \
pr.kd_tree.get_neighbour_info(orig_grid,
new_grid, radius_of_influence,
neighbours=1)
# loop through time steps, one at a time.
for time_step in time_steps_to_load:
i, = np.where(ecco_dataset_all.timestep == time_step)
if(verbose):
print (ecco_dataset_all.timestep.values)
print ('time step ', time_step, i)
# load the dataset
ecco_dataset = ecco_dataset_all.isel(time=i)
# pull out the year, month day, hour, min, sec associated with
# this time step