Skip to content

Commit

Permalink
simplified wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Aug 16, 2024
1 parent 1df79a5 commit 66b7fe2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 176 deletions.
2 changes: 1 addition & 1 deletion clima/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
os.environ['OMP_NUM_THREADS'] = '1'

from ._clima import AdiabatClimate, ClimaException
from ._clima import rebin, rebin_with_errors, grid_at_resolution, make_bins # rebin routine from futils
from ._clima import rebin, rebin_with_errors # rebin routine from futils
from ._clima import __version__
86 changes: 6 additions & 80 deletions clima/cython/futils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@ cimport futils_pxd as f_pxd

cdef void rebin_error_message(int ierr):

cdef int err_length
cdef void *err_ptr
cdef ndarray err_
cdef char err[ERR_LEN+1]

if ierr != 0:
f_pxd.futils_rebin_error_message_wrapper1(
f_pxd.futils_rebin_error_message_wrapper(
&ierr,
&err_length,
&err_ptr
err
)
err_ = np.zeros((),dtype=np.dtype(('S', err_length+1)))
f_pxd.futils_rebin_error_message_wrapper2(
&err_length,
err_ptr,
<char *> err_.data
)
raise Exception(err_.item().decode())

if len(err.strip()) > 0:
raise Exception(err.decode().strip())

cpdef rebin(ndarray[double, ndim=1] old_bins, ndarray[double, ndim=1] old_vals, ndarray[double, ndim=1] new_bins):
"""Rebins `old_vals` defined on `old_bins` to `new_bins`. An example is
Expand Down Expand Up @@ -106,69 +97,4 @@ cpdef rebin_with_errors(ndarray[double, ndim=1] old_bins, ndarray[double, ndim=1

rebin_error_message(ierr)

return new_vals, new_errs

cpdef grid_at_resolution(double wv_min, double wv_max, double R):
"""Computes a grid of bins at a given resolution `R`
Parameters
----------
wv_min : double
Minimum bin extent
wv_max : double
Maximum bin extent
R : double
Bin resolution (dlam = lam/R)
Returns
-------
wavl : ndarray[double,ndim=1]
Ouput grid
"""

cdef int wavl_len
cdef void *wavl_ptr
cdef int ierr

f_pxd.futils_grid_at_resolution_wrapper1(
&wv_min, &wv_max, &R,
&wavl_len, &wavl_ptr,
&ierr
)
cdef ndarray wavl = np.empty(wavl_len, np.double)
f_pxd.futils_grid_at_resolution_wrapper2(
&wavl_len, wavl_ptr, <double *> wavl.data
)

rebin_error_message(ierr)

return wavl

cpdef make_bins(ndarray[double, ndim=1] wv):
"""Given a series of wavelength points, find the corresponding bin edges
Parameters
----------
wv : ndarray[double,ndim=1]
Wavelength points.
Returns
-------
wavl : ndarray[double,ndim=1]
Bin edges
"""

cdef int wv_len = wv.shape[0]
cdef int wavl_len = wv_len + 1
cdef ndarray wavl = np.empty(wavl_len, np.double)
cdef int ierr

f_pxd.futils_make_bins_wrapper(
&wv_len, <double *> wv.data,
&wavl_len, <double *> wavl.data,
&ierr
)

rebin_error_message(ierr)

return wavl
return new_vals, new_errs
17 changes: 2 additions & 15 deletions clima/cython/futils_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ cdef extern void futils_rebin_with_errors_wrapper(
int *ierr
)

cdef extern void futils_grid_at_resolution_wrapper1(
double *wv_min, double *wv_max, double *R, int *wavl_len,
void *wavl_ptr, int *ierr
cdef extern void futils_rebin_error_message_wrapper(
int *ierr, char *err
)
cdef extern void futils_grid_at_resolution_wrapper2(int *wavl_len, void *wavl_ptr, double *wavl)

cdef extern void futils_make_bins_wrapper(
int *wv_len, double *wv, int *wavl_len, double *wavl, int *ierr
)

cdef extern void futils_rebin_error_message_wrapper1(
int *ierr, int *err_length, void *err_ptr
)
cdef extern void futils_rebin_error_message_wrapper2(
int *err_length, void *err_ptr, char *err
)
87 changes: 7 additions & 80 deletions clima/fortran/futils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -34,91 +34,18 @@ subroutine futils_rebin_with_errors_wrapper(old_bins_len, old_bins, old_vals_len
call rebin_with_errors(old_bins, old_vals, old_errs, new_bins, new_vals, new_errs, ierr)
end subroutine

subroutine futils_grid_at_resolution_wrapper1(wv_min, wv_max, R, wavl_len, wavl_ptr, ierr) bind(c)
use futils, only: grid_at_resolution, dp
real(c_double), intent(in) :: wv_min
real(c_double), intent(in) :: wv_max
real(c_double), intent(in) :: R
integer(c_int), intent(out) :: wavl_len
type(c_ptr), intent(out) :: wavl_ptr
integer(c_int), intent(out) :: ierr

real(dp), allocatable :: wavl(:)
real(c_double), pointer :: wavl_p(:)

call grid_at_resolution(wv_min, wv_max, R, wavl, ierr)

if (allocated(wavl)) then
wavl_len = size(wavl)
allocate(wavl_p(size(wavl)))
wavl_p = wavl
wavl_ptr = c_loc(wavl_p)
else
wavl_len = 0
wavl_ptr = c_null_ptr
endif

end subroutine

subroutine futils_grid_at_resolution_wrapper2(wavl_len, wavl_ptr, wavl) bind(c)
integer(c_int), intent(in) :: wavl_len
type(c_ptr), value, intent(in) :: wavl_ptr
real(c_double), intent(out) :: wavl(wavl_len)

real(c_double), pointer :: wavl_p(:)

if (.not.c_associated(wavl_ptr)) return

call c_f_pointer(wavl_ptr, wavl_p, [wavl_len])
wavl = wavl_p
deallocate(wavl_p)

end subroutine

subroutine futils_make_bins_wrapper(wv_len, wv, wavl_len, wavl, ierr) bind(c)
use futils, only: make_bins
integer(c_int), intent(in) :: wv_len
real(c_double), intent(in) :: wv(wv_len)
integer(c_int), intent(in) :: wavl_len
real(c_double), intent(out) :: wavl(wavl_len)
integer(c_int), intent(out) :: ierr
call make_bins(wv, wavl, ierr)
end subroutine

subroutine futils_rebin_error_message_wrapper1(ierr, err_length, err_ptr) bind(c)
subroutine futils_rebin_error_message_wrapper(ierr, err) bind(c)
use futils, only: rebin_error_message
integer(c_int), intent(in) :: ierr
integer(c_int), intent(out) :: err_length
type(c_ptr), intent(out) :: err_ptr
character(kind=c_char), intent(out) :: err(err_len+1)

character(:), allocatable :: err
character(kind=c_char), pointer :: err_p(:)
character(:), allocatable :: err_f

err = rebin_error_message(ierr)
err_f = rebin_error_message(ierr)

if (allocated(err)) then
err_length = len(err)
allocate(err_p(err_length+1))
call copy_string_ftoc(err, err_p)
err_ptr = c_loc(err_p)
else
err_length = 0
err_ptr = c_null_ptr
err(1) = c_null_char
if (allocated(err_f)) then
call copy_string_ftoc(err_f, err)
endif

end subroutine

subroutine futils_rebin_error_message_wrapper2(err_length, err_ptr, err) bind(c)
integer(c_int), intent(in) :: err_length
type(c_ptr), value, intent(in) :: err_ptr
character(c_char), intent(out) :: err(err_length+1)

character(kind=c_char), pointer :: err_p(:)

if (.not.c_associated(err_ptr)) return

call c_f_pointer(err_ptr, err_p, [err_length+1])
err = err_p
deallocate(err_p)

end subroutine

0 comments on commit 66b7fe2

Please sign in to comment.