Revision ed01044e
Added by Benoit Parmentier over 8 years ago
climate/research/oregon/interpolation/NASA2016_conference_temperature_predictions.R | ||
---|---|---|
153 | 153 |
|
154 | 154 |
#run_figure_by_year <- TRUE # param 10, arg 7 |
155 | 155 |
list_year_predicted <- "1984,2014" |
156 |
scaling <- 100 |
|
156 |
scaling <- 0.01 #was scaled on 100 |
|
157 |
|
|
158 |
df_centroids_fname <- "/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/output_reg4_1999/df_centroids_19990701_reg4_1999.txt" |
|
159 |
|
|
160 |
raster_name_lf <- c("/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990101_reg4_1999_m_gam_CAI_dailyTmax_19990101_reg4_1999.tif", |
|
161 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990102_reg4_1999_m_gam_CAI_dailyTmax_19990102_reg4_1999.tif", |
|
162 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990103_reg4_1999_m_gam_CAI_dailyTmax_19990103_reg4_1999.tif", |
|
163 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990701_reg4_1999_m_gam_CAI_dailyTmax_19990701_reg4_1999.tif", |
|
164 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990702_reg4_1999_m_gam_CAI_dailyTmax_19990702_reg4_1999.tif", |
|
165 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990703_reg4_1999_m_gam_CAI_dailyTmax_19990703_reg4_1999.tif") |
|
166 |
|
|
167 |
l_dates <- c("19990101","19990102","19990103","19990701","19990702","19990703") |
|
157 | 168 |
|
158 | 169 |
##################### START SCRIPT ################# |
159 | 170 |
|
... | ... | |
173 | 184 |
|
174 | 185 |
#start_date <- day_to_mosaic_range[1] |
175 | 186 |
#end_date <- day_to_mosaic_range[2] |
176 |
start_date <- day_start #PARAM 12 arg 12 |
|
177 |
end_date <- day_end #PARAM 13 arg 13 |
|
187 |
#start_date <- day_start #PARAM 12 arg 12
|
|
188 |
#end_date <- day_end #PARAM 13 arg 13
|
|
178 | 189 |
|
179 |
date_to_plot <- seq(as.Date(strptime(start_date,"%Y%m%d")), as.Date(strptime(end_date,"%Y%m%d")), 'day') |
|
180 |
l_dates <- format(date_to_plot,"%Y%m%d") #format back to the relevant date format for files |
|
190 |
#date_to_plot <- seq(as.Date(strptime(start_date,"%Y%m%d")), as.Date(strptime(end_date,"%Y%m%d")), 'day')
|
|
191 |
#l_dates <- format(date_to_plot,"%Y%m%d") #format back to the relevant date format for files
|
|
181 | 192 |
|
182 |
raster_name_lf <- c("/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990101_reg4_1999_m_gam_CAI_dailyTmax_19990101_reg4_1999.tif", |
|
183 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990102_reg4_1999_m_gam_CAI_dailyTmax_19990102_reg4_1999.tif", |
|
184 |
"/data/project/layers/commons/NEX_data/climateLayers/out/reg4/mosaic/int_mosaics/comp_r_m_use_edge_weights_weighted_mean_gam_CAI_dailyTmax_19990103_reg4_1999_m_gam_CAI_dailyTmax_19990103_reg4_1999.tif") |
|
185 | 193 |
|
186 |
pred_temp_s <- raster(raster_name_lf[1]) |
|
187 | 194 |
|
188 |
lf_files <- c(raster_name_in) #match to mask |
|
189 |
rast_ref <- infile_mask |
|
190 |
##Maching resolution is probably only necessary for the r mosaic function |
|
191 |
#Modify later to take into account option R or python... |
|
192 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir) |
|
193 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
|
194 |
r_pred_matched <- raster_match(1,list_param_raster_match) |
|
195 |
raster_name_in <- c(r_pred_matched) |
|
195 |
mask_pred <- TRUE |
|
196 |
list_param_pre_process <- list(raster_name_lf,python_bin,infile_mask,scaling,mask_pred,NA_flag_val,out_suffix,out_dir) |
|
197 |
names(list_param_pre_process) <- c("lf","python_bin","infile_mask","scaling","mask_pred","NA_flag_val","out_suffix","out_dir") |
|
198 |
|
|
199 |
#debug(pre_process_raster_mosaic_fun) |
|
200 |
|
|
201 |
#lf_mosaic_scaled <- mclapply(1:length(raster_name_lf),FUN=pre_process_raster_mosaic_fun,list_param=list_param_pre_process,mc.preschedule=FALSE,mc.cores = num_cores) |
|
202 |
lf_mosaic_scaled <- mclapply(1:length(raster_name_lf),FUN=pre_process_raster_mosaic_fun,list_param=list_param_pre_process,mc.preschedule=FALSE,mc.cores = num_cores) |
|
203 |
|
|
204 |
test <- pre_process_raster_mosaic_fun(2,list_param_pre_process) |
|
196 | 205 |
|
197 |
if(mask_pred==TRUE){ |
|
206 |
pre_process_raster_mosaic_fun <- function(i,list_param){ |
|
207 |
|
|
208 |
|
|
209 |
## Extract parameters |
|
198 | 210 |
|
199 |
r_mask <- raster(infile_mask) |
|
211 |
lf <- list_param$lf |
|
212 |
python_bin <- list_param$python_bin |
|
213 |
infile_mask <- list_param$infile_mask |
|
214 |
scaling <- list_param$scaling |
|
215 |
mask_pred <- list_param$mask_pred |
|
216 |
NA_flag_val <- list_param$NA_flag_val |
|
217 |
out_suffix <- list_param$out_suffix |
|
218 |
out_dir <- list_param$out_dir |
|
219 |
|
|
220 |
raster_name_in <- lf[i] |
|
221 |
|
|
222 |
#Step 1: match extent and resolution |
|
223 |
|
|
224 |
lf_files <- c(raster_name_in) #match to mask |
|
225 |
rast_ref <- infile_mask |
|
226 |
##Maching resolution is probably only necessary for the r mosaic function |
|
227 |
#Modify later to take into account option R or python... |
|
228 |
list_param_raster_match <- list(lf_files,rast_ref,file_format,python_bin,out_suffix,out_dir) |
|
229 |
names(list_param_raster_match) <- c("lf_files","rast_ref","file_format","python_bin","out_suffix","out_dir_str") |
|
230 |
r_pred_matched <- raster_match(1,list_param_raster_match) |
|
231 |
raster_name_in <- c(r_pred_matched) |
|
232 |
|
|
233 |
#Step 2: mask |
|
234 |
if(mask_pred==TRUE){ |
|
235 |
r_mask <- raster(infile_mask) |
|
236 |
extension_str <- extension(raster_name_in) |
|
237 |
raster_name_tmp <- gsub(extension_str,"",basename(raster_name_in)) |
|
238 |
raster_name <- file.path(out_dir,paste(raster_name_tmp,"_masked.tif",sep = "")) |
|
239 |
r_pred <- mask(raster(raster_name_in),r_mask,filename = raster_name,overwrite = TRUE) |
|
240 |
} |
|
241 |
|
|
242 |
NAvalue(r_pred) <- NA_flag_val |
|
243 |
#r_pred <- setMinMax(r_pred) |
|
244 |
|
|
245 |
#Step 3: remove scaling factor |
|
246 |
raster_name_in <- filename(r_pred) |
|
200 | 247 |
extension_str <- extension(raster_name_in) |
201 |
raster_name_tmp <- gsub(extension_str,"",basename(raster_name_in)) |
|
202 |
raster_name <- file.path(out_dir,paste(raster_name_tmp,"_masked.tif",sep = "")) |
|
203 |
r_pred <- mask(raster(raster_name_in),r_mask,filename = raster_name,overwrite = TRUE) |
|
248 |
raster_name_tmp <- gsub(extension_str,"",basename(filename(r_pred))) |
|
249 |
raster_name_out <- file.path(out_dir,paste(raster_name_tmp,"_rescaled.tif",sep = "")) |
|
250 |
#r_pred <- overlay(r_pred, fun=function(x){x*1/scaling},filename=raster_name,overwrite=T) |
|
251 |
|
|
252 |
#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" |
|
253 |
python_cmd <- file.path(python_bin,"gdal_calc.py") |
|
254 |
cmd_str3 <- paste(python_cmd, |
|
255 |
paste("-A ", raster_name_in,sep=""), |
|
256 |
paste("--outfile=",raster_name_out,sep=""), |
|
257 |
#paste("--type=","Int32",sep=""), |
|
258 |
"--co='COMPRESS=LZW'", |
|
259 |
paste("--NoDataValue=",NA_flag_val,sep=""), |
|
260 |
paste("--calc='(A)*",scaling,"'",sep=""), |
|
261 |
"--overwrite",sep=" ") #division by zero is problematic... |
|
262 |
#system(cmd_str3) |
|
263 |
system(cmd_str3) |
|
264 |
#NAvalue(r_pred) <- NA_flag_val |
|
265 |
#r_pred <- |
|
266 |
r_pred <- setMinMax(raster(raster_name_out)) |
|
267 |
|
|
268 |
return(raster_name_out) |
|
204 | 269 |
} |
205 | 270 |
|
206 |
extension_str <- extension(filename(r_pred)) |
|
207 |
raster_name_tmp <- gsub(extension_str,"",basename(filename(r_pred))) |
|
208 |
raster_name <- file.path(out_dir,paste(raster_name_tmp,"_rescaled.tif",sep = "")) |
|
209 |
|
|
210 |
r_pred <- overlay(r_pred, fun=function(x){x*1/scaling},filename=raster_name) |
|
211 |
NAvalue(r_pred) <- NA_flag_val |
|
212 |
r_pred <- setMinMax(r_pred) |
|
213 |
|
|
214 | 271 |
month_name <- month.name() |
215 | 272 |
l_dates <- as.Date(strptime(date_proc,"%Y%m%d")) |
216 | 273 |
|
... | ... | |
234 | 291 |
#paste(raster_name[1:7],collapse="_") |
235 | 292 |
#add filename option later |
236 | 293 |
|
294 |
|
|
295 |
|
|
237 | 296 |
res_pix <- 1200 |
238 | 297 |
#res_pix <- 480 |
239 | 298 |
|
... | ... | |
251 | 310 |
#col.regions=temp.colors(25)) |
252 | 311 |
#dev.off() |
253 | 312 |
|
313 |
#### Extract time series |
|
314 |
|
|
315 |
#-65,-22 |
|
254 | 316 |
|
317 |
df_centroids <- read.table(df_centroids_fname,sep=",") |
|
255 | 318 |
|
319 |
raster() |
|
256 | 320 |
############################ END OF SCRIPT ################################## |
Also available in: Unified diff
more changes to screen data for figures of mosaic