Skip to content

Convert Data Formats

cs2sc(field)

converts the square (L+1)x(L+1) matrix 'field', containing spherical harmonics coefficients in |C\S| storage format into a rectangular (L+1)x(2L+1) matrix in /S|C\ format.

Parameters:

Name Type Description Default
field ndarray

the square (L+1)x(L+1) numpy matrix field , containing spherical harmonics coefficients in |C\S| storage format

required

Returns:

Type Description

numpy.ndarray: Rectangular (L+1)x(2L+1) numpy matrix in /S|C\ format

Raises:

Type Description
TypeError

Input neither in cs nor in sc format

Todo
  • Rather use TypeError instead of base Exception

Examples:

>>> sc_shcoeff = cs2sc(cs_shcoeff)
TO DO: write the output
Source code in pyshbundle/cs2sc.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def cs2sc(field):
    """converts the square (L+1)x(L+1) matrix 'field', containing
    spherical harmonics coefficients in |C\S| storage format into a 
    rectangular (L+1)x(2L+1) matrix in  /S|C\ format.

    Args:
        field (np.ndarray): the square (L+1)x(L+1) numpy matrix field , containing
                   spherical harmonics coefficients in |C\S| storage format

    Returns:
        numpy.ndarray: Rectangular (L+1)x(2L+1) numpy matrix in  /S|C\ format

    Raises:
        TypeError: Input neither in cs nor in sc format

    Todo:
        + Rather use TypeError instead of base Exception

    Examples:
        >>> sc_shcoeff = cs2sc(cs_shcoeff)
        TO DO: write the output
    """
    rows = len(field)
    cols = len(field[0])

    if (rows != cols) and (cols != 2*rows - 1):
        raise TypeError("Input neither in cs nor in sc format")
    elif cols == 2*rows - 1:
        sc = field
    else:
        c    = numpy.tril(field)
        ut   = numpy.triu(field)
        i = numpy.identity(rows)
        i = 1-i
        s    = numpy.fliplr(numpy.transpose(numpy.multiply(ut, i, )))
        sc   = numpy.concatenate((s[:,1:rows], c), axis=1)

    return(sc)

sc2cs(field)

converts the rectangular \((L+1) imes (2L+1)\) matrix FIELD, containing spherical harmonics coefficients in /S|C\ storage format into a square (L+1)x(L+1) matrix in |C\S| format.

Parameters:

Name Type Description Default
field ndarray

the rectangular (L+1)x(2L+1) matrix FIELD, containing spherical harmonics coefficients in /S|C\ storage format

required

Returns:

Name Type Description
cs ndarray

square (L+1)x(L+1) matrix in |C\S| format

References

See the SHBundle docs or PySHBundle docs for more info about SH coeff. storage and retrival formats being implementd.

Examples:

>>> cs_fmt = sc2cs(field)
TO DO: show suitable output
Source code in pyshbundle/sc2cs.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def sc2cs(field):
    """converts the rectangular $(L+1) \times (2L+1)$ matrix FIELD, containing
    spherical harmonics coefficients in /S|C\ storage format into a 
    square (L+1)x(L+1) matrix in |C\S| format.

    Parameters:
        field (numpy.ndarray()):
            the rectangular (L+1)x(2L+1) matrix FIELD, containing
            spherical harmonics coefficients in /S|C\ storage format

    Returns: 
        cs (numpy.ndarray): 
            square (L+1)x(L+1) matrix in |C\S| format

    References:
        See the SHBundle docs or PySHBundle docs for more info about SH coeff. storage and retrival formats being implementd.

    Examples:
        >>> cs_fmt = sc2cs(field)
        TO DO: show suitable output
    """

    rows = len(field)
    cols = len(field[0])

    if (rows!=cols) and (cols!=2*rows - 1):
        sc2cs.exit("Input neither in cs nor in sc format")
    elif cols == rows:
        cs = field
    else:
        c    = field[:, rows-1:cols]
        st   = numpy.transpose(numpy.fliplr(field[:, 0:rows-1]))
        z    = numpy.zeros([1,rows])
        s    = numpy.concatenate((st, z), axis=0)
        cs   = numpy.add(c, s)

    return(cs)

clm2cs(data_mat, lmax, sigma_flag=False)

Converts the format from CLM to |C\S| Under the hood uses the clm2sc and sc2cs function

Parameters:

Name Type Description Default
data_mat ndarray

list containing [degree; order; clm; slm; delta clm; delta slm; start data; end date]

required
lmax int

Max Degree of the spherical harmonic expansion

required
sigma_flag boolean

