def test_interpolation_wrapper(self): """Interpolation library works for linear function """ # Create test data lon_ul = 100 # Longitude of upper left corner lat_ul = 10 # Latitude of upper left corner numlon = 8 # Number of longitudes numlat = 5 # Number of latitudes dlon = 1 dlat = -1 # Define array where latitudes are rows and longitude columns A = numpy.zeros((numlat, numlon)) # Establish coordinates for lower left corner lat_ll = lat_ul - numlat lon_ll = lon_ul # Define pixel centers along each direction longitudes = numpy.linspace(lon_ll + 0.5, lon_ll + numlon - 0.5, numlon) latitudes = numpy.linspace(lat_ll + 0.5, lat_ll + numlat - 0.5, numlat) # Define raster with latitudes going bottom-up (south to north). # Longitudes go left-right (west to east) for i in range(numlat): for j in range(numlon): A[numlat - 1 - i, j] = linear_function(longitudes[j], latitudes[i]) # Create bilinear interpolation function F = raster_spline(longitudes, latitudes, A) # Test first that original points are reproduced correctly for i, eta in enumerate(latitudes): for j, xi in enumerate(longitudes): assert numpy.allclose(F(xi, eta), linear_function(xi, eta), rtol=1e-12, atol=1e-12) # Then test that genuinly interpolated points are correct xis = numpy.linspace(lon_ll + 1, lon_ll + numlon - 1, 10 * numlon) etas = numpy.linspace(lat_ll + 1, lat_ll + numlat - 1, 10 * numlat) for xi in xis: for eta in etas: assert numpy.allclose(F(xi, eta), linear_function(xi, eta), rtol=1e-12, atol=1e-12)
def test_riab_interpolation(self): """Interpolation using Raster and Vector objects """ # Create test data lon_ul = 100 # Longitude of upper left corner lat_ul = 10 # Latitude of upper left corner numlon = 8 # Number of longitudes numlat = 5 # Number of latitudes dlon = 1 dlat = -1 # Define array where latitudes are rows and longitude columns A = numpy.zeros((numlat, numlon)) # Establish coordinates for lower left corner lat_ll = lat_ul - numlat lon_ll = lon_ul # Define pixel centers along each direction longitudes = numpy.linspace(lon_ll + 0.5, lon_ll + numlon - 0.5, numlon) latitudes = numpy.linspace(lat_ll + 0.5, lat_ll + numlat - 0.5, numlat) # Define raster with latitudes going bottom-up (south to north). # Longitudes go left-right (west to east) for i in range(numlat): for j in range(numlon): A[numlat - 1 - i, j] = linear_function(longitudes[j], latitudes[i]) # Create bilinear interpolation function F = raster_spline(longitudes, latitudes, A) # Write array to a raster file geotransform = (lon_ul, dlon, 0, lat_ul, 0, dlat) projection = ('GEOGCS["GCS_WGS_1984",' 'DATUM["WGS_1984",' 'SPHEROID["WGS_1984",6378137.0,298.257223563]],' 'PRIMEM["Greenwich",0.0],' 'UNIT["Degree",0.0174532925199433]]') raster_filename = unique_filename(suffix='.tif') write_raster_data(A, projection, geotransform, raster_filename) # Write test interpolation point to a vector file coordinates = [] for xi in longitudes: for eta in latitudes: coordinates.append((xi, eta)) vector_filename = unique_filename(suffix='.shp') write_point_data(data=None, projection=projection, geometry=coordinates, filename=vector_filename) # Read both datasets back in R = read_layer(raster_filename) V = read_layer(vector_filename) # Then test that axes and data returned by R are correct x, y = R.get_geometry() msg = 'X axes was %s, should have been %s' % (longitudes, x) assert numpy.allclose(longitudes, x), msg msg = 'Y axes was %s, should have been %s' % (latitudes, y) assert numpy.allclose(latitudes, y), msg AA = R.get_data() msg = 'Raster data was %s, should have been %s' % (AA, A) assert numpy.allclose(AA, A), msg # Test riab's interpolation function I = R.interpolate(V, name='value') Icoordinates = I.get_geometry() Iattributes = I.get_data() assert numpy.allclose(Icoordinates, coordinates) # Test that interpolated points are correct for i, (xi, eta) in enumerate(Icoordinates): z = Iattributes[i]['value'] #print xi, eta, z, linear_function(xi, eta) assert numpy.allclose(z, linear_function(xi, eta), rtol=1e-12) # FIXME (Ole): Need test for values outside grid. # They should be NaN or something # Cleanup # FIXME (Ole): Shape files are a collection of files. How to remove? os.remove(vector_filename)