0

I am using the netCDF4.stringtochar() function before saving an N-D array of strings before exporting to a netCDF file; however, the function adds empty strings between each character.

Is this a bug in the function and/or am I missing a simple fix to this issue?

Here is an example:

import netCDF4
import numpy as np

data = np.array([['AAAA', 'BBBB', 'AAAB', 'BAAA'],['BABA', 'ABAB', 'AABB', 'BBAA']])

data_char = netCDF4.stringtochar(data,encoding='utf-8')

data_char returns:

array([[['A', '', '', '', 'A', '', '', '', 'A', '', '', '', 'A', '', '', ''], ['B', '', '', '', 'B', '', '', '', 'B', '', '', '', 'B', '', '', ''], ['A', '', '', '', 'A', '', '', '', 'A', '', '', '', 'B', '', '', ''], ['B', '', '', '', 'A', '', '', '', 'A', '', '', '', 'A', '', '', '']], [['B', '', '', '', 'A', '', '', '', 'B', '', '', '', 'A', '', '', ''], ['A', '', '', '', 'B', '', '', '', 'A', '', '', '', 'B', '', '', ''], ['A', '', '', '', 'A', '', '', '', 'B', '', '', '', 'B', '', '', ''], ['B', '', '', '', 'B', '', '', '', 'A', '', '', '', 'A', '', '', '']]], dtype='<U1')

BenT
  • 3,172
  • 3
  • 18
  • 38

1 Answers1

1

Although the documentation says that a u or s datatype can be provided, the issue is fixed when manually setting the dtype to 's4'.

BenT
  • 3,172
  • 3
  • 18
  • 38