Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_gauss_fwhm(self):
data = numpy.fromfunction(lambda y, x: (y + x)*10**-5, (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)
if sys.version_info < (2, 6):
res = kd_tree.resample_gauss(swath_def, data.ravel(),\
self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), segments=1)
else:
with warnings.catch_warnings(record=True) as w:
res = kd_tree.resample_gauss(swath_def, data.ravel(),\
self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), 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')
cross_sum = res.sum()
expected = 4872.81050892
self.assertAlmostEqual(cross_sum, expected,\
msg='Swath resampling gauss failed')
def test_self_map_multi(self):
data = np.column_stack((self.tb37v, self.tb37v, self.tb37v))
swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats)
if sys.version_info < (2, 6):
res = kd_tree.resample_gauss(swath_def, data, swath_def,
radius_of_influence=70000, sigmas=[56500, 56500, 56500])
else:
with warnings.catch_warnings(record=True) as w:
res = kd_tree.resample_gauss(swath_def, data, swath_def,
radius_of_influence=70000, sigmas=[56500, 56500, 56500])
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')
self.assertAlmostEqual(res[:, 0].sum() / 100., 668848.082208, 1,
msg='Failed self mapping swath multi for channel 1')
self.assertAlmostEqual(res[:, 1].sum() / 100., 668848.082208, 1,
msg='Failed self mapping swath multi for channel 2')
self.assertAlmostEqual(res[:, 2].sum() / 100., 668848.082208, 1,
msg='Failed self mapping swath multi for channel 3')
def test_gauss_multi_mp(self):
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):
res = kd_tree.resample_gauss(swath_def, data_multi,\
self.area_def, 50000, [25000, 15000, 10000],\
nprocs=2, segments=1)
else:
with warnings.catch_warnings(record=True) as w:
res = kd_tree.resample_gauss(swath_def, data_multi,\
self.area_def, 50000, [25000, 15000, 10000],\
nprocs=2, 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')
cross_sum = res.sum()
expected = 1461.84313918
self.assertAlmostEqual(cross_sum, expected,\
msg='Swath multi channel resampling gauss failed')
def test_self_map(self):
swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats)
if sys.version_info < (2, 6):
res = kd_tree.resample_gauss(swath_def, self.tb37v.copy(), swath_def,
radius_of_influence=70000, sigmas=56500)
else:
with warnings.catch_warnings(record=True) as w:
res = kd_tree.resample_gauss(swath_def, self.tb37v.copy(), swath_def,
radius_of_influence=70000, sigmas=56500)
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')
self.assertAlmostEqual(res.sum() / 100., 668848.082208, 1,
msg='Failed self mapping swath for 1 channel')
def test_gauss_multi_mp_segments_empty(self):
data = numpy.fromfunction(lambda y, x: (y + x)*10**-6, (5000, 100))
lons = numpy.fromfunction(lambda y, x: 165 + (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()))
res = kd_tree.resample_gauss(swath_def, data_multi,\
self.area_def, 50000, [25000, 15000, 10000],\
nprocs=2, segments=1)
cross_sum = res.sum()
self.assertTrue(cross_sum == 0,
msg=('Swath multi channel segments empty '
'resampling gauss failed'))
lats = df.Latitude.values[index]
lons = df.Longitude.values[index]
grid2 = geometry.GridDefinition(lons=vstack(lons), lats=vstack(lats))
sites = df.Site_Code.values[index]
utc = df.utcoffset.values[index]
vals = pd.Series(dtype=df.Obs.dtype)
date = pd.Series(dtype=df.datetime.dtype)
site = pd.Series(dtype=df.Site_Code.dtype)
utcoffset = pd.Series(dtype=df.utcoffset.dtype)
for i, j in enumerate(self.cmaq.indexdates):
if interp.lower() == 'idw':
val = kd_tree.resample_custom(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
fill_value=NaN, neighbours=n, weight_funcs=weight_func,
nprocs=2).squeeze()
elif interp.lower() == 'gauss':
val = kd_tree.resample_gauss(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
sigmas=r / 2., fill_value=NaN, neighbours=n, nprocs=2).squeeze()
else:
interp = 'nearest'
val = kd_tree.resample_nearest(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
fill_value=NaN, nprocs=2).squeeze()
vals = vals.append(pd.Series(val)).reset_index(drop=True)
date = date.append(pd.Series([self.cmaq.dates[j] for k in lons])).reset_index(drop=True)
site = site.append(pd.Series(sites)).reset_index(drop=True)
utcoffset = utcoffset.append(pd.Series(utc)).reset_index(drop=True)
dfs = pd.concat([vals, date, site, utcoffset], axis=1, keys=['CMAQ', 'datetime', 'Site_Code', 'utcoffset'])
dfs.index = dfs.datetime
r = dfs.groupby('Site_Code').resample('24H').mean().reset_index()
df = pd.merge(df, r, how='left', on=['Site_Code', 'datetime', 'utcoffset']).dropna(subset=['CMAQ'])
df['Obs'][df['Obs'] < 0] = NaN
df.dropna(subset=['Obs'], inplace=True)
return df
def interpolate(self, input_def, output_def, data):
""" Interpolates data given input and output definitions
and the selected interpolation algorithm.
"""
# Ignore pyresample warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
warnings.simplefilter("ignore", UserWarning)
# Interpolation with gaussian weighting
if self.interp == "gaussian":
return pyresample.kd_tree.resample_gauss(input_def, data,
output_def, radius_of_influence=float(self.radius), sigmas=self.radius / 2, fill_value=None,
nprocs=8)
# Bilinear weighting
elif self.interp == "bilinear":
"""
Weight function used to determine the effect of surrounding points
on a given point
"""
def weight(r):
r = np.clip(r, np.finfo(r.dtype).eps,
np.finfo(r.dtype).max)
return 1. / r
return pyresample.kd_tree.resample_custom(input_def, data,
output_def, radius_of_influence=float(self.radius), neighbours=self.neighbours, fill_value=None,
site = array([], dtype=df.SCS.dtype)
print ' Interpolating using ' + interp + ' method'
for i, j in enumerate(dates):
con = df.datetime == j
lats = df[con].Latitude.values
lons = df[con].Longitude.values
grid2 = geometry.GridDefinition(lons=vstack(lons), lats=vstack(lats))
if interp.lower() == 'nearest':
val = kd_tree.resample_nearest(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
fill_value=NaN, nprocs=2).squeeze()
elif interp.lower() == 'idw':
val = kd_tree.resample_custom(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
fill_value=NaN, neighbours=n, weight_funcs=weight_func,
nprocs=2).squeeze()
elif interp.lower() == 'gauss':
val = kd_tree.resample_gauss(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
sigmas=r / 2., fill_value=NaN, neighbours=n, nprocs=2).squeeze()
vals = append(vals, val)
dd = empty(lons.shape[0], dtype=date.dtype)
dd[:] = j
date = append(date, dd)
site = append(site, df[con].SCS.values)
vals = pd.Series(vals)
date = pd.Series(date)
site = pd.Series(site)
dfs = concat([vals, date, site], axis=1, keys=['CMAQ', 'datetime', 'SCS'])
df = pd.merge(df, dfs, how='left', on=['SCS', 'datetime'])
return df
for i, j in enumerate(dates):
con = df.datetime == j
print j
try:
lats = df[con].Latitude.values
lons = df[con].Longitude.values
grid2 = geometry.GridDefinition(lons=vstack(lons), lats=vstack(lats))
if interp.lower() == 'nearest':
val = kd_tree.resample_nearest(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
fill_value=NaN, nprocs=2).squeeze()
elif interp.lower() == 'idw':
val = kd_tree.resample_custom(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
fill_value=NaN, neighbours=n, weight_funcs=weight_func,
nprocs=2).squeeze()
elif interp.lower() == 'gauss':
val = kd_tree.resample_gauss(grid1, cmaqvar[i, :, :].squeeze(), grid2, radius_of_influence=r,
sigmas=r / 2., fill_value=NaN, neighbours=n, nprocs=2).squeeze()
vals = append(vals, val)
dd = empty(lons.shape[0], dtype=date.dtype)
dd[:] = j
date = append(date, dd)
site = append(site, df[con].SCS.values)
collect()
except:
pass
vals = Series(vals)
date = Series(date)
site = Series(site)
dfs = concat([vals, date, site], axis=1, keys=['CMAQ', 'datetime', 'SCS'])
# if daily:
# a = dfs.merge(dfo3,on=