Skip to content

Commit

Permalink
Rpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
ahomoudi committed Apr 13, 2024
1 parent fd7cfaf commit 75a4d69
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![License](https://img.shields.io/badge/License-CCBY-blue)](#license)
<!-- badges: end -->

This is the official repository of the Project AquaFortR: Streamlining Atmospheric Science, Oceanography, Climate, and Water Research with Fortran-accelerated R.
This is the official repository of the project AquaFortR: Streamlining Atmospheric Science, Oceanography, Climate, and Water Research with Fortran-accelerated R.

The repository is structured as follows:

Expand Down Expand Up @@ -38,7 +38,7 @@ swirl::install_course_zip("path/to/AquaFortR_Swirl.zip")

### Book

Materials for the chapter 2 are available <a href="AquaFortR_Codes.zip">here</a>. Please,
Materials for Chapter 2 are available <a href="AquaFortR_Codes.zip">here</a>. Please,
revise the path to the shared libraries files in the R-Fortran functions.

## Acknowledgment
Expand Down
1 change: 0 additions & 1 deletion RPackage/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export(mixing_ratio_from_dewpoint)
export(saturation_vapour_pressure)
export(xcorr2D_f)
export(xcorr2D_r)
useDynLib(AquaFortR)
useDynLib(AquaFortR, .registration=TRUE)
25 changes: 12 additions & 13 deletions RPackage/R/cape_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,26 @@
#' vtc = TRUE
#' )
#' @author Klemens Barfus (Original in Fortran), Ahmed Homoudi (Integration in R)
#' @useDynLib AquaFortR
#' @export
cape_f <- function(t_parcel, dwpt_parcel, mr_parcel,
p_profile, t_profile, mr_profile,
vtc = TRUE) {
DIM <- c(
length(p_profile),
4,
as.integer(vtc)
)

vtc <-as.integer(vtc)

result <- .Call(
c_cape_f,
as.double(t_parcel),
as.double(dwpt_parcel),
as.double(mr_parcel),
as.double(p_profile),
as.double(t_profile),
as.double(mr_profile),
as.integer(DIM)
t_parcel,
dwpt_parcel,
mr_parcel,
p_profile,
t_profile,
mr_profile,
vtc
)

names(result) <- c("CAPE", "CIN", "p_LCL", "p_LFC")
return(result)
}


1 change: 0 additions & 1 deletion RPackage/R/cape_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#' vtc = TRUE
#' )
#' @author Klemens Barfus (Original in Fortran), Ahmed Homoudi (Translation to R)
#' @useDynLib AquaFortR
#' @export
cape_r <- function(t_parcel, dwpt_parcel, mr_parcel,
p_profile, t_profile, mr_profile,
Expand Down
7 changes: 2 additions & 5 deletions RPackage/R/conv2D_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
#' b <- matrix(c(5, 6, 7, 8), ncol = 2)
#' conv2D_f(a, b)
#' @author Ahmed Homoudi
#' @useDynLib AquaFortR
#' @export
conv2D_f <- function(a, b) {
stopifnot(length(dim(a)) == 2 | length(dim(b)) == 2)
result <- .Call(
c_conv2d_f,
as.integer(dim(a)),
as.double(a),
as.integer(dim(b)),
as.double(b)
a,
b
)
return(result)
}
1 change: 0 additions & 1 deletion RPackage/R/conv2D_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' b <- matrix(c(5, 6, 7, 8), ncol = 2)
#' conv2D_f(a, b)
#' @author Ahmed Homoudi
#' @useDynLib AquaFortR
#' @export
conv2D_r <- function(a, b) {
stopifnot(length(dim(a)) == 2 | length(dim(b)) == 2)
Expand Down
7 changes: 2 additions & 5 deletions RPackage/R/xcorr2D_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
#' b <- matrix(c(5, 6, 7, 8), ncol = 2)
#' xcorr2D_f(a, b)
#' @author Ahmed Homoudi
#' @useDynLib AquaFortR
#' @export
xcorr2D_f <- function(a, b) {
stopifnot(length(dim(a)) == 2 | length(dim(b)) == 2)
result <- .Call(
c_xcorr2d_f,
as.integer(dim(a)),
as.double(a),
as.integer(dim(b)),
as.double(b)
a,
b
)
return(result)
}
1 change: 0 additions & 1 deletion RPackage/R/xcorr2D_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' b <- matrix(c(5, 6, 7, 8), ncol = 2)
#' xcorr2D_f(a, b)
#' @author Ahmed Homoudi
#' @useDynLib AquaFortR
#' @export
xcorr2D_r <- function(a, b) {
stopifnot(length(dim(a)) == 2 | length(dim(b)) == 2)
Expand Down
5 changes: 3 additions & 2 deletions RPackage/man/AquaFortR-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion RPackage/man/saturation_vapour_pressure.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 22 additions & 26 deletions RPackage/src/AquaFortRmodulec.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
#define R_NO_REMAP
#include <R.h>
#include <Rinternals.h>
#include <stdlib.h> // for NULL
#include <Rmath.h>
#include <R_ext/Rdynload.h>
#include <R_ext/Arith.h>

void F77_NAME(xcorr2d_f)(int m, int n, int p, int q, int k, int l, double *a, double *b, double *cc);
void F77_NAME(conv2d_f)(int m, int n, int p, int q, int k, int l, double *a, double *b, double *conv);
void F77_NAME(cape_f)(double *t_parcel, double *dwpt_parcel, double *mr_parcel, int nlevels, double *p_profile,
double *t_profile, double *mr_profile, int vtc, int nresult, double *result);

extern SEXP c_xcorr2d_f(SEXP dim_a, SEXP a, SEXP dim_b, SEXP b)
extern SEXP c_xcorr2d_f(SEXP a, SEXP b)
{
int m = INTEGER(dim_a)[0];
int n = INTEGER(dim_a)[1];
int m = Rf_nrows(a);
int n = Rf_ncols(a);
//
int p = INTEGER(dim_b)[0];
int q = INTEGER(dim_b)[1];
int p = Rf_nrows(b);
int q = Rf_ncols(b);
//
int k = m + p - 1;
int l = n + q - 1;

SEXP ret;
PROTECT(ret = allocMatrix(REALSXP, k, l));
PROTECT(ret = Rf_allocMatrix(REALSXP, k, l));
F77_CALL(xcorr2d_f)
(m, n, p, q, k, l, REAL(a), REAL(b), REAL(ret));
UNPROTECT(1);
return (ret);
}

extern SEXP c_conv2d_f(SEXP dim_a, SEXP a, SEXP dim_b, SEXP b)
extern SEXP c_conv2d_f(SEXP a, SEXP b)
{
int m = INTEGER(dim_a)[0];
int n = INTEGER(dim_a)[1];
int m = Rf_nrows(a);
int n = Rf_ncols(a);
//
int p = INTEGER(dim_b)[0];
int q = INTEGER(dim_b)[1];
int p = Rf_nrows(b);
int q = Rf_ncols(b);
//
int k = m + p - 1;
int l = n + q - 1;

SEXP ret;
PROTECT(ret = allocMatrix(REALSXP, k, l));
PROTECT(ret = Rf_allocMatrix(REALSXP, k, l));
F77_CALL(conv2d_f)
(m, n, p, q, k, l, REAL(a), REAL(b), REAL(ret));
UNPROTECT(1);
Expand All @@ -50,34 +48,32 @@ extern SEXP c_conv2d_f(SEXP dim_a, SEXP a, SEXP dim_b, SEXP b)

extern SEXP c_cape_f(SEXP t_parcel, SEXP dwpt_parcel, SEXP mr_parcel,
SEXP p_profile, SEXP t_profile, SEXP mr_profile,
SEXP DIM)
SEXP vtc)
{
int nlevels = INTEGER(DIM)[0];
int nret = INTEGER(DIM)[1];
int vtc = INTEGER(DIM)[2];
int nlevels = Rf_length(t_profile);
int vtc2 = INTEGER(vtc)[0];
int nret = 4;

SEXP ret;
/* Order: CAPE, CIN, p_LCL, p_LFC */
PROTECT(ret = allocVector(REALSXP, 4));
PROTECT(ret = Rf_allocVector(REALSXP, 4));
F77_CALL(cape_f)
(REAL(t_parcel), REAL(dwpt_parcel), REAL(mr_parcel), nlevels,
REAL(p_profile), REAL(t_profile), REAL(mr_profile), vtc,
REAL(p_profile), REAL(t_profile), REAL(mr_profile), vtc2,
nret, REAL(ret));
UNPROTECT(1);
return (ret);
}

static const R_CallMethodDef CallEntries[] = {
{"c_xcorr2d_f", (DL_FUNC)&c_xcorr2d_f, 4},
{"c_conv2d_f", (DL_FUNC)&c_conv2d_f, 4},
{"c_xcorr2d_f", (DL_FUNC)&c_xcorr2d_f, 2},
{"c_conv2d_f", (DL_FUNC)&c_conv2d_f, 2},
{"c_cape_f", (DL_FUNC)&c_cape_f, 7},
{NULL, NULL, 0}};

static const R_FortranMethodDef FEntries[] = {
{NULL, NULL, 0}};

void R_init_AquaFortR(DllInfo *dll)
{
R_registerRoutines(dll, NULL, CallEntries, FEntries, NULL);
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
1 change: 1 addition & 0 deletions RPackage/src/AquaFortRmodulef.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module AquaFortRmodule

public :: xcorr2d_f
public :: conv2d_f
public :: cape_f
contains

! Cross-Correlation
Expand Down

0 comments on commit 75a4d69

Please sign in to comment.