Flag to return the standard deviation data in |C\S| format or not. Defaults to False

False

Returns:

Type Description

numpy.ndarray: Spherical Harmonic Coefficients in |C\S| format

Source code in pyshbundle/clm2cs.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def clm2cs(data_mat: np.ndarray, lmax: int, sigma_flag=False):
    """Converts the format from CLM to |C\S|
    Under the hood uses the `clm2sc` and `sc2cs` function

    Args:
        data_mat (numpy.ndarray): list containing [degree;  order; clm; slm; delta clm; delta slm; start data; end date]
        lmax (int): Max Degree of the spherical harmonic expansion
        sigma_flag (boolean): Flag to return the standard deviation data in |C\S| format or not. Defaults to False

    Returns:
        numpy.ndarray: Spherical Harmonic Coefficients in |C\S| format

    """
    if sigma_flag:
        sc_mat, dev_sc = clm2sc(data_mat=data_mat, lmax=lmax, sigma_flag=True)
        return sc2cs(sc_mat), sc2cs.sc2cs(dev_sc)
    else:
        sc_mat = clm2sc(data_mat=data_mat, lmax=lmax, sigma_flag=False)
        return sc2cs(sc_mat)

clm2sc(data_mat, lmax, sigma_flag=False)

Converts the spherical harmonic coefficients from clm format to /S|C\ format

Parameters:

Name Type Description Default
data_mat ndarray

list containing [degree; order; clm; slm; delta clm; delta slm; start data; end date]

required
lmax int

Max Degree of the spherical harmonic expansion

required
sigma_flag boolean

Flag to return the standard deviation data in /S|C\ format or not. Defaults to False

False

Returns:

Type Description

numpy.ndarray: Spherical Harmonic Coefficients in /S|C\ format

References

Refer to the SHBundle or PySHBundle docs for the different data storage and retrival formats.

Source code in pyshbundle/clm2sc.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def clm2sc(data_mat: np.ndarray, lmax: int, sigma_flag=False):
    """Converts the spherical harmonic coefficients from clm format to /S|C\ format

    Args:
        data_mat (numpy.ndarray): list containing [degree;  order; clm; slm; delta clm; delta slm; start data; end date]
        lmax (int): Max Degree of the spherical harmonic expansion
        sigma_flag (boolean): Flag to return the standard deviation data in /S|C\ format or not. Defaults to False

    Returns:
        numpy.ndarray: Spherical Harmonic Coefficients in /S|C\ format

    References:
        Refer to the SHBundle or PySHBundle docs for the different data storage and retrival formats.

    """    

    sc_mat = np.zeros((lmax+1, 2*lmax + 2))
    dev_sc_mat = np.zeros((lmax+1, 2*lmax + 2))

    # as per the convention
    clm = data_mat[:, 2]
    slm = data_mat[:, 3]
    clm_std_dev = data_mat[:, 4]
    slm_std_dev = data_mat[:, 5]

    i = 0
    for index1 in range(0,lmax+1, 1):
        for index2 in range(0,index1+1, 1):

            sc_mat[index1, lmax-index2] = slm[i]
            sc_mat[index1, lmax+index2+1] = clm[i]

            dev_sc_mat[index1, lmax-index2] = slm_std_dev[i]
            dev_sc_mat[index1, lmax+index2+1] = clm_std_dev[i]

            i = i + 1

    sc_mat = np.delete(sc_mat, lmax, 1)
    dev_sc_mat = np.delete(dev_sc_mat, lmax, 1)

    if sigma_flag:
        return sc_mat, dev_sc_mat
    else:
        return sc_mat

klm2sc(data)

Converts the spherical harmonic coefficients from klm format to /S|C\ format

Parameters:

Name Type Description Default
data list

list containing [degree; order; clm; slm; delta clm; delta slm; start data; end date]

required

Returns:

Type Description

np.ndarray: Spherical Harmonic Coefficients in /S|C\ format [[files or months]; [2-D matrix of /S|C\ format]]

np.ndarray: Standard Deviations of correcponding Spherical Harmonic Coefficients in /S|C\ format [[files or months]; [2-D matrix of /S|C\ format]]

