Python NetCDF – How to Read Multiple netCDF4 Files Using Python 3

netcdfpython

I need to read a multiple netCDF4 files using python 3. The files are in different folder named 2019, 2018 with months and days. And the files have the same variables I use to read and plot it.

I can do that just for one file. How I can read all files and variable IR in all files in all folder?

path='/home/all/2019/03/25/Mmult_2.nc'
nc = netCDF4.Dataset('/home/all/2019/03/25/Mmult_2.nc')
IR=nc.variables['IR'][:]
plt.pcolormesh(IR)
plt.colorbar()
plt.show()

Best Answer

You can use the glob.glob module to search all the nc-files in your sub-folder like this (taken from https://stackoverflow.com/questions/14798220/how-can-i-search-sub-folders-using-glob-glob-module):

import glob

list_of_paths = glob.glob('C:/Users/sam/Desktop/myfolder/**/*.nc', recursive=True)

Then, if there is a dimension in your netcdfs that is unlimited, you can open all the files in one line like this:

import netCDF4

nc = netCDF4.MFDataset(list_of_paths)

After this you will probably want to be carefull, because doing:

IR=nc.variables['IR'][:]

will load the 'IR'-data from ALL your nc-files into your memory at once, which could crash you computer. So it's probably better to load small parts at a time by replacing [:] with, for example, [0:2].

Related Question