Skip to content

Commit 25acb27

Browse files
committed
Implemented technique to convert field dtypes to ND-arrays
1 parent 456c9fe commit 25acb27

File tree

4 files changed

+750
-304
lines changed

4 files changed

+750
-304
lines changed

astropy/erfa/cython_generator.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@
2020
'double *' : "numpy.double",
2121
'int' : "numpy.intc",
2222
'int *' : "numpy.intc",
23-
'int[4]' : "numpy.dtype([('', 'i', (4,))])",
24-
'double[2]' : "numpy.dtype([('', 'd', (2,))])",
25-
'double[3]' : "numpy.dtype([('p', 'd', (3,))])",
26-
'double[2][3]' : "numpy.dtype([('pv', 'd', (2,3))])",
27-
'double[3][3]' : "numpy.dtype([('r', 'd', (3,3))])",
23+
'int[4]' : "numpy.dtype([('fi0', 'i', (4,))])",
24+
'double[2]' : "numpy.dtype([('fi0', 'd', (2,))])",
25+
'double[3]' : "numpy.dtype([('fi0', 'd', (3,))])",
26+
'double[2][3]' : "numpy.dtype([('fi0', 'd', (2,3))])",
27+
'double[3][3]' : "numpy.dtype([('fi0', 'd', (3,3))])",
2828
'eraASTROM *' : "dt_eraASTROM",
29-
'char *' : "numpy.dtype('S1')",
29+
'eraLDBODY[]' : "dt_eraLDBODY",
30+
'char *' : "numpy.dtype('S16')",
31+
'const char *' : "numpy.dtype('S16')",
3032
}
3133

3234

35+
NDIMS_REX = re.compile(re.escape("numpy.dtype([('fi0', '.*', <(.*)>)])").replace(r'\.\*','.*').replace(r'\<', '(').replace(r'\>',')'))
36+
37+
3338
class FunctionDoc(object):
3439

3540
def __init__(self, doc):
@@ -159,6 +164,21 @@ def ctype_ptr(self):
159164
def dtype(self):
160165
return ctype_to_dtype[self.ctype]
161166

167+
@property
168+
def ndims(self):
169+
"""
170+
This is an argument that has a multi-dimensional output, like
171+
double[3][3]
172+
"""
173+
if self.dtype.startswith('numpy.dtype'):
174+
mtch = NDIMS_REX.match(self.dtype)
175+
if mtch:
176+
return len(eval(mtch.group(1)))
177+
else:
178+
return 0
179+
else:
180+
return 0
181+
162182
def __repr__(self):
163183
return "Argument('{0}', name='{1}', ctype='{2}', inout_state='{3}')".format(self.definition, self.name, self.ctype, self.inout_state)
164184

@@ -209,6 +229,14 @@ def __repr__(self):
209229
def dtype(self):
210230
return ctype_to_dtype[self.ctype]
211231

232+
@property
233+
def nd_dtype(self):
234+
"""
235+
This if the return type has a multi-dimensional output, like
236+
double[3][3]
237+
"""
238+
return "'fi0'" in self.dtype
239+
212240
@property
213241
def doc_info(self):
214242
return self.doc.ret_info
@@ -365,6 +393,7 @@ def surround(a_list, pre, post):
365393
"found in the string that "
366394
"spawned it. This should be "
367395
"impossible!")
396+
368397
print("Rendering template")
369398
erfa_pyx = erfa_pyx_in.render(funcs=funcs)
370399

0 commit comments

Comments
 (0)