Pull SNODAS data and collect into a "database" of local netcdf files.
Load the rwrfhydro package.
library("rwrfhydro")
options(width = 120, warn=1)
This is the path to the directory where you want your database to be built (it should exist and be empty for the purposes of this example):
snodasPath <- '~/wrfHydroTestCases/snodas/' if(dir.exists(snodasPath)) file.remove(list.files(snodasPath,'.', full.names=TRUE)) dir.create(snodasPath)
Grab a single date (both depth and SWE) and transform to netcdf. For the sake of illustration, grab three days ago.
threeDaysBack <- Sys.Date() + lubridate::period(-3,'day') snodasGot <- GetSnodasDepthSweDate(threeDaysBack, outputDir=snodasPath, overwrite=TRUE) if(snodasGot) { snodasList <- ReadSnodasDepthSweDate(threeDaysBack, outputDir=snodasPath) snodasNcFile <- PutSnodasNcdf(snodasList, outputDir=snodasPath) snodasNcFile }
swe <- ncdump(snodasNcFile, variable = 'SWE') ## Filter outliers. swe[swe>quantile(as.vector(swe), .999, na.rm=TRUE)] <- NA image(swe)
The above can be repeated over multiple dates one might want to get, for example from three days ago until now
datesWanted <- seq(threeDaysBack, Sys.Date(), by = 'days') datesWanted
Encapsulate the above in a function with some file existance checking and return summary.
UpdateSnodas <- function(POSIXct, outPath='.') { ## check if we already processed this date/POSIXct, if we didnt process, we'll ## download again and process it. (note we could have downloaded but not processed ## so it might not be efficient). file <- paste0(outPath, 'SNODAS_',format(POSIXct,'%Y%m%d'),'.nc') processed <- file.exists(file) if(processed) return(data.frame(date=POSIXct, snodasGot=FALSE, ncdfFile=file)) snodasGot <- GetSnodasDepthSweDate(POSIXct, outputDir=outPath) if(snodasGot) { snodasList <- ReadSnodasDepthSweDate(POSIXct, outputDir=outPath) ncdfFile <- PutSnodasNcdf(snodasList, outputDir=outPath) } else ncdfFile <- '' data.frame(date=POSIXct, snodasGot=snodasGot, ncdfFile=ncdfFile) } update <- plyr::ldply(NamedList(datesWanted), UpdateSnodas, outPath=snodasPath) update
Shows that the first file wasnt "got" - that's because it was already in place.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.