Project

General

Profile

« Previous | Next » 

Revision 22ff01d6

Added by Benoit Parmentier almost 11 years ago

assessing scaling up, script showing how to collect monthly stations information from North America tiles

View differences:

climate/research/oregon/interpolation/collecting_monthly_data_over_tiles.R
1
####################################  INTERPOLATION OF TEMPERATURES  #######################################
2
############################  Script for assessment of scaling up on NEX ##############################
3
#This script uses the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA.
4
#It shows how to load the monthly data at station locations.
5

  
6
### Loading R library and packages        
7
#library used in the workflow production:
8
library(gtools)                              # loading some useful tools 
9
library(mgcv)                                # GAM package by Simon Wood
10
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
11
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
12
library(rgdal)                               # GDAL wrapper for R, spatial utilities
13
library(gstat)                               # Kriging and co-kriging by Pebesma et al.
14
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
15
library(raster)                              # Hijmans et al. package for raster processing
16
library(gdata)                               # various tools with xls reading, cbindX
17
library(rasterVis)                           # Raster plotting functions
18
library(parallel)                            # Parallelization of processes with multiple cores
19
library(maptools)                            # Tools and functions for sp and other spatial objects e.g. spCbind
20
library(maps)                                # Tools and data for spatial/geographic objects
21
library(reshape)                             # Change shape of object, summarize results 
22
library(plotrix)                             # Additional plotting functions
23
library(plyr)                                # Various tools including rbind.fill
24
library(spgwr)                               # GWR method
25
library(automap)                             # Kriging automatic fitting of variogram using gstat
26
library(rgeos)                               # Geometric, topologic library of functions
27
#RPostgreSQL                                 # Interface R and Postgres, not used in this script
28
library(gridExtra)
29
#Additional libraries not used in workflow
30
library(pgirmess)                            # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess}  
31
library(colorRamps)
32

  
33
#### FUNCTION USED IN SCRIPT
34

  
35
load_obj <- function(f)
36
{
37
  env <- new.env()
38
  nm <- load(f, env)[1]
39
  env[[nm]]
40
}
41

  
42
extract_list_from_list_obj<-function(obj_list,list_name){
43
  #Create a list of an object from a given list of object using a name prodived as input
44
  
45
  list_tmp<-vector("list",length(obj_list))
46
  for (i in 1:length(obj_list)){
47
    tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame
48
    list_tmp[[i]]<-tmp
49
  }
50
  return(list_tmp) #this is  a data.frame
51
}
52

  
53
#This extract a data.frame object from raster prediction obj and combine them in one data.frame 
54
extract_from_list_obj<-function(obj_list,list_name){
55
  #extract object from list of list. This useful for raster_prediction_obj
56
  library(plyr)
57
  
58
  list_tmp<-vector("list",length(obj_list))
59
  for (i in 1:length(obj_list)){
60
    tmp<-obj_list[[i]][[list_name]] #double bracket to return data.frame
61
    list_tmp[[i]]<- as.data.frame(tmp) #if spdf
62
  }
63
  tb_list_tmp<-do.call(rbind.fill,list_tmp) #long rownames
64
  #tb_list_tmp<-do.call(rbind,list_tmp) #long rownames
65
  
66
  return(tb_list_tmp) #this is  a data.frame
67
}
68

  
69

  
70
##############################
71
#### Parameters and constants  
72

  
73
#in_dir1 <- "/data/project/layers/commons/NEX_data/test_run1_03232014/output" #On Atlas
74
in_dir1 <- "/nobackupp4/aguzman4/climateLayers/output" #On NEX
75
in_dir_list <- list.dirs(path=in_dir1) #get the list of directories with resutls by 10x10 degree tiles
76
#in_dir_list <- as.list(in_dir_list[-1])
77
in_dir_list <- in_dir_list[grep("output",basename(in_dir_list),invert=TRUE)] #the first one is the in_dir1
78
in_dir_list <- in_dir_list[grep("shapefiles",basename(in_dir_list),invert=TRUE)] 
79
#the first one is the in_dir1
80
# the last directory contains shapefiles 
81

  
82
##raster_prediction object : contains testing and training stations with RMSE and model object
83

  
84
list_raster_obj_files <- lapply(in_dir_list,FUN=function(x){list.files(path=x,pattern="^raster_prediction_obj.*.RData",full.names=T)})
85

  
86
names(list_raster_obj_files)<- paste("tile",1:length(list_raster_obj_files),sep="_")
87
                                                                        
