# TODO: Add comment
#
# Author: Dennis Jongsomjit (djongsomjit@pointblue.org) and Leo Salas (lsalas@pointblue.org)
##############################################################################################
########################
# Dependencies
########################
# Load packages
# list packages required
list.of.packages <- c("rgdal", "proj4","rgeos","maptools","raster","stringr","plyr","dplyr","xml2","httr")
# see if packages are missing and install them
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)>0) {install.packages(new.packages)}
# load the packages
lapply(list.of.packages, require, character.only = TRUE)
#########################
#Set environment variables - EDIT AS NEEDED
#########################
#location where downloaded NIC ice layers will be saved
nicsavedir<-"c:/temp"
#your local git repo path
pathToGit<-"c:/users/lsalas/git/fasticecovars/"
#########################
# Define functions
#########################
## FUNCTION download and unzip NIC data
# filename is the name of a single NIC dataset, retrieved with the function getNICfilename
# fileloc is the full path to the downloaded NIC data file, with the .zip file name
# dataproj is the projection of the data, here named "primaryproj"
getFastIce<-function(fileloc,dataproj){
print("processing single fast ice date")
#First function for 1 shapefile
##########################Read unzipped shapefile, Select subset of points from full set.
#use this subset to attribute with Colony and Fast ice attributes
#Read shapefile
loopdsn <-paste0(str_sub(fileloc,0,-5),".shp")
shpname<-str_sub(fileloc,-16,-5)
#read shapefile
region <- readOGR(dsn=loopdsn,shpname)
#Project to match full points shapefile
region<-spTransform(region,dataproj)
# Define fast ice and get region layer for creating land and ocean edges
region@data$FP <- ifelse(region@data$FP == "08",8,0)
#get fast ice only regions
fast<-subset(region,region@data$FP == 8)
functionList <- list("fast" = fast, "region" = region)
return(functionList)
}
## FUNCTION to retrieve the NIC data filenames, one of which would be used to generate covariates
# getmonth is the name of a month, first three characters, first inupper case; defaults to "Nov"
# getyear is the 4-number year; defaults to 2011
getNICfilename<-function(getmonth="Nov",getyear=2011){
#we send request here
urlv<-"https://www.natice.noaa.gov/products/weekly_products.html"
#make sure to ignore security cert error
set_config( config( ssl_verifypeer = 0L ) )
dd<-ifelse(getmonth=="Feb","28",
ifelse(getmonth %in% c("Apr","Jun","Sep","Nov"),"30","31"))
#this is what we want
body <- list(
area="Antarctic",
day0="01",
day1=dd,
format="Shapefiles",
month0=getmonth,
month1=getmonth,
oldarea="Antarctic",
oldformat="Shapefiles",
subareas="Hemispheric",
year0=as.character(getyear),
year1=as.character(getyear))
#request the data
r <- POST(urlv, body = body, encode = "form")
#check that it worked
r$status_code==200
#it returns XML, and that can be a pain to parse.
#yet, we know we want something that looks like this: ...shapefiles/hemispheric/antarc999999
#so, using regular expressions to search for it...
nv<-as.numeric(gregexpr("shapefiles/hemispheric/antarc",content(r,encoding="UTF-8"))[[1]])
filenames<-character()
#here getting the address including year
for(nn in nv){
filenames<-c(filenames,substr(content(r,encoding="UTF-8"),nn-5,nn+38))
}
#the above include the .html and .xml files. Filter for zips only...
filenames<-subset(filenames,grepl("^[0-9][0-9][0-9][0-9].*hemispheric.*.zip",filenames))
}
## FUNCTION to get edges from fast ice areas
# areas is a list of shapefiles (spatialPolygons or spatialPolygonsDataFrame) which is the output of the getFastIce function
# areas has two named elements: region and fast. See getFastIce function for details.
getEdges<-function(areas){
region<-myareas$region
#fast<-myareas$fast
# Dissolve regions based on fast ice or not
unionfp <- gUnaryUnion(region, id = region@data$FP)
#create line of fast ice edge away from continent
oceanedge = gDifference(
as(unionfp,"SpatialLines"),
as(gUnaryUnion(unionfp),"SpatialLines"),
byid=TRUE)
#create line of fast ice edge nearest to continent
landedge<-as(gUnaryUnion(unionfp),"SpatialLines")
return(list=c(oceanedge=oceanedge,landedge=landedge))
}
## FUNCTION to generate the file landEdge.RData and save in the git/data folder
makeLandEdge<-function(nicsavedir,pathToGit){
filename<-getNICfilename() # Using defaults - it's the same continental edge no matter the NIC date
#Downloading one file using the first date listed.
Filelocation<-paste0("https://www.natice.noaa.gov/pub/weekly/antarctic/",filename[1])
savename<-paste0(nicsavedir,"/",substr(filename[1],29,46))
download.file(Filelocation,destfile=savename,method="libcurl")
unzip(zipfile=savename,exdir=nicsavedir)
primaryproj<-CRS("+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
#get the land and sea edges from the download
myareas<-getFastIce(fileloc=savename,dataproj=primaryproj)
#get the lines for land (landedge) and save
edges<-getEdges(areas=myareas)
ledge<-edges$landedge
save(ledge,file=paste0(pathToGit,"data/landEdge.RData"))
print("done making landEdge.RData")
}
###############################
# Make the data file
###############################
makeLandEdge(nicsavedir=nicsavedir,pathToGit=pathToGit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.