def test_xyz2grd_input_df(ship_data): """ Run xyz2grd by passing in a data frame. """ output = xyz2grd(data=ship_data, spacing=5, region=[245, 255, 20, 30]) assert isinstance(output, xr.DataArray) assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type return output
def test_xyz2grd_input_file(): """ Run xyz2grd by passing in a filename. """ output = xyz2grd(data="@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30]) assert isinstance(output, xr.DataArray) assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type return output
def test_xyz2grd_input_array(array_func, ship_data, expected_grid): """ Run xyz2grd by passing in an xarray datset or numpy array. """ output = xyz2grd(data=array_func(ship_data), spacing=5, region=[245, 255, 20, 30]) assert isinstance(output, xr.DataArray) assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type xr.testing.assert_allclose(a=output, b=expected_grid)
def test_xyz2grd_input_array_file_out(ship_data): """ Run xyz2grd by passing in a numpy array and set an outgrid file. """ with GMTTempFile(suffix=".nc") as tmpfile: result = xyz2grd( data=np.array(ship_data), spacing=5, region=[245, 255, 20, 30], outgrid=tmpfile.name, ) assert result is None # return value is None assert os.path.exists(path=tmpfile.name) result = grdinfo(tmpfile.name, per_column=True).strip() assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0"
def test_xyz2grd_input_array_file_out(ship_data, expected_grid): """ Run xyz2grd by passing in a numpy array and set an outgrid file. """ with GMTTempFile(suffix=".nc") as tmpfile: result = xyz2grd( data=ship_data, spacing=5, region=[245, 255, 20, 30], outgrid=tmpfile.name, ) assert result is None # return value is None assert os.path.exists(path=tmpfile.name) temp_grid = load_dataarray(tmpfile.name) xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
def test_xyz2grd_missing_region_spacing(ship_data): """ Test xyz2grd raise an exception if region or spacing is missing. """ with pytest.raises(GMTInvalidInput): xyz2grd(data=ship_data) with pytest.raises(GMTInvalidInput): xyz2grd(data=ship_data, region=[245, 255, 20, 30]) with pytest.raises(GMTInvalidInput): xyz2grd(data=ship_data, spacing=5)
data = pygmt.datasets.load_japan_quakes() # Select only needed columns data = data[["longitude", "latitude", "depth_km"]] # Set the region for the plot region = [130, 152.5, 32.5, 52.5] # Define spacing in x and y direction (150 by 150 minute blocks) spacing = "150m" fig = pygmt.Figure() # Calculate mean depth in km from all events within 150x150 minute # bins using blockmean df = pygmt.blockmean(data=data, region=region, spacing=spacing) # convert to grid grd = pygmt.xyz2grd(data=df, region=region, spacing=spacing) fig.grdimage( grid=grd, region=region, frame=["af", '+t"Mean earthquake depth inside each block"'], cmap="batlow", ) # plot slightly transparent landmasses on top fig.coast(land="darkgray", transparency=40) # plot original data points fig.plot(x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black")