88
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
89
                                     
90
###################### PART I: Generate tables to collect information over all tiles in North America ##########
91

  
92
##Quick exploration of raster object
93

  
94
robj1 <- load_obj(list_raster_obj_files[[12]]) #load raster object
95
names(robj1)  #list the content of raster object, it is a R list
96
names(robj1$clim_method_mod_obj[[1]]$data_month) # monthly data for January
97
#names(robj1$validation_mod_month_obj[[1]]$data_s) # monthly for January with predictions
98
robj1$tb_diagnostic_v[1:10,] #first 10 rows of accuarcy metrics per day and model (for specific tile)
99
robj1$summary_metrics_v # accuracy averages per model (for specific tile)
100

  
101
#load data_month for specific tiles
102
data_month <- extract_from_list_obj(robj1$clim_method_mod_obj,"data_month")
103

  
104
names(data_month) #this contains LST means (mm_1, mm_2 etc.) as well as TMax and other info
105

  
106
#problem with tile 12...the raster ojbect has missing sub object
107
#data_month_list <- lapply(1:length(list_raster_obj_files),x=list_raster_obj_files,
108
#                          FUN=function(i,x){x<-load_obj(x[[i]]);
109
#                                            extract_from_list_obj(x$validation_mod_month_obj,"data_s")})                           
110

  
111
data_month_list <- lapply(1:length(list_raster_obj_files),x=list_raster_obj_files,
112
                          FUN=function(i,x){x<-load_obj(x[[i]]);
113
                                            extract_from_list_obj(x$clim_method_mod_obj,"data_month")})                           
114

  
115
names(data_month_list) <- paste("tile","_",1:length(data_month_list),sep="")
116

  
117

  
118
#names(data_month_list) <- basename(in_dir_list) #use folder id instead
119

  
120
tile_id <- lapply(1:length(data_month_list),
121
                  FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=data_month_list)
122
data_month_NAM <- do.call(rbind.fill,data_month_list) #combined data_month for "NAM" North America
123
data_month_NAM$tile_id <- unlist(tile_id)
124

  
125
#plot(mm_01 ~ elev_s,data=data_month_NAM) #Jan across all tiles
126
#plot(mm_06 ~ elev_s,data=data_month_NAM) #June across all tiles
127
#plot(TMax ~ mm_01,data=data_month_NAM) #monthly tmax averages and LST across all tiles
128

  
129
###### Additional information ######
130

  
131
# #LAND COVER INFORMATION (Tunamu et al., Jetz lab)
132

  
133
# LC1: Evergreen/deciduous needleleaf trees
134
# LC2: Evergreen broadleaf trees
135
# LC3: Deciduous broadleaf trees
136
# LC4: Mixed/other trees
137
# LC5: Shrubs
138
# LC6: Herbaceous vegetation
139
# LC7: Cultivated and managed vegetation
140
# LC8: Regularly flooded shrub/herbaceous vegetation
141
# LC9: Urban/built-up
142
# LC10: Snow/ice
143
# LC11: Barren lands/sparse vegetation
144
# LC12: Open water
145

  
146
## LST information: mm_01, mm_02 ...to mm_12 are monthly mean LST at station locaitons
147
## LST information: nobs_01, nobs_02 ... to nobs_12 number of valid obs used in mean LST averages
148
## TMax : monthly mean tmax at meteorological stations
149
## nbs_stt: number of stations used in the monthly mean tmax at stations
150
###
151

  

Also available in: Unified diff