Grid package

The grid package is an extension for the SpatialDataset to support horizontal grid transformations and functions.

Grid builder

class pymepps.grid.builder.GridBuilder(griddes)[source]
build_grid()[source]

This method build up the grid with the griddes attribute.

Returns:grid – The built grid. The class of the grid is defined by the gridtype. The values of the grid are calculated with griddes.
Return type:child instance of Grid
static decode_str(grid_str)[source]

Method to clean the given grid str and to get a python dict. Key and value are separated with =. Every new key value pair needs a new line delimiter. Only alphanumeric characters are allowed as key and value. To delimit a value list use spaces and new lines. Lines with # are used as comment lines.

Steps to decode the grid string:
  1. String splitting by new line delimiter
  2. Clean the lines from unallowed characters
  3. Split the non-comment lines to key, value pairs
  4. Append elements where no key, value pair is available to the previous value
  5. Clean and split the key, value elements from spaces
  6. Convert the values to float numbers
Parameters:grid_str (str or list(str)) – The given grid_str which should be decoded. If this is a string the string will be splitten by new line into a list. It is necessary that every list entry has only one key = value entry.
Returns:grid_dict – The decoded grid dict from the str.
Return type:dict(str, str or float)
griddes
static open_string(path_str)[source]

This method is used to check if the given str is a path or a grid string.

Parameters:path_str (str) – This string is checked and if it is a path it will be read.
Returns:grid_str – The given str or the read str.
Return type:str
Raises:TypeError – If path_str is not a str type.

Grids

BaseGrid

class pymepps.grid.grid.Grid(grid_dict)[source]

The base class for every grid type.

static convert_to_deg(field, unit)[source]

Method to convert given field with given unit into degree.

Parameters:
  • field
  • unit
copy()[source]
get_coord_names()[source]

Returns the name of the coordinates.

Returns:
  • yname (str) – The name of the y-dimension.
  • xname (str) – The name of the x-dimension
get_coords()[source]

Get the coordinates in a xarray-compatible way.

Returns:coords – The coordinates in a xarray compatible coordinates format. The key is the coordinate name. The coordinates have as value a tuple with their own name, indicating that the they are self-describing, and the coordinate values as numpy array.
Return type:dict(str, (str, numpy.ndarray))
get_nearest_point(data, coord)[source]

Get the nearest neighbour grid point for a given coordinate. The distance between the grid points and the given coordinates is calculated with the haversine formula.

Parameters:
  • data (numpy.array or xarray.DataArray) – The return value is extracted from this array. The array should have at least two dimensions. If the array has more than two dimensions the last two dimensions will be used as horizontal grid dimensions.
  • coord (tuple(float, float)) – The data of the nearest grid point to this coordinate (latitude, longitude) will be returned. The coordinate should be in degree.
Returns:

nearest_data – The extracted data for the nearest neighbour grid point. The dimensions of this array are the same as the input data array without the horizontal coordinate dimensions. There is at least one dimension.

Return type:

numpy.ndarray or xarray.DataArray

interpolate(data, other_grid, order=0)[source]

Interpolate the given data to the given other grid.

Parameters:
  • data (numpy.ndarray or xarray.DataArray) – This data is used for the interpolation. The shape of data’s grid axis needs to be the same as this grid.
  • other_grid (Grid instance) – The other_grid is used as target grid for the interpolation.
  • order (int, optional) – Specifies the interpolation order, based on basemap.interp order. 0. order: nearest neighbour 1. order: bilinear interpolation
Returns:

remapped_data – The remapped data with the same type as the input data. If the input data is a xarray.DataArray the output data will use the same attributes and non-grid dimensions as the input data.

Return type:

numpy.ndarray or xarray.DataArray

lat_lon

Get latitudes and longitudes for every grid point as xarray.Dataset.

Returns:lat_lon – The latitude and longitude values for every grid point as xarray.Dataset with latitude and longitude as variables.
Return type:xarray.Dataset
len_coords

Get the number of coordinates for this grid.

Returns:len_coords – Number of coordinates for this grid.
Return type:int
lonlatbox(data, ll_box)[source]

Slice a lonlatbox from the given data.

nearest_point(coord)[source]
static normalize_lat_lon(lat, lon, data=None)[source]

