|
| 1 | +""" |
| 2 | +dimfilter - Directional filtering of grids in the space domain. |
| 3 | +""" |
| 4 | + |
| 5 | +from pygmt.clib import Session |
| 6 | +from pygmt.exceptions import GMTInvalidInput |
| 7 | +from pygmt.helpers import ( |
| 8 | + GMTTempFile, |
| 9 | + build_arg_string, |
| 10 | + fmt_docstring, |
| 11 | + kwargs_to_strings, |
| 12 | + use_alias, |
| 13 | +) |
| 14 | +from pygmt.io import load_dataarray |
| 15 | + |
| 16 | + |
| 17 | +@fmt_docstring |
| 18 | +@use_alias( |
| 19 | + D="distance", |
| 20 | + F="filter", |
| 21 | + G="outgrid", |
| 22 | + I="spacing", |
| 23 | + N="sectors", |
| 24 | + R="region", |
| 25 | + V="verbose", |
| 26 | +) |
| 27 | +@kwargs_to_strings(I="sequence", R="sequence") |
| 28 | +def dimfilter(grid, **kwargs): |
| 29 | + r""" |
| 30 | + Filter a grid by dividing the filter circle. |
| 31 | +
|
| 32 | + Filter a grid in the space (or time) domain by |
| 33 | + dividing the given filter circle into the given number of sectors, |
| 34 | + applying one of the selected primary convolution or non-convolution |
| 35 | + filters to each sector, and choosing the final outcome according to the |
| 36 | + selected secondary filter. It computes distances using Cartesian or |
| 37 | + Spherical geometries. The output grid can optionally be generated as a |
| 38 | + subregion of the input and/or with a new increment using ``spacing``, |
| 39 | + which may add an "extra space" in the input data to prevent edge |
| 40 | + effects for the output grid. If the filter is low-pass, then the output |
| 41 | + may be less frequently sampled than the input. **dimfilter** will not |
| 42 | + produce a smooth output as other spatial filters |
| 43 | + do because it returns a minimum median out of *N* medians of *N* |
| 44 | + sectors. The output can be rough unless the input data is noise-free. |
| 45 | + Thus, an additional filtering (e.g., Gaussian via :func:`pygmt.grdfilter`) |
| 46 | + of the DiM-filtered data is generally recommended. |
| 47 | +
|
| 48 | + Full option list at :gmt-docs:`dimfilter.html` |
| 49 | +
|
| 50 | + {aliases} |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + grid : str or xarray.DataArray |
| 55 | + The file name of the input grid or the grid loaded as a DataArray. |
| 56 | + outgrid : str or None |
| 57 | + The name of the output netCDF file with extension .nc to store the grid |
| 58 | + in. |
| 59 | + distance : int or str |
| 60 | + Distance flag tells how grid (x,y) relates to filter width, as follows: |
| 61 | +
|
| 62 | + - **0**\ : grid (x,y) in same units as *width*, Cartesian distances. |
| 63 | + - **1**\ : grid (x,y) in degrees, *width* in kilometers, Cartesian |
| 64 | + distances. |
| 65 | + - **2**\ : grid (x,y) in degrees, *width* in km, dx scaled by |
| 66 | + cos(middle y), Cartesian distances. |
| 67 | +
|
| 68 | + The above options are fastest because they allow weight matrix to be |
| 69 | + computed only once. The next two options are slower because they |
| 70 | + recompute weights for each latitude. |
| 71 | +
|
| 72 | + - **3**\ : grid (x,y) in degrees, *width* in km, dx scaled by |
| 73 | + cosine(y), Cartesian distance calculation. |
| 74 | + - **4**\ : grid (x,y) in degrees, *width* in km, Spherical distance |
| 75 | + calculation. |
| 76 | + filter : str |
| 77 | + **x**\ *width*\ [**+l**\|\ **u**]. |
| 78 | + Sets the primary filter type. Choose among convolution and |
| 79 | + non-convolution filters. Use the filter code **x** followed by |
| 80 | + the full diameter *width*. Available convolution filters are: |
| 81 | +
|
| 82 | + - (**b**) Boxcar: All weights are equal. |
| 83 | + - (**c**) Cosine Arch: Weights follow a cosine arch curve. |
| 84 | + - (**g**) Gaussian: Weights are given by the Gaussian function. |
| 85 | +
|
| 86 | + Non-convolution filters are: |
| 87 | +
|
| 88 | + - (**m**) Median: Returns median value. |
| 89 | + - (**p**) Maximum likelihood probability (a mode estimator): Return |
| 90 | + modal value. If more than one mode is found we return their average |
| 91 | + value. Append **+l** or **+h** to the filter width if you want |
| 92 | + to return the smallest or largest of each sector's modal values. |
| 93 | + sectors : str |
| 94 | + **x**\ *sectors*\ [**+l**\|\ **u**] |
| 95 | + Sets the secondary filter type **x** and the number of bow-tie sectors. |
| 96 | + *sectors* must be integer and larger than 0. When *sectors* is |
| 97 | + set to 1, the secondary filter is not effective. Available secondary |
| 98 | + filters **x** are: |
| 99 | +
|
| 100 | + - (**l**) Lower: Return the minimum of all filtered values. |
| 101 | + - (**u**) Upper: Return the maximum of all filtered values. |
| 102 | + - (**a**) Average: Return the mean of all filtered values. |
| 103 | + - (**m**) Median: Return the median of all filtered values. |
| 104 | + - (**p**) Mode: Return the mode of all filtered values: |
| 105 | + If more than one mode is found we return their average |
| 106 | + value. Append **+l** or **+h** to the sectors if you rather want to |
| 107 | + return the smallest or largest of the modal values. |
| 108 | + spacing : str or list |
| 109 | + *x_inc* [and optionally *y_inc*] is the output Increment. Append |
| 110 | + **m** to indicate minutes, or **c** to indicate seconds. If the new |
| 111 | + *x_inc*, *y_inc* are NOT integer multiples of the old ones (in the |
| 112 | + input data), filtering will be considerably slower. [Default: Same |
| 113 | + as input.] |
| 114 | + region : str or list |
| 115 | + [*xmin*, *xmax*, *ymin*, *ymax*]. |
| 116 | + Defines the region of the output points. [Default: Same as input.] |
| 117 | + {V} |
| 118 | +
|
| 119 | + Returns |
| 120 | + ------- |
| 121 | + ret: xarray.DataArray or None |
| 122 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 123 | +
|
| 124 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 125 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 126 | + ``outgrid``) |
| 127 | + """ |
| 128 | + if not all(arg in kwargs for arg in ["D", "F", "N"]) and "Q" not in kwargs: |
| 129 | + raise GMTInvalidInput( |
| 130 | + """At least one of the following parameters must be specified: |
| 131 | + distance, filters, or sectors.""" |
| 132 | + ) |
| 133 | + |
| 134 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 135 | + with Session() as lib: |
| 136 | + file_context = lib.virtualfile_from_data(check_kind=None, data=grid) |
| 137 | + with file_context as infile: |
| 138 | + if "G" not in kwargs: # if outgrid is unset, output to tempfile |
| 139 | + kwargs.update({"G": tmpfile.name}) |
| 140 | + outgrid = kwargs["G"] |
| 141 | + arg_str = " ".join([infile, build_arg_string(kwargs)]) |
| 142 | + lib.call_module("dimfilter", arg_str) |
| 143 | + |
| 144 | + return load_dataarray(outgrid) if outgrid == tmpfile.name else None |
0 commit comments