Revision b31c2ea8
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/global_product_assessment_part1.R | ||
---|---|---|
4 | 4 |
#Combining tables and figures for individual runs for years and tiles. |
5 | 5 |
#AUTHOR: Benoit Parmentier |
6 | 6 |
#CREATED ON: 05/15/2016 |
7 |
#MODIFIED ON: 09/11/2016
|
|
7 |
#MODIFIED ON: 09/16/2016
|
|
8 | 8 |
#Version: 1 |
9 | 9 |
#PROJECT: Environmental Layers project |
10 | 10 |
#COMMENTS: Initial commit, script based on part NASA biodiversity conferenc |
... | ... | |
19 | 19 |
# |
20 | 20 |
#setfacl -Rmd user:aguzman4:rwx /nobackupp8/bparmen1/output_run10_1500x4500_global_analyses_pred_1992_10052015 |
21 | 21 |
|
22 |
#COMMIT: testing plot function for training residuals at at stations and fixing bugs, region 1
|
|
22 |
#COMMIT: fixing extraction function and and missing dates
|
|
23 | 23 |
|
24 | 24 |
################################################################################################# |
25 | 25 |
|
... | ... | |
148 | 148 |
python_bin <- "/usr/bin" #PARAM 30 |
149 | 149 |
|
150 | 150 |
day_start <- "1984101" #PARAM 12 arg 12 |
151 |
day_end <- "19981231" #PARAM 13 arg 13 |
|
151 |
day_end <- "19991231" #PARAM 13 arg 13 |
|
152 |
#date_start <- |
|
153 |
#date_end <- |
|
152 | 154 |
|
153 | 155 |
#infile_mask <- "/nobackupp8/bparmen1/NEX_data/regions_input_files/r_mask_LST_reg4.tif" |
154 | 156 |
#infile_mask <- "/data/project/layers/commons/NEX_data/regions_input_files/r_mask_LST_reg5.tif" |
... | ... | |
163 | 165 |
df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/mosaic/output_reg1_1984/df_centroids_19840101_reg1_1984.txt" |
164 | 166 |
#/nobackupp6/aguzman4/climateLayers/out/reg1/assessment//output_reg1_1984/df_assessment_files_reg1_1984_reg1_1984.txt |
165 | 167 |
|
166 |
#raster_name_lf <- c("/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920101_reg4_1992_m_gam_CAI_dailyTmax_19920101_reg4_1992.tif", |
|
167 |
# "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920102_reg4_1992_m_gam_CAI_dailyTmax_19920102_reg4_1992.tif", |
|
168 |
# "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920103_reg4_1992_m_gam_CAI_dailyTmax_19920103_reg4_1992.tif", |
|
169 |
# "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920701_reg4_1992_m_gam_CAI_dailyTmax_19920701_reg4_1992.tif", |
|
170 |
# "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920702_reg4_1992_m_gam_CAI_dailyTmax_19920702_reg4_1992.tif", |
|
171 |
# "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19920703_reg4_1992_m_gam_CAI_dailyTmax_19920703_reg4_1992.tif") |
|
168 |
#dates to plot and analyze |
|
172 | 169 |
|
173 | 170 |
#l_dates <- c("19990101","19990102","19990103","19990701","19990702","19990703") |
174 |
l_dates <- c("19990101","19990102","19990103","19990104","19990105") #dates to plot and analze |
|
175 |
|
|
171 |
l_dates <- c("19990101","19990102","19990103","19990104","19990105") |
|
176 | 172 |
#df_points_extracted_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg5/mosaic/int_mosaics/data_points_extracted.txt" |
177 |
df_points_extracted_fname <- NULL #if null compute on the fly
|
|
173 |
df_points_extracted_fname <- NULL #if null extract on the fly
|
|
178 | 174 |
#r_mosaic_fname <- "r_mosaic.RData" |
179 | 175 |
r_mosaic_fname <- NULL #if null create a stack from input dir |
180 | 176 |
|
... | ... | |
532 | 528 |
|
533 | 529 |
df_produced <- data.frame(basename(lf_mosaic_list),list_dates_produced_date_val,month_str,year_str,day_str,dirname(lf_mosaic_list)) |
534 | 530 |
|
535 |
finding_missing_dates <- function(date_start,date_end,list_dates){ |
|
536 |
|
|
537 |
date_start <- "19840101" |
|
538 |
date_end <- "19991231" |
|
539 |
date1 <- as.Date(strptime(date_start,"%Y%m%d")) |
|
540 |
date2 <- as.Date(strptime(date_end,"%Y%m%d")) |
|
541 |
dates_range <- seq.Date(date1, date2, by="1 day") #sequence of dates |
|
542 |
|
|
543 |
missing_dates <- setdiff(as.character(dates_range),as.character(list_dates_produced_date_val)) |
|
544 |
|
|
545 |
return(missing_dates) |
|
546 |
} |
|
547 |
|
|
548 |
|
|
549 | 531 |
|
550 | 532 |
|
551 | 533 |
#### |
... | ... | |
558 | 540 |
in_dir_mosaic_rmse <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg1/mosaicsRMSE/mosaic" |
559 | 541 |
#pattern_str <- ".*.tif" |
560 | 542 |
|
561 |
extract_from_time_series_raster_stack <- function(df_points,date_start,date_end,lf_raster,item_no=13,num_cores=4,pattern_str=NULL,in_dir=NULL,out_dir=".",out_suffix=""){ |
|
562 |
# |
|
563 |
#This function extract value given from a raster stack time series given a spatial data.frame and a list of files |
|
543 |
extract_from_time_series_raster_stack(df_points,date_start,date_end,lf_raster,item_no=13,num_cores=4,pattern_str=NULL,in_dir=NULL,out_dir=".",out_suffix="") |
|
564 | 544 |
# |
565 |
#INPUTS |
|
566 |
#1) df_points |
|
567 |
#2) date_start,num_cores=4,pattern_str=NULL,in_dir=NULL,out_dir=".",out_suffix= |
|
568 |
#3) date_end |
|
569 |
#3) lf_raster |
|
570 |
#4) item_no=13 |
|
571 |
#5) num_cores=4, |
|
572 |
#6) pattern_str=NULL |
|
573 |
#7) in_dir=NULL, |
|
574 |
#8) out_dir="." |
|
575 |
#9) out_suffix="" |
|
576 |
#OUTPUTS |
|
577 |
# |
|
578 |
# |
|
579 |
|
|
580 |
#### Start script #### |
|
581 |
|
|
582 |
if(is.null(lf_raster)){ |
|
583 |
#pattern_str <- ".*.tif" |
|
584 |
pattern_str <-"*.tif" |
|
585 |
lf_raster <- list.files(path=in_dir_mosaic,pattern=pattern_str,recursive=F,full.names=T) |
|
586 |
r_stack <- stack(lf_raster,quick=T) #this is very fast now with the quick option! |
|
587 |
#save(r_mosaic,file="r_mosaic.RData") |
|
588 |
|
|
589 |
}else{ |
|
590 |
r_stack <- stack(lf_raster,quick=T) #this is very fast now with the quick option! |
|
591 |
} |
|
592 |
|
|
593 |
#df_points$files <- lf_mosaic_list |
|
594 |
#Use the global output?? |
|
595 |
|
|
596 |
##23.09 (on 05/22) |
|
597 |
#df_points_day_extracted <- extract(r_mosaic,data_stations,df=T) |
|
598 |
#df_points_day_extracted_fname <- paste0("df_points_day_extracted_fname",".txt") |
|
599 |
#write.table(df_points_day_extracted,file=df_points_day_extracted_fname) #5.1 Giga |
|
600 |
#4.51 (on 05/23) |
|
601 |
#df_points_day <- data_stations_var_pred_data_s |
|
602 |
|
|
603 |
#15.17 (on 09/08) |
|
604 |
##10.41 (on 05/22) |
|
605 |
#took about 7h for 5262 layers, maybe can be sped up later |
|
606 |
df_points_extracted <- extract(r_stack,df_points,df=T,sp=T) #attach back to the original data... |
|
607 |
|
|
608 |
#17.19 (on 05/23) |
|
609 |
#22.27 (on 09/08) |
|
610 |
#df_points_extracted_fname <- paste0("df_points_day_extracted_fname2",".txt") |
|
611 |
#17.27 (on 05/23) |
|
612 |
|
|
613 |
df_points_extracted_fname <- file.path(out_dir,paste0("df_points_extracted_",out_suffix,".txt")) |
|
614 |
write.table(df_points_extracted,file= df_points_extracted_fname,sep=",",row.names = F) |
|
615 |
#17.19 (on 05/23) |
|
616 |
|
|
617 |
#### Now check for missing dates |
|
618 |
|
|
619 |
#debug(extract_date) |
|
620 |
#test <- extract_date(6431,lf_mosaic_list,12) #extract item number 12 from the name of files to get the data |
|
621 |
#list_dates_produced <- unlist(mclapply(1:length(lf_raster),FUN=extract_date,x=lf_raster,item_no=13,mc.preschedule=FALSE,mc.cores = num_cores)) |
|
622 |
#list_dates_produced <- mclapply(1:2,FUN=extract_date,x=lf_mosaic_list,item_no=13,mc.preschedule=FALSE,mc.cores = 2) |
|
623 |
list_dates_produced <- unlist(mclapply(1:length(lf_raster),FUN=extract_date,x=lf_raster,item_no=item_no, |
|
624 |
mc.preschedule=FALSE,mc.cores = num_cores)) |
|
625 |
|
|
626 |
list_dates_produced_date_val <- as.Date(strptime(list_dates_produced,"%Y%m%d")) |
|
627 |
month_str <- format(list_dates_produced_date_val, "%b") ## Month, char, abbreviated |
|
628 |
year_str <- format(list_dates_produced_date_val, "%Y") ## Year with century |
|
629 |
day_str <- as.numeric(format(list_dates_produced_date_val, "%d")) ## numeric month |
|
630 |
|
|
631 |
df_raster <- data.frame(basename(lf_mosaic_list),list_dates_produced_date_val,month_str,year_str,day_str,dirname(lf_mosaic_list)) |
|
632 |
|
|
633 |
df_raster_fname <- file.path(out_dir,paste0("df_raster_",out_suffix,".txt")) |
|
634 |
write.table(df_raster,file= df_raster_fname,sep=",",row.names = F) |
|
635 |
|
|
636 |
df_points_extracted_fname |
|
637 |
df_raster_fname |
|
638 |
|
|
639 |
extract_obj <- list(df_points_extracted_fname,df_raster_fname) |
|
640 |
names(extract_obj) <- c("df_points_extracted_fname","df_raster_fname") |
|
641 |
|
|
642 |
return(extract_obj) |
|
643 |
} |
|
644 |
|
|
645 |
|
|
646 |
|
|
647 | 545 |
|
648 | 546 |
|
649 | 547 |
}else{ |
Also available in: Unified diff
more clean up and moving functions from product assessment part 1 to functions script