Project

General

Profile

« Previous | Next » 

Revision 3edc5450

Added by Benoit Parmentier over 8 years ago

cleaning code and plotting time series for raster stack at centroids for NASA conference

View differences:

climate/research/oregon/interpolation/NASA2016_conference_temperature_predictions.R
141 141
  return(raster_name_out)
142 142
}
143 143

  
144
plot_raster_mosaic <- function(i,list_param){
145
  #Function to plot mosaic for poster
146
  #
147
  l_dates <- list_param$l_dates
148
  r_mosaiced_scaled <- list_param$r_mosaiced_scaled
149
  NA_flag_val <- list_param$NA_flag_val
150
  out_dir <- list_param$out_dir
151
  out_suffix <- list_param$out_suffix
152
  region_name <- list_param$region_name
153
  variable_name <- list_param$variable_name
154

  
155
#for (i in 1:length(nlayers(r_mosaic_scaled))){
156
  
157
  date_proc <- l_dates[i]
158
  r_pred <- subset(r_mosaic_scaled,i)
159
  NAvalue(r_pred)<- NA_flag_val 
160
 
161
  date_proc <- l_dates[i]
162
  date_val <- as.Date(strptime(date_proc,"%Y%m%d"))
163
  #month_name <- month.name(date_val)
164
  month_str <- format(date_val, "%b") ## Month, char, abbreviated
165
  year_str <- format(date_val, "%Y") ## Year with century
166
  day_str <- as.numeric(format(date_val, "%d")) ## numeric month
167
  date_str <- paste(month_str," ",day_str,", ",year_str,sep="")
168
  
169
  res_pix <- 1200
170
  #res_pix <- 480
171
  col_mfrow <- 1
172
  row_mfrow <- 1
173
  
174
  png_filename <-  file.path(out_dir,paste("Figure4_clim_mosaics_day_","_",date_proc,"_",region_name,"_",out_suffix,".png",sep =""))
175
  title_str <-  paste("Predicted ",variable_name, " on ",date_str , " ", sep = "")
176
  
177
  png(filename=png_filename,width = col_mfrow * res_pix,height = row_mfrow * res_pix)
178
  plot(r_pred,main =title_str,cex.main =1.5,col=matlab.like(255),zlim=c(-50,50),
179
       legend.shrink=0.8,legend.width=0.8)
180
       #axis.args = list(cex.axis = 1.6), #control size of legend z
181
       #legend.args=list(text='dNBR', side=4, line=2.5, cex=2.2))
182
       #legend.args=list(text='dNBR', side=4, line=2.49, cex=1.6))
183
  dev.off()
184

  
185
  return(png_filename)
186
}
187

  
188
extract_date <- function(i,x,item_no=NULL){
189
  y <- unlist(strsplit(x[[i]],"_"))
190
  if(is.null(item_no)){
191
    date_str <- y[length(y)-2] #count from end
192
  }else{
193
    date_str <- y[item_no]
194
  }
195
  return(date_str)
196
}
197

  
144 198
###############################
145 199
####### Parameters, constants and arguments ###
146 200

  
......
281 335

  
282 336
lf_mosaic_plot_fig <- mclapply(1:length(lf_mosaic_scaled),FUN=plot_raster_mosaic,list_param=list_param_plot_raster_mosaic,mc.preschedule=FALSE,mc.cores = num_cores)                         
283 337

  
284
plot_raster_mosaic <- function(i,list_param){
285
  #Function to plot mosaic for poster
286
  #
287
  l_dates <- list_param$l_dates
288
  r_mosaiced_scaled <- list_param$r_mosaiced_scaled
289
  NA_flag_val <- list_param$NA_flag_val
290
  out_dir <- list_param$out_dir
291
  out_suffix <- list_param$out_suffix
292
  region_name <- list_param$region_name
293
  variable_name <- list_param$variable_name
294

  
295
#for (i in 1:length(nlayers(r_mosaic_scaled))){
296
  
297
  date_proc <- l_dates[i]
298
  r_pred <- subset(r_mosaic_scaled,i)
299
  NAvalue(r_pred)<- NA_flag_val 
300
 
301
  date_proc <- l_dates[i]
302
  date_val <- as.Date(strptime(date_proc,"%Y%m%d"))
303
  #month_name <- month.name(date_val)
304
  month_str <- format(date_val, "%b") ## Month, char, abbreviated
305
  year_str <- format(date_val, "%Y") ## Year with century
306
  day_str <- as.numeric(format(date_val, "%d")) ## numeric month
307
  date_str <- paste(month_str," ",day_str,", ",year_str,sep="")
308
  
309
  res_pix <- 1200
310
  #res_pix <- 480
311
  col_mfrow <- 1
312
  row_mfrow <- 1
313
  
314
  png_filename <-  file.path(out_dir,paste("Figure4_clim_mosaics_day_","_",date_proc,"_",region_name,"_",out_suffix,".png",sep =""))
315
  title_str <-  paste("Predicted ",variable_name, " on ",date_str , " ", sep = "")
316
  
317
  png(filename=png_filename,width = col_mfrow * res_pix,height = row_mfrow * res_pix)
318
  plot(r_pred,main =title_str,cex.main =1.5,col=matlab.like(255),zlim=c(-50,50),
319
       legend.shrink=0.8,legend.width=0.8)
320
       #axis.args = list(cex.axis = 1.6), #control size of legend z
321
       #legend.args=list(text='dNBR', side=4, line=2.5, cex=2.2))
322
       #legend.args=list(text='dNBR', side=4, line=2.49, cex=1.6))
323
  dev.off()
324 338

  
325
  return(png_filename)
326
}
327 339

  
328 340
############### PART2: temporal profile #############
329 341
#### Extract time series
......
331 343
#-65,-22
332 344

  
333 345
df_points <- read.table(df_points_extracted_fname,sep=",") 
334
df_centroids <- read.table(df_centroids_fname,sep=",")
346
df_points_tmp <- df_points
347
df_points <- as.data.frame(t(df_points))
348
names(df_points) <- paste0("ID_",1:ncol(df_points))
349

  
350
#df_centroids <- read.table(df_centroids_fname,sep=",")
335 351

  
336 352
coordinates(df_centroids)<- c("long","lat")
337 353
proj4string(df_centroids) <- CRS_locs_WGS84
......
342 358

  
343 359
df_points$files <- lf_mosaic_list
344 360

  
345
extract_date <- function(i,x){
346
  y <- unlist(strsplit(x[[i]],"_"))
347
  date_str <- y[length(y)-2]
348
}
349 361
#debug(extract_date)
350
#test <- (extract_date(1,lf_mosaic_list))
351

  
352
list_dates_produced <- unlist(mclapply(1:length(lf_mosaic_list),FUN=extract_date,mc.preschedule=FALSE,mc.cores = num_cores))                         
353
#list_dates_produced <- mclapply(1:11,FUN=extract_date,mc.preschedule=FALSE,x=lf_mosaic_list,mc.cores = num_cores)                         
362
#test <- extract_date(6431,lf_mosaic_list,12) #extract item number 12 from the name of files to get the data
354 363

  
364
list_dates_produced <- unlist(mclapply(1:length(lf_mosaic_list),FUN=extract_date,x=lf_mosaic_list,item_no=12,mc.preschedule=FALSE,mc.cores = num_cores))                         
365
#list_dates_produced <-  mclapply(6400:6431,FUN=extract_date,x=lf_mosaic_list,item_no=12,mc.preschedule=FALSE,mc.cores = num_cores)                         
355 366

  
356 367
list_dates_produced_date_val <- as.Date(strptime(list_dates_produced,"%Y%m%d"))
357 368
month_str <- format(list_dates_produced_date_val, "%b") ## Month, char, abbreviated
......
363 374
df_points$year <- year_str
364 375
df_points$day <- day_str
365 376

  
377
unique_date_tb <-table(df_points$date)
378
unique_date <- unique(df_points$date)
379

  
380
station_id <- 8
381
var_name <-paste0("ID_",station_id)
382
#aggregate(sdf_tmp
383
if(max(unique_date_tb)>1){
384
#  formula_str <- paste(var_name," ~ ","TRIP_START_DATE_f",sep="")
385
   var_pix <- aggregate(ID_8 ~ date, data = df_points, mean) #aggregate by date
386
}
387

  
388
 
389
#start_date <-input$dates[1]
390
#end_date <-input$dates[2]
391

  
392
d_z_tmp <- zoo(df_points[,station_id],df_points$date)
393

  
394

  
395
d_z <- window(d_z_tmp,start=start_date,end=end_date)   
396

  
397
  
366 398
#data_pixel <- data_df[id_selected,]
367 399
#data_pixel$rainfall <- as.numeric(data_pixel$rainfall)
368 400
#d_z_tmp <-zoo(data_pixel$rainfall,as.Date(data_pixel$date))
......
378 410
#df2 <- as.data.frame(d_z2)
379 411
#names(df2)<- var_name
380 412

  
413
#df_tmp <- subset(data_var,data_var$ID_stat==id_name)
414
#if(da)
415

  
381 416

  
382 417
############################ END OF SCRIPT ##################################

Also available in: Unified diff