spatial
Spatial data class for reading, writing and processing spatial data
Can read ascii, netCDF4, HDF5 files
Can read from an index of the above file types
Can merge a list of
spatialobjects into a single objectCan subset to a list of GRACE/GRACE-FO months
Can calculate the mean field of a
spatialobjectCan output
spatialobjects to ascii, netCDF4 or HDF5 files
Calling Sequence
Reading a netCDF4 file
from gravity_toolkit.spatial import spatial
grid = spatial().from_netCDF4(path_to_netCDF4_file)
Reading a HDF5 file
from gravity_toolkit.spatial import spatial
grid = spatial().from_HDF5(path_to_HDF5_file)
Reading an index file of netCDF4 files and then outputting as a single file
from gravity_toolkit.spatial import spatial
grid = spatial().from_index(path_to_index_file,'netCDF4')
grid.to_netCDF4(path_to_netCDF4_file)
Reading an index file of HDF5 files and subsetting to specific months
from gravity_toolkit.spatial import spatial
grid = spatial().from_index(path_to_index_file,'HDF5').subset(months)
Converting a dictionary object to a spatial object and removing the mean field
from gravity_toolkit.spatial import spatial
grid = spatial().from_dict(grid_dict)
grid.mean(apply=True)
General Attributes and Methods
- class gravity_toolkit.spatial(**kwargs)[source]
Data class for reading, writing and processing spatial data
- Attributes:
- data: np.ndarray
spatial grid data
- mask: np.ndarray
spatial grid mask
- lon: np.ndarray
grid longitudes
- lat: np.ndarray
grid latitudes
- time: np.ndarray
time variable of the spatial data
- month: np.ndarray
GRACE/GRACE-FO months variable of the spatial data
- fill_value: float or NoneType, default None
invalid value for spatial grid data
- attributes: dict
attributes of
spatialvariables- filename: str
input or output filename
- case_insensitive_filename(filename)[source]
Searches a directory for a filename without case dependence
- Parameters:
- filename: str, io.IOBase, pathlib.Path or None
input filename
- compressuser(filename=None)[source]
Tilde-compresses a file to be relative to the home directory
- Parameters:
- filename: str or None, default None
output filename
- from_ascii(filename, date=True, **kwargs)[source]
Read a
spatialobject from an ascii file- Parameters:
- filename: str
full path of input ascii file
- date: bool, default True
ascii file has date information
- compression: str or NoneType, default None
file compression type
'gzip''zip''bytes'
- spacing: list, default [None,None]
grid step size
[longitude,latitude]- extent: list, default [None,None,None,None]
spatial grid bounds
[minimum longitude, maximum longitude, minimum latitude, maximum latitude]- nlat: int or NoneType, default None
length of latitude dimension
- nlon: int or NoneType, default None
length of longitude dimension
- columns: list, default [‘lon’,’lat’,’data’,’time’]
variable names for each column
- header: int, default 0
Number of rows of header lines to skip
- verbose: bool, default False
print file and variable information
- from_netCDF4(filename, **kwargs)[source]
Read a
spatialobject from a netCDF4 file- Parameters:
- filename: str
full path of input netCDF4 file
- date: bool, default True
netCDF4 file has date information
- compression: str or NoneType, default None
file compression type
'gzip''zip''bytes'
- varname: str, default ‘data’
name for data variable
- lonname: str, default ‘lon’
name for longitude variable
- latname: str, default ‘lat’
name for latitude variable
- timename: str, default ‘time’
name for time-dimension variable
- field_mapping: dict, default {}
mapping between output variables and input netCDF4
- verbose: bool, default False
print file and variable information
- from_HDF5(filename, **kwargs)[source]
Read a
spatialobject from a HDF5 file- Parameters:
- filename: str
full path of input HDF5 file
- date: bool, default True
HDF5 file has date information
- compression: str or NoneType, default None
file compression type
'gzip''zip''bytes'
- varname: str, default ‘data’
name for data variable
- lonname: str, default ‘lon’
name for longitude variable
- latname: str, default ‘lat’
name for latitude variable
- timename: str, default ‘time’
name for time-dimension variable
- field_mapping: dict, default {}
mapping between output variables and input HDF5
- verbose: bool, default False
print file and variable information
- from_index(filename, **kwargs)[source]
Read a
spatialobject from an index of ascii, netCDF4 or HDF5 files- Parameters:
- filename: str
full path of index file
- format: str or NoneType, default None
format of individual files within index
'ascii''netCDF4''HDF5'
- date: bool, default True
files contains date information
- sort: bool, default True
sort
spatialobjects by date information- **kwargs: dict
keyword arguments for input readers
- from_list(object_list, **kwargs)[source]
Build a sorted
spatialobject from a list of otherspatialobjects- Parameters:
- object_list: list
list of
spatialobjects to be merged- date: bool, default True
files contains date information
- sort: bool, default True
sort
spatialobjects by date information- clear: bool, default True
clear the list of
spatialobjects from memory
- from_file(filename, format=None, date=True, **kwargs)[source]
Read a
spatialobject from a specified format- Parameters:
- filename: str
full path of input file
- format: str or NoneType, default None
file format
'ascii''netCDF4''HDF5'
- date: bool, default True
file contains date information
- verbose: bool, default False
print file and variable information
- **kwargs: dict
keyword arguments for input readers
- from_dict(d, **kwargs)[source]
Convert a
dictobject to aspatialobject- Parameters:
- d: dict
dictionary object to be converted
- to_ascii(filename, **kwargs)[source]
Write a
spatialobject to ascii file- Parameters:
- filename: str
full path of output ascii file
- date: bool, default True
spatialobjects contain date information- verbose: bool, default False
Output file and variable information
- to_netCDF4(filename, **kwargs)[source]
Write a
spatialobject to netCDF4 file- Parameters:
- filename: str
full path of output netCDF4 file
- varname: str, default ‘z’
data variable name in netCDF4 file
- lonname: str
longitude variable name in netCDF4 file
- latname: str
latitude variable name in netCDF4 file
- field_mapping: dict, default {}
mapping between input variables and output netCDF4
- attributes: dict, default {}
output netCDF4 variable and file-level attributes
- units: str or NoneType, default: None
data variable units
- longname: str or NoneType, default: None
data variable unit description
- time_units: str, default ‘years’
time variable units
- time_longname: str, default ‘Date_in_Decimal_Years’
time variable unit description
- title: str or NoneType, default None
title attribute of dataset
- source: str or NoneType, default None
source attribute of dataset
- reference: str or NoneType, default None
reference attribute of dataset
- date: bool, default True
spatialobjects contain date information- clobber: bool, default True
Overwrite an existing netCDF4 file
- verbose: bool, default False
Output file and variable information
- to_HDF5(filename, **kwargs)[source]
Write a
spatialobject to HDF5 file- Parameters:
- filename: str
full path of output HDF5 file
- varname: str, default ‘z’
data variable name in HDF5 file
- lonname: str
longitude variable name in HDF5 file
- latname: str
latitude variable name in HDF5 file
- field_mapping: dict, default {}
mapping between input variables and output HDF5
- attributes: dict, default {}
output HDF5 variable and file-level attributes
- units: str or NoneType, default: None
data variable units
- longname: str or NoneType, default: None
data variable unit description
- time_units: str, default ‘years’
time variable units
- time_longname: str, default ‘Date_in_Decimal_Years’
time variable unit description
- title: str or NoneType, default None
description attribute of dataset
- source: str or NoneType, default None
source attribute of dataset
- reference: str or NoneType, default None
reference attribute of dataset
- date: bool, default True
spatialobjects contain date information- clobber: bool, default True
Overwrite an existing HDF5 file
- verbose: bool, default False
Output file and variable information
- to_index(filename, file_list, format=None, date=True, **kwargs)[source]
Write a
spatialobject to index of ascii, netCDF4 or HDF5 files- Parameters:
- filename: str
full path of index file to be written
- file_list: list
list of filenames for each output file
- format: str or NoneType, default None
format of files in index
'ascii''netCDF4''HDF5'
- date: bool, default True
spatialobject contains date information- verbose: bool, default False
print file and variable information
- kwargs: dict
keyword arguments for output writers
- to_file(filename, format=None, date=True, **kwargs)[source]
Write a
spatialobject to a specified format- Parameters:
- filename: str
full path of output file
- format: str or NoneType, default None
file format
'ascii''netCDF4''HDF5'
- date: bool, default True
spatialobject contains date information- verbose: bool, default False
print file and variable information
- kwargs: dict
keyword arguments for output writers
- default_field_mapping(variables)[source]
Builds field mappings from a variable list
- Parameters:
- variables: list
netCDF4/HDF5 variables names to be mapped
lonnamelatnamevarnametimename
- Returns:
- field_mapping: dict
Field mappings for netCDF4/HDF5 read and write functions
- extend_matrix()[source]
Extends a global matrix to wrap along longitudes
- Returns:
- temp: float
extended matrix
- index(indice, date=True)[source]
Subset a
spatialobject to specific index- Parameters:
- indice: int
index in matrix for subsetting
- date: bool, default True
spatialobjects contain date information
- subset(months)[source]
Subset a
spatialobject to specific GRACE/GRACE-FO months- Parameters:
- months: int
GRACE/GRACE-FO to subset
- offset(var)[source]
Offset a
spatialobject by a constant- Parameters:
- var: float
scalar value to which the
spatialobject will be offset
- scale(var)[source]
Multiply a
spatialobject by a constant- Parameters:
- var: float
scalar value to which the
spatialobject will be multiplied
- mean(apply=False, indices=Ellipsis)[source]
Compute mean spatial field and remove from data if specified
- Parameters:
- apply: bool, default False
remove the mean field from the input
spatialobject- indices: int, default Ellipsis
indices of input
spatialobject to compute mean
- flip(axis=0)[source]
Reverse the order of data and dimensions along an axis
- Parameters:
- axis: int, default 0
axis to reorder
- transpose(axes=None)[source]
Transpose or permute the axes of a
spatialobject- Parameters:
- axis: int or NoneType, default None
order of the output axes
- sum(power=1)[source]
Compute summation of a
spatialobject- Parameters:
- power: int, default 1
apply a power before calculating summation
- power(power)[source]
Raise a
spatialobject to a power- Parameters:
- power: int
power to which the
spatialobject will be raised
- replace_invalid(fill_value, mask=None)[source]
Replace the masked values with a new
fill_value- Parameters:
- fill_value: float
Replacement invalid value
- mask: bool or NoneType, default None
Update the current mask
- property dtype
Main data type of
spatialobject
- property spacing
Step size of
spatialobject[longitude,latitude]
- property extent
Bounds of
spatialobject[minimum longitude, maximum longitude, minimum latitude, maximum latitude]
- property shape
Dimensions of
spatialobject
- property ndim
Number of dimensions in
spatialobject
- class gravity_toolkit.scaling_factors(**kwargs)[source]
Inheritance of
spatialclass for outputting scaling factors- Attributes:
- data: float
spatial scaling factor data
- error: float
spatial scaling factor errors
- magnitude: float
spatial magnitude of the original data
- mask: bool
spatial grid mask
- x: float
x-coordinate array
- y: float
y-coordinate array
- lon: float
grid longitudes
- lat: float
grid latitudes
- fill_value: float or NoneType, default None
invalid value for spatial grid data
- attributes: dict
attributes of spatial variables
- extent: list, default [None,None,None,None]
spatial grid bounds
[minimum x, maximum x, minimum y, maximum y]- spacing: list, default [None,None]
grid step size
[x, y]- shape: tuple
dimensions of spatial object
- ndim: int
number of dimensions of spatial object
- filename: str
input or output filename
- from_ascii(filename, **kwargs)[source]
Read a
scaling_factorsobject from an ascii file- Parameters:
- filename: str
full path of input ascii file
- compression: str or NoneType, default None
file compression type
'gzip''zip''bytes'
- spacing: list, default [None,None]
grid step size
[longitude,latitude]- extent: list, default [None,None,None,None]
spatial grid bounds
[minimum longitude, maximum longitude, minimum latitude, maximum latitude]- nlat: int or NoneType, default None
length of latitude dimension
- nlon: int or NoneType, default None
length of longitude dimension
- columns: list, default [‘lon’,’lat’,’kfactor’,’error’,’magnitude’]
variable names for each column
- header: int, default 0
Number of rows of header lines to skip
- verbose: bool, default False
print file and variable information
- to_ascii(filename, **kwargs)[source]
Write a
scaling_factorsobject to ascii file- Parameters:
- filename: str
full path of output ascii file
- verbose: bool, default False
Output file and variable information