2

I have a folder that contains 61 netcdf files containing 4 dimension (lat, lon, time and depth) and 9 variables.

I would like to combine all the files in the folder to one netcdf file with the variable "thetao" so that the 4 other dimensions remain.

I do not have a lot of background in R. So far I have come to this from what I have gathered from other sources:

library(ncdf4)
library(raster)

# Set working directory
setwd("C:/Users/L1586/Downloads/CMEMS_2015/05/BAL-HBM_PHY-201505-DailyMeans/")

# Declare data frame
df = NULL

#Open all files
files = list.files("C:/Users/L1586/Downloads/CMEMS_2015/05/BAL-HBM_PHY-201505-DailyMeans/",pattern='*.nc',full.names=TRUE)

# Loop over files
for(i in seq_along(files)) {
  nc = nc_open(files[i])
}
  • are all the netcdfs on the same dimensions? will 'thetao' represent something related to the original file? – Sam Aug 4 '17 at 12:10
  • Also, is time the dimension you will combine on? And, is it already the unlimited dimension? Can you provide the results of print(nc) for the first file. I think the best answer here for R would be "get a copy of the first file with only thetao variable and with time as the unlimited dimension, and then bind each other files's thetao into that. It's a big hassle in R really, and you'd be better doing it at the command line (but that's also a hassle, and even more so on Windows). In R from scratch you'll have to learn about ncdf4 for defining or copying dimensions and attributes in a new file. – mdsumner Aug 4 '17 at 12:15
  • I think getting a ".cdf" (text description) of the file is the best way, you can hack at that to get the basic starting structure. I don't know of any prior examples using R for this though. – mdsumner Aug 4 '17 at 12:17
  • 2
    May be this is off topic, because it addresses not a R solution. Merging and concatenig NETCDF ensembles and records is often reflected topic in "modelling comunity". You could several tools to do that like: NCO - ncecat/ncrcat (nco.sourceforge.net/…), NCL (ncl.ucar.edu/Applications/addfiles.shtml) or CDO (hannahlab.org/cdo-extracting-a-variable-across-several-files) – huckfinn Aug 5 '17 at 7:32
  • Another potential off-topic solution might be using TDS/THREDDS. You could then aggregate the 61 files in-situ with NCML without creating a new file, and then use the aggregated dataset through a DAP-aware netcdf tools. I use this for large multi-file datasets. – Dave X Oct 3 '17 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.