Skip to content

Commit

Permalink
Improved the clima wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Oct 26, 2022
1 parent 1f8fce1 commit 3335eba
Show file tree
Hide file tree
Showing 23 changed files with 866 additions and 446 deletions.
5 changes: 3 additions & 2 deletions clima/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
os.environ['OMP_NUM_THREADS'] = '1'

from ._clima import WaterAdiabatClimate

from ._clima import WaterAdiabatClimate, ClimaException
from ._clima import rebin # rebin routine from futils
6 changes: 5 additions & 1 deletion clima/cython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ endif()

add_library(_clima MODULE ${_clima})
set(_clima_deps
futils.pyx
OpticalProperties.pyx
ClimaRadtranWrk.pyx
Radtran.pyx
WaterAdiabatClimate.pyx
)
foreach(PDEP ${_clima_deps})
Expand All @@ -15,5 +19,5 @@ endforeach()

python_extension_module(_clima)

target_link_libraries(_clima clima_wrapper)
target_link_libraries(_clima clima_c_api)
target_include_directories(_clima PUBLIC ${NumPy_INCLUDE_DIRS})
44 changes: 44 additions & 0 deletions clima/cython/ClimaRadtranWrk.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
cimport ClimaRadtranWrk_pxd as rwrk_pxd

cdef class ClimaRadtranWrk:
cdef void *_ptr

def __init__(self):
pass

def __dealloc__(self):
pass

property fup_a:
def __get__(self):
cdef int dim1, dim2
rwrk_pxd.climaradtranwrk_fup_a_get_size(&self._ptr, &dim1, &dim2)
cdef ndarray arr = np.empty((dim1, dim2), np.double, order="F")
rwrk_pxd.climaradtranwrk_fup_a_get(&self._ptr, &dim1, &dim2, <double *>arr.data)
return arr

property fdn_a:
def __get__(self):
cdef int dim1, dim2
rwrk_pxd.climaradtranwrk_fdn_a_get_size(&self._ptr, &dim1, &dim2)
cdef ndarray arr = np.empty((dim1, dim2), np.double, order="F")
rwrk_pxd.climaradtranwrk_fdn_a_get(&self._ptr, &dim1, &dim2, <double *>arr.data)
return arr

property fup_n:
def __get__(self):
cdef int dim1
rwrk_pxd.climaradtranwrk_fup_n_get_size(&self._ptr, &dim1)
cdef ndarray arr = np.empty(dim1, np.double)
rwrk_pxd.climaradtranwrk_fup_n_get(&self._ptr, &dim1, <double *>arr.data)
return arr

property fdn_n:
def __get__(self):
cdef int dim1
rwrk_pxd.climaradtranwrk_fdn_n_get_size(&self._ptr, &dim1)
cdef ndarray arr = np.empty(dim1, np.double)
rwrk_pxd.climaradtranwrk_fdn_n_get(&self._ptr, &dim1, <double *>arr.data)
return arr


14 changes: 14 additions & 0 deletions clima/cython/ClimaRadtranWrk_pxd.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# getters and setters

cdef extern void climaradtranwrk_fup_a_get_size(void *ptr, int *dim1, int *dim2)
cdef extern void climaradtranwrk_fup_a_get(void *ptr, int *dim1, int *dim2, double *arr)

cdef extern void climaradtranwrk_fdn_a_get_size(void *ptr, int *dim1, int *dim2)
cdef extern void climaradtranwrk_fdn_a_get(void *ptr, int *dim1, int *dim2, double *arr)

cdef extern void climaradtranwrk_fup_n_get_size(void *ptr, int *dim1)
cdef extern void climaradtranwrk_fup_n_get(void *ptr, int *dim1, double *arr)

cdef extern void climaradtranwrk_fdn_n_get_size(void *ptr, int *dim1)
cdef extern void climaradtranwrk_fdn_n_get(void *ptr, int *dim1, double *arr)
26 changes: 26 additions & 0 deletions clima/cython/OpticalProperties.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cimport OpticalProperties_pxd as op_pxd

cdef class OpticalProperties:
cdef void *_ptr

def __init__(self):
pass

def __dealloc__(self):
pass

property wavl:
def __get__(self):
cdef int dim1
op_pxd.opticalproperties_wavl_get_size(&self._ptr, &dim1)
cdef ndarray arr = np.empty(dim1, np.double)
op_pxd.opticalproperties_wavl_get(&self._ptr, &dim1, <double *>arr.data)
return arr

