diff --git a/notebooks/SNODAS/Download_Process_masked_SNODAS_data.Rmd b/notebooks/SNODAS/Download_Process_masked_SNODAS_data.Rmd new file mode 100644 index 0000000..3f4b725 --- /dev/null +++ b/notebooks/SNODAS/Download_Process_masked_SNODAS_data.Rmd @@ -0,0 +1,214 @@ +--- +title: "How to download and work with SNDOAS data" +output: html_notebook +--- + +## 1. Tutorial Overview +This notebook demonstrates how to download and unzip masked SNODAS files that are from after October 2013 and select one variable and compile it into a RasterStack for quick visualization and finally save as a .grd file. + +### Credits +This tutorial is based on an R script written in 2021 by Cara Thompson at New Mexico State University and has been converted to a tutorial with some minor additions by Jennifer Roebuck of NSIDC. + +For questions regarding the tutorial or to report problems, please create a new issue in the [NSIDC-Data-Tutorials repo](https://github.com/nsidc/NSIDC-Data-Tutorials/issues) + +### Learning Objectives + +By the end of this demonstration you will be able to: +1. Download and unzip masked SNODAS files +2. Convert the files to .bil format and put them in a RasterStack +3. Quickly visualize the rasters of the data +4. Save the RasterStack as .grd file + +### Prerequisites +1. The following libraries should already be installed: R.utils, rvest, dplyr, raster, sp, maptools, and data.table. + + +### Time requirement +Allow approximately 20 minutes to complete this tutorial. + +## 2. Tutorial steps + +### Load necessary libraries +```{r} +library(R.utils) +library(rvest) +library(dplyr) +library(raster) +library(sp) +library(maptools) +library(data.table) +``` + +### Download SNODAS data +We will download all the SNODAS masked files for June 2023. If you wish to download masked files from a different time period you can modify the path in the url variable below. + +```{r} +# set the URL for the SNODAS directory we want to download files from +url <- "https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun" + +# read html content from url +page <- read_html(url) + +# Get a list of the files listed in the directory at this url +files <- page %>% html_nodes("a") %>% html_attr("href") + +for(i in 2:length(files)){ + # generate the url for each of the files + u <- paste(url,files[i], sep="/") + # download each of the files + download.file(u,files[i], mode = "wb") +} +``` + +We will save the working directory path to a variable. +```{r} +WD <- getwd() +``` + +### Read in the SNODAS files +Next, we read in the .tar files as a list containing .tar +```{r} +files_1 <- list.files(path = WD, pattern = "*.tar", full.names = T, recursive = T) +``` + +Decompress the .tar files into the working directory +```{r} +asd <- sapply(files_1, untar) +``` + +### Select the variable we wish to use +There are eight parameters from SNODAS, each has an associated ID code which is used the filename. A full description of the ID codes can be found in Table 1 of the [SNODAS user guide](https://nsidc.org/sites/default/files/g02158-v001-userguide_2_1.pdf). We will choose one of the parameters to compile all the files related to this parameter. + +We can uncomment one of the variables below that corresponds to the parameter we are interested in. In this tutorial we will use the snow depth parameter as an example. + +```{r} +id_code = "us_ssmv11036tS" # Snow Depth +#id_code = "us_ssmv11034tS" # SWE +#id_code = "us_ssmv11044bS" # Snow melt runoff at base of snow pack +#id_code = "us_ssmv11050IL00" # Sublimation from the snow pack +#id_code = "us_ssmv11039IL00" # sublimation of blowing snow +#id_code = "us_ssmv11038wS" # snow pack average temperature +#id_code = "us_ssmv01025SIL01" # Solid precipitation +#id_code = "us_smsv01025SIL00" # Liquid precipitation +``` + +Now we will compile all the files that contain this id_code in the filename i.e. a .dat.gz file and .txt.gz file for each day of data that we downloaded. + +```{r} +files_2 <- list.files(path = WD, pattern = id_code, full.names = T, recursive = T) +files_2 +``` + +Decompress all the .gz files. +```{r} +dcompSD <- sapply(files_2, gunzip, remove=T) +``` + +We will create a new directory for these uncompressed files and move them over. +```{r} +dir.create("Snow Depth") +file.copy(dcompSD, "Snow Depth") +``` + +### Convert the .dat files to .bil +Next we need to convert the .dat files to .bil so they can be read as rasters. + +```{r} +files <- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern="*.dat", full.names = T) +newfiles <- gsub(".dat$", ".bil", files) +file.rename(files, newfiles) +newfiles +``` + +### Create a text file header for each file +Now we will create a corresponding text header file for each .bil file using the associated .txt file that was downloaded. + +**Please note** that the parameters used in the m_post_2013 variable below are specific to SNODAS masked files for October 2013 onwards. If you wish to process masked files prior to October 2013 then you would need to change ulxmap and ulymap to the following: +ulxmap -124.72958333333334 +ulymap 52.870416666666664 + +If you wish to process unmasked files prior to October 2013 then you would need to change nrow, ncols, ulxmap and ulymap to the following: +nrows 4096 +ncols 8192 +ulxmap -130.51291666666665 +ulymap 58.228750000000005 + +If you wish to process unmasked files from October 2013 onwards then you would need to change the nrows, ncols, ulxmap and ulymap to the following: +nrows 4096 +ncols 8192 +ulxmap -130.51250000000002 +ulymap 58.229166666666664 +```{r} + +mylist<- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern = "*.bil", full.names = T) +m_post_2013<-"SNODAS Snow Depth Description = +nrows 3351 +ncols 6935 +nbands 1 +nbits 16 +pixeltype signedint +byteorder M +layout dat +ulxmap -124.72916666666667 +ulymap 52.87083333333334 +xdim 0.00833333333 +ydim 0.00833333333 +pixeltype SIGNEDINT +nodata -9999" + + +mylist.hdr<-paste(mylist,".hdr",sep="") + +for(i in mylist.hdr) { + write(m_post_2013,i) +} + +``` + +Change the file type of the header text files from .bil.hdr to .hdr since the .bil file is already separate. + +```{r} +files4 <- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern="*.bil.hdr", full.names = T) +newfiles <- gsub(".bil.hdr$", ".hdr", files4) +file.rename(files4, newfiles) +newfiles +``` + +### Create a RasterStack of all the rasters +Create a RasterStack of all the snow depth raster files. +```{r} +list.ras <- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern = ".bil", full.names = T) +list.ras +ras_stack <- stack(list.ras) +ras_stack +``` + +### Plot RasterStack +Now we define a projection for the RasterStack and plot each of the rasters (must be in the format of what it is in, not what you want it to be). Please note that SNODAS data are point estimates in latitude/longitude coordinates with the horizontal datum WGS 84 thus the decision to project the data and the choice of projection is up to the user. In this tutorial we will use Geographic longitude/latitude. + +```{r} +crs.geo <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84") +proj4string(ras_stack) <- crs.geo +ras_stack +plot(ras_stack) +``` + +### Save RasterStack +Save the RasterStack to a new directory. We will write in the "raster"/native format to preserve layer names (i.e. dates). There is no attribute table because there is only one attribute per file (snow depth in mm). + +```{r} +#create a new directory Rasters +dir.create(paste(WD,"/Snow Depth","/Rasters",sep="")) +fp <- paste(WD,"/Snow Depth","/Rasters",sep="") +#save the RasterStack as a .grd file +writeRaster(ras_stack,filename=file.path(fp,"ras_stack.grd"), format="raster", progress="text", options=c("INTERLEAVE=BAND","COMPRESS=LZW")) + +``` + +## 3. Learning outcomes recap + +We have learned how to: +1. Download and unzip masked SNODAS files +2. Convert the files to .bil format and put them in a raster stack +3. Quickly visualize the rasters of the data +4. Save the raster stack as .grd file \ No newline at end of file diff --git a/notebooks/SNODAS/Download_Process_masked_SNODAS_data.ipynb b/notebooks/SNODAS/Download_Process_masked_SNODAS_data.ipynb new file mode 100644 index 0000000..8ff03aa --- /dev/null +++ b/notebooks/SNODAS/Download_Process_masked_SNODAS_data.ipynb @@ -0,0 +1,464 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c0b71b3c", + "metadata": {}, + "source": [ + "
This notebook demonstrates how to download and unzip masked SNODAS files that are from after October 2013 and select one variable and compile it into a RasterStack for quick visualization and finally save as a .grd file.
+This tutorial is based on an R script written in 2021 by Cara Thompson at New Mexico State University and has been converted to a tutorial with some minor additions by Jennifer Roebuck of NSIDC.
+For questions regarding the tutorial or to report problems, please create a new issue in the NSIDC-Data-Tutorials repo
+By the end of this demonstration you will be able to: 1. Download and unzip masked SNODAS files 2. Convert the files to .bil format and put them in a RasterStack 3. Quickly visualize the rasters of the data 4. Save the RasterStack as .grd file
+Allow approximately 20 minutes to complete this tutorial.
+library(R.utils)
+library(rvest)
+
+
+Loading required package: xml2
+
+
+library(dplyr)
+library(raster)
+library(sp)
+library(maptools)
+library(data.table)
+
+
+
+We will download all the SNODAS masked files for June 2023. If you wish to download masked files from a different time period you can modify the path in the url variable below.
+ + + +# set the URL for the SNODAS directory we want to download files from
+url <- "https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun"
+
+# read html content from url
+page <- read_html(url)
+
+# Get a list of the files listed in the directory at this url
+files <- page %>% html_nodes("a") %>% html_attr("href")
+
+for(i in 2:length(files)){
+ # generate the url for each of the files
+ u <- paste(url,files[i], sep="/")
+ # download each of the files
+ download.file(u,files[i], mode = "wb")
+}
+
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230601.tar'
+Content type 'application/octet-stream' length 5724160 bytes (5.5 MB)
+==================================================
+downloaded 5.5 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230602.tar'
+Content type 'application/octet-stream' length 6543360 bytes (6.2 MB)
+==================================================
+downloaded 6.2 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230603.tar'
+Content type 'application/octet-stream' length 6737920 bytes (6.4 MB)
+==================================================
+downloaded 6.4 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230604.tar'
+Content type 'application/octet-stream' length 6021120 bytes (5.7 MB)
+==================================================
+downloaded 5.7 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230605.tar'
+Content type 'application/octet-stream' length 5621760 bytes (5.4 MB)
+==================================================
+downloaded 5.4 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230606.tar'
+Content type 'application/octet-stream' length 6307840 bytes (6.0 MB)
+==================================================
+downloaded 6.0 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230607.tar'
+Content type 'application/octet-stream' length 5038080 bytes (4.8 MB)
+==================================================
+downloaded 4.8 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230608.tar'
+Content type 'application/octet-stream' length 5345280 bytes (5.1 MB)
+==================================================
+downloaded 5.1 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230609.tar'
+Content type 'application/octet-stream' length 5171200 bytes (4.9 MB)
+==================================================
+downloaded 4.9 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230610.tar'
+Content type 'application/octet-stream' length 5427200 bytes (5.2 MB)
+==================================================
+downloaded 5.2 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230611.tar'
+Content type 'application/octet-stream' length 5806080 bytes (5.5 MB)
+==================================================
+downloaded 5.5 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230612.tar'
+Content type 'application/octet-stream' length 5724160 bytes (5.5 MB)
+==================================================
+downloaded 5.5 MB
+
+trying URL 'https://noaadata.apps.nsidc.org/NOAA/G02158/masked/2023/06_Jun/SNODAS_20230613.tar'
+Content type 'application/octet-stream' length 5478400 bytes (5.2 MB)
+==================================================
+downloaded 5.2 MB
+
+
+
+We will save the working directory path to a variable.
+ + + +WD <- getwd()
+
+
+
+Next, we read in the .tar files as a list containing .tar
+ + + +files_1 <- list.files(path = WD, pattern = "*.tar", full.names = T, recursive = T)
+
+
+
+Decompress the .tar files into the working directory
+ + + +asd <- sapply(files_1, untar)
+
+
+
+There are eight parameters from SNODAS, each has an associated ID code which is used the filename. A full description of the ID codes can be found in Table 1 of the SNODAS user guide. We will choose one of the parameters to compile all the files related to this parameter.
+We can uncomment one of the variables below that corresponds to the parameter we are interested in. In this tutorial we will use the snow depth parameter as an example.
+ + + +id_code = "us_ssmv11036tS" # Snow Depth
+#id_code = "us_ssmv11034tS" # SWE
+#id_code = "us_ssmv11044bS" # Snow melt runoff at base of snow pack
+#id_code = "us_ssmv11050IL00" # Sublimation from the snow pack
+#id_code = "us_ssmv11039IL00" # sublimation of blowing snow
+#id_code = "us_ssmv11038wS" # snow pack average temperature
+#id_code = "us_ssmv01025SIL01" # Solid precipitation
+#id_code = "us_smsv01025SIL00" # Liquid precipitation
+
+
+
+Now we will compile all the files that contain this id_code in the filename i.e. a .dat.gz file and .txt.gz file for each day of data that we downloaded.
+ + + +files_2 <- list.files(path = WD, pattern = id_code, full.names = T, recursive = T)
+files_2
+
+
+ [1] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060105HP001.dat.gz"
+ [2] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060105HP001.txt.gz"
+ [3] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060205HP001.dat.gz"
+ [4] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060205HP001.txt.gz"
+ [5] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060305HP001.dat.gz"
+ [6] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060305HP001.txt.gz"
+ [7] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060405HP001.dat.gz"
+ [8] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060405HP001.txt.gz"
+ [9] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060505HP001.dat.gz"
+[10] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060505HP001.txt.gz"
+[11] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060605HP001.dat.gz"
+[12] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060605HP001.txt.gz"
+[13] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060705HP001.dat.gz"
+[14] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060705HP001.txt.gz"
+[15] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060805HP001.dat.gz"
+[16] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060805HP001.txt.gz"
+[17] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060905HP001.dat.gz"
+[18] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023060905HP001.txt.gz"
+[19] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061005HP001.dat.gz"
+[20] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061005HP001.txt.gz"
+[21] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061105HP001.dat.gz"
+[22] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061105HP001.txt.gz"
+[23] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061205HP001.dat.gz"
+[24] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061205HP001.txt.gz"
+[25] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061305HP001.dat.gz"
+[26] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/us_ssmv11036tS__T0001TTNATS2023061305HP001.txt.gz"
+
+
+
+Decompress all the .gz files.
+ + + +dcompSD <- sapply(files_2, gunzip, remove=T)
+
+
+
+We will create a new directory for these uncompressed files and move them over.
+ + + +dir.create("Snow Depth")
+file.copy(dcompSD, "Snow Depth")
+
+
+ [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
+[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
+
+
+
+Next we need to convert the .dat files to .bil so they can be read as rasters.
+ + + +files <- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern="*.dat", full.names = T)
+newfiles <- gsub(".dat$", ".bil", files)
+file.rename(files, newfiles)
+
+
+ [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
+
+
+newfiles
+
+
+ [1] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060105HP001.bil"
+ [2] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060205HP001.bil"
+ [3] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060305HP001.bil"
+ [4] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060405HP001.bil"
+ [5] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060505HP001.bil"
+ [6] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060605HP001.bil"
+ [7] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060705HP001.bil"
+ [8] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060805HP001.bil"
+ [9] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060905HP001.bil"
+[10] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061005HP001.bil"
+[11] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061105HP001.bil"
+[12] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061205HP001.bil"
+[13] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061305HP001.bil"
+
+
+
+Now we will create a corresponding text header file for each .bil file using the associated .txt file that was downloaded.
+Please note that the parameters used in the m_post_2013 variable below are specific to SNODAS masked files for October 2013 onwards. If you wish to process masked files prior to October 2013 then you would need to change ulxmap and ulymap to the following: ulxmap -124.72958333333334 ulymap 52.870416666666664
+If you wish to process unmasked files prior to October 2013 then you would need to change nrow, ncols, ulxmap and ulymap to the following: nrows 4096 ncols 8192 ulxmap -130.51291666666665 ulymap 58.228750000000005
+If you wish to process unmasked files from October 2013 onwards then you would need to change the nrows, ncols, ulxmap and ulymap to the following: nrows 4096 ncols 8192 ulxmap -130.51250000000002 ulymap 58.229166666666664
+ + + +#setwd("/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth")
+mylist<- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern = "*.bil", full.names = T)
+x<-"SNODAS Snow Depth Description =
+nrows 3351
+ncols 6935
+nbands 1
+nbits 16
+pixeltype signedint
+byteorder M
+layout dat
+ulxmap -124.72958333333334
+ulymap 52.870416666666664
+xdim 0.00833333333
+ydim 0.00833333333
+pixeltype SIGNEDINT
+nodata -9999"
+
+mylist.hdr<-paste(mylist,".hdr",sep="")
+
+for(i in mylist.hdr) {
+ write(x,i)
+}
+
+
+
+
+Change the file type of the header text files from .bil.hdr to .hdr since the .bil file is already separate.
+ + + +#setwd("/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth")
+files4 <- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern="*.bil.hdr", full.names = T)
+newfiles <- gsub(".bil.hdr$", ".hdr", files4)
+file.rename(files4, newfiles)
+
+
+ [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
+
+
+newfiles
+
+
+ [1] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060105HP001.hdr"
+ [2] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060205HP001.hdr"
+ [3] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060305HP001.hdr"
+ [4] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060405HP001.hdr"
+ [5] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060505HP001.hdr"
+ [6] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060605HP001.hdr"
+ [7] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060705HP001.hdr"
+ [8] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060805HP001.hdr"
+ [9] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060905HP001.hdr"
+[10] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061005HP001.hdr"
+[11] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061105HP001.hdr"
+[12] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061205HP001.hdr"
+[13] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061305HP001.hdr"
+
+
+
+Create a RasterStack of all the snow depth raster files.
+ + + +#create new directory to save the RasterStack in
+#dir.create("Rasters")
+#setwd("/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/Rasters")
+
+list.ras <- list.files(path=paste(WD,"/Snow Depth",sep=""), pattern = ".bil", full.names = T)
+list.ras
+
+
+ [1] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060105HP001.bil"
+ [2] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060205HP001.bil"
+ [3] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060305HP001.bil"
+ [4] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060405HP001.bil"
+ [5] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060505HP001.bil"
+ [6] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060605HP001.bil"
+ [7] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060705HP001.bil"
+ [8] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060805HP001.bil"
+ [9] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023060905HP001.bil"
+[10] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061005HP001.bil"
+[11] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061105HP001.bil"
+[12] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061205HP001.bil"
+[13] "/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/us_ssmv11036tS__T0001TTNATS2023061305HP001.bil"
+
+
+ras_stack <- stack(list.ras)
+ras_stack
+
+
+class : RasterStack
+dimensions : 3351, 6935, 23239185, 13 (nrow, ncol, ncell, nlayers)
+resolution : 0.008333333, 0.008333333 (x, y)
+extent : -124.7338, -66.94208, 24.94958, 52.87458 (xmin, xmax, ymin, ymax)
+crs : NA
+names : us_ssmv11//60105HP001, us_ssmv11//60205HP001, us_ssmv11//60305HP001, us_ssmv11//60405HP001, us_ssmv11//60505HP001, us_ssmv11//60605HP001, us_ssmv11//60705HP001, us_ssmv11//60805HP001, us_ssmv11//60905HP001, us_ssmv11//61005HP001, us_ssmv11//61105HP001, us_ssmv11//61205HP001, us_ssmv11//61305HP001
+min values : -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768
+max values : 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767
+
+
+
+Now we define a projection for the RasterStack and plot each of the rasters (must be in the format of what it is in, not what you want it to be). Please note that SNODAS data are point estimates in latitude/longitude coordinates with the horizontal datum WGS 84 thus the decision to project the data and the choice of projection is up to the user. In this tutorial we will use Geographic longitude/latitude.
+ + + +crs.geo <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
+proj4string(ras_stack) <- crs.geo
+ras_stack
+
+
+class : RasterStack
+dimensions : 3351, 6935, 23239185, 13 (nrow, ncol, ncell, nlayers)
+resolution : 0.008333333, 0.008333333 (x, y)
+extent : -124.7338, -66.94208, 24.94958, 52.87458 (xmin, xmax, ymin, ymax)
+crs : +proj=longlat +datum=WGS84 +no_defs
+names : us_ssmv11//60105HP001, us_ssmv11//60205HP001, us_ssmv11//60305HP001, us_ssmv11//60405HP001, us_ssmv11//60505HP001, us_ssmv11//60605HP001, us_ssmv11//60705HP001, us_ssmv11//60805HP001, us_ssmv11//60905HP001, us_ssmv11//61005HP001, us_ssmv11//61105HP001, us_ssmv11//61205HP001, us_ssmv11//61305HP001
+min values : -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768
+max values : 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767
+
+
+plot(ras_stack)
+
+
+Save the RasterStack to a new directory. We will write in the “raster”/native format to preserve layer names (i.e. dates). There is no attribute table because there is only one attribute per file (snow depth in mm).
+ + + +#setwd("/Users/jero7025/Documents/NOAA/Datasets/G02158/R_code/Snow Depth/Rasters")
+dir.create(paste(WD,"/Snow Depth","/Rasters",sep=""))
+fp <- paste(WD,"/Snow Depth","/Rasters",sep="")
+writeRaster(ras_stack,filename=file.path(fp,"ras_stack.grd"), format="raster", progress="text", options=c("INTERLEAVE=BAND","COMPRESS=LZW"))
+
+
+
+ |
+ | | 0%
+ |
+ |=== | 4%
+ |
+ |====== | 8%
+ |
+ |======== | 12%
+ |
+ |=========== | 16%
+ |
+ |============== | 20%
+ |
+ |================= | 24%
+ |
+ |==================== | 28%
+ |
+ |====================== | 32%
+ |
+ |========================= | 36%
+ |
+ |============================ | 40%
+ |
+ |=============================== | 44%
+ |
+ |================================== | 48%
+ |
+ |==================================== | 52%
+ |
+ |======================================= | 56%
+ |
+ |========================================== | 60%
+ |
+ |============================================= | 64%
+ |
+ |================================================ | 68%
+ |
+ |================================================== | 72%
+ |
+ |===================================================== | 76%
+ |
+ |======================================================== | 80%
+ |
+ |=========================================================== | 84%
+ |
+ |============================================================== | 88%
+ |
+ |================================================================ | 92%
+ |
+ |=================================================================== | 96%
+ |
+ |======================================================================| 100%
+
+
+
+We have learned how to: 1. Download and unzip masked SNODAS files 2. Convert the files to .bil format and put them in a raster stack 3. Quickly visualize the rasters of the data 4. Save the raster stack as .grd file
+ +