-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWAPDaily_queries.R
319 lines (276 loc) · 12.9 KB
/
AWAPDaily_queries.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Queries for AWAPDaily, M. Kearney Feb 2012
#
# Tables: 1900-2010
#
# [id]
# [day]
# [rr] rainfall (mm), 1900-2010
# [tmax] maximum air temperature, 1911-2010
# [tmin] minimum air temperature, 1911-2010
# [vpr] vapour pressure hPa
# [sol] global daily solar radiation MJ m-2 d-1
#
# Table: latlon
#
# [id]
# [latitude]
# [longitude]
####################################################################################
####################################################################################
####################################################################################
# begin query for a specific site for a given period, site specified with Google Maps lookup
####### BEGIN USER INPUT #######################################
loc <- "Newman, Western Australia" # type in a location here
datestart<-"01/01/2015" # day, month, year
datefinish<-"31/12/2015" # day, month, year
####### END USER INPUT #########################################
library(RODBC)
library(chron)
channel <- odbcConnect("AWAPDaily")
library(dismo)
longlat <- geocode(loc)[1, 3:4] # assumes first geocode match is correct
# specify bounding box around selected lat/long
lat<-longlat[2]
lon<-longlat[1]
lat1<-lat-0.025
lat2<-lat+0.025
lon1<-lon-0.025
lon2<-lon+0.025
datestart<-strptime(datestart, "%d/%m/%Y") # convert to date format
datefinish<-strptime(datefinish, "%d/%m/%Y") # convert to date format
yearstart<-as.numeric(format(datestart, "%Y")) # yet year start
yearfinish<-as.numeric(format(datefinish, "%Y")) # yet year finish
years<-seq(yearstart,yearfinish,1) # get sequence of years to do
juldaystart<-datestart$yday+1 # get Julian day of year at start
juldayfinish<-datefinish$yday+1 # get Julian day of year at finish
for(i in 1:length(years)){ # start loop through years
# syntax for query
if(length(years)==1){ # doing a period within a year
query<-paste("SELECT a.latitude, a.longitude, b.*
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day between ",juldaystart," and ",juldayfinish,")
order by b.day",sep="")
}else{
if(i==1){ # doing first year, start at day requested
query<-paste("SELECT a.latitude, a.longitude, b.*
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day >= ",juldaystart,")
order by b.day",sep="")
}else{
if(i==length(years)){ # doing last year, only go up to last day requested
query<-paste("SELECT a.latitude, a.longitude, b.*
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day <= ",juldayfinish,")
order by b.day",sep="")
}else{ # doing in between years, so get all data for this year
query<-paste("SELECT a.latitude, a.longitude, b.*
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,")
order by b.day",sep="")
}}}
# exectue query and concatenate if necessary
if(i==1){
output<- sqlQuery(channel,query)
}else{
output<-rbind(output,sqlQuery(channel,query))
}
} # end loop through years
output$sol<-as.numeric(as.character(output$sol))
# plot data
dates<-seq(datestart,datefinish,"days")
output<-cbind(dates,output)
plot(output$dates,output$rr, type='h', xlab = "month", ylab = "rainfall (mm/d)")
plot(output$dates,output$tmin, type='l', xlab = "month", col='4', ylab = expression("min/max air temp ("*degree*"C)"), ylim=c(min(output$tmin),max(output$tmax)))
points(output$dates,output$tmax, type='l', col='2')
plot(output$dates,output$vpr, type='l', xlab = "month", ylab = "vapour pressure (hPa)")
plot(output$dates,output$sol, type='l', xlab = "month", ylab = "solar radiation (MJ/m^2/d)")
write.csv(output,'/NicheMapR_Working/test_output.csv')
# end query for a specific site for a given period, site specified with Google Maps lookup
####################################################################################
####################################################################################
####################################################################################
# begin query AWAPDaily from a list of sites specified in a .csv file for a given time period
####### BEGIN USER INPUT #######################################
sitesfile <- "C:/NicheMapR_Working/PredEcolServer/tawny_points.csv" # file with longitude/latitudes
datestart<-"1/1/1960" # day, month, year
datefinish<-"31/12/2013" # day, month, year
####### END USER INPUT #########################################
library(RODBC)
channel <- odbcConnect("AWAPDaily")
# get sites list
x<-as.data.frame(read.table(file = sitesfile, sep = ",",head=TRUE))
numsites<-length(x[,1])
colnames(x)<-c("lat","lon")
x<-cbind(x$lon,x$lat)
# process period of interest
datestart<-strptime(datestart, "%d/%m/%Y") # convert to date format
datefinish<-strptime(datefinish, "%d/%m/%Y") # convert to date format
yearstart<-as.numeric(format(datestart, "%Y")) # get year start
yearfinish<-as.numeric(format(datefinish, "%Y")) # yet year finish
years<-seq(yearstart,yearfinish,1) # get sequence of years to do
juldaystart<-datestart$yday+1 # get Julian day of year at start
juldayfinish<-datefinish$yday+1 # get Julian day of year at finish
dates<-seq(datestart,datefinish+3600,"days") # sequence of dates
# loop through sites, make queries and collate data
for(j in 1:numsites){ # start loop through sites
lat<-x[j,2]
lon<-x[j,1]
lat1<-lat-0.025
lat2<-lat+0.025
lon1<-lon-0.025
lon2<-lon+0.025
for(i in 1:length(years)){ # start loop through years
# syntax for query
if(length(years)==1){ # doing a period within a year
query<-paste("SELECT a.latitude, a.longitude, b.tmax, b.tmin, b.rr
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day between ",juldaystart," and ",juldayfinish,")
order by b.day",sep="")
}else{
if(i==1){ # doing first year, start at day requested
query<-paste("SELECT a.latitude, a.longitude, b.tmax, b.tmin, b.rr
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day >= ",juldaystart,")
order by b.day",sep="")
}else{
if(i==length(years)){ # doing last year, only go up to last day requested
query<-paste("SELECT a.latitude, a.longitude, b.tmax, b.tmin, b.rr
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day <= ",juldayfinish,")
order by b.day",sep="")
}else{ # doing in between years, so get all data for this year
query<-paste("SELECT a.latitude, a.longitude, b.tmax, b.tmin, b.rr
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,")
order by b.day",sep="")
}}}
# exectue query and concatenate if necessary
if(i==1){
output<- sqlQuery(channel,query)
}else{
output<-rbind(output,sqlQuery(channel,query))
}
} # end loop through years
if(j==1){
results<-output
}else{
results<-rbind(results,output)
}
} # end loop through sites
results<-cbind(rep(dates,nrow(results)/nrow(output)),results)
colnames(results)[1]<-"date"
# write output
setwd("C:/NicheMapR_Working/projects/Tawny Dragons/")
filename<-paste("results_flinders.csv")
write.table(results, file = filename, sep = ",", col.names = NA, qmethod = "double")
# end query AWAPDaily from a list of sites specified in a .csv file for a given time period
####################################################################################
####################################################################################
####################################################################################
# begin query AWAPDaily for a grid of pixels of a particular variable at a particular time
####### BEGIN USER INPUT #######################################
loc <- "Kulgera, Australia" # type in a location here
datestart<-"17/11/2015" # day, month, year
datefinish<-"17/11/2015" # day, month, year
variable<-"sol"
quadlen<-1000 # quadrangle length (# of 0.05 degree steps north/south, centred on location)
quadwid<-1000 # quadrangel width (# of 0.05 degree steps east/west, centred on location)
####### END USER INPUT #########################################
library(RODBC)
channel <- odbcConnect("AWAPDaily")
# process dates
datestart<-strptime(datestart, "%d/%m/%Y") # convert to date format
datefinish<-strptime(datefinish, "%d/%m/%Y") # convert to date format
yearstart<-as.numeric(format(datestart, "%Y")) # get year start
yearfinish<-as.numeric(format(datefinish, "%Y")) # yet year finish
years<-seq(yearstart,yearfinish,1) # get sequence of years to do
juldaystart<-datestart$yday+1 # get Julian day of year at start
juldayfinish<-datefinish$yday+1 # get Julian day of year at finish
dates<-seq(datestart,datefinish,"days") # sequence of dates
library(dismo)
longlat <- geocode(loc)[1, 2:3] # assumes first geocode match is correct
longlat<-c(131.1676,-22.724)
quadres<-0.05 # quadrangle resolution (degrees)
# make a grid of points centred on the chosen location
gt = GridTopology(cellcentre.offset = c(as.numeric(longlat[2]-(quadlen-1)*quadres/2), as.numeric(longlat[1]-(quadwid-1)*quadres/2)), cellsize = c(quadres,quadres), cells.dim = c(quadlen, quadwid))
grd = SpatialGrid(gt)
x<-data.frame(coordinates(grd))
names(x)<-c("lat","lon")
x<-subset(x,select=c(lon,lat))
lat1<-min(x[,2]) # min latitude
lat2<-max(x[,2]) # max latitude
lon1<-min(x[,1]) # min longitude
lon2<-max(x[,1]) # max longitude
# query syntax
for(i in 1:length(years)){ # start loop through years
# syntax for query
if(length(years)==1){ # doing a period within a year
query<-paste("SELECT a.latitude, a.longitude, b.",variable,"
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day between ",juldaystart," and ",juldayfinish,")
order by b.day",sep="")
}else{
if(i==1){ # doing first year, start at day requested
query<-paste("SELECT a.latitude, a.longitude, b.",variable,"
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day >= ",juldaystart,")
order by b.day",sep="")
}else{
if(i==length(years)){ # doing last year, only go up to last day requested
query<-paste("SELECT a.latitude, a.longitude, b.",variable,"
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,") and (b.day <= ",juldayfinish,")
order by b.day",sep="")
}else{ # doing in between years, so get all data for this year
query<-paste("SELECT a.latitude, a.longitude, b.",variable,"
FROM [AWAPDaily].[dbo].[latlon] as a
, [AWAPDaily].[dbo].[",years[i],"] as b
where (a.id = b.id) and (a.latitude between ",lat1," and ",lat2,") and (a.longitude between ",lon1," and ",lon2,")
order by b.day",sep="")
}}}
# exectue query and concatenate if necessary
if(i==1){
output<- sqlQuery(channel,query)
}else{
output<-rbind(output,sqlQuery(channel,query))
}
} # end loop through years
alldates<-rep(dates,nrow(output)/length(dates))
alldates<-sort(alldates)
output<-cbind(alldates,output)
colnames(output)[1]<-"date"
# make raster for a given variable
# create an empty raster
lat1<-min(output$latitude)-.025 # min latitude
lat2<-max(output$latitude)-.025 # max latitude
lon1<-min(output$longitude)-.025 # min longitude
lon2<-max(output$longitude)-.025 # max longitude
quadwid<-(lon2-lon1)/.05
quadlen<-(lat2-lat1)/.05
gridout <- raster(ncol=quadwid, nrow=quadlen, xmn=lon1, xmx=lon2, ymn=lat1, ymx=lat2)
output[ is.na(output) ] <- -1 # get rid of na values which are often in the solar dataset (otherwise can't make grids)
brks<-round(seq(min(output[4]),max(output[4]),(max(output[4])-min(output[4]))/10))
# setwd("c:/nichemapr/temp")
for(i in 1:length(dates)){
vals <- subset(output, subset=(date==dates[i]))
x<-cbind(vals$longitude,vals$latitude) # list of co-ordinates
# make, plot and write grid
grid <- rasterize(x, gridout, vals[,4])
if(i==1){s<-grid}else{s<-stack(s,grid)}
#plot(grid,breaks=brks)
#filename<-paste(variable,"_",i,"_",year,".tif",sep="")
# writeRaster(gridout, filename=filename, progress='window', overwrite=TRUE)
}
plot(grid)
#plot(s,breaks=brks,col=terrain.colors(length(brks)))