Skip to content

Commit

Permalink
7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ahomoudi committed Feb 7, 2024
1 parent df96cb7 commit 604389b
Show file tree
Hide file tree
Showing 35 changed files with 3,630 additions and 54 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ po/*~
# RStudio Connect folder
rsconnect/

# compiled files
*.o
*.so
*.mod

*.html

*.html
QuartoBook/_book/*
QuartoBook/fortran/*
QuartoBook/*.png
toc_*
AquaFortR.Rproj
6 changes: 5 additions & 1 deletion QuartoBook/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ project:

book:
title: "QuartoBook"
author: "Norah Jones"
author: "Ahmed Homoudi"
date: "16/08/2023"
chapters:
- index.qmd
- intro.qmd
- scripts.qmd
- package.qmd
- summary.qmd
- references.qmd

Expand All @@ -16,8 +18,10 @@ bibliography: references.bib
format:
html:
theme: cosmo
code-fold: true
pdf:
documentclass: scrreprt
toc-depth: 2



9 changes: 4 additions & 5 deletions QuartoBook/index.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Preface {.unnumbered}

This is a Quarto book.

To learn more about Quarto books visit <https://quarto.org/docs/books>.
## Acknowledgement {.unnumbered}
This work has been funded by the German Research Foundation (DFG) through the project NFDI4Earth (DFG project no. 460036893, <https://www.nfdi4earth.de/>) within the German National Research Data Infrastructure (NFDI, <https://www.nfdi.de/>).

```{r}
1 + 1
```
## License {.unnumbered}
This book is licensed under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) License (<https://creativecommons.org/licenses/by-nc/4.0/>).
13 changes: 8 additions & 5 deletions QuartoBook/intro.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Introduction

This is a book created from markdown and executable code.
This chapter briefly introduces the R programming language and how to install R on different operating systems. Furthermore, we will have a look at the installation of RStudio. Finally, we will learn briefly about Fortran.

See @knuth84 for additional discussion of literate programming.
The book is designed to cater to individuals with a foundational understanding of programming, irrespective of their familiarity with a specific programming language.

```{r}
1 + 1
```
## R Programming lanaguage
## Installing R
## Installing Rstudio
## Fortran

@intro2rbook
10 changes: 10 additions & 0 deletions QuartoBook/package.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Accelerate R Packages with Fortran

This chapter will focus on wrapping the developed routines from the previous chapter in an R package.

## Brief Introduction to R Packages

## Developing AquaFortR Package
```{r}
1 + 1
```
9 changes: 8 additions & 1 deletion QuartoBook/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ @article{knuth84
numpages = {15}
}


@online{intro2rbook,
author = {Alex Douglas, Deon Roos, Francesca Mancini, Ana Couto, David Lusseau},
title = {{An Introduction to R}},
year = 2023,
month = 09,
url = {https://intro2r.com/},
urldate = {2024-01-08}
}
12 changes: 12 additions & 0 deletions QuartoBook/summary.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ In summary, this book has no content whatsoever.
```{r}
1 + 1
```

Some issues on memory allocation https://masuday.github.io/fortran_tutorial/r.html

OpenMP support https://cran.r-project.org/doc/manuals/R-exts.html#OpenMP-support

using FFTW library

The Need for Speed Part 1: Building an R Package with Fortran (or C) https://www.r-bloggers.com/2018/12/the-need-for-speed-part-1-building-an-r-package-with-fortran-or-c/

Advanced R by Hadley Wickham R’s C interface

Conv and Xcorr shapes https://cran.r-project.org/web/packages/gsignal/gsignal.pdf
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ The repository is structured as follows:
- RPackage
- SwirlCourse

The documentation of the project is rendered [here](https://ahomoudi.github.io/AquaFortR/)

## Installation

To install the AquaFortR package, please use:





## Documentation

The documentation is of the project is rendered [here](https://ahomoudi.github.io/AquaFortR/)


```r
remotes::install_github("AHomoudi/AquaFortR", subdir = "RPackage")
```
## Acknowledgment

This work has been funded by the German Research Foundation (DFG) through the project NFDI4Earth (DFG project no. 460036893, https://www.nfdi4earth.de/) within the German National Research Data Infrastructure (NFDI, https://www.nfdi.de/).
Expand Down
1 change: 1 addition & 0 deletions RPackage/.Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^docs$
^pkgdown$
^\.github$
^data-raw$
4 changes: 3 additions & 1 deletion RPackage/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ License: CC BY 4.0
Encoding: UTF-8
LazyData: true
URL: https://AHomoudi.github.io/AquaFortR/
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
knitr,
learnr,
rmarkdown
VignetteBuilder: knitr
Depends:
R (>= 2.10)
6 changes: 6 additions & 0 deletions RPackage/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(conv2D_f)
export(conv2D_r)
export(xcorr2D_f)
export(xcorr2D_r)
useDynLib(AquaFortR)
useDynLib(AquaFortR, .registration=TRUE)
7 changes: 7 additions & 0 deletions RPackage/R/AquaFortR-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"
#' @name AquaFortR
#' @useDynLib AquaFortR, .registration=TRUE
## usethis namespace: start
## usethis namespace: end
NULL
Empty file added RPackage/R/cape_f.R
Empty file.
Empty file added RPackage/R/cape_r.R
Empty file.
27 changes: 27 additions & 0 deletions RPackage/R/conv2D_f.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' @title 2D Convolution using Fortran.
#'
#' @description This function calculates the 2D Convolution of two matrices
#' `a` and `b` using compiled Fortran subroutine.
#'
#' @param a A matrix (2D array) of values.
#' @param b A matrix (2D array) of values.
#' @return A matrix representing the 2D cross-correlation of the input matrices.
#' @export
#' @examples
#' a <- matrix(c(1, 2, 3, 4), ncol = 2)
#' 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)
)
return(result)
}
55 changes: 55 additions & 0 deletions RPackage/R/conv2D_r.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' @title 2D Convolution using R.
#'
#' @description This function calculates the 2D Convolution of two matrices
#' `a` and `b` using compiled Fortran subroutine.
#'
#' @param a A matrix (2D array) of values.
#' @param b A matrix (2D array) of values.
#' @return A matrix representing the 2D cross-correlation of the input matrices.
#' @export
#' @examples
#' a <- matrix(c(1, 2, 3, 4), ncol = 2)
#' 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)
# the full convolution matrix
conv_row <- nrow(a) + nrow(b) - 1
conv_col <- ncol(a) + ncol(b) - 1
conv <- matrix(1:c(conv_row * conv_col), byrow = FALSE, ncol = conv_col)

# obtain possible shifts
min_row_shift <- -(nrow(b) - 1)
max_row_shift <- (nrow(a) - 1)
min_col_shift <- -(ncol(b) - 1)
max_col_shift <- (ncol(a) - 1)

# Padded matrix
rows_padded <- abs(min_row_shift) + nrow(a) + abs(max_row_shift)
cols_padded <- abs(min_col_shift) + ncol(a) + abs(max_col_shift)
# a
padded_a <- matrix(0, nrow = rows_padded, ncol = cols_padded)
padded_a[
(abs(min_row_shift) + 1):(abs(min_row_shift) + nrow(a)),
(abs(min_col_shift) + 1):(abs(min_col_shift) + ncol(a))
] <- a

for (icol in 1:conv_col) {
for (irow in 1:conv_row) {
iconv <- irow + ((icol - 1) * conv_row)
cols <- (icol):(icol + ncol(b) - 1)
rows <- (irow):(irow + nrow(b) - 1)
# b
padded_b <- array(0, dim = c(rows_padded, cols_padded))
# flip the kernel i.e. b
padded_b[rows, cols] <- b[nrow(b):1, ncol(b):1]

conv[irow, icol] <- sum(padded_a * padded_b)
}
}

return(conv)
}
18 changes: 0 additions & 18 deletions RPackage/R/hello.R

This file was deleted.

11 changes: 11 additions & 0 deletions RPackage/R/radiosonde.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' @title Radiosonde data
#' @description radiosonde example data to calculate CAPE.
#' The data measured at Stuttgart radiosonde station on 2021-06-01 12:00 mid day.
#' For more information about about the
#' variables meaning, please refer to this sheet
#' \url{https://opendata.dwd.de/climate_environment/CDC/help/Abkuerzung_Spaltenname_CDC_20180914.xlsx}
#'
#' @format A data frame with 14 columns and 2962 rows
#' @source German Weather Service (DWD). (2024). Radiosondes data set.
#' Climate Data Center (CDC). \url{https://opendata.dwd.de/}
"radiosonde"
27 changes: 27 additions & 0 deletions RPackage/R/xcorr2D_f.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' @title 2D cross-correlation using Fortran.
#'
#' @description This function calculates the 2D cross-correlation of two matrices
#' `a` and `b` using compiled Fortran subroutine.
#'
#' @param a A matrix (2D array) of values.
#' @param b A matrix (2D array) of values.
#' @return A matrix representing the 2D cross-correlation of the input matrices.
#' @export
#' @examples
#' a <- matrix(c(1, 2, 3, 4), ncol = 2)
#' 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)
)
return(result)
}
54 changes: 54 additions & 0 deletions RPackage/R/xcorr2D_r.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' @title 2D cross-correlation using R.
#'
#' @description This function calculates the 2D cross-correlation of two matrices
#' `a` and `b` using compiled Fortran subroutine.
#'
#' @param a A matrix (2D array) of values.
#' @param b A matrix (2D array) of values.
#' @return A matrix representing the 2D cross-correlation of the input matrices.
#' @export
#' @examples
#' a <- matrix(c(1, 2, 3, 4), ncol = 2)
#' 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)
# the full CC matrix
cc_row <- nrow(a) + nrow(b) - 1
cc_col <- ncol(a) + ncol(b) - 1
cc <- matrix(1:c(cc_row * cc_col), byrow = FALSE, ncol = cc_col)

# obtain possible shifts
min_row_shift <- -(nrow(b) - 1)
max_row_shift <- (nrow(a) - 1)
min_col_shift <- -(ncol(b) - 1)
max_col_shift <- (ncol(a) - 1)

# Padded matrix
rows_padded <- abs(min_row_shift) + nrow(a) + abs(max_row_shift)
cols_padded <- abs(min_col_shift) + ncol(a) + abs(max_col_shift)
# a
padded_a <- matrix(0, nrow = rows_padded, ncol = cols_padded)
padded_a[
(abs(min_row_shift) + 1):(abs(min_row_shift) + nrow(a)),
(abs(min_col_shift) + 1):(abs(min_col_shift) + ncol(a))
] <- a

for (icol in 1:cc_col) {
for (irow in 1:cc_row) {
icc <- irow + ((icol - 1) * cc_row)
cols <- (icol):(icol + ncol(b) - 1)
rows <- (irow):(irow + nrow(b) - 1)
# b
padded_b <- array(0, dim = c(rows_padded, cols_padded))
padded_b[rows, cols] <- b

cc[irow, icol] <- sum(padded_a * padded_b)
}
}

return(cc)
}
Loading

0 comments on commit 604389b

Please sign in to comment.