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 convertToNetCDF output script #718

Merged
merged 1 commit into from
Sep 19, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).


### added
- **scripts** added output script converting all grid-level .mz files to .nc (netCDF)
- **scripts** added output report `EU_report.R` that uses `EU_report.Rmd`
- **scripts** added out of bounds check as output script
- **70_livestock** added realization `fbask_jan16_sticky`
Expand Down
43 changes: 43 additions & 0 deletions scripts/output/extra/convertToNetCDF.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
# | authors, and contributors see CITATION.cff file. This file is part
# | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
# | AGPL-3.0, you are granted additional permissions described in the
# | MAgPIE License Exception, version 1.0 (see LICENSE file).
# | Contact: [email protected]

# --------------------------------------------------------------
# description: Convert all .mz files to NetCDF (.nc) for a magpie directory
# comparison script: FALSE
# ---------------------------------------------------------------

# Version 1.00 - Michael Crawford
# 1.00: first working version

library(gms)
library(magclass)

############################# BASIC CONFIGURATION #######################################
if (!exists("source_include")) {

title <- NULL
outputdir <- NULL

# Define arguments that can be read from command line
readArgs("outputdir", "title")

}
#########################################################################################

message("Converting all .mz outputs to .nc for output directory: ", outputdir)

mzFiles <- list.files(path = outputdir, pattern = "0\\.5(_share)?\\.mz$", full.names = TRUE)

lapply(X = mzFiles, FUN = function(x) {
tryCatch({
magObj <- magclass::read.magpie(x)
newName <- sub("\\.[^.]+$", "", x)
write.magpie(x = magObj, file_name = paste0(newName, ".nc"), file_folder = outputdir, append = FALSE)
}, error = function(e) {
message("Error processing file: ", x, " - ", e$message)
})
})
Loading