vcor_dual_prf/vcor_dual_prf.py
def correct_dualprf(radar, method_det, vel_field='velocity', 
                    kernel_det=np.ones((7,7)), min_valid_det=1, 
                    max_dev=1.0, two_step=True, method_cor=None, 
                    kernel_cor=None, min_valid_cor=1, new_field='velocity_cor',
                    replace=False, new_field_name='velocity_cor', 
                    new_field_lname='Dual-PRF outlier corrected velocity'):
    """
    Correction of dual-PRF outliers in radar velocity data. 
    Includes the corrected field in the input radar object. 
  
    Available reference statistics:
    'mean' : local mean velocity (Joe and May, 2003)
    'median' : local median velocity (Holleman and Beekhuis, 2003)
    'cmean_sc' : local circular mean velocity (PRF-scaled) (Altube et al., 2017)
    'cmean' : local circular mean velocity (Hengstebeck et al., 2018)
                

    Parameters
    ----------
    radar : Radar
        Py-ART radar structure
    method_det : str
        Detection method
    vel_field: str
        Input velocity field name (dual-PRF)
    kernel_det : array
        Neighbour kernel, 1/0 values (detection), if None a 7x7 ones array 
        is used, excluding the central value
    min_valid_det : int
        Minimum number of valid neighbours (detection)
    max_dev : float
        Maximum deviation threshold (detection)
    two_step : bool
        Whether to separate detection and correction stages
    method_cor : str or None
        Correction method, if None, method_det is used (except in the case of 
        'cmean_sc', for which 'cmean' is used by default, due to error 
        propagation issues when PRF scaling)
    kernel_cor : array
        Neighbour kernel 1/0 values (correction), if None, kernel_det is used
    min_valid_cor : int
        Minimum number of valid neighbours (correction)
    new_field : str
        Output (corrected) velocity field name
    replace : bool
        Whether to replace input field
    new_field_name : str
        Output (corrected) velocity field standard name
    new_field_lname : str
        Output (corrected) velocity field long name

    """

    vcorr = radar.fields[vel_field]['data'].copy()

    for sw, sweep_slice in enumerate(radar.iter_slice()):

        # Dual-PRF parameters
        v_ny, prf_h, prf_factor, prf_flag = file_tools.get_prf_pars(radar, sw)
        if prf_flag is None:
            continue
        
        prf_factor_sw = file_tools.prf_factor_array(radar, sw)

        # primary velocities
        vp_sw = v_ny/prf_factor
        
        #extract velocity sweep
        v_sw = radar.fields[vel_field]['data'][sweep_slice]

        # ERROR DETECTION
        # Reference velocities at each gate
        vref_det = _vel_ref(data_ma=v_sw, method=method_det, 
                              kernel=kernel_det, v_ny=v_ny, 
                              mask=None,
                              prf_factor_arr=prf_factor_sw, 
                              min_valid=min_valid_det)

        if (method_det=='cmean')|(method_det=='cmean_sc'):
            # Calculate difference in phase space
            ph_obs = v_sw*(m.pi/v_ny)
            ph_ref = vref_det*(m.pi/v_ny)
            
            ph_diff = ph_obs - ph_ref
            diff_ma = (v_ny/m.pi)*np.ma.arctan2(np.ma.sin(ph_diff), np.ma.cos(ph_diff))
            
        else:                       
            diff_ma = v_sw - vref_det

        # Outlier mask
        err_mask = _mask_diff_above(diff_ma=diff_ma, th_ma=max_dev*vp_sw)

        if two_step:

            if kernel_cor is None:
                kernel_cor = kernel_det

            if method_cor is None:
                if (method_det=='cmean')|(method_det=='cmean_sc'):
                    method_cor = 'median'
                else:
                    method_cor = method_det

            vref_cor = _vel_ref(data_ma=v_sw, method=method_cor, 
                                  kernel=kernel_cor, v_ny=v_ny, 
                                  mask=err_mask, 
                                  prf_factor_arr=prf_factor_sw, 
                                  min_valid=min_valid_cor)

        else:
            vref_cor = vref_det

        # ERROR CORRECTION
        # Unwrap number and corrected velocity field
        uwp = _dualprf_error_unwrap(data_ma=v_sw, ref_ma=vref_cor, 
                                   err_mask=err_mask, pvel_arr=vp_sw, 
                                   prf_arr=prf_factor_sw)

        # Correct velocity field
        vc = v_sw + 2 * uwp * vp_sw
        
        # Fold velocity values into Nyquist interval
        vcorr[sweep_slice] = fold_circular(data_ma=vc, mod=v_ny)

    # ADD CORRECTED VELOCITY FIELD
    file_tools.add_vcor_field(radar, field_i=vel_field, field_o=new_field, 
                        data=vcorr, std_name=None,
                        description='Correction of dual-PRF applied using technique developed by Hengstebeck et al. (2018) doi:10.1175/JTECH-D-16-0230.1. Implementation by Servei Meteorològic de Catalunya at https://github.com/meteocat/vcor_dual_prf',
                        long_name=new_field_lname, replace=replace)

    Correction of dual-PRF outliers in radar velocity data. 
    Includes the corrected field in the input radar object. 
  
    Available reference statistics:
    'mean' : local mean velocity (Joe and May, 2003)
    'median' : local median velocity (Holleman and Beekhuis, 2003)
    'cmean_sc' : local circular mean velocity (PRF-scaled) (Altube et al., 2017)
    'cmean' : local circular mean velocity (Hengstebeck et al., 2018)
                

    Parameters
    ----------
    radar : Radar
        Py-ART radar structure
    method_det : str
        Detection method
    vel_field: str
        Input velocity field name (dual-PRF)
    kernel_det : array
        Neighbour kernel, 1/0 values (detection), if None a 7x7 ones array 
        is used, excluding the central value
    min_valid_det : int
        Minimum number of valid neighbours (detection)
    max_dev : float
        Maximum deviation threshold (detection)
    two_step : bool
        Whether to separate detection and correction stages
    method_cor : str or None
        Correction method, if None, method_det is used (except in the case of 
        'cmean_sc', for which 'cmean' is used by default, due to error 
        propagation issues when PRF scaling)
    kernel_cor : array
        Neighbour kernel 1/0 values (correction), if None, kernel_det is used
    min_valid_cor : int
        Minimum number of valid neighbours (correction)
    new_field : str
        Output (corrected) velocity field name
    replace : bool
        Whether to replace input field
    new_field_name : str
        Output (corrected) velocity field standard name
    new_field_lname : str
        Output (corrected) velocity field long name

    
