polartoolkit.utils#

Functions#

default_hemisphere(hemisphere)

Returns the default hemisphere set in the users environment variables or raises a

rmse(data[, as_median])

function to give the root mean/median squared error (RMSE) of data

get_grid_info(grid[, print_info])

Returns information of the specified grid.

dd2dms(dd)

Convert decimal degrees to minutes, seconds. Modified from

region_to_df(region[, coord_names, reverse])

Convert region bounds in format [xmin, xmax, ymin, ymax] to pandas dataframe with

region_xy_to_ll(region[, hemisphere, dms])

Convert region in format [xmin, xmax, ymin, ymax] in projected meters to lat / lon

region_ll_to_xy(region[, hemisphere])

Convert region in format [lon_min, lon_max, lat_min, lat_max] to projected meters in

region_to_bounding_box(region)

Convert region in format [xmin, xmax, ymin, ymax] to bounding box format used for

epsg3031_to_latlon(df[, reg, input_coord_names, ...])

Convert coordinates from EPSG:3031 Antarctic Polar Stereographic in meters to

epsg3413_to_latlon(df[, reg, input_coord_names, ...])

Convert coordinates from EPSG:3413 North Polar Stereographic in meters to

latlon_to_epsg3031(df[, reg, input_coord_names, ...])

Convert coordinates from EPSG:4326 WGS84 in decimal degrees to EPSG:3031 Antarctic

latlon_to_epsg3413(df[, reg, input_coord_names, ...])

Convert coordinates from EPSG:4326 WGS84 in decimal degrees to EPSG:3413 North Polar

reproject(df, input_crs, output_crs[, ...])

Convert coordinates from input CRS to output CRS. Coordinates can be supplied as a

points_inside_region(df, region[, names, reverse])

return a subset of a dataframe which is within a region

block_reduce(df, reduction[, input_coord_names, ...])

perform a block reduction of a dataframe.

nearest_grid_fill(grid[, method, crs])

fill missing values in a grid with the nearest value.

filter_grid(grid[, filter_width, filt_type, ...])

Apply a spatial filter to a grid.

points_inside_shp(points, shapefile[, crs, ...])

Add a column to a dataframe indicating whether each point is inside a shapefile.

mask_from_shp(shapefile[, hemisphere, invert, grid, ...])

Create a mask or a masked grid from area inside or outside of a closed shapefile.

alter_region(starting_region, **kwargs)

deprecated function, use regions.alter_region instead

set_proj(region[, hemisphere, fig_height, fig_width])

Gives GMT format projection string from region and figure height or width.

grd_trend(da[, coords, deg, plot])

Fit an arbitrary order trend to a grid and use it to detrend.

get_combined_min_max(values[, shapefile, robust, ...])

Get a grids max and min values.

grd_compare(da1, da2[, plot, plot_type, robust])

Find the difference between 2 grids and plot the results, if necessary resample and

make_grid(region, spacing, value, name)

Create a grid with 1 variable by defining a region, spacing, name and constant value

square_subplots(n)

From matplotlib/grid-strategy

random_color()

generate a random color in format R/G/B

subset_grid(grid, region)

Return a subset of a grid based on a region

get_min_max(values[, shapefile, robust, region, ...])

Get a grids max and min values.

shapes_to_df(shapes[, hemisphere])

convert the output of regions.draw_region and profiles.draw_lines to a dataframe

polygon_to_region(polygon[, hemisphere])

convert the output of regions.draw_region to bounding region in EPSG:3031 for the

mask_from_polygon(polygon[, hemisphere, invert, ...])

convert the output of regions.draw_region to a mask or use it to mask a grid

change_reg(grid)

Use GMT grdedit to change the registration type in the metadata.

grd_blend(grid1, grid2)

Use GMT grdblend to blend 2 grids into 1.

get_fig_width()

Get the width of the current PyGMT figure instance.

get_fig_height()

Get the height of the current PyGMT figure instance.

gmt_str_to_list(region)

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.

Parameters:

hemisphere (str | None) – hemisphere to use, either “north” or “south”, or None to use the default set in the users environment variables.

Returns:

hemisphere to use, either “north” or “south”

Return type:

str

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:

float

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:

tuple

dd2dms(dd)[source]#

Convert decimal degrees to minutes, seconds. Modified from https://stackoverflow.com/a/10286690/18686384

Parameters:

dd (float) – input decimal degrees

Returns:

