Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved handling of string encoding so that netcdf files saved by xarray can be read back in as if they were the original simulation data files.
  • Loading branch information
daminton committed Sep 1, 2022
1 parent 719d6b0 commit 374b0a7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions python/swiftest/swiftest/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,18 +727,31 @@ def clean_string_values(param, ds):
Returns
-------
ds : xarray dataset with the strings cleaned up
"""
"""

def xstrip(a):
func = lambda x: np.char.strip(x)
return xr.apply_ufunc(func, a.str.decode(encoding='utf-8'))

def string_converter(da):
if da.dtype == np.dtype(object):
da = da.astype('<U32')
elif da.dtype != np.dtype('<U32'):
da = xstrip(da)
return da

if 'name' in ds:
ds['name'] = xstrip(ds['name'])
ds['name'] = string_converter(ds['name'])
if 'particle_type' in ds:
ds['particle_type'] = xstrip(ds['particle_type'])
ds['particle_type'] = string_converter(ds['particle_type'])
if 'status' in ds:
ds['status'] = xstrip(ds['status'])
ds['status'] = string_converter(ds['status'])
if 'origin_type' in ds:
ds['origin_type'] = xstrip(ds['origin_type'])
ds['origin_type'] = string_converter(ds['origin_type'])
return ds



def swiftest_particle_stream(f):
"""
Reads in a Swiftest particle.dat file and returns a single frame of particle data as a datastream
Expand Down

0 comments on commit 374b0a7

Please sign in to comment.