首页 > 解决方案 > 在 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)

标签: python

解决方案


推荐阅读