The filters module.

This module contains the code to generate a selection of 2-dimensional filters:

  • Gaussian - specified by \sigma, the standard deviation of the Gaussian. Note that, in MONC, we have found that the Smagorinsky sub-filter model corresponds roughly with a Gaussian filter with \sigma = \Delta.

  • Spectral Cutoff - specified by the wavenumber. A rough equivalence with the Gaussian filter is wavenumber = \pi/(2\sigma). Hence wavelength=4\sigma.

  • Spectral Cylindrical Cutoff - specified by the wavenumber. A rough equivalence with the Gaussian filter is wavenumber = \pi/(2\sigma). Hence wavelength=4\sigma.

  • 2D version of the 1-2-1 filter. Note: if options['FFT_type'] is set to 'DIRECT', this is calculated directly, not using FFTs.

  • Running mean - specified by the width in grid points. A rough equivalence with the Gaussian filter is width = int(\sigma /dx \times \pi \times 2.0/3.0)+1, where dx is the grid spacing.

New at 0.3

  1. The filters.filter_2D class has been replaced with filters.Filter. This now accepts an optional argument ndim when creating a Filter instance. This may be 1 or 2 and defaults to 2. The use_ave option is no longer supported.

Detailed Module Contents

The entire module is documented below.

filters.py.

This module contains the code to generate a selection of 2-dimensional filters.

@author: Peter Clark

class subfilter.filters.Filter(filter_id, filter_name, delta_x=1000.0, cutoff=1e-06, npoints=None, high_pass=0, wavenumber=- 1, width=- 1, sigma=- 1, ndim=2, set_fft=False)

Class defining a filter function.

Parameters
  • filter_name (str) – Name of filter used. Chices are: gaussian, wave-cutoff, circular_wave_cutoff, running-mean, one_two-one

  • wavenumber (float) – If a wave-cutoff filter is used, contains the cutoff wavenumber.

  • delta_x (float) – Distance between points in the horizontal, used to caculate the filter

  • width (int) – If set, controls the width of the filter. Must be set for running-mean filter.

  • cutoff (float) – If float is not set, this controls the width of the filter. The width of the filter is extended until the minimum value in the filter is less than this cutoff value.

  • high_pass (bool) – If a wave-cutoff filter is used, this determines whether it is high or low pass (note high pass hasn’t actually been coded yet!)

  • sigma (float) – If a Gaussian filter is used, this is the lengthscale of the filter.

  • ndim (int) – Number of dimensions (default=2)

filter_error(problem)

Print error when parameter required by filter does not exist.

Parameters
  • filter_name (str) – Name of filter

  • problem (str) – Name of parameter that has not been set

Returns

Error code for filter.

Return type

filter_err (-9999)

subfilter.filters.is_npi(x, tol=1e-06)
subfilter.filters.running_mean_filter(width, npoints, ndim=2)

Calculate a square 1 or 2D running mean filter with the given width.

Parameters
  • width (int) – Width of the filter.

  • npoints (int) – Number of points in output array.

  • ndim (int (default=2)) – Number of dimensions.

Returns

ndarray – Every element equals 1.0/(width**ndim)

Return type

ndim dimensional array of size width in each dimension.

subfilter.filters.one_two_one_filter(width, npoints, ndim=2)

Calculate a square 1 or 2D running mean filter with the given width.

Parameters
  • width (int) – Width of the filter.

  • npoints (int) – Number of points in output array.

  • ndim (int (default=2)) – Number of dimensions.

Returns

ndarray – Every element equals 1.0/(width**ndim)

Return type

ndim dimensional array of size width in each dimension.

subfilter.filters.wave_cutoff_filter(wavenumber, delta_x=1000.0, npoints=- 1, cutoff=1e-06, high_pass=0, ndim=2, set_fft=True)

Calculate a 2D wave-cutoff filter caculated using the given wavenumber.

Uses filter(x,y) = \sin(wavenumber * x)/x * \sin(wavenumber * y)/y in 2D. Normalised by sum(filter(x,y)). Note that this returns the point sampled value of filter(x).

Parameters
  • wavenumber (float) – Cutoff wavenumber in radians/wavelength.

  • delta_x ((float, default=1000.0)) – The distance between two points in the data that the filter will be applied to.

  • npoints (int (default=-1)) – If not -1, used to explicitly set the npoints of the filter.

  • cutoff (float (default=0.0001)) – If npoints=-1, the npoints of the filter is set dynamically, and increased until the smallest value of the filter is less than the cutoff value.

  • high_pass (bool (default=0)) – If true a high pass filter is calculated

  • ndim (int) – Number of dimensions (default=2)

Returns

ndarray

Return type

2D array of filter values

subfilter.filters.circular_wave_cutoff_filter(wavenumber, delta_x=1000.0, npoints=- 1, cutoff=1e-06, high_pass=0, ndim=2)

Calculate a 2D wave-cutoff filter caculated using the given wavenumber.

Uses filter(x,y) = \sin(wavenumber * x)/x * \sin(wavenumber * y)/y in 2D. Normalised by sum(filter(x,y)). Note that this returns the point sampled value of filter(x).

Parameters
  • wavenumber (float) – Cutoff wavenumber in radians/wavelength.

  • delta_x ((float, default=1000.0)) – The distance between two points in the data that the filter will be applied to.

  • npoints (int (default=-1)) – If not -1, used to explicitly set the npoints of the filter.

  • cutoff (float (default=0.0001)) – If npoints=-1, the npoints of the filter is set dynamically, and increased until the smallest value of the filter is less than the cutoff value.

  • high_pass (bool (default=0)) – If true a high pass filter is calculated

  • ndim (int) – Number of dimensions (default=2)

Returns

ndarray

Return type

2D array of filter values

subfilter.filters.gaussian_filter(sigma, delta_x=1000.0, npoints=- 1, cutoff=1e-06, ndim=2)

Calculates a 1 or 2D Gaussian filter calculated with the given lengthscale (sigma)

Uses filter(x,y) = \exp(-(x^2+y^2)/(2\sigma^2)) in 2D. Normalised by sum(filter(x)). Note that this returns the point sampled value of filter(x).

Parameters
  • sigma (float) – The lengthscale of the filter.

  • delta_x (float (default=1000.0)) – The distance between two points in the data that the filter will be applied to.

  • npoints (int (default=-1)) – If not -1, used to explicitly set the npoints of the filter.

  • cutoff (float (default=0.0001)) – If npoints=-1, the npoints of the filter is set dynamically, and increased until the smallest value of the filter is less than the cutoff value.

  • ndim (int) – Number of dimensions (default=2)

Returns

ndarray

Return type

2D array of filter values