avni.tools.xarray module#

avni.tools.xarray.xarray_to_epix(data: Union[xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset], latname: str = 'latitude', lonname: str = 'longitude') numpy.ndarray[source]#

Convert multi-dimensional pixel grid in xarray to extended pixel (.epix) format

Parameters
datatp.Union[xr.DataArray,xr.Dataset]

Multi-dimensional pixel grid in xarray formats

latnamestr, optional

Name to use for latitude column in named numpy array, by default ‘latitude’

lonnamestr, optional

Name to use for longitude column in named numpy array, by default ‘longitude’

Returns
np.ndarray

Array containing (latitude, longitude, pixel_size, value)

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.epix_to_xarray(epixarr: numpy.ndarray, latname: str = 'latitude', lonname: str = 'longitude') xarray.core.dataarray.DataArray[source]#

Convert extended pixel (.epix) to multi-dimensional pixel grid in xarray format

Parameters
epixarrnp.ndarray

Array containing (latitude, longitude, pixel_size, value)

latnamestr, optional

Name to use for latitude column in named numpy array, by default ‘latitude’

lonnamestr, optional

Name to use for longitude column in named numpy array, by default ‘longitude’

Returns
xr.DataArray

Multi-dimensional pixel grid in xarray formats

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.tree3D(treefile: str, latitude: Union[None, list, tuple, numpy.ndarray] = None, longitude: Union[None, list, tuple, numpy.ndarray] = None, radius_in_km: Union[None, list, tuple, numpy.ndarray] = None)[source]#

Build a KD-tree at specific locations

Parameters
treefilestr

Name of the file where tree is (or will be) stored

latitudetp.Union[list,tuple,np.ndarray], optional

Latitudes of locations queried, by default None

longitudetp.Union[list,tuple,np.ndarray], optional

Longitudes of locations queried, by default None

radius_in_kmtp.Union[list,tuple,np.ndarray], optional

Radii of locations queried, by default None

Returns
tree

A scipy.spatial.cKDTree()

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.querytree3D(tree, latitude: Union[list, tuple, numpy.ndarray], longitude: Union[list, tuple, numpy.ndarray], radius_in_km: Union[list, tuple, numpy.ndarray], values: Union[None, scipy.sparse.csc.csc_matrix, scipy.sparse.csr.csr_matrix, list, tuple, numpy.ndarray] = None, nearest: int = 1)[source]#

Query a KD-tree for values at specific locations

Parameters
tree

A scipy.spatial.cKDTree()

latitudetp.Union[list,tuple,np.ndarray], optional

Latitudes of locations queried, by default None

longitudetp.Union[list,tuple,np.ndarray], optional

Longitudes of locations queried, by default None

radius_in_kmtp.Union[list,tuple,np.ndarray], optional

Radii of locations queried, by default None

valuestp.Union[None,sparse.csc_matrix,sparse.csr_matrix,list,tuple,np.ndarray], optional

Values at each KD-tree point, by default None

nearestint, optional

Number of nearest values in the KD-tree to interpolated from, by default 1 so nearest

Returns
inds or interp, inds

indices of the nearest points in te KD-tree and the interporlated value (if values is not None)

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.get_stride(resolution: str) int[source]#

Get the stride to use for various resolution of plotting. This dictates downsampling before interpolation.

Parameters
resolutionstr

Resolution of boundary database to use in Basemap. Can be c (crude), l (low), i (intermediate), h (high), f (full)

Returns
int

stride to use for resolution

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.ncfile2tree3D(ncfile: str, treefile: str, lonlatdepth: list = ['longitude', 'latitude', 'depth'], resolution: str = 'h', radius_in_km: Union[None, list, tuple, numpy.ndarray] = None)[source]#

Read or write a pickle interpolant with KD-tree

Parameters
ncfilestr

Name of the topography file in NETCDF4 format

treefilestr

Name of the file where tree is (or will be) stored

lonlatdepthlist, optional

A list of variable names of the longitude, latitude, depth (in km) arrays, by default [‘longitude’,’latitude’,’depth’]

resolutionstr, optional

Dictates downsampling before interpolation, by default ‘h’

