@@ -374,7 +374,7 @@ DSensemble.t2m <- function(y,plot=TRUE,path="CMIP5.monthly/",
374374 attr(X ,' area.mean.expl' ) <- FALSE
375375 class(X ) <- c(" dsensemble" ," zoo" )
376376 if (! is.null(path.ds )) file.ds <- file.path(path.ds ,file.ds )
377- save(file = file.ds ,X )# "DSensemble.rda",X)
377+ # save(file=file.ds,X)#"DSensemble.rda",X)
378378 print(" ---" )
379379 invisible (X )
380380}
@@ -1028,7 +1028,7 @@ DSensemble.pca <- function(y,plot=TRUE,path="CMIP5.monthly/",
10281028 # Recursive: do each season seperately if there are more than one season
10291029 if (length(table(season(y )))> 1 ) {
10301030 if (verbose ) print(' --- Apply DS to seasons seperately ---' )
1031- Z <- list (info = ' DSensembe .pca for different seasons' )
1031+ Z <- list (info = ' DSensemble .pca for different seasons' )
10321032 for (season in names(table(season(y )))) {
10331033 if (verbose ) print(paste(' Select' ,season ))
10341034 z <- DSensemble.pca(subset(y ,it = season ),plot = plot ,path = path ,
@@ -1080,8 +1080,8 @@ DSensemble.pca <- function(y,plot=TRUE,path="CMIP5.monthly/",
10801080 flog <- file(" DSensemble.pca-log.txt" ," at" )
10811081
10821082 # # Set up a list environment to keep all the results
1083- Q <- list (info = ' DSensemble.pca' ,pca = y )
1084-
1083+ Q <- list (info = ' DSensemble.pca' ,pca = y ) # # KMP 06-08-2015
1084+
10851085 if (verbose ) print(" loop..." )
10861086 for (i in 1 : N ) {
10871087 if (verbose ) print(ncfiles [select [i ]])
@@ -1092,7 +1092,7 @@ DSensemble.pca <- function(y,plot=TRUE,path="CMIP5.monthly/",
10921092 gcmnm [i ] <- paste(attr(gcm ,' model_id' ),attr(gcm ,' realization' ),sep = " -r" )
10931093 if (inherits(y ,' season' )) {
10941094 GCM <- as.4seasons(gcm ,FUN = FUNX )
1095- GCM <- subset(gcm ,it = season(T2M )[1 ])
1095+ GCM <- subset(GCM ,it = season(T2M )[1 ])
10961096 } else if (inherits(y ,' annual' )) {
10971097 GCM <- annual(gcm ,FUN = FUNX )
10981098 } else if (inherits(y ,' month' )) {
@@ -1102,6 +1102,7 @@ DSensemble.pca <- function(y,plot=TRUE,path="CMIP5.monthly/",
11021102 }
11031103 rm(" gcm" ); gc(reset = TRUE )
11041104 T2MGCM <- combine(T2M ,GCM )
1105+
11051106 if (verbose ) print(" - - - > EOFs" )
11061107 Z <- try(EOF(T2MGCM ))
11071108
@@ -1118,36 +1119,26 @@ DSensemble.pca <- function(y,plot=TRUE,path="CMIP5.monthly/",
11181119 if (biascorrect ) Z <- biasfix(Z )
11191120 ds <- try(DS(y ,Z ,eofs = eofs ,verbose = verbose ))
11201121 if (verbose ) print(" post-processing" )
1121-
1122+
11221123 # # Keep the results for the projections:
11231124 if (verbose ) print(' Extract the downscaled projection' )
1124- z <- attr(ds ,' appendix.1' )
1125+ z <- attr(ds ,' appendix.1' ) # # KMP 09.08.2015
1126+ # #attr(z,'model') <- attr(ds,'model') ## KMP 09-08-2015
1127+ # # model takes up too much space! can it be stored more efficiently?
1128+ attr(z ,' predictor.pattern' ) <- attr(ds ,' predictor.pattern' )
1129+ attr(z ,' evaluation' ) <- attr(ds ,' evaluation' )
1130+ # #class(z) <- class(ds)
1131+
11251132 cl <- paste(' Q$i' ,i ,' _' ,gsub(' -' ,' .' ,gcmnm [i ]),' <- z' ,sep = ' ' )
11261133 eval(parse(text = cl ))
1127-
1134+ # #browser()
11281135 if (verbose ) {
1129- print(' Test to see if as.station has all information needed' )
1130- test.stations <- as.station(z )
1131- }
1132-
1133- # The test lines are included to assess for non-stationarity
1134- if (non.stationarity.check ) {
1135- if (verbose ) print(' non.stationarity.check' )
1136- testds <- DS(testy ,testZ ,biascorrect = biascorrect ,eofs = eofs )
1137- # REB 29.04.2014
1138- testz <- attr(testds ,' appendix.1' ) # REB 29.04.2014
1139- difference.z <- testy - testz # REB 29.04.2014
1136+ print(' Test to see if as.station has all information needed' )
1137+ test.stations.ds <- as.station(ds )
1138+ a <- attrcp(y ,z ); class(a ) <- c(" ds" ,class(y ))
1139+ test.stations.z <- as.station(a )
11401140 }
1141-
1142- i1 <- is.element(paste(years ,months ,sep = ' -' ),
1143- paste(year(z ),month(z ),sep = ' -' ))
1144- i2 <- is.element(paste(year(z ),month(z ),sep = ' -' ),
1145- paste(years ,months ,sep = ' -' ))
1146-
1147- if (verbose ) print(paste(' i=' ,i ,gcmnm [i ],' data points' ,
1148- sum(i1 ),' =' ,sum(i2 )))
1149- X [,i ,i1 ] <- z [i2 ,]
1150-
1141+
11511142 # Diagnose the residual: ACF, pdf, trend. These will together with the
11521143 # cross-validation and the common EOF diagnostics provide a set of
11531144 # quality indicators.
@@ -1220,51 +1211,33 @@ DSensemble.pca <- function(y,plot=TRUE,path="CMIP5.monthly/",
12201211 round(quality )))
12211212 }
12221213
1223- if (verbose ) print(' Downscaling finished - need to convert PCAs to stations ' )
1214+ if (verbose ) print(' Downscaling finished' )
12241215
12251216 # # Unpacking the information tangled up in GCMs, PCs and stations:
12261217 # # Save GCM-wise in the form of PCAs
12271218 gcmnm <- gsub(' -' ,' .' ,gcmnm )
1228- Y <- as.station(y )
1229-
1230-
1231- # # Save the information station-wise
1232- # if (verbose) print('Save the results station-wise')
1233- # x <- matrix(rep(NA,dim(X)[3]*dim(X)[2]),dim(X)[3],dim(X)[2])
1234- # ns <- length(loc(y))
1235- # if (verbose) print(loc(y))
1236- # for (i in 1:(ns)) {
1237- # x[,] <- NA
1238- # ## Loop over the GCMs and pick the selected downscaled station series:
1239- # for (j in 1:N){
1240- # x[,j] <- as.station(Z[[j+1]])[,i]
1241- # }
1242- # yloc <- tolower(loc(y)[i])
1243- # yloc <- gsub('-','.',yloc)
1244- # yloc <- gsub(' ','.',yloc)
1245- # yloc <- gsub('/','.',yloc)
1246- # if (verbose) {print(yloc); str(x)}
1247- # ds.x <- zoo(x,order.by=index(Z[[2]]))
1248- # attr(ds.x,'station') <- subset(Y,is=i)
1249- # class(ds.x) <- c('dsensemble','zoo')
1250- # eval(parse(text=paste('Z$',yloc,' <- ds.x',sep='')))
1251- # }
1219+ # #Y <- as.station(y)
12521220
12531221 # Z <- attrcp(y,Z)
12541222 if (verbose ) print(' Set attributes' )
1255- attr(Z ,' predictor' ) <- attr(T2M ,' source' )
1256- attr(Z ,' domain' ) <- list (lon = lon ,lat = lat )
1257- attr(Z ,' scorestats' ) <- scorestats
1258- attr(Z ,' path' ) <- path
1259- attr(Z ,' scenario' ) <- rcp
1260- attr(Z ,' history' ) <- history.stamp(y )
1223+ attr(Q ,' predictor' ) <- attr(T2M ,' source' )
1224+ attr(Q ,' domain' ) <- list (lon = lon ,lat = lat )
1225+ attr(Q ,' scorestats' ) <- scorestats
1226+ attr(Q ,' path' ) <- path
1227+ attr(Q ,' scenario' ) <- rcp
1228+ attr(Q ,' variable' ) <- attr(y ," variable" )[1 ]
1229+ attr(Q ,' unit' ) <- attr(y ," unit" )[1 ]
1230+ attr(Q ,' history' ) <- history.stamp(y )
12611231 if (non.stationarity.check )
1262- attr(Z ,' on.stationarity.check' ) <- difference.z else
1263- attr(Z ,' on.stationarity.check' ) <- NULL
1264- attr(Z ,' area.mean.expl' ) <- FALSE
1232+ attr(Q ,' on.stationarity.check' ) <- difference.z else
1233+ attr(Q ,' on.stationarity.check' ) <- NULL
1234+ attr(Q ,' area.mean.expl' ) <- FALSE
1235+ class(Q ) <- c(" dsensemble" ," pca" ," list" )
12651236
1237+ # #browser()
1238+ dse.pca <- Q
12661239 if (! is.null(path.ds )) file.ds <- file.path(path.ds ,file.ds ) # # KMP 2015-08-05
1267- save(file = file.ds ,Z )
1240+ save(file = file.ds ,dse.pca )
12681241 if (verbose ) print(" ---" )
1269- invisible (Z )
1242+ invisible (dse.pca )
12701243}
0 commit comments