Skip to content

Instantly share code, notes, and snippets.

@mikshila
Created November 21, 2018 16:39
Show Gist options
  • Save mikshila/ce11fc92682bb3af0fd9fb2305613f42 to your computer and use it in GitHub Desktop.
Save mikshila/ce11fc92682bb3af0fd9fb2305613f42 to your computer and use it in GitHub Desktop.
R script for manipulating PRISM data - from .bil, reproject, clip, and export as .asc
library(sp)
library(raster)
library(rgdal)
reference <- raster("D:\\ClipGridv1\\ca_270m_v8.asc")
projection(reference) <- "+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
teale <- CRS("+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")
#resample DEM at 4km
reference4k <- raster("D:\\Metgrid\\PRISM_daily\\reference4k.tif")
res(reference4k) <- 4000
writeRaster(reference4k, "reference4k2.tif", crs=teale, overwrite = TRUE, prj = TRUE)
setwd("D:\\Metgrid\\PRISM_daily")
#set up initial file names for all daily grids
l <- list.files("D:\\Metgrid\\PRISM_daily", pattern=".bil$")
lc <- substr(l,24,31)
#need to change the first characters to the climate parameter (eg ppt, tmn, tmx)
lc2 <- paste0("ppt_",lc)
#tempo starts at a date and creates a sequence for each day to name each daily file
#change the length.out to be the number of daily files
tempo <- seq(as.Date("2000/01/01"),
by = "day", length.out = 15)
tempodoy <- strftime(tempo, format = "%Y_%j")
filename = paste0("ppt",tempodoy,".asc")
#create raster stack with all .bil files in the path folder
s <- stack(list.files(path = "D:\\Metgrid\\PRISM_daily",
pattern="*.bil$", full.names=FALSE))
projection(s) <- "+proj=longlat +ellps=GRS80"
projectRaster(s,crs = teale, method = "bilinear", res = 4000)
#crop did not work
#x <- crop(s, reference4k)
#prints projection file to check and does not match Teale projection, not in meters.
writeRaster(s, filename, format = "ascii", bylayer=TRUE, overwrite=TRUE,
CRS = teale, prj = T, NAflag = -9999)
@OneGneissGuy
Copy link

Do I need the the .tifs? If so, please send them, =-P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment