From 884bde7b0d5da4f94263b173afe48eea80b2116b Mon Sep 17 00:00:00 2001 From: carocamargo Date: Thu, 17 Sep 2020 21:36:20 +0200 Subject: [PATCH 01/40] added gridfilter function --- pygmt/gridops.py | 92 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 7 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 122aa42340e..74823ab717f 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -20,13 +20,15 @@ @fmt_docstring @use_alias( - G="outgrid", - R="region", - J="projection", - N="extend", - S="circ_subregion", - Z="z_subregion", -) + G="outgrid", + R="region", + J="projection", + N="extend", + S="circ_subregion", + Z="z_subregion", + F="filter", + D="distance" + ) @kwargs_to_strings(R="sequence") def grdcut(grid, **kwargs): """ @@ -113,3 +115,79 @@ def grdcut(grid, **kwargs): result = None # if user sets an outgrid, return None return result + + +def grdfilter(grid, **kwargs): + """ + + filter a grid file in the time domain using one of the selected convolution + or non-convolution isotropic or rectangular filters and compute distances + using Cartesian or Spherical geometries. The output grid file can optionally + be generated as a sub-region of the input (via -R) and/or with new increment + (via -I) or registration (via -T). In this way, one may have “extra space” in + the input data so that the edges will not be used and the output can be within + one half-width of the input edges. If the filter is low-pass, then the output + may be less frequently sampled than the input. + + Parameters + ---------- + grid : str or xarray.DataArray + The file name of the input grid or the grid loaded as a DataArray. + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {F} : str + Name of filter type you which to apply, followed by the width + b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; p: Maximum Likelihood probability; h: histogram + {D}: str + Distance flag, that tells how grid (x,y) rrlated to the filter width as follows: + flag = p: grid (px,py) with width an odd number of pixels; Cartesian distances. + + flag = 0: grid (x,y) same units as width, Cartesian distances. + + flag = 1: grid (x,y) in degrees, width in kilometers, Cartesian distances. + + flag = 2: grid (x,y) in degrees, width in km, dx scaled by cos(middle y), Cartesian distances. + + The above options are fastest because they allow weight matrix to be computed only once. The next three options are slower because they recompute weights for each latitude. + + flag = 3: grid (x,y) in degrees, width in km, dx scaled by cosine(y), Cartesian distance calculation. + + flag = 4: grid (x,y) in degrees, width in km, Spherical distance calculation. + + flag = 5: grid (x,y) in Mercator -Jm1 img units, width in km, Spherical distance calculation. + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grdfilter", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result + From c2a7d3ed188ccc4e94f493c54d676ec9a56222f5 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Thu, 17 Sep 2020 21:37:10 +0200 Subject: [PATCH 02/40] added to call gridfilter from grdops.py --- pygmt/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 512865264b0..d031c76ab5a 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -19,7 +19,7 @@ from .sampling import grdtrack from .mathops import makecpt from .modules import GMTDataArrayAccessor, config, info, grdinfo, which -from .gridops import grdcut +from .gridops import grdcut, gridfilter from .x2sys import x2sys_init, x2sys_cross from . import datasets From 2cb715db3dd1d242cfb41a64d70f338a09b0f868 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Fri, 18 Sep 2020 09:44:10 +0200 Subject: [PATCH 03/40] added grdfilter caller --- pygmt/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index d031c76ab5a..4a38fe40ad6 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -19,7 +19,7 @@ from .sampling import grdtrack from .mathops import makecpt from .modules import GMTDataArrayAccessor, config, info, grdinfo, which -from .gridops import grdcut, gridfilter +from .gridops import grdcut, grdfilter from .x2sys import x2sys_init, x2sys_cross from . import datasets From 6139c1735cff7f7d615d243145c21b1efef3f2c6 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Fri, 18 Sep 2020 09:53:45 +0200 Subject: [PATCH 04/40] corrected indentation in grdfilter docstring, and made each function with each owns decorators --- pygmt/gridops.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 74823ab717f..4adca2f6e6b 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -25,9 +25,7 @@ J="projection", N="extend", S="circ_subregion", - Z="z_subregion", - F="filter", - D="distance" + Z="z_subregion" ) @kwargs_to_strings(R="sequence") def grdcut(grid, **kwargs): @@ -116,30 +114,36 @@ def grdcut(grid, **kwargs): return result +@fmt_docstring +@use_alias( + G="outgrid", + F="filter", + D="distance" + ) +@kwargs_to_strings(R="sequence") def grdfilter(grid, **kwargs): """ + filter a grid file in the time domain using one of the selected convolution + or non-convolution isotropic or rectangular filters and compute distances + using Cartesian or Spherical geometries. The output grid file can optionally + be generated as a sub-region of the input (via -R) and/or with new increment + (via -I) or registration (via -T). In this way, one may have “extra space” in + the input data so that the edges will not be used and the output can be within + one half-width of the input edges. If the filter is low-pass, then the output + may be less frequently sampled than the input. - filter a grid file in the time domain using one of the selected convolution - or non-convolution isotropic or rectangular filters and compute distances - using Cartesian or Spherical geometries. The output grid file can optionally - be generated as a sub-region of the input (via -R) and/or with new increment - (via -I) or registration (via -T). In this way, one may have “extra space” in - the input data so that the edges will not be used and the output can be within - one half-width of the input edges. If the filter is low-pass, then the output - may be less frequently sampled than the input. - - Parameters - ---------- - grid : str or xarray.DataArray + Parameters + ---------- + grid : str or xarray.DataArray The file name of the input grid or the grid loaded as a DataArray. outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. - {F} : str + {F} : str Name of filter type you which to apply, followed by the width b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; p: Maximum Likelihood probability; h: histogram - {D}: str + {D}: str Distance flag, that tells how grid (x,y) rrlated to the filter width as follows: flag = p: grid (px,py) with width an odd number of pixels; Cartesian distances. @@ -157,13 +161,13 @@ def grdfilter(grid, **kwargs): flag = 5: grid (x,y) in Mercator -Jm1 img units, width in km, Spherical distance calculation. - Returns - ------- - ret: xarray.DataArray or None + Returns + ------- + ret: xarray.DataArray or None Return type depends on whether the *outgrid* parameter is set: - xarray.DataArray if *outgrid* is not set - None if *outgrid* is set (grid output will be stored in *outgrid*) - """ + """ kind = data_kind(grid) with GMTTempFile(suffix=".nc") as tmpfile: From d2e0aee6be71955db622be77e7729150e3a9ff9a Mon Sep 17 00:00:00 2001 From: carocamargo Date: Fri, 18 Sep 2020 10:12:03 +0200 Subject: [PATCH 05/40] added usage example to grdfilter --- pygmt/gridops.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 4adca2f6e6b..6e5042205f2 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -143,6 +143,7 @@ def grdfilter(grid, **kwargs): {F} : str Name of filter type you which to apply, followed by the width b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; p: Maximum Likelihood probability; h: histogram + Example: 'm600' for a median filter with width of 600 {D}: str Distance flag, that tells how grid (x,y) rrlated to the filter width as follows: flag = p: grid (px,py) with width an odd number of pixels; Cartesian distances. @@ -167,6 +168,14 @@ def grdfilter(grid, **kwargs): Return type depends on whether the *outgrid* parameter is set: - xarray.DataArray if *outgrid* is not set - None if *outgrid* is set (grid output will be stored in *outgrid*) + + Usage + ------- + pygmt.grdfilter('/Users/Desktop/input.nc',F='m1600',D='4', G='/Users/Desktop/filtered_output.nc') + Applies a filter of 1600km (full width) in the input.nc and returns a a filtered filed (saved as netcdf) + + out=pygmt.grdfiler(dataarray,F='g600',D='4') + Applies a gaussian smoothing filter of 600 km in the input data array, and returns a filtered data array """ kind = data_kind(grid) From db2cdc7a388a42ccc4cffe2a9a73969921db7a5d Mon Sep 17 00:00:00 2001 From: carocamargo <47150926+carocamargo@users.noreply.github.com> Date: Fri, 18 Sep 2020 14:32:48 +0200 Subject: [PATCH 06/40] Update pygmt/gridops.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/gridops.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 6e5042205f2..c64be746036 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -20,13 +20,13 @@ @fmt_docstring @use_alias( - G="outgrid", - R="region", - J="projection", - N="extend", - S="circ_subregion", - Z="z_subregion" - ) + G="outgrid", + R="region", + J="projection", + N="extend", + S="circ_subregion", + Z="z_subregion", +) @kwargs_to_strings(R="sequence") def grdcut(grid, **kwargs): """ @@ -203,4 +203,3 @@ def grdfilter(grid, **kwargs): result = None # if user sets an outgrid, return None return result - From 8b8403967589584bebd1b618afb717d3360dc123 Mon Sep 17 00:00:00 2001 From: carocamargo <47150926+carocamargo@users.noreply.github.com> Date: Fri, 18 Sep 2020 14:41:53 +0200 Subject: [PATCH 07/40] Update pygmt/gridops.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/gridops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index c64be746036..14d9d5ddea2 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -127,8 +127,8 @@ def grdfilter(grid, **kwargs): filter a grid file in the time domain using one of the selected convolution or non-convolution isotropic or rectangular filters and compute distances using Cartesian or Spherical geometries. The output grid file can optionally - be generated as a sub-region of the input (via -R) and/or with new increment - (via -I) or registration (via -T). In this way, one may have “extra space” in + be generated as a sub-region of the input (via *region*) and/or with new increment + (via *spacing*) or registration (via *toggle*). In this way, one may have “extra space” in the input data so that the edges will not be used and the output can be within one half-width of the input edges. If the filter is low-pass, then the output may be less frequently sampled than the input. From a8dd55c71ba6101a79ae09ad874ab0770dc7b4a1 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Mon, 28 Sep 2020 10:47:07 +0200 Subject: [PATCH 08/40] added more optional aliases --- pygmt/gridops.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 6e5042205f2..9de0068519a 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -118,7 +118,13 @@ def grdcut(grid, **kwargs): @use_alias( G="outgrid", F="filter", - D="distance" + D="distance", + I="increment", + N="nans", + R="region", + T="toggle", + V="verbose", + f="colinfo" ) @kwargs_to_strings(R="sequence") @@ -161,6 +167,25 @@ def grdfilter(grid, **kwargs): flag = 4: grid (x,y) in degrees, width in km, Spherical distance calculation. flag = 5: grid (x,y) in Mercator -Jm1 img units, width in km, Spherical distance calculation. + + {I}: str + x_inc [and optionally y_inc] is the grid spacing. + (http://docs.generic-mapping-tools.org/latest/grdfilter.html#i) + {N}: Str or Number + Determine how NaN-values in the input grid affects the filtered output. + Values are i|p|r (http://docs.generic-mapping-tools.org/latest/grdfilter.html#n) + {R}: Str or list or GMTgrid + Specify the region of interest. Set to data minimum BoundinBox if not provided. + (http://docs.generic-mapping-tools.org/latest/gmt.html#r-full) + {T}: Bool + Toggle the node registration for the output grid so as to become the opposite of the input grid + (http://docs.generic-mapping-tools.org/latest/grdfilter.html#t) + {V}: Bool or Str + Select verbosity level, which will send progress reports to stderr. + (http://docs.generic-mapping-tools.org/latest/gmt.html#v-full) + {f}: Str + Specify the data types of input and/or output columns (time or geographical data). + (http://docs.generic-mapping-tools.org/latest/gmt.html#f-full) Returns ------- @@ -204,3 +229,4 @@ def grdfilter(grid, **kwargs): return result + From 9f47f01e97d1aab2ceb4b5ac8e88cd646a74a778 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Mon, 28 Sep 2020 11:06:30 +0200 Subject: [PATCH 09/40] added more optional aliases --- pygmt/gridops.py | 235 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 pygmt/gridops.py diff --git a/pygmt/gridops.py b/pygmt/gridops.py new file mode 100644 index 00000000000..eba2278acdb --- /dev/null +++ b/pygmt/gridops.py @@ -0,0 +1,235 @@ +""" +GMT modules for grid operations +""" + +import xarray as xr + + +from .clib import Session +from .helpers import ( + build_arg_string, + fmt_docstring, + kwargs_to_strings, + GMTTempFile, + use_alias, + data_kind, + dummy_context, +) +from .exceptions import GMTInvalidInput + + +@fmt_docstring +@use_alias( + G="outgrid", + R="region", + J="projection", + N="extend", + S="circ_subregion", + Z="z_subregion", +) +@kwargs_to_strings(R="sequence") +def grdcut(grid, **kwargs): + """ + Extract subregion from a grid. + + Produce a new *outgrid* file which is a subregion of *grid*. The + subregion is specified with *region*; the specified range must not exceed + the range of *grid* (but see *extend*). If in doubt, run + :meth:`pygmt.grdinfo` to check range. Alternatively, define the subregion + indirectly via a range check on the node values or via distances from a + given point. Finally, you can give *projection* for oblique projections to + determine the corresponding rectangular *region* setting that will give a + grid that fully covers the oblique domain. + + Full option list at :gmt-docs:`grdcut.html` + + {aliases} + + Parameters + ---------- + grid : str or xarray.DataArray + The file name of the input grid or the grid loaded as a DataArray. + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {J} + {R} + extend : bool or int or float + Allow grid to be extended if new *region* exceeds existing boundaries. + Give a value to initialize nodes outside current region. + circ_subregion : str + ``'lon/lat/radius[unit][+n]'``. + Specify an origin (*lon* and *lat*) and *radius*; append a distance + *unit* and we determine the corresponding rectangular region so that + all grid nodes on or inside the circle are contained in the subset. + If **+n** is appended we set all nodes outside the circle to NaN. + z_subregion : str + ``'[min/max][+n|N|r]'``. + Determine a new rectangular region so that all nodes outside this + region are also outside the given z-range [-inf/+inf]. To indicate no + limit on *min* or *max* only, specify a hyphen (-). Normally, any NaNs + encountered are simply skipped and not considered in the + range-decision. Append **+n** to consider a NaN to be outside the given + z-range. This means the new subset will be NaN-free. Alternatively, + append **+r** to consider NaNs to be within the data range. In this + case we stop shrinking the boundaries once a NaN is found [Default + simply skips NaNs when making the range decision]. Finally, if your + core subset grid is surrounded by rows and/or columns that are all + NaNs, append **+N** to strip off such columns before (optionally) + considering the range of the core subset for further reduction of the + area. + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grdcut", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result + +@fmt_docstring +@use_alias( + G="outgrid", + F="filter", + D="distance", + I="increment", + N="nans", + R="region", + T="toggle", + V="verbose", + f="colinfo" + ) +@kwargs_to_strings(R="sequence") + +def grdfilter(grid, **kwargs): + """ + filter a grid file in the time domain using one of the selected convolution + or non-convolution isotropic or rectangular filters and compute distances + using Cartesian or Spherical geometries. The output grid file can optionally + be generated as a sub-region of the input (via *region*) and/or with new increment + (via *spacing*) or registration (via *toggle*). In this way, one may have “extra space” in + the input data so that the edges will not be used and the output can be within + one half-width of the input edges. If the filter is low-pass, then the output + may be less frequently sampled than the input. + + Parameters + ---------- + grid : str or xarray.DataArray + The file name of the input grid or the grid loaded as a DataArray. + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {F} : str + Name of filter type you which to apply, followed by the width + b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; p: Maximum Likelihood probability; h: histogram + Example: 'm600' for a median filter with width of 600 + {D}: str + Distance flag, that tells how grid (x,y) rrlated to the filter width as follows: + flag = p: grid (px,py) with width an odd number of pixels; Cartesian distances. + + flag = 0: grid (x,y) same units as width, Cartesian distances. + + flag = 1: grid (x,y) in degrees, width in kilometers, Cartesian distances. + + flag = 2: grid (x,y) in degrees, width in km, dx scaled by cos(middle y), Cartesian distances. + + The above options are fastest because they allow weight matrix to be computed only once. The next three options are slower because they recompute weights for each latitude. + + flag = 3: grid (x,y) in degrees, width in km, dx scaled by cosine(y), Cartesian distance calculation. + + flag = 4: grid (x,y) in degrees, width in km, Spherical distance calculation. + + flag = 5: grid (x,y) in Mercator -Jm1 img units, width in km, Spherical distance calculation. + + {I}: str + x_inc [and optionally y_inc] is the grid spacing. + (http://docs.generic-mapping-tools.org/latest/grdfilter.html#i) + {N}: Str or Number + Determine how NaN-values in the input grid affects the filtered output. + Values are i|p|r (http://docs.generic-mapping-tools.org/latest/grdfilter.html#n) + {R}: Str or list or GMTgrid + Specify the region of interest. Set to data minimum BoundinBox if not provided. + (http://docs.generic-mapping-tools.org/latest/gmt.html#r-full) + {T}: Bool + Toggle the node registration for the output grid so as to become the opposite of the input grid + (http://docs.generic-mapping-tools.org/latest/grdfilter.html#t) + {V}: Bool or Str + Select verbosity level, which will send progress reports to stderr. + (http://docs.generic-mapping-tools.org/latest/gmt.html#v-full) + {f}: Str + Specify the data types of input and/or output columns (time or geographical data). + (http://docs.generic-mapping-tools.org/latest/gmt.html#f-full) + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + + Usage + ------- + pygmt.grdfilter('/Users/Desktop/input.nc',F='m1600',D='4', G='/Users/Desktop/filtered_output.nc') + Applies a filter of 1600km (full width) in the input.nc and returns a a filtered filed (saved as netcdf) + + out=pygmt.grdfiler(dataarray,F='g600',D='4') + Applies a gaussian smoothing filter of 600 km in the input data array, and returns a filtered data array + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grdfilter", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result +<<<<<<< HEAD + + +======= +>>>>>>> 8b8403967589584bebd1b618afb717d3360dc123 From 874bca4fdb85c665f2a39276089e63ed56d36c48 Mon Sep 17 00:00:00 2001 From: carocamargo <47150926+carocamargo@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:11:45 +0200 Subject: [PATCH 10/40] Update gridops.py removed header from local conflict --- pygmt/gridops.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index eba2278acdb..43fe4608fe6 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -228,8 +228,4 @@ def grdfilter(grid, **kwargs): result = None # if user sets an outgrid, return None return result -<<<<<<< HEAD - -======= ->>>>>>> 8b8403967589584bebd1b618afb717d3360dc123 From 0373c3247da840d375ff60592f4862f9e01b48ae Mon Sep 17 00:00:00 2001 From: carocamargo <47150926+carocamargo@users.noreply.github.com> Date: Tue, 29 Sep 2020 09:55:19 +0200 Subject: [PATCH 11/40] Update pygmt/gridops.py Co-authored-by: Dongdong Tian --- pygmt/gridops.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 43fe4608fe6..da4f70ad63c 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -127,7 +127,6 @@ def grdcut(grid, **kwargs): f="colinfo" ) @kwargs_to_strings(R="sequence") - def grdfilter(grid, **kwargs): """ filter a grid file in the time domain using one of the selected convolution @@ -228,4 +227,3 @@ def grdfilter(grid, **kwargs): result = None # if user sets an outgrid, return None return result - From fd64a4eb05acf7b26bbb9ef5545bf48e3c001f1b Mon Sep 17 00:00:00 2001 From: carocamargo <47150926+carocamargo@users.noreply.github.com> Date: Tue, 29 Sep 2020 09:55:27 +0200 Subject: [PATCH 12/40] Update pygmt/gridops.py Co-authored-by: Dongdong Tian --- pygmt/gridops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index da4f70ad63c..43ff913453d 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -145,7 +145,7 @@ def grdfilter(grid, **kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. - {F} : str + filter : str Name of filter type you which to apply, followed by the width b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; p: Maximum Likelihood probability; h: histogram Example: 'm600' for a median filter with width of 600 From df53508b85d3338bc64e10c253b399bc8bd6e4f6 Mon Sep 17 00:00:00 2001 From: carocamargo <47150926+carocamargo@users.noreply.github.com> Date: Tue, 29 Sep 2020 09:55:38 +0200 Subject: [PATCH 13/40] Update pygmt/gridops.py Co-authored-by: Dongdong Tian --- pygmt/gridops.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 43ff913453d..4c841269dd4 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -173,9 +173,7 @@ def grdfilter(grid, **kwargs): {N}: Str or Number Determine how NaN-values in the input grid affects the filtered output. Values are i|p|r (http://docs.generic-mapping-tools.org/latest/grdfilter.html#n) - {R}: Str or list or GMTgrid - Specify the region of interest. Set to data minimum BoundinBox if not provided. - (http://docs.generic-mapping-tools.org/latest/gmt.html#r-full) + {R} {T}: Bool Toggle the node registration for the output grid so as to become the opposite of the input grid (http://docs.generic-mapping-tools.org/latest/grdfilter.html#t) From 6fae1036bc5c741d2eccf1f72da250904f0c8ddc Mon Sep 17 00:00:00 2001 From: carocamargo Date: Tue, 29 Sep 2020 10:22:49 +0200 Subject: [PATCH 14/40] formated code with black --- pygmt/gridops.py | 78 +++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/pygmt/gridops.py b/pygmt/gridops.py index 4c841269dd4..d6eaf08ef76 100644 --- a/pygmt/gridops.py +++ b/pygmt/gridops.py @@ -114,18 +114,19 @@ def grdcut(grid, **kwargs): return result + @fmt_docstring @use_alias( - G="outgrid", - F="filter", - D="distance", - I="increment", - N="nans", - R="region", - T="toggle", - V="verbose", - f="colinfo" - ) + G="outgrid", + F="filter", + D="distance", + I="increment", + N="nans", + R="region", + T="toggle", + V="verbose", + f="colinfo", +) @kwargs_to_strings(R="sequence") def grdfilter(grid, **kwargs): """ @@ -133,11 +134,11 @@ def grdfilter(grid, **kwargs): or non-convolution isotropic or rectangular filters and compute distances using Cartesian or Spherical geometries. The output grid file can optionally be generated as a sub-region of the input (via *region*) and/or with new increment - (via *spacing*) or registration (via *toggle*). In this way, one may have “extra space” in - the input data so that the edges will not be used and the output can be within - one half-width of the input edges. If the filter is low-pass, then the output - may be less frequently sampled than the input. - + (via *spacing*) or registration (via *toggle*). + In this way, one may have “extra space” in the input data so that the edges + will not be used and the output can be within one half-width of the input edges. + If the filter is low-pass, then the output may be less frequently sampled than the input. + Parameters ---------- grid : str or xarray.DataArray @@ -147,26 +148,28 @@ def grdfilter(grid, **kwargs): in. filter : str Name of filter type you which to apply, followed by the width - b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; p: Maximum Likelihood probability; h: histogram - Example: 'm600' for a median filter with width of 600 + b: Box Car; c: Cosine Arch; g: Gaussian; o: Operator; m: Median; + p: Maximum Likelihood probability; h: histogram + Example: F='m600' for a median filter with width of 600 {D}: str Distance flag, that tells how grid (x,y) rrlated to the filter width as follows: flag = p: grid (px,py) with width an odd number of pixels; Cartesian distances. - + flag = 0: grid (x,y) same units as width, Cartesian distances. - + flag = 1: grid (x,y) in degrees, width in kilometers, Cartesian distances. - + flag = 2: grid (x,y) in degrees, width in km, dx scaled by cos(middle y), Cartesian distances. - - The above options are fastest because they allow weight matrix to be computed only once. The next three options are slower because they recompute weights for each latitude. - + + The above options are fastest because they allow weight matrix to be computed only once. + The next three options are slower because they recompute weights for each latitude. + flag = 3: grid (x,y) in degrees, width in km, dx scaled by cosine(y), Cartesian distance calculation. - + flag = 4: grid (x,y) in degrees, width in km, Spherical distance calculation. - + flag = 5: grid (x,y) in Mercator -Jm1 img units, width in km, Spherical distance calculation. - + {I}: str x_inc [and optionally y_inc] is the grid spacing. (http://docs.generic-mapping-tools.org/latest/grdfilter.html#i) @@ -183,24 +186,25 @@ def grdfilter(grid, **kwargs): {f}: Str Specify the data types of input and/or output columns (time or geographical data). (http://docs.generic-mapping-tools.org/latest/gmt.html#f-full) - + Returns ------- ret: xarray.DataArray or None Return type depends on whether the *outgrid* parameter is set: - xarray.DataArray if *outgrid* is not set - None if *outgrid* is set (grid output will be stored in *outgrid*) - + Usage ------- - pygmt.grdfilter('/Users/Desktop/input.nc',F='m1600',D='4', G='/Users/Desktop/filtered_output.nc') - Applies a filter of 1600km (full width) in the input.nc and returns a a filtered filed (saved as netcdf) - - out=pygmt.grdfiler(dataarray,F='g600',D='4') - Applies a gaussian smoothing filter of 600 km in the input data array, and returns a filtered data array + pygmt.grdfilter('input.nc',F='m1600',D='4', G='filtered_output.nc') + Applies a filter of 1600km (full width) in the input.nc and returns a a filtered field (saved as netcdf) + + smooth_field=pygmt.grdfiler(dataarray,F='g600',D='4') + Applies a gaussian smoothing filter of 600 km in the input data array, + and returns a filtered data array withthe smoothed field. """ kind = data_kind(grid) - + with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: if kind == "file": @@ -209,19 +213,19 @@ def grdfilter(grid, **kwargs): file_context = lib.virtualfile_from_grid(grid) else: raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) - + with file_context as infile: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile kwargs.update({"G": tmpfile.name}) outgrid = kwargs["G"] arg_str = " ".join([infile, build_arg_string(kwargs)]) lib.call_module("grdfilter", arg_str) - + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray with xr.open_dataarray(outgrid) as dataarray: result = dataarray.load() _ = result.gmt # load GMTDataArray accessor information else: result = None # if user sets an outgrid, return None - + return result From e653fa5eb054287d965c131ef0df0c617e62f500 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Tue, 29 Sep 2020 10:45:11 +0200 Subject: [PATCH 15/40] added xyz2grd function to gridding.py --- pygmt/gridding.py | 197 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) diff --git a/pygmt/gridding.py b/pygmt/gridding.py index 9ddaa2155c9..4a15a604191 100644 --- a/pygmt/gridding.py +++ b/pygmt/gridding.py @@ -95,3 +95,200 @@ def surface(x=None, y=None, z=None, data=None, **kwargs): result = None return result + + +@fmt_docstring +@use_alias( + G="outgrid", + I="increment", + R="region", + S="zfile", + V="level", + Z="flags", + bi="binary", + di="nodata", + f="flags", + h="headers", + i="flags", +) +@kwargs_to_strings(R="sequence") +def xyz2grd(grid, **kwargs): + """ + xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will + report if some of the nodes are not filled in with data. Such unconstrained nodes are set + to a value specified by the user [Default is NaN]. Nodes with more than one value will be + set to the mean value. As an option (using -Z), a 1-column z-table may be read assuming all + nodes are present (z-tables can be in organized in a number of formats, see -Z below.) + + http://gmt.soest.hawaii.edu/doc/5.3.2/xyz2grd.html + + Parameters + ---------- + grid : ascii file (xyz tables) + The file name of the input xyz file, with the extension: file.xyz + + {G}: str or None + The name of the output netCDF file with extension .nc to store the grid + in. If non is given, it will be the same name of the input file, just with different extensions. + + {I} : str + xinc[unit][=|+][/yinc[unit][=|+]] + x_inc [and optionally y_inc] is the grid spacing. Optionally, append a suffix modifier. + Geographical (degrees) coordinates: Append m to indicate arc minutes or s to indicate arc + seconds. If one of the units e, f, k, M, n or u is appended instead, the increment is + assumed to be given in meter, foot, km, Mile, nautical mile or US survey foot, + respectively, and will be converted to the equivalent degrees longitude at the middle + latitude of the region (the conversion depends on PROJ_ELLIPSOID). If y_inc is given but + set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees + latitude. All coordinates: If = is appended then the corresponding max x (east) or y + (north) may be slightly adjusted to fit exactly the given increment [by default the + increment may be adjusted slightly to fit the given domain]. Finally, instead of giving an + increment you may specify the number of nodes desired by appending + to the supplied + integer argument; the increment is then recalculated from the number of nodes and + the domain. The resulting increment value depends on whether you have selected a gri + dline-registered or pixel-registered grid; see GMT File Formats for details. Note: if + -Rgrdfile is used then the grid spacing has already been initialized; use -I to override + the values. + + {R}: str + [unit]xmin/xmax/ymin/ymax[r] (more ...) + Specify the region of interest. + + Optional Arguments + ------------------ + + table + One or more ASCII [or binary, see -bi] files holding z or (x,y,z) values. + The xyz triplets do not have to be sorted. + One-column z tables must be sorted and the -Z must be set. + + -A[f|l|m|n|r|s|u|z] + By default we will calculate mean values if multiple entries fall on the same node. + Use -A to change this behavior, except it is ignored if -Z is given. Append f or s + to simply keep the first or last data point that was assigned to each node. Append + l or u to find the lowest (minimum) or upper (maximum) value at each node, respectively. + Append m or r to compute mean or RMS value at each node, respectively. Append n to + simply count the number of data points that were assigned to each node (this only + requires two input columns x and y as z is not consulted). Append z to sum multiple + values that belong to the same node. + + -Dxname/yname/zname/scale/offset/invalid/title/remark + Give values for xname, yname, zname (give the names of those variables and in square + bracket their units, e.g., “distance [km]”), scale (to multiply grid values after read + [normally 1]), offset (to add to grid after scaling [normally 0]), invalid (a value to + represent missing data [NaN]), title (anything you like), and remark (anything you like). + To leave some of these values untouched, leave field blank. Empty fields in the end may + be skipped. Alternatively, to allow “/” to be part of one of the values, use any + non-alphanumeric character (and not the equal sign) as separator by both starting and + ending with it. For example: -D:xname:yname:zname:scale:offset:invalid:title:remark: + Use quotes to group texts with more than one word. Note that for geographic grids (-fg) + xname and yname are set automatically. + + -S[zfile] + Swap the byte-order of the input only. No grid file is produced. + You must also supply the -Z option. The output is written to zfile (or stdout if not supplied). + + -V[level] (more ...) + Select verbosity level [c]. + + -Z[flags] + Read a 1-column ASCII [or binary] table. This assumes that all the nodes are present + and sorted according to specified ordering convention contained in flags. If incoming + data represents rows, make flags start with T(op) if first row is y = ymax or B(ottom) + if first row is y = ymin. Then, append L or R to indicate that first element is at left + or right end of row. Likewise for column formats: start with L or R to position first + column, and then append T or B to position first element in a row. Note: These two + row/column indicators are only required for grids; for other tables they do not apply. + For gridline registered grids: If data are periodic in x but the incoming data do not + contain the (redundant) column at x = xmax, append x. For data periodic in y without + redundant row at y = ymax, append y. Append sn to skip the first n number of bytes + (probably a header). If the byte-order or the words needs to be swapped, append w. + Select one of several data types (all binary except a): + + A ASCII representation of one or more floating point values per record + + a ASCII representation of a single item per record + + c int8_t, signed 1-byte character + + u uint8_t, unsigned 1-byte character + + h int16_t, signed 2-byte integer + + H uint16_t, unsigned 2-byte integer + + i int32_t, signed 4-byte integer + + I uint32_t, unsigned 4-byte integer + + l int64_t, long (8-byte) integer + + L uint64_t, unsigned long (8-byte) integer + + f 4-byte floating point single precision + + d 8-byte floating point double precision + + Default format is scanline orientation of ASCII numbers: -ZTLa. + Note that -Z only applies to 1-column input. + The difference between A and a is that the latter can decode both dateTclock and + ddd:mm:ss[.xx] formats while the former is strictly for regular floating point values. + + -bi[ncols][t] (more ...) + Select native binary input. [Default is 3 input columns]. This option only applies + to xyz input files; see -Z for z tables. + + -dinodata (more ...) + Replace input columns that equal nodata with NaN. Also sets nodes with no input + xyz triplet to this value [Default is NaN]. + + -f[i|o]colinfo (more ...) + Specify data types of input and/or output columns. + + -h[i|o][n][+c][+d][+rremark][+rtitle] (more ...) + Skip or produce header record(s). Not used with binary data. + + -icols[l][sscale][ooffset][,...] (more ...) + Select input columns (0 is first column). + + -r (more ...) + Set pixel node registration [gridline]. + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + + Usage + ------- + pygmt.xyz2grd('file.xyz',G='file.nc',I='res',R='g') + + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".xyz") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("xyz2grd", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result From 3cfff68c8c80020697607c655b252accd3dbed88 Mon Sep 17 00:00:00 2001 From: carocamargo Date: Tue, 29 Sep 2020 10:45:55 +0200 Subject: [PATCH 16/40] modified __init__.py to import xyz2grd from gridding.py --- pygmt/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 4a38fe40ad6..2e0e2df930c 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -15,7 +15,7 @@ from .session_management import begin as _begin, end as _end from .figure import Figure from .filtering import blockmedian -from .gridding import surface +from .gridding import surface, xyz2grd from .sampling import grdtrack from .mathops import makecpt from .modules import GMTDataArrayAccessor, config, info, grdinfo, which From 162a3d763c94539f1b3805b40511834d074a24fa Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 17 Feb 2021 15:19:25 +1300 Subject: [PATCH 17/40] Move xyz2grd under pygmt/src and initialize test file --- pygmt/__init__.py | 1 + pygmt/src/__init__.py | 1 + pygmt/src/surface.py | 197 --------------------------------- pygmt/src/xyz2grd.py | 212 ++++++++++++++++++++++++++++++++++++ pygmt/tests/test_xyz2grd.py | 16 +++ 5 files changed, 230 insertions(+), 197 deletions(-) create mode 100644 pygmt/src/xyz2grd.py create mode 100644 pygmt/tests/test_xyz2grd.py diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 3afdf093e4f..b8e2fca3830 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -28,6 +28,7 @@ makecpt, surface, which, + xyz2grd, ) from pygmt.x2sys import x2sys_cross, x2sys_init diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 3ef9af36070..9276794a465 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -28,3 +28,4 @@ from pygmt.src.surface import surface from pygmt.src.text import text_ as text # "text" is an argument within "text_" from pygmt.src.which import which +from pygmt.src.xyz2grd import xyz2grd diff --git a/pygmt/src/surface.py b/pygmt/src/surface.py index 9564abdf3a1..24ba883c4e2 100644 --- a/pygmt/src/surface.py +++ b/pygmt/src/surface.py @@ -98,200 +98,3 @@ def surface(x=None, y=None, z=None, data=None, **kwargs): result = None return result - - -@fmt_docstring -@use_alias( - G="outgrid", - I="increment", - R="region", - S="zfile", - V="level", - Z="flags", - bi="binary", - di="nodata", - f="flags", - h="headers", - i="flags", -) -@kwargs_to_strings(R="sequence") -def xyz2grd(grid, **kwargs): - """ - xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will - report if some of the nodes are not filled in with data. Such unconstrained nodes are set - to a value specified by the user [Default is NaN]. Nodes with more than one value will be - set to the mean value. As an option (using -Z), a 1-column z-table may be read assuming all - nodes are present (z-tables can be in organized in a number of formats, see -Z below.) - - http://gmt.soest.hawaii.edu/doc/5.3.2/xyz2grd.html - - Parameters - ---------- - grid : ascii file (xyz tables) - The file name of the input xyz file, with the extension: file.xyz - - {G}: str or None - The name of the output netCDF file with extension .nc to store the grid - in. If non is given, it will be the same name of the input file, just with different extensions. - - {I} : str - xinc[unit][=|+][/yinc[unit][=|+]] - x_inc [and optionally y_inc] is the grid spacing. Optionally, append a suffix modifier. - Geographical (degrees) coordinates: Append m to indicate arc minutes or s to indicate arc - seconds. If one of the units e, f, k, M, n or u is appended instead, the increment is - assumed to be given in meter, foot, km, Mile, nautical mile or US survey foot, - respectively, and will be converted to the equivalent degrees longitude at the middle - latitude of the region (the conversion depends on PROJ_ELLIPSOID). If y_inc is given but - set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees - latitude. All coordinates: If = is appended then the corresponding max x (east) or y - (north) may be slightly adjusted to fit exactly the given increment [by default the - increment may be adjusted slightly to fit the given domain]. Finally, instead of giving an - increment you may specify the number of nodes desired by appending + to the supplied - integer argument; the increment is then recalculated from the number of nodes and - the domain. The resulting increment value depends on whether you have selected a gri - dline-registered or pixel-registered grid; see GMT File Formats for details. Note: if - -Rgrdfile is used then the grid spacing has already been initialized; use -I to override - the values. - - {R}: str - [unit]xmin/xmax/ymin/ymax[r] (more ...) - Specify the region of interest. - - Optional Arguments - ------------------ - - table - One or more ASCII [or binary, see -bi] files holding z or (x,y,z) values. - The xyz triplets do not have to be sorted. - One-column z tables must be sorted and the -Z must be set. - - -A[f|l|m|n|r|s|u|z] - By default we will calculate mean values if multiple entries fall on the same node. - Use -A to change this behavior, except it is ignored if -Z is given. Append f or s - to simply keep the first or last data point that was assigned to each node. Append - l or u to find the lowest (minimum) or upper (maximum) value at each node, respectively. - Append m or r to compute mean or RMS value at each node, respectively. Append n to - simply count the number of data points that were assigned to each node (this only - requires two input columns x and y as z is not consulted). Append z to sum multiple - values that belong to the same node. - - -Dxname/yname/zname/scale/offset/invalid/title/remark - Give values for xname, yname, zname (give the names of those variables and in square - bracket their units, e.g., “distance [km]”), scale (to multiply grid values after read - [normally 1]), offset (to add to grid after scaling [normally 0]), invalid (a value to - represent missing data [NaN]), title (anything you like), and remark (anything you like). - To leave some of these values untouched, leave field blank. Empty fields in the end may - be skipped. Alternatively, to allow “/” to be part of one of the values, use any - non-alphanumeric character (and not the equal sign) as separator by both starting and - ending with it. For example: -D:xname:yname:zname:scale:offset:invalid:title:remark: - Use quotes to group texts with more than one word. Note that for geographic grids (-fg) - xname and yname are set automatically. - - -S[zfile] - Swap the byte-order of the input only. No grid file is produced. - You must also supply the -Z option. The output is written to zfile (or stdout if not supplied). - - -V[level] (more ...) - Select verbosity level [c]. - - -Z[flags] - Read a 1-column ASCII [or binary] table. This assumes that all the nodes are present - and sorted according to specified ordering convention contained in flags. If incoming - data represents rows, make flags start with T(op) if first row is y = ymax or B(ottom) - if first row is y = ymin. Then, append L or R to indicate that first element is at left - or right end of row. Likewise for column formats: start with L or R to position first - column, and then append T or B to position first element in a row. Note: These two - row/column indicators are only required for grids; for other tables they do not apply. - For gridline registered grids: If data are periodic in x but the incoming data do not - contain the (redundant) column at x = xmax, append x. For data periodic in y without - redundant row at y = ymax, append y. Append sn to skip the first n number of bytes - (probably a header). If the byte-order or the words needs to be swapped, append w. - Select one of several data types (all binary except a): - - A ASCII representation of one or more floating point values per record - - a ASCII representation of a single item per record - - c int8_t, signed 1-byte character - - u uint8_t, unsigned 1-byte character - - h int16_t, signed 2-byte integer - - H uint16_t, unsigned 2-byte integer - - i int32_t, signed 4-byte integer - - I uint32_t, unsigned 4-byte integer - - l int64_t, long (8-byte) integer - - L uint64_t, unsigned long (8-byte) integer - - f 4-byte floating point single precision - - d 8-byte floating point double precision - - Default format is scanline orientation of ASCII numbers: -ZTLa. - Note that -Z only applies to 1-column input. - The difference between A and a is that the latter can decode both dateTclock and - ddd:mm:ss[.xx] formats while the former is strictly for regular floating point values. - - -bi[ncols][t] (more ...) - Select native binary input. [Default is 3 input columns]. This option only applies - to xyz input files; see -Z for z tables. - - -dinodata (more ...) - Replace input columns that equal nodata with NaN. Also sets nodes with no input - xyz triplet to this value [Default is NaN]. - - -f[i|o]colinfo (more ...) - Specify data types of input and/or output columns. - - -h[i|o][n][+c][+d][+rremark][+rtitle] (more ...) - Skip or produce header record(s). Not used with binary data. - - -icols[l][sscale][ooffset][,...] (more ...) - Select input columns (0 is first column). - - -r (more ...) - Set pixel node registration [gridline]. - - Returns - ------- - ret: xarray.DataArray or None - Return type depends on whether the *outgrid* parameter is set: - - xarray.DataArray if *outgrid* is not set - - None if *outgrid* is set (grid output will be stored in *outgrid*) - - Usage - ------- - pygmt.xyz2grd('file.xyz',G='file.nc',I='res',R='g') - - """ - kind = data_kind(grid) - - with GMTTempFile(suffix=".xyz") as tmpfile: - with Session() as lib: - if kind == "file": - file_context = dummy_context(grid) - elif kind == "grid": - file_context = lib.virtualfile_from_grid(grid) - else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) - - with file_context as infile: - if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] - arg_str = " ".join([infile, build_arg_string(kwargs)]) - lib.call_module("xyz2grd", arg_str) - - if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray - with xr.open_dataarray(outgrid) as dataarray: - result = dataarray.load() - _ = result.gmt # load GMTDataArray accessor information - else: - result = None # if user sets an outgrid, return None - - return result diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py new file mode 100644 index 00000000000..56765306c32 --- /dev/null +++ b/pygmt/src/xyz2grd.py @@ -0,0 +1,212 @@ +""" +xyz2grd - Convert data table to a grid. +""" +import xarray as xr +from pygmt.clib import Session +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import ( + GMTTempFile, + build_arg_string, + data_kind, + dummy_context, + fmt_docstring, + kwargs_to_strings, + use_alias, +) + + +@fmt_docstring +@use_alias( + G="outgrid", + I="spacing", + R="region", + S="zfile", + V="level", + Z="flags", + bi="binary", + di="nodata", + f="flags", + h="headers", + i="flags", +) +@kwargs_to_strings(R="sequence") +def xyz2grd(grid, **kwargs): + """ + xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will + report if some of the nodes are not filled in with data. Such unconstrained nodes are set + to a value specified by the user [Default is NaN]. Nodes with more than one value will be + set to the mean value. As an option (using -Z), a 1-column z-table may be read assuming all + nodes are present (z-tables can be in organized in a number of formats, see -Z below.) + + http://gmt.soest.hawaii.edu/doc/5.3.2/xyz2grd.html + + Parameters + ---------- + grid : ascii file (xyz tables) + The file name of the input xyz file, with the extension: file.xyz + + {G}: str or None + The name of the output netCDF file with extension .nc to store the grid + in. If non is given, it will be the same name of the input file, just with different extensions. + + I : str + xinc[unit][=|+][/yinc[unit][=|+]] + x_inc [and optionally y_inc] is the grid spacing. Optionally, append a suffix modifier. + Geographical (degrees) coordinates: Append m to indicate arc minutes or s to indicate arc + seconds. If one of the units e, f, k, M, n or u is appended instead, the increment is + assumed to be given in meter, foot, km, Mile, nautical mile or US survey foot, + respectively, and will be converted to the equivalent degrees longitude at the middle + latitude of the region (the conversion depends on PROJ_ELLIPSOID). If y_inc is given but + set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees + latitude. All coordinates: If = is appended then the corresponding max x (east) or y + (north) may be slightly adjusted to fit exactly the given increment [by default the + increment may be adjusted slightly to fit the given domain]. Finally, instead of giving an + increment you may specify the number of nodes desired by appending + to the supplied + integer argument; the increment is then recalculated from the number of nodes and + the domain. The resulting increment value depends on whether you have selected a gri + dline-registered or pixel-registered grid; see GMT File Formats for details. Note: if + -Rgrdfile is used then the grid spacing has already been initialized; use -I to override + the values. + + {R}: str + [unit]xmin/xmax/ymin/ymax[r] (more ...) + Specify the region of interest. + + Optional Arguments + ------------------ + + table + One or more ASCII [or binary, see -bi] files holding z or (x,y,z) values. + The xyz triplets do not have to be sorted. + One-column z tables must be sorted and the -Z must be set. + + -A[f|l|m|n|r|s|u|z] + By default we will calculate mean values if multiple entries fall on the same node. + Use -A to change this behavior, except it is ignored if -Z is given. Append f or s + to simply keep the first or last data point that was assigned to each node. Append + l or u to find the lowest (minimum) or upper (maximum) value at each node, respectively. + Append m or r to compute mean or RMS value at each node, respectively. Append n to + simply count the number of data points that were assigned to each node (this only + requires two input columns x and y as z is not consulted). Append z to sum multiple + values that belong to the same node. + + -Dxname/yname/zname/scale/offset/invalid/title/remark + Give values for xname, yname, zname (give the names of those variables and in square + bracket their units, e.g., “distance [km]”), scale (to multiply grid values after read + [normally 1]), offset (to add to grid after scaling [normally 0]), invalid (a value to + represent missing data [NaN]), title (anything you like), and remark (anything you like). + To leave some of these values untouched, leave field blank. Empty fields in the end may + be skipped. Alternatively, to allow “/” to be part of one of the values, use any + non-alphanumeric character (and not the equal sign) as separator by both starting and + ending with it. For example: -D:xname:yname:zname:scale:offset:invalid:title:remark: + Use quotes to group texts with more than one word. Note that for geographic grids (-fg) + xname and yname are set automatically. + + -S[zfile] + Swap the byte-order of the input only. No grid file is produced. + You must also supply the -Z option. The output is written to zfile (or stdout if not supplied). + + -V[level] (more ...) + Select verbosity level [c]. + + -Z[flags] + Read a 1-column ASCII [or binary] table. This assumes that all the nodes are present + and sorted according to specified ordering convention contained in flags. If incoming + data represents rows, make flags start with T(op) if first row is y = ymax or B(ottom) + if first row is y = ymin. Then, append L or R to indicate that first element is at left + or right end of row. Likewise for column formats: start with L or R to position first + column, and then append T or B to position first element in a row. Note: These two + row/column indicators are only required for grids; for other tables they do not apply. + For gridline registered grids: If data are periodic in x but the incoming data do not + contain the (redundant) column at x = xmax, append x. For data periodic in y without + redundant row at y = ymax, append y. Append sn to skip the first n number of bytes + (probably a header). If the byte-order or the words needs to be swapped, append w. + Select one of several data types (all binary except a): + + A ASCII representation of one or more floating point values per record + + a ASCII representation of a single item per record + + c int8_t, signed 1-byte character + + u uint8_t, unsigned 1-byte character + + h int16_t, signed 2-byte integer + + H uint16_t, unsigned 2-byte integer + + i int32_t, signed 4-byte integer + + I uint32_t, unsigned 4-byte integer + + l int64_t, long (8-byte) integer + + L uint64_t, unsigned long (8-byte) integer + + f 4-byte floating point single precision + + d 8-byte floating point double precision + + Default format is scanline orientation of ASCII numbers: -ZTLa. + Note that -Z only applies to 1-column input. + The difference between A and a is that the latter can decode both dateTclock and + ddd:mm:ss[.xx] formats while the former is strictly for regular floating point values. + + -bi[ncols][t] (more ...) + Select native binary input. [Default is 3 input columns]. This option only applies + to xyz input files; see -Z for z tables. + + -dinodata (more ...) + Replace input columns that equal nodata with NaN. Also sets nodes with no input + xyz triplet to this value [Default is NaN]. + + -f[i|o]colinfo (more ...) + Specify data types of input and/or output columns. + + -h[i|o][n][+c][+d][+rremark][+rtitle] (more ...) + Skip or produce header record(s). Not used with binary data. + + -icols[l][sscale][ooffset][,...] (more ...) + Select input columns (0 is first column). + + -r (more ...) + Set pixel node registration [gridline]. + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + + Usage + ------- + pygmt.xyz2grd('file.xyz',G='file.nc',I='res',R='g') + + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".xyz") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + file_context = lib.virtualfile_from_grid(grid) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("xyz2grd", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py new file mode 100644 index 00000000000..4fab3081063 --- /dev/null +++ b/pygmt/tests/test_xyz2grd.py @@ -0,0 +1,16 @@ +""" +Tests for xyz2grd. +""" +import xarray as xr +from pygmt import xyz2grd + + +def test_xyz2grd_input_file(): + """ + Run xyz2grd by passing in a filename. + """ + output = xyz2grd("@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30]) + assert isinstance(output, xr.DataArray) + assert output.gmt.registration == 0 # Gridline registration + assert output.gmt.gtype == 0 # Cartesian type + return output From 208c3769a4c8ef6de58253c7f356079366f61aa8 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 07:54:53 +0100 Subject: [PATCH 18/40] change GMT doc link --- pygmt/src/xyz2grd.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 56765306c32..67046ff2e79 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -32,13 +32,16 @@ @kwargs_to_strings(R="sequence") def xyz2grd(grid, **kwargs): """ - xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will - report if some of the nodes are not filled in with data. Such unconstrained nodes are set - to a value specified by the user [Default is NaN]. Nodes with more than one value will be - set to the mean value. As an option (using -Z), a 1-column z-table may be read assuming all - nodes are present (z-tables can be in organized in a number of formats, see -Z below.) + xyz2grd reads one or more z or xyz tables and creates a binary grid file. + xyz2grd will report if some of the nodes are not filled in with data. Such + unconstrained nodes are set to a value specified by the user [Default is + NaN]. Nodes with more than one value will be set to the mean value. As an + option (using -Z), a 1-column z-table may be read assuming all nodes are + present (z-tables can be in organized in a number of formats, see -Z + below.) + + Full option list at :gmt-docs:`xyz2grd.html` - http://gmt.soest.hawaii.edu/doc/5.3.2/xyz2grd.html Parameters ---------- @@ -47,7 +50,8 @@ def xyz2grd(grid, **kwargs): {G}: str or None The name of the output netCDF file with extension .nc to store the grid - in. If non is given, it will be the same name of the input file, just with different extensions. + in. If non is given, it will be the same name of the input file, just with + different extensions. I : str xinc[unit][=|+][/yinc[unit][=|+]] @@ -182,7 +186,6 @@ def xyz2grd(grid, **kwargs): Usage ------- pygmt.xyz2grd('file.xyz',G='file.nc',I='res',R='g') - """ kind = data_kind(grid) From 74ca8a75cd5f1b1380a80ed4cf4108f6732fa1fd Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:03:47 +0100 Subject: [PATCH 19/40] rename Z and i parameters --- pygmt/src/xyz2grd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 67046ff2e79..0e0e913b39c 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -22,12 +22,12 @@ R="region", S="zfile", V="level", - Z="flags", + Z="single_column", bi="binary", di="nodata", f="flags", h="headers", - i="flags", + i="select_column", ) @kwargs_to_strings(R="sequence") def xyz2grd(grid, **kwargs): From 67efb669fd40ed2353a90092d318ba8164955382 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:08:15 +0100 Subject: [PATCH 20/40] change grid to table --- pygmt/src/xyz2grd.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 0e0e913b39c..9ed68758333 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -30,7 +30,7 @@ i="select_column", ) @kwargs_to_strings(R="sequence") -def xyz2grd(grid, **kwargs): +def xyz2grd(table, **kwargs): """ xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will report if some of the nodes are not filled in with data. Such @@ -45,7 +45,7 @@ def xyz2grd(grid, **kwargs): Parameters ---------- - grid : ascii file (xyz tables) + table : ascii file (xyz tables) The file name of the input xyz file, with the extension: file.xyz {G}: str or None @@ -187,22 +187,23 @@ def xyz2grd(grid, **kwargs): ------- pygmt.xyz2grd('file.xyz',G='file.nc',I='res',R='g') """ - kind = data_kind(grid) + kind = data_kind(table) with GMTTempFile(suffix=".xyz") as tmpfile: with Session() as lib: if kind == "file": - file_context = dummy_context(grid) - elif kind == "grid": - file_context = lib.virtualfile_from_grid(grid) + file_context = dummy_context(table) + elif kind == "matrix": + file_context = lib.virtualfile_from_matrix(matrix=table) else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + raise GMTInvalidInput("Unrecognized data type: {}".format(type(table))) with file_context as infile: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile kwargs.update({"G": tmpfile.name}) outgrid = kwargs["G"] - arg_str = " ".join([infile, build_arg_string(kwargs)]) + arg_str = build_arg_string(kwargs) + arg_str = " ".join([infile, arg_str]) lib.call_module("xyz2grd", arg_str) if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray From 6b1e49d926bab168e29b67baabc8458aaa5bdf78 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:16:35 +0100 Subject: [PATCH 21/40] add pytest fixture --- pygmt/tests/test_xyz2grd.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 4fab3081063..25b69eedeb4 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -1,9 +1,17 @@ """ Tests for xyz2grd. """ +import pytest import xarray as xr +from pygmt.datasets import load_sample_bathymetry from pygmt import xyz2grd +@pytest.fixture(scope="module", name="ship_data") +def fixture_ship_data(): + """ + Load the data from the sample bathymetry dataset. + """ + return load_sample_bathymetry() def test_xyz2grd_input_file(): """ From 549e1e1b5dd369dada5889a64678e937f950954e Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:18:35 +0100 Subject: [PATCH 22/40] add xyz2grd to index.rst --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 40ab49ac430..536c9ced9d3 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -95,6 +95,7 @@ Operations on grids: grdlandmask grdgradient grdtrack + xyz2grd Crossover analysis with x2sys: From 63e23325b80736136daa8f01fb89ca78604a0f97 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:22:10 +0100 Subject: [PATCH 23/40] reorganize import in test_xyz2grd.py --- pygmt/tests/test_xyz2grd.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 25b69eedeb4..0294ea0c6fe 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -3,8 +3,9 @@ """ import pytest import xarray as xr -from pygmt.datasets import load_sample_bathymetry from pygmt import xyz2grd +from pygmt.datasets import load_sample_bathymetry + @pytest.fixture(scope="module", name="ship_data") def fixture_ship_data(): @@ -13,6 +14,7 @@ def fixture_ship_data(): """ return load_sample_bathymetry() + def test_xyz2grd_input_file(): """ Run xyz2grd by passing in a filename. From 711ac4aceb7a639840f974f8fb372f245cc0540b Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:28:00 +0100 Subject: [PATCH 24/40] update docstring --- pygmt/src/xyz2grd.py | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 9ed68758333..2ca0abec95a 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -45,36 +45,15 @@ def xyz2grd(table, **kwargs): Parameters ---------- - table : ascii file (xyz tables) - The file name of the input xyz file, with the extension: file.xyz - - {G}: str or None - The name of the output netCDF file with extension .nc to store the grid - in. If non is given, it will be the same name of the input file, just with - different extensions. - - I : str - xinc[unit][=|+][/yinc[unit][=|+]] - x_inc [and optionally y_inc] is the grid spacing. Optionally, append a suffix modifier. - Geographical (degrees) coordinates: Append m to indicate arc minutes or s to indicate arc - seconds. If one of the units e, f, k, M, n or u is appended instead, the increment is - assumed to be given in meter, foot, km, Mile, nautical mile or US survey foot, - respectively, and will be converted to the equivalent degrees longitude at the middle - latitude of the region (the conversion depends on PROJ_ELLIPSOID). If y_inc is given but - set to 0 it will be reset equal to x_inc; otherwise it will be converted to degrees - latitude. All coordinates: If = is appended then the corresponding max x (east) or y - (north) may be slightly adjusted to fit exactly the given increment [by default the - increment may be adjusted slightly to fit the given domain]. Finally, instead of giving an - increment you may specify the number of nodes desired by appending + to the supplied - integer argument; the increment is then recalculated from the number of nodes and - the domain. The resulting increment value depends on whether you have selected a gri - dline-registered or pixel-registered grid; see GMT File Formats for details. Note: if - -Rgrdfile is used then the grid spacing has already been initialized; use -I to override - the values. - - {R}: str - [unit]xmin/xmax/ymin/ymax[r] (more ...) - Specify the region of interest. + table : str or {table-like} + Pass in either a file name to an ASCII data table, a 1D/2D + {table-classes}. + + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {I} + {R} Optional Arguments ------------------ From b150564da163c19dbbfd1ab03fc19cdbb8c94535 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 24 Jul 2021 15:29:41 +0100 Subject: [PATCH 25/40] remove "Optional Arguments" sub-header --- pygmt/src/xyz2grd.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 2ca0abec95a..1edd81bcb97 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -54,15 +54,6 @@ def xyz2grd(table, **kwargs): in. {I} {R} - - Optional Arguments - ------------------ - - table - One or more ASCII [or binary, see -bi] files holding z or (x,y,z) values. - The xyz triplets do not have to be sorted. - One-column z tables must be sorted and the -Z must be set. - -A[f|l|m|n|r|s|u|z] By default we will calculate mean values if multiple entries fall on the same node. Use -A to change this behavior, except it is ignored if -Z is given. Append f or s From 456eb1b724115a26f96b3d18b7329cf8aaf92ddd Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 27 Jul 2021 17:32:08 +0100 Subject: [PATCH 26/40] add single-line docstring --- pygmt/src/xyz2grd.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 1edd81bcb97..6ec2bf6bd85 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -32,6 +32,8 @@ @kwargs_to_strings(R="sequence") def xyz2grd(table, **kwargs): """ + Create a grid file from table data. + xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will report if some of the nodes are not filled in with data. Such unconstrained nodes are set to a value specified by the user [Default is From 34c3d1ef421d2f0d94e1f000cd8a71ce33e28e27 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 31 Jul 2021 06:27:56 +0100 Subject: [PATCH 27/40] update docstring for xyz2grd.py --- pygmt/src/xyz2grd.py | 121 ++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 58 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 6ec2bf6bd85..cc0be89e759 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -57,84 +57,93 @@ def xyz2grd(table, **kwargs): {I} {R} -A[f|l|m|n|r|s|u|z] - By default we will calculate mean values if multiple entries fall on the same node. - Use -A to change this behavior, except it is ignored if -Z is given. Append f or s - to simply keep the first or last data point that was assigned to each node. Append - l or u to find the lowest (minimum) or upper (maximum) value at each node, respectively. - Append m or r to compute mean or RMS value at each node, respectively. Append n to - simply count the number of data points that were assigned to each node (this only - requires two input columns x and y as z is not consulted). Append z to sum multiple - values that belong to the same node. + By default we will calculate mean values if multiple entries fall on + the same node. Use **-A** to change this behavior, except it is + ignored if **-Z** is given. Append **f** or **s** to simply keep the + first or last data point that was assigned to each node. Append + **l** or **u** or **d** to find the lowest (minimum) or upper (maximum) + value or the difference between the maximum and miminum value at each + node, respectively. Append **m** or **r** or **S** to compute mean or + RMS value or standard deviation at each node, respectively. Append **n** + to simply count the number of data points that were assigned to each node + (this only requires two input columns *x* and *y* as *z* is not consulted). + Append **z** to sum multiple values that belong to the same node. -Dxname/yname/zname/scale/offset/invalid/title/remark - Give values for xname, yname, zname (give the names of those variables and in square - bracket their units, e.g., “distance [km]”), scale (to multiply grid values after read - [normally 1]), offset (to add to grid after scaling [normally 0]), invalid (a value to - represent missing data [NaN]), title (anything you like), and remark (anything you like). - To leave some of these values untouched, leave field blank. Empty fields in the end may - be skipped. Alternatively, to allow “/” to be part of one of the values, use any - non-alphanumeric character (and not the equal sign) as separator by both starting and - ending with it. For example: -D:xname:yname:zname:scale:offset:invalid:title:remark: - Use quotes to group texts with more than one word. Note that for geographic grids (-fg) - xname and yname are set automatically. + Give values for xname, yname, zname (give the names of those variables and + in square bracket their units, e.g., “distance [km]”), scale (to multiply + grid values after read [normally 1]), offset (to add to grid after + scaling [normally 0]), invalid (a value to represent missing data [NaN]), + title (anything you like), and remark (anything you like). To leave some + of these values untouched, leave field blank. Empty fields in the end may + be skipped. Alternatively, to allow “/” to be part of one of the values, + use any non-alphanumeric character (and not the equal sign) as separator + by both starting and ending with it. -S[zfile] Swap the byte-order of the input only. No grid file is produced. - You must also supply the -Z option. The output is written to zfile (or stdout if not supplied). - - -V[level] (more ...) - Select verbosity level [c]. + You must also supply the -Z option. The output is written to zfile + (or stdout if not supplied). + {V} -Z[flags] - Read a 1-column ASCII [or binary] table. This assumes that all the nodes are present - and sorted according to specified ordering convention contained in flags. If incoming - data represents rows, make flags start with T(op) if first row is y = ymax or B(ottom) - if first row is y = ymin. Then, append L or R to indicate that first element is at left - or right end of row. Likewise for column formats: start with L or R to position first - column, and then append T or B to position first element in a row. Note: These two - row/column indicators are only required for grids; for other tables they do not apply. - For gridline registered grids: If data are periodic in x but the incoming data do not - contain the (redundant) column at x = xmax, append x. For data periodic in y without - redundant row at y = ymax, append y. Append sn to skip the first n number of bytes - (probably a header). If the byte-order or the words needs to be swapped, append w. - Select one of several data types (all binary except a): + Read a 1-column ASCII [or binary] table. This assumes that all the + nodes are present and sorted according to specified ordering + convention contained in *flags*. If incoming data represents rows, + make *flags* start with **T**\ (op) if first row is y + = ymax or **B**\ (ottom) if first row is y = ymin. + Then, append **L** or **R** to indicate that first element is at + left or right end of row. Likewise for column formats: start with + **L** or **R** to position first column, and then append **T** or + **B** to position first element in a row. **Note**: These two row/column + indicators are only required for grids; for other tables they do not + apply. For gridline registered grids: If data are periodic in x but + the incoming data do not contain the (redundant) column at x = xmax, + append **x**. For data periodic in y without redundant row at y = + ymax, append **y**. Append **s**\ *n* to skip the first *n* number + of bytes (probably a header). If the byte-order or the words needs + to be swapped, append **w**. Select one of several data types (all + binary except **a**): - A ASCII representation of one or more floating point values per record + **A** ASCII representation of one or more floating point values per record - a ASCII representation of a single item per record + **a** ASCII representation of a single item per record - c int8_t, signed 1-byte character + **c** int8_t, signed 1-byte character - u uint8_t, unsigned 1-byte character + **u** uint8_t, unsigned 1-byte character - h int16_t, signed 2-byte integer + **h** int16_t, signed 2-byte integer - H uint16_t, unsigned 2-byte integer + **H** uint16_t, unsigned 2-byte integer - i int32_t, signed 4-byte integer + **i** int32_t, signed 4-byte integer - I uint32_t, unsigned 4-byte integer + **I** uint32_t, unsigned 4-byte integer - l int64_t, long (8-byte) integer + **l** int64_t, long (8-byte) integer - L uint64_t, unsigned long (8-byte) integer + **L** uint64_t, unsigned long (8-byte) integer - f 4-byte floating point single precision + **f** 4-byte floating point single precision - d 8-byte floating point double precision + **d** 8-byte floating point double precision - Default format is scanline orientation of ASCII numbers: -ZTLa. - Note that -Z only applies to 1-column input. - The difference between A and a is that the latter can decode both dateTclock and - ddd:mm:ss[.xx] formats while the former is strictly for regular floating point values. + Default format is scanline orientation of ASCII numbers: **TLa**. + The difference between **A** and **a** is that the latter can decode both + *date*\ **T**\ *clock* and *ddd:mm:ss[.xx]* formats but expects each + input record to have a single value, while the former can handle multiple + values per record but can only parse regular floating point values. + Translate incoming *z*-values via the **-i**\ 0 option and needed + modifiers. -bi[ncols][t] (more ...) - Select native binary input. [Default is 3 input columns]. This option only applies - to xyz input files; see -Z for z tables. + Select native binary input. [Default is 3 input columns]. This option only + applies to xyz input files. -dinodata (more ...) - Replace input columns that equal nodata with NaN. Also sets nodes with no input - xyz triplet to this value [Default is NaN]. + Replace input columns that equal nodata with NaN. Also sets nodes with no + input xyz triplet to this value [Default is NaN]. -f[i|o]colinfo (more ...) Specify data types of input and/or output columns. @@ -154,10 +163,6 @@ def xyz2grd(table, **kwargs): Return type depends on whether the *outgrid* parameter is set: - xarray.DataArray if *outgrid* is not set - None if *outgrid* is set (grid output will be stored in *outgrid*) - - Usage - ------- - pygmt.xyz2grd('file.xyz',G='file.nc',I='res',R='g') """ kind = data_kind(table) From 5cd1a0f8fd794fa400f5d4ab634b224a99ad3167 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 31 Jul 2021 06:57:18 +0100 Subject: [PATCH 28/40] change file suffic for GMTTempFile --- pygmt/src/xyz2grd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index cc0be89e759..b23b0fd270b 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -166,7 +166,7 @@ def xyz2grd(table, **kwargs): """ kind = data_kind(table) - with GMTTempFile(suffix=".xyz") as tmpfile: + with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: if kind == "file": file_context = dummy_context(table) From 3b241a6e13478c66332a9b63688008bc3b819bde Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 31 Jul 2021 06:57:28 +0100 Subject: [PATCH 29/40] add parameter name in test_xyz2grd.py --- pygmt/tests/test_xyz2grd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 0294ea0c6fe..664b4bfd9b4 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -19,8 +19,8 @@ def test_xyz2grd_input_file(): """ Run xyz2grd by passing in a filename. """ - output = xyz2grd("@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30]) + output = xyz2grd(table="@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30]) assert isinstance(output, xr.DataArray) assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type - return output + return output \ No newline at end of file From 6d62c8f7b8928d272b9c204a2ca27a1c8914a502 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 15:24:50 +0100 Subject: [PATCH 30/40] updating test_xyz2grd.py --- pygmt/tests/test_xyz2grd.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 664b4bfd9b4..01ed0156091 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -5,6 +5,7 @@ import xarray as xr from pygmt import xyz2grd from pygmt.datasets import load_sample_bathymetry +import numpy as np @pytest.fixture(scope="module", name="ship_data") @@ -23,4 +24,24 @@ def test_xyz2grd_input_file(): assert isinstance(output, xr.DataArray) assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type + return output + +def test_xyz2grd_input_array(ship_data): + """ + Run xyz2grd by passing in a numpy array. + """ + output = xyz2grd(table=np.array(ship_data), spacing=5, region=[245, 255, 20, 30]) + assert isinstance(output, xr.DataArray) + assert output.gmt.registration == 0 # Gridline registration + assert output.gmt.gtype == 0 # Cartesian type + return output + +def test_xyz2grd_input_df(ship_data): + """ + Run xyz2grd by passing in a data frame. + """ + output = xyz2grd(table=ship_data, spacing=5, region=[245, 255, 20, 30]) + assert isinstance(output, xr.DataArray) + assert output.gmt.registration == 0 # Gridline registration + assert output.gmt.gtype == 0 # Cartesian type return output \ No newline at end of file From 2c374504e4e1adf119ac2134457a400f6cce5c87 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 15:38:50 +0100 Subject: [PATCH 31/40] update xyz2grd.py and test_xyz2grd.py to accept dataframe --- pygmt/src/xyz2grd.py | 8 +------- pygmt/tests/test_xyz2grd.py | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index b23b0fd270b..4d2d252a95d 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -168,13 +168,7 @@ def xyz2grd(table, **kwargs): with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: - if kind == "file": - file_context = dummy_context(table) - elif kind == "matrix": - file_context = lib.virtualfile_from_matrix(matrix=table) - else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(table))) - + file_context = lib.virtualfile_from_data(check_kind="vector", data=table) with file_context as infile: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile kwargs.update({"G": tmpfile.name}) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 01ed0156091..f231a1e9ab0 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -3,7 +3,7 @@ """ import pytest import xarray as xr -from pygmt import xyz2grd +from pygmt import xyz2grd, grdinfo from pygmt.datasets import load_sample_bathymetry import numpy as np @@ -44,4 +44,4 @@ def test_xyz2grd_input_df(ship_data): assert isinstance(output, xr.DataArray) assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type - return output \ No newline at end of file + return output From 97ece78e8f6fe82984c3da2136e94920a1da919b Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 15:47:17 +0100 Subject: [PATCH 32/40] add outgrid set test to test_xyz2grd.py --- pygmt/tests/test_xyz2grd.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index f231a1e9ab0..5da00c102a0 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -1,11 +1,14 @@ """ Tests for xyz2grd. """ +import os + +import numpy as np import pytest import xarray as xr -from pygmt import xyz2grd, grdinfo +from pygmt import grdinfo, xyz2grd from pygmt.datasets import load_sample_bathymetry -import numpy as np +from pygmt.helpers import GMTTempFile @pytest.fixture(scope="module", name="ship_data") @@ -26,6 +29,7 @@ def test_xyz2grd_input_file(): assert output.gmt.gtype == 0 # Cartesian type return output + def test_xyz2grd_input_array(ship_data): """ Run xyz2grd by passing in a numpy array. @@ -36,6 +40,7 @@ def test_xyz2grd_input_array(ship_data): assert output.gmt.gtype == 0 # Cartesian type return output + def test_xyz2grd_input_df(ship_data): """ Run xyz2grd by passing in a data frame. @@ -45,3 +50,21 @@ def test_xyz2grd_input_df(ship_data): assert output.gmt.registration == 0 # Gridline registration assert output.gmt.gtype == 0 # Cartesian type return output + + +def test_xyz2grd_input_array_file_out(ship_data): + """ + Run xyz2grd by passing in a numpy array and set an outgrid file. + """ + with GMTTempFile(suffix=".nc") as tmpfile: + result = xyz2grd( + table=np.array(ship_data), + spacing=5, + region=[245, 255, 20, 30], + outgrid=tmpfile.name, + ) + assert result is None # return value is None + assert os.path.exists(path=tmpfile.name) + result = grdinfo(tmpfile.name, per_column=True).strip() + print(result) + assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0" From 3e5f7df664bfceccf961c3fe8c6c6861c08a328d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 15:48:26 +0100 Subject: [PATCH 33/40] remove kind check in xyz2grd.py --- pygmt/src/xyz2grd.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 4d2d252a95d..7e87ee54d0b 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -164,8 +164,6 @@ def xyz2grd(table, **kwargs): - xarray.DataArray if *outgrid* is not set - None if *outgrid* is set (grid output will be stored in *outgrid*) """ - kind = data_kind(table) - with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="vector", data=table) From d92ec3c4962c2ae72ec7df8d7b904eba757b5550 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 16:01:53 +0100 Subject: [PATCH 34/40] remove optional parameters --- pygmt/src/xyz2grd.py | 111 +------------------------------------------ 1 file changed, 2 insertions(+), 109 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 7e87ee54d0b..69d4db5d669 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -20,14 +20,7 @@ G="outgrid", I="spacing", R="region", - S="zfile", - V="level", - Z="single_column", - bi="binary", - di="nodata", - f="flags", - h="headers", - i="select_column", + V="verbose", ) @kwargs_to_strings(R="sequence") def xyz2grd(table, **kwargs): @@ -56,107 +49,7 @@ def xyz2grd(table, **kwargs): in. {I} {R} - -A[f|l|m|n|r|s|u|z] - By default we will calculate mean values if multiple entries fall on - the same node. Use **-A** to change this behavior, except it is - ignored if **-Z** is given. Append **f** or **s** to simply keep the - first or last data point that was assigned to each node. Append - **l** or **u** or **d** to find the lowest (minimum) or upper (maximum) - value or the difference between the maximum and miminum value at each - node, respectively. Append **m** or **r** or **S** to compute mean or - RMS value or standard deviation at each node, respectively. Append **n** - to simply count the number of data points that were assigned to each node - (this only requires two input columns *x* and *y* as *z* is not consulted). - Append **z** to sum multiple values that belong to the same node. - - -Dxname/yname/zname/scale/offset/invalid/title/remark - Give values for xname, yname, zname (give the names of those variables and - in square bracket their units, e.g., “distance [km]”), scale (to multiply - grid values after read [normally 1]), offset (to add to grid after - scaling [normally 0]), invalid (a value to represent missing data [NaN]), - title (anything you like), and remark (anything you like). To leave some - of these values untouched, leave field blank. Empty fields in the end may - be skipped. Alternatively, to allow “/” to be part of one of the values, - use any non-alphanumeric character (and not the equal sign) as separator - by both starting and ending with it. - - -S[zfile] - Swap the byte-order of the input only. No grid file is produced. - You must also supply the -Z option. The output is written to zfile - (or stdout if not supplied). - {V} - - -Z[flags] - Read a 1-column ASCII [or binary] table. This assumes that all the - nodes are present and sorted according to specified ordering - convention contained in *flags*. If incoming data represents rows, - make *flags* start with **T**\ (op) if first row is y - = ymax or **B**\ (ottom) if first row is y = ymin. - Then, append **L** or **R** to indicate that first element is at - left or right end of row. Likewise for column formats: start with - **L** or **R** to position first column, and then append **T** or - **B** to position first element in a row. **Note**: These two row/column - indicators are only required for grids; for other tables they do not - apply. For gridline registered grids: If data are periodic in x but - the incoming data do not contain the (redundant) column at x = xmax, - append **x**. For data periodic in y without redundant row at y = - ymax, append **y**. Append **s**\ *n* to skip the first *n* number - of bytes (probably a header). If the byte-order or the words needs - to be swapped, append **w**. Select one of several data types (all - binary except **a**): - - **A** ASCII representation of one or more floating point values per record - - **a** ASCII representation of a single item per record - - **c** int8_t, signed 1-byte character - - **u** uint8_t, unsigned 1-byte character - - **h** int16_t, signed 2-byte integer - - **H** uint16_t, unsigned 2-byte integer - - **i** int32_t, signed 4-byte integer - - **I** uint32_t, unsigned 4-byte integer - - **l** int64_t, long (8-byte) integer - - **L** uint64_t, unsigned long (8-byte) integer - - **f** 4-byte floating point single precision - - **d** 8-byte floating point double precision - - Default format is scanline orientation of ASCII numbers: **TLa**. - The difference between **A** and **a** is that the latter can decode both - *date*\ **T**\ *clock* and *ddd:mm:ss[.xx]* formats but expects each - input record to have a single value, while the former can handle multiple - values per record but can only parse regular floating point values. - Translate incoming *z*-values via the **-i**\ 0 option and needed - modifiers. - - -bi[ncols][t] (more ...) - Select native binary input. [Default is 3 input columns]. This option only - applies to xyz input files. - - -dinodata (more ...) - Replace input columns that equal nodata with NaN. Also sets nodes with no - input xyz triplet to this value [Default is NaN]. - - -f[i|o]colinfo (more ...) - Specify data types of input and/or output columns. - - -h[i|o][n][+c][+d][+rremark][+rtitle] (more ...) - Skip or produce header record(s). Not used with binary data. - - -icols[l][sscale][ooffset][,...] (more ...) - Select input columns (0 is first column). - - -r (more ...) - Set pixel node registration [gridline]. - + {v} Returns ------- ret: xarray.DataArray or None From 99d81f67ebeb784a508f35dc8a80ea7bcf726eff Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 16:06:19 +0100 Subject: [PATCH 35/40] fix doc error --- pygmt/src/xyz2grd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 69d4db5d669..f5203023228 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -49,7 +49,7 @@ def xyz2grd(table, **kwargs): in. {I} {R} - {v} + {V} Returns ------- ret: xarray.DataArray or None From 567d8213daff4fd5f0a97f31d59ea2a3de1cf885 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 5 Aug 2021 16:17:10 +0100 Subject: [PATCH 36/40] remove unused imports --- pygmt/src/xyz2grd.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index f5203023228..7e8e303c8da 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -3,12 +3,9 @@ """ import xarray as xr from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( GMTTempFile, build_arg_string, - data_kind, - dummy_context, fmt_docstring, kwargs_to_strings, use_alias, From e47516560498bdc33f307e454c008b993e46ccd6 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 7 Aug 2021 07:47:22 +0100 Subject: [PATCH 37/40] remove print statement --- pygmt/tests/test_xyz2grd.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 5da00c102a0..530c6b0905a 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -66,5 +66,4 @@ def test_xyz2grd_input_array_file_out(ship_data): assert result is None # return value is None assert os.path.exists(path=tmpfile.name) result = grdinfo(tmpfile.name, per_column=True).strip() - print(result) assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0" From 38adbb27745016ce5b99ed4ea8999b49f6083059 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 7 Aug 2021 07:52:37 +0100 Subject: [PATCH 38/40] shorten docstring --- pygmt/src/xyz2grd.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 7e8e303c8da..29c3cc2bcfb 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -27,10 +27,7 @@ def xyz2grd(table, **kwargs): xyz2grd reads one or more z or xyz tables and creates a binary grid file. xyz2grd will report if some of the nodes are not filled in with data. Such unconstrained nodes are set to a value specified by the user [Default is - NaN]. Nodes with more than one value will be set to the mean value. As an - option (using -Z), a 1-column z-table may be read assuming all nodes are - present (z-tables can be in organized in a number of formats, see -Z - below.) + NaN]. Nodes with more than one value will be set to the mean value. Full option list at :gmt-docs:`xyz2grd.html` From cae8e042c35c0c17cb0c039c65124a7513ef31c3 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 8 Aug 2021 18:42:35 +0100 Subject: [PATCH 39/40] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/xyz2grd.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 29c3cc2bcfb..3ff13377da5 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -31,7 +31,6 @@ def xyz2grd(table, **kwargs): Full option list at :gmt-docs:`xyz2grd.html` - Parameters ---------- table : str or {table-like} @@ -39,17 +38,20 @@ def xyz2grd(table, **kwargs): {table-classes}. outgrid : str or None - The name of the output netCDF file with extension .nc to store the grid - in. + Optional. The name of the output netCDF file with extension .nc to + store the grid in. {I} {R} {V} + Returns ------- ret: xarray.DataArray or None - Return type depends on whether the *outgrid* parameter is set: - - xarray.DataArray if *outgrid* is not set - - None if *outgrid* is set (grid output will be stored in *outgrid*) + Return type depends on whether the ``outfile`` parameter is set: + + - :class:`xarray.DataArray`: if ``outfile`` is not set + - None if ``outfile`` is set (grid output will be stored in file set by + ``outfile``)``` """ with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: From 10f24747e34d317a321c18cdb68f475030821f10 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 9 Aug 2021 09:44:08 +1200 Subject: [PATCH 40/40] Update pygmt/src/xyz2grd.py --- pygmt/src/xyz2grd.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 3ff13377da5..ef40abc889d 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -47,11 +47,11 @@ def xyz2grd(table, **kwargs): Returns ------- ret: xarray.DataArray or None - Return type depends on whether the ``outfile`` parameter is set: + Return type depends on whether the ``outgrid`` parameter is set: - - :class:`xarray.DataArray`: if ``outfile`` is not set - - None if ``outfile`` is set (grid output will be stored in file set by - ``outfile``)``` + - :class:`xarray.DataArray`: if ``outgrid`` is not set + - None if ``outgrid`` is set (grid output will be stored in file set by + ``outgrid``)``` """ with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: