def test_grdtrack_without_newcolname_setting(dataarray): """ Run grdtrack by not passing in newcolname parameter setting. """ dataframe = load_ocean_ridge_points() with pytest.raises(GMTInvalidInput): grdtrack(points=dataframe, grid=dataarray)
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)
def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): """ Run grdtrack using points input that is not a pandas.DataFrame (matrix) or file. """ invalid_points = dataframe.longitude.to_xarray() assert data_kind(invalid_points) == "grid" with pytest.raises(GMTInvalidInput): grdtrack(points=invalid_points, grid=dataarray, newcolname="bathymetry")
def test_grdtrack_wrong_kind_of_grid_input(dataarray, dataframe): """ Run grdtrack using grid input that is not as xarray.DataArray (grid) or file. """ invalid_grid = dataarray.to_dataset() assert data_kind(invalid_grid) == "matrix" with pytest.raises(GMTInvalidInput): grdtrack(points=dataframe, grid=invalid_grid, newcolname="bathymetry")
def gridtrack(Longitude='', Latitude='', geotif='', geotif_name='', geotif_epsg=''): import pygmt import rioxarray import pandas as pd Longitude = Longitude Latitude = Latitude geotif = geotif geotif_name = geotif_name geotif_epsg = geotif_epsg df = pd.DataFrame(Longitude) df['Latitude'] = pd.DataFrame(Latitude) df.columns = ['Longitude', 'Latitude'] rds = rioxarray.open_rasterio(geotif) rds.rio.set_crs(geotif_epsg) rds = rds.rio.reproject("epsg:4326") rds = rds.squeeze('band') rds = rds.astype(float) df = pygmt.grdtrack(df, rds, newcolname=geotif_name) raster_vals = df[geotif_name].values return raster_vals del df, Latitude, Longitude del rds
def get_crustal_thickness_points(points, grid=None, top_name='CRUST1-TOP', bottom_name='CRUST3-BOTTOM'): # if no grid is provided, we take the layer thickness from litho1.0 if not grid: import litho1pt0 as litho ptlats = np.array([pt.to_lat_lon()[0] for pt in points]) ptlons = np.array([pt.to_lat_lon()[1] for pt in points]) top_depth = litho.layer_depth(ptlats, ptlons, top_name) bottom_depth = litho.layer_depth(ptlats, ptlons, bottom_name) layer_thickness = bottom_depth - top_depth return layer_thickness # if a grid is provided, sample using grdtrack else: ptlats = np.array([pt.to_lat_lon()[0] for pt in points]) ptlons = np.array([pt.to_lat_lon()[1] for pt in points]) layer_thickness = pygmt.grdtrack(points=np.vstack((ptlons, ptlats)), grid=grid, newcolname='z') return np.array(layer_thickness['z'])
def test_grdtrack_input_csvfile_and_ncfile_to_dataframe(expected_array): """ Run grdtrack by passing in a csv file and netcdf file as inputs with a pandas.DataFrame output. """ output = grdtrack(points=POINTS_DATA, grid="@static_earth_relief.nc") assert isinstance(output, pd.DataFrame) npt.assert_allclose(np.array(output), expected_array)
def test_grdtrack_without_outfile_setting(csvfile, ncfile): """ Run grdtrack by not passing in outfile parameter setting. """ output = grdtrack(points=csvfile, grid=ncfile) npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245]) return output
def test_grdtrack_input_dataframe_and_ncfile(dataframe, expected_array): """ Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs. """ output = grdtrack(points=dataframe, grid="@static_earth_relief.nc", newcolname="bathymetry") assert isinstance(output, pd.DataFrame) assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] npt.assert_allclose(np.array(output), expected_array)
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
def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe): """ Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as inputs. """ output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry") assert isinstance(output, pd.DataFrame) assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] npt.assert_allclose(output.iloc[0], [-110.9536, -42.2489, -2797.394987]) return output
def test_grdtrack_input_dataframe_and_ncfile(dataframe, ncfile): """ Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs. """ 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
def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_array): """ Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as inputs. """ output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry") assert isinstance(output, pd.DataFrame) assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] npt.assert_allclose(np.array(output), expected_array)
def visualize_grid_in_3D( df_lake: pd.DataFrame, azimuth: float, elevation: float, context ): """ Create 3D plots of gridded ice surface elevation over time. """ # Get 3D grid_region (xmin/xmax/ymin/ymax/zmin/zmax), # and calculate normalized z-values as Elevation delta relative to Cycle 3 z_limits: tuple = ( float(context.ds_lake.z.min()), float(context.ds_lake.z.max()), ) # original z limits grid_region: tuple = context.region.bounds() + z_limits ds_lake_diff: xr.Dataset = ( context.ds_lake - context.ds_lake.sel(cycle_number=context.cycles[0]).z ) z_diff_limits: tuple = (float(ds_lake_diff.z.min()), float(ds_lake_diff.z.max())) diff_grid_region: np.ndarray = np.append(arr=grid_region[:4], values=z_diff_limits) print(f"Elevation limits are: {z_limits}") # 3D plots of gridded ice surface elevation over time for cycle in tqdm.tqdm(iterable=context.cycles): time_nsec: pd.Timestamp = df_lake[f"utc_time_{cycle}"].mean() time_sec: str = np.datetime_as_string(arr=time_nsec.to_datetime64(), unit="s") grid = ( f"figures/{context.placename}/h_corr_{context.placename}_cycle_{cycle}.nc" ) points = pd.DataFrame( data=np.vstack(context.lake.geometry.boundary.coords.xy).T, columns=("x", "y"), ) outline_points = pygmt.grdtrack(points=points, grid=grid, newcolname="z") outline_points["z"] = outline_points.z.fillna(value=outline_points.z.median()) fig = deepicedrain.plot_icesurface( grid=grid, # ds_lake.sel(cycle_number=cycle).z grid_region=grid_region, diff_grid=ds_lake_diff.sel(cycle_number=cycle).z, diff_grid_region=diff_grid_region, track_points=df_lake[["x", "y", f"h_corr_{cycle}"]].dropna().to_numpy(), outline_points=outline_points, azimuth=azimuth, elevation=elevation, title=f"{context.region.name} at Cycle {cycle} ({time_sec})", ) fig.savefig( f"figures/{context.placename}/dsm_{context.placename}_cycle_{cycle}.png" ) return fig
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
def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array): """ Run grdtrack by passing in a csvfile and xarray.DataArray as inputs. """ with GMTTempFile() as tmpfile: output = grdtrack(points=POINTS_DATA, grid=dataarray, outfile=tmpfile.name) assert output is None # check that output is None since outfile is set assert os.path.exists( path=tmpfile.name) # check that outfile exists at path output = np.loadtxt(tmpfile.name) npt.assert_allclose(np.array(output), expected_array)
def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile): """ Run grdtrack by passing in a csvfile and xarray.DataArray as inputs. """ 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, -2797.394987]) finally: os.remove(path=TEMP_TRACK) return output
def test_grdtrack_input_csvfile_and_ncfile(csvfile, ncfile): """ Run grdtrack by passing in a csvfile and netcdf file as inputs. """ 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
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;
# always_xy=True, # ) # # start_xy = reprj_func.transform(xx=start_lonlat[0], yy=start_lonlat[1]) # stop_xy = reprj_func.transform(xx=stop_lonlat[0], yy=stop_lonlat[1]) # start_stop_inc = f"{start_xy[0]}/{start_xy[1]}/{stop_xy[0]}/{stop_xy[1]}/+i125" # print(start_stop_inc) # %% elevpoints = {} # Get elevation (z) values for name, grid in gridDict.items(): elevpoints[name] = gmt.grdtrack( points=oibpoints[["x", "y"]], grid=grid, newcolname="elevation", R="/".join(str(r) for r in region), # E="-1573985/-470866/-993375/-464996+i250", ) elevpoints[name] = elevpoints[name].dropna() elevpoints[name].x = elevpoints[name].x.astype(float) print(len(elevpoints[name]), name) # Get roughness values for name, grid in roughDict.items(): roughness = gmt.grdtrack( points=oibpoints[["x", "y"]], grid=grid, newcolname="roughness", R="/".join(str(r) for r in region), # E=start_stop_inc, ).roughness
# - DeepBedMap3 grid (predicted from our [Super Resolution Generative Adversarial Network model](/srgan_train.ipynb)) # - CubicBedMap grid (interpolated from BEDMAP2 using a [bicubic spline algorithm](http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.rescale)) # - Synthetic High Res grid (created by [Graham et al. 2017](https://doi.org/10.5194/essd-9-267-2017)) # # References: # # - Wessel, P., Smith, W. H. F., Scharroo, R., Luis, J., & Wobbe, F. (2013). Generic Mapping Tools: Improved Version Released. Eos, Transactions American Geophysical Union, 94(45), 409–410. https://doi.org/10.1002/2013EO450001 # - Wessel, P. (2010). Tools for analyzing intersecting tracks: The x2sys package. Computers & Geosciences, 36(3), 348–354. https://doi.org/10.1016/j.cageo.2009.05.009 # %% tracks = [data_prep.ascii_to_xyz(pipeline_file=f"{pf}.json") for pf in test_filepaths] points: pd.DataFrame = pd.concat(objs=tracks) # concatenate all tracks into one table # %% df_groundtruth = gmt.grdtrack( points=points, grid=groundtruth, newcolname="z_interpolated" ) # df_deepbedmap3 = gmt.grdtrack( # points=points, grid=deepbedmap3_grid, newcolname="z_interpolated" # ) df_deepbedmap3 = gmt.grdtrack( points=points, grid="model/deepbedmap3.nc", newcolname="z_interpolated" ) df_cubicbedmap = gmt.grdtrack( points=points, grid="model/cubicbedmap.nc", newcolname="z_interpolated" ) df_synthetichr = gmt.grdtrack( points=points, grid="model/synthetichr.nc", newcolname="z_interpolated" ) # %% [markdown]
number_of_samples = 100000 filin = '/home/sberton2/tmp/LDAM_8.GRD' # '/home/sberton2/Works/NASA/Mercury_tides/aux/HDEM_64.GRD' # rng = np.random.default_rng() lon = rng.random((number_of_samples)) * 360. # if grd lon is [0,360) lat = (rng.random( (number_of_samples)) * 2 - 1) * 90. # if grd lat is [-90,90) if method == 'xarray': import xarray as xr import pyproj if filin.split('.')[-1] == 'GRD': # to read grd/netcdf files z = get_demz_grd(filin, lon, lat) elif filin.split('.')[-1] == 'TIF': # to read geotiffs usgs z = np.squeeze(get_demz_tiff(filin, lon, lat)) out = pd.DataFrame([lon, lat, z], index=['lon', 'lat', 'z']).T elif method == 'pygmt': import pygmt # needs GMT 6.1.1 installed, plus linking of GMTdir/lib64/libgmt.so to some general xovutil dir (see bottom of https://www.pygmt.org/dev/install.html) points = pd.DataFrame([lon, lat], index=['lon', 'lat']).T out = pygmt.grdtrack(points, filin, newcolname='z') print(out) end = time.time() print("## Reading/interpolation of", number_of_samples, "samples finished after", str(np.round(end - start, 2)), "sec!") exit()
diff_grid_region: np.ndarray = np.append(arr=grid_region[:4], values=z_diff_limits) print(f"Elevation limits are: {z_limits}") # %% # 3D plots of gridded ice surface elevation over time for cycle in tqdm.tqdm(iterable=cycles): time_nsec: pd.Timestamp = df_lake[f"utc_time_{cycle}"].to_pandas().mean() time_sec: str = np.datetime_as_string(arr=time_nsec.to_datetime64(), unit="s") grid = f"figures/{placename}/h_corr_{placename}_cycle_{cycle}.nc" points = pd.DataFrame(data=np.vstack(lake.geometry.boundary.coords.xy).T, columns=("x", "y")) outline_points = pygmt.grdtrack(points=points, grid=grid, newcolname="z") outline_points["z"] = outline_points.z.fillna( value=outline_points.z.median()) fig = deepicedrain.plot_icesurface( grid=grid, # ds_lake.sel(cycle_number=cycle).z grid_region=grid_region, diff_grid=ds_lake_diff.sel(cycle_number=cycle).z, diff_grid_region=diff_grid_region, track_points=df_lake[["x", "y", f"h_corr_{cycle}"]].dropna().as_matrix(), outline_points=outline_points, azimuth=157.5, # 202.5 # 270 elevation=45, # 60 title=f"{region.name} at Cycle {cycle} ({time_sec})", )
I=250, G=f"Models/{bedname.lower()}_{z_attribute}.nc", ) # %% # Gridded plots, and transect plot of friction/velocity/rheologyB grid = xr.open_dataarray(f"Models/{bedname.lower()}_{z_attribute}.nc") pointXY = collections.namedtuple(typename="pointXY", field_names="x y") pointA = pointXY(x=-1590_000, y=-99_000) pointB = pointXY(x=-1580_000, y=-255_000) points = pd.DataFrame( data=np.linspace(start=pointA, stop=pointB, num=50), columns=["x", "y"], ) transect = pygmt.grdtrack(points=points, grid=grid, newcolname=z_attribute) fig = pygmt.Figure() # Plot 2D grid and transect line on map pygmt.makecpt( cmap="hawaii", series=[z.min(), z.max(), (z.max() - z.min()) / 10], reverse=True, continuous=True, ) with pygmt.config(FONT_TITLE="18p"): fig.grdimage( grid=grid, cmap=True, frame=["af", f'WSne+t"Mean {z_attribute}={grid.mean().item():.4e}"'], projection="X8c/12c",
reverse=True, continuous=True, ) with pygmt.clib.Session() as session: subplot = lambda args: session.call_module(module="subplot", args=args) # Begin subplot subplot(args="begin 2x3 -BWSne -Bxaf -Byaf -Fs10c/4c,14c -M0c/0.5c -SRl") # Plot transect line graph subplot(args=f"set 0,0") transectproj = "X32c/4c" _xyz = pd.concat( pygmt.grdtrack( points=points, grid=grid, newcolname=z_attr.varname, ) for grid in grids) fig.basemap( region=pygmt.info(table=_xyz[["x", z_attr.varname]], per_column=True, spacing=10), projection=transectproj, frame=[ "af", f'WSne+t"{z_attr.varname.title().replace("_", " ")} ({z_attr.symbol}) at Pine Island Glacier"', ], ) for bedname in ("DeepBedMap", "BedMachine"): grid = f"Models/{bedname.lower()}_{z_attr.varname}.nc" transect = pygmt.grdtrack(points=points,
def raster_topological_reconstruction(grid, topological_model, reconstruction_time, reverse=True, region=None, spacing=1.): """ Generate a reconstructed raster based on a topological reconstruction By default, the operation will use the input raster to determine the extent and sampling of the output raster (Which would work for global grids). Optionally, a different region and sampling for the output grid can be specified. """ coord_keys = [key for key in grid.coords.keys()] if 'lon' in coord_keys[0].lower(): latitude_key = 1 longitude_key = 0 elif 'x' in coord_keys[0].lower(): latitude_key = 1 longitude_key = 0 else: latitude_key = 0 longitude_key = 1 if region: coords = [('lat', np.arange(region[2], region[3] + spacing, spacing)), ('lon', np.arange(region[0], region[1] + spacing, spacing))] XX, YY = np.meshgrid( np.arange(region[0], region[1] + spacing, spacing), np.arange(region[2], region[3] + spacing, spacing)) else: coords = [('lat', grid.coords[coord_keys[latitude_key]].data), ('lon', grid.coords[coord_keys[longitude_key]].data)] XX, YY = np.meshgrid(grid.coords[coord_keys[longitude_key]].data, grid.coords[coord_keys[latitude_key]].data) geometry_points = [(lat, lon) for lat, lon in zip(YY.flatten(), XX.flatten())] if reverse: pts, valid_index = topological_reconstruction( topological_model, geometry_points, 0., initial_time=reconstruction_time, oldest_time=reconstruction_time, return_inactive_points=True, deactivate_points=False) res = pygmt.grdtrack(grid=grid, points=pd.DataFrame(data={ 'x': pts[1], 'y': pts[0] }), newcolname='z') x = XX.flatten()[valid_index] y = YY.flatten()[valid_index] resg = xyz2grd(x, y, np.array(res['z']), XX, YY) reconstructed_raster = xr.DataArray(resg, coords=coords, name='z') return reconstructed_raster else: print('Forward reconstruction not yet implemented')
:class:`pandas.DataFrame` table where the first two columns are x and y (or longitude and latitude). Note also that there is a ``newcolname`` parameter that will be used to name the new column of values sampled from the grid. Alternatively, a NetCDF file path can be passed to ``grid``. An ASCII file path can also be accepted for ``points``. To save an output ASCII file, a file name argument needs to be passed to the ``outfile`` parameter. """ import pygmt # Load sample grid and point datasets grid = pygmt.datasets.load_earth_relief() points = pygmt.datasets.load_ocean_ridge_points() # Sample the bathymetry along the world's ocean ridges at specified track points track = pygmt.grdtrack(points=points, grid=grid, newcolname="bathymetry") fig = pygmt.Figure() # Plot the earth relief grid on Cylindrical Stereographic projection, masking land areas fig.basemap(region="g", frame=True, projection="Cyl_stere/150/-20/15c") fig.grdimage(grid=grid, cmap="gray") fig.coast(land="#666666") # Plot using circles (c) of 0.15 cm, the sampled bathymetry points # Points are colored using elevation values (normalized for visual purposes) fig.plot( x=track.longitude, y=track.latitude, style="c0.15c", cmap="terra", color=(track.bathymetry - track.bathymetry.mean()) / track.bathymetry.std(),
def test_grdtrack_without_outfile_setting(dataarray, dataframe): """ Run grdtrack by not passing in outfile parameter setting. """ with pytest.raises(GMTInvalidInput): grdtrack(points=dataframe, grid=dataarray)