Background

Pull SNODAS data and collect into a “database” of local netcdf files.

Setup

Load the rwrfhydro package.

library("rwrfhydro")
## To check rwrfhydro updates run: CheckForUpdates()

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/' 

Get some data

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)
if(snodasGot) {
  snodasList <- ReadSnodasDepthSweDate(threeDaysBack, outputDir=snodasPath)
  snodasNcFile <- PutSnodasNcdf(snodasList, outputDir=snodasPath)
  snodasNcFile
}
## [1] "~/wrfHydroTestCases/snodas//SNODAS_20150426.nc"

Read ncdf and visualize

swe <- ncdump(snodasNcFile, variable = 'SWE')
## File: ~/wrfHydroTestCases/snodas//SNODAS_20150426.nc
## ( NC_FORMAT_NETCDF4 ):
## dimensions (3):
##     Latitude = 6935 ; 
##     Longitude = 3351 ; 
##     Time = UNLIMITED ; // (1 currently)
## variables (3):
##     integer Time(Time) ; 
##         Time:units = "POSIXct"
##         Time:long_name = "Time"
##     double SWE(Time,Longitude,Latitude)   (Chunking: [1265,610,1])   ; 
##         SWE:units = "m" ;
##         SWE:_FillValue = "-9.999" ;
##         SWE:long_name = "Snow water equivalent" ;
##     double snowDepth(Time,Longitude,Latitude)   (Chunking: [1265,610,1])   ; 
##         snowDepth:units = "m" ;
##         snowDepth:_FillValue = "-9.999" ;
##         snowDepth:long_name = "Snow depth" ;
## 
## // global attributes (2):
##     :Time = "2012-07-05_00:00:00"
##     :POSIXct Origin = "1970-01-01 00:00.00 UTC"
## There were some serious outliers three days ago.
swe[swe>quantile(as.vector(swe),.999, na.rm=TRUE)] <- NA
image(swe)

Update

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    
## [1] "2015-04-26" "2015-04-27" "2015-04-28" "2015-04-29"

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
##          .id       date snodasGot                                       ncdfFile
## 1 2015-04-26 2015-04-26     FALSE  ~/wrfHydroTestCases/snodas/SNODAS_20150426.nc
## 2 2015-04-27 2015-04-27      TRUE ~/wrfHydroTestCases/snodas//SNODAS_20150427.nc
## 3 2015-04-28 2015-04-28      TRUE ~/wrfHydroTestCases/snodas//SNODAS_20150428.nc
## 4 2015-04-29 2015-04-29      TRUE ~/wrfHydroTestCases/snodas//SNODAS_20150429.nc

Shows that the first file wasnt “got” - that’s because it was already in place.