pytesmo.grid package¶
Submodules¶
pytesmo.grid.grids module¶
Created on Aug 26, 2013
@author: Christoph Paulik Christoph.Paulik@geo.tuwien.ac.at
- class pytesmo.grid.grids.BasicGrid(lon, lat, gpis=None, subset=None, setup_kdTree=True, shape=None)[source]¶
Bases: object
Grid that just has lat,lon coordinates and can find the nearest neighbour. It can also yield the gpi, lat, lon information in order.
Parameters: lon : numpy.array
longitudes of the points in the grid
lat : numpy.array
latitudes of the points in the grid
gpis : numpy.array, optional
if the gpi numbers are in a different order than the lon and lat arrays an array containing the gpi numbers can be given if no array is given here the lon lat arrays are given gpi numbers starting at 0
subset : numpy.array, optional
if the active part of the array is only a subset of all the points then the subset array which is a index into lon and lat can be given here
setup_kdTree : boolean, optional
if set (default) then the kdTree for nearest neighbour search will be built on initialization
shape : tuple, optional
if given the grid can be reshaped into the given shape this indicates that it is a regular grid and fills the attributes self.londim and self.latdim which define the grid only be the meridian coordinates(self.londim) and the coordinates of the circles of latitude(self.latdim). The shape has to be given as (latdim, londim) It it is not given the shape is set to the length of the input lon and lat arrays.
Attributes
arrlon (numpy.array) array of all longitudes of the grid arrlat (numpy.array) array of all latitudes of the grid n_gpi (int) number of gpis in the grid gpidirect (boolean) if true the gpi number is equal to the index of arrlon and arrlat gpis (numpy.array) gpi number for elements in arrlon and arrlat gpi[i] is located at arrlon[i],arrlat[i] subset (numpy.array) if given then this contains the indices of a subset of the grid. This can be used if only a part of a grid is interesting for a application. e.g. land points, or only a specific country allpoints (boolean) if False only a subset of the grid is active activearrlon (numpy.array) array of longitudes that are active, is defined by arrlon[subset] if a subset is given otherwise equal to arrlon activearrlat (numpy.array) array of latitudes that are active, is defined by arrlat[subset] if a subset is given otherwise equal to arrlat activegpis (numpy.array) array of gpis that are active, is defined by gpis[subset] if a subset is given otherwise equal to gpis issplit (boolean) if True then the array was split in n parts with the self.split function kdTree (object) grid.nearest_neighbor.findGeoNN object for nearest neighbor search shape (tuple, optional) if given during initialization then this is the shape the grid can be reshaped to this only makes sense for regular lat,lon grids latdim (numpy.array, optional) if shape is given this attribute has contains all latitudes that make up the regular lat,lon grid londim (numpy.array, optional) if shape is given this attribute has contains all longitudes that make up the regular lat,lon grid Methods
calc_lut(other[, max_dist, into_subset]) takes other BasicGrid or CellGrid objects and computes find_nearest_gpi(lon, lat[, max_dist]) finds nearest gpi, builds kdTree if it does not yet exist get_bbox_grid_points([latmin, latmax, ...]) Returns all grid points located in a submitted geographic box, get_grid_points(*args) Returns all active grid points gpi2lonlat(gpi) Longitude and Latitude for given GPI. gpi2rowcol(gpi) if the grid can be reshaped into a grid_points(*args) Yields all grid points in order split(n) function splits the grid into n parts to_cell_grid([cellsize, cellsize_lat, ...]) convert grid to cellgrid with a cell partition of unite() unites a split array, so that it can be iterated over as a whole - calc_lut(other, max_dist=inf, into_subset=False)[source]¶
takes other BasicGrid or CellGrid objects and computes a lookup table between them. the lut will have the size of self.n_gpis and will for every grid point have the nearest index into other.arrlon etc.
Parameters: other : grid object
to which to calculate the lut to
max_dist : float, optional
maximum allowed distance in meters
into_subset : boolean, optional
if set the returned lut will have the index into the subset if the other grid is a subset of a grid. Example: if e.g. ind_l is used for the warp_grid some datasets will be given as arrays with len(ind_l) elements. These datasets can not be indexed with gpi numbers but have to be indexed with indices into the subset
- find_nearest_gpi(lon, lat, max_dist=inf)[source]¶
finds nearest gpi, builds kdTree if it does not yet exist
Parameters: lon : float
longitude of point
lat : float
latitude of point
Returns: gpi : long
grid point index
distance : float
distance of gpi to given lon, lat At the moment not on a great circle but in spherical cartesian coordinates
- get_bbox_grid_points(latmin=-90, latmax=90, lonmin=-180, lonmax=180, coords=False)[source]¶
Returns all grid points located in a submitted geographic box, optinal as coordinates
Parameters: latmin : float, optional
minimum latitude
latmax : float, optional
maximum latitude
lonmin : float, optional
minimum latitude
lonmax : float, optional
maximum latitude
coords : boolean, optional
set to True if coordinates should be returned
Returns: gpi : numpy.array
grid point indices, if coords=False
lat : numpy.array
longitudes of gpis, if coords=True
lon : numpy.array
longitudes of gpis, if coords=True
- get_grid_points(*args)[source]¶
Returns all active grid points
Parameters: n : int, optional
if the grid is split in n parts using the split function then this function will only return the nth part of the grid
Returns: gpis : numpy.array
arrlon : numpy.array
arrlat :numpy.array
- gpi2lonlat(gpi)[source]¶
Longitude and Latitude for given GPI.
Parameters: gpi : int32
Grid Point Index.
Returns: lon : float
Longitude (deg) of GPI.
lat : float
- gpi2rowcol(gpi)[source]¶
if the grid can be reshaped into a sensible 2D shape then this function gives the row(latitude dimension) and column(longitude dimension) indices of the gpi in the 2D grid
Parameters: gpi : int32
Grid Point Index.
Returns: row : int
row in 2D array
col : int
column in 2D array
- grid_points(*args)[source]¶
Yields all grid points in order
Parameters: n : int, optional
if the grid is split in n parts using the split function then this iterator will only iterate of the nth part of the grid
Returns: gpi : long
grid point index
lon : float
longitude of gpi
lat : float
longitude of gpi
- split(n)[source]¶
function splits the grid into n parts this changes not function but grid_points() which takes the argument n and will only iterate through this part of the grid
Parameters: n : int
number of parts the grid should be split into
- to_cell_grid(cellsize=5.0, cellsize_lat=None, cellsize_lon=None)[source]¶
convert grid to cellgrid with a cell partition of cellsize
Parameters: cellsize : float, optional
cell size in degrees
cellsize_lon : float, optional
Cell size in degrees on the longitude axis
cellsize_lat : float, optional
Cell size in degrees on the latitude axis
Returns: cell_grid : CellGrid object
cell grid object
- class pytesmo.grid.grids.CellGrid(lon, lat, cells, gpis=None, subset=None, setup_kdTree=False, **kwargs)[source]¶
Bases: pytesmo.grid.grids.BasicGrid
Grid that has lat,lon coordinates as well as cell informatin. It can find nearest neighbour. It can also yield the gpi, lat, lon, cell information in cell order. This is important if the data on the grid is saved in cell files on disk as we can go through all grid points with optimized IO performance
Parameters: lon : numpy.array
longitudes of the points in the grid
lat : numpy.array
latitudes of the points in the grid
cells : numpy.array
of same shape as lon and lat, containing the cell number of each gpi
gpis : numpy.array, optional
if the gpi numbers are in a different order than the lon and lat arrays an array containing the gpi numbers can be given
subset : numpy.array, optional
if the active part of the array is only a subset of all the points then the subset array which is a index into lon, lat and cells can be given here
Attributes
arrcell (numpy.array) array of cell number with same shape as arrlon,arrlat activearrcell (numpy.array) array of longitudes that are active, is defined by arrlon[subset] if a subset is given otherwise equal to arrlon Methods
calc_lut(other[, max_dist, into_subset]) takes other BasicGrid or CellGrid objects and computes find_nearest_gpi(lon, lat[, max_dist]) finds nearest gpi, builds kdTree if it does not yet exist get_bbox_grid_points([latmin, latmax, ...]) Returns all grid points located in a submitted geographic box, get_cells() function to get all cell numbers of the grid get_grid_points(*args) Returns all active grid points gpi2cell(gpi) Cell for given GPI. gpi2lonlat(gpi) Longitude and Latitude for given GPI. gpi2rowcol(gpi) if the grid can be reshaped into a grid_points(*args) Yields all grid points in order grid_points_for_cell(cell) get all grid points for a given cell number split(n) function splits the grid into n parts to_cell_grid([cellsize, cellsize_lat, ...]) convert grid to cellgrid with a cell partition of unite() unites a split array, so that it can be iterated over as a whole - get_cells()[source]¶
function to get all cell numbers of the grid
Returns: cells : numpy.array
unique cell numbers
- get_grid_points(*args)[source]¶
Returns all active grid points
Parameters: n : int, optional
if the grid is split in n parts using the split function then this function will only return the nth part of the grid
Returns: gpis : numpy.array
arrlon : numpy.array
arrlat :numpy.array
cells : numpy.array
- gpi2cell(gpi)[source]¶
Cell for given GPI.
Parameters: gpi : int32
Grid Point Index.
Returns: cell : int
Cell number of GPI.
Raises: IndexError
if gpi is not found
- exception pytesmo.grid.grids.GridDefinitionError[source]¶
Bases: exceptions.Exception
- exception pytesmo.grid.grids.GridIterationError[source]¶
Bases: exceptions.Exception
- pytesmo.grid.grids.genreg_grid(grd_spc_lat=1, grd_spc_lon=1, minlat=-90.0, maxlat=90.0, minlon=-180.0, maxlon=180.0)[source]¶
Define a global regular lon lat grid which starts in the North Western Corner of minlon, maxlat. The grid points are defined to be in the middle of a grid cell. e.g. the first point on a 1x1 degree grid with minlon -180.0 and maxlat 90.0 will be at -179.5 longitude, 89.5 latitude
Parameters: grd_spc_lat: float, optional
grid spacing in latitude direction
grd_spc_lon: float, optional
grid spacing in longitude direction
minlat : float, optional
minimum latitude of the grid
maxlat : float, optional
maximum latitude of the grid
minlon : float, optional
minimum longitude of the grid
maxlon : float, optional
maximum longitude of the grid
- pytesmo.grid.grids.gridfromdims(londim, latdim)[source]¶
Defines new grid object from latitude and longitude dimensions. latitude and longitude dimensions are 1D arrays that give the latitude and longitude values of a 2D latitude-longitude array
Parameters: londim: numpy.array
longitude dimension
latdim: numpy.array
latitude dimension
- pytesmo.grid.grids.lonlat2cell(lon, lat, cellsize=5.0, cellsize_lon=None, cellsize_lat=None)[source]¶
Partition lon, lat points into cells.
Parameters: lat: float64, or numpy.array
Latitude.
lon: float64, or numpy.array
Longitude.
cellsize: float
Cell size in degrees
cellsize_lon : float, optional
Cell size in degrees on the longitude axis
cellsize_lat : float, optional
Cell size in degrees on the latitude axis
Returns: cell: int32, or numpy.array
Cell.
pytesmo.grid.nearest_neighbor module¶
Created on Jul 30, 2013
@author: Christoph Paulik christoph.paulik@geo.tuwien.ac.at
- class pytesmo.grid.nearest_neighbor.findGeoNN(lon, lat, R=6370997.0, grid=False, kd_tree_name='pykdtree')[source]¶
Bases: object
class that takes lat,lon coordinates, transformes them to cartesian (X,Y,Z) coordinates and provides a interface to scipy.spatial.kdTree as well as pykdtree if installed
Parameters: lon : numpy.array or list
longitudes of the points in the grid
lat : numpy.array or list
latitudes of the points in the grid
R : float, optional
Radius of the earth to use for calculating 3D coordinates
grid : boolean, optional
if True then lon and lat are assumed to be the coordinates of a grid and will be used in numpy.meshgrid to get coordinates for all grid points
kd_tree_name : string, optional
name of kdTree implementation to use, either ‘pykdtree’ to use pykdtree or ‘scipy’ to use scipy.spatial.kdTree Fallback is always scipy if any other string is given or if pykdtree is not installed. standard is pykdtree since it is faster
Attributes
R (float) earth radius to use in computation of x,y,z coordinates coords (numpy.array) 3D array of cartesian x,y,z coordinates kd_tree_name: string name of kdTree implementation to use, either ‘pykdtree’ to use pykdtree or ‘scipy’ to use scipy.spatial.kdTree Fallback is always scipy if any other string is given or if pykdtree is not installed kdtree: object kdTree object that is built only once and saved in this attribute Methods
find_nearest_index(lon,lat) finds the nearest neighbor of the given lon,lat coordinates in the lon,lat arrays given during initialization and returns the index of the nearest neighbour in those arrays. - find_nearest_index(lon, lat, max_dist=inf)[source]¶
finds nearest index, builds kdTree if it does not yet exist
Parameters: lon : float, list or numpy.array
longitude of point
lat : float, list or numpy.array
latitude of point
max_dist : float, optional
maximum distance to consider for search
Returns: d : float, numpy.array
distances of query coordinates to the nearest grid point, distance is given in cartesian coordinates and is not the great circle distance at the moment. This should be OK for most applications that look for the nearest neighbor which should not be hundreds of kilometers away.
ind : int, numpy.array
indices of nearest neighbor
index_lon : numpy.array, optional
if self.grid is True then return index into lon array of grid definition
index_lat : numpy.array, optional
if self.grid is True then return index into lat array of grid definition
pytesmo.grid.netcdf module¶
Created on Jan 21, 2014
Module for saving grid to netCDF
@author: Christoph Paulik christoph.paulik@geo.tuwien.ac.at
- pytesmo.grid.netcdf.load_grid(filename)[source]¶
load a grid from netCDF file
Parameters: filename : string
filename
Returns: grid : BasicGrid or CellGrid instance
grid instance initialized with the loaded data
- pytesmo.grid.netcdf.save_grid(filename, grid, subset_name='land_points', subset_meaning='water land', global_attrs=None)[source]¶
save a BasicGrid or CellGrid to netCDF it is assumed that a subset should be used as land_points
Parameters: filename : string
name of file
grid : BasicGrid or CellGrid object
grid whose definition to save to netCDF
subset_name : string, optional
long_name of the variable ‘subset_flag’ if the subset symbolises something other than a land/sea mask
subset_meaning : string, optional
will be written into flag_meanings metadata of variable ‘subset_flag’
global_attrs : dict, optional
if given will be written as global attributs into netCDF file
- pytesmo.grid.netcdf.save_lonlat(filename, arrlon, arrlat, arrcell=None, gpis=None, subset_points=None, subset_name='land_points', subset_meaning='water land', global_attrs=None)[source]¶
saves grid information to netCDF file
Parameters: filename : string
name of file
arrlon : numpy.array
array of longitudes
arrlat : numpy.array
array of latitudes
arrcell : numpy.array, optional
array of cell numbers
gpis : numpy.array, optional
gpi numbers if not index of arrlon, arrlat
subset_points : numpy.array, optional
and indication if the given grid point is over land must be an indices into arrlon, arrlat
subset_name : string, optional
long_name of the variable ‘subset_flag’ if the subset symbolises something other than a land/sea mask
subset_meaning : string, optional
will be written into flag_meanings metadata of variable ‘subset_flag’
global_attrs : dict, optional
if given will be written as global attributs into netCDF file
pytesmo.grid.resample module¶
Created on Mar 25, 2014
@author: Christoph Paulik christoph.paulik@geo.tuwien.ac.at
- pytesmo.grid.resample.resample_to_grid(input_data, src_lon, src_lat, target_lon, target_lat, methods='nn', weight_funcs=None, min_neighbours=1, search_rad=18000, neighbours=8, fill_values=None)[source]¶
resamples data from dictionary of numpy arrays using pyresample to given grid. Searches for the neighbours and then resamples the data to the grid given in togrid if at least min_neighbours neighbours are found
Parameters: input_data : dict of numpy.arrays
src_lon : numpy.array
longitudes of the input data
src_lat : numpy.array
src_latitudes of the input data
target_lon : numpy.array
longitudes of the output data
target_src_lat : numpy.array
src_latitudes of the output data
methods : string or dict, optional
method of spatial averaging. this is given to pyresample and can be ‘nn’ : nearest neighbour ‘custom’ : custom weight function has to be supplied in weight_funcs see pyresample documentation for more details can also be a dictionary with a method for each array in input data dict
weight_funcs : function or dict of functions, optional
if method is ‘custom’ a function like func(distance) has to be given can also be a dictionary with a function for each array in input data dict
min_neighbours: int, optional
if given then only points with at least this number of neighbours will be resampled Default : 1
search_rad : float, optional
search radius in meters of neighbour search Default : 18000
neighbours : int, optional
maximum number of neighbours to look for for each input grid point Default : 8
fill_values : number or dict, optional
if given the output array will be filled with this value if no valid resampled value could be computed, if not a masked array will be returned can also be a dict with a fill value for each variable
Returns
——-
data : dict of numpy.arrays
resampled data on given grid
Raises
——
ValueError :
if empty dataset is resampled