The given coordinates will be normalized and reorder into basemap conform coordinates. If the longitude values are between 0° and 360°, they will be normalized to values between -180° and 180°. Then the coordinates will be reorder, such that they are in an increasing order.

Parameters:
  • lat (numpy.ndarray) – The latitude values. They are representing the first data dimension.
  • lon (numpy.ndarray) – The longitude values. They are representing the second data dimension.
  • data (numpy.ndarray, xarray.DataArray or None, optional) – The data values. They will be also reordered by lat and lon. If this is None, only lat and lon will be reordered and returned. Default is None.
Returns:

  • ordered_lat (numpy.ndarray) – Ordered latitude values.
  • ordered_lon (numpy.ndarray) – Ordered and normalized longitude values.
  • ordered_data (numpy.ndarray, xarray.DataArray or None) – The orderd data based on given latitudes and longitudes. This is None if no other data was given as parameter.

raw_dim

Get the raw dimension values, as they are constructed by the grid description.

Returns:constructed_dim – The constructed dimensions. Depending on the given grid type, it is either a tuple of arrays or a single array.
Return type:tuple(numpy.ndarray) or numpy.ndarray
raw_lat_lon()[source]
shape
pymepps.grid.grid.distance_haversine(p1, p2)[source]

Calculate the great circle distance between two points on the earth. The formula is based on the haversine formula [1].

Parameters:
  • p1 (tuple (array_like, array_like)) – The coordinates (latitude, longitude) of the first point in degrees.
  • p2 (tuple (array_like, array_like)) – The coordinates (latitude, longitude) of the second point in degrees.
Returns:

d – The calculated haversine distance in meters.

Return type:

float

Notes

Script based on: http://stackoverflow.com/a/29546836

References

[1](1, 2) de Mendoza y Ríos, Memoria sobre algunos métodos nuevos de calcular la longitud por las distancias lunares: y aplication de su teórica á la solucion de otros problemas de navegacion, 1795.

LonLat

class pymepps.grid.lonlat.LonLatGrid(grid_dict)[source]

Bases: pymepps.grid.grid.Grid

A LonLatGrid is a grid with evenly distributed longitude and latitude values. This is the right grid if the grid could be described with a evenly distributed range of values for longitude and latitude.

lonlatbox(data, ll_box)[source]

The data is sliced as structured grid with given lonlat box.

Parameters:
  • data (numpy.ndarray or xarray.DataArray) – The data which should be sliced. The shape of the last two dimensions should be the same as the grid dimensions.
  • ll_box (tuple(float)) –

    The longitude and latitude box with four entries as degree. The entries are handled in the following way:

    (left/west, top/north, right/east, bottom/south)
Returns:

  • sliced_data (numpy.ndarray or xarray.DataArray) – The sliced data with the same type as the input data. If the input data is a xarray.DataArray the output data will use the same attributes and non-grid dimensions as the input data.
  • sliced_grid (Grid) – A new child instance of Grid with the sliced coordinates as values and the same Grid type as this grid.

Gaussian

class pymepps.grid.gaussian.GaussianGrid(grid_dict)[source]

Bases: pymepps.grid.lonlat.LonLatGrid

The gaussian grid is similar to the lonlat grid. This is the right grid if longitude and/or latitude could be described with a non-evenly distributed list of values.

Projection

class pymepps.grid.projection.BaseProj[source]

BaseProj is a base class for every projection in a proj4-conform way.

transform_from_latlon(lon, lat)[source]

Transform the given lon, lat arrays to x and y values.

transform_to_latlon(x, y)[source]

Transform the given x and y arrays to latitude and longitude values.

class pymepps.grid.projection.ProjectionGrid(grid_dict)[source]

A projection grid could be defined by a evenly distributed grid. The grid could be translated to a longitude and latitude grid by a predefined projection. At the moment only projections defined by a proj4 string or a rotated latitude and longitude are supported.

get_projection()[source]
lonlatbox(data, ll_box)[source]

The data is sliced as unstructured grid with given lonlat box.

Parameters:
  • data (numpy.ndarray or xarray.DataArray) – The data which should be sliced. The shape of the last two dimensions should be the same as the grid dimensions.
  • ll_box (tuple(float)) –

    The longitude and latitude box with four entries as degree. The entries are handled in the following way:

    (left/west, top/north, right/east, bottom/south)
