Revision c8780299
Added by Benoit Parmentier about 8 years ago
climate/research/oregon/interpolation/global_product_assessment_part0_functions.R | ||
---|---|---|
9 | 9 |
# |
10 | 10 |
#AUTHOR: Benoit Parmentier |
11 | 11 |
#CREATED ON: 10/31/2016 |
12 |
#MODIFIED ON: 11/03/2016
|
|
12 |
#MODIFIED ON: 11/04/2016
|
|
13 | 13 |
#Version: 1 |
14 | 14 |
#PROJECT: Environmental Layers project |
15 | 15 |
#COMMENTS: removing unused functions and clean up for part0 global prodduct assessment part0 |
... | ... | |
26 | 26 |
# |
27 | 27 |
#setfacl -Rmd user:aguzman4:rwx /nobackupp8/bparmen1/output_run10_1500x4500_global_analyses_pred_1992_10052015 |
28 | 28 |
|
29 |
##COMMIT: modifying function to check missing files and dates for predictions and others
|
|
29 |
##COMMIT: adding necessary function for assessment from other script
|
|
30 | 30 |
|
31 | 31 |
################################################################################################# |
32 | 32 |
|
... | ... | |
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
|
74 |
extract_date <- function(i,x,item_no=NULL){ |
|
75 |
y <- unlist(strsplit(x[[i]],"_")) |
|
76 |
if(is.null(item_no)){ |
|
77 |
date_str <- y[length(y)-2] #count from end |
|
78 |
}else{ |
|
79 |
date_str <- y[item_no] |
|
80 |
} |
|
81 |
return(date_str) |
|
82 |
} |
|
83 |
|
|
84 |
finding_missing_dates <- function(date_start,date_end,list_dates){ |
|
85 |
#this assumes daily time steps!! |
|
86 |
#can be improved later on |
|
87 |
|
|
88 |
#date_start <- "19840101" |
|
89 |
#date_end <- "19991231" |
|
90 |
date1 <- as.Date(strptime(date_start,"%Y%m%d")) |
|
91 |
date2 <- as.Date(strptime(date_end,"%Y%m%d")) |
|
92 |
dates_range <- seq.Date(date1, date2, by="1 day") #sequence of dates |
|
93 |
|
|
94 |
missing_dates <- setdiff(as.character(dates_range),as.character(list_dates)) |
|
95 |
#df_dates_missing <- data.frame(date=missing_dates) |
|
96 |
#which(df_dates$date%in%missing_dates) |
|
97 |
#df_dates_missing$missing <- 1 |
|
98 |
|
|
99 |
df_dates <- data.frame(date=as.character(dates_range),missing = 0) |
|
100 |
|
|
101 |
df_dates$missing[df_dates$date %in% missing_dates] <- 1 |
|
102 |
#a$flag[a$id %in% temp] <- 1 |
|
74 | 103 |
|
104 |
missing_dates_obj <- list(missing_dates,df_dates) |
|
105 |
names(missing_dates_obj) <- c("missing_dates","df_dates") |
|
106 |
return(missing_dates_obj) |
|
107 |
} |
|
75 | 108 |
check_missing <- function(lf, pattern_str=NULL,in_dir=".",date_start="1984101",date_end="20141231",item_no=13,out_suffix="",num_cores=1,out_dir="."){ |
76 | 109 |
#Function to check for missing files such as mosaics or predictions for tiles etc. |
77 | 110 |
#The function assumes the name of the files contain "_". |
... | ... | |
137 | 170 |
return(df_time_series_obj) |
138 | 171 |
} |
139 | 172 |
|
173 |
centroids_shp_fun <- function(i,list_shp_reg_files){ |
|
174 |
|
|
175 |
# |
|
176 |
shp_filename <- list_shp_reg_files[[i]] |
|
177 |
layer_name <- sub(".shp","",basename(shp_filename)) |
|
178 |
path_to_shp <- dirname(shp_filename) |
|
179 |
shp1 <- try(readOGR(path_to_shp, layer_name)) #use try to resolve error below |
|
180 |
#shp_61.0_-160.0 |
|
181 |
#Geographical CRS given to non-conformant data: -186.331747678 |
|
182 |
|
|
183 |
#shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]]))) |
|
184 |
if (!inherits(shp1,"try-error")) { |
|
185 |
pt <- gCentroid(shp1) |
|
186 |
#centroids_pts[[i]] <- pt |
|
187 |
}else{ |
|
188 |
pt <- shp1 |
|
189 |
#centroids_pts[[i]] <- pt |
|
190 |
} |
|
191 |
|
|
192 |
#shps_tiles[[i]] <- shp1 |
|
193 |
#centroids_pts[[i]] <- centroids |
|
194 |
|
|
195 |
shp_obj <- list(shp1,pt) |
|
196 |
names(shp_obj) <- c("spdf","centroid") |
|
197 |
return(shp_obj) |
|
198 |
} |
|
199 |
|
|
200 |
rasterize_tile_day <- function(i,list_spdf,df_missing,list_r_ref,col_name,date_val){ |
|
201 |
# |
|
202 |
# |
|
203 |
tile_spdf <- list_spdf[[i]] |
|
204 |
tile_coord <- names(list_spdf)[i] |
|
205 |
r_ref <- list_r_ref[[i]] |
|
206 |
|
|
207 |
df_tmp <- subset(df_missing,date==date_val,select=tile_coord) |
|
208 |
#for each row (date) |
|
209 |
val <- df_tmp[[tile_coord]] |
|
210 |
if(val==1){ |
|
211 |
val<-0 #missing then not predicted |
|
212 |
}else{ |
|
213 |
val<-1 |
|
214 |
} |
|
215 |
|
|
216 |
tile_spdf$predicted <- val |
|
217 |
tile_spdf$tile_coord <- tile_coord |
|
218 |
tile_spdf$overlap <- 1 |
|
219 |
|
|
220 |
#r <- rasterize(tile_spdf,r_ref,"predicted") |
|
221 |
#r <- rasterize(tile_spdf,r_ref,col_name) |
|
222 |
#r <- raster(r_ref,crs=projection(r_ref)) #new layer without values |
|
223 |
r <- raster(r_ref) #new layer without values |
|
224 |
|
|
225 |
if(col_name=="overlap"){ |
|
226 |
set1f <- function(x){rep(1, x)} |
|
227 |
r <- init(r, fun=set1f, overwrite=TRUE) |
|
228 |
} |
|
229 |
if(col_name=="predicted"){ |
|
230 |
set1f <- function(x){rep(val, x)} |
|
231 |
r <- init(r, fun=set1f, overwrite=TRUE) |
|
232 |
} |
|
233 |
|
|
234 |
return(r) |
|
235 |
} |
|
140 | 236 |
|
141 | 237 |
############################ END OF SCRIPT ################################## |
Also available in: Unified diff
adding necessary function for assessment from other script