Skip to content

Commit 549245c

Browse files
committed
Merge branch 'master' of https://github.com/metno/esd
2 parents a405907 + da8975f commit 549245c

File tree

10 files changed

+197
-94
lines changed

10 files changed

+197
-94
lines changed

NAMESPACE

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,11 @@ export(subset.trajectory)
539539
export(corfield.trajectory)
540540
export(DS.trajectory)
541541

542-
#export(dX)
543-
#export(dY)
544-
#export(dT)
545-
#export(regfit)
542+
export(CCI)
543+
export(dX)
544+
export(dY)
545+
export(dT)
546+
export(regfit)
546547

547548
export(histwet)
548549
export(visprob)

R/DS.R

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ DS.station <- function(y,X,biascorrect=FALSE,mon=NULL,
264264

265265
if ( (!inherits(X,'eof')) & (inherits(X,'field')) ) {
266266
#print("HERE")
267-
ds <- DS.field(y=y,X=X,biascorrect=biascorrect,mon=mon,
268-
method=method,swsm=swsm,m=m,
269-
rmtrend=rmtrend,eofs=eofs,
270-
area.mean.expl=area.mean.expl,verbose=verbose,
271-
weighted=TRUE,pca=FALSE,npca=20,...)
272-
return(ds)
267+
ds <- DS.field(y=y,X=X,biascorrect=biascorrect,mon=mon,
268+
method=method,swsm=swsm,m=m,
269+
rmtrend=rmtrend,eofs=eofs,
270+
area.mean.expl=area.mean.expl,verbose=verbose,
271+
weighted=TRUE,pca=FALSE,npca=20,...)
272+
return(ds)
273273
} else if (is.list(X)) {
274274
# REB 2014-10-08
275275
if (verbose) print("The predictor is a list")
@@ -426,7 +426,7 @@ DS.comb <- function(y,X,biascorrect=FALSE,mon=NULL,
426426
Y <- zoo(z,order.by=index(Z))
427427
Y <- attrcp(Z,Y)
428428

429-
# Store in a similar structure as in the common EOF:
429+
# Store in a similar structure as in the common EOF:
430430
eval(parse(text=paste("attr(ds,'appendix.",i,"') <- Y",sep="")))
431431
}
432432
rm("X0"); gc(reset=TRUE)
@@ -665,6 +665,25 @@ DS.pca <- function(y,X,biascorrect=FALSE,mon=NULL,
665665
verbose=verbose,weighted=weighted,pca=pca,npca=npca,...)
666666
return(z)
667667
}
668+
669+
if (!inherits(X,"eof") & inherits(X,"zoo")) {
670+
## If the predictor is an index, then use the same code to
671+
## estimate teleconnection patterns
672+
if (verbose) print('Predictor is a zoo object')
673+
attr(X,'history') <- history.stamp()
674+
attr(X,'pattern') <- attr(y,'pattern')
675+
attr(X,'eigenvalues') <- attr(y,'eigenvalues')
676+
attr(X,'longitude') <- lon(y)
677+
attr(X,'latitude') <- lat(y)
678+
attr(X,'variable') <- varid(y)
679+
attr(X,'unit') <- unit(y)
680+
681+
class(X) <- c('eof',class(X))
682+
z <- DS.pca(y,X,method=method,swsm=swsm,m=m,
683+
eofs=eofs,rmtrend=rmtrend,verbose=verbose,
684+
weighted=weighted,...)
685+
return(z)
686+
}
668687
stopifnot(!missing(y),!missing(X),
669688
inherits(X,"eof"),inherits(y,"station"))
670689

@@ -675,8 +694,8 @@ DS.pca <- function(y,X,biascorrect=FALSE,mon=NULL,
675694
# synchronise the two zoo objects through 'merge' (zoo)
676695
y <- matchdate(y,it=X,verbose=verbose) # REB: 2014-12-16
677696
X <- matchdate(X,it=y,verbose=verbose) # REB: 2014-12-16
678-
dy <- dim(y)
679-
dx <- dim(X)
697+
dy <- dim(y); if (is.null(dy)) dy <- c(length(y),1)
698+
dx <- dim(X); if (is.null(dx)) dx <- c(length(X),1)
680699

681700
# Use method for downscaling
682701
#str(y); str(X)
@@ -729,9 +748,12 @@ DS.pca <- function(y,X,biascorrect=FALSE,mon=NULL,
729748
if (verbose) print(attr(X0,'n.apps'))
730749
Xp <- attr(X0,'appendix.1')
731750
} else Xp <- X
751+
752+
732753
y.out <- matrix(rep(NA,dx[1]*dy[2]),dx[1],dy[2])
733754
fit.val <- y.out
734-
yp.out <- matrix(rep(NA,dim(Xp)[1]*dy[2]),dim(Xp)[1],dy[2])
755+
dxp <- dim(Xp); if (is.null(dxp)) dxp <- c(length(Xp),1)
756+
yp.out <- matrix(rep(NA,dxp[1]*dy[2]),dxp[1],dy[2])
735757

736758
if (verbose) print(paste('PC',eofs,collapse=' '))
737759
# Loop over the PCs...
@@ -740,16 +762,16 @@ DS.pca <- function(y,X,biascorrect=FALSE,mon=NULL,
740762
## pattern for each PC. Combine into one matrix. The predictor pattern
741763
## for each station can be recovered by multiplying with the PCA pattern
742764
if (verbose) print('Predictor pattern')
743-
#browser()
744-
x0p <- attr(X0,'pattern')
765+
x0p <- attr(X0,'pattern')
745766
dp <- dim(x0p)
746767
if (is.null(dp)) dp <- c(length(x0p),1,1) # list combining EOFs
768+
if (length(dp)==2) dp <- c(dp,1)[c(1,3,2)] # if PCA rather than EOF
747769
if (verbose) {str(x0p); print(dp); print(dy)}
748770
predpatt <- rep(NA,dp[1]*dp[2]*dy[2])
749771
dim(predpatt) <- c(dp[1]*dp[2],dy[2])
750772
dim(x0p) <- c(dp[1]*dp[2],dp[3])
751773
## This works for single predictors, but is more complicated for
752-
#3 multiple predictors.
774+
## multiple predictors.
753775
if (dp[3] == length(attr(X0,'eigenvalues'))) x0p <- x0p %*% diag(attr(X0,'eigenvalues'))
754776
model <- list(); eof <- list()
755777
for (i in 1:dy[2]) {

R/DSensemble.R

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

R/diagnose.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ diagnose.dsensemble <- function(x,plot=TRUE,type='target',...) {
405405
N <- sum(i1,na.rm=TRUE)
406406
## browser()
407407
if (plot) {
408-
x <- -round(200*(0.5-pbinom(outside,size=N,prob=0.1)),2)
409-
y <- -round(200*(0.5-pnorm(deltaobs,mean=mean(deltagcm),sd=sd(deltagcm))),2)
408+
x <- -round(200*(0.5-pnorm(deltaobs,mean=mean(deltagcm),sd=sd(deltagcm))),2)
409+
y <- -round(200*(0.5-pbinom(outside,size=N,prob=0.1)),2)
410410
#print(c(x,y))
411411

412412
par(bty="n",xaxt="n",yaxt="n")

R/infographics.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ visprob.station.precip <- function(x,y=NULL,is=1,threshold=1,dy=0.005,
744744
if (verbose) print('visprob.station.precip')
745745
## If y is provided, synchronise the two time series:
746746
if (!is.null(y)) {
747-
y <- subset(y,,it=c(start(x),end(x)))
747+
y <- subset(y,it=c(start(x),end(x)))
748748
x <- subset(x,it=c(start(y),end(y)))
749749
y <- annual(y)
750750
} else y <- year(annual(x))

R/plot.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ plot.station <- function(x,plot.type="single",new=TRUE,
1515
## else
1616
## ylim <- apply(x,2,pretty,n=5)
1717

18-
##<<<<<<< HEAD
19-
##=======
2018
if (is.null(ylim))
2119
ylim <- pretty(as.numeric(x))
22-
##>>>>>>> d6d9c84656c9b9b73e711c2e7ee2c8d0fb230980
20+
2321
unit <- attr(x,'unit')[1]
2422
for (i in 1:length(unit)) {
2523
if ( (is.na(unit[i]) | is.null(unit[i])) ) unit[i] <- " "

R/wheel.R

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44

55
wheel <- function(x,...) UseMethod("wheel")
66

7-
wheel.station <- function(x,new=TRUE,lwd=2,col=NULL,bg="grey90",...) {
7+
wheel.station <- function(x,y=NULL,new=TRUE,lwd=2,col=NULL,
8+
bg="grey90",verbose=FALSE,...) {
9+
10+
if (verbose) print('wheel.station')
11+
## Copied from visprob.station.precip:
12+
## If y is provided, synchronise the two time series:
13+
if (!is.null(y)) {
14+
y <- subset(y,it=c(start(x),end(x)))
15+
x <- subset(x,it=c(start(y),end(y)))
16+
years <- annual(y)
17+
} else years <- year(annual(x))
818
mx <- max(abs(coredata(x)),na.rm=TRUE)
919
# r <- mean(coredata(x),na.rm=TRUE)
1020
r <- mx
1121
#jday <- julian(zoo(x))
1222
MD <- month(x)*100 + day(x)
13-
years <- as.integer(rownames(table(year(x))))
23+
#REB2015-08-20 years <- as.integer(rownames(table(year(x))))
1424
#print(years)
1525
ny <- length(years)
1626
md <- as.integer(rownames(table(MD)))
@@ -33,6 +43,12 @@ wheel.station <- function(x,new=TRUE,lwd=2,col=NULL,bg="grey90",...) {
3343
w <- seq(0,2*pi,length=m)
3444
j <- 1:ny
3545
col <- rgb(j/ny,abs(sin(pi*j/ny)),(1-j/ny))
46+
## REB 2015-08-20: if y is given, use it for setting the colours.
47+
## e.g. according to an index like NINO3.4
48+
srtc <- order(years)
49+
col <- col[srtc]
50+
years <- year(annual(x))
51+
if (verbose) {print(srtc); print(col)}
3652

3753
for (i in 1:m) {
3854
wj <- -w[i]
@@ -62,13 +78,22 @@ wheel.station <- function(x,new=TRUE,lwd=2,col=NULL,bg="grey90",...) {
6278
image(1:2,years,colbar,col=col)
6379
}
6480

65-
wheel.spell <- function(x,new=TRUE,lwd=2,col=NULL...) {
81+
wheel.spell <- function(x,y=NULL,new=TRUE,lwd=2,col=NULL,verbose=FALSE,...) {
82+
83+
if (verbose) print('wheel.spell')
84+
## Copied from visprob.station.precip:
85+
## If y is provided, synchronise the two time series:
86+
if (!is.null(y)) {
87+
y <- subset(y,it=c(start(x),end(x)))
88+
x <- subset(x,it=c(start(y),end(y)))
89+
years <- annual(y)
90+
} else years <- year(annual(x))
6691
mx <- max(abs(coredata(x)),na.rm=TRUE)
6792
# r <- mean(coredata(x),na.rm=TRUE)
6893
r <- mx
6994
#jday <- julian(zoo(x))
7095
MD <- month(x)*100 + day(x)
71-
years <- as.integer(rownames(table(year(x))))
96+
#REB2015-08-20 years years <- as.integer(rownames(table(year(x))))
7297
#print(years)
7398
ny <- length(years)
7499
md <- as.integer(rownames(table(MD)))
@@ -91,6 +116,12 @@ wheel.spell <- function(x,new=TRUE,lwd=2,col=NULL...) {
91116
w <- seq(0,2*pi,length=m)
92117
j <- 1:ny
93118
col <- rgb(j/ny,abs(sin(pi*j/ny)),(1-j/ny))
119+
## REB 2015-08-20: if y is given, use it for setting the colours.
120+
## e.g. according to an index like NINO3.4
121+
srtc <- order(years)
122+
col <- col[srtc]
123+
years <- year(annual(x))
124+
if (verbose) {print(srtc); print(col)}
94125

95126
for (i in 1:m) {
96127
wj <- -w[i]

0 commit comments

Comments
 (0)