Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to write units in netCDF files based on comment argument #106

Merged
merged 6 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ValidationKey: '11447946'
ValidationKey: '11489376'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: unable to verify current time
AutocreateReadme: yes
UseGithubActions: yes
allowLinterWarnings: yes
79 changes: 79 additions & 0 deletions .github/workflows/lucode2-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Run CI for R using https://eddelbuettel.github.io/r-ci/

name: lucode2-check

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

env:
USE_BSPM: "true"
_R_CHECK_FORCE_SUGGESTS_: "false"
NO_BINARY_INSTALL_R_PACKAGES: 'c("madrat", "magclass", "citation", "gms", "goxygen")'

jobs:
lucode2-check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Bootstrap
run: |
curl -OLs https://eddelbuettel.github.io/r-ci/run.sh
chmod 0755 run.sh
./run.sh bootstrap

- name: Enable r-universe repo, modify bspm integration
run: |
# install packages from https://pik-piam.r-universe.dev and CRAN
echo '
options(repos = c(universe = "https://pik-piam.r-universe.dev",
CRAN = "https://cloud.r-project.org"))
' >> .Rprofile
cat .Rprofile
# modify bspm integration to never install binary builds of PIK CRAN packages
sudo sed -i '/bspm::enable()/d' /etc/R/Rprofile.site
# need double % because of printf, %s is replaced with "$NO_BINARY_INSTALL_R_PACKAGES" (see "env:" above)
printf '
local({
expr <- quote({
if (!is.null(repos)) {
noBinaryInstallRPackages <- %s
pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]),
pkgs[pkgs %%in%% noBinaryInstallRPackages])
}
})
trace(utils::install.packages, expr, print = FALSE)
})
' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null
cat /etc/R/Rprofile.site

- uses: r-lib/actions/setup-pandoc@v1

- name: Install dependencies
run: |
./run.sh install_aptget libhdf5-dev
./run.sh install_all
./run.sh install_r lucode2 covr

- name: Remove bspm integration # to get rid of error when running install.packages
run: |
sudo sed -i '/ trace(utils::install.packages, expr, print = FALSE)/d' /etc/R/Rprofile.site
cat /etc/R/Rprofile.site

- name: Verify validation key
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)

- name: Checks
shell: Rscript {0}
run: |
options(crayon.enabled = TRUE)
lucode2::check(runLinter = FALSE)

