<commit_before># -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:         dp
# Purpose:      Processing related to Dual-Pol and Differential Phase
#
# Authors:      <NAME>, <NAME> and <NAME>
#
# Created:      20.09.2013
# Copyright:    (c) <NAME>, <NAME> and <NAME> 2011
# Licence:      The MIT License
#-------------------------------------------------------------------------------
#!/usr/bin/env python

"""
Dual-Pol and Differential Phase
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This module provides algorithms to process polarimentric radar moments,
namely the differential phase, PhiDP, and, based on successful PhiDP retrieval,
also the specific differential phase, Kdp. Please note that the actual application
of polarimetric moments is implemented in the corresponding wradlib modules, e.g.:

    - fuzzy echo classification from polarimetric moments (:doc:`classify_echo_fuzzy <generated/wradlib.clutter.classify_echo_fuzzy>`)

    - attenuation correction (:doc:`pia_from_kdp <generated/wradlib.atten.pia_from_kdp>`)

    - direct precipitation retrieval from Kdp (:doc:`kdp2r <generated/wradlib.trafo.kdp2r>`)

Establishing a valid PhiDP profile for Kdp retrieval involves despeckling (linear_despeckle),
gap filling (fill_phidp), phase unfolding and smoothing. For convenience, these
steps have been combined in the function :doc:`process_raw_phidp <generated/wradlib.dp.process_raw_phidp>`.

Once a valid PhiDP profile has been established, :doc:`kdp_from_phidp <generated/wradlib.dp.kdp_from_phidp>`
can be used to retrieve Kdp.

Please note that so far, the functions in this module were designed to increase
performance. This was mainly achieved by allowing the simultaneous application
of functions over multiple array dimensions. The only requirement to apply these
function is that the **range dimension must be the last dimension** of all input arrays.

Another increase in performance was achieved by replacing the naive (Python/numpy)
implementation of the phase unfolding by a Fortan implementation using f2py
(http://cens.ioc.ee/projects/f2py2e/). f2py usually ships with numpy and should
be available via the command line. To test whether f2py is available on your
system, execute ``f2py`` on the system console. Or, alternatively, ``f2py.py``. If it is
available, you should get a bunch of help instructions. Now change to the wradlib
module directory and execute on the system console:

   ``f2py.py -c -m speedup speedup.f``



.. autosummary::
   :nosignatures:
   :toctree: generated/

    process_raw_phidp
    kdp_from_phidp
    fill_phidp
    unfold_phi
    unfold_phi_naive
    texture

"""

import numpy as np
from scipy.interpolate import interp1d
from scipy.signal import medfilt
from scipy.stats import nanmedian, nanmean

# Check whether fast Fortran implementation is available
speedupexists = True
try:
    from wradlib.speedup import f_unfold_phi
except ImportError:
    print "WARNING: To increase performance, you should try to build module <speedup>."
    print "See module documentation for details."
    speedupexists = False



def process_raw_phidp(phidp, rho, copy=False):
    """Establish consistent PhiDP profiles from raw data.

    Processing of raw PhiDP data contains the following steps:

        - Despeckle

        - Fill missing data
          (general asssumption: PhiDP is monotonically increasing along the beam)

        - Phase unfolding

        - Smoothing

    Parameters
    ----------
    phidp : array of shape (n azimuth angles, n range gates)
    rho : array of shape (n azimuth angles, n range gates)
    copy : boolean
        leaves the original phidp array untouched

    """
    if copy:
        phidp = phidp.copy()
    # despeckle
    phidp = linear_despeckle(phidp)
    phidp = fill_phidp(phidp)
    # apply unfolding
    if speedupexists:
        phidp = unfold_phi(phidp, rho)
    else:
        phidp = unfold_phi_naive(phidp, rho)
    # median filter smoothing
    phidp = medfilt_along_axis(phidp,5)
    return phidp


def kdp_from_phidp(phidp, L=7):
    """Retrieves Kdp from PhiDP by applying a moving window range derivative.

    See [Vulpiani2012]_ for details about the moving window approach.

    Please note that the moving window size *L* is specified as the number of range
    gates. Thus, this argument might need adjustment in case the range resolution changes.
    In the original publication ([Vulpiani2012]_), the value L=7 was chosen for
    a range resolution of 1km.

    ATTENTION: The function is designed for speed by allowing to process
    multiple dimensions in one step. For this purpose, the RANGE dimension needs
    to be the LAST dimension of the input array.

    Parameters
    ----------
    data : multi-dimensional array
        Note that the range dimension must be the last dimension of the input array.

    L : integer
        Width of the window (as number of range gates)<commit_msg>MIN: Provides the option to pass parameters accepted by the functions to process_raw_phidp.<commit_after># -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:         dp
# Purpose:      Processing related to Dual-Pol and Differential Phase
#
# Authors:      <NAME>, <NAME> and <NAME>
#
# Created:      20.09.2013
# Copyright:    (c) <NAME>, <NAME> and <NAME> 2011
# Licence:      The MIT License
#-------------------------------------------------------------------------------
#!/usr/bin/env python

"""
Dual-Pol and Differential Phase
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This module provides algorithms to process polarimentric radar moments,
namely the differential phase, PhiDP, and, based on successful PhiDP retrieval,
also the specific differential phase, Kdp. Please note that the actual application
of polarimetric moments is implemented in the corresponding wradlib modules, e.g.:

    - fuzzy echo classification from polarimetric moments (:doc:`classify_echo_fuzzy <generated/wradlib.clutter.classify_echo_fuzzy>`)

    - attenuation correction (:doc:`pia_from_kdp <generated/wradlib.atten.pia_from_kdp>`)

    - direct precipitation retrieval from Kdp (:doc:`kdp2r <generated/wradlib.trafo.kdp2r>`)

