Skip to content

Instantly share code, notes, and snippets.

@dblodgett-usgs
Created May 20, 2021 00:37
Show Gist options
  • Save dblodgett-usgs/8d8e7a4ffe295cbf8ad06dda010dafce to your computer and use it in GitHub Desktop.
Save dblodgett-usgs/8d8e7a4ffe295cbf8ad06dda010dafce to your computer and use it in GitHub Desktop.
Pull NLDAS Data Rods
httr::GET("https://ldas.gsfc.nasa.gov/sites/default/files/ldas/nldas/NLDAS_masks-veg-soil.nc4",
httr::write_disk("NLDAS_masks-veg-soil.nc"))
f <- "NLDAS_masks-veg-soil.nc"
meta <- ncmeta::nc_meta(f)
meta$variable
mask <- stars::read_ncdf(f, var = "NLDAS_mask")
stars::st_dimensions(mask)
lon <- stars::st_get_dimension_values(mask, which = "lon")
lat <- stars::st_get_dimension_values(mask, which = "lat")
p <- c(-90, 40)
nearest <- function(x, y) {
z <- abs(x - y)
which(z == min(z))[1]
}
X <- nearest(p[1], lon)
Y <- nearest(p[2], lat)
u <- paste0("https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/access/timeseries.cgi",
"?variable=",
"NLDAS:NLDAS_FORA0125_H.002:TMP2m",
"&location=",
"NLDAS:X", stringr::str_pad(X, 3, "left", "0"), "-Y", stringr::str_pad(Y, 3, "left", "0"),
"&startDate=",
"2015-01-01T00",
"&endDate=",
"2015-06-20T23",
"&type=",
"asc2")
asc <- httr::GET(u)
parse_list <- function(x) {
lapply(x, function(y) {
y <- strsplit(y, "=")[[1]]
out <- list()
out[[y[1]]] <- y[2]
out
})
}
parse_asc2 <- function(asc) {
asc <- rawToChar(asc$content)
asc <- strsplit(asc, "\n")[[1]]
headers <- list(f = parse_list(asc[3:21]), d = parse_list(asc[24:38]))
d <- read.table(text = asc[40:length(asc)], sep = "\t")
return(list(headers = headers, data = d))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment