swarmpal.toolboxes.dsecs.aux_tools
==================================

.. py:module:: swarmpal.toolboxes.dsecs.aux_tools


Attributes
----------

.. autoapisummary::

   swarmpal.toolboxes.dsecs.aux_tools.logger


Functions
---------

.. autoapisummary::

   swarmpal.toolboxes.dsecs.aux_tools.sph2sph
   swarmpal.toolboxes.dsecs.aux_tools.sub_FindLongestNonZero
   swarmpal.toolboxes.dsecs.aux_tools.sub_inversion
   swarmpal.toolboxes.dsecs.aux_tools.sub_LonInterp
   swarmpal.toolboxes.dsecs.aux_tools.sub_Swarm_grids_1D
   swarmpal.toolboxes.dsecs.aux_tools.sub_Swarm_grids
   swarmpal.toolboxes.dsecs.aux_tools.get_eq
   swarmpal.toolboxes.dsecs.aux_tools._normalizev
   swarmpal.toolboxes.dsecs.aux_tools.sub_points_along_fieldline


Module Contents
---------------

.. py:data:: logger

.. py:function:: sph2sph(latP, lonP, latOld, lonOld, VtOld, VpOld)

   Change coordinates and (horizontal part of) vectors from one spherical
       coordinate system to another.

   :param latP: Latitude and longitude of the new pole in the old coordinates, [deg]
   :type latP: float
   :param lonP: Latitude and longitude of the new pole in the old coordinates, [deg]
   :type lonP: float
   :param latOld: Latitudes and longitudes of the data points to be transformed in the
                  old coordinate system, [deg]
   :type latOld: array
   :param lonOld: Latitudes and longitudes of the data points to be transformed in the
                  old coordinate system, [deg]
   :type lonOld: array
   :param VtOld: Theta- and phi-components of a vector field at locations
                 (latOld,lonOld). If you want to transform only coordinates make these
                 empty  arrays (np.array([])).
   :type VtOld: array
   :param VpOld: Theta- and phi-components of a vector field at locations
                 (latOld,lonOld). If you want to transform only coordinates make these
                 empty  arrays (np.array([])).
   :type VpOld: array

   :returns: * **latNew, lonNew** (*array*) -- Coordinates of the points (latOld,lonOld) in the new system, [deg],
               same size as input parameters. Note that the old pole has longitude 0
               and longitude is between 0 and 360.
             * **VtNew, VpNew** (*array*) -- Theta- and phi-components of the vector field in the new system. If no
               input vector field was given, these are empty vectors [].


.. py:function:: sub_FindLongestNonZero(x)

   Find the longest sequence of consecutive non-zero elements in an array.
       Return the sequence and indices as y=x[ind].

   :param x: Input array
   :type x: array

   :returns: * **y** (*array*) -- Longest sequence of consecutive non-zero elements
             * **ind** (*array*) -- Indices of the sequence


.. py:function:: sub_inversion(secsMat, regMat, epsSVD, alpha, magVec)

   Solve matrix equation sysMat*resultVec = dataVec, using truncated
       singular value decomposition.

   :param secsMat: Matrix giving the magnetic field from SECS amplitudes
   :type secsMat: ndarray
   :param regMat: Regularization matrix for the SECS amplitudes
   :type regMat: ndarray
   :param epsSVD: Pre-defined truncation parameter so that all singular values smaller
                  than epsSVD*(largest singular value) will be ignored
   :type epsSVD: float
   :param alpha: Parameter scaling the amount of regularization
   :type alpha: float
   :param magVec: Magnetic measurements to be fitted
   :type magVec: array

   :returns: **result_Vec** -- Vector of SECS amplitudes
   :rtype: array


.. py:function:: sub_LonInterp(lat, lon, intLat, method, ekstra=np.nan)

   Robust interpolation of longitude by interpolating the sine and cosine
   of the angle. This way a 360 degree jump does not matter.

   :param lat: Original coordinates, [degree].
   :type lat: ndarray
   :param lon: Original coordinates, [degree].
   :type lon: ndarray
   :param intLat: Latitudes where original longitudes are interpolated to, [degree].
   :type intLat: ndarray
   :param method: Interpolation method. Same as for scipy.interpolate.interp1d.
   :type method: str

   :returns: **intLon** -- Interpolated longitudes at latitudes intLat, [degree]. -180 <= intlon <= 180.
   :rtype: ndarray


