pyharm.shc#
Module to work with spherical harmonic coefficients. Defines the Shc
class, including its methods that are designed to:
read/write spherical harmonic coefficients,
rescale spherical harmonic coefficients, and
compute (difference) degree variances and amplitudes.
Note
This documentation is written for double precision version of PyHarm.
- pyharm.shc.WRITE_N: int = 0#
Ordering scheme to write spherical harmonic coefficients with the
pyharm.shc.Shc.to_file()method: harmonic degree varies fastest.- Type:
int
- pyharm.shc.WRITE_M: int = 1#
Ordering scheme to write spherical harmonic coefficients with the
pyharm.shc.Shc.to_file()method: harmonic order varies fastest.- Type:
int
- class pyharm.shc.Shc(nmax, mu, r, coeffs)#
Class for spherical harmonic coefficients.
To create an
Shcclass instance, always use one of the following factory methods:Examples
>>> import pyharm as ph >>> shcs = ph.shc.Shc.from_garbage(10)
- Parameters:
nmax (integer) – Maximum spherical harmonic degree
mu (floating point) – Scaling parameter
r (floating point) – Radius of the reference sphere
coeffs (None, 0 or tuple) –
Determines the way of initializing spherical harmonic coefficients:
Noneto not initialize spherical harmonic coefficients (mallocin C),0to set all spherical harmonic coefficients to zero (callocin C),(c, s)to define spherical harmonic coefficients based on two numpy floating point arrayscands, each of shape(((nmax + 2) * (nmax + 1)) // 2,). For ordering scheme of the coefficients in thecandsarrays, see thecandsproperties.
Note
Once an
Shcclass instance is created, its attributes are not writable. Therandmuattributes can properly be changed by therescale()method.- property nmax#
Maximum harmonic degree of the spherical harmonic coefficients.
- property mu#
Scaling parameter \(\mu\) associated with the spherical harmonic coefficients, for instance, the geocentric gravitational constant. In case the coefficients are not associated with any scaling parameter (as it is, for instance, with planetary topographies), simply set this variable to
1.0(not to0.0!).
- property r#
Radius of the reference sphere \(R\), to which the spherical harmonic coefficients refer (are scaled). The value must be greater than zero. To get the unit sphere, as needed, for instance, when working with planetary topographies, set this variable to
1.0.
- property c#
The \(\bar{C}_{nm}\) spherical harmonic coefficients. A numpy array with the shape (((
nmax+ 2) * (nmax+ 1)) / 2,) and the array structure \(\bar{C}_{00}\), \(\bar{C}_{10}\), …, \(\bar{C}_{\mathrm{nmax},0}\), \(\bar{C}_{1,1}\), …, \(\bar{C}_{\mathrm{nmax},\mathrm{nmax}}\).
- property owner#
If
True, the memory associated with spherical harmonic coefficients is owned by CHarm and therefore it is automatically deallocated by CHarm when the user deletesShcclass instances or when the instances get out of scope, etc. Theownerattribute isTrueforShcinstances returned by all factory methods except for thefrom_arrays()method.Examples
>>> import pyharm as ph >>> shcs = ph.shc.Shc.from_garbage(10) >>> del shcs # Deletes "shcs" and properly deallocates all memory >>> # that is associated with "shcs"
If
False, neither CHarm nor PyHarm own the memory so neither CHarm nor PyHarm will ever deallocate it. This is the case ofShcclass instances returned by thefrom_arrays()factory method. This method buildsShcinstances from user-owned external numpy arrays, so it is the responsibility of the user to delete the original arrays (if needed) once theShcclass instance is deleted or got out of scope, etc.Examples
>>> import numpy as np >>> import pyharm as ph >>> nmax = 10 >>> ncs = ((nmax + 2) * (nmax + 1)) // 2 >>> c = np.random.randn(ncs) >>> s = np.random.randn(ncs) >>> shcs = ph.shc.Shc.from_arrays(nmax, c, s) >>> del shcs # Deletes "shcs" but not the original "c" and "s" arrays >>> # Here, you can still work with "c" and "s" >>> del c, s # Finally, release the memory associated with "c" and >>> # "s" if needed
- classmethod from_garbage(nmax, mu=np.float64(1.0), r=np.float64(1.0))#
Returns an
Shcclass instance with uninitialized spherical harmonic coefficients (mallocin C).
- classmethod from_zeros(nmax, mu=np.float64(1.0), r=np.float64(1.0))#
Returns an
Shcclass instance with all spherical harmonic coefficients initialized to zero (callocin C).
- classmethod from_arrays(nmax, c, s, mu=np.float64(1.0), r=np.float64(1.0))#
Returns an
Shcclass instance with spherical harmonic coefficients copied from thecandsinput arrays. The copy is shallow, meaning that thecandsattributes of the returnedShcclass instance share the memory space with the inputcandsarrays.- Parameters:
nmax (integer) – Maximum spherical harmonic degree
c (numpy array of floating points) – Spherical harmonic coefficients \(\bar{C}_{nm}\). For details on the structure of the array, see
c.s (numpy array of floating points) – Spherical harmonic coefficients \(\bar{S}_{nm}\). For details on the structure of the array, see
s.mu (floating point) – Scaling parameter, optional. Default is
1.0.r (floating point) – Radius of the reference sphere, optional. Default is
1.0.
- Returns:
out – An
Shcclass instance- Return type:
- classmethod from_file(file_type, pathname, nmax=-1, epoch=None, encoding='utf-8')#
Reads spherical harmonic coefficients up to degree
nmaxfrom thepathnamefile of a givenfile_type. For time variable models stored in thegfcfile type,epochadditionally transforms the coefficients to a given epoch.Tip
To get the maximum harmonic degree stored in
pathname, usenmax_from_file().Tip
To print all supported file types, use
get_file_types():>>> import pyharm as ph >>> ph.shc.Shc.get_file_types()
For the structure of the file types, refer to charm_shc.
- Parameters:
file_type (str) – Type of the input file
pathname (str) – Input file path
nmax (integer) – Maximum harmonic degree to read the spherical harmonic coefficients. If negative, all coefficients are read. Default value is
-1.epoch (str) –
Epoch, to which spherical harmonic coefficients from
gfcfile types are transformed in case of time variable gravity field models; optional. For the structure of the string, refer to charm_shc. This input parameter is used only iffile_typeisgfc. Default value isNone.encoding (str) –
Encoding of the
pathnameandepochstrings. In case you have special characters in yourpathnamestring, you may need to select proper encoding.The encoding does not apply to the content of the file. The file reading is done by CHarm, so the file content encoding depends on your operating system.
- Returns:
out – An
Shcclass instance- Return type:
- to_file(file_type, pathname, nmax=-1, formatting='%0.18e', ordering=0, encoding='utf-8')#
Writes an
Shcclass instance up to degreenmaxto a filepathnameof a givenfile_type. Iffile_typerepresents a text file (dov,tblormtx),formattingspecifies the formatting for all floating point numbers. Iffile_typeisdovortbl,orderingdefines the ordering of spherical harmonic coefficients in the output file.Tip
To print all supported file types, use
get_file_types():>>> import pyharm as ph >>> ph.shc.Shc.get_file_types()
For the structure of the file types, refer to charm_shc.
- Parameters:
file_type (str) – Type of the input file
nmax (integer) – Maximum harmonic degree to write the spherical harmonic coefficients. If negative, all coefficients are written. Default value is
-1.pathname (str) – Output file path
formatting (str) – Formatting to write floating point numbers to text formats, optional. Default is
'%0.18e'.ordering (integer) – Scheme to sort spherical harmonic coefficients when
file_typeisdovortbl, optional. Accepted values arepyharm.shc.WRITE_Nandpyharm.shc.WRITE_M. Default ispyharm.shc.WRITE_N.encoding (str) –
Encoding of the
pathnameandformattingstrings. In case you have special characters in yourpathnamestring, you may need to select proper encoding.The encoding does not apply to the content of the file. The file writting is done by CHarm, so the file content encoding depends on your operating system.
- static get_file_types()#
Prints all file types supported by the
to_file()andfrom_file()methods of theShcclass.
- static nmax_from_file(file_type, pathname, encoding='utf-8')#
Returns the maximum harmonic degree of coefficients stored in the
pathnamefile that is of a givenfile_type.Tip
To print all supported file types, use
get_file_types():>>> import pyharm as ph >>> ph.shc.Shc.get_file_types()
For the structure of the file types, refer to charm_shc.
- Parameters:
file_type (str) – Type of the input file
pathname (str) – Input file path
encoding (str) –
Encoding of the
pathnamestring. In case you have special characters in yourpathnamestring, you may need to select proper encoding.The encoding does not apply to the content of the input file. The file reading is done by CHarm, so the file content encoding depends on your operating system.
- Returns:
out – Maximum harmonic degree of the model
- Return type:
integer
- get_coeffs(n=None, m=None)#
Returns spherical harmonic coefficients \(\bar{C}_{nm}\) and \(\bar{S}_{nm}\) of degree
nand orderm. If the returned variables are arrays, these are deep copies, meaning that the arrays have their own memory space.The behaviour of the method depends on the type of the input variables
nandm.If both
nandmare integers, returned are two spherical harmonic coefficients \(\bar{C}_{nm}\) and \(\bar{S}_{nm}\), each of degreenand orderm. The two returned values are floating points.If
nis integer andmisNone, returned are two arrays of spherical harmonic coefficients \(\bar{C}_{nm}\) and \(\bar{S}_{nm}\) of degreenand all corresponding ordersm = 0, 1, ..., n.If
nisNoneandmis integer, returned are two arrays of spherical harmonic coefficients \(\bar{C}_{nm}\) and \(\bar{S}_{nm}\) of ordermand all corresponding degreesn = m, m + 1, ..., self.nmax.If
nandmare lists of equal size, returned are two arrays of spherical harmonic coefficients \(\bar{C}_{nm}\) and \(\bar{S}_{nm}\) of degrees taken from thenlist and orders taken from themlist.
Note
When the method returns numpy arrays, the output arrays are always deep copies of the original spherical harmonic coefficients.
Examples
>>> import numpy as np >>> import pyharm as ph >>> nmax = 10 >>> ncs = ((nmax + 2) * (nmax + 1)) // 2 >>> c = np.random.randn(ncs) >>> s = np.random.randn(ncs) >>> shcs = ph.shc.Shc.from_arrays(nmax, c, s) >>> shcs.get_coeffs(3, 2) # Returns "C3,2" and "S3,2" >>> shcs.get_coeffs(n=3) # Returns "C3,0", "C3,1", "C3,2", "C3,3" and >>> # "S3,0", "S3,1", "S3,2", "S3,3" >>> shcs.get_coeffs(m=8) # Returns "C8,8", "C9,8", "C10,8" and >>> # "S8,8", "S9,8", "S10,8" >>> n = [3, 5, 6] >>> m = [2, 4, 6] >>> shcs.get_coeffs(n, m) # Returns "C3,2", "C5,4", "C6,6" and "S3,2", >>> # "S5,4", "S6,6"
- Parameters:
n (integer, list of integers, None) – Spherical harmonic degree, optional.
m (integer, list of integers, None) – Spherical harmonic order, optional.
- Returns:
c (floating point, numpy array of floating points) – Spherical harmonic coefficient(s) \(\bar{C}_{nm}\) of degree
nand orderms (floating point, numpy array of floating points) – The same as
cbut with the \(\bar{S}_{nm}\) coefficient(s)
- set_coeffs(n=None, m=None, c=None, s=None)#
Sets spherical harmonic coefficients \(\bar{C}_{nm}\) and \(\bar{S}_{nm}\) of degree
nand ordermto the values ofcands, respectively.If both
nandmare integers, sets the spherical harmonic coefficients \(\bar{C}_{nm}\) and/or \(\bar{S}_{nm}\) to the input parameterscand/ors, respectively.candsmust be floating point scalars.If
nis integer andmisNone, sets spherical harmonic coefficients of degreenand all corresponding harmonic ordersm = 0, 1, ..., nto the input parameterscand/ors.candsmust be numpy floating point arrays of shapes(n + 1,).If
nisNoneandmis integer, sets spherical harmonic coefficients of ordermand all corresponding harmonic degreesn = m, m + 1, ..., self.nmaxto the input parameterscand/ors.candsmust be numpy floating point arrays of shapes(self.nmax + 1 - m,).If
nandmare lists of equal size, sets spherical spherical harmonic coefficients of degrees and orders taken from thenandmlists, respectively, to the corresponding values taken from the input parameterscand/ors. The length of the input parameters must match.
Note
If the object’s
ownerattribute isTrue, the copy of the new coefficients is deep. IfownerisFalse, the copy is shallow.Examples
>>> import numpy as np >>> import pyharm as ph >>> nmax = 10 >>> ncs = ((nmax + 2) * (nmax + 1)) // 2 >>> c = np.random.randn(ncs) >>> s = np.random.randn(ncs) >>> shcs = ph.shc.Shc.from_arrays(nmax, c, s) >>> # Set "C3,2" and "S3,2" >>> shcs.set_coeffs(3, 2, 3.4, 1.3) >>> # Set "S3,2" >>> shcs.set_coeffs(3, 2, s=5.3) >>> # Set "C3,0", "C3,1", "C3,2", "C3,3" and "S3,0", "S3,1", "S3,2", >>> #"S3,3" >>> shcs.set_coeffs(n=3, c=np.array([1.1, 1.2, 1.3, 1.4]), >>> s=np.array([0.0, 1.2, 1.3, 1.4])) >>> # Set "C8,8", "C9,8", "C10,8" and "S8,8", "S9,8", "S10,8" >>> shcs.set_coeffs(m=8, c=np.array([1.1, 1.2, 1.3]), >>> s=np.array([1.1, 1.2, 1.3])) >>> # Set "C3,2", "C5,4", "C6,6" and "S3,2", "S5,4", "S6,6" >>> shcs.set_coeffs(n=[3, 5, 6], m=[2, 4, 6], >>> c=np.array([1.1, 1.2, 1.3]), >>> s=np.array([1.1, 1.2, 1.3]))
- Parameters:
n (integer, list of integers, None) – Spherical harmonic degree, optional.
m (integer, list of integers, None) – Spherical harmonic order, optional.
c (floating point, numpy array of floating points, None) – Spherical harmonic coefficient(s) \(\bar{C}_{nm}\), optional.
s (floating point, numpy array of floating points, None) – Spherical harmonic coefficient(s) \(\bar{S}_{nm}\), optional.
- get_degrees_orders()#
Returns arrays of spherical harmonic degrees and orders matching the spherical harmonic coefficients stored in
cands.
- rescale(mu=None, r=None)#
Rescales spherical harmonic coefficients to a new scaling parameter
muand/or a new radius of the reference spherer.
- dv(nmax=None)#
Computes degree variances up to degree
nmax.- Parameters:
nmax (integer) – Maximum spherical harmonic degree to compute the degree variances, optional. If not provided, it will be set to the object’s
nmax.- Returns:
out – A numpy floating point array with the degree variances
- Return type:
numpy array of floating points
- da(nmax=None)#
Computes degree amplitudes up to degree
nmax.- Parameters:
nmax (integer) – Maximum spherical harmonic degree to compute the degree amplitudes, optional. If not provided, it will be set to
self.nmax.- Returns:
out – A numpy floating point array with the degree amplitudes
- Return type:
numpy array of floating points
- ddv(shcs, nmax=None)#
Computes difference degree variances with respect to
shcsup to degreenmax.- Parameters:
- Returns:
out – A numpy floating point array with the difference degree variances
- Return type:
numpy array of floating points
- dda(shcs, nmax=None)#
Computes difference degree amplitudes with respect to
shcsup to degreenmax.- Parameters:
- Returns:
out – A numpy floating point array with the difference degree amplitudes
- Return type:
numpy array of floating points