15
15
from pathlib import Path
16
16
from typing import Any , Literal
17
17
18
- import numpy as np
19
18
import xarray as xr
20
19
from pygmt .encodings import charset
21
20
from pygmt .exceptions import GMTInvalidInput
46
45
]
47
46
48
47
49
- def _validate_data_input (
50
- data = None ,
51
- x = None ,
52
- y = None ,
53
- z = None ,
54
- required_z : bool = False ,
55
- required_data : bool = True ,
56
- kind : Kind | None = None ,
57
- ) -> None :
48
+ def _validate_data_input (data : Any , kind : Kind , required_z : bool = False ) -> None :
58
49
"""
59
50
Check if the combination of data/x/y/z is valid.
60
51
@@ -126,29 +117,18 @@ def _validate_data_input(
126
117
GMTInvalidInput
127
118
If the data input is not valid.
128
119
"""
129
- # Check if too much data is provided.
130
- if data is not None and any (v is not None for v in (x , y , z )):
131
- msg = "Too much data. Use either data or x/y/z."
132
- raise GMTInvalidInput (msg )
133
-
134
- # Determine the data kind if not provided.
135
- kind = kind or data_kind (data , required = required_data )
136
-
137
120
# Determine the required number of columns based on the required_z flag.
138
121
required_cols = 3 if required_z else 1
139
122
140
123
# Check based on the data kind.
141
124
match kind :
142
- case "empty" : # data is given via a series vectors like x/y/z.
143
- if x is None and y is None :
144
- msg = "No input data provided ."
125
+ case "empty" : # data = [x, y, z]
126
+ if required_z and len ( data ) < 3 :
127
+ msg = "Must provide x, y, and z ."
145
128
raise GMTInvalidInput (msg )
146
- if x is None or y is None :
129
+ if any ( v is None for v in data ) :
147
130
msg = "Must provide both x and y."
148
131
raise GMTInvalidInput (msg )
149
- if required_z and z is None :
150
- msg = "Must provide x, y, and z."
151
- raise GMTInvalidInput (msg )
152
132
case "matrix" : # 2-D numpy.ndarray
153
133
if (actual_cols := data .shape [1 ]) < required_cols :
154
134
msg = (
@@ -157,16 +137,8 @@ def _validate_data_input(
157
137
)
158
138
raise GMTInvalidInput (msg )
159
139
case "vectors" :
160
- # The if-else block should match the codes in the virtualfile_in function.
161
- if hasattr (data , "items" ) and not hasattr (data , "to_frame" ):
162
- # Dict, pandas.DataFrame, or xarray.Dataset, but not pd.Series.
163
- _data = [array for _ , array in data .items ()]
164
- else :
165
- # Python list, tuple, numpy.ndarray, and pandas.Series types
166
- _data = np .atleast_2d (np .asanyarray (data ).T )
167
-
168
140
# Check if the number of columns is sufficient.
169
- if (actual_cols := len (_data )) < required_cols :
141
+ if (actual_cols := len (data )) < required_cols :
170
142
msg = (
171
143
f"Need at least { required_cols } columns but { actual_cols } "
172
144
"column(s) are given."
0 commit comments