polartoolkit.utils#
Functions#
|
Returns the default hemisphere set in the users environment variables or raises a |
|
function to give the root mean/median squared error (RMSE) of data |
|
Returns information of the specified grid. |
|
Convert decimal degrees to minutes, seconds. Modified from |
|
Convert region bounds in format [xmin, xmax, ymin, ymax] to pandas dataframe with |
|
Convert region in format [xmin, xmax, ymin, ymax] in projected meters to lat / lon |
|
Convert region in format [lon_min, lon_max, lat_min, lat_max] to projected meters in |
|
Convert region in format [xmin, xmax, ymin, ymax] to bounding box format used for |
|
Convert coordinates from EPSG:3031 Antarctic Polar Stereographic in meters to |
|
Convert coordinates from EPSG:3413 North Polar Stereographic in meters to |
|
Convert coordinates from EPSG:4326 WGS84 in decimal degrees to EPSG:3031 Antarctic |
|
Convert coordinates from EPSG:4326 WGS84 in decimal degrees to EPSG:3413 North Polar |
|
Convert coordinates from input CRS to output CRS. Coordinates can be supplied as a |
|
return a subset of a dataframe which is within a region |
|
perform a block reduction of a dataframe. |
|
fill missing values in a grid with the nearest value. |
|
Apply a spatial filter to a grid. |
|
Add a column to a dataframe indicating whether each point is inside a shapefile. |
|
Create a mask or a masked grid from area inside or outside of a closed shapefile. |
|
deprecated function, use regions.alter_region instead |
|
Gives GMT format projection string from region and figure height or width. |
|
Fit an arbitrary order trend to a grid and use it to detrend. |
|
Get a grids max and min values. |
|
Find the difference between 2 grids and plot the results, if necessary resample and |
|
Create a grid with 1 variable by defining a region, spacing, name and constant value |
generate a random color in format R/G/B |
|
|
Return a subset of a grid based on a region |
|
Get a grids max and min values. |
|
convert the output of regions.draw_region and profiles.draw_lines to a dataframe |
|
convert the output of regions.draw_region to bounding region in EPSG:3031 for the |
|
convert the output of regions.draw_region to a mask or use it to mask a grid |
|
Use GMT grdedit to change the registration type in the metadata. |
|
Use GMT grdblend to blend 2 grids into 1. |
Get the width of the current PyGMT figure instance. |
|
Get the height of the current PyGMT figure instance. |
|
|
convert a tuple of floats representing the boundaries of a region into a GMT-style |
Module Contents#
- default_hemisphere(hemisphere)[source]#
Returns the default hemisphere set in the users environment variables or raises a error.
- rmse(data, as_median=False)[source]#
function to give the root mean/median squared error (RMSE) of data
- Parameters:
data (numpy.ndarray[Any, Any]) – input data
as_median (bool, optional) – choose to give root median squared error instead, by default False
- Returns:
RMSE value
- Return type:
- get_grid_info(grid, print_info=False)[source]#
Returns information of the specified grid.
- Parameters:
grid (str or xarray.DataArray) – Input grid to get info from. Filename string or loaded grid.
print_info (bool, optional) – If true, prints out the grid info, by default False
- Returns:
(string of grid spacing, array with the region boundary, data min, data max, grid registration)
- Return type:
- dd2dms(dd)[source]#
Convert decimal degrees to minutes, seconds. Modified from https://stackoverflow.com/a/10286690/18686384
- region_to_df(region, coord_names=('easting', 'northing'), reverse=False)[source]#
Convert region bounds in format [xmin, xmax, ymin, ymax] to pandas dataframe with coordinates of region corners, or reverse this if reverse is True.
- Parameters:
region (tuple[Any, Any, Any, Any] | pandas.DataFrame) – bounding region in format [xmin, xmax, ymin, ymax] or, if reverse is True, a DataFrame with coordinate columns with names set by cood_names
coord_names (tuple[str, str], optional) – names of input or output coordinate columns, by default (“easting”, “northing”)
reverse (bool, optional) – If True, convert from df to region tuple, else, convert from region tuple to df, by default False
- Returns:
Dataframe with easting and northing columns, and a row for each corner of the region, or, if reverse is True, an array in the format [xmin, xmax, ymin, ymax].
- Return type:
- region_xy_to_ll(region, hemisphere=None, dms=False)[source]#
Convert region in format [xmin, xmax, ymin, ymax] in projected meters to lat / lon
- Parameters:
- Returns:
region boundaries in format [lon_min, lon_max, lat_min, lat_max]
- Return type:
- region_ll_to_xy(region, hemisphere=None)[source]#
Convert region in format [lon_min, lon_max, lat_min, lat_max] to projected meters in the north or south polar stereographic projections.
- Parameters:
- Returns:
region boundaries in format [x_min, x_max, y_min, y_max]
- Return type:
- region_to_bounding_box(region)[source]#
Convert region in format [xmin, xmax, ymin, ymax] to bounding box format used for icepyx: [ lower left longitude, lower left latitude, upper right longitude, upper right latitude ] Same format as [xmin, ymin, xmax, ymax], used for bbox parameter of geopandas.read_file
- epsg3031_to_latlon(df, reg=False, input_coord_names=None, output_coord_names=('lon', 'lat'))[source]#
Convert coordinates from EPSG:3031 Antarctic Polar Stereographic in meters to EPSG:4326 WGS84 in decimal degrees.
- Parameters:
df (pandas.DataFrame or tuple[Any]) – input dataframe with easting and northing columns, or tuple [x,y]
reg (bool, optional) – if true, returns a GMT formatted region string, by default False
input_coord_names (tuple | None, optional) – set names for input coordinate columns, by default (“x”, “y”) or (“easting”, “northing”)
output_coord_names (tuple | None, optional) – set names for output coordinate columns, by default (“lon”, “lat”)
- Returns:
Updated dataframe with new latitude and longitude columns, numpy.ndarray in format [xmin, xmax, ymin, ymax], or tuple in format [lat, lon]
- Return type:
pandas.DataFrame or tuple[Any]
- epsg3413_to_latlon(df, reg=False, input_coord_names=None, output_coord_names=('lon', 'lat'))[source]#
Convert coordinates from EPSG:3413 North Polar Stereographic in meters to EPSG:4326 WGS84 in decimal degrees.
- Parameters:
df (pandas.DataFrame or tuple[Any]) – input dataframe with easting and northing columns, or tuple [x,y]
reg (bool, optional) – if true, returns a GMT formatted region string, by default False
input_coord_names (tuple | None, optional) – set names for input coordinate columns, by default (“x”, “y”) or (“easting”, “northing”)
output_coord_names (tuple | None, optional) – set names for output coordinate columns, by default (“lon”, “lat”)
- Returns:
Updated dataframe with new latitude and longitude columns, numpy.ndarray in format [xmin, xmax, ymin, ymax], or tuple in format [lat, lon]
- Return type:
pandas.DataFrame or tuple[Any]
- latlon_to_epsg3031(df, reg=False, input_coord_names=None, output_coord_names=('easting', 'northing'))[source]#
Convert coordinates from EPSG:4326 WGS84 in decimal degrees to EPSG:3031 Antarctic Polar Stereographic in meters.
- Parameters:
df (pandas.DataFrame or numpy.ndarray[Any, Any]) – input dataframe with latitude and longitude columns
reg (bool, optional) – if true, returns a GMT formatted region string, by default False
input_coord_names (tuple | None, optional) – set names for input coordinate columns, by default (“lon”, “lat”)
output_coord_names (tuple | None, optional) – set names for output coordinate columns, by default (“easting”, “northing”)
- Returns:
Updated dataframe with new easting and northing columns or numpy.ndarray in format [xmin, xmax, ymin, ymax]
- Return type:
- latlon_to_epsg3413(df, reg=False, input_coord_names=None, output_coord_names=('easting', 'northing'))[source]#
Convert coordinates from EPSG:4326 WGS84 in decimal degrees to EPSG:3413 North Polar Stereographic in meters.
- Parameters:
df (pandas.DataFrame or numpy.ndarray[Any, Any]) – input dataframe with latitude and longitude columns
reg (bool, optional) – if true, returns a GMT formatted region string, by default False
input_coord_names (tuple | None, optional) – set names for input coordinate columns, by default (“lon”, “lat”)
output_coord_names (tuple | None, optional) – set names for output coordinate columns, by default (“easting”, “northing”)
- Returns:
Updated dataframe with new easting and northing columns or numpy.ndarray in format [xmin, xmax, ymin, ymax]
- Return type:
- reproject(df, input_crs, output_crs, input_coord_names=None, output_coord_names=None, reg=False)[source]#
Convert coordinates from input CRS to output CRS. Coordinates can be supplied as a dataframe with coordinate columns set by input_coord_names, or as a tuple of a list of x coordinates and a list of y coordinates.
- Parameters:
df (pandas.DataFrame or tuple[Any]) – input dataframe with easting/longitude and northing/latitude columns, or tuple [x,y]
input_crs (str) – input CRS in EPSG format, e.g. “epsg:4326”
output_crs (str) – output CRS in EPSG format, e.g. “epsg:3413”
reg (bool, optional) – if true, returns a GMT formatted region string, by default False
input_coord_names (tuple, optional) – set names for input coordinate columns, by default “x”/”y” or “easting”/”northing” if input_crs is “epsg:3413” or “epsg:3031”, or if input_crs is “epsg_4326”, “lon”/”lat”
output_coord_names (tuple, optional) – set names for output coordinate columns, by default “x”/”y” if output_crs is “epsg:3413” or “epsg:3031”, or if output_crs is “epsg_4326”, “lon”/”lat”.
- Returns:
Updated dataframe with new latitude and longitude columns, numpy.ndarray in format [xmin, xmax, ymin, ymax], or tuple in format [lat, lon]
- Return type:
pandas.DataFrame or tuple[Any]
- points_inside_region(df, region, names=('x', 'y'), reverse=False)[source]#
return a subset of a dataframe which is within a region
- Parameters:
df (pandas.DataFrame) – dataframe with coordinate columns to use for defining if within region
region (tuple[float, float, float, float]) – bounding region in format [xmin, xmax, ymin, ymax] for bounds of new subset dataframe
names (tuple[str, str], optional) – column names to use for x and y coordinates, by default (“x”, “y”) or (“easting”, “northing”)
reverse (bool, optional) – if True, will return points outside the region, by default False
- Returns:
returns a subset dataframe
- Return type:
- block_reduce(df, reduction, input_coord_names=('x', 'y'), input_data_names=None, **kwargs)[source]#
perform a block reduction of a dataframe.
- Parameters:
df (pandas.DataFrame) – data to block reduce
reduction (Callable) – function to use in reduction, e.g. np.mean
input_coord_names (tuple[str, str], optional) – strings of coordinate column names, by default (“x”, “y”) or (“easting”, “northing”)
input_data_names (Any | None, optional) – strings of data column names, by default None
kwargs (Any)
- Returns:
a block-reduced dataframe
- Return type:
- nearest_grid_fill(grid, method='verde', crs=None)[source]#
fill missing values in a grid with the nearest value.
- Parameters:
grid (xarray.DataArray) – grid with missing values
method (str, optional) – choose method of filling, by default “verde”
crs (str | None, optional) – if method is ‘rioxarray’, provide the crs of the grid, in format ‘epsg:xxxx’, by default None
- Returns:
filled grid
- Return type:
- filter_grid(grid, filter_width=None, filt_type='lowpass', pad_width_factor=3, pad_mode='linear_ramp', pad_constant=None, pad_end_values=None)[source]#
Apply a spatial filter to a grid.
- Parameters:
grid (xarray.DataArray) – grid to filter the values of
filter_width (float | None, optional) – width of the filter in meters for high and low pass filtering, by default None
filt_type (str, optional) – type of filter to use from ‘lowpass’, ‘highpass’ ‘up_deriv’, ‘easting_deriv’, ‘northing_deriv’, or ‘total_gradient’ by default “lowpass”
pad_width_factor (int, optional) – factor of grid width to pad the grid by, by default 3, which equates to a pad with a width of 1/3 of the grid width.
pad_mode (str, optional) – mode of padding, can be “linear”, by default “linear_ramp”
pad_constant (float | None, optional) – constant value to use for padding, by default None
pad_end_values (float | None, optional) – value to use for end of padding if pad_mode is “linear_ramp”, by default None
- Returns:
a filtered grid
- Return type:
- points_inside_shp(points, shapefile, crs=None, coord_names=None, hemisphere=None)[source]#
Add a column to a dataframe indicating whether each point is inside a shapefile.
- Parameters:
points (pd.DataFrame | gpd.geodataframe.GeoDataFrame) – dataframe with coordinate columns specified by coord_names to use for defining if within shapefile
shapefile (gpd.geodataframe.GeoDataFrame) – shapefile to use for defining if point are within it or not
crs (str | None, optional) – if points is not a geodataframe, crs to use to convert into a geodataframe, by default None
coord_names (tuple[str, str] | None, optional) – names of coordinate columns, by default ‘x’ and ‘y’ or ‘easting’ and ‘northing’
hemisphere (str | None, optional) – hemisphere to use for automatically detecting crs, by default None
- Returns:
Dataframe with a new column ‘inside’ which is True if the point is inside the shapefile
- Return type:
pd.DataFrame | gpd.geodataframe.GeoDataFrame
- mask_from_shp(shapefile, hemisphere=None, invert=True, grid=None, xr_grid=None, grid_file=None, region=None, spacing=None, masked=False, pixel_register=True, input_coord_names=('easting', 'northign'))[source]#
Create a mask or a masked grid from area inside or outside of a closed shapefile.
- Parameters:
shapefile (str or geopandas.GeoDataFrame) – either path to .shp filename, must by in same directory as accompanying files : .shx, .prj, .dbf, should be a closed polygon file, or shapefile which as already been loaded into a geodataframe.
hemisphere (str, optional) – choose “north” for EPSG:3413 or “south” for EPSG:3031
invert (bool, optional) – choose whether to mask data outside the shape (False) or inside the shape (True), by default True (masks inside of shape)
grid (xarray.DataArray or str, optional) – _xarray.DataArray or path to a .nc file; to use to define region, or to mask, by default None
xr_grid (xarray.DataArray, optional) – deprecated, use grid instead, by default None
grid_file (str, optional) – deprecated, use grid instead, by default None
region (str or tuple[float, float, float, float], optional) – bounding region in format [xmin, xmax, ymin, ymax] in meters to create a dummy grid if none are supplied, by default None
spacing (str or int, optional) – grid spacing in meters to create a dummy grid if none are supplied, by default None
masked (bool, optional) – choose whether to return the masked grid (True) or the mask itself (False), by default False
pixel_register (bool, optional) – choose whether the grid is pixel registered (True) or grid registered (False), by default True
input_coord_names (tuple[str, str], optional) – set names for input coordinate columns, by default (“easting”, “northing”)
- Returns:
Returns either a masked grid, or the mask grid itself.
- Return type:
- alter_region(starting_region, **kwargs)[source]#
deprecated function, use regions.alter_region instead
- set_proj(region, hemisphere=None, fig_height=15, fig_width=None)[source]#
Gives GMT format projection string from region and figure height or width. Inspired from mrsiegfried/Venturelli2020-GRL.
- Parameters:
region (tuple[float, float, float, float]) – region boundaries in format [xmin, xmax, ymin, ymax] in projected meters
hemisphere (str, optional) – set whether to lat lon projection is for “north” hemisphere (EPSG:3413) or “south” hemisphere (EPSG:3031)
fig_height (float) – desired figure height in cm
fig_width (float) – instead of using figure height, set the projection based on figure width in cm, by default is None
- Returns:
returns a tuple of the following variables: proj, proj_latlon, fig_width, fig_height
- Return type:
- grd_trend(da, coords=('x', 'y', 'z'), deg=1, plot=False, **kwargs)[source]#
Fit an arbitrary order trend to a grid and use it to detrend.
- Parameters:
- Returns:
returns xarray.DataArrays of the fitted surface, and the detrended grid.
- Return type:
- get_combined_min_max(values, shapefile=None, robust=False, region=None, hemisphere=None, absolute=False, robust_percentiles=(0.02, 0.98))[source]#
Get a grids max and min values.
- Parameters:
values (tuple[xarray.DataArray | pandas.Series | numpy.ndarray]) – values to get min and max for
shapefile (Union[str or geopandas.GeoDataFrame], optional) – path or loaded shapefile to use for a mask, by default None
robust (bool, optional) – choose whether to return the 2nd and 98th percentile values, instead of the min/max
region (tuple[float, float, float, float], optional) – give a subset region to get min and max values from, in format [xmin, xmax, ymin, ymax], by default None
hemisphere (str, optional) – set whether to lat lon projection is for “north” hemisphere (EPSG:3413) or “south” hemisphere (EPSG:3031)
absolute (bool, optional) – choose whether to return the absolute min and max values, by default False
robust_percentiles (tuple[float, float], optional) – percentiles to use for robust min and max values, by default (0.02, 0.98)
- Returns:
returns the min and max values.
- Return type:
- grd_compare(da1, da2, plot=True, plot_type=None, robust=False, **kwargs)[source]#
Find the difference between 2 grids and plot the results, if necessary resample and cut grids to match
- Parameters:
da1 (xarray.DataArray or str) – first grid, loaded grid of filename
da2 (xarray.DataArray or str) – second grid, loaded grid of filename
plot (bool, optional) – plot the results, by default True
plot_type (Any or None, optional) – this argument has been deprecated and will default to ‘pygmt’
robust (bool, optional) – use xarray robust color lims instead of min and max, by default is False.
kwargs (Any)
- Keyword Arguments:
shp_mask (str) – shapefile filename to use to mask the grids for setting the color range.
robust (bool) – use xarray robust color lims instead of min and max, by default is False.
region (tuple[float, float, float, float]) – choose a specific region to compare, in format [xmin, xmax, ymin, ymax].
rmse_in_title (bool) – add the RMSE to the title, by default is True.
cpt_lims (tuple[float, float]) – set the colorbar limits for the two grids.
diff_lims (tuple[float, float]) – set the colorbar limits for the difference grid.
- Returns:
three xarray.DataArrays: (diff, resampled grid1, resampled grid2)
- Return type:
- make_grid(region, spacing, value, name)[source]#
Create a grid with 1 variable by defining a region, spacing, name and constant value
- Parameters:
- Returns:
Returns a xarray.DataArray with 1 variable of constant value.
- Return type:
- square_subplots(n)[source]#
From matplotlib/grid-strategy Calculate the number of rows and columns based on the total number of items (n) to make an arrangement as close to square as looks good.
- random_color()[source]#
generate a random color in format R/G/B
- Returns:
returns a random color string, for example ‘95/226/100’
- Return type:
- subset_grid(grid, region)[source]#
Return a subset of a grid based on a region
- Parameters:
- Returns:
clipped grid
- Return type:
- get_min_max(values, shapefile=None, robust=False, region=None, hemisphere=None, absolute=False, robust_percentiles=(0.02, 0.98))[source]#
Get a grids max and min values.
- Parameters:
values (xarray.DataArray or pandas.Series or numpy.ndarray) – values to find min or max for
shapefile (Union[str or geopandas.GeoDataFrame], optional) – path or loaded shapefile to use for a mask, by default None
robust (bool, optional) – choose whether to return the 2nd and 98th percentile values, instead of the min/max
region (tuple[float, float, float, float], optional) – give a subset region to get min and max values from, in format [xmin, xmax, ymin, ymax], by default None
hemisphere (str, optional) – set whether to lat lon projection is for “north” hemisphere (EPSG:3413) or “south” hemisphere (EPSG:3031)
absolute (bool, optional) – return the absolute min and max values, by default False
robust_percentiles (tuple[float, float], optional) – decimal percentiles to use for robust min and max, by default (0.02, 0.98)
- Returns:
returns the min and max values.
- Return type:
- shapes_to_df(shapes, hemisphere=None)[source]#
convert the output of regions.draw_region and profiles.draw_lines to a dataframe of easting and northing points
- Parameters:
- Returns:
Dataframe with easting, northing, and shape_num.
- Return type:
- polygon_to_region(polygon, hemisphere=None)[source]#
convert the output of regions.draw_region to bounding region in EPSG:3031 for the south hemisphere and EPSG:3413 for the north hemisphere.
- mask_from_polygon(polygon, hemisphere=None, invert=False, drop_nans=False, grid=None, region=None, spacing=None, **kwargs)[source]#
convert the output of regions.draw_region to a mask or use it to mask a grid
- Parameters:
polygon (list) – list of polygon vertices
hemisphere (str, optional) – choose between the “north” or “south” hemispheres
invert (bool, optional) – reverse the sense of masking, by default False
drop_nans (bool, optional) – drop nans after masking, by default False
grid (Union[str, xarray.DataArray], optional) – grid to mask, by default None
region (tuple[float, float, float, float], optional) – region to create a grid if none is supplied, in format [xmin, xmax, ymin, ymax], by default None
spacing (int, optional) – spacing to create a grid if none is supplied, by default None
kwargs (Any)
- Returns:
masked grid or mask grid with 1’s inside the mask.
- Return type:
- change_reg(grid)[source]#
Use GMT grdedit to change the registration type in the metadata.
- Parameters:
grid (xarray.DataArray) – input grid to change the reg for.
- Returns:
returns a xarray.DataArray with switched reg type.
- Return type:
- grd_blend(grid1, grid2)[source]#
Use GMT grdblend to blend 2 grids into 1.
- Parameters:
grid1 (xarray.DataArray) – input grid to change the reg for.
grid2 (xarray.DataArray) – input grid to change the reg for.
- Returns:
returns a blended grid.
- Return type:
- get_fig_width()[source]#
Get the width of the current PyGMT figure instance.
- Returns:
width of the figure
- Return type: