-
Notifications
You must be signed in to change notification settings - Fork 1
/
descarga_noaa.R
72 lines (52 loc) · 1.94 KB
/
descarga_noaa.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
library(raster)
library(landsat)
library(rgdal)
library(XML)
library(sp)
library(stringr)
library(RCurl)
library(plyr)
library(R.utils)
library(httr)
setwd("/Volumes/LaCie")
#1. Get URL's
url <- "http://www.ngdc.noaa.gov/eog/data/web_data/v4composites/"
links <- getHTMLLinks(url)
filenames <- links[str_detect(links, ".v4.tar")]
filenames_list <- as.list(filenames)
#2. Create a download function (we use the option write binary "wb" in mode, this option is valid if you're working on Windows)
download <- function(filename, baseurl, folder) {
dir.create(folder, showWarnings = F)
fileurl <- str_c(baseurl, filename)
if (!file.exists(str_c(folder, "/", filename))) {
download.file(fileurl,
destfile = str_c(folder,"/", filename), mode = "wb")
Sys.sleep(1)
}
}
#3. Apply to list of filenames
l_ply(filenames_list, download,
baseurl = "http://www.ngdc.noaa.gov/eog/data/web_data/v4composites/",
folder = "NOAA2"
)
#4. Open .tar files into the same folder (only extract the stable lights .tif and the .tfw file)
untar_tiff <- function(filename, folder) {
dir.create(folder, showWarnings = F)
list <- untar(filename, list = T)
untar(filename, files = str_c(list[str_detect(list, "web.stable")]), exdir = folder)
}
lapply(filenames_list, untar_tiff, folder = "TIFF")
#5.Decompress the .tif files (as you may note the .tif remain compressed)
list_tiff <- list.files("TIFF")
list_tiff <- list_tiff[str_detect(list_tiff, ".tif.gz")]
lapply(str_c("TIFF", "/", list_tiff), gunzip)
#6. Open .tar files into the same folder (only extract the stable lights .tif and the .tfw file)
untar_tiff <- function(filename, folder) {
dir.create(folder, showWarnings = F)
list <- untar(filename, list = T)
untar(filename, files = str_c(list[str_detect(list, "web.stable")]), exdir = folder)
}
#7. Delete .tfw files
list_tfw <- list.files("TIFF")
list_tfw <- list_tfw[str_detect(list_tfw, ".tfw")]
lapply(list_tfw, file.remove)