nbtools.map

Functions/classes relative to maps

Functions

inpolygon(xin_2d, yin_2d, x_pol, y_pol) Determines whether points of a 2D-grid are within a polygon.
lonflip(lon, data) Reorders a longitude and a data array about the central longitude
make_bmap(lon, lat, **kwargs) Creates a basemap object by using the limites of input lon/lat arrays.

Classes

Basemap([llcrnrlon, llcrnrlat, urcrnrlon, ...])
BoundaryNorm(boundaries, ncolors[, clip]) Generate a colormap index based on discrete intervals.
Lambert(lonmin, lonmax, latmin, latmax[, ...]) Class for the handling of Lambert Conic Projection.
ListedColormap(colors[, name, N]) Colormap object generated from a list of colors.
class nbtools.map.Lambert(lonmin, lonmax, latmin, latmax, resolution='l')[source]

Class for the handling of Lambert Conic Projection.

It is initialised by providing the bounding longitudes and latitudes of the domain to plot. It may also take the resolution of the mpl_toolkits.basemap.Basemap class.

Parameters:
  • lonmin (float) – minimum longitude
  • lonmax (float) – maximum longitude
  • latmin (float) – minimum latitude
  • latmax (float) – maximum latitude
  • resolution (str) – resolution of the map (cf Basemap)
add_lc_labels(labelleft=True, labelright=True, spacelat=10, spacelon=10, zorder=10001, nbspaces=15, **kwargs)[source]

Add the longitude and latitude labels on masked projection.

Adapted from NCL

Note

Does not work if usetex=True. Hence a call to this function sets it to False

Parameters:
  • labelleft (bool) – defines whether latitudes labels on the left of the map should be displayed
  • labelright (bool) – defines whether latitudes on the right of the map should be displayed
  • spacelat (float) – Latitude spacing between the labels
  • spacelon (float) – Longitude spacing between the labels
  • zorder (int) – plot order when the labels are drawn
  • nbspaces (int) – number of whitespaces to add at the end (for left labels) or at the beginning (for right labels) of the string
  • **kwargs (dict) – Text keyword arguments (fontsize for instance)
Returns:

None

make_mask(mask_res=300, zorder=1000, edcol='k', bgcolor='white', **kwargs)[source]

Masks the Lambert Conic projection.

It creates a dummy NxN array, which is then overlayed to the map using the imshow function. Data inside the data are plotted in transparent, data outside the domains are plotted in white. Finally, the domain boundaries are drawn.

Parameters:
  • mask_res (int) – size of the 2-D array used to draw the mask
  • zorder (int) – the plot order at which the mask will be drawn
  • edcol (str) – color of the domain boundaries
  • bgcolor (str) – color of the mask
  • **kwargs (dict) – keyword argument for the boundary lines
nbtools.map.inpolygon(xin_2d, yin_2d, x_pol, y_pol)[source]

Determines whether points of a 2D-grid are within a polygon.

Equivalent to the inpolygon function of Matlab.

Note

If the input polygon is not closed, it is automatically closed

Parameters:
  • xin_2d (numpy.array) – 2-D array with the x-coords of the domain
  • yin_2d (numpy.array) – 2-D array with the y-coords of the domain
  • x_pol (numpy.array) – 1-D array with the x-coords of the polygon
  • y_pol (numpy.array) – 1-D array with the y-coords of the polygon
Returns:

A 2D array (same shape as xin_2d and yin_2d) with 1 when the point is within the polygon, else 0.

Return type:

numpy.array

nbtools.map.lonflip(lon, data)[source]

Reorders a longitude and a data array about the central longitude

Adapted from the NCL lonFlip function.

Parameters:
  • lon (numpy.array) – 1D longitude array. The number of longitudes must be even
  • data (numpy.array) – the data array. Can be 1D, 2D, 3D or 4D. Longitude must be the last dimension
Returns:

A tuple with the first element containing the flipped longitude array, and the second element containing the flipped data array

Return type:

tuple

nbtools.map.make_bmap(lon, lat, **kwargs)[source]

Creates a basemap object by using the limites of input lon/lat arrays.

Lower left corner coordinates = min(lon), min(lat)
Upper right corner coordinates = max(lon), max(lat)
Parameters:
  • lon (numpy.array) – longitude array (any dimension)
  • lat (numpy.array) – latitude array (any dimension)
  • **kwargs (dict) – keyword arguments that are passed to the Basemap class (proj, resolution, etc).
Returns:

A Basemap object

Return type:

mpl_toolkits.basemap.Basemap