def test_grdlandmask_fails(): """ Check that grdlandmask fails correctly when region and spacing are not given. """ with pytest.raises(GMTInvalidInput): grdlandmask()
def test_grdlandmask_no_outgrid(): """ Test grdlandmask with no set outgrid. """ temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5]) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == 1 # Geographic grid assert temp_grid.gmt.registration == 0 # Pixel registration assert temp_grid.min() == 0 assert temp_grid.max() == 1
def test_grdlandmask_no_outgrid(expected_grid): """ Test grdlandmask with no set outgrid. """ result = grdlandmask(spacing=1, region=[125, 130, 30, 35]) # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == 1 # Geographic grid assert result.gmt.registration == 0 # Gridline registration # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid)
def test_grdlandmask_outgrid(expected_grid): """ Creates a grid land mask with an outgrid argument. """ with GMTTempFile(suffix=".nc") as tmpfile: result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35]) assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists temp_grid = load_dataarray(tmpfile.name) xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
def test_grdlandmask_outgrid(): """ Creates a grid land mask with an outgrid argument. """ with GMTTempFile(suffix=".nc") as tmpfile: result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[-5, 5, -5, 5]) assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists result = (grdinfo(grid=tmpfile.name, force_scan=0, per_column="n").strip().split()) assert result == [ "-5", "5", "-5", "5", "0", "1", "1", "1", "11", "11", "0", "1" ]
def filter_vectors_to_land_only(region, elon, nlat, e, n): maskfile = pygmt.grdlandmask(region=region, spacing='10m', resolution='i'); points, newelon, newnlat, newe, newn = [], [], [], [], []; for i in range(len(elon)): points.append(np.array([elon[i], nlat[i]])); # build np array of points points = np.array(points); if len(points) == 0: return [], [], [], []; values = pygmt.grdtrack(points, maskfile); # values is a pandas DataFrame for i, item in enumerate(values[2]): # column 2 is the grdtrack output if item > 0.8: # if grdtrack gives something on land newelon.append(elon[i]); newnlat.append(nlat[i]); newe.append(e[i]); newn.append(n[i]); return newelon, newnlat, newe, newn;
the ``maskvalues`` parameter. """ import pygmt fig = pygmt.Figure() # Define region of interest region = [-65, -40, -40, -20] # Assign a value of 0 for all water masses and a value of 1 for all land # masses. # Use shoreline data with (l)ow resolution and set the grid spacing to # 5 arc minute in x and y direction. grid = pygmt.grdlandmask(region=region, spacing="5m", maskvalues=[0, 1], resolution="l") # Plot clipped grid fig.basemap(region=region, projection="M12c", frame=True) # Define a colormap to be used for two categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), # use color_model="+cwater,land" to write the discrete color palette # "batlow" in categorical format and add water/land as annotations for the # colorbar. pygmt.makecpt(cmap="batlow", series=(0, 1, 1), color_model="+cwater,land") fig.grdimage(grid=grid, cmap=True) fig.colorbar(position="JMR+o0.5c/0c+w8c")