44import re
55import numpy as np
66from typing import List
7+ import logging
78
89DEPEND_REGEX = re .compile ("DEPEND_\\ d" )
910
11+ ISTP_NOT_COMPLIANT_W = "Non compliant ISTP file"
12+
13+ log = logging .getLogger (__name__ )
14+
1015
1116def _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
2536def _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
3347def _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 )
0 commit comments