property freq:
def __get__(self):
cdef int dim1
op_pxd.opticalproperties_freq_get_size(&self._ptr, &dim1)
cdef ndarray arr = np.empty(dim1, np.double)
op_pxd.opticalproperties_freq_get(&self._ptr, &dim1, <double *>arr.data)
return arr
8 changes: 8 additions & 0 deletions clima/cython/OpticalProperties_pxd.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# getters and setters

cdef extern void opticalproperties_wavl_get_size(void *ptr, int *dim1)
cdef extern void opticalproperties_wavl_get(void *ptr, int *dim1, double *arr)

cdef extern void opticalproperties_freq_get_size(void *ptr, int *dim1)
cdef extern void opticalproperties_freq_get(void *ptr, int *dim1, double *arr)
46 changes: 46 additions & 0 deletions clima/cython/Radtran.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
cimport Radtran_pxd as rad_pxd

cdef class Radtran:
cdef void *_ptr

def __init__(self):
pass

def __dealloc__(self):
pass

property ir:
def __get__(self):
cdef void *ptr1;
rad_pxd.radtran_ir_get(&self._ptr, &ptr1)
var = OpticalProperties()
var._ptr = ptr1
return var

property sol:
def __get__(self):
cdef void *ptr1;
rad_pxd.radtran_sol_get(&self._ptr, &ptr1)
var = OpticalProperties()
var._ptr = ptr1
return var

property wrk_ir:
def __get__(self):
cdef void *ptr1;
rad_pxd.radtran_wrk_ir_get(&self._ptr, &ptr1)
var = ClimaRadtranWrk()
var._ptr = ptr1
return var

property wrk_sol:
def __get__(self):
cdef void *ptr1;
rad_pxd.radtran_wrk_sol_get(&self._ptr, &ptr1)
var = ClimaRadtranWrk()
var._ptr = ptr1
return var




9 changes: 9 additions & 0 deletions clima/cython/Radtran_pxd.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# getters and setters
cdef extern void radtran_ir_get(void *ptr, void *ptr1);

cdef extern void radtran_sol_get(void *ptr, void *ptr1);

cdef extern void radtran_wrk_ir_get(void *ptr, void *ptr1);

cdef extern void radtran_wrk_sol_get(void *ptr, void *ptr1);
8 changes: 8 additions & 0 deletions clima/cython/WaterAdiabatClimate.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ cdef class WaterAdiabatClimate:
wa_pxd.wateradiabatclimate_species_names_get(&self._ptr, &dim1, <char *>species_names_c.data)
return c2stringarr(species_names_c, S_STR_LEN, dim1)

property rad:
def __get__(self):
cdef void *ptr1
wa_pxd.wateradiabatclimate_rad_get(&self._ptr, &ptr1)
var = Radtran()
var._ptr = ptr1
return var

property P:
def __get__(self):
cdef int dim1
Expand Down
4 changes: 4 additions & 0 deletions clima/cython/WaterAdiabatClimate_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cdef extern from "<stdbool.h>":
cdef extern void allocate_wateradiabatclimate(void *ptr);
cdef extern void deallocate_wateradiabatclimate(void *ptr);

# wrappers for functions
cdef extern void wateradiabatclimate_create_wrapper(void *ptr, char *data_dir, char *species_file,
char *settings_file, char *flux_file, char *err);

