一、glob.glob()获取文件路径
返回所有匹配的文件路径列表。
参数pathname,定义了文件路径匹配规则,可以是绝对路径,也可以是相对路径。
例如以下代码就会返回所有符合该规则的文件路径:
t2m_path_list = glob.glob('G:/aaa/sat_dir/mask_daily/era5_t2m_????????.tif')
可以通过循环遍历每一个文件:
for path in tif_path_list:
print(path)
二、索引NC文件时间
1.loc关键字索引,如索引文件中u10变量和v10变量2002-12-01的数据
file = xr.open_dataset('/mnt/g/aaa/wind/2002.nc')
u = file.u10.loc['2002-12-01']
v = file.v10.loc['2002-12-01']
2.位置索引
通过查看u、v数据,发现其都是三维数据,分别为:时间维、纬度、经度。
时间维共12个,取的12月为最后一个,代码如下:
file = xr.open_dataset('/mnt/g/aaa/wind/2002.nc')
u = file.u10[-1,:,:]
v = file.v10[-1,:,:]