Vishal Singh
Vishal Singh

Reputation: 55

how to open multiple netcdf files stored in multiple folders Python

right now I am able to open multiple netcdf files from a single folder using the command given below:

dsmerged = xarray.open_mfdataset('F:/netcdf/example/*.nc')

However, I am unable to open multiple netcdf files from different folders or directories using this command. Suppose I am having multiple netcdf files stored in multiple folders so how can I open together? Suggestions are appreciated.

Upvotes: 1

Views: 3070

Answers (1)

tda
tda

Reputation: 2133

From the docs, you can either pass in a glob string (like you have) or a list of explicit filenames to open. Therefore I would do the following:

import glob

# Get a list of all .nc files available in different folders
filenames = glob.glob("/parent/directory/*/*/*.nc")

dsmerged = xarray.open_mfdataset(filenames)

This works on Python 2.7 and 3.6.

Note you may have to run this a few times and concatenate the returned lists if not all files are in the same directory structure. I.e. if some .nc files are in /path/one/here/file.nc and others are in /path/here/file.nc

Upvotes: 2

Related Questions