def test_repeated(self): """Check that block can be used for interpolation to several sets of positions""" reader = reader_netCDF_CF_generic.Reader(script_folder + '/../test_data/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_interpolation_3dArrays(self): """Test interpolation.""" reader = reader_netCDF_CF_generic.Reader(script_folder + '/../test_data/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.0750191849228) self.assertAlmostEqual(prof['sea_water_temperature'][0,11], 7.5499997138977051) self.assertAlmostEqual(prof['sea_water_temperature'][-1,11], 8.38000011444) self.assertEqual(prof['z'][-1], b.z[-1])
def test_reader_netcdf(self): """Check reader functionality.""" reader1 = reader_netCDF_CF_generic.Reader(script_folder + '/../test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc') reader2 = reader_ROMS_native.Reader(script_folder + '/../test_data/2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc') readers = [reader1, reader2] for r in readers: print r # Make four points: # 1) outside lower left, 2) lower left, 3) center of domain # 4) outside upper right # and assure that only 2) and 3) are marked as covered # Upper right is skipped, as lonlat2xy may lie slightly outside x = np.array([r.xmin - r.delta_x, r.xmin, (r.xmin + r.xmax)/2, r.xmax + r.delta_x]) y = np.array([r.ymin - r.delta_y, r.ymin, (r.ymin + r.ymax)/2, r.ymax + r.delta_y]) lons, lats = r.xy2lonlat(x, y) covered = r.covers_positions(lons, lats, 0) self.assertEqual(covered.tolist(), [1, 2]) self.assertTrue(r.covers_time(r.start_time)) self.assertFalse(r.covers_time(r.start_time - r.time_step)) self.assertFalse(r.proj.is_latlong())
def test_vertical_profiles(self): norkyst3d = reader_netCDF_CF_generic.Reader(script_folder + '/../test_data/14Jan2016_NorKyst_z_3d/NorKyst-800m_ZDEPTHS_his_00_3Dsubset.nc') lon = np.array([4.73]) lat = np.array([62.35]) variables = ['x_sea_water_velocity', 'x_sea_water_velocity', 'sea_water_temperature'] x,y = norkyst3d.lonlat2xy(lon, lat) data = norkyst3d.get_variables(variables, time=norkyst3d.start_time, x=x, y=y, z=[0, -100], block=True) self.assertEqual(data['z'][4], -25) self.assertEqual(data['z'][4], -25) self.assertAlmostEqual(data['sea_water_temperature'][:,0,0][7], 9.220000267028809)
def test_vertical_interpolation(self): norkyst3d = reader_netCDF_CF_generic.Reader(script_folder + '/../test_data/14Jan2016_NorKyst_z_3d/NorKyst-800m_ZDEPTHS_his_00_3Dsubset.nc') lon = np.array([4.73, 4.75]) lat = np.array([62.35, 62.30]) z = np.array([0, -33]) variables = ['x_sea_water_velocity', 'x_sea_water_velocity', 'sea_water_temperature'] # Call get_variables_interpolated which interpolates both in # space (horizontally, vertically) and then in time data, profiles = norkyst3d.get_variables_interpolated( variables, profiles=['sea_water_temperature'], profiles_depth = [-100, 0], time = norkyst3d.start_time + timedelta(seconds=900), lon=lon, lat=lat, z=z, block=True) # Check surface value self.assertEqual(data['sea_water_temperature'][0], profiles['sea_water_temperature'][0,0]) # Check interpolated temperature at 33 m depth self.assertAlmostEqual(data['sea_water_temperature'][1], 8.2648999309539786)