1

I am trying to merge multiple NetCDFs containing 4 dimensions (lat, lon, depth, time) into one NetCDF. Each contain the same variable (sea surface temperature or 'thetao') and the same depth value and coordinates. The only differing parameter is time and values of thetao. I have some overlapping time values too. I am working with monthly data of multiple years. So far I have the following code. But it is giving me NA values in my final array. Thank you so much for the help!

nc_files<- list.files(path = "C:/Users/dell/OneDrive - UGent/Thesis/NetCDFs/sst")

#empty lists
sst_all<- list()
dates_all<- c()

#looping through each file
for (i in seq_along(nc_files)) {
  #open netcdf
  nc<- open.nc(nc_files[i])
  var.get.nc(nc, "thetao") #to check variable exsists
  
  #read dimensions/variables
  lon<- var.get.nc(nc, "longitude")
  lat<- var.get.nc(nc, "latitude")
  depth<- var.get.nc(nc, "depth")
  time<- var.get.nc(nc, "time")
  time_units<- att.get.nc(nc, "time", "units")
  date <- utcal.nc(time_units, time, "c") # Transform times to POSIXct
  dates_all <- format(date, format="%Y-%m")
  
  #read sst 
  sst<- var.get.nc(nc, "thetao")
  sst_all[[i]] <- sst
  
  close.nc(nc)
}

#combining all SST arrays 
sst_combined <- abind::abind(sst_all, along=4)


print(head(dates_all))

I tried looping all the 3 NetCDFs but the final NetCDF seems to have no values. Ultimately, I want one NetCDF or one .CSV with data from all 3 NetCDFs. Thank you!

3
  • Welcome to SO, Bubbles! It can be difficult sometimes to answer questions when we aren't familiar with the OP's data to be able to reproduce the same effect. Further, while it's somewhat clear you're working on NetCDFs, it's not clear which package. Your use of open.nc suggests it is not the recent ncdf4, and its predecessor ncdf is archived and no longer available. There are other packages, but I'm not sure which. Questions here on SO do much better if they are fully reproducible, including sample data and clearly listing non-base packages, etc. Please add context. Thanks! Commented Apr 29 at 13:33
  • Can you explain why you want to merge the 3 netCDF files into 1, or (even worse) convert it to a CSV? Perhaps there is a better way to achieve what you are after. Commented Apr 29 at 20:16
  • I want to merge them because I want time series data and they can't be downloaded into one netCDF. Instead I have 3 ranging from year 1950-2020. And I want to plot so that the plots show the whole time range instead of sub ranges (i.e, 1950-2000, 2000-2015,2015-2020.) Commented Apr 30 at 7:25

1 Answer 1

1

There is a reason that time series in netCDF files are often cut up into multiple files: the files are too big to transmit over the internet and to process on a computer (1 year of monthly CMEMS 0.083deg data is 10GB per variable). So typically you select a smaller study area when ordering data and I'll assume that you have done just that.

Using raw RNetCDF to read the netCDF files is possible but tedious. With the ncdfCF package you can do this:

library(ncdfCF)

sst_all <- lapply(nc_files, function(f) {
  ds <- open_ncdf(f)

  var <- ds[["thetao"]] # Get the variable from the file
  var$raw()             # Get the actual array from the variable
  # You can also do: 
  # ds[["thetao"]]$raw()
})

sst_combined <- abind::abind(sst_all, along=4)

This also gives you all of the dimnames on the resulting array which you can then use for plotting or further analysis.

If you are really only interested in SST, you can do arr <- var$subset(depth = c(0,2))$raw() and then use abind(sst_all, along = 3) at the end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.