@@ -26,14 +26,110 @@ cimport numpy as np
26
26
import numpy as np
27
27
from bisect import bisect_right
28
28
29
- # from pyne cimport nucname
30
- # from pyne import nucname
31
- # from pyne.rxname import label
29
+ from libc.stdlib cimport malloc, free
30
+ from libc.stdlib cimport atof
31
+ from libc.string cimport strtok, strcpy, strncpy
32
+ from cython.operator cimport dereference as deref
32
33
33
- # fromstring func should depend on numpy verison
34
- from pyne._utils import fromstring_split, fromstring_token
35
34
cdef bint NP_LE_V15 = int (np.__version__.split(' .' )[1 ]) <= 5 and np.__version__.startswith(' 1' )
36
35
36
+ def fromstring_split (s , sep = None , dtype = float ):
37
+ """ A replacement for numpy.fromstring() using the Python str.split()
38
+ and np.array().
39
+
40
+ Parameters
41
+ ----------
42
+ s : str
43
+ String of data.
44
+ sep : str or None
45
+ String of separator characters, has the same meaning as in
46
+ str.split().
47
+ dtype : np.dtype
48
+ Numpy dtype to cast elements enough.
49
+
50
+ Returns
51
+ -------
52
+ data : ndarray, 1d
53
+ Will always return a 1d array of dtype. You must reshape to the
54
+ appropriate shape.
55
+
56
+ See Also
57
+ --------
58
+ fromstring_token : May faster depending on the data.
59
+
60
+ """
61
+ cdef list rawdata
62
+ rawdata = s.split(sep)
63
+ return np.array(rawdata, dtype = dtype)
64
+
65
+
66
+ def fromstring_token (s , sep = " " , bint inplace = False , int maxsize = - 1 ):
67
+ """ A replacement for numpy.fromstring() using the C standard
68
+ library atof() and strtok() functions.
69
+
70
+ Parameters
71
+ ----------
72
+ s : str
73
+ String of data.
74
+ sep : str
75
+ String of separator characters. Unlike numpy.fromstring(),
76
+ all characters are separated on independently.
77
+ inplace : bool
78
+ Whether s should tokenized in-place or whether a copy should
79
+ be made. If done in-place, the first instance of sep between
80
+ any tokens will replaced with the NULL character.
81
+ maxsize : int
82
+ Specifies the size of the array to pre-allocate. If negative,
83
+ this will be set to the maximum possible number of elements,
84
+ ie len(s)/2 + 1.
85
+
86
+ Returns
87
+ -------
88
+ data : ndarray, 1d, float64
89
+ Will always return a 1d float64 array. You must cast and reshape
90
+ to the appropriate type and shape.
91
+
92
+ See Also
93
+ --------
94
+ fromstring_split : May faster depending on the data.
95
+
96
+ """
97
+ cdef char * cstring
98
+ cdef char * cs
99
+ cdef char * csep
100
+ cdef int i, I
101
+ cdef np.ndarray[np.float64_t, ndim= 1 ] cdata
102
+
103
+ s_bytes = s.encode()
104
+ I = len (s_bytes)
105
+ sep_bytes = sep.encode()
106
+ csep = sep_bytes
107
+
108
+ if inplace:
109
+ cs = s_bytes
110
+ else :
111
+ cs = < char * > malloc(I * sizeof(char ))
112
+ strcpy(cs, s_bytes)
113
+
114
+ if maxsize < 0 :
115
+ maxsize = (I // 2 ) + 1
116
+
117
+ data = np.empty(maxsize, dtype = np.float64)
118
+ cdata = data
119
+
120
+ i = 0
121
+ cstring = strtok(cs, csep)
122
+ while cstring != NULL :
123
+ cdata[i] = atof(cstring)
124
+ cstring = strtok(NULL , csep)
125
+ i += 1
126
+
127
+ if not inplace:
128
+ free(cs)
129
+
130
+ data = data[:i].copy()
131
+ return data
132
+
37
133
def ascii_to_binary (ascii_file , binary_file ):
38
134
""" Convert an ACE file in ASCII format (type 1) to binary format (type 2).
39
135
0 commit comments