Project

General

Profile

« Previous | Next » 

Revision 557475f8

Added by Benoit Parmentier about 8 years ago

removing unused functions and clean up for part0 global prodduct assessment part0

View differences:

climate/research/oregon/interpolation/global_product_assessment_part0_functions.R
1 1
####################################  INTERPOLATION OF TEMPERATURES  #######################################
2
#######################  Assessment of product part 2 functions: mosaic and accuracy ##############################
2
#######################  Script for assessment of scaling up on NEX : part 0 ##############################
3 3
#This script uses the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA.
4
#This part 2 of the product assessment focuses on graphics to explore the spatial patterns of raster times series as figures.
5
#The file contains functions to genrate figures and animation (movie).
4
#This script checks the number of predictions by tiles and years.
5
#with the goal of predicting potential gaps or missing predictions in fugure mosaics by region.
6
#The general logic is to check the number of overlap by shapefile polyon tiles
7
#along with the predicitons for every day of the year (*.tif)
8
#Summary tables and data are also produced in the script.
9
#
6 10
#AUTHOR: Benoit Parmentier 
7
#CREATED ON: 10/03/2016  
8
#MODIFIED ON: 10/22/2016            
9
#Version: 2
11
#CREATED ON: 10/31/2016  
12
#MODIFIED ON: 11/03/2016            
13
#Version: 1
10 14
#PROJECT: Environmental Layers project     
15
#COMMENTS: removing unused functions and clean up for part0 global prodduct assessment part0 
16
#TODO:#PROJECT: Environmental Layers project     
11 17
#COMMENTS:
12 18
#TODO:
13 19
#1) Add plot broken down by year and region 
......
64 70
  env[[nm]]
65 71
}
66 72

  
67
pre_process_raster_mosaic_fun <- function(i,list_param){
68
  
69
  
70
  ## Extract parameters
71
  
72
  lf <- list_param$lf
73
  python_bin <- list_param$python_bin
74
  infile_mask <- list_param$infile_mask
75
  scaling <- list_param$scaling
76
  mask_pred <- list_param$mask_pred
77
  matching <- list_param$matching
78
  NA_flag_val <- list_param$NA_flag_val
79
  out_suffix <- list_param$out_suffix
80
  out_dir <- list_param$out_dir
81
  
82
  raster_name_in <- lf[i]
83
  
84
  #Step 1: match extent and resolution
85
  if(matching==TRUE){
86
    lf_files <- c(raster_name_in) #match to mask
87
    rast_ref <- infile_mask
88
    ##Maching resolution is probably only necessary for the r mosaic function
89
    #Modify later to take into account option R or python...
90
    list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir)
91
    names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str")
92
    r_pred_matched <- raster_match(1,list_param_raster_match)
93
    raster_name_in <- c(r_pred_matched)
94
  }
95

  
96
  #Step 2: mask
97
  if(mask_pred==TRUE){
98
    r_mask <- raster(infile_mask)
99
    extension_str <- extension(raster_name_in)
100
    raster_name_tmp <- gsub(extension_str,"",basename(raster_name_in))
101
    raster_name <- file.path(out_dir,paste(raster_name_tmp,"_masked.tif",sep = ""))
102
    r_pred <- mask(raster(raster_name_in),r_mask,filename = raster_name,overwrite = TRUE)
103
  }
104
  
105
  NAvalue(r_pred) <- NA_flag_val
106
  #r_pred <- setMinMax(r_pred)
107 73

  
108
  #Step 3: remove scaling factor
109
  raster_name_in <- filename(r_pred)
110
  extension_str <- extension(raster_name_in)
111
  raster_name_tmp <- gsub(extension_str,"",basename(filename(r_pred)))
112
  raster_name_out <- file.path(out_dir,paste(raster_name_tmp,"_rescaled.tif",sep = ""))
113
  #r_pred <- overlay(r_pred, fun=function(x){x*1/scaling},filename=raster_name,overwrite=T)
114

  
115
  #raster_name_in <- "comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990102_reg4_1999_m_gam_CAI_dailyTmax_19990102_reg4_1999_m__meeting_NASA_reg4_04292016_masked.tif"
116
  python_cmd <- file.path(python_bin,"gdal_calc.py")
117
  cmd_str3 <- paste(python_cmd, 
118
                     paste("-A ", raster_name_in,sep=""),
119
                     paste("--outfile=",raster_name_out,sep=""),
120
                     #paste("--type=","Int32",sep=""),
121
                     "--co='COMPRESS=LZW'",
122
                     paste("--NoDataValue=",NA_flag_val,sep=""),
123
                     paste("--calc='(A)*",scaling,"'",sep=""),
124
                     "--overwrite",sep=" ") #division by zero is problematic...
125
  #system(cmd_str3)
126
  system(cmd_str3)
127
  #NAvalue(r_pred) <- NA_flag_val
128
  #r_pred <- 
129
  r_pred <- setMinMax(raster(raster_name_out))
130
  
131
  return(raster_name_out)
