How to get date and depth from netcdf file and merge into a data frame?

403 Views Asked by At

I downloaded temperature data from https://www.nodc.noaa.gov/cgi-bin/OC5/woa18/woa18.pl

and I opened it in R. I am trying to create a data frame Temp where all the temperature, depth and date information are merged into a single data frame .

I can do this for a single time point depth_Temp <- data.frame(Temp_Jan@z) where I get temperature by depth

Is it possible to get depth and date from Jan - May and merge them together?

 library(raster)
 library(ncdf4)

 Temp_Jan <- brick("woa18_decav_t01_01.nc", stopIfNotEqualSpaced = FALSE, varname = "t_an", lvar=4)
 Temp_Feb <- brick("woa18_decav_t02_01.nc", stopIfNotEqualSpaced = FALSE, varname = "t_an", lvar=4)
 Temp_Mar <- brick("woa18_decav_t03_01.nc", stopIfNotEqualSpaced = FALSE, varname = "t_an", lvar=4)
 Temp_Apr <- brick("woa18_decav_t04_01.nc", stopIfNotEqualSpaced = FALSE, varname = "t_an", lvar=4)
 Temp_May <- brick("woa18_decav_t05_01.nc", stopIfNotEqualSpaced = FALSE, varname = "t_an", lvar=4)


 depth_Temp <- data.frame(Temp_Jan@z)

 Temp <- rbind(Temp_Jan, Temp_Feb, Temp_Mar, Temp_Apr, Temp_May)

Also I am importing each file individually Temp_Jan <- brick("woa18_decav_t01_01.nc", stopIfNotEqualSpaced = FALSE, varname = "t_an", lvar=4). Is it possible to import all the files using one line of code or is it safer/easier to do them individually?

1

There are 1 best solutions below

2
On

You should be able to solve this easily using tidync:

df <- tidync::tidync("woa18_decav_t01_01.nc") %>% 
  tidync::hyper_tibble()