4545]
4646
4747
48- def _validate_data_input (data : Any , kind : Kind , required_z : bool = False ) -> None :
48+ def _validate_data_input (data : Any , kind : Kind , ncols = 2 ) -> None :
4949 """
5050 Check if the data to be passed to the virtualfile_from_ functions is valid.
5151
@@ -67,7 +67,8 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
6767 Traceback (most recent call last):
6868 ...
6969 pygmt.exceptions.GMTInvalidInput: Must provide both x and y.
70- >>> _validate_data_input(data=[[1, 2, 3], [4, 5, 6]], kind="empty", required_z=True)
70+ >>> _validate_data_input(data=[[1, 2, 3], [4, 5, 6]], kind="empty", ncols=3)
71+ >>> _validate_data_input(x=[1, 2, 3], y=[4, 5, 6], ncols=3)
7172 Traceback (most recent call last):
7273 ...
7374 pygmt.exceptions.GMTInvalidInput: Must provide x, y, and z.
@@ -78,7 +79,7 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
7879 >>> import pandas as pd
7980 >>> import xarray as xr
8081 >>> data = np.arange(8).reshape((4, 2))
81- >>> _validate_data_input(data=data, kind="matrix", required_z=True )
82+ >>> _validate_data_input(data=data, ncols=3, kind="matrix")
8283 Traceback (most recent call last):
8384 ...
8485 pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
@@ -88,16 +89,16 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
8889
8990 >>> _validate_data_input(
9091 ... data=pd.DataFrame(data, columns=["x", "y"]),
92+ ... ncols=3,
9193 ... kind="vectors",
92- ... required_z=True,
9394 ... )
9495 Traceback (most recent call last):
9596 ...
9697 pygmt.exceptions.GMTInvalidInput: Need at least 3 columns but 2 column(s) are given.
9798 >>> _validate_data_input(
9899 ... data=xr.Dataset(pd.DataFrame(data, columns=["x", "y"])),
100+ ... ncols=3,
99101 ... kind="vectors",
100- ... required_z=True,
101102 ... )
102103 Traceback (most recent call last):
103104 ...
@@ -108,28 +109,25 @@ def _validate_data_input(data: Any, kind: Kind, required_z: bool = False) -> Non
108109 GMTInvalidInput
109110 If the data input is not valid.
110111 """
111- # Determine the required number of columns based on the required_z flag.
112- required_cols = 3 if required_z else 1
113-
114112 match kind :
115113 case "empty" : # data = [x, y], [x, y, z], [x, y, z, ...]
116114 if len (data ) < 2 or any (v is None for v in data [:2 ]):
117115 msg = "Must provide both x and y."
118116 raise GMTInvalidInput (msg )
119- if required_z and (len (data ) < 3 or data [:3 ] is None ):
117+ if ncols >= 3 and (len (data ) < 3 or data [:3 ] is None ):
120118 msg = "Must provide x, y, and z."
121119 raise GMTInvalidInput (msg )
122120 case "matrix" : # 2-D numpy.ndarray
123- if (actual_cols := data .shape [1 ]) < required_cols :
124- msg = f"Need at least { required_cols } columns but { actual_cols } column(s) are given."
121+ if (actual_cols := data .shape [1 ]) < ncols :
122+ msg = f"Need at least { ncols } columns but { actual_cols } column(s) are given."
125123 raise GMTInvalidInput (msg )
126124 case "vectors" :
127125 # "vectors" means the original data is either dictionary, list, tuple,
128126 # pandas.DataFrame, pandas.Series, xarray.Dataset, or xarray.DataArray.
129127 # The original data is converted to a list of vectors or a 2-D numpy.ndarray
130128 # in the virtualfile_in function.
131- if (actual_cols := len (data )) < required_cols :
132- msg = f"Need at least { required_cols } columns but { actual_cols } column(s) are given."
129+ if (actual_cols := len (data )) < ncols :
130+ msg = f"Need at least { ncols } columns but { actual_cols } column(s) are given."
133131 raise GMTInvalidInput (msg )
134132
135133
0 commit comments