-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_ens_gosat_data.R
147 lines (113 loc) · 4.67 KB
/
merge_ens_gosat_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#-- This function merges the ensemble realizations
#-- The resolution is needed to adjust model errors based upon
#-- multiple observations per grid cell
#-- Test args
#-- ensemble.dir = "/scratch4/aschuh/GEOS-CHEM_output/scottbetas2/ascends/"
#-- observation.matrix=output
merge_ens_gosat_data = function(ensemble.dir=NULL,model.res_x=2.5,model.res_y=2,return.landmask=FALSE)
{
#require(akima) #for interp
fls.model = sort(list.files(ensemble.dir,full.names=TRUE))
fls.finalrun.model = list.files(ensemble.dir,full.names=TRUE,pattern="FINALRUN")
fls.priors.model = list.files(ensemble.dir,full.names=TRUE,pattern="PRIOR")
fls.exc = c(fls.finalrun.model,fls.priors.model)
fls.model = fls.model[!(fls.model %in% fls.exc)]
fls.short.model = sort(list.files(ensemble.dir,full.names=FALSE))
fls.short.finalrun.model = list.files(ensemble.dir,full.names=FALSE,pattern="FINALRUN")
fls.short.priors.model = list.files(ensemble.dir,full.names=FALSE,pattern="PRIOR")
fls.short.exc = c(fls.short.finalrun.model,fls.short.priors.model)
fls.short.model = fls.short.model[!(fls.short.model %in% fls.short.exc)]
#-- Drop in the PSEUDO test case
#pseudo.fls = sort(list.files("/scratch4/aschuh/GEOS-CHEM_output/control/ascends/",full.names=TRUE))
#pseudo.fls.short = sort(list.files("/scratch4/aschuh/GEOS-CHEM_output/control/ascends/",full.names=FALSE))
#fls = c(fls.model,pseudo.fls[pseudo.fls.short %in% fls.short.model])
#fls.short = c(fls.short.model,pseudo.fls.short[pseudo.fls.short %in% fls.short.model])
fls = fls.model
fls.short = fls.short.model
ensnum = sapply(fls.short,
FUN=function(x){
xx = strsplit(x,"\\.")[[1]]
return(xx[length(xx)])
})
ensnum = as.numeric(as.character(ensnum))
#-- For pseudo case
#ensnum[(length(fls.model)+1):(length(fls))] = max(ensnum) + 1
#-------------------
tim.offset = 6*24*366 + 18*24*365
for(i in 1:max(ensnum))
{
print(paste("working on ens ",i," of ",max(ensnum),sep=""))
tmpfls = fls[ensnum == i]
for(j in 1:length(tmpfls))
{
tmpdat = read.table(tmpfls[j],header=TRUE)
if(j==1){
#nobs = dim(tmpdat)[1]
enstmpdat = tmpdat
}else{
enstmpdat = rbind(enstmpdat,tmpdat)
}
}
if(i==1)
{
nobs = dim(enstmpdat)[1]
fullensdat = array(NA,dim=c(nobs,max(ensnum)))
fullensdat[,1] = enstmpdat$TRA_001
#-- Get observations
obs = enstmpdat$XCO2
#-- needed to adjust errors
lat.gcell = floor(enstmpdat$LAT + (90+0.5*model.res_y)/2)+1
lon.gcell = floor(enstmpdat$LON + (90+0.5*model.res_x)/2)+1
count = c(1)
for(k in 2:(dim(enstmpdat)[1]))
{
if(lat.gcell[k] == lat.gcell[k-1] & lon.gcell[k] == lon.gcell[k-1])
{
count[k] = count[k-1] + 1
}
else
{
count[k] = 1
}
}
indx = dim(enstmpdat)[1]
while(indx > 0)
{
if(count[indx] > 1)
{
count[indx:(indx-count[indx]+1)] = count[indx]
indx = indx - count[indx]
}else{indx = indx - 1}
}
err.adj = sqrt(count) * enstmpdat$ERROR
if(return.landmask)
{
longrid=seq(-181.25,181.25-model.res_x,by=model.res_x)
latgrid=c(-90,seq(-90+0.5*model.res_y, 90-1.5*model.res_y,by=model.res_y), 89)
#Then you have to make the following assignment on longitude
lonbox = sapply(enstmpdat$LON,FUN=function(x){max(grep(TRUE,x > longrid))})
latbox = sapply(enstmpdat$LAT,FUN=function(x){max(grep(TRUE,x > latgrid))})
lonbox[lonbox==145] = 1
LAND.MASK = !is.na(rotate270.matrix(read.fwf("/discover/nobackup/aschuh/run/ENSCODE/Regions_land.dat",widths=rep(1,144))))
land.grid_ind = LAND.MASK[cbind(lonbox,latbox)] == 1
}
coords = enstmpdat[,c("LON","LAT")]
}
else{
fullensdat[,i] = enstmpdat$TRA_001
}
}
#-- Add pseudo situation
#for(j in 1:length(pseudo.fls))
# {
# tmpdat = read.table(pseudo.fls[j],header=TRUE)
# if(j==1){enstmpdat = tmpdat}else{enstmpdat = rbind(enstmpdat,tmpdat)}
# }
# fullensdat= cbind(fullensdat,enstmpdat$TRA_001)
return.data = list(fullensdat=fullensdat*10^6,err = err.adj,obs=obs,coords=coords)
if(return.landmask)
{
return.data$landmask = land.grid_ind
}
return(return.data)
}