Establishing a valid PhiDP profile for Kdp retrieval involves despeckling (linear_despeckle),
gap filling (fill_phidp), phase unfolding and smoothing. For convenience, these
steps have been combined in the function :doc:`process_raw_phidp <generated/wradlib.dp.process_raw_phidp>`.

Once a valid PhiDP profile has been established, :doc:`kdp_from_phidp <generated/wradlib.dp.kdp_from_phidp>`
can be used to retrieve Kdp.

Please note that so far, the functions in this module were designed to increase
performance. This was mainly achieved by allowing the simultaneous application
of functions over multiple array dimensions. The only requirement to apply these
function is that the **range dimension must be the last dimension** of all input arrays.

Another increase in performance was achieved by replacing the naive (Python/numpy)
implementation of the phase unfolding by a Fortan implementation using f2py
(http://cens.ioc.ee/projects/f2py2e/). f2py usually ships with numpy and should
be available via the command line. To test whether f2py is available on your
system, execute ``f2py`` on the system console. Or, alternatively, ``f2py.py``. If it is
available, you should get a bunch of help instructions. Now change to the wradlib
module directory and execute on the system console:

   ``f2py.py -c -m speedup speedup.f``



.. autosummary::
   :nosignatures:
   :toctree: generated/

    process_raw_phidp
    kdp_from_phidp
    fill_phidp
    unfold_phi
    unfold_phi_naive
    texture

"""

import numpy as np
from scipy.interpolate import interp1d
from scipy.signal import medfilt
from scipy.stats import nanmedian, nanmean

# Check whether fast Fortran implementation is available
speedupexists = True
try:
    from wradlib.speedup import f_unfold_phi
except ImportError:
    print "WARNING: To increase performance, you should try to build module <speedup>."
    print "See module documentation for details."
    speedupexists = False



def process_raw_phidp(phidp, rho, N_despeckle=3, N_fillmargin=3, N_unfold=5, N_filter=5, copy=False):
    """Establish consistent PhiDP profiles from raw data.

    Processing of raw PhiDP data contains the following steps:

        - Despeckle

        - Fill missing data
          (general asssumption: PhiDP is monotonically increasing along the beam)

        - Phase unfolding

        - Smoothing

    Parameters
    ----------
    phidp : array of shape (n azimuth angles, n range gates)
    rho : array of shape (n azimuth angles, n range gates)
    N_despeckle : integer
        *N* parameter of function dp.linear_despeckle
    N_fillmargin : integer
        *margin* parameter of function dp.fill_phidp
    N_unfold : integer
        *width* parameter of function dp.unfold_phi
    N_filter : integer
        *N* parameter of function dp.medfilt_along_axis
    copy : boolean
        leaves the original phidp array untouched

    """
    if copy:
        phidp = phidp.copy()
    # despeckle
    phidp = linear_despeckle(phidp, N=N_despeckle)
    phidp = fill_phidp(phidp, margin=N_fillmargin)
    # apply unfolding
    if speedupexists:
        phidp = unfold_phi(phidp, rho, width=N_unfold)
    else:
        phidp = unfold_phi_naive(phidp, rho, width=N_unfold)
    # median filter smoothing
    phidp = medfilt_along_axis(phidp, N=N_filter)
    return phidp


def kdp_from_phidp(phidp, L=7):
    """Retrieves Kdp from PhiDP by applying a moving window range derivative.

    See [Vulpiani2012]_ for details about the moving window approach.

    Please note that the moving window size *L* is specified as the number of range
    gates. Thus, this argument might need adjustment in case the range resolution changes.
    In the original publication ([Vulpiani2012]_), the value L=7 was chosen for
    a range resolution of 1km.

    ATTENTION: The function is designed for speed by allowing to process
    multiple dimensions in one step. For this purpose, the RANGE dimension needs
    to be the LAST dimension of the input array.

    Parameters
    ----------
    data : multi-dimensional array
        Note that the range dimension must be the last dimension of the input array.

    L : integer
        Width of the window (as number of range gates)
{'fraction_non_alphanumeric': 0.10161552432845808, 'fraction_numerical': 0.007169486664754803, 'mean_word_length': 3.1598409542743537, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1492168', 'n_tokens_mistral': 3114, 'n_tokens_neox': 2909, 'n_words': 1174}