106 |
106 |
|
107 |
107 |
|
108 |
108 |
|
109 |
|
|
|
109 |
#must be cleaned up
|
110 |
110 |
tb <- read.table(file=file.path(in_dir,paste("tb_diagnostic_v_NA","_",out_suffix_str,".txt",sep="")),sep=",")
|
111 |
111 |
#tb_diagnostic_v_NA_run10_1500x4500_global_analyses_pred_1992_10052015.txt
|
112 |
112 |
|
113 |
113 |
### Start new function here
|
114 |
114 |
|
115 |
|
#tb
|
116 |
|
date_processed <- day_to_mosaic[i]
|
117 |
|
lf_to_mosaic <-list.files(path=file.path(in_dir_tiles),
|
118 |
|
pattern=paste(".*.",date_processed,".*.tif$",sep=""),full.names=T) #choosing date 2...20100901
|
119 |
|
|
120 |
|
lf<- gsub(file_format,"",lf_to_mosaic)
|
121 |
|
tx<-strsplit(as.character(lf),"_")
|
122 |
|
#deal with the fact that we have number "1" attached to the out_suffix (centroids of tiles)
|
123 |
|
pos_lat <- lapply(1:length(tx),function(i,x){length(x[[i]])-1},x=tx)
|
124 |
|
pos_lon <- lapply(1:length(tx),function(i,x){length(x[[i]])},x=tx)
|
125 |
|
lat_val <- unlist(lapply(1:length(tx),function(i,x,y){x[[i]][pos_lat[[i]]]},x=tx,y=pos_lat))
|
126 |
|
lat <- as.character(lapply(1:length(lat_val),function(i,x){substr(x[[i]],2,nchar(x[i]))},x=lat_val)) #first number not in the coordinates
|
127 |
|
long <- as.character(lapply(1:length(tx),function(i,x,y){x[[i]][pos_lon[[i]]]},x=tx,y=lon_lat))
|
128 |
|
|
129 |
|
df_centroids <- data.frame(long=as.numeric(long),lat=as.numeric(lat))
|
130 |
|
df_centroids$ID <- as.numeric(1:nrow(df_centroids))
|
131 |
|
df_centroids$tile_coord <- paste(lat,long,sep="_")
|
132 |
|
df_centroids$files <- lf_to_mosaic
|
133 |
|
df_centroids$date <- date_processed
|
134 |
|
write.table(df_centroids,paste("df_centroids_",out_suffix,".txt",sep="_"),sep=',')
|
135 |
|
|
136 |
|
#sprintf(" %3.1f", df_centroids$lat)
|
137 |
|
|
138 |
|
#merge()
|
139 |
|
df_centroids
|
140 |
|
tb_date <- subset(tb,date==date_processed & pred_mod=="mod1")
|
141 |
|
tb_date$tile_coord <- as.character(tb_date$tile_coord)
|
142 |
|
df_centroids <- merge(df_centroids,tb_date,by="tile_coord")
|
143 |
|
|
144 |
|
r1 <- raster(lf_to_mosaic[i])
|
145 |
|
r1 <- raster(df_centroids$files[i])
|
146 |
|
r1[] <- df_centroids$rmse[i]
|
147 |
|
writeRaster()
|
|
115 |
create_accuracy_metric_raster <- function(i, list_param){
|
|
116 |
#This function generates weights from a point location on a raster layer.
|
|
117 |
#Note that the weights are normatlized on 0-1 scale using max and min values.
|
|
118 |
#Inputs:
|
|
119 |
#lf: list of raster files
|
|
120 |
#tb: data.frame table #fitting or validation table with all days
|
|
121 |
#metric_name <- list_param$metric_name #RMSE, MAE etc.
|
|
122 |
#pred_mod_name <- list_param$pred_mod_name #mod1, mod_kr etc.
|
|
123 |
#y_var_name <- list_param$y_var_name #"dailyTmax" #PARAM2
|
|
124 |
#interpolation_method <- list_param$interpolation_method #c("gam_CAI") #PARAM3
|
|
125 |
#date_processed <- list_param$days_to_process[i]
|
|
126 |
#NA_flag_val <- list_param$NA_flag_val
|
|
127 |
#NAflag,file_format,out_suffix etc...
|
|
128 |
#file_format <- list_param$file_format
|
|
129 |
#out_dir_str <- list_param$out_dir
|
|
130 |
#out_suffix_str <- list_param$out_suffix
|
|
131 |
#Outputs:
|
|
132 |
#raster list of weights and product of wegihts and inuts
|
|
133 |
#TODO:
|
|
134 |
|
|
135 |
# - improve efficiency
|
|
136 |
#
|
|
137 |
############
|
|
138 |
|
|
139 |
lf <- list_param$lf #list of files to mosaic
|
|
140 |
tb <- list_param$tb #fitting or validation table with all days
|
|
141 |
metric_name <- list_param$metric_name #RMSE, MAE etc.
|
|
142 |
pred_mod_name <- list_param$pred_mod_name #mod1, mod_kr etc.
|
|
143 |
y_var_name <- list_param$y_var_name #"dailyTmax" #PARAM2
|
|
144 |
interpolation_method <- list_param$interpolation_method #c("gam_CAI") #PARAM3
|
|
145 |
date_processed <- list_param$days_to_process[i]
|
|
146 |
NA_flag_val <- list_param$NA_flag_val
|
|
147 |
#NAflag,file_format,out_suffix etc...
|
|
148 |
file_format <- list_param$file_format
|
|
149 |
out_dir_str <- list_param$out_dir
|
|
150 |
out_suffix_str <- list_param$out_suffix
|
|
151 |
|
|
152 |
####### START SCRIPT #####
|
|
153 |
|
|
154 |
#r_in <- raster(lf[i]) #input image
|
|
155 |
|
|
156 |
#date_processed <- day_to_mosaic[i]
|
|
157 |
#lf_to_mosaic <-list.files(path=file.path(in_dir_tiles),
|
|
158 |
# pattern=paste(".*.",date_processed,".*.tif$",sep=""),full.names=T) #choosing date 2...20100901
|
|
159 |
|
|
160 |
lf_tmp <- gsub(file_format,"",lf)
|
|
161 |
tx<-strsplit(as.character(lf_tmp),"_")
|
|
162 |
#deal with the fact that we have number "1" attached to the out_suffix (centroids of tiles)
|
|
163 |
pos_lat <- lapply(1:length(tx),function(i,x){length(x[[i]])-1},x=tx)
|
|
164 |
pos_lon <- lapply(1:length(tx),function(i,x){length(x[[i]])},x=tx)
|
|
165 |
lat_val <- unlist(lapply(1:length(tx),function(i,x,y){x[[i]][pos_lat[[i]]]},x=tx,y=pos_lat))
|
|
166 |
lat <- as.character(lapply(1:length(lat_val),function(i,x){substr(x[[i]],2,nchar(x[i]))},x=lat_val)) #first number not in the coordinates
|
|
167 |
long <- as.character(lapply(1:length(tx),function(i,x,y){x[[i]][pos_lon[[i]]]},x=tx,y=lon_lat))
|
|
168 |
|
|
169 |
df_centroids <- data.frame(long=as.numeric(long),lat=as.numeric(lat))
|
|
170 |
df_centroids$ID <- as.numeric(1:nrow(df_centroids))
|
|
171 |
df_centroids$tile_coord <- paste(lat,long,sep="_")
|
|
172 |
df_centroids$files <- lf
|
|
173 |
df_centroids$date <- date_processed
|
|
174 |
write.table(df_centroids,paste("df_centroids_",date_processed,"_",out_suffix,".txt",sep=""),sep=',')
|
|
175 |
|
|
176 |
#sprintf(" %3.1f", df_centroids$lat)
|
|
177 |
|
|
178 |
tb_date <- subset(tb,date==date_processed & pred_mod==pred_mod_name)
|
|
179 |
tb_date$tile_coord <- as.character(tb_date$tile_coord)
|
|
180 |
df_centroids <- merge(df_centroids,tb_date,by="tile_coord")
|
|
181 |
|
|
182 |
#r1 <- raster(lf[i])
|
|
183 |
r1 <- raster(df_centroids$files[i])
|
|
184 |
r1[] <- df_centroids[[metric_name]][i] #improve this
|
|
185 |
#set1f <- function(x){rep(NA, x)}
|
|
186 |
#r_init <- init(r_in, fun=set1f)
|
|
187 |
|
|
188 |
raster_name_tmp <- paste("r_",metric_name,"_",interpolation_method,"_",date_processed,"_",out_suffix_str,sep="")
|
|
189 |
raster_name <- file.path(out_dir_str,raster_name_tmp)
|
|
190 |
writeRaster(r1, NAflag=NA_flag_val,filename=raster_name,overwrite=TRUE)
|
|
191 |
|
|
192 |
raster_created_obj <- list(raster_name,df_centroids)
|
|
193 |
names(raster_created_obj) <- c("raster_name","df_centroids")
|
|
194 |
return(raster_created_obj)
|
148 |
195 |
|
149 |
|
#### end of function
|
|
196 |
}
|
150 |
197 |
|
151 |
|
#extract(r1,)
|
152 |
|
#coordinates(df_centroids) <- cbind(df_centroids$long,df_centroids$lat)
|
153 |
|
#proj4string(df_centroids) <- projection(r1)
|
|
198 |
#### end of function
|
154 |
199 |
|
155 |
200 |
|
156 |
|
#df_tile_processed$lat <- lat
|
157 |
|
#df_tile_processed$lon <- long
|
158 |
201 |
|
159 |
202 |
lf_mosaic1 <-list.files(path=file.path(in_dir_tiles),
|
160 |
203 |
pattern=paste(".*.",day_to_mosaic[1],".*.tif$",sep=""),full.names=T) #choosing date 2...20100901
|
161 |
204 |
|
162 |
205 |
lf_mosaic2 <-list.files(path=file.path(in_dir_tiles),
|
163 |
206 |
pattern=paste(".*.",day_to_mosaic[2],".*.tif$",sep=""),full.names=T) #choosing date 2...20100901
|
|
207 |
|
|
208 |
lf <- lf_mosaic1 #list of files to mosaic
|
|
209 |
#tb <- list_param$tb #fitting or validation table with all days
|
|
210 |
metric_name <- "rmse" #RMSE, MAE etc.
|
|
211 |
pred_mod_name <- "mod1"
|
|
212 |
#y_var_name
|
|
213 |
#interpolation_method #c("gam_CAI") #PARAM3
|
|
214 |
days_to_process <- day_to_mosaic
|
|
215 |
#NA_flag_val <- list_param$NA_flag_val
|
|
216 |
#file_format <- list_param$file_format
|
|
217 |
out_dir_str <- out_dir
|
|
218 |
out_suffix_str <- out_suffix
|
|
219 |
|
|
220 |
list_param_accuracy_metric_raster <- list(lf,tb,metric_name,pred_mod_name,y_var_name,interpolation_method,
|
|
221 |
days_to_process,NA_flag_val,file_format,out_dir_str,out_suffix_str)
|
|
222 |
|
|
223 |
names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method",
|
|
224 |
"days_to_process","NA_flag_val","file_format","out_dir_str","out_suffix_str")
|
|
225 |
debug(create_accuracy_metric_raster)
|
|
226 |
test <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster)
|
|
227 |
|
164 |
228 |
#lf_mosaic <- lf_mosaic[1:20]
|
165 |
229 |
r1 <- raster(lf_mosaic1[1])
|
166 |
230 |
r2 <- raster(lf_mosaic2[2])
|
creating function to produce accuracy metric raster for eact tile