spatial

Functions for reading, writing and processing spatial data

  • Extends the GRACE/GRACE-FO gravity_toolkit.spatial module adding raster support

Source code

General Methods

class model_harmonics.spatial.raster(projection=None, **kwargs)[source]

Inheritance of spatial class for reading and writing raster data

Attributes:
data: np.ndarray

spatial grid data

mask: np.ndarray

spatial grid mask

x: np.ndarray

x-coordinate array

y: np.ndarray

y-coordinate array

lon: np.ndarray

grid longitudes

lat: np.ndarray

grid latitudes

fill_value: float or NoneType, default None

invalid value for spatial grid data

attributes: dict

attributes of spatial variables

extent: list, default [None,None,None,None]

spatial grid bounds [minimum x, maximum x, minimum y, maximum y]

spacing: list, default [None,None]

grid step size [x, y]

shape: tuple

dimensions of spatial object

ndim: int

number of dimensions of spatial object

filename: str

input or output filename

from_geotiff(filename, **kwargs)[source]

Read data from a geotiff file

Parameters:
filename: str

full path of input geotiff file

compression: str or NoneType, default None

file compression type

bounds: list or NoneType, default bounds

extent of the file to read: [minimum x, maximum x, minimum y, maximum y]

to_geotiff(filename, **kwargs)[source]

Write a spatial object to a geotiff file

Parameters:
filename: str

full path of output geotiff file

driver: str, default ‘cog’

GDAL driver

  • 'GTiff': GeoTIFF

  • 'cog': Cloud Optimized GeoTIFF

dtype: obj, default osgeo.gdal.GDT_Float64

GDAL data type

options: list, default [‘COMPRESS=LZW’]

GDAL driver creation options

get_latlon(srs_proj4=None, srs_wkt=None, srs_epsg=None)[source]

Get the latitude and longitude of grid cells

Parameters:
srs_proj4: str or NoneType, default None

PROJ4 projection string

srs_wkt: str or NoneType, default None

Well-Known Text (WKT) projection string

srs_epsg: int or NoneType, default None

EPSG projection code

Returns:
longitude: np.ndarray

longitude coordinates of grid cells

latitude: np.ndarray

latitude coordinates of grid cells

property spacing

Step size of raster object [x, y]

property extent

Bounds of raster object [minimum x, maximum x, minimum y, maximum y]

copy()[source]

Copy a raster object to a new raster object

expand_dims()[source]

Add a singleton dimension to a spatial object if non-existent

flip(axis=0)[source]

Reverse the order of data and dimensions along an axis

Parameters:
axis: int, default 0

axis to reorder

class model_harmonics.spatial.mosaic(**kwargs)[source]

Utility for creating spatial mosaics

update_spacing(x, y)[source]

update the step size of mosaic

update_bounds(x, y)[source]

update the bounds of mosaic

image_coordinates(x, y)[source]

get the image coordinates

property dimensions

Dimensions of the mosaic

property shape

Shape of the mosaic

property x

X-coordinates of the mosaic

property y

Y-coordinates of the mosaic

model_harmonics.spatial.geocentric_latitude(lon: ndarray, lat: ndarray, a_axis: float = 637813700.0, flat: float = 0.0033528106647474805)[source]

Converts from geodetic latitude to geocentric latitude for an ellipsoid [23]

Parameters:
lon: np.ndarray,

longitude (degrees east)

lat: np.ndarray,

geodetic latitude (degrees north)

a_axis: float, default 6378137.0

semimajor axis of the ellipsoid

flat: float, default 1.0/298.257223563

ellipsoidal flattening

Returns:
geocentric_latitude: np.ndarray

latitude intersecting the center of the Earth (degrees north)

model_harmonics.spatial.scale_factors(lat: ndarray, flat: float = 0.0033528106647474805, reference_latitude: float = 70.0, metric: str = 'area')[source]

Calculates scaling factors to account for polar stereographic distortion including special case of at the exact pole [23]

Parameters:
lat: np.ndarray

latitude (degrees north)

flat: float, default 1.0/298.257223563

ellipsoidal flattening

reference_latitude: float, default 70.0

reference latitude (true scale latitude)

metric: str, default ‘area’

metric to calculate scaling factors

  • 'distance': scale factors for distance

  • 'area': scale factors for area

Returns:
scale: np.ndarray

scaling factors at input latitudes

model_harmonics.spatial.get_latlon(x, y, srs_proj4=None, srs_wkt=None, srs_epsg=None)[source]

Get the latitude and longitude of grid cells

Parameters:
srs_proj4: str or NoneType, default None

PROJ4 projection string

srs_wkt: str or NoneType, default None

Well-Known Text (WKT) projection string

srs_epsg: int or NoneType, default None

EPSG projection code

Returns:
longitude: np.ndarray

longitude coordinates of grid cells

latitude: np.ndarray

latitude coordinates of grid cells