Skip to content

Commit 6a82cc0

Browse files
committed
Several fixes and adds some logging about non compliant files
Signed-off-by: Alexis Jeandet <[email protected]>
1 parent 22e3811 commit 6a82cc0

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

pyistp/_impl.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,44 @@
44
import re
55
import numpy as np
66
from typing import List
7+
import logging
78

89
DEPEND_REGEX = re.compile("DEPEND_\\d")
910

11+
ISTP_NOT_COMPLIANT_W = "Non compliant ISTP file"
12+
13+
log = logging.getLogger(__name__)
14+
1015

1116
def _get_attributes(cdf: object, var: str):
1217
attrs = {}
1318
for attr in cdf.variable_attributes(var):
19+
value = cdf.variable_attribute_value(var, attr)
1420
if attr.endswith("_PTR") or attr[:-1].endswith("_PTR_"):
15-
value = cdf.values(cdf.variable_attribute_value(var, attr))
16-
if hasattr(value, 'tolist'):
17-
attrs[attr] = value.tolist()
21+
if cdf.has_variable(value):
22+
value = cdf.values(value)
23+
if hasattr(value, 'tolist'):
24+
attrs[attr] = value.tolist()
25+
else:
26+
attrs[attr] = value
1827
else:
28+
log.warning(
29+
f"{ISTP_NOT_COMPLIANT_W}: variable {var} has {attr} attribute which points to a variable which does not exist")
1930
attrs[attr] = value
2031
else:
21-
attrs[attr] = cdf.variable_attribute_value(var, attr)
32+
attrs[attr] = value
2233
return attrs
2334

2435

2536
def _get_axis(cdf: object, var: str):
26-
if cdf.is_char(var):
27-
if 'sig_digits' in cdf.variable_attributes(var): # cluster CSA trick :/
28-
return SupportDataVariable(name=var, values=np.asarray(cdf.values(var), dtype=float),
29-
attributes=_get_attributes(cdf, var))
30-
return SupportDataVariable(name=var, values=cdf.values(var), attributes=_get_attributes(cdf, var))
37+
if cdf.has_variable(var):
38+
if cdf.is_char(var):
39+
if 'sig_digits' in cdf.variable_attributes(var): # cluster CSA trick :/
40+
return SupportDataVariable(name=var, values=np.asarray(cdf.values(var), dtype=float),
41+
attributes=_get_attributes(cdf, var))
42+
return SupportDataVariable(name=var, values=cdf.values(var), attributes=_get_attributes(cdf, var))
43+
log.warning(f"{ISTP_NOT_COMPLIANT_W}: trying to load {var} as support data but it is absent from the file")
44+
return None
3145

3246

3347
def _get_axes(cdf: object, var: str, data_shape):
@@ -39,8 +53,8 @@ def _get_axes(cdf: object, var: str, data_shape):
3953
if len(unix_time) == data_shape[0]:
4054
unix_time.values = (unix_time.values * 1e9).astype('<M8[ns]')
4155
axes[0] = unix_time
42-
Warning(
43-
"Non compliant CDF file, swapping DEPEND_0 with DEPEND_TIME, if you think this is a bug report it here: https://github.com/SciQLop/PyISTP/issues")
56+
log.warning(
57+
f"{ISTP_NOT_COMPLIANT_W}: swapping DEPEND_0 with DEPEND_TIME, if you think this is a bug report it here: https://github.com/SciQLop/PyISTP/issues")
4458
return axes
4559

4660

@@ -56,6 +70,9 @@ def _load_data_var(cdf: object, var: str) -> DataVariable or None:
5670
axes = _get_axes(cdf, var, values.shape)
5771
attributes = _get_attributes(cdf, var)
5872
labels = _get_labels(attributes)
73+
if len(axes) == 0:
74+
log.warning(f"{ISTP_NOT_COMPLIANT_W}: {var} was marked as data variable but it has 0 support variable")
75+
return None
5976
if None in axes or axes[0].values.shape[0] != values.shape[0]:
6077
return None
6178
return DataVariable(name=var, values=values, attributes=attributes, axes=axes, labels=labels)

pyistp/drivers/pycdfpp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def variables(self):
2020
return list(self.cdf)
2121
return []
2222

23-
def is_char(self,var):
23+
def has_variable(self, name):
24+
return name in self.cdf
25+
26+
def is_char(self, var):
2427
return self.cdf[var].type == pycdfpp.CDF_CHAR
2528

2629
def variable_attributes(self, var):

pyistp/drivers/spacepy.py

Lines changed: 3 additions & 0 deletions
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 has_variable(self, name):
32+
return name in self.cdf
33+
3134
def variable_attributes(self, var):
3235
if self.cdf:
3336
return self.cdf[var].attrs.keys()

pyistp/support_data_variable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import numpy as np
2+
3+
14
class SupportDataVariable:
25
__slots__ = ("name", "values", 'attributes')
36

47
def __init__(self, name, values, attributes):
58
self.name = name
69
self.values = values
10+
if type(self.values) is list:
11+
self.values = np.array(self.values)
712
self.attributes = attributes
813

914
def __len__(self):
59.8 KB
Binary file not shown.

tests/test_pyistp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@
5858
"tha_peer_vthermal", "tha_peer_ptens", "tha_peeb_magfQ"]),
5959
(f"{current_path}/resources/thd_l2_efi_00000000_v01.cdf",
6060
['thd_eff_e34_efs', 'thd_eff_q_mag', 'thd_efs_dot0_gsm', 'thd_efs_dot0_dsl', 'thd_efs_dot0_gse', 'thd_eff_q_pha',
61-
'thd_eff_dot0_gse', 'thd_eff_e12_efs', 'thd_eff_dot0_dsl', 'thd_eff_dot0_gsm'])
61+
'thd_eff_dot0_gse', 'thd_eff_e12_efs', 'thd_eff_dot0_dsl', 'thd_eff_dot0_gsm']),
62+
(f"{current_path}/resources/c3_cp_efw_l3_e3d_inert_00000000_v01.cdf",
63+
['delta_Ez_ISR2__C3_CP_EFW_L3_E3D_INERT', 'E_Vec_xyz_ISR2__C3_CP_EFW_L3_E3D_INERT',
64+
'E_sigma__C3_CP_EFW_L3_E3D_INERT']
65+
)
6266
)
6367

6468

0 commit comments

Comments
 (0)