-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sea_level.Rmd
92 lines (73 loc) · 2.35 KB
/
Sea_level.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Sea level data processing"
author: "UN"
date: "July 25, 2017"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Import raw sea level data}
sea_level_raw <- read.csv('Sea_level_rawdata.csv')
SL_2000 <- sea_level[sea_level$year==2000,]
```
```{r import libraries}
library(raster)
library(rasterVis)
library(maptools)
library(rgdal)
library(dplyr)
library(reshape2)
library (rgdal)
library(sp)
library(tidyr)
library(gstat)
```
```{r Interpolation}
#Create a function that extract sea level data to each of counties
#Import points
sea_level_extract <- function(sea_level){
#remove NA from data
sea_level <- sea_level %>% drop_na()
#Create spatial points data frame
sea_level_sp <- sea_level
coordinates(sea_level_sp) <- ~long + lat
#Create raster grid
#Create spatial extent, x,y min, max
x_range <- as.numeric(c(-99,-80))
y_range <- as.numeric(c(24,32))
#Create values for grid
grd <- expand.grid(x=seq(from=x_range[1],to = x_range[2], by=.1), y=seq(from=y_range[1],to = y_range[2], by=.1))
#Convert grid to matrix
coordinates(grd) <- ~x + y
gridded(grd) <- TRUE
#Interpolating using IDW
# ~1 setting independent variable to a constant
idw_pow1 <- idw(formula=Sea_level.mm ~ 1, locations = sea_level_sp, newdata = grd, idp = 1)
#extract sea level data
sea_level_r <- raster(idw_pow1) #Convert interpolated data frame to raster
county <- readOGR("/home/shares/oss2017/social/DATA/counties.coast.shp") #Read in coastal county shape file
sea_level_county <- raster::extract(sea_level_r,
county,
buffer = 0.5,
fun = mean,
sp = TRUE) #Extract value for counties
sea_level_county_df <<- as.data.frame(sea_level_county)
return(sea_level_county_df)
}
sea_level_table <- data.frame() #Create an empty dataframe
#Get sea_level data for each year
list <- (1978:2016)
for (i in list){
print(i)
SL <- sea_level_raw[sea_level_raw$year==i,]
sea_level_table <- rbind(sea_level_table, sea_level_extract (SL))}
#Create a year column to add to the sea level table
year <- rep(list,each=60)
sea_level_table <- cbind(sea_level_table, year)
#Name the sea level column
names(sea_level_table)[18]="Sea_level.mm"
write.csv(x=sea_level_table,file="sea_level.csv")
```