from dataclasses import dataclass, field, asdict
from copy import copy
from enum import Enum, auto
from typing import Sequence, TYPE_CHECKING, Optional, Union, TypeVar, Type
import numpy as np
from .witt import witt
import lightweaver.constants as Const
from numpy.polynomial.legendre import leggauss
from .utils import ConvergenceError, view_flatten, get_data_path
from .atomic_table import PeriodicTable, AtomicAbundance, DefaultAtomicAbundance
import astropy.units as u
import pickle

if TYPE_CHECKING:
    from .LwCompiled import LwSpectrum

class ScaleType(Enum):
    '''
    Atmospheric scales used in the definition of 1D atmospheres to allow the
    correct conversion to a height based system.
    Options:

        - `Geometric`

        - `ColumnMass`

        - `Tau500`

    '''
    Geometric = 0
    ColumnMass = auto()
    Tau500 = auto()

class BoundaryCondition:
    '''
    Base class for boundary conditions.

    Defines the interface; do not use directly.

    Attributes
    ----------
    These attributes are only available after set_required_angles has been called.

    mux : np.ndarray
        The mu_x to return from compute_bc (in order).
    muy : np.ndarray
        The mu_y to return from compute_bc (in order).
    muz : np.ndarray
        The mu_z to return from compute_bc (in order).
    indexVector : np.ndarray
        A 2D array of integer shape (mu, toObs) - where mu is the mu index on
        the associated atmosphere - relating each index of the second (Nrays)
        axis of a pair of (mu, toObs). Used to construct and destructure this
        array.

    '''
    def compute_bc(self, atmos: 'Atmosphere', spect: 'LwSpectrum') -> np.ndarray:
        '''
        Called when the radiation boundary condition is needed by the backend.

        Parameters
        ----------
        atmos : Atmosphere
            The atmospheric object in which to compute the radiation.
        spect : LwSpectrum
            The computational spectrum object provided by the Context.

        Returns
        -------
        result : np.ndarray
            This function needs to return a contiguous array of shape [Nwave,
            Nrays, Nbc], where Nwave is the number of wavelengths in the
            wavelength grid, Nrays is the number of rays in the angular
            quadrature (also including up/down directions) ordered as
            specified by the mux/y/z and indexVector variables on this
            object, Nbc is the number of spatial positions the boundary
            condition needs to be defined at ordered in a flattened [Nz, Ny,
            Nx] fashion. (dtype: <f8)

        '''
        raise NotImplementedError

    def set_required_angles(self, mux, muy, muz, indexVector):
        '''
        The angles (and their ordering) to be used for this boundary
        condition (in the case of a callable)
        '''
        self.mux = mux
        self.muy = muy
        self.muz = muz
        self.indexVector = indexVector

class NoBc(BoundaryCondition):
    '''
    Indicates no boundary condition on the axis because it is invalid for the
    current simulation.
    Used only by the backend.
    '''
    pass

class ZeroRadiation(BoundaryCondition):
    '''
    Zero radiation boundary condition.
    Commonly used for coronal situations.
    '''
    pass

class ThermalisedRadiation(BoundaryCondition):
    '''
    Thermalised radiation (blackbody) boundary condition.
    Commonly used for photospheric situations.
    '''
    pass

class PeriodicRadiation(BoundaryCondition):
    '''
    Periodic boundary condition.
    Commonly used on the x-axis in 2D simulations.
    '''
    pass

def get_top_pressure(eos: witt, temp, ne=None, rho=None):
    '''
    Return a pressure for the top of atmosphere.
    For internal use.

    In order this is deduced from:
        - the electron density `ne`, if provided
        - the mass density `rho`, if provided
        - the electron pressure present in FALC

    Returns
    -------
    pressure : float
        pressure IN CGS [dyn cm-2]

    '''
    if ne is not None:
        pe = ne * Const.CM_TO_M**3 * eos.BK * temp
        return eos.pg_from_pe(temp, pe)
    elif rho is not None:
        return eos.pg_from_rho(temp, rho)

    pgasCgs = np.array([0.70575286, 0.59018545, 0.51286639, 0.43719268, 0.37731009,
        0.33516886, 0.31342915, 0.30604891, 0.30059491, 0.29207645,
        0.2859011 , 0.28119224, 0.27893046, 0.27949676, 0.28299726,
        0.28644693, 0.28825946, 0.29061192, 0.29340255, 0.29563072,
        0.29864548, 0.30776456, 0.31825915, 0.32137574, 0.3239401 ,
        0.32622212, 0.32792196, 0.3292243 , 0.33025437, 0.33146736,
        0.3319676 , 0.33217821, 0.3322355 , 0.33217166, 0.33210297,
        0.33203833, 0.33198508])
    tempCoord = np.array([  7600.,   7780.,   7970.,   8273.,   8635.,   8988.,   9228.,
        9358.,   9458.,   9587.,   9735.,   9983.,  10340.,  10850.,
        11440.,  12190.,  13080.,  14520.,  16280.,  17930.,  20420.,
        24060.,  27970.,  32150.,  36590.,  41180.,  45420.,  49390.,
        53280.,  60170.,  66150.,  71340.,  75930.,  83890.,  90820.,
        95600., 100000.])

    ptop = np.interp(temp, tempCoord, pgasCgs)
    return ptop

@dataclass
class Stratifications:
    '''
    Stores the optional derived z-stratifications of an atmospheric model.

    Attributes
    ----------
    cmass : np.ndarray
        Column mass [kg m-2].
    tauRef : np.ndarray
        Reference optical depth at 500 nm.
    '''
    cmass: np.ndarray
    tauRef: np.ndarray

    def dimensioned_view(self, shape) -> 'Stratifications':
        '''
        Makes an instance of `Stratifications` reshaped to the provided
        shape for multi-dimensional atmospheres.
        For internal use.

        Parameters
        ----------
        shape : tuple
            Shape to reform the stratifications, provided by
            `Layout.dimensioned_shape`.

        Returns
        -------
        stratifications : Stratifications
            Reshaped stratifications.
        '''
        strat = copy(self)
        strat.cmass = self.cmass.reshape(shape)
        strat.tauRef = self.tauRef.reshape(shape)
        return strat

    def unit_view(self) -> 'Stratifications':
        '''
        Makes an instance of `Stratifications`  with the correct `astropy.units`
        For internal use.

        Returns
        -------
        stratifications : Stratifications
            The same data with units applied.
        '''
        strat = copy(self)
        strat.cmass = self.cmass << u.kg / u.m**2
        strat.tauRef = self.tauRef << u.dimensionless_unscaled
        return strat

    def dimensioned_unit_view(self, shape) -> 'Stratifications':
        '''
        Makes an instance of `Stratifications` reshaped to the provided shape
        with the correct `astropy.units` for multi-dimensional atmospheres.
        For internal use.

        Parameters
        ----------
        shape : tuple
            Shape to reform the stratifications, provided by
            `Layout.dimensioned_shape`.

        Returns
        -------
        stratifications : Stratifications
            Reshaped stratifications with units.
        '''
        strat = self.dimensioned_view(shape)
        return strat.unit_view()

