我的目录中有126个文件,每个文件都包含一个2D数组,表示每个纬度/经度点的光学深度值。我想创建一个数组,该数组具有每个纬度/经度点的所有文件的平均值。
import numpy as np
from netCDF4 import Dataset
import os
#read all files
sum1=0
folder="/home/User/MARCI_WaterIce/"
for file in os.listdir("/home/User/MARCI_WaterIce"):
file=folder+file
cldstr = file[46:57]
nc = Dataset(file)
#Defining variables
waterice_tau1 = nc.variables['waterice_tau'][:,:]
waterice_flag1=nc.variables['waterice_flag'][:,:]
# replace all missing data with a 0
waterice_tau1=np.where(waterice_tau1==-1,0,waterice_tau1)
waterice_tau1=np.where(waterice_flag1>0,0,waterice_tau1)
if sum1==0:
#create a 2D array that sums all water ice optical depth values for each lat and lon point
Array1 = waterice_tau1
#create another 2D that counts the number non zero elements
Array2 = np.full_like(waterice_tau1, None)
else:
Array1= Array1 + waterice_tau1
Array2 =np.append(Array2, np.count_nonzero(waterice_tau1))
sum1=sum1+1
MyMean = Array1/Array2
这是我尝试过的。我期望Array1和Array2的长度相等为90,但是我得到的长度更大而又不相等。我认为我应该使用索引,但不确定如何执行。