Project

General

Profile

« Previous | Next » 

Revision c3c3b468

Added by Benoit Parmentier over 8 years ago

extracting dates from raster file name and assembling station data from assessment

View differences:

climate/research/oregon/interpolation/global_product_assessment_part1.R
72 72
function_assessment_part1a <-"global_run_scalingup_assessment_part1a_01042016.R"
73 73
function_assessment_part2 <- "global_run_scalingup_assessment_part2_02092016.R"
74 74
function_assessment_part2_functions <- "global_run_scalingup_assessment_part2_functions_01032016.R"
75
function_assessment_part3 <- "global_run_scalingup_assessment_part3_04292016b.R"
75
function_assessment_part3 <- "global_run_scalingup_assessment_part3_05162016.R"
76 76
source(file.path(script_path,function_assessment_part1_functions)) #source all functions used in this script 
77 77
source(file.path(script_path,function_assessment_part1a)) #source all functions used in this script 
78 78
source(file.path(script_path,function_assessment_part2)) #source all functions used in this script 
......
155 155
                    "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/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")
156 156

  
157 157
#l_dates <- c("19990101","19990102","19990103","19990701","19990702","19990703")
158
l_dates <- c("19920101","19920102","19920103","19920701","19920702","19990703")
158
l_dates <- c("19920101","19920102","19920103","19920701","19920702","19920703") #dates to plot and analze
159 159

  
160 160
df_points_extracted_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/data_points_extracted.txt"
161 161
NA_flag_val_mosaic <- -3399999901438340239948148078125514752.000
......
196 196
list_pred_data_month_info_fname <- list.files(path=in_dir_list,"pred_data_month_info.*.txt",full.names=T)
197 197
list_pred_data_day_info_fname <- list.files(path=in_dir_list,"pred_data_day_info.*.txt",full.names=T)
198 198

  
199
### Get data information
200
data_date <- unlist(lapply(list_data_s_fname, function(x){unlist(strsplit(basename(x),"_"))[5]}))
201
df_data_s_fname <- data.frame(data_date,region_name,dataset="data_s",file=list_data_s_fname)
202

  
203
year_str <- as.character(unlist(lapply(list_data_v_fname, function(x){unlist(strsplit(basename(x),"_"))[5]})))
204
df_data_v_fname <- data.frame(year_str,region_name,dataset="data_v",file=list_data_s_fname)
205

  
206
df_data_v_fname$year <- df_data_v_fname$year_str
207
df_data_v_fname$year <- as.character(df_data_v_fname$year_str)
208

  
209

  
199 210
## Use station from specific year and date?
200 211

  
212
list_dates_val <- as.Date(strptime(l_dates,"%Y%m%d"))
213
month_str <- format(list_dates_val, "%b") ## Month, char, abbreviated
214
year_str <- format(list_dates_val, "%Y") ## Year with century
215
day_str <- as.numeric(format(list_dates_val, "%d")) ## numeric month
216

  
217
for(i in 1:length(unique(year_str))){
218
  df_data_v_fname$data_date==year_str[i]
219
  filename_tmp <- as.character(df_data_v_fname$file[9])
220
  df_stations_tmp <- read.table(filename_tmp,stringsAsFactors=F,sep=",")
221
  
222
}
223

  
224
df_points <- subset(df_stations,df_stations_tmp$date==l_dates[1])
225
table(df_points$tile_id)
226
plot(df_points,add=T)
227
coordinates(df_points) <- cbind(df_points$x,df_points$y)
228
proj4string(df_points) <- CRS_locs_WGS84
229

  
230
#####################
231
#select one station based on id or coordinates and find that in the list of data.frame??
232

  
233
id_selected <- "82111099999"
234
dim(df_points)
235

  
236

  
237
### loop through all files and get the time series
238
extract_from_df <- function(x,col_selected,val_selected){
239
  df_tmp <- read.table(x,stringsAsFactors=F,sep=",")
240
  #data_subset <- subset(data_stations,col_selected==val_selected)
241
  data_subset <- subset(df_tmp,df_tmp$id%in%val_selected)
242
  return(data_subset)
243
}
244

  
245
lf_data_s_subset <- mclapply(list_data_s_fname,
246
                           FUN=extract_from_df,
247
                           col_selected="id",
248
                           val_selected=id_selected,
249
                           mc.preschedule=FALSE,
250
                           mc.cores = num_cores)                         
251
lf_data_v_subset <- mclapply(list_data_v_fname,
252
                           FUN=extract_from_df,
253
                           col_selected="id",
254
                           val_selected=id_selected,
255
                           mc.preschedule=FALSE,
256
                           mc.cores = num_cores)   
257

  
258
data_v_subset <- do.call(rbind,lf_data_v_subset)
259
data_s_subset <- do.call(rbind,lf_data_s_subset)
260

  
261
data_s_subset$training <- 1
262
data_v_subset$training <- 0
263

  
264
data_stations <- rbind(data_s_subset,data_v_subset)
265
dim(data_s_subset)
266
dim(data_v_subset)
267

  
268
#rbind.fill(mtcars[c("mpg", "wt")], mtcars[c("wt", "cyl")])
269
data_stations <- rbind.fill(data_v_subset, data_s_subset)
201 270

  
271
coordinates(data_stations) <- cbind(data_stations$x,data_stations$y)
272
proj4string(data_stations) <- CRS_locs_WGS84
273

  
274
dim(data_stations)
275

  
276

  
277

  
278
##Now grab year year 1992 or matching year...maybe put this in a data.frame??
202 279

  
203 280
pattern_str <-"*.tif"
204 281
lf_mosaic_list <- list.files(path=in_dir_mosaic,pattern=pattern_str,recursive=F,full.names=T)
......
255 332

  
256 333
#Use the global output??
257 334

  
335
dim(data_stations)
336

  
337

  
338
df_points_extracted <- extract(r_mosaic,data_stations,df=T)
339
df_points_extracted_fname <- paste0("df_points_extracted_fname",".txt")
340
write.table(df_points_extracted,file=df_points_extracted_fname) 
258 341

  
259 342
df_points <- read.table(df_points_extracted_fname,sep=",") 
260 343
df_points_tmp <- df_points

Also available in: Unified diff