2

My task is to get multiple similar NetCDF (.nc) files from a folder and stack one a variable out of 10 variables.

I used:

a <- list.files(path=ncpath, pattern = "nc$", full.names = TRUE)

This gets me all the files with .nc extenstion.

How to proceed for the second task?

I want this variable a from these number of files in a folder and stack them.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Dipu
  • 123
  • 2
  • 14
  • Your second task is a bit unclear with what you want, mainly because I dont know what you mean by "stack". It sounds like you are making 10 assignments or that the variable "a" is length 10 and you want to retrieve a single element from it. – Justin Landis Dec 19 '19 at 15:36
  • Each NetCDF file has 10 variables. Out of these 10 variables I just one variable, say 'a'. My task is to extract this variable from multiple files so that I have just one file at the end with variable 'a". "Stack, merge, concatenate, append"- make one single raster file by combining multiple raster layers, each containing just one variable "a". – Dipu Dec 19 '19 at 18:28
  • do those files have the same coordinates? In that case, you might open them with open_mfdataset and save only the variable 'a' – Matteo De Felice Dec 21 '19 at 10:52
  • Yes, they have coordinates...Thanks for your help Justin and Matleo...i managed to extract... – Dipu Dec 21 '19 at 16:56

1 Answers1

0

If you just want the output in a netcdf file, you might consider doing this task from the command line in linux and using cdo?

files=$(ls *.nc)   # you might want to be more selective with your wildcard
# this loop selects the variable from each file and puts into file with the var name at the end
for file in $files ; do 
   cdo selvar,varname $file ${file%???}_varname.nc 
done 
# now merge into one file:
cdo merge *_varname.nc merged_file.nc  

where you need to obviously replace varname with the name of your variable of choice.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • thanks..cdo does it very well but i am unable to install cdo in cygwin and WLS windows 10 (ubuntu)...thats why trying in R – Dipu Dec 24 '19 at 09:56
  • Installing a full linux subsystem in windows 10 is now super straightforward, type "install linux in windows 10" into google, for example the first link is here: https://www.windowscentral.com/how-install-linux-distros-windows-10 then just follow the steps. Once you have linux installed, just open a window, and type sudo apt-get install cdo, just as you would do on a linux only platform. Then you can use this script. This is definitely the way to go, cygwin is essentially obsolete on w10... – ClimateUnboxed Dec 25 '19 at 10:30