Skip to content

Commit 879eab9

Browse files
committed
Exposes variable CDF type
Signed-off-by: Alexis Jeandet <[email protected]>
1 parent 7606cac commit 879eab9

7 files changed

+29
-7
lines changed

pyistp/_impl.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ def _get_axis(master_cdf: Driver, cdf: Driver, axis_var: str, data_var: str):
3939
if 'sig_digits' in master_cdf.variable_attributes(axis_var): # cluster CSA trick :/
4040
return SupportDataVariable(name=axis_var, values=np.asarray(cdf.values(axis_var), dtype=float),
4141
attributes=_get_attributes(master_cdf, cdf, axis_var),
42-
is_nrv=cdf.is_nrv(axis_var)
42+
is_nrv=cdf.is_nrv(axis_var),
43+
cdf_type=cdf.cdf_type(axis_var)
4344
)
4445
return SupportDataVariable(name=axis_var, values=cdf.values(axis_var),
4546
attributes=_get_attributes(master_cdf, cdf, axis_var),
46-
is_nrv=cdf.is_nrv(axis_var)
47+
is_nrv=cdf.is_nrv(axis_var),
48+
cdf_type=cdf.cdf_type(axis_var)
4749
)
4850
else:
4951
log.warning(
@@ -78,13 +80,14 @@ def _load_data_var(master_cdf: Driver, cdf: Driver, var: str) -> DataVariable or
7880
shape = cdf.shape(var)
7981
axes = _get_axes(master_cdf, cdf, var, shape)
8082
attributes = _get_attributes(master_cdf, cdf, var)
83+
cdf_type = cdf.cdf_type(var)
8184
labels = _get_labels(attributes)
8285
if len(axes) == 0:
8386
log.warning(f"{ISTP_NOT_COMPLIANT_W}: {var} was marked as data variable but it has 0 support variable")
8487
return None
8588
if None in axes or axes[0].values.shape[0] != shape[0]:
8689
return None
87-
return DataVariable(name=var, values=values, attributes=attributes, axes=axes, labels=labels)
90+
return DataVariable(name=var, values=values, attributes=attributes, axes=axes, cdf_type=cdf_type, labels=labels)
8891

8992

9093
class ISTPLoaderImpl:

pyistp/data_variable.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33

44
class DataVariable:
5-
__slots__ = ("name", "_values", 'attributes', 'axes', 'labels')
5+
__slots__ = ("name", "_values", 'attributes', 'axes', 'labels', 'cdf_type')
66

7-
def __init__(self, name: str, values: Union[Collection[Any], Callable], attributes, axes, labels=None):
7+
def __init__(self, name: str, values: Union[Collection[Any], Callable], attributes, axes, cdf_type, labels=None):
88
self.name = name
99
self._values = values
1010
self.attributes = attributes
1111
self.axes = axes or []
1212
self.labels = labels
13+
self.cdf_type = cdf_type
1314

1415
@property
1516
def values(self):

pyistp/drivers/_driver.py

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def data_variables(self) -> List[AnyStr]:
1515
def data_variable(self, var_name: AnyStr):
1616
...
1717

18+
def cdf_type(self, var_name: AnyStr) -> AnyStr:
19+
...
20+
1821
def variable_attribute_value(self, var: AnyStr, attr: AnyStr) -> Any:
1922
...
2023

pyistp/drivers/pycdfpp.py

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def variables(self):
3131
return list(self.cdf)
3232
return []
3333

34+
def cdf_type(self, var_name):
35+
return self.cdf[var_name].type.name
36+
3437
def has_variable(self, name):
3538
return name in self.cdf
3639

pyistp/drivers/spacepy.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def variables(self):
2828
return self.cdf.keys()
2929
return []
3030

31+
def cdf_type(self, var_name):
32+
return pycdf.lib.cdftypenames[self.cdf[var_name].type()]
33+
3134
def has_variable(self, name):
3235
return name in self.cdf
3336

pyistp/support_data_variable.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33

44
class SupportDataVariable:
5-
__slots__ = ("name", "values", 'attributes', 'is_nrv')
5+
__slots__ = ("name", "values", 'attributes', 'is_nrv', 'cdf_type')
66

7-
def __init__(self, name, values, attributes, is_nrv):
7+
def __init__(self, name, values, attributes, is_nrv, cdf_type):
88
self.name = name
99
self.values = values
1010
if type(self.values) is list:
1111
self.values = np.array(self.values)
1212
self.attributes = attributes
1313
self.is_nrv = is_nrv
14+
self.cdf_type = cdf_type
1415

1516
def __len__(self):
1617
return len(self.values)

tests/test_pyistp.py

+8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def test_non_compliant_cdf_with_master(self):
111111
self.assertIsNotNone(istp_file)
112112
self.assertIsNotNone(istp_file.data_variable('MOM.P.AVGTEMP'))
113113

114+
def test_get_data_variable_type(self):
115+
istp_file = pyistp.load(file=f"{current_path}/resources/solo_l3_rpw-bia-density-10-seconds_00000000_v01.cdf")
116+
self.assertEqual(istp_file.data_variable('DENSITY').cdf_type, 'CDF_FLOAT')
117+
118+
def test_get_support_variable_type(self):
119+
istp_file = pyistp.load(file=f"{current_path}/resources/solo_l3_rpw-bia-density-10-seconds_00000000_v01.cdf")
120+
self.assertEqual(istp_file.data_variable('DENSITY').axes[0].cdf_type, 'CDF_TIME_TT2000')
121+
114122
def test_is_nrv(self):
115123
istp_file = pyistp.load(file=f"{current_path}/resources/solo_l3_rpw-bia-density-10-seconds_00000000_v01.cdf")
116124
self.assertFalse(istp_file.data_variable('DENSITY').axes[0].is_nrv)

0 commit comments

Comments
 (0)