python - 在 for 循环中剪辑 netcdf 文件时减少处理时间
问题描述
我编写了一些代码,在 for 循环中使用 shapefile 剪辑 netcdf 文件。我正在处理数月的数据,并在每个季节进行平均。下面的代码是秋季的示例。当我运行它时,大约需要 4 个小时才能得到平均值。有什么办法可以减少时间吗??
shapefile = gpd.read_file(shp)
directory = 'C:/Users/[myname]/data/noaa-goes16/ABI-L2-ACMC/2019/'
sum = np.zeros((16, 74));
count = 0;
#the range of day folders which represents the fall season
for i in range(244,305):
day_folder = directory + str(i).zfill(3) + "/"
#range of hour folders
for j in range(5,19):
hour_folder = directory + str(i).zfill(3) + "/" + str(j).zfill(2) + "/"
#files are netcdf
for filename in os.listdir(hour_folder):
#opening each file with rioxarray
rio_fn = rioxarray.open_rasterio(hour_folder+filename)
#reprojecting the data to match that of the shapefile
rio_fn_wgs = rio_fn.rio.reproject(shapefile.crs)
#clipping the data to the shapefile
clipped = rio_fn_wgs.rio.clip(shapefile.geometry.apply(mapping), shapefile.crs)
#calling the variable 'BCM' from the netcdf file
bcm_clipped = clipped['BCM']
bcm_clipped = np.clip(bcm_clipped, 0, 1)
bcm_clipped = np.array(bcm_clipped)
bcm_clipped = np.squeeze(bcm_clipped)
sum = np.add(sum, bcm_clipped)
count = count + 1
avg = np.divide(sum, count)
print('The average is, ', avg)
解决方案
推荐阅读
- unity3d - 如何统一去除按钮的高亮状态?
- openstack - 新的 Web 应用程序或现有 OpenStack Horizon 的定制
- jestjs - Jest 每个测试套件和跨测试用例初始化和共享对象一次
- python - 在python中检测音频文件是否有语音
- python - 如何重启 PyQt5 应用程序
- python - numpy.r_ 将 1 放在中间
- c# - Unity 2D 'Attack()' 是一个局部函数,因此必须始终有一个主体
- html - 如何将带有参数的函数传递给Angular中的内部组件
- python - RuntimeError: Gtk 无法初始化。如果要处理这种情况,请使用 Gtk.init_check()
- java - 如何通过在Java中的开头添加新项目来移动数组项目