Returns:

  • sliced_data (numpy.ndarray or xarray.DataArray) – The sliced data with the same type as the input data. If the input data is a xarray.DataArray the output data will use the same attributes and non-grid dimensions as the input data.
  • sliced_grid (UnstructuredGrid) – A new UnstructuredGrid with the sliced coordinates as values.

class pymepps.grid.projection.RotPoleProj(npole_lat, npole_lon)[source]

Class for to calculate the transformation from rotated pole coordinates to normal latitude and longitude coordinates. The rotated pole coordinates are calculated in a cf-conform manner, with a rotated north pole. The calculations are based on [1]. If the resulting latitude coordinate equals -90° or 90° the longitude coordinate will be set to 0°.

Parameters:
  • npole_lat (float) – The latitude of the rotated north pole in degrees.
  • npole_lon (float) – The longitude of the rotated north pole in degrees.

References

[1] http://de.mathworks.com/matlabcentral/fileexchange/43435-rotated-grid-
transform
lonlatbox(data, ll_box)[source]

The data is sliced with given lonlat box to a unstructured grid.

Parameters:
  • data (numpy.ndarray) – The data which should be sliced. The shape of the last two dimensions should be the same as the grid dimensions.
  • ll_box (tuple(float)) –

    The longitude and latitude box with four entries as degree. The entries are handled in the following way:

    (left/west, top/north, right/east, bottom/south)
Returns:

  • sliced_data (numpy.ndarray) – The sliced data. The last two dimensions are flattened and sliced.
  • grid (UnstructuredGrid) – A new instance of UnstructuredGrid with the sliced coordinates as values.

north_pole

Get the north pole for the rotated pole projection.

transform_from_lonlat(lon, lat)[source]

Transform the given lon, lat arrays to x and y values.

transform_to_lonlat(x, y)[source]

Transform the given x and y arrays to latitude and longitude values.

Unstructured

class pymepps.grid.unstructured.UnstructuredGrid(grid_dict)[source]

Bases: pymepps.grid.grid.Grid

In an unstructured grid the grid could have any shape. A famous example is the triangulated ICON grid. At the moment the longitude and latitude values should have been precomputed. The grid could be calculated with the number of vertices and the coordinates of the boundary.

get_coord_names()[source]

Returns the name of the coordinates.

Returns:coord_names – The coordinate name for this unstructured grid. This is always a list, with only one entry: ncells.
Return type:list(str)
len_coords

Get the number of coordinates for this grid.

Returns:len_coords – Number of coordinates for this grid.
Return type:int
lonlatbox(data, ll_box)[source]

The data is sliced as unstructured grid with given lonlat box.

Parameters:
  • data (numpy.ndarray or xarray.DataArray) – The data which should be sliced. The shape of the last two dimensions should be the same as the grid dimensions.
  • ll_box (tuple(float)) –

    The longitude and latitude box with four entries as degree. The entries are handled in the following way:

    (left/west, top/north, right/east, bottom/south)
Returns:

  • sliced_data (numpy.ndarray or xarray.DataArray) – The sliced data with the same type as the input data. If the input data is a xarray.DataArray the output data will use the same attributes and non-grid dimensions as the input data.
  • sliced_grid (UnstructuredGrid) – A new UnstructuredGrid with the sliced coordinates as values.

Curvilinear

class pymepps.grid.curvilinear.CurvilinearGrid(grid_dict)[source]

Bases: pymepps.grid.lonlat.LonLatGrid

A curvilinear grid could be described as special case of a lonlat grid where the number of vertices is 4. The raw grid values are calculated based on the given grid rules. At the moment the lon lat values had to be precomputed.

lonlatbox(data, ll_box)[source]

The data is sliced as unstructured grid with given lonlat box.

Parameters:
  • data (numpy.ndarray or xarray.DataArray) – The data which should be sliced. The shape of the last two dimensions should be the same as the grid dimensions.
  • ll_box (tuple(float)) –

    The longitude and latitude box with four entries as degree. The entries are handled in the following way:

    (left/west, top/north, right/east, bottom/south)
Returns:

  • sliced_data (numpy.ndarray or xarray.DataArray) – The sliced data with the same type as the input data. If the input data is a xarray.DataArray the output data will use the same attributes and non-grid dimensions as the input data.
  • sliced_grid (UnstructuredGrid) – A new UnstructuredGrid with the sliced coordinates as values.