@@ -310,53 +310,62 @@ def numpy_to_pds_type(dtype, ascii_numerics=False):
310310 dtype : np.dtype
311311 A NumPy data type.
312312 ascii_numerics
313- If True, the returned PDS4 data type will be an ASCII numeric type if the input dtype is numeric
314- or boolean. If False, the returned PDS4 data type will be a binary type. Defaults to False.
313+ If True, the returned PDS4 data type will be an ASCII numeric or boolean type if the input dtype
314+ is numeric or boolean. If False, the returned PDS4 data type will be a binary type. Defaults to
315+ False.
315316
316317 Returns
317318 -------
318319 PDSdtype
319320 A PDS4 data type that could plausibly (see description above) correspond to the input dtype.
320321 """
321322
323+ # Ensure *dtype* is a NumPy dtype
324+ dtype = np .dtype (dtype )
325+
322326 # For string dtypes
323327 if np .issubdtype (dtype , np_unicode ):
324328 data_type = 'UTF8_String'
325329
326- elif np .issubdtype (dtype , np .string_ ):
330+ elif np .issubdtype (dtype , np .bytes_ ):
327331 data_type = 'ASCII_String'
328332
329333 # For datetime dtypes
330334 elif np .issubdtype (dtype , np .datetime64 ):
331- data_type = 'ASCII_Date_Time_YMD'
335+
336+ if dtype .name == PDS4_DATE_TYPES ['ASCII_Date_YMD' ][1 ]:
337+ data_type = 'ASCII_Date_YMD'
338+ else :
339+ data_type = 'ASCII_Date_Time_YMD'
332340
333341 # For numeric dtypes
334342 else :
335343
336- # Get numeric ASCII types. We obtain these from builtin portion because if we attempt to match
337- # e.g. 'int16' to 'int64' it would fail but for ASCII types this should succeed.
338- ascii_types = dict ((value [2 ], key )
339- for key , value in six .iteritems (PDS_NUMERIC_TYPES )
340- if ('ASCII' in key ) and ('Numeric_Base' not in key ))
344+ # Get numeric ASCII types
345+ # (compare via np.dtype.kind because kind is unique for each ASCII type supported here)
346+ if ascii_numerics :
341347
342- # Get numeric non-ASCII types, including the correct endianness.
343- non_ascii_types = dict ((np .dtype (value [1 ]).newbyteorder (value [0 ]), key )
348+ ascii_types = dict ((np .dtype (value [1 ]).kind , key )
344349 for key , value in six .iteritems (PDS_NUMERIC_TYPES )
345- if ('ASCII' not in key ) and ('Numeric_Base' not in key ))
350+ if ('ASCII' in key ) and ('Numeric_Base' not in key ))
346351
347- if ascii_numerics :
348-
349- builtin_type = type (np .asscalar (np .array (0 , dtype = dtype ))).__name__
350- data_type = ascii_types .get (builtin_type , None )
352+ data_type = ascii_types .get (dtype .kind , None )
351353
354+ # Get numeric non-ASCII types, including the correct endianness
355+ # (compare via full np.dtype)
352356 else :
357+
358+ non_ascii_types = dict ((np .dtype (value [1 ]).newbyteorder (value [0 ]), key )
359+ for key , value in six .iteritems (PDS_NUMERIC_TYPES )
360+ if ('ASCII' not in key ) and ('Numeric_Base' not in key ))
361+
353362 data_type = non_ascii_types .get (dtype , None )
354363
355364 # Raise error if we were unable to find a match
356365 if data_type is None :
357366
358367 raise ValueError ("Unable to convert NumPy data type, '{0}', to a PDS4 {1} data type." .
359- format (dtype , 'ASCII' if ascii_numerics else 'binary' ))
368+ format (dtype . name , 'ASCII' if ascii_numerics else 'binary' ))
360369
361370 return PDSdtype (data_type )
362371
0 commit comments