Source code in pyshbundle/klm2sc.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def klm2sc(data):
    """Converts the spherical harmonic coefficients from klm format to /S|C\ format

    Args:
        data (list): list containing [degree;  order; clm; slm; delta clm; delta slm; start data; end date]

    Returns:
        np.ndarray: Spherical Harmonic Coefficients in /S|C\ format [[files or months]; [2-D matrix of /S|C\ format]]
        np.ndarray: Standard Deviations of correcponding Spherical Harmonic Coefficients in /S|C\ format [[files or months]; [2-D matrix of /S|C\ format]]
    """
    # import pickle
    # with open("/path/saved_as_num", "rb") as pk:
    #     data=pickle.load(pk)

    ''' Read variables '''
    no_of_years = len(data[0])
    degree = data[0]
    clm = data[2]
    slm = data[3]
    clm_std_dev = data[4]
    slm_std_dev = data[5]

    lmax=degree[0][-1]
    degree_order=int((lmax+1)*(lmax+2)/2)

    ''' Count no of months of data '''
    month_count =0
    for i in range(0,len(data[0]),1):
        month_count= month_count+round(len(data[0][i])/degree_order)

    ''' klm >>> sc '''
    month = 0
    sc_mat = np.zeros([month_count,lmax+1,2*lmax+2])
    dev_sc_mat = np.zeros((month_count, lmax+1, 2*lmax + 2))

    for year in range(0,no_of_years,1):
        index2 =0
        for tile in range(0,int(len(clm[year])/degree_order),1):  
            for index1 in range(0,lmax+1,1):
                sc_mat[month,index1:,lmax-index1] = slm[year][(index2):(index2+lmax-index1+1)]
                sc_mat[month,index1:,index1+lmax] = clm[year][(index2):(index2+lmax-index1+1)]

                dev_sc_mat[month,index1:,lmax-index1] = slm_std_dev[year][(index2):(index2+lmax-index1+1)]
                dev_sc_mat[month,index1:,index1+lmax] = clm_std_dev[year][(index2):(index2+lmax-index1+1)]



                #print(month,'\t',index1,'\t',Lmax-index1,'\t',year,'\t',index2,'\t',index2+Lmax-index1+1)
                index2 = index2+lmax-index1+1
            month=month+1

    sc_mat=np.delete(sc_mat,lmax,axis=2)
    dev_sc_mat=np.delete(dev_sc_mat,lmax,axis=2)

    print('Conversion into clm format complete')
    return sc_mat, dev_sc_mat

klm2sc_new(data_mat, lmax, sigma_flag=False)

Converts the spherical harmonic coefficients from klm format to /S|C\ format

Parameters:

Name Type Description Default
data_mat ndarray

A 2-D matrix(numpy ndarray)

required
lmax int

maximum degree of spherical harmonic expansion

required
sigma_flag bool

Flag to return the associated standard deviation values. Defaults to False.

False

Returns:

Type Description

np.ndarray: Spherical Harmonic Coefficients or/and associated standard deviations in /S|C\ format

Source code in pyshbundle/klm2sc.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def klm2sc_new(data_mat: np.ndarray, lmax: int, sigma_flag=False):
    """Converts the spherical harmonic coefficients from klm format to /S|C\ format

    Args:
        data_mat (np.ndarray): A 2-D matrix(numpy ndarray)
        lmax (int): maximum degree of spherical harmonic expansion
        sigma_flag (bool, optional): Flag to return the associated standard deviation values. Defaults to False.

    Returns:
        np.ndarray: Spherical Harmonic Coefficients or/and associated standard deviations in /S|C\ format
    """
    sc_mat = np.zeros((lmax+1, 2*lmax + 2))
    dev_sc_mat = np.zeros((lmax+1, 2*lmax + 2))
    clm = data_mat[:, 2]
    slm = data_mat[:, 3]
    clm_std_dev = data_mat[:, 4]
    slm_std_dev = data_mat[:, 5]

    # first place the slm and then clm
    index2 =0
    for index1 in range(0,lmax+1,1):
        sc_mat[index1:, lmax-index1] = slm[(index2):(index2 + lmax-index1+1)]
        sc_mat[index1:, index1+lmax] = clm[(index2):(index2 + lmax-index1+1)]

        dev_sc_mat[index1:, lmax-index1] = slm_std_dev[(index2):(index2 + lmax-index1+1)]
        dev_sc_mat[index1:, index1+lmax] = clm_std_dev[(index2):(index2 + lmax-index1+1)]

        index2 = index2 + lmax-index1+1

    sc_mat=np.delete(sc_mat,lmax,axis=1)
    dev_sc_mat=np.delete(dev_sc_mat,lmax,axis=1)

    if sigma_flag:
        return sc_mat, dev_sc_mat
    else: 
        return sc_mat

Reference

  • Nico Sneeuw, Matthias Weigelt, Markus Antoni, Matthias Roth, Balaji Devaraju, et. al. (2021). SHBUNDLE 2021. http://www.gis.uni-stuttgart.de/research/projects/Bundles.