Beispiel #1
0
def test_which_fails():
    """
    which should fail with a FileNotFoundError.
    """
    bogus_file = unique_name()
    with pytest.raises(FileNotFoundError):
        which(bogus_file)
Beispiel #2
0
def test_which_fails():
    """
    Make sure `which` will fail with a FileNotFoundError.
    """
    bogus_file = unique_name()
    with pytest.raises(FileNotFoundError):
        which(bogus_file)
    with pytest.raises(FileNotFoundError):
        which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"])
Beispiel #3
0
def test_grdtrack_without_outfile_setting():
    """
    Run grdtrack by not passing in outfile parameter setting.
    """
    csvfile = which("@ridge.txt", download="c")
    ncfile = which("@earth_relief_01d", download="a")

    output = grdtrack(points=csvfile, grid=ncfile)
    npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245])

    return output
Beispiel #4
0
def test_plot_shapefile():
    """
    Make sure that plot works for shapefile.

    See https://github.com/GenericMappingTools/pygmt/issues/1616.
    """
    datasets = [
        "@RidgeTest" + suffix for suffix in [".shp", ".shx", ".dbf", ".prj"]
    ]
    which(fname=datasets, download="a")
    fig = Figure()
    fig.plot(data="@RidgeTest.shp", pen="1p")
    return fig
Beispiel #5
0
def test_grdtrack_without_outfile_setting(dataarray):
    """
    Run grdtrack by not passing in outfile parameter setting.
    """
    csvfile = which("@ridge.txt", download="c")

    with pytest.raises(GMTInvalidInput):
        grdtrack(points=csvfile, grid=dataarray)
Beispiel #6
0
def test_which():
    """
    Make sure `which` returns file paths for @files correctly without errors.
    """
    for fname in ["tut_quakes.ngdc", "tut_bathy.nc"]:
        cached_file = which(f"@{fname}", download="c")
        assert os.path.exists(cached_file)
        assert os.path.basename(cached_file) == fname
Beispiel #7
0
def test_grdview_grid_file_with_region_subset(region):
    """
    Run grdview by passing in a grid filename, and cropping it to a region.
    """
    gridfile = which("@earth_relief_01d_g", download="a")

    fig = Figure()
    fig.grdview(grid=gridfile, region=region)
    return fig
Beispiel #8
0
def test_accessor_pixel_geographic():
    """
    Check that a grid returns a registration value of 1 when Pixel registered,
    and a gtype value of 0 when using Cartesian coordinates.
    """
    fname = which(fname="@earth_relief_01d_p", download="a")
    grid = xr.open_dataarray(fname, engine="netcdf4")
    assert grid.gmt.registration == 1  # pixel registration
    assert grid.gmt.gtype == 1  # geographic coordinate type
Beispiel #9
0
def test_accessor_gridline_cartesian():
    """
    Check that a grid returns a registration value of 0 when Gridline
    registered, and a gtype value of 1 when using Geographic coordinates.
    """
    fname = which(fname="@test.dat.nc", download="a")
    grid = xr.open_dataarray(fname)
    assert grid.gmt.registration == 0  # gridline registration
    assert grid.gmt.gtype == 0  # cartesian coordinate type
Beispiel #10
0
def test_grdtrack_input_csvfile_and_ncfile():
    """
    Run grdtrack by passing in a csvfile and netcdf file as inputs.
    """
    csvfile = which("@ridge.txt", download="c")
    ncfile = which("@earth_relief_01d", download="a")

    try:
        output = grdtrack(points=csvfile, grid=ncfile, outfile=TEMP_TRACK)
        assert output is None  # check that output is None since outfile is set
        assert os.path.exists(path=TEMP_TRACK)  # check that outfile exists at path

        track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">")
        npt.assert_allclose(track.iloc[0], [-32.2971, 37.4118, -1939.748245])
    finally:
        os.remove(path=TEMP_TRACK)

    return output
Beispiel #11
0
def test_which_multiple():
    """
    Make sure `which` returns file paths for multiple @files correctly.
    """
    filenames = ["ridge.txt", "tut_ship.xyz"]
    cached_files = which(fname=[f"@{fname}" for fname in filenames],
                         download="c")
    for cached_file in cached_files:
        assert os.path.exists(cached_file)
        assert os.path.basename(cached_file) in filenames
Beispiel #12
0
def test_surface_input_file():
    """
    Run surface by passing in a filename
    """
    fname = which("@tut_ship.xyz", download="c")
    output = surface(data=fname, spacing="5m", 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
Beispiel #13
0
def fixture_dataframe():
    """
    Load the table data from the sample bathymetry dataset.
    """
    fname = which("@Table_5_11_mean.xyz", download="c")
    return pd.read_csv(fname,
                       sep=r"\s+",
                       header=None,
                       names=["x", "y", "z"],
                       skiprows=1)[:10]
Beispiel #14
0
def test_grdtrack_input_dataframe_and_ncfile():
    """
    Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs.
    """
    dataframe = load_ocean_ridge_points()
    ncfile = which("@earth_relief_01d", download="a")

    output = grdtrack(points=dataframe, grid=ncfile, newcolname="bathymetry")
    assert isinstance(output, pd.DataFrame)
    assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"]
    npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245])

    return output
Beispiel #15
0
def test_grdtrack_input_csvfile_and_dataarray(dataarray):
    """
    Run grdtrack by passing in a csvfile and xarray.DataArray as inputs.
    """
    csvfile = which("@ridge.txt", download="c")

    try:
        output = grdtrack(points=csvfile, grid=dataarray, outfile=TEMP_TRACK)
        assert output is None  # check that output is None since outfile is set
        assert os.path.exists(path=TEMP_TRACK)  # check that outfile exists at path

        track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">")
        npt.assert_allclose(track.iloc[0], [-110.9536, -42.2489, -2790.488422])
    finally:
        os.remove(path=TEMP_TRACK)

    return output
Beispiel #16
0
def test_accessor_sliced_datacube():
    """
    Check that a 2D grid which is sliced from an n-dimensional datacube works
    with accessor methods.

    This is a regression test for
    https://github.com/GenericMappingTools/pygmt/issues/1578.
    """
    try:
        fname = which(
            "https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc",
            download="u",
        )
        with xr.open_dataset(fname) as dataset:
            grid = dataset.sel(level=500, month=1, drop=True).z

        assert grid.gmt.registration == 0  # gridline registration
        assert grid.gmt.gtype == 1  # geographic coordinate type
    finally:
        os.remove(fname)
Beispiel #17
0
def fixture_csvfile():
    """
    Load the csvfile.
    """
    return which("@ridge.txt", download="c")
Beispiel #18
0
def fixture_ncfile():
    """
    Load the ncfile.
    """
    return which("@earth_relief_01d", download="a")