-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetCDF_files.Rmd
82 lines (62 loc) · 1.33 KB
/
NetCDF_files.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
---
title: "NetCDF_files"
author: "Patricia DeRepentigny"
date: "1/18/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{r}
library(ncdf4)
library(dplyr)
library(tidyr)
library(ggplot2)
```
# Read in data from a file
```{r}
nc <- nc_open("data/WG2013CTD.nc")
```
```{r}
print(nc)
```
```{r}
vars <- attributes(nc$var)$names
dims <- attributes(nc$dim)$names
```
```{r}
salinity <- ncvar_get(nc, "sal")
time <- ncvar_get(nc, "time")
depth <- ncvar_get(nc, "z")
```
# Reformat the output
```{r}
time <- as.POSIXct((time + 719529)*86400, origin = "1970-01-01", tz = "UTC")
```
Coerce into a data frame
```{r}
salinity_data <- as.data.frame(salinity)
```
Assign colum names to depth values
```{r}
names(salinity_data) <- as.character(depth)
```
Reshape the data and add time
```{r}
salinity_data_long <- salinity_data %>%
mutate(time = time) %>%
gather(key = "depth", value = "salinity", -time) %>%
mutate(depth = as.numeric(depth)) %>%
arrange(time)
head(salinity_data_long)
```
# Plots
```{r}
ggplot(salinity_data_long, mapping = aes(x = time, y = depth, fill = salinity)) +
geom_raster()
```
```{r}
ggplot(salinity_data_long, mapping = aes(x = salinity, y = depth, color = time, group = time)) +
geom_line(size = 0.1) +
scale_y_reverse()
```