-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCM.Rmd
405 lines (296 loc) · 14.2 KB
/
GCM.Rmd
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
---
title: "GCMs_download"
author: "Gustavo Facincani Dourado"
date: "8/2/2020"
output: html_document
---
```{r}
init_year <- 2030
end_year <- 2060
climate_scenario <- 'CanESM2'
variables <- c('ET','Tair','baseflow','precip','rainfall','runoff','snow_melt','snowfall','tot_runoff')
study_path <- 'http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/'
for (i in variables) {
folder = toString(study_path, climate_scenario, rcp, sep="/")
save_path = toString(study_path, climate_scenario, rcp, i, sep="/")
for (year in range(init_year,end_year+1)){
data_file = toString(variable, ".", toString(year),".v0.CA_NV.nc",sep="/")
path = toString(folder, data_file, sep="/")
print('now downloading: ', path, sep="/")
testfile = urllib.URLopener()
testfile.retrieve(path, save_path,data_file, sep="/")
}
```
```{r}
#download_nc <- function(model, rcp, variable){
year <- c(2006:2100)
model <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
#YearStart <- "2006" #earliest year for GCM data
#YearStop<- "2100" #latest year for GCM data
rcp <- c("rcp45", "rcp85") #emission scenarios
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
for (year in years) {
# for(model in models){
# for(rcp in rcps){
# for(variable in variables){
ncdfURL <- paste0("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/",model,"/",rcp,"/",variable,".",year,".v0.CA_NV.nc")
#newdir <- paste0("C:/Users/gusta/Desktop/PhD/CERCWET/")
#dir.create(newdir) # should test for error
# cwd <- getwd() # CURRENT dir
# setwd(newdir)
download.file(url=ncdfURL,destfile=paste0(newdir,"/", model,"_",rcp,"_",variable,"_",year,"_","CA_NV.nc"), mode = "wb")
setwd(cwd)
}#}}}
#}
```
download.file(url=fullurl, destfile=paste0("somedata/CALADAPT/projected_data_monthly/", ccmods[[c]], "/", ccscenario[[s]], "/", varofinterest[[v]], "/", varofinterest[[v]], "_month_", ccmods[[c]], "_", ccscenario[[s]], "_", time[[t]], "-", month[[m]], ".v0.CA_NV.tif"), method="curl")
```{r}
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
modelMat <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
rcp <- c("rcp45", "rcp85") #emission scenarios
lapply(modelMat, download_nc, rcp=rcp, variable=variable)
```
```{r}
download_nc <- function(model, rcp, variable){
year <- c(2006:2100)
model <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
#YearStart <- "2006" #earliest year for GCM data
#YearStop<- "2100" #latest year for GCM data
rcp <- c("rcp45", "rcp85") #emission scenarios
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
for (i in seq_along(year)) {
ncdfURL <- paste0("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/",model,"/",rcp,"/",variable,".",i,".v0.CA_NV.nc")
for(a in seq_along(model)){
for(b in seq_along(rcp)){
newdir <- setwd("C:/Users/gusta/Desktop/PhD/CERCWET/")
newdir2 <- dir.create(newdir,a,b)}}
#dir.create(newdir) # should test for error
# cwd <- getwd() # CURRENT dir
# setwd(newdir)
for(a in seq_along(model)){
for(b in seq_along(rcp)){
files <- paste0(a,"_",b,"_",variable,"_",i,"_","CA_NV.nc")}}
#for(a in model){
# for(b in rcp){
# for(c in variable){
for (file in seq_along(files)) {
download.file(url=ncdfURL,destfile= paste(file),mode = "wb")
#setwd(cwd)
}
}
}
```
```{r}
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
modelMat <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
rcp <- c("rcp45", "rcp85") #emission scenarios
lapply(modelMat, download_nc, rcp=rcp, variable=variable)
```
```{r}
#download_nc <- function(model, rcp, variable){
year <- c(2006:2100)
model <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
#YearStart <- "2006" #earliest year for GCM data
#YearStop<- "2100" #latest year for GCM data
rcp <- c("rcp45", "rcp85") #emission scenarios
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
for (i in year) {
ncdfURL <- paste0("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/",model,"/",rcp,"/",variable,".",i,".v0.CA_NV.nc")
for(a in model){
for(b in rcp){
#newdir <- paste0("C:/Users/gusta/Desktop/PhD/CERCWET/",a,"_",b,"_",variable,"_",i,"_","CA_NV.nc")
#newdir2 <- dir.create(newdir,a,b)}}
# should test for error
# cwd <- getwd() # CURRENT dir
# setwd(newdir)
for(a in model){
for(b in rcp){
#files <- paste0(a,"_",b,"_",variable,"_",i,"_","CA_NV.nc")}}
newdir <- paste0("C:/Users/gusta/Desktop/PhD/CERCWET/",a,"_",b,"_",variable,"_",i,"_","CA_NV.nc")
dir.create(newdir)
cwd <- getwd() # CURRENT dir
setwd(newdir)
#for(a in model){
# for(b in rcp){
# for(c in variable){
for (file in files) {
#download.file(url=ncdfURL,destfile= paste0(newdir, "/",file),mode = "wb")
}
}}
func.download_files <- function(link) {
download.file(url=link,destfile= paste0(newdir2),mode = "wb")
}
}}}
mapply(FUN = func.download_files,link=ncdfURL)
setwd(cwd)
#}
```
```{r}
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
modelMat <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
rcp <- c("rcp45", "rcp85") #emission scenarios
lapply(modelMat, download_nc, rcp=rcp, variable=variable)
```
```{r}
download_nc <- function(model, rcp, variable){
year <- c(2006:2100)
ncdfURL <- paste0("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/",rep(model, length(model)),"/",rep(rcp, length(rcp)),"/",rep(variable, length(variable)), ".",rep(year, length(year)),".v0.CA_NV.nc")
dest <- paste0("C:/Users/gusta/Desktop/PhD/CERCWET/",rep(model, length(model)),"/",rep(rcp, length(rcp)),sep="")
#,"/",rep(variable, length(variable)),"_",rep(year, length(year)),"_","CA_NV.nc", sep="")
dir.create(dest)
# download.file(url=ncdfURL,destfile=dest, mode = "wb")
Map(function(u, d) download.file(u, d, mode="wb"), ncdfURL, dest)
}
```
```{r}
modelMat <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
YearStart <- "2006"
YearStop<- "2100"
rcp <- c("rcp45", "rcp85")
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "snow_melt", "snowfall", "tot_runoff")
lapply(modelMat, download_nc, rcp=rcp, variable=variable)
```
```{r}
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "runoff", "SWE", "snow_melt", "snowfall", "tot_runoff") #variables of interest
modelMat <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
rcp <- c("rcp45", "rcp85") #emission scenarios
lapply(modelMat, download_nc, rcp=rcp, variable=variable)
```
```{r}
#working on this
download_nc <- function(model, rcp, variable){
year <- c(2006:2100)
ncdfURL <- paste0("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/",rep(model, length(model)),"/",rep(rcp, length(rcp)),"/",rep(variable, length(variable)), ".",rep(year, length(year)),".v0.CA_NV.nc")
neway<-paste0("C:/Users/gusta/Desktop/PhD/CERCWET/",rep(model, length(model)),"/",rep(rcp, length(rcp)),"/",rep(variable, length(variable)), ".",rep(year, length(year)),".v0.CA_NV.nc")
dest <- paste0(rep(model, length(model)),"/",rep(rcp, length(rcp)),"/",rep(variable, length(variable)), ".",rep(year, length(year)),".v0.CA_NV.nc")
# dir.create(dest)
download.file(url=ncdfURL,destfile=dest, mode = "wb")
# Map(function(u, d) download.file(u, d, method = "libcurl", quiet = TRUE), ncdfURL, dest)
}
```
```{r}
models <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
YearStart <- "2006"
year <- c(2006:2100)
YearStop<- "2100"
rcp <- c("rcp45", "rcp85")
variable <- c("ET", "Tair", "baseflow", "precip", "rainfall", "SWE", "runoff", "snow_melt", "snowfall", "tot_runoff")
lapply(models, download_nc, rcp=rcp, variable=variable)
```
```{r}
#working on this
#download_nc <- function(models, rcps, variables){
years <- c(2006:2100)
GCMs_loca <- "http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/"
GCM_dir <- paste(GCMs_loca,model,"/",rcp,"/",variable, ".",year, sep = "")
setwd("C:/Users/gusta/Desktop/PhD/CERCWET/")
models <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
rcps <- c("rcp45", "rcp85")
variables <- c("ET", "Tair", "baseflow", "precip", "rainfall", "SWE", "runoff", "snow_melt", "snowfall", "tot_runoff")
for(year in years) {
for(variable in variables) {
for(rcp in rcps){
for(model in models) {
# ncdfURL <- paste0("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/",model,"/",rcp,"/",variable, ".",year,".v0.CA_NV.nc", sep = "")
# dest <- paste0(~model,"/",rcp,"/",variable, ".",year,".v0.CA_NV.nc", sep = "")
# dir.create(dest)
filenames <- paste(variable, ".",year, ".v0.CA_NV.nc", sep="" )
#determine the GCM and RCP directory
for(i in filenames) {
pth <- paste( "./", model,"/", rcp,"/", sep = "")
#if the directory doesn't exist, make it!
# if (!file.exists(pth)){
# dir.create(pth)
}
# download.file(url=ncdfURL,destfile=dest, mode = "wb")
# Map(function(u, d) download.file(u, d, mode = "wb"), ncdfURL, dest)
download.file( paste( GCM_dir , i , sep="" ) , paste( "./", model,"/", rcp,"/" , i, sep="" ) , mode="wb" )
}}}}
#}
```
```{r}
library(rvest)
library(stringr)
page <- xml2::read_html("http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/")
page %>%
html_nodes("a") %>% # find all links
html_attr("href") %>% # get the url
str_subset("\\.nc") # look at the first one
```
```{r}
url <- "http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/ACCESS1-3/rcp45/"
doc <- read_html(url)
href <- html_attr(html_nodes(doc, "a"), "href")[[2]]
href
```
```{r}
setwd("C:/Users/gusta/Desktop/PhD/CERCWET/GCMs")
#main NHIS FTP directory with documentation
nhis_doc_ftp <- "ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Dataset_Documentation/NHIS/"
#loop through 1963 - 2010
for ( year in 1963:1964 ){
#create the full string to the FTP folder of the current year
year_ftp_dir <- paste( nhis_doc_ftp , year , "/" , sep="" )
#figure out what all of the files within that folder are named
filenames <- getURL( year_ftp_dir , dirlistonly=T )
filenames1 <- tolower( strsplit(filenames, "\r*\n")[[1]] )
#as written, the program downloads EVERY file in each year's directory
#however, if you only want to download files with "person" or "core" in their filename
#uncomment this line..
#save only the files with the word "person" or "core" in them
filenames2 <- filenames1[ grepl("person" , filenames1) | grepl("core" , filenames1) ]
print(filenames)}
```
#loop through all of those files and save them to your working directory
for ( i in filenames ){
#determine the year directory
pth <- paste( "./" , year , "/" , sep="" )
#if the directory doesn't exist, make it!
if (!file.exists(pth)){
dir.create(pth)
}
download.file( paste( year_ftp_dir , i , sep="" ) , paste( "./" , year , "/" , i , sep = "" ) , mode="wb" )
}
}
```{r}
library(RCurl)
#set your output folder
#setwd("C:/Users/gusta/Desktop/PhD/CERCWET/GCMs")
GCMs_loca <- "http://albers.cnr.berkeley.edu/data/scripps/loca_vic-output/"
rcps <- c("rcp45", "rcp85")
variables <- c("ET", "Tair")#, "baseflow", "precip", "rainfall", "SWE", "runoff", "snow_melt", "snowfall", "tot_runoff")
GCMs <- c(#"CanESM2", "CNRM-CM5", "HadGEM2-ES","MIROC5" these we already have
"ACCESS1‐0","CCSM4", "CESM1-BGC","CMCC-CMS","GFDL-CM3","HadGEM2-CC")#all 10 GCMs
#loop through 2006-2099
for (GCM in GCMs){
for(rcp in rcps) {
for(variable in variables) {
for ( year in 2006:2007){
GCM_dir <- paste(GCMs_loca,GCM,"/",rcp,"/", sep = "")
doc <- read_html(GCM_dir)
filenames <- html_attr(html_nodes(doc, "a"), "href")
filenames2 <- filenames[ grepl("ET.2" , filenames) | grepl("Tair" , filenames)| grepl("baseflow" , filenames) | grepl("precip" , filenames) | grepl("rainfall" , filenames) | grepl("SWE" , filenames) | grepl("runoff" , filenames) | grepl("snow_melt" , filenames) | grepl("snowfall" , filenames) | grepl("tot_runoff" , filenames)]
#print(filenames)
#print(filenames2)
#loop through all of those files and save them to your working directory
for ( i in filenames2 ){
#determine the GCM and RCP directory
pth <- paste( "C:/Users/gusta/Desktop/PhD/CERCWET/GCMs/", GCM,"/", rcp,"/" , sep="" )
#if the directory doesn't exist, make it!
if (!dir.exists(pth)){
dir.create(file.path(pth), recursive = TRUE)
}
download.file( paste( GCM_dir , i , sep="" ) , paste("C:/Users/gusta/Desktop/PhD/CERCWET/GCMs/", GCM,"/", rcp,"/" , i, sep="" ) , mode="wb" )
}
}}}}
```