geoist.others package¶
Submodules¶
geoist.others.addcopyfighandler module¶
Monkey-patch plt.figure() to support Ctrl+C for copying to clipboard as an image
@author: Josh Burnett Modified from code found on Stack Exchange:
geoist.others.datarepo module¶
Functions to download, verify, and update a sample dataset.
-
class
geoist.others.datarepo.
Drepo
(path, base_url, registry=None, urls=None)[源代码]¶ 基类:
object
Manager for a local data storage that can fetch from a remote source.
- 参数
path (str) -- The path to the local data storage folder. The path must exist in the file system.
base_url (str) -- Base URL for the remote data source. All requests will be made relative to this URL.
registry (dict or None) -- A record of the files that are managed by this good boy. Keys should be the file names and the values should be their SHA256 hashes. Only files in the registry can be fetched from the local storage. Files in subdirectories of path must use Unix-style separators (
'/'
) even on Windows.urls (dict or None) -- Custom URLs for downloading individual files in the registry. A dictionary with the file names as keys and the custom URLs as values. Not all files in registry need an entry in urls. If a file has an entry in urls, the base_url will be ignored when downloading it in favor of
urls[fname]
.
-
property
abspath
¶ Absolute path to the local storage
-
fetch
(fname)[源代码]¶ Get the absolute path to a file in the local storage.
If it's not in the local storage, it will be downloaded. If the hash of the file in local storage doesn't match the one in the registry, will download a new copy of the file. This is considered a sign that the file was updated in the remote storage. If the hash of the downloaded file still doesn't match the one in the registry, will raise an exception to warn of possible file corruption.
- 参数
fname (str) -- The file name (relative to the base_url of the remote data storage) to fetch from the local storage.
- 返回
full_path -- The absolute path (including the file name) of the file in the local storage.
- 返回类型
str
-
load_registry
(fname)[源代码]¶ Load entries form a file and add them to the registry.
Use this if you are managing many files.
Each line of the file should have file name and its SHA256 hash separate by a space. Only one file per line is allowed. Custom download URLs for individual files can be specified as a third element on the line.
- 参数
fname (str) -- File name and path to the registry file.
-
geoist.others.datarepo.
create
(path, base_url, version=None, version_dev='master', env=None, registry=None, urls=None)[源代码]¶ Create a new
Drepo
with sensible defaults to fetch data files.If a version string is given, the Drepo will be versioned, meaning that the local storage folder and the base URL depend on the projection version. This is necessary if your users have multiple versions of your library installed (using virtual environments) and you updated the data files between versions. Otherwise, every time a user switches environments would trigger a re-download of the data. The version string will be appended to the local storage path (for example,
~/.myDrepo/cache/v0.1
) and inserted into the base URL (for example,https://github.com/fatiando/Drepo/raw/v0.1/data
). If the version string contains+XX.XXXXX
, it will be interpreted as a development version.- 参数
path (str, PathLike, list or tuple) -- The path to the local data storage folder. If this is a list or tuple, we'll join the parts with the appropriate separator. The version will be appended to the end of this path. Use
Drepo.os_cache
for a sensible default.base_url (str) -- Base URL for the remote data source. All requests will be made relative to this URL. The string should have a
{version}
formatting mark in it. We will call.format(version=version)
on this string. If the URL is a directory path, it must end in a'/'
because we will not include it.version (str or None) -- The version string for your project. Should be PEP440 compatible. If None is given, will not attempt to format base_url and no subfolder will be appended to path.
version_dev (str) -- The name used for the development version of a project. If your data is hosted on Github (and base_url is a Github raw link), then
"master"
is a good choice (default). Ignored if version is None.env (str or None) -- An environment variable that can be used to overwrite path. This allows users to control where they want the data to be stored. We'll append version to the end of this value as well.
registry (dict or None) -- A record of the files that are managed by this Drepo. Keys should be the file names and the values should be their SHA256 hashes. Only files in the registry can be fetched from the local storage. Files in subdirectories of path must use Unix-style separators (
'/'
) even on Windows.urls (dict or None) -- Custom URLs for downloading individual files in the registry. A dictionary with the file names as keys and the custom URLs as values. Not all files in registry need an entry in urls. If a file has an entry in urls, the base_url will be ignored when downloading it in favor of
urls[fname]
.
- 返回
Drepo -- The
Drepo
initialized with the given arguments.- 返回类型
Drepo
实际案例
Create a
Drepo
for a release (v0.1):>>> pup = create(path="myproject", ... base_url="http://some.link.com/{version}/", ... version="v0.1", ... registry={"data.txt": "9081wo2eb2gc0u..."}) >>> print(pup.path.parts) # The path is a pathlib.Path ('myproject', 'v0.1') >>> print(pup.base_url) http://some.link.com/v0.1/ >>> print(pup.registry) {'data.txt': '9081wo2eb2gc0u...'}
If this is a development version (12 commits ahead of v0.1), then the
version_dev
will be used (defaults to"master"
):>>> pup = create(path="myproject", ... base_url="http://some.link.com/{version}/", ... version="v0.1+12.do9iwd") >>> print(pup.path.parts) ('myproject', 'master') >>> print(pup.base_url) http://some.link.com/master/
Versioning is optional (but highly encouraged):
>>> pup = create(path="myproject", ... base_url="http://some.link.com/", ... registry={"data.txt": "9081wo2eb2gc0u..."}) >>> print(pup.path.parts) # The path is a pathlib.Path ('myproject',) >>> print(pup.base_url) http://some.link.com/
To place the storage folder at a subdirectory, pass in a list and we'll join the path for you using the appropriate separator for your operating system:
>>> pup = create(path=["myproject", "cache", "data"], ... base_url="http://some.link.com/{version}/", ... version="v0.1") >>> print(pup.path.parts) ('myproject', 'cache', 'data', 'v0.1')
The user can overwrite the storage path by setting an environment variable:
>>> # The variable is not set so we'll use *path* >>> pup = create(path=["myproject", "not_from_env"], ... base_url="http://some.link.com/{version}/", ... version="v0.1", ... env="MYPROJECT_DATA_DIR") >>> print(pup.path.parts) ('myproject', 'not_from_env', 'v0.1') >>> # Set the environment variable and try again >>> import os >>> os.environ["MYPROJECT_DATA_DIR"] = os.path.join("myproject", "from_env") >>> pup = create(path=["myproject", "not_from_env"], ... base_url="http://some.link.com/{version}/", ... version="v0.1", ... env="MYPROJECT_DATA_DIR") >>> print(pup.path.parts) ('myproject', 'from_env', 'v0.1')
geoist.others.dataset module¶
DataSet
-
class
geoist.others.dataset.
DataSet
[源代码]¶ 基类:
object
-
abstract
getBounds
()[源代码]¶ Return the lon/lat range of the data.
- 返回
Tuple of (lonmin,lonmax,latmin,latmax)
-
abstract
getData
(getCopy=False)[源代码]¶ Return a reference to or copy of the data inside the Grid
- 参数
getCopy -- True indicates that the user wants a copy of the data, not a reference to it.
- 返回
A reference to or copy of a numpy array of data.
-
abstract
getValue
(lat, lon, method='nearest', default=None)[源代码]¶ Return numpy array at given latitude and longitude (using given resampling method).
- 参数
lat -- Latitude (in decimal degrees) of desired data value.
lon -- Longitude (in decimal degrees) of desired data value.
method -- Interpolation method, one of ('nearest','linear','cubic','quintic')
default -- Default value to return when lat/lon is outside of grid bounds.
- 返回
Value at input latitude,longitude position.
-
abstract
interpolateToGrid
(geodict, method='linear')[源代码]¶ Given a geodict specifying a grid extent and resolution, resample current data set to match.
- 参数
geodict -- geodict object from a grid whose extents are inside the extent of this grid.
method -- Optional interpolation method - ['linear', 'cubic','quintic','nearest']
- 返回
Interpolated grid.
- 引发
DataSetException -- If the Grid object upon which this function is being called is not completely contained by the grid to which this Grid is being resampled.
DataSetException -- If the resulting interpolated grid shape does not match input geodict.
This function modifies the internal griddata and geodict object variables.
-
abstract
load
(bounds=None, resample=False, padValue=None)[源代码]¶ Load data into a Grid subclass. Parameters below are suggested for subclasses.
- 参数
filename -- File where data is stored
bounds -- Optional tuple of (lonmin,lonmax,latmin,latmax) used to subset data from file.
resample -- If subsetting data, True indicates that exact bounds are desired and data should be resampled to fit.
padValue -- If asking for data outside bounds of grid, any value not None means fill in those cells with padValue. None means don't pad the grid at all.
- 引发
NotImplementedError -- Always for this base class.
-
abstract
save
(filename)[源代码]¶ Save the data contained in the grid to a format specific file. Other attributes may be required for format specific files.
- 参数
filename -- Where file containing data should be written.
-
abstract
setData
(data)[源代码]¶ Modify the data inside the Grid.
- 参数
data -- numpy array of desired data.
-
abstract
trim
(geodict, resample=False, method='linear')[源代码]¶ Trim data to a smaller set of bounds, resampling if requested. If not resampling, data will be trimmed to smallest grid boundary possible.
- 参数
geodict -- GeoDict object used to specify subset bounds and resolution (if resample is selected)
resample -- Boolean indicating whether the data should be resampled to exactly match input bounds.
method -- If resampling, method used, one of ('linear','nearest','cubic','quintic')
-
abstract
geoist.others.fetch_data module¶
Functions to load sample data
-
geoist.others.fetch_data.
delete_downloads
()[源代码]¶ Delete all downloaded examples to free space or update the files.
-
geoist.others.fetch_data.
fetch_catalog
(filename)[源代码]¶ Fetch earthquake catalog data from Github Repos.
-
geoist.others.fetch_data.
fetch_catalogCN
()[源代码]¶ Fetch Chinese earthquake catalog data from Github Repos.
-
geoist.others.fetch_data.
fetch_catalogGEM
()[源代码]¶ Fetch GEM Global earthquake catalog data from Github Repos.
-
geoist.others.fetch_data.
setup_baja_bathymetry_map
(ax, region=245.0, 254.705, 20.0, 29.99, land='gray', ocean=None)[源代码]¶
-
geoist.others.fetch_data.
setup_california_gps_map
(ax, region=235.2, 245.3, 31.9, 42.3, land='gray', ocean='skyblue')[源代码]¶
-
geoist.others.fetch_data.
setup_cn_bathymetry_map
(ax, region=100.0, 112.0, 40.0, 55.0, land='gray', ocean=None)[源代码]¶ Setup a Cartopy map for the bathymetry dataset.
- 参数
ax (matplotlib Axes) -- The axes where the map is being plotted.
region (list = [W, E, S, N]) -- The boundaries of the map region in the coordinate system of the data.
land (str or None) -- The name of the color of the land feature or None to omit it.
ocean (str or None) -- The name of the color of the ocean feature or None to omit it.
-
geoist.others.fetch_data.
setup_texas_wind_map
(ax, region=- 107, - 93, 25.5, 37, land='#dddddd', borders=0.5, states=0.1)[源代码]¶
-
geoist.others.fetch_data.
usgs_catalog
(usgsfile, stime, etime, minlat='-90', maxlat='90', minlon='-180', maxlon='180', minmag='5', maxmag='10', mind='0', maxd='1000', catformat='csv')[源代码]¶ 获取USGS地震目录
- 参数
usgsfile (string) -- 保存本地文件名.
stime (string) -- 起始日期.
etime (string) -- 终止日期.
minlat (string , optional) -- 起始纬度. The default is '-90'.
maxlat (string , optional) -- 终止纬度. The default is '90'.
minlon (string , optional) -- 起始经度. The default is '-180'.
maxlon (string , optional) -- 终止经度. The default is '180'.
minmag (string , optional) -- 最小震级. The default is '5'.
maxmag (string, optional) -- 最小震级.. The default is '10'.
mind (string, optional) -- 最小深度. The default is '0'.
maxd (string, optional) -- 最大深度. The default is '1000'.
catformat (string, optional) -- 返回格式. The default is 'csv'.
- 返回
保存本地文件路径.
- 返回类型
string
geoist.others.findnearest module¶
-
geoist.others.findnearest.
find_nearest
(x, x0) → Tuple[int, Any][源代码]¶ This find_nearest function does NOT assume sorted input
inputs: x: array (float, int, datetime, h5py.Dataset) within which to search for x0 x0: singleton or array of values to search for in x
outputs: idx: index of flattened x nearest to x0 (i.e. works with higher than 1-D arrays also) xidx: x[idx]
Observe how bisect.bisect() gives the incorrect result!
idea based on: http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array
geoist.others.gdal module¶
Description : functions for GDAL drivers
-
class
geoist.others.gdal.
GDALGrid
(data, geodict)[源代码]¶ 基类:
geoist.others.grid2d.Grid2D
-
classmethod
getFileGeoDict
(filename)[源代码]¶ Get the spatial extent, resolution, and shape of grid inside ESRI grid file. :param filename:
File name of ESRI grid file.
- 返回
GeoDict object specifying spatial extent, resolution, and shape of grid inside ESRI grid file.
- 引发
DataSetException -- When the file contains a grid with more than one band. When the file geodict is internally inconsistent.
-
classmethod
load
(filename, samplegeodict=None, resample=False, method='linear', doPadding=False, padValue=nan)[源代码]¶ This method should do the following: 1) If resampling, buffer bounds outwards. 2) Translate geographic bounds to pixel bounds (Grid2D static method) 3) Create a Grid2D subclass instance 4) Call readFile() with pixel bounds 5) Pad as requested. 6) Resample as requested. 7) Return Grid2D subclass.
-
classmethod
readFile
(filename, data_range)[源代码]¶ Read an ESRI flt/bip/bil/bsq formatted file using rasterIO (GDAL Python wrapper). :param filename:
Input ESRI formatted grid file.
- 参数
data_range --
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
- 返回
A tuple of (data,geodict) where data is a 2D numpy array of all data found inside bounds, and geodict gives the geo-referencing information for the data.
-
save
(filename, format='EHdr')[源代码]¶ Save the data contained in this grid to a float or integer ESRI grid file. Described here: http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=BIL,_BIP,_and_BSQ_raster_files
- 参数
filename -- String representing file to which data should be saved.
format -- Currently this code only supports the GDAL format 'EHdr' (see formats above.) As rasterIO write support is expanded, this code should add functionality accordingly.
- 引发
DataSetException -- When format is not 'EHdr'.
-
classmethod
geoist.others.geodict module¶
geodict
-
class
geoist.others.geodict.
GeoDict
(geodict, adjust='bounds')[源代码]¶ 基类:
object
-
DEFAULT_PROJ4
= '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'¶
-
DIST_THRESH
= 8.99353364930615e-08¶
-
EPS
= 1e-12¶
-
REQ_KEYS
= ['xmin', 'xmax', 'ymin', 'ymax', 'dx', 'dy', 'ny', 'nx']¶
-
asDict
()[源代码]¶ Return GeoDict object in dictionary representation. :returns:
Dictionary containing the same fields as found in constructor.
-
contains
(geodict)[源代码]¶ Determine if input geodict is completely outside this GeoDict.
- 参数
geodict -- Input GeoDict object.
- 返回
True if input geodict is completely outside this GeoDict, False if not.
-
copy
()[源代码]¶ Return an object that is a complete copy of this GeoDict.
- 返回
A GeoDict object whose elements (xmin, xmax, ...) are an exact copy of this object's elements.
-
classmethod
createDictFromBox
(xmin, xmax, ymin, ymax, dx, dy, inside=False)[源代码]¶ Create GeoDict from two corner points and an x/y resolution.
- 参数
xmin -- X coordinate of center of upper left pixel.
xmax -- X coordinate of center of lower right pixel.
ymin -- Y coordinate of center of lower right pixel.
ymax -- Y coordinate of center of upper left pixel.
dx -- Width of pixels.
dy -- Height of pixels.
inside -- Boolean, indicating whether to ensure that resulting GeoDict falls inside or outside the input bounds.
- 返回
GeoDict object.
-
classmethod
createDictFromCenter
(cx, cy, dx, dy, xspan, yspan)[源代码]¶ Create GeoDict from a center point, dx/dy and a width and height.
- 参数
cx -- X coordinate of center point.
cy -- Y coordinate of center point.
dx -- Width of pixels.
dy -- Height of pixels.
xspan -- Width of desired box.
yspan -- Height of desired box.
- 返回
GeoDict object.
-
doesNotContain
(geodict)[源代码]¶ Determine if input geodict is completely outside this GeoDict.
- 参数
geodict -- Input GeoDict object.
- 返回
True if input geodict is completely outside this GeoDict, False if not.
-
property
dx
¶ Get dx value. :returns:
dx value.
-
property
dy
¶ Get dy value. :returns:
dy value.
-
getAligned
(geodict, inside=False)[源代码]¶ Return a geodict that is close to the input geodict bounds but aligned with this GeoDict.
- 参数
geodict -- Input GeoDict object, whose bounds will be used, but dx/dy and nx/ny ignored.
inside -- Boolean indicating whether the aligned geodict should be inside or outside input geodict.
- 返回
GeoDict object which is guaranteed to be grid-aligned with this GeoDict.
-
getBoundsWithin
(geodict)[源代码]¶ - Create a GeoDict by finding the maximum bounding
box aligned with enclosing GeoDict that is guaranteed to be inside input GeoDict.
- 参数
geodict -- GeoDict object that output GeoDict will be contained by.
- 引发
DataSetException -- When input geodict is not fully contained by this GeoDict, or if the output GeoDict cannot be aligned with this GeoDict (this shouldn't happen).
-
getIntersection
(geodict)[源代码]¶ Return a geodict defining intersected area, retaining resolution of the input geodict.
- 参数
geodict -- Input GeoDict object, which should intersect with this GeoDict.
- 返回
GeoDict which represents the intersected area, and is aligned with the input geodict.
- 引发
DataSetException -- When input geodict does not intersect at all with this GeoDict.
-
getLatLon
(row, col)[源代码]¶ Return geographic coordinates (lat/lon decimal degrees) for given data row and column.
- 参数
row -- Row dimension index into internal data array.
col -- Column dimension index into internal data array.
- 返回
Tuple of latitude and longitude.
-
getRowCol
(lat, lon, returnFloat=False, intMethod='round')[源代码]¶ Return data row and column from given geographic coordinates (lat/lon decimal degrees).
- 参数
lat -- Input latitude.
lon -- Input longitude.
returnFloat -- Boolean indicating whether floating point row/col coordinates should be returned.
intMethod -- String indicating the desired method by which floating point row/col values should be converted to integers. Choices are 'round' (default), 'floor', or 'ceil'.
- 返回
Tuple of row and column.
-
intersects
(geodict)[源代码]¶ Determine if input geodict intersects this GeoDict.
- 参数
geodict -- Input GeoDict object.
- 返回
True if input geodict intersects with this GeoDict, False if not.
-
isAligned
(geodict)[源代码]¶ Determines if input geodict is grid-aligned with this GeoDict.
- 参数
geodict -- Input geodict whose cells must all be grid-aligned with this GeoDict.
- 返回
True when geodict is grid-aligned, and False if not.
-
property
nodata
¶ Get projection Proj4 string. :returns:
Valid nodata value.
-
property
nx
¶ Get nx value. :returns:
nx value.
-
property
ny
¶ Get ny value. :returns:
ny value.
-
property
projection
¶ Get projection Proj4 string. :returns:
Valid Proj4 string.
-
setProjection
(projection)[源代码]¶ Set a new projection for the GeoDict.
- 参数
projection -- Valid proj4 string.
- 引发
DataSetException -- When input is not valid proj4.
-
property
xmax
¶ Get xmin value. :returns:
xmin value.
-
property
xmin
¶ Get xmin value. :returns:
xmin value.
-
property
ymax
¶ Get xmax value. :returns:
xmax value.
-
property
ymin
¶ Get xmax value. :returns:
xmax value.
-
geoist.others.gmt module¶
Description : Application for GMT io
-
class
geoist.others.gmt.
GMTGrid
(data, geodict)[源代码]¶ 基类:
geoist.others.grid2d.Grid2D
Grid2D subclass for reading,writing, and manipulating GMT format grids.
Usage:
gmtgrid = GMTGrid.load(gmtfilename) gmtgrid.getGeoDict() This class supports reading and writing of all three GMT formats: NetCDF, HDF, and the GMT "native" format.
-
classmethod
getFileGeoDict
(filename)[源代码]¶ Get the spatial extent, resolution, and shape of grid inside NetCDF file. :param filename:
File name of NetCDF file.
- 返回
GeoDict specifying spatial extent, resolution, and shape of grid inside NetCDF file.
- 引发
DataSetException -- When the file is not detectable as one of the three flavors of GMT grids.
-
classmethod
getFileType
(grdfile)[源代码]¶ Get the GMT sub-format (netcdf, hdf, or GMT binary). :param grdfile:
File name of suspected GMT grid file.
- 返回
One of 'netcdf' (NetCDF version 3), 'hdf' (NetCDF version 4), 'native' (so-called GMT native format), or 'unknown'.
-
classmethod
getHDFHeader
(hdffile)[源代码]¶ Get the header information from a GMT NetCDF4 (HDF) file. :param fname:
File name of GMT NetCDF4 grid
- 返回
GeoDict specifying spatial extent, resolution, and shape of grid inside NetCDF file.
-
classmethod
getNativeHeader
(fname, fmt=None)[源代码]¶ Get the header information from a GMT native grid file. :param fname:
File name of GMT native grid
- 参数
fmt --
- One of:
'h' for 16 bit signed integer
'i' for 32 bit signed integer
'f' for 32 bit float
'd' for 64 bit float
- 返回
GeoDict specifying spatial extent, resolution, and shape of grid inside NetCDF file.
xvar array specifying X coordinates of data columns
yvar array specifying Y coordinates of data rows
fmt If input fmt is None, this will be a best guess as to the data format in the file.
zscale Data multiplier
zoffset Value to be added to data
-
classmethod
getNetCDFHeader
(filename)[源代码]¶ Get the header information from a GMT NetCDF3 file. :param fname:
File name of GMT NetCDF3 grid
- 返回
GeoDict specifying spatial extent, resolution, and shape of grid inside NetCDF file.
xvar array specifying X coordinates of data columns
xvar array specifying Y coordinates of data rows
-
classmethod
load
(filename, samplegeodict=None, resample=False, method='linear', doPadding=False, padValue=nan)[源代码]¶ Create a GMTGrid object from a (possibly subsetted, resampled, or padded) GMT grid file. :param gmtfilename:
Name of input file.
- 参数
samplegeodict -- GeoDict used to specify subset bounds and resolution (if resample is selected)
resample -- Boolean used to indicate whether grid should be resampled from the file based on samplegeodict.
method -- If resample=True, resampling method to use ('nearest','linear','cubic','quintic')
doPadding -- Boolean used to indicate whether, if samplegeodict is outside bounds of grid, to pad values around the edges.
padValue -- Value to fill in around the edges if doPadding=True.
- 返回
GMTgrid instance (possibly subsetted, padded, or resampled)
- 引发
When sample bounds are outside (or too close to outside) the bounds of the grid and doPadding=False.
When the input file type is not recognized.
-
classmethod
readFile
(filename, data_range)[源代码]¶ Read any GMT formatted file. :param filename:
Input GMT formatted grid file.
- 参数
data_range --
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
- 返回
data is a 2D numpy array of all data found inside bounds, and
geodict gives the geo-referencing information for the data.
-
classmethod
readGMTNative
(fname, data_range, fmt=None)[源代码]¶ Read the data and geo-referencing information from a GMT native grid file, subsetting if requested.
http://gmt.soest.hawaii.edu/doc/5.1.2/GMT_Docs.html#native-binary-grid-files
- 参数
fname -- File name of GMT native grid
data_range --
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
fmt --
- Data width, one of:
'i' (16 bit signed integer)
'l' (32 bit signed integer)
'f' (32 bit float)
'd' (64 bit float)
Strictly speaking, this is only necessary when the data file is 32 bit float or 32 bit integer, as there is no sure way to tell from the header or data which data type is contained in the file. If fmt is None, then the code will try to guess as best it can from the data whether it is integer or floating point data. Caveat emptor!
- returns
Tuple of data (2D numpy array of data, possibly subsetted from file) and geodict (see above).
-
classmethod
readHDF
(hdffile, data_range)[源代码]¶ Read a NetCDF formatted GMT file.
- 参数
filename -- Input GMT formatted grid file.
data_range --
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
- 返回
data is a 2D numpy array of all data found inside bounds, and
geodict gives the geo-referencing information for the data.
-
classmethod
readNetCDF
(filename, data_range)[源代码]¶ Read a NetCDF formatted GMT file.
- 参数
filename -- Input GMT formatted grid file.
data_range --
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
- 返回
data is a 2D numpy array of all data found inside bounds, and
geodict gives the geo-referencing information for the data.
-
save
(filename, format='netcdf')[源代码]¶ Save a GMTGrid object to a file. :param filename:
Name of desired output file.
- 参数
format -- One of 'netcdf','hdf' or 'native'.
- 引发
DataSetException -- When format not one of ('netcdf,'hdf','native')
-
classmethod
-
geoist.others.gmt.
createSampleGrid
(M, N)[源代码]¶ Used for internal testing - create an NxN grid with lower left corner at 0.5,0.5, dx/dy = 1.0. :param M:
Number of rows in output grid
- 参数
N -- Number of columns in output grid
- 返回
GMTGrid object where data values are an NxN array of values from 0 to N-squared minus 1, and geodict lower left corner is at 0.5/0.5 and cell dimensions are 1.0.
geoist.others.grid2d module¶
Description : Application for Grid data processing
-
class
geoist.others.grid2d.
Grid2D
(data=None, geodict=None)[源代码]¶ 基类:
geoist.others.gridbase.Grid
-
applyNaN
(force=False)[源代码]¶ Apply no data value to internal data, cast to float if necessary.
Intelligently cast data in grid to be able to handle NaN values.
Usage: Integer data with a precision of 16 bits or less will be cast to 32 bit floating point.
Integer data with precision of 32 bits will be cast to 32 bit floating point if maximum/minimum values can be cast without losing precision.
Integer data with precision of 64 bits or greater will be cast to 64 bit floating point if maximum/minimum values can be cast without losing precision. Otherwise, this method will raise an OverflowError unless the force option is set to True.
- 参数
force -- Boolean indicating whether to override OverflowError (see Usage).
-
static
bufferBounds
(samplegeodict, filegeodict, resample=False, buffer_pixels=1, doPadding=False)[源代码]¶ Buffer requested bounds out by buffer_pixels pixels, or edge of grid.
Buffer pixels shoud be at filegeodict resolution.
- 参数
samplegeodict -- GeoDict object describing grid to use for sampling.
filegeodict -- GeoDict object describing file.
resample -- Boolean indicating whether we want to resample.
buffer_pixels -- Number of pixels to buffer bounds in any possible direction.
- 返回
GeoDict which has been buffered by the appropriate number of pixels.
-
static
checkFirstColumnDuplicated
(geodict)[源代码]¶ Check to see if the first column in a file described by geodict is duplicated by the last column.
- 参数
geodict -- GeoDict object which may have duplicate column.
- 返回
- Tuple containing:
GeoDict object representing a grid with the last column removed.
Boolean indicating whether the last column was a duplicate.
-
classmethod
copyFromGrid
(grid)[源代码]¶ Copy constructor - can be used to create an instance of any Grid2D subclass from another.
- 参数
grid -- Any Grid2D instance.
- 返回
A copy of the data in that input Grid2D instance.
-
cut
(xmin, xmax, ymin, ymax, align=False)[源代码]¶ Cut out a section of Grid and return it.
- 参数
xmin -- Longitude coordinate of upper left pixel, must be aligned with Grid.
xmax -- Longitude coordinate of lower right pixel, must be aligned with Grid.
ymin -- Latitude coordinate of upper left pixel, must be aligned with Grid.
ymax -- Latitude coordinate of lower right pixel, must be aligned with Grid.
align -- Boolean indicating whether input boundaries should be modified to align with host grid.
-
static
getDataRange
(fgeodict, sampledict, first_column_duplicated=False, padding=None)[源代码]¶ For a given set of input bounds and information about a file, determine the rows and columns for bounds.
- 参数
fgeodict -- GeoDict object for a given file.
sampledict -- Sampling GeoDict object.
first_column_duplicated -- Boolean indicating whether the last column in a file is a duplicate of the first column.
- 返回
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
-
abstract
getFileGeoDict
()[源代码]¶ Abstract method to return the bounding box, resolution, and shape of a file in whatever Grid format.
- param filename
The path to the filename of whatever grid format this is being implemented in.
- 返回
A geodict specifying the bounding box, resolution, and shape of the data in a file.
-
getGeoDict
()[源代码]¶ Return a reference to the geodict inside the Grid. :returns:
A reference to a dictionary (see constructor).
-
getLatLon
(row, col)[源代码]¶ Return geographic coordinates (lat/lon decimal degrees) for given data row and column.
- 参数
row -- Row dimension index into internal data array.
col -- Column dimension index into internal data array.
- 返回
Tuple of latitude and longitude.
-
static
getPadding
(filegeodict, samplegeodict, doPadding=False)[源代码]¶ Determine how many pixels of padding there need to be on each side of requested grid.
- 参数
filegeodict -- GeoDict object specifying the spatial information from a source file.
samplegeodict -- GeoDict object specifying the spatial information for a desired sampling regime.
resampling -- Boolean indicating that user wants to resample the data from the file to the samplegeodict.
- 引发
DataSetException -- When resampling is False and filegeodict and samplegeodict are not pixel aligned.
- 返回
- A dictionary containing fields:
padleft The number of padding pixels on the left edge.
padright The number of padding pixels on the right edge.
padbottom The number of padding pixels on the bottom edge.
padtop The number of padding pixels on the top edge.
-
getRowCol
(lat, lon, returnFloat=False)[源代码]¶ Return data row and column from given geographic coordinates (lat/lon decimal degrees).
- 参数
lat -- Input latitude.
lon -- Input longitude.
returnFloat -- Boolean indicating whether floating point row/col coordinates should be returned.
- 返回
Tuple of row and column.
-
getValue
(lat, lon, method='nearest', default=None)[源代码]¶ Return numpy array at given latitude and longitude (using nearest neighbor).
- 参数
lat -- Latitude (in decimal degrees) of desired data value.
lon -- Longitude (in decimal degrees) of desired data value.
method -- Interpolation method, one of ('nearest','linear','cubic','quintic')
default -- Default value to return when lat/lon is outside of grid bounds.
- 返回
Value at input latitude,longitude position.
- 引发
DataSetException -- When lat/lon is outside of bounds and default is None.
-
interpolate2
(geodict, method='linear')[源代码]¶ Given a geodict specifying another grid extent and resolution, resample current grid to match.
This method uses the rasterio reproject method instead of scipy. In tests with small grids that can be replicated easily by hand, the results from this method match interpolateToGrid. Limited tests with larger, random grids indicate some differences, the extent of which has not been documented. These same limited tests indicate this method is 5 to 7 times faster than interpolateToGrid.
- 参数
geodict -- geodict dictionary from another grid whose extents are inside the extent of this grid.
method -- Optional interpolation method - ['linear', 'cubic','nearest']
- 引发
DataSetException -- If the Grid object upon which this function is being called is not completely contained by the grid to which this Grid is being resampled.
DataSetException -- If the method is not one of ['nearest','linear','cubic'] If the resulting interpolated grid shape does not match input geodict.
- 返回
A new instance of the Grid2D class or subclass with interpolated data.
-
interpolateToGrid
(geodict, method='linear')[源代码]¶ Given a geodict specifying another grid extent and resolution, resample current grid to match.
- 参数
geodict -- geodict dictionary from another grid whose extents are inside the extent of this grid.
method -- Optional interpolation method - ['linear', 'cubic','nearest']
- 引发
DataSetException -- If the Grid object upon which this function is being called is not completely contained by the grid to which this Grid is being resampled.
DataSetException -- If the method is not one of ['nearest','linear','cubic'] If the resulting interpolated grid shape does not match input geodict.
- 返回
A new instance of the Grid2D class or subclass with interpolated data.
-
static
padGrid
(data, geodict, pad_dict)[源代码]¶ Pad input data array with pixels specified by pad_dict on each side.
- 参数
data -- 2D numpy array of data.
geodict -- GeoDict object describing data.
pad_dict --
- A dictionary containing fields:
padleft The number of padding pixels on the left edge.
padright The number of padding pixels on the right edge.
padbottom The number of padding pixels on the bottom edge.
padtop The number of padding pixels on the top edge.
- 返回
Tuple of (data,geodict) where data has been padded and geodict represents new padded data.
-
project
(projection, method='bilinear')[源代码]¶ Project Grid2D data into desired projection.
- 参数
projection -- Valid proj4 projection string.
method --
- One of the sampling methods described here:
https://mapbox.github.io/rasterio/topics/resampling.html#resampling-methods
- 引发
DataSetException -- If input projection is not a valid Proj4 string. If method is not a valid resampling method found in above URL.
- 返回
Re-projected Grid2D object.
-
classmethod
rasterizeFromGeometry
(shapes, geodict, burnValue=1.0, fillValue=nan, mustContainCenter=False, attribute=None)[源代码]¶ Create a Grid2D object from vector shapes, where the presence of a shape (point, line, polygon) inside a cell turns that cell "on".
- 参数
shapes --
- One of:
One shapely geometry object (Point, Polygon, etc.) or a sequence of such objects
One GeoJSON like object or sequence of such objects. (http://geojson.org/)
A tuple of (geometry,value) or sequence of (geometry,value).
geodict -- GeoDict object which defines the grid onto which the shape values should be "burned".
burnValue -- Optional value which will be used to set the value of the pixels if there is no value in the geometry field.
fillValue -- Optional value which will be used to fill the cells not touched by any geometry.
mustContainCenter -- Optional boolean which indicates whether the geometry must touch the center of the cell or merely be inside the cell in order to set the value.
- 引发
DataSetException -- When geometry input is not a subclass of shapely.geometry.base.BaseGeometry.
- 返回
Grid2D object.
This method is a thin wrapper around rasterio->features->rasterize(), documented here: https://github.com/mapbox/rasterio/blob/master/docs/features.rst
which is itself a Python wrapper around the functionality found in gdal_rasterize, documented here: http://www.gdal.org/gdal_rasterize.html
-
abstract
readFile
(data_range)[源代码]¶ Read in data from the given file, at the pixels specified in data_range.
- 参数
filename -- Name of file to read.
data_range --
- Dictionary containing fields:
iulx1 Upper left X of first (perhaps only) segment.
iuly1 Upper left Y of first (perhaps only) segment.
ilrx1 Lower right X of first (perhaps only) segment.
ilry1 Lower right Y of first (perhaps only) segment.
(if bounds cross 180 meridian...) - iulx2 Upper left X of second segment. - iuly2 Upper left Y of second segment. - ilrx2 Lower right X of second segment. - ilry2 Lower right Y of second segment.
-
reqfields
= {'dx', 'dy', 'nx', 'ny', 'xmax', 'xmin', 'ymax', 'ymin'}¶
-
abstract
save
(filename)[源代码]¶ Save the data contained in the grid to a format specific file. Other attributes may be required for format specific files.
- 参数
filename -- Where file containing data should be written.
-
setData
(data)[源代码]¶ Set the data inside the Grid.
- 参数
data -- A 2D numpy array.
- Raises
DataSetException if the number of rows and columns do not match the the internal GeoDict, or if the input is not a numpy array.
-
subdivide
(finerdict, cellFill='max')[源代码]¶ Subdivide the cells of the host grid into finer-resolution cells.
- 参数
finerdict -- GeoDict object defining a grid with a finer resolution than the host grid.
cellFill --
String defining how to fill cells that span more than one host grid cell. Choices are:
'max': Choose maximum value of host grid cells. 'min': Choose minimum value of host grid cells. 'mean': Choose mean value of host grid cells.
- 返回
Grid2D instance with host grid values subdivided onto finer grid.
- 引发
DataSetException -- When finerdict is not a) finer resolution or b) does not intersect.x or cellFill is not valid.
-
static
verifyBounds
(filegeodict, samplegeodict, resample=False)[源代码]¶ Ensure that the two grids represented at least 1) intersect and 2) are aligned if resampling is True.
- 参数
filegeodict -- GeoDict object describing file.
samplegeodict -- GeoDict object describing grid to use for sampling.
resample -- Boolean indicating whether we want to resample.
- Raises
DataSetException when geodicts do not intersect or if the grids are not aligned.
-
geoist.others.gridbase module¶
Application for gridbase
-
class
geoist.others.gridbase.
Grid
[源代码]¶ 基类:
geoist.others.dataset.DataSet
An abstract class to represent lat/lon gridded datasets. Grids are assumed to be pixel-registered - that is, grid coordinates represent the value at the center of the cells.
-
abstract
blockmean
(geodict)[源代码]¶ Abstract method to calculate average values for cells of larger size than the current grid.
- 参数
geodict -- Geodict that defines the new coarser grid.
-
abstract
getBoundsWithin
(geodict)[源代码]¶ Abstract method to return a geodict for this file that is guaranteed to be inside the input geodict defined, without resampling.
- param filename
The name of the file whose resolution/extent should be used.
- 参数
geodict -- The geodict which is used as the base for finding the bounds for this file guaranteed to be inside of this geodict.
- 引发
NotImplementedError -- Always in base class
-
abstract
getFileGeoDict
()[源代码]¶ Abstract method to return the bounding box, resolution, and shape of a file in whatever Grid format.
- param filename
The path to the filename of whatever grid format this is being implemented in.
- 返回
A geodict specifying the bounding box, resolution, and shape of the data in a file.
-
abstract
getGeoDict
()[源代码]¶ Return a reference to the geodict inside the Grid
- 返回
A reference to a dictionary (see constructor).
-
abstract
getLatLon
(row, col)[源代码]¶ Return geographic coordinates (lat/lon decimal degrees) for given data row and column.
- 参数
row -- Row dimension index into internal data array.
col -- Column dimension index into internal data array.
- 返回
Tuple of latitude and longitude.
-
abstract
getRowCol
(lat, lon, returnFloat=False)[源代码]¶ Return data row and column from given geographic coordinates (lat/lon decimal degrees).
- 参数
lat -- Input latitude.
lon -- Input longitude.
returnFloat -- Boolean indicating whether floating point row/col coordinates should be returned.
- 返回
Tuple of row and column.
-
abstract
geoist.others.gridcontainer module¶
Description : Application for grid processing.
geoist.others.reader module¶
Description : Application for grid processing.
-
geoist.others.reader.
get_file_geodict
(filename)[源代码]¶ Get the GeoDict describing the entire file.
- 参数
filename (str) -- rasterio supported file format.
- 返回
GeoDict: GeoDict describing the entire file.
- 返回类型
-
geoist.others.reader.
read
(filename, samplegeodict=None, resample=False, method='linear', doPadding=False, padValue=nan, apply_nan=True, force_cast=True)[源代码]¶ Read part or all of a rasterio file, resampling and padding as necessary.
If samplegeodict is not provided, then the entire file will be read.
If samplegeodict and resample are provided, then the smallest subset of data containing the samplegeodict plus a 1 pixel wide ring of data will be read in, then that data will be resampled to the samplegeodict bounds/resolution.
If doPadding is set to True, then pixels on the edge of the source grid will be padded 1 pixel deep with input padValue.
In addition to pad pixels, extra data may be read around the edges of the desired area to ensure raw data is read on integer row/column offsets.
- 参数
filename (str) -- rasterio supported file format.
samplegeodict (GeoDict) -- GeoDict describing the subset we wish to read.
resample (bool) -- True if resampling should be performed.
method (str) -- One of ('nearest','linear').
doPadding (bool) -- Whether to add ring of padValue pixels after reading from file.
padValue (float) -- Value to insert in ring of padding pixels.
apply_nan (bool) -- Convert nodata values to NaNs, upcasting to float if necessary.
force_cast (bool) -- If data values exceed
geoist.others.scidates module¶
geoist.others.ticks module¶
These functions separated due to time-consuming imports
geoist.others.tz module¶
-
geoist.others.tz.
forceutc
(t: Union[str, datetime.datetime, datetime.date, numpy.datetime64]) → Union[datetime.datetime, datetime.date][源代码]¶ Add UTC to datetime-naive and convert to UTC for datetime aware
input: python datetime (naive, utc, non-utc) or Numpy datetime64 #FIXME add Pandas and AstroPy time classes output: utc datetime
geoist.others.utils module¶
Misc utilities
-
geoist.others.utils.
Grid2Xyz
(grid)[源代码]¶ Transform grid data object to x,y,z of 1-D array
- Parameters:
grid is a Grid2D object
- 返回
The x and y coordinates of the grid points z : The value at the grid points
- 返回类型
x,y
-
geoist.others.utils.
file_hash
(fname)[源代码]¶ Calculate the SHA256 hash of a given file.
Useful for checking if a file has changed or been corrupted.
- 参数
fname (str) -- The name of the file.
- 返回
hash -- The hash of the file.
- 返回类型
str
实际案例
>>> fname = "test-file-for-hash.txt" >>> with open(fname, "w") as f: ... __ = f.write("content of the file") >>> print(file_hash(fname)) 0fc74468e6a9a829f103d069aeb2bb4f8646bad58bf146bb0e3379b759ec4a00 >>> import os >>> os.remove(fname)
-
geoist.others.utils.
find_nearest
(x, x0) → Tuple[int, Any][源代码]¶ This find_nearest function does NOT assume sorted input
inputs: x: array (float, int, datetime, h5py.Dataset) within which to search for x0 x0: singleton or array of values to search for in x
outputs: idx: index of flattened x nearest to x0 (i.e. works with higher than 1-D arrays also) xidx: x[idx]
Observe how bisect.bisect() gives the incorrect result!
idea based on: http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array
-
geoist.others.utils.
get_prism_index
(ix, iy, iz, nx, ny, nz)[源代码]¶ calculate prism's index given its position
- 参数
ix,iy,iz (int) -- prism's position
nx,ny,nz (int) -- how many prisms along x,y,z axis.
- 返回
index of the prism
- 返回类型
int
-
geoist.others.utils.
get_prism_pos
(index, nx, ny, nz)[源代码]¶ calculate prism's position given its index.
- 参数
index (int) -- prism's index
nx,ny,nz (int) -- how many prisms along x,y,z axis.
- 返回
position of the prism
-
geoist.others.utils.
grid2srf
(grid, filename='outsrf.grd', fformat='asc')[源代码]¶ grid is a Grid2D object
-
geoist.others.utils.
make_registry
(directory, output, recursive=True)[源代码]¶ Make a registry of files and hashes for the given directory.
This is helpful if you have many files in your test dataset as it keeps you from needing to manually update the registry.
- 参数
directory (str) -- Directory of the test data to put in the registry. All file names in the registry will be relative to this directory.
output (str) -- Name of the output registry file.
recursive (bool) -- If True, will recursively look for files in subdirectories of directory.
-
geoist.others.utils.
map2DGrid
(ax, grid, tstr, xlen=1.0, ylen=1.0, isLeft=False, prj='lcc', bous=0, gmap=0, xinc=None, yinc=None, pnts=None, cmap='gist_earth')[源代码]¶ grid is a Grid2D object prj : lcc / merc
-
geoist.others.utils.
os_cache
(project, platform=None)[源代码]¶ Default cache location based on the operating system.
Will insert the project name in the proper location of the path.
- 参数
project (str) -- The project name.
platform (str or None) -- The name of operating system as returned by
sys.platform
('darwin'
for Mac,'win32'
for Windows, and anything else will be treated as generic Linux/Unix. If None, will use the value ofsys.platform
.
- 返回
cache_path -- The default location for the data cache. User directories (
'~'
) are not expanded.- 返回类型
pathlib.Path
实际案例
>>> for os in ['darwin', 'win32', 'anything else']: ... path = os_cache("myproject", platform=os) ... print(path.parts) ('~', 'Library', 'Caches', 'myproject') ('~', 'AppData', 'Local', 'myproject', 'cache') ('~', '.cache', 'myproject')
-
geoist.others.utils.
plot_kernel
(ggz, nxyz=None, nobs=None, obs_extent=- 100, 100, - 100, 100, image_grid=3, 5, fname=None)[源代码]¶ inspect the kernel matrix
- 参数
ggz (ndarray) -- Kernel matrix. Each column correspond to a source point. Each row correspond to an observe station.
nxyz (tuple) -- How many source points along x,y,z axis respectively.
nobs (tuple) -- How many observe stations along x,y axis respectively.
obs_extent (tuple) -- Define the observe area in a order of (min_x,max_x,min_y,max_y).
image_grid (tuple) -- Define the dimension of image grid.
-
geoist.others.utils.
plot_matrix
(A, cbar_location='right', figsize=18, 18, cmap='coolwarm', fname=None)[源代码]¶