def test_repeated(self): """Check that block can be used for interpolation to several sets of positions""" reader = reader_netCDF_CF_generic.Reader(o.test_data_folder() + '14Jan2016_NorKyst_z_3d/NorKyst-800m_ZDEPTHS_his_00_3Dsubset.nc') # 100 points within 50x50 pixels over sea (corner of domain) num_points = 100 np.random.seed(0) # To get the same random numbers each time x = np.random.uniform(reader.xmin, reader.xmin+800*50, num_points) y = np.random.uniform(reader.ymax-800*50, reader.ymax, num_points) z = np.random.uniform(-200, 0, num_points) variables = ['x_sea_water_velocity', 'y_sea_water_velocity', 'sea_water_temperature'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) b = ReaderBlock(data, interpolation_horizontal='nearest') env, prof = b.interpolate(x, y, z, 'sea_water_temperature') x2 = x[20:30] y2 = y[20:30] z2 = z[20:30] env2, prof2 = b.interpolate(x2, y2, z2, 'sea_water_temperature') env3, prof3 = b.interpolate(x, y, z, 'sea_water_temperature') self.assertEqual(env['sea_water_temperature'][0], env3['sea_water_temperature'][0]) self.assertEqual(env['sea_water_temperature'][20], env2['sea_water_temperature'][0])
def test_linearNDFast(self): """Test interpolation.""" reader = reader_ROMS_native.Reader( o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc' ) reader.buffer = 3 num_points = 50 np.random.seed(0) # To get the same random numbers each time lons = np.random.uniform(14, 15, num_points) lats = np.random.uniform(68, 68.4, num_points) z = np.random.uniform(-20, 0, num_points) x, y = reader.lonlat2xy(lons, lats) #z=None variables = ['x_sea_water_velocity'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) b = ReaderBlock(data.copy(), interpolation_horizontal='linearNDFast') x2 = np.random.uniform(x.min(), x.max(), num_points) y2 = np.random.uniform(y.min(), y.max(), num_points) z2 = np.random.uniform(-20, 0, num_points) self.assertTrue(b.covers_positions(x, y, z)) self.assertTrue(b.covers_positions(x2, y2, z2)) # Check that there are holes in the arrays of the ReaderBlock self.assertEqual( np.sum(~np.isfinite(b.data_dict['x_sea_water_velocity'])), 1001) # Check that LinearNDFast interpolation gives a real value env, prof = b.interpolate(x2, y2, z2, variables, profiles=variables, profiles_depth=[-30, 0]) self.assertEqual(np.sum(~np.isfinite(env['x_sea_water_velocity'])), 0) # Check that the arrays of the ReaderBlock have been filled in self.assertEqual( np.sum(~np.isfinite(b.data_dict['x_sea_water_velocity'])), 0) # Check that nearest interpolation contains some NaN values b2 = ReaderBlock(data.copy(), interpolation_horizontal='nearest') env, prof = b2.interpolate(x2, y2, z2, variables, profiles=variables, profiles_depth=[-30, 0]) self.assertEqual(np.sum(~np.isfinite(env['x_sea_water_velocity'])), 31)
def test_interpolation_missing(self): """Test interpolation.""" reader = reader_ROMS_native.Reader(o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc') num_points = 50 np.random.seed(0) # To get the same random numbers each time lons = np.random.uniform(10, 11, num_points) lats = np.random.uniform(66, 67.0, num_points) z = np.random.uniform(-200, 0, num_points) x, y = reader.lonlat2xy(lons, lats) variables = ['x_sea_water_velocity', 'y_sea_water_velocity', 'sea_water_temperature'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) # Introduce missing values data['x_sea_water_velocity'] = np.ma.masked_where( data['x_sea_water_velocity']>.08, data['x_sea_water_velocity']) b = ReaderBlock(data, interpolation_horizontal='linearND') env, prof = b.interpolate(x, y, z, variables, profiles=['x_sea_water_velocity'], profiles_depth=[-30, 0]) self.assertAlmostEqual(env['x_sea_water_velocity'][10], 0.074, 2) self.assertAlmostEqual(prof['x_sea_water_velocity'][5,48], -0.090, 2)
def test_interpolation_3dArrays(self): """Test interpolation.""" reader = reader_netCDF_CF_generic.Reader(o.test_data_folder() + '14Jan2016_NorKyst_z_3d/NorKyst-800m_ZDEPTHS_his_00_3Dsubset.nc') # 100000 points within 50x50 pixels over sea (corner of domain) num_points = 1000 np.random.seed(0) # To get the same random numbers each time x = np.random.uniform(reader.xmin, reader.xmin+800*50, num_points) y = np.random.uniform(reader.ymax-800*50, reader.ymax, num_points) z = np.random.uniform(-200, 0, num_points) variables = ['x_sea_water_velocity', 'y_sea_water_velocity', 'sea_water_temperature'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) b = ReaderBlock(data, interpolation_horizontal='nearest') env, prof = b.interpolate(x, y, z, variables, profiles=['sea_water_temperature'], profiles_depth=[-30, 0]) self.assertAlmostEqual(env['x_sea_water_velocity'][100], 0.075019, 3) self.assertAlmostEqual(prof['sea_water_temperature'][0,11], 7.549999, 3) self.assertAlmostEqual(prof['sea_water_temperature'][-1,11], 8.389999, 3) self.assertEqual(prof['z'][-1], b.z[-1])
def test_interpolation_ensemble(self): data_dict, x, y, z = self.get_synthetic_data_dict() x = x[0:15] y = y[0:15] z = z[0:15] data_dict['var2d'] = np.ones(data_dict['var2d'].shape) data_dict['var3d'] = np.ones(data_dict['var3d'].shape) data_dict['var2de'] = [data_dict['var2d']*1, data_dict['var2d']*2, data_dict['var2d']*3] data_dict['var3de'] = [data_dict['var3d']*31, data_dict['var3d']*32, data_dict['var3d']*33] b = ReaderBlock(data_dict) interp = b.interpolate(x, y, z)[0] # 1 is profiles v2 = interp['var2d'] v2e = interp['var2de'] v3 = interp['var3d'] v3e = interp['var3de'] self.assertEqual(v2[0], 1) self.assertEqual(v2e[0], 1) self.assertEqual(v2e[1], 2) self.assertEqual(v2e[3], 1) self.assertEqual(v3[0], 1) self.assertEqual(v3e[0], 31) self.assertEqual(v3e[1], 32) self.assertAlmostEqual(v3e[3], 31)
def test_interpolation_missing(self): """Test interpolation.""" reader = reader_ROMS_native.Reader(o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc') num_points = 50 np.random.seed(0) # To get the same random numbers each time lons = np.random.uniform(10, 11, num_points) lats = np.random.uniform(66, 67.0, num_points) z = np.random.uniform(-200, 0, num_points) x, y = reader.lonlat2xy(lons, lats) variables = ['x_sea_water_velocity', 'y_sea_water_velocity', 'sea_water_temperature'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) # Introduce missing values data['x_sea_water_velocity'].mask[[data['x_sea_water_velocity']>.08]] = True b = ReaderBlock(data, interpolation_horizontal='linearND') env, prof = b.interpolate(x, y, z, variables, profiles=['x_sea_water_velocity'], profiles_depth=[-30, 0]) self.assertAlmostEqual(env['x_sea_water_velocity'][10], 0.056, 2) self.assertAlmostEqual(prof['x_sea_water_velocity'][5,48], -0.090, 2)
def test_linearNDFast(self): """Test interpolation.""" reader = reader_ROMS_native.Reader(o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc') reader.buffer=3 num_points = 50 np.random.seed(0) # To get the same random numbers each time lons = np.random.uniform(14, 15, num_points) lats = np.random.uniform(68, 68.4, num_points) z = np.random.uniform(-20, 0, num_points) x, y = reader.lonlat2xy(lons, lats) #z=None variables = ['x_sea_water_velocity'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) b = ReaderBlock(data.copy(), interpolation_horizontal='linearNDFast') x2 = np.random.uniform(x.min(), x.max(), num_points) y2 = np.random.uniform(y.min(), y.max(), num_points) z2 = np.random.uniform(-20, 0, num_points) self.assertTrue(b.covers_positions(x, y, z)) self.assertTrue(b.covers_positions(x2, y2, z2)) # Check that there are holes in the arrays of the ReaderBlock self.assertEqual( np.sum(~np.isfinite(b.data_dict['x_sea_water_velocity'])), 1001) # Check that LinearNDFast interpolation gives a real value env, prof = b.interpolate(x2, y2, z2, variables, profiles=variables, profiles_depth=[-30, 0]) self.assertEqual( np.sum(~np.isfinite(env['x_sea_water_velocity'])), 0) # Check that the arrays of the ReaderBlock have been filled in self.assertEqual( np.sum(~np.isfinite(b.data_dict['x_sea_water_velocity'])), 0) # Check that nearest interpolation contains some NaN values b2 = ReaderBlock(data.copy(), interpolation_horizontal='nearest') env, prof = b2.interpolate(x2, y2, z2, variables, profiles=variables, profiles_depth=[-30, 0]) self.assertEqual( np.sum(~np.isfinite(env['x_sea_water_velocity'])), 31)
def test_zNone(self): d = {} d['x'] = np.arange(5) d['y'] = np.arange(7) d['z'] = 0 d['time'] = None d['var'] = np.random.rand(5, 6) rb = ReaderBlock(d) z = None i, p = rb.interpolate(np.array([1, 2]), np.array([2, 3]), z, 'var') self.assertTrue(i['var'][0] > 0)
def test_zNone(self): d = {} d['x'] = np.arange(5) d['y'] = np.arange(7) d['z'] = 0 d['time'] = None d['var'] = np.random.rand(5,6) rb = ReaderBlock(d) z = None i,p = rb.interpolate(np.array([1, 2]), np.array([2, 3]), z, 'var') self.assertTrue(i['var'][0] > 0)
v = reader_arctic.get_variables(variables, time=t, x=x, y=y, z=z, block=True) time_spent = datetime.now() - start_time print '%6.2f seconds on this machine' % time_spent.total_seconds() print '--------------------------------------------------------' print 'Test 3: Interpolating 3D arrays onto particle positions' print ' 2.5 seconds on reference machine.' start_time = datetime.now() b = ReaderBlock(v, interpolation_horizontal='linearND') num_points = 10000 x = np.random.uniform(reader_arctic.xmin, reader_arctic.xmax, num_points) y = np.random.uniform(reader_arctic.ymin, reader_arctic.ymax, num_points) z = np.random.uniform(-200, 0, num_points) env, prof = b.interpolate(x, y, z, variables, profiles=['sea_water_temperature'], profiles_depth=[-30, 0]) time_spent = datetime.now() - start_time print '%6.1f seconds on this machine' % time_spent.total_seconds() print '--------------------------------------------------------' print 'Test 4: Vertical mixing with 50 elements and 7200 cycles (CPU-heavy)' print ' 10.0 seconds on reference machine.' reader_arctic.buffer=10 reader_arctic.verticalbuffer=1 o = OpenOil3D(loglevel=50) # Quiet o.add_reader(reader_arctic) o.fallback_values['x_wind'] = 10 o.set_config('turbulentmixing:timestep', 1) o.seed_elements(lon=15, lat=72, number=50, radius=10000,
v = reader_arctic.get_variables(variables, time=t, x=x, y=y, z=z) time_spent = datetime.now() - start_time print('%6.2f seconds on this machine' % time_spent.total_seconds()) print('--------------------------------------------------------') print('Test 3: Interpolating 3D arrays onto particle positions') print(' 2.5 seconds on reference machine.') start_time = datetime.now() b = ReaderBlock(v, interpolation_horizontal='linearND') num_points = 10000 x = np.random.uniform(reader_arctic.xmin, reader_arctic.xmax, num_points) y = np.random.uniform(reader_arctic.ymin, reader_arctic.ymax, num_points) z = np.random.uniform(-200, 0, num_points) env, prof = b.interpolate(x, y, z, variables, profiles=['sea_water_temperature'], profiles_depth=[-30, 0]) time_spent = datetime.now() - start_time print('%6.1f seconds on this machine' % time_spent.total_seconds()) print('--------------------------------------------------------') print('Test 4: Vertical mixing with 50 elements and 7200 cycles (CPU-heavy)') print(' 10.0 seconds on reference machine.') reader_arctic.buffer = 10 reader_arctic.verticalbuffer = 1 o = OpenOil(loglevel=50) # Quiet o.add_reader(reader_arctic) o.set_config('environment:fallback:x_wind', 10) o.set_config('environment:fallback:y_wind', 0) o.set_config('vertical_mixing:timestep', 1)