|
| 1 | +""" |
| 2 | +binstats - Bin spatial data and determine statistics per bin |
| 3 | +""" |
| 4 | +from pygmt.clib import Session |
| 5 | +from pygmt.helpers import ( |
| 6 | + GMTTempFile, |
| 7 | + build_arg_string, |
| 8 | + fmt_docstring, |
| 9 | + kwargs_to_strings, |
| 10 | + use_alias, |
| 11 | +) |
| 12 | +from pygmt.io import load_dataarray |
| 13 | + |
| 14 | + |
| 15 | +@fmt_docstring |
| 16 | +@use_alias( |
| 17 | + C="statistic", |
| 18 | + E="empty", |
| 19 | + G="outgrid", |
| 20 | + I="spacing", |
| 21 | + N="normalize", |
| 22 | + R="region", |
| 23 | + S="search_radius", |
| 24 | + V="verbose", |
| 25 | + W="weight", |
| 26 | + a="aspatial", |
| 27 | + b="binary", |
| 28 | + h="header", |
| 29 | + i="incols", |
| 30 | + r="registration", |
| 31 | +) |
| 32 | +@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma") |
| 33 | +def binstats(data, **kwargs): |
| 34 | + r""" |
| 35 | + Bin spatial data and determine statistics per bin. |
| 36 | +
|
| 37 | + Reads arbitrarily located (x,y[,z][,w]) points |
| 38 | + (2-4 columns) from ``data`` and for each |
| 39 | + node in the specified grid layout determines which points are |
| 40 | + within the given radius. These point are then used in the |
| 41 | + calculation of the specified statistic. The results may be |
| 42 | + presented as is or may be normalized by the circle area to |
| 43 | + perhaps give density estimates. |
| 44 | +
|
| 45 | + Full option list at :gmt-docs:`gmtbinstats.html` |
| 46 | +
|
| 47 | + {aliases} |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + data : str or {table-like} |
| 52 | + A file name of an ASCII data table or a 2D |
| 53 | + {table-classes}. |
| 54 | + outgrid : str or None |
| 55 | + The name of the output netCDF file with extension .nc to store the grid |
| 56 | + in. |
| 57 | + statistic : str |
| 58 | + **a**\|\ **d**\|\ **g**\|\ **i**\|\ **l**\|\ **L**\|\ **m**\|\ **n**\ |
| 59 | + \|\ **o**\|\ **p**\|\ **q**\ [*quant*]\|\ **r**\|\ **s**\|\ **u**\ |
| 60 | + \|\ **U**\|\ **z**. |
| 61 | + Choose the statistic that will be computed per node based on the |
| 62 | + points that are within *radius* distance of the node. Select one of: |
| 63 | +
|
| 64 | + - **a** for mean (average) |
| 65 | + - **d** for median absolute deviation (MAD) |
| 66 | + - **g** for full (max-min) range |
| 67 | + - **i** for 25-75% interquartile range |
| 68 | + - **l** for minimum (low) |
| 69 | + - **L** for minimum of positive values only |
| 70 | + - **m** for median |
| 71 | + - **n** the number of values |
| 72 | + - **o** for LMS scale |
| 73 | + - **p** for mode (maximum likelihood) |
| 74 | + - **q** for selected quantile (append desired quantile in |
| 75 | + 0-100% range [50]) |
| 76 | + - **r** for the r.m.s. |
| 77 | + - **s** for standard deviation |
| 78 | + - **u** for maximum (upper) |
| 79 | + - **U** for maximum of negative values only |
| 80 | + - **z** for the sum |
| 81 | + empty : float or int |
| 82 | + Set the value assigned to empty nodes [Default is NaN]. |
| 83 | + normalize : bool |
| 84 | + Normalize the resulting grid values by the area represented by the |
| 85 | + search *radius* [no normalization]. |
| 86 | + search_radius : float or str |
| 87 | + Sets the *search_radius* that determines which data points are |
| 88 | + considered close to a node. Append the distance unit. |
| 89 | + Not compatible with ``tiling``. |
| 90 | + weight : str |
| 91 | + Input data have an extra column containing observation point weight. |
| 92 | + If weights are given then weighted statistical quantities will be |
| 93 | + computed while the count will be the sum of the weights instead of |
| 94 | + number of points. If the weights are actually uncertainties |
| 95 | + (one sigma) then append **+s** and weight = 1/sigma. |
| 96 | + {I} |
| 97 | + {R} |
| 98 | + {V} |
| 99 | + {a} |
| 100 | + {b} |
| 101 | + {h} |
| 102 | + {i} |
| 103 | + {r} |
| 104 | +
|
| 105 | + Returns |
| 106 | + ------- |
| 107 | + ret: xarray.DataArray or None |
| 108 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 109 | +
|
| 110 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 111 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 112 | + ``outgrid``) |
| 113 | + """ |
| 114 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 115 | + with Session() as lib: |
| 116 | + file_context = lib.virtualfile_from_data(check_kind="vector", data=data) |
| 117 | + with file_context as infile: |
| 118 | + if (outgrid := kwargs.get("G")) is None: |
| 119 | + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile |
| 120 | + lib.call_module( |
| 121 | + module="binstats", args=build_arg_string(kwargs, infile=infile) |
| 122 | + ) |
| 123 | + |
| 124 | + return load_dataarray(outgrid) if outgrid == tmpfile.name else None |
0 commit comments