Skip to content

Commit b25d426

Browse files
committed
Fix float precision in csv writing
1 parent 84f4726 commit b25d426

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _to_csv(hapi_csv_file: HapiCsvFile, dest:io.IOBase, with_headers=True) -> bo
3535
data[f"{param.name}_{i}"] = vals[:, i]
3636

3737
df = pds.DataFrame(data)
38-
df.to_csv(dest, index=False, header=False, date_format='%Y-%m-%dT%H:%M:%S.%fZ', float_format='%.3g')
38+
df.to_csv(dest, index=False, header=False, date_format='%Y-%m-%dT%H:%M:%S.%fZ', float_format='%.7g')
3939
return True
4040

4141

tests/test_hapi_codecs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,17 @@ def test_hapi_csv_compliant_headers(self):
189189
headers = _extract_headers(f)
190190
for key in ["HAPI", "startDate", "stopDate", "format", "status", "parameters"]:
191191
self.assertIn(key, headers)
192+
193+
def test_hapi_csv_precision(self):
194+
hapi_csv_codec: CodecInterface = get_codec('hapi/csv')
195+
imf_data = spz.get_data(
196+
spz.inventories.tree.amda.Parameters.ACE.MFI.ace_imf_all.imf,
197+
"2008-01-01",
198+
"2008-01-02",
199+
)
200+
with tempfile.NamedTemporaryFile(suffix='.csv', delete=True) as tmp:
201+
hapi_csv_codec.save_variables(variables=[imf_data], file=tmp)
202+
df = pd.read_csv(tmp.name, comment='#', sep=',', header=None, skiprows=0, parse_dates=[0], index_col=0)
203+
self.assertAlmostEqual(float(df.iloc[0, 0]), float(imf_data.values[0, 0]), places=3)
204+
self.assertAlmostEqual(float(df.iloc[0, 1]), float(imf_data.values[0, 1]), places=3)
205+
self.assertAlmostEqual(float(df.iloc[0, 2]), float(imf_data.values[0, 2]), places=3)

0 commit comments

Comments
 (0)