radius_in_kmtp.Union[None,list,tuple,np.ndarray], optional

Radius in kilometer when a 2D surface. Ignores the 3rd field in lonlatdepth, by default None

Returns
tree

A scipy.spatial.cKDTree()

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.readtopography(model: Union[None, str] = None, resolution: str = 'h', field: str = 'z', latitude: str = 'lat', longitude: str = 'lon', latitude_limits: list = [- 90, 90], longitude_limits: list = [- 180, 180], dbs_path: Union[None, str] = None)[source]#

Read standard topography file in NETCDF4 format specified in constants()

Parameters
modelstr, optional

Name of the topography file in NETCDF4 format, by default constants.topography()

resolutionstr, optional

Dictates downsampling before interpolation, by default ‘h’

fieldstr, optional

Field name in the NETCDF4 file to use, by default ‘z’

latitudestr, optional

Name to use for latitude column in named numpy array, by default ‘lat’

longitudestr, optional

Name to use for longitude column in named numpy array, by default ‘lon’

latitude_limitslist, optional

Limit for restricting the domain for reading topography, by default [-90,90]

longitude_limitslist, optional

Limit for restricting the domain for reading topography, by default [-180,180]

dbs_pathtp.Union[None,str], optional

Database path to the folder contianing topography file, by default None so takes the value in constants.topofolder()

Returns
xr.Dataset

Multi-dimensional pixel grid in xarray formats

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.checkxarray(data: Union[xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset], latname: str = 'latitude', lonname: str = 'longitude', Dataset=True) Tuple[list, float, tuple][source]#

Checks whether the data input is a DataArray and the coordinates are compatible

Parameters
datatp.Union[xr.DataArray,xr.Dataset]

Multi-dimensional pixel grid in xarray formats

latnamestr, optional

Name to use for latitude column in named numpy array, by default ‘latitude’

lonnamestr, optional

Name to use for longitude column in named numpy array, by default ‘longitude’

Datasetbool, optional

Allow Dataset or not, by default True

Returns
tp.Tuple[list,float,tuple]

First element is a list of error warnings while performing checks

Second element is size of the uniform pixel.

Third element is a tuple containing the shape of the grid

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.areaxarray(data: Union[xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset], latname: str = 'latitude', lonname: str = 'longitude', pix_width: Union[None, numpy.ndarray] = None) xarray.core.dataarray.DataArray[source]#

Calculate area for multi-dimensional pixel grid in xarray formats

Parameters
datatp.Union[xr.DataArray,xr.Dataset]

Multi-dimensional pixel grid in xarray formats

latnamestr, optional

Name to use for latitude column, by default ‘latitude’

lonnamestr, optional

Name to use for longitude column, by default ‘longitude’

pix_widthtp.Union[None,np.ndarray], optional

Width of pixels if not the default derived from data, by default None so is derived

Returns
xr.DataArray

A DataArray object with area of each pixel

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00

avni.tools.xarray.meanxarray(data: Union[xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset], area: Union[None, xarray.core.dataarray.DataArray] = None, latname: str = 'latitude', lonname: str = 'longitude', pix_width: Union[None, numpy.ndarray] = None) Tuple[float, xarray.core.dataarray.DataArray, float][source]#

Calculate geographically weighted average of a multi-dimensional pixel grid in xarray formats

Parameters
datatp.Union[xr.DataArray,xr.Dataset]

Multi-dimensional pixel grid in xarray formats to average over

areatp.Union[None,xr.DataArray], optional

Area of each pixel, by default None so calculated on the fly

latnamestr, optional

Name to use for latitude column, by default ‘latitude’

lonnamestr, optional

Name to use for longitude column, by default ‘longitude’

pix_widthtp.Union[None,np.ndarray], optional

Width of pixels if not the default derived from data, by default None so is derived

Returns
tp.Tuple[float,xr.DataArray,float]

First element is the global average Second element is a DataArray containing area weights for each pixel Third element is the percentage of global area covered by this basis set

Authors

Raj Moulik (moulik@caa.columbia.edu)

Last Modified

2023.02.16 5.00