for循环打开不同目录下的netcdf文件

For loop open netcdf files in different directories

我是 python 的新手,希望在不同的目录中打开特定的 netcdf 文件,以便我可以对它们进行平均并绘制它们(表面温度数据)

目录在 yyyy/mm/dd/filename 中。 (每天两个 nc 文件,白天和晚上)。 即我想在 2002/07/daytime 打开所有文件,然后在 2002-2018 的每个月都以此类推。 请问我该怎么做?我正在研究 for loops 和 xarray,但我无法解决它。

谢谢!

如果我没理解错的话,你应该可以使用 glob 创建所有文件的列表,然后使用 open_mfdataset 自动组合它们。

from glob import glob
import xarray as xr

list_files = glob('./*/*/*/*.nc')
#Create a list of all nc files with a path matching this format :
#whatever(year)/whatever(month)/wathever(day)/whatever(file name).nc

DS = xr.open_mfdataset(list_files)