Commit 2a09c227 authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

bugfix R2NetCDF. At Revision 1.3

parent 1c89791a
......@@ -14,7 +14,7 @@ if (submitscript) {
locName <- 'X'
resolution <- 'X'
inPath <- sprintf("../DATA/System4_seasonal_15_rev1.0/%s_%s", locName, resolution)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.2/%sdeg/%s_noBC", resolution, locName)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.3/%sdeg/%s_noBC", resolution, locName)
} else {
members <- c(1:2)
initYears <- c(1981:1982)
......@@ -23,7 +23,7 @@ if (submitscript) {
#locName <- "EU"
resolution <- "0.75"
inPath <- sprintf("../DATA/System4_seasonal_15_rev1.0/%s_%s", locName, resolution)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.2/%sdeg/%s_noBC", resolution, locName)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.3/%sdeg/%s_noBC", resolution, locName)
}
mask<-Netcdf2R(inFile = sprintf("../DATA/mask/mask_wfdei_%s_%s.nc4", locName, resolution), "mask")
......
rm(list=ls())
library(fields) # e.g: using the fields library
library(abind)
library(ncdf4)
library(ecomsUDG.Raccess)
source(file = "./functions/functionsGeneral.R")
source(file = "./functions/functionReformat.R")
source(file = "./functions/functionNetcdf2R.R")
source(file = "./functions/functionR2Netcdf.R")
source(file = "./functions/infoGeneral.R")
......@@ -17,8 +13,8 @@ if (submitscript) {
locName <- 'X'
resolution <- 'X'
bcInfo <- 'X'
inPath <- sprintf("../DATA/System4_seasonal_15_rev1.2/%sdeg/%s_%s", resolution, locName, bcInfo)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.2/%sdeg/%s_%s_biasformat", resolution, locName, bcInfo)
inPath <- sprintf("../DATA/System4_seasonal_15_rev1.3/%sdeg/%s_%s", resolution, locName, bcInfo)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.3/%sdeg/%s_%s_biasformat", resolution, locName, bcInfo)
} else {
members <- c(1:2)
initYears <- c(1981:1982)
......@@ -28,41 +24,97 @@ if (submitscript) {
resolution <- "0.75"
# bcInfo <- "BC"
bcInfo <- "noBC"
inPath <- sprintf("../DATA/System4_seasonal_15_rev1.2/%sdeg/%s_%s", resolution, locName, bcInfo)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.2/%sdeg/%s_%s_biasformat", resolution, locName, bcInfo)
inPath <- sprintf("../DATA/System4_seasonal_15_rev1.3/%sdeg/%s_%s", resolution, locName, bcInfo)
outPath <- sprintf("../DATA/System4_seasonal_15_rev1.3/%sdeg/%s_%s_biasformat2", resolution, locName, bcInfo)
}
variables<-names(variableInfo)
dir.create(outPath, recursive = TRUE, showWarnings = FALSE)
leadMonths<-c(0:6)
dir.create(outPath, recursive = TRUE, showWarnings = FALSE)
print("start")
for (initMonth in initMonths) {
for (variableName in variables) {
inFile <- sprintf("%s/%s/%s_forcing_seas15_%s_%s_E<MEMBERS>_INIT<YEARS>_%02d.nc4", inPath, variableName, variableName, locName, bcInfo, initMonth)
print(inFile)
RDataAllLeadMonths <- reformat2Bias(members, initYears, initMonth, variableName, locName, iFile = inFile)
allocatedOutputArray<-FALSE
strMembers<-NULL
strInitDates<-NULL
# indexesOfDaysYears(1,sYear,eYear)
for (initYear in initYears) {
for (iMember in members) {
initDates<-NULL
iInitYear<-1
indexesYearsLeadMonths<-indexesForYearsPerLeadMonth(initYears[1],initYears[length(initYears)],initMonth)
## Open file...
inFile <- sprintf("%s/%s/%s_forcing_seas15_%s_%s_E%02d_INIT%04d_%02d", inPath, variableName, variableName, locName, bcInfo, iMember, initYear, initMonth)
RData <- Netcdf2R(inFile = sprintf("%s.nc4",inFile), variableName)
if (!allocatedOutputArray) {
## allocate arrays
finalList<-NULL
listNames<-NULL
for (iLM in 1:7) {
finalList[[iLM]] <- RData
listNames<-c(listNames,paste0("LeadMonth_",iLM-1))
totalDays<-indexesYearsLeadMonths$eIndexes[as.character(initYears[length(initYears)]),iLM]
finalList[[iLM]]$Data<-array(0,dim=c(length(members),totalDays,length(RData$xyCoords$y),length(RData$xyCoords$x)))
finalList[[iLM]]$Dates$start<-character(length=totalDays)
finalList[[iLM]]$Dates$end<-character(length=totalDays)
attr(finalList[[iLM]]$Data,"dimensions") <- c("member","time","lat","lon")
}
names(finalList)<-listNames
allocatedOutputArray<-TRUE
}
## Split the seven months
indexes<-indexesOfDaysPerMonth(initMonth, 7, initYear = initYear)
## Split the data in seperate months
parts<-NULL
for (iLM in 1:7) {
month <- indexes[iLM,"month"]
year <- indexes[iLM,"year"]
datesInCurrMonth <- seq(as.Date(as.Date(sprintf("%04d-%02d-01", year, month))),by = "1 day", length=indexes[iLM,"nDaysTotalInMonth"])
iDates <- c(indexesYearsLeadMonths$sIndexes[as.character(initYear),iLM]:indexesYearsLeadMonths$eIndexes[as.character(initYear),iLM])
finalList[[iLM]]$Dates$start[iDates] <- RData$Dates$start[indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"]]
finalList[[iLM]]$Dates$end[iDates] <- RData$Dates$end[indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"]]
finalList[[iLM]]$Data[iMember,iDates,,] <- RData$Data[indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"],,]
tmp<-c(indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"])
}
initDates[iInitYear]<-RData$Dates$start[1]
iInitYear<-iInitYear+1
if (iMember == members[1]) {
strInitDates<-c(strInitDates, RData$InitializationDates)
}
}
strMembers<-c(strMembers,paste0("Member_",iMember))
for (iLM in 1:7) {
finalList[[iLM]]$Members <- strMembers
finalList[[iLM]]$InitializationDates <- strInitDates
}
}
for (leadMonth in leadMonths) {
targetMonth <- getInitTargetInfo( initYear = 0000, initMonth = initMonth, leadMonth = leadMonth )$targetMonth
targetSYear <- getInitTargetInfo( initYear = initYears[1], initMonth = initMonth, leadMonth = leadMonth )$targetYear
targetEYear <- getInitTargetInfo( initYear = initYears[length(initYears)], initMonth = initMonth, leadMonth = leadMonth )$targetYear
print(sprintf("targetmonth: %s, leadMonth: %s",targetMonth, leadMonth))
for (leadMonth in 1:7) {
targetMonth <- getInitTargetInfo( initYear = 0000, initMonth = initMonth, leadMonth = leadMonth-1 )$targetMonth
targetSYear <- getInitTargetInfo( initYear = initYears[1], initMonth = initMonth, leadMonth = leadMonth-1 )$targetYear
targetEYear <- getInitTargetInfo( initYear = initYears[length(initYears)], initMonth = initMonth, leadMonth = leadMonth-1 )$targetYear
RData<-finalList[[leadMonth]]
print(sprintf("targetmonth: %s, leadMonth: %s",targetMonth, leadMonth-1))
oPrefix <- sprintf("%s/%s_forcing_seas15_%s_%s_E%02d-%02d_TAR%4d-%4d_%02d_LM%d",
outPath, variableName, locName, bcInfo,
members[1], members[length(members)],
targetSYear, targetEYear,
targetMonth, leadMonth)
targetMonth, leadMonth-1)
print(oPrefix)
RData<-RDataAllLeadMonths[[paste0("LeadMonth_",leadMonth)]]
# print(RData$InitializationDates)
# print(RData$Dates$start)
#save(RData, file = paste0(oPrefix, ".RData"))
R2Netcdf(paste0(oPrefix, ".nc4"), RData)
}
}
......
#!/bin/bash
#SBATCH --account=5120867-01
#SBATCH --time=5000
#SBATCH --mem=32024
#SBATCH --mem=16024
#SBATCH --ntasks=1
#SBATCH --output=./log/log_2_doReorder_combYears_combMembers_sepLeadmonths_<location>_<bcInfo>_<resolution>deg_initMonth<initMonth>_%j.txt
#SBATCH --job-name=Reorder2bias
......
#!/bin/bash
tmpScripts="./tmpScripts"
location="EU"; resolution="0.50"
#location="EU"; resolution="0.50"
#location="GHA"; resolution="0.50"
#location="GHA"; resolution="0.75"
location="GHA"; resolution="0.75"
bcInfo="noBC"
#bcInfo="BC"
......
......@@ -104,7 +104,7 @@ for (initMonth in initMonths) {
RData$InitializationDates<-finalList[[iLM]]$InitializationDates[which(initYears==initYear)]
## Save to file...
outFile <- sprintf("%s/%s_forcing_seas15_%s_%s_E%02d_%04d_%02d", outPathTmp, variableName, locName, bcInfo, iMember, initYear, initMonth)
outFile <- sprintf("%s/%s_forcing_seas15_%s_%s_E%02d_INIT%04d_%02d", outPathTmp, variableName, locName, bcInfo, iMember, initYear, initMonth)
R2Netcdf(outFile = sprintf("%s.nc4",outFile), RData)
}
}
......
......@@ -33,6 +33,8 @@ if (submitscript) {
outPath <- sprintf("../Analysis_output/pics/checkBias_rev1.2")
}
mask<-Netcdf2R(inFile = sprintf("../DATA/mask/mask_wfdei_%s_%s.nc4", locName, resolution), "mask")
variables<-names(variableInfo)
#variables<-c("pr")
......@@ -89,6 +91,11 @@ for (variableName in variables) {
RData_noBC <- convert(RData_noBC, toUnit = variableInfo[[variableName]]$unitsEasy)
RData_BC <- convert(RData_BC, toUnit = variableInfo[[variableName]]$unitsEasy)
## Apply mask on Observed-data
for (iTime in 1:length(RData_obs$Dates$start)) {
RData_obs$Data[iTime,,]<-RData_obs$Data[iTime,,]*mask$Data[1,,]
}
## Check-Correction:
barLimits<-c(colorbarLimits[[variableName]]$minValue,colorbarLimits[[variableName]]$maxValue)
barLimitsDiff<-c(colorbarLimits[[variableName]]$diffMin,colorbarLimits[[variableName]]$diffMax)
......
......@@ -20,7 +20,6 @@ R2Netcdf <- function(outFile, RData) {
dimY <- ncdim_def("lat", "degrees_north",RData$xyCoords$y)
if (nDims > 3) {
dimZ <- ncdim_def("member", "layer",1:dim(RData$Data)[which(dimensions == "member")])
#dimZ <- ncdim_def(name = "layer",units = '',vals = 1:dim(RData$Data)[which(dimensions == "member")], create_dimvar = FALSE)
}
timeString<-format(strptime(RData$Dates$start[1], format = "%Y-%m-%d", tz = "GMT"),format="%Y-%m-%d %T")
timeArray<-as.double(difftime(RData$Dates$start,RData$Dates$start[1], units = c("days"), tz = "GMT"))
......@@ -46,8 +45,8 @@ R2Netcdf <- function(outFile, RData) {
if (nDims > 3) {
ncvar_put(ncid, data, aperm(RData$Data, c(which(dimensions == "lon"),
which(dimensions == "lat"),
which(dimensions == "time"),
which(dimensions == "member"))))
which(dimensions == "member"),
which(dimensions == "time"))))
} else {
ncvar_put(ncid, data, aperm(RData$Data, c(which(dimensions == "lon"),
which(dimensions == "lat"),
......
rm(list=ls())
source(file = "./functions/functionsGeneral.R")
source(file = "./functions/functionNetcdf2R.R")
reformat2Bias<-function(members, initYears, initMonth, varName, locName,
iFile = "./forcing_seas15_GHA_ref__E<MEMBERS>_<YEARS>_01.nc4") {
sYear<-initYears[1]
eYear<-initYears[length(initYears)]
nYears<-length(initYears)
sMember<-members[1]
eMember<-members[length(members)]
nMembers<-length(members)
strMembers<-NULL
strInitDates<-NULL
# indexesOfDaysYears(1,sYear,eYear)
allocatedOutputArray<-FALSE
for (iMember in members) {
initDates<-NULL
iInitYear<-1
for (initYear in initYears) {
indexesYearsLeadMonths<-indexesForYearsPerLeadMonth(sYear,eYear,initMonth)
## Change to proper filename
nameFile <- iFile
strYear <- sprintf("%4d",initYear)
strMember <- sprintf("%02d",iMember)
nameFile <-gsub("<MEMBERS>", strMember, nameFile)
nameFile <-gsub("<YEARS>", strYear, nameFile)
print(paste0("Opening: ", nameFile))
RData<-Netcdf2R(nameFile, varName)
if (sMember == iMember) {
strInitDates<-c(strInitDates, RData$InitializationDates)
}
if (!allocatedOutputArray) {
sYear<-initYears[1]
eYear<-initYears[length(initYears)]
nYears<-eYear-sYear+1
## allocate list
final<-RData
## allocate arrays
finalList<-NULL
listNames<-NULL
for (iLM in 1:7) {
finalList[[iLM]] <- final
listNames<-c(listNames,paste0("LeadMonth_",iLM-1))
totalDays<-indexesYearsLeadMonths$eIndexes[as.character(eYear),iLM]
finalList[[iLM]]$Data<-array(0,dim=c(nMembers,totalDays,length(RData$xyCoords$y),length(RData$xyCoords$x)))
finalList[[iLM]]$Dates$start<-character(length=totalDays)
finalList[[iLM]]$Dates$end<-character(length=totalDays)
attr(finalList[[iLM]]$Data,"dimensions") <- c("member","time","lat","lon")
# attr(finalList[[iLM]],"LeadMonth") <- iLM-1
}
names(finalList)<-listNames
allocatedOutputArray<-TRUE
}
## Split the seven months
indexes<-indexesOfDaysPerMonth(initMonth, 7, initYear)
# print(indexes)
## Split the data in seperate months
parts<-NULL
for (iLM in 1:7) {
parts[[iLM]] <- list(startDates = RData$Dates$start[indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"]],
endDates = RData$Dates$end[indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"]],
data = RData$Data[indexes[iLM,"sIndex"]:indexes[iLM,"eIndex"],,])
}
## Fill big data array
for (iLM in 1:7) {
sIndex<-indexesYearsLeadMonths$sIndexes[as.character(initYear),iLM]
eIndex<-indexesYearsLeadMonths$eIndexes[as.character(initYear),iLM]
finalList[[iLM]]$Data[iMember,sIndex:eIndex,,] <-parts[[iLM]]$data
finalList[[iLM]]$Dates$start[sIndex:eIndex] <-parts[[iLM]]$startDates
finalList[[iLM]]$Dates$end[sIndex:eIndex] <-parts[[iLM]]$endDates
}
initDates[iInitYear]<-RData$Dates$start[1]
iInitYear<-iInitYear+1
}
strMembers<-c(strMembers,paste0("Member_",iMember))
for (iLM in 1:7) {
finalList[[iLM]]$Members <- strMembers
finalList[[iLM]]$InitializationDates <- strInitDates
}
}
return(finalList)
}
\ No newline at end of file
......@@ -27,8 +27,8 @@ colorbarLimits <- list(
tasmax = list(minValue = -20, maxValue = 40, diffMin = -0.5, diffMax = 0.5),
pr = list(minValue = 0, maxValue = 10, diffMin = -0.2, diffMax = 0.2),
# psl = list(minValue = 0, maxValue = 10, diffMin = -0.2, diffMax = 0.2),
rsds = list(minValue = -200, maxValue = 500, diffMin = -0.5, diffMax = 0.5),
rlds = list(minValue = -200, maxValue = 500, diffMin = -0.5, diffMax = 0.5),
huss = list(minValue = 0, maxValue = 0.02, diffMin = -0.5, diffMax = 0.5),
rsds = list(minValue = 0, maxValue = 400, diffMin = -1.0, diffMax = 1.0),
rlds = list(minValue = -200, maxValue = 500, diffMin = -1.0, diffMax = 1.0),
huss = list(minValue = 0, maxValue = 0.02, diffMin = -0.001, diffMax = 0.001),
sfcWind = list(minValue = 0, maxValue = 10, diffMin = -0.2, diffMax = 0.2)
)
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