1818from .writer import save_hapi_csv
1919
2020
21+ def _time_dependent_axis_name (ax : VariableAxis ) -> str :
22+ return f"{ ax .name } _centers_time_varying"
23+
24+ def _get_variable_axes (variable : SpeasyVariable , is_time_dependent : bool ) -> List [VariableAxis ]:
25+ return [ax for ax in variable .axes [1 :] if ax .is_time_dependent == is_time_dependent ]
26+
2127def _create_meta (variable :SpeasyVariable ) -> Dict [str , Any ]:
2228 meta = {
2329 "name" : variable .name ,
@@ -44,12 +50,23 @@ def _create_meta(variable:SpeasyVariable) -> Dict[str, Any]:
4450 if len (variable .values .shape ) > 1 :
4551 meta ["size" ] = variable .values .shape [1 :]
4652
47- time_independent_axes = [ax for ax in variable .axes [1 :] if not ax .is_time_dependent ]
53+ bins = []
54+ time_independent_axes = _get_variable_axes (variable , is_time_dependent = False )
4855 if time_independent_axes :
49- meta [ " bins" ] = [
50- {"name" : ax .name , "units " : ax .unit , "centers" : ax .values .tolist ()}
56+ bins . extend ( [
57+ {"name" : ax .name , "unit " : ax .unit , "centers" : ax .values .tolist ()}
5158 for ax in time_independent_axes
52- ]
59+ ])
60+
61+ time_dependent_axes = _get_variable_axes (variable , is_time_dependent = True )
62+ if time_dependent_axes :
63+ bins .extend ([
64+ {"name" : ax .name , "unit" : ax .unit , "centers" : _time_dependent_axis_name (ax )}
65+ for ax in time_dependent_axes
66+ ])
67+
68+ if bins :
69+ meta ["bins" ] = bins
5370
5471 return meta
5572
@@ -67,8 +84,9 @@ def _bin_to_axis(json_bin: Dict[str, Any], hap_csv_file: HapiCsvFile) -> Variabl
6784 raise ValueError ("Invalid bin specification: missing 'centers' field" )
6885 if isinstance (centers , str ):
6986 hapi_parameter = hap_csv_file .get_parameter (centers )
87+ _meta = _decode_meta (hapi_parameter .meta )
7088 variable_axis = VariableAxis (values = hapi_parameter .values ,
71- meta = hapi_parameter . meta ,
89+ meta = _meta ,
7290 is_time_dependent = True ,
7391 name = name )
7492 elif isinstance (centers , list ):
@@ -77,7 +95,7 @@ def _bin_to_axis(json_bin: Dict[str, Any], hap_csv_file: HapiCsvFile) -> Variabl
7795 except ValueError :
7896 raise ValueError ("Invalid bin specification: 'centers' list must contain numeric values" )
7997 variable_axis = VariableAxis (values = axis_values ,
80- meta = {"name" : "centers" },
98+ meta = {"name" : "centers" , "UNITS" : json_bin . get ( "units" , None ) },
8199 is_time_dependent = False ,
82100 name = name )
83101 else :
@@ -122,6 +140,18 @@ def _make_hapi_csv_time_axis(time_axis: VariableTimeAxis) -> HapiCsvParameter:
122140 return HapiCsvParameter (values = time_axis .values ,
123141 meta = {"name" : "Time" , "type" : "isotime" , "units" : "UTC" , "length" : 30 , "fill" : None })
124142
143+ def _get_hapi_csv_varying_axes (variable : SpeasyVariable ) -> List [HapiCsvParameter ]:
144+ result = []
145+ for ax in _get_variable_axes (variable , is_time_dependent = True ):
146+ meta = {
147+ "name" : _time_dependent_axis_name (ax ),
148+ "type" : "double" ,
149+ "units" : ax .unit ,
150+ "size" : [ax .values .shape [1 ]]
151+ }
152+ result .append (HapiCsvParameter (values = ax .values , meta = meta ))
153+ return result
154+
125155
126156def _speasy_variables_to_hapi_csv (variables : List [SpeasyVariable ]) -> HapiCsvFile :
127157 if not same_time_axis (variables ):
@@ -132,6 +162,8 @@ def _speasy_variables_to_hapi_csv(variables: List[SpeasyVariable]) -> HapiCsvFil
132162 hapi_csv_file .add_parameter (_make_hapi_csv_time_axis (variables [0 ].axes [0 ]))
133163 for var in variables :
134164 hapi_csv_file .add_parameter (_make_hapi_csv_parameter (var ))
165+ for hapi_axis_parameter in _get_hapi_csv_varying_axes (var ):
166+ hapi_csv_file .add_parameter (hapi_axis_parameter )
135167 return hapi_csv_file
136168
137169
0 commit comments