使用Python提取不同级别的网格化大气数据并转换为netCDF<;-已完成
使用Python查找区域的网格数据,然后在子网格(2x2)网格上平均这些数据<;-不正确
我可以让它在Octave/Matlab中工作,但我想用Python来保持这一切。问题在于,我相信,索引语法和我在索引方面无法击败Python。你知道吗
资料图:经纬度和气压水平的一维阵列。经度有49个元素,纬度有13个元素,水平面有12个元素。我试图平均的数据在第一个实例中是一个二维矩阵(13x49),在第二个实例中是一个三维矩阵(shape=12x13x49)。你知道吗
#DEFINE LARGE AREA OF GLOBLE
londim_g = 49
latdim_g = 13
lonmin_g = 60
lonmax_g = 180
latmin_g = -60
latmax_g = -30
dlat=dlon = 2.5
lats_g = arange(latmin_g,latmax_g+dlon,dlon)
lons_g = arange(lonmin_g,lonmax_g+dlat,dlat)
LON_G,LAT_G = meshgrid(lons_g,lats_g) #THE SHAPE OF THIS IS A PROBLEM!!
# DEFINE SMALLER REGION
lonmin = 120;
lonmax = 130;
latmax = -40;
latmin = -50;
N = 2; #THIS IS NxN SUB-GRID AVERAGE OF SMALLER REGION
ind = argwhere( (LON_G>=lonmin) & (LON_G<=lonmax) & (LAT_G<=latmax) & (LAT_G>=latmin) )
ri = ind[:,0];
ci = ind[:,1];
LON = LON_G[ix_(ri,ci)]
LAT = LAT_G[ix_(ri,ci)]
LON = LON[1].reshape(5,5) #THIS IS STEP IS A RESULT OF LON_G,LAT_G BEING MIS-SHAPEN
LAT = LAT[1].reshape(5,5) #THIS IS STEP IS A RESULT OF LON_G,LAT_G BEING MIS-SHAPEN
# AVERAGE on NxN sub-grids such that
#INDEX GRID
#
# Essentially we averaging each sub-grid within the domain, that is each 2x2, grid points
# IF the following is the domain:
#
# (ln1,lt1) (ln2,lt1) (ln3,lt1) (ln4,lt1) (ln5,lt1)
#
# (ln1,lt2) (ln2,lt2) (ln3,lt2) (ln4,lt2) (ln5,lt2)
#
# (ln1,lt3) (ln2,lt3) (ln3,lt3) (ln4,lt3) (ln5,lt3)
#
# (ln1,lt4) (ln2,lt4) (ln3,lt4) (ln4,lt4) (ln5,lt4)
#
# (ln1,lt5) (ln2,lt5) (ln3,lt5) (ln4,lt5) (ln5,lt5)
#
# then the first sub-grid is:
#
# (ln1,lt1) (ln2,lt1)
#
# (ln1,lt2) (ln2,lt2)
#
# the next sub-grid is:
#
# (ln2,lt1) (ln3,lt1)
#
# (ln2,lt2) (ln3,lt2)
#
# So on, and so forth. If we associate each grid point with it's data then compute the average
# value of that sub-grid then we will have an `array', in this of 16 mean values, i.e.:
#
# (ln1,lt1) (ln2,lt1) (ln3,lt1) (ln4,lt1) (ln5,lt1)
# mean1 mean2 mean3 mean4
# (ln1,lt2) (ln2,lt2) (ln3,lt2) (ln4,lt2) (ln5,lt2)
# mean5 mean6 mean7 mean8
# (ln1,lt3) (ln2,lt3) (ln3,lt3) (ln4,lt3) (ln5,lt3)
# mean9 mean10 mean11 mean12
# (ln1,lt4) (ln2,lt4) (ln3,lt4) (ln4,lt4) (ln5,lt4)
# mean13 mean14 mean15 mean16
# (ln1,lt5) (ln2,lt5) (ln3,lt5) (ln4,lt5) (ln5,lt5)
#
# We then take the mean of those means to get the mean of domain/region of each level.
# In doing the mean this way the over-lap in averaging towards the interior values provides
# more weight to those values and hence a more statistically significant mean for the
# the region.
#
TROP = trop[ix_(ri,ci)]
TROP = TROP[1].reshape(5,5) #Hmmm, I FEEL LIKE I'M REALLY NOT UNDERSTANDING PYTHON INDEXING
n,m = TROP.shape
TROP_BAR = average(split(average(split(TROP, m // N, axis=1), axis=-1), n // N, axis=1), axis=-1)
print(TROP_BAR)
OMEGA_BAR = zeros(12)
for i1 in range (0,11):
oms = om[i1]
OMS = OMS[ix_(ri,ci)]
OMS = OMS[1].reshape(5,5)
OMEGA_BAR[i1] = average(split(average(split(, m // N, axis=1), axis=-1), n // N, axis=1), axis=-1)
我得到的平均值没有意义。所以我想得到真正有意义的平均值。提前谢谢。你知道吗
虽然我不相信这是最有效的方法。我已经想出了一个解决方案,给我正确的答案,所以我将张贴在这里(下面)。然而,我很好奇是否有人有一个更有效的解决方案,而不是通过循环矩阵。再次提前感谢。你知道吗
你能检查一下这个代码吗:
不幸的是,这只适用于2x2矩阵,但它应该比使用NumPy数组和矩阵运算求平均值快得多。你知道吗
对于更普遍的方法,您可以尝试以下方法:
我试着用随机矩阵和k值2和3似乎工作。你知道吗
相关问题 更多 >
编程相关推荐