@dataclass
class Layout:
    '''
    Storage for basic atmospheric parameters whose presence is determined by problem dimensionality, boundary conditions and optional stratifications.

    Attributes
    ----------
    Ndim : int
        Number of dimensions in model.

    x : np.ndarray
        Ordinates of grid points along the x-axis (present for Ndim >= 2) [m].
    y : np.ndarray
        Ordinates of grid points along the y-axis (present for Ndim == 3) [m].
    z : np.ndarray
        Ordinates of grid points along the z-axis (present for all Ndim) [m].
    vx : np.ndarray
        x component of plasma velocity (present for Ndim >= 2) [m/s].
    vy : np.ndarray
        y component of plasma velocity (present for Ndim == 3) [m/s].
    vz : np.ndarray
        z component of plasma velocity (present for all Ndim) [m/s]. Aliased to `vlos` when `Ndim==1`
    xLowerBc : BoundaryCondition
        Boundary condition for the plane of minimal x-coordinate.
    xUpperBc : BoundaryCondition
        Boundary condition for the plane of maximal x-coordinate.
    yLowerBc : BoundaryCondition
        Boundary condition for the plane of minimal y-coordinate.
    yUpperBc : BoundaryCondition
        Boundary condition for the plane of maximal y-coordinate.
    zLowerBc : BoundaryCondition
        Boundary condition for the plane of minimal z-coordinate.
    zUpperBc : BoundaryCondition
        Boundary condition for the plane of maximal z-coordinate.
    '''

    Ndim: int
    x: np.ndarray
    y: np.ndarray
    z: np.ndarray
    vx: np.ndarray
    vy: np.ndarray
    vz: np.ndarray
    xLowerBc: BoundaryCondition
    xUpperBc: BoundaryCondition
    yLowerBc: BoundaryCondition
    yUpperBc: BoundaryCondition
    zLowerBc: BoundaryCondition
    zUpperBc: BoundaryCondition
    stratifications: Optional[Stratifications] = None

    @classmethod
    def make_1d(cls, z: np.ndarray, vz: np.ndarray,
                lowerBc: BoundaryCondition, upperBc: BoundaryCondition,
                stratifications: Optional[Stratifications]=None) -> 'Layout':
        '''
        Construct 1D Layout.
        '''

        return cls(Ndim=1, x=np.array(()), y=np.array(()),
                   z=z, vx=np.array(()), vy=np.array(()),
                   vz=vz, xLowerBc=NoBc(), xUpperBc=NoBc(),
                   yLowerBc=NoBc(), yUpperBc=NoBc(),
                   zLowerBc=lowerBc, zUpperBc=upperBc,
                   stratifications=stratifications)

    @classmethod
    def make_2d(cls, x: np.ndarray, z: np.ndarray,
                vx: np.ndarray, vz: np.ndarray,
                xLowerBc: BoundaryCondition, xUpperBc: BoundaryCondition,
                zLowerBc: BoundaryCondition, zUpperBc: BoundaryCondition,
                stratifications: Optional[Stratifications]=None) -> 'Layout':
        '''
        Construct 2D Layout.
        '''

        Bc = BoundaryCondition
        return cls(Ndim=2, x=x, y=np.array(()), z=z,
                   vx=vx, vy=np.array(()), vz=vz,
                   xLowerBc=xLowerBc, xUpperBc=xUpperBc,
                   yLowerBc=NoBc(), yUpperBc=NoBc(),
                   zLowerBc=zLowerBc, zUpperBc=zUpperBc,
                   stratifications=stratifications)

    @classmethod
    def make_3d(cls, x: np.ndarray, y: np.ndarray, z: np.ndarray,
                vx: np.ndarray, vy: np.ndarray, vz: np.ndarray,
                xLowerBc: BoundaryCondition, xUpperBc: BoundaryCondition,
                yLowerBc: BoundaryCondition, yUpperBc: BoundaryCondition,
                zLowerBc: BoundaryCondition, zUpperBc: BoundaryCondition,
                stratifications: Optional[Stratifications]=None) -> 'Layout':
        '''
        Construct 3D Layout.
        '''

        return cls(Ndim=3, x=x, y=y, z=z,
                   vx=vx, vy=vy, vz=vz,
                   xLowerBc=xLowerBc, xUpperBc=xUpperBc,
                   yLowerBc=yLowerBc, yUpperBc=yUpperBc,
                   zLowerBc=zLowerBc, zUpperBc=zUpperBc,
                   stratifications=stratifications)

    @property
    def Nx(self) -> int:
        '''
        Number of grid points along the x-axis.
        '''
        return self.x.shape[0]

    @property
    def Ny(self) -> int:
        '''
        Number of grid points along the y-axis.
        '''
        return self.y.shape[0]

    @property
    def Nz(self) -> int:
        '''
        Number of grid points along the z-axis.
        '''
        return self.z.shape[0]

    @property
    def Noutgoing(self) -> int:
        '''
        Number of grid points at which the outgoing radiation is computed.
        '''
        return max(1, self.Nx, self.Nx * self.Ny)

    @property
    def vlos(self) -> np.ndarray:
        if self.Ndim > 1:
            raise ValueError('vlos is ambiguous when Ndim > 1, use vx, vy, or vz instead.')
        return self.vz

    @property
    def Nspace(self) -> int:
        '''
        Number of spatial points present in the grid.
        '''
        if self.Ndim == 1:
            return self.Nz
        elif self.Ndim == 2:
            return self.Nx * self.Nz
        elif self.Ndim == 3:
            return self.Nx * self.Ny * self.Nz
        else:
            raise ValueError('Invalid Ndim: %d, check geometry initialisation' % self.Ndim)

    @property
    def tauRef(self):
        '''
        Alias to `self.stratifications.tauRef`, if computed.
        '''
        if self.stratifications is not None:
            return self.stratifications.tauRef
        else:
            raise ValueError('tauRef not computed for this Atmosphere')

    @property
    def cmass(self):
        '''
        Alias to `self.stratifications.cmass`, if computed.
        '''
        if self.stratifications is not None:
            return self.stratifications.cmass
        else:
            raise ValueError('tauRef not computed for this Atmosphere')

    @property
    def dimensioned_shape(self):
        '''
        Tuple defining the shape to which the arrays of atmospheric paramters
        can be reshaped to be indexed in a 1/2/3D fashion.
        '''
        if self.Ndim == 1:
            shape = (self.Nz,)
        elif self.Ndim == 2:
            shape = (self.Nz, self.Nx)
        elif self.Ndim == 3:
            shape = (self.Nz, self.Ny, self.Nx)
        else:
            raise ValueError('Unreasonable Ndim (%d)' % self.Ndim)
        return shape

    def dimensioned_view(self) ->  'Layout':
        '''
        Returns a view over the contents of Layout reshaped so all data has
        the correct (1/2/3D) dimensionality for the atmospheric model, as
        these are all stored under a flat scheme.
        '''
        layout = copy(self)
        shape = self.dimensioned_shape
        if self.stratifications is not None:
            layout.stratifications = self.stratifications.dimensioned_view(shape)
        if self.vx.size > 0:
            layout.vx = self.vx.reshape(shape)
        if self.vy.size > 0:
            layout.vy = self.vy.reshape(shape)
        if self.vz.size > 0:
            layout.vz = self.vz.reshape(shape)
        return layout

    def unit_view(self) -> 'Layout':
        '''
        Returns a view over the contents of the Layout with the correct
        `astropy.units`.
        '''
        layout = copy(self)
        layout.x = self.x << u.m
        layout.y = self.y << u.m
        layout.z = self.z << u.m
        layout.vx = self.vx << u.m / u.s
        layout.vy = self.vy << u.m / u.s
        layout.vz = self.vz << u.m / u.s
        if self.stratifications is not None:
            layout.stratifications = self.stratifications.unit_view()
        return layout

    def dimensioned_unit_view(self) -> 'Layout':
        '''
        Returns a view over the contents of Layout reshaped so all data has
        the correct (1/2/3D) dimensionality for the atmospheric model, and
        the correct `astropy.units`.
        '''
        layout = self.dimensioned_view()
        return layout.unit_view()