- name: Test coverage
shell: Rscript {0}
run: covr::codecov(quiet = FALSE)
67 changes: 0 additions & 67 deletions .github/workflows/test-buildlibrary.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "magclass: Data Class and Tools for Handling Spatial-Temporal Data",
"version": "6.0.6",
"version": "6.0.8",
"description": "<p>Data class for increased interoperability working with spatial-\n temporal data together with corresponding functions and methods (conversions,\n basic calculations and basic data manipulation). The class distinguishes\n between spatial, temporal and other dimensions to facilitate the development\n and interoperability of tools build for it. Additional features are name-based\n addressing of data and internal consistency checks (e.g. checking for the right\n data order in calculations).<\/p>",
"creators": [
{
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: magclass
Type: Package
Title: Data Class and Tools for Handling Spatial-Temporal Data
Version: 6.0.6
Date: 2021-09-21
Version: 6.0.8
Date: 2021-09-27
Authors@R: c(person("Jan Philipp", "Dietrich", email = "[email protected]", role = c("aut","cre")),
person("Benjamin Leon", "Bodirsky", email = "[email protected]", role = "aut"),
person("Markus", "Bonsch", role = "aut"),
Expand Down Expand Up @@ -51,5 +51,5 @@ BugReports: https://github.com/pik-piam/magclass/issues
License: LGPL-3 | file LICENSE
LazyData: true
Encoding: UTF-8
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
VignetteBuilder: knitr
17 changes: 14 additions & 3 deletions R/write.magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
#' [ sets | Set names with \\n as delimiter | character | 1*nchar_sets Byte] \cr
#' [ metadata | serialized metadata information (currently not in use) | bytes | 1*nbyte_metadata Byte] \cr
#'
#' @author Jan Philipp Dietrich, Stephen Bi
#' @author Jan Philipp Dietrich, Stephen Bi, Florian Humpenoeder
#' @seealso \code{"\linkS4class{magpie}"},
#' \code{\link{read.magpie}},\code{\link{mbind}}
#' @examples
Expand Down Expand Up @@ -197,15 +197,26 @@ write.magpie <- function(x, file_name, file_folder = "", file_type = NULL, appen
varnames <- getItems(x, dim = 3)
zunit <- ifelse(all(isYear(getYears(x))), "years", "")
if (is.null(varnames)) varnames <- "Variable"
if (is.null(comment)) {
unit <- "not specified"
} else {
indicators <- substring(text = comment, first = 1, last = regexpr(pattern = ": ", text = comment) - 1)
units <- substring(text = comment, first = (regexpr(pattern = ": ", text = comment) + 2))
if (!any(indicators == "unit")) {
unit <- "not specified"
} else {
unit <- units[which(indicators == "unit")]
}
}
raster::writeRaster(.sub(rx, varnames[1]), filename = filePath, format = "CDF", overwrite = TRUE,
compression = 9, zname = "Time", zunit = zunit, varname = varnames[1], ...)
compression = 9, zname = "Time", zunit = zunit, varname = varnames[1], varunit = unit, ...)
nc <- ncdf4::nc_open(filePath, write = TRUE)
if (zunit == "years") {
ncdf4::ncvar_put(nc, "Time", getYears(x, as.integer = TRUE))
}
if (length(varnames) > 1) {
for (i in varnames[-1]) {
nc <- ncdf4::ncvar_add(nc, ncdf4::ncvar_def(i, "", nc$dim, compression = 9))
nc <- ncdf4::ncvar_add(nc, ncdf4::ncvar_def(i, unit, nc$dim, compression = 9))
ncdf4::ncvar_put(nc, i, aperm(as.array(.sub(rx, i)), c(2, 1, 3)))
}
}
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Data Class and Tools for Handling Spatial-Temporal Data

R package **magclass**, version **6.0.6**
R package **magclass**, version **6.0.8**

[![CRAN status](https://www.r-pkg.org/badges/version/magclass)](https://cran.r-project.org/package=magclass) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158580.svg)](https://doi.org/10.5281/zenodo.1158580) [![R build status](https://github.com/pik-piam/magclass/workflows/check/badge.svg)](https://github.com/pik-piam/magclass/actions) [![codecov](https://codecov.io/gh/pik-piam/magclass/branch/master/graph/badge.svg)](https://codecov.io/gh/pik-piam/magclass) [![r-universe](https://pik-piam.r-universe.dev/badges/magclass)](https://pik-piam.r-universe.dev/ui#builds)
[![CRAN status](https://www.r-pkg.org/badges/version/magclass)](https://cran.r-project.org/package=magclass) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158580.svg)](https://doi.org/10.5281/zenodo.1158580) [![R build status](https://github.com/pik-piam/magclass/workflows/check/badge.svg)](https://github.com/pik-piam/magclass/actions) [![codecov](https://codecov.io/gh/pik-piam/magclass/branch/master/graph/badge.svg)](https://codecov.io/gh/pik-piam/magclass) [![r-universe](https://pik-piam.r-universe.dev/badges/magclass)](https://pik-piam.r-universe.dev/ui#builds)

## Purpose and Functionality

Expand Down Expand Up @@ -55,7 +55,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **magclass** in publications use:

Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D (2021). _magclass: Data Class and Tools for Handling Spatial-Temporal Data_. doi: 10.5281/zenodo.1158580 (URL: https://doi.org/10.5281/zenodo.1158580), R package version 6.0.6, <URL: https://github.com/pik-piam/magclass>.
Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D (2021). _magclass: Data Class and Tools for Handling Spatial-Temporal Data_. doi: 10.5281/zenodo.1158580 (URL: https://doi.org/10.5281/zenodo.1158580), R package version 6.0.8, <URL: https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

Expand All @@ -64,7 +64,7 @@ A BibTeX entry for LaTeX users is
title = {magclass: Data Class and Tools for Handling Spatial-Temporal Data},
author = {Jan Philipp Dietrich and Benjamin Leon Bodirsky and Markus Bonsch and Florian Humpenoeder and Stephen Bi and Kristine Karstens and Debbora Leip},
year = {2021},
note = {R package version 6.0.6},
note = {R package version 6.0.8},
doi = {10.5281/zenodo.1158580},
url = {https://github.com/pik-piam/magclass},
}
Expand Down
2 changes: 1 addition & 1 deletion man/write.magpie.Rd

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