source: src/htune_EOF.R @ 49

Last change on this file since 49 was 45, checked in by htune, 8 years ago

Add first draft of script to emulate profile, using EOF - Romain Roehrig

  • Property svn:executable set to *
File size: 8.5 KB
Line 
1#Step 1 Source files and load data
2source('BuildEmulator/BuildEmulator.R')
3source("BuildEmulator/DannyDevelopment.R")
4source("BuildEmulator/JamesNewDevelopment.R")
5library("ncdf4") # to manipulate ncdf
6#load("Demonstrations/TnSReal.RData")
7
8#########################
9# First settings
10
11case_name="AYOTTE"
12subcase_name="24SC"
13variable="theta" # qv ou rneb # Don't use "var" which is un R function !!!
14
15WAVEN=1
16
17source('htune_case_setup.R')
18file = paste("WAVE",WAVEN,"/Wave",WAVEN,".RData",sep="")
19load(file)
20
21casesu <-case_setup(case_name)
22NLES=casesu[1]
23TimeLES=casesu[2]
24TimeSCM=casesu[3]
25
26NRUNS=dim(wave_param_US)[1]
27nparam=dim(wave_param_US)[2]-1
28
29#########################
30# Get vertical grid to project everything
31print("Get vertical grid")
32
33file=paste("WAVE",WAVEN,"/",case_name,"/",subcase_name,"/SCM_",WAVEN,"-001.nc",sep="")
34print(file)
35nc =  nc_open(file)
36zf = ncvar_get(nc,"zf")
37zfSCM = zf[,1]
38nlev = length(zfSCM)
39nc_close(nc)
40
41# Get vertical grid of LES
42file = paste("LES/",case_name,"/",subcase_name,"/LES",0,".nc",sep="")
43nc =  nc_open(file)
44zfLES = ncvar_get(nc,"zf")
45nc_close(nc)
46
47# Extract the SCM vertical below the top of the LES
48zgrid = rep(0,nlev)
49ii = 1
50maxzLES = max(zfLES)
51minzLES = min(zfLES)
52for (k in 1:nlev) {
53  if ( zfSCM[k] <= maxzLES & zfSCM[k] >= minzLES ) {
54    zgrid[ii] = zfSCM[k]
55    ii = ii +1
56  }
57}
58zgrid=zgrid[1:ii-1]
59nlevout = length(zgrid)[1]
60
61print("Chosen vertical grid:")
62print(zgrid)
63
64#########################
65# Interpolate LES data on the chosen vertical grid
66print("Interpolate LES data on the chosen vertical grid")
67
68dataLES = matrix(0,ncol=NLES+2,nrow=nlevout)
69dataLES[,1] = zgrid
70
71for ( k in 0:(NLES-1) ) {
72  file = paste("LES/",case_name,"/",subcase_name,"/LES",k,".nc",sep="")
73  nc =  nc_open(file)
74  zf = ncvar_get(nc,"zf")
75  data = ncvar_get(nc,variable)
76  nc_close(nc)
77
78  tempFun <- approxfun(zf,data[,TimeLES],yleft=data[1,TimeLES])
79  dataLES[,k+2] <- tempFun(zgrid) 
80 
81  if ( k == 0 ) {
82    # Check the interpolation
83    plot(x=dataLES[,k+2],y=dataLES[,1],type='l')
84    lines(x=data[,TimeLES],y=zf,col="red")
85  }
86}
87
88# Compute observation uncertainty
89for ( k in 1:nlevout ) {
90  dataLES[k,NLES+2] = sd(dataLES[k,2:NLES+1])^2
91}
92
93save(dataLES,file="dataLES.RData")
94load("dataLES.RData")
95
96#########################
97# Interpolate SCM data on the chosen vertical grid
98print("Interpolate SCM data on the chosen vertical grid")
99
100dataSCM = matrix(0,ncol=NRUNS+1,nrow=nlevout)
101dataSCM[,1] = zgrid
102
103for ( k in 1:NRUNS ) {
104  i = sprintf("%03i",k)
105  file=paste("WAVE",WAVEN,"/",case_name,"/",subcase_name,"/SCM_",WAVEN,"-",i,".nc",sep="")
106  nc =  nc_open(file)
107  zf = ncvar_get(nc,"zf")
108  data = ncvar_get(nc,variable)
109  nc_close(nc)
110  nlev = dim(zf)[1]
111 
112  # yright, yleft to constrain what happen at the edge. Not clear yet how to do it properly
113  tempFun <- approxfun(zf[,TimeSCM],data[,TimeSCM],yright=data[1,TimeSCM],yleft=data[nlev,TimeSCM])
114  dataSCM[,k+1] <- tempFun(zgrid) 
115 
116  if ( k == 0 ) {
117    # Check the interpolation
118    plot(x=dataSCM[,k+2],y=dataSCM[,1],type='l')
119    lines(x=data[,TimeSCM],y=zf,col="red")
120  }
121 
122}
123
124save(dataSCM,file="dataSCM.RData")
125load("dataSCM.RData")
126
127# For memory yet
128#nt=dim(data)[2]
129#dataout = matrix(0,ncol=nt,nrow=nlevout)
130#for ( it in 1:nt){
131#  tempFun <- approxfun(zf,data[,it],yleft=data[1,it])
132#  dataout[,it] <- tempFun(zgrid) 
133#}
134
135#load("Demonstrations/TSensembleNW1.RData")
136plot(x=dataLES[,2],y=dataLES[,1],type='l',lwd=2,col=2,xlab=variable,ylab="Altitude")
137depths <- dataLES[,1]
138for(j in 1:NRUNS){
139  lines(x=dataSCM[,j],y=depths,col=8,lwd=0.5)
140}
141lines(x=dataLES[,2],y=dataLES[,1],lwd=2,col=2)
142#Step 2 Extract the fields you want
143SCMdata <- dataSCM[,-1]
144#ORCA2Data <- t(as.matrix(tData[,23:52]))
145#dim(ORCA2Data)
146
147#Step 3: Centre the data and find the Singular vectors (EOFs)
148SCMsvd <- CentreAndBasis(SCMdata)
149par(mfrow=c(3,3))
150for ( i in 1:9 ) {
151  plot(x=SCMsvd$tBasis[,i],y=dataSCM[,1],type='l')
152}
153
154#Step 4: Set a discrepancy variance and check the performance of basis reconstructions
155# To be thought about
156tmp = rep(0.01,nlevout)
157#for ( i in 1:nlevout ) {
158#  if ( dataSCM[i,1] >= 2500. ) {tmp[i] = 1}
159#}
160
161#for ( i in 1:nlevout ) {
162#  if ( dataSCM[i,1] >= 2000. ) {tmp[i] = 0.1}
163#}
164
165#Disc <- diag(0.01,nlevout)
166Disc <- diag(tmp)
167DiscInv <- GetInverse(Disc)
168attributes(DiscInv)
169ObsDat <- dataLES[,2] - SCMsvd$EnsembleMean
170par(mfrow=c(1,2), mar = c(4,4,2,4))
171vSVD <- VarMSEplot(SCMsvd, ObsDat, weightinv = DiscInv, ylim = c(0,80), qmax = NULL)
172abline(v = which(vSVD[,2] > 0.9)[1])
173
174#Step 5: Rotate the basis for optimal calibration
175rotSCM <- RotateBasis(SCMsvd, ObsDat, kmax = 4, weightinv = DiscInv, v = c(0.3,0.15,0.1,0.1,0.1), vtot = 0.95, MaxTime = 20)
176#save(rotSCM,file="Demonstrations/rotOrca.RData")
177
178#load("rotSCM.RData")
179vROT <- VarMSEplot(rotSCM, ObsDat, weightinv = DiscInv, ylim = c(0,80), qmax = NULL)
180abline(v = which(vROT[,2] > 0.99)[1])
181qROT <- which(vROT[,2] > 0.99)[1]
182qROT
183
184#Step 6: Emulate the basis coefficients and tune emulators!
185tDataSCM <- GetEmulatableDataWeighted(Design = wave_param_US[,-1], EnsembleData = rotSCM, HowManyBasisVectors = qROT, weightinv = DiscInv)
186tData = tDataSCM
187StanEmsO <- InitialBasisEmulators(tData, HowManyEmulators=qROT)
188# If it seems not working, relaunch source('BuildEmulator/BuildEmulator.R')
189# A fast check that it worked
190names(StanEmsO[[1]])
191
192# To remove heavy data, especially if you want to save the emulators
193#for ( i in 1:qROT ) {
194#  StanEmsO[[i]]$StanModel <- NULL
195#}
196#save(StanEmsO,file="Demonstrations/StanEmsO.RData")
197
198#load("Demonstrations/StanEmsO.RData")
199for ( i in 1:qROT ) {
200  tLOOs1 <- LOO.plot(StanEmulator = StanEmsO[[i]], ParamNames = StanEmsO[[i]]$Names)
201}
202
203#Step 7: History Matching preliminaries. If your emulator is good enough to history match, you don't always want to do it on the whole field as that inverts massive matrices everywhere in parameter space! We now model the relationship between field and coefficient implausibilities.
204###NOTE IF WE WILL RULE OUT ALL OF SPACE, THERE IS A DISCREPANCY SCALING STEP TO AVOID THIS, SEE SPATIALDEMO AND USE CAUTION!
205ImplDesign <- 2*as.data.frame(randomLHS(nparam, nparam)) - 1
206colnames(ImplDesign) <- colnames(tData)[1:nparam]
207EmOutput1 <- lapply(1:length(StanEmsO), function(e) EMULATOR.gpstan(ImplDesign,StanEmsO[[e]],FastVersion = TRUE))
208Expectation1 <- matrix(0, nrow = dim(ImplDesign)[1], ncol = length(StanEmsO))
209Variance1 <- matrix(0, nrow = dim(ImplDesign)[1], ncol = length(StanEmsO))
210for (j in 1:length(StanEmsO)){
211  Expectation1[,j] <- EmOutput1[[j]]$Expectation
212  Variance1[,j] <- EmOutput1[[j]]$Variance
213}
214
215#ImplModel <- HMCoeffBound(JJArotSAT, tObsJJASAT[[1]], Expectation1, Variance1, Error = 0*diag(8192), Disc = DiscScaled, weightinv = DiscInvScaled) # Error set as 0 matrix as combined into Discrepancy
216#With raw discrepancy (in case emulator uncertainty is large)
217ImplModel <- HMCoeffBound(rotSCM, ObsDat, Expectation1, Variance1, Error = 0*diag(nlevout), Disc = Disc, weightinv = DiscInv)
218#save(ImplModel, file="Demonstrations/ImplModel.RData")
219
220#load("Demonstrations/ImplModel.RData")
221T_c <- ImplModel$Bound
222stan_dens(ImplModel$model, pars = "T_c")
223par(mfrow=c(1,1))
224plot(ImplModel$SampleIf, ImplModel$SampleIc, pch=18, xlim = c(0,max(ImplModel$SampleIf)), ylim = c(0,max(ImplModel$SampleIc)))
225abline(v = qchisq(0.995, 30)) # bound on the field
226abline(h = qchisq(0.995, length(StanEmsO)), lty = 2) # where the chi-squared bound is, based on the number of basis vectors
227abline(h = T_c)
228
229#Final step: History matching in the coefficient space.
230CoeffHM <- DesignHM <- NULL
231tempDesign1000 <- 2*as.data.frame(randomLHS(1000, 20)) - 1
232colnames(tempDesign1000) <- colnames(tData)[1:20]
233EmOutput1000 <- lapply(1:length(StanEmsO), function(e) EMULATOR.gpstan(tempDesign1000,StanEmsO[[e]], FastVersion = TRUE))
234Expectation1000 <- Variance1000 <- matrix(0, nrow = dim(tempDesign1000)[1], ncol = length(StanEmsO))
235for (j in 1:length(StanEmsO)){
236  Expectation1000[,j] <- EmOutput1000[[j]]$Expectation
237  Variance1000[,j] <- EmOutput1000[[j]]$Variance
238}
239CoeffHM <- HistoryMatch(rotSCM, type="scoresMV", ObsDat, Expectation1000, Variance1000, Error = 0*diag(nlevout), Disc = Disc, ModelBound = FALSE, weightinv = DiscInv)$impl
240sum(CoeffHM < T_c) / length(CoeffHM)
241summary(CoeffHM)
242
243ans <- Reconstruct(Expectation1000[which.min(CoeffHM),],rotSCM$tBasis[,1:qROT])+rotSCM$EnsembleMean
244plot(x=dataLES[,2],y=dataLES[,1],type='l',lwd=2,col=2,xlab=variable,ylab="Altitude")
245for(j in 2:NRUNS+1){
246  lines(x=dataSCM[,j],y=dataSCM[,1],col=8,lwd=0.5)
247}
248lines(x=dataLES[,2],y=dataLES[,1],type='l',lwd=2,col=2)
249for(k in 1:10){
250anss <- Reconstruct(Expectation1000[k,],rotSCM$tBasis[,1:qROT])+rotSCM$EnsembleMean
251lines(x=anss,y=dataSCM[,1],col=3,lwd=1.5)
252}
253lines(x=ans,y=dataSCM[,1],col=4,lwd=1.5)
254
Note: See TracBrowser for help on using the repository browser.