Skip to content

Commit 66aceee

Browse files
committed
Use None to encode NaNs in metadata and enable JSON dumps of results
1 parent 995b302 commit 66aceee

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ git submodule update --recursive
599599
Format the code:
600600

601601
```sh
602-
clang-format -i astrometry_extension/astrometry_extension.c
602+
clang-format -i astrometry_extension/astrometry_extension.c astrometry_extension/astrometry_extension_utilities.h
603603
```
604604

605605
Build a local version:

astrometry/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import astrometry_extension
33
import dataclasses
44
import enum
5+
import json
56
import math
67
import operator
78
import pathlib
@@ -154,6 +155,12 @@ def has_match(self) -> bool:
154155
def best_match(self) -> Match:
155156
return self.matches[0]
156157

158+
def to_json(self):
159+
solution_as_dict = dataclasses.asdict(self)
160+
for match in solution_as_dict["matches"]:
161+
match["index_path"] = str(match["index_path"])
162+
return json.dumps(solution_as_dict)
163+
157164

158165
class Solver(astrometry_extension.Solver):
159166
def __init__(self, index_files: list[pathlib.Path]):

astrometry_extension/astrometry_extension_utilities.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define PY_SSIZE_T_CLEAN
66
#include <Python.h>
77
#include <libgen.h>
8+
#include <math.h>
89
#include <string.h>
910
#include <structmember.h>
1011

@@ -255,6 +256,13 @@ static void error_callback(
255256
context->save = PyEval_SaveThread();
256257
}
257258

259+
static PyObject* double_to_python_object(double value) {
260+
if (isnan(value) || isinf(value)) {
261+
Py_RETURN_NONE;
262+
}
263+
return PyFloat_FromDouble(value);
264+
}
265+
258266
static PyObject*
259267
tagalong_to_python_object(startree_t* tree, int column_index, const char* column_name, int star_id, PyObject* logging) {
260268
int size = startree_get_tagalong_column_array_size(tree, column_index);
@@ -276,20 +284,20 @@ tagalong_to_python_object(startree_t* tree, int column_index, const char* column
276284
if (row_size > 1) {
277285
result = PyTuple_New(row_size);
278286
for (int index = 0; index < row_size; ++index) {
279-
PyTuple_SET_ITEM(result, index, PyFloat_FromDouble(((double*)row)[index]));
287+
PyTuple_SET_ITEM(result, index, double_to_python_object(((double*)row)[index]));
280288
}
281289
} else {
282-
result = PyFloat_FromDouble(*(double*)row);
290+
result = double_to_python_object(*(double*)row);
283291
}
284292
break;
285293
case TFITS_BIN_TYPE_E: // float
286294
if (row_size > 1) {
287295
result = PyTuple_New(row_size);
288296
for (int index = 0; index < row_size; ++index) {
289-
PyTuple_SET_ITEM(result, index, PyFloat_FromDouble(((float*)row)[index]));
297+
PyTuple_SET_ITEM(result, index, double_to_python_object(((float*)row)[index]));
290298
}
291299
} else {
292-
result = PyFloat_FromDouble(*(float*)row);
300+
result = double_to_python_object(*(float*)row);
293301
}
294302
break;
295303
case TFITS_BIN_TYPE_A: // char

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def run(self):
249249

250250
setuptools.setup(
251251
name="astrometry",
252-
version="4.0.0",
252+
version="4.1.0",
253253
url="https://github.com/neuromorphicsystems/astrometry",
254254
author="ICNS, Alexandre Marcireau",
255255
author_email="[email protected]",

0 commit comments

Comments
 (0)