from _shtools import shexpanddhc
from utilities import *
import numpy as np


def SHExpandDHC(grid, lmax_calc, degmax=None, norm=4, sampling=2, csphase=1):
    """
    Expand a grid data into its COMPLEX coefficients.
    ARGUMENTS
        grid      - 2D array with data. Should be of datatype complex128, but if not
                    then it is automatically converted from its datatype to complex128
                    if possible. 
        lmax_calc - the maximum degree to decompose upto
        degmax    - degmax determines the shape of the returned array (see RETURNS)
        norm      - SH normalization
        sampling  - If 1: grid is shaped (n,n), If 2: grid is shaped (n,2n)
        csphase   - If 1, include the condon-shortley phase factor
    RETURNS
        3D array of shape (2, lmax_calc+1, lmax_calc+1) containing the sine and 
        cosine coefficients. If degmax is specified and is greater than lmax_calc then
        the shape of the returned array is (2, degmax+1, degmax+1). 
    """
    if (grid.dtype != np.dtype('complex128')):
        print("PySHTOOLS Warning: In SHExpandDHC the input grid datatype is {0}. \
It was automatically converted to complex128.".format(grid.dtype))
        # print("         It was automatically converted to complex128.")
        _grid = np.complex128(grid)
        converted = True
    else:
        converted = False
    
    s = grid.shape
    if (degmax == None):
        py_lmax = lmax_calc
    else:
        py_lmax = degmax
        if (py_lmax < lmax_calc):
            print("ERROR !!!")
            print(" |- In routine - PySHTOOLS::SHExpandDH")
            print(" |->>> degmax cannot be less than lmax_calc")
            raise PySHTOOLSError()
    
    CheckNorm(norm)
    CheckCSphase(csphase)
    CheckSampling1(sampling)
    CheckSampling2(sampling, s)
    if not converted:
        cilm, lmax = shexpanddhc(grid, lmax_calc, py_lmax, sampling=sampling, csphase=csphase, norm=norm)
    else:
        cilm, lmax = shexpanddhc(_grid, lmax_calc, py_lmax, sampling=sampling, csphase=csphase, norm=norm)
    return cilm
from _shtools import shexpanddhc
from utilities import *
import numpy as np


def SHExpandDHC(grid, lmax_calc, degmax=None, norm=4, sampling=2, csphase=1):
    """
    Expand a grid data into its COMPLEX coefficients.
    ARGUMENTS
        grid      - 2D array with data. Should be of datatype complex128, but if not
                    then it is automatically converted from its datatype to complex128
                    if possible. 
        lmax_calc - the maximum degree to decompose upto
        degmax    - degmax determines the shape of the returned array (see RETURNS)
        norm      - SH normalization
        sampling  - If 1: grid is shaped (n,n), If 2: grid is shaped (n,2n)
        csphase   - If 1, include the condon-shortley phase factor
    RETURNS
        3D array of shape (2, lmax_calc+1, lmax_calc+1) containing the sine and 
        cosine coefficients. If degmax is specified and is greater than lmax_calc then
        the shape of the returned array is (2, degmax+1, degmax+1). 
    """
    if (grid.dtype != np.dtype('complex128')):
        print("PySHTOOLS Warning: In SHExpandDHC the input grid datatype is {0}. \
It was automatically converted to complex128.".format(grid.dtype))
        # print("         It was automatically converted to complex128.")
        _grid = np.complex128(grid)
        converted = True
    else:
        converted = False
    
    s = grid.shape
    if (degmax == None):
        py_lmax = lmax_calc
    else:
        py_lmax = degmax
        if (py_lmax < lmax_calc):
            print("ERROR !!!")
            print(" |- In routine - PySHTOOLS::SHExpandDH")
            print(" |->>> degmax cannot be less than lmax_calc")
            raise PySHTOOLSError()
    
    CheckNorm(norm)
    CheckCSphase(csphase)
    CheckSampling1(sampling)
    CheckSampling2(sampling, s)
    if not converted:
        cilm, lmax = shexpanddhc(grid, lmax_calc, py_lmax, sampling=sampling, csphase=csphase, norm=norm)
    else:
        cilm, lmax = shexpanddhc(_grid, lmax_calc, py_lmax, sampling=sampling, csphase=csphase, norm=norm)
    return cilm
Expand a grid data into its COMPLEX coefficients.     ARGUMENTS         grid      - 2D array with data. Should be of datatype complex128, but if not                     then it is automatically converted from its datatype to complex128                     if possible.          lmax_calc - the maximum degree to decompose upto         degmax    - degmax determines the shape of the returned array (see RETURNS)         norm      - SH normalization         sampling  - If 1: grid is shaped (n,n), If 2: grid is shaped (n,2n)         csphase   - If 1, include the condon-shortley phase factor     RETURNS         3D array of shape (2, lmax_calc+1, lmax_calc+1) containing the sine and          cosine coefficients. If degmax is specified and is greater than lmax_calc then         the shape of the returned array is (2, degmax+1, degmax+1). # print("         It was automatically converted to complex128.")