132
}
133

  
134
plot_raster_mosaic <- function(i,list_param){
135
  #Function to plot raster image
136
  #
137
  #INPUTS
138
  #1) l_dates
139
  #2) r_stack
140
  #3) NA_flag_val
141
  #4) out_dir,
142
  #5) out_suffix_str
143
  #6) region_name
144
  #7) variable_name
145
  #8) zlim_val
146
  #
147
  
148
  ############# Start script #########
149
  
150
  l_dates <- list_param$l_dates
151
  r_mosaiced_scaled <- list_param$r_mosaiced_scaled
152
  NA_flag_val <- list_param$NA_flag_val
153
  out_dir <- list_param$out_dir
154
  out_suffix <- list_param$out_suffix
155
  region_name <- list_param$region_name
156
  variable_name <- list_param$variable_name
157
  zlim_val <- list_param$zlim_val
158

  
159
  #for (i in 1:length(nlayers(r_mosaic_scaled))){
160
  
161
  date_proc <- l_dates[i]
162
  r_pred <- subset(r_mosaiced_scaled,i)
163
  NAvalue(r_pred)<- NA_flag_val 
164
 
165
  raster_name <- filename(r_pred)
166
  extension_str <- extension(raster_name)
167
  raster_name_tmp <- gsub(extension_str,"",basename(raster_name))
168
  
169
  date_proc <- l_dates[i]
170
  if(class(date_proc)!="Date"){
171
    date_val <- as.Date(strptime(date_proc,"%Y%m%d"))
172
    #month_name <- month.name(date_val)
173
  }else{
174
    date_val <- date_proc
175
  }
176
  
177
  month_str <- format(date_val, "%b") ## Month, char, abbreviated
178
  year_str <- format(date_val, "%Y") ## Year with century
179
  day_str <- as.numeric(format(date_val, "%d")) ## numeric month
180
  date_str <- paste(month_str," ",day_str,", ",year_str,sep="")
181
  
182
  res_pix <- 1200
183
  #res_pix <- 480
184
  col_mfrow <- 1
185
  row_mfrow <- 1
186
  
187
  
188
  if(is.null(zlim_val)){
189
    
190
    if(is.na(minValue(r_pred))){
191
      r_pred <- setMinMax(r_pred)
192
    }
193
    
194
    zlim_val_str <- paste(c(minValue(r_pred),maxValue(r_pred)),sep="_",collapse="_")
195
    #png_filename <-  file.path(out_dir,paste("Figure4_clim_mosaics_day_","_",date_proc,"_",region_name,"_zlim_",zlim_val_str,"_",out_suffix,".png",sep =""))
196
    #raster_name_tmp
197
    png_filename <-  file.path(out_dir,paste("Figure_",raster_name_tmp,"_zlim_",zlim_val_str,"_",out_suffix,".png",sep =""))
198
    
199
    title_str <-  paste("Predicted ",variable_name, " on ",date_str , " ", sep = "")
200
    #browser()
201
    
202
    png(filename=png_filename,width = col_mfrow * res_pix,height = row_mfrow * res_pix)
203

  
204
    plot(r_pred,main =title_str,cex.main =1.5,col=matlab.like(255),
205
       legend.shrink=0.8,legend.width=0.8)
206
       #axis.args = list(cex.axis = 1.6), #control size of legend z
207
       #legend.args=list(text='dNBR', side=4, line=2.5, cex=2.2))
208
       #legend.args=list(text='dNBR', side=4, line=2.49, cex=1.6))
209
    dev.off()
210
  }else{
211
    
212
    zlim_val_str <- paste(zlim_val,sep="_",collapse="_")
213
    #png_filename <-  file.path(out_dir,paste("Figure_mosaics_day_","_",date_proc,"_",region_name,"_",zlim_val_str,"_",out_suffix,".png",sep =""))
214
    
215
    png_filename <-  file.path(out_dir,paste("Figure_",raster_name_tmp,"_zlim_",zlim_val_str,"_",out_suffix,".png",sep =""))
216

  
217
    title_str <-  paste("Predicted ",variable_name, " on ",date_str , " ", sep = "")
218
    
219
    png(filename=png_filename,width = col_mfrow * res_pix,height = row_mfrow * res_pix)
220
    
221
    plot(r_pred,main =title_str,cex.main =1.5,col=matlab.like(255),zlim=zlim_val,
222
       legend.shrink=0.8,legend.width=0.8)
223
       #axis.args = list(cex.axis = 1.6), #control size of legend z
224
       #legend.args=list(text='dNBR', side=4, line=2.5, cex=2.2))
225
       #legend.args=list(text='dNBR', side=4, line=2.49, cex=1.6))
226
    dev.off()
227
  }
228
  
229
  return(png_filename)
230
}
231

  
232
extract_date <- function(i,x,item_no=NULL){
233
  y <- unlist(strsplit(x[[i]],"_"))
234
  if(is.null(item_no)){
235
    date_str <- y[length(y)-2] #count from end
236
  }else{
237
    date_str <- y[item_no]
238
  }
239
  return(date_str)
240
}
241

  
242
finding_missing_dates <- function(date_start,date_end,list_dates){
243
  #this assumes daily time steps!!
244
  #can be improved later on
245
  
246
  #date_start <- "19840101"
247
  #date_end <- "19991231"
248
  date1 <- as.Date(strptime(date_start,"%Y%m%d"))
249
  date2 <- as.Date(strptime(date_end,"%Y%m%d"))
250
  dates_range <- seq.Date(date1, date2, by="1 day") #sequence of dates
251

  
252
  missing_dates <- setdiff(as.character(dates_range),as.character(list_dates))
253
  #df_dates_missing <- data.frame(date=missing_dates)
254
  #which(df_dates$date%in%missing_dates)
255
  #df_dates_missing$missing <- 1
256
  
257
  df_dates <- data.frame(date=as.character(dates_range),missing = 0) 
258

  
259
  df_dates$missing[df_dates$date %in% missing_dates] <- 1
260
  #a$flag[a$id %in% temp] <- 1
261

  
262
  missing_dates_obj <- list(missing_dates,df_dates)
263
  names(missing_dates_obj) <- c("missing_dates","df_dates")
264
  return(missing_dates_obj)
265
}
266 74

  
267 75
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="."){
268 76
  #Function to check for missing files such as mosaics or predictions for tiles etc.
......
329 137
  return(df_time_series_obj)
330 138
}
331 139

  
140

  
332 141
############################ END OF SCRIPT ##################################

Also available in: Unified diff