Skip to content

Commit

Permalink
fixed output type issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
cylammarco committed Apr 25, 2023
1 parent 90ff5e2 commit f7efefb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
name="spectresc",
maintainer="Marco C Lam",
maintainer_email="lam@mail.tau.ac.il",
version="0.1",
version="1.0.0.rc2",
description="SpectRes in C",
long_description=Path("README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
Expand Down
30 changes: 13 additions & 17 deletions src/spectresc/spectres.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
double *make_bins(double *wavs, int wavs_len)
{
double *edges = (double *)malloc(sizeof(double) * (wavs_len + 1));
double *widths = (double *)malloc(sizeof(double) * wavs_len);

edges[0] = wavs[0] - (wavs[1] - wavs[0]) / 2;
widths[wavs_len - 1] = (wavs[wavs_len - 1] - wavs[wavs_len - 2]);
edges[wavs_len] = wavs[wavs_len - 1] + (wavs[wavs_len - 1] - wavs[wavs_len - 2]) / 2;

for (int i = 1; i < wavs_len; i++)
{
edges[i] = (wavs[i] + wavs[i - 1]) / 2;
widths[i - 1] = edges[i] - edges[i - 1];
}

return edges;
}

Expand Down Expand Up @@ -153,35 +151,33 @@ static PyObject *spectres(PyObject *self, PyObject *args, PyObject *kwargs)
}
}
}

// Free the memory
free(spec_edges);
free(new_edges);
free(spec_widths);

// Create NumPy arrays to return the data to Python
npy_intp dims[1] = {new_wavs_len};
PyObject *new_fluxes_array = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, new_fluxes);
PyArray_ENABLEFLAGS((PyArrayObject *)new_fluxes_array, NPY_ARRAY_OWNDATA);

// Set the base object for the arrays to NULL to indicate that the arrays are not owned by Python
// and reate a tuple to return the array(s)
PyObject *new_errs_array = NULL;
PyObject *result = NULL;
if (spec_errs != NULL)
{
result = PyTuple_New(2);
PyTuple_SetItem(result, 0, new_fluxes_array);
PyObject *new_errs_array = NULL;
PyObject *result_list = PyList_New(0);
PyList_Append(result_list, PyArray_Return(new_fluxes_array));
new_errs_array = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, new_errs);
PyArray_ENABLEFLAGS((PyArrayObject *)new_errs_array, NPY_ARRAY_OWNDATA);
PyTuple_SetItem(result, 1, new_errs_array);
PyList_Append(result_list, PyArray_Return(new_errs_array));
return result_list;
}
else
{
result = PyTuple_New(1);
PyTuple_SetItem(result, 0, new_fluxes_array);
return new_fluxes_array;
}

// Free the memory
free(spec_edges);
free(new_edges);
free(spec_widths);

return result;
}

// Define the module methods
Expand Down

0 comments on commit f7efefb

Please sign in to comment.