@dataclass
class Atmosphere:
    '''
    Storage for all atmospheric data. These arrays will be shared directly
    with the backend, so a modification here also modifies the data seen by
    the backend. Be careful to modify these arrays *in place*, as their data
    is shared by direct memory reference. Use the class methods to construct
    atmospheres of different dimensionality.

    Attributes
    ----------
    structure : Layout
        A layout structure holding the atmospheric stratification, and
        velocity description.
    temperature : np.ndarray
        The atmospheric temperature structure.
    vturb : np.ndarray
        The atmospheric microturbulent velocity structure.
    ne : np.ndarray
        The electron density structure in the atmosphere.
    nHTot : np.ndarray
        The total hydrogen number density distribution throughout the
        atmosphere.
    B : np.ndarray, optional
        The magnitude of the stratified magnetic field throughout the
        atmosphere (Tesla).
    gammaB : np.ndarray, optional
        Co-altitude of magnetic field vector (radians) throughout the
        atmosphere from the local vertical.
    chiB : np.ndarray, optional
        Azimuth of magnetic field vector (radians) in the x-y plane, measured
        from the x-axis.
    '''

    structure: Layout
    temperature: np.ndarray
    vturb: np.ndarray
    ne: np.ndarray
    nHTot: np.ndarray
    B: Optional[np.ndarray] = None
    gammaB: Optional[np.ndarray] = None
    chiB: Optional[np.ndarray] = None

    @property
    def Ndim(self) -> int:
        '''
        Ndim : int
            The dimensionality (1, 2, or 3) of the atmospheric model.
        '''
        return self.structure.Ndim

    @property
    def Nx(self) -> int:
        '''
        Nx : int
            The number of points in the x-direction discretisation.
        '''
        return self.structure.Nx

    @property
    def Ny(self) -> int:
        '''
        Ny : int
            The number of points in the y-direction discretisation.
        '''
        return self.structure.Ny

    @property
    def Nz(self) -> int:
        '''
        Nz : int
            The number of points in the y-direction discretisation.
        '''
        return self.structure.Nz

    @property
    def Noutgoing(self) -> int:
        '''
        Noutgoing : int
            The number of cells at the top of the atmosphere (that each produce a
            spectrum).
        '''
        return self.structure.Noutgoing

    @property
    def vx(self) -> np.ndarray:
        '''
        vx : np.ndarray
            x component of plasma velocity (present for Ndim >= 2) [m/s].
        '''
        return self.structure.vx

    @property
    def vy(self) -> np.ndarray:
        '''
        vy : np.ndarray
            y component of plasma velocity (present for Ndim == 3) [m/s].
        '''
        return self.structure.vy

    @property
    def vz(self) -> np.ndarray:
        '''
        vz : np.ndarray
            z component of plasma velocity (present for all Ndim) [m/s]. Aliased
            to `vlos` when `Ndim==1`
        '''
        return self.structure.vz

    @property
    def vlos(self) -> np.ndarray:
        '''
        vz : np.ndarray
            z component of plasma velocity (present for all Ndim) [m/s]. Only
            available when Ndim==1`.
        '''
        return self.structure.vlos

    @property
    def cmass(self) -> np.ndarray:
        '''
        cmass : np.ndarray
            Column mass [kg m-2].
        '''
        return self.structure.cmass

    @property
    def tauRef(self) -> np.ndarray:
        '''
        tauRef : np.ndarray
            Reference optical depth at 500 nm.
        '''
        return self.structure.tauRef

    @property
    def height(self) -> np.ndarray:
        return self.structure.z

    @property
    def x(self) -> np.ndarray:
        '''
        x : np.ndarray
            Ordinates of grid points along the x-axis (present for Ndim >= 2) [m].
        '''
        return self.structure.x

    @property
    def y(self) -> np.ndarray:
        '''
        y : np.ndarray
            Ordinates of grid points along the y-axis (present for Ndim == 3) [m].
        '''
        return self.structure.y

    @property
    def z(self) -> np.ndarray:
        '''
        z : np.ndarray
            Ordinates of grid points along the z-axis (present for all Ndim) [m].
        '''
        return self.structure.z

    @property
    def zLowerBc(self) -> BoundaryCondition:
        '''
        zLowerBc : BoundaryCondition
            Boundary condition for the plane of minimal z-coordinate.
        '''
        return self.structure.zLowerBc

    @property
    def zUpperBc(self) -> BoundaryCondition:
        '''
        zUpperBc : BoundaryCondition
            Boundary condition for the plane of maximal z-coordinate.
        '''
        return self.structure.zUpperBc

    @property
    def yLowerBc(self) -> BoundaryCondition:
        '''
        yLowerBc : BoundaryCondition
            Boundary condition for the plane of minimal y-coordinate.
        '''
        return self.structure.yLowerBc

    @property
    def yUpperBc(self) -> BoundaryCondition:
        '''
        yUpperBc : BoundaryCondition
            Boundary condition for the plane of maximal y-coordinate.
        '''
        return self.structure.yUpperBc

    @property
    def xLowerBc(self) -> BoundaryCondition:
        '''
        xLowerBc : BoundaryCondition
            Boundary condition for the plane of minimal x-coordinate.
        '''
        return self.structure.xLowerBc

    @property
    def xUpperBc(self) -> BoundaryCondition:
        '''
        xUpperBc : BoundaryCondition
            Boundary condition for the plane of maximal x-coordinate.
        '''
        return self.structure.xUpperBc

    @property
    def Nspace(self):
        '''
        Nspace : int
            Total number of points in the atmospheric spatial discretistaion.
        '''
        return self.structure.Nspace

    @property
    def Nrays(self):
        '''
        Nrays : int
            Number of rays in angular discretisation used.
        '''
        if self.muz is None:
            raise AttributeError('Nrays not set, call atmos.rays or .quadrature first')

        return self.muz.shape[0]

    def dimensioned_view(self):
        '''
        Returns a view over the contents of Layout reshaped so all data has
        the correct (1/2/3D) dimensionality for the atmospheric model, as
        these are all stored under a flat scheme.
        '''
        shape = self.structure.dimensioned_shape
        atmos = copy(self)
        atmos.structure = self.structure.dimensioned_view()
        atmos.temperature = self.temperature.reshape(shape)
        atmos.vturb = self.vturb.reshape(shape)
        atmos.ne = self.ne.reshape(shape)
        atmos.nHTot = self.nHTot.reshape(shape)
        if self.B is not None:
            atmos.B = self.B.reshape(shape)
            atmos.chiB = self.chiB.reshape(shape)
            atmos.gammaB = self.gammaB.reshape(shape)
        return atmos

    def unit_view(self):
        '''
        Returns a view over the contents of the Layout with the correct
        `astropy.units`.
        '''
        atmos = copy(self)
        atmos.structure = self.structure.unit_view()
        atmos.temperature = self.temperature << u.K
        atmos.vturb = self.vturb << u.m / u.s
        atmos.ne = self.ne << u.m**(-3)
        atmos.nHTot = self.nHTot << u.m**(-3)
        if self.B is not None:
            atmos.B = self.B << u.T
            atmos.chiB = self.chiB << u.rad
            atmos.gammaB = self.gammaB << u.rad
        return atmos

    def dimensioned_unit_view(self):
        '''
        Returns a view over the contents of Layout reshaped so all data has
        the correct (1/2/3D) dimensionality for the atmospheric model, and
        the correct `astropy.units`.
        '''
        atmos = self.dimensioned_view()
        return atmos.unit_view()

    @classmethod
    def make_1d(cls, scale: ScaleType, depthScale: np.ndarray,
                temperature: np.ndarray, vlos: np.ndarray,
                vturb: np.ndarray, ne: Optional[np.ndarray]=None,
                hydrogenPops: Optional[np.ndarray]=None,
                nHTot: Optional[np.ndarray]=None,
                B: Optional[np.ndarray]=None,
                gammaB: Optional[np.ndarray]=None,
                chiB: Optional[np.ndarray]=None,
                lowerBc: Optional[BoundaryCondition]=None,
                upperBc: Optional[BoundaryCondition]=None,
                convertScales: bool=True,
                abundance: Optional[AtomicAbundance]=None,
                logG: float=2.44,
                Pgas: Optional[np.ndarray]=None,
                Pe: Optional[np.ndarray]=None,
                Ptop: Optional[float]=None,
                PeTop: Optional[float]=None,
                verbose: bool=False):
        '''
        Constructor for 1D Atmosphere objects. Optionally will use an
        equation of state (EOS) to estimate missing parameters.

        If sufficient information is provided (i.e. all required parameters
        and ne and (hydrogenPops or nHTot)) then the EOS is not invoked to
        estimate any thermodynamic properties. If both of nHTot and
        hydrogenPops are omitted, then the electron pressure will be used
        with the Wittmann equation of state to estimate the mass density, and
        the hydrogen number density will be inferred from this and the
        abundances. If, instead, ne is omitted, then the mass density will be
        used with the Wittmann EOS to estimate the electron pressure.
        If both of these are omitted then the EOS will be used to estimate
        both. If:

            - Pgas is provided, then this gas pressure will define the
              atmospheric stratification and will be used with the EOS.

            - Pe is provided, then this electron pressure will define the
              atmospheric stratification and will be used with the EOS.

            - Ptop is provided, then this gas pressure at the top of the
              atmosphere will be used with the log gravitational acceleration
              logG, and the EOS to estimate the missing parameters assuming
              hydrostatic equilibrium.

            - PeTop is provided, then this electron pressure at the top of
              the atmosphere will be used with the log gravitational
              acceleration logG, and the EOS to estimate the missing parameters
              assuming hydrostatic equilibrium.

            - If all of Pgas, Pe, Ptop, PeTop are omitted then Ptop will be
              estimated from the gas pressure in the FALC model at the
              temperature at the top boundary. The hydrostatic reconstruction
              will then continue as usual.

        convertScales will substantially slow down this function due to the
        slow calculation of background opacities used to compute tauRef. If
        an atmosphere is constructed with a Geometric stratification, and an
        estimate of tauRef is not required before running the main RT module,
        then this can be set to False.
        All of these parameters can be provided as astropy Quantities, and
        will be converted in the constructor.

        Parameters
        ----------
        scale : ScaleType
            The type of stratification used along the z-axis.
        depthScale : np.ndarray
            The z-coordinates used along the chosen stratification. The
            stratification is expected to start at the top of the atmosphere
            (closest to the observer), and descend along the observer's line
            of sight.
        temperature : np.ndarray
            Temperature structure of the atmosphere [K].
        vlos : np.ndarray
            Velocity structure of the atmosphere along z [m/s].
        vturb : np.ndarray
            Microturbulent velocity structure of the atmosphere [m/s].
        ne : np.ndarray
            Electron density structure of the atmosphere [m-3].
        hydrogenPops : np.ndarray, optional
            Detailed (per level) hydrogen number density structure of the
            atmosphere [m-3], 2D array [Nlevel, Nspace].
        nHTot : np.ndarray, optional
            Total hydrogen number density structure of the atmosphere [m-3]
        B : np.ndarray, optional.
            Magnetic field strength [T].
        gammaB : np.ndarray, optional
            Co-altitude of magnetic field vector [radians].
        chiB : np.ndarray, optional
            Azimuth of magnetic field vector (in x-y plane, from x) [radians].
        lowerBc : BoundaryCondition, optional
            Boundary condition for incoming radiation at the minimal z
            coordinate (default: ThermalisedRadiation).
        upperBc : BoundaryCondition, optional
            Boundary condition for incoming radiation at the maximal z
            coordinate (default: ZeroRadiation).
        convertScales : bool, optional
            Whether to automatically compute tauRef and cmass for an
            atmosphere given in a stratification of m (default: True).
        abundance: AtomicAbundance, optional
            An instance of AtomicAbundance giving the abundances of each
            atomic species in the given atmosphere, only used if the EOS is
            invoked. (default: DefaultAtomicAbundance)
        logG: float, optional
            The log10 of the magnitude of gravitational acceleration [m/s2]
            (default: 2.44).
        Pgas: np.ndarray, optional
            The gas pressure stratification of the atmosphere [Pa],
            optionally used by the EOS.
        Pe: np.ndarray, optional
            The electron pressure stratification of the atmosphere [Pa],
            optionally used by the EOS.
        Ptop: np.ndarray, optional
            The gas pressure at the top of the atmosphere [Pa], optionally
            used by the EOS for a hydrostatic reconstruction.
        Petop: np.ndarray, optional
            The electron pressure at the top of the atmosphere [Pa],
            optionally used by the EOS for a hydrostatic reconstruction.
        verbose: bool, optional
            Explain decisions made with the EOS to estimate missing
            parameters (if invoked) through print calls (default: False).

        Raises
        ------
        ValueError
            if incorrect arguments or unable to construct estimate missing
            parameters.
        '''
        if scale == ScaleType.Geometric:
            depthScale = (depthScale << u.m).value
        elif scale == ScaleType.ColumnMass:
            depthScale = (depthScale << u.kg / u.m**2).value
        temperature = (temperature << u.K).value
        vlos = (vlos << u.m / u.s).value
        vturb = (vturb << u.m / u.s).value
        if ne is not None:
            ne = (ne << u.m**(-3)).value
        if hydrogenPops is not None:
            hydrogenPops = (hydrogenPops << u.m**(-3)).value
        if nHTot is not None:
            nHTot = (nHTot << u.m**(-3)).value
        if B is not None:
            B = (B << u.T).value
        if gammaB is not None:
            gammaB = (gammaB << u.rad).value
        if chiB is not None:
            chiB = (chiB << u.rad).value

        if lowerBc is None:
            lowerBc = ThermalisedRadiation()
        elif isinstance(lowerBc, PeriodicRadiation):
            raise ValueError('Cannot set periodic boundary conditions for 1D atmosphere')
        if upperBc is None:
            upperBc = ZeroRadiation()
        elif isinstance(upperBc, PeriodicRadiation):
            raise ValueError('Cannot set periodic boundary conditions for 1D atmosphere')

        if scale != ScaleType.Geometric and not convertScales:
            raise ValueError('Height scale must be provided if scale conversion is not applied')

        if nHTot is None and hydrogenPops is not None:
            nHTot = np.sum(hydrogenPops, axis=0)

        if np.any(temperature < 2000):
            # NOTE(cmo): Minimum value was decreased in NICOLE so should be safe
            raise ValueError('Minimum temperature too low for EOS (< 2000 K)')

        if abundance is None:
            abundance = DefaultAtomicAbundance

        wittAbundances = np.array([abundance[e] for e in PeriodicTable.elements])
        eos = witt(abund_init=wittAbundances)

        Nspace = depthScale.shape[0]
        if nHTot is None and ne is not None:
            if verbose:
                print('Setting nHTot from electron pressure.')
            pe = ne * Const.CM_TO_M**3 * eos.BK * temperature
            rho = np.zeros(Nspace)
            for k in range(Nspace):
                rho[k] = eos.rho_from_pe(temperature[k], pe[k])
            nHTot = np.copy(rho / (Const.CM_TO_M**3 / Const.G_TO_KG) / (Const.Amu * abundance.massPerH))
        elif ne is None and nHTot is not None:
            if verbose:
                print('Setting ne from mass density.')
            rho = Const.Amu * abundance.massPerH * nHTot * Const.CM_TO_M**3 / Const.G_TO_KG
            pe = np.zeros(Nspace)
            for k in range(Nspace):
                pe[k] = eos.pe_from_rho(temperature[k], rho[k])
            ne = np.copy(pe / (eos.BK * temperature) / Const.CM_TO_M**3)
        elif ne is None and nHTot is None:
            if Pgas is not None and Pgas.shape[0] != Nspace:
                raise ValueError('Dimensions of Pgas do not match atmospheric depth')
            if Pe is not None and Pe.shape[0] != Nspace:
                raise ValueError('Dimensions of Pe do not match atmospheric depth')

            if Pgas is not None and Pe is None:
                if verbose:
                    print('Setting ne, nHTot from provided gas pressure.')
                # Convert to cgs for eos
                pgas = Pgas * (Const.CM_TO_M**2 / Const.G_TO_KG)
                pe = np.zeros(Nspace)
                for k in range(Nspace):
                    pe[k] = eos.pe_from_pg(temperature[k], pgas[k])
            elif Pe is not None and Pgas is None:
                if verbose:
                    print('Setting ne, nHTot from provided electron pressure.')
                # Convert to cgs for eos
                pe = Pe * (Const.CM_TO_M**2 / Const.G_TO_KG)
                pgas = np.zeros(Nspace)
                for k in range(Nspace):
                    pgas[k] = eos.pg_from_pe(temperature[k], pe[k])
            elif Pgas is None and Pe is None:
                # Doing Hydrostatic Eq. based here on NICOLE implementation
                gravAcc = 10**logG / Const.CM_TO_M
                Avog = 6.022045e23 # Avogadro's Number
                if Ptop is None and PeTop is not None:
                    if verbose:
                        print('Setting ne, nHTot to hydrostatic equilibrium (logG=%f) from provided top electron pressure.' % logG)
                    PeTop *= (Const.CM_TO_M**2 / Const.G_TO_KG)
                    Ptop = eos.pg_from_pe(temperature[0], PeTop)
                elif Ptop is not None and PeTop is None:
                    if verbose:
                        print('Setting ne, nHTot to hydrostatic equilibrium (logG=%f) from provided top gas pressure.' % logG)
                    Ptop *= (Const.CM_TO_M**2 / Const.G_TO_KG)
                    PeTop = eos.pe_from_pg(temperature[0], Ptop)
                elif Ptop is None and PeTop is None:
                    if verbose:
                        print('Setting ne, nHTot to hydrostatic equilibrium (logG=%f) from FALC gas pressure at upper boundary temperature.' % logG)
                    Ptop = get_top_pressure(eos, temperature[0])
                    PeTop = eos.pe_from_pg(temperature[0], Ptop)
                else:
                    raise ValueError("Cannot set both Ptop and PeTop")

                if scale == ScaleType.Tau500:
                    tau = depthScale
                elif scale == ScaleType.Geometric:
                    height = depthScale / Const.CM_TO_M
                else:
                    cmass = depthScale / Const.G_TO_KG * Const.CM_TO_M**2

                # NOTE(cmo): Compute HSE following the NICOLE method.
                rho = np.zeros(Nspace)
                chi_c = np.zeros(Nspace)
                pgas = np.zeros(Nspace)
                pe = np.zeros(Nspace)
                pgas[0] = Ptop
                pe[0] = PeTop
                chi_c[0] = eos.contOpacity(temperature[0], pgas[0], pe[0], np.array([5000.0]))
                avg_mol_weight = lambda k: abundance.massPerH / (abundance.totalAbundance + pe[k] / pgas[k])
                rho[0] = Ptop * avg_mol_weight(0) / Avog / eos.BK / temperature[0]
                chi_c[0] /= rho[0]

                for k in range(1, Nspace):
                    chi_c[k] = chi_c[k-1]
                    rho[k] = rho[k-1]
                    for it in range(200):
                        if scale == ScaleType.Tau500:
                            dtau = tau[k] - tau[k-1]
                            pgas[k] = pgas[k-1] + gravAcc * dtau / (0.5 * (chi_c[k-1] + chi_c[k]))
                        elif scale == ScaleType.Geometric:
                            pgas[k] = pgas[k-1] * np.exp(-gravAcc / Avog / eos.BK * avg_mol_weight(k-1) * 0.5 * (1.0 / temperature[k-1] + 1.0 / temperature[k]) * (height[k] - height[k-1]))
                        else:
                            pgas[k] = gravAcc * cmass[k]

                        pe[k] = eos.pe_from_pg(temperature[k], pgas[k])
                        prevChi = chi_c[k]
                        chi_c[k] = eos.contOpacity(temperature[k], pgas[k], pe[k], np.array([5000.0]))
                        rho[k] = pgas[k] * avg_mol_weight(k) / Avog / eos.BK / temperature[k]
                        chi_c[k] /= rho[k]

                        change = np.abs(prevChi - chi_c[k]) / (prevChi + chi_c[k])
                        if change < 1e-5:
                            break
                    else:
                        raise ConvergenceError('No convergence in HSE at depth point %d, last change %2.4e' % (k, change))
            nHTot = np.copy(rho / (Const.CM_TO_M**3 / Const.G_TO_KG) / (Const.Amu * abundance.massPerH))
            ne = np.copy(pe / (eos.BK * temperature) / Const.CM_TO_M**3)

        # NOTE(cmo): Compute final pgas, pe from EOS that will be used for
        # background opacity.
        rhoSI = Const.Amu * abundance.massPerH * nHTot
        rho = Const.Amu * abundance.massPerH * nHTot * Const.CM_TO_M**3 / Const.G_TO_KG
        pgas = np.zeros_like(depthScale)
        pe = np.zeros_like(depthScale)
        for k in range(Nspace):
            pgas[k] = eos.pg_from_rho(temperature[k], rho[k])
            pe[k] = eos.pe_from_rho(temperature[k], rho[k])

        chi_c = np.zeros_like(depthScale)
        for k in range(depthScale.shape[0]):
            chi_c[k] = eos.contOpacity(temperature[k], pgas[k], pe[k], np.array([5000.0])) / Const.CM_TO_M

        # NOTE(cmo): We should now have a uniform minimum set of data (other
        # than the scale type), allowing us to simply convert between the
        # scales we do have!
        if convertScales:
            if scale == ScaleType.ColumnMass:
                height = np.zeros_like(depthScale)
                tau_ref = np.zeros_like(depthScale)
                cmass = depthScale

                height[0] = 0.0
                tau_ref[0] = chi_c[0] / rhoSI[0] * cmass[0]
                for k in range(1, cmass.shape[0]):
                    height[k] = height[k-1] - 2.0 * (cmass[k] - cmass[k-1]) / (rhoSI[k-1] + rhoSI[k])
                    tau_ref[k] = tau_ref[k-1] + 0.5 * (chi_c[k-1] + chi_c[k]) * (height[k-1] - height[k])

                hTau1 = np.interp(1.0, tau_ref, height)
                height -= hTau1
            elif scale == ScaleType.Geometric:
                cmass = np.zeros(Nspace)
                tau_ref = np.zeros(Nspace)
                height = depthScale

                cmass[0] = (nHTot[0] * abundance.massPerH + ne[0]) * (Const.KBoltzmann * temperature[0] / 10**logG)
                tau_ref[0] = 0.5 * chi_c[0] * (height[0] - height[1])
                if tau_ref[0] > 1.0:
                    tau_ref[0] = 0.0

                for k in range(1, Nspace):
                    cmass[k] = cmass[k-1] + 0.5 * (rhoSI[k-1] + rhoSI[k]) * (height[k-1] - height[k])
                    tau_ref[k] = tau_ref[k-1] + 0.5 * (chi_c[k-1] + chi_c[k]) * (height[k-1] - height[k])
            elif scale == ScaleType.Tau500:
                cmass = np.zeros(Nspace)
                height = np.zeros(Nspace)
                tau_ref = depthScale

                cmass[0] = (tau_ref[0] / chi_c[0]) * rhoSI[0]
                for k in range(1, Nspace):
                    height[k] = height[k-1] - 2.0 * (tau_ref[k] - tau_ref[k-1]) / (chi_c[k-1] + chi_c[k])
                    cmass[k] = cmass[k-1] + 0.5 * (chi_c[k-1] + chi_c[k]) * (height[k-1] - height[k])

                hTau1 = np.interp(1.0, tau_ref, height)
                height -= hTau1
            else:
                raise ValueError("Other scales not handled yet")

            stratifications: Optional[Stratifications] = Stratifications(cmass=cmass, tauRef=tau_ref)

        else:
            stratifications = None

        layout = Layout.make_1d(z=height, vz=vlos,
                                lowerBc=lowerBc, upperBc=upperBc,
                                stratifications=stratifications)
        atmos = cls(structure=layout, temperature=temperature, vturb=vturb,
                    ne=ne, nHTot=nHTot, B=B, gammaB=gammaB, chiB=chiB)

        return atmos

    @classmethod
    def make_2d(cls, height: np.ndarray, x: np.ndarray,
                temperature: np.ndarray, vx: np.ndarray,
                vz: np.ndarray, vturb: np.ndarray,
                ne: Optional[np.ndarray]=None,
                nHTot: Optional[np.ndarray]=None,
                B: Optional[np.ndarray]=None,
                gammaB: Optional[np.ndarray]=None,
                chiB: Optional[np.ndarray]=None,
                xUpperBc: Optional[BoundaryCondition]=None,
                xLowerBc: Optional[BoundaryCondition]=None,
                zUpperBc: Optional[BoundaryCondition]=None,
                zLowerBc: Optional[BoundaryCondition]=None,
                abundance: Optional[AtomicAbundance]=None,
                verbose=False):
        '''
        Constructor for 2D Atmosphere objects.

        No provision for estimating parameters using hydrostatic equilibrium
        is provided, but one of ne, or nHTot can be omitted and inferred by
        use of the Wittmann equation of state.
        The atmosphere must be defined on a geometric stratification.
        All atmospheric parameters are expected in a 2D [z, x] array.

        Parameters
        ----------
        height : np.ndarray
            The z-coordinates of the atmospheric grid. The stratification is
            expected to start at the top of the atmosphere (closest to the
            observer), and descend along the observer's line of sight.
        x : np.ndarray
            The (horizontal) x-coordinates of the atmospheric grid.
        temperature : np.ndarray
            Temperature structure of the atmosphere [K].
        vx : np.ndarray
            x-component of the atmospheric velocity [m/s].
        vz : np.ndarray
            z-component of the atmospheric velocity [m/s].
        vturb : np.ndarray
            Microturbulent velocity structure [m/s].
        ne : np.ndarray
            Electron density structure of the atmosphere [m-3].
        nHTot : np.ndarray, optional
            Total hydrogen number density structure of the atmosphere [m-3].
        B : np.ndarray, optional.
            Magnetic field strength [T].
        gammaB : np.ndarray, optional
            Co-altitude of magnetic field vector [radians].
        chiB : np.ndarray, optional
            Azimuth of magnetic field vector (in x-y plane, from x) [radians].
        xLowerBc : BoundaryCondition, optional
            Boundary condition for incoming radiation at the minimal x
            coordinate (default: PeriodicRadiation).
        xUpperBc : BoundaryCondition, optional
            Boundary condition for incoming radiation at the maximal x
            coordinate (default: PeriodicRadiation).
        zLowerBc : BoundaryCondition, optional
            Boundary condition for incoming radiation at the minimal z
            coordinate (default: ThermalisedRadiation).
        zUpperBc : BoundaryCondition, optional
            Boundary condition for incoming radiation at the maximal z
            coordinate (default: ZeroRadiation).
        convertScales : bool, optional
            Whether to automatically compute tauRef and cmass for an
            atmosphere given in a stratification of m (default: True).
        abundance: AtomicAbundance, optional
            An instance of AtomicAbundance giving the abundances of each
            atomic species in the given atmosphere, only used if the EOS is
            invoked. (default: DefaultAtomicAbundance)
        verbose: bool, optional
            Explain decisions made with the EOS to estimate missing
            parameters (if invoked) through print calls (default: False).

        Raises
        ------
        ValueError
            if incorrect arguments or unable to construct estimate missing
            parameters.
        '''

        x = (x << u.m).value
        height = (height << u.m).value
        temperature = (temperature << u.K).value
        vx = (vx << u.m / u.s).value
        vz = (vz << u.m / u.s).value
        vturb = (vturb << u.m / u.s).value
        if ne is not None:
            ne = (ne << u.m**(-3)).value
        if nHTot is not None:
            nHTot = (nHTot << u.m**(-3)).value
        if B is not None:
            B = (B << u.T).value
            flatB = view_flatten(B)
        else:
            flatB = None

        if gammaB is not None:
            gammaB = (gammaB << u.rad).value
            flatGammaB = view_flatten(gammaB)
        else:
            flatGammaB = None

        if chiB is not None:
            chiB = (chiB << u.rad).value
            flatChiB = view_flatten(chiB)
        else:
            flatChiB = None

        if zLowerBc is None:
            zLowerBc = ThermalisedRadiation()
        elif isinstance(zLowerBc, PeriodicRadiation):
            raise ValueError('Cannot set periodic boundary conditions for z-axis.')
        if zUpperBc is None:
            zUpperBc = ZeroRadiation()
        elif isinstance(zUpperBc, PeriodicRadiation):
            raise ValueError('Cannot set periodic boundary conditions for z-axis.')
        if xUpperBc is None:
            xUpperBc = PeriodicRadiation()
        if xLowerBc is None:
            xLowerBc = PeriodicRadiation()
        if abundance is None:

            abundance = DefaultAtomicAbundance

        wittAbundances = np.array([abundance[e] for e in PeriodicTable.elements])
        eos = witt(abund_init=wittAbundances)

        flatHeight = view_flatten(height)
        flatTemperature = view_flatten(temperature)
        Nspace = flatHeight.shape[0]
        if nHTot is None and ne is not None:
            if verbose:
                print('Setting nHTot from electron pressure.')
            flatNe = view_flatten(ne)
            pe = flatNe * Const.CM_TO_M**3 * eos.BK * flatTemperature
            rho = np.zeros(Nspace)
            for k in range(Nspace):
                rho[k] = eos.rho_from_pe(flatTemperature[k], pe[k])
            nHTot = np.copy(rho / (Const.CM_TO_M**3 / Const.G_TO_KG) / (Const.Amu * abundance.massPerH))
        elif ne is None and nHTot is not None:
            if verbose:
                print('Setting ne from mass density.')
            flatNHTot = view_flatten(nHTot)
            rho = Const.Amu * abundance.massPerH * flatNHTot * Const.CM_TO_M**3 / Const.G_TO_KG
            pe = np.zeros(Nspace)
            for k in range(Nspace):
                pe[k] = eos.pe_from_rho(flatTemperature[k], rho[k])
            ne = np.copy(pe / (eos.BK * flatTemperature) / Const.CM_TO_M**3)
        elif ne is None and nHTot is None:
            raise ValueError('Cannot omit both ne and nHTot (currently).')
        flatX = view_flatten(x)
        flatNHTot = view_flatten(nHTot)
        flatNe = view_flatten(ne)
        flatVx = view_flatten(vx)
        flatVz = view_flatten(vz)
        flatVturb = view_flatten(vturb)

        layout = Layout.make_2d(x=flatX, z=flatHeight, vx=flatVx, vz=flatVz,
                                xLowerBc=xLowerBc, xUpperBc=xUpperBc,
                                zLowerBc=zLowerBc, zUpperBc=zUpperBc,
                                stratifications=None)

        atmos = cls(structure=layout, temperature=flatTemperature,
                    vturb=flatVturb, ne=flatNe, nHTot=flatNHTot, B=flatB,
                    gammaB=flatGammaB, chiB=flatChiB)
        return atmos

    def quadrature(self, Nrays: Optional[int]=None,
                   mu: Optional[Sequence[float]]=None,
                   wmu: Optional[Sequence[float]]=None):
        '''
        Compute the angular quadrature for solving the RTE and Kinetic
        Equilibrium in a given atmosphere.

        Procedure varies with dimensionality.

        By convention muz is always positive, as the direction on this axis
        is determined by the toObs term that is used internally to the formal
        solver.

        1D:
            If a number of rays is given (typically 3 or 5), then the
            Gauss-Legendre quadrature for this set is used.
            If mu and wmu are instead given then these will be validated and
            used.

        2+D:
            If the number of rays selected is in the list of near optimal
            quadratures for unpolarised radiation provided by Stepan et al
            2020 (A&A, 646 A24), then this is used. Otherwise an exception is
            raised.

            The available quadratures are:

            +--------+-------+
            | Points | Order |
            +========+=======+
            |   1    |  3    |
            +--------+-------+
            |   3    |  7    |
            +--------+-------+
            |   6    |  9    |
            +--------+-------+
            |   7    |  11   |
            +--------+-------+
            |   10   |  13   |
            +--------+-------+
            |   11   |  15   |
            +--------+-------+

        Parameters
        ----------
        Nrays : int, optional
            The number of rays to use in the quadrature. See notes above.
        mu : sequence of float, optional
            The cosine of the angle made between the between each of the set
            of rays and the z axis, only used in 1D.
        wmu : sequence of float, optional
            The integration weights for each mu, must be provided if mu is provided.

        Raises
        ------
        ValueError
            on incorrect input.
        '''

        if self.Ndim == 1:
            if Nrays is not None and mu is None:
                if Nrays >= 1:
                    x, w = leggauss(Nrays)
                    mid, halfWidth = 0.5, 0.5
                    x = mid + halfWidth * x
                    w *= halfWidth

                    self.muz = x
                    self.wmu = w
                else:
                    raise ValueError('Unsupported Nrays=%d' % Nrays)
            elif Nrays is not None and mu is not None:
                if wmu is None:
                    raise ValueError('Must provide wmu when providing mu')
                if Nrays != len(mu):
                    raise ValueError('mu must be Nrays long if Nrays is provided')
                if len(mu) != len(wmu):
                    raise ValueError('mu and wmu must be the same shape')

                self.muz = np.array(mu)
                self.wmu = np.array(wmu)

            self.muy = np.zeros_like(self.muz)
            self.mux = np.sqrt(1.0 - self.muz**2)
        else:
            with open(get_data_path() + 'Quadratures.pickle', 'rb') as pkl:
                quads = pickle.load(pkl)

            rays = {int(q.split('n')[1]): q for q in quads}
            if Nrays not in rays:
                raise ValueError('For multidimensional cases Nrays must be in %s' % repr(rays))

            quad = quads[rays[Nrays]]

            if self.Ndim == 2:
                Nrays *= 2
                theta = np.deg2rad(quad[:, 1])
                chi = np.deg2rad(quad[:, 2])
                # polar coords:
                # x = sin theta cos chi
                # y = sin theta sin chi
                # z = cos theta
                self.mux = np.zeros(Nrays)
                self.mux[:Nrays // 2] = np.sin(theta) * np.cos(chi)
                self.mux[Nrays // 2:] = -np.sin(theta) * np.cos(chi)
                self.muz = np.zeros(Nrays)
                self.muz[:Nrays // 2] = np.cos(theta)
                self.muz[Nrays // 2:] = np.cos(theta)
                self.wmu = np.zeros(Nrays)
                self.wmu[:Nrays // 2] = quad[:, 0]
                self.wmu[Nrays // 2:] = quad[:, 0]
                self.wmu /= np.sum(self.wmu)
                self.muy = np.sqrt(1.0 - (self.mux**2 + self.muz**2))

            else:
                raise NotImplementedError()

        self.configure_bcs()

    def rays(self, muz: Union[float, Sequence[float]],
             mux: Optional[Union[float, Sequence[float]]]=None,
             muy: Optional[Union[float, Sequence[float]]]=None,
             wmu: Optional[Union[float, Sequence[float]]]=None):
        '''
        Set up the rays on the Atmosphere for computing the intensity in a
        particular direction (or set of directions).

        If only the z angle is set then the ray is assumed in the x-z plane.
        If either muz or muy is omitted then this angle is inferred by
        normalisation of the projection.

        By convention muz is always positive, as the direction on this axis
        is determined by the toObs term that is used internally to the formal
        solver.

        Parameters
        ----------
        muz : float or sequence of float, optional
            The angular projections along the z axis.
        mux : float or sequence of float, optional
            The angular projections along the x axis.
        muy : float or sequence of float, optional
            The angular projections along the y axis.
        wmu : float or sequence of float, optional
            The integration weights for the given ray if J is to be
            integrated for angle set.

        Raises
        ------
        ValueError
            if the angular projections or integration weights are incorrectly
            normalised.
        '''

        if isinstance(muz, float):
            muz = [muz]
        if isinstance(mux, float):
            mux = [mux]
        if isinstance(muy, float):
            muy = [muy]

        if mux is None and muy is None:
            self.muz = np.array(muz)
            self.wmu = np.zeros_like(self.muz)
            self.muy = np.zeros_like(self.muz)
            self.mux = np.sqrt(1.0 - self.muz**2)
        elif muy is None:
            self.muz = np.array(muz)
            self.wmu = np.zeros_like(self.muz)
            self.mux = np.array(mux)
            self.muy = np.sqrt(1.0 - (self.muz**2 + self.mux**2))
        elif mux is None:
            self.muz = np.array(muz)
            self.wmu = np.zeros_like(self.muz)
            self.muy = np.array(muy)
            self.mux = np.sqrt(1.0 - (self.muz**2 + self.muy**2))
        else:
            self.muz = np.array(muz)
            self.mux = np.array(mux)
            self.muy = np.array(muy)
            self.wmu = np.zeros_like(muz)

            if not np.allclose(self.muz**2 + self.mux**2 + self.muy**2, 1):
                raise ValueError('mux**2 + muy**2 + muz**2 != 1.0')

        if not np.all(self.muz > 0):
            raise ValueError('muz must be > 0')

        if wmu is not None:
            self.wmu = np.array(wmu)

            if not np.isclose(self.wmu.sum(), 1.0):
                raise ValueError('sum of wmus is not 1.0')

        self.configure_bcs()

    def configure_bcs(self):
        '''
        Configure the required angular information for all boundary
        conditions on the model.
        '''

        # NOTE(cmo): We always have z-bcs
        # For zLowerBc, muz is positive, and we have all mux, muz
        mux, muy, muz = self.mux, self.muy, self.muz
        # NOTE(cmo): indexVector is of shape (mu, toObs) to allow the core to
        # easily destructure the blob that will be handed to it from
        # compute_bc.
        indexVector = np.ones((self.mux.shape[0], 2), dtype=np.int32) * -1
        indexVector[:, 1] = np.arange(mux.shape[0])
        self.zLowerBc.set_required_angles(mux, muy, muz, indexVector)

        indexVector = np.ones((mux.shape[0], 2), dtype=np.int32) * -1
        indexVector[:, 0] = np.arange(mux.shape[0])
        self.zUpperBc.set_required_angles(-mux, -muy, -muz, indexVector)

        # NOTE(cmo): If 2+D we have x-bcs too
        # xLowerBc has all muz and all mux > 0
        mux, muy, muz = [], [], []
        indexVector = np.ones((self.mux.shape[0], 2), dtype=np.int32) * -1
        count = 0
        musDone = np.zeros(self.muz.shape[0], dtype=np.bool)
        for mu in range(self.muz.shape[0]):
            for equalMu in np.argwhere(np.abs(self.muz) == self.muz[mu]).reshape(-1)[::-1]:
                if musDone[equalMu]:
                    continue
                musDone[equalMu] = True

                for toObsI in range(2):
                    sign = [-1, 1][toObsI]
                    sMux = sign * self.mux[equalMu]
                    if sMux > 0:
                        mux.append(sMux)
                        muy.append(sign * self.muy[equalMu])
                        muz.append(sign * self.muz[equalMu])
                        indexVector[equalMu, toObsI] = count
                        count += 1
            if np.all(musDone):
                break

        mux = np.array(mux)
        muy = np.array(muy)
        muz = np.array(muz)
        self.xLowerBc.set_required_angles(mux, muy, muz, indexVector)

        mux, muy, muz = [], [], []
        indexVector = np.ones((self.mux.shape[0], 2), dtype=np.int32) * -1
        count = 0
        musDone = np.zeros(self.muz.shape[0], dtype=np.bool)
        for mu in range(self.muz.shape[0]):
            for equalMu in np.argwhere(np.abs(self.muz) == self.muz[mu]).reshape(-1):
                if musDone[equalMu]:
                    continue
                musDone[equalMu] = True

                for toObsI in range(2):
                    sign = [-1, 1][toObsI]
                    sMux = sign * self.mux[equalMu]
                    if sMux < 0:
                        mux.append(sMux)
                        muy.append(sign * self.muy[equalMu])
                        muz.append(sign * self.muz[equalMu])
                        indexVector[equalMu, toObsI] = count
                        count += 1
            if np.all(musDone):
                break

        mux = np.array(mux)
        muy = np.array(muy)
        muz = np.array(muz)
        self.xUpperBc.set_required_angles(mux, muy, muz, indexVector)

        self.yLowerBc.set_required_angles(np.zeros((0)), np.zeros((0)), np.zeros((0)),
                                          np.ones((self.mux.shape[0], 2), dtype=np.int32) * -1)
        self.yUpperBc.set_required_angles(np.zeros((0)), np.zeros((0)), np.zeros((0)),
                                          np.ones((self.mux.shape[0], 2), dtype=np.int32) * -1)

        if self.Ndim > 2:
            raise ValueError('Only <= 2D atmospheres supported currently.')


# Licensed under a 3-clause BSD style license - see LICENSE.rst

from warnings import warn
import collections
import socket
import json
import urllib.request
import urllib.error
import urllib.parse

import numpy as np
import erfa

from astropy import units as u
from astropy import constants as consts
from astropy.units.quantity import QuantityInfoBase
from astropy.utils.exceptions import AstropyUserWarning
from .angles import Angle, Longitude, Latitude
from .representation import CartesianRepresentation, CartesianDifferential
from .errors import UnknownSiteException
from astropy.utils import data

__all__ = ['EarthLocation']

GeodeticLocation = collections.namedtuple('GeodeticLocation', ['lon', 'lat', 'height'])

# Available ellipsoids (defined in erfam.h, with numbers exposed in erfa).
ELLIPSOIDS = ('WGS84', 'GRS80', 'WGS72')

OMEGA_EARTH = u.Quantity(7.292115855306589e-5, 1./u.s)
"""
Rotational velocity of Earth. In UT1 seconds, this would be 2 pi / (24 * 3600),
but we need the value in SI seconds.
See Explanatory Supplement to the Astronomical Almanac, ed. P. Kenneth Seidelmann (1992),
University Science Books.
"""

def _check_ellipsoid(ellipsoid=None, default='WGS84'):
    if ellipsoid is None:
        ellipsoid = default
    if ellipsoid not in ELLIPSOIDS:
        raise ValueError(f'Ellipsoid {ellipsoid} not among known ones ({ELLIPSOIDS})')
    return ellipsoid

def _get_json_result(url, err_str, use_google):

    # need to do this here to prevent a series of complicated circular imports
    from .name_resolve import NameResolveError
    try:
        # Retrieve JSON response from Google maps API
        resp = urllib.request.urlopen(url, timeout=data.conf.remote_timeout)
        resp_data = json.loads(resp.read().decode('utf8'))

    except urllib.error.URLError as e:
        # This catches a timeout error, see:
        #   http://stackoverflow.com/questions/2712524/handling-urllib2s-timeout-python
        if isinstance(e.reason, socket.timeout):
            raise NameResolveError(err_str.format(msg="connection timed out"))
        else:
            raise NameResolveError(err_str.format(msg=e.reason))

    except socket.timeout:
        # There are some cases where urllib2 does not catch socket.timeout
        # especially while receiving response data on an already previously
        # working request
        raise NameResolveError(err_str.format(msg="connection timed out"))

    if use_google:
        results = resp_data.get('results', [])

        if resp_data.get('status', None) != 'OK':
            raise NameResolveError(err_str.format(msg="unknown failure with "
                                                  "Google API"))

    else:  # OpenStreetMap returns a list
        results = resp_data

    if not results:
        raise NameResolveError(err_str.format(msg="no results returned"))

    return results

class EarthLocationInfo(QuantityInfoBase):
    """
    Container for meta information like name, description, format.  This is
    required when the object is used as a mixin column within a table, but can
    be used as a general way to store meta information.
    """
    _represent_as_dict_attrs = ('x', 'y', 'z', 'ellipsoid')

    def _construct_from_dict(self, map):
        # Need to pop ellipsoid off and update post-instantiation.  This is
        # on the to-fix list in #4261.
        ellipsoid = map.pop('ellipsoid')
        out = self._parent_cls(**map)
        out.ellipsoid = ellipsoid
        return out

    def new_like(self, cols, length, metadata_conflicts='warn', name=None):
        """
        Return a new EarthLocation instance which is consistent with the
        input ``cols`` and has ``length`` rows.

        This is intended for creating an empty column object whose elements can
        be set in-place for table operations like join or vstack.

        Parameters
        ----------
        cols : list
            List of input columns
        length : int
            Length of the output column object
        metadata_conflicts : str ('warn'|'error'|'silent')
            How to handle metadata conflicts
        name : str
            Output column name

        Returns
        -------
        col : EarthLocation (or subclass)
            Empty instance of this class consistent with ``cols``
        """
        # Very similar to QuantityInfo.new_like, but the creation of the
        # map is different enough that this needs its own rouinte.
        # Get merged info attributes shape, dtype, format, description.
        attrs = self.merge_cols_attributes(cols, metadata_conflicts, name,
                                           ('meta', 'format', 'description'))
        # The above raises an error if the dtypes do not match, but returns
        # just the string representation, which is not useful, so remove.
        attrs.pop('dtype')
        # Make empty EarthLocation using the dtype and unit of the last column.
        # Use zeros so we do not get problems for possible conversion to
        # geodetic coordinates.
        shape = (length,) + attrs.pop('shape')
        data = u.Quantity(np.zeros(shape=shape, dtype=cols[0].dtype),
                          unit=cols[0].unit, copy=False)
        # Get arguments needed to reconstruct class
        map = {key: (data[key] if key in 'xyz' else getattr(cols[-1], key))
               for key in self._represent_as_dict_attrs}
        out = self._construct_from_dict(map)
        # Set remaining info attributes
        for attr, value in attrs.items():
            setattr(out.info, attr, value)

        return out

class EarthLocation(u.Quantity):
    """
    Location on the Earth.

    Initialization is first attempted assuming geocentric (x, y, z) coordinates
    are given; if that fails, another attempt is made assuming geodetic
    coordinates (longitude, latitude, height above a reference ellipsoid).
    When using the geodetic forms, Longitudes are measured increasing to the
    east, so west longitudes are negative. Internally, the coordinates are
    stored as geocentric.

    To ensure a specific type of coordinates is used, use the corresponding
    class methods (`from_geocentric` and `from_geodetic`) or initialize the
    arguments with names (``x``, ``y``, ``z`` for geocentric; ``lon``, ``lat``,
    ``height`` for geodetic).  See the class methods for details.

    Notes
    -----
    This class fits into the coordinates transformation framework in that it
    encodes a position on the `~astropy.coordinates.ITRS` frame.  To get a
    proper `~astropy.coordinates.ITRS` object from this object, use the ``itrs``
    property.
    """

    _ellipsoid = 'WGS84'
    _location_dtype = np.dtype({'names': ['x', 'y', 'z'],
                                'formats': [np.float64]*3})
    _array_dtype = np.dtype((np.float64, (3,)))

    info = EarthLocationInfo()

    def __new__(cls, *args, **kwargs):
        # TODO: needs copy argument and better dealing with inputs.
        if (len(args) == 1 and len(kwargs) == 0 and
                isinstance(args[0], EarthLocation)):
            return args[0].copy()
        try:
            self = cls.from_geocentric(*args, **kwargs)
        except (u.UnitsError, TypeError) as exc_geocentric:
            try:
                self = cls.from_geodetic(*args, **kwargs)
            except Exception as exc_geodetic:
                raise TypeError('Coordinates could not be parsed as either '
                                'geocentric or geodetic, with respective '
                                'exceptions "{}" and "{}"'
                                .format(exc_geocentric, exc_geodetic))
        return self

    @classmethod
    def from_geocentric(cls, x, y, z, unit=None):
        """
        Location on Earth, initialized from geocentric coordinates.

        Parameters
        ----------
        x, y, z : `~astropy.units.Quantity` or array_like
            Cartesian coordinates.  If not quantities, ``unit`` should be given.
        unit : `~astropy.units.UnitBase` object or None
            Physical unit of the coordinate values.  If ``x``, ``y``, and/or
            ``z`` are quantities, they will be converted to this unit.

        Raises
        ------
        astropy.units.UnitsError
            If the units on ``x``, ``y``, and ``z`` do not match or an invalid
            unit is given.
        ValueError
            If the shapes of ``x``, ``y``, and ``z`` do not match.
        TypeError
            If ``x`` is not a `~astropy.units.Quantity` and no unit is given.
        """
        if unit is None:
            try:
                unit = x.unit
            except AttributeError:
                raise TypeError("Geocentric coordinates should be Quantities "
                                "unless an explicit unit is given.")
        else:
            unit = u.Unit(unit)

        if unit.physical_type != 'length':
            raise u.UnitsError("Geocentric coordinates should be in "
                               "units of length.")

        try:
            x = u.Quantity(x, unit, copy=False)
            y = u.Quantity(y, unit, copy=False)
            z = u.Quantity(z, unit, copy=False)
        except u.UnitsError:
            raise u.UnitsError("Geocentric coordinate units should all be "
                               "consistent.")

        x, y, z = np.broadcast_arrays(x, y, z)
        struc = np.empty(x.shape, cls._location_dtype)
        struc['x'], struc['y'], struc['z'] = x, y, z
        return super().__new__(cls, struc, unit, copy=False)

    @classmethod
    def from_geodetic(cls, lon, lat, height=0., ellipsoid=None):
        """
        Location on Earth, initialized from geodetic coordinates.

        Parameters
        ----------
        lon : `~astropy.coordinates.Longitude` or float
            Earth East longitude.  Can be anything that initialises an
            `~astropy.coordinates.Angle` object (if float, in degrees).
        lat : `~astropy.coordinates.Latitude` or float
            Earth latitude.  Can be anything that initialises an
            `~astropy.coordinates.Latitude` object (if float, in degrees).
        height : `~astropy.units.Quantity` or float, optional
            Height above reference ellipsoid (if float, in meters; default: 0).
        ellipsoid : str, optional
            Name of the reference ellipsoid to use (default: 'WGS84').
            Available ellipsoids are:  'WGS84', 'GRS80', 'WGS72'.

        Raises
        ------
        astropy.units.UnitsError
            If the units on ``lon`` and ``lat`` are inconsistent with angular
            ones, or that on ``height`` with a length.
        ValueError
            If ``lon``, ``lat``, and ``height`` do not have the same shape, or
            if ``ellipsoid`` is not recognized as among the ones implemented.

        Notes
        -----
        For the conversion to geocentric coordinates, the ERFA routine
        ``gd2gc`` is used.  See https://github.com/liberfa/erfa
        """
        ellipsoid = _check_ellipsoid(ellipsoid, default=cls._ellipsoid)
        # We use Angle here since there is no need to wrap the longitude -
        # gd2gc will just take cos/sin anyway.  And wrapping might fail
        # on readonly input.
        lon = Angle(lon, u.degree, copy=False)
        lat = Latitude(lat, u.degree, copy=False)
        # don't convert to m by default, so we can use the height unit below.
        if not isinstance(height, u.Quantity):
            height = u.Quantity(height, u.m, copy=False)
        # get geocentric coordinates. Have to give one-dimensional array.
        xyz = erfa.gd2gc(getattr(erfa, ellipsoid),
                         lon.to_value(u.radian),
                         lat.to_value(u.radian),
                         height.to_value(u.m))
        self = xyz.ravel().view(cls._location_dtype,
                                cls).reshape(xyz.shape[:-1])
        self._unit = u.meter
        self._ellipsoid = ellipsoid
        return self.to(height.unit)

    @classmethod
    def of_site(cls, site_name):
        """
        Return an object of this class for a known observatory/site by name.

        This is intended as a quick convenience function to get basic site
        information, not a fully-featured exhaustive registry of observatories
        and all their properties.

        Additional information about the site is stored in the ``.info.meta``
        dictionary of sites obtained using this method (see the examples below).

        .. note::
            When this function is called, it will attempt to download site
            information from the astropy data server. If you would like a site
            to be added, issue a pull request to the
            `astropy-data repository <https://github.com/astropy/astropy-data>`_ .
            If a site cannot be found in the registry (i.e., an internet
            connection is not available), it will fall back on a built-in list,
            In the future, this bundled list might include a version-controlled
            list of canonical observatories extracted from the online version,
            but it currently only contains the Greenwich Royal Observatory as an
            example case.

        Parameters
        ----------
        site_name : str
            Name of the observatory (case-insensitive).

        Returns
        -------
        site : This class (a `~astropy.coordinates.EarthLocation` or subclass)
            The location of the observatory.

        Examples
        --------

        >>> from astropy.coordinates import EarthLocation
        >>> keck = EarthLocation.of_site('Keck Observatory')  # doctest: +REMOTE_DATA
        >>> keck.geodetic  # doctest: +REMOTE_DATA +FLOAT_CMP
        GeodeticLocation(lon=<Longitude -155.47833333 deg>, lat=<Latitude 19.82833333 deg>, height=<Quantity 4160. m>)
        >>> keck.info  # doctest: +REMOTE_DATA
        name = W. M. Keck Observatory
        dtype = void192
        unit = m
        class = EarthLocation
        n_bad = 0
        >>> keck.info.meta  # doctest: +REMOTE_DATA
        {'source': 'IRAF Observatory Database', 'timezone': 'US/Aleutian'}

        See Also
        --------
        get_site_names : the list of sites that this function can access
        """  # noqa
        registry = cls._get_site_registry()
        try:
            el = registry[site_name]
        except UnknownSiteException as e:
            raise UnknownSiteException(e.site, 'EarthLocation.get_site_names',
                                       close_names=e.close_names)

        if cls is el.__class__:
            return el
        else:
            newel = cls.from_geodetic(*el.to_geodetic())
            newel.info.name = el.info.name
            return newel

    @classmethod
    def of_address(cls, address, get_height=False, google_api_key=None):
        """
        Return an object of this class for a given address by querying either
        the OpenStreetMap Nominatim tool [1]_ (default) or the Google geocoding
        API [2]_, which requires a specified API key.

        This is intended as a quick convenience function to get easy access to
        locations. If you need to specify a precise location, you should use the
        initializer directly and pass in a longitude, latitude, and elevation.

        In the background, this just issues a web query to either of
        the APIs noted above. This is not meant to be abused! Both
        OpenStreetMap and Google use IP-based query limiting and will ban your
        IP if you send more than a few thousand queries per hour [2]_.

        .. warning::
            If the query returns more than one location (e.g., searching on
            ``address='springfield'``), this function will use the **first**
            returned location.

        Parameters
        ----------
        address : str
            The address to get the location for. As per the Google maps API,
            this can be a fully specified street address (e.g., 123 Main St.,
            New York, NY) or a city name (e.g., Danbury, CT), or etc.
        get_height : bool, optional
            This only works when using the Google API! See the ``google_api_key``
            block below. Use the retrieved location to perform a second query to
            the Google maps elevation API to retrieve the height of the input
            address [3]_.
        google_api_key : str, optional
            A Google API key with the Geocoding API and (optionally) the
            elevation API enabled. See [4]_ for more information.

        Returns
        -------
        location : This class (a `~astropy.coordinates.EarthLocation` or subclass)
            The location of the input address.

        References
        ----------
        .. [1] https://nominatim.openstreetmap.org/
        .. [2] https://developers.google.com/maps/documentation/geocoding/start
        .. [3] https://developers.google.com/maps/documentation/elevation/start
        .. [4] https://developers.google.com/maps/documentation/geocoding/get-api-key

        """

        use_google = google_api_key is not None

        # Fail fast if invalid options are passed:
        if not use_google and get_height:
            raise ValueError(
                'Currently, `get_height` only works when using '
                'the Google geocoding API, which requires passing '
                'a Google API key with `google_api_key`. See: '
                'https://developers.google.com/maps/documentation/geocoding/get-api-key '
                'for information on obtaining an API key.')

        if use_google:  # Google
            pars = urllib.parse.urlencode({'address': address,
                                           'key': google_api_key})
            geo_url = f"https://maps.googleapis.com/maps/api/geocode/json?{pars}"

        else:  # OpenStreetMap
            pars = urllib.parse.urlencode({'q': address,
                                           'format': 'json'})
            geo_url = f"https://nominatim.openstreetmap.org/search?{pars}"

        # get longitude and latitude location
        err_str = f"Unable to retrieve coordinates for address '{address}'; {{msg}}"
        geo_result = _get_json_result(geo_url, err_str=err_str,
                                      use_google=use_google)

        if use_google:
            loc = geo_result[0]['geometry']['location']
            lat = loc['lat']
            lon = loc['lng']

        else:
            loc = geo_result[0]
            lat = float(loc['lat'])  # strings are returned by OpenStreetMap
            lon = float(loc['lon'])

        if get_height:
            pars = {'locations': f'{lat:.8f},{lon:.8f}',
                    'key': google_api_key}
            pars = urllib.parse.urlencode(pars)
            ele_url = f"https://maps.googleapis.com/maps/api/elevation/json?{pars}"

            err_str = f"Unable to retrieve elevation for address '{address}'; {{msg}}"
            ele_result = _get_json_result(ele_url, err_str=err_str,
                                          use_google=use_google)
            height = ele_result[0]['elevation']*u.meter

        else:
            height = 0.

        return cls.from_geodetic(lon=lon*u.deg, lat=lat*u.deg, height=height)

    @classmethod
    def get_site_names(cls):
        """
        Get list of names of observatories for use with
        `~astropy.coordinates.EarthLocation.of_site`.

        .. note::
            When this function is called, it will first attempt to
            download site information from the astropy data server.  If it
            cannot (i.e., an internet connection is not available), it will fall
            back on the list included with astropy (which is a limited and dated
            set of sites).  If you think a site should be added, issue a pull
            request to the
            `astropy-data repository <https://github.com/astropy/astropy-data>`_ .

        Returns
        -------
        names : list of str
            List of valid observatory names

        See Also
        --------
        of_site : Gets the actual location object for one of the sites names
                  this returns.
        """
        return cls._get_site_registry().names

    @classmethod
    def _get_site_registry(cls, force_download=False, force_builtin=False):
        """
        Gets the site registry.  The first time this either downloads or loads
        from the data file packaged with astropy.  Subsequent calls will use the
        cached version unless explicitly overridden.

        Parameters
        ----------
        force_download : bool or str
            If not False, force replacement of the cached registry with a
            downloaded version. If a str, that will be used as the URL to
            download from (if just True, the default URL will be used).
        force_builtin : bool
            If True, load from the data file bundled with astropy and set the
            cache to that.

        returns
        -------
        reg : astropy.coordinates.sites.SiteRegistry
        """
        # need to do this here at the bottom to avoid circular dependencies
        from .sites import get_builtin_sites, get_downloaded_sites

        if force_builtin and force_download:
            raise ValueError('Cannot have both force_builtin and force_download True')

        if force_builtin:
            reg = cls._site_registry = get_builtin_sites()
        else:
            reg = getattr(cls, '_site_registry', None)
            if force_download or not reg:
                try:
                    if isinstance(force_download, str):
                        reg = get_downloaded_sites(force_download)
                    else:
                        reg = get_downloaded_sites()
                except OSError:
                    if force_download:
                        raise
                    msg = ('Could not access the online site list. Falling '
                           'back on the built-in version, which is rather '
                           'limited. If you want to retry the download, do '
                           '{0}._get_site_registry(force_download=True)')
                    warn(AstropyUserWarning(msg.format(cls.__name__)))
                    reg = get_builtin_sites()
                cls._site_registry = reg

        return reg

    @property
    def ellipsoid(self):
        """The default ellipsoid used to convert to geodetic coordinates."""
        return self._ellipsoid

    @ellipsoid.setter
    def ellipsoid(self, ellipsoid):
        self._ellipsoid = _check_ellipsoid(ellipsoid)

    @property
    def geodetic(self):
        """Convert to geodetic coordinates for the default ellipsoid."""
        return self.to_geodetic()

    def to_geodetic(self, ellipsoid=None):
        """Convert to geodetic coordinates.

        Parameters
        ----------
        ellipsoid : str, optional
            Reference ellipsoid to use.  Default is the one the coordinates
            were initialized with.  Available are: 'WGS84', 'GRS80', 'WGS72'

        Returns
        -------
        (lon, lat, height) : tuple
            The tuple contains instances of `~astropy.coordinates.Longitude`,
            `~astropy.coordinates.Latitude`, and `~astropy.units.Quantity`

        Raises
        ------
        ValueError
            if ``ellipsoid`` is not recognized as among the ones implemented.

        Notes
        -----
        For the conversion to geodetic coordinates, the ERFA routine
        ``gc2gd`` is used.  See https://github.com/liberfa/erfa
        """
        ellipsoid = _check_ellipsoid(ellipsoid, default=self.ellipsoid)
        self_array = self.to(u.meter).view(self._array_dtype, np.ndarray)
        lon, lat, height = erfa.gc2gd(getattr(erfa, ellipsoid), self_array)
        return GeodeticLocation(
            Longitude(lon * u.radian, u.degree,
                      wrap_angle=180.*u.degree, copy=False),
            Latitude(lat * u.radian, u.degree, copy=False),
            u.Quantity(height * u.meter, self.unit, copy=False))

    @property
    def lon(self):
        """Longitude of the location, for the default ellipsoid."""
        return self.geodetic[0]

    @property
    def lat(self):
        """Longitude of the location, for the default ellipsoid."""
        return self.geodetic[1]

    @property
    def height(self):
        """Height of the location, for the default ellipsoid."""
        return self.geodetic[2]

    # mostly for symmetry with geodetic and to_geodetic.
    @property
    def geocentric(self):
        """Convert to a tuple with X, Y, and Z as quantities"""
        return self.to_geocentric()

    def to_geocentric(self):
        """Convert to a tuple with X, Y, and Z as quantities"""
        return (self.x, self.y, self.z)

    def get_itrs(self, obstime=None):
        """
        Generates an `~astropy.coordinates.ITRS` object with the location of
        this object at the requested ``obstime``.

        Parameters
        ----------
        obstime : `~astropy.time.Time` or None
            The ``obstime`` to apply to the new `~astropy.coordinates.ITRS`, or
            if None, the default ``obstime`` will be used.

        Returns
        -------
        itrs : `~astropy.coordinates.ITRS`
            The new object in the ITRS frame
        """
        # Broadcast for a single position at multiple times, but don't attempt
        # to be more general here.
        if obstime and self.size == 1 and obstime.shape:
            self = np.broadcast_to(self, obstime.shape, subok=True)

        # do this here to prevent a series of complicated circular imports
        from .builtin_frames import ITRS
        return ITRS(x=self.x, y=self.y, z=self.z, obstime=obstime)

    itrs = property(get_itrs, doc="""An `~astropy.coordinates.ITRS` object  with
                                     for the location of this object at the
                                     default ``obstime``.""")

    def get_gcrs(self, obstime):
        """GCRS position with velocity at ``obstime`` as a GCRS coordinate.

        Parameters
        ----------
        obstime : `~astropy.time.Time`
            The ``obstime`` to calculate the GCRS position/velocity at.

        Returns
        --------
        gcrs : `~astropy.coordinates.GCRS` instance
            With velocity included.
        """
        # do this here to prevent a series of complicated circular imports
        from .builtin_frames import GCRS

        itrs = self.get_itrs(obstime)
        # Assume the observatory itself is fixed on the ground.
        # We do a direct assignment rather than an update to avoid validation
        # and creation of a new object.
        zeros = np.broadcast_to(0. * u.km / u.s, (3,) + itrs.shape, subok=True)
        itrs.data.differentials['s'] = CartesianDifferential(zeros)
        return itrs.transform_to(GCRS(obstime=obstime))

    def get_gcrs_posvel(self, obstime):
        """
        Calculate the GCRS position and velocity of this object at the
        requested ``obstime``.

        Parameters
        ----------
        obstime : `~astropy.time.Time`
            The ``obstime`` to calculate the GCRS position/velocity at.

        Returns
        --------
        obsgeoloc : `~astropy.coordinates.CartesianRepresentation`
            The GCRS position of the object
        obsgeovel : `~astropy.coordinates.CartesianRepresentation`
            The GCRS velocity of the object
        """
        # GCRS position
        gcrs_data = self.get_gcrs(obstime).data
        obsgeopos = gcrs_data.without_differentials()
        obsgeovel = gcrs_data.differentials['s'].to_cartesian()
        return obsgeopos, obsgeovel

    def gravitational_redshift(self, obstime,
                               bodies=['sun', 'jupiter', 'moon'],
                               masses={}):
        """Return the gravitational redshift at this EarthLocation.

        Calculates the gravitational redshift, of order 3 m/s, due to the
        requested solar system bodies.

        Parameters
        ----------
        obstime : `~astropy.time.Time`
            The ``obstime`` to calculate the redshift at.

        bodies : iterable, optional
            The bodies (other than the Earth) to include in the redshift
            calculation.  List elements should be any body name
            `get_body_barycentric` accepts.  Defaults to Jupiter, the Sun, and
            the Moon.  Earth is always included (because the class represents
            an *Earth* location).

        masses : dict of str to Quantity, optional
            The mass or gravitational parameters (G * mass) to assume for the
            bodies requested in ``bodies``. Can be used to override the
            defaults for the Sun, Jupiter, the Moon, and the Earth, or to
            pass in masses for other bodies.

        Returns
        --------
        redshift :  `~astropy.units.Quantity`
            Gravitational redshift in velocity units at given obstime.
        """
        # needs to be here to avoid circular imports
        from .solar_system import get_body_barycentric

        bodies = list(bodies)
        # Ensure earth is included and last in the list.
        if 'earth' in bodies:
            bodies.remove('earth')
        bodies.append('earth')
        _masses = {'sun': consts.GM_sun,
                   'jupiter': consts.GM_jup,
                   'moon': consts.G * 7.34767309e22*u.kg,
                   'earth': consts.GM_earth}
        _masses.update(masses)
        GMs = []
        M_GM_equivalency = (u.kg, u.Unit(consts.G * u.kg))
        for body in bodies:
            try:
                GMs.append(_masses[body].to(u.m**3/u.s**2, [M_GM_equivalency]))
            except KeyError:
                raise KeyError(f'body "{body}" does not have a mass!')
            except u.UnitsError as exc:
                exc.args += ('"masses" argument values must be masses or '
                             'gravitational parameters',)
                raise

        positions = [get_body_barycentric(name, obstime) for name in bodies]
        # Calculate distances to objects other than earth.
        distances = [(pos - positions[-1]).norm() for pos in positions[:-1]]
        # Append distance from Earth's center for Earth's contribution.
        distances.append(CartesianRepresentation(self.geocentric).norm())
        # Get redshifts due to all objects.
        redshifts = [-GM / consts.c / distance for (GM, distance) in
                     zip(GMs, distances)]
        # Reverse order of summing, to go from small to big, and to get
        # "earth" first, which gives m/s as unit.
        return sum(redshifts[::-1])

    @property
    def x(self):
        """The X component of the geocentric coordinates."""
        return self['x']

    @property
    def y(self):
        """The Y component of the geocentric coordinates."""
        return self['y']

    @property
    def z(self):
        """The Z component of the geocentric coordinates."""
        return self['z']

    def __getitem__(self, item):
        result = super().__getitem__(item)
        if result.dtype is self.dtype:
            return result.view(self.__class__)
        else:
            return result.view(u.Quantity)

    def __array_finalize__(self, obj):
        super().__array_finalize__(obj)
        if hasattr(obj, '_ellipsoid'):
            self._ellipsoid = obj._ellipsoid

    def __len__(self):
        if self.shape == ():
            raise IndexError('0-d EarthLocation arrays cannot be indexed')
        else:
            return super().__len__()

    def _to_value(self, unit, equivalencies=[]):
        """Helper method for to and to_value."""
        # Conversion to another unit in both ``to`` and ``to_value`` goes
        # via this routine. To make the regular quantity routines work, we
        # temporarily turn the structured array into a regular one.
        array_view = self.view(self._array_dtype, np.ndarray)
        if equivalencies == []:
            equivalencies = self._equivalencies
        new_array = self.unit.to(unit, array_view, equivalencies=equivalencies)
        return new_array.view(self.dtype).reshape(self.shape)