Correction of dual-PRF outliers in radar velocity data.
Includes the corrected field in the input radar object.



Parameters


Correction of dual - PRF outliers in radar velocity data . Includes the corrected field in the input radar object . Parameters
def correct_dualprf(radar, method_det, vel_field='velocity', 
                    kernel_det=np.ones((7,7)), min_valid_det=1, 
                    max_dev=1.0, two_step=True, method_cor=None, 
                    kernel_cor=None, min_valid_cor=1, new_field='velocity_cor',
                    replace=False, new_field_name='velocity_cor', 
                    new_field_lname='Dual-PRF outlier corrected velocity'):
    vcorr = radar.fields[vel_field]['data'].copy()
    for sw, sweep_slice in enumerate(radar.iter_slice()):
        v_ny, prf_h, prf_factor, prf_flag = file_tools.get_prf_pars(radar, sw)
        if prf_flag is None:
            continue
        prf_factor_sw = file_tools.prf_factor_array(radar, sw)
        vp_sw = v_ny/prf_factor
        v_sw = radar.fields[vel_field]['data'][sweep_slice]
        vref_det = _vel_ref(data_ma=v_sw, method=method_det, 
                              kernel=kernel_det, v_ny=v_ny, 
                              mask=None,
                              prf_factor_arr=prf_factor_sw, 
                              min_valid=min_valid_det)
        if (method_det=='cmean')|(method_det=='cmean_sc'):
            ph_obs = v_sw*(m.pi/v_ny)
            ph_ref = vref_det*(m.pi/v_ny)
            ph_diff = ph_obs - ph_ref
            diff_ma = (v_ny/m.pi)*np.ma.arctan2(np.ma.sin(ph_diff), np.ma.cos(ph_diff))
        else:                       
            diff_ma = v_sw - vref_det
        err_mask = _mask_diff_above(diff_ma=diff_ma, th_ma=max_dev*vp_sw)
        if two_step:
            if kernel_cor is None:
                kernel_cor = kernel_det
            if method_cor is None:
                if (method_det=='cmean')|(method_det=='cmean_sc'):
                    method_cor = 'median'
                else:
                    method_cor = method_det
            vref_cor = _vel_ref(data_ma=v_sw, method=method_cor, 
                                  kernel=kernel_cor, v_ny=v_ny, 
                                  mask=err_mask, 
                                  prf_factor_arr=prf_factor_sw, 
                                  min_valid=min_valid_cor)
        else:
            vref_cor = vref_det
        uwp = _dualprf_error_unwrap(data_ma=v_sw, ref_ma=vref_cor, 
                                   err_mask=err_mask, pvel_arr=vp_sw, 
                                   prf_arr=prf_factor_sw)
        vc = v_sw + 2 * uwp * vp_sw
        vcorr[sweep_slice] = fold_circular(data_ma=vc, mod=v_ny)
    file_tools.add_vcor_field(radar, field_i=vel_field, field_o=new_field, 
                        data=vcorr, std_name=None,
                        description='Correction of dual-PRF applied using technique developed by Hengstebeck et al. (2018) doi:10.1175/JTECH-D-16-0230.1. Implementation by Servei Meteorològic de Catalunya at https://github.com/meteocat/vcor_dual_prf',
                        long_name=new_field_lname, replace=replace)