.. py:function:: sub_Swarm_grids_1D(lat1, lat2, Dlat1D, ExtLat1D)

   Creates 1D grid around the two Swarm satellite paths.

   :param lat1: Geographic latitudes of the satellites' paths, [degree].
   :type lat1: ndarray
   :param lat2: Geographic latitudes of the satellites' paths, [degree].
   :type lat2: ndarray
   :param Dlat1D: 1D grid spacing in latitudinal direction, [degree].
   :type Dlat1D: int or float
   :param ExtLat1D: Number of points to extend the 1D grid outside the data area in latitudinal
                    direction.
   :type ExtLat1D: int

   :returns: * **lat1D** (*ndarray*) -- Geographic latitudes of the 1D grid, [degree].
             * **mat1Dsecond** (*ndarray*) -- Second gradient matrix.


.. py:function:: sub_Swarm_grids(lat1, lon1, lat2, lon2, Dlat2D, LonRatio, ExtLat2D, ExtLon2D)

   Creates 2D grids around the two Swarm satellite paths.

   :param lat1: Geographic latitudes of the satellites' paths, [degree].
   :type lat1: ndarray
   :param lat2: Geographic latitudes of the satellites' paths, [degree].
   :type lat2: ndarray
   :param lon1: Geographic longitudes of the satellites' paths, [degree].
   :type lon1: ndarray
   :param lon2: Geographic longitudes of the satellites' paths, [degree].
   :type lon2: ndarray
   :param Dlat2D: 2D grid spacing in latitudinal direction, [degree].
   :type Dlat2D: float
   :param LonRatio: Ratio between the satellite separation and longitudinal spacing of
                    the 2D grid.
   :type LonRatio: float
   :param ExtLat2D: Number of points to extend the 2D grid outside the data area in latitudinal
                    and longitudinal directions.
   :type ExtLat2D: int
   :param ExtLon2D: Number of points to extend the 2D grid outside the data area in latitudinal
                    and longitudinal directions.
   :type ExtLon2D: int

   :returns: * **lat2D, lon2D** (*ndarray*) -- Geographic latitudes and longitudes of the 2D grid, [degree].
             * **angle2D** (*ndarray*) -- Half-angle of such a spherical cap that has the same area as the 2D grid
               cell, [radian].
             * **mat2DsecondLat** (*ndarray*) -- Second gradient matrix in latitude.


.. py:function:: get_eq(ds, lat_filter_max=60, ovals=None)

   Splits data into a list of pieces suitable for DSECS analysis based on latitude.

   :param ds: Input dataset.
   :type ds: xarray
   :param lat_filter_max: Maximum geographic latitude (absolute value). All data above this limit is ignored. Default: 60
   :type lat_filter_max: int, optional
   :param ovals: list of pre-calculated numpy slices with which to split the data.
   :type ovals: list

   :returns: **out** -- List of data segments split for DSECS analysis if they have been precalculated.
   :rtype: list of xarray, or None, None by default


.. py:function:: _normalizev(v)

   Creates an unit vector.
   :param v: Input array.
   :type v: ndarray

   :returns: Normalized unit vector.
   :rtype: ndarray


.. py:function:: sub_points_along_fieldline(thetaSECS, Rsecs, L, minD)

   Calculate points to be used in the Biot-Savart integral along field line
       in function SECS_2D_CurlFree_AntiSym_magnetic_lineintegral

   :param thetaSECS: Co-latitude of the CF SECS, [radian]
   :type thetaSECS: float
   :param Rsecs: Radius of the sphere where CF SECS is located, [km]
   :type Rsecs: float
   :param L: L-value of the field line starting from the CF SECS pole, [km    NOTE: this is in kilometers, so really L*Rsecs;
   :type L: float
   :param minD: minimum horizontal distance between the CF SECS pole and footpoints of
                the points where magnetic field is needed, [km], SCALAR
   :type minD: float

   :returns: co-latitudes of the integration points (= end points of the current
             elements), [radian]
   :rtype: array