Expand All @@ -31,6 +32,7 @@ cdef extern void wateradiabatclimate_to_regular_grid_wrapper(void *ptr, char *er

cdef extern void wateradiabatclimate_out2atmosphere_txt_wrapper(void *ptr, char *filename, int *nz, double *eddy, bool *overwrite, bool *clip, char *err)

# getters and setters
cdef extern void wateradiabatclimate_p_top_get(void *ptr, double *val)
cdef extern void wateradiabatclimate_p_top_set(void *ptr, double *val)

Expand Down Expand Up @@ -61,4 +63,6 @@ cdef extern void wateradiabatclimate_dz_get(void *ptr, int *dim1, double *arr)
cdef extern void wateradiabatclimate_densities_get_size(void *ptr, int *dim1, int *dim2)
cdef extern void wateradiabatclimate_densities_get(void *ptr, int *dim1, int *dim2, double *arr)

cdef extern void wateradiabatclimate_rad_get(void *ptr, void *ptr1)


4 changes: 4 additions & 0 deletions clima/cython/_clima.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import os
DEF S_STR_LEN = 20;
DEF ERR_LEN = 1024;

include "futils.pyx"
include "OpticalProperties.pyx"
include "ClimaRadtranWrk.pyx"
include "Radtran.pyx"
include "WaterAdiabatClimate.pyx"

# utils
Expand Down
19 changes: 19 additions & 0 deletions clima/cython/futils.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cimport futils_pxd as f_pxd

cpdef rebin(ndarray[double, ndim=1] old_bins, ndarray[double, ndim=1] old_vals, ndarray[double, ndim=1] new_bins):
cdef int n_old = old_bins.shape[0] - 1
cdef int n_new = new_bins.shape[0] - 1
cdef ndarray new_vals = np.empty(n_new, np.double)
cdef int ierr

if old_vals.shape[0] != n_old:
raise ValueError('"old_bins" and "old_vals" arguments to "rebin" have incompatible shapes')

f_pxd.futils_rebin_wrapper(&n_old, <double *> old_bins.data, <double *> old_vals.data,
&n_new, <double *> new_bins.data, <double *> new_vals.data, &ierr)
if ierr < 0:
raise Exception("rebin returned error code: "+str(ierr))

return new_vals


2 changes: 2 additions & 0 deletions clima/cython/futils_pxd.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cdef extern void futils_rebin_wrapper(int *n_old, double *old_bins, double *old_vals,
int *n_new, double *new_bins, double *new_vals, int *ierr)
17 changes: 13 additions & 4 deletions clima/fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@

add_library(clima_wrapper
wrapper_utils.f90
WaterAdiabatClimate_wrapper.f90
add_library(clima_c_api
clima_c_api.f90
)
target_link_libraries(clima_wrapper clima)
set(clima_c_api_deps
futils.f90
OpticalProperties.f90
ClimaRadtranWrk.f90
Radtran.f90
WaterAdiabatClimate.f90
)
foreach(PDEP ${_clima_deps})
set_source_files_properties(clima_c_api.f90 PROPERTIES OBJECT_DEPENDS ${PDEP})
endforeach()
target_link_libraries(clima_c_api clima)
84 changes: 84 additions & 0 deletions clima/fortran/ClimaRadtranWrk.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
! ClimaRadtranWrk

!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! getters and setters !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!

subroutine climaradtranwrk_fup_a_get_size(ptr, dim1, dim2) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(out) :: dim1, dim2
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
dim1 = size(w%fup_a,1)
dim2 = size(w%fup_a,2)
end subroutine

subroutine climaradtranwrk_fup_a_get(ptr, dim1, dim2, arr) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(in) :: dim1, dim2
real(c_double), intent(out) :: arr(dim1,dim2)
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
arr = w%fup_a
end subroutine

subroutine climaradtranwrk_fdn_a_get_size(ptr, dim1, dim2) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(out) :: dim1, dim2
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
dim1 = size(w%fdn_a,1)
dim2 = size(w%fdn_a,2)
end subroutine

subroutine climaradtranwrk_fdn_a_get(ptr, dim1, dim2, arr) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(in) :: dim1, dim2
real(c_double), intent(out) :: arr(dim1,dim2)
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
arr = w%fdn_a
end subroutine

subroutine climaradtranwrk_fup_n_get_size(ptr, dim1) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(out) :: dim1
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
dim1 = size(w%fup_n,1)
end subroutine

subroutine climaradtranwrk_fup_n_get(ptr, dim1, arr) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(in) :: dim1
real(c_double), intent(out) :: arr(dim1)
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
arr = w%fup_n
end subroutine

subroutine climaradtranwrk_fdn_n_get_size(ptr, dim1) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(out) :: dim1
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
dim1 = size(w%fdn_n,1)
end subroutine

subroutine climaradtranwrk_fdn_n_get(ptr, dim1, arr) bind(c)
use clima_radtran, only: ClimaRadtranWrk
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(in) :: dim1
real(c_double), intent(out) :: arr(dim1)
type(ClimaRadtranWrk), pointer :: w
call c_f_pointer(ptr, w)
arr = w%fdn_n
end subroutine

Loading

0 comments on commit 3335eba

Please sign in to comment.