degrees in the format “DD:MM:SS”

Return type:

str

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:

tuple[Any, Any, Any, Any] | pandas.DataFrame

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:
  • hemisphere (str, optional,) – choose between the “north” or “south” hemispheres

  • region (tuple[Any, Any, Any, Any]) – region boundaries in format [xmin, xmax, ymin, ymax] in meters

  • dms (bool) – if True, will return results as deg:min:sec instead of decimal degrees, by default False

Returns:

region boundaries in format [lon_min, lon_max, lat_min, lat_max]

Return type:

tuple[Any, Any, Any, Any]

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:
  • hemisphere (str, optional,) – choose between the “north” or “south” hemispheres

  • region (tuple[float, float, float, float]) – region boundaries in format [xmin, xmax, ymin, ymax] in decimal degrees

Returns:

region boundaries in format [x_min, x_max, y_min, y_max]

Return type:

tuple[float, float, float, float]

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

Parameters:

region (tuple[Any, Any, Any, Any]) – region boundaries in format [xmin, xmax, ymin, ymax] in meters or degrees.

Returns:

region boundaries in bounding box format.

Return type:

tuple[Any, Any, Any, Any]

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:

pandas.DataFrame or numpy.ndarray[Any, Any]

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:

pandas.DataFrame or numpy.ndarray[Any, Any]

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:

pandas.DataFrame

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:

pandas.DataFrame

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:

xarray.DataArray

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:

xarray.DataArray

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:

xarray.DataArray

alter_region(starting_region, **kwargs)[source]#

deprecated function, use regions.alter_region instead

Parameters:
Return type:

tuple[float, float, float, float]

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:

tuple

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:
  • da (xarray.DataArray) – input grid

  • coords (tuple[str, str, str], optional) – coordinate names of the supplied grid, by default [‘x’, ‘y’, ‘z’]

  • deg (int, optional) – trend order to use, by default 1

  • plot (bool, optional) – plot the results, by default False

  • kwargs (Any)

Returns:

returns xarray.DataArrays of the fitted surface, and the detrended grid.

Return type:

tuple[xarray.DataArray, xarray.DataArray]

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:

tuple[float, float]

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:

tuple[xarray.DataArray, xarray.DataArray, xarray.DataArray]

make_grid(region, spacing, value, name)[source]#

Create a grid with 1 variable by defining a region, spacing, name and constant value

Parameters:
  • region (tuple[float, float, float, float]) – bounding region in format [xmin, xmax, ymin, ymax]

  • spacing (float) – spacing for grid

  • value (float) – constant value to use for variable

  • name (str) – name for variable

Returns:

Returns a xarray.DataArray with 1 variable of constant value.

Return type:

xarray.DataArray

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.

Parameters:

n (int) – The number of total plots in the subplot

Returns:

Returns a tuple in the format (number of rows, number of columns), so for example a 3 x 2 grid would be represented as (3, 3), because there are 2 rows of length 3.

Return type:

tuple[int, int]

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:

str

subset_grid(grid, region)[source]#

Return a subset of a grid based on a region

Parameters:
Returns:

clipped grid

Return type:

xarray.DataArray

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:

tuple[float, float]

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:
  • hemisphere (str, optional) – choose between the “north” or “south” hemispheres

  • shapes (list) – list of vertices

Returns:

Dataframe with easting, northing, and shape_num.

Return type:

pandas.DataFrame

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.

Parameters:
  • polyon (list) – list of polygon vertices

  • hemisphere (str, optional) – choose between the “north” or “south” hemispheres

  • polygon (list[float])

Returns:

region in format in format [xmin, xmax, ymin, ymax]

Return type:

tuple[float, float, float, float]

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:

xarray.DataArray

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:

xarray.DataArray

grd_blend(grid1, grid2)[source]#

Use GMT grdblend to blend 2 grids into 1.

Parameters:
Returns:

returns a blended grid.

Return type:

xarray.DataArray

get_fig_width()[source]#

Get the width of the current PyGMT figure instance.

Returns:

width of the figure

Return type:

float

get_fig_height()[source]#

Get the height of the current PyGMT figure instance.

Returns:

height of the figure

Return type:

float

gmt_str_to_list(region)[source]#

convert a tuple of floats representing the boundaries of a region into a GMT-style region string

Parameters:

region (tuple[float, float, float, float]) – bounding region in format [xmin, xmax, ymin, ymax]

Returns:

a GMT style region string

Return type:

str