def correct_dualprf ( radar , method_det , vel_field = 'velocity' , kernel_det = np . ones ( ( 7 , 7 ) ) , min_valid_det = 1 , max_dev = 1.0 , two_step = True , method_cor = None , kernel_cor = None , min_valid_cor = 1 , new_field = 'velocity_cor' , replace = False , new_field_name = 'velocity_cor' , new_field_lname = 'Dual-PRF outlier corrected velocity' ) : vcorr = radar . fields [ vel_field ] [ 'data' ] . copy ( ) for sw , sweep_slice in enumerate ( radar . iter_slice ( ) ) : v_ny , prf_h , prf_factor , prf_flag = file_tools . get_prf_pars ( radar , sw ) if prf_flag is None : continue prf_factor_sw = file_tools . prf_factor_array ( radar , sw ) vp_sw = v_ny / prf_factor v_sw = radar . fields [ vel_field ] [ 'data' ] [ sweep_slice ] vref_det = _vel_ref ( data_ma = v_sw , method = method_det , kernel = kernel_det , v_ny = v_ny , mask = None , prf_factor_arr = prf_factor_sw , min_valid = min_valid_det ) if ( method_det == 'cmean' ) | ( method_det == 'cmean_sc' ) : ph_obs = v_sw * ( m . pi / v_ny ) ph_ref = vref_det * ( m . pi / v_ny ) ph_diff = ph_obs - ph_ref diff_ma = ( v_ny / m . pi ) * np . ma . arctan2 ( np . ma . sin ( ph_diff ) , np . ma . cos ( ph_diff ) ) else : diff_ma = v_sw - vref_det err_mask = _mask_diff_above ( diff_ma = diff_ma , th_ma = max_dev * vp_sw ) if two_step : if kernel_cor is None : kernel_cor = kernel_det if method_cor is None : if ( method_det == 'cmean' ) | ( method_det == 'cmean_sc' ) : method_cor = 'median' else : method_cor = method_det vref_cor = _vel_ref ( data_ma = v_sw , method = method_cor , kernel = kernel_cor , v_ny = v_ny , mask = err_mask , prf_factor_arr = prf_factor_sw , min_valid = min_valid_cor ) else : vref_cor = vref_det uwp = _dualprf_error_unwrap ( data_ma = v_sw , ref_ma = vref_cor , err_mask = err_mask , pvel_arr = vp_sw , prf_arr = prf_factor_sw ) vc = v_sw + 2 * uwp * vp_sw vcorr [ sweep_slice ] = fold_circular ( data_ma = vc , mod = v_ny ) file_tools . add_vcor_field ( radar , field_i = vel_field , field_o = new_field , data = vcorr , std_name = None , description = 'Correction of dual-PRF applied using technique developed by Hengstebeck et al. (2018) doi:10.1175/JTECH-D-16-0230.1. Implementation by Servei Meteorològic de Catalunya at https://github.com/meteocat/vcor_dual_prf', long_name = new_field_lname , replace = replace )
Correction of dual-PRF outliers in radar velocity data.
Correction of dual - PRF outliers in radar velocity data .
"""
    Correction of dual-PRF outliers in radar velocity data. 
    Includes the corrected field in the input radar object. 
  
    Available reference statistics:
    'mean' : local mean velocity (Joe and May, 2003)
    'median' : local median velocity (Holleman and Beekhuis, 2003)
    'cmean_sc' : local circular mean velocity (PRF-scaled) (Altube et al., 2017)
    'cmean' : local circular mean velocity (Hengstebeck et al., 2018)
                

    Parameters
    ----------
    radar : Radar
        Py-ART radar structure
    method_det : str
        Detection method
    vel_field: str
        Input velocity field name (dual-PRF)
    kernel_det : array
        Neighbour kernel, 1/0 values (detection), if None a 7x7 ones array 
        is used, excluding the central value
    min_valid_det : int
        Minimum number of valid neighbours (detection)
    max_dev : float
        Maximum deviation threshold (detection)
    two_step : bool
        Whether to separate detection and correction stages
    method_cor : str or None
        Correction method, if None, method_det is used (except in the case of 
        'cmean_sc', for which 'cmean' is used by default, due to error 
        propagation issues when PRF scaling)
    kernel_cor : array
        Neighbour kernel 1/0 values (correction), if None, kernel_det is used
    min_valid_cor : int
        Minimum number of valid neighbours (correction)
    new_field : str
        Output (corrected) velocity field name
    replace : bool
        Whether to replace input field
    new_field_name : str
        Output (corrected) velocity field standard name
    new_field_lname : str
        Output (corrected) velocity field long name

    """ # Dual-PRF parameters # primary velocities #extract velocity sweep # ERROR DETECTION # Reference velocities at each gate # Calculate difference in phase space # Outlier mask # ERROR CORRECTION # Unwrap number and corrected velocity field # Correct velocity field # Fold velocity values into Nyquist interval # ADD CORRECTED VELOCITY FIELD
radar
method_det
vel_field
kernel_det
min_valid_det
max_dev
two_step
method_cor
kernel_cor
min_valid_cor
new_field
replace
new_field_name
new_field_lname