Commit ed82c678 authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

Improved R2Netcdf and NetCDF2R functions

parent 283d0567
......@@ -38,15 +38,28 @@ Netcdf2R <- function(inFile, variableName) {
# Variable
lVariable <- list(varName = ncid$var[[variableName]]$name, isStandard = TRUE, level = NULL)
attributeList<-ncatt_get(ncid,variableName)
if ('_FillValue' %in% names(attributeList)) {
attributeList['_FillValue']<-NULL
}
for (iAttribute in 1:length(attributeList)) {
attr(lVariable,names(attributeList[iAttribute])) <- attributeList[[iAttribute]]
}
# Members
if ('_FillValue' %in% names(attributeList)) {
lMembers <- ncatt_get(ncid,0)$Members
RData <- c(RData, Members = strsplit(lMembers, ","))
attributeList["Members"]<-NULL
}
# data
lData <- ncvar_get(ncid,variableName)
attr(lData,"dimensions") <- dimensions
# xyCoords
lxyCoords <- list(x = ncid$dim$lon$vals, y = ncid$dim$lat$vals)
attr(lxyCoords,"projection") <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"
# Dates
lDates <- list(start = startDates,end = endDates)
# Combining everything
......@@ -57,18 +70,23 @@ Netcdf2R <- function(inFile, variableName) {
attributeList<-ncatt_get(ncid,0)
if ('InitializationDates' %in% names(attributeList)) {
lInitDates <- ncatt_get(ncid,0)$InitializationDates
RData <- c(RData, InitializationDates = lInitDates)
RData <- c(RData, InitializationDates = strsplit(lInitDates, ","))
attributeList["InitializationDates"]<-NULL
}
# Members
if ('Members' %in% names(attributeList)) {
lMembers <- ncatt_get(ncid,0)$Members
RData <- c(RData, Members = lMembers)
RData <- c(RData, Members = strsplit(lMembers, ","))
attributeList["Members"]<-NULL
}
for (iAttribute in 1:length(attributeList)) {
attr(RData,names(attributeList[iAttribute])) <- attributeList[[iAttribute]]
if(grepl("xyCoords_", names(attributeList[iAttribute]))) {
attr(RData$xyCoords, gsub("xyCoords_", "", names(attributeList[iAttribute]))) <- attributeList[[iAttribute]]
} else {
attr(RData,names(attributeList[iAttribute])) <- attributeList[[iAttribute]]
}
}
nc_close(ncid)
return(RData)
}
\ No newline at end of file
......@@ -68,9 +68,13 @@ R2Netcdf <- function(outFile, RData) {
}
## Global Attributes
if ('InitializationDates' %in% names(RData)) ncatt_put( ncid, 0, "InitializationDates", RData$InitializationDates)
if ('InitializationDates' %in% names(RData)) {
initDateNames<-paste(RData$InitializationDates, sep = ',', collapse = ',')
ncatt_put( ncid, 0, "InitializationDates", initDateNames)
}
if ('Members' %in% names(RData)) {
membernames<-paste(RData$Members, sep = ' ', collapse = ' ')
membernames<-paste(RData$Members, sep = ',', collapse = ',')
ncatt_put( ncid, 0, "Members", membernames)
}
......@@ -90,6 +94,15 @@ R2Netcdf <- function(outFile, RData) {
ncatt_put( ncid, RData$Variable$varName, names(attributeList)[iAttribute], as.character(attributeList[iAttribute[]]))
}
}
## Get all xyCoords attributes and put them in the NetCDF file
attributeList<-attributes(RData$xyCoords)
attributeList["names"]<-NULL
if (length(attributeList) > 0 ) {
for (iAttribute in 1:length(attributeList)) {
ncatt_put( ncid, 0, paste0("xyCoords_", names(attributeList)[iAttribute]), as.character(attributeList[iAttribute[]]))
}
}
# length(attributeList)
## Close Netcdf file
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment