Project

General

Profile

« Previous | Next » 

Revision e1df6e22

Added by Adam Wilson almost 12 years ago

Updated summary script to work with netcdf output files

View differences:

climate/procedures/GHCND_stations_extraction_study_area.R
1
##################    Data preparation for interpolation   #######################################
2
############################ Extraction of station data ##########################################
3
#This script perform queries on the Postgres database ghcn for stations matching the             #
4
#interpolation area. It requires the following inputs:                                           #
5
# 1)the text file ofGHCND  stations from NCDC matching the database version release              #
6
# 2)a shape file of the study area with geographic coordinates: lonlat WGS84                     #                                                     #       
7
# 3)a new coordinate system can be provided as an argument                                       #
8
# 4)the variable of interest: "TMAX","TMIN" or "PRCP"                                            #
9
#                                                                                                #
10
#The outputs are text files and a shape file of a time subset of the database                    #
11
#AUTHOR: Benoit Parmentier                                                                       #
12
#DATE: 06/02/212                                                                                 #
13
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--                                  #
14
##################################################################################################
15

  
16
###Loading R library and packages   
17
library(RPostgreSQL)
18
library(sp)                                             # Spatial pacakge with class definition by Bivand et al.
19
library(spdep)                                          # Spatial pacakge with methods and spatial stat. by Bivand et al.
20
library(rgdal)                                          # GDAL wrapper for R, spatial utilities
21
library(rgeos)                                          # Polygon buffering and other vector operations
22
library(reshape)
23
library(raster)
24

  
25
### Parameters and arguments
26

  
27
db.name <- "ghcn"                #name of the Postgres database
28
var <- "PRCP"                    #name of the variables to keep: TMIN, TMAX or PRCP
29
year_start<-"1970"               #starting year for the query (included)
30
year_end<-"2011"                 #end year for the query (excluded)
31
buffer=500                      #  size of buffer around shapefile to include in spatial query (in km), set to 0 to use no buffer
32
infile2<-"ghcnd-stations.txt"                             #This is the textfile of station locations from GHCND
33
new_proj<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
34
#tile="h11v08" 
35
tile="h09v04"
36

  
37
#out_prefix<-"20121212"                                                 #User defined output prefix
38

  
39
#for Adam
40
outpath="/home/wilson/data"
41
path="/home/wilson/data"
42
setwd(path) 
43

  
44

  
45
############ START OF THE SCRIPT #################
46

  
47
#####  Connect to Station database
48
drv <- dbDriver("PostgreSQL")
49
db <- dbConnect(drv, dbname=db.name)#,options="statement_timeout = 1m")
50

  
51
##### STEP 1: Select station in the study area
52
## get MODLAND tile information
53
#tb=read.table("http://landweb.nascom.nasa.gov/developers/sn_tiles/sn_bound_10deg.txt",skip=6,nrows=648,header=T)
54
#tb$tile=paste("h",sprintf("%02d",tb$ih),"v",sprintf("%02d",tb$iv),sep="")
55
#tile_bb=tb[tb$tile==tile,] ## identify tile of interest
56
#roi_ll=extent(tile_bb$lon_min,tile_bb$lon_max,tile_bb$lat_min,tile_bb$lat_max)
57

  
58
modgrid=readOGR("/home/wilson/data/modisgrid","modis_sinusoidal_grid_world")
59
roi=modgrid[modgrid$h==as.numeric(substr(tile,2,3))&modgrid$v==as.numeric(substr(tile,5,6)),]
60
CRS_interp="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
61
interp_area=spTransform(roi,CRS(CRS_interp))
62

  
63
#####  Buffer shapefile if desired
64
##     This is done to include stations from outside the region in the interpolation fitting process and reduce edge effects when stiching regions
65
if(buffer>0){  #only apply buffer if buffer >0
66
  interp_area=gUnionCascaded(interp_area)  #dissolve any subparts of roi (if there are islands, lakes, etc.)
67
  interp_areaC=gCentroid(interp_area)       # get centroid of region
68
  interp_areaB=spTransform(                # buffer roi (transform to azimuthal equidistant with centroid of region for most (?) accurate buffering, add buffer, then transform to WGS84)
69
    gBuffer(
70
      spTransform(interp_area,
71
                  CRS(paste("+proj=aeqd +lat_0=",interp_areaC@coords[2]," +lon_0=",interp_areaC@coords[1]," +ellps=WGS84 +datum=WGS84 +units=m +no_defs ",sep=""))),
72
      width=buffer*1000),                  # convert buffer (km) to meters
73
    CRS(CRS_interp))                       # reproject back to original projection
74
  interp_area=interp_areaB                 # replace original region with buffered region
75
}
76

  
77
## get bounding box of study area
78
bbox=bbox(interp_area)
79

  
80
### read in station location information from database
81
### use the bbox of the region to include only station in rectangular region to speed up overlay
82
dat_stat=dbGetQuery(db, paste("SELECT id,name,latitude,longitude
83
                  FROM stations
84
                  WHERE latitude>=",bbox[2,1]," AND latitude<=",bbox[2,2],"
85
                  AND longitude>=",bbox[1,1]," AND longitude<=",bbox[1,2],"
86
                  ;",sep=""))
87
coordinates(dat_stat)<-c("longitude","latitude")
88
proj4string(dat_stat)<-CRS_interp
89

  
90

  
91
# Spatial query to find relevant stations
92
inside <- !is.na(over(dat_stat, as(interp_area, "SpatialPolygons")))  #Finding stations contained in the current interpolation area
93
stat_roi<-dat_stat[inside,]                                            #Finding stations contained in the current interpolation area
94
stat_roi<-spTransform(stat_roi,CRS(new_proj))         # Project from WGS84 to new coord. system
95

  
96
#Quick visualization of station locations
97
#plot(interp_area, axes =TRUE)
98
#plot(stat_roi, pch=1, col="red", cex= 0.7, add=TRUE)
99
#legend("topleft", pch=1,col="red",bty="n",title= "Stations",cex=1.6)
100

  
101
#### STEP 2: Connecting to the database and query for relevant data 
102

  
103
##  Query to link station location information and observations
104
##  Concatenate date columns into single field for easy convert to date
105
##  Divide value by 10 to convert to degrees C and mm
106
##  Subset to years in year_start -> year_end
107
##  Drop missing values (-9999)
108
##  Drop observations that failed quality control (keep only qflag==NA)
109
system.time(
110
  dd<<-dbGetQuery(db,  #save object dd (data daily)
111
                          paste("SELECT station,year||'-'||month||'-'||day AS date,value / 10.0 as value,latitude,longitude,elevation
112
                                 FROM ghcn, stations
113
                                 WHERE station = id
114
                                 AND id IN ('",paste(stat_roi$id,collapse="','"),"')
115
                                 AND element='",var,"'
116
                                 AND year>=",year_start,"
117
                                 AND year<",year_end,"
118
                                 AND value<>-9999
119
                                 AND qflag IS NULL
120
                                 ;",sep=""))
121
  )  ### print used time in seconds  - only taking ~30 seconds....
122

  
123
dd=dd[!is.na(dd$value),]  #drop any NA values
124

  
125
#################################################################
126
### STEP 3: generate monthly means for climate-aided interpolation
127

  
128
### first extract average daily values by year.  
129
system.time(
130
           dm<<-dbGetQuery(db,  # create dm object (data monthly)
131
                          paste("SELECT station,month,count(value) as count,avg(value)/10.0 as value,latitude,longitude,elevation
132
                                 FROM ghcn, stations
133
                                 WHERE station = id
134
                                 AND id IN ('",paste(stat_roi$id,collapse="','"),"')
135
                                 AND element='",var,"'
136
                                 AND year>=",year_start,"
137
                                 AND year<",year_end,"
138
                                 AND value<>-9999
139
                                 AND qflag IS NULL
140
                                 GROUP BY station, month,latitude,longitude,elevation
141
                                 ;",sep=""))
142
            )  ### print used time in seconds  - only taking ~30 seconds....
143

  
144
### drop months with fewer than 75% of the data observations
145
### is 75% the right number?
146
#dm=dm[dm$count>=(.75*10*30),]
147

  
148
## add the monthly data to the daily table
149
dd$month=as.numeric(as.character(format(as.Date(dd$date),"%m")))
150
dd$avgvalue=dm$value[match(paste(dd$station,dd$month),paste(dm$station,dm$month))]
151
dd$avgcount=dm$count[match(paste(dd$station,dd$month),paste(dm$station,dm$month))]
152

  
153
### Generate log-transformed ratio of daily:monthly ppt and add it to daily data
154
if(var=="PRCP"){
155
  dd$anom=log((1+dd$value)/(1+dd$avgvalue))
156
  dd$anom[dd$anom==Inf|dd$anom2==-Inf]=NA
157
}
158

  
159
### Generate temperature anomolies for each daily value
160
if(var!="PRCP"){
161
  dd$anom=dd$value-dd$avgvalue
162
}
163

  
164

  
165
###  Transform the subset data frame in a spatial data frame and reproject
166
dd_sp<-SpatialPointsDataFrame (dd[,c('longitude','latitude')],data=dd,proj=CRS(CRS_interp)) 
167
dd_sp<-spTransform(dd_sp,CRS(new_proj))         # Project from WGS84 to new coord. system
168

  
169
###  Transform the subset data frame in a spatial data frame and reproject
170
dm_sp<-SpatialPointsDataFrame (dm[,c('longitude','latitude')],data=dm,proj=CRS(CRS_interp)) 
171
dm_sp<-spTransform(dm_sp,CRS(new_proj))         # Project from WGS84 to new coord. system
172

  
173
################################################################
174
### STEP 4: Save results and outuput in textfile and a shape file
175

  
176
##  Save shapefile of station locations
177
writeOGR(stat_roi,outpath,paste("station_location_",tile,"_",var,sep=""),driver ="ESRI Shapefile")
178

  
179
## save shapefile of daily observations
180
writeOGR(dd_sp,outpath,paste("/station_daily_",tile,"_",var,sep=""), driver ="ESRI Shapefile") #Note that the layer name is the file name without extension
181

  
182
### write monthly data
183
writeOGR(dm_sp, outpath, paste("/station_monthly_",tile,"_",var,sep=""), driver ="ESRI Shapefile") #Note that the layer name is the file name without extension
184

  
185
##### END OF SCRIPT ##########
climate/procedures/MOD06_L2_data_compile.r
35 35

  
36 36
### use MODIS tile as ROI instead
37 37
modt=readOGR("/home/adamw/acrobates/Global/modis_sinusoidal","modis_sinusoidal_grid_world",)
38
tiles=c("H9V4")
38
tiles=c("H9V4") #oregon
39
tiles=c("H11V8") #Venezuela
40

  
39 41
roi=modt[modt$HV%in%tiles,]
40 42

  
41 43
## Bounding box of region in lat/lon
......
51 53

  
52 54
## specify some working directories
53 55
gdir="output/"
54
datadir="data/modis/MOD06_L2_hdf"
55
outdir="data/modis/MOD06_L2_hdf2"
56
tifdir="/media/data/MOD06_L2_tif"
56
#datadir="data/modis/MOD06_L2_hdf"
57
#outdir="data/modis/MOD06_L2_hdf2"
58
#tifdir="/media/data/MOD06_L2_tif"
59
#summarydatadir="data/modis/MOD06_climatologies"
60
datadir="data/modis/Venezuela/MOD06"
61
outdir="data/modis/Venezuela/MOD06_"
62
#tifdir="/media/data/MOD06_L2_tif"
57 63
summarydatadir="data/modis/MOD06_climatologies"
58 64

  
59 65

  
climate/procedures/MOD06_summary.r
1
###################################################################################
2
###  R code to aquire and process MOD06_L2 cloud data from the MODIS platform
3

  
4

  
5
## connect to server of choice
6
#system("ssh litoria")
7
#R
8

  
9
library(sp)
10
library(spgrass6)
11
library(rgdal)
12
library(reshape)
13
library(ncdf4)
14
library(geosphere)
15
library(rgeos)
16
library(multicore)
17
library(raster)
18
library(lattice)
19
library(rgl)
20
library(hdf5)
21
library(rasterVis)
22
library(heR.Misc)
23
library(car)
24

  
25
X11.options(type="Xlib")
26
ncores=20  #number of threads to use
27

  
28
setwd("/home/adamw/acrobates/projects/interp")
29

  
30
psin=CRS("+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs")
31

  
32
## get MODLAND tile information
33
tb=read.table("http://landweb.nascom.nasa.gov/developers/sn_tiles/sn_bound_10deg.txt",skip=6,nrows=648,header=T)
34
tb$tile=paste("h",sprintf("%02d",tb$ih),"v",sprintf("%02d",tb$iv),sep="")
35
save(tb,file="modlandTiles.Rdata")
36

  
37
tile="h11v08"   #can move this to submit script if needed
38
#tile="h09v04"   #oregon
39

  
40
tile_bb=tb[tb$tile==tile,] ## identify tile of interest
41
roi_ll=extent(tile_bb$lon_min,tile_bb$lon_max,tile_bb$lat_min,tile_bb$lat_max) 
42
#roi=spTransform(roi,psin)
43
#roil=as(roi,"SpatialLines")
44

  
45
dmod06="data/modis/mod06/summary"
46

  
47

  
48
##########################
49
#### explore the data
50

  
51
months=seq(as.Date("2000-01-15"),as.Date("2000-12-15"),by="month")
52

  
53
getmod06<-function(variable){
54
  d=brick(list.files(dmod06,pattern=paste("MOD06_",tile,".nc",sep=""),full=T),varname=toupper(variable))
55
  projection(d)=psin
56
  setZ(d,format(as.Date(d@z$Date),"%m"),name="time")
57
#  d@z=list(months)
58
  layerNames(d) <- as.character(format(as.Date(d@z$Date),"%b"))
59
  return(d)
60
}
61

  
62
cer=getmod06("cer")
63
cld=getmod06("cld")
64
cot=getmod06("cot")
65

  
66
pcol=colorRampPalette(c("brown","red","yellow","darkgreen"))
67
#levelplot(cer,col.regions=pcol(20))
68

  
69
## load WorldClim data for comparison (download then uncompress)
70
#system("wget -P data/worldclim/ http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/prec_30s_bil.zip",wait=F)
71
#system("wget -P data/worldclim/ http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/alt_30s_bil.zip",wait=F)
72

  
73
### load WORLDCLIM data for comparison
74
wc=stack(list.files("data/worldclim/prec_30s_bil/",pattern="bil$",full=T)[c(4:12,1:3)])
75
projection(wc)=CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
76
wc=crop(wc,roi_ll)
77
wc[wc==55537]=NA
78
wc=projectRaster(wc,cer)#crs=projection(psin))
79
setZ(wc,months,name="time")
80
wc@z=list(months)
81
layerNames(wc) <- as.character(format(months,"%b"))
82
writeRaster(wc,file=paste("data/tiles/",tile,"/worldclim_",tile,".tif",sep=""),format="GTiff")
83

  
84
### load WORLDCLIM elevation 
85
dem=raster(list.files("data/worldclim/alt_30s_bil/",pattern="bil$",full=T))
86
projection(dem)=CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
87
dem=crop(dem,roi_ll)
88
dem[dem>60000]=NA
89
dem=projectRaster(dem,cer)
90
writeRaster(dem,file=paste("data/tiles/",tile,"/dem_",tile,".tif",sep=""),format="GTiff")
91

  
92

  
93
### get station data, subset to stations in region, and transform to sinusoidal
94
dm=readOGR(paste("data/tiles/",tile,sep=""),paste("station_monthly_",tile,"_PRCP",sep=""))
95
xyplot(latitude~longitude|month,data=dm@data)
96
dm2=spTransform(dm,CRS(projection(cer)))
97
dm2@data[,c("x","y")]=coordinates(dm2)
98

  
99
### extract MOD06 data for each station
100
stcer=extract(cer,dm2,fun=mean);colnames(stcer)=paste("cer_mean_",as.numeric(format(as.Date(cer@z$Date),"%m")),sep="")
101
#stcer20=extract(cer20,st2)#;colnames(stcer)=paste("cer_mean_",1:12,sep="")
102
stcot=extract(cot,dm2);colnames(stcot)=paste("cot_mean_",as.numeric(format(as.Date(cot@z$Date),"%m")),sep="")
103
stcld=extract(cld,dm2);colnames(stcld)=paste("cld_mean_",as.numeric(format(as.Date(cld@z$Date),"%m")),sep="")
104
stdem=extract(dem,dm2)
105
mod06=cbind.data.frame(station=dm$station,stcer,stcot,stcld)
106
mod06l=melt(mod06,id.vars=c("station"));colnames(mod06l)[grep("value",colnames(mod06l))]="mod06"
107
mod06l[,c("variable","moment","month")]=do.call(rbind,strsplit(as.character(mod06l$variable),"_"))
108
mod06l=unique(mod06l)
109
mod06l=cast(mod06l,station+moment+month~variable,value="mod06")
110
mod06l=merge(dm2@data,mod06l,by=c("station","month"))
111
mod06l=mod06l[!is.na(mod06l$cer),]
112

  
113
                                        #mod06l=melt(mod06,id.vars=c("station","longitude","latitude","elevation","month","count","value"))
114
#mod06l[,c("variable","moment","month2")]=do.call(rbind,strsplit(as.character(mod06l$variable),"_"))
115
#mod06l=as.data.frame(cast(mod06l,station+longitude+latitude+month~variable,value="value.1"))
116
#mod06l=mod06l[mod06l$month==mod06l$month2&!is.na(mod06l$value.1),]
117
mod06l=mod06l[order(mod06l$month),]
118

  
119
xyplot(value~cer|month,data=mod06l,scales=list(relation="free"),pch=16,cex=.5)
120
xyplot(value~cer|station,data=mod06l[mod06l$count>400,],pch=16,cex=.5)
121

  
122
xyplot(cot~month,groups=station,data=mod06l,type="l")
123

  
124

  
125
## explore fit of simple model
126
m=11
127
cor(mod06l[mod06l$month==m,c("value","cer","cld","cot")])
128
lm1=lm(value~latitude+longitude+elevation+cer+cld+cot,data=mod06l[mod06l$month==m,])
129
summary(lm1)
130
crPlots(lm1)
131

  
132
plot(mod06l$value[mod06l$month==m],as.vector(predict(lm1,data=mod06l[mod06l$month==m,])))
133

  
134
### draw some plots
135
gq=function(x,n=10,cut=F) {
136
  if(!cut) return(unique(quantile(x,seq(0,1,len=n+1),na.rm=T)))
137
  if(cut)  return(cut(x,unique(quantile(x,seq(0,1,len=n+1),na.rm=T))))
138
}
139

  
140
### add some additional variables
141
mod06s$month=factor(mod06s$month,labels=format(as.Date(paste("2000",1:12,"15",sep="-")),"%b"))
142
mod06s$lppt=log(mod06s$ppt)
143
mod06s$glon=cut(mod06s$lon,gq(mod06s$lon,n=5),include.lowest=T,ordered=T)#gq(mod06s$lon,n=3))
144
mod06s$glon2=cut(mod06s$lon,breaks=c(-125,-122,-115),labels=c("Coastal","Inland"),include.lowest=T,ordered=T)#gq(mod06s$lon,n=3))
145
mod06s$gelev=cut(mod06s$elev,breaks=gq(mod06s$elev,n=3),labels=c("Low","Mid","High"),include.lowest=T,ordered=T)
146
mod06s$gbin=factor(paste(mod06s$gelev,mod06s$glon2,sep="_"),levels=c("Low_Coastal","Mid_Coastal","High_Coastal","Low_Inland","Mid_Inland","High_Inland"),ordered=T)
147
mod06s$LWP_mean=(2/3)*mod06s$CER_mean*mod06s$COT_mean
148

  
149
## melt it
150
mod06sl=melt(mod06s[,!grepl("lppt",colnames(mod06s))],id.vars=c("id","lon","lat","elev","month","ppt","glon","glon2","gelev","gbin"))
151
levels(mod06sl$variable)=c("Effective Radius (um)","Very Cloudy Days (%)","Cloudy Days (%)","Optical Thickness (%)","Liquid Water Path")
152

  
153
###################################################################
154
###################################################################
155

  
156
bgyr=colorRampPalette(c("blue","green","yellow","red"))
157

  
158
X11.options(type="cairo")
159
pdf("output/MOD06_summary.pdf",width=11,height=8.5)
160

  
161
# % cloudy maps
162
title="Cloudiness (% cloudy days) "
163
at=unique(quantile(as.matrix(cld),seq(0,1,len=100),na.rm=T))
164
p=levelplot(cld,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=1.2, col='black'))
165
print(p)
166
#bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
167

  
168
# CER maps
169
title="Cloud Effective Radius (microns)"
170
at=quantile(as.matrix(cer),seq(0,1,len=100),na.rm=T)
171
p=levelplot(cer,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=1.2, col='black'))
172
print(p)
173
#bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
174

  
175
# COT maps
176
title="Cloud Optical Thickness (%)"
177
at=quantile(as.matrix(cot),seq(0,1,len=100),na.rm=T)
178
p=levelplot(cot,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=0.8, col='black'))
179
print(p)
180
#bwplot(cot,xlab.top=title,ylab="Cloud Optical Thickness (%)")
181

  
182
###########################################
183
#### compare with PRISM data
184

  
185
at=quantile(as.matrix(subset(m01,subset=1)),seq(0,1,len=100),na.rm=T)
186
p1=levelplot(subset(m01,subset=1),xlab.top="Effective Radius (um)",at=at,col.regions=bgyr(length(at)),margin=F,
187
  )+layer(sp.lines(roi_geo, lwd=1.2, col='black'))
188
at=quantile(as.matrix(subset(m01,subset=2)),seq(0,1,len=100),na.rm=T)
189
p2=levelplot(subset(m01,subset=2),xlab.top="Cloudy days (%)",at=at,col.regions=bgyr(length(at)),margin=F,
190
   )+layer(sp.lines(roi_geo, lwd=1.2, col='black'))
191
at=quantile(as.matrix(subset(m01,subset=3)),seq(0,1,len=100),na.rm=T)
192
p3=levelplot(subset(m01,subset=3),xlab.top="Optical Thickness (%)",at=at,col.regions=bgyr(length(at)),margin=F,
193
   )+layer(sp.lines(roi_geo, lwd=1.2, col='black'))
194
at=quantile(as.matrix(subset(m01,subset=4)),seq(0,1,len=100),na.rm=T)
195
p4=levelplot(subset(m01,subset=4),xlab.top="PRISM MAP",at=at,col.regions=bgyr(length(at)),margin=F,
196
    )+layer(sp.lines(roi_geo, lwd=1.2, col='black'))
197

  
198
print(p1,split=c(1,1,2,2))
199
print(p2,split=c(1,2,2,2),new=F)
200
print(p3,split=c(2,1,2,2),new=F)
201
print(p4,split=c(2,2,2,2),new=F)
202

  
203
### compare COT and PRISM
204
print(p3,split=c(1,1,2,1))
205
print(p4,split=c(2,1,2,1),new=F)
206

  
207

  
208
### sample to speed up processing
209
s=sample(1:nrow(bd),10000)
210

  
211
## melt it to ease comparisons
212
bdl=melt(bd[s,],measure.vars=c("cer","cld","cot"))
213

  
214
combineLimits(useOuterStrips(xyplot(prism~value|variable+month,data=bdl,pch=16,cex=.2,scales=list(y=list(log=T),x=list(relation="free")),
215
                                    ylab="PRISM Monthly mean precipitation (mm)",xlab="MOD06 metric",main="PRISM vs. MOD06 (mean monthly ppt)")))+
216
  layer(panel.abline(lm(y~x),col="red"))+
217
  layer(panel.text(0,2.5,paste("R2=",round(summary(lm(y~x))$r.squared,2))))
218

  
219

  
220
### Comparison at station values
221
at=quantile(as.matrix(cotm),seq(0,1,len=100),na.rm=T)
222
p=levelplot(cotm, layers=1,at=at,col.regions=bgyr(length(at)),main="Mean Annual Cloud Optical Thickness",FUN.margin=function(x) 0)+
223
  layer(sp.lines(roil, lwd=1.2, col='black'))+layer(sp.points(st2_sin, pch=16, col='black'))
224
print(p)
225

  
226
### monthly comparisons of variables
227
#mod06sl=melt(mod06s,measure.vars=c("ppt","COT_mean","CER_mean","CER_P20um"))
228
#bwplot(value~month|variable,data=mod06sl,cex=.5,pch=16,col="black",scales=list(y=list(relation="free")),layout=c(1,3))
229
#splom(mod06s[grep("CER|COT|CLD|ppt",colnames(mod06s))],cex=.2,pch=16,main="Scatterplot matrix of MOD06 products")
230

  
231
### run some regressions
232
#plot(log(ppt)~COT_mean,data=mod06s)
233
#summary(lm(log(ppt)~COT_mean*month,data=mod06s))
234

  
235
## ppt~metric with longitude bins
236
 xyplot(ppt~value|variable,groups=glon,data=mod06sl,
237
       scales=list(y=list(log=T),x=list(relation="free",log=F)),
238
       par.settings = list(superpose.symbol = list(col=bgyr(5),pch=16,cex=.5)),auto.key=list(space="top",title="Station Longitude"),
239
       main="Comparison of MOD06_L2 and Precipitation Monthly Climatologies",ylab="Mean Monthly Station Precipitation (mm)",xlab="MOD06_L2 Product",layout=c(5,1))+
240
  layer(panel.text(9,2.5,label="Coastal stations",srt=30,cex=1.3,col="blue"),columns=1)+
241
  layer(panel.text(13,.9,label="Inland stations",srt=10,cex=1.3,col="red"),columns=1)+
242
  layer(panel.abline(lm(y~x),col="red"))+
243
  layer(panel.text(0,0,paste("R2=",round(summary(lm(y~x))$r.squared,2)),pos=4,cex=.5,col="grey"))
244

  
245

  
246
## ppt~metric with longitude bins
247
#CairoPNG("output/COT.png",width=10,height=5,units="in",dpi=300,pointsize=20)
248
#png("output/COT.png",width=10,height=5,units="in",res=150)
249
#trellis.par.set("fontsize",12)
250
at=quantile(as.matrix(subset(m01,subset=3)),seq(0,1,len=100),na.rm=T)
251
p1=levelplot(subset(m01,subset=3),xlab.top="Optical Thickness (%)",at=at,col.regions=bgyr(length(at)),margin=F,
252
   )+layer(sp.lines(roi_geo, lwd=1.2, col='black'))+layer(sp.points(st2, cex=.5,col='black'))
253
at=quantile(as.matrix(subset(m01,subset=3)),seq(0,1,len=100),na.rm=T)
254

  
255
at=quantile(as.matrix(subset(m01,subset=4)),seq(0,1,len=100),na.rm=T)
256
p2=levelplot(subset(m01,subset=4),xlab.top="PRISM MAP",at=at,col.regions=bgyr(length(at)),margin=F,
257
    )+layer(sp.lines(roi_geo, lwd=1.2, col='black'))+layer(sp.points(st2, cex=.5, col='black'))
258

  
259
p3=xyplot(ppt~value,groups=glon,data=mod06sl[mod06sl$variable=="Optical Thickness (%)",],
260
       scales=list(y=list(log=T),x=list(relation="free",log=F)),
261
       par.settings = list(superpose.symbol = list(col=bgyr(5),pch=16,cex=.3)),auto.key=list(space="right",title="Station \n Longitude"),
262
ylab="Mean Monthly Station Precipitation (mm)",xlab="Cloud Optical Thickness from MOD06_L2 (%)",layout=c(1,1))+
263
  layer(panel.text(9,2.6,label="Coastal stations",srt=10,cex=1.3,col="blue"),columns=1)+
264
  layer(panel.text(13,.95,label="Inland stations",srt=10,cex=1.3,col="red"),columns=1)
265

  
266
p4=xyplot(ppt~value,groups=glon,data=mod06sl[mod06sl$variable=="Very Cloudy Days (%)",],
267
       scales=list(y=list(log=T),x=list(relation="free",log=F)),
268
       par.settings = list(superpose.symbol = list(col=bgyr(5),pch=16,cex=.3)),auto.key=list(space="right",title="Station \n Longitude"),
269
ylab="Mean Monthly Station Precipitation (mm)",xlab="Proportion days with Cloud Effective Radius >20um from MOD06_L2 (%)",layout=c(1,1))+
270
  layer(panel.text(9,2.6,label="Coastal stations",srt=10,cex=1.3,col="blue"),columns=1)+
271
  layer(panel.text(13,.95,label="Inland stations",srt=10,cex=1.3,col="red"),columns=1)
272

  
273
#save(p1,p2,p3,file="plotdata.Rdata")
274
#load("plotdata.Rdata")
275

  
276
#CairoPDF("output/MOD06_Summaryfig.pdf",width=11,height=8.5)
277
print(p3,position=c(0,0,1,.5),save.object=F)
278
#print(p4,position=c(0,0,1,.5),save.object=F)
279
print(p1,split=c(1,1,2,2),new=F)
280
print(p2,split=c(2,1,2,2),new=F)
281
#dev.off()
282
#system("convert output/MOD06_Summaryfig.pdf output/MOD06_Summaryfig.png")
283
                                        #dev.off()
284

  
285
## with elevation
286
# xyplot(ppt~value|variable,groups=gbin,data=mod06sl,
287
#       scales=list(y=list(log=T),x=list(relation="free")),
288
#       par.settings = list(superpose.symbol = list(col=c(rep("blue",3),rep("red",3)),pch=rep(c(3,4,8),2),cex=.5)),auto.key=list(space="right",title="Station Longitude"),
289
#       main="Comparison of MOD06_L2 and Precipitation Monthly Climatologies",ylab="Precipitation",xlab="MOD06_L2 Product",layout=c(3,1))+
290
#  layer(panel.text(9,2.5,label="Coastal stations",srt=30,cex=1.3,col="blue"),columns=1)+
291
#  layer(panel.text(13,.9,label="Inland stations",srt=10,cex=1.3,col="red"),columns=1)
292

  
293
## with elevation and longitude bins
294
combineLimits(useOuterStrips(xyplot(ppt~value|variable+gbin,data=mod06sl,
295
       scales=list(y=list(log=T),x=list(relation="free")),col="black",pch=16,cex=.5,type=c("p","r"),
296
       main="Comparison of MOD06_L2 and Precipitation Monthly Climatologies",ylab="Precipitation",xlab="MOD06_L2 Product")))+
297
  layer(panel.xyplot(x,y,type="r",col="red"))
298

  
299
## *** MOD06 vars vs precipitation by month, colored by longitude
300
combineLimits(useOuterStrips(xyplot(ppt~value|month+variable,groups=glon,data=mod06sl,cex=.5,pch=16,
301
                      scales=list(y=list(log=T),x=list(relation="free")),
302
                      par.settings = list(superpose.symbol = list(pch =16, col=bgyr(5),cex=1)),auto.key=list(space="top",title="Station Longitude"),
303
                      main="Comparison of MOD06_L2 and Precipitation Monthly Climatologies",ylab="Precipitation",xlab="MOD06_L2 Product")),
304
              margin.x=1,adjust.labels=F)+
305
  layer(panel.abline(lm(y~x),col="red"))+
306
  layer(panel.text(0,0,paste("R2=",round(summary(lm(y~x))$r.squared,2)),pos=4,cex=.5,col="grey"))
307

  
308

  
309
 xyplot(ppt~CLD_mean|id,data=mod06s,panel=function(x,y,group){
310
  panel.xyplot(x,y,type=c("r"),cex=.5,pch=16,col="red")
311
  panel.xyplot(x,y,type=c("p"),cex=.5,pch=16,col="black")
312
} ,scales=list(y=list(log=T)),strip=F,main="Monthly Mean Precipitation and % Cloudy by station",
313
        sub="Each panel is a station, each point is a monthly mean",
314
        ylab="Precipitation (mm, log axis)",xlab="% of Cloudy Days")+
315
  layer(panel.text(.5,.5,round(summary(lm(y~x))$r.squared,2),pos=4,cex=.75,col="grey"))
316

  
317
 xyplot(ppt~CER_mean|id,data=mod06s,panel=function(x,y,group){
318
  panel.xyplot(x,y,type=c("r"),cex=.5,pch=16,col="red")
319
  panel.xyplot(x,y,type=c("p"),cex=.5,pch=16,col="black")
320
} ,scales=list(y=list(log=T)),strip=F,main="Monthly Mean Precipitation and Cloud Effective Radius by station",sub="Each panel is a station, each point is a monthly mean",ylab="Precipitation (mm, log axis)",xlab="Mean Monthly Cloud Effective Radius (mm)")+
321
  layer(panel.text(10,.5,round(summary(lm(y~x))$r.squared,2),pos=4,cex=.75,col="grey"))
322

  
323
 xyplot(ppt~COT_mean|id,data=mod06s,panel=function(x,y,group){
324
  panel.xyplot(x,y,type=c("r"),cex=.5,pch=16,col="red")
325
  panel.xyplot(x,y,type=c("p"),cex=.5,pch=16,col="black")
326
} ,scales=list(y=list(log=T)),strip=F,main="Monthly Mean Precipitation and Cloud Optical Thickness by station",
327
        sub="Each panel is a station, each point is a monthly mean \n Number in lower right of each panel is R^2",
328
        ylab="Precipitation (mm, log axis)",xlab="Mean Monthly Cloud Optical Thickness (%)")+
329
  layer(panel.text(10,.5,round(summary(lm(y~x))$r.squared,2),pos=4,cex=.75,col="grey"))
330

  
331
 xyplot(ppt~LWP_mean|id,data=mod06s,panel=function(x,y,group){
332
  panel.xyplot(x,y,type=c("r"),cex=.5,pch=16,col="red")
333
  panel.xyplot(x,y,type=c("p"),cex=.5,pch=16,col="black")
334
} ,scales=list(y=list(log=T)),strip=F,main="Monthly Mean Precipitation and Liquid Water Path by station",
335
        sub="Each panel is a station, each point is a monthly mean \n Number in lower right of each panel is R^2",
336
        ylab="Precipitation (mm, log axis)",xlab="Mean Monthly Liquid Water Path")+
337
  layer(panel.text(10,.5,round(summary(lm(y~x))$r.squared,2),pos=4,cex=.75,col="grey"))
338

  
339
### Calculate the slope of each line
340
mod06s.sl=dapply(mod06s,list(id=mod06s$id),function(x){
341
  lm1=lm(log(x$ppt)~x$CER_mean,)
342
  data.frame(lat=x$lat[1],lon=x$lon[1],elev=x$elev[1],intcpt=coefficients(lm1)[1],cer=coefficients(lm1)[2],r2=summary(lm1)$r.squared)
343
})
344
mod06s.sl$cex=gq(mod06s.sl$r2,n=5,cut=T)
345
mod06s.sl$cer.s=gq(mod06s.sl$cer,n=5,cut=T)
346

  
347
###  and plot it on a map
348
xyplot(lat~lon,group=cer.s,data=mod06s.sl,par.settings = list(superpose.symbol = list(pch =16, col=bgyr(5),cex=1)),auto.key=list(space="right",title="Slope Coefficient"),asp=1,
349
       main="Slopes of linear regressions {log(ppt)~CloudEffectiveRadius}")+
350
  layer(sp.lines(roi_geo, lwd=1.2, col='black'))
351

  
352
### look for relationships with longitude
353
xyplot(cer~lon,group=cut(mod06s.sl$elev,gq(mod06s.sl$elev,n=5)),data=mod06s.sl,
354
       par.settings = list(superpose.symbol = list(col=bgyr(5),pch=16,cex=1)),auto.key=list(space="right",title="Station Elevation"),
355
       ylab="Slope of lm(ppt~EffectiveRadius)",xlab="Longitude",main="Precipitation~Effective Radius relationship by latitude")
356

  
357

  
358
############################################################
359
### simple regression to get spatial residuals
360
m="01"
361
mod06s2=mod06s#[mod06s$month==m,]
362

  
363
lm1=lm(log(ppt)~CER_mean*month*lon,data=mod06s2); summary(lm1)
364
mod06s2$pred=exp(predict(lm1,mod06s2))
365
mod06s2$resid=mod06s2$pred-mod06s2$ppt
366
mod06s2$residg=gq(mod06s2$resid,n=5,cut=T)
367
mod06s2$presid=mod06s2$resid/mod06s2$ppt
368

  
369
for(l in c(F,T)){
370
## all months
371
  xyplot(pred~ppt,groups=gelev,data=mod06s2,
372
       par.settings = list(superpose.symbol = list(col=bgyr(3),pch=16,cex=.75)),auto.key=list(space="right",title="Station Elevation"),
373
       scales=list(log=l),
374
       ylab="Predicted Mean Monthly Precipitation (mm)",xlab="Observed Mean Monthly Precipitation (mm)",main="Predicted vs. Observed for Simple Model",
375
       sub="Red line is y=x")+
376
  layer(panel.abline(0,1,col="red"))
377

  
378
## month by month
379
  print(xyplot(pred~ppt|month,groups=gelev,data=mod06s2,
380
       par.settings = list(superpose.symbol = list(col=bgyr(3),pch=16,cex=.75)),auto.key=list(space="right",title="Station Elevation"),
381
       scales=list(log=l),
382
       ylab="Predicted Mean Monthly Precipitation (mm)",xlab="Observed Mean Monthly Precipitation (mm)",main="Predicted vs. Observed for Simple Model",
383
       sub="Red line is y=x")+
384
  layer(panel.abline(0,1,col="red"))
385
)}
386

  
387
## residuals by month
388
xyplot(lat~lon|month,group=residg,data=mod06s2,
389
       par.settings = list(superpose.symbol = list(pch =16, col=bgyr(5),cex=.5)),
390
       auto.key=list(space="right",title="Residuals"),
391
       main="Spatial plot of monthly residuals")+
392
    layer(sp.lines(roi_geo, lwd=1.2, col='black'))
393

  
394

  
395
dev.off()
396

  
397

  
398
####################################
399
#### build table comparing various metrics
400
mods=data.frame(
401
  models=c(
402
    "log(ppt)~CER_mean",
403
    "log(ppt)~CLD_mean",
404
    "log(ppt)~COT_mean",
405
    "log(ppt)~CER_mean*month",
406
    "log(ppt)~CLD_mean*month",
407
    "log(ppt)~COT_mean*month",
408
    "log(ppt)~CER_mean*month*lon",
409
    "log(ppt)~CLD_mean*month*lon",
410
    "log(ppt)~COT_mean*month*lon",
411
    "ppt~CER_mean*month*lon",
412
    "ppt~CLD_mean*month*lon",
413
    "ppt~COT_mean*month*lon"),stringsAsFactors=F)
414
  
415
mods$r2=
416
  do.call(rbind,lapply(1:nrow(mods),function(i){
417
    lm1=lm(as.formula(mods$models[i]),data=mod06s2)
418
    summary(lm1)$r.squared}))
419

  
420
mods
421

  
422

  
423

  
424

  
425

  
426

  
427

  
428

  
429
load("data/modis/pointsummary.Rdata")
430

  
431

  
432
dsl=melt(ds,id.vars=c("id","date","ppt","lon","lat"),measure.vars=  c("Cloud_Water_Path","Cloud_Effective_Radius","Cloud_Optical_Thickness"))
433

  
434
dsl=dsl[!is.nan(dsl$value),]
435

  
436

  
437

  
438

  
439
####
440
## mean annual precip
441
dp=d[d$variable=="ppt",]
442
dp$year=format(dp$date,"%Y")
443
dm=tapply(dp$value,list(id=dp$id,year=dp$year),sum,na.rm=T)
444
dms=apply(dm,1,mean,na.rm=T)
445
dms=data.frame(id=names(dms),ppt=dms/10)
446

  
447
dslm=tapply(dsl$value,list(id=dsl$id,variable=dsl$variable),mean,na.rm=T)
448
dslm=data.frame(id=rownames(dslm),dslm)
449

  
450
dms=merge(dms,dslm)
451
dmsl=melt(dms,id.vars=c("id","ppt"))
452

  
453
summary(lm(ppt~Cloud_Effective_Radius,data=dms))
454
summary(lm(ppt~Cloud_Water_Path,data=dms))
455
summary(lm(ppt~Cloud_Optical_Thickness,data=dms))
456
summary(lm(ppt~Cloud_Effective_Radius+Cloud_Water_Path+Cloud_Optical_Thickness,data=dms))
457

  
458

  
459
#### draw some plots
460
#pdf("output/MOD06_summary.pdf",width=11,height=8.5)
461
png("output/MOD06_summary_%d.png",width=1024,height=780)
462

  
463
 ## daily data
464
xyplot(value~ppt/10|variable,data=dsl,
465
       scales=list(relation="free"),type=c("p","r"),
466
       pch=16,cex=.5,layout=c(3,1))
467

  
468

  
469
densityplot(~value|variable,groups=cut(dsl$ppt,c(0,50,100,500)),data=dsl,auto.key=T,
470
            scales=list(relation="free"),plot.points=F)
471

  
472
## annual means
473

  
474
xyplot(value~ppt|variable,data=dmsl,
475
       scales=list(relation="free"),type=c("p","r"),pch=16,cex=0.5,layout=c(3,1),
476
       xlab="Mean Annual Precipitation (mm)",ylab="Mean value")
477

  
478
densityplot(~value|variable,groups=cut(dsl$ppt,c(0,50,100,500)),data=dmsl,auto.key=T,
479
            scales=list(relation="free"),plot.points=F)
480

  
481

  
482
## plot some swaths
483

  
484
nc1=raster(fs$path[3],varname="Cloud_Effective_Radius")
485
nc2=raster(fs$path[4],varname="Cloud_Effective_Radius")
486
nc3=raster(fs$path[5],varname="Cloud_Effective_Radius")
487

  
488
nc1[nc1<=0]=NA
489
nc2[nc2<=0]=NA
490
nc3[nc3<=0]=NA
491

  
492
plot(roi)
493
plot(nc3)
494

  
495
plot(nc1,add=T)
496
plot(nc2,add=T)
497

  
498

  
499
dev.off()
500

  
501

  
climate/research/CaseStudySelection.R
1
### Script to explore selection of Case Study Regions
2

  
3
library(rgdal)
4
library(latticeExtra)
5
library(maptools)
6

  
7
## make local copy of ghcn database on atlas
8
#  pg_dump ghcn > ghcn
9
#  copy file to local server
10
#  createdb ghcn
11
#  psql -d ghcn < ghcn
12

  
13

  
14
### get modis tiles
15
modt=readOGR("/home/adamw/acrobates/Global/modis_sinusoidal","modis_sinusoidal_grid_world",)
16
tiles=c("H18V1","H18V2","H18V3","H11V8","H19V12","H20V12","H21V9","H21V8","H31V11","H31V10","H29V5","H28V4","H28V5")
17
modt$roi=as.factor(modt$HV%in%tiles)
18

  
19
## get coastline data
20
#coast=readOGR("/media/data/globallayers/GSHHS/GSHHS_shp/c/","GSHHS_c_L2")
21
coast=Rgshhs("/media/data/globallayers/GSHHS/gshhs/gshhs_c.b", ylim=c(-60,90),xlim=c(0.01,359.99), level = 4, minarea = 1, shift = T, verbose = TRUE, checkPolygons=T)[["SP"]]
22
coast2=nowrapSpatialPolygons(coast)
23
coast=getRgshhsMap("/media/data/globallayers/GSHHS/gshhs/gshhs_c.b", ylim=c(-60,90),xlim=c(-180,180), level=4,shift = T, verbose = TRUE, checkPolygons=T)
24

  
25
## create union version
26
coast2=gUnionCascaded(coast)  #dissolve any subparts of coast
27
coast2=SpatialPolygonsDataFrame(coast2,data=data.frame(id=1))
28
## create spatial lines version
29
coastl=as(coast,"SpatialLines")
30

  
31

  
32
### transform to dproj
33
coast=spTransform(coast,projection(modt))
34
coastl=spTransform(coastl,CRS(proj4string(modt)))
35

  
36

  
37
spplot(modt,zcol="roi",col.regions=c("transparent","red"))+layer(sp.lines(coastl))
38

  
39
## Bounding box of region in lat/lon
40
roi_ll=spTransform(roi,CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
41
roi_bb=bbox(roi_ll)

Also available in: Unified diff