Skip to content

Commit

Permalink
Added convertToNetCDF output script
Browse files Browse the repository at this point in the history
  • Loading branch information
mscrawford committed Sep 11, 2024
1 parent 2a2e344 commit e801bcd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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)
})
})

0 comments on commit e801bcd

Please sign in to comment.