Skip to content

Commit 3861a94

Browse files
committed
Fix from copilot review
1 parent c1e819c commit 3861a94

5 files changed

Lines changed: 12 additions & 9 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .istp_cdf import IstpCdf
22
from .hapi.csv import HapiCsv
3+
from .hapi.binary import HapiBinary

speasy/core/codecs/bundled_codecs/hapi/binary/reader.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from ast import Tuple
21
import io
32
import json
43
from typing import Optional, Union, Dict, Any, Tuple
@@ -71,7 +70,7 @@ def _parse_hapi_binary(file: io.IOBase) -> Tuple[np.array, Dict[str, Any]]:
7170

7271
def _load_binary(file: Union[Buffer, str, io.IOBase]) -> Tuple[Optional[np.array], Optional[Dict[str, Any]]]:
7372
if isinstance(file, str):
74-
with any_loc_open(file, cache_remote_files=False, mode='br') as f:
73+
with any_loc_open(file, cache_remote_files=False, mode='rb') as f:
7574
return _parse_hapi_binary(f)
7675
if isinstance(file, io.IOBase) or hasattr(file, 'read'):
7776
return _parse_hapi_binary(file)

speasy/core/codecs/bundled_codecs/hapi/codec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _decode_meta(meta: Dict[str, Any]) -> Dict[str, Any]:
2727
return meta
2828

2929
def _make_hapi_time_axis(time_axis: VariableTimeAxis) -> HapiParameter:
30-
return HapiParameter(values=time_axis,
30+
return HapiParameter(values=time_axis.values,
3131
meta={"name": "Time", "type": "isotime", "units": "UTC", "length": 24, "fill": None})
3232

3333
def _make_hapi_parameter(variable: SpeasyVariable) -> HapiParameter:
@@ -105,7 +105,7 @@ def _speasy_variables_to_hapi(variables: List[SpeasyVariable]) -> HapiFile:
105105
if len(variables) == 0:
106106
raise ValueError("No variables to save")
107107
hapi_file = HapiFile()
108-
hapi_file.add_parameter(_make_hapi_time_axis(variables[0].time))
108+
hapi_file.add_parameter(_make_hapi_time_axis(variables[0].axes[0]))
109109
for spz_var in variables:
110110
# add spz_var as hapi_param
111111
hapi_file.add_parameter(_make_hapi_parameter(spz_var))

speasy/core/codecs/bundled_codecs/hapi/writer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
def save_hapi(
99
hapi_file: HapiFile,
1010
file: Optional[Union[str, io.IOBase]] = None,
11-
to_func: Callable[[HapiFile, io.IOBase, bool], bool] = None,
11+
to_func: Optional[Callable[[HapiFile, io.IOBase, bool], bool]] = None,
1212
mode: str = "wb",
1313
with_headers: bool = True
1414
) -> Union[bool, Buffer]:
1515

16+
if to_func is None:
17+
raise ValueError("to_func must be provided and cannot be None")
18+
1619
if isinstance(file, str):
1720
with open(file, mode) as f:
1821
return to_func(hapi_file, f, with_headers=with_headers)

tests/test_hapi_codecs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class TestHapiBinaryCodec(unittest.TestCase):
221221
)
222222
@unpack
223223
def test_load_headers(self, fname, var_name, unit, description):
224-
with open(os.path.join(__HERE__, 'resources', fname), 'br') as f:
224+
with open(os.path.join(__HERE__, 'resources', fname), 'rb') as f:
225225
headers = hapi_binary.reader._extract_headers(f)
226226
time_param = headers['parameters'][0]
227227
self.assertEqual(time_param['units'], 'UTC')
@@ -258,7 +258,7 @@ def test_load_multiple_variables(self):
258258
fname = "HAPI_amda_imf_all.binary"
259259
file=str(os.path.join(__HERE__, 'resources', fname))
260260
var_names = ["imf_mag", "imf"]
261-
with open(file, 'br') as f:
261+
with open(file, 'rb') as f:
262262
variables = hapi_binary_codec.load_variables(file=f, variables=var_names, disable_cache=True)
263263
self.assertEqual(len(variables), len(var_names))
264264
for v in variables.values():
@@ -273,7 +273,7 @@ def test_load_multiple_variables(self):
273273

274274
def test_load_time_independent_axis(self):
275275
hapi_binary_codec: CodecInterface = get_codec('hapi/binary')
276-
with open(os.path.join(__HERE__, 'resources', 'HAPI_ndData_TimeIndependent_Axis.binary'), 'br') as f:
276+
with open(os.path.join(__HERE__, 'resources', 'HAPI_ndData_TimeIndependent_Axis.binary'), 'rb') as f:
277277
variables = hapi_binary_codec.load_variables(file=f, variables=['ace_epam_de_e'], disable_cache=True)
278278
self.assertIn('ace_epam_de_e', variables)
279279
v: SpeasyVariable = variables['ace_epam_de_e']
@@ -310,7 +310,7 @@ def test_spz_getdata_to_binary(self):
310310

311311
def test_save_time_varying_axis(self):
312312
hapi_binary_codec: CodecInterface = get_codec('hapi/binary')
313-
with open(os.path.join(__HERE__, 'resources', 'HAPI_ndData_TimeVarying_Axis.binary'), 'br') as f:
313+
with open(os.path.join(__HERE__, 'resources', 'HAPI_ndData_TimeVarying_Axis.binary'), 'rb') as f:
314314
# read to generate spz_vars
315315
var_name = 'spectra_time_dependent_bins'
316316
variables = hapi_binary_codec.load_variables(file=f, variables=[var_name], disable_cache=True)

0 commit comments

Comments
 (0)