Revision 46deff43
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/global_run_scalingup_mosaicing.R | ||
---|---|---|
153 | 153 |
#9) pred_mod_name : model name used e.g. "mod1" #PARAM 9 |
154 | 154 |
#10) var_pred : variable for use in residuals mapping (e.g. "res_mod1") #PARAM 10 |
155 | 155 |
#11) create_out_dir_param: if TRUE then create a new dir #PARAM 11 |
156 |
#12) day_to_mosaic: daily mosaics NULL then mosaic all days of the year #PARAM 12
|
|
156 |
#12) day_to_mosaic_range: start and end date for daily mosaics, if NULL then mosaic all days of the year #PARAM 12
|
|
157 | 157 |
#13) proj_str :porjection used by tiles e.g. CRS_WGS84 #PARAM 13 |
158 | 158 |
#14) file_format: output file format used for raster eg ".tif" #PARAM 14 |
159 | 159 |
#15) NA_value: NA value used e.g. -9999 #PARAM 15 |
... | ... | |
220 | 220 |
create_out_dir_param <- list_param_run_mosaicing_prediction$create_out_dir_param # FALSE #PARAM 12 |
221 | 221 |
|
222 | 222 |
#if daily mosaics NULL then mosaicas all days of the year #PARAM 13 |
223 |
day_to_mosaic <- list_param_run_mosaicing_prediction$day_to_mosaic # c("19920101","19920102","19920103") #,"19920104","19920105") #PARAM9, two dates note in /tiles for now on NEX
|
|
223 |
day_to_mosaic_range <- list_param_run_mosaicing_prediction$day_to_mosaic_range # c("19920101","19920102","19920103") #,"19920104","19920105") #PARAM9, two dates note in /tiles for now on NEX
|
|
224 | 224 |
|
225 | 225 |
#CRS_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1 |
226 | 226 |
#CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
... | ... | |
260 | 260 |
|
261 | 261 |
####### PART 1: Read in data and process data ######## |
262 | 262 |
|
263 |
|
|
264 | 263 |
#out_dir <- in_dir #PARAM 11 |
265 | 264 |
#in_dir_tiles <- file.path(in_dir,"tiles") #this is valid both for Atlas and NEX |
266 | 265 |
NA_flag_val <- NA_value #PARAM 16 |
... | ... | |
268 | 267 |
#in_dir <- file.path(in_dir,region_name) |
269 | 268 |
#out_dir <- in_dir |
270 | 269 |
if(create_out_dir_param==TRUE){ |
271 |
create_dir_fun() |
|
272 | 270 |
out_dir_tmp <- file.path(out_dir,"mosaic") |
271 |
# #create if does not exists |
|
272 |
if(!file.exists(out_dir_tmp)){ |
|
273 |
dir.create(out_dir_tmp) |
|
274 |
} |
|
273 | 275 |
out_dir <- create_dir_fun(out_dir_tmp,out_suffix) |
274 | 276 |
setwd(out_dir) |
275 | 277 |
}else{ |
... | ... | |
278 | 280 |
|
279 | 281 |
setwd(out_dir) |
280 | 282 |
|
281 |
###on 04/05 skipping this for now |
|
282 | 283 |
### Read in assessment and accuracy files |
283 | 284 |
df_assessment_files <- read.table(df_assessment_files_name,stringsAsFactors=F,sep=",") |
284 | 285 |
|
285 |
#tb_v_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[2])) |
|
286 |
#tb_s_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[4])) |
|
287 |
#tb_s_month_accuracy_name <- file.path(in_dir, basename(df_assessment_files$files[3])) |
|
288 |
#data_month_s_name <- file.path(in_dir,basename(df_assessment_files$files[5])) |
|
289 |
#data_day_v_name <- file.path(in_dir, basename(df_assessment_files$files[6])) |
|
290 |
#data_day_s_name <- file.path(in_dir, basename(df_assessment_files$files[7])) |
|
291 |
##data_month_v_name <- file.path(in_dir,basename(df_assessment_files$files[8])) |
|
292 |
#pred_data_month_info_name <- file.path(in_dir, basename(df_assessment_files$files[9])) |
|
293 |
#pred_data_day_info_name <- file.path(in_dir, basename(df_assessment_files$files[10])) |
|
294 |
#df_tile_processed_name <- file.path(in_dir, basename(df_assessment_files$files[11])) |
|
295 |
|
|
296 | 286 |
tb_v_accuracy_name <- df_assessment_files$files[2] |
297 | 287 |
tb_s_accuracy_name <- df_assessment_files$files[4] |
298 | 288 |
tb_s_month_accuracy_name <- df_assessment_files$files[3] |
299 | 289 |
data_month_s_name <- df_assessment_files$files[5] |
300 |
data_day_v_name <- df_assessment_files$files[6] |
|
301 |
data_day_s_name <- df_assessment_files$files[7] |
|
290 |
data_day_s_name <- df_assessment_files$files[6] |
|
291 |
data_day_v_name <- df_assessment_files$files[7] |
|
292 |
|
|
302 | 293 |
##data_month_v_name <- file.path(in_dir,basename(df_assessment_files$files[8])) |
303 |
pred_data_month_info_name <- df_assessment_files$files[9]
|
|
304 |
pred_data_day_info_name <- df_assessment_files$files[10]
|
|
305 |
df_tile_processed_name <- df_assessment_files$files[11]
|
|
294 |
pred_data_month_info_name <- df_assessment_files$files[10]
|
|
295 |
pred_data_day_info_name <- df_assessment_files$files[11]
|
|
296 |
df_tile_processed_name <- df_assessment_files$files[12]
|
|
306 | 297 |
# accuracy table by tiles |
307 | 298 |
tb <- read.table(tb_v_accuracy_name,sep=",") |
308 | 299 |
tb_s <- read.table(tb_s_accuracy_name,sep=",") |
... | ... | |
311 | 302 |
data_day_v <- read.table(data_day_v_name,sep=",") #daily training stations by dates and tiles |
312 | 303 |
df_tile_processed <- read.table(df_tile_processed_name,sep=",") |
313 | 304 |
|
314 |
#list all files to mosaic for a list of day |
|
315 |
#Take into account multiple region in some cases!!! |
|
316 |
reg_lf_mosaic <- vector("list",length=length(region_name)) |
|
317 |
|
|
318 |
#for(k in 1:length(region_names)){ |
|
319 | 305 |
#this part needs to be improve make this a function and use multicore to loop through files... |
320 | 306 |
#give a range of dates to run... |
321 |
if(is.null(day_to_mosaic)){ |
|
322 |
start_date <- #first date |
|
323 |
end_date <- |
|
324 |
} |
|
325 | 307 |
|
326 |
for(k in 1:length(region_name)){ |
|
327 |
in_dir_tiles_tmp <- file.path(in_dir, region_name[k]) |
|
328 |
#fix this later and add the year.. |
|
329 |
#gam_CAI_dailyTmax_predicted_mod1 |
|
330 |
reg_lf_mosaic[[k]] <- lapply(1:length(day_to_mosaic),FUN=function(i){list.files(path=file.path(in_dir_tiles_tmp), |
|
331 |
pattern=paste("gam_CAI_dailyTmax_predicted_",pred_mod_name,".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=T)}) |
|
308 |
if(is.null(day_to_mosaic_range)){ |
|
309 |
# start_date <- #first date |
|
310 |
start_date <- paste0(year_processed,"0101") #change this later!! |
|
311 |
end_date <- paste0(year_processed,"0101") #change this later!! |
|
312 |
day_to_mosaic <- seq(as.Date(strptime(start_date,"%Y%m%d")), as.Date(strptime(end_date,"%Y%m%d")), 'day') |
|
313 |
day_to_mosaic <- format(day_to_mosaic,"%Y%m%d") #format back to the relevant date format for files |
|
314 |
}else{ |
|
315 |
start_date <- day_to_mosaic_range[1] |
|
316 |
end_date <- day_to_mosaic_range[2] |
|
317 |
day_to_mosaic <- seq(as.Date(strptime(start_date,"%Y%m%d")), as.Date(strptime(end_date,"%Y%m%d")), 'day') |
|
318 |
day_to_mosaic <- format(day_to_mosaic,"%Y%m%d") #format back to the relevant date format for files |
|
332 | 319 |
} |
333 | 320 |
|
321 |
in_dir_tiles_tmp <- file.path(in_dir, region_name) |
|
322 |
#fix this later and add the year.. |
|
323 |
#gam_CAI_dailyTmax_predicted_mod1 |
|
324 |
|
|
325 |
lf_mosaic <- lapply(1:length(day_to_mosaic),FUN=function(i){list.files(path=file.path(in_dir_tiles_tmp), |
|
326 |
pattern=paste("gam_CAI_dailyTmax_predicted_",pred_mod_name,".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=T)}) |
|
327 |
|
|
334 | 328 |
#reg_lf_mosaic[[k]] <- list.files(path=file.path(in_dir_tiles_tmp),pattern=paste(".*.",day_to_mosaic[i],".*.tif$",sep=""),full.names=T,recursive=T) |
335 | 329 |
##################### PART 2: produce the mosaic ################## |
336 | 330 |
|
337 | 331 |
#This is is assuming a list of file for a region!! |
338 | 332 |
#this is where the main function for mosaicing region starts!! |
339 | 333 |
#use reg4 to test the code for now, redo later for any regions!!! |
340 |
k<-1 |
|
341 |
#for(k in 1:length(region_name)){ |
|
342 |
region_selected <- region_name[k] |
|
343 |
########################## |
|
344 |
#### First generate rmse images for each date and tile for the region |
|
345 |
|
|
346 |
|
|
347 |
lf_mosaic <- reg_lf_mosaic[[k]] #list of files to mosaic by regions |
|
334 |
|
|
335 |
region_selected <- region_name |
|
336 |
|
|
348 | 337 |
#There a 28 files for reg4, South America |
349 | 338 |
|
350 | 339 |
####################################### |
... | ... | |
383 | 372 |
#Plot as quick check |
384 | 373 |
#r1 <- raster(lf_mosaic[[1]][1]) |
385 | 374 |
#plot(r1) |
375 |
#browser() |
|
386 | 376 |
|
387 | 377 |
#################################### |
388 | 378 |
### Now create accuracy surfaces from residuals... |
... | ... | |
407 | 397 |
#NA_flag_val |
408 | 398 |
#file_format |
409 | 399 |
out_dir_str <- out_dir |
410 |
out_suffix_str <- out_suffix |
|
400 |
#out_suffix_str <- out_suffix
|
|
411 | 401 |
days_to_process <- day_to_mosaic |
402 |
out_suffix_str <- paste("data_day_v_",out_suffix,sep="") |
|
403 |
|
|
404 |
#browser() |
|
412 | 405 |
df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX) |
413 | 406 |
df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX)) |
414 | 407 |
|
... | ... | |
502 | 495 |
##took 31 minutes to generate the residuals maps for each tiles (28) for region 4 |
503 | 496 |
|
504 | 497 |
###################################################### |
505 |
#### PART 2: GENETATE MOSAIC FOR LIST OF FILES #####
|
|
498 |
#### PART 2: GENERATE MOSAIC FOR LIST OF FILES #####
|
|
506 | 499 |
################################# |
507 | 500 |
#### Mosaic tiles for the variable predicted and accuracy metric |
508 | 501 |
|
Also available in: Unified diff
fixing data range for mosaicing